1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-26 08:32:00 +00:00

Add --debug-tables <filename> option and output struct and union fields

This commit is contained in:
empathicqubit 2021-09-10 21:09:22 +02:00 committed by mrdudz
parent 314cdd7feb
commit 2bd30afdeb
4 changed files with 77 additions and 13 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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 },

View File

@ -37,6 +37,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
/* 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);