More work on .sizeof, fixed problems with cheap locals

git-svn-id: svn://svn.cc65.org/cc65/trunk@2704 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-12-03 09:18:31 +00:00
parent 500f6c994a
commit 14d0577ef0
7 changed files with 55 additions and 60 deletions

View file

@ -394,6 +394,19 @@ static ExprNode* FuncSizeOf (void)
long Size; long Size;
if (Tok == TOK_LOCAL_IDENT) {
/* Cheap local symbol, special handling */
Sym = SymFindLocal (SymLast, SVal, SYM_FIND_EXISTING);
if (Sym == 0) {
Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
return GenLiteralExpr (0);
} else {
SizeSym = GetSizeOfSymbol (Sym);
}
} else {
/* Parse the scope and the name */ /* Parse the scope and the name */
SymTable* ParentScope = ParseScopedIdent (Name, &FullName); SymTable* ParentScope = ParseScopedIdent (Name, &FullName);
@ -414,6 +427,7 @@ static ExprNode* FuncSizeOf (void)
Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName)); Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
return GenLiteralExpr (0); return GenLiteralExpr (0);
} }
}
/* Check if we have a size */ /* Check if we have a size */
if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) { if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {

View file

@ -37,6 +37,7 @@
#include "addrsize.h" #include "addrsize.h"
/* ca65 */ /* ca65 */
#include "expr.h"
#include "sizeof.h" #include "sizeof.h"
#include "symtab.h" #include "symtab.h"
@ -88,6 +89,7 @@ SymEntry* DefSizeOfScope (SymTable* Scope, long Size)
{ {
SymEntry* SizeSym = GetSizeOfScope (Scope); SymEntry* SizeSym = GetSizeOfScope (Scope);
SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE); SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
return SizeSym;
} }
@ -97,6 +99,7 @@ SymEntry* DefSizeOfSymbol (SymEntry* Sym, long Size)
{ {
SymEntry* SizeSym = GetSizeOfSymbol (Sym); SymEntry* SizeSym = GetSizeOfSymbol (Sym);
SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE); SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
return SizeSym;
} }

View file

@ -174,7 +174,7 @@ SymEntry* ParseScopedSymName (int AllocNew)
* symbol. * symbol.
*/ */
if (AllocNew) { if (AllocNew) {
return NewSymEntry (Ident); return NewSymEntry (Ident, SF_NONE);
} else { } else {
return 0; return 0;
} }

View file

@ -72,23 +72,7 @@ SymEntry* SymLast = 0;
int IsLocalName (const char* Name) SymEntry* NewSymEntry (const char* Name, unsigned Flags)
/* Return true if Name is the name of a local symbol */
{
return (*Name == LocalStart);
}
int IsLocalNameId (unsigned Name)
/* Return true if Name is the name of a local symbol */
{
return (*GetString (Name) == LocalStart);
}
SymEntry* NewSymEntry (const char* Name)
/* Allocate a symbol table entry, initialize and return it */ /* Allocate a symbol table entry, initialize and return it */
{ {
/* Allocate memory */ /* Allocate memory */
@ -100,7 +84,7 @@ SymEntry* NewSymEntry (const char* Name)
S->Locals = 0; S->Locals = 0;
S->SymTab = 0; S->SymTab = 0;
S->Pos = CurPos; S->Pos = CurPos;
S->Flags = 0; S->Flags = Flags;
S->V.Expr = 0; S->V.Expr = 0;
S->ExprRefs = AUTO_COLLECTION_INITIALIZER; S->ExprRefs = AUTO_COLLECTION_INITIALIZER;
S->ExportSize = ADDR_SIZE_DEFAULT; S->ExportSize = ADDR_SIZE_DEFAULT;
@ -243,7 +227,7 @@ void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags
} }
/* If this is not a local symbol, remember it as the last global one */ /* If this is not a local symbol, remember it as the last global one */
if (!IsLocalNameId (S->Name)) { if ((S->Flags & SF_LOCAL) == 0) {
SymLast = S; SymLast = S;
} }
} }

View file

@ -62,6 +62,7 @@
#define SF_EXPORT 0x0004 /* Export this symbol */ #define SF_EXPORT 0x0004 /* Export this symbol */
#define SF_IMPORT 0x0008 /* Import this symbol */ #define SF_IMPORT 0x0008 /* Import this symbol */
#define SF_GLOBAL 0x0010 /* Global symbol */ #define SF_GLOBAL 0x0010 /* Global symbol */
#define SF_LOCAL 0x0020 /* Cheap local symbol */
#define SF_LABEL 0x0080 /* Used as a label */ #define SF_LABEL 0x0080 /* Used as a label */
#define SF_FORCED 0x0100 /* Forced import, SF_IMPORT also set */ #define SF_FORCED 0x0100 /* Forced import, SF_IMPORT also set */
#define SF_INDEXED 0x0800 /* Index is valid */ #define SF_INDEXED 0x0800 /* Index is valid */
@ -85,8 +86,7 @@ struct SymEntry {
unsigned Flags; /* Symbol flags */ unsigned Flags; /* Symbol flags */
unsigned Index; /* Index of import/export entries */ unsigned Index; /* Index of import/export entries */
union { union {
struct ExprNode* Expr; /* Expression if CONST not set */ struct ExprNode* Expr; /* Symbol expression */
long Val; /* Value (if CONST set) */
SymEntry* Sym; /* Symbol (if trampoline entry) */ SymEntry* Sym; /* Symbol (if trampoline entry) */
} V; } V;
Collection ExprRefs; /* Expressions using this symbol */ Collection ExprRefs; /* Expressions using this symbol */
@ -111,13 +111,7 @@ extern SymEntry* SymLast;
int IsLocalName (const char* Name); SymEntry* NewSymEntry (const char* Name, unsigned Flags);
/* Return true if Name is the name of a local symbol */
int IsLocalNameId (unsigned Name);
/* Return true if Name is the name of a local symbol */
SymEntry* NewSymEntry (const char* Name);
/* Allocate a symbol table entry, initialize and return it */ /* Allocate a symbol table entry, initialize and return it */
int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E); int SymSearchTree (SymEntry* T, const char* Name, SymEntry** E);

View file

@ -269,7 +269,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const char* Name, int AllocNew)
/* No last global, so there's no local table */ /* No last global, so there's no local table */
Error ("No preceeding global symbol"); Error ("No preceeding global symbol");
if (AllocNew) { if (AllocNew) {
return NewSymEntry (Name); return NewSymEntry (Name, SF_LOCAL);
} else { } else {
return 0; return 0;
} }
@ -286,7 +286,7 @@ SymEntry* SymFindLocal (SymEntry* Parent, const char* Name, int AllocNew)
if (AllocNew) { if (AllocNew) {
/* Otherwise create a new entry, insert and return it */ /* Otherwise create a new entry, insert and return it */
SymEntry* N = NewSymEntry (Name); SymEntry* N = NewSymEntry (Name, SF_LOCAL);
if (S == 0) { if (S == 0) {
Parent->Locals = N; Parent->Locals = N;
} else if (Cmp < 0) { } else if (Cmp < 0) {
@ -325,7 +325,7 @@ SymEntry* SymFind (SymTable* Scope, const char* Name, int AllocNew)
if (AllocNew) { if (AllocNew) {
/* Otherwise create a new entry, insert and return it */ /* Otherwise create a new entry, insert and return it */
SymEntry* N = NewSymEntry (Name); SymEntry* N = NewSymEntry (Name, SF_NONE);
if (S == 0) { if (S == 0) {
Scope->Table[Hash] = N; Scope->Table[Hash] = N;
} else if (Cmp < 0) { } else if (Cmp < 0) {
@ -377,7 +377,7 @@ int SymIsZP (SymEntry* S)
* enclosing scope for a symbol with the same name, and return the ZP * enclosing scope for a symbol with the same name, and return the ZP
* attribute of this symbol if we find one. * attribute of this symbol if we find one.
*/ */
if (!IsLocalNameId (S->Name) && (S->Flags & (SF_DEFINED | SF_IMPORT)) == 0 && if ((S->Flags & (SF_DEFINED | SF_IMPORT | SF_LOCAL)) == 0 &&
S->SymTab->Parent != 0) { S->SymTab->Parent != 0) {
/* Try to find a symbol with the same name in the enclosing scope */ /* Try to find a symbol with the same name in the enclosing scope */