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 Warning ("Parameter `%s' is never used", Entry->Name);
169 Warning ("`%s' is defined but never used", Entry->Name);
174 /* If the entry is a label, check if it was defined in the function */
175 if (Flags & SC_LABEL) {
176 if (!SymIsDef (Entry)) {
177 /* Undefined label */
178 Error ("Undefined label: `%s'", Entry->Name);
179 } else if (!SymIsRef (Entry)) {
180 /* Defined but not used */
181 Warning ("`%s' is defined but never used", Entry->Name);
188 Entry = Entry->NextSym;
194 /*****************************************************************************/
195 /* Handling of lexical levels */
196 /*****************************************************************************/
200 unsigned GetLexicalLevel (void)
201 /* Return the current lexical level */
208 void EnterGlobalLevel (void)
209 /* Enter the program global lexical level */
212 PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
214 /* Create and assign the symbol table */
215 SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
217 /* Create and assign the tag table */
218 TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
223 void LeaveGlobalLevel (void)
224 /* Leave the program global lexical level */
227 PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
229 /* Check the tables */
230 CheckSymTable (SymTab0);
232 /* Dump the tables if requested */
234 PrintSymTable (SymTab0, stdout, "Global symbol table");
235 PrintSymTable (TagTab0, stdout, "Global tag table");
238 /* Don't delete the symbol and struct tables! */
245 void EnterFunctionLevel (void)
246 /* Enter function lexical level */
250 /* New lexical level */
253 /* Get a new symbol table and make it current */
254 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
258 /* Get a new tag table and make it current */
259 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
263 /* Create and assign a new label table */
264 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
269 void RememberFunctionLevel (struct FuncDesc* F)
270 /* Remember the symbol tables for the level and leave the level without checks */
272 /* Leave the lexical level */
275 /* Remember the tables */
279 /* Don't delete the tables */
280 SymTab = SymTab->PrevTab;
281 TagTab = TagTab->PrevTab;
286 void ReenterFunctionLevel (struct FuncDesc* F)
287 /* Reenter the function lexical level using the existing tables from F */
289 /* New lexical level */
292 /* Make the tables current again */
293 F->SymTab->PrevTab = SymTab;
296 F->TagTab->PrevTab = TagTab;
299 /* Create and assign a new label table */
300 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
305 void LeaveFunctionLevel (void)
306 /* Leave function lexical level */
308 /* Leave the lexical level */
311 /* Check the tables */
312 CheckSymTable (SymTab);
313 CheckSymTable (LabelTab);
315 /* Drop the label table if it is empty */
316 if (LabelTab->SymCount == 0) {
317 FreeSymTable (LabelTab);
320 /* Don't delete the tables */
321 SymTab = SymTab->PrevTab;
322 TagTab = TagTab->PrevTab;
328 void EnterBlockLevel (void)
329 /* Enter a nested block in a function */
333 /* New lexical level */
336 /* Get a new symbol table and make it current */
337 S = NewSymTable (SYMTAB_SIZE_BLOCK);
341 /* Get a new tag table and make it current */
342 S = NewSymTable (SYMTAB_SIZE_BLOCK);
349 void LeaveBlockLevel (void)
350 /* Leave a nested block in a function */
352 /* Leave the lexical level */
355 /* Check the tables */
356 CheckSymTable (SymTab);
358 /* Don't delete the tables */
359 SymTab = SymTab->PrevTab;
360 TagTab = TagTab->PrevTab;
365 void EnterStructLevel (void)
366 /* Enter a nested block for a struct definition */
370 /* Get a new symbol table and make it current. Note: Structs and enums
371 * nested in struct scope are NOT local to the struct but visible in the
372 * outside scope. So we will NOT create a new struct or enum table.
374 S = NewSymTable (SYMTAB_SIZE_BLOCK);
381 void LeaveStructLevel (void)
382 /* Leave a nested block for a struct definition */
384 /* Don't delete the table */
385 SymTab = SymTab->PrevTab;
390 /*****************************************************************************/
392 /*****************************************************************************/
396 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
397 /* Search for an entry in one table */
399 /* Get the start of the hash chain */
400 SymEntry* E = T->Tab [Hash % T->Size];
402 /* Compare the name */
403 if (strcmp (E->Name, Name) == 0) {
407 /* Not found, next entry in hash chain */
417 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
418 /* Find the symbol with the given name in the table tree that starts with T */
420 /* Get the hash over the name */
421 unsigned Hash = HashStr (Name);
423 /* Check all symbol tables for the symbol */
425 /* Try to find the symbol in this table */
426 SymEntry* E = FindSymInTable (Tab, Name, Hash);
428 /* Bail out if we found it */
433 /* Repeat the search in the next higher lexical level */
443 SymEntry* FindSym (const char* Name)
444 /* Find the symbol with the given name */
446 return FindSymInTree (SymTab, Name);
451 SymEntry* FindGlobalSym (const char* Name)
452 /* Find the symbol with the given name in the global symbol table only */
454 return FindSymInTable (SymTab0, Name, HashStr (Name));
459 SymEntry* FindLocalSym (const char* Name)
460 /* Find the symbol with the given name in the current symbol table only */
462 return FindSymInTable (SymTab, Name, HashStr (Name));
467 SymEntry* FindTagSym (const char* Name)
468 /* Find the symbol with the given name in the tag table */
470 return FindSymInTree (TagTab, Name);
475 SymEntry* FindStructField (const Type* T, const char* Name)
476 /* Find a struct field in the fields list */
480 /* The given type may actually be a pointer to struct */
485 /* Non-structs do not have any struct fields... */
486 if (IsClassStruct (T)) {
488 /* Get a pointer to the struct/union type */
489 const SymEntry* Struct = GetSymEntry (T);
492 /* Now search in the struct symbol table. Beware: The table may not
495 if (Struct->V.S.SymTab) {
496 Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
505 /*****************************************************************************/
506 /* Add stuff to the symbol table */
507 /*****************************************************************************/
511 static void AddSymEntry (SymTable* T, SymEntry* S)
512 /* Add a symbol to a symbol table */
514 /* Get the hash value for the name */
515 unsigned Hash = HashStr (S->Name) % T->Size;
517 /* Insert the symbol into the list of all symbols in this level */
519 T->SymTail->NextSym = S;
521 S->PrevSym = T->SymTail;
523 if (T->SymHead == 0) {
529 /* Insert the symbol into the hash chain */
530 S->NextHash = T->Tab[Hash];
533 /* Tell the symbol in which table it is */
539 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
540 /* Add a struct/union entry and return it */
542 /* Do we have an entry with this name already? */
543 SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
546 /* We do have an entry. This may be a forward, so check it. */
547 if ((Entry->Flags & SC_STRUCT) == 0) {
548 /* Existing symbol is not a struct */
549 Error ("Symbol `%s' is already different kind", Name);
550 } else if (Size > 0 && Entry->V.S.Size > 0) {
551 /* Both structs are definitions. */
552 Error ("Multiple definition for `%s'", Name);
554 /* Define the struct size if it is given */
556 Entry->V.S.SymTab = Tab;
557 Entry->V.S.Size = Size;
563 /* Create a new entry */
564 Entry = NewSymEntry (Name, SC_STRUCT);
566 /* Set the struct data */
567 Entry->V.S.SymTab = Tab;
568 Entry->V.S.Size = Size;
570 /* Add it to the current table */
571 AddSymEntry (TagTab, Entry);
574 /* Return the entry */
580 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
581 /* Add a bit field to the local symbol table and return the symbol entry */
583 /* Do we have an entry with this name already? */
584 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
587 /* We have a symbol with this name already */
588 Error ("Multiple definition for `%s'", Name);
592 /* Create a new entry */
593 Entry = NewSymEntry (Name, SC_BITFIELD);
595 /* Set the symbol attributes. Bit-fields are always of type unsigned */
596 Entry->Type = type_uint;
597 Entry->V.B.Offs = Offs;
598 Entry->V.B.BitOffs = BitOffs;
599 Entry->V.B.BitWidth = Width;
601 /* Add the entry to the symbol table */
602 AddSymEntry (SymTab, Entry);
606 /* Return the entry */
612 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
613 /* Add an constant symbol to the symbol table and return it */
615 /* Enums must be inserted in the global symbol table */
616 SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
618 /* Do we have an entry with this name already? */
619 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
621 if ((Entry->Flags & SC_CONST) != SC_CONST) {
622 Error ("Symbol `%s' is already different kind", Name);
624 Error ("Multiple definition for `%s'", Name);
629 /* Create a new entry */
630 Entry = NewSymEntry (Name, Flags);
632 /* Enum values are ints */
633 Entry->Type = TypeDup (T);
635 /* Set the enum data */
636 Entry->V.ConstVal = Val;
638 /* Add the entry to the symbol table */
639 AddSymEntry (Tab, Entry);
641 /* Return the entry */
647 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
648 /* Add a goto label to the label table */
650 /* Do we have an entry with this name already? */
651 SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
654 if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
655 /* Trying to define the label more than once */
656 Error ("Label `%s' is defined more than once", Name);
658 Entry->Flags |= Flags;
662 /* Create a new entry */
663 Entry = NewSymEntry (Name, SC_LABEL | Flags);
665 /* Set a new label number */
666 Entry->V.Label = GetLocalLabel ();
668 /* Generate the assembler name of the label */
669 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
671 /* Add the entry to the label table */
672 AddSymEntry (LabelTab, Entry);
676 /* Return the entry */
682 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
683 /* Add a local symbol and return the symbol entry */
685 /* Do we have an entry with this name already? */
686 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
689 /* We have a symbol with this name already */
690 Error ("Multiple definition for `%s'", Name);
694 /* Create a new entry */
695 Entry = NewSymEntry (Name, Flags);
697 /* Set the symbol attributes */
698 Entry->Type = TypeDup (T);
699 if ((Flags & SC_AUTO) == SC_AUTO) {
700 Entry->V.Offs = Offs;
701 } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
702 Entry->V.R.RegOffs = Offs;
703 Entry->V.R.SaveOffs = StackPtr;
704 } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
705 Entry->V.Label = Offs;
706 } else if ((Flags & SC_STATIC) == SC_STATIC) {
707 /* Generate the assembler name from the label number */
708 Entry->V.Label = Offs;
709 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
710 } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
711 Entry->V.Offs = Offs;
713 Internal ("Invalid flags in AddLocalSym: %04X", Flags);
716 /* Add the entry to the symbol table */
717 AddSymEntry (SymTab, Entry);
721 /* Return the entry */
727 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
728 /* Add an external or global symbol to the symbol table and return the entry */
730 /* There is some special handling for functions, so check if it is one */
731 int IsFunc = IsTypeFunc (T);
733 /* Functions must be inserted in the global symbol table */
734 SymTable* Tab = IsFunc? SymTab0 : SymTab;
736 /* Do we have an entry with this name already? */
737 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
742 /* We have a symbol with this name already */
743 if (Entry->Flags & SC_TYPE) {
744 Error ("Multiple definition for `%s'", Name);
748 /* Get the type string of the existing symbol */
751 /* If we are handling arrays, the old entry or the new entry may be an
752 * incomplete declaration. Accept this, and if the exsting entry is
753 * incomplete, complete it.
755 if (IsTypeArray (T) && IsTypeArray (EType)) {
757 /* Get the array sizes */
758 long Size = GetElementCount (T);
759 long ESize = GetElementCount (EType);
761 if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
762 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
763 /* Types not identical: Conflicting types */
764 Error ("Conflicting types for `%s'", Name);
767 /* Check if we have a size in the existing definition */
768 if (ESize == UNSPECIFIED) {
769 /* Existing, size not given, use size from new def */
770 SetElementCount (EType, Size);
775 /* New type must be identical */
776 if (TypeCmp (EType, T) < TC_EQUAL) {
777 Error ("Conflicting types for `%s'", Name);
781 /* In case of a function, use the new type descriptor, since it
782 * contains pointers to the new symbol tables that are needed if
783 * an actual function definition follows. Be sure not to use the
784 * new descriptor if it contains a function declaration with an
785 * empty parameter list.
788 /* Get the function descriptor from the new type */
789 FuncDesc* F = GetFuncDesc (T);
790 /* Use this new function descriptor if it doesn't contain
791 * an empty parameter list.
793 if ((F->Flags & FD_EMPTY) == 0) {
795 SetFuncDesc (EType, F);
800 /* Add the new flags */
801 Entry->Flags |= Flags;
807 /* Create a new entry */
808 Entry = NewSymEntry (Name, Flags);
810 /* Set the symbol attributes */
811 Entry->Type = TypeDup (T);
813 /* If this is a function, set the function descriptor and clear
817 Entry->V.F.Func = GetFuncDesc (Entry->Type);
821 /* Add the assembler name of the symbol */
823 Entry->AsmName = xmalloc (Len + 2);
824 Entry->AsmName[0] = '_';
825 memcpy (Entry->AsmName+1, Name, Len+1);
827 /* Add the entry to the symbol table */
828 AddSymEntry (Tab, Entry);
831 /* Return the entry */
837 /*****************************************************************************/
839 /*****************************************************************************/
843 SymTable* GetSymTab (void)
844 /* Return the current symbol table */
851 SymTable* GetGlobalSymTab (void)
852 /* Return the global symbol table */
859 int SymIsLocal (SymEntry* Sym)
860 /* Return true if the symbol is defined in the highest lexical level */
862 return (Sym->Owner == SymTab || Sym->Owner == TagTab);
867 void MakeZPSym (const char* Name)
868 /* Mark the given symbol as zero page symbol */
870 /* Get the symbol table entry */
871 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
873 /* Mark the symbol as zeropage */
875 Entry->Flags |= SC_ZEROPAGE;
877 Error ("Undefined symbol: `%s'", Name);
883 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
884 /* Write the symbol table to the given file */
887 const SymEntry* Entry;
889 /* Print the header */
891 va_start (ap, Header);
893 Len = vfprintf (F, Header, ap);
897 /* Underline the header */
904 Entry = Tab->SymHead;
906 fprintf (F, "(empty)\n");
909 DumpSymEntry (F, Entry);
910 Entry = Entry->NextSym;
913 fprintf (F, "\n\n\n");
918 void EmitExternals (void)
919 /* Write import/export statements for external symbols */
923 Entry = SymTab->SymHead;
925 unsigned Flags = Entry->Flags;
926 if (Flags & SC_EXTERN) {
927 /* Only defined or referenced externs */
928 if (SymIsRef (Entry) && !SymIsDef (Entry)) {
930 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
931 } else if (SymIsDef (Entry)) {
933 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
936 Entry = Entry->NextSym;