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 SPAdjustTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
234 void LeaveGlobalLevel (void)
235 /* Leave the program global lexical level */
238 PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
240 /* Check the tables */
241 CheckSymTable (SymTab0);
243 /* Dump the tables if requested */
245 PrintSymTable (SymTab0, stdout, "Global symbol table");
246 PrintSymTable (TagTab0, stdout, "Global tag table");
249 /* Don't delete the symbol and struct tables! */
256 void EnterFunctionLevel (void)
257 /* Enter function lexical level */
261 /* New lexical level */
264 /* Get a new symbol table and make it current */
265 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
269 /* Get a new tag table and make it current */
270 S = NewSymTable (SYMTAB_SIZE_FUNCTION);
274 /* Create and assign a new label table */
275 S = NewSymTable (SYMTAB_SIZE_LABEL);
276 S->PrevTab = LabelTab;
282 void RememberFunctionLevel (struct FuncDesc* F)
283 /* Remember the symbol tables for the level and leave the level without checks */
285 /* Leave the lexical level */
288 /* Remember the tables */
292 /* Don't delete the tables */
293 SymTab = SymTab->PrevTab;
294 TagTab = TagTab->PrevTab;
295 LabelTab = LabelTab->PrevTab;
300 void ReenterFunctionLevel (struct FuncDesc* F)
301 /* Reenter the function lexical level using the existing tables from F */
303 /* New lexical level */
306 /* Make the tables current again */
307 F->SymTab->PrevTab = SymTab;
310 F->TagTab->PrevTab = TagTab;
313 /* Create and assign a new label table */
314 LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
319 void LeaveFunctionLevel (void)
320 /* Leave function lexical level */
322 /* Leave the lexical level */
325 /* Check the tables */
326 CheckSymTable (SymTab);
327 CheckSymTable (LabelTab);
329 /* Drop the label table if it is empty */
330 if (LabelTab->SymCount == 0) {
331 FreeSymTable (LabelTab);
334 /* Don't delete the tables */
335 SymTab = SymTab->PrevTab;
336 TagTab = TagTab->PrevTab;
342 void EnterBlockLevel (void)
343 /* Enter a nested block in a function */
347 /* New lexical level */
350 /* Get a new symbol table and make it current */
351 S = NewSymTable (SYMTAB_SIZE_BLOCK);
355 /* Get a new tag table and make it current */
356 S = NewSymTable (SYMTAB_SIZE_BLOCK);
363 void LeaveBlockLevel (void)
364 /* Leave a nested block in a function */
366 /* Leave the lexical level */
369 /* Check the tables */
370 CheckSymTable (SymTab);
372 /* Don't delete the tables */
373 SymTab = SymTab->PrevTab;
374 TagTab = TagTab->PrevTab;
379 void EnterStructLevel (void)
380 /* Enter a nested block for a struct definition */
384 /* Get a new symbol table and make it current. Note: Structs and enums
385 ** nested in struct scope are NOT local to the struct but visible in the
386 ** outside scope. So we will NOT create a new struct or enum table.
388 S = NewSymTable (SYMTAB_SIZE_BLOCK);
395 void LeaveStructLevel (void)
396 /* Leave a nested block for a struct definition */
398 /* Don't delete the table */
399 SymTab = SymTab->PrevTab;
404 /*****************************************************************************/
406 /*****************************************************************************/
410 SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
411 /* Search for an entry in one table */
413 /* Get the start of the hash chain */
414 SymEntry* E = T->Tab [Hash % T->Size];
416 /* Compare the name */
417 if (strcmp (E->Name, Name) == 0) {
421 /* Not found, next entry in hash chain */
431 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
432 /* Find the symbol with the given name in the table tree that starts with T */
434 /* Get the hash over the name */
435 unsigned Hash = HashStr (Name);
437 /* Check all symbol tables for the symbol */
439 /* Try to find the symbol in this table */
440 SymEntry* E = FindSymInTable (Tab, Name, Hash);
442 /* Bail out if we found it */
447 /* Repeat the search in the next higher lexical level */
457 SymEntry* FindSym (const char* Name)
458 /* Find the symbol with the given name */
460 return FindSymInTree (SymTab, Name);
465 SymEntry* FindGlobalSym (const char* Name)
466 /* Find the symbol with the given name in the global symbol table only */
468 return FindSymInTable (SymTab0, Name, HashStr (Name));
473 SymEntry* FindLocalSym (const char* Name)
474 /* Find the symbol with the given name in the current symbol table only */
476 return FindSymInTable (SymTab, Name, HashStr (Name));
481 SymEntry* FindTagSym (const char* Name)
482 /* Find the symbol with the given name in the tag table */
484 return FindSymInTree (TagTab, Name);
489 SymEntry* FindStructField (const Type* T, const char* Name)
490 /* Find a struct field in the fields list */
494 /* The given type may actually be a pointer to struct */
499 /* Non-structs do not have any struct fields... */
500 if (IsClassStruct (T)) {
502 /* Get a pointer to the struct/union type */
503 const SymEntry* Struct = GetSymEntry (T);
506 /* Now search in the struct symbol table. Beware: The table may not
509 if (Struct->V.S.SymTab) {
510 Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
519 /*****************************************************************************/
520 /* Add stuff to the symbol table */
521 /*****************************************************************************/
525 static void AddSymEntry (SymTable* T, SymEntry* S)
526 /* Add a symbol to a symbol table */
528 /* Get the hash value for the name */
529 unsigned Hash = HashStr (S->Name) % T->Size;
531 /* Insert the symbol into the list of all symbols in this level */
533 T->SymTail->NextSym = S;
535 S->PrevSym = T->SymTail;
537 if (T->SymHead == 0) {
543 /* Insert the symbol into the hash chain */
544 S->NextHash = T->Tab[Hash];
547 /* Tell the symbol in which table it is */
553 SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab)
554 /* Add a struct/union entry and return it */
558 /* Type must be struct or union */
559 PRECONDITION (Type == SC_STRUCT || Type == SC_UNION);
561 /* Do we have an entry with this name already? */
562 Entry = FindSymInTable (TagTab, Name, HashStr (Name));
565 /* We do have an entry. This may be a forward, so check it. */
566 if ((Entry->Flags & SC_TYPEMASK) != Type) {
567 /* Existing symbol is not a struct */
568 Error ("Symbol `%s' is already different kind", Name);
569 } else if (Size > 0 && Entry->V.S.Size > 0) {
570 /* Both structs are definitions. */
571 Error ("Multiple definition for `%s'", Name);
573 /* Define the struct size if it is given */
575 Entry->V.S.SymTab = Tab;
576 Entry->V.S.Size = Size;
582 /* Create a new entry */
583 Entry = NewSymEntry (Name, Type);
585 /* Set the struct data */
586 Entry->V.S.SymTab = Tab;
587 Entry->V.S.Size = Size;
589 /* Add it to the current table */
590 AddSymEntry (TagTab, Entry);
593 /* Return the entry */
599 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
600 /* Add a bit field to the local symbol table and return the symbol entry */
602 /* Do we have an entry with this name already? */
603 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
606 /* We have a symbol with this name already */
607 Error ("Multiple definition for `%s'", Name);
611 /* Create a new entry */
612 Entry = NewSymEntry (Name, SC_BITFIELD);
614 /* Set the symbol attributes. Bit-fields are always of type unsigned */
615 Entry->Type = type_uint;
616 Entry->V.B.Offs = Offs;
617 Entry->V.B.BitOffs = BitOffs;
618 Entry->V.B.BitWidth = Width;
620 /* Add the entry to the symbol table */
621 AddSymEntry (SymTab, Entry);
625 /* Return the entry */
631 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
632 /* Add an constant symbol to the symbol table and return it */
634 /* Enums must be inserted in the global symbol table */
635 SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
637 /* Do we have an entry with this name already? */
638 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
640 if ((Entry->Flags & SC_CONST) != SC_CONST) {
641 Error ("Symbol `%s' is already different kind", Name);
643 Error ("Multiple definition for `%s'", Name);
648 /* Create a new entry */
649 Entry = NewSymEntry (Name, Flags);
651 /* Enum values are ints */
652 Entry->Type = TypeDup (T);
654 /* Set the enum data */
655 Entry->V.ConstVal = Val;
657 /* Add the entry to the symbol table */
658 AddSymEntry (Tab, Entry);
660 /* Return the entry */
665 DefOrRef* AddDefOrRef (SymEntry* E, unsigned Flags)
666 /* Add definition or reference to the SymEntry and preserve its attributes */
670 DOR = xmalloc (sizeof (DefOrRef));
671 CollAppend (E->V.L.DefsOrRefs, DOR);
672 DOR->Line = GetCurrentLine ();
673 DOR->LocalsBlockId = (long)CollLast (&CurrentFunc->LocalsBlockStack);
675 DOR->StackPtr = StackPtr;
676 DOR->Depth = CollCount (&CurrentFunc->LocalsBlockStack);
677 DOR->LateSP_Label = GetLocalLabel ();
682 unsigned short FindSPAdjustment (const char* Name)
684 SymEntry* Entry = FindSymInTable (SPAdjustTab, Name, HashStr (Name));
687 printf("L: %s sa: %d\n", Name, Entry->V.G.SPAdjustment);
688 return Entry->V.G.SPAdjustment;
691 Fatal("ICE: No label entry found");
696 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
697 /* Add a goto label to the label table */
700 DefOrRef *DOR, *NewDOR;
701 /* We juggle it so much that a shortcut will help with clarity */
702 Collection *AIC = &CurrentFunc->LocalsBlockStack;
704 /* Do we have an entry with this name already? */
705 SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
708 if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
709 /* Trying to define the label more than once */
710 Error ("Label `%s' is defined more than once", Name);
713 NewDOR = AddDefOrRef (Entry, Flags);
715 /* Walk through all occurrences of the label so far and evaluate
716 ** their relationship with the one passed to the function.
718 for (i = 0; i < CollCount (Entry->V.L.DefsOrRefs); i++) {
719 DOR = CollAt (Entry->V.L.DefsOrRefs, i);
721 if ((DOR->Flags & SC_DEF) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
722 /* We're processing a goto and here is its destination label.
723 ** This means the difference between SP values is already known,
724 ** so we simply emit the SP adjustment code.
726 if (StackPtr != DOR->StackPtr) {
727 g_space (StackPtr - DOR->StackPtr);
730 /* Are we jumping into a block with initalization of an object that
731 ** has automatic storage duration? Let's emit a warning.
733 if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
734 (CollCount (AIC) < DOR->Depth ||
735 (long)CollAt (AIC, DOR->Depth - 1) != DOR->LocalsBlockId)) {
736 Warning ("Goto at line %d to label %s jumps into a block with "
737 "initialization of an object that has automatic storage duration",
738 GetCurrentLine (), Name);
743 if ((DOR->Flags & SC_REF) && (DOR->Flags & SC_GOTO) && (Flags & SC_DEF)) {
744 /* We're processing a label, let's update all gotos encountered
749 g_defdatalabel (DOR->LateSP_Label);
750 g_defdata (CF_CONST | CF_INT, StackPtr - DOR->StackPtr, 0);
751 E = NewSymEntry (LocalLabelName(DOR->LateSP_Label), SC_SPADJUSTMENT);
752 E->V.G.SPAdjustment = StackPtr - DOR->StackPtr;
753 AddSymEntry (SPAdjustTab, E);
756 /* Are we jumping into a block with initalization of an object that
757 ** has automatic storage duration? Let's emit a warning.
759 if ((long)CollLast (AIC) != DOR->LocalsBlockId &&
760 (CollCount (AIC) >= DOR->Depth ||
761 (long)CollLast (AIC) >= (long)DOR->Line))
762 Warning ("Goto at line %d to label %s jumps into a block with "
763 "initialization of an object that has automatic storage duration",
769 Entry->Flags |= Flags;
773 /* Create a new entry */
774 Entry = NewSymEntry (Name, SC_LABEL | Flags);
776 /* Set a new label number */
777 Entry->V.L.Label = GetLocalLabel ();
779 /* Create Collection for label definition and references */
780 Entry->V.L.DefsOrRefs = NewCollection ();
781 NewDOR = AddDefOrRef (Entry, Flags);
783 /* Generate the assembler name of the label */
784 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
786 /* Add the entry to the label table */
787 AddSymEntry (LabelTab, Entry);
791 /* We are processing a goto, but the label has not yet been defined */
792 if (!SymIsDef (Entry) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
793 g_lateadjustSP (NewDOR->LateSP_Label);
796 /* Return the entry */
802 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
803 /* Add a local symbol and return the symbol entry */
805 /* Do we have an entry with this name already? */
806 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
809 /* We have a symbol with this name already */
810 Error ("Multiple definition for `%s'", Name);
814 /* Create a new entry */
815 Entry = NewSymEntry (Name, Flags);
817 /* Set the symbol attributes */
818 Entry->Type = TypeDup (T);
819 if ((Flags & SC_AUTO) == SC_AUTO) {
820 Entry->V.Offs = Offs;
821 } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
822 Entry->V.R.RegOffs = Offs;
823 Entry->V.R.SaveOffs = StackPtr;
824 } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
825 Entry->V.L.Label = Offs;
826 SymSetAsmName (Entry);
827 } else if ((Flags & SC_STATIC) == SC_STATIC) {
828 /* Generate the assembler name from the label number */
829 Entry->V.L.Label = Offs;
830 Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
831 } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
832 Entry->V.Offs = Offs;
834 Internal ("Invalid flags in AddLocalSym: %04X", Flags);
837 /* Add the entry to the symbol table */
838 AddSymEntry (SymTab, Entry);
842 /* Return the entry */
848 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
849 /* Add an external or global symbol to the symbol table and return the entry */
851 /* There is some special handling for functions, so check if it is one */
852 int IsFunc = IsTypeFunc (T);
854 /* Functions must be inserted in the global symbol table */
855 SymTable* Tab = IsFunc? SymTab0 : SymTab;
857 /* Do we have an entry with this name already? */
858 SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
862 /* If the existing symbol is an enumerated constant,
863 ** then avoid a compiler crash. See GitHub issue #728.
865 if (Entry->Flags & SC_ENUM) {
866 Fatal ("Can't redeclare enum constant `%s' as global variable", Name);
869 /* We have a symbol with this name already */
870 if (Entry->Flags & SC_TYPE) {
871 Error ("Multiple definition for `%s'", Name);
875 /* Get the type string of the existing symbol */
878 /* If we are handling arrays, the old entry or the new entry may be an
879 ** incomplete declaration. Accept this, and if the exsting entry is
880 ** incomplete, complete it.
882 if (IsTypeArray (T) && IsTypeArray (EType)) {
884 /* Get the array sizes */
885 long Size = GetElementCount (T);
886 long ESize = GetElementCount (EType);
888 if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
889 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
890 /* Types not identical: Conflicting types */
891 Error ("Conflicting types for `%s'", Name);
894 /* Check if we have a size in the existing definition */
895 if (ESize == UNSPECIFIED) {
896 /* Existing, size not given, use size from new def */
897 SetElementCount (EType, Size);
902 /* New type must be identical */
903 if (TypeCmp (EType, T) < TC_EQUAL) {
904 Error ("Conflicting types for `%s'", Name);
908 /* In case of a function, use the new type descriptor, since it
909 ** contains pointers to the new symbol tables that are needed if
910 ** an actual function definition follows. Be sure not to use the
911 ** new descriptor if it contains a function declaration with an
912 ** empty parameter list.
915 /* Get the function descriptor from the new type */
916 FuncDesc* F = GetFuncDesc (T);
917 /* Use this new function descriptor if it doesn't contain
918 ** an empty parameter list.
920 if ((F->Flags & FD_EMPTY) == 0) {
922 SetFuncDesc (EType, F);
927 /* If a static declaration follows a non-static declaration, then
928 ** warn about the conflict. (It will compile a public declaration.)
930 if ((Flags & SC_EXTERN) == 0 && (Entry->Flags & SC_EXTERN) != 0) {
931 Warning ("static declaration follows non-static declaration of `%s'.", Name);
934 /* An extern declaration must not change the current linkage. */
935 if (IsFunc || (Flags & (SC_EXTERN | SC_STORAGE)) == SC_EXTERN) {
939 /* If a public declaration follows a static declaration, then
940 ** warn about the conflict. (It will compile a public declaration.)
942 if ((Flags & SC_EXTERN) != 0 && (Entry->Flags & SC_EXTERN) == 0) {
943 Warning ("public declaration follows static declaration of `%s'.", Name);
946 /* Add the new flags */
947 Entry->Flags |= Flags;
951 /* Create a new entry */
952 Entry = NewSymEntry (Name, Flags);
954 /* Set the symbol attributes */
955 Entry->Type = TypeDup (T);
957 /* If this is a function, set the function descriptor and clear
958 ** additional fields.
961 Entry->V.F.Func = GetFuncDesc (Entry->Type);
965 /* Add the assembler name of the symbol */
966 SymSetAsmName (Entry);
968 /* Add the entry to the symbol table */
969 AddSymEntry (Tab, Entry);
972 /* Return the entry */
978 /*****************************************************************************/
980 /*****************************************************************************/
984 SymTable* GetSymTab (void)
985 /* Return the current symbol table */
992 SymTable* GetGlobalSymTab (void)
993 /* Return the global symbol table */
1000 int SymIsLocal (SymEntry* Sym)
1001 /* Return true if the symbol is defined in the highest lexical level */
1003 return (Sym->Owner == SymTab || Sym->Owner == TagTab);
1008 void MakeZPSym (const char* Name)
1009 /* Mark the given symbol as zero page symbol */
1011 /* Get the symbol table entry */
1012 SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
1014 /* Mark the symbol as zeropage */
1016 Entry->Flags |= SC_ZEROPAGE;
1018 Error ("Undefined symbol: `%s'", Name);
1024 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
1025 /* Write the symbol table to the given file */
1028 const SymEntry* Entry;
1030 /* Print the header */
1032 va_start (ap, Header);
1034 Len = vfprintf (F, Header, ap);
1038 /* Underline the header */
1044 /* Dump the table */
1045 Entry = Tab->SymHead;
1047 fprintf (F, "(empty)\n");
1050 DumpSymEntry (F, Entry);
1051 Entry = Entry->NextSym;
1054 fprintf (F, "\n\n\n");
1059 void EmitExternals (void)
1060 /* Write import/export statements for external symbols */
1064 Entry = SymTab->SymHead;
1066 unsigned Flags = Entry->Flags;
1067 if (Flags & SC_EXTERN) {
1068 /* Only defined or referenced externs */
1069 if (SymIsRef (Entry) && !SymIsDef (Entry)) {
1071 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
1072 } else if (SymIsDef (Entry)) {
1074 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
1077 Entry = Entry->NextSym;
1083 void EmitDebugInfo (void)
1084 /* Emit debug infos for the locals of the current scope */
1087 const SymEntry* Sym;
1089 /* Output info for locals if enabled */
1091 /* For cosmetic reasons in the output file, we will insert two tabs
1092 ** on global level and just one on local level.
1094 if (LexicalLevel == LEX_LEVEL_GLOBAL) {
1095 Head = "\t.dbg\t\tsym";
1097 Head = "\t.dbg\tsym";
1099 Sym = SymTab->SymHead;
1101 if ((Sym->Flags & (SC_CONST|SC_TYPE)) == 0) {
1102 if (Sym->Flags & SC_AUTO) {
1103 AddTextLine ("%s, \"%s\", \"00\", auto, %d",
1104 Head, Sym->Name, Sym->V.Offs);
1105 } else if (Sym->Flags & SC_REGISTER) {
1106 AddTextLine ("%s, \"%s\", \"00\", register, \"regbank\", %d",
1107 Head, Sym->Name, Sym->V.R.RegOffs);
1109 } else if (SymIsRef (Sym) && !SymIsDef (Sym)) {
1110 AddTextLine ("%s, \"%s\", \"00\", %s, \"%s\"",
1112 (Sym->Flags & SC_EXTERN)? "extern" : "static",