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;
}
/* 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.
*/
{
/* Check for constness */
- return (SymHasExpr (S) && IsConstExpr (S->V.Expr, Val));
+ return (SymHasExpr (S) && IsConstExpr (S->Expr, Val));
}
/* Get the expression for a non-const symbol */
{
PRECONDITION (S != 0 && SymHasExpr (S));
- return S->V.Expr;
+ return S->Expr;
}
* NULL. Do not call in other contexts!
*/
{
- return SymHasExpr (S)? S->V.Expr : 0;
+ return SymHasExpr (S)? S->Expr : 0;
}
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 */
#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.
#include "segment.h"
#include "sizeof.h"
#include "spool.h"
+#include "studyexpr.h"
#include "symtab.h"
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;
}
* 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) {
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;
}
}
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 */
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 */
ObjWrite32 (ConstVal);
} else {
/* Expression involved */
- WriteExpr (S->V.Expr);
+ WriteExpr (S->Expr);
}
/* Write the source file position */
ObjWrite32 (ConstVal);
} else {
/* Expression involved */
- WriteExpr (S->V.Expr);
+ WriteExpr (S->Expr);
}
/* Write the source file position */