removed some obsolete switches
[fw/sdcc] / src / SDCCsymt.c
1 /*-------------------------------------------------------------------------
2   SDCCsymt.c - Code file for Symbols table related structures and MACRO's.
3         Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any
8    later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19    In other words, you are welcome to use, share and improve this program.
20    You are forbidden to forbid anyone else to use, share and improve
21    what you give them.   Help stamp out software-hoarding!
22 -------------------------------------------------------------------------*/
23
24 #include "common.h"
25 #include "newalloc.h"
26
27 bucket *SymbolTab[256];         /* the symbol    table  */
28 bucket *StructTab[256];         /* the structure table  */
29 bucket *TypedefTab[256];        /* the typedef   table  */
30 bucket *LabelTab[256];          /* the Label     table  */
31 bucket *enumTab[256];           /* enumerated    table  */
32
33 /*------------------------------------------------------------------*/
34 /* initSymt () - initialises symbol table related stuff             */
35 /*------------------------------------------------------------------*/
36 void 
37 initSymt ()
38 {
39   int i = 0;
40
41   for (i = 0; i < 256; i++)
42     SymbolTab[i] = StructTab[i] = (void *) NULL;
43
44
45 }
46 /*-----------------------------------------------------------------*/
47 /* newBucket - allocates & returns a new bucket        */
48 /*-----------------------------------------------------------------*/
49 bucket *
50 newBucket ()
51 {
52   bucket *bp;
53
54   bp = Safe_calloc (1, sizeof (bucket));
55
56   return bp;
57 }
58
59 /*-----------------------------------------------------------------*/
60 /* hashKey - computes the hashkey given a symbol name              */
61 /*-----------------------------------------------------------------*/
62 int 
63 hashKey (const char *s)
64 {
65   unsigned long key = 0;
66
67   while (*s)
68     key += *s++;
69   return key % 256;
70 }
71
72 /*-----------------------------------------------------------------*/
73 /* addSym - adds a symbol to the hash Table                        */
74 /*-----------------------------------------------------------------*/
75 void 
76 addSym (bucket ** stab,
77         void *sym,
78         char *sname,
79         int level,
80         int block)
81 {
82   int i;                        /* index into the hash Table */
83   bucket *bp;                   /* temp bucket    *         */
84
85   /* the symbols are always added at the head of the list  */
86   i = hashKey (sname);
87   /* get a free entry */
88   bp = Safe_calloc (1, sizeof (bucket));
89
90   bp->sym = sym;                /* update the symbol pointer  */
91   bp->level = level;            /* update the nest level      */
92   bp->block = block;
93   strcpy (bp->name, sname);     /* copy the name into place */
94
95   /* if this is the first entry */
96   if (stab[i] == NULL)
97     {
98       bp->prev = bp->next = (void *) NULL;      /* point to nothing */
99       stab[i] = bp;
100     }
101   /* not first entry then add @ head of list */
102   else
103     {
104       bp->prev = NULL;
105       stab[i]->prev = bp;
106       bp->next = stab[i];
107       stab[i] = bp;
108     }
109 }
110
111 /*-----------------------------------------------------------------*/
112 /* deleteSym - deletes a symbol from the hash Table  entry     */
113 /*-----------------------------------------------------------------*/
114 void 
115 deleteSym (bucket ** stab, void *sym, char *sname)
116 {
117   int i = 0;
118   bucket *bp;
119
120   i = hashKey (sname);
121
122   bp = stab[i];
123   /* find the symbol */
124   while (bp)
125     {
126       if (bp->sym == sym)       /* found it then break out */
127         break;                  /* of the loop       */
128       bp = bp->next;
129     }
130
131   if (!bp)                      /* did not find it */
132     return;
133   /* if this is the first one in the chain */
134   if (!bp->prev)
135     {
136       stab[i] = bp->next;
137       if (stab[i])              /* if chain ! empty */
138         stab[i]->prev = (void *) NULL;
139     }
140   /* middle || end of chain */
141   else
142     {
143       if (bp->next)             /* if not end of chain */
144         bp->next->prev = bp->prev;
145
146       bp->prev->next = bp->next;
147     }
148
149 }
150
151 /*-----------------------------------------------------------------*/
152 /* findSym - finds a symbol in a table           */
153 /*-----------------------------------------------------------------*/
154 void *
155 findSym (bucket ** stab, void *sym, const char *sname)
156 {
157   bucket *bp;
158
159   bp = stab[hashKey (sname)];
160   while (bp)
161     {
162       if (bp->sym == sym || strcmp (bp->name, sname) == 0)
163         break;
164       bp = bp->next;
165     }
166
167   return (bp ? bp->sym : (void *) NULL);
168 }
169
170 /*-----------------------------------------------------------------*/
171 /* findSymWithLevel - finds a symbol with a name & level           */
172 /*-----------------------------------------------------------------*/
173 void *
174 findSymWithLevel (bucket ** stab, symbol * sym)
175 {
176   bucket *bp;
177
178   bp = stab[hashKey (sym->name)];
179
180   /**
181    **  do the search from the head of the list since the
182    **  elements are added at the head it is ensured that
183    ** we will find the deeper definitions before we find
184    ** the global ones. we need to check for symbols with
185    ** level <= to the level given, if levels match then block
186    ** numbers need to match as well
187    **/
188   while (bp)
189     {
190
191       if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
192         {
193           /* if this is parameter then nothing else need to be checked */
194           if (((symbol *) (bp->sym))->_isparm)
195             return (bp->sym);
196           /* if levels match then block numbers hsould also match */
197           if (bp->level && bp->level == sym->level && bp->block == sym->block)
198             return (bp->sym);
199           /* if levels don't match then we are okay */
200           if (bp->level && bp->level != sym->level && bp->block <= sym->block)
201             return (bp->sym);
202           /* if this is a global variable then we are ok too */
203           if (bp->level == 0)
204             return (bp->sym);
205         }
206
207       bp = bp->next;
208     }
209
210   return (void *) NULL;
211 }
212
213 /*-----------------------------------------------------------------*/
214 /* findSymWithBlock - finds a symbol with name in with a block     */
215 /*-----------------------------------------------------------------*/
216 void *
217 findSymWithBlock (bucket ** stab, symbol * sym, int block)
218 {
219   bucket *bp;
220
221   bp = stab[hashKey (sym->name)];
222   while (bp)
223     {
224       if (strcmp (bp->name, sym->name) == 0 &&
225           bp->block <= block)
226         break;
227       bp = bp->next;
228     }
229
230   return (bp ? bp->sym : (void *) NULL);
231 }
232
233 /*------------------------------------------------------------------*/
234 /* newSymbol () - returns a new pointer to a symbol                 */
235 /*------------------------------------------------------------------*/
236 symbol *
237 newSymbol (char *name, int scope)
238 {
239   symbol *sym;
240
241   sym = Safe_calloc (1, sizeof (symbol));
242
243   strcpy (sym->name, name);     /* copy the name    */
244   sym->level = scope;           /* set the level    */
245   sym->block = currBlockno;
246   sym->lineDef = yylineno;      /* set the line number */
247   return sym;
248 }
249
250 /*------------------------------------------------------------------*/
251 /* newLink - creates a new link (declarator,specifier)              */
252 /*------------------------------------------------------------------*/
253 sym_link *
254 newLink ()
255 {
256   sym_link *p;
257
258   p = Safe_calloc (1, sizeof (sym_link));
259
260   return p;
261 }
262
263 /*------------------------------------------------------------------*/
264 /* newStruct - creats a new structdef from the free list            */
265 /*------------------------------------------------------------------*/
266 structdef *
267 newStruct (char *tag)
268 {
269   structdef *s;
270
271   s = Safe_calloc (1, sizeof (structdef));
272
273   strcpy (s->tag, tag);         /* copy the tag            */
274   return s;
275 }
276
277 /*------------------------------------------------------------------*/
278 /* pointerTypes - do the computation for the pointer types          */
279 /*------------------------------------------------------------------*/
280 void 
281 pointerTypes (sym_link * ptr, sym_link * type)
282 {
283   if (IS_SPEC (ptr))
284     return;
285
286   /* find the first pointer type */
287   while (ptr && !IS_PTR (ptr))
288     ptr = ptr->next;
289
290   /* could not find it */
291   if (!ptr || IS_SPEC (ptr) ||
292       DCL_TYPE (ptr) != UPOINTER)
293     return;
294
295   /* change the pointer type depending on the
296      storage class of the type */
297   if (IS_SPEC (type))
298     {
299       DCL_PTR_CONST (ptr) = SPEC_CONST (type);
300       DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
301       switch (SPEC_SCLS (type))
302         {
303         case S_XDATA:
304           DCL_TYPE (ptr) = FPOINTER;
305           break;
306         case S_IDATA:
307           DCL_TYPE (ptr) = IPOINTER;
308           break;
309         case S_PDATA:
310           DCL_TYPE (ptr) = PPOINTER;
311           break;
312         case S_DATA:
313           DCL_TYPE (ptr) = POINTER;
314           break;
315         case S_CODE:
316           DCL_PTR_CONST (ptr) = port->mem.code_ro;
317           DCL_TYPE (ptr) = CPOINTER;
318           break;
319         case S_EEPROM:
320           DCL_TYPE (ptr) = EEPPOINTER;
321           break;
322         default:
323           DCL_TYPE (ptr) = GPOINTER;
324           break;
325         }
326       /* the storage class of type ends here */
327       SPEC_SCLS (type) =
328         SPEC_CONST (type) =
329         SPEC_VOLATILE (type) = 0;
330     }
331
332   /* now change all the remaining unknown pointers
333      to generic pointers */
334   while (ptr)
335     {
336       if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
337         DCL_TYPE (ptr) = GPOINTER;
338       ptr = ptr->next;
339     }
340
341   /* same for the type although it is highly unlikely that
342      type will have a pointer */
343   while (type)
344     {
345       if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
346         DCL_TYPE (type) = GPOINTER;
347       type = type->next;
348     }
349
350 }
351
352 /*------------------------------------------------------------------*/
353 /* addDecl - adds a declarator @ the end of a chain                 */
354 /*------------------------------------------------------------------*/
355 void 
356 addDecl (symbol * sym, int type, sym_link * p)
357 {
358   sym_link *head;
359   sym_link *tail;
360   sym_link *t;
361
362   /* if we are passed a link then set head & tail */
363   if (p)
364     {
365       tail = head = p;
366       while (tail->next)
367         tail = tail->next;
368     }
369   else
370     {
371       head = tail = newLink ();
372       DCL_TYPE (head) = type;
373     }
374
375   /* if this is the first entry   */
376   if (!sym->type)
377     {
378       sym->type = head;
379       sym->etype = tail;
380     }
381   else
382     {
383       if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
384         {
385           sym->etype = mergeSpec (sym->etype, head);
386         }
387       else
388         {
389           if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
390             {
391               t = sym->type;
392               while (t->next != sym->etype)
393                 t = t->next;
394               t->next = head;
395               tail->next = sym->etype;
396             }
397           else
398             {
399               sym->etype->next = head;
400               sym->etype = tail;
401             }
402         }
403     }
404
405   /* if the type is a unknown pointer and has
406      a tspec then take the storage class const & volatile
407      attribute from the tspec & make it those of this
408      symbol */
409   if (p &&
410       !IS_SPEC (p) &&
411       DCL_TYPE (p) == UPOINTER &&
412       DCL_TSPEC (p))
413     {
414       if (!IS_SPEC (sym->etype))
415         {
416           sym->etype = sym->etype->next = newLink ();
417           sym->etype->class = SPECIFIER;
418         }
419       SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
420       SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
421       SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
422       DCL_TSPEC (p) = NULL;
423     }
424   return;
425 }
426
427 /*------------------------------------------------------------------*/
428 /* mergeSpec - merges two specifiers and returns the new one       */
429 /*------------------------------------------------------------------*/
430 sym_link *
431 mergeSpec (sym_link * dest, sym_link * src)
432 {
433   /* if noun different then src overrides */
434   if (SPEC_NOUN (dest) != SPEC_NOUN (src) && !SPEC_NOUN (dest))
435     SPEC_NOUN (dest) = SPEC_NOUN (src);
436
437   /* if destination has no storage class */
438   if (!SPEC_SCLS (dest) || 
439       ((SPEC_SCLS(dest) == S_CONSTANT || SPEC_SCLS(dest) == S_REGISTER) && 
440        SPEC_SCLS (src)))
441     SPEC_SCLS (dest) = SPEC_SCLS (src);
442   /* special case for const */
443   /* copy all the specifications  */
444   SPEC_LONG (dest) |= SPEC_LONG (src);
445   SPEC_SHORT (dest) |= SPEC_SHORT (src);
446   SPEC_USIGN (dest) |= SPEC_USIGN (src);
447   SPEC_STAT (dest) |= SPEC_STAT (src);
448   SPEC_EXTR (dest) |= SPEC_EXTR (src);
449   SPEC_ABSA (dest) |= SPEC_ABSA (src);
450   SPEC_RENT (dest) |= SPEC_RENT (src);
451   SPEC_INTN (dest) |= SPEC_INTN (src);
452   SPEC_BANK (dest) |= SPEC_BANK (src);
453   SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
454   SPEC_CRTCL (dest) |= SPEC_CRTCL (src);
455   SPEC_ADDR (dest) |= SPEC_ADDR (src);
456   SPEC_OCLS (dest) = SPEC_OCLS (src);
457   SPEC_BLEN (dest) |= SPEC_BLEN (src);
458   SPEC_BSTR (dest) |= SPEC_BSTR (src);
459   SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
460   SPEC_NONBANKED (dest) |= SPEC_NONBANKED (src);
461
462   if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
463     SPEC_STRUCT (dest) = SPEC_STRUCT (src);
464
465   return dest;
466 }
467
468 /*------------------------------------------------------------------*/
469 /* cloneSpec - copies the entire spec and returns a new spec        */
470 /*------------------------------------------------------------------*/
471 sym_link *
472 cloneSpec (sym_link * src)
473 {
474   sym_link *spec;
475
476   /* go thru chain till we find the specifier */
477   while (src && src->class != SPECIFIER)
478     src = src->next;
479
480   spec = newLink ();
481   memcpy (spec, src, sizeof (sym_link));
482   return spec;
483 }
484
485 /*------------------------------------------------------------------*/
486 /* genSymName - generates and returns a name used for anonymous vars */
487 /*------------------------------------------------------------------*/
488 char *
489 genSymName (int level)
490 {
491   static int gCount = 0;
492   static char gname[SDCC_NAME_MAX + 1];
493
494   sprintf (gname, "__%04d%04d", level, gCount++);
495   return gname;
496 }
497
498 /*------------------------------------------------------------------*/
499 /* getSpec - returns the specifier part from a declaration chain    */
500 /*------------------------------------------------------------------*/
501 sym_link *
502 getSpec (sym_link * p)
503 {
504   sym_link *loop;
505
506   loop = p;
507   while (p && !(IS_SPEC (p)))
508     p = p->next;
509
510   return p;
511 }
512
513 /*------------------------------------------------------------------*/
514 /* newCharLink() - creates an int type                              */
515 /*------------------------------------------------------------------*/
516 sym_link *
517 newCharLink ()
518 {
519   sym_link *p;
520
521   p = newLink ();
522   p->class = SPECIFIER;
523   SPEC_NOUN (p) = V_CHAR;
524
525   return p;
526 }
527
528 /*------------------------------------------------------------------*/
529 /* newFloatLink - a new Float type                                  */
530 /*------------------------------------------------------------------*/
531 sym_link *
532 newFloatLink ()
533 {
534   sym_link *p;
535
536   p = newLink ();
537   p->class = SPECIFIER;
538   SPEC_NOUN (p) = V_FLOAT;
539
540   return p;
541 }
542
543 /*------------------------------------------------------------------*/
544 /* newLongLink() - new long type                                    */
545 /*------------------------------------------------------------------*/
546 sym_link *
547 newLongLink ()
548 {
549   sym_link *p;
550
551   p = newLink ();
552   p->class = SPECIFIER;
553   SPEC_NOUN (p) = V_INT;
554   SPEC_LONG (p) = 1;
555
556   return p;
557 }
558
559 /*------------------------------------------------------------------*/
560 /* newIntLink() - creates an int type                               */
561 /*------------------------------------------------------------------*/
562 sym_link *
563 newIntLink ()
564 {
565   sym_link *p;
566
567   p = newLink ();
568   p->class = SPECIFIER;
569   SPEC_NOUN (p) = V_INT;
570
571   return p;
572 }
573
574 /*------------------------------------------------------------------*/
575 /* getSize - returns size of a type chain in bits                   */
576 /*------------------------------------------------------------------*/
577 unsigned int 
578 getSize (sym_link * p)
579 {
580   /* if nothing return 0 */
581   if (!p)
582     return 0;
583   if (IS_SPEC (p))
584     {                           /* if this is the specifier then */
585       switch (SPEC_NOUN (p))
586         {                       /* depending on the specifier type */
587         case V_INT:
588           return (IS_LONG (p) ? LONGSIZE : (IS_SHORT (p) ? SHORTSIZE : INTSIZE));
589         case V_FLOAT:
590           return FLOATSIZE;
591         case V_CHAR:
592           return CHARSIZE;
593         case V_VOID:
594           return 0;
595         case V_STRUCT:
596           return SPEC_STRUCT (p)->size;
597         case V_LABEL:
598           return 0;
599         case V_SBIT:
600           return BITSIZE;
601         case V_BIT:
602           return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
603         default:
604           return 0;
605         }
606     }
607
608   /* this is a specifier  */
609   switch (DCL_TYPE (p))
610     {
611     case FUNCTION:
612       return 2;
613     case ARRAY:
614       return DCL_ELEM (p) * getSize (p->next);
615     case IPOINTER:
616     case PPOINTER:
617     case POINTER:
618       return (PTRSIZE);
619     case EEPPOINTER:
620     case FPOINTER:
621     case CPOINTER:
622       return (FPTRSIZE);
623     case GPOINTER:
624       return (GPTRSIZE);
625
626     default:
627       return 0;
628     }
629 }
630
631 /*------------------------------------------------------------------*/
632 /* bitsForType - returns # of bits required to store this type      */
633 /*------------------------------------------------------------------*/
634 unsigned int 
635 bitsForType (sym_link * p)
636 {
637   /* if nothing return 0 */
638   if (!p)
639     return 0;
640
641   if (IS_SPEC (p))
642     {                           /* if this is the specifier then */
643
644       switch (SPEC_NOUN (p))
645         {                       /* depending on the specifier type */
646         case V_INT:
647           return (IS_LONG (p) ? LONGSIZE * 8 : (IS_SHORT (p) ? SHORTSIZE * 8 : INTSIZE * 8));
648         case V_FLOAT:
649           return FLOATSIZE * 8;
650         case V_CHAR:
651           return CHARSIZE * 8;
652         case V_VOID:
653           return 0;
654         case V_STRUCT:
655           return SPEC_STRUCT (p)->size * 8;
656         case V_LABEL:
657           return 0;
658         case V_SBIT:
659           return 1;
660         case V_BIT:
661           return SPEC_BLEN (p);
662         default:
663           return 0;
664         }
665     }
666
667   /* this is a specifier  */
668   switch (DCL_TYPE (p))
669     {
670     case FUNCTION:
671       return 2;
672     case ARRAY:
673       return DCL_ELEM (p) * getSize (p->next) * 8;
674     case IPOINTER:
675     case PPOINTER:
676     case POINTER:
677       return (PTRSIZE * 8);
678     case EEPPOINTER:
679     case FPOINTER:
680     case CPOINTER:
681       return (FPTRSIZE * 8);
682     case GPOINTER:
683       return (GPTRSIZE * 8);
684
685     default:
686       return 0;
687     }
688 }
689
690 /*------------------------------------------------------------------*/
691 /* copySymbolChain - copies a symbol chain                          */
692 /*------------------------------------------------------------------*/
693 symbol *
694 copySymbolChain (symbol * src)
695 {
696   symbol *dest;
697
698   if (!src)
699     return NULL;
700
701   dest = copySymbol (src);
702   dest->next = copySymbolChain (src->next);
703   return dest;
704 }
705
706 /*------------------------------------------------------------------*/
707 /* copySymbol - makes a copy of a symbol                            */
708 /*------------------------------------------------------------------*/
709 symbol *
710 copySymbol (symbol * src)
711 {
712   symbol *dest;
713
714   if (!src)
715     return NULL;
716
717   dest = newSymbol (src->name, src->level);
718   memcpy (dest, src, sizeof (symbol));
719   dest->level = src->level;
720   dest->block = src->block;
721   dest->ival = copyIlist (src->ival);
722   dest->type = copyLinkChain (src->type);
723   dest->etype = getSpec (dest->type);
724   dest->next = NULL;
725   dest->args = copyValueChain (src->args);
726   dest->key = src->key;
727   dest->calleeSave = src->calleeSave;
728   dest->allocreq = src->allocreq;
729   return dest;
730 }
731
732 /*------------------------------------------------------------------*/
733 /* reverseSyms - reverses the links for a symbol chain      */
734 /*------------------------------------------------------------------*/
735 symbol *
736 reverseSyms (symbol * sym)
737 {
738   symbol *prev, *curr, *next;
739
740   if (!sym)
741     return NULL;
742
743   prev = sym;
744   curr = sym->next;
745
746   while (curr)
747     {
748       next = curr->next;
749       curr->next = prev;
750       prev = curr;
751       curr = next;
752     }
753   sym->next = (void *) NULL;
754   return prev;
755 }
756
757 /*------------------------------------------------------------------*/
758 /* reverseLink - reverses the links for a type chain        */
759 /*------------------------------------------------------------------*/
760 sym_link *
761 reverseLink (sym_link * type)
762 {
763   sym_link *prev, *curr, *next;
764
765   if (!type)
766     return NULL;
767
768   prev = type;
769   curr = type->next;
770
771   while (curr)
772     {
773       next = curr->next;
774       curr->next = prev;
775       prev = curr;
776       curr = next;
777     }
778   type->next = (void *) NULL;
779   return prev;
780 }
781
782 /*------------------------------------------------------------------*/
783 /* addSymChain - adds a symbol chain to the symboltable             */
784 /*------------------------------------------------------------------*/
785 void 
786 addSymChain (symbol * symHead)
787 {
788   symbol *sym = symHead;
789   symbol *csym = NULL;
790
791   for (; sym != NULL; sym = sym->next)
792     {
793       changePointer(sym);
794       /* if already exists in the symbol table then check if
795          the previous was an extern definition if yes then
796          then check if the type match, if the types match then
797          delete the current entry and add the new entry      */
798       if ((csym = findSymWithLevel (SymbolTab, sym)) &&
799           csym->level == sym->level)
800         {
801
802           /* previous definition extern ? */
803           if (IS_EXTERN (csym->etype))
804             {
805               /* do types match ? */
806               if (checkType (csym->type, sym->type) != 1)
807                 /* no then error */
808                 werror (E_DUPLICATE, csym->name);
809
810               /* delete current entry */
811               deleteSym (SymbolTab, csym, csym->name);
812               /* add new entry */
813               addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
814             }
815           else                  /* not extern */
816             werror (E_DUPLICATE, sym->name);
817           continue;
818         }
819
820       /* check if previously defined */
821       if (csym && csym->level == sym->level)
822         {
823           /* if the previous one was declared as extern */
824           /* then check the type with the current one         */
825           if (IS_EXTERN (csym->etype))
826             {
827               if (checkType (csym->type, sym->type) <= 0)
828                 werror (W_EXTERN_MISMATCH, csym->name);
829             }
830         }
831
832       addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
833     }
834 }
835
836
837 /*------------------------------------------------------------------*/
838 /* funcInChain - DCL Type 'FUNCTION' found in type chain            */
839 /*------------------------------------------------------------------*/
840 int 
841 funcInChain (sym_link * lnk)
842 {
843   while (lnk)
844     {
845       if (IS_FUNC (lnk))
846         return 1;
847       lnk = lnk->next;
848     }
849   return 0;
850 }
851
852 /*------------------------------------------------------------------*/
853 /* structElemType - returns the type info of a sturct member        */
854 /*------------------------------------------------------------------*/
855 sym_link *
856 structElemType (sym_link * stype, value * id, value ** argsp)
857 {
858   symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
859   sym_link *type, *etype;
860   sym_link *petype = getSpec (stype);
861
862   if (!fields || !id)
863     return NULL;
864
865   /* look for the id */
866   while (fields)
867     {
868       if (strcmp (fields->rname, id->name) == 0)
869         {
870           if (argsp)
871             {
872               *argsp = fields->args;
873             }
874           type = copyLinkChain (fields->type);
875           etype = getSpec (type);
876           SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
877                                SPEC_SCLS (etype) : SPEC_SCLS (petype));
878           return type;
879         }
880       fields = fields->next;
881     }
882   werror (E_NOT_MEMBER, id->name);
883
884   return NULL;
885 }
886
887 /*------------------------------------------------------------------*/
888 /* getStructElement - returns element of a tructure definition      */
889 /*------------------------------------------------------------------*/
890 symbol *
891 getStructElement (structdef * sdef, symbol * sym)
892 {
893   symbol *field;
894
895   for (field = sdef->fields; field; field = field->next)
896     if (strcmp (field->name, sym->name) == 0)
897       return field;
898
899   werror (E_NOT_MEMBER, sym->name);
900
901   return sdef->fields;
902 }
903
904 /*------------------------------------------------------------------*/
905 /* compStructSize - computes the size of a structure                */
906 /*------------------------------------------------------------------*/
907 int 
908 compStructSize (int su, structdef * sdef)
909 {
910     int sum = 0, usum = 0;
911     int bitOffset = 0;
912     symbol *loop;
913
914     /* for the identifiers  */
915     loop = sdef->fields;
916     while (loop) {
917
918         /* create the internal name for this variable */
919         sprintf (loop->rname, "_%s", loop->name);
920         loop->offset = (su == UNION ? sum = 0 : sum);
921         SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
922
923         /* if this is a bit field  */
924         if (loop->bitVar) {
925
926             /* change it to a unsigned bit */
927             SPEC_NOUN (loop->etype) = V_BIT;
928             SPEC_USIGN (loop->etype) = 1;
929             /* check if this fit into the remaining   */
930             /* bits of this byte else align it to the */
931             /* next byte boundary                     */
932             if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
933                 SPEC_BSTR (loop->etype) = bitOffset;
934                 if ((bitOffset += (loop->bitVar % 8)) == 8)
935                     sum++;
936             }
937             else /* does not fit */ {
938                 bitOffset = 0;
939                 SPEC_BSTR (loop->etype) = bitOffset;
940                 sum += (loop->bitVar / 8);
941                 bitOffset += (loop->bitVar % 8);
942             }
943             /* if this is the last field then pad */
944             if (!loop->next && bitOffset && bitOffset != 8) {
945                 bitOffset = 0;
946                 sum++;
947             }
948         }
949         else {
950             checkDecl (loop);
951             sum += getSize (loop->type);
952         }
953
954         /* if function then do the arguments for it */
955         if (funcInChain (loop->type)) {
956             processFuncArgs (loop, 1);
957         }
958
959         loop = loop->next;
960
961         /* if this is not a bitfield but the */
962         /* previous one was and did not take */
963         /* the whole byte then pad the rest  */
964         if ((loop && !loop->bitVar) && bitOffset) {
965             bitOffset = 0;
966             sum++;
967         }
968
969         /* if union then size = sizeof larget field */
970         if (su == UNION)
971             usum = max (usum, sum);
972
973     }
974
975     return (su == UNION ? usum : sum);
976 }
977
978 /*------------------------------------------------------------------*/
979 /* checkSClass - check the storage class specification              */
980 /*------------------------------------------------------------------*/
981 static void 
982 checkSClass (symbol * sym)
983 {
984   /* type is literal can happen foe enums change
985      to auto */
986   if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
987     SPEC_SCLS (sym->etype) = S_AUTO;
988
989   /* if sfr or sbit then must also be */
990   /* volatile the initial value will be xlated */
991   /* to an absolute address */
992   if (SPEC_SCLS (sym->etype) == S_SBIT ||
993       SPEC_SCLS (sym->etype) == S_SFR)
994     {
995       SPEC_VOLATILE (sym->etype) = 1;
996       /* if initial value given */
997       if (sym->ival)
998         {
999           SPEC_ABSA (sym->etype) = 1;
1000           SPEC_ADDR (sym->etype) =
1001             (int) list2int (sym->ival);
1002           sym->ival = NULL;
1003         }
1004     }
1005
1006   /* if absolute address given then it mark it as
1007      volatile */
1008   if (IS_ABSOLUTE (sym->etype))
1009     SPEC_VOLATILE (sym->etype) = 1;
1010
1011   /* global variables declared const put into code */
1012   if (sym->level == 0 &&
1013       SPEC_SCLS (sym->etype) == S_CONSTANT)
1014     {
1015       SPEC_SCLS (sym->etype) = S_CODE;
1016       SPEC_CONST (sym->etype) = 1;
1017     }
1018
1019   /* global variable in code space is a constant */
1020   if (sym->level == 0 &&
1021       SPEC_SCLS (sym->etype) == S_CODE &&
1022       port->mem.code_ro)
1023     SPEC_CONST (sym->etype) = 1;
1024
1025
1026   /* if bit variable then no storage class can be */
1027   /* specified since bit is already a storage */
1028   if (IS_BITVAR (sym->etype) &&
1029       (SPEC_SCLS (sym->etype) != S_FIXED &&
1030        SPEC_SCLS (sym->etype) != S_SBIT &&
1031        SPEC_SCLS (sym->etype) != S_BIT)
1032     )
1033     {
1034       werror (E_BITVAR_STORAGE, sym->name);
1035       SPEC_SCLS (sym->etype) = S_FIXED;
1036     }
1037
1038   /* extern variables cannot be initialized */
1039   if (IS_EXTERN (sym->etype) && sym->ival)
1040     {
1041       werror (E_EXTERN_INIT, sym->name);
1042       sym->ival = NULL;
1043     }
1044
1045   /* if this is an automatic symbol then */
1046   /* storage class will be ignored and   */
1047   /* symbol will be allocated on stack/  */
1048   /* data depending on flag             */
1049   if (sym->level &&
1050       (options.stackAuto || reentrant) &&
1051       (SPEC_SCLS (sym->etype) != S_AUTO &&
1052        SPEC_SCLS (sym->etype) != S_FIXED &&
1053        SPEC_SCLS (sym->etype) != S_REGISTER &&
1054        SPEC_SCLS (sym->etype) != S_STACK &&
1055        SPEC_SCLS (sym->etype) != S_XSTACK &&
1056        SPEC_SCLS (sym->etype) != S_CONSTANT))
1057     {
1058
1059       werror (E_AUTO_ASSUMED, sym->name);
1060       SPEC_SCLS (sym->etype) = S_AUTO;
1061     }
1062
1063   /* automatic symbols cannot be given   */
1064   /* an absolute address ignore it      */
1065   if (sym->level &&
1066       SPEC_ABSA (sym->etype) &&
1067       (options.stackAuto || reentrant))
1068     {
1069       werror (E_AUTO_ABSA, sym->name);
1070       SPEC_ABSA (sym->etype) = 0;
1071     }
1072
1073   /* arrays & pointers cannot be defined for bits   */
1074   /* SBITS or SFRs or BIT                           */
1075   if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1076       (SPEC_NOUN (sym->etype) == V_BIT ||
1077        SPEC_NOUN (sym->etype) == V_SBIT ||
1078        SPEC_SCLS (sym->etype) == S_SFR))
1079     werror (E_BIT_ARRAY, sym->name);
1080
1081   /* if this is a bit|sbit then set length & start  */
1082   if (SPEC_NOUN (sym->etype) == V_BIT ||
1083       SPEC_NOUN (sym->etype) == V_SBIT)
1084     {
1085       SPEC_BLEN (sym->etype) = 1;
1086       SPEC_BSTR (sym->etype) = 0;
1087     }
1088
1089   /* variables declared in CODE space must have */
1090   /* initializers if not an extern */
1091   if (SPEC_SCLS (sym->etype) == S_CODE &&
1092       sym->ival == NULL &&
1093       !sym->level &&
1094       port->mem.code_ro &&
1095       !IS_EXTERN (sym->etype) &&
1096       !funcInChain (sym->type))
1097     werror (E_CODE_NO_INIT, sym->name);
1098
1099   /* if parameter or local variable then change */
1100   /* the storage class to reflect where the var will go */
1101   if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED)
1102     {
1103       if (options.stackAuto || (currFunc && IS_RENT (currFunc->etype)))
1104         {
1105           SPEC_SCLS (sym->etype) = (options.useXstack ?
1106                                     S_XSTACK : S_STACK);
1107         }
1108       else
1109         {
1110           /* hack-o-matic! I see no reason why the useXstack option should ever
1111            * control this allcoation, but the code was originally that way, and
1112            * changing it for non-390 ports breaks the compiler badly.
1113            */
1114           bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1115           SPEC_SCLS (sym->etype) = (useXdata ?
1116                                     S_XDATA : S_FIXED);
1117         }
1118     }
1119 }
1120
1121 /*------------------------------------------------------------------*/
1122 /* changePointer - change pointer to functions                      */
1123 /*------------------------------------------------------------------*/
1124 void 
1125 changePointer (symbol * sym)
1126 {
1127   sym_link *p;
1128
1129   /* go thru the chain of declarations   */
1130   /* if we find a pointer to a function  */
1131   /* unconditionally change it to a ptr  */
1132   /* to code area                        */
1133   for (p = sym->type; p; p = p->next)
1134     {
1135       if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1136         DCL_TYPE (p) = GPOINTER;
1137       if (IS_PTR (p) && IS_FUNC (p->next))
1138         DCL_TYPE (p) = CPOINTER;
1139     }
1140 }
1141
1142 /*------------------------------------------------------------------*/
1143 /* checkDecl - does semantic validation of a declaration                   */
1144 /*------------------------------------------------------------------*/
1145 int 
1146 checkDecl (symbol * sym)
1147 {
1148
1149   checkSClass (sym);            /* check the storage class      */
1150   changePointer (sym);          /* change pointers if required */
1151
1152   /* if this is an array without any dimension
1153      then update the dimension from the initial value */
1154   if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1155     DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1156
1157   return 0;
1158 }
1159
1160 /*------------------------------------------------------------------*/
1161 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1162 /*------------------------------------------------------------------*/
1163 sym_link *
1164 copyLinkChain (sym_link * p)
1165 {
1166   sym_link *head, *curr, *loop;
1167
1168   curr = p;
1169   head = loop = (curr ? newLink () : (void *) NULL);
1170   while (curr)
1171     {
1172       memcpy (loop, curr, sizeof (sym_link));   /* copy it */
1173       loop->next = (curr->next ? newLink () : (void *) NULL);
1174       loop = loop->next;
1175       curr = curr->next;
1176     }
1177
1178   return head;
1179 }
1180
1181
1182 /*------------------------------------------------------------------*/
1183 /* cleanUpBlock - cleansup the symbol table specified for all the   */
1184 /*                symbols in the given block                        */
1185 /*------------------------------------------------------------------*/
1186 void 
1187 cleanUpBlock (bucket ** table, int block)
1188 {
1189   int i;
1190   bucket *chain;
1191
1192   /* go thru the entire  table  */
1193   for (i = 0; i < 256; i++)
1194     {
1195       for (chain = table[i]; chain; chain = chain->next)
1196         {
1197           if (chain->block >= block)
1198             {
1199               deleteSym (table, chain->sym, chain->name);
1200             }
1201         }
1202     }
1203 }
1204
1205 /*------------------------------------------------------------------*/
1206 /* cleanUpLevel - cleansup the symbol table specified for all the   */
1207 /*                symbols in the given level                        */
1208 /*------------------------------------------------------------------*/
1209 void 
1210 cleanUpLevel (bucket ** table, int level)
1211 {
1212   int i;
1213   bucket *chain;
1214
1215   /* go thru the entire  table  */
1216   for (i = 0; i < 256; i++)
1217     {
1218       for (chain = table[i]; chain; chain = chain->next)
1219         {
1220           if (chain->level >= level)
1221             {
1222               deleteSym (table, chain->sym, chain->name);
1223             }
1224         }
1225     }
1226 }
1227
1228 /*------------------------------------------------------------------*/
1229 /* computeType - computes the resultant type from two types         */
1230 /*------------------------------------------------------------------*/
1231 sym_link *
1232 computeType (sym_link * type1, sym_link * type2)
1233 {
1234   sym_link *rType;
1235   sym_link *reType;
1236   sym_link *etype1 = getSpec (type1);
1237   sym_link *etype2 = getSpec (type2);
1238
1239   /* if one of them is a float then result is a float */
1240   /* here we assume that the types passed are okay */
1241   /* and can be cast to one another                */
1242   /* which ever is greater in size */
1243   if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1244     rType = newFloatLink ();
1245   else
1246     /* if only one of them is a bit variable
1247        then the other one prevails */
1248   if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1249     rType = copyLinkChain (type2);
1250   else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1251     rType = copyLinkChain (type1);
1252   else
1253     /* if one of them is a pointer then that
1254        prevails */
1255   if (IS_PTR (type1))
1256     rType = copyLinkChain (type1);
1257   else if (IS_PTR (type2))
1258     rType = copyLinkChain (type2);
1259   else if (getSize (type1) > getSize (type2))
1260     rType = copyLinkChain (type1);
1261   else
1262     rType = copyLinkChain (type2);
1263
1264   reType = getSpec (rType);
1265
1266   /* if either of them unsigned then make this unsigned */
1267   if ((SPEC_USIGN (etype1) || SPEC_USIGN (etype2)) && !IS_FLOAT (reType))
1268     SPEC_USIGN (reType) = 1;
1269
1270   /* if result is a literal then make not so */
1271   if (IS_LITERAL (reType))
1272     SPEC_SCLS (reType) = S_REGISTER;
1273
1274   return rType;
1275 }
1276
1277 /*------------------------------------------------------------------*/
1278 /* checkType - will do type check return 1 if match                 */
1279 /*------------------------------------------------------------------*/
1280 int 
1281 checkType (sym_link * dest, sym_link * src)
1282 {
1283   if (!dest && !src)
1284     return 1;
1285
1286   if (dest && !src)
1287     return 0;
1288
1289   if (src && !dest)
1290     return 0;
1291
1292   /* if dest is a declarator then */
1293   if (IS_DECL (dest))
1294     {
1295       if (IS_DECL (src))
1296         {
1297           if (DCL_TYPE (src) == DCL_TYPE (dest))
1298             return checkType (dest->next, src->next);
1299           else if (IS_PTR (src) && IS_PTR (dest))
1300             return -1;
1301           else if (IS_PTR (dest) && IS_ARRAY (src))
1302             return -1;
1303           else if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1304             return -1 * checkType (dest->next, src);
1305           else
1306             return 0;
1307         }
1308       else if (IS_PTR (dest) && IS_INTEGRAL (src))
1309         return -1;
1310       else
1311         return 0;
1312     }
1313
1314   /* if one is a specifier and the other is not */
1315   if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1316       (IS_SPEC (dest) && !IS_SPEC (src)))
1317     return 0;
1318
1319   /* if one of them is a void then ok */
1320   if (SPEC_NOUN (dest) == V_VOID &&
1321       SPEC_NOUN (src) != V_VOID)
1322     return -1;
1323
1324   if (SPEC_NOUN (dest) != V_VOID &&
1325       SPEC_NOUN (src) == V_VOID)
1326     return -1;
1327
1328   /* char === to short */
1329   if (SPEC_NOUN (dest) == V_CHAR &&
1330       SPEC_NOUN (src) == V_INT &&
1331       SPEC_SHORT (src))
1332     return (SPEC_USIGN (src) == SPEC_USIGN (dest) ? 1 : -2);
1333
1334   if (SPEC_NOUN (src) == V_CHAR &&
1335       SPEC_NOUN (dest) == V_INT &&
1336       SPEC_SHORT (dest))
1337     return (SPEC_USIGN (src) == SPEC_USIGN (dest) ? 1 : -2);
1338
1339   /* if they are both bitfields then if the lengths
1340      and starts don't match */
1341   if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1342       (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1343        SPEC_BSTR (dest) != SPEC_BSTR (src)))
1344     return -1;
1345
1346   /* it is a specifier */
1347   if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1348     {
1349       if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1350           IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1351           getSize (dest) == getSize (src))
1352         return 1;
1353       else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1354         return -1;
1355       else
1356         return 0;
1357     }
1358   else if (IS_STRUCT (dest))
1359     {
1360       if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1361         return 0;
1362       else
1363         return 1;
1364     }
1365   if (SPEC_LONG (dest) != SPEC_LONG (src))
1366     return -1;
1367
1368   if (SPEC_SHORT (dest) != SPEC_SHORT (src))
1369     return -1;
1370
1371   if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1372     return -2;
1373
1374   return 1;
1375 }
1376
1377 /*------------------------------------------------------------------*/
1378 /* inCalleeSaveList - return 1 if found in calle save list          */
1379 /*------------------------------------------------------------------*/
1380 bool 
1381 inCalleeSaveList (char *s)
1382 {
1383   int i;
1384
1385   for (i = 0; options.calleeSaves[i]; i++)
1386     if (strcmp (options.calleeSaves[i], s) == 0)
1387       return 1;
1388
1389   return 0;
1390 }
1391
1392 /*-----------------------------------------------------------------*/
1393 /* aggregateArgToPointer:  change an agggregate type function      */
1394 /*         argument to a pointer to that type.     */
1395 /*-----------------------------------------------------------------*/
1396 void 
1397 aggregateArgToPointer (value * val)
1398 {
1399   if (IS_AGGREGATE (val->type))
1400     {
1401       /* if this is a structure */
1402       /* then we need to add a new link */
1403       if (IS_STRUCT (val->type))
1404         {
1405           /* first lets add DECLARATOR type */
1406           sym_link *p = val->type;
1407
1408           werror (W_STRUCT_AS_ARG, val->name);
1409           val->type = newLink ();
1410           val->type->next = p;
1411         }
1412
1413       /* change to a pointer depending on the */
1414       /* storage class specified        */
1415       switch (SPEC_SCLS (val->etype))
1416         {
1417         case S_IDATA:
1418           DCL_TYPE (val->type) = IPOINTER;
1419           break;
1420         case S_PDATA:
1421           DCL_TYPE (val->type) = PPOINTER;
1422           break;
1423         case S_FIXED:
1424           if (TARGET_IS_DS390)
1425             {
1426               /* The AUTO and REGISTER classes should probably
1427                * also become generic pointers, but I haven't yet
1428                * devised a test case for that.
1429                */
1430               DCL_TYPE (val->type) = GPOINTER;
1431               break;
1432             }
1433           /* fall through! */
1434         case S_AUTO:
1435         case S_DATA:
1436         case S_REGISTER:
1437           DCL_TYPE (val->type) = POINTER;
1438           break;
1439         case S_CODE:
1440           DCL_TYPE (val->type) = CPOINTER;
1441           break;
1442         case S_XDATA:
1443           DCL_TYPE (val->type) = FPOINTER;
1444           break;
1445         case S_EEPROM:
1446           DCL_TYPE (val->type) = EEPPOINTER;
1447           break;
1448         default:
1449           DCL_TYPE (val->type) = GPOINTER;
1450         }
1451
1452       /* is there is a symbol associated then */
1453       /* change the type of the symbol as well */
1454       if (val->sym)
1455         {
1456           val->sym->type = copyLinkChain (val->type);
1457           val->sym->etype = getSpec (val->sym->type);
1458         }
1459     }
1460 }
1461 /*------------------------------------------------------------------*/
1462 /* checkFunction - does all kinds of check on a function            */
1463 /*------------------------------------------------------------------*/
1464 int 
1465 checkFunction (symbol * sym)
1466 {
1467   symbol *csym;
1468   value *exargs, *acargs;
1469   int argCnt = 0;
1470
1471   /* if not type then some kind of error */
1472   if (!sym->type)
1473     return 0;
1474
1475   /* if the function has no type then make it return int */
1476   if (!sym->type->next)
1477     sym->type->next = sym->etype = newIntLink ();
1478
1479   /* function cannot return aggregate */
1480   if (IS_AGGREGATE (sym->type->next))
1481     {
1482       werror (E_FUNC_AGGR, sym->name);
1483       return 0;
1484     }
1485
1486   /* function cannot return bit */
1487   if (IS_BITVAR (sym->type->next))
1488     {
1489       werror (E_FUNC_BIT, sym->name);
1490       return 0;
1491     }
1492
1493   /* check if this function is defined as calleeSaves
1494      then mark it as such */
1495   sym->calleeSave = inCalleeSaveList (sym->name);
1496
1497   /* if interrupt service routine  */
1498   /* then it cannot have arguments */
1499   if (sym->args && IS_ISR (sym->etype) && !IS_VOID (sym->args->type))
1500     {
1501       werror (E_INT_ARGS, sym->name);
1502       sym->args = NULL;
1503     }
1504
1505   if (!(csym = findSym (SymbolTab, sym, sym->name)))
1506     return 1;                   /* not defined nothing more to check  */
1507
1508   /* check if body already present */
1509   if (csym && csym->fbody)
1510     {
1511       werror (E_FUNC_BODY, sym->name);
1512       return 0;
1513     }
1514
1515   /* check the return value type   */
1516   if (checkType (csym->type, sym->type) <= 0)
1517     {
1518       werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1519       werror (E_CONTINUE, "previous defintion type ");
1520       printTypeChain (csym->type, stderr);
1521       fprintf (stderr, "\n");
1522       werror (E_CONTINUE, "current definition type ");
1523       printTypeChain (sym->type, stderr);
1524       fprintf (stderr, "\n");
1525       return 0;
1526     }
1527
1528   if (SPEC_INTRTN (csym->etype) != SPEC_INTRTN (sym->etype))
1529     {
1530       werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1531       return 0;
1532     }
1533
1534   if (SPEC_BANK (csym->etype) != SPEC_BANK (sym->etype))
1535     {
1536       werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1537       return 0;
1538     }
1539
1540   /* compare expected agrs with actual args */
1541   exargs = csym->args;
1542   acargs = sym->args;
1543
1544   /* for all the expected args do */
1545   for (argCnt = 1;
1546        exargs && acargs;
1547        exargs = exargs->next, acargs = acargs->next, argCnt++)
1548     {
1549       value *checkValue;
1550       /* If the actual argument is an array, any prototype
1551        * will have modified it to a pointer. Duplicate that
1552        * change here.
1553        */
1554       if (IS_AGGREGATE (acargs->type))
1555         {
1556           checkValue = copyValue (acargs);
1557           aggregateArgToPointer (checkValue);
1558         }
1559       else
1560         {
1561           checkValue = acargs;
1562         }
1563
1564       if (checkType (exargs->type, checkValue->type) <= 0)
1565         {
1566           werror (E_ARG_TYPE, argCnt);
1567           return 0;
1568         }
1569     }
1570
1571   /* if one them ended we have a problem */
1572   if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1573       (!exargs && acargs && !IS_VOID (acargs->type)))
1574     werror (E_ARG_COUNT);
1575
1576   /* replace with this defition */
1577   sym->cdef = csym->cdef;
1578   deleteSym (SymbolTab, csym, csym->name);
1579   addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
1580   if (IS_EXTERN (csym->etype) && !
1581       IS_EXTERN (sym->etype))
1582     {
1583       addSet (&publics, sym);
1584     }
1585   return 1;
1586 }
1587
1588 /*-----------------------------------------------------------------*/
1589 /* processFuncArgs - does some processing with function args       */
1590 /*-----------------------------------------------------------------*/
1591 void 
1592 processFuncArgs (symbol * func, int ignoreName)
1593 {
1594   value *val;
1595   int pNum = 1;
1596
1597   /* if this function has variable argument list */
1598   /* then make the function a reentrant one    */
1599   if (func->hasVargs)
1600     SPEC_RENT (func->etype) = 1;
1601
1602   /* check if this function is defined as calleeSaves
1603      then mark it as such */
1604   func->calleeSave = inCalleeSaveList (func->name);
1605
1606   val = func->args;             /* loop thru all the arguments   */
1607
1608   /* if it is void then remove parameters */
1609   if (val && IS_VOID (val->type))
1610     {
1611       func->args = NULL;
1612       return;
1613     }
1614
1615   /* reset regparm for the port */
1616   (*port->reset_regparms) ();
1617   /* if any of the arguments is an aggregate */
1618   /* change it to pointer to the same type */
1619   while (val)
1620     {
1621       /* mark it as a register parameter if
1622          the function does not have VA_ARG
1623          and as port dictates
1624          not inhibited by command line option or #pragma */
1625       if (!func->hasVargs &&
1626           !IS_RENT (func->etype) &&
1627           (*port->reg_parm) (val->type))
1628         {
1629           SPEC_REGPARM (val->etype) = 1;
1630         }
1631
1632       if (IS_AGGREGATE (val->type))
1633         {
1634           aggregateArgToPointer (val);
1635         }
1636       val = val->next;
1637       pNum++;
1638     }
1639
1640   /* if this is an internal generated function call */
1641   if (func->cdef) {
1642     /* ignore --stack-auto for this one, we don't know how it is compiled */
1643     /* simply trust on --int-long-reent or --float-reent */
1644     if (IS_RENT(func->etype)) {
1645       return;
1646     }
1647   } else {
1648     /* if this function is reentrant or */
1649     /* automatics r 2b stacked then nothing */
1650     if (IS_RENT (func->etype) || options.stackAuto)
1651       return;
1652   }
1653
1654   val = func->args;
1655   pNum = 1;
1656   while (val)
1657     {
1658
1659       /* if a symbolname is not given  */
1660       /* synthesize a variable name */
1661       if (!val->sym)
1662         {
1663
1664           sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1665           val->sym = newSymbol (val->name, 1);
1666           SPEC_OCLS (val->etype) = port->mem.default_local_map;
1667           val->sym->type = copyLinkChain (val->type);
1668           val->sym->etype = getSpec (val->sym->type);
1669           val->sym->_isparm = 1;
1670           strcpy (val->sym->rname, val->name);
1671           SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1672             SPEC_STAT (func->etype);
1673           addSymChain (val->sym);
1674
1675         }
1676       else                      /* symbol name given create synth name */
1677         {
1678
1679           sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1680           strcpy (val->sym->rname, val->name);
1681           val->sym->_isparm = 1;
1682           SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1683             (options.model != MODEL_SMALL ? xdata : data);
1684           SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1685             SPEC_STAT (func->etype);
1686         }
1687       val = val->next;
1688     }
1689 }
1690
1691 /*-----------------------------------------------------------------*/
1692 /* isSymbolEqual - compares two symbols return 1 if they match     */
1693 /*-----------------------------------------------------------------*/
1694 int 
1695 isSymbolEqual (symbol * dest, symbol * src)
1696 {
1697   /* if pointers match then equal */
1698   if (dest == src)
1699     return 1;
1700
1701   /* if one of them is null then don't match */
1702   if (!dest || !src)
1703     return 0;
1704
1705   /* if both of them have rname match on rname */
1706   if (dest->rname[0] && src->rname[0])
1707     return (!strcmp (dest->rname, src->rname));
1708
1709   /* otherwise match on name */
1710   return (!strcmp (dest->name, src->name));
1711 }
1712
1713 void PT(sym_link *type)
1714 {
1715         printTypeChain(type,0);
1716 }
1717 /*-----------------------------------------------------------------*/
1718 /* printTypeChain - prints the type chain in human readable form   */
1719 /*-----------------------------------------------------------------*/
1720 void 
1721 printTypeChain (sym_link * type, FILE * of)
1722 {
1723   int nlr = 0;
1724
1725   if (!of)
1726     {
1727       of = stdout;
1728       nlr = 1;
1729     }
1730
1731   while (type)
1732     {
1733       if (IS_DECL (type))
1734         {
1735           if (DCL_PTR_VOLATILE(type)) {
1736             fprintf (of, "volatile ");
1737           }
1738           switch (DCL_TYPE (type))
1739             {
1740             case FUNCTION:
1741               fprintf (of, "function ");
1742               break;
1743             case GPOINTER:
1744               fprintf (of, "_generic * ");
1745               if (DCL_PTR_CONST (type))
1746                 fprintf (of, "const ");
1747               break;
1748             case CPOINTER:
1749               fprintf (of, "_code * ");
1750               if (DCL_PTR_CONST (type))
1751                 fprintf (of, "const ");
1752               break;
1753             case FPOINTER:
1754               fprintf (of, "_far * ");
1755               if (DCL_PTR_CONST (type))
1756                 fprintf (of, "const ");
1757               break;
1758             case EEPPOINTER:
1759               fprintf (of, "_eeprom * ");
1760               if (DCL_PTR_CONST (type))
1761                 fprintf (of, "const ");
1762               break;
1763
1764             case POINTER:
1765               fprintf (of, "_near * ");
1766               if (DCL_PTR_CONST (type))
1767                 fprintf (of, "const ");
1768               break;
1769             case IPOINTER:
1770               fprintf (of, "_idata *");
1771               if (DCL_PTR_CONST (type))
1772                 fprintf (of, "const ");
1773               break;
1774             case PPOINTER:
1775               fprintf (of, "_pdata *");
1776               if (DCL_PTR_CONST (type))
1777                 fprintf (of, "const ");
1778               break;
1779             case UPOINTER:
1780               fprintf (of, " _unkown *");
1781               if (DCL_PTR_CONST (type))
1782                 fprintf (of, "const ");
1783               break;
1784             case ARRAY:
1785               fprintf (of, "array of ");
1786               break;
1787             }
1788         }
1789       else
1790         {
1791           if (SPEC_VOLATILE (type))
1792             fprintf (of, "volatile ");
1793           if (SPEC_USIGN (type))
1794             fprintf (of, "unsigned ");
1795           if (SPEC_CONST (type))
1796             fprintf (of, "const ");
1797
1798           switch (SPEC_NOUN (type))
1799             {
1800             case V_INT:
1801               if (IS_LONG (type))
1802                 fprintf (of, "long ");
1803               else if (IS_SHORT (type))
1804                 fprintf (of, "short ");
1805               else
1806                 fprintf (of, "int ");
1807               break;
1808
1809             case V_CHAR:
1810               fprintf (of, "char ");
1811               break;
1812
1813             case V_VOID:
1814               fprintf (of, "void ");
1815               break;
1816
1817             case V_FLOAT:
1818               fprintf (of, "float ");
1819               break;
1820
1821             case V_STRUCT:
1822               fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
1823               break;
1824
1825             case V_SBIT:
1826               fprintf (of, "sbit ");
1827               break;
1828
1829             case V_BIT:
1830               fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
1831               break;
1832
1833             default:
1834               break;
1835             }
1836         }
1837       type = type->next;
1838     }
1839   if (nlr)
1840     fprintf (of, "\n");
1841 }
1842
1843 /*-----------------------------------------------------------------*/
1844 /* cdbTypeInfo - print the type information for debugger           */
1845 /*-----------------------------------------------------------------*/
1846 void 
1847 cdbTypeInfo (sym_link * type, FILE * of)
1848 {
1849   fprintf (of, "{%d}", getSize (type));
1850   while (type)
1851     {
1852       if (IS_DECL (type))
1853         {
1854           switch (DCL_TYPE (type))
1855             {
1856             case FUNCTION:
1857               fprintf (of, "DF,");
1858               break;
1859             case GPOINTER:
1860               fprintf (of, "DG,");
1861               break;
1862             case CPOINTER:
1863               fprintf (of, "DC,");
1864               break;
1865             case FPOINTER:
1866               fprintf (of, "DX,");
1867               break;
1868             case POINTER:
1869               fprintf (of, "DD,");
1870               break;
1871             case IPOINTER:
1872               fprintf (of, "DI,");
1873               break;
1874             case PPOINTER:
1875               fprintf (of, "DP,");
1876               break;
1877             case EEPPOINTER:
1878               fprintf (of, "DA,");
1879               break;
1880             case ARRAY:
1881               fprintf (of, "DA%d,", DCL_ELEM (type));
1882               break;
1883             default:
1884               break;
1885             }
1886         }
1887       else
1888         {
1889           switch (SPEC_NOUN (type))
1890             {
1891             case V_INT:
1892               if (IS_LONG (type))
1893                 fprintf (of, "SL");
1894               else if (IS_SHORT (type))
1895                 fprintf (of, "SS");
1896               else
1897                 fprintf (of, "SI");
1898               break;
1899
1900             case V_CHAR:
1901               fprintf (of, "SC");
1902               break;
1903
1904             case V_VOID:
1905               fprintf (of, "SV");
1906               break;
1907
1908             case V_FLOAT:
1909               fprintf (of, "SF");
1910               break;
1911
1912             case V_STRUCT:
1913               fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
1914               break;
1915
1916             case V_SBIT:
1917               fprintf (of, "SX");
1918               break;
1919
1920             case V_BIT:
1921               fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
1922               break;
1923
1924             default:
1925               break;
1926             }
1927           fputs (":", of);
1928           if (SPEC_USIGN (type))
1929             fputs ("U", of);
1930           else
1931             fputs ("S", of);
1932         }
1933       type = type->next;
1934     }
1935 }
1936 /*-----------------------------------------------------------------*/
1937 /* cdbSymbol - prints a symbol & its type information for debugger */
1938 /*-----------------------------------------------------------------*/
1939 void 
1940 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
1941 {
1942   memmap *map;
1943
1944   if (!sym)
1945     return;
1946   if (!of)
1947     of = stdout;
1948
1949   if (isFunc)
1950     fprintf (of, "F:");
1951   else
1952     fprintf (of, "S:");         /* symbol record */
1953   /* if this is not a structure symbol then
1954      we need to figure out the scope information */
1955   if (!isStructSym)
1956     {
1957       if (!sym->level)
1958         {
1959           /* global */
1960           if (IS_STATIC (sym->etype))
1961             fprintf (of, "F%s$", moduleName);   /* scope is file */
1962           else
1963             fprintf (of, "G$"); /* scope is global */
1964         }
1965       else
1966         /* symbol is local */
1967         fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
1968     }
1969   else
1970     fprintf (of, "S$");         /* scope is structure */
1971
1972   /* print the name, & mangled name */
1973   fprintf (of, "%s$%d$%d(", sym->name,
1974            sym->level, sym->block);
1975
1976   cdbTypeInfo (sym->type, of);
1977   fprintf (of, "),");
1978
1979   /* print the address space */
1980   map = SPEC_OCLS (sym->etype);
1981   fprintf (of, "%c,%d,%d",
1982            (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
1983
1984   /* if assigned to registers then output register names */
1985   /* if this is a function then print
1986      if is it an interrupt routine & interrupt number
1987      and the register bank it is using */
1988   if (isFunc)
1989     fprintf (of, ",%d,%d,%d", SPEC_INTRTN (sym->etype),
1990              SPEC_INTN (sym->etype), SPEC_BANK (sym->etype));
1991   /* alternate location to find this symbol @ : eg registers
1992      or spillication */
1993
1994   if (!isStructSym)
1995     fprintf (of, "\n");
1996 }
1997
1998 /*-----------------------------------------------------------------*/
1999 /* cdbStruct - print a structure for debugger                      */
2000 /*-----------------------------------------------------------------*/
2001 void 
2002 cdbStruct (structdef * sdef, int block, FILE * of,
2003            int inStruct, char *tag)
2004 {
2005   symbol *sym;
2006
2007   fprintf (of, "T:");
2008   /* if block # then must have function scope */
2009   fprintf (of, "F%s$", moduleName);
2010   fprintf (of, "%s[", (tag ? tag : sdef->tag));
2011   for (sym = sdef->fields; sym; sym = sym->next)
2012     {
2013       fprintf (of, "({%d}", sym->offset);
2014       cdbSymbol (sym, of, TRUE, FALSE);
2015       fprintf (of, ")");
2016     }
2017   fprintf (of, "]");
2018   if (!inStruct)
2019     fprintf (of, "\n");
2020 }
2021
2022 /*------------------------------------------------------------------*/
2023 /* cdbStructBlock - calls struct printing for a blcks               */
2024 /*------------------------------------------------------------------*/
2025 void 
2026 cdbStructBlock (int block, FILE * of)
2027 {
2028   int i;
2029   bucket **table = StructTab;
2030   bucket *chain;
2031   wassert (of);
2032
2033   /* go thru the entire  table  */
2034   for (i = 0; i < 256; i++)
2035     {
2036       for (chain = table[i]; chain; chain = chain->next)
2037         {
2038           if (chain->block >= block)
2039             {
2040               cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2041             }
2042         }
2043     }
2044 }
2045
2046 /*-----------------------------------------------------------------*/
2047 /* powof2 - returns power of two for the number if number is pow 2 */
2048 /*-----------------------------------------------------------------*/
2049 int 
2050 powof2 (unsigned long num)
2051 {
2052   int nshifts = 0;
2053   int n1s = 0;
2054
2055   while (num)
2056     {
2057       if (num & 1)
2058         n1s++;
2059       num >>= 1;
2060       nshifts++;
2061     }
2062
2063   if (n1s > 1 || nshifts == 0)
2064     return 0;
2065   return nshifts - 1;
2066 }
2067
2068 symbol *__fsadd;
2069 symbol *__fssub;
2070 symbol *__fsmul;
2071 symbol *__fsdiv;
2072 symbol *__fseq;
2073 symbol *__fsneq;
2074 symbol *__fslt;
2075 symbol *__fslteq;
2076 symbol *__fsgt;
2077 symbol *__fsgteq;
2078
2079 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2080 symbol *__muldiv[3][3][2];
2081 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2082 sym_link *__multypes[3][2];
2083 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2084 symbol *__conv[2][3][2];
2085
2086 sym_link *floatType;
2087
2088 static void 
2089 _makeRegParam (symbol * sym)
2090 {
2091   value *val;
2092
2093   val = sym->args;              /* loop thru all the arguments   */
2094
2095   /* reset regparm for the port */
2096   (*port->reset_regparms) ();
2097   while (val)
2098     {
2099       SPEC_REGPARM (val->etype) = 1;
2100       val = val->next;
2101     }
2102 }
2103
2104 /*-----------------------------------------------------------------*/
2105 /* initCSupport - create functions for C support routines          */
2106 /*-----------------------------------------------------------------*/
2107 void 
2108 initCSupport ()
2109 {
2110   const char *smuldivmod[] =
2111   {
2112     "mul", "div", "mod"
2113   };
2114   const char *sbwd[] =
2115   {
2116     "char", "int", "long"
2117   };
2118   const char *ssu[] =
2119   {
2120     "s", "u"
2121   };
2122
2123   int bwd, su, muldivmod, tofrom;
2124
2125   floatType = newFloatLink ();
2126
2127   for (bwd = 0; bwd < 3; bwd++)
2128     {
2129       sym_link *l;
2130       switch (bwd)
2131         {
2132         case 0:
2133           l = newCharLink ();
2134           break;
2135         case 1:
2136           l = newIntLink ();
2137           break;
2138         case 2:
2139           l = newLongLink ();
2140           break;
2141         default:
2142           assert (0);
2143         }
2144       __multypes[bwd][0] = l;
2145       __multypes[bwd][1] = copyLinkChain (l);
2146       SPEC_USIGN (__multypes[bwd][1]) = 1;
2147     }
2148
2149   __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2150   __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2151   __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2152   __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2153   __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2154   __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2155   __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2156   __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2157   __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2158   __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2159
2160   for (tofrom = 0; tofrom < 2; tofrom++)
2161     {
2162       for (bwd = 0; bwd < 3; bwd++)
2163         {
2164           for (su = 0; su < 2; su++)
2165             {
2166               if (tofrom)
2167                 {
2168                   sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2169                   __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
2170                 }
2171               else
2172                 {
2173                   sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2174                   __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
2175                 }
2176             }
2177         }
2178     }
2179
2180   for (muldivmod = 0; muldivmod < 3; muldivmod++)
2181     {
2182       for (bwd = 0; bwd < 3; bwd++)
2183         {
2184           for (su = 0; su < 2; su++)
2185             {
2186               sprintf (buffer, "_%s%s%s",
2187                        smuldivmod[muldivmod],
2188                        ssu[su],
2189                        sbwd[bwd]);
2190               __muldiv[muldivmod][bwd][su] = funcOfType (buffer, __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2191               SPEC_NONBANKED (__muldiv[muldivmod][bwd][su]->etype) = 1;
2192               if (bwd < port->muldiv.force_reg_param_below)
2193                 _makeRegParam (__muldiv[muldivmod][bwd][su]);
2194             }
2195         }
2196     }
2197 }