I was too fast: this breaks regression test bug-221220.c
[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 #include "SDCCsymt.h"
28
29 value *aggregateToPointer (value *val);
30 void printTypeChainRaw (sym_link * start, FILE * of);
31
32 void printFromToType(sym_link *from, sym_link *to) {
33   fprintf (stderr, "from type '");
34   printTypeChain (from, stderr);
35   fprintf (stderr, "'\nto type '");
36   printTypeChain (to, stderr);
37   fprintf (stderr, "'\n");
38 }
39
40 /* noun strings */
41 char *nounName(sym_link *sl) {
42   switch (SPEC_NOUN(sl)) 
43     {
44     case V_INT: {
45       if (SPEC_LONG(sl)) return "long";
46       if (SPEC_SHORT(sl)) return "short";
47       return "int";
48     }
49     case V_FLOAT: return "float";
50     case V_FIXED16X16: return "fixed16x16";
51     case V_CHAR: return "char";
52     case V_VOID: return "void";
53     case V_STRUCT: return "struct";
54     case V_LABEL: return "label";
55     case V_BITFIELD: return "bitfield";
56     case V_BIT: return "bit";
57     case V_SBIT: return "sbit";
58     case V_DOUBLE: return "double";
59     }
60   return "unknown";
61 };
62
63 bucket *SymbolTab[256];         /* the symbol    table  */
64 bucket *StructTab[256];         /* the structure table  */
65 bucket *TypedefTab[256];        /* the typedef   table  */
66 bucket *LabelTab[256];          /* the Label     table  */
67 bucket *enumTab[256];           /* enumerated    table  */
68
69 /*------------------------------------------------------------------*/
70 /* initSymt () - initialises symbol table related stuff             */
71 /*------------------------------------------------------------------*/
72 void 
73 initSymt ()
74 {
75   int i = 0;
76
77   for (i = 0; i < 256; i++)
78     SymbolTab[i] = StructTab[i] = (void *) NULL;
79
80
81 }
82 /*-----------------------------------------------------------------*/
83 /* newBucket - allocates & returns a new bucket        */
84 /*-----------------------------------------------------------------*/
85 bucket *
86 newBucket ()
87 {
88   bucket *bp;
89
90   bp = Safe_alloc ( sizeof (bucket));
91
92   return bp;
93 }
94
95 /*-----------------------------------------------------------------*/
96 /* hashKey - computes the hashkey given a symbol name              */
97 /*-----------------------------------------------------------------*/
98 int 
99 hashKey (const char *s)
100 {
101   unsigned long key = 0;
102
103   while (*s)
104     key += *s++;
105   return key % 256;
106 }
107
108 /*-----------------------------------------------------------------*/
109 /* addSym - adds a symbol to the hash Table                        */
110 /*-----------------------------------------------------------------*/
111 void 
112 addSym (bucket ** stab,
113         void *sym,
114         char *sname,
115         int level,
116         int block,
117         int checkType)
118 {
119   int i;                        /* index into the hash Table */
120   bucket *bp;                   /* temp bucket    *         */
121
122   if (checkType) {
123     symbol *csym = (symbol *)sym;
124
125     if (getenv("DEBUG_SANITY")) {
126       fprintf (stderr, "addSym: %s ", sname);
127     }
128     /* make sure the type is complete and sane */
129     checkTypeSanity(csym->etype, csym->name);
130   }
131
132   /* prevent overflow of the (r)name buffers */
133   if (strlen(sname)>SDCC_SYMNAME_MAX) {
134     werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
135     sname[SDCC_SYMNAME_MAX]='\0';
136   }
137
138   /* the symbols are always added at the head of the list  */
139   i = hashKey (sname);
140   /* get a free entry */
141   bp = Safe_alloc ( sizeof (bucket));
142
143   bp->sym = sym;                /* update the symbol pointer  */
144   bp->level = level;            /* update the nest level      */
145   bp->block = block;
146   strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
147
148   /* if this is the first entry */
149   if (stab[i] == NULL)
150     {
151       bp->prev = bp->next = (void *) NULL;      /* point to nothing */
152       stab[i] = bp;
153     }
154   /* not first entry then add @ head of list */
155   else
156     {
157       bp->prev = NULL;
158       stab[i]->prev = bp;
159       bp->next = stab[i];
160       stab[i] = bp;
161     }
162 }
163
164 /*-----------------------------------------------------------------*/
165 /* deleteSym - deletes a symbol from the hash Table  entry     */
166 /*-----------------------------------------------------------------*/
167 void 
168 deleteSym (bucket ** stab, void *sym, char *sname)
169 {
170   int i = 0;
171   bucket *bp;
172
173   i = hashKey (sname);
174
175   bp = stab[i];
176   /* find the symbol */
177   while (bp)
178     {
179       if (bp->sym == sym)       /* found it then break out */
180         break;                  /* of the loop       */
181       bp = bp->next;
182     }
183
184   if (!bp)                      /* did not find it */
185     return;
186   /* if this is the first one in the chain */
187   if (!bp->prev)
188     {
189       stab[i] = bp->next;
190       if (stab[i])              /* if chain ! empty */
191         stab[i]->prev = (void *) NULL;
192     }
193   /* middle || end of chain */
194   else
195     {
196       if (bp->next)             /* if not end of chain */
197         bp->next->prev = bp->prev;
198
199       bp->prev->next = bp->next;
200     }
201
202 }
203
204 /*-----------------------------------------------------------------*/
205 /* findSym - finds a symbol in a table           */
206 /*-----------------------------------------------------------------*/
207 void *
208 findSym (bucket ** stab, void *sym, const char *sname)
209 {
210   bucket *bp;
211
212   bp = stab[hashKey (sname)];
213   while (bp)
214     {
215       if (bp->sym == sym || strcmp (bp->name, sname) == 0)
216         break;
217       bp = bp->next;
218     }
219
220   return (bp ? bp->sym : (void *) NULL);
221 }
222
223 /*-----------------------------------------------------------------*/
224 /* findSymWithLevel - finds a symbol with a name & level           */
225 /*-----------------------------------------------------------------*/
226 void *
227 findSymWithLevel (bucket ** stab, symbol * sym)
228 {
229   bucket *bp;
230
231   bp = stab[hashKey (sym->name)];
232
233   /**
234    **  do the search from the head of the list since the
235    **  elements are added at the head it is ensured that
236    ** we will find the deeper definitions before we find
237    ** the global ones. we need to check for symbols with
238    ** level <= to the level given, if levels match then block
239    ** numbers need to match as well
240    **/
241   while (bp)
242     {
243       if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
244         {
245           /* if this is parameter then nothing else need to be checked */
246           if (((symbol *) (bp->sym))->_isparm)
247             return (bp->sym);
248           /* if levels match then block numbers should also match */
249           if (bp->level && bp->level == sym->level && bp->block == sym->block)
250             return (bp->sym);
251           /* if levels don't match then we are okay */
252           if (bp->level && bp->level != sym->level && bp->block <= sym->block)
253             return (bp->sym);
254           /* if this is a global variable then we are ok too */
255           if (bp->level == 0)
256             return (bp->sym);
257         }
258
259       bp = bp->next;
260     }
261
262   return (void *) NULL;
263 }
264
265 /*-----------------------------------------------------------------*/
266 /* findSymWithBlock - finds a symbol with name in with a block     */
267 /*-----------------------------------------------------------------*/
268 void *
269 findSymWithBlock (bucket ** stab, symbol * sym, int block)
270 {
271   bucket *bp;
272
273   bp = stab[hashKey (sym->name)];
274   while (bp)
275     {
276       if (strcmp (bp->name, sym->name) == 0 &&
277           bp->block <= block)
278         break;
279       bp = bp->next;
280     }
281
282   return (bp ? bp->sym : (void *) NULL);
283 }
284
285 /*------------------------------------------------------------------*/
286 /* newSymbol () - returns a new pointer to a symbol                 */
287 /*------------------------------------------------------------------*/
288 symbol *
289 newSymbol (char *name, int scope)
290 {
291   symbol *sym;
292
293   sym = Safe_alloc ( sizeof (symbol));
294
295   strncpyz (sym->name, name, sizeof(sym->name));        /* copy the name */
296   sym->level = scope;           /* set the level    */
297   sym->block = currBlockno;
298   sym->lineDef = mylineno;      /* set the line number */
299   sym->fileDef = currFname;
300   return sym;
301 }
302
303 /*------------------------------------------------------------------*/
304 /* newLink - creates a new link (declarator,specifier)              */
305 /*------------------------------------------------------------------*/
306 sym_link *
307 newLink (SYM_LINK_CLASS select)
308 {
309   sym_link *p;
310
311   p = Safe_alloc ( sizeof (sym_link));
312   p->class=select;
313
314   return p;
315 }
316
317 /*------------------------------------------------------------------*/
318 /* newStruct - creats a new structdef from the free list            */
319 /*------------------------------------------------------------------*/
320 structdef *
321 newStruct (char *tag)
322 {
323   structdef *s;
324
325   s = Safe_alloc ( sizeof (structdef));
326
327   strncpyz (s->tag, tag, sizeof(s->tag));               /* copy the tag */
328   return s;
329 }
330   
331 /*------------------------------------------------------------------*/
332 /* copyStruct - copies a structdef including the fields-list        */
333 /*------------------------------------------------------------------*/
334 static structdef *
335 copyStruct (structdef *src)
336 {
337   structdef *dest;
338
339   dest = newStruct ("");
340   memcpy (dest, src, sizeof (structdef));
341   dest->fields = copySymbolChain (src->fields);
342   return dest;
343 }
344   
345 /*------------------------------------------------------------------*/
346 /* sclsFromPtr - Return the storage class a pointer points into.    */
347 /*               S_FIXED is returned for generic pointers or other  */
348 /*               unexpected cases                                   */
349 /*------------------------------------------------------------------*/
350 STORAGE_CLASS
351 sclsFromPtr(sym_link *ptr)
352 {
353   switch (DCL_TYPE (ptr))
354     {
355     case POINTER:
356       return S_DATA;
357     case GPOINTER:
358       return S_FIXED;
359     case FPOINTER:
360       return S_XDATA;
361     case CPOINTER:
362       return S_CODE;
363     case IPOINTER:
364       return S_IDATA;
365     case PPOINTER:
366       return S_PDATA;
367     case EEPPOINTER:
368       return S_EEPROM;
369     case FUNCTION:
370       return S_CODE;
371     default:
372       return S_FIXED;
373     }
374 }
375
376 /*------------------------------------------------------------------*/
377 /* pointerTypes - do the computation for the pointer types          */
378 /*------------------------------------------------------------------*/
379 void 
380 pointerTypes (sym_link * ptr, sym_link * type)
381 {
382   if (IS_SPEC (ptr))
383     return;
384
385   /* find the first pointer type */
386   while (ptr && !IS_PTR (ptr))
387     ptr = ptr->next;
388
389   /* could not find it */
390   if (!ptr || IS_SPEC (ptr))
391     return;
392   
393   if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
394     pointerTypes (ptr->next, type);
395     return;
396   }
397
398   /* change the pointer type depending on the
399      storage class of the type */
400   if (IS_SPEC (type))
401     {
402       switch (SPEC_SCLS (type))
403         {
404         case S_XDATA:
405           DCL_TYPE (ptr) = FPOINTER;
406           break;
407         case S_IDATA:
408           DCL_TYPE (ptr) = IPOINTER;
409           break;
410         case S_PDATA:
411           DCL_TYPE (ptr) = PPOINTER;
412           break;
413         case S_DATA:
414           DCL_TYPE (ptr) = POINTER;
415           break;
416         case S_CODE:
417           DCL_TYPE (ptr) = CPOINTER;
418           break;
419         case S_EEPROM:
420           DCL_TYPE (ptr) = EEPPOINTER;
421           break;
422         default:
423           DCL_TYPE (ptr) = port->unqualified_pointer;
424           break;
425         }
426       /* the storage class of type ends here */
427       SPEC_SCLS (type) = 0;
428     }
429
430   /* now change all the remaining unknown pointers
431      to generic pointers */
432   while (ptr)
433     {
434       if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
435         DCL_TYPE (ptr) = port->unqualified_pointer;
436       ptr = ptr->next;
437     }
438
439   /* same for the type although it is highly unlikely that
440      type will have a pointer */
441   while (type)
442     {
443       if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
444         DCL_TYPE (type) = port->unqualified_pointer;
445       type = type->next;
446     }
447 }
448
449 /*------------------------------------------------------------------*/
450 /* addDecl - adds a declarator @ the end of a chain                 */
451 /*------------------------------------------------------------------*/
452 void 
453 addDecl (symbol * sym, int type, sym_link * p)
454 {
455   sym_link *head;
456   sym_link *tail;
457   sym_link *t;
458
459   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
460     fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
461
462   /* if we are passed a link then set head & tail */
463   if (p)
464     {
465       tail = head = p;
466       while (tail->next)
467         tail = tail->next;
468     }
469   else
470     {
471       head = tail = newLink (DECLARATOR);
472       DCL_TYPE (head) = type;
473     }
474
475   /* if this is the first entry   */
476   if (!sym->type)
477     {
478       sym->type = head;
479       sym->etype = tail;
480     }
481   else
482     {
483       if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
484         {
485           sym->etype = mergeSpec (sym->etype, head, sym->name);
486         }
487       else
488         {
489           if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
490             {
491               t = sym->type;
492               while (t->next != sym->etype)
493                 t = t->next;
494               t->next = head;
495               tail->next = sym->etype;
496             }
497           else
498             {
499               sym->etype->next = head;
500               sym->etype = tail;
501             }
502         }
503     }
504
505   /* if the type is an unknown pointer and has
506      a tspec then take the storage class const & volatile
507      attribute from the tspec & make it those of this
508      symbol */
509   if (p &&
510       !IS_SPEC (p) &&
511       //DCL_TYPE (p) == UPOINTER &&
512       DCL_TSPEC (p))
513     {
514       if (!IS_SPEC (sym->etype))
515         {
516           sym->etype = sym->etype->next = newLink (SPECIFIER);
517         }
518       SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
519       DCL_TSPEC (p) = NULL;
520     }
521
522   // if there is a function in this type chain
523   if (p && funcInChain(sym->type)) {
524     processFuncArgs (sym);
525   }
526
527   return;
528 }
529
530 /*------------------------------------------------------------------
531   checkTypeSanity: prevent the user from doing e.g.:
532   unsigned float uf;
533   ------------------------------------------------------------------*/
534 void checkTypeSanity(sym_link *etype, char *name) {
535   char *noun;
536
537   if (!etype) {
538     if (getenv("DEBUG_SANITY")) {
539       fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
540     }
541     return;
542   }
543
544   if (!IS_SPEC(etype)) {
545     if (getenv("DEBUG_SANITY")) {
546       fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
547     }
548     return;
549   }
550
551   noun=nounName(etype);
552
553   if (getenv("DEBUG_SANITY")) {
554     fprintf (stderr, "checking sanity for %s %p\n", name, etype);
555   }
556
557   if ((SPEC_NOUN(etype)==V_CHAR || 
558        SPEC_NOUN(etype)==V_FLOAT || 
559        SPEC_NOUN(etype)==V_FIXED16X16 ||
560        SPEC_NOUN(etype)==V_DOUBLE || 
561        SPEC_NOUN(etype)==V_VOID) &&
562       (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
563     // long or short for char float double or void
564     werror (E_LONG_OR_SHORT_INVALID, noun, name);
565   }
566   if ((SPEC_NOUN(etype)==V_FLOAT || 
567        SPEC_NOUN(etype)==V_FIXED16X16 ||
568        SPEC_NOUN(etype)==V_DOUBLE || 
569        SPEC_NOUN(etype)==V_VOID) && 
570       (etype->select.s.b_signed || SPEC_USIGN(etype))) {
571     // signed or unsigned for float double or void
572     werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
573   }
574
575   // special case for "short"
576   if (SPEC_SHORT(etype)) {
577     SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
578     SPEC_SHORT(etype) = 0;
579   }
580
581   /* if no noun e.g. 
582      "const a;" or "data b;" or "signed s" or "long l"
583      assume an int */
584   if (!SPEC_NOUN(etype)) {
585     SPEC_NOUN(etype)=V_INT;
586   }
587
588   /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
589   /* a "plain" int bitfield is unsigned */
590   if (SPEC_NOUN(etype)==V_BIT ||
591       SPEC_NOUN(etype)==V_SBIT) {
592     if (!etype->select.s.b_signed)
593       SPEC_USIGN(etype) = 1;
594   }
595
596   if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
597     // signed AND unsigned 
598     werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
599   }
600   if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
601     // short AND long
602     werror (E_LONG_AND_SHORT_INVALID, noun, name);
603   }
604
605 }
606
607 /*------------------------------------------------------------------*/
608 /* mergeSpec - merges two specifiers and returns the new one        */
609 /*------------------------------------------------------------------*/
610 sym_link *
611 mergeSpec (sym_link * dest, sym_link * src, char *name)
612 {
613   if (!IS_SPEC(dest) || !IS_SPEC(src)) {
614 #if 0
615     werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
616     exit (1);
617 #else
618     werror (E_SYNTAX_ERROR, yytext);
619     // the show must go on
620     return newIntLink();
621 #endif
622   }
623
624   if (SPEC_NOUN(src)) {
625     if (!SPEC_NOUN(dest)) {
626       SPEC_NOUN(dest)=SPEC_NOUN(src);
627     } else {
628       /* we shouldn't redeclare the type */
629       if (getenv("DEBUG_SANITY")) {
630         fprintf (stderr, "mergeSpec: ");
631       }
632       werror(E_TWO_OR_MORE_DATA_TYPES, name);
633     }
634   }
635   
636   if (SPEC_SCLS(src)) {
637     /* if destination has no storage class */
638     if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
639       SPEC_SCLS (dest) = SPEC_SCLS (src);
640     } else {
641       if (getenv("DEBUG_SANITY")) {
642         fprintf (stderr, "mergeSpec: ");
643       }
644       werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
645     }
646   }
647
648   /* copy all the specifications  */
649
650   // we really should do: 
651 #if 0
652   if (SPEC_what(src)) {
653     if (SPEC_what(dest)) {
654       werror(W_DUPLICATE_SPEC, "what");
655     }
656     SPEC_what(dst)|=SPEC_what(src);
657   }
658 #endif
659   // but there are more important thing right now
660
661   SPEC_LONG (dest) |= SPEC_LONG (src);
662   SPEC_SHORT(dest) |= SPEC_SHORT(src);
663   SPEC_USIGN (dest) |= SPEC_USIGN (src);
664   dest->select.s.b_signed|=src->select.s.b_signed;
665   SPEC_STAT (dest) |= SPEC_STAT (src);
666   SPEC_EXTR (dest) |= SPEC_EXTR (src);
667   SPEC_CONST(dest) |= SPEC_CONST (src);
668   SPEC_ABSA (dest) |= SPEC_ABSA (src);
669   SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
670   SPEC_ADDR (dest) |= SPEC_ADDR (src);
671   SPEC_OCLS (dest) = SPEC_OCLS (src);
672   SPEC_BLEN (dest) |= SPEC_BLEN (src);
673   SPEC_BSTR (dest) |= SPEC_BSTR (src);
674   SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
675   SPEC_ENUM (dest) |= SPEC_ENUM (src);
676   if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
677       SPEC_ARGREG(dest) = SPEC_ARGREG(src);
678   
679   if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
680     SPEC_STRUCT (dest) = SPEC_STRUCT (src);
681
682   /* these are the only function attributes that will be set 
683      in a specifier while parsing */
684   FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
685   FUNC_BANKED(dest) |= FUNC_BANKED(src);
686   FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
687   FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
688   FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
689   FUNC_ISISR(dest) |= FUNC_ISISR(src);
690   FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
691   FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
692   FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
693   FUNC_INTNO(dest) |= FUNC_INTNO(src);
694   FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
695
696   return dest;
697 }
698
699 /*------------------------------------------------------------------*/
700 /* genSymName - generates and returns a name used for anonymous vars */
701 /*------------------------------------------------------------------*/
702 char *
703 genSymName (int level)
704 {
705   static int gCount = 0;
706   static char gname[SDCC_NAME_MAX + 1];
707
708   SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
709   return gname;
710 }
711
712 /*------------------------------------------------------------------*/
713 /* getSpec - returns the specifier part from a declaration chain    */
714 /*------------------------------------------------------------------*/
715 sym_link *
716 getSpec (sym_link * p)
717 {
718   sym_link *loop;
719
720   loop = p;
721   while (p && !(IS_SPEC (p)))
722     p = p->next;
723
724   return p;
725 }
726
727 /*------------------------------------------------------------------*/
728 /* newCharLink() - creates an char type                             */
729 /*------------------------------------------------------------------*/
730 sym_link *
731 newCharLink ()
732 {
733   sym_link *p;
734
735   p = newLink (SPECIFIER);
736   SPEC_NOUN (p) = V_CHAR;
737
738   return p;
739 }
740
741 /*------------------------------------------------------------------*/
742 /* newFloatLink - a new Float type                                  */
743 /*------------------------------------------------------------------*/
744 sym_link *
745 newFloatLink ()
746 {
747   sym_link *p;
748
749   p = newLink (SPECIFIER);
750   SPEC_NOUN (p) = V_FLOAT;
751
752   return p;
753 }
754
755 /*------------------------------------------------------------------*/
756 /* newFixed16x16Link - a new Float type                                  */
757 /*------------------------------------------------------------------*/
758 sym_link *
759 newFixed16x16Link ()
760 {
761   sym_link *p;
762
763   p = newLink (SPECIFIER);
764   SPEC_NOUN (p) = V_FIXED16X16;
765
766   return p;
767 }
768
769 /*------------------------------------------------------------------*/
770 /* newLongLink() - new long type                                    */
771 /*------------------------------------------------------------------*/
772 sym_link *
773 newLongLink ()
774 {
775   sym_link *p;
776
777   p = newLink (SPECIFIER);
778   SPEC_NOUN (p) = V_INT;
779   SPEC_LONG (p) = 1;
780
781   return p;
782 }
783
784 /*------------------------------------------------------------------*/
785 /* newIntLink() - creates an int type                               */
786 /*------------------------------------------------------------------*/
787 sym_link *
788 newIntLink ()
789 {
790   sym_link *p;
791
792   p = newLink (SPECIFIER);
793   SPEC_NOUN (p) = V_INT;
794
795   return p;
796 }
797
798 /*------------------------------------------------------------------*/
799 /* newBoolLink() - creates an bool type                             */
800 /*------------------------------------------------------------------*/
801 sym_link *
802 newBoolLink ()
803 {
804   sym_link *p;
805
806   p = newLink (SPECIFIER);
807   SPEC_NOUN (p) = V_BIT;
808
809   return p;
810 }
811
812 /*------------------------------------------------------------------*/
813 /* getSize - returns size of a type chain in bits                   */
814 /*------------------------------------------------------------------*/
815 unsigned int 
816 getSize (sym_link * p)
817 {
818   /* if nothing return 0 */
819   if (!p)
820     return 0;
821   if (IS_SPEC (p))
822     {                           /* if this is the specifier then */
823       switch (SPEC_NOUN (p))
824         {                       /* depending on the specifier type */
825         case V_INT:
826           return (IS_LONG (p) ? LONGSIZE : INTSIZE);
827         case V_FLOAT:
828           return FLOATSIZE;
829         case V_FIXED16X16:
830           return (4);
831         case V_CHAR:
832           return CHARSIZE;
833         case V_VOID:
834           return 0;
835         case V_STRUCT:
836           return SPEC_STRUCT (p)->size;
837         case V_LABEL:
838           return 0;
839         case V_SBIT:
840         case V_BIT:
841           return BITSIZE;
842         case V_BITFIELD:
843           return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
844         default:
845           return 0;
846         }
847     }
848
849   /* this is a declarator */
850   switch (DCL_TYPE (p))
851     {
852     case ARRAY:
853       if (DCL_ELEM(p)) {
854         return DCL_ELEM (p) * getSize (p->next);
855       } else {
856           //    werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
857           //    "can not tell the size of an array[]");
858         return 0;
859       }
860     case IPOINTER:
861     case PPOINTER:
862     case POINTER:
863       return (PTRSIZE);
864     case EEPPOINTER:
865     case FPOINTER:
866     case CPOINTER:
867     case FUNCTION:
868       return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
869     case GPOINTER:
870       return (GPTRSIZE);
871
872     default:
873       return 0;
874     }
875 }
876
877 /*---------------------------------------------------------------------*/
878 /* getAllocSize - returns size of a type chain in bytes for allocation */
879 /*---------------------------------------------------------------------*/
880 unsigned int
881 getAllocSize (sym_link *p)
882 {
883   if (IS_STRUCT (p) && SPEC_STRUCT (p)->type == STRUCT)
884     {
885       /* if this is a struct specifier then  */
886       /* calculate the size as it could end  */
887       /* with an array of unspecified length */
888       symbol *sflds = SPEC_STRUCT (p)->fields;
889
890       while (sflds && sflds->next)
891         sflds = sflds->next;
892
893       if (sflds && !IS_BITFIELD (sflds->type))
894         return sflds->offset + getAllocSize (sflds->type);
895       else
896         return SPEC_STRUCT (p)->size;
897     }
898   else
899     return getSize (p);
900 }
901
902 /*------------------------------------------------------------------*/
903 /* bitsForType - returns # of bits required to store this type      */
904 /*------------------------------------------------------------------*/
905 unsigned int 
906 bitsForType (sym_link * p)
907 {
908   /* if nothing return 0 */
909   if (!p)
910     return 0;
911
912   if (IS_SPEC (p))
913     {                           /* if this is the specifier then */
914
915       switch (SPEC_NOUN (p))
916         {                       /* depending on the specifier type */
917         case V_INT:
918           return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
919         case V_FLOAT:
920           return FLOATSIZE * 8;
921         case V_FIXED16X16:
922           return (32);
923         case V_CHAR:
924           return CHARSIZE * 8;
925         case V_VOID:
926           return 0;
927         case V_STRUCT:
928           return SPEC_STRUCT (p)->size * 8;
929         case V_LABEL:
930           return 0;
931         case V_SBIT:
932         case V_BIT:
933           return 1;
934         case V_BITFIELD:
935           return SPEC_BLEN (p);
936         default:
937           return 0;
938         }
939     }
940
941   /* this is a specifier  */
942   switch (DCL_TYPE (p))
943     {
944     case ARRAY:
945       return DCL_ELEM (p) * getSize (p->next) * 8;
946     case IPOINTER:
947     case PPOINTER:
948     case POINTER:
949       return (PTRSIZE * 8);
950     case EEPPOINTER:
951     case FPOINTER:
952     case CPOINTER:
953     case FUNCTION:
954       return (FPTRSIZE * 8);
955     case GPOINTER:
956       return (GPTRSIZE * 8);
957
958     default:
959       return 0;
960     }
961 }
962
963 /*------------------------------------------------------------------*/
964 /* copySymbolChain - copies a symbol chain                          */
965 /*------------------------------------------------------------------*/
966 symbol *
967 copySymbolChain (symbol * src)
968 {
969   symbol *dest;
970
971   if (!src)
972     return NULL;
973
974   dest = copySymbol (src);
975   dest->next = copySymbolChain (src->next);
976   return dest;
977 }
978
979 /*------------------------------------------------------------------*/
980 /* copySymbol - makes a copy of a symbol                            */
981 /*------------------------------------------------------------------*/
982 symbol *
983 copySymbol (symbol * src)
984 {
985   symbol *dest;
986
987   if (!src)
988     return NULL;
989
990   dest = newSymbol (src->name, src->level);
991   memcpy (dest, src, sizeof (symbol));
992   dest->level = src->level;
993   dest->block = src->block;
994   dest->ival = copyIlist (src->ival);
995   dest->type = copyLinkChain (src->type);
996   dest->etype = getSpec (dest->type);
997   dest->next = NULL;
998   dest->key = src->key;
999   dest->allocreq = src->allocreq;
1000   return dest;
1001 }
1002
1003 /*------------------------------------------------------------------*/
1004 /* reverseSyms - reverses the links for a symbol chain      */
1005 /*------------------------------------------------------------------*/
1006 symbol *
1007 reverseSyms (symbol * sym)
1008 {
1009   symbol *prev, *curr, *next;
1010
1011   if (!sym)
1012     return NULL;
1013
1014   prev = sym;
1015   curr = sym->next;
1016
1017   while (curr)
1018     {
1019       next = curr->next;
1020       curr->next = prev;
1021       prev = curr;
1022       curr = next;
1023     }
1024   sym->next = (void *) NULL;
1025   return prev;
1026 }
1027
1028 /*------------------------------------------------------------------*/
1029 /* reverseLink - reverses the links for a type chain        */
1030 /*------------------------------------------------------------------*/
1031 sym_link *
1032 reverseLink (sym_link * type)
1033 {
1034   sym_link *prev, *curr, *next;
1035
1036   if (!type)
1037     return NULL;
1038
1039   prev = type;
1040   curr = type->next;
1041
1042   while (curr)
1043     {
1044       next = curr->next;
1045       curr->next = prev;
1046       prev = curr;
1047       curr = next;
1048     }
1049   type->next = (void *) NULL;
1050   return prev;
1051 }
1052
1053 /*------------------------------------------------------------------*/
1054 /* addSymChain - adds a symbol chain to the symboltable             */
1055 /*------------------------------------------------------------------*/
1056 void 
1057 addSymChain (symbol ** symHead)
1058 {
1059   symbol *sym = *symHead;
1060   symbol *csym = NULL;
1061   symbol **symPtrPtr;
1062   int error = 0;
1063
1064   for (; sym != NULL; sym = sym->next)
1065     {
1066       changePointer(sym->type);
1067       checkTypeSanity(sym->etype, sym->name);
1068
1069       if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1070         checkDecl (sym, 0);
1071       
1072       /* if already exists in the symbol table then check if
1073          one of them is an extern definition if yes then
1074          then check if the type match, if the types match then
1075          delete the current entry and add the new entry      */
1076       if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1077           csym->level == sym->level) {
1078
1079         /* If the previous definition was for an array with incomplete */
1080         /* type, and the new definition has completed the type, update */
1081         /* the original type to match */
1082         if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1083             && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1084           {
1085             if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1086               DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1087           }
1088
1089         #if 0
1090         /* If only one of the definitions used the "at" keyword, copy */
1091         /* the address to the other. */
1092         if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1093             && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1094           {
1095             SPEC_ABSA (sym->etype) = 1;
1096             SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1097           }
1098         if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1099             && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1100           {
1101             SPEC_ABSA (csym->etype) = 1;
1102             SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1103           }
1104         #endif
1105   
1106         error = 0;        
1107         if (csym->ival && sym->ival)
1108           error = 1;
1109         if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1110           error = 1;
1111         
1112         if (error) {
1113           /* one definition extern ? */
1114           if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1115             werror (E_EXTERN_MISMATCH, sym->name);
1116           else
1117             werror (E_DUPLICATE, sym->name);
1118           werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1119           #if 0
1120           fprintf (stderr, "from type '");
1121           printTypeChain (csym->type, stderr);
1122           if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1123             fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1124           fprintf (stderr, "'\nto type '");
1125           printTypeChain (sym->type, stderr);
1126           if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1127             fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1128           fprintf (stderr, "'\n");
1129           #endif
1130           continue;
1131         }
1132
1133         if (csym->ival && !sym->ival)
1134           sym->ival = csym->ival;
1135
1136         /* delete current entry */
1137         deleteSym (SymbolTab, csym, csym->name);
1138         deleteFromSeg(csym);
1139         
1140         symPtrPtr = symHead;
1141         while (*symPtrPtr && *symPtrPtr != csym)
1142           symPtrPtr = &(*symPtrPtr)->next;
1143         if (*symPtrPtr == csym)
1144           *symPtrPtr = csym->next;
1145           
1146       }
1147       
1148       /* add new entry */
1149       addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1150     }
1151 }
1152
1153
1154 /*------------------------------------------------------------------*/
1155 /* funcInChain - DCL Type 'FUNCTION' found in type chain            */
1156 /*------------------------------------------------------------------*/
1157 int 
1158 funcInChain (sym_link * lnk)
1159 {
1160   while (lnk)
1161     {
1162       if (IS_FUNC (lnk))
1163         return 1;
1164       lnk = lnk->next;
1165     }
1166   return 0;
1167 }
1168
1169 /*------------------------------------------------------------------*/
1170 /* structElemType - returns the type info of a struct member        */
1171 /*------------------------------------------------------------------*/
1172 sym_link *
1173 structElemType (sym_link * stype, value * id)
1174 {
1175   symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1176   sym_link *type, *etype;
1177   sym_link *petype = getSpec (stype);
1178
1179   if (fields && id) {
1180     
1181     /* look for the id */
1182     while (fields)
1183       {
1184         if (strcmp (fields->rname, id->name) == 0)
1185           {
1186             type = copyLinkChain (fields->type);
1187             etype = getSpec (type);
1188             SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1189                                  SPEC_SCLS (etype) : SPEC_SCLS (petype));
1190             if (IS_SPEC (type))
1191               SPEC_CONST (type) |= SPEC_CONST (stype);
1192             else
1193               DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1194             return type;
1195           }
1196         fields = fields->next;
1197       }
1198   }
1199
1200   werror (E_NOT_MEMBER, id->name);
1201     
1202   // the show must go on
1203   return newIntLink();
1204 }
1205
1206 /*------------------------------------------------------------------*/
1207 /* getStructElement - returns element of a tructure definition      */
1208 /*------------------------------------------------------------------*/
1209 symbol *
1210 getStructElement (structdef * sdef, symbol * sym)
1211 {
1212   symbol *field;
1213
1214   for (field = sdef->fields; field; field = field->next)
1215     if (strcmp (field->name, sym->name) == 0)
1216       return field;
1217
1218   werror (E_NOT_MEMBER, sym->name);
1219
1220   return sdef->fields;
1221 }
1222
1223 /*------------------------------------------------------------------*/
1224 /* compStructSize - computes the size of a structure                */
1225 /*------------------------------------------------------------------*/
1226 int 
1227 compStructSize (int su, structdef * sdef)
1228 {
1229     int sum = 0, usum = 0;
1230     int bitOffset = 0;
1231     symbol *loop;
1232
1233     /* for the identifiers  */
1234     loop = sdef->fields;
1235     while (loop) {
1236
1237         /* create the internal name for this variable */
1238         SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1239         if (su == UNION) {
1240             sum = 0;
1241             bitOffset = 0;
1242         }
1243         SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1244
1245         /* if this is a bit field  */
1246         if (loop->bitVar) {
1247
1248             /* change it to a unsigned bit */
1249             SPEC_NOUN (loop->etype) = V_BITFIELD;
1250             /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1251             /* a "plain" int bitfield is unsigned */
1252             if (!loop->etype->select.s.b_signed)
1253               SPEC_USIGN(loop->etype) = 1;
1254
1255             SPEC_BLEN (loop->etype) = loop->bitVar;
1256
1257             if (loop->bitVar == BITVAR_PAD) {
1258                 /* A zero length bitfield forces padding */
1259                 SPEC_BSTR (loop->etype) = bitOffset;
1260                 SPEC_BLEN (loop->etype) = 0;
1261                 bitOffset = 8;
1262                 loop->offset = sum;
1263             }
1264             else {
1265                 if (bitOffset == 8) {
1266                     bitOffset = 0;
1267                     sum++;
1268                 }
1269                 /* check if this fit into the remaining   */
1270                 /* bits of this byte else align it to the */
1271                 /* next byte boundary                     */
1272                 if (loop->bitVar <= (8 - bitOffset)) {
1273                     /* fits into current byte */
1274                     loop->offset = sum;
1275                     SPEC_BSTR (loop->etype) = bitOffset;
1276                     bitOffset += loop->bitVar;
1277                 }
1278                 else if (!bitOffset) {
1279                     /* does not fit, but is already byte aligned */
1280                     loop->offset = sum;
1281                     SPEC_BSTR (loop->etype) = bitOffset;
1282                     bitOffset += loop->bitVar;
1283                 } 
1284                 else {
1285                     /* does not fit; need to realign first */
1286                     sum++;
1287                     loop->offset = (su == UNION ? sum = 0 : sum);
1288                     bitOffset = 0;
1289                     SPEC_BSTR (loop->etype) = bitOffset;
1290                     bitOffset += loop->bitVar;
1291                 }
1292                 while (bitOffset>8) {
1293                     bitOffset -= 8;
1294                     sum++;
1295                 } 
1296             }
1297         }
1298         else {
1299             /* This is a non-bit field. Make sure we are */
1300             /* byte aligned first */
1301             if (bitOffset) {
1302                 sum++;
1303                 loop->offset = (su == UNION ? sum = 0 : sum);
1304                 bitOffset = 0;
1305             }
1306             loop->offset = sum;
1307             checkDecl (loop, 1);
1308             sum += getSize (loop->type);
1309         }
1310
1311         loop = loop->next;
1312
1313         /* if union then size = sizeof largest field */
1314         if (su == UNION) {
1315             /* For UNION, round up after each field */
1316             sum += ((bitOffset+7)/8);
1317             usum = max (usum, sum);
1318         }
1319
1320     }
1321     
1322     /* For STRUCT, round up after all fields processed */
1323     if (su != UNION)
1324         sum += ((bitOffset+7)/8);
1325
1326     return (su == UNION ? usum : sum);
1327 }
1328
1329 /*-------------------------------------------------------------------*/
1330 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1331 /*                      an enclosing struct/union                    */
1332 /*-------------------------------------------------------------------*/
1333 void
1334 promoteAnonStructs (int su, structdef * sdef)
1335 {
1336   symbol *field;
1337   symbol *subfield;
1338   symbol **tofield;
1339   symbol *nextfield;
1340   symbol *dupfield;
1341   int base;
1342
1343   tofield = &sdef->fields;
1344   field = sdef->fields;
1345   while (field)
1346     {
1347       nextfield = field->next;
1348       if (!*field->name && IS_STRUCT (field->type))
1349         {
1350           /* Found an anonymous struct/union. Replace it */
1351           /* with the fields it contains and adjust all  */
1352           /* the offsets */
1353           
1354           base = field->offset;
1355           subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1356           if (!subfield)
1357             continue;           /* just in case it's empty */
1358           
1359           *tofield = subfield;
1360           for (;;)
1361             {
1362               /* check for field name conflicts resulting from promotion */
1363               dupfield = sdef->fields;
1364               while (dupfield && dupfield != subfield)
1365                 {
1366                   if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1367                     {
1368                       werrorfl (subfield->fileDef, subfield->lineDef,
1369                                 E_DUPLICATE_MEMBER,
1370                                 su==STRUCT ? "struct" : "union",
1371                                 subfield->name);
1372                       werrorfl (dupfield->fileDef, dupfield->lineDef,
1373                                 E_PREVIOUS_DEF);
1374                     }
1375                   dupfield = dupfield->next;
1376                 }
1377               
1378               subfield->offset += base;
1379               if (subfield->next)
1380                 subfield = subfield->next;
1381               else
1382                 break;
1383             }
1384           subfield->next = nextfield;
1385           tofield = &subfield->next;
1386         }
1387       else
1388         tofield = &field->next;
1389       field = nextfield;
1390     }
1391 }
1392
1393
1394 /*------------------------------------------------------------------*/
1395 /* checkSClass - check the storage class specification              */
1396 /*------------------------------------------------------------------*/
1397 static void 
1398 checkSClass (symbol * sym, int isProto)
1399 {
1400   sym_link *t;
1401   
1402   if (getenv("DEBUG_SANITY")) {
1403     fprintf (stderr, "checkSClass: %s \n", sym->name);
1404   }
1405   
1406   /* type is literal can happen for enums change
1407      to auto */
1408   if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1409     SPEC_SCLS (sym->etype) = S_AUTO;
1410   
1411   /* if sfr or sbit then must also be volatile */
1412   if (SPEC_SCLS (sym->etype) == S_SBIT ||
1413       SPEC_SCLS (sym->etype) == S_SFR)
1414     {
1415       SPEC_VOLATILE (sym->etype) = 1;
1416     }
1417   
1418   /* if absolute address given then it mark it as
1419      volatile -- except in the PIC port */
1420
1421 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1422   /* The PIC port uses a different peep hole optimizer based on "pCode" */
1423   if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1424 #endif
1425
1426     if (IS_ABSOLUTE (sym->etype))
1427       SPEC_VOLATILE (sym->etype) = 1;
1428   
1429   if (TARGET_IS_MCS51 &&
1430       IS_ABSOLUTE (sym->etype) &&
1431       SPEC_SCLS (sym->etype) == S_SFR)
1432     {
1433       int n, size;
1434       unsigned addr;
1435
1436       if (SPEC_NOUN (sym->etype) == V_CHAR)
1437         size = 8;
1438       else if (SPEC_LONG (sym->etype) == 0)
1439         size = 16;
1440       else
1441         size = 32;
1442
1443       addr = SPEC_ADDR (sym->etype);
1444       for (n=0; n<size; n+=8)
1445         if (((addr >> n) & 0xFF) < 0x80)
1446           werror (W_SFR_ABSRANGE, sym->name);
1447     }
1448
1449   /* If code memory is read only, then pointers to code memory */
1450   /* implicitly point to constants -- make this explicit       */
1451   t = sym->type;
1452   while (t && t->next) {
1453     if (IS_CODEPTR(t) && port->mem.code_ro) {
1454       if (IS_SPEC(t->next)) {
1455         SPEC_CONST (t->next) = 1;
1456       } else {
1457         DCL_PTR_CONST (t->next) = 1;
1458       }
1459     }
1460     t = t->next;
1461   }
1462
1463   /* global variables declared const put into code */
1464   /* if no other storage class specified */
1465   if (sym->level == 0 &&
1466       SPEC_SCLS(sym->etype) == S_FIXED &&
1467       !IS_FUNC(sym->type)) {
1468     /* find the first non-array link */
1469     t = sym->type;
1470     while (IS_ARRAY(t))
1471       t = t->next;
1472     if (IS_CONSTANT (t)) {
1473       SPEC_SCLS (sym->etype) = S_CODE;
1474     }
1475   }
1476
1477   /* global variable in code space is a constant */
1478   if (sym->level == 0 &&
1479       SPEC_SCLS (sym->etype) == S_CODE &&
1480       port->mem.code_ro) {
1481     /* find the first non-array link */
1482     t = sym->type;
1483     while (IS_ARRAY(t))
1484       t = t->next;
1485     if (IS_SPEC(t)) {
1486       SPEC_CONST (t) = 1;
1487     } else {
1488       DCL_PTR_CONST (t) = 1;
1489     }
1490   }
1491
1492   /* if bit variable then no storage class can be */
1493   /* specified since bit is already a storage */
1494   if (IS_BITVAR (sym->etype) &&
1495       (SPEC_SCLS (sym->etype) != S_FIXED &&
1496        SPEC_SCLS (sym->etype) != S_SBIT &&
1497        SPEC_SCLS (sym->etype) != S_BIT)
1498     )
1499     {
1500       werror (E_BITVAR_STORAGE, sym->name);
1501       SPEC_SCLS (sym->etype) = S_FIXED;
1502     }
1503
1504   /* extern variables cannot be initialized */
1505   if (IS_EXTERN (sym->etype) && sym->ival)
1506     {
1507       werror (E_EXTERN_INIT, sym->name);
1508       sym->ival = NULL;
1509     }
1510
1511   /* if this is an automatic symbol */
1512   if (sym->level && (options.stackAuto || reentrant)) {
1513     if (SPEC_SCLS (sym->etype) != S_BIT) {
1514       if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1515            SPEC_SCLS (sym->etype) == S_FIXED ||
1516            SPEC_SCLS (sym->etype) == S_REGISTER ||
1517            SPEC_SCLS (sym->etype) == S_STACK ||
1518            SPEC_SCLS (sym->etype) == S_XSTACK)) {
1519         SPEC_SCLS (sym->etype) = S_AUTO;
1520       } else {
1521         /* storage class may only be specified for statics */
1522         if (!IS_STATIC(sym->etype)) {
1523           werror (E_AUTO_ASSUMED, sym->name);
1524         }
1525       }
1526     }
1527   }
1528
1529   /* automatic symbols cannot be given   */
1530   /* an absolute address ignore it      */
1531   if (sym->level &&
1532       SPEC_ABSA (sym->etype) &&
1533       (options.stackAuto || reentrant))
1534     {
1535       werror (E_AUTO_ABSA, sym->name);
1536       SPEC_ABSA (sym->etype) = 0;
1537     }
1538
1539   /* arrays & pointers cannot be defined for bits   */
1540   /* SBITS or SFRs or BIT                           */
1541   if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1542       !IS_FUNCPTR (sym->type) &&
1543       (SPEC_NOUN (sym->etype) == V_BIT ||
1544        SPEC_NOUN (sym->etype) == V_SBIT ||
1545        SPEC_NOUN (sym->etype) == V_BITFIELD ||
1546        SPEC_SCLS (sym->etype) == S_SFR))
1547     werror (E_BIT_ARRAY, sym->name);
1548
1549   /* if this is a bit|sbit then set length & start  */
1550   if (SPEC_NOUN (sym->etype) == V_BIT ||
1551       SPEC_NOUN (sym->etype) == V_SBIT)
1552     {
1553       SPEC_BLEN (sym->etype) = 1;
1554       SPEC_BSTR (sym->etype) = 0;
1555     }
1556
1557   if (!isProto) {
1558     /* variables declared in CODE space must have */
1559     /* initializers if not an extern */
1560     if (SPEC_SCLS (sym->etype) == S_CODE &&
1561         sym->ival == NULL &&
1562         !sym->_isparm &&
1563         //!sym->level &&
1564         port->mem.code_ro &&
1565         !IS_EXTERN (sym->etype) &&
1566         !funcInChain (sym->type))
1567       werror (E_CODE_NO_INIT, sym->name);
1568   }
1569
1570   /* if parameter or local variable then change */
1571   /* the storage class to reflect where the var will go */
1572   if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1573    && !IS_STATIC(sym->etype)
1574       )
1575     {
1576       if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1577         {
1578           SPEC_SCLS (sym->etype) = (options.useXstack ?
1579                                     S_XSTACK : S_STACK);
1580         }
1581       else
1582         {
1583           /* hack-o-matic! I see no reason why the useXstack option should ever
1584            * control this allocation, but the code was originally that way, and
1585            * changing it for non-390 ports breaks the compiler badly.
1586            */
1587           bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ? 
1588                 1 : options.useXstack;
1589           SPEC_SCLS (sym->etype) = (useXdata ?
1590                                     S_XDATA : S_FIXED);
1591         }
1592     }
1593 }
1594
1595 /*------------------------------------------------------------------*/
1596 /* changePointer - change pointer to functions                      */
1597 /*------------------------------------------------------------------*/
1598 void 
1599 changePointer (sym_link * p)
1600 {
1601
1602   /* go thru the chain of declarations   */
1603   /* if we find a pointer to a function  */
1604   /* change it to a ptr to code area     */
1605   /* unless the function is banked.      */
1606   for (; p; p = p->next)
1607     {
1608       if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1609         DCL_TYPE (p) = port->unqualified_pointer;
1610       if (IS_PTR (p) && IS_FUNC (p->next))
1611         if (!IFFUNC_BANKED(p->next))
1612         DCL_TYPE (p) = CPOINTER;
1613     }
1614 }
1615
1616 /*------------------------------------------------------------------*/
1617 /* checkDecl - does semantic validation of a declaration                   */
1618 /*------------------------------------------------------------------*/
1619 int 
1620 checkDecl (symbol * sym, int isProto)
1621 {
1622
1623   checkSClass (sym, isProto);           /* check the storage class      */
1624   changePointer (sym->type);          /* change pointers if required */
1625
1626   /* if this is an array without any dimension
1627      then update the dimension from the initial value */
1628   if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1629     DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1630
1631   return 0;
1632 }
1633
1634 /*------------------------------------------------------------------*/
1635 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1636 /*------------------------------------------------------------------*/
1637 sym_link *
1638 copyLinkChain (sym_link * p)
1639 {
1640   sym_link *head, *curr, *loop;
1641
1642   curr = p;
1643   head = loop = (curr ? newLink (p->class) : (void *) NULL);
1644   while (curr)
1645     {
1646       memcpy (loop, curr, sizeof (sym_link));   /* copy it */
1647 if (getenv ("SDCCCOPYSTRUCT")) // this breaks regression test bug-221220??
1648       if (IS_STRUCT (loop))
1649         SPEC_STRUCT (loop) = copyStruct (SPEC_STRUCT (loop));
1650       loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1651       loop = loop->next;
1652       curr = curr->next;
1653     }
1654
1655   return head;
1656 }
1657
1658 /*------------------------------------------------------------------*/
1659 /* cleanUpBlock - cleansup the symbol table specified for all the   */
1660 /*                symbols in the given block                        */
1661 /*------------------------------------------------------------------*/
1662 void 
1663 cleanUpBlock (bucket ** table, int block)
1664 {
1665   int i;
1666   bucket *chain;
1667
1668   /* go thru the entire  table  */
1669   for (i = 0; i < 256; i++)
1670     {
1671       for (chain = table[i]; chain; chain = chain->next)
1672         {
1673           if (chain->block >= block)
1674             {
1675               deleteSym (table, chain->sym, chain->name);
1676             }
1677         }
1678     }
1679 }
1680
1681 /*------------------------------------------------------------------*/
1682 /* cleanUpLevel - cleansup the symbol table specified for all the   */
1683 /*                symbols in the given level                        */
1684 /*------------------------------------------------------------------*/
1685 void 
1686 cleanUpLevel (bucket ** table, int level)
1687 {
1688   int i;
1689   bucket *chain;
1690
1691   /* go thru the entire  table  */
1692   for (i = 0; i < 256; i++)
1693     {
1694       for (chain = table[i]; chain; chain = chain->next)
1695         {
1696           if (chain->level >= level)
1697             {
1698               deleteSym (table, chain->sym, chain->name);
1699             }
1700         }
1701     }
1702 }
1703
1704 /*------------------------------------------------------------------*/
1705 /* computeTypeOr - computes the resultant type from two types       */
1706 /*------------------------------------------------------------------*/
1707 static sym_link *
1708 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1709 {
1710   /* sanity check */
1711   assert (   (IS_CHAR (etype1) || IS_BIT (etype1))
1712           && (IS_CHAR (etype2) || IS_BIT (etype2)));
1713
1714   if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1715     {
1716       SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1717       return reType;
1718     }
1719   
1720   if (SPEC_USIGN (etype1))
1721     {
1722       if (   IS_LITERAL (etype2)
1723           && floatFromVal (valFromType (etype2)) >= 0)
1724         SPEC_USIGN (reType) = 1;
1725       else
1726         {
1727           /* promote to int */
1728           SPEC_USIGN (reType) = 0;
1729           SPEC_NOUN (reType) = V_INT;
1730         }
1731     }
1732   else /* etype1 signed */
1733     {
1734       if (   IS_LITERAL (etype2)
1735           && floatFromVal (valFromType (etype2)) <= 127)
1736         SPEC_USIGN (reType) = 0;
1737       else
1738         {
1739           /* promote to int */
1740           SPEC_USIGN (reType) = 0;
1741           SPEC_NOUN (reType) = V_INT;
1742         }
1743     }
1744
1745   if (SPEC_USIGN (etype2))
1746     {
1747       if (   IS_LITERAL (etype1)
1748           && floatFromVal (valFromType (etype1)) >= 0)
1749         SPEC_USIGN (reType) = 1;
1750       else
1751         {
1752           /* promote to int */
1753           SPEC_USIGN (reType) = 0;
1754           SPEC_NOUN (reType) = V_INT;
1755         }
1756     }
1757   else /* etype2 signed */
1758     {
1759       if (   IS_LITERAL (etype1)
1760           && floatFromVal (valFromType (etype1)) <= 127)
1761         SPEC_USIGN (reType) = 0;
1762       else
1763         {
1764           /* promote to int */
1765           SPEC_USIGN (reType) = 0;
1766           SPEC_NOUN (reType) = V_INT;
1767         }
1768     }
1769   return reType;
1770 }
1771
1772 /*------------------------------------------------------------------*/
1773 /* computeType - computes the resultant type from two types         */
1774 /*------------------------------------------------------------------*/
1775 sym_link *
1776 computeType (sym_link * type1, sym_link * type2,
1777              RESULT_TYPE resultType, int op)
1778 {
1779   sym_link *rType;
1780   sym_link *reType;
1781   sym_link *etype1 = getSpec (type1);
1782   sym_link *etype2;
1783
1784   etype2 = type2 ? getSpec (type2) : type1;
1785
1786   /* if one of them is a float then result is a float */
1787   /* here we assume that the types passed are okay */
1788   /* and can be cast to one another                */
1789   /* which ever is greater in size */
1790   if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1791     rType = newFloatLink ();
1792   /* if both are fixed16x16 then result is float */
1793   else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1794     rType = newFixed16x16Link();
1795   else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1796     rType = newFloatLink ();
1797   else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1798     rType = newFloatLink ();
1799             
1800   /* if both are bitvars choose the larger one */
1801   else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1802     rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1803             copyLinkChain (type1) : copyLinkChain (type1);
1804                           
1805   /* if only one of them is a bit variable then the other one prevails */
1806   else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1807     {
1808       rType = copyLinkChain (type2);
1809       /* bitfield can have up to 16 bits */
1810       if (getSize (etype1) > 1)
1811         SPEC_NOUN (getSpec (rType)) = V_INT;
1812     }
1813   else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1814     {
1815       rType = copyLinkChain (type1);
1816       /* bitfield can have up to 16 bits */
1817       if (getSize (etype2) > 1)
1818         SPEC_NOUN (getSpec (rType)) = V_INT;
1819     }
1820   /* if one of them is a pointer or array then that
1821      prevails */
1822   else if (IS_PTR (type1) || IS_ARRAY (type1))
1823     rType = copyLinkChain (type1);
1824   else if (IS_PTR (type2) || IS_ARRAY (type2))
1825     rType = copyLinkChain (type2);
1826   else if (getSize (type1) > getSize (type2))
1827     rType = copyLinkChain (type1);
1828   else
1829     rType = copyLinkChain (type2);
1830
1831   reType = getSpec (rType);
1832
1833   /* avoid conflicting types */
1834   reType->select.s.b_signed = 0;
1835
1836   /* if result is a literal then make not so */
1837   if (IS_LITERAL (reType))
1838     SPEC_SCLS (reType) = S_REGISTER;
1839
1840   switch (resultType)
1841     {
1842       case RESULT_TYPE_CHAR:
1843         if (IS_BITVAR (reType))
1844           {
1845             SPEC_NOUN (reType) = V_CHAR;
1846             SPEC_SCLS (reType) = 0;
1847             SPEC_USIGN (reType) = 0;
1848             return rType;
1849           }
1850         break;
1851       case RESULT_TYPE_INT:
1852       case RESULT_TYPE_NONE:
1853       case RESULT_TYPE_OTHER:
1854         if (IS_BIT (reType))
1855           {
1856             SPEC_NOUN (reType) = V_CHAR;
1857             SPEC_SCLS (reType) = 0;
1858             SPEC_USIGN (reType) = 0;
1859             return rType;
1860           }
1861         else if (IS_BITFIELD (reType))
1862           {
1863             /* could be smarter, but it depends on the op */
1864             /* this is for the worst case: a multiplication of 4 * 4 bit */
1865             SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1866             SPEC_SCLS (reType) = 0;
1867             SPEC_USIGN (reType) = 0;
1868             return rType;
1869           }
1870         else if (IS_CHAR (reType))
1871           {
1872             if (op == '|' || op == '^')
1873               return computeTypeOr (etype1, etype2, reType);
1874             else if (   op == '&'
1875                      && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1876               {
1877                 SPEC_USIGN (reType) = 1;
1878                 return rType;
1879               }
1880             else if (op == '*')
1881               {
1882                 SPEC_NOUN (reType) = V_INT;
1883                 SPEC_USIGN (reType) = 0;
1884                 return rType;
1885               }
1886             /* TODO: should be in SDCCast.c */
1887             else if (   op == '/'
1888                      && (   !SPEC_USIGN (etype1)
1889                          || !SPEC_USIGN (etype2)))
1890               {
1891                 SPEC_NOUN (reType) = V_INT;
1892                 SPEC_USIGN (reType) = 0;
1893                 return rType;
1894               }
1895           }
1896         break;
1897       default:
1898         break;
1899     }
1900
1901   /* SDCC's sign promotion:
1902      - if one or both operands are unsigned, the resultant type will be unsigned
1903        (except char, see below)
1904      - if an operand is promoted to a larger type (char -> int, int -> long),
1905        the larger type will be signed
1906
1907      SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1908      much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1909      the standard. The standard demands, that the result has to be the same
1910      "as if" the promotion would have been performed:
1911
1912      - if the result of an operation with two char's is promoted to a
1913        larger type, the result will be signed.
1914
1915      More sophisticated are these:
1916      - if the result of an operation with two char's is a char again,
1917        the result will only then be unsigned, if both operands are
1918        unsigned. In all other cases the result will be signed.
1919
1920        This seems to be contradictionary to the first two rules, but it makes
1921        real sense (all types are char's):
1922
1923         A signed char can be negative; this must be preserved in the result
1924                 -1 * 100 = -100;
1925
1926         Only if both operands are unsigned it's safe to make the result
1927         unsigned; this helps to avoid overflow:
1928                 2 * 100 =  200;
1929
1930      - ToDo: document '|', '^' and '&'
1931      
1932      Homework: - why is (200 * 200 < 0) true?
1933                - why is { char l = 200, r = 200; (r * l > 0) } true?
1934   */
1935
1936   if (!IS_FLOAT (reType)
1937       && (   (SPEC_USIGN (etype1)
1938               /* if this operand is promoted to a larger type,
1939                  then it will be promoted to a signed type */
1940               && !(getSize (etype1) < getSize (reType))
1941               /* char require special handling */
1942               && !IS_CHAR (etype1))
1943           || /* same for 2nd operand */  
1944              (SPEC_USIGN (etype2)
1945               && !(getSize (etype2) < getSize (reType))
1946               && !IS_CHAR (etype2))
1947           || /* if both are 'unsigned char' and not promoted
1948                 let the result be unsigned too */
1949              (   SPEC_USIGN (etype1)
1950               && SPEC_USIGN (etype2)
1951               && IS_CHAR (etype1)
1952               && IS_CHAR (etype2)
1953               && IS_CHAR (reType))))
1954     SPEC_USIGN (reType) = 1;
1955   else
1956     SPEC_USIGN (reType) = 0;
1957
1958   return rType;
1959 }
1960
1961 /*--------------------------------------------------------------------*/
1962 /* compareType - will do type check return 1 if match, -1 if castable */
1963 /*--------------------------------------------------------------------*/
1964 int
1965 compareType (sym_link * dest, sym_link * src)
1966 {
1967   if (!dest && !src)
1968     return 1;
1969
1970   if (dest && !src)
1971     return 0;
1972
1973   if (src && !dest)
1974     return 0;
1975
1976   /* if dest is a declarator then */
1977   if (IS_DECL (dest))
1978     {
1979       if (IS_DECL (src))
1980         {
1981           /* banked function pointer */
1982           if (IS_GENPTR (dest) && IS_GENPTR (src))
1983             {
1984               if (IS_FUNC (src->next) && IS_VOID(dest->next))
1985                 return -1;
1986               if (IS_FUNC (dest->next) && IS_VOID(src->next))
1987                 return -1;
1988               return compareType (dest->next, src->next);
1989             }
1990
1991           if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1992             if (IS_FUNC(src)) {
1993               //checkFunction(src,dest);
1994             }
1995             return compareType (dest->next, src->next);
1996           }
1997           if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1998             return -1;
1999           }
2000           if (IS_PTR (src) && 
2001               (IS_GENPTR (dest) ||
2002                ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2003              ))
2004             return -1;
2005           if (IS_PTR (dest) && IS_ARRAY (src)) {
2006             value *val=aggregateToPointer (valFromType(src));
2007             int res=compareType (dest, val->type);
2008             Safe_free(val->type);
2009             Safe_free(val);
2010             return res;
2011           }
2012           if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2013             return compareType (dest->next, src);
2014           return 0;
2015         }
2016       else if (IS_PTR (dest) && IS_INTEGRAL (src))
2017         return -1;
2018       else
2019         return 0;
2020     }
2021
2022   /* if one is a specifier and the other is not */
2023   if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2024       (IS_SPEC (dest) && !IS_SPEC (src)))
2025     return 0;
2026
2027   /* if one of them is a void then ok */
2028   if (SPEC_NOUN (dest) == V_VOID &&
2029       SPEC_NOUN (src) != V_VOID)
2030     return -1;
2031
2032   if (SPEC_NOUN (dest) != V_VOID &&
2033       SPEC_NOUN (src) == V_VOID)
2034     return -1;
2035
2036   /* if they are both bitfields then if the lengths
2037      and starts don't match */
2038   if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2039       (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2040        SPEC_BSTR (dest) != SPEC_BSTR (src)))
2041     return -1;
2042
2043   /* it is a specifier */
2044   if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2045     {
2046       if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2047           IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2048           /* I would prefer
2049           bitsForType (dest) == bitsForType (src))
2050              instead of the next two lines, but the regression tests fail with
2051              them; I guess it's a problem with replaceCheaperOp  */
2052           getSize (dest) == getSize (src) &&
2053           !(!IS_BIT (dest) && IS_BIT (src)))
2054         return 1;
2055       else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2056         return -1;
2057       else
2058         return 0;
2059     }
2060   else if (IS_STRUCT (dest))
2061     {
2062       if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2063         return 0;
2064       else
2065         return 1;
2066     }
2067   if (SPEC_LONG (dest) != SPEC_LONG (src))
2068     return -1;
2069
2070   if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2071     return -1;
2072
2073   return 1;
2074 }
2075
2076 /*--------------------------------------------------------------------*/
2077 /* compareTypeExact - will do type check return 1 if match exactly    */
2078 /*--------------------------------------------------------------------*/
2079 int
2080 compareTypeExact (sym_link * dest, sym_link * src, int level)
2081 {
2082   STORAGE_CLASS srcScls, destScls;
2083   
2084   if (!dest && !src)
2085     return 1;
2086
2087   if (dest && !src)
2088     return 0;
2089
2090   if (src && !dest)
2091     return 0;
2092
2093   /* if dest is a declarator then */
2094   if (IS_DECL (dest))
2095     {
2096       if (IS_DECL (src))
2097         {
2098           if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2099             if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2100               return 0;
2101             if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2102               return 0;
2103             if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2104               return 0;
2105             if (IS_FUNC(src))
2106               {
2107                 value *exargs, *acargs, *checkValue;
2108
2109                 /* verify function return type */
2110                 if (!compareTypeExact (dest->next, src->next, -1))
2111                   return 0;
2112                 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2113                   return 0;
2114                 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2115                   return 0;
2116                 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2117                   return 0;
2118                 #if 0
2119                 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2120                   return 0;
2121                 #endif
2122
2123                 /* compare expected args with actual args */
2124                 exargs = FUNC_ARGS(dest);
2125                 acargs = FUNC_ARGS(src);
2126
2127                 /* for all the expected args do */
2128                 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2129                   {
2130                     //checkTypeSanity(acargs->etype, acargs->name);
2131
2132                     if (IS_AGGREGATE (acargs->type))
2133                       {
2134                         checkValue = copyValue (acargs);
2135                         aggregateToPointer (checkValue);
2136                       }
2137                     else
2138                       checkValue = acargs;
2139
2140                     #if 0
2141                     if (!compareTypeExact (exargs->type, checkValue->type, -1))
2142                       return 0;
2143                     #endif
2144                   }
2145
2146                   /* if one them ended we have a problem */
2147                   if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2148                       (!exargs && acargs && !IS_VOID (acargs->type)))
2149                     return 0;                  
2150                   return 1;
2151               }
2152             return compareTypeExact (dest->next, src->next, level);
2153           }
2154           return 0;
2155         }
2156       return 0;
2157     }
2158
2159   /* if one is a specifier and the other is not */
2160   if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2161       (IS_SPEC (dest) && !IS_SPEC (src)))
2162     return 0;
2163
2164   /* if one of them is a void then ok */
2165   if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2166     return 0;
2167
2168   /* if they are both bitfields then if the lengths
2169      and starts don't match */
2170   if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2171       (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2172        SPEC_BSTR (dest) != SPEC_BSTR (src)))
2173     return 0;
2174
2175   if (IS_INTEGRAL (dest))
2176     {
2177       /* signedness must match */
2178       if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2179         return 0;
2180       /* size must match */
2181       if (SPEC_LONG (dest) != SPEC_LONG (src))
2182         return 0;
2183       if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2184         return 0;
2185     }
2186   
2187   if (IS_STRUCT (dest))
2188     {
2189       if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2190         return 0;
2191     }
2192
2193   if (SPEC_CONST (dest) != SPEC_CONST (src))
2194     return 0;
2195   if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2196     return 0;
2197   if (SPEC_STAT (dest) != SPEC_STAT (src))
2198     return 0;
2199   if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2200     return 0;
2201   if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2202     return 0;
2203       
2204   destScls = SPEC_SCLS (dest);
2205   srcScls = SPEC_SCLS (src);
2206   
2207   /* Compensate for const to const code change in checkSClass() */
2208   if (!level & port->mem.code_ro && SPEC_CONST (dest))
2209     {
2210       if (srcScls == S_CODE && destScls == S_FIXED)
2211         destScls = S_CODE;
2212       if (destScls == S_CODE && srcScls == S_FIXED)
2213         srcScls = S_CODE;
2214     }
2215
2216   /* compensate for allocGlobal() */  
2217   if ((srcScls == S_FIXED || srcScls == S_AUTO)
2218       && port->mem.default_globl_map == xdata
2219       && !level)
2220     srcScls = S_XDATA;
2221   
2222   if (level>0 && !SPEC_STAT (dest))
2223     {
2224       /* Compensate for hack-o-matic in checkSClass() */
2225       if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2226         {
2227           if (destScls == S_FIXED)
2228             destScls = (options.useXstack ? S_XSTACK : S_STACK);
2229           if (srcScls == S_FIXED)
2230             srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2231         }
2232       else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2233         {
2234           if (destScls == S_FIXED)
2235             destScls = S_XDATA;
2236           if (srcScls == S_FIXED)
2237             srcScls = S_XDATA;
2238         }
2239     }
2240
2241   if (srcScls != destScls)
2242     {
2243       #if 0
2244       printf ("level = %d\n", level);
2245       printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2246                 SPEC_SCLS (src), SPEC_SCLS (dest));
2247       printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2248       #endif
2249       return 0;
2250     }
2251   
2252   return 1;
2253 }
2254
2255 /*------------------------------------------------------------------*/
2256 /* inCalleeSaveList - return 1 if found in callee save list          */
2257 /*------------------------------------------------------------------*/
2258 static int
2259 calleeCmp(void *p1, void *p2)
2260 {
2261   return (strcmp((char *)p1, (char *)(p2)) == 0);
2262 }
2263
2264 bool
2265 inCalleeSaveList(char *s)
2266 {
2267   if (options.all_callee_saves)
2268     return 1;
2269   return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2270 }
2271
2272 /*-----------------------------------------------------------------*/
2273 /* aggregateToPointer:  change an agggregate type function      */
2274 /*         argument to a pointer to that type.     */
2275 /*-----------------------------------------------------------------*/
2276 value *
2277 aggregateToPointer (value * val)
2278 {
2279   if (IS_AGGREGATE (val->type))
2280     {
2281       /* if this is a structure */
2282       /* then we need to add a new link */
2283       if (IS_STRUCT (val->type))
2284         {
2285           /* first lets add DECLARATOR type */
2286           sym_link *p = val->type;
2287
2288           werror (W_STRUCT_AS_ARG, val->name);
2289           val->type = newLink (DECLARATOR);
2290           val->type->next = p;
2291         }
2292
2293       /* change to a pointer depending on the */
2294       /* storage class specified        */
2295       switch (SPEC_SCLS (val->etype))
2296         {
2297         case S_IDATA:
2298           DCL_TYPE (val->type) = IPOINTER;
2299           break;
2300         case S_PDATA:
2301           DCL_TYPE (val->type) = PPOINTER;
2302           break;
2303         case S_FIXED:
2304           if (SPEC_OCLS(val->etype)) {
2305             DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2306           } else {
2307             // this happens for (external) function parameters
2308             DCL_TYPE (val->type) = port->unqualified_pointer;
2309           }
2310           break;
2311         case S_AUTO:
2312         case S_DATA:
2313         case S_REGISTER:
2314           DCL_TYPE (val->type) = POINTER;
2315           break;
2316         case S_CODE:
2317           DCL_TYPE (val->type) = CPOINTER;
2318           break;
2319         case S_XDATA:
2320           DCL_TYPE (val->type) = FPOINTER;
2321           break;
2322         case S_EEPROM:
2323           DCL_TYPE (val->type) = EEPPOINTER;
2324           break;
2325         default:
2326           DCL_TYPE (val->type) = port->unqualified_pointer;
2327         }
2328       
2329       /* is there is a symbol associated then */
2330       /* change the type of the symbol as well */
2331       if (val->sym)
2332         {
2333           val->sym->type = copyLinkChain (val->type);
2334           val->sym->etype = getSpec (val->sym->type);
2335         }
2336     }
2337   return val;
2338 }
2339 /*------------------------------------------------------------------*/
2340 /* checkFunction - does all kinds of check on a function            */
2341 /*------------------------------------------------------------------*/
2342 int 
2343 checkFunction (symbol * sym, symbol *csym)
2344 {
2345   value *exargs, *acargs;
2346   value *checkValue;
2347   int argCnt = 0;
2348
2349   if (getenv("DEBUG_SANITY")) {
2350     fprintf (stderr, "checkFunction: %s ", sym->name);
2351   }
2352
2353   if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2354     {
2355       werror(E_SYNTAX_ERROR, sym->name);
2356       return 0;
2357     }
2358     
2359   /* make sure the type is complete and sane */
2360   checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2361
2362   /* if not type then some kind of error */
2363   if (!sym->type)
2364     return 0;
2365
2366   /* if the function has no type then make it return int */
2367   if (!sym->type->next)
2368     sym->type->next = sym->etype = newIntLink ();
2369
2370   /* function cannot return aggregate */
2371   if (IS_AGGREGATE (sym->type->next))
2372     {
2373       werror (E_FUNC_AGGR, sym->name);
2374       return 0;
2375     }
2376
2377   /* check if this function is defined as calleeSaves
2378      then mark it as such */
2379   FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2380
2381   /* if interrupt service routine  */
2382   /* then it cannot have arguments */
2383   if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2384     {
2385       if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2386         werror (E_INT_ARGS, sym->name);
2387         FUNC_ARGS(sym->type)=NULL;
2388       }
2389     }
2390
2391   if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2392     {
2393       werror (E_SHADOWREGS_NO_ISR, sym->name);
2394     }
2395
2396
2397   for (argCnt=1, acargs = FUNC_ARGS(sym->type); 
2398        acargs; 
2399        acargs=acargs->next, argCnt++) {
2400     if (!acargs->sym) { 
2401       // this can happen for reentrant functions
2402       werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2403       // the show must go on: synthesize a name and symbol
2404       SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2405       acargs->sym = newSymbol (acargs->name, 1);
2406       SPEC_OCLS (acargs->etype) = istack;
2407       acargs->sym->type = copyLinkChain (acargs->type);
2408       acargs->sym->etype = getSpec (acargs->sym->type);
2409       acargs->sym->_isparm = 1;
2410       strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2411     } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) { 
2412       // synthesized name
2413       werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2414     }
2415   }
2416   argCnt--;
2417
2418   if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2419     return 1;                   /* not defined nothing more to check  */
2420
2421   /* check if body already present */
2422   if (csym && IFFUNC_HASBODY(csym->type))
2423     {
2424       werror (E_FUNC_BODY, sym->name);
2425       return 0;
2426     }
2427
2428   /* check the return value type   */
2429   if (compareType (csym->type, sym->type) <= 0)
2430     {
2431       werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2432       printFromToType(csym->type, sym->type);
2433       return 0;
2434     }
2435
2436   if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2437     {
2438       werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2439     }
2440
2441   /* I don't think this is necessary for interrupts. An isr is a  */
2442   /* root in the calling tree.                                    */
2443   if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2444       (!FUNC_ISISR (sym->type)))
2445     {
2446       werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2447     }
2448
2449   if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2450     {
2451       werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2452     }
2453
2454   /* Really, reentrant should match regardless of argCnt, but     */
2455   /* this breaks some existing code (the fp lib functions). If    */
2456   /* the first argument is always passed the same way, this       */
2457   /* lax checking is ok (but may not be true for in future ports) */
2458   if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2459       && argCnt>1)
2460     {
2461       //printf("argCnt = %d\n",argCnt);
2462       werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2463     }
2464
2465   if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2466     {
2467       werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2468     }
2469
2470   if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2471     {
2472       werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2473     }
2474   
2475
2476   /* compare expected args with actual args */
2477   exargs = FUNC_ARGS(csym->type);
2478   acargs = FUNC_ARGS(sym->type);
2479
2480   /* for all the expected args do */
2481   for (argCnt = 1;
2482        exargs && acargs;
2483        exargs = exargs->next, acargs = acargs->next, argCnt++)
2484     {
2485       if (getenv("DEBUG_SANITY")) {
2486         fprintf (stderr, "checkFunction: %s ", exargs->name);
2487       }
2488       /* make sure the type is complete and sane */
2489       checkTypeSanity(exargs->etype, exargs->name);
2490
2491       /* If the actual argument is an array, any prototype
2492        * will have modified it to a pointer. Duplicate that
2493        * change here.
2494        */
2495       if (IS_AGGREGATE (acargs->type))
2496         {
2497           checkValue = copyValue (acargs);
2498           aggregateToPointer (checkValue);
2499         }
2500       else
2501         {
2502           checkValue = acargs;
2503         }
2504
2505       if (compareType (exargs->type, checkValue->type) <= 0)
2506         {
2507           werror (E_ARG_TYPE, argCnt);
2508           printFromToType(exargs->type, checkValue->type);
2509           return 0;
2510         }
2511     }
2512
2513   /* if one them ended we have a problem */
2514   if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2515       (!exargs && acargs && !IS_VOID (acargs->type)))
2516     werror (E_ARG_COUNT);
2517
2518   /* replace with this defition */
2519   sym->cdef = csym->cdef;
2520   deleteSym (SymbolTab, csym, csym->name);
2521   deleteFromSeg(csym);
2522   addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2523   if (IS_EXTERN (csym->etype) && !
2524       IS_EXTERN (sym->etype))
2525     {
2526       addSet (&publics, sym);
2527     }
2528   return 1;
2529 }
2530
2531 /*------------------------------------------------------------------*/
2532 /* cdbStructBlock - calls struct printing for a blcks               */
2533 /*------------------------------------------------------------------*/
2534 void cdbStructBlock (int block)
2535 {
2536   int i;
2537   bucket **table = StructTab;
2538   bucket *chain;
2539
2540   /* go thru the entire  table  */
2541   for (i = 0; i < 256; i++)
2542     {
2543       for (chain = table[i]; chain; chain = chain->next)
2544         {
2545           if (chain->block >= block)
2546             {
2547               if(debugFile)
2548                 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2549             }
2550         }
2551     }
2552 }
2553
2554 /*-----------------------------------------------------------------*/
2555 /* processFuncArgs - does some processing with function args       */
2556 /*-----------------------------------------------------------------*/
2557 void 
2558 processFuncArgs (symbol * func)
2559 {
2560   value *val;
2561   int pNum = 1;
2562   sym_link *funcType=func->type;
2563
2564   if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2565     fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2566
2567   /* find the function declaration within the type */
2568   while (funcType && !IS_FUNC(funcType))
2569     funcType=funcType->next;
2570
2571   /* if this function has variable argument list */
2572   /* then make the function a reentrant one    */
2573   if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2574     FUNC_ISREENT(funcType)=1;
2575
2576   /* check if this function is defined as calleeSaves
2577      then mark it as such */
2578   FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2579
2580   /* loop thru all the arguments   */
2581   val = FUNC_ARGS(funcType);
2582
2583   /* if it is void then remove parameters */
2584   if (val && IS_VOID (val->type))
2585     {
2586       FUNC_ARGS(funcType) = NULL;
2587       return;
2588     }
2589
2590   /* reset regparm for the port */
2591   (*port->reset_regparms) ();
2592
2593   /* if any of the arguments is an aggregate */
2594   /* change it to pointer to the same type */
2595   while (val)
2596     {
2597       int argreg = 0;
2598       char buffer[SDCC_NAME_MAX+1];
2599       
2600       SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2601       checkTypeSanity (val->etype, buffer);
2602       
2603       /* mark it as a register parameter if
2604          the function does not have VA_ARG
2605          and as port dictates */
2606       if (!IFFUNC_HASVARARGS(funcType) &&
2607           (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2608         {
2609           SPEC_REGPARM (val->etype) = 1;
2610           SPEC_ARGREG(val->etype) = argreg;
2611         } else if (IFFUNC_ISREENT(funcType)) {
2612             FUNC_HASSTACKPARM(funcType) = 1;
2613         }
2614
2615       if (IS_AGGREGATE (val->type))
2616         {
2617           aggregateToPointer (val);
2618         }
2619
2620       val = val->next;
2621       pNum++;
2622     }
2623
2624   /* if this is an internal generated function call */
2625   if (func->cdef) {
2626     /* ignore --stack-auto for this one, we don't know how it is compiled */
2627     /* simply trust on --int-long-reent or --float-reent */
2628     if (IFFUNC_ISREENT(funcType)) {
2629       return;
2630     }
2631   } else {
2632     /* if this function is reentrant or */
2633     /* automatics r 2b stacked then nothing */
2634     if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2635       return;
2636   }
2637
2638   val = FUNC_ARGS(funcType);
2639   pNum = 1;
2640   while (val)
2641     {
2642
2643       /* if a symbolname is not given  */
2644       /* synthesize a variable name */
2645       if (!val->sym)
2646         {
2647           SNPRINTF (val->name, sizeof(val->name), 
2648                     "_%s_PARM_%d", func->name, pNum++);
2649           val->sym = newSymbol (val->name, 1);
2650           if (SPEC_SCLS(val->etype) == S_BIT)
2651             SPEC_OCLS (val->etype) = bit;
2652           else
2653             SPEC_OCLS (val->etype) = port->mem.default_local_map;
2654           val->sym->type = copyLinkChain (val->type);
2655           val->sym->etype = getSpec (val->sym->type);
2656           val->sym->_isparm = 1;
2657           strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2658           #if 0
2659           /* ?? static functions shouldn't imply static parameters - EEP */
2660           if (IS_SPEC(func->etype)) {
2661             SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2662               SPEC_STAT (func->etype);
2663           }
2664           #endif
2665           addSymChain (&val->sym);
2666
2667         }
2668       else                      /* symbol name given create synth name */
2669         {
2670
2671           SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2672           strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2673           val->sym->_isparm = 1;
2674           if (SPEC_SCLS(val->etype) == S_BIT)
2675             SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2676           else
2677             SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2678               port->mem.default_local_map;
2679           
2680           #if 0
2681           /* ?? static functions shouldn't imply static parameters - EEP */
2682           if (IS_SPEC(func->etype)) {
2683             SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2684               SPEC_STAT (func->etype);
2685           }
2686           #endif
2687         }
2688       if (SPEC_OCLS (val->sym->etype) == pdata)
2689         val->sym->iaccess = 1;
2690       if (!isinSet(operKeyReset, val->sym)) {
2691         addSet (&operKeyReset, val->sym);
2692         applyToSet (operKeyReset, resetParmKey);
2693       }
2694       val = val->next;
2695     }
2696 }
2697
2698 /*-----------------------------------------------------------------*/
2699 /* isSymbolEqual - compares two symbols return 1 if they match     */
2700 /*-----------------------------------------------------------------*/
2701 int 
2702 isSymbolEqual (symbol * dest, symbol * src)
2703 {
2704   /* if pointers match then equal */
2705   if (dest == src)
2706     return 1;
2707
2708   /* if one of them is null then don't match */
2709   if (!dest || !src)
2710     return 0;
2711
2712   /* if both of them have rname match on rname */
2713   if (dest->rname[0] && src->rname[0])
2714     return (!strcmp (dest->rname, src->rname));
2715
2716   /* otherwise match on name */
2717   return (!strcmp (dest->name, src->name));
2718 }
2719
2720 void PT(sym_link *type)
2721 {
2722         printTypeChain(type,0);
2723 }
2724 /*-----------------------------------------------------------------*/
2725 /* printTypeChain - prints the type chain in human readable form   */
2726 /*-----------------------------------------------------------------*/
2727 void
2728 printTypeChain (sym_link * start, FILE * of)
2729 {
2730   int nlr = 0;
2731   value *args;
2732   sym_link * type, * search;
2733   STORAGE_CLASS scls;
2734
2735   if (!of)
2736     {
2737       of = stdout;
2738       nlr = 1;
2739     }
2740
2741   if (start==NULL) {
2742     fprintf (of, "void");
2743     return;
2744   }
2745
2746   /* Print the chain as it is written in the source: */
2747   /* start with the last entry.                      */
2748   /* However, the storage class at the end of the    */
2749   /* chain reall applies to the first in the chain!  */
2750
2751   for (type = start; type && type->next; type = type->next)
2752     ;
2753   if (IS_SPEC (type))
2754     scls=SPEC_SCLS(type);
2755   else
2756     scls=0;
2757   while (type)
2758     {
2759       if (type==start) {
2760         switch (scls) 
2761           {
2762           case S_DATA: fprintf (of, "data-"); break;
2763           case S_XDATA: fprintf (of, "xdata-"); break;
2764           case S_SFR: fprintf (of, "sfr-"); break;
2765           case S_SBIT: fprintf (of, "sbit-"); break;
2766           case S_CODE: fprintf (of, "code-"); break;
2767           case S_IDATA: fprintf (of, "idata-"); break;
2768           case S_PDATA: fprintf (of, "pdata-"); break;
2769           case S_LITERAL: fprintf (of, "literal-"); break;
2770           case S_STACK: fprintf (of, "stack-"); break;
2771           case S_XSTACK: fprintf (of, "xstack-"); break;
2772           case S_BIT: fprintf (of, "bit-"); break;
2773           case S_EEPROM: fprintf (of, "eeprom-"); break;
2774           default: break;
2775           }
2776       }
2777
2778       if (IS_DECL (type))
2779         {
2780           if (!IS_FUNC(type)) {
2781             if (DCL_PTR_VOLATILE (type)) {
2782               fprintf (of, "volatile-");
2783             }
2784             if (DCL_PTR_CONST (type)) {
2785               fprintf (of, "const-");
2786             }
2787           }
2788           switch (DCL_TYPE (type))
2789             {
2790             case FUNCTION:
2791               fprintf (of, "function %s %s", 
2792                        (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2793                        (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2794               fprintf (of, "( ");
2795               for (args = FUNC_ARGS(type); 
2796                    args; 
2797                    args=args->next) {
2798                 printTypeChain(args->type, of);
2799                 if (args->next)
2800                   fprintf(of, ", ");
2801               }
2802               fprintf (of, ") ");
2803               break;
2804             case GPOINTER:
2805               fprintf (of, "generic* ");
2806               break;
2807             case CPOINTER:
2808               fprintf (of, "code* ");
2809               break;
2810             case FPOINTER:
2811               fprintf (of, "xdata* ");
2812               break;
2813             case EEPPOINTER:
2814               fprintf (of, "eeprom* ");
2815               break;
2816             case POINTER:
2817               fprintf (of, "near* ");
2818               break;
2819             case IPOINTER:
2820               fprintf (of, "idata* ");
2821               break;
2822             case PPOINTER:
2823               fprintf (of, "pdata* ");
2824               break;
2825             case UPOINTER:
2826               fprintf (of, "unknown* ");
2827               break;
2828             case ARRAY:
2829               if (DCL_ELEM(type)) {
2830                 fprintf (of, "[%d] ", DCL_ELEM(type));
2831               } else {
2832                 fprintf (of, "[] ");
2833               }
2834               break;
2835             }
2836         }
2837       else
2838         {
2839           if (SPEC_VOLATILE (type))
2840             fprintf (of, "volatile-");
2841           if (SPEC_CONST (type))
2842             fprintf (of, "const-");
2843           if (SPEC_USIGN (type))
2844             fprintf (of, "unsigned-");
2845           switch (SPEC_NOUN (type))
2846             {
2847             case V_INT:
2848               if (IS_LONG (type))
2849                 fprintf (of, "long-");
2850               fprintf (of, "int");
2851               break;
2852
2853             case V_CHAR:
2854               fprintf (of, "char");
2855               break;
2856
2857             case V_VOID:
2858               fprintf (of, "void");
2859               break;
2860
2861             case V_FLOAT:
2862               fprintf (of, "float");
2863               break;
2864
2865             case V_FIXED16X16:
2866               fprintf (of, "fixed16x16");
2867               break;
2868
2869             case V_STRUCT:
2870               fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2871               break;
2872
2873             case V_SBIT:
2874               fprintf (of, "sbit");
2875               break;
2876
2877             case V_BIT:
2878               fprintf (of, "bit");
2879               break;
2880
2881             case V_BITFIELD:
2882               fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2883               break;
2884
2885             case V_DOUBLE:
2886               fprintf (of, "double");
2887               break;
2888
2889             default:
2890               fprintf (of, "unknown type");
2891               break;
2892             }
2893         }
2894       /* search entry in list before "type" */
2895       for (search = start; search && search->next != type;)
2896         search = search->next;
2897       type = search;
2898       if (type)
2899         fputc (' ', of);
2900     }
2901   if (nlr)
2902     fprintf (of, "\n");
2903 }
2904
2905 /*--------------------------------------------------------------------*/
2906 /* printTypeChainRaw - prints the type chain in human readable form   */
2907 /*                     in the raw data structure ordering             */
2908 /*--------------------------------------------------------------------*/
2909 void
2910 printTypeChainRaw (sym_link * start, FILE * of)
2911 {
2912   int nlr = 0;
2913   value *args;
2914   sym_link * type;
2915
2916   if (!of)
2917     {
2918       of = stdout;
2919       nlr = 1;
2920     }
2921
2922   if (start==NULL) {
2923     fprintf (of, "void");
2924     return;
2925   }
2926
2927   type = start;
2928   
2929   while (type)
2930     {
2931       if (IS_DECL (type))
2932         {
2933           if (!IS_FUNC(type)) {
2934             if (DCL_PTR_VOLATILE (type)) {
2935               fprintf (of, "volatile-");
2936             }
2937             if (DCL_PTR_CONST (type)) {
2938               fprintf (of, "const-");
2939             }
2940           }
2941           switch (DCL_TYPE (type))
2942             {
2943             case FUNCTION:
2944               fprintf (of, "function %s %s", 
2945                        (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2946                        (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2947               fprintf (of, "( ");
2948               for (args = FUNC_ARGS(type); 
2949                    args; 
2950                    args=args->next) {
2951                 printTypeChain(args->type, of);
2952                 if (args->next)
2953                   fprintf(of, ", ");
2954               }
2955               fprintf (of, ") ");
2956               break;
2957             case GPOINTER:
2958               fprintf (of, "generic* ");
2959               break;
2960             case CPOINTER:
2961               fprintf (of, "code* ");
2962               break;
2963             case FPOINTER:
2964               fprintf (of, "xdata* ");
2965               break;
2966             case EEPPOINTER:
2967               fprintf (of, "eeprom* ");
2968               break;
2969             case POINTER:
2970               fprintf (of, "near* ");
2971               break;
2972             case IPOINTER:
2973               fprintf (of, "idata* ");
2974               break;
2975             case PPOINTER:
2976               fprintf (of, "pdata* ");
2977               break;
2978             case UPOINTER:
2979               fprintf (of, "unknown* ");
2980               break;
2981             case ARRAY:
2982               if (DCL_ELEM(type)) {
2983                 fprintf (of, "[%d] ", DCL_ELEM(type));
2984               } else {
2985                 fprintf (of, "[] ");
2986               }
2987               break;
2988             }
2989           if (DCL_TSPEC(type))
2990             {
2991               fprintf (of, "{");
2992               printTypeChainRaw(DCL_TSPEC(type), of);
2993               fprintf (of, "}");
2994             }
2995         }
2996       else if (IS_SPEC (type))
2997         {
2998         switch (SPEC_SCLS (type)) 
2999           {
3000           case S_DATA: fprintf (of, "data-"); break;
3001           case S_XDATA: fprintf (of, "xdata-"); break;
3002           case S_SFR: fprintf (of, "sfr-"); break;
3003           case S_SBIT: fprintf (of, "sbit-"); break;
3004           case S_CODE: fprintf (of, "code-"); break;
3005           case S_IDATA: fprintf (of, "idata-"); break;
3006           case S_PDATA: fprintf (of, "pdata-"); break;
3007           case S_LITERAL: fprintf (of, "literal-"); break;
3008           case S_STACK: fprintf (of, "stack-"); break;
3009           case S_XSTACK: fprintf (of, "xstack-"); break;
3010           case S_BIT: fprintf (of, "bit-"); break;
3011           case S_EEPROM: fprintf (of, "eeprom-"); break;
3012           default: break;
3013           }
3014           if (SPEC_VOLATILE (type))
3015             fprintf (of, "volatile-");
3016           if (SPEC_CONST (type))
3017             fprintf (of, "const-");
3018           if (SPEC_USIGN (type))
3019             fprintf (of, "unsigned-");
3020           switch (SPEC_NOUN (type))
3021             {
3022             case V_INT:
3023               if (IS_LONG (type))
3024                 fprintf (of, "long-");
3025               fprintf (of, "int");
3026               break;
3027
3028             case V_CHAR:
3029               fprintf (of, "char");
3030               break;
3031
3032             case V_VOID:
3033               fprintf (of, "void");
3034               break;
3035
3036             case V_FLOAT:
3037               fprintf (of, "float");
3038               break;
3039
3040             case V_FIXED16X16:
3041               fprintf (of, "fixed16x16");
3042               break;
3043
3044             case V_STRUCT:
3045               fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3046               break;
3047
3048             case V_SBIT:
3049               fprintf (of, "sbit");
3050               break;
3051
3052             case V_BIT:
3053               fprintf (of, "bit");
3054               break;
3055
3056             case V_BITFIELD:
3057               fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3058               break;
3059
3060             case V_DOUBLE:
3061               fprintf (of, "double");
3062               break;
3063
3064             default:
3065               fprintf (of, "unknown type");
3066               break;
3067             }
3068         }
3069       else
3070         fprintf (of, "NOT_SPEC_OR_DECL");
3071       type = type->next;
3072       if (type)
3073         fputc (' ', of);
3074     }
3075   if (nlr)
3076     fprintf (of, "\n");
3077 }
3078
3079
3080 /*-----------------------------------------------------------------*/
3081 /* powof2 - returns power of two for the number if number is pow 2 */
3082 /*-----------------------------------------------------------------*/
3083 int
3084 powof2 (TYPE_UDWORD num)
3085 {
3086   int nshifts = 0;
3087   int n1s = 0;
3088
3089   while (num)
3090     {
3091       if (num & 1)
3092         n1s++;
3093       num >>= 1;
3094       nshifts++;
3095     }
3096
3097   if (n1s > 1 || nshifts == 0)
3098     return -1;
3099   return nshifts - 1;
3100 }
3101
3102 symbol *__fsadd;
3103 symbol *__fssub;
3104 symbol *__fsmul;
3105 symbol *__fsdiv;
3106 symbol *__fseq;
3107 symbol *__fsneq;
3108 symbol *__fslt;
3109 symbol *__fslteq;
3110 symbol *__fsgt;
3111 symbol *__fsgteq;
3112
3113 symbol *__fps16x16_add;
3114 symbol *__fps16x16_sub;
3115 symbol *__fps16x16_mul;
3116 symbol *__fps16x16_div;
3117 symbol *__fps16x16_eq;
3118 symbol *__fps16x16_neq;
3119 symbol *__fps16x16_lt;
3120 symbol *__fps16x16_lteq;
3121 symbol *__fps16x16_gt;
3122 symbol *__fps16x16_gteq;
3123
3124 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3125 symbol *__muldiv[3][3][2];
3126 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3127 sym_link *__multypes[3][2];
3128 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3129 symbol *__conv[2][3][2];
3130 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3131 symbol *__fp16x16conv[2][4][2];
3132 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3133 symbol *__rlrr[2][3][2];
3134
3135 sym_link *floatType;
3136 sym_link *fixed16x16Type;
3137
3138 static char *
3139 _mangleFunctionName(char *in)
3140 {
3141   if (port->getMangledFunctionName)
3142     {
3143       return port->getMangledFunctionName(in);
3144     }
3145   else
3146     {
3147       return in;
3148     }
3149 }
3150
3151 /*-----------------------------------------------------------------*/
3152 /* typeFromStr - create a typechain from an encoded string         */
3153 /* basic types -        'c' - char                                 */
3154 /*                      's' - short                                */
3155 /*                      'i' - int                                  */
3156 /*                      'l' - long                                 */
3157 /*                      'f' - float                                */
3158 /*                      'q' - fixed16x16                           */
3159 /*                      'v' - void                                 */
3160 /*                      '*' - pointer - default (GPOINTER)         */
3161 /* modifiers -          'u' - unsigned                             */
3162 /* pointer modifiers -  'g' - generic                              */
3163 /*                      'x' - xdata                                */
3164 /*                      'p' - code                                 */
3165 /*                      'd' - data                                 */                     
3166 /*                      'F' - function                             */                     
3167 /* examples : "ig*" - generic int *                                */
3168 /*            "cx*" - char xdata *                                 */
3169 /*            "ui" -  unsigned int                                 */
3170 /*-----------------------------------------------------------------*/
3171 sym_link *typeFromStr (char *s)
3172 {
3173     sym_link *r = newLink(DECLARATOR);
3174     int usign = 0;
3175
3176     do {
3177         sym_link *nr;
3178         switch (*s) {
3179         case 'u' : 
3180             usign = 1;
3181             s++;
3182             continue ;
3183             break ;
3184         case 'c':
3185             r->class = SPECIFIER;
3186             SPEC_NOUN(r) = V_CHAR;
3187             break;
3188         case 's':
3189         case 'i':
3190             r->class = SPECIFIER;
3191             SPEC_NOUN(r) = V_INT;
3192             break;
3193         case 'l':
3194             r->class = SPECIFIER;
3195             SPEC_NOUN(r) = V_INT;
3196             SPEC_LONG(r) = 1;
3197             break;
3198         case 'f':
3199             r->class = SPECIFIER;
3200             SPEC_NOUN(r) = V_FLOAT;
3201             break;
3202         case 'q':
3203             r->class = SPECIFIER;
3204             SPEC_NOUN(r) = V_FIXED16X16;
3205             break;
3206         case 'v':
3207             r->class = SPECIFIER;
3208             SPEC_NOUN(r) = V_VOID;
3209             break;
3210         case '*':
3211             DCL_TYPE(r) = port->unqualified_pointer;
3212             break;
3213         case 'g':
3214         case 'x':
3215         case 'p':
3216         case 'd':
3217         case 'F':
3218             assert(*(s+1)=='*');
3219             nr = newLink(DECLARATOR);
3220             nr->next = r;
3221             r = nr;
3222             switch (*s) {
3223             case 'g':
3224                 DCL_TYPE(r) = GPOINTER;
3225                 break;
3226             case 'x':
3227                 DCL_TYPE(r) = FPOINTER;
3228                 break;
3229             case 'p':
3230                 DCL_TYPE(r) = CPOINTER;
3231                 break;
3232             case 'd':
3233                 DCL_TYPE(r) = POINTER;
3234                 break;
3235             case 'F':
3236                 DCL_TYPE(r) = FUNCTION;
3237                 nr = newLink(DECLARATOR);
3238                 nr->next = r;
3239                 r = nr;
3240                 DCL_TYPE(r) = CPOINTER;
3241                 break;
3242             }
3243             s++;
3244             break;
3245         default:
3246             werror(E_INTERNAL_ERROR, __FILE__, __LINE__, 
3247                    "typeFromStr: unknown type");
3248             break;
3249         }
3250         if (IS_SPEC(r) && usign) {
3251             SPEC_USIGN(r) = 1;
3252             usign = 0;
3253         }
3254         s++;
3255     } while (*s);
3256     return r;
3257 }
3258
3259 /*-----------------------------------------------------------------*/
3260 /* initCSupport - create functions for C support routines          */
3261 /*-----------------------------------------------------------------*/
3262 void 
3263 initCSupport ()
3264 {
3265   const char *smuldivmod[] =
3266   {
3267     "mul", "div", "mod"
3268   };
3269   const char *sbwd[] =
3270   {
3271     "char", "int", "long", "fixed16x16",
3272   };
3273   const char *fp16x16sbwd[] =
3274   {
3275     "char", "int", "long", "float",
3276   };
3277   const char *ssu[] =
3278   {
3279     "s", "u"
3280   };
3281   const char *srlrr[] =
3282   {
3283     "rl", "rr"
3284   };
3285
3286   int bwd, su, muldivmod, tofrom, rlrr;
3287
3288   if (getenv("SDCC_NO_C_SUPPORT")) {
3289     /* for debugging only */
3290     return;
3291   }
3292
3293   floatType = newFloatLink ();
3294   fixed16x16Type = newFixed16x16Link ();
3295
3296   for (bwd = 0; bwd < 3; bwd++)
3297     {
3298       sym_link *l = NULL;
3299       switch (bwd)
3300         {
3301         case 0:
3302           l = newCharLink ();
3303           break;
3304         case 1:
3305           l = newIntLink ();
3306           break;
3307         case 2:
3308           l = newLongLink ();
3309           break;
3310         default:
3311           assert (0);
3312         }
3313       __multypes[bwd][0] = l;
3314       __multypes[bwd][1] = copyLinkChain (l);
3315       SPEC_USIGN (__multypes[bwd][1]) = 1;
3316     }
3317
3318   __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3319   __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3320   __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3321   __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3322   __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3323   __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3324   __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3325   __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3326   __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3327   __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3328
3329   __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3330   __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3331   __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3332   __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3333   __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3334   __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3335   __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3336   __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3337   __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3338   __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3339
3340
3341   for (tofrom = 0; tofrom < 2; tofrom++)
3342     {
3343       for (bwd = 0; bwd < 3; bwd++)
3344         {
3345           for (su = 0; su < 2; su++)
3346             {
3347               if (tofrom)
3348                 {
3349                   SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3350                   __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3351                 }
3352               else
3353                 {
3354                   SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3355                   __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3356                 }
3357             }
3358         }
3359     }
3360
3361   for (tofrom = 0; tofrom < 2; tofrom++)
3362     {
3363       for (bwd = 0; bwd < 4; bwd++)
3364         {
3365           for (su = 0; su < 2; su++)
3366             {
3367               if (tofrom)
3368                 {
3369                   SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3370                   if(bwd == 3) {
3371                     __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3372                   } else
3373                     __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3374                 }
3375               else
3376                 {
3377                   SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3378                   if(bwd == 3) {
3379                     __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3380                   } else
3381                     __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3382                 }
3383             }
3384         }
3385     }
3386
3387 /*
3388   for (muldivmod = 0; muldivmod < 3; muldivmod++)
3389     {
3390       for (bwd = 0; bwd < 3; bwd++)
3391         {
3392           for (su = 0; su < 2; su++)
3393             {
3394               SNPRINTF (buffer, sizeof(buffer),
3395                         "_%s%s%s",
3396                        smuldivmod[muldivmod],
3397                        ssu[su],
3398                        sbwd[bwd]);
3399               __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3400               FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3401             }
3402         }
3403     }
3404
3405   muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3406   Therefore they've been merged into mulint() and mullong().
3407 */
3408
3409   for (bwd = 0; bwd < 3; bwd++)
3410     {
3411       for (su = 0; su < 2; su++)
3412         {
3413           for (muldivmod = 1; muldivmod < 3; muldivmod++)
3414             {
3415               /* div and mod */
3416               SNPRINTF (buffer, sizeof(buffer),
3417                         "_%s%s%s",
3418                        smuldivmod[muldivmod],
3419                        ssu[su],
3420                        sbwd[bwd]);
3421               __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3422               FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3423             }
3424         }
3425     }
3426   /* mul only */
3427   muldivmod = 0;
3428   /* byte */
3429   bwd = 0;
3430   for (su = 0; su < 2; su++)
3431     {
3432       /* muluchar and mulschar are still separate functions, because e.g. the z80
3433          port is sign/zero-extending to int before calling mulint() */
3434       SNPRINTF (buffer, sizeof(buffer),
3435                 "_%s%s%s",
3436                 smuldivmod[muldivmod],
3437                 ssu[su],
3438                 sbwd[bwd]);
3439       __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3440       FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3441     }
3442   /* signed only */
3443   su = 0;
3444   /* word and doubleword */
3445   for (bwd = 1; bwd < 3; bwd++)
3446     {
3447       /* mul, int/long */
3448       SNPRINTF (buffer, sizeof(buffer),
3449                 "_%s%s",
3450                 smuldivmod[muldivmod],
3451                 sbwd[bwd]);
3452       __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3453       FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3454       /* signed = unsigned */
3455       __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3456     }
3457
3458   for (rlrr = 0; rlrr < 2; rlrr++)
3459     {
3460       for (bwd = 0; bwd < 3; bwd++)
3461         {
3462           for (su = 0; su < 2; su++)
3463             {
3464               SNPRINTF (buffer, sizeof(buffer),
3465                         "_%s%s%s",
3466                        srlrr[rlrr],
3467                        ssu[su],
3468                        sbwd[bwd]);
3469               __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3470               FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3471             }
3472         }
3473     }
3474 }
3475
3476 /*-----------------------------------------------------------------*/
3477 /* initBuiltIns - create prototypes for builtin functions          */
3478 /*-----------------------------------------------------------------*/
3479 void initBuiltIns()
3480 {
3481     int i;
3482     symbol *sym;
3483
3484     if (!port->builtintable) return ;
3485
3486     for (i = 0 ; port->builtintable[i].name ; i++) {
3487         sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3488                              port->builtintable[i].nParms,port->builtintable[i].parm_types);
3489         FUNC_ISBUILTIN(sym->type) = 1;
3490         FUNC_ISREENT(sym->type) = 0;    /* can never be reentrant */
3491     }
3492 }
3493
3494 sym_link *validateLink(sym_link         *l, 
3495                         const char      *macro,
3496                         const char      *args,
3497                         const char      select,
3498                         const char      *file, 
3499                         unsigned        line)
3500 {    
3501   if (l && l->class==select)
3502     {
3503         return l;
3504     }
3505     fprintf(stderr, 
3506             "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3507             " expected %s, got %s\n",
3508             macro, args, file, line, 
3509             DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3510     exit(-1);
3511     return l; // never reached, makes compiler happy.
3512 }
3513
3514 /*--------------------------------------------------------------------*/
3515 /* newEnumType - create an integer type compatible with enumerations  */
3516 /*--------------------------------------------------------------------*/
3517 sym_link *
3518 newEnumType (symbol *enumlist)
3519 {
3520   int min, max, v;
3521   symbol *sym;
3522   sym_link *type;
3523
3524   if (!enumlist)
3525     {
3526       type = newLink (SPECIFIER);
3527       SPEC_NOUN (type) = V_INT;
3528       return type;
3529     }
3530       
3531   /* Determine the range of the enumerated values */
3532   sym = enumlist;
3533   min = max = (int) floatFromVal (valFromType (sym->type));
3534   for (sym = sym->next; sym; sym = sym->next)
3535     {
3536       v = (int) floatFromVal (valFromType (sym->type));
3537       if (v<min)
3538         min = v;
3539       if (v>max)
3540         max = v;
3541     }
3542
3543   /* Determine the smallest integer type that is compatible with this range */
3544   type = newLink (SPECIFIER);
3545   if (min>=0 && max<=255)
3546     {
3547       SPEC_NOUN (type) = V_CHAR;
3548       SPEC_USIGN (type) = 1;
3549     }
3550   else if (min>=-128 && max<=127)
3551     {
3552       SPEC_NOUN (type) = V_CHAR;
3553     }
3554   else if (min>=0 && max<=65535)
3555     {
3556       SPEC_NOUN (type) = V_INT;
3557       SPEC_USIGN (type) = 1;
3558     }
3559   else if (min>=-32768 && max<=32767)
3560     {
3561       SPEC_NOUN (type) = V_INT;
3562     }
3563   else
3564     {
3565       SPEC_NOUN (type) = V_INT;
3566       SPEC_LONG (type) = 1;
3567       if (min>=0)
3568         SPEC_USIGN (type) = 1;
3569     }
3570   
3571   return type;    
3572 }