1 /*****************************************************************************/
5 /* Symbol table management 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 /*****************************************************************************/
43 #include "debugflag.h"
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* An empty symbol table */
70 SymTable EmptySymTab = {
79 /* Symbol table sizes */
80 #define SYMTAB_SIZE_GLOBAL 211U
81 #define SYMTAB_SIZE_FUNCTION 29U
82 #define SYMTAB_SIZE_BLOCK 13U
83 #define SYMTAB_SIZE_STRUCT 19U
84 #define SYMTAB_SIZE_LABEL 7U
86 /* The current and root symbol tables */
87 static unsigned LexicalLevel = 0; /* For safety checks */
88 static SymTable* SymTab0 = 0;
89 static SymTable* SymTab = 0;
90 static SymTable* TagTab0 = 0;
91 static SymTable* TagTab = 0;
92 static SymTable* LabelTab = 0;
96 /*****************************************************************************/
98 /*****************************************************************************/
102 static SymTable* NewSymTable (unsigned Size)
103 /* Create and return a symbol table for the given lexical level */
107 /* Allocate memory for the table */
108 SymTable* S = xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*));
110 /* Initialize the symbol table structure */
116 for (I = 0; I < Size; ++I) {
120 /* Return the symbol table */
126 static void FreeSymTable (SymTable* S)
127 /* Free the given symbo table including all symbols */
129 /* Free all symbols */
130 SymEntry* Sym = S->SymHead;
132 SymEntry* NextSym = Sym->NextSym;
137 /* Free the table itself */
143 /*****************************************************************************/
144 /* Check symbols in a table */
145 /*****************************************************************************/
149 static void CheckSymTable (SymTable* Tab)
150 /* Check a symbol table for open references, unused symbols ... */
152 SymEntry* Entry = Tab->SymHead;
155 /* Get the storage flags for tne entry */
156 unsigned Flags = Entry->Flags;
158 /* Ignore typedef entries */
159 if (!SymIsTypeDef (Entry)) {
161 /* Check if the symbol is one with storage, and it if it was
162 * defined but not used.
164 if (((Flags & SC_AUTO) || (Flags & SC_STATIC)) && (Flags & SC_EXTERN) == 0) {
165 if (SymIsDef (Entry) && !SymIsRef (Entry)) {
166 if (Flags & SC_PARAM) {
167 if (IS_Get (&WarnUnusedParam)) {
168 Warning ("Parameter `%s' is never used", Entry->Name);
171 if (IS_Get (&WarnUnusedVar)) {
172 Warning ("`%s' is defined but never used", Entry->Name);
178 /* If the entry is a label, check if it was defined in the function */
179 if (Flags & SC_LABEL) {
180 if (!SymIsDef (Entry)) {
181 /* Undefined label */
182 Error ("Undefined label: `%s'", Entry->Name);
183 } else if (!SymIsRef (Entry)) {
184 /* Defined but not used */
185 if (IS_Get (&WarnUnusedLabel)) {
186 Warning ("`%s' is defined but never used", Entry->Name);
194 Entry = Entry->NextSym;
200 /*****************************************************************************/
201 /* Handling of lexical levels */
202 /*****************************************************************************/
206 unsigned GetLexicalLevel (void)
207 /* Return the current lexical level */
214 void EnterGlobalLevel (void)
215 /* Enter the program global lexical level */
218 PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
220 /* Create and assign the symbol table */
221 SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
223 /* Create and assign the tag table */
224 TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
229 void LeaveGlobalLevel (void)
230 /* Leave the program global lexical level */
233 PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
235 /* Check the tables */
236 CheckSymTable (SymTab0);
238 /* Dump the tables if requested */
240 PrintSymTable (SymTab0, stdout, "Global symbol table");
241 PrintSymTable (TagTab0, stdout, "Global tag table");
244 /* Don't delete the symbol and struct tables! */
251 void EnterFunctionLevel (void)
252 /* Enter function lexical level */
256 /* New lexical level */
259 /* Get a new symbol table and make it current */
260 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
264 /* Get a new tag table and make it current */
265 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
269 /* Create and assign a new label table */
270 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
275 void RememberFunctionLevel (struct FuncDesc* F)
276 /* Remember the symbol tables for the level and leave the level without checks */
278 /* Leave the lexical level */
281 /* Remember the tables */
285 /* Don't delete the tables */
286 SymTab = SymTab->PrevTab;
287 TagTab = TagTab->PrevTab;
292 void ReenterFunctionLevel (struct FuncDesc* F)
293 /* Reenter the function lexical level using the existing tables from F */
295 /* New lexical level */
298 /* Make the tables current again */
299 F->SymTab->PrevTab = SymTab;
302 F->TagTab->PrevTab = TagTab;
305 /* Create and assign a new label table */
306 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
311 void LeaveFunctionLevel (void)
312 /* Leave function lexical level */
314 /* Leave the lexical level */
317 /* Check the tables */
318 CheckSymTable (SymTab);
319 CheckSymTable (LabelTab);
321 /* Drop the label table if it is empty */
322 if (LabelTab->SymCount == 0) {
323 FreeSymTable (LabelTab);
326 /* Don't delete the tables */
327 SymTab = SymTab->PrevTab;
328 TagTab = TagTab->PrevTab;
334 void EnterBlockLevel (void)
335 /* Enter a nested block in a function */
339 /* New lexical level */
342 /* Get a new symbol table and make it current */
343 S = NewSymTable (SYMTAB_SIZE_BLOCK);
347 /* Get a new tag table and make it current */
348 S = NewSymTable (SYMTAB_SIZE_BLOCK);
355 void LeaveBlockLevel (void)
356 /* Leave a nested block in a function */
358 /* Leave the lexical level */
361 /* Check the tables */
362 CheckSymTable (SymTab);
364 /* Don't delete the tables */
365 SymTab = SymTab->PrevTab;
366 TagTab = TagTab->PrevTab;
371 void EnterStructLevel (void)
372 /* Enter a nested block for a struct definition */
376 /* Get a new symbol table and make it current. Note: Structs and enums
377 * nested in struct scope are NOT local to the struct but visible in the
378 * outside scope. So we will NOT create a new struct or enum table.
380 S = NewSymTable (SYMTAB_SIZE_BLOCK);
387 void LeaveStructLevel (void)
388 /* Leave a nested block for a struct definition */
390 /* Don't delete the table */
391 SymTab = SymTab->PrevTab;
396 /*****************************************************************************/
398 /*****************************************************************************/
402 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
403 /* Search for an entry in one table */
405 /* Get the start of the hash chain */
406 SymEntry* E = T->Tab [Hash % T->Size];
408 /* Compare the name */
409 if (strcmp (E->Name, Name) == 0) {
413 /* Not found, next entry in hash chain */
423 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
424 /* Find the symbol with the given name in the table tree that starts with T */
426 /* Get the hash over the name */
427 unsigned Hash = HashStr (Name);
429 /* Check all symbol tables for the symbol */
431 /* Try to find the symbol in this table */
432 SymEntry* E = FindSymInTable (Tab, Name, Hash);
434 /* Bail out if we found it */
439 /* Repeat the search in the next higher lexical level */
449 SymEntry* FindSym (const char* Name)
450 /* Find the symbol with the given name */
452 return FindSymInTree (SymTab, Name);
457 SymEntry* FindGlobalSym (const char* Name)
458 /* Find the symbol with the given name in the global symbol table only */
460 return FindSymInTable (SymTab0, Name, HashStr (Name));
465 SymEntry* FindLocalSym (const char* Name)
466 /* Find the symbol with the given name in the current symbol table only */
468 return FindSymInTable (SymTab, Name, HashStr (Name));
473 SymEntry* FindTagSym (const char* Name)
474 /* Find the symbol with the given name in the tag table */
476 return FindSymInTree (TagTab, Name);
481 SymEntry* FindStructField (const Type* T, const char* Name)
482 /* Find a struct field in the fields list */
486 /* The given type may actually be a pointer to struct */
491 /* Non-structs do not have any struct fields... */
492 if (IsClassStruct (T)) {
494 /* Get a pointer to the struct/union type */
495 const SymEntry* Struct = GetSymEntry (T);
498 /* Now search in the struct symbol table. Beware: The table may not
501 if (Struct->V.S.SymTab) {
502 Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
511 /*****************************************************************************/
512 /* Add stuff to the symbol table */
513 /*****************************************************************************/
517 static void AddSymEntry (SymTable* T, SymEntry* S)
518 /* Add a symbol to a symbol table */
520 /* Get the hash value for the name */
521 unsigned Hash = HashStr (S->Name) % T->Size;
523 /* Insert the symbol into the list of all symbols in this level */
525 T->SymTail->NextSym = S;
527 S->PrevSym = T->SymTail;
529 if (T->SymHead == 0) {
535 /* Insert the symbol into the hash chain */
536 S->NextHash = T->Tab[Hash];
539 /* Tell the symbol in which table it is */
545 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
546 /* Add a struct/union entry and return it */
548 /* Do we have an entry with this name already? */
549 SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
552 /* We do have an entry. This may be a forward, so check it. */
553 if ((Entry->Flags & SC_STRUCT) == 0) {
554 /* Existing symbol is not a struct */
555 Error ("Symbol `%s' is already different kind", Name);
556 } else if (Size > 0 && Entry->V.S.Size > 0) {
557 /* Both structs are definitions. */
558 Error ("Multiple definition for `%s'", Name);
560 /* Define the struct size if it is given */
562 Entry->V.S.SymTab = Tab;
563 Entry->V.S.Size = Size;
569 /* Create a new entry */
570 Entry = NewSymEntry (Name, SC_STRUCT);
572 /* Set the struct data */
573 Entry->V.S.SymTab = Tab;
574 Entry->V.S.Size = Size;
576 /* Add it to the current table */
577 AddSymEntry (TagTab, Entry);
580 /* Return the entry */
586 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
587 /* Add a bit field to the local symbol table and return the symbol entry */
589 /* Do we have an entry with this name already? */
590 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
593 /* We have a symbol with this name already */
594 Error ("Multiple definition for `%s'", Name);
598 /* Create a new entry */
599 Entry = NewSymEntry (Name, SC_BITFIELD);
601 /* Set the symbol attributes. Bit-fields are always of type unsigned */
602 Entry->Type = type_uint;
603 Entry->V.B.Offs = Offs;
604 Entry->V.B.BitOffs = BitOffs;
605 Entry->V.B.BitWidth = Width;
607 /* Add the entry to the symbol table */
608 AddSymEntry (SymTab, Entry);
612 /* Return the entry */
618 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
619 /* Add an constant symbol to the symbol table and return it */
621 /* Enums must be inserted in the global symbol table */
622 SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
624 /* Do we have an entry with this name already? */
625 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
627 if ((Entry->Flags & SC_CONST) != SC_CONST) {
628 Error ("Symbol `%s' is already different kind", Name);
630 Error ("Multiple definition for `%s'", Name);
635 /* Create a new entry */
636 Entry = NewSymEntry (Name, Flags);
638 /* Enum values are ints */
639 Entry->Type = TypeDup (T);
641 /* Set the enum data */
642 Entry->V.ConstVal = Val;
644 /* Add the entry to the symbol table */
645 AddSymEntry (Tab, Entry);
647 /* Return the entry */
653 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
654 /* Add a goto label to the label table */
656 /* Do we have an entry with this name already? */
657 SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
660 if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
661 /* Trying to define the label more than once */
662 Error ("Label `%s' is defined more than once", Name);
664 Entry->Flags |= Flags;
668 /* Create a new entry */
669 Entry = NewSymEntry (Name, SC_LABEL | Flags);
671 /* Set a new label number */
672 Entry->V.Label = GetLocalLabel ();
674 /* Generate the assembler name of the label */
675 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
677 /* Add the entry to the label table */
678 AddSymEntry (LabelTab, Entry);
682 /* Return the entry */
688 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
689 /* Add a local symbol and return the symbol entry */
691 /* Do we have an entry with this name already? */
692 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
695 /* We have a symbol with this name already */
696 Error ("Multiple definition for `%s'", Name);
700 /* Create a new entry */
701 Entry = NewSymEntry (Name, Flags);
703 /* Set the symbol attributes */
704 Entry->Type = TypeDup (T);
705 if ((Flags & SC_AUTO) == SC_AUTO) {
706 Entry->V.Offs = Offs;
707 } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
708 Entry->V.R.RegOffs = Offs;
709 Entry->V.R.SaveOffs = StackPtr;
710 } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
711 Entry->V.Label = Offs;
712 } else if ((Flags & SC_STATIC) == SC_STATIC) {
713 /* Generate the assembler name from the label number */
714 Entry->V.Label = Offs;
715 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
716 } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
717 Entry->V.Offs = Offs;
719 Internal ("Invalid flags in AddLocalSym: %04X", Flags);
722 /* Add the entry to the symbol table */
723 AddSymEntry (SymTab, Entry);
727 /* Return the entry */
733 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
734 /* Add an external or global symbol to the symbol table and return the entry */
736 /* There is some special handling for functions, so check if it is one */
737 int IsFunc = IsTypeFunc (T);
739 /* Functions must be inserted in the global symbol table */
740 SymTable* Tab = IsFunc? SymTab0 : SymTab;
742 /* Do we have an entry with this name already? */
743 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
748 /* We have a symbol with this name already */
749 if (Entry->Flags & SC_TYPE) {
750 Error ("Multiple definition for `%s'", Name);
754 /* Get the type string of the existing symbol */
757 /* If we are handling arrays, the old entry or the new entry may be an
758 * incomplete declaration. Accept this, and if the exsting entry is
759 * incomplete, complete it.
761 if (IsTypeArray (T) && IsTypeArray (EType)) {
763 /* Get the array sizes */
764 long Size = GetElementCount (T);
765 long ESize = GetElementCount (EType);
767 if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
768 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
769 /* Types not identical: Conflicting types */
770 Error ("Conflicting types for `%s'", Name);
773 /* Check if we have a size in the existing definition */
774 if (ESize == UNSPECIFIED) {
775 /* Existing, size not given, use size from new def */
776 SetElementCount (EType, Size);
781 /* New type must be identical */
782 if (TypeCmp (EType, T) < TC_EQUAL) {
783 Error ("Conflicting types for `%s'", Name);
787 /* In case of a function, use the new type descriptor, since it
788 * contains pointers to the new symbol tables that are needed if
789 * an actual function definition follows. Be sure not to use the
790 * new descriptor if it contains a function declaration with an
791 * empty parameter list.
794 /* Get the function descriptor from the new type */
795 FuncDesc* F = GetFuncDesc (T);
796 /* Use this new function descriptor if it doesn't contain
797 * an empty parameter list.
799 if ((F->Flags & FD_EMPTY) == 0) {
801 SetFuncDesc (EType, F);
806 /* Add the new flags */
807 Entry->Flags |= Flags;
813 /* Create a new entry */
814 Entry = NewSymEntry (Name, Flags);
816 /* Set the symbol attributes */
817 Entry->Type = TypeDup (T);
819 /* If this is a function, set the function descriptor and clear
823 Entry->V.F.Func = GetFuncDesc (Entry->Type);
827 /* Add the assembler name of the symbol */
829 Entry->AsmName = xmalloc (Len + 2);
830 Entry->AsmName[0] = '_';
831 memcpy (Entry->AsmName+1, Name, Len+1);
833 /* Add the entry to the symbol table */
834 AddSymEntry (Tab, Entry);
837 /* Return the entry */
843 /*****************************************************************************/
845 /*****************************************************************************/
849 SymTable* GetSymTab (void)
850 /* Return the current symbol table */
857 SymTable* GetGlobalSymTab (void)
858 /* Return the global symbol table */
865 int SymIsLocal (SymEntry* Sym)
866 /* Return true if the symbol is defined in the highest lexical level */
868 return (Sym->Owner == SymTab || Sym->Owner == TagTab);
873 void MakeZPSym (const char* Name)
874 /* Mark the given symbol as zero page symbol */
876 /* Get the symbol table entry */
877 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
879 /* Mark the symbol as zeropage */
881 Entry->Flags |= SC_ZEROPAGE;
883 Error ("Undefined symbol: `%s'", Name);
889 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
890 /* Write the symbol table to the given file */
893 const SymEntry* Entry;
895 /* Print the header */
897 va_start (ap, Header);
899 Len = vfprintf (F, Header, ap);
903 /* Underline the header */
910 Entry = Tab->SymHead;
912 fprintf (F, "(empty)\n");
915 DumpSymEntry (F, Entry);
916 Entry = Entry->NextSym;
919 fprintf (F, "\n\n\n");
924 void EmitExternals (void)
925 /* Write import/export statements for external symbols */
929 Entry = SymTab->SymHead;
931 unsigned Flags = Entry->Flags;
932 if (Flags & SC_EXTERN) {
933 /* Only defined or referenced externs */
934 if (SymIsRef (Entry) && !SymIsDef (Entry)) {
936 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
937 } else if (SymIsDef (Entry)) {
939 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
942 Entry = Entry->NextSym;