]> git.sur5r.net Git - cc65/blob - src/cc65/symtab.c
Fixed the existing but unused attribute parsing code. Added
[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 (!SymIsTypeDef (Entry)) {
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                         if (IS_Get (&WarnUnusedParam)) {
168                             Warning ("Parameter `%s' is never used", Entry->Name);
169                         }
170                     } else {
171                         if (IS_Get (&WarnUnusedVar)) {
172                             Warning ("`%s' is defined but never used", Entry->Name);
173                         }
174                     }
175                 }
176             }
177
178             /* If the entry is a label, check if it was defined in the function */
179             if (Flags & SC_LABEL) {
180                 if (!SymIsDef (Entry)) {
181                     /* Undefined label */
182                     Error ("Undefined label: `%s'", Entry->Name);
183                 } else if (!SymIsRef (Entry)) {
184                     /* Defined but not used */
185                     if (IS_Get (&WarnUnusedLabel)) {
186                         Warning ("`%s' is defined but never used", Entry->Name);
187                     }
188                 }
189             }
190
191         }
192
193         /* Next entry */
194         Entry = Entry->NextSym;
195     }
196 }
197
198
199
200 /*****************************************************************************/
201 /*                        Handling of lexical levels                         */
202 /*****************************************************************************/
203
204
205
206 unsigned GetLexicalLevel (void)
207 /* Return the current lexical level */
208 {
209     return LexicalLevel;
210 }
211
212
213
214 void EnterGlobalLevel (void)
215 /* Enter the program global lexical level */
216 {
217     /* Safety */
218     PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
219
220     /* Create and assign the symbol table */
221     SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
222
223     /* Create and assign the tag table */
224     TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
225 }
226
227
228
229 void LeaveGlobalLevel (void)
230 /* Leave the program global lexical level */
231 {
232     /* Safety */
233     PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
234
235     /* Check the tables */
236     CheckSymTable (SymTab0);
237
238     /* Dump the tables if requested */
239     if (Debug) {
240         PrintSymTable (SymTab0, stdout, "Global symbol table");
241         PrintSymTable (TagTab0, stdout, "Global tag table");
242     }
243
244     /* Don't delete the symbol and struct tables! */
245     SymTab = 0;
246     TagTab = 0;
247 }
248
249
250
251 void EnterFunctionLevel (void)
252 /* Enter function lexical level */
253 {
254     SymTable* S;
255
256     /* New lexical level */
257     ++LexicalLevel;
258
259     /* Get a new symbol table and make it current */
260     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
261     S->PrevTab = SymTab;
262     SymTab     = S;
263
264     /* Get a new tag table and make it current */
265     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
266     S->PrevTab = TagTab;
267     TagTab  = S;
268
269     /* Create and assign a new label table */
270     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
271 }
272
273
274
275 void RememberFunctionLevel (struct FuncDesc* F)
276 /* Remember the symbol tables for the level and leave the level without checks */
277 {
278     /* Leave the lexical level */
279     --LexicalLevel;
280
281     /* Remember the tables */
282     F->SymTab = SymTab;
283     F->TagTab = TagTab;
284
285     /* Don't delete the tables */
286     SymTab = SymTab->PrevTab;
287     TagTab = TagTab->PrevTab;
288 }
289
290
291
292 void ReenterFunctionLevel (struct FuncDesc* F)
293 /* Reenter the function lexical level using the existing tables from F */
294 {
295     /* New lexical level */
296     ++LexicalLevel;
297
298     /* Make the tables current again */
299     F->SymTab->PrevTab = SymTab;
300     SymTab = F->SymTab;
301
302     F->TagTab->PrevTab = TagTab;
303     TagTab = F->TagTab;
304
305     /* Create and assign a new label table */
306     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
307 }
308
309
310
311 void LeaveFunctionLevel (void)
312 /* Leave function lexical level */
313 {
314     /* Leave the lexical level */
315     --LexicalLevel;
316
317     /* Check the tables */
318     CheckSymTable (SymTab);
319     CheckSymTable (LabelTab);
320
321     /* Drop the label table if it is empty */
322     if (LabelTab->SymCount == 0) {
323         FreeSymTable (LabelTab);
324     }
325
326     /* Don't delete the tables */
327     SymTab = SymTab->PrevTab;
328     TagTab = TagTab->PrevTab;
329     LabelTab  = 0;
330 }
331
332
333
334 void EnterBlockLevel (void)
335 /* Enter a nested block in a function */
336 {
337     SymTable* S;
338
339     /* New lexical level */
340     ++LexicalLevel;
341
342     /* Get a new symbol table and make it current */
343     S = NewSymTable (SYMTAB_SIZE_BLOCK);
344     S->PrevTab  = SymTab;
345     SymTab      = S;
346
347     /* Get a new tag table and make it current */
348     S = NewSymTable (SYMTAB_SIZE_BLOCK);
349     S->PrevTab = TagTab;
350     TagTab     = S;
351 }
352
353
354
355 void LeaveBlockLevel (void)
356 /* Leave a nested block in a function */
357 {
358     /* Leave the lexical level */
359     --LexicalLevel;
360
361     /* Check the tables */
362     CheckSymTable (SymTab);
363
364     /* Don't delete the tables */
365     SymTab = SymTab->PrevTab;
366     TagTab = TagTab->PrevTab;
367 }
368
369
370
371 void EnterStructLevel (void)
372 /* Enter a nested block for a struct definition */
373 {
374     SymTable* S;
375
376     /* Get a new symbol table and make it current. Note: Structs and enums
377      * nested in struct scope are NOT local to the struct but visible in the
378      * outside scope. So we will NOT create a new struct or enum table.
379      */
380     S = NewSymTable (SYMTAB_SIZE_BLOCK);
381     S->PrevTab  = SymTab;
382     SymTab      = S;
383 }
384
385
386
387 void LeaveStructLevel (void)
388 /* Leave a nested block for a struct definition */
389 {
390     /* Don't delete the table */
391     SymTab = SymTab->PrevTab;
392 }
393
394
395
396 /*****************************************************************************/
397 /*                              Find functions                               */
398 /*****************************************************************************/
399
400
401
402 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
403 /* Search for an entry in one table */
404 {
405     /* Get the start of the hash chain */
406     SymEntry* E = T->Tab [Hash % T->Size];
407     while (E) {
408         /* Compare the name */
409         if (strcmp (E->Name, Name) == 0) {
410             /* Found */
411             return E;
412         }
413         /* Not found, next entry in hash chain */
414         E = E->NextHash;
415     }
416
417     /* Not found */
418     return 0;
419 }
420
421
422
423 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
424 /* Find the symbol with the given name in the table tree that starts with T */
425 {
426     /* Get the hash over the name */
427     unsigned Hash = HashStr (Name);
428
429     /* Check all symbol tables for the symbol */
430     while (Tab) {
431         /* Try to find the symbol in this table */
432         SymEntry* E = FindSymInTable (Tab, Name, Hash);
433
434         /* Bail out if we found it */
435         if (E != 0) {
436             return E;
437         }
438
439         /* Repeat the search in the next higher lexical level */
440         Tab = Tab->PrevTab;
441     }
442
443     /* Not found */
444     return 0;
445 }
446
447
448
449 SymEntry* FindSym (const char* Name)
450 /* Find the symbol with the given name */
451 {
452     return FindSymInTree (SymTab, Name);
453 }
454
455
456
457 SymEntry* FindGlobalSym (const char* Name)
458 /* Find the symbol with the given name in the global symbol table only */
459 {
460     return FindSymInTable (SymTab0, Name, HashStr (Name));
461 }
462
463
464
465 SymEntry* FindLocalSym (const char* Name)
466 /* Find the symbol with the given name in the current symbol table only */
467 {
468     return FindSymInTable (SymTab, Name, HashStr (Name));
469 }
470
471
472
473 SymEntry* FindTagSym (const char* Name)
474 /* Find the symbol with the given name in the tag table */
475 {
476     return FindSymInTree (TagTab, Name);
477 }
478
479
480
481 SymEntry* FindStructField (const Type* T, const char* Name)
482 /* Find a struct field in the fields list */
483 {
484     SymEntry* Field = 0;
485
486     /* The given type may actually be a pointer to struct */
487     if (IsTypePtr (T)) {
488         ++T;
489     }
490
491     /* Non-structs do not have any struct fields... */
492     if (IsClassStruct (T)) {
493
494         /* Get a pointer to the struct/union type */
495         const SymEntry* Struct = GetSymEntry (T);
496         CHECK (Struct != 0);
497
498         /* Now search in the struct symbol table. Beware: The table may not
499          * exist.
500          */
501         if (Struct->V.S.SymTab) {
502             Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
503         }
504     }
505
506     return Field;
507 }
508
509
510
511 /*****************************************************************************/
512 /*                       Add stuff to the symbol table                       */
513 /*****************************************************************************/
514
515
516
517 static void AddSymEntry (SymTable* T, SymEntry* S)
518 /* Add a symbol to a symbol table */
519 {
520     /* Get the hash value for the name */
521     unsigned Hash = HashStr (S->Name) % T->Size;
522
523     /* Insert the symbol into the list of all symbols in this level */
524     if (T->SymTail) {
525         T->SymTail->NextSym = S;
526     }
527     S->PrevSym = T->SymTail;
528     T->SymTail = S;
529     if (T->SymHead == 0) {
530         /* First symbol */
531         T->SymHead = S;
532     }
533     ++T->SymCount;
534
535     /* Insert the symbol into the hash chain */
536     S->NextHash  = T->Tab[Hash];
537     T->Tab[Hash] = S;
538
539     /* Tell the symbol in which table it is */
540     S->Owner = T;
541 }
542
543
544
545 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
546 /* Add a struct/union entry and return it */
547 {
548     /* Do we have an entry with this name already? */
549     SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
550     if (Entry) {
551
552         /* We do have an entry. This may be a forward, so check it. */
553         if ((Entry->Flags & SC_STRUCT) == 0) {
554             /* Existing symbol is not a struct */
555             Error ("Symbol `%s' is already different kind", Name);
556         } else if (Size > 0 && Entry->V.S.Size > 0) {
557             /* Both structs are definitions. */
558             Error ("Multiple definition for `%s'", Name);
559         } else {
560             /* Define the struct size if it is given */
561             if (Size > 0) {
562                 Entry->V.S.SymTab = Tab;
563                 Entry->V.S.Size   = Size;
564             }
565         }
566
567     } else {
568
569         /* Create a new entry */
570         Entry = NewSymEntry (Name, SC_STRUCT);
571
572         /* Set the struct data */
573         Entry->V.S.SymTab = Tab;
574         Entry->V.S.Size   = Size;
575
576         /* Add it to the current table */
577         AddSymEntry (TagTab, Entry);
578     }
579
580     /* Return the entry */
581     return Entry;
582 }
583
584
585
586 SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
587 /* Add a bit field to the local symbol table and return the symbol entry */
588 {
589     /* Do we have an entry with this name already? */
590     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
591     if (Entry) {
592
593         /* We have a symbol with this name already */
594         Error ("Multiple definition for `%s'", Name);
595
596     } else {
597
598         /* Create a new entry */
599         Entry = NewSymEntry (Name, SC_BITFIELD);
600
601         /* Set the symbol attributes. Bit-fields are always of type unsigned */
602         Entry->Type         = type_uint;
603         Entry->V.B.Offs     = Offs;
604         Entry->V.B.BitOffs  = BitOffs;
605         Entry->V.B.BitWidth = Width;
606
607         /* Add the entry to the symbol table */
608         AddSymEntry (SymTab, Entry);
609
610     }
611
612     /* Return the entry */
613     return Entry;
614 }
615
616
617
618 SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
619 /* Add an constant symbol to the symbol table and return it */
620 {
621     /* Enums must be inserted in the global symbol table */
622     SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
623
624     /* Do we have an entry with this name already? */
625     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
626     if (Entry) {
627         if ((Entry->Flags & SC_CONST) != SC_CONST) {
628             Error ("Symbol `%s' is already different kind", Name);
629         } else {
630             Error ("Multiple definition for `%s'", Name);
631         }
632         return Entry;
633     }
634
635     /* Create a new entry */
636     Entry = NewSymEntry (Name, Flags);
637
638     /* Enum values are ints */
639     Entry->Type = TypeDup (T);
640
641     /* Set the enum data */
642     Entry->V.ConstVal = Val;
643
644     /* Add the entry to the symbol table */
645     AddSymEntry (Tab, Entry);
646
647     /* Return the entry */
648     return Entry;
649 }
650
651
652
653 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
654 /* Add a goto label to the label table */
655 {
656     /* Do we have an entry with this name already? */
657     SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
658     if (Entry) {
659
660         if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
661             /* Trying to define the label more than once */
662             Error ("Label `%s' is defined more than once", Name);
663         }
664         Entry->Flags |= Flags;
665
666     } else {
667
668         /* Create a new entry */
669         Entry = NewSymEntry (Name, SC_LABEL | Flags);
670
671         /* Set a new label number */
672         Entry->V.Label = GetLocalLabel ();
673
674         /* Generate the assembler name of the label */
675         Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
676
677         /* Add the entry to the label table */
678         AddSymEntry (LabelTab, Entry);
679
680     }
681
682     /* Return the entry */
683     return Entry;
684 }
685
686
687
688 SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
689 /* Add a local symbol and return the symbol entry */
690 {
691     /* Do we have an entry with this name already? */
692     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
693     if (Entry) {
694
695         /* We have a symbol with this name already */
696         Error ("Multiple definition for `%s'", Name);
697
698     } else {
699
700         /* Create a new entry */
701         Entry = NewSymEntry (Name, Flags);
702
703         /* Set the symbol attributes */
704         Entry->Type = TypeDup (T);
705         if ((Flags & SC_AUTO) == SC_AUTO) {
706             Entry->V.Offs = Offs;
707         } else if ((Flags & SC_REGISTER) == SC_REGISTER) {
708             Entry->V.R.RegOffs  = Offs;
709             Entry->V.R.SaveOffs = StackPtr;
710         } else if ((Flags & SC_EXTERN) == SC_EXTERN) {
711             Entry->V.Label = Offs;
712         } else if ((Flags & SC_STATIC) == SC_STATIC) {
713             /* Generate the assembler name from the label number */
714             Entry->V.Label = Offs;
715             Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
716         } else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
717             Entry->V.Offs = Offs;
718         } else {
719             Internal ("Invalid flags in AddLocalSym: %04X", Flags);
720         }
721
722         /* Add the entry to the symbol table */
723         AddSymEntry (SymTab, Entry);
724
725     }
726
727     /* Return the entry */
728     return Entry;
729 }
730
731
732
733 SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
734 /* Add an external or global symbol to the symbol table and return the entry */
735 {
736     /* There is some special handling for functions, so check if it is one */
737     int IsFunc = IsTypeFunc (T);
738
739     /* Functions must be inserted in the global symbol table */
740     SymTable* Tab = IsFunc? SymTab0 : SymTab;
741
742     /* Do we have an entry with this name already? */
743     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
744     if (Entry) {
745
746         Type* EType;
747
748         /* We have a symbol with this name already */
749         if (Entry->Flags & SC_TYPE) {
750             Error ("Multiple definition for `%s'", Name);
751             return Entry;
752         }
753
754         /* Get the type string of the existing symbol */
755         EType = Entry->Type;
756
757         /* If we are handling arrays, the old entry or the new entry may be an
758          * incomplete declaration. Accept this, and if the exsting entry is
759          * incomplete, complete it.
760          */
761         if (IsTypeArray (T) && IsTypeArray (EType)) {
762
763             /* Get the array sizes */
764             long Size  = GetElementCount (T);
765             long ESize = GetElementCount (EType);
766
767             if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
768                 TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
769                 /* Types not identical: Conflicting types */
770                 Error ("Conflicting types for `%s'", Name);
771                 return Entry;
772             } else {
773                 /* Check if we have a size in the existing definition */
774                 if (ESize == UNSPECIFIED) {
775                     /* Existing, size not given, use size from new def */
776                     SetElementCount (EType, Size);
777                 }
778             }
779
780         } else {
781             /* New type must be identical */
782             if (TypeCmp (EType, T) < TC_EQUAL) {
783                 Error ("Conflicting types for `%s'", Name);
784                 return Entry;
785             }
786
787             /* In case of a function, use the new type descriptor, since it
788              * contains pointers to the new symbol tables that are needed if
789              * an actual function definition follows. Be sure not to use the
790              * new descriptor if it contains a function declaration with an
791              * empty parameter list.
792              */
793             if (IsFunc) {
794                 /* Get the function descriptor from the new type */
795                 FuncDesc* F = GetFuncDesc (T);
796                 /* Use this new function descriptor if it doesn't contain
797                  * an empty parameter list.
798                  */
799                 if ((F->Flags & FD_EMPTY) == 0) {
800                     Entry->V.F.Func = F;
801                     SetFuncDesc (EType, F);
802                 }
803             }
804         }
805
806         /* Add the new flags */
807         Entry->Flags |= Flags;
808
809     } else {
810
811         unsigned Len;
812
813         /* Create a new entry */
814         Entry = NewSymEntry (Name, Flags);
815
816         /* Set the symbol attributes */
817         Entry->Type = TypeDup (T);
818
819         /* If this is a function, set the function descriptor and clear
820          * additional fields.
821          */
822         if (IsFunc) {
823             Entry->V.F.Func = GetFuncDesc (Entry->Type);
824             Entry->V.F.Seg  = 0;
825         }
826
827         /* Add the assembler name of the symbol */
828         Len = strlen (Name);
829         Entry->AsmName = xmalloc (Len + 2);
830         Entry->AsmName[0] = '_';
831         memcpy (Entry->AsmName+1, Name, Len+1);
832
833         /* Add the entry to the symbol table */
834         AddSymEntry (Tab, Entry);
835     }
836
837     /* Return the entry */
838     return Entry;
839 }
840
841
842
843 /*****************************************************************************/
844 /*                                   Code                                    */
845 /*****************************************************************************/
846
847
848
849 SymTable* GetSymTab (void)
850 /* Return the current symbol table */
851 {
852     return SymTab;
853 }
854
855
856
857 SymTable* GetGlobalSymTab (void)
858 /* Return the global symbol table */
859 {
860     return SymTab0;
861 }
862
863
864
865 int SymIsLocal (SymEntry* Sym)
866 /* Return true if the symbol is defined in the highest lexical level */
867 {
868     return (Sym->Owner == SymTab || Sym->Owner == TagTab);
869 }
870
871
872
873 void MakeZPSym (const char* Name)
874 /* Mark the given symbol as zero page symbol */
875 {
876     /* Get the symbol table entry */
877     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
878
879     /* Mark the symbol as zeropage */
880     if (Entry) {
881         Entry->Flags |= SC_ZEROPAGE;
882     } else {
883         Error ("Undefined symbol: `%s'", Name);
884     }
885 }
886
887
888
889 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
890 /* Write the symbol table to the given file */
891 {
892     unsigned Len;
893     const SymEntry* Entry;
894
895     /* Print the header */
896     va_list ap;
897     va_start (ap, Header);
898     fputc ('\n', F);
899     Len = vfprintf (F, Header, ap);
900     va_end (ap);
901     fputc ('\n', F);
902
903     /* Underline the header */
904     while (Len--) {
905         fputc ('=', F);
906     }
907     fputc ('\n', F);
908
909     /* Dump the table */
910     Entry = Tab->SymHead;
911     if (Entry == 0) {
912         fprintf (F, "(empty)\n");
913     } else {
914         while (Entry) {
915             DumpSymEntry (F, Entry);
916             Entry = Entry->NextSym;
917         }
918     }
919     fprintf (F, "\n\n\n");
920 }
921
922
923
924 void EmitExternals (void)
925 /* Write import/export statements for external symbols */
926 {
927     SymEntry* Entry;
928
929     Entry = SymTab->SymHead;
930     while (Entry) {
931         unsigned Flags = Entry->Flags;
932         if (Flags & SC_EXTERN) {
933             /* Only defined or referenced externs */
934             if (SymIsRef (Entry) && !SymIsDef (Entry)) {
935                 /* An import */
936                 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
937             } else if (SymIsDef (Entry)) {
938                 /* An export */
939                 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
940             }
941         }
942         Entry = Entry->NextSym;
943     }
944 }
945
946
947