1 /*****************************************************************************/
5 /* Symbol table management for the cc65 C compiler */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* An empty symbol table */
67 SymTable EmptySymTab = {
76 /* Symbol table sizes */
77 #define SYMTAB_SIZE_GLOBAL 211U
78 #define SYMTAB_SIZE_FUNCTION 29U
79 #define SYMTAB_SIZE_BLOCK 13U
80 #define SYMTAB_SIZE_STRUCT 19U
81 #define SYMTAB_SIZE_LABEL 7U
83 /* Predefined lexical levels */
84 #define LEX_LEVEL_GLOBAL 1U
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 ((Flags & SC_TYPEDEF) != SC_TYPEDEF) {
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 ((Flags & SC_DEF) && !(Flags & SC_REF)) {
166 if (Flags & SC_PARAM) {
167 Warning (WARN_UNUSED_PARM, Entry->Name);
169 Warning (WARN_UNUSED_ITEM, Entry->Name);
174 /* If the entry is a label, check if it was defined in the function */
175 if (Flags & SC_LABEL) {
176 if ((Flags & SC_DEF) == 0) {
177 /* Undefined label */
178 Error (ERR_UNDEFINED_LABEL, Entry->Name);
179 } else if ((Flags & SC_REF) == 0) {
180 /* Defined but not used */
181 Warning (WARN_UNUSED_ITEM, Entry->Name);
188 Entry = Entry->NextSym;
194 /*****************************************************************************/
195 /* Handling of lexical levels */
196 /*****************************************************************************/
200 void EnterGlobalLevel (void)
201 /* Enter the program global lexical level */
204 PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
206 /* Create and assign the symbol table */
207 SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
209 /* Create and assign the tag table */
210 TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
215 void LeaveGlobalLevel (void)
216 /* Leave the program global lexical level */
219 PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
221 /* Check the tables */
222 CheckSymTable (SymTab0);
224 /* Dump the tables if requested */
226 PrintSymTable (SymTab0, stdout, "Global symbol table");
227 PrintSymTable (TagTab0, stdout, "Global tag table");
230 /* Don't delete the symbol and struct tables! */
231 SymTab0 = SymTab = 0;
232 TagTab0 = TagTab = 0;
237 void EnterFunctionLevel (void)
238 /* Enter function lexical level */
242 /* New lexical level */
245 /* Get a new symbol table and make it current */
246 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
250 /* Get a new tag table and make it current */
251 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
255 /* Create and assign a new label table */
256 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
261 void RememberFunctionLevel (struct FuncDesc* F)
262 /* Remember the symbol tables for the level and leave the level without checks */
264 /* Leave the lexical level */
267 /* Remember the tables */
271 /* Don't delete the tables */
272 SymTab = SymTab->PrevTab;
273 TagTab = TagTab->PrevTab;
278 void ReenterFunctionLevel (struct FuncDesc* F)
279 /* Reenter the function lexical level using the existing tables from F */
281 /* New lexical level */
284 /* Make the tables current again */
285 F->SymTab->PrevTab = SymTab;
288 F->TagTab->PrevTab = TagTab;
291 /* Create and assign a new label table */
292 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
297 void LeaveFunctionLevel (void)
298 /* Leave function lexical level */
300 /* Leave the lexical level */
303 /* Check the tables */
304 CheckSymTable (SymTab);
305 CheckSymTable (LabelTab);
307 /* Drop the label table if it is empty */
308 if (LabelTab->SymCount == 0) {
309 FreeSymTable (LabelTab);
312 /* Don't delete the tables */
313 SymTab = SymTab->PrevTab;
314 TagTab = TagTab->PrevTab;
320 void EnterBlockLevel (void)
321 /* Enter a nested block in a function */
325 /* New lexical level */
328 /* Get a new symbol table and make it current */
329 S = NewSymTable (SYMTAB_SIZE_BLOCK);
333 /* Get a new tag table and make it current */
334 S = NewSymTable (SYMTAB_SIZE_BLOCK);
341 void LeaveBlockLevel (void)
342 /* Leave a nested block in a function */
344 /* Leave the lexical level */
347 /* Check the tables */
348 CheckSymTable (SymTab);
350 /* Don't delete the tables */
351 SymTab = SymTab->PrevTab;
352 TagTab = TagTab->PrevTab;
357 void EnterStructLevel (void)
358 /* Enter a nested block for a struct definition */
362 /* Get a new symbol table and make it current. Note: Structs and enums
363 * nested in struct scope are NOT local to the struct but visible in the
364 * outside scope. So we will NOT create a new struct or enum table.
366 S = NewSymTable (SYMTAB_SIZE_BLOCK);
373 void LeaveStructLevel (void)
374 /* Leave a nested block for a struct definition */
376 /* Don't delete the table */
377 SymTab = SymTab->PrevTab;
382 /*****************************************************************************/
384 /*****************************************************************************/
388 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
389 /* Search for an entry in one table */
391 /* Get the start of the hash chain */
392 SymEntry* E = T->Tab [Hash % T->Size];
394 /* Compare the name */
395 if (strcmp (E->Name, Name) == 0) {
399 /* Not found, next entry in hash chain */
409 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
410 /* Find the symbol with the given name in the table tree that starts with T */
412 /* Get the hash over the name */
413 unsigned Hash = HashStr (Name);
415 /* Check all symbol tables for the symbol */
417 /* Try to find the symbol in this table */
418 SymEntry* E = FindSymInTable (Tab, Name, Hash);
420 /* Bail out if we found it */
425 /* Repeat the search in the next higher lexical level */
435 SymEntry* FindSym (const char* Name)
436 /* Find the symbol with the given name */
438 return FindSymInTree (SymTab, Name);
443 SymEntry* FindLocalSym (const char* Name)
444 /* Find the symbol with the given name in the current symbol table only */
446 return FindSymInTable (SymTab, Name, HashStr (Name));
451 SymEntry* FindTagSym (const char* Name)
452 /* Find the symbol with the given name in the tag table */
454 return FindSymInTree (TagTab, Name);
459 SymEntry* FindStructField (const type* Type, const char* Name)
460 /* Find a struct field in the fields list */
464 /* The given type may actually be a pointer to struct */
465 if (Type[0] == T_PTR) {
469 /* Non-structs do not have any struct fields... */
470 if (IsStruct (Type)) {
474 /* Get a pointer to the struct/union type */
475 const SymEntry* Struct = (const SymEntry*) Decode (Type+1);
478 /* Get the field symbol table from the struct entry.
479 * Beware: The table may not exist.
481 Tab = Struct->V.S.SymTab;
483 /* Now search in the struct symbol table */
485 Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
494 /*****************************************************************************/
495 /* Add stuff to the symbol table */
496 /*****************************************************************************/
500 static void AddSymEntry (SymTable* T, SymEntry* S)
501 /* Add a symbol to a symbol table */
503 /* Get the hash value for the name */
504 unsigned Hash = HashStr (S->Name) % T->Size;
506 /* Insert the symbol into the list of all symbols in this level */
508 T->SymTail->NextSym = S;
510 S->PrevSym = T->SymTail;
512 if (T->SymHead == 0) {
518 /* Insert the symbol into the hash chain */
519 S->NextHash = T->Tab[Hash];
522 /* Tell the symbol in which table it is */
528 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
529 /* Add a struct/union entry and return it */
531 /* Do we have an entry with this name already? */
532 SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
535 /* We do have an entry. This may be a forward, so check it. */
536 if ((Entry->Flags & SC_STRUCT) == 0) {
537 /* Existing symbol is not a struct */
538 Error (ERR_SYMBOL_KIND);
539 } else if (Size > 0 && Entry->V.S.Size > 0) {
540 /* Both structs are definitions. */
541 Error (ERR_MULTIPLE_DEFINITION, Name);
543 /* Define the struct size if it is given */
545 Entry->V.S.SymTab = Tab;
546 Entry->V.S.Size = Size;
552 /* Create a new entry */
553 Entry = NewSymEntry (Name, SC_STRUCT);
555 /* Set the struct data */
556 Entry->V.S.SymTab = Tab;
557 Entry->V.S.Size = Size;
559 /* Add it to the current table */
560 AddSymEntry (TagTab, Entry);
563 /* Return the entry */
569 SymEntry* AddEnumSym (const char* Name, int Val)
570 /* Add an enum symbol to the symbol table and return it */
572 /* Do we have an entry with this name already? */
573 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
575 if (Entry->Flags != SC_ENUM) {
576 Error (ERR_SYMBOL_KIND);
578 Error (ERR_MULTIPLE_DEFINITION, Name);
583 /* Create a new entry */
584 Entry = NewSymEntry (Name, SC_ENUM);
586 /* Enum values are ints */
587 Entry->Type = TypeDup (type_int);
589 /* Set the enum data */
590 Entry->V.EnumVal = Val;
592 /* Add the entry to the symbol table */
593 AddSymEntry (SymTab, Entry);
595 /* Return the entry */
601 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
602 /* Add a goto label to the label table */
604 /* Do we have an entry with this name already? */
605 SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
608 if ((Entry->Flags & SC_DEF) != 0 && (Flags & SC_DEF) != 0) {
609 /* Trying to define the label more than once */
610 Error (ERR_MULTIPLE_DEFINITION, Name);
612 Entry->Flags |= Flags;
616 /* Create a new entry */
617 Entry = NewSymEntry (Name, SC_LABEL | Flags);
619 /* Set a new label number */
620 Entry->V.Label = GetLabel ();
622 /* Add the entry to the label table */
623 AddSymEntry (LabelTab, Entry);
627 /* Return the entry */
633 SymEntry* AddLocalSym (const char* Name, type* Type, unsigned Flags, int Offs)
634 /* Add a local symbol and return the symbol entry */
636 /* Do we have an entry with this name already? */
637 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
640 /* We have a symbol with this name already */
641 Error (ERR_MULTIPLE_DEFINITION, Name);
645 /* Create a new entry */
646 Entry = NewSymEntry (Name, Flags);
648 /* Set the symbol attributes */
649 Entry->Type = TypeDup (Type);
650 Entry->V.Offs = Offs;
652 /* Add the entry to the symbol table */
653 AddSymEntry (SymTab, Entry);
657 /* Return the entry */
663 SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
664 /* Add an external or global symbol to the symbol table and return the entry */
666 /* Functions must be inserted in the global symbol table */
667 SymTable* Tab = IsFunc (Type)? SymTab0 : SymTab;
669 /* Do we have an entry with this name already? */
670 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
675 /* We have a symbol with this name already */
676 if (Entry->Flags & SC_TYPE) {
677 Error (ERR_MULTIPLE_DEFINITION, Name);
681 /* Get the type string of the existing symbol */
684 /* If we are handling arrays, the old entry or the new entry may be an
685 * incomplete declaration. Accept this, and if the exsting entry is
686 * incomplete, complete it.
688 if (IsArray (Type) && IsArray (EType)) {
690 /* Get the array sizes */
691 unsigned Size = Decode (Type + 1);
692 unsigned ESize = Decode (EType + 1);
694 if ((Size != 0 && ESize != 0) ||
695 TypeCmp (Type+DECODE_SIZE+1, EType+DECODE_SIZE+1) != 0) {
696 /* Types not identical: Duplicate definition */
697 Error (ERR_MULTIPLE_DEFINITION, Name);
699 /* Check if we have a size in the existing definition */
701 /* Existing, size not given, use size from new def */
702 Encode (EType + 1, Size);
707 /* New type must be identical */
708 if (!EqualTypes (EType, Type) != 0) {
709 Error (ERR_MULTIPLE_DEFINITION, Name);
712 /* In case of a function, use the new type descriptor, since it
713 * contains pointers to the new symbol tables that are needed if
714 * an actual function definition follows.
717 CopyEncode (Type+1, EType+1);
721 /* Add the new flags */
722 Entry->Flags |= Flags;
726 /* Create a new entry */
727 Entry = NewSymEntry (Name, Flags);
729 /* Set the symbol attributes */
730 Entry->Type = TypeDup (Type);
732 /* Add the entry to the symbol table */
733 AddSymEntry (Tab, Entry);
736 /* Return the entry */
742 /*****************************************************************************/
744 /*****************************************************************************/
748 SymTable* GetSymTab (void)
749 /* Return the current symbol table */
756 int SymIsLocal (SymEntry* Sym)
757 /* Return true if the symbol is defined in the highest lexical level */
759 return (Sym->Owner == SymTab || Sym->Owner == TagTab);
764 static int EqualSymTables (SymTable* Tab1, SymTable* Tab2)
765 /* Compare two symbol tables. Return 1 if they are equal and 0 otherwise */
767 /* Compare the parameter lists */
768 SymEntry* Sym1 = Tab1->SymHead;
769 SymEntry* Sym2 = Tab2->SymHead;
771 /* Compare the fields */
772 while (Sym1 && Sym2) {
774 /* Compare this field */
775 if (!EqualTypes (Sym1->Type, Sym2->Type)) {
776 /* Field types not equal */
780 /* Get the pointers to the next fields */
781 Sym1 = Sym1->NextSym;
782 Sym2 = Sym2->NextSym;
785 /* Check both pointers against NULL to compare the field count */
786 return (Sym1 == 0 && Sym2 == 0);
791 int EqualTypes (const type* Type1, const type* Type2)
792 /* Recursively compare two types. Return 1 if the types match, return 0
806 /* Shortcut here: If the pointers are identical, the types are identical */
807 if (Type1 == Type2) {
811 /* Compare two types. Determine, where they differ */
812 while (*Type1 == *Type2 && *Type1 != T_END) {
817 /* Compare the function descriptors */
818 F1 = DecodePtr (Type1+1);
819 F2 = DecodePtr (Type2+1);
821 /* If one of the functions is implicitly declared, both
822 * functions are considered equal. If one of the functions is
823 * old style, and the other is empty, the functions are
826 if ((F1->Flags & FD_IMPLICIT) != 0 || (F2->Flags & FD_IMPLICIT) != 0) {
828 } else if ((F1->Flags & FD_OLDSTYLE) != 0 && (F2->Flags & FD_EMPTY) != 0) {
830 } else if ((F1->Flags & FD_EMPTY) != 0 && (F2->Flags & FD_OLDSTYLE) != 0) {
838 /* Check the remaining flags */
839 if ((F1->Flags & ~FD_IGNORE) != (F2->Flags & ~FD_IGNORE)) {
844 /* Compare the parameter lists */
845 if (EqualSymTables (F1->SymTab, F2->SymTab) == 0 ||
846 EqualSymTables (F1->TagTab, F2->TagTab) == 0) {
847 /* One of the tables is not identical */
852 /* Skip the FuncDesc pointers to compare the return type */
853 Type1 += DECODE_SIZE;
854 Type2 += DECODE_SIZE;
858 /* Check member count */
859 v1 = Decode (Type1+1);
860 v2 = Decode (Type2+1);
861 if (v1 != 0 && v2 != 0 && v1 != v2) {
862 /* Member count given but different */
865 Type1 += DECODE_SIZE;
866 Type2 += DECODE_SIZE;
871 /* Compare the fields recursively. To do that, we fetch the
872 * pointer to the struct definition from the type, and compare
875 Sym1 = DecodePtr (Type1+1);
876 Sym2 = DecodePtr (Type2+1);
878 /* Get the field tables from the struct entry */
879 Tab1 = Sym1->V.S.SymTab;
880 Tab2 = Sym2->V.S.SymTab;
882 /* One or both structs may be forward definitions. In this case,
883 * the symbol tables are both non existant. Assume that the
884 * structs are equal in this case.
886 if (Tab1 != 0 && Tab2 != 0) {
888 if (EqualSymTables (Tab1, Tab2) == 0) {
889 /* Field lists are not equal */
895 /* Structs are equal */
896 Type1 += DECODE_SIZE;
897 Type2 += DECODE_SIZE;
904 /* Done, types are equal */
910 void MakeZPSym (const char* Name)
911 /* Mark the given symbol as zero page symbol */
913 /* Get the symbol table entry */
914 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
916 /* Mark the symbol as zeropage */
918 Entry->Flags |= SC_ZEROPAGE;
920 Error (ERR_UNDEFINED_SYMBOL, Name);
926 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
927 /* Write the symbol table to the given file */
930 const SymEntry* Entry;
932 /* Print the header */
934 va_start (ap, Header);
936 Len = vfprintf (F, Header, ap);
940 /* Underline the header */
947 Entry = Tab->SymHead;
949 fprintf (F, "(empty)\n");
952 DumpSymEntry (F, Entry);
953 Entry = Entry->NextSym;
956 fprintf (F, "\n\n\n");
961 void EmitExternals (void)
962 /* Write import/export statements for external symbols */
968 Entry = SymTab->SymHead;
970 unsigned Flags = Entry->Flags;
971 if (Flags & SC_EXTERN) {
972 /* Only defined or referenced externs */
973 if ((Flags & SC_REF) != 0 && (Flags & SC_DEF) == 0) {
975 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
976 } else if (Flags & SC_DEF) {
978 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
981 Entry = Entry->NextSym;