diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index 34900b765..b5f399c47 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -35,6 +35,7 @@ #include #include +#include /* 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 : ""); + } 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); } } diff --git a/src/cc65/global.c b/src/cc65/global.c index f644e51d4..4884283f3 100644 --- a/src/cc65/global.c +++ b/src/cc65/global.c @@ -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 */ diff --git a/src/cc65/global.h b/src/cc65/global.h index 0fda8eb05..dc624b0da 100644 --- a/src/cc65/global.h +++ b/src/cc65/global.h @@ -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 */ diff --git a/src/cc65/main.c b/src/cc65/main.c index 4165bc857..03f06fff5 100644 --- a/src/cc65/main.c +++ b/src/cc65/main.c @@ -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 }, diff --git a/src/cc65/output.c b/src/cc65/output.c index 1c40e04a2..5db77d0c4 100644 --- a/src/cc65/output.c +++ b/src/cc65/output.c @@ -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; } diff --git a/src/cc65/output.h b/src/cc65/output.h index cdcd42c4d..485e20a46 100644 --- a/src/cc65/output.h +++ b/src/cc65/output.h @@ -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. */