From 2bd30afdeb8eeac44ce46763f711a3ddb2f21e2a Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Fri, 10 Sep 2021 21:09:22 +0200 Subject: [PATCH] Add --debug-tables option and output struct and union fields --- src/cc65/global.c | 7 +++-- src/cc65/global.h | 1 + src/cc65/main.c | 8 ++++- src/cc65/symtab.c | 74 +++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/cc65/global.c b/src/cc65/global.c index a337549fe..8b9838dc5 100644 --- a/src/cc65/global.c +++ b/src/cc65/global.c @@ -66,6 +66,7 @@ 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 */ -StrBuf DepTarget = STATIC_STRBUF_INITIALIZER; /* Name of dependency target */ +StrBuf DepName = STATIC_STRBUF_INITIALIZER; /* Name of dependencies file */ +StrBuf FullDepName = STATIC_STRBUF_INITIALIZER; /* Name of full dependencies file */ +StrBuf DepTarget = STATIC_STRBUF_INITIALIZER; /* Name of dependency target */ +StrBuf DebugTableName = STATIC_STRBUF_INITIALIZER; /* Name of debug table dump file */ diff --git a/src/cc65/global.h b/src/cc65/global.h index b9bcf5550..266035346 100644 --- a/src/cc65/global.h +++ b/src/cc65/global.h @@ -77,6 +77,7 @@ extern IntStack DataAlignment; /* Alignment for data */ extern StrBuf DepName; /* Name of dependencies file */ extern StrBuf FullDepName; /* Name of full dependencies file */ extern StrBuf DepTarget; /* Name of dependency target */ +extern StrBuf DebugTableName; /* Name of debug table dump file */ diff --git a/src/cc65/main.c b/src/cc65/main.c index 0ed5af986..74d4fd146 100644 --- a/src/cc65/main.c +++ b/src/cc65/main.c @@ -114,6 +114,7 @@ static void Usage (void) " --create-full-dep name\tCreate a full make dependency file\n" " --data-name seg\t\tSet the name of the DATA segment\n" " --debug\t\t\tDebug mode\n" + " --debug-tables name\t\tWrite symbol table debug info to a file\n" " --debug-info\t\t\tAdd debug info to object file\n" " --debug-opt name\t\tDebug optimization steps\n" " --debug-opt-output\t\tDebug output of each optimization step\n" @@ -494,7 +495,11 @@ static void OptDebug (const char* Opt attribute ((unused)), ++Debug; } - +static void OptDebugTables (const char* Opt, const char* Arg) +/* Dump tables to file */ +{ + FileNameOption (Opt, Arg, &DebugTableName); +} static void OptDebugInfo (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) @@ -865,6 +870,7 @@ int main (int argc, char* argv[]) { "--create-full-dep", 1, OptCreateFullDep }, { "--data-name", 1, OptDataName }, { "--debug", 0, OptDebug }, + { "--debug-tables", 1, OptDebugTables }, { "--debug-info", 0, OptDebugInfo }, { "--debug-opt", 1, OptDebugOpt }, { "--debug-opt-output", 0, OptDebugOptOutput }, diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index c1c5c4696..b4c97e962 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -37,6 +37,7 @@ #include #include #include +#include /* common */ #include "check.h" @@ -68,8 +69,6 @@ /* Data */ /*****************************************************************************/ - - /* An empty symbol table */ SymTable EmptySymTab = { 0, /* PrevTab */ @@ -99,6 +98,7 @@ static SymTable* LabelTab = 0; static SymTable* SPAdjustTab = 0; static SymTable* FailSafeTab = 0; /* For errors */ +static FILE* DebugTableFile = 0; /*****************************************************************************/ /* struct SymTable */ @@ -256,11 +256,25 @@ void PopLexicalLevel (void) --LexLevelDepth; } - - void EnterGlobalLevel (void) /* Enter the program global lexical level */ { + const char* OutName = NULL; + if (!SB_IsEmpty (&DebugTableName)) { + OutName = SB_GetConstBuf (&DebugTableName); + } + + if (OutName) { + /* Open the table file */ + DebugTableFile = fopen (OutName, "w"); + if (DebugTableFile == 0) { + Error ("Cannot create table dump file '%s': %s", OutName, strerror (errno)); + } + } + else if (Debug) { + DebugTableFile = stdout; + } + /* Safety */ PRECONDITION (GetLexicalLevel () == LEX_LEVEL_NONE); @@ -280,8 +294,6 @@ void EnterGlobalLevel (void) FailSafeTab = NewSymTable (SYMTAB_SIZE_GLOBAL); } - - void LeaveGlobalLevel (void) /* Leave the program global lexical level */ { @@ -292,9 +304,41 @@ void LeaveGlobalLevel (void) CheckSymTable (SymTab0); /* Dump the tables if requested */ - if (Debug) { - PrintSymTable (SymTab0, stdout, "Global symbol table"); - PrintSymTable (TagTab0, stdout, "Global tag table"); + if (DebugTableFile) { + SymEntry* Entry; + StrBuf* Header; + + PrintSymTable (SymTab0, DebugTableFile, "Global symbol table"); + PrintSymTable (TagTab0, DebugTableFile, "Global tag table"); + + Entry = TagTab0->SymHead; + if (Entry) { + fputs ("\nGlobal struct and union definitions", DebugTableFile); + fputs ("\n=========================\n", DebugTableFile); + + do { + if (!((Entry->Flags & SC_STRUCT) || (Entry->Flags & SC_UNION)) || !Entry->V.S.SymTab) { + continue; + } + + Header = NewStrBuf(); + if(Entry->Flags & SC_STRUCT) { + SB_AppendStr (Header, "SC_STRUCT: "); + } + else { + SB_AppendStr (Header, "SC_UNION: "); + } + SB_AppendStr (Header, Entry->Name); + SB_Terminate (Header); + + PrintSymTable (Entry->V.S.SymTab, DebugTableFile, SB_GetConstBuf (Header)); + } while ((Entry = Entry->NextSym)); + } + + /* Close the file */ + if (DebugTableFile != stdout && fclose (DebugTableFile) != 0) { + Error ("Error closing table dump file '%s': %s", SB_GetConstBuf(&DebugTableName), strerror (errno)); + } } /* Don't delete the symbol and struct tables! */ @@ -386,6 +430,18 @@ void LeaveFunctionLevel (void) CheckSymTable (SymTab); CheckSymTable (LabelTab); + /* Dump the tables if requested */ + if (DebugTableFile) { + StrBuf* SymbolHeader = NewStrBuf(); + + SB_AppendStr (SymbolHeader, "SC_FUNC: "); + SB_AppendStr (SymbolHeader, CurrentFunc->FuncEntry->AsmName); + SB_AppendStr (SymbolHeader, ": Symbol table"); + SB_Terminate (SymbolHeader); + + PrintSymTable (SymTab, DebugTableFile, SB_GetConstBuf(SymbolHeader)); + } + /* Drop the label table if it is empty */ if (LabelTab->SymCount == 0) { FreeSymTable (LabelTab);