From: cuz Date: Sat, 13 Dec 2003 20:56:31 +0000 (+0000) Subject: Set the address size once assembly is terminated X-Git-Tag: V2.12.0~1054 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6a3ea294297866eace96fb2129665719f8343f55;p=cc65 Set the address size once assembly is terminated git-svn-id: svn://svn.cc65.org/cc65/trunk@2750 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/ca65/symentry.c b/src/ca65/symentry.c index 6163bf02a..1f0420956 100644 --- a/src/ca65/symentry.c +++ b/src/ca65/symentry.c @@ -85,7 +85,7 @@ SymEntry* NewSymEntry (const char* Name, unsigned Flags) S->SymTab = 0; S->Pos = CurPos; S->Flags = Flags; - S->V.Expr = 0; + S->Expr = 0; S->ExprRefs = AUTO_COLLECTION_INITIALIZER; S->ExportSize = ADDR_SIZE_DEFAULT; S->AddrSize = ADDR_SIZE_DEFAULT; @@ -200,7 +200,7 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags } /* Set the symbol value */ - S->V.Expr = Expr; + S->Expr = Expr; /* If the symbol is marked as global, export it. Address size is checked * below. @@ -480,7 +480,7 @@ int SymIsConst (SymEntry* S, long* Val) */ { /* Check for constness */ - return (SymHasExpr (S) && IsConstExpr (S->V.Expr, Val)); + return (SymHasExpr (S) && IsConstExpr (S->Expr, Val)); } @@ -499,7 +499,7 @@ struct ExprNode* GetSymExpr (SymEntry* S) /* Get the expression for a non-const symbol */ { PRECONDITION (S != 0 && SymHasExpr (S)); - return S->V.Expr; + return S->Expr; } @@ -509,7 +509,7 @@ const struct ExprNode* SymResolve (const SymEntry* S) * NULL. Do not call in other contexts! */ { - return SymHasExpr (S)? S->V.Expr : 0; + return SymHasExpr (S)? S->Expr : 0; } diff --git a/src/ca65/symentry.h b/src/ca65/symentry.h index f72e3bf17..ab3ec5e6c 100644 --- a/src/ca65/symentry.h +++ b/src/ca65/symentry.h @@ -85,10 +85,7 @@ struct SymEntry { FilePos Pos; /* File position for this symbol */ unsigned Flags; /* Symbol flags */ unsigned Index; /* Index of import/export entries */ - union { - struct ExprNode* Expr; /* Symbol expression */ - SymEntry* Sym; /* Symbol (if trampoline entry) */ - } V; + struct ExprNode* Expr; /* Symbol expression */ Collection ExprRefs; /* Expressions using this symbol */ unsigned char ExportSize; /* Export address size */ unsigned char AddrSize; /* Address size of label */ @@ -247,7 +244,7 @@ INLINE int SymHasUserMark (SymEntry* S) #else # define SymHasUserMark(S) (((S)->Flags & SF_USER) != 0) #endif - + struct SymTable* GetSymParentScope (SymEntry* S); /* Get the parent scope of the symbol (not the one it is defined in). Return * NULL if the symbol is a cheap local, or defined on global level. diff --git a/src/ca65/symtab.c b/src/ca65/symtab.c index 46b6468ed..5d7e864c6 100644 --- a/src/ca65/symtab.c +++ b/src/ca65/symtab.c @@ -52,6 +52,7 @@ #include "segment.h" #include "sizeof.h" #include "spool.h" +#include "studyexpr.h" #include "symtab.h" @@ -384,15 +385,16 @@ SymEntry* SymFindAny (SymTable* Scope, const char* Name) Sym = SymFind (Scope, Name, SYM_FIND_EXISTING); if (Sym) { /* Found, return it */ - return Sym; - } else { - /* Not found, search in the parent scope, if we have one */ - Scope = Scope->Parent; + break; } + + /* Not found, search in the parent scope, if we have one */ + Scope = Scope->Parent; + } while (Sym == 0 && Scope != 0); - /* Not found */ - return 0; + /* Return the result */ + return Sym; } @@ -420,26 +422,17 @@ static void SymCheckUndefined (SymEntry* S) * AutoImport flag is not set, it's an error. */ SymEntry* Sym = 0; - if (S->SymTab) { - /* It's a global symbol, get the higher level table */ - SymTable* Tab = S->SymTab->Parent; - while (Tab) { - Sym = SymFindAny (Tab, GetString (S->Name)); - if (Sym) { - if (Sym->Flags & (SF_DEFINED | SF_IMPORT)) { - /* We've found a symbol in a higher level that is - * either defined in the source, or an import. - */ - break; - } else { - /* The symbol found is undefined itself. Look further */ - Tab = Sym->SymTab->Parent; - } - } else { - /* No symbol found */ - break; - } - } + SymTable* Tab = GetSymParentScope (S); + while (Tab) { + Sym = SymFind (Tab, GetString (S->Name), SYM_FIND_EXISTING); + if (Sym && (Sym->Flags & (SF_DEFINED | SF_IMPORT)) != 0) { + /* We've found a symbol in a higher level that is + * either defined in the source, or an import. + */ + break; + } + /* No matching symbol found in this level. Look further */ + Tab = Tab->Parent; } if (Sym) { @@ -529,9 +522,9 @@ void SymCheck (void) if (S->Flags & SF_GLOBAL) { S->Flags &= ~SF_GLOBAL; if (S->Flags & SF_DEFINED) { - S->Flags |= SF_EXPORT; + S->Flags |= SF_EXPORT; } else { - S->Flags |= SF_IMPORT; + S->Flags |= SF_IMPORT; } } @@ -545,20 +538,24 @@ void SymCheck (void) S = S->List; } - /* Second pass: Walk again through the symbols. Ignore undefined's, since - * we handled them in the last pass, and ignore unused symbols, since - * we handled them in the last pass, too. + /* Second pass: Walk again through the symbols. Count exports and imports + * and set address sizes where this has not happened before. Ignore + * undefined's, since we handled them in the last pass, and ignore unused + * symbols, since we handled them in the last pass, too. */ S = SymList; while (S) { if ((S->Flags & SF_UNUSED) == 0 && (S->Flags & SF_UNDEFMASK) != SF_UNDEFVAL) { + + /* Check for defined symbols that were never referenced */ if ((S->Flags & SF_DEFINED) != 0 && (S->Flags & SF_REFERENCED) == 0) { - /* Symbol was defined but never referenced */ PWarning (&S->Pos, 2, "Symbol `%s' is defined but never used", GetString (S->Name)); } + + /* Assign an index to all imports */ if (S->Flags & SF_IMPORT) { if ((S->Flags & (SF_REFERENCED | SF_FORCED)) == SF_NONE) { /* Imported symbol is not referenced */ @@ -571,11 +568,24 @@ void SymCheck (void) S->Flags |= SF_INDEXED; } } + + /* Assign an index to all exports */ if (S->Flags & SF_EXPORT) { /* Give the export an index, count exports */ S->Index = ExportCount++; S->Flags |= SF_INDEXED; } + + /* If the symbol is defined but has an unknown address size, + * recalculate it. + */ + if (SymHasExpr (S) && S->AddrSize == ADDR_SIZE_DEFAULT) { + ExprDesc ED; + ED_Init (&ED); + StudyExpr (S->Expr, &ED); + S->AddrSize = ED.AddrSize; + ED_Done (&ED); + } } /* Next symbol */ @@ -696,7 +706,7 @@ void WriteExports (void) ObjWrite32 (ConstVal); } else { /* Expression involved */ - WriteExpr (S->V.Expr); + WriteExpr (S->Expr); } /* Write the source file position */ @@ -762,7 +772,7 @@ void WriteDbgSyms (void) ObjWrite32 (ConstVal); } else { /* Expression involved */ - WriteExpr (S->V.Expr); + WriteExpr (S->Expr); } /* Write the source file position */