static unsigned OptShift4 (CodeSeg* S)
-/* A call to the shraxN routine may get replaced by one or more lsr insns
- * if the value of X is zero.
+/* Calls to the asraxN or shraxN routines may get replaced by one or more lsr
+ * insns if the value of X is zero.
*/
{
unsigned Changes = 0;
CodeEntry* E = CS_GetEntry (S, I);
/* Check for the sequence */
- if (E->OPC == OP65_JSR &&
- strncmp (E->Arg, "shrax", 5) == 0 &&
- strlen (E->Arg) == 6 &&
- IsDigit (E->Arg[5]) &&
+ if (E->OPC == OP65_JSR &&
+ (strncmp (E->Arg, "shrax", 5) == 0 ||
+ strncmp (E->Arg, "asrax", 5) == 0) &&
+ strlen (E->Arg) == 6 &&
+ IsDigit (E->Arg[5]) &&
E->RI->In.RegX == 0) {
/* Insert shift insns */
static OptFunc DOptAdd4 = { OptAdd4, "OptAdd4", 90, 0, 0, 0, 0, 0 };
static OptFunc DOptAdd5 = { OptAdd5, "OptAdd5", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptAdd6 = { OptAdd6, "OptAdd6", 40, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegA1 = { OptBNegA1, "OptBNegA1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegA2 = { OptBNegA2, "OptBNegA2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegAX1 = { OptBNegAX1, "OptBNegAX1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegAX2 = { OptBNegAX2, "OptBNegAX2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegAX3 = { OptBNegAX3, "OptBNegAX3", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBNegAX4 = { OptBNegAX4, "OptBNegAX4", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 0, 0, 0, 0, 0, 0 };
static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 42, 0, 0, 0, 0, 0 };
static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 165, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 200, 0, 0, 0, 0, 0 };
static OptFunc DOptRTS = { OptRTS, "OptRTS", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptRTSJumps1 = { OptRTSJumps1, "OptRTSJumps1", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptRTSJumps2 = { OptRTSJumps2, "OptRTSJumps2", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegA1 = { OptBNegA1, "OptBNegA1", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegA2 = { OptBNegA2, "OptBNegA2", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegAX1 = { OptBNegAX1, "OptBNegAX1", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegAX2 = { OptBNegAX2, "OptBNegAX2", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegAX3 = { OptBNegAX3, "OptBNegAX3", 100, 0, 0, 0, 0, 0 };
-static OptFunc DOptBNegAX4 = { OptBNegAX4, "OptBNegAX4", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 };
&DOptAdd4,
&DOptAdd5,
&DOptAdd6,
+ &DOptBNegA1,
+ &DOptBNegA2,
+ &DOptBNegAX1,
+ &DOptBNegAX2,
+ &DOptBNegAX3,
+ &DOptBNegAX4,
&DOptBoolTrans,
&DOptBranchDist,
&DOptCmp1,
&DOptJumpTarget3,
&DOptLoad1,
&DOptLoad2,
- &DOptBNegA1,
- &DOptBNegA2,
- &DOptBNegAX1,
- &DOptBNegAX2,
- &DOptBNegAX3,
- &DOptBNegAX4,
+ &DOptNegAX1,
+ &DOptNegAX2,
&DOptPrecalc,
&DOptPtrLoad1,
&DOptPtrLoad11,
C += RunOptFunc (S, &DOptBNegA1, 1);
C += RunOptFunc (S, &DOptBNegA2, 1);
+ C += RunOptFunc (S, &DOptNegAX1, 1);
+ C += RunOptFunc (S, &DOptNegAX2, 1);
C += RunOptFunc (S, &DOptStackOps, 1);
C += RunOptFunc (S, &DOptSub1, 1);
C += RunOptFunc (S, &DOptSub2, 1);
+/*****************************************************************************/
+/* negax optimizations */
+/*****************************************************************************/
+
+
+
+unsigned OptNegAX1 (CodeSeg* S)
+/* Search for a call to negax and replace it by
+ *
+ * eor #$FF
+ * clc
+ * adc #$01
+ *
+ * if X isn't used later.
+ */
+{
+ unsigned Changes = 0;
+ unsigned I;
+
+ /* Generate register info for this step */
+ CS_GenRegInfo (S);
+
+ /* Walk over the entries */
+ I = 0;
+ while (I < CS_GetEntryCount (S)) {
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if this is a call to negax, and if X isn't used later */
+ if (CE_IsCallTo (E, "negax") && !RegXUsed (S, I+1)) {
+
+ CodeEntry* X;
+
+ /* Add replacement code behind */
+ X = NewCodeEntry (OP65_EOR, AM65_IMM, "$FF", 0, E->LI);
+ CS_InsertEntry (S, X, I+1);
+
+ X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
+ CS_InsertEntry (S, X, I+2);
+
+ X = NewCodeEntry (OP65_ADC, AM65_IMM, "$01", 0, E->LI);
+ CS_InsertEntry (S, X, I+3);
+
+ /* Delete the call to negax */
+ CS_DelEntry (S, I);
+
+ /* Skip the generated code */
+ I += 2;
+
+ /* We had changes */
+ ++Changes;
+ }
+
+ /* Next entry */
+ ++I;
+
+ }
+
+ /* Free register info */
+ CS_FreeRegInfo (S);
+
+ /* Return the number of changes made */
+ return Changes;
+}
+
+
+
+unsigned OptNegAX2 (CodeSeg* S)
+/* Search for a call to negax and replace it by
+ *
+ * ldx #$FF
+ * eor #$FF
+ * clc
+ * adc #$01
+ * bne L1
+ * inx
+ * L1:
+ *
+ * if X is known and zero on entry.
+ */
+{
+ unsigned Changes = 0;
+ unsigned I;
+
+ /* Generate register info for this step */
+ CS_GenRegInfo (S);
+
+ /* Walk over the entries */
+ I = 0;
+ while (I < CS_GetEntryCount (S)) {
+
+ CodeEntry* P;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if this is a call to negax, and if X is known and zero */
+ if (E->RI->In.RegX == 0 &&
+ CE_IsCallTo (E, "negax") &&
+ (P = CS_GetNextEntry (S, I)) != 0) {
+
+ CodeEntry* X;
+ CodeLabel* L;
+
+ /* Add replacement code behind */
+
+ /* ldx #$FF */
+ X = NewCodeEntry (OP65_LDX, AM65_IMM, "$FF", 0, E->LI);
+ CS_InsertEntry (S, X, I+1);
+
+ /* eor #$FF */
+ X = NewCodeEntry (OP65_EOR, AM65_IMM, "$FF", 0, E->LI);
+ CS_InsertEntry (S, X, I+2);
+
+ /* clc */
+ X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
+ CS_InsertEntry (S, X, I+3);
+
+ /* adc #$01 */
+ X = NewCodeEntry (OP65_ADC, AM65_IMM, "$01", 0, E->LI);
+ CS_InsertEntry (S, X, I+4);
+
+ /* Get the label attached to the insn following the call */
+ L = CS_GenLabel (S, P);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, E->LI);
+ CS_InsertEntry (S, X, I+5);
+
+ /* inx */
+ X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, E->LI);
+ CS_InsertEntry (S, X, I+6);
+
+ /* Delete the call to negax */
+ CS_DelEntry (S, I);
+
+ /* Skip the generated code */
+ I += 5;
+
+ /* We had changes */
+ ++Changes;
+ }
+
+ /* Next entry */
+ ++I;
+
+ }
+
+ /* Free register info */
+ CS_FreeRegInfo (S);
+
+ /* Return the number of changes made */
+ return Changes;
+}
+
+
+