]> git.sur5r.net Git - cc65/blob - src/cc65/symtab.c
Rename Width to BitWidth.
[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-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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* T, 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 (IsTypePtr (T)) {
482         ++T;
483     }
484
485     /* Non-structs do not have any struct fields... */
486     if (IsClassStruct (T)) {
487
488         /* Get a pointer to the struct/union type */
489         const SymEntry* Struct = GetSymEntry (T);
490         CHECK (Struct != 0);
491
492         /* Now search in the struct symbol table. Beware: The table may not
493          * exist.
494          */
495         if (Struct->V.S.SymTab) {
496             Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
497         }
498     }
499
500     return Field;
501 }
502
503
504
505 /*****************************************************************************/
506 /*                       Add stuff to the symbol table                       */
507 /*****************************************************************************/
508
509
510
511 static void AddSymEntry (SymTable* T, SymEntry* S)
512 /* Add a symbol to a symbol table */
513 {
514     /* Get the hash value for the name */
515     unsigned Hash = HashStr (S->Name) % T->Size;
516
517     /* Insert the symbol into the list of all symbols in this level */
518     if (T->SymTail) {
519         T->SymTail->NextSym = S;
520     }
521     S->PrevSym = T->SymTail;
522     T->SymTail = S;
523     if (T->SymHead == 0) {
524         /* First symbol */
525         T->SymHead = S;
526     }
527     ++T->SymCount;
528
529     /* Insert the symbol into the hash chain */
530     S->NextHash  = T->Tab[Hash];
531     T->Tab[Hash] = S;
532
533     /* Tell the symbol in which table it is */
534     S->Owner = T;
535 }
536
537
538
539 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
540 /* Add a struct/union entry and return it */
541 {
542     /* Do we have an entry with this name already? */
543     SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
544     if (Entry) {
545
546         /* We do have an entry. This may be a forward, so check it. */
547         if ((Entry->Flags & SC_STRUCT) == 0) {
548             /* Existing symbol is not a struct */
549             Error ("Symbol `%s' is already different kind", Name);
550         } else if (Size > 0 && Entry->V.S.Size > 0) {
551             /* Both structs are definitions. */
552             Error ("Multiple definition for `%s'", Name);
553         } else {
554             /* Define the struct size if it is given */
555             if (Size > 0) {
556                 Entry->V.S.SymTab = Tab;
557                 Entry->V.S.Size   = Size;
558             }
559         }
560
561     } else {
562
563         /* Create a new entry */
564         Entry = NewSymEntry (Name, SC_STRUCT);
565
566         /* Set the struct data */
567         Entry->V.S.SymTab = Tab;
568         Entry->V.S.Size   = Size;
569
570         /* Add it to the current table */
571         AddSymEntry (TagTab, Entry);
572     }
573
574     /* Return the entry */
575     return Entry;
576 }
577
578
579
580 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
581 /* Add a bit field to the local symbol table and return the symbol entry */
582 {
583     /* Do we have an entry with this name already? */
584     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
585     if (Entry) {
586
587         /* We have a symbol with this name already */
588         Error ("Multiple definition for `%s'", Name);
589
590     } else {
591
592         /* Create a new entry */
593         Entry = NewSymEntry (Name, SC_BITFIELD);
594
595         /* Set the symbol attributes. Bit-fields are always of type unsigned */
596         Entry->Type         = type_uint;
597         Entry->V.B.Offs     = Offs;
598         Entry->V.B.BitOffs  = BitOffs;
599         Entry->V.B.BitWidth = Width;
600
601         /* Add the entry to the symbol table */
602         AddSymEntry (SymTab, Entry);
603
604     }
605
606     /* Return the entry */
607     return Entry;
608 }
609
610
611
612 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
613 /* Add an constant symbol to the symbol table and return it */
614 {
615     /* Enums must be inserted in the global symbol table */
616     SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
617
618     /* Do we have an entry with this name already? */
619     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
620     if (Entry) {
621         if ((Entry->Flags & SC_CONST) != SC_CONST) {
622             Error ("Symbol `%s' is already different kind", Name);
623         } else {
624             Error ("Multiple definition for `%s'", Name);
625         }
626         return Entry;
627     }
628
629     /* Create a new entry */
630     Entry = NewSymEntry (Name, Flags);
631
632     /* Enum values are ints */
633     Entry->Type = TypeDup (T);
634
635     /* Set the enum data */
636     Entry->V.ConstVal = Val;
637
638     /* Add the entry to the symbol table */
639     AddSymEntry (Tab, Entry);
640
641     /* Return the entry */
642     return Entry;
643 }
644
645
646
647 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
648 /* Add a goto label to the label table */
649 {
650     /* Do we have an entry with this name already? */
651     SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
652     if (Entry) {
653
654         if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
655             /* Trying to define the label more than once */
656             Error ("Label `%s' is defined more than once", Name);
657         }
658         Entry->Flags |= Flags;
659
660     } else {
661
662         /* Create a new entry */
663         Entry = NewSymEntry (Name, SC_LABEL | Flags);
664
665         /* Set a new label number */
666         Entry->V.Label = GetLocalLabel ();
667
668         /* Generate the assembler name of the label */
669         Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
670
671         /* Add the entry to the label table */
672         AddSymEntry (LabelTab, Entry);
673
674     }
675
676     /* Return the entry */
677     return Entry;
678 }
679
680
681
682 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
683 /* Add a local symbol and return the symbol entry */
684 {
685     /* Do we have an entry with this name already? */
686     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
687     if (Entry) {
688
689         /* We have a symbol with this name already */
690         Error ("Multiple definition for `%s'", Name);
691
692     } else {
693
694         /* Create a new entry */
695         Entry = NewSymEntry (Name, Flags);
696
697         /* Set the symbol attributes */
698         Entry->Type = TypeDup (T);
699         if ((Flags & SC_AUTO) == SC_AUTO) {
700             Entry->V.Offs = Offs;
701         } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
702             Entry->V.R.RegOffs  = Offs;
703             Entry->V.R.SaveOffs = StackPtr;
704         } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
705             Entry->V.Label = Offs;
706         } else if ((Flags & SC_STATIC) == SC_STATIC) {
707             /* Generate the assembler name from the label number */
708             Entry->V.Label = Offs;
709             Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
710         } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
711             Entry->V.Offs = Offs;
712         } else {
713             Internal ("Invalid flags in AddLocalSym: %04X", Flags);
714         }
715
716         /* Add the entry to the symbol table */
717         AddSymEntry (SymTab, Entry);
718
719     }
720
721     /* Return the entry */
722     return Entry;
723 }
724
725
726
727 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
728 /* Add an external or global symbol to the symbol table and return the entry */
729 {
730     /* There is some special handling for functions, so check if it is one */
731     int IsFunc = IsTypeFunc (T);
732
733     /* Functions must be inserted in the global symbol table */
734     SymTable* Tab = IsFunc? SymTab0 : SymTab;
735
736     /* Do we have an entry with this name already? */
737     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
738     if (Entry) {
739
740         Type* EType;
741
742         /* We have a symbol with this name already */
743         if (Entry->Flags & SC_TYPE) {
744             Error ("Multiple definition for `%s'", Name);
745             return Entry;
746         }
747
748         /* Get the type string of the existing symbol */
749         EType = Entry->Type;
750
751         /* If we are handling arrays, the old entry or the new entry may be an
752          * incomplete declaration. Accept this, and if the exsting entry is
753          * incomplete, complete it.
754          */
755         if (IsTypeArray (T) && IsTypeArray (EType)) {
756
757             /* Get the array sizes */
758             long Size  = GetElementCount (T);
759             long ESize = GetElementCount (EType);
760
761             if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
762                 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
763                 /* Types not identical: Conflicting types */
764                 Error ("Conflicting types for `%s'", Name);
765                 return Entry;
766             } else {
767                 /* Check if we have a size in the existing definition */
768                 if (ESize == UNSPECIFIED) {
769                     /* Existing, size not given, use size from new def */
770                     SetElementCount (EType, Size);
771                 }
772             }
773
774         } else {
775             /* New type must be identical */
776             if (TypeCmp (EType, T) < TC_EQUAL) {
777                 Error ("Conflicting types for `%s'", Name);
778                 return Entry;
779             }
780
781             /* In case of a function, use the new type descriptor, since it
782              * contains pointers to the new symbol tables that are needed if
783              * an actual function definition follows. Be sure not to use the
784              * new descriptor if it contains a function declaration with an
785              * empty parameter list.
786              */
787             if (IsFunc) {
788                 /* Get the function descriptor from the new type */
789                 FuncDesc* F = GetFuncDesc (T);
790                 /* Use this new function descriptor if it doesn't contain
791                  * an empty parameter list.
792                  */
793                 if ((F->Flags & FD_EMPTY) == 0) {
794                     Entry->V.F.Func = F;
795                     SetFuncDesc (EType, F);
796                 }
797             }
798         }
799
800         /* Add the new flags */
801         Entry->Flags |= Flags;
802
803     } else {
804
805         unsigned Len;
806
807         /* Create a new entry */
808         Entry = NewSymEntry (Name, Flags);
809
810         /* Set the symbol attributes */
811         Entry->Type = TypeDup (T);
812
813         /* If this is a function, set the function descriptor and clear
814          * additional fields.
815          */
816         if (IsFunc) {
817             Entry->V.F.Func = GetFuncDesc (Entry->Type);
818             Entry->V.F.Seg  = 0;
819         }
820
821         /* Add the assembler name of the symbol */
822         Len = strlen (Name);
823         Entry->AsmName = xmalloc (Len + 2);
824         Entry->AsmName[0] = '_';
825         memcpy (Entry->AsmName+1, Name, Len+1);
826
827         /* Add the entry to the symbol table */
828         AddSymEntry (Tab, Entry);
829     }
830
831     /* Return the entry */
832     return Entry;
833 }
834
835
836
837 /*****************************************************************************/
838 /*                                   Code                                    */
839 /*****************************************************************************/
840
841
842
843 SymTable* GetSymTab (void)
844 /* Return the current symbol table */
845 {
846     return SymTab;
847 }
848
849
850
851 SymTable* GetGlobalSymTab (void)
852 /* Return the global symbol table */
853 {
854     return SymTab0;
855 }
856
857
858
859 int SymIsLocal (SymEntry* Sym)
860 /* Return true if the symbol is defined in the highest lexical level */
861 {
862     return (Sym->Owner == SymTab || Sym->Owner == TagTab);
863 }
864
865
866
867 void MakeZPSym (const char* Name)
868 /* Mark the given symbol as zero page symbol */
869 {
870     /* Get the symbol table entry */
871     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
872
873     /* Mark the symbol as zeropage */
874     if (Entry) {
875         Entry->Flags |= SC_ZEROPAGE;
876     } else {
877         Error ("Undefined symbol: `%s'", Name);
878     }
879 }
880
881
882
883 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
884 /* Write the symbol table to the given file */
885 {
886     unsigned Len;
887     const SymEntry* Entry;
888
889     /* Print the header */
890     va_list ap;
891     va_start (ap, Header);
892     fputc ('\n', F);
893     Len = vfprintf (F, Header, ap);
894     va_end (ap);
895     fputc ('\n', F);
896
897     /* Underline the header */
898     while (Len--) {
899         fputc ('=', F);
900     }
901     fputc ('\n', F);
902
903     /* Dump the table */
904     Entry = Tab->SymHead;
905     if (Entry == 0) {
906         fprintf (F, "(empty)\n");
907     } else {
908         while (Entry) {
909             DumpSymEntry (F, Entry);
910             Entry = Entry->NextSym;
911         }
912     }
913     fprintf (F, "\n\n\n");
914 }
915
916
917
918 void EmitExternals (void)
919 /* Write import/export statements for external symbols */
920 {
921     SymEntry* Entry;
922
923     Entry = SymTab->SymHead;
924     while (Entry) {
925         unsigned Flags = Entry->Flags;
926         if (Flags & SC_EXTERN) {
927             /* Only defined or referenced externs */
928             if (SymIsRef (Entry) && !SymIsDef (Entry)) {
929                 /* An import */
930                 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
931             } else if (SymIsDef (Entry)) {
932                 /* An export */
933                 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
934             }
935         }
936         Entry = Entry->NextSym;
937     }
938 }
939
940
941