+typedef struct OptFunc OptFunc;
+struct OptFunc {
+ unsigned (*Func) (CodeSeg*); /* Optimizer function */
+ const char* Name; /* Name of the function/group */
+ unsigned CodeSizeFactor; /* Code size factor for this opt func */
+ unsigned long TotalRuns; /* Total number of runs */
+ unsigned long LastRuns; /* Last number of runs */
+ unsigned long TotalChanges; /* Total number of changes */
+ unsigned long LastChanges; /* Last number of changes */
+ char Disabled; /* True if function disabled */
+};
+
+
+
+/*****************************************************************************/
+/* Code */
+/*****************************************************************************/
+
+
+
+/* Macro that builds a function description */
+#define OptFuncEntry(func) static OptFuncDesc D##func = { func, #func, 0 }
+
+/* A list of all the function descriptions */
+static OptFunc DOpt65C02Ind = { Opt65C02Ind, "Opt65C02Ind", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptAdd1 = { OptAdd1, "OptAdd1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptAdd2 = { OptAdd2, "OptAdd2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptAdd3 = { OptAdd3, "OptAdd3", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBoolTrans = { OptBoolTrans, "OptBoolTrans", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptBranchDist = { OptBranchDist, "OptBranchDist", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp1 = { OptCmp1, "OptCmp1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp2 = { OptCmp2, "OptCmp2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp3 = { OptCmp3, "OptCmp3", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp4 = { OptCmp4, "OptCmp4", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp5 = { OptCmp5, "OptCmp5", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp6 = { OptCmp6, "OptCmp6", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCmp7 = { OptCmp7, "OptCmp7", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptCondBranches = { OptCondBranches, "OptCondBranches", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptDeadCode = { OptDeadCode, "OptDeadCode", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptDeadJumps = { OptDeadJumps, "OptDeadJumps", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptDecouple = { OptDecouple, "OptDecouple", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptDupLoads = { OptDupLoads, "OptDupLoads", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptJumpCascades = { OptJumpCascades, "OptJumpCascades", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptJumpTarget = { OptJumpTarget, "OptJumpTarget", 100, 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 DOptNegA1 = { OptNegA1, "OptNegA1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegA2 = { OptNegA2, "OptNegA2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX3 = { OptNegAX3, "OptNegAX3", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptNegAX4 = { OptNegAX4, "OptNegAX4", 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 };
+static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptShift1 = { OptShift1, "OptShift1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptShift2 = { OptShift2, "OptShift2", 100, 0, 0, 0, 0, 0 };
+/*static OptFunc DOptSize1 = { OptSize1, "OptSize1", 100, 0, 0, 0, 0, 0 };*/
+static OptFunc DOptSize2 = { OptSize2, "OptSize2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptStackOps = { OptStackOps, "OptStackOps", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptStoreLoad = { OptStoreLoad, "OptStoreLoad", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptSub1 = { OptSub1, "OptSub1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptSub2 = { OptSub2, "OptSub2", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptTest1 = { OptTest1, "OptTest1", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptTransfers = { OptTransfers, "OptTransfers", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptUnusedLoads = { OptUnusedLoads, "OptUnusedLoads", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptUnusedStores = { OptUnusedStores, "OptUnusedStores", 100, 0, 0, 0, 0, 0 };
+
+
+/* Table containing all the steps in alphabetical order */
+static OptFunc* OptFuncs[] = {
+ &DOpt65C02Ind,
+ &DOptAdd1,
+ &DOptAdd2,
+ &DOptAdd3,
+ &DOptBoolTrans,
+ &DOptBranchDist,
+ &DOptCmp1,
+ &DOptCmp2,
+ &DOptCmp3,
+ &DOptCmp4,
+ &DOptCmp5,
+ &DOptCmp6,
+ &DOptCmp7,
+ &DOptCondBranches,
+ &DOptDeadCode,
+ &DOptDeadJumps,
+ &DOptDecouple,
+ &DOptDupLoads,
+ &DOptJumpCascades,
+ &DOptJumpTarget,
+ &DOptNegA1,
+ &DOptNegA2,
+ &DOptNegAX1,
+ &DOptNegAX2,
+ &DOptNegAX3,
+ &DOptNegAX4,
+ &DOptPtrLoad1,
+ &DOptPtrLoad2,
+ &DOptPtrLoad3,
+ &DOptPtrLoad4,
+ &DOptPtrLoad5,
+ &DOptPtrLoad6,
+ &DOptPtrStore1,
+ &DOptPtrStore2,
+ &DOptRTS,
+ &DOptRTSJumps1,
+ &DOptRTSJumps2,
+ &DOptShift1,
+ &DOptShift2,
+ /*&DOptSize1,*/
+ &DOptSize2,
+ &DOptStackOps,
+ &DOptStoreLoad,
+ &DOptSub1,
+ &DOptSub2,
+ &DOptTest1,
+ &DOptTransfers,
+ &DOptUnusedLoads,
+ &DOptUnusedStores,
+};
+#define OPTFUNC_COUNT (sizeof(OptFuncs) / sizeof(OptFuncs[0]))
+
+
+
+static int CmpOptStep (const void* Key, const void* Func)
+/* Compare function for bsearch */
+{
+ return strcmp (Key, (*(const OptFunc**)Func)->Name);
+}
+
+
+
+static OptFunc* FindOptFunc (const char* Name)
+/* Find an optimizer step by name in the table and return a pointer. Return
+ * NULL if no such step is found.
+ */
+{
+ /* Search for the function in the list */
+ OptFunc** O = bsearch (Name, OptFuncs, OPTFUNC_COUNT, sizeof (OptFuncs[0]), CmpOptStep);
+ return O? *O : 0;
+}
+
+
+
+static OptFunc* GetOptFunc (const char* Name)
+/* Find an optimizer step by name in the table and return a pointer. Print an
+ * error and call AbEnd if not found.
+ */
+{
+ /* Search for the function in the list */
+ OptFunc* F = FindOptFunc (Name);
+ if (F == 0) {
+ /* Not found */
+ AbEnd ("Optimization step `%s' not found", Name);
+ }
+ return F;
+}
+
+
+
+void DisableOpt (const char* Name)
+/* Disable the optimization with the given name */
+{
+ if (strcmp (Name, "any") == 0) {
+ unsigned I;
+ for (I = 0; I < OPTFUNC_COUNT; ++I) {
+ OptFuncs[I]->Disabled = 1;
+ }
+ } else {
+ GetOptFunc(Name)->Disabled = 1;
+ }
+}
+
+
+
+void EnableOpt (const char* Name)
+/* Enable the optimization with the given name */
+{
+ if (strcmp (Name, "any") == 0) {
+ unsigned I;
+ for (I = 0; I < OPTFUNC_COUNT; ++I) {
+ OptFuncs[I]->Disabled = 0;
+ }
+ } else {
+ GetOptFunc(Name)->Disabled = 0;
+ }
+}
+
+
+
+void ListOptSteps (FILE* F)
+/* List all optimization steps */
+{
+ unsigned I;
+ for (I = 0; I < OPTFUNC_COUNT; ++I) {
+ fprintf (F, "%s\n", OptFuncs[I]->Name);
+ }
+}
+
+
+
+static void ReadOptStats (const char* Name)
+/* Read the optimizer statistics file */
+{
+ char Buf [256];
+ unsigned Lines;
+
+ /* Try to open the file */
+ FILE* F = fopen (Name, "r");
+ if (F == 0) {
+ /* Ignore the error */
+ return;
+ }
+
+ /* Read and parse the lines */
+ Lines = 0;
+ while (fgets (Buf, sizeof (Buf), F) != 0) {
+
+ char* B;
+ unsigned Len;
+ OptFunc* Func;
+
+ /* Fields */
+ char Name[32];
+ unsigned long TotalRuns;
+ unsigned long TotalChanges;
+
+ /* Count lines */
+ ++Lines;
+
+ /* Remove trailing white space including the line terminator */
+ B = Buf;
+ Len = strlen (B);
+ while (Len > 0 && IsSpace (B[Len-1])) {
+ --Len;
+ }
+ B[Len] = '\0';
+
+ /* Remove leading whitespace */
+ while (IsSpace (*B)) {
+ ++B;
+ }
+
+ /* Check for empty and comment lines */
+ if (*B == '\0' || *B == ';' || *B == '#') {
+ continue;
+ }
+
+ /* Parse the line */
+ if (sscanf (B, "%31s %lu %*u %lu %*u", Name, &TotalRuns, &TotalChanges) != 3) {
+ /* Syntax error */
+ continue;
+ }
+
+ /* Search for the optimizer step. */
+ Func = FindOptFunc (Name);
+ if (Func == 0) {
+ /* Not found */
+ continue;
+ }
+
+ /* Found the step, set the fields */
+ Func->TotalRuns = TotalRuns;
+ Func->TotalChanges = TotalChanges;
+
+ }
+
+ /* Close the file, ignore errors here. */
+ fclose (F);
+}
+
+
+
+static void WriteOptStats (const char* Name)
+/* Write the optimizer statistics file */
+{
+ unsigned I;
+
+ /* Try to open the file */
+ FILE* F = fopen (Name, "w");
+ if (F == 0) {
+ /* Ignore the error */
+ return;
+ }
+
+ /* Write the file */
+ for (I = 0; I < OPTFUNC_COUNT; ++I) {
+ const OptFunc* O = OptFuncs[I];
+ fprintf (F,
+ "%-20s %6lu %6lu %6lu %6lu\n",
+ O->Name,
+ O->TotalRuns,
+ O->LastRuns,
+ O->TotalChanges,
+ O->LastChanges);
+ }
+
+ /* Close the file, ignore errors here. */
+ fclose (F);
+}
+
+
+
+static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
+/* Run one optimizer function Max times or until there are no more changes */
+{
+ unsigned Changes, C;
+
+ /* Don't run the function if it is disabled or if it is prohibited by the
+ * code size factor
+ */
+ if (F->Disabled || CodeSizeFactor < F->CodeSizeFactor) {
+ return 0;
+ }
+
+ /* Run this until there are no more changes */
+ Changes = 0;
+ do {
+
+ /* Run the function */
+ C = F->Func (S);
+ Changes += C;
+
+ /* Do statistics */
+ ++F->TotalRuns;
+ ++F->LastRuns;
+ F->TotalChanges += C;
+ F->LastChanges += C;
+
+ } while (--Max && C > 0);
+
+ /* Return the number of changes */
+ return Changes;
+}
+
+
+
+static void RunOptGroup1 (CodeSeg* S)
+/* Run the first group of optimization steps. These steps translate known
+ * patterns emitted by the code generator into more optimal patterns. Order
+ * of the steps is important, because some of the steps done earlier cover
+ * the same patterns as later steps as subpatterns.
+ */
+{
+ RunOptFunc (S, &DOptPtrStore1, 1);
+ RunOptFunc (S, &DOptPtrStore2, 1);
+ RunOptFunc (S, &DOptPtrLoad1, 1);
+ RunOptFunc (S, &DOptPtrLoad2, 1);
+ RunOptFunc (S, &DOptPtrLoad3, 1);
+ RunOptFunc (S, &DOptPtrLoad4, 1);
+ RunOptFunc (S, &DOptPtrLoad5, 1);
+ RunOptFunc (S, &DOptNegAX1, 1);
+ RunOptFunc (S, &DOptNegAX2, 1);
+ RunOptFunc (S, &DOptNegAX3, 1);
+ RunOptFunc (S, &DOptNegAX4, 1);
+ RunOptFunc (S, &DOptAdd1, 1);
+ RunOptFunc (S, &DOptAdd2, 1);
+ RunOptFunc (S, &DOptShift1, 1);
+ RunOptFunc (S, &DOptShift2, 1);
+}
+
+
+
+static void RunOptGroup2 (CodeSeg* S)
+/* Run one group of optimization steps. This step involves just decoupling
+ * instructions by replacing them by instructions that do not depend on
+ * previous instructions. This makes it easier to find instructions that
+ * aren't used.
+ */
+{
+ RunOptFunc (S, &DOptDecouple, 1);
+}
+
+
+
+
+static void RunOptGroup3 (CodeSeg* S)
+/* Run one group of optimization steps. These steps depend on each other,
+ * that means that one step may allow another step to do additional work,
+ * so we will repeat the steps as long as we see any changes.
+ */
+{
+ unsigned Changes;
+
+ do {
+ Changes = 0;
+
+ Changes += RunOptFunc (S, &DOptPtrLoad6, 1);
+ Changes += RunOptFunc (S, &DOptNegA1, 1);
+ Changes += RunOptFunc (S, &DOptNegA2, 1);
+ Changes += RunOptFunc (S, &DOptSub1, 1);
+ Changes += RunOptFunc (S, &DOptSub2, 1);
+ Changes += RunOptFunc (S, &DOptAdd3, 1);
+ Changes += RunOptFunc (S, &DOptStackOps, 1);
+ Changes += RunOptFunc (S, &DOptJumpCascades, 1);
+ Changes += RunOptFunc (S, &DOptDeadJumps, 1);
+ Changes += RunOptFunc (S, &DOptRTS, 1);
+ Changes += RunOptFunc (S, &DOptDeadCode, 1);
+ Changes += RunOptFunc (S, &DOptJumpTarget, 1);
+ Changes += RunOptFunc (S, &DOptCondBranches, 1);
+ Changes += RunOptFunc (S, &DOptRTSJumps1, 1);
+ Changes += RunOptFunc (S, &DOptBoolTrans, 1);
+ Changes += RunOptFunc (S, &DOptCmp1, 1);
+ Changes += RunOptFunc (S, &DOptCmp2, 1);
+ Changes += RunOptFunc (S, &DOptCmp3, 1);
+ Changes += RunOptFunc (S, &DOptCmp4, 1);
+ Changes += RunOptFunc (S, &DOptCmp5, 1);
+ Changes += RunOptFunc (S, &DOptCmp6, 1);
+ Changes += RunOptFunc (S, &DOptCmp7, 1);
+ Changes += RunOptFunc (S, &DOptTest1, 1);
+ Changes += RunOptFunc (S, &DOptUnusedLoads, 1);
+ Changes += RunOptFunc (S, &DOptUnusedStores, 1);
+ Changes += RunOptFunc (S, &DOptDupLoads, 1);
+ Changes += RunOptFunc (S, &DOptStoreLoad, 1);
+ Changes += RunOptFunc (S, &DOptTransfers, 1);
+
+ } while (Changes);
+}
+
+
+
+static void RunOptGroup4 (CodeSeg* S)
+/* 65C02 specific optimizations. */
+{
+ if (CPU < CPU_65C02) {
+ return;
+ }
+
+ /* Replace (zp),y by (zp) if Y is zero. If we have changes, run register
+ * load optimization again, since loads of Y may have become unnecessary.
+ */
+ if (RunOptFunc (S, &DOpt65C02Ind, 1) > 0) {
+ RunOptFunc (S, &DOptUnusedLoads, 1);
+ }
+}
+
+
+
+static void RunOptGroup5 (CodeSeg* S)
+/* The last group of optimization steps. Adjust branches, do size optimizations.
+ */
+{
+ /* Optimize for size, that is replace operations by shorter ones, even
+ * if this does hinder further optimizations (no problem since we're
+ * done soon).
+ */
+ RunOptFunc (S, &DOptSize2, 1);
+
+ /* Run the jump target optimization again, since the size optimization
+ * above may have opened new oportunities.
+ */
+ RunOptFunc (S, &DOptJumpTarget, 5);
+
+ /* Adjust branch distances */
+ RunOptFunc (S, &DOptBranchDist, 3);
+
+ /* Replace conditional branches to RTS. If we had changes, we must run dead
+ * code elimination again, since the change may have introduced dead code.
+ */
+ if (RunOptFunc (S, &DOptRTSJumps2, 1)) {
+ RunOptFunc (S, &DOptDeadCode, 1);
+ }
+}
+
+
+