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