Skip to content

Commit 8c329df

Browse files
authored
Merge pull request cc65#2344 from acqn/Cleanup
[cc65] Cleanup for symbol types and flags
2 parents 0f7d2dd + 38dac90 commit 8c329df

File tree

12 files changed

+368
-259
lines changed

12 files changed

+368
-259
lines changed

src/cc65/asmstmt.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ static void AsmErrorSkip (void)
8080

8181

8282

83-
static SymEntry* AsmGetSym (unsigned Arg, unsigned Type)
84-
/* Find the symbol with the name currently in NextTok. The symbol must be of
85-
** the given type. On errors, NULL is returned.
83+
static SymEntry* AsmGetSym (unsigned Arg, int OnStack)
84+
/* Find the symbol with the name currently in NextTok. The symbol must be on
85+
** the stack if OnStack is true. On errors, NULL is returned.
8686
*/
8787
{
8888
SymEntry* Sym;
@@ -110,8 +110,8 @@ static SymEntry* AsmGetSym (unsigned Arg, unsigned Type)
110110
/* We found the symbol - skip the name token */
111111
NextToken ();
112112

113-
/* Check if we have a global symbol */
114-
if ((Sym->Flags & Type) != Type) {
113+
/* Check if the symbol is on the stack */
114+
if ((Sym->Flags & SC_STORAGEMASK) != SC_AUTO ? OnStack : !OnStack) {
115115
Error ("Type of argument %u differs from format specifier", Arg);
116116
AsmErrorSkip ();
117117
return 0;
@@ -218,23 +218,24 @@ static void ParseGVarArg (StrBuf* T, unsigned Arg)
218218
*/
219219
{
220220
/* Parse the symbol name parameter and check the type */
221-
SymEntry* Sym = AsmGetSym (Arg, SC_STATIC);
221+
SymEntry* Sym = AsmGetSym (Arg, 0);
222222
if (Sym == 0) {
223223
/* Some sort of error */
224224
return;
225225
}
226226

227-
/* Check for external linkage */
228-
if (Sym->Flags & (SC_EXTERN | SC_STORAGE | SC_FUNC)) {
229-
/* External linkage or a function */
227+
/* Get the correct asm name */
228+
if ((Sym->Flags & SC_TYPEMASK) == SC_FUNC || SymIsGlobal (Sym)) {
229+
/* External or internal linkage or a function */
230230
SB_AppendChar (T, '_');
231231
SB_AppendStr (T, Sym->Name);
232-
} else if (Sym->Flags & SC_REGISTER) {
232+
} else if ((Sym->Flags & SC_STORAGEMASK) == SC_REGISTER) {
233+
/* Register variable */
233234
char Buf[32];
234235
xsprintf (Buf, sizeof (Buf), "regbank+%d", Sym->V.R.RegOffs);
235236
SB_AppendStr (T, Buf);
236237
} else {
237-
/* Static variable */
238+
/* Local static variable */
238239
SB_AppendStr (T, LocalDataLabelName (Sym->V.L.Label));
239240
}
240241
}
@@ -248,7 +249,7 @@ static void ParseLVarArg (StrBuf* T, unsigned Arg)
248249
char Buf [16];
249250

250251
/* Parse the symbol name parameter and check the type */
251-
SymEntry* Sym = AsmGetSym (Arg, SC_AUTO);
252+
SymEntry* Sym = AsmGetSym (Arg, 1);
252253
if (Sym == 0) {
253254
/* Some sort of error */
254255
return;

src/cc65/compile.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@ static void Parse (void)
121121
}
122122

123123
/* Read the declaration specifier */
124-
ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_INT, SC_EXTERN | SC_STATIC);
124+
ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_INT, SC_NONE);
125125

126126
/* Don't accept illegal storage classes */
127-
if ((Spec.StorageClass & SC_TYPEMASK) == 0) {
128-
if ((Spec.StorageClass & SC_AUTO) != 0 ||
129-
(Spec.StorageClass & SC_REGISTER) != 0) {
130-
Error ("Illegal storage class");
131-
Spec.StorageClass = SC_EXTERN | SC_STATIC;
132-
}
127+
if ((Spec.StorageClass & SC_STORAGEMASK) == SC_AUTO ||
128+
(Spec.StorageClass & SC_STORAGEMASK) == SC_REGISTER) {
129+
Error ("Illegal storage class");
130+
Spec.StorageClass &= ~SC_STORAGEMASK;
133131
}
134132

135133
/* Check if this is only a type declaration */
@@ -172,26 +170,26 @@ static void Parse (void)
172170
** - if the storage class is explicitly specified as static,
173171
** - or if there is an initialization.
174172
**
175-
** This means that "extern int i;" will not get storage allocated.
173+
** This means that "extern int i;" will not get storage allocated
174+
** in this translation unit.
176175
*/
177-
if ((Decl.StorageClass & SC_FUNC) != SC_FUNC &&
176+
if ((Decl.StorageClass & SC_TYPEMASK) != SC_FUNC &&
178177
(Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
179-
if ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
180-
(Decl.StorageClass & (SC_EXTERN|SC_STATIC)) == SC_STATIC ||
181-
((Decl.StorageClass & SC_EXTERN) != 0 &&
178+
/* The variable is visible in the file scope */
179+
if ((Decl.StorageClass & SC_STORAGEMASK) == SC_NONE ||
180+
(Decl.StorageClass & SC_STORAGEMASK) == SC_STATIC ||
181+
((Decl.StorageClass & SC_STORAGEMASK) == SC_EXTERN &&
182182
CurTok.Tok == TOK_ASSIGN)) {
183-
/* We will allocate storage */
184-
Decl.StorageClass |= SC_STORAGE;
185-
} else {
186-
/* It's a declaration */
187-
Decl.StorageClass |= SC_DECL;
183+
/* We will allocate storage in this translation unit */
184+
Decl.StorageClass |= SC_TU_STORAGE;
188185
}
189186
}
190187

191188
/* If this is a function declarator that is not followed by a comma
192189
** or semicolon, it must be followed by a function body.
193190
*/
194-
if ((Decl.StorageClass & SC_FUNC) != 0) {
191+
if ((Decl.StorageClass & SC_TYPEMASK) == SC_FUNC) {
192+
/* The function is now visible in the file scope */
195193
if (CurTok.Tok == TOK_LCURLY) {
196194
/* A definition */
197195
Decl.StorageClass |= SC_DEF;
@@ -205,8 +203,6 @@ static void Parse (void)
205203
}
206204
} else {
207205
/* Just a declaration */
208-
Decl.StorageClass |= SC_DECL;
209-
210206
FuncDef = GetFuncDesc (Decl.Type);
211207
if ((FuncDef->Flags & (FD_EMPTY | FD_OLDSTYLE)) == FD_OLDSTYLE) {
212208
/* A parameter list without types is only allowed in a
@@ -224,7 +220,7 @@ static void Parse (void)
224220
SymUseAttr (Sym, &Decl);
225221

226222
/* Reserve storage for the variable if we need to */
227-
if (Decl.StorageClass & SC_STORAGE) {
223+
if (Decl.StorageClass & SC_TU_STORAGE) {
228224

229225
/* Get the size of the variable */
230226
unsigned Size = SizeOf (Decl.Type);
@@ -327,9 +323,13 @@ static void Parse (void)
327323
}
328324

329325
/* Make the symbol zeropage according to the segment address size */
330-
if ((Sym->Flags & SC_STATIC) != 0) {
331-
if (GetSegAddrSize (GetSegName (CS->CurDSeg)) == ADDR_SIZE_ZP) {
332-
Sym->Flags |= SC_ZEROPAGE;
326+
if ((Sym->Flags & SC_TYPEMASK) == SC_NONE) {
327+
if (SymIsGlobal (Sym) ||
328+
(Sym->Flags & SC_STORAGEMASK) == SC_STATIC ||
329+
(Sym->Flags & SC_STORAGEMASK) == SC_REGISTER) {
330+
if (GetSegAddrSize (GetSegName (CS->CurDSeg)) == ADDR_SIZE_ZP) {
331+
Sym->Flags |= SC_ZEROPAGE;
332+
}
333333
}
334334
}
335335

@@ -517,7 +517,10 @@ void Compile (const char* FileName)
517517
** global variables.
518518
*/
519519
for (Entry = GetGlobalSymTab ()->SymHead; Entry; Entry = Entry->NextSym) {
520-
if ((Entry->Flags & (SC_STORAGE | SC_DEF | SC_STATIC)) == (SC_STORAGE | SC_STATIC)) {
520+
/* Is it a global (with or without static) tentative declaration of
521+
** an uninitialized variable?
522+
*/
523+
if ((Entry->Flags & (SC_TU_STORAGE | SC_DEF)) == SC_TU_STORAGE) {
521524
/* Assembly definition of uninitialized global variable */
522525
SymEntry* TagSym = GetESUTagSym (Entry->Type);
523526
unsigned Size = SizeOf (Entry->Type);
@@ -552,9 +555,9 @@ void Compile (const char* FileName)
552555
Entry->Name,
553556
GetFullTypeName (Entry->Type));
554557
}
555-
} else if (!SymIsDef (Entry) && (Entry->Flags & SC_FUNC) == SC_FUNC) {
558+
} else if (!SymIsDef (Entry) && (Entry->Flags & SC_TYPEMASK) == SC_FUNC) {
556559
/* Check for undefined functions */
557-
if ((Entry->Flags & (SC_EXTERN | SC_STATIC)) == SC_STATIC && SymIsRef (Entry)) {
560+
if ((Entry->Flags & SC_STORAGEMASK) == SC_STATIC && SymIsRef (Entry)) {
558561
Warning ("Static function '%s' used but never defined",
559562
Entry->Name);
560563
}

src/cc65/declare.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static unsigned ParseOneStorageClass (void)
9191
switch (CurTok.Tok) {
9292

9393
case TOK_EXTERN:
94-
StorageClass = SC_EXTERN | SC_STATIC;
94+
StorageClass = SC_EXTERN;
9595
NextToken ();
9696
break;
9797

@@ -101,7 +101,7 @@ static unsigned ParseOneStorageClass (void)
101101
break;
102102

103103
case TOK_REGISTER:
104-
StorageClass = SC_REGISTER | SC_STATIC;
104+
StorageClass = SC_REGISTER;
105105
NextToken ();
106106
break;
107107

@@ -136,9 +136,9 @@ static int ParseStorageClass (DeclSpec* Spec)
136136
}
137137

138138
while (StorageClass != 0) {
139-
if (Spec->StorageClass == 0) {
140-
Spec->StorageClass = StorageClass;
141-
} else if (Spec->StorageClass == StorageClass) {
139+
if ((Spec->StorageClass & SC_STORAGEMASK) == 0) {
140+
Spec->StorageClass |= StorageClass;
141+
} else if ((Spec->StorageClass & SC_STORAGEMASK) == StorageClass) {
142142
Warning ("Duplicate storage class specifier");
143143
} else {
144144
Error ("Conflicting storage class specifier");
@@ -618,12 +618,12 @@ static SymEntry* ForwardESU (const char* Name, unsigned Flags, unsigned* DSFlags
618618
*/
619619
SymEntry* TagEntry = FindTagSym (Name);
620620
if (TagEntry == 0) {
621-
if ((Flags & SC_ESUTYPEMASK) != SC_ENUM) {
621+
if ((Flags & SC_TYPEMASK) != SC_ENUM) {
622622
TagEntry = AddStructSym (Name, Flags, 0, 0, DSFlags);
623623
} else {
624624
TagEntry = AddEnumSym (Name, Flags, 0, 0, DSFlags);
625625
}
626-
} else if ((TagEntry->Flags & SC_TYPEMASK) != (Flags & SC_ESUTYPEMASK)) {
626+
} else if ((TagEntry->Flags & SC_TYPEMASK) != (Flags & SC_TYPEMASK)) {
627627
/* Already defined, but not the same type class */
628628
Error ("Symbol '%s' is already different kind", Name);
629629
}
@@ -798,7 +798,7 @@ static SymEntry* ParseEnumSpec (const char* Name, unsigned* DSFlags)
798798
}
799799

800800
/* Add an entry of the enumerator to the symbol table */
801-
AddConstSym (Ident, NewType, SC_ENUMERATOR | SC_CONST, EnumVal);
801+
AddConstSym (Ident, NewType, SC_DEF | SC_ENUMERATOR, EnumVal);
802802

803803
/* Use this type for following members */
804804
MemberType = NewType;
@@ -1825,8 +1825,8 @@ static void ParseOldStyleParamDeclList (FuncDesc* F attribute ((unused)))
18251825
/* We accept only auto and register as storage class specifiers, but
18261826
** we ignore all this, since we use auto anyway.
18271827
*/
1828-
if ((Spec.StorageClass & SC_AUTO) == 0 &&
1829-
(Spec.StorageClass & SC_REGISTER) == 0) {
1828+
if ((Spec.StorageClass & SC_STORAGEMASK) != SC_AUTO &&
1829+
(Spec.StorageClass & SC_STORAGEMASK) != SC_REGISTER) {
18301830
Error ("Illegal storage class");
18311831
}
18321832

@@ -1942,12 +1942,12 @@ static void ParseAnsiParamList (FuncDesc* F)
19421942
ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_NONE, SC_AUTO);
19431943

19441944
/* We accept only auto and register as storage class specifiers */
1945-
if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
1946-
Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1947-
} else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
1948-
Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
1945+
if ((Spec.StorageClass & SC_STORAGEMASK) == SC_REGISTER) {
1946+
Spec.StorageClass = SC_REGISTER | SC_PARAM | SC_DEF;
19491947
} else {
1950-
Error ("Illegal storage class");
1948+
if ((Spec.StorageClass & SC_STORAGEMASK) != SC_AUTO) {
1949+
Error ("Illegal storage class");
1950+
}
19511951
Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
19521952
}
19531953

@@ -2355,7 +2355,9 @@ int ParseDecl (DeclSpec* Spec, Declarator* D, declmode_t Mode)
23552355
D->StorageClass = Spec->StorageClass;
23562356

23572357
/* If we have a function, add a special symbol type */
2358-
if (IsTypeFunc (D->Type)) {
2358+
if (Mode != DM_ACCEPT_PARAM_IDENT &&
2359+
IsTypeFunc (D->Type) &&
2360+
(D->StorageClass & SC_TYPEMASK) == SC_NONE) {
23592361
D->StorageClass |= SC_FUNC;
23602362
}
23612363

@@ -2479,9 +2481,9 @@ void ParseDeclSpec (DeclSpec* Spec, typespec_t TSFlags, unsigned DefStorage)
24792481
ParseTypeSpec (Spec, TSFlags | TS_STORAGE_CLASS_SPEC | TS_FUNCTION_SPEC);
24802482

24812483
/* If no explicit storage class is given, use the default */
2482-
if (Spec->StorageClass == 0) {
2484+
if ((Spec->StorageClass & SC_STORAGEMASK) == 0) {
24832485
Spec->Flags |= DS_DEF_STORAGE;
2484-
Spec->StorageClass = DefStorage;
2486+
Spec->StorageClass |= DefStorage;
24852487
}
24862488
}
24872489

src/cc65/expr.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,8 @@ static void Primary (ExprDesc* E)
12231223
NextToken ();
12241224

12251225
/* Check for illegal symbol types */
1226-
CHECK ((Sym->Flags & SC_LABEL) != SC_LABEL);
1227-
if (Sym->Flags & SC_ESUTYPEMASK) {
1226+
CHECK ((Sym->Flags & SC_TYPEMASK) != SC_LABEL);
1227+
if ((Sym->Flags & SC_TYPEMASK) == SC_TYPEDEF) {
12281228
/* Cannot use type symbols */
12291229
Error ("Variable identifier expected");
12301230
/* Assume an int type to make E valid */
@@ -1244,7 +1244,7 @@ static void Primary (ExprDesc* E)
12441244
/* Enum or some other numeric constant */
12451245
E->Flags = E_LOC_NONE | E_RTYPE_RVAL;
12461246
E->IVal = Sym->V.ConstVal;
1247-
} else if ((Sym->Flags & SC_AUTO) == SC_AUTO) {
1247+
} else if ((Sym->Flags & SC_STORAGEMASK) == SC_AUTO) {
12481248
/* Local variable. If this is a parameter for a variadic
12491249
** function, we have to add some address calculations, and the
12501250
** address is not const.
@@ -1258,26 +1258,25 @@ static void Primary (ExprDesc* E)
12581258
E->Flags = E_LOC_STACK | E_RTYPE_LVAL;
12591259
E->IVal = Sym->V.Offs;
12601260
}
1261-
} else if ((Sym->Flags & SC_FUNC) == SC_FUNC) {
1261+
} else if ((Sym->Flags & SC_TYPEMASK) == SC_FUNC) {
12621262
/* Function */
12631263
E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
12641264
E->Name = (uintptr_t) Sym->Name;
1265-
} else if ((Sym->Flags & SC_REGISTER) == SC_REGISTER) {
1265+
} else if ((Sym->Flags & SC_STORAGEMASK) == SC_REGISTER) {
12661266
/* Register variable, zero page based */
12671267
E->Flags = E_LOC_REGISTER | E_RTYPE_LVAL;
12681268
E->Name = Sym->V.R.RegOffs;
1269-
} else if ((Sym->Flags & SC_STATIC) == SC_STATIC) {
1270-
/* Static variable */
1271-
if (Sym->Flags & (SC_EXTERN | SC_STORAGE | SC_DECL)) {
1272-
E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
1273-
E->Name = (uintptr_t) Sym->Name;
1274-
} else {
1275-
E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
1276-
E->Name = Sym->V.L.Label;
1277-
}
1278-
} else {
1269+
} else if (SymIsGlobal (Sym)) {
1270+
/* Global variable */
1271+
E->Flags = E_LOC_GLOBAL | E_RTYPE_LVAL;
1272+
E->Name = (uintptr_t) Sym->Name;
1273+
} else if ((Sym->Flags & SC_STORAGEMASK) == SC_STATIC) {
12791274
/* Local static variable */
12801275
E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
1276+
E->Name = Sym->V.L.Label;
1277+
} else {
1278+
/* Other */
1279+
E->Flags = E_LOC_STATIC | E_RTYPE_LVAL;
12811280
E->Name = Sym->V.Offs;
12821281
}
12831282

@@ -1311,7 +1310,7 @@ static void Primary (ExprDesc* E)
13111310
} else {
13121311
Warning ("Call to undeclared function '%s'", Ident);
13131312
}
1314-
Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_EXTERN | SC_REF | SC_FUNC);
1313+
Sym = AddGlobalSym (Ident, GetImplicitFuncType(), SC_REF | SC_FUNC);
13151314
E->Type = Sym->Type;
13161315
E->Flags = E_LOC_GLOBAL | E_RTYPE_RVAL;
13171316
E->Name = (uintptr_t) Sym->Name;

src/cc65/exprdesc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ enum {
9898
E_LOC_NONE = 0x0000, /* Pure rvalue with no storage */
9999
E_LOC_ABS = 0x0001, /* Absolute numeric addressed variable */
100100
E_LOC_GLOBAL = 0x0002, /* Global variable */
101-
E_LOC_STATIC = 0x0004, /* Static variable */
101+
E_LOC_STATIC = 0x0004, /* Local static variable */
102102
E_LOC_REGISTER = 0x0008, /* Register variable */
103103
E_LOC_STACK = 0x0010, /* Value on the stack */
104104
E_LOC_PRIMARY = 0x0020, /* Temporary in primary register */

src/cc65/goto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void GotoStatement (void)
9292

9393
/* Find array size */
9494
if (!IsTypeArray (arr->Type) || SizeOf (arr->Type) == 0 ||
95-
!(arr->Flags & SC_STATIC) ||
95+
(arr->Flags & SC_STORAGEMASK) != SC_STATIC ||
9696
SizeOf (GetElementType(arr->Type)) != 2) {
9797
Error ("Expected a static array");
9898
} else if (GetElementCount (arr->Type) > 127) {

0 commit comments

Comments
 (0)