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