Write scopes in id order, so we don't need to write out the id itself. Add the

size of the scope to the output file and a flag bit that tells us if the scope
has a size.


git-svn-id: svn://svn.cc65.org/cc65/trunk@5097 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz
2011-07-31 15:37:51 +00:00
parent 8ec6f66bf0
commit 72a13e1a21
7 changed files with 82 additions and 47 deletions

View File

@@ -67,7 +67,7 @@ void DoEnum (void)
int Anon = (CurTok.Tok != TOK_IDENT); int Anon = (CurTok.Tok != TOK_IDENT);
if (!Anon) { if (!Anon) {
/* Enter a new scope, then skip the name */ /* Enter a new scope, then skip the name */
SymEnterLevel (&CurTok.SVal, SCOPETYPE_ENUM, ADDR_SIZE_ABS, 0); SymEnterLevel (&CurTok.SVal, SCOPE_ENUM, ADDR_SIZE_ABS, 0);
NextTok (); NextTok ();
} }

View File

@@ -869,7 +869,7 @@ int main (int argc, char* argv [])
/* Enter the base lexical level. We must do that here, since we may /* Enter the base lexical level. We must do that here, since we may
* define symbols using -D. * define symbols using -D.
*/ */
SymEnterLevel (&GlobalNameSpace, SCOPETYPE_FILE, ADDR_SIZE_DEFAULT, 0); SymEnterLevel (&GlobalNameSpace, SCOPE_FILE, ADDR_SIZE_DEFAULT, 0);
/* Initialize the line infos. Must be done here, since we need line infos /* Initialize the line infos. Must be done here, since we need line infos
* for symbol definitions. * for symbol definitions.
@@ -1018,12 +1018,17 @@ int main (int argc, char* argv [])
SymCheck (); SymCheck ();
} }
/* If we didn't have any errors, close the file scope lexical level */
if (ErrorCount == 0) {
SymLeaveLevel ();
}
/* If we didn't have any errors, check and resolve the segment data */ /* If we didn't have any errors, check and resolve the segment data */
if (ErrorCount == 0) { if (ErrorCount == 0) {
SegCheck (); SegCheck ();
} }
/* If we didn't have any errors, check the assertions */ /* If we didn't have any errors, check the assertions */
if (ErrorCount == 0) { if (ErrorCount == 0) {
CheckAssertions (); CheckAssertions ();
} }

View File

@@ -817,7 +817,7 @@ static void DoEnd (void)
static void DoEndProc (void) static void DoEndProc (void)
/* Leave a lexical level */ /* Leave a lexical level */
{ {
if (GetCurrentSymTabType () != SCOPETYPE_PROC) { if (GetCurrentSymTabType () != SCOPE_PROC) {
/* No local scope */ /* No local scope */
ErrorSkip ("No open .PROC"); ErrorSkip ("No open .PROC");
} else { } else {
@@ -830,7 +830,7 @@ static void DoEndProc (void)
static void DoEndScope (void) static void DoEndScope (void)
/* Leave a lexical level */ /* Leave a lexical level */
{ {
if ( GetCurrentSymTabType () != SCOPETYPE_SCOPE) { if ( GetCurrentSymTabType () != SCOPE_SCOPE) {
/* No local scope */ /* No local scope */
ErrorSkip ("No open .SCOPE"); ErrorSkip ("No open .SCOPE");
} else { } else {
@@ -1539,7 +1539,7 @@ static void DoProc (void)
} }
/* Enter a new scope */ /* Enter a new scope */
SymEnterLevel (&Name, SCOPETYPE_PROC, AddrSize, Sym); SymEnterLevel (&Name, SCOPE_PROC, AddrSize, Sym);
/* Free memory for Name */ /* Free memory for Name */
SB_Done (&Name); SB_Done (&Name);
@@ -1666,7 +1666,7 @@ static void DoScope (void)
AddrSize = OptionalAddrSize (); AddrSize = OptionalAddrSize ();
/* Enter the new scope */ /* Enter the new scope */
SymEnterLevel (&Name, SCOPETYPE_SCOPE, AddrSize, 0); SymEnterLevel (&Name, SCOPE_SCOPE, AddrSize, 0);
/* Free memory for Name */ /* Free memory for Name */
SB_Done (&Name); SB_Done (&Name);
@@ -1760,7 +1760,7 @@ static void DoTag (void)
ErrorSkip ("Unknown struct"); ErrorSkip ("Unknown struct");
return; return;
} }
if (GetSymTabType (Struct) != SCOPETYPE_STRUCT) { if (GetSymTabType (Struct) != SCOPE_STRUCT) {
ErrorSkip ("Not a struct"); ErrorSkip ("Not a struct");
return; return;
} }

View File

@@ -108,7 +108,7 @@ static long DoStructInternal (long Offs, unsigned Type)
int Anon = (CurTok.Tok != TOK_IDENT); int Anon = (CurTok.Tok != TOK_IDENT);
if (!Anon) { if (!Anon) {
/* Enter a new scope, then skip the name */ /* Enter a new scope, then skip the name */
SymEnterLevel (&CurTok.SVal, SCOPETYPE_STRUCT, ADDR_SIZE_ABS, 0); SymEnterLevel (&CurTok.SVal, SCOPE_STRUCT, ADDR_SIZE_ABS, 0);
NextTok (); NextTok ();
/* Start at zero offset in the new scope */ /* Start at zero offset in the new scope */
Offs = 0; Offs = 0;
@@ -195,7 +195,7 @@ static long DoStructInternal (long Offs, unsigned Type)
Struct = ParseScopedSymTable (); Struct = ParseScopedSymTable ();
if (Struct == 0) { if (Struct == 0) {
ErrorSkip ("Unknown struct/union"); ErrorSkip ("Unknown struct/union");
} else if (GetSymTabType (Struct) != SCOPETYPE_STRUCT) { } else if (GetSymTabType (Struct) != SCOPE_STRUCT) {
ErrorSkip ("Not a struct/union"); ErrorSkip ("Not a struct/union");
} else { } else {
SymEntry* SizeSym = GetSizeOfScope (Struct); SymEntry* SizeSym = GetSizeOfScope (Struct);

View File

@@ -111,14 +111,16 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
SymTable* S = xmalloc (sizeof (SymTable) + (Slots-1) * sizeof (SymEntry*)); SymTable* S = xmalloc (sizeof (SymTable) + (Slots-1) * sizeof (SymEntry*));
/* Set variables and clear hash table entries */ /* Set variables and clear hash table entries */
S->Next = 0;
S->Left = 0; S->Left = 0;
S->Right = 0; S->Right = 0;
S->Childs = 0; S->Childs = 0;
S->OwnerSym = 0; S->OwnerSym = 0;
S->SegRanges = AUTO_COLLECTION_INITIALIZER; S->SegRanges = AUTO_COLLECTION_INITIALIZER;
S->Id = ScopeCount++;
S->Flags = ST_NONE; S->Flags = ST_NONE;
S->AddrSize = ADDR_SIZE_DEFAULT; S->AddrSize = ADDR_SIZE_DEFAULT;
S->Type = SCOPETYPE_UNDEF; S->Type = SCOPE_UNDEF;
S->Level = Level; S->Level = Level;
S->TableSlots = Slots; S->TableSlots = Slots;
S->TableEntries = 0; S->TableEntries = 0;
@@ -128,18 +130,13 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
S->Table[Slots] = 0; S->Table[Slots] = 0;
} }
/* Insert the symbol table into the list of all symbol tables and maintain /* Insert the symbol table into the list of all symbol tables */
* a unqiue id for each scope. Count the number of scopes.
*/
S->Next = LastScope;
if (RootScope == 0) { if (RootScope == 0) {
S->Id = 0;
RootScope = S; RootScope = S;
} else { } else {
S->Id = LastScope->Id + 1; LastScope->Next = S;
} }
LastScope = S; LastScope = S;
++ScopeCount;
/* Insert the symbol table into the child tree of the parent */ /* Insert the symbol table into the child tree of the parent */
if (Parent) { if (Parent) {
@@ -224,7 +221,7 @@ void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type,
* does not allocate memory for useless data (unhandled types here don't * does not allocate memory for useless data (unhandled types here don't
* occupy space in any segment). * occupy space in any segment).
*/ */
if (CurrentScope->Type <= SCOPETYPE_HAS_DATA) { if (CurrentScope->Type <= SCOPE_HAS_DATA) {
AddSegRanges (&CurrentScope->SegRanges); AddSegRanges (&CurrentScope->SegRanges);
} }
} }
@@ -905,7 +902,7 @@ void WriteScopes (void)
if (DbgSyms) { if (DbgSyms) {
/* Get head of list */ /* Get head of list */
const SymTable* S = LastScope; SymTable* S = RootScope;
/* Write the scope count to the file */ /* Write the scope count to the file */
ObjWriteVar (ScopeCount); ObjWriteVar (ScopeCount);
@@ -913,11 +910,20 @@ void WriteScopes (void)
/* Walk through all scopes and write them to the file */ /* Walk through all scopes and write them to the file */
while (S) { while (S) {
/* Type must be defined */ /* Flags for this scope */
CHECK (S->Type != SCOPETYPE_UNDEF); unsigned Flags = 0;
/* Id of scope */ /* Check if this scope has a size. If so, remember it in the
ObjWriteVar (S->Id); * flags.
*/
long Size;
SymEntry* SizeSym = FindSizeOfScope (S);
if (SizeSym != 0 && SymIsConst (SizeSym, &Size)) {
Flags |= SCOPE_SIZE;
}
/* Scope must be defined */
CHECK (S->Type != SCOPE_UNDEF);
/* Id of parent scope */ /* Id of parent scope */
if (S->Parent) { if (S->Parent) {
@@ -929,8 +935,8 @@ void WriteScopes (void)
/* Lexical level */ /* Lexical level */
ObjWriteVar (S->Level); ObjWriteVar (S->Level);
/* Scope flags (currently always zero) */ /* Scope flags */
ObjWriteVar (0); ObjWriteVar (Flags);
/* Type of scope */ /* Type of scope */
ObjWriteVar (S->Type); ObjWriteVar (S->Type);
@@ -938,6 +944,11 @@ void WriteScopes (void)
/* Name of the scope */ /* Name of the scope */
ObjWriteVar (S->Name); ObjWriteVar (S->Name);
/* If the scope has a size, write it to the file */
if (SCOPE_HAS_SIZE (Flags)) {
ObjWriteVar (Size);
}
/* Segment ranges for this scope */ /* Segment ranges for this scope */
WriteSegRanges (&S->SegRanges); WriteSegRanges (&S->SegRanges);

View File

@@ -44,16 +44,24 @@
/* Size of scope available? */
#define SCOPE_SIZELESS 0x00U /* No scope size available */
#define SCOPE_SIZE 0x01U /* Scope has a size */
#define SCOPE_MASK_SIZE 0x01U /* Size mask */
#define SCOPE_HAS_SIZE(x) (((x) & SCOPE_MASK_SIZE) == SCOPE_SIZE)
/* Scope types */ /* Scope types */
enum { enum {
SCOPETYPE_GLOBAL, /* Global level */ SCOPE_GLOBAL, /* Global level */
SCOPETYPE_FILE, /* File level */ SCOPE_FILE, /* File level */
SCOPETYPE_PROC, /* .PROC */ SCOPE_PROC, /* .PROC */
SCOPETYPE_SCOPE, /* .SCOPE */ SCOPE_SCOPE, /* .SCOPE */
SCOPETYPE_HAS_DATA = SCOPETYPE_SCOPE, /* Last scope that contains data */ SCOPE_HAS_DATA = SCOPE_SCOPE, /* Last scope that contains data */
SCOPETYPE_STRUCT, /* .STRUCT/.UNION */ SCOPE_STRUCT, /* .STRUCT/.UNION */
SCOPETYPE_ENUM, /* .ENUM */ SCOPE_ENUM, /* .ENUM */
SCOPETYPE_UNDEF = 0xFF SCOPE_UNDEF = 0xFF
}; };

View File

@@ -238,13 +238,13 @@ static const char* GetScopeType (unsigned Type)
/* Return the name of a scope type */ /* Return the name of a scope type */
{ {
switch (Type) { switch (Type) {
case SCOPETYPE_GLOBAL: return "Global scope"; case SCOPE_GLOBAL: return "Global scope";
case SCOPETYPE_FILE: return "File scope"; case SCOPE_FILE: return "File scope";
case SCOPETYPE_PROC: return ".PROC"; case SCOPE_PROC: return ".PROC";
case SCOPETYPE_SCOPE: return ".SCOPE"; case SCOPE_SCOPE: return ".SCOPE";
case SCOPETYPE_STRUCT: return ".STRUCT"; case SCOPE_STRUCT: return ".STRUCT";
case SCOPETYPE_ENUM: return ".ENUM"; case SCOPE_ENUM: return ".ENUM";
case SCOPETYPE_UNDEF: return "Undefined"; case SCOPE_UNDEF: return "Undefined";
default: return "Unknown scope type"; default: return "Unknown scope type";
} }
} }
@@ -709,7 +709,7 @@ void DumpObjDbgSyms (FILE* F, unsigned long Offset)
printf (" Value:%15s0x%08lX (%lu)\n", "", Value, Value); printf (" Value:%15s0x%08lX (%lu)\n", "", Value, Value);
} }
if (SYM_HAS_SIZE (Type)) { if (SYM_HAS_SIZE (Type)) {
printf (" Size:%16s0x%04lX (%lu)\n", "", Size, Size); printf (" Size:%20s0x%04lX (%lu)\n", "", Size, Size);
} }
} }
@@ -821,21 +821,32 @@ void DumpObjScopes (FILE* F, unsigned long Offset)
unsigned SegCount; unsigned SegCount;
unsigned J; unsigned J;
/* Read the data */
unsigned ParentId = ReadVar (F);
unsigned LexicalLevel = ReadVar (F);
unsigned Flags = ReadVar (F);
const char* ScopeType = GetScopeType (ReadVar (F));
/* Print the header */ /* Print the header */
printf (" Index:%27u\n", I); printf (" Index:%27u\n", I);
/* Print the data */ /* Print the data */
printf (" Id:%28lu\n", ReadVar (F)); printf (" Parent id:%21u\n", ParentId);
printf (" Parent id:%21lu\n", ReadVar (F)); printf (" Lexical level:%17u\n", LexicalLevel);
printf (" Lexical level:%17lu\n", ReadVar (F)); printf (" Flags:%21s0x%02X\n", "", Flags);
printf (" Flags:%25lu\n", ReadVar (F)); printf (" Type:%26s\n", ScopeType);
printf (" Type:%26s\n", GetScopeType (ReadVar (F)));
/* Resolve and print the name */ /* Resolve and print the name */
Name = GetString (&StrPool, ReadVar (F)); Name = GetString (&StrPool, ReadVar (F));
Len = strlen (Name); Len = strlen (Name);
printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name); printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name);
/* Size */
if (SCOPE_HAS_SIZE (Flags)) {
unsigned long Size = ReadVar (F);
printf (" Size:%20s0x%04lX (%lu)\n", "", Size, Size);
}
/* Segment ranges */ /* Segment ranges */
SegCount = ReadVar (F); SegCount = ReadVar (F);
printf (" Segment ranges:\n"); printf (" Segment ranges:\n");