]> git.sur5r.net Git - cc65/blob - src/cc65/symtab.c
Remove io.*, some cleanup
[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     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 #include "../common/hashstr.h"
42 #include "../common/xmalloc.h"
43
44 #include "asmcode.h"
45 #include "asmlabel.h"
46 #include "check.h"
47 #include "codegen.h"
48 #include "datatype.h"
49 #include "declare.h"
50 #include "error.h"
51 #include "funcdesc.h"
52 #include "global.h"
53 #include "symentry.h"
54 #include "symtab.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* An empty symbol table */
65 SymTable        EmptySymTab = {
66     0,          /* PrevTab */
67     0,          /* SymHead */
68     0,          /* SymTail */
69     0,          /* SymCount */
70     1,          /* Size */
71     { 0 }       /* Tab[1] */
72 };
73
74 /* Symbol table sizes */
75 #define SYMTAB_SIZE_GLOBAL      211U
76 #define SYMTAB_SIZE_FUNCTION     29U
77 #define SYMTAB_SIZE_BLOCK        13U
78 #define SYMTAB_SIZE_STRUCT       19U
79 #define SYMTAB_SIZE_LABEL         7U
80
81 /* Predefined lexical levels */
82 #define LEX_LEVEL_GLOBAL        1U
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 ((Flags & SC_DEF) && !(Flags & SC_REF)) {
164                     if (Flags & SC_PARAM) {
165                         Warning (WARN_UNUSED_PARM, Entry->Name);
166                     } else {
167                         Warning (WARN_UNUSED_ITEM, 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 ((Flags & SC_DEF) == 0) {
175                     /* Undefined label */
176                     Error (ERR_UNDEFINED_LABEL, Entry->Name);
177                 } else if ((Flags & SC_REF) == 0) {
178                     /* Defined but not used */
179                     Warning (WARN_UNUSED_ITEM, 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 void EnterGlobalLevel (void)
199 /* Enter the program global lexical level */
200 {
201     /* Safety */
202     PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
203
204     /* Create and assign the symbol table */
205     SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
206
207     /* Create and assign the tag table */
208     TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
209 }
210
211
212
213 void LeaveGlobalLevel (void)
214 /* Leave the program global lexical level */
215 {
216     /* Safety */
217     PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
218
219     /* Check the tables */
220     CheckSymTable (SymTab0);
221
222     /* Dump the tables if requested */
223     if (Debug) {
224         PrintSymTable (SymTab0, stdout, "Global symbol table");
225         PrintSymTable (TagTab0, stdout, "Global tag table");
226     }
227
228     /* Don't delete the symbol and struct tables! */
229     SymTab0 = SymTab = 0;
230     TagTab0 = TagTab = 0;
231 }
232
233
234
235 void EnterFunctionLevel (void)
236 /* Enter function lexical level */
237 {
238     SymTable* S;
239
240     /* New lexical level */
241     ++LexicalLevel;
242
243     /* Get a new symbol table and make it current */
244     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
245     S->PrevTab = SymTab;
246     SymTab     = S;
247
248     /* Get a new tag table and make it current */
249     S = NewSymTable (SYMTAB_SIZE_FUNCTION);
250     S->PrevTab = TagTab;
251     TagTab  = S;
252
253     /* Create and assign a new label table */
254     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
255 }
256
257
258
259 void RememberFunctionLevel (struct FuncDesc* F)
260 /* Remember the symbol tables for the level and leave the level without checks */
261 {
262     /* Leave the lexical level */
263     --LexicalLevel;
264
265     /* Remember the tables */
266     F->SymTab = SymTab;
267     F->TagTab = TagTab;
268
269     /* Don't delete the tables */
270     SymTab = SymTab->PrevTab;
271     TagTab = TagTab->PrevTab;
272 }
273
274
275
276 void ReenterFunctionLevel (struct FuncDesc* F)
277 /* Reenter the function lexical level using the existing tables from F */
278 {
279     /* New lexical level */
280     ++LexicalLevel;
281
282     /* Make the tables current again */
283     F->SymTab->PrevTab = SymTab;
284     SymTab = F->SymTab;
285
286     F->TagTab->PrevTab = TagTab;
287     TagTab = F->TagTab;
288
289     /* Create and assign a new label table */
290     LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
291 }
292
293
294
295 void LeaveFunctionLevel (void)
296 /* Leave function lexical level */
297 {
298     /* Leave the lexical level */
299     --LexicalLevel;
300
301     /* Check the tables */
302     CheckSymTable (SymTab);
303     CheckSymTable (LabelTab);
304
305     /* Drop the label table if it is empty */
306     if (LabelTab->SymCount == 0) {
307         FreeSymTable (LabelTab);
308     }
309
310     /* Don't delete the tables */
311     SymTab = SymTab->PrevTab;
312     TagTab = TagTab->PrevTab;
313     LabelTab  = 0;
314 }
315
316
317
318 void EnterBlockLevel (void)
319 /* Enter a nested block in a function */
320 {
321     SymTable* S;
322
323     /* New lexical level */
324     ++LexicalLevel;
325
326     /* Get a new symbol table and make it current */
327     S = NewSymTable (SYMTAB_SIZE_BLOCK);
328     S->PrevTab  = SymTab;
329     SymTab      = S;
330
331     /* Get a new tag table and make it current */
332     S = NewSymTable (SYMTAB_SIZE_BLOCK);
333     S->PrevTab = TagTab;
334     TagTab     = S;
335 }
336
337
338
339 void LeaveBlockLevel (void)
340 /* Leave a nested block in a function */
341 {
342     /* Leave the lexical level */
343     --LexicalLevel;
344
345     /* Check the tables */
346     CheckSymTable (SymTab);
347
348     /* Don't delete the tables */
349     SymTab = SymTab->PrevTab;
350     TagTab = TagTab->PrevTab;
351 }
352
353
354
355 void EnterStructLevel (void)
356 /* Enter a nested block for a struct definition */
357 {
358     SymTable* S;
359
360     /* Get a new symbol table and make it current. Note: Structs and enums
361      * nested in struct scope are NOT local to the struct but visible in the
362      * outside scope. So we will NOT create a new struct or enum table.
363      */
364     S = NewSymTable (SYMTAB_SIZE_BLOCK);
365     S->PrevTab  = SymTab;
366     SymTab      = S;
367 }
368
369
370
371 void LeaveStructLevel (void)
372 /* Leave a nested block for a struct definition */
373 {
374     /* Don't delete the table */
375     SymTab = SymTab->PrevTab;
376 }
377
378
379
380 /*****************************************************************************/
381 /*                              Find functions                               */
382 /*****************************************************************************/
383
384
385
386 static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
387 /* Search for an entry in one table */
388 {
389     /* Get the start of the hash chain */
390     SymEntry* E = T->Tab [Hash % T->Size];
391     while (E) {
392         /* Compare the name */
393         if (strcmp (E->Name, Name) == 0) {
394             /* Found */
395             return E;
396         }
397         /* Not found, next entry in hash chain */
398         E = E->NextHash;
399     }
400
401     /* Not found */
402     return 0;
403 }
404
405
406
407 static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
408 /* Find the symbol with the given name in the table tree that starts with T */
409 {
410     /* Get the hash over the name */
411     unsigned Hash = HashStr (Name);
412
413     /* Check all symbol tables for the symbol */
414     while (Tab) {
415         /* Try to find the symbol in this table */
416         SymEntry* E = FindSymInTable (Tab, Name, Hash);
417
418         /* Bail out if we found it */
419         if (E != 0) {
420             return E;
421         }
422
423         /* Repeat the search in the next higher lexical level */
424         Tab = Tab->PrevTab;
425     }
426
427     /* Not found */
428     return 0;
429 }
430
431
432
433 SymEntry* FindSym (const char* Name)
434 /* Find the symbol with the given name */
435 {
436     return FindSymInTree (SymTab, Name);
437 }
438
439
440
441 SymEntry* FindLocalSym (const char* Name)
442 /* Find the symbol with the given name in the current symbol table only */
443 {
444     return FindSymInTable (SymTab, Name, HashStr (Name));
445 }
446
447
448
449 SymEntry* FindTagSym (const char* Name)
450 /* Find the symbol with the given name in the tag table */
451 {
452     return FindSymInTree (TagTab, Name);
453 }
454
455
456
457 SymEntry* FindStructField (const type* Type, const char* Name)
458 /* Find a struct field in the fields list */
459 {
460     SymEntry* Field = 0;
461
462     /* The given type may actually be a pointer to struct */
463     if (Type[0] == T_PTR) {
464         ++Type;
465     }
466
467     /* Non-structs do not have any struct fields... */
468     if (IsStruct (Type)) {
469
470         const SymTable* Tab;
471
472         /* Get a pointer to the struct/union type */
473         const SymEntry* Struct = (const SymEntry*) Decode (Type+1);
474         CHECK (Struct != 0);
475
476         /* Get the field symbol table from the struct entry.
477          * Beware: The table may not exist.
478          */
479         Tab = Struct->V.S.SymTab;
480
481         /* Now search in the struct symbol table */
482         if (Tab) {
483             Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
484         }
485     }
486
487     return Field;
488 }
489
490
491
492 /*****************************************************************************/
493 /*                       Add stuff to the symbol table                       */
494 /*****************************************************************************/
495
496
497
498 static void AddSymEntry (SymTable* T, SymEntry* S)
499 /* Add a symbol to a symbol table */
500 {
501     /* Get the hash value for the name */
502     unsigned Hash = HashStr (S->Name) % T->Size;
503
504     /* Insert the symbol into the list of all symbols in this level */
505     if (T->SymTail) {
506         T->SymTail->NextSym = S;
507     }
508     S->PrevSym = T->SymTail;
509     T->SymTail = S;
510     if (T->SymHead == 0) {
511         /* First symbol */
512         T->SymHead = S;
513     }
514     T->SymCount++;
515
516     /* Insert the symbol into the hash chain */
517     S->NextHash  = T->Tab[Hash];
518     T->Tab[Hash] = S;
519
520     /* Tell the symbol in which table it is */
521     S->Owner = T;
522 }
523
524
525
526 SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab)
527 /* Add a struct/union entry and return it */
528 {
529     /* Do we have an entry with this name already? */
530     SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name));
531     if (Entry) {
532
533         /* We do have an entry. This may be a forward, so check it. */
534         if ((Entry->Flags & SC_STRUCT) == 0) {
535             /* Existing symbol is not a struct */
536             Error (ERR_SYMBOL_KIND);
537         } else if (Size > 0 && Entry->V.S.Size > 0) {
538             /* Both structs are definitions. */
539             Error (ERR_MULTIPLE_DEFINITION, Name);
540         } else {
541             /* Define the struct size if it is given */
542             if (Size > 0) {
543                 Entry->V.S.SymTab = Tab;
544                 Entry->V.S.Size   = Size;
545             }
546         }
547
548     } else {
549
550         /* Create a new entry */
551         Entry = NewSymEntry (Name, SC_STRUCT);
552
553         /* Set the struct data */
554         Entry->V.S.SymTab = Tab;
555         Entry->V.S.Size   = Size;
556
557         /* Add it to the current table */
558         AddSymEntry (TagTab, Entry);
559     }
560
561     /* Return the entry */
562     return Entry;
563 }
564
565
566
567 SymEntry* AddEnumSym (const char* Name, int Val)
568 /* Add an enum symbol to the symbol table and return it */
569 {
570     /* Do we have an entry with this name already? */
571     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
572     if (Entry) {
573         if (Entry->Flags != SC_ENUM) {
574             Error (ERR_SYMBOL_KIND);
575         } else {
576             Error (ERR_MULTIPLE_DEFINITION, Name);
577         }
578         return Entry;
579     }
580
581     /* Create a new entry */
582     Entry = NewSymEntry (Name, SC_ENUM);
583
584     /* Enum values are ints */
585     Entry->Type = TypeDup (type_int);
586
587     /* Set the enum data */
588     Entry->V.EnumVal = Val;
589
590     /* Add the entry to the symbol table */
591     AddSymEntry (SymTab, Entry);
592
593     /* Return the entry */
594     return Entry;
595 }
596
597
598
599 SymEntry* AddLabelSym (const char* Name, unsigned Flags)
600 /* Add a goto label to the label table */
601 {
602     /* Do we have an entry with this name already? */
603     SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
604     if (Entry) {
605
606         if ((Entry->Flags & SC_DEF) != 0 && (Flags & SC_DEF) != 0) {
607             /* Trying to define the label more than once */
608             Error (ERR_MULTIPLE_DEFINITION, Name);
609         }
610         Entry->Flags |= Flags;
611
612     } else {
613
614         /* Create a new entry */
615         Entry = NewSymEntry (Name, SC_LABEL | Flags);
616
617         /* Set a new label number */
618         Entry->V.Label = GetLabel ();
619
620         /* Add the entry to the label table */
621         AddSymEntry (LabelTab, Entry);
622
623     }
624
625     /* Return the entry */
626     return Entry;
627 }
628
629
630
631 SymEntry* AddLocalSym (const char* Name, type* Type, unsigned Flags, int Offs)
632 /* Add a local symbol and return the symbol entry */
633 {
634     SymEntry* Entry;
635
636     /* Functions declared inside of functions do always have external linkage */
637     if (Type != 0 && IsFunc (Type)) {
638         if ((Flags & (SC_DEFAULT | SC_EXTERN)) == 0) {
639             Warning (WARN_FUNC_MUST_BE_EXTERN);
640         }
641         Flags = SC_EXTERN;
642     }
643
644     /* Do we have an entry with this name already? */
645     Entry = FindSymInTable (SymTab, Name, HashStr (Name));
646     if (Entry) {
647
648         /* We have a symbol with this name already */
649         Error (ERR_MULTIPLE_DEFINITION, Name);
650
651     } else {
652
653         /* Create a new entry */
654         Entry = NewSymEntry (Name, Flags);
655
656         /* Set the symbol attributes */
657         Entry->Type   = TypeDup (Type);
658         Entry->V.Offs = Offs;
659
660         /* Add the entry to the symbol table */
661         AddSymEntry (SymTab, Entry);
662
663     }
664
665     /* Return the entry */
666     return Entry;
667 }
668
669
670
671 SymEntry* AddGlobalSym (const char* Name, type* Type, unsigned Flags)
672 /* Add an external or global symbol to the symbol table and return the entry */
673 {
674     /* Functions must be inserted in the global symbol table */
675     SymTable* Tab = IsFunc (Type)? SymTab0 : SymTab;
676
677     /* Do we have an entry with this name already? */
678     SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
679     if (Entry) {
680
681         type* EType;
682
683         /* We have a symbol with this name already */
684         if (Entry->Flags & SC_TYPE) {
685             Error (ERR_MULTIPLE_DEFINITION, Name);
686             return Entry;
687         }
688
689         /* Get the type string of the existing symbol */
690         EType = Entry->Type;
691
692         /* If we are handling arrays, the old entry or the new entry may be an
693          * incomplete declaration. Accept this, and if the exsting entry is
694          * incomplete, complete it.
695          */
696         if (IsArray (Type) && IsArray (EType)) {
697
698             /* Get the array sizes */
699             unsigned Size  = Decode (Type + 1);
700             unsigned ESize = Decode (EType + 1);
701
702             if ((Size != 0 && ESize != 0) ||
703                 TypeCmp (Type+DECODE_SIZE+1, EType+DECODE_SIZE+1) != 0) {
704                 /* Types not identical: Duplicate definition */
705                 Error (ERR_MULTIPLE_DEFINITION, Name);
706             } else {
707                 /* Check if we have a size in the existing definition */
708                 if (ESize == 0) {
709                     /* Existing, size not given, use size from new def */
710                     Encode (EType + 1, Size);
711                 }
712             }
713
714         } else {
715             /* New type must be identical */
716             if (!EqualTypes (EType, Type) != 0) {
717                 Error (ERR_MULTIPLE_DEFINITION, Name);
718             }
719
720             /* In case of a function, use the new type descriptor, since it
721              * contains pointers to the new symbol tables that are needed if
722              * an actual function definition follows.
723              */
724             if (IsFunc (Type)) {
725                 CopyEncode (Type+1, EType+1);
726             }
727         }
728
729         /* Add the new flags */
730         Entry->Flags |= Flags;
731
732     } else {
733
734         /* Create a new entry */
735         Entry = NewSymEntry (Name, Flags);
736
737         /* Set the symbol attributes */
738         Entry->Type = TypeDup (Type);
739
740         /* Add the entry to the symbol table */
741         AddSymEntry (Tab, Entry);
742     }
743
744     /* Return the entry */
745     return Entry;
746 }
747
748
749
750 /*****************************************************************************/
751 /*                                   Code                                    */
752 /*****************************************************************************/
753
754
755
756 SymTable* GetSymTab (void)
757 /* Return the current symbol table */
758 {
759     return SymTab;
760 }
761
762
763
764 int SymIsLocal (SymEntry* Sym)
765 /* Return true if the symbol is defined in the highest lexical level */
766 {
767     return (Sym->Owner == SymTab || Sym->Owner == TagTab);
768 }
769
770
771
772 static int EqualSymTables (SymTable* Tab1, SymTable* Tab2)
773 /* Compare two symbol tables. Return 1 if they are equal and 0 otherwise */
774 {
775     /* Compare the parameter lists */
776     SymEntry* Sym1 = Tab1->SymHead;
777     SymEntry* Sym2 = Tab2->SymHead;
778
779     /* Compare the fields */
780     while (Sym1 && Sym2) {
781
782         /* Compare this field */
783         if (!EqualTypes (Sym1->Type, Sym2->Type)) {
784             /* Field types not equal */
785             return 0;
786         }
787
788         /* Get the pointers to the next fields */
789         Sym1 = Sym1->NextSym;
790         Sym2 = Sym2->NextSym;
791     }
792
793     /* Check both pointers against NULL to compare the field count */
794     return (Sym1 == 0 && Sym2 == 0);
795 }
796
797
798
799 int EqualTypes (const type* Type1, const type* Type2)
800 /* Recursively compare two types. Return 1 if the types match, return 0
801  * otherwise.
802  */
803 {
804     int v1, v2;
805     SymEntry* Sym1;
806     SymEntry* Sym2;
807     SymTable* Tab1;
808     SymTable* Tab2;
809     FuncDesc* F1;
810     FuncDesc* F2;
811     int       Ok;
812
813
814     /* Shortcut here: If the pointers are identical, the types are identical */
815     if (Type1 == Type2) {
816         return 1;
817     }
818
819     /* Compare two types. Determine, where they differ */
820     while (*Type1 == *Type2 && *Type1 != T_END) {
821
822         switch (*Type1) {
823
824             case T_FUNC:
825                 /* Compare the function descriptors */
826                 F1 = DecodePtr (Type1+1);
827                 F2 = DecodePtr (Type2+1);
828
829                 /* If one of the functions is implicitly declared, both
830                  * functions are considered equal. If one of the functions is
831                  * old style, and the other is empty, the functions are
832                  * considered equal.
833                  */
834                 if ((F1->Flags & FD_IMPLICIT) != 0 || (F2->Flags & FD_IMPLICIT) != 0) {
835                     Ok = 1;
836                 } else if ((F1->Flags & FD_OLDSTYLE) != 0 && (F2->Flags & FD_EMPTY) != 0) {
837                     Ok = 1;
838                 } else if ((F1->Flags & FD_EMPTY) != 0 && (F2->Flags & FD_OLDSTYLE) != 0) {
839                     Ok = 1;
840                 } else {
841                     Ok = 0;
842                 }
843
844                 if (!Ok) {
845
846                     /* Check the remaining flags */
847                     if ((F1->Flags & ~FD_IGNORE) != (F2->Flags & ~FD_IGNORE)) {
848                         /* Flags differ */
849                         return 0;
850                     }
851
852                     /* Compare the parameter lists */
853                     if (EqualSymTables (F1->SymTab, F2->SymTab) == 0 ||
854                         EqualSymTables (F1->TagTab, F2->TagTab) == 0) {
855                         /* One of the tables is not identical */
856                         return 0;
857                     }
858                 }
859
860                 /* Skip the FuncDesc pointers to compare the return type */
861                 Type1 += DECODE_SIZE;
862                 Type2 += DECODE_SIZE;
863                 break;
864
865             case T_ARRAY:
866                 /* Check member count */
867                 v1 = Decode (Type1+1);
868                 v2 = Decode (Type2+1);
869                 if (v1 != 0 && v2 != 0 && v1 != v2) {
870                     /* Member count given but different */
871                     return 0;
872                 }
873                 Type1 += DECODE_SIZE;
874                 Type2 += DECODE_SIZE;
875                 break;
876
877             case T_STRUCT:
878             case T_UNION:
879                 /* Compare the fields recursively. To do that, we fetch the
880                  * pointer to the struct definition from the type, and compare
881                  * the fields.
882                  */
883                 Sym1 = DecodePtr (Type1+1);
884                 Sym2 = DecodePtr (Type2+1);
885
886                 /* Get the field tables from the struct entry */
887                 Tab1 = Sym1->V.S.SymTab;
888                 Tab2 = Sym2->V.S.SymTab;
889
890                 /* One or both structs may be forward definitions. In this case,
891                  * the symbol tables are both non existant. Assume that the
892                  * structs are equal in this case.
893                  */
894                 if (Tab1 != 0 && Tab2 != 0) {
895
896                     if (EqualSymTables (Tab1, Tab2) == 0) {
897                         /* Field lists are not equal */
898                         return 0;
899                     }
900
901                 }
902
903                 /* Structs are equal */
904                 Type1 += DECODE_SIZE;
905                 Type2 += DECODE_SIZE;
906                 break;
907         }
908         ++Type1;
909         ++Type2;
910     }
911
912     /* Done, types are equal */
913     return 1;
914 }
915
916
917
918 void MakeZPSym (const char* Name)
919 /* Mark the given symbol as zero page symbol */
920 {
921     /* Get the symbol table entry */
922     SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
923
924     /* Mark the symbol as zeropage */
925     if (Entry) {
926         Entry->Flags |= SC_ZEROPAGE;
927     } else {
928         Error (ERR_UNDEFINED_SYMBOL, Name);
929     }
930 }
931
932
933
934 void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
935 /* Write the symbol table to the given file */
936 {
937     unsigned Len;
938     const SymEntry* Entry;
939
940     /* Print the header */
941     va_list ap;
942     va_start (ap, Header);
943     fputc ('\n', F);
944     Len = vfprintf (F, Header, ap);
945     va_end (ap);
946     fputc ('\n', F);
947
948     /* Underline the header */
949     while (Len--) {
950         fputc ('=', F);
951     }
952     fputc ('\n', F);
953
954     /* Dump the table */
955     Entry = Tab->SymHead;
956     if (Entry == 0) {
957         fprintf (F, "(empty)\n");
958     } else {
959         while (Entry) {
960             DumpSymEntry (F, Entry);
961             Entry = Entry->NextSym;
962         }
963     }
964     fprintf (F, "\n\n\n");
965 }
966
967
968
969 void EmitExternals (void)
970 /* Write import/export statements for external symbols */
971 {
972     SymEntry* Entry;
973
974     AddEmptyLine ();
975
976     Entry = SymTab->SymHead;
977     while (Entry) {
978         unsigned Flags = Entry->Flags;
979         if (Flags & SC_EXTERN) {
980             /* Only defined or referenced externs */
981             if ((Flags & SC_REF) != 0 && (Flags & SC_DEF) == 0) {
982                 /* An import */
983                 g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
984             } else if (Flags & SC_DEF) {
985                 /* An export */
986                 g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
987             }
988         }
989         Entry = Entry->NextSym;
990     }
991 }
992
993
994