]> git.sur5r.net Git - cc65/blob - src/cc65/symtab.c
Move the compiler stack pointer into its own module.
[cc65] / src / cc65 / symtab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 symtab.c                                  */
4 /*                                                                           */
5 /*              Symbol table management for the cc65 C compiler              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <string.h>
40
41 /* common */
42 #include "check.h"
43 #include "debugflag.h"
44 #include "hashstr.h"
45 #include "xmalloc.h"
46
47 /* cc65 */
48 #include "asmcode.h"
49 #include "asmlabel.h"
50 #include "codegen.h"
51 #include "datatype.h"
52 #include "declare.h"
53 #include "error.h"
54 #include "funcdesc.h"
55 #include "global.h"
56 #include "stackptr.h"
57 #include "symentry.h"
58 #include "typecmp.h"
59 #include "symtab.h"
60
61
62
63 /*****************************************************************************/
64 /*                                   Data                                    */
65 /*****************************************************************************/
66
67
68
69 /* An empty symbol table */
70 SymTable        EmptySymTab = {
71     0,          /* PrevTab */
72     0,          /* SymHead */
73     0,          /* SymTail */
74     0,          /* SymCount */
75     1,          /* Size */
76     { 0 }       /* Tab[1] */
77 };
78
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
85
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;
93
94
95
96 /*****************************************************************************/
97 /*                              struct SymTable                              */
98 /*****************************************************************************/
99
100
101
102 static SymTable* NewSymTable (unsigned Size)
103 /* Create and return a symbol table for the given lexical level */
104 {
105     unsigned I;
106
107     /* Allocate memory for the table */
108     SymTable* S = xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*));
109
110     /* Initialize the symbol table structure */
111     S->PrevTab  = 0;
112     S->SymHead  = 0;
113     S->SymTail  = 0;
114     S->SymCount = 0;
115     S->Size     = Size;
116     for (I = 0; I < Size; ++I) {
117         S->Tab[I] = 0;
118     }
119
120     /* Return the symbol table */
121     return S;
122 }
123
124
125
126 static void FreeSymTable (SymTable* S)
127 /* Free the given symbo table including all symbols */
128 {
129     /* Free all symbols */
130     SymEntry* Sym = S->SymHead;
131     while (Sym) {
132         SymEntry* NextSym = Sym->NextSym;
133         FreeSymEntry (Sym);
134         Sym = NextSym;
135     }
136
137     /* Free the table itself */
138     xfree (S);
139 }
140
141
142
143 /*****************************************************************************/
144 /*                         Check symbols in a table                          */
145 /*****************************************************************************/
146
147
148
149 static void CheckSymTable (SymTable* Tab)
150 /* Check a symbol table for open references, unused symbols ... */
151 {
152     SymEntry* Entry = Tab->SymHead;
153     while (Entry) {
154
155         /* Get the storage flags for tne entry */
156         unsigned Flags = Entry->Flags;
157
158         /* Ignore typedef entries */
159         if ((Flags & SC_TYPEDEF) != SC_TYPEDEF) {
160
161             /* Check if the symbol is one with storage, and it if it was
162              * defined but not used.
163              */
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);
168                     } else {
169                         Warning ("`%s' is defined but never used", Entry->Name);
170                     }
171                 }
172             }
173
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);
182                 }
183             }
184
185         }
186
187         /* Next entry */
188         Entry = Entry->NextSym;
189     }
190 }
191
192
193
194 /*****************************************************************************/
195 /*                        Handling of lexical levels                         */
196 /*****************************************************************************/
197
198
199
200 unsigned GetLexicalLevel (void)
201 /* Return the current lexical level */
202 {
203     return LexicalLevel;
204 }
205
206
207
208 void EnterGlobalLevel (void)
209 /* Enter the program global lexical level */
210 {
211     /* Safety */
212     PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
213
214     /* Create and assign the symbol table */
215     SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
216
217     /* Create and assign the tag table */
218     TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
219 }
220
221
222
223 void LeaveGlobalLevel (void)
224 /* Leave the program global lexical level */
225 {
226     /* Safety */
227     PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
228
229     /* Check the tables */
230     CheckSymTable (SymTab0);
231
232     /* Dump the tables if requested */
233     if (Debug) {
234         PrintSymTable (SymTab0, stdout, "Global symbol table");
235         PrintSymTable (TagTab0, stdout, "Global tag table");
236     }
237
238     /* Don't delete the symbol and struct tables! */
239     SymTab = 0;
240     TagTab = 0;
241 }
242
243
244
245 void EnterFunctionLevel (void)
246 /* Enter function lexical level */
247 {
248     SymTable* S;
249
250     /* New lexical level */
251     ++LexicalLevel;
252
253     /* Get a new symbol table and make it current */
254     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
255     S->PrevTab = SymTab;
256     SymTab     = S;
257
258     /* Get a new tag table and make it current */
259     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
260     S->PrevTab = TagTab;
261     TagTab  = S;
262
263     /* Create and assign a new label table */
264     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
265 }
266
267
268
269 void RememberFunctionLevel (struct FuncDesc* F)
270 /* Remember the symbol tables for the level and leave the level without checks */
271 {
272     /* Leave the lexical level */
273     --LexicalLevel;
274
275     /* Remember the tables */
276     F->SymTab = SymTab;
277     F->TagTab = TagTab;
278
279     /* Don't delete the tables */
280     SymTab = SymTab->PrevTab;
281     TagTab = TagTab->PrevTab;
282 }
283
284
285
286 void ReenterFunctionLevel (struct FuncDesc* F)
287 /* Reenter the function lexical level using the existing tables from F */
288 {
289     /* New lexical level */
290     ++LexicalLevel;
291
292     /* Make the tables current again */
293     F->SymTab->PrevTab = SymTab;
294     SymTab = F->SymTab;
295
296     F->TagTab->PrevTab = TagTab;
297     TagTab = F->TagTab;
298
299     /* Create and assign a new label table */
300     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
301 }
302
303
304
305 void LeaveFunctionLevel (void)
306 /* Leave function lexical level */
307 {
308     /* Leave the lexical level */
309     --LexicalLevel;
310
311     /* Check the tables */
312     CheckSymTable (SymTab);
313     CheckSymTable (LabelTab);
314
315     /* Drop the label table if it is empty */
316     if (LabelTab->SymCount == 0) {
317         FreeSymTable (LabelTab);
318     }
319
320     /* Don't delete the tables */
321     SymTab = SymTab->PrevTab;
322     TagTab = TagTab->PrevTab;
323     LabelTab  = 0;
324 }
325
326
327
328 void EnterBlockLevel (void)
329 /* Enter a nested block in a function */
330 {
331     SymTable* S;
332
333     /* New lexical level */
334     ++LexicalLevel;
335
336     /* Get a new symbol table and make it current */
337     S = NewSymTable (SYMTAB_SIZE_BLOCK);
338     S->PrevTab  = SymTab;
339     SymTab      = S;
340
341     /* Get a new tag table and make it current */
342     S = NewSymTable (SYMTAB_SIZE_BLOCK);
343     S->PrevTab = TagTab;
344     TagTab     = S;
345 }
346
347
348
349 void LeaveBlockLevel (void)
350 /* Leave a nested block in a function */
351 {
352     /* Leave the lexical level */
353     --LexicalLevel;
354
355     /* Check the tables */
356     CheckSymTable (SymTab);
357
358     /* Don't delete the tables */
359     SymTab = SymTab->PrevTab;
360     TagTab = TagTab->PrevTab;
361 }
362
363
364
365 void EnterStructLevel (void)
366 /* Enter a nested block for a struct definition */
367 {
368     SymTable* S;
369
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.
373      */
374     S = NewSymTable (SYMTAB_SIZE_BLOCK);
375     S->PrevTab  = SymTab;
376     SymTab      = S;
377 }
378
379
380
381 void LeaveStructLevel (void)
382 /* Leave a nested block for a struct definition */
383 {
384     /* Don't delete the table */
385     SymTab = SymTab->PrevTab;
386 }
387
388
389
390 /*****************************************************************************/
391 /*                              Find functions                               */
392 /*****************************************************************************/
393
394
395
396 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
397 /* Search for an entry in one table */
398 {
399     /* Get the start of the hash chain */
400     SymEntry* E = T->Tab [Hash % T->Size];
401     while (E) {
402         /* Compare the name */
403         if (strcmp (E->Name, Name) == 0) {
404             /* Found */
405             return E;
406         }
407         /* Not found, next entry in hash chain */
408         E = E->NextHash;
409     }
410
411     /* Not found */
412     return 0;
413 }
414
415
416
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 */
419 {
420     /* Get the hash over the name */
421     unsigned Hash = HashStr (Name);
422
423     /* Check all symbol tables for the symbol */
424     while (Tab) {
425         /* Try to find the symbol in this table */
426         SymEntry* E = FindSymInTable (Tab, Name, Hash);
427
428         /* Bail out if we found it */
429         if (E != 0) {
430             return E;
431         }
432
433         /* Repeat the search in the next higher lexical level */
434         Tab = Tab->PrevTab;
435     }
436
437     /* Not found */
438     return 0;
439 }
440
441
442
443 SymEntry* FindSym (const char* Name)
444 /* Find the symbol with the given name */
445 {
446     return FindSymInTree (SymTab, Name);
447 }
448
449
450
451 SymEntry* FindGlobalSym (const char* Name)
452 /* Find the symbol with the given name in the global symbol table only */
453 {
454     return FindSymInTable (SymTab0, Name, HashStr (Name));
455 }
456
457
458
459 SymEntry* FindLocalSym (const char* Name)
460 /* Find the symbol with the given name in the current symbol table only */
461 {
462     return FindSymInTable (SymTab, Name, HashStr (Name));
463 }
464
465
466
467 SymEntry* FindTagSym (const char* Name)
468 /* Find the symbol with the given name in the tag table */
469 {
470     return FindSymInTree (TagTab, Name);
471 }
472
473
474
475 SymEntry* FindStructField (const type* Type, const char* Name)
476 /* Find a struct field in the fields list */
477 {
478     SymEntry* Field = 0;
479
480     /* The given type may actually be a pointer to struct */
481     if (Type[0] == T_PTR) {
482         ++Type;
483     }
484
485     /* Non-structs do not have any struct fields... */
486     if (IsClassStruct (Type)) {
487
488         const SymTable* Tab;
489
490         /* Get a pointer to the struct/union type */
491         const SymEntry* Struct = (const SymEntry*) Decode (Type+1);
492         CHECK (Struct != 0);
493
494         /* Get the field symbol table from the struct entry.
495          * Beware: The table may not exist.
496          */
497         Tab = Struct->V.S.SymTab;
498
499         /* Now search in the struct symbol table */
500         if (Tab) {
501             Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
502         }
503     }
504
505     return Field;
506 }
507
508
509
510 /*****************************************************************************/
511 /*                       Add stuff to the symbol table                       */
512 /*****************************************************************************/
513
514
515
516 static void AddSymEntry (SymTable* T, SymEntry* S)
517 /* Add a symbol to a symbol table */
518 {
519     /* Get the hash value for the name */
520     unsigned Hash = HashStr (S->Name) % T->Size;
521
522     /* Insert the symbol into the list of all symbols in this level */
523     if (T->SymTail) {
524         T->SymTail->NextSym = S;
525     }
526     S->PrevSym = T->SymTail;
527     T->SymTail = S;
528     if (T->SymHead == 0) {
529         /* First symbol */
530         T->SymHead = S;
531     }
532     ++T->SymCount;
533
534     /* Insert the symbol into the hash chain */
535     S->NextHash  = T->Tab[Hash];
536     T->Tab[Hash] = S;
537
538     /* Tell the symbol in which table it is */
539     S->Owner = T;
540 }
541
542
543
544 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
545 /* Add a struct/union entry and return it */
546 {
547     /* Do we have an entry with this name already? */
548     SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
549     if (Entry) {
550
551         /* We do have an entry. This may be a forward, so check it. */
552         if ((Entry->Flags & SC_STRUCT) == 0) {
553             /* Existing symbol is not a struct */
554             Error ("Symbol `%s' is already different kind", Name);
555         } else if (Size > 0 && Entry->V.S.Size > 0) {
556             /* Both structs are definitions. */
557             Error ("Multiple definition for `%s'", Name);
558         } else {
559             /* Define the struct size if it is given */
560             if (Size > 0) {
561                 Entry->V.S.SymTab = Tab;
562                 Entry->V.S.Size   = Size;
563             }
564         }
565
566     } else {
567
568         /* Create a new entry */
569         Entry = NewSymEntry (Name, SC_STRUCT);
570
571         /* Set the struct data */
572         Entry->V.S.SymTab = Tab;
573         Entry->V.S.Size   = Size;
574
575         /* Add it to the current table */
576         AddSymEntry (TagTab, Entry);
577     }
578
579     /* Return the entry */
580     return Entry;
581 }
582
583
584
585 SymEntry* AddConstSym (const char* Name, const type* Type, unsigned Flags, long Val)
586 /* Add an constant symbol to the symbol table and return it */
587 {
588     /* Enums must be inserted in the global symbol table */
589     SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
590
591     /* Do we have an entry with this name already? */
592     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
593     if (Entry) {
594         if ((Entry->Flags & SC_CONST) != SC_CONST) {
595             Error ("Symbol `%s' is already different kind", Name);
596         } else {
597             Error ("Multiple definition for `%s'", Name);
598         }
599         return Entry;
600     }
601
602     /* Create a new entry */
603     Entry = NewSymEntry (Name, Flags);
604
605     /* Enum values are ints */
606     Entry->Type = TypeDup (Type);
607
608     /* Set the enum data */
609     Entry->V.ConstVal = Val;
610
611     /* Add the entry to the symbol table */
612     AddSymEntry (Tab, Entry);
613
614     /* Return the entry */
615     return Entry;
616 }
617
618
619
620 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
621 /* Add a goto label to the label table */
622 {
623     /* Do we have an entry with this name already? */
624     SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
625     if (Entry) {
626
627         if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
628             /* Trying to define the label more than once */
629             Error ("Label `%s' is defined more than once", Name);
630         }
631         Entry->Flags |= Flags;
632
633     } else {
634
635         /* Create a new entry */
636         Entry = NewSymEntry (Name, SC_LABEL | Flags);
637
638         /* Set a new label number */
639         Entry->V.Label = GetLocalLabel ();
640
641         /* Generate the assembler name of the label */
642         Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
643
644         /* Add the entry to the label table */
645         AddSymEntry (LabelTab, Entry);
646
647     }
648
649     /* Return the entry */
650     return Entry;
651 }
652
653
654
655 SymEntry* AddLocalSym (const char* Name, const type* Type, unsigned Flags, int Offs)
656 /* Add a local symbol and return the symbol entry */
657 {
658     /* Do we have an entry with this name already? */
659     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
660     if (Entry) {
661
662         /* We have a symbol with this name already */
663         Error ("Multiple definition for `%s'", Name);
664
665     } else {
666
667         /* Create a new entry */
668         Entry = NewSymEntry (Name, Flags);
669
670         /* Set the symbol attributes */
671         Entry->Type   = TypeDup (Type);
672         if ((Flags & SC_AUTO) == SC_AUTO) {
673             Entry->V.Offs = Offs;
674         } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
675             Entry->V.R.RegOffs  = Offs;
676             Entry->V.R.SaveOffs = StackPtr;      
677         } else if ((Flags & SC_STATIC) == SC_STATIC) {
678             /* Generate the assembler name from the label number */
679             Entry->V.Label = Offs;
680             Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
681         } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
682             Entry->V.Offs = Offs;
683         } else {
684             Internal ("Invalid flags in AddLocalSym: %04X", Flags);
685         }
686
687         /* Add the entry to the symbol table */
688         AddSymEntry (SymTab, Entry);
689
690     }
691
692     /* Return the entry */
693     return Entry;
694 }
695
696
697
698 SymEntry* AddGlobalSym (const char* Name, const type* Type, unsigned Flags)
699 /* Add an external or global symbol to the symbol table and return the entry */
700 {
701     /* There is some special handling for functions, so check if it is one */
702     int IsFunc = IsTypeFunc (Type);
703
704     /* Functions must be inserted in the global symbol table */
705     SymTable* Tab = IsFunc? SymTab0 : SymTab;
706
707     /* Do we have an entry with this name already? */
708     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
709     if (Entry) {
710
711         type* EType;
712
713         /* We have a symbol with this name already */
714         if (Entry->Flags & SC_TYPE) {
715             Error ("Multiple definition for `%s'", Name);
716             return Entry;
717         }
718
719         /* Get the type string of the existing symbol */
720         EType = Entry->Type;
721
722         /* If we are handling arrays, the old entry or the new entry may be an
723          * incomplete declaration. Accept this, and if the exsting entry is
724          * incomplete, complete it.
725          */
726         if (IsTypeArray (Type) && IsTypeArray (EType)) {
727
728             /* Get the array sizes */
729             long Size  = GetElementCount (Type);
730             long ESize = GetElementCount (EType);
731
732             if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
733                 TypeCmp (Type+DECODE_SIZE+1, EType+DECODE_SIZE+1) < TC_EQUAL) {
734                 /* Types not identical: Conflicting types */
735                 Error ("Conflicting types for `%s'", Name);
736                 return Entry;
737             } else {
738                 /* Check if we have a size in the existing definition */
739                 if (ESize == UNSPECIFIED) {
740                     /* Existing, size not given, use size from new def */
741                     Encode (EType + 1, Size);
742                 }
743             }
744
745         } else {
746             /* New type must be identical */
747             if (TypeCmp (EType, Type) < TC_EQUAL) {
748                 Error ("Conflicting types for `%s'", Name);
749                 return Entry;
750             }
751
752             /* In case of a function, use the new type descriptor, since it
753              * contains pointers to the new symbol tables that are needed if
754              * an actual function definition follows.
755              */
756             if (IsFunc) {
757                 /* Get the function descriptor from the new type */
758                 FuncDesc* F = GetFuncDesc (Type);
759                 /* Use this new function descriptor */
760                 Entry->V.F.Func = F;
761                 EncodePtr (EType+1, F);
762             }
763         }
764
765         /* Add the new flags */
766         Entry->Flags |= Flags;
767
768     } else {
769
770         unsigned Len;
771
772         /* Create a new entry */
773         Entry = NewSymEntry (Name, Flags);
774
775         /* Set the symbol attributes */
776         Entry->Type = TypeDup (Type);
777
778         /* If this is a function, set the function descriptor and clear
779          * additional fields.
780          */
781         if (IsFunc) {
782             Entry->V.F.Func = GetFuncDesc (Entry->Type);
783             Entry->V.F.Seg  = 0;
784         }
785
786         /* Add the assembler name of the symbol */
787         Len = strlen (Name);
788         Entry->AsmName = xmalloc (Len + 2);
789         Entry->AsmName[0] = '_';
790         memcpy (Entry->AsmName+1, Name, Len+1);
791
792         /* Add the entry to the symbol table */
793         AddSymEntry (Tab, Entry);
794     }
795
796     /* Return the entry */
797     return Entry;
798 }
799
800
801
802 /*****************************************************************************/
803 /*                                   Code                                    */
804 /*****************************************************************************/
805
806
807
808 SymTable* GetSymTab (void)
809 /* Return the current symbol table */
810 {
811     return SymTab;
812 }
813
814
815
816 SymTable* GetGlobalSymTab (void)
817 /* Return the global symbol table */
818 {
819     return SymTab0;
820 }
821
822
823
824 int SymIsLocal (SymEntry* Sym)
825 /* Return true if the symbol is defined in the highest lexical level */
826 {
827     return (Sym->Owner == SymTab || Sym->Owner == TagTab);
828 }
829
830
831
832 void MakeZPSym (const char* Name)
833 /* Mark the given symbol as zero page symbol */
834 {
835     /* Get the symbol table entry */
836     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
837
838     /* Mark the symbol as zeropage */
839     if (Entry) {
840         Entry->Flags |= SC_ZEROPAGE;
841     } else {
842         Error ("Undefined symbol: `%s'", Name);
843     }
844 }
845
846
847
848 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
849 /* Write the symbol table to the given file */
850 {
851     unsigned Len;
852     const SymEntry* Entry;
853
854     /* Print the header */
855     va_list ap;
856     va_start (ap, Header);
857     fputc ('\n', F);
858     Len = vfprintf (F, Header, ap);
859     va_end (ap);
860     fputc ('\n', F);
861
862     /* Underline the header */
863     while (Len--) {
864         fputc ('=', F);
865     }
866     fputc ('\n', F);
867
868     /* Dump the table */
869     Entry = Tab->SymHead;
870     if (Entry == 0) {
871         fprintf (F, "(empty)\n");
872     } else {
873         while (Entry) {
874             DumpSymEntry (F, Entry);
875             Entry = Entry->NextSym;
876         }
877     }
878     fprintf (F, "\n\n\n");
879 }
880
881
882
883 void EmitExternals (void)
884 /* Write import/export statements for external symbols */
885 {
886     SymEntry* Entry;
887
888     Entry = SymTab->SymHead;
889     while (Entry) {
890         unsigned Flags = Entry->Flags;
891         if (Flags & SC_EXTERN) {
892             /* Only defined or referenced externs */
893             if (SymIsRef (Entry) && !SymIsDef (Entry)) {
894                 /* An import */
895                 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
896             } else if (SymIsDef (Entry)) {
897                 /* An export */
898                 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
899             }
900         }
901         Entry = Entry->NextSym;
902     }
903 }
904
905
906