]> git.sur5r.net Git - cc65/commitdiff
Added an option to output the assembly after each transformation step of the
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 12 Jul 2012 20:30:01 +0000 (20:30 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 12 Jul 2012 20:30:01 +0000 (20:30 +0000)
optimizer.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5781 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/cc65/codeopt.c
src/cc65/global.c
src/cc65/global.h
src/cc65/main.c
src/cc65/output.c
src/cc65/output.h

index 34900b7656becd07da270400735147d284a3afe5..b5f399c4754414b7a99f4695f8bb6c0eacfd77b4 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 /* common */
 #include "abend.h"
@@ -42,6 +43,7 @@
 #include "cpu.h"
 #include "debugflag.h"
 #include "print.h"
+#include "strbuf.h"
 #include "xmalloc.h"
 #include "xsprintf.h"
 
@@ -49,6 +51,7 @@
 #include "asmlabel.h"
 #include "codeent.h"
 #include "codeinfo.h"
+#include "codeopt.h"
 #include "coptadd.h"
 #include "coptc02.h"
 #include "coptcmp.h"
@@ -65,7 +68,7 @@
 #include "copttest.h"
 #include "error.h"
 #include "global.h"
-#include "codeopt.h"
+#include "output.h"
 
 
 
@@ -1019,6 +1022,48 @@ static void WriteOptStats (const char* Name)
 
 
 
+static void OpenDebugFile (const CodeSeg* S)
+/* Open the debug file for the given segment if the flag is on */
+{
+    if (DebugOptOutput) {
+        StrBuf Name = AUTO_STRBUF_INITIALIZER;
+        if (S->Func) {
+            SB_CopyStr (&Name, S->Func->Name);
+        } else {
+            SB_CopyStr (&Name, "global");
+        }
+        SB_AppendStr (&Name, ".opt");
+        SB_Terminate (&Name);
+        OpenDebugOutputFile (SB_GetConstBuf (&Name));
+        SB_Done (&Name);
+    }
+}
+
+
+
+static void WriteDebugOutput (CodeSeg* S, const char* Step)
+/* Write a separator line into the debug file if the flag is on */
+{
+    if (DebugOptOutput) {
+        /* Output a separator */
+        WriteOutput ("=========================================================================\n");
+
+        /* Output a header line */
+        if (Step == 0) {
+            /* Initial output */
+            WriteOutput ("Initial code for function `%s':\n",
+                         S->Func? S->Func->Name : "<global>");
+        } else {
+            WriteOutput ("Code after applying `%s':\n", Step);
+        }
+
+        /* Output the code segment */
+        CS_Output (S);
+    }
+}
+
+
+
 static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
 /* Run one optimizer function Max times or until there are no more changes */
 {
@@ -1037,9 +1082,6 @@ static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
 
        /* Run the function */
        C = F->Func (S);
-        if (Debug && C > 0) {
-            printf ("Applied %s: %u changes\n", F->Name, C);
-        }
        Changes += C;
 
        /* Do statistics */
@@ -1048,8 +1090,12 @@ static unsigned RunOptFunc (CodeSeg* S, OptFunc* F, unsigned Max)
        F->TotalChanges += C;
        F->LastChanges  += C;
 
-        /* If we had changes, regenerate register info */
+        /* If we had changes, output stuff and regenerate register info */
         if (C) {
+            if (Debug) {
+                printf ("Applied %s: %u changes\n", F->Name, C);
+            }
+            WriteDebugOutput (S, F->Name);
             CS_GenRegInfo (S);
         }
 
@@ -1352,6 +1398,10 @@ void RunOpt (CodeSeg* S)
        Print (stdout, 1, "Running optimizer for global code segment\n");
     }
 
+    /* If requested, open an output file */
+    OpenDebugFile (S);
+    WriteDebugOutput (S, 0);
+
     /* Generate register info for all instructions */
     CS_GenRegInfo (S);
 
@@ -1367,9 +1417,14 @@ void RunOpt (CodeSeg* S)
     /* Free register info */
     CS_FreeRegInfo (S);
 
+    /* Close output file if necessary */
+    if (DebugOptOutput) {
+        CloseOutputFile ();
+    }
+
     /* Write statistics */
     if (StatFileName) {
-       WriteOptStats (StatFileName);
+       WriteOptStats (StatFileName);
     }
 }
 
index f644e51d4f297ca9c9534afdac1653e43c6a7436..4884283f393298a2378de82e26a5ddc6f2b7d91d 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/* (C) 1998-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -46,6 +46,7 @@
 unsigned char AddSource                = 0;    /* Add source lines as comments */
 unsigned char DebugInfo                = 0;    /* Add debug info to the obj */
 unsigned char PreprocessOnly    = 0;    /* Just preprocess the input */
+unsigned char DebugOptOutput    = 0;    /* Output debug stuff */
 unsigned      RegisterSpace     = 6;    /* Space available for register vars */
 
 /* Stackable options */
@@ -61,7 +62,7 @@ IntStack CheckStack         = INTSTACK(0);  /* Generate stack overflow checks */
 IntStack Optimize                  = INTSTACK(0);  /* Optimize flag */
 IntStack CodeSizeFactor            = INTSTACK(100);/* Size factor for generated code */
 IntStack DataAlignment      = INTSTACK(1);  /* Alignment for data */
-                
+
 /* File names */
 StrBuf DepName     = STATIC_STRBUF_INITIALIZER; /* Name of dependencies file */
 StrBuf FullDepName = STATIC_STRBUF_INITIALIZER; /* Name of full dependencies file */
index 0fda8eb056a94c2309cc07cd403d1c121568aa30..dc624b0da29662d5fc32c719a1fdbe319ad6f919 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/* (C) 1998-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -54,6 +54,7 @@
 extern unsigned char   AddSource;              /* Add source lines as comments */
 extern unsigned char   DebugInfo;              /* Add debug info to the obj */
 extern unsigned char    PreprocessOnly;         /* Just preprocess the input */
+extern unsigned char    DebugOptOutput;         /* Output debug stuff */
 extern unsigned         RegisterSpace;          /* Space available for register vars */
 
 /* Stackable options */
index 4165bc8575e6d93a5b55d55c7a3f8048e6bf2c50..03f06fff59f8b3d60d91ff6615bcdc3bf6cd8a67 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2011, Ullrich von Bassewitz                                      */
+/* (C) 2000-2012, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -505,6 +505,15 @@ static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
 
 
 
+static void OptDebugOptOutput (const char* Opt attribute ((unused)), 
+                               const char* Arg attribute ((unused)))
+/* Output optimization steps */
+{
+    DebugOptOutput = 1;
+}
+
+
+
 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
 /* Handle the --dep-target option */
 {
@@ -782,6 +791,7 @@ int main (int argc, char* argv[])
                { "--debug",            0,      OptDebug                },
        { "--debug-info",       0,      OptDebugInfo            },
         { "--debug-opt",        1,      OptDebugOpt             },
+        { "--debug-opt-output", 0,      OptDebugOptOutput       },
         { "--dep-target",       1,      OptDepTarget            },
        { "--disable-opt",      1,      OptDisableOpt           },
        { "--enable-opt",       1,      OptEnableOpt            },
index 1c40e04a2471c71577c4d63768a6140912390b2f..5db77d0c42045d5f90bc7d9f81ce55652c702f10 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2009,     Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2009-2012, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -109,6 +109,24 @@ void OpenOutputFile ()
 
 
 
+void OpenDebugOutputFile (const char* Name)
+/* Open an output file for debugging purposes. Will call Fatal() in case of
+ * failures.
+ */
+{
+    /* Output file must not be open and we must have a name*/
+    PRECONDITION (OutputFile == 0);
+
+    /* Open the file */
+    OutputFile = fopen (Name, "w");
+    if (OutputFile == 0) {
+        Fatal ("Cannot open debug output file `%s': %s", Name, strerror (errno));
+    }
+    Print (stdout, 1, "Opened debug output file `%s'\n", Name);
+}
+
+
+
 void CloseOutputFile ()
 /* Close the output file. Will call Fatal() in case of failures. */
 {
@@ -121,6 +139,8 @@ void CloseOutputFile ()
         Fatal ("Cannot write to output file (disk full?)");
     }
     Print (stdout, 1, "Closed output file `%s'\n", OutputFilename);
+
+    OutputFile = 0;
 }
 
 
index cdcd42c4df4ae1d450a8dea324ea024a21637f6b..485e20a463247cad00dbbf449e94d357372ccd6e 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2009,     Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2009-2012, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -46,7 +46,7 @@
 
 
 /*****************************************************************************/
-/*                                          Data                                    */
+/*                                          Data                                    */
 /*****************************************************************************/
 
 
@@ -60,7 +60,7 @@ extern FILE* OutputFile;
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
@@ -76,6 +76,11 @@ void MakeDefaultOutputName (const char* InputFilename);
 void OpenOutputFile ();
 /* Open the output file. Will call Fatal() in case of failures. */
 
+void OpenDebugOutputFile (const char* Name);
+/* Open an output file for debugging purposes. Will call Fatal() in case of 
+ * failures. 
+ */
+
 void CloseOutputFile ();
 /* Close the output file. Will call Fatal() in case of failures. */