1 /*****************************************************************************/
5 /* Symbol table entries for the cc65 C compiler */
9 /* (C) 2000-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 SymEntry* NewSymEntry (const char* Name, unsigned Flags)
56 /* Create a new symbol table with the given name */
58 /* Get the length of the name */
59 unsigned Len = strlen (Name);
61 /* Allocate memory for the symbol entry */
62 SymEntry* E = xmalloc (sizeof (SymEntry) + Len);
64 /* Initialize the entry */
74 memcpy (E->Name, Name, Len+1);
76 /* Return the new entry */
82 void FreeSymEntry (SymEntry* E)
83 /* Free a symbol entry */
92 void DumpSymEntry (FILE* F, const SymEntry* E)
93 /* Dump the given symbol table entry to the file in readable form */
99 /* Beware: Order is important! */
100 { "SC_TYPEDEF", SC_TYPEDEF },
101 { "SC_BITFIELD", SC_BITFIELD },
102 { "SC_STRUCTFIELD", SC_STRUCTFIELD },
103 { "SC_STRUCT", SC_STRUCT },
104 { "SC_AUTO", SC_AUTO },
105 { "SC_REGISTER", SC_REGISTER },
106 { "SC_STATIC", SC_STATIC },
107 { "SC_EXTERN", SC_EXTERN },
108 { "SC_ENUM", SC_ENUM },
109 { "SC_CONST", SC_CONST },
110 { "SC_LABEL", SC_LABEL },
111 { "SC_PARAM", SC_PARAM },
112 { "SC_FUNC", SC_FUNC },
113 { "SC_STORAGE", SC_STORAGE },
114 { "SC_DEF", SC_DEF },
115 { "SC_REF", SC_REF },
116 { "SC_ZEROPAGE", SC_ZEROPAGE },
123 fprintf (F, "%s:\n", E->Name);
125 /* Print the assembler name if we have one */
127 fprintf (F, " AsmName: %s\n", E->AsmName);
130 /* Print the flags */
132 fprintf (F, " Flags: ");
133 for (I = 0; I < sizeof (Flags) / sizeof (Flags[0]) && SymFlags != 0; ++I) {
134 if ((SymFlags & Flags[I].Val) == Flags[I].Val) {
135 SymFlags &= ~Flags[I].Val;
136 fprintf (F, "%s ", Flags[I].Name);
140 fprintf (F, "%04X", SymFlags);
145 fprintf (F, " Type: ");
147 PrintType (F, E->Type);
149 fprintf (F, "(none)");
156 const DeclAttr* SymGetAttr (const SymEntry* Sym, DeclAttrType AttrType)
157 /* Return an attribute for this symbol or NULL if the attribute does not exist */
159 /* Beware: We may not even have a collection */
162 for (I = 0; I < CollCount (Sym->Attr); ++I) {
164 /* Get the next attribute */
165 const DeclAttr* A = CollConstAt (Sym->Attr, I);
167 /* If this is the one we're searching for, return it */
168 if (A->AttrType == AttrType) {
180 void SymUseAttr (SymEntry* Sym, struct Declaration* D)
181 /* Use the attributes from the declaration for this symbol */
183 /* We cannot specify attributes twice */
184 if ((Sym->Flags & SC_HAVEATTR) != 0) {
185 if (D->Attributes != 0) {
186 Error ("Attributes must be specified in the first declaration");
191 /* Move the attributes */
192 Sym->Attr = D->Attributes;
194 Sym->Flags |= SC_HAVEATTR;
199 void CvtRegVarToAuto (SymEntry* Sym)
200 /* Convert a register variable to an auto variable */
202 /* Change the storage class */
203 Sym->Flags = (Sym->Flags & ~(SC_REGISTER | SC_STATIC | SC_EXTERN)) | SC_AUTO;
205 /* Transfer the stack offset from register save area to actual offset */
206 Sym->V.Offs = Sym->V.R.SaveOffs;
211 void ChangeSymType (SymEntry* Entry, Type* T)
212 /* Change the type of the given symbol */
214 TypeFree (Entry->Type);
215 Entry->Type = TypeDup (T);
220 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName)
221 /* Change the assembler name of the symbol */
223 xfree (Entry->AsmName);
224 Entry->AsmName = xstrdup (NewAsmName);
229 int HasAnonName (const SymEntry* Entry)
230 /* Return true if the symbol entry has an anonymous name */
232 return IsAnonName (Entry->Name);