1 /*****************************************************************************/
5 /* Symbol table management for the cc65 C compiler */
9 /* (C) 2000-2013, 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"
65 /*****************************************************************************/
67 /*****************************************************************************/
71 /* An empty symbol table */
72 SymTable EmptySymTab = {
81 /* Symbol table sizes */
82 #define SYMTAB_SIZE_GLOBAL 211U
83 #define SYMTAB_SIZE_FUNCTION 29U
84 #define SYMTAB_SIZE_BLOCK 13U
85 #define SYMTAB_SIZE_STRUCT 19U
86 #define SYMTAB_SIZE_LABEL 7U
88 /* The current and root symbol tables */
89 static unsigned LexicalLevel = 0; /* For safety checks */
90 static SymTable* SymTab0 = 0;
91 static SymTable* SymTab = 0;
92 static SymTable* TagTab0 = 0;
93 static SymTable* TagTab = 0;
94 static SymTable* LabelTab = 0;
95 static SymTable* SPAdjustTab = 0;
98 /*****************************************************************************/
100 /*****************************************************************************/
104 static SymTable* NewSymTable (unsigned Size)
105 /* Create and return a symbol table for the given lexical level */
109 /* Allocate memory for the table */
110 SymTable* S = xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*));
112 /* Initialize the symbol table structure */
118 for (I = 0; I < Size; ++I) {
122 /* Return the symbol table */
128 static void FreeSymTable (SymTable* S)
129 /* Free the given symbo table including all symbols */
131 /* Free all symbols */
132 SymEntry* Sym = S->SymHead;
134 SymEntry* NextSym = Sym->NextSym;
139 /* Free the table itself */
145 /*****************************************************************************/
146 /* Check symbols in a table */
147 /*****************************************************************************/
151 static void CheckSymTable (SymTable* Tab)
152 /* Check a symbol table for open references, unused symbols ... */
154 SymEntry* Entry = Tab->SymHead;
157 /* Get the storage flags for tne entry */
158 unsigned Flags = Entry->Flags;
160 /* Ignore typedef entries */
161 if (!SymIsTypeDef (Entry)) {
163 /* Check if the symbol is one with storage, and it if it was
164 ** defined but not used.
166 if (((Flags & SC_AUTO) || (Flags & SC_STATIC)) && (Flags & SC_EXTERN) == 0) {
167 if (SymIsDef (Entry) && !SymIsRef (Entry) &&
168 !SymHasAttr (Entry, atUnused)) {
169 if (Flags & SC_PARAM) {
170 if (IS_Get (&WarnUnusedParam)) {
171 Warning ("Parameter '%s' is never used", Entry->Name);
174 if (IS_Get (&WarnUnusedVar)) {
175 Warning ("'%s' is defined but never used", Entry->Name);
181 /* If the entry is a label, check if it was defined in the function */
182 if (Flags & SC_LABEL) {
183 if (!SymIsDef (Entry)) {
184 /* Undefined label */
185 Error ("Undefined label: '%s'", Entry->Name);
186 } else if (!SymIsRef (Entry)) {
187 /* Defined but not used */
188 if (IS_Get (&WarnUnusedLabel)) {
189 Warning ("'%s' is defined but never used", Entry->Name);
197 Entry = Entry->NextSym;
203 /*****************************************************************************/
204 /* Handling of lexical levels */
205 /*****************************************************************************/
209 unsigned GetLexicalLevel (void)
210 /* Return the current lexical level */
217 void EnterGlobalLevel (void)
218 /* Enter the program global lexical level */
221 PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
223 /* Create and assign the symbol table */
224 SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
226 /* Create and assign the tag table */
227 TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
229 /* Create and assign the table of SP adjustment symbols */
230 SPAdjustTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
235 void LeaveGlobalLevel (void)
236 /* Leave the program global lexical level */
239 PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
241 /* Check the tables */
242 CheckSymTable (SymTab0);
244 /* Dump the tables if requested */
246 PrintSymTable (SymTab0, stdout, "Global symbol table");
247 PrintSymTable (TagTab0, stdout, "Global tag table");
250 /* Don't delete the symbol and struct tables! */
257 void EnterFunctionLevel (void)
258 /* Enter function lexical level */
262 /* New lexical level */
265 /* Get a new symbol table and make it current */
266 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
270 /* Get a new tag table and make it current */
271 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
275 /* Create and assign a new label table */
276 S = NewSymTable (SYMTAB_SIZE_LABEL);
277 S->PrevTab = LabelTab;
283 void RememberFunctionLevel (struct FuncDesc* F)
284 /* Remember the symbol tables for the level and leave the level without checks */
286 /* Leave the lexical level */
289 /* Remember the tables */
293 /* Don't delete the tables */
294 SymTab = SymTab->PrevTab;
295 TagTab = TagTab->PrevTab;
296 LabelTab = LabelTab->PrevTab;
301 void ReenterFunctionLevel (struct FuncDesc* F)
302 /* Reenter the function lexical level using the existing tables from F */
304 /* New lexical level */
307 /* Make the tables current again */
308 F->SymTab->PrevTab = SymTab;
311 F->TagTab->PrevTab = TagTab;
314 /* Create and assign a new label table */
315 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
320 void LeaveFunctionLevel (void)
321 /* Leave function lexical level */
323 /* Leave the lexical level */
326 /* Check the tables */
327 CheckSymTable (SymTab);
328 CheckSymTable (LabelTab);
330 /* Drop the label table if it is empty */
331 if (LabelTab->SymCount == 0) {
332 FreeSymTable (LabelTab);
335 /* Don't delete the tables */
336 SymTab = SymTab->PrevTab;
337 TagTab = TagTab->PrevTab;
343 void EnterBlockLevel (void)
344 /* Enter a nested block in a function */
348 /* New lexical level */
351 /* Get a new symbol table and make it current */
352 S = NewSymTable (SYMTAB_SIZE_BLOCK);
356 /* Get a new tag table and make it current */
357 S = NewSymTable (SYMTAB_SIZE_BLOCK);
364 void LeaveBlockLevel (void)
365 /* Leave a nested block in a function */
367 /* Leave the lexical level */
370 /* Check the tables */
371 CheckSymTable (SymTab);
373 /* Don't delete the tables */
374 SymTab = SymTab->PrevTab;
375 TagTab = TagTab->PrevTab;
380 void EnterStructLevel (void)
381 /* Enter a nested block for a struct definition */
385 /* Get a new symbol table and make it current. Note: Structs and enums
386 ** nested in struct scope are NOT local to the struct but visible in the
387 ** outside scope. So we will NOT create a new struct or enum table.
389 S = NewSymTable (SYMTAB_SIZE_BLOCK);
396 void LeaveStructLevel (void)
397 /* Leave a nested block for a struct definition */
399 /* Don't delete the table */
400 SymTab = SymTab->PrevTab;
405 /*****************************************************************************/
407 /*****************************************************************************/
411 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
412 /* Search for an entry in one table */
414 /* Get the start of the hash chain */
415 SymEntry* E = T->Tab [Hash % T->Size];
417 /* Compare the name */
418 if (strcmp (E->Name, Name) == 0) {
422 /* Not found, next entry in hash chain */
432 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
433 /* Find the symbol with the given name in the table tree that starts with T */
435 /* Get the hash over the name */
436 unsigned Hash = HashStr (Name);
438 /* Check all symbol tables for the symbol */
440 /* Try to find the symbol in this table */
441 SymEntry* E = FindSymInTable (Tab, Name, Hash);
443 /* Bail out if we found it */
448 /* Repeat the search in the next higher lexical level */
458 SymEntry* FindSym (const char* Name)
459 /* Find the symbol with the given name */
461 return FindSymInTree (SymTab, Name);
466 SymEntry* FindGlobalSym (const char* Name)
467 /* Find the symbol with the given name in the global symbol table only */
469 return FindSymInTable (SymTab0, Name, HashStr (Name));
474 SymEntry* FindLocalSym (const char* Name)
475 /* Find the symbol with the given name in the current symbol table only */
477 return FindSymInTable (SymTab, Name, HashStr (Name));
482 SymEntry* FindTagSym (const char* Name)
483 /* Find the symbol with the given name in the tag table */
485 return FindSymInTree (TagTab, Name);
490 SymEntry* FindStructField (const Type* T, const char* Name)
491 /* Find a struct field in the fields list */
495 /* The given type may actually be a pointer to struct */
500 /* Non-structs do not have any struct fields... */
501 if (IsClassStruct (T)) {
503 /* Get a pointer to the struct/union type */
504 const SymEntry* Struct = GetSymEntry (T);
507 /* Now search in the struct symbol table. Beware: The table may not
510 if (Struct->V.S.SymTab) {
511 Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
520 /*****************************************************************************/
521 /* Add stuff to the symbol table */
522 /*****************************************************************************/
526 static void AddSymEntry (SymTable* T, SymEntry* S)
527 /* Add a symbol to a symbol table */
529 /* Get the hash value for the name */
530 unsigned Hash = HashStr (S->Name) % T->Size;
532 /* Insert the symbol into the list of all symbols in this level */
534 T->SymTail->NextSym = S;
536 S->PrevSym = T->SymTail;
538 if (T->SymHead == 0) {
544 /* Insert the symbol into the hash chain */
545 S->NextHash = T->Tab[Hash];
548 /* Tell the symbol in which table it is */
554 SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab)
555 /* Add a struct/union entry and return it */
559 /* Type must be struct or union */
560 PRECONDITION (Type == SC_STRUCT || Type == SC_UNION);
562 /* Do we have an entry with this name already? */
563 Entry = FindSymInTable (TagTab, Name, HashStr (Name));
566 /* We do have an entry. This may be a forward, so check it. */
567 if ((Entry->Flags & SC_TYPEMASK) != Type) {
568 /* Existing symbol is not a struct */
569 Error ("Symbol '%s' is already different kind", Name);
570 } else if (Size > 0 && Entry->V.S.Size > 0) {
571 /* Both structs are definitions. */
572 Error ("Multiple definition for '%s'", Name);
574 /* Define the struct size if it is given */
576 Entry->V.S.SymTab = Tab;
577 Entry->V.S.Size = Size;
583 /* Create a new entry */
584 Entry = NewSymEntry (Name, Type);
586 /* Set the struct data */
587 Entry->V.S.SymTab = Tab;
588 Entry->V.S.Size = Size;
590 /* Add it to the current table */
591 AddSymEntry (TagTab, Entry);
594 /* Return the entry */
600 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
601 /* Add a bit field to the local symbol table and return the symbol entry */
603 /* Do we have an entry with this name already? */
604 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
607 /* We have a symbol with this name already */
608 Error ("Multiple definition for '%s'", Name);
612 /* Create a new entry */
613 Entry = NewSymEntry (Name, SC_BITFIELD);
615 /* Set the symbol attributes. Bit-fields are always of type unsigned */
616 Entry->Type = type_uint;
617 Entry->V.B.Offs = Offs;
618 Entry->V.B.BitOffs = BitOffs;
619 Entry->V.B.BitWidth = Width;
621 /* Add the entry to the symbol table */
622 AddSymEntry (SymTab, Entry);
626 /* Return the entry */
632 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
633 /* Add an constant symbol to the symbol table and return it */
635 /* Enums must be inserted in the global symbol table */
636 SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
638 /* Do we have an entry with this name already? */
639 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
641 if ((Entry->Flags & SC_CONST) != SC_CONST) {
642 Error ("Symbol '%s' is already different kind", Name);
644 Error ("Multiple definition for '%s'", Name);
649 /* Create a new entry */
650 Entry = NewSymEntry (Name, Flags);
652 /* Enum values are ints */
653 Entry->Type = TypeDup (T);
655 /* Set the enum data */
656 Entry->V.ConstVal = Val;
658 /* Add the entry to the symbol table */
659 AddSymEntry (Tab, Entry);
661 /* Return the entry */
666 DefOrRef* AddDefOrRef (SymEntry* E, unsigned Flags)
667 /* Add definition or reference to the SymEntry and preserve its attributes */
671 DOR = xmalloc (sizeof (DefOrRef));
672 CollAppend (E->V.L.DefsOrRefs, DOR);
673 DOR->Line = GetCurrentLine ();
674 DOR->LocalsBlockId = (long)CollLast (&CurrentFunc->LocalsBlockStack);
676 DOR->StackPtr = StackPtr;
677 DOR->Depth = CollCount (&CurrentFunc->LocalsBlockStack);
678 DOR->LateSP_Label = GetLocalLabel ();
683 unsigned short FindSPAdjustment (const char* Name)
684 /* Search for an entry in the table of SP adjustments */
686 SymEntry* Entry = FindSymInTable (SPAdjustTab, Name, HashStr (Name));
689 Internal ("No SP adjustment label entry found");
692 return Entry->V.SPAdjustment;
695 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
696 /* Add a goto label to the label table */
699 DefOrRef *DOR, *NewDOR;
700 /* We juggle it so much that a shortcut will help with clarity */
701 Collection *AIC = &CurrentFunc->LocalsBlockStack;
703 /* Do we have an entry with this name already? */
704 SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
707 if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
708 /* Trying to define the label more than once */
709 Error ("Label '%s' is defined more than once", Name);
712 NewDOR = AddDefOrRef (Entry, Flags);
714 /* Walk through all occurrences of the label so far and evaluate
715 ** their relationship with the one passed to the function.
717 for (i = 0; i < CollCount (Entry->V.L.DefsOrRefs); i++) {
718 DOR = CollAt (Entry->V.L.DefsOrRefs, i);
720 if ((DOR->Flags & SC_DEF) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
721 /* We're processing a goto and here is its destination label.
722 ** This means the difference between SP values is already known,
723 ** so we simply emit the SP adjustment code.
725 if (StackPtr != DOR->StackPtr) {
726 g_space (StackPtr - DOR->StackPtr);
729 /* Are we jumping into a block with initalization of an object that
730 ** has automatic storage duration? Let's emit a warning.
732 if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
733 (CollCount (AIC) < DOR->Depth ||
734 (long)CollAt (AIC, DOR->Depth - 1) != DOR->LocalsBlockId)) {
735 Warning ("Goto at line %d to label %s jumps into a block with "
736 "initialization of an object that has automatic storage duration",
737 GetCurrentLine (), Name);
742 if ((DOR->Flags & SC_REF) && (DOR->Flags & SC_GOTO) && (Flags & SC_DEF)) {
743 /* We're processing a label, let's update all gotos encountered
748 g_defdatalabel (DOR->LateSP_Label);
749 g_defdata (CF_CONST | CF_INT, StackPtr - DOR->StackPtr, 0);
751 /* Optimizer will need the information about the value of SP adjustment
752 ** later, so let's preserve it.
754 E = NewSymEntry (LocalLabelName (DOR->LateSP_Label), SC_SPADJUSTMENT);
755 E->V.SPAdjustment = StackPtr - DOR->StackPtr;
756 AddSymEntry (SPAdjustTab, E);
758 /* Are we jumping into a block with initalization of an object that
759 ** has automatic storage duration? Let's emit a warning.
761 if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
762 (CollCount (AIC) >= DOR->Depth ||
763 (long)CollLast (AIC) >= (long)DOR->Line))
764 Warning ("Goto at line %d to label %s jumps into a block with "
765 "initialization of an object that has automatic storage duration",
771 Entry->Flags |= Flags;
775 /* Create a new entry */
776 Entry = NewSymEntry (Name, SC_LABEL | Flags);
778 /* Set a new label number */
779 Entry->V.L.Label = GetLocalLabel ();
781 /* Create Collection for label definition and references */
782 Entry->V.L.DefsOrRefs = NewCollection ();
783 NewDOR = AddDefOrRef (Entry, Flags);
785 /* Generate the assembler name of the label */
786 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
788 /* Add the entry to the label table */
789 AddSymEntry (LabelTab, Entry);
793 /* We are processing a goto, but the label has not yet been defined */
794 if (!SymIsDef (Entry) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
795 g_lateadjustSP (NewDOR->LateSP_Label);
798 /* Return the entry */
804 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
805 /* Add a local symbol and return the symbol entry */
807 /* Do we have an entry with this name already? */
808 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
811 /* We have a symbol with this name already */
812 Error ("Multiple definition for '%s'", Name);
816 /* Create a new entry */
817 Entry = NewSymEntry (Name, Flags);
819 /* Set the symbol attributes */
820 Entry->Type = TypeDup (T);
821 if ((Flags & SC_AUTO) == SC_AUTO) {
822 Entry->V.Offs = Offs;
823 } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
824 Entry->V.R.RegOffs = Offs;
825 Entry->V.R.SaveOffs = StackPtr;
826 } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
827 Entry->V.L.Label = Offs;
828 SymSetAsmName (Entry);
829 } else if ((Flags & SC_STATIC) == SC_STATIC) {
830 /* Generate the assembler name from the label number */
831 Entry->V.L.Label = Offs;
832 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
833 } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
834 Entry->V.Offs = Offs;
836 Internal ("Invalid flags in AddLocalSym: %04X", Flags);
839 /* Add the entry to the symbol table */
840 AddSymEntry (SymTab, Entry);
844 /* Return the entry */
850 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
851 /* Add an external or global symbol to the symbol table and return the entry */
853 /* There is some special handling for functions, so check if it is one */
854 int IsFunc = IsTypeFunc (T);
856 /* Functions must be inserted in the global symbol table */
857 SymTable* Tab = IsFunc? SymTab0 : SymTab;
859 /* Do we have an entry with this name already? */
860 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
864 /* If the existing symbol is an enumerated constant,
865 ** then avoid a compiler crash. See GitHub issue #728.
867 if (Entry->Flags & SC_ENUM) {
868 Fatal ("Can't redeclare enum constant '%s' as global variable", Name);
871 /* We have a symbol with this name already */
872 if (Entry->Flags & SC_TYPE) {
873 Error ("Multiple definition for '%s'", Name);
877 /* Get the type string of the existing symbol */
880 /* If we are handling arrays, the old entry or the new entry may be an
881 ** incomplete declaration. Accept this, and if the exsting entry is
882 ** incomplete, complete it.
884 if (IsTypeArray (T) && IsTypeArray (EType)) {
886 /* Get the array sizes */
887 long Size = GetElementCount (T);
888 long ESize = GetElementCount (EType);
890 if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
891 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
892 /* Types not identical: Conflicting types */
893 Error ("Conflicting types for '%s'", Name);
896 /* Check if we have a size in the existing definition */
897 if (ESize == UNSPECIFIED) {
898 /* Existing, size not given, use size from new def */
899 SetElementCount (EType, Size);
904 /* New type must be identical */
905 if (TypeCmp (EType, T) < TC_EQUAL) {
906 Error ("Conflicting types for '%s'", Name);
910 /* In case of a function, use the new type descriptor, since it
911 ** contains pointers to the new symbol tables that are needed if
912 ** an actual function definition follows. Be sure not to use the
913 ** new descriptor if it contains a function declaration with an
914 ** empty parameter list.
917 /* Get the function descriptor from the new type */
918 FuncDesc* F = GetFuncDesc (T);
919 /* Use this new function descriptor if it doesn't contain
920 ** an empty parameter list.
922 if ((F->Flags & FD_EMPTY) == 0) {
924 SetFuncDesc (EType, F);
929 /* If a static declaration follows a non-static declaration, then
930 ** warn about the conflict. (It will compile a public declaration.)
932 if ((Flags & SC_EXTERN) == 0 && (Entry->Flags & SC_EXTERN) != 0) {
933 Warning ("static declaration follows non-static declaration of '%s'.", Name);
936 /* An extern declaration must not change the current linkage. */
937 if (IsFunc || (Flags & (SC_EXTERN | SC_STORAGE)) == SC_EXTERN) {
941 /* If a public declaration follows a static declaration, then
942 ** warn about the conflict. (It will compile a public declaration.)
944 if ((Flags & SC_EXTERN) != 0 && (Entry->Flags & SC_EXTERN) == 0) {
945 Warning ("public declaration follows static declaration of '%s'.", Name);
948 /* Add the new flags */
949 Entry->Flags |= Flags;
953 /* Create a new entry */
954 Entry = NewSymEntry (Name, Flags);
956 /* Set the symbol attributes */
957 Entry->Type = TypeDup (T);
959 /* If this is a function, set the function descriptor and clear
960 ** additional fields.
963 Entry->V.F.Func = GetFuncDesc (Entry->Type);
967 /* Add the assembler name of the symbol */
968 SymSetAsmName (Entry);
970 /* Add the entry to the symbol table */
971 AddSymEntry (Tab, Entry);
974 /* Return the entry */
980 /*****************************************************************************/
982 /*****************************************************************************/
986 SymTable* GetSymTab (void)
987 /* Return the current symbol table */
994 SymTable* GetGlobalSymTab (void)
995 /* Return the global symbol table */
1002 int SymIsLocal (SymEntry* Sym)
1003 /* Return true if the symbol is defined in the highest lexical level */
1005 return (Sym->Owner == SymTab || Sym->Owner == TagTab);
1010 void MakeZPSym (const char* Name)
1011 /* Mark the given symbol as zero page symbol */
1013 /* Get the symbol table entry */
1014 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
1016 /* Mark the symbol as zeropage */
1018 Entry->Flags |= SC_ZEROPAGE;
1020 Error ("Undefined symbol: '%s'", Name);
1026 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
1027 /* Write the symbol table to the given file */
1030 const SymEntry* Entry;
1032 /* Print the header */
1034 va_start (ap, Header);
1036 Len = vfprintf (F, Header, ap);
1040 /* Underline the header */
1046 /* Dump the table */
1047 Entry = Tab->SymHead;
1049 fprintf (F, "(empty)\n");
1052 DumpSymEntry (F, Entry);
1053 Entry = Entry->NextSym;
1056 fprintf (F, "\n\n\n");
1061 void EmitExternals (void)
1062 /* Write import/export statements for external symbols */
1066 Entry = SymTab->SymHead;
1068 unsigned Flags = Entry->Flags;
1069 if (Flags & SC_EXTERN) {
1070 /* Only defined or referenced externs */
1071 if (SymIsRef (Entry) && !SymIsDef (Entry)) {
1073 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
1074 } else if (SymIsDef (Entry)) {
1076 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
1079 Entry = Entry->NextSym;
1085 void EmitDebugInfo (void)
1086 /* Emit debug infos for the locals of the current scope */
1089 const SymEntry* Sym;
1091 /* Output info for locals if enabled */
1093 /* For cosmetic reasons in the output file, we will insert two tabs
1094 ** on global level and just one on local level.
1096 if (LexicalLevel == LEX_LEVEL_GLOBAL) {
1097 Head = "\t.dbg\t\tsym";
1099 Head = "\t.dbg\tsym";
1101 Sym = SymTab->SymHead;
1103 if ((Sym->Flags & (SC_CONST|SC_TYPE)) == 0) {
1104 if (Sym->Flags & SC_AUTO) {
1105 AddTextLine ("%s, \"%s\", \"00\", auto, %d",
1106 Head, Sym->Name, Sym->V.Offs);
1107 } else if (Sym->Flags & SC_REGISTER) {
1108 AddTextLine ("%s, \"%s\", \"00\", register, \"regbank\", %d",
1109 Head, Sym->Name, Sym->V.R.RegOffs);
1111 } else if (SymIsRef (Sym) && !SymIsDef (Sym)) {
1112 AddTextLine ("%s, \"%s\", \"00\", %s, \"%s\"",
1114 (Sym->Flags & SC_EXTERN)? "extern" : "static",