1 /*-------------------------------------------------------------------------
2 SDCCsymt.c - Code file for Symbols table related structures and MACRO's.
3 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
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
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.
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.
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 -------------------------------------------------------------------------*/
26 #include "dbuf_string.h"
30 value *aggregateToPointer (value *val);
31 void printTypeChainRaw (sym_link * start, FILE * of);
33 void printFromToType(sym_link *from, sym_link *to) {
34 fprintf (stderr, "from type '");
35 printTypeChain (from, stderr);
36 fprintf (stderr, "'\nto type '");
37 printTypeChain (to, stderr);
38 fprintf (stderr, "'\n");
42 char *nounName(sym_link *sl) {
43 switch (SPEC_NOUN(sl))
46 if (SPEC_LONG(sl)) return "long";
47 if (SPEC_SHORT(sl)) return "short";
50 case V_FLOAT: return "float";
51 case V_FIXED16X16: return "fixed16x16";
52 case V_CHAR: return "char";
53 case V_VOID: return "void";
54 case V_STRUCT: return "struct";
55 case V_LABEL: return "label";
56 case V_BITFIELD: return "bitfield";
57 case V_BIT: return "bit";
58 case V_SBIT: return "sbit";
59 case V_DOUBLE: return "double";
64 bucket *SymbolTab[256]; /* the symbol table */
65 bucket *StructTab[256]; /* the structure table */
66 bucket *TypedefTab[256]; /* the typedef table */
67 bucket *LabelTab[256]; /* the Label table */
68 bucket *enumTab[256]; /* enumerated table */
70 /*------------------------------------------------------------------*/
71 /* initSymt () - initialises symbol table related stuff */
72 /*------------------------------------------------------------------*/
78 for (i = 0; i < 256; i++)
79 SymbolTab[i] = StructTab[i] = (void *) NULL;
83 /*-----------------------------------------------------------------*/
84 /* newBucket - allocates & returns a new bucket */
85 /*-----------------------------------------------------------------*/
91 bp = Safe_alloc ( sizeof (bucket));
96 /*-----------------------------------------------------------------*/
97 /* hashKey - computes the hashkey given a symbol name */
98 /*-----------------------------------------------------------------*/
100 hashKey (const char *s)
102 unsigned long key = 0;
109 /*-----------------------------------------------------------------*/
110 /* addSym - adds a symbol to the hash Table */
111 /*-----------------------------------------------------------------*/
113 addSym (bucket ** stab,
120 int i; /* index into the hash Table */
121 bucket *bp; /* temp bucket * */
124 symbol *csym = (symbol *)sym;
126 if (getenv("DEBUG_SANITY")) {
127 fprintf (stderr, "addSym: %s ", sname);
129 /* make sure the type is complete and sane */
130 checkTypeSanity(csym->etype, csym->name);
133 /* prevent overflow of the (r)name buffers */
134 if (strlen(sname)>SDCC_SYMNAME_MAX) {
135 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
136 sname[SDCC_SYMNAME_MAX]='\0';
139 /* the symbols are always added at the head of the list */
141 /* get a free entry */
142 bp = Safe_alloc ( sizeof (bucket));
144 bp->sym = sym; /* update the symbol pointer */
145 bp->level = level; /* update the nest level */
147 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
149 /* if this is the first entry */
152 bp->prev = bp->next = (void *) NULL; /* point to nothing */
155 /* not first entry then add @ head of list */
165 /*-----------------------------------------------------------------*/
166 /* deleteSym - deletes a symbol from the hash Table entry */
167 /*-----------------------------------------------------------------*/
169 deleteSym (bucket ** stab, void *sym, char *sname)
177 /* find the symbol */
180 if (bp->sym == sym) /* found it then break out */
181 break; /* of the loop */
185 if (!bp) /* did not find it */
187 /* if this is the first one in the chain */
191 if (stab[i]) /* if chain ! empty */
192 stab[i]->prev = (void *) NULL;
194 /* middle || end of chain */
197 if (bp->next) /* if not end of chain */
198 bp->next->prev = bp->prev;
200 bp->prev->next = bp->next;
205 /*-----------------------------------------------------------------*/
206 /* findSym - finds a symbol in a table */
207 /*-----------------------------------------------------------------*/
209 findSym (bucket ** stab, void *sym, const char *sname)
213 bp = stab[hashKey (sname)];
216 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
221 return (bp ? bp->sym : (void *) NULL);
224 /*-----------------------------------------------------------------*/
225 /* findSymWithLevel - finds a symbol with a name & level */
226 /*-----------------------------------------------------------------*/
228 findSymWithLevel (bucket ** stab, symbol * sym)
232 bp = stab[hashKey (sym->name)];
235 ** do the search from the head of the list since the
236 ** elements are added at the head it is ensured that
237 ** we will find the deeper definitions before we find
238 ** the global ones. we need to check for symbols with
239 ** level <= to the level given, if levels match then block
240 ** numbers need to match as well
244 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
246 /* if this is parameter then nothing else need to be checked */
247 if (((symbol *) (bp->sym))->_isparm)
249 /* if levels match then block numbers should also match */
250 if (bp->level && bp->level == sym->level && bp->block == sym->block)
252 /* if levels don't match then we are okay */
253 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
255 /* if this is a global variable then we are ok too */
263 return (void *) NULL;
266 /*-----------------------------------------------------------------*/
267 /* findSymWithBlock - finds a symbol with name in with a block */
268 /*-----------------------------------------------------------------*/
270 findSymWithBlock (bucket ** stab, symbol * sym, int block)
274 bp = stab[hashKey (sym->name)];
277 if (strcmp (bp->name, sym->name) == 0 &&
283 return (bp ? bp->sym : (void *) NULL);
286 /*------------------------------------------------------------------*/
287 /* newSymbol () - returns a new pointer to a symbol */
288 /*------------------------------------------------------------------*/
290 newSymbol (char *name, int scope)
294 sym = Safe_alloc ( sizeof (symbol));
296 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
297 sym->level = scope; /* set the level */
298 sym->block = currBlockno;
299 sym->lineDef = lexLineno; /* set the line number */
300 sym->fileDef = lexFilename;
304 /*------------------------------------------------------------------*/
305 /* newLink - creates a new link (declarator,specifier) */
306 /*------------------------------------------------------------------*/
308 newLink (SYM_LINK_CLASS select)
312 p = Safe_alloc ( sizeof (sym_link));
318 /*------------------------------------------------------------------*/
319 /* newStruct - creats a new structdef from the free list */
320 /*------------------------------------------------------------------*/
322 newStruct (char *tag)
326 s = Safe_alloc ( sizeof (structdef));
328 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
332 /*------------------------------------------------------------------*/
333 /* sclsFromPtr - Return the storage class a pointer points into. */
334 /* S_FIXED is returned for generic pointers or other */
335 /* unexpected cases */
336 /*------------------------------------------------------------------*/
338 sclsFromPtr(sym_link *ptr)
340 switch (DCL_TYPE (ptr))
363 /*------------------------------------------------------------------*/
364 /* pointerTypes - do the computation for the pointer types */
365 /*------------------------------------------------------------------*/
367 pointerTypes (sym_link * ptr, sym_link * type)
372 /* find the first pointer type */
373 while (ptr && !IS_PTR (ptr))
376 /* could not find it */
377 if (!ptr || IS_SPEC (ptr))
380 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
381 pointerTypes (ptr->next, type);
385 /* change the pointer type depending on the
386 storage class of the type */
389 switch (SPEC_SCLS (type))
392 DCL_TYPE (ptr) = FPOINTER;
395 DCL_TYPE (ptr) = IPOINTER;
398 DCL_TYPE (ptr) = PPOINTER;
401 DCL_TYPE (ptr) = POINTER;
404 DCL_TYPE (ptr) = CPOINTER;
407 DCL_TYPE (ptr) = EEPPOINTER;
410 DCL_TYPE (ptr) = port->unqualified_pointer;
413 /* the storage class of type ends here */
414 SPEC_SCLS (type) = 0;
417 /* now change all the remaining unknown pointers
418 to generic pointers */
421 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
422 DCL_TYPE (ptr) = port->unqualified_pointer;
426 /* same for the type although it is highly unlikely that
427 type will have a pointer */
430 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
431 DCL_TYPE (type) = port->unqualified_pointer;
436 /*------------------------------------------------------------------*/
437 /* addDecl - adds a declarator @ the end of a chain */
438 /*------------------------------------------------------------------*/
440 addDecl (symbol * sym, int type, sym_link * p)
442 static sym_link *empty = NULL;
447 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
448 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
451 empty = newLink(SPECIFIER);
453 /* if we are passed a link then set head & tail */
462 head = tail = newLink (DECLARATOR);
463 DCL_TYPE (head) = type;
466 /* if this is the first entry */
472 else if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
474 sym->etype = mergeSpec (sym->etype, head, sym->name);
476 else if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
479 while (t->next != sym->etype)
482 tail->next = sym->etype;
484 else if (IS_FUNC (sym->type) && IS_SPEC (sym->type->next) &&
485 !memcmp(sym->type->next, empty, sizeof(sym_link)))
487 sym->type->next = head;
492 sym->etype->next = head;
496 /* if the type is an unknown pointer and has
497 a tspec then take the storage class const & volatile
498 attribute from the tspec & make it those of this
502 //DCL_TYPE (p) == UPOINTER &&
505 if (!IS_SPEC (sym->etype))
507 sym->etype = sym->etype->next = newLink (SPECIFIER);
509 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
510 DCL_TSPEC (p) = NULL;
513 // if there is a function in this type chain
514 if (p && funcInChain(sym->type)) {
515 processFuncArgs (sym);
521 /*------------------------------------------------------------------
522 checkTypeSanity: prevent the user from doing e.g.:
524 ------------------------------------------------------------------*/
525 void checkTypeSanity(sym_link *etype, char *name) {
529 if (getenv("DEBUG_SANITY")) {
530 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
535 if (!IS_SPEC(etype)) {
536 if (getenv("DEBUG_SANITY")) {
537 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
542 noun=nounName(etype);
544 if (getenv("DEBUG_SANITY")) {
545 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
548 if ((SPEC_NOUN(etype)==V_CHAR ||
549 SPEC_NOUN(etype)==V_FLOAT ||
550 SPEC_NOUN(etype)==V_FIXED16X16 ||
551 SPEC_NOUN(etype)==V_DOUBLE ||
552 SPEC_NOUN(etype)==V_VOID) &&
553 (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
554 // long or short for char float double or void
555 werror (E_LONG_OR_SHORT_INVALID, noun, name);
557 if ((SPEC_NOUN(etype)==V_FLOAT ||
558 SPEC_NOUN(etype)==V_FIXED16X16 ||
559 SPEC_NOUN(etype)==V_DOUBLE ||
560 SPEC_NOUN(etype)==V_VOID) &&
561 (etype->select.s.b_signed || SPEC_USIGN(etype))) {
562 // signed or unsigned for float double or void
563 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
566 // special case for "short"
567 if (SPEC_SHORT(etype)) {
568 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
569 SPEC_SHORT(etype) = 0;
573 "const a;" or "data b;" or "signed s" or "long l"
575 if (!SPEC_NOUN(etype)) {
576 SPEC_NOUN(etype)=V_INT;
579 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
580 /* a "plain" int bitfield is unsigned */
581 if (SPEC_NOUN(etype)==V_BIT ||
582 SPEC_NOUN(etype)==V_SBIT) {
583 if (!etype->select.s.b_signed)
584 SPEC_USIGN(etype) = 1;
587 if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
588 // signed AND unsigned
589 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
591 if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
593 werror (E_LONG_AND_SHORT_INVALID, noun, name);
598 /*------------------------------------------------------------------*/
599 /* mergeSpec - merges two specifiers and returns the new one */
600 /*------------------------------------------------------------------*/
602 mergeSpec (sym_link * dest, sym_link * src, char *name)
604 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
606 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
609 werror (E_SYNTAX_ERROR, yytext);
610 // the show must go on
615 if (SPEC_NOUN(src)) {
616 if (!SPEC_NOUN(dest)) {
617 SPEC_NOUN(dest)=SPEC_NOUN(src);
619 /* we shouldn't redeclare the type */
620 if (getenv("DEBUG_SANITY")) {
621 fprintf (stderr, "mergeSpec: ");
623 werror(E_TWO_OR_MORE_DATA_TYPES, name);
627 if (SPEC_SCLS(src)) {
628 /* if destination has no storage class */
629 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
630 SPEC_SCLS (dest) = SPEC_SCLS (src);
632 if (getenv("DEBUG_SANITY")) {
633 fprintf (stderr, "mergeSpec: ");
635 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
639 /* copy all the specifications */
641 // we really should do:
643 if (SPEC_what(src)) {
644 if (SPEC_what(dest)) {
645 werror(W_DUPLICATE_SPEC, "what");
647 SPEC_what(dst)|=SPEC_what(src);
650 // but there are more important thing right now
652 SPEC_LONG (dest) |= SPEC_LONG (src);
653 SPEC_SHORT(dest) |= SPEC_SHORT(src);
654 SPEC_USIGN (dest) |= SPEC_USIGN (src);
655 dest->select.s.b_signed|=src->select.s.b_signed;
656 SPEC_STAT (dest) |= SPEC_STAT (src);
657 SPEC_EXTR (dest) |= SPEC_EXTR (src);
658 SPEC_INLINE (dest) |= SPEC_INLINE (src);
659 SPEC_CONST(dest) |= SPEC_CONST (src);
660 SPEC_ABSA (dest) |= SPEC_ABSA (src);
661 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
662 SPEC_RESTRICT (dest) |= SPEC_RESTRICT (src);
663 SPEC_ADDR (dest) |= SPEC_ADDR (src);
664 SPEC_OCLS (dest) = SPEC_OCLS (src);
665 SPEC_BLEN (dest) |= SPEC_BLEN (src);
666 SPEC_BSTR (dest) |= SPEC_BSTR (src);
667 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
668 SPEC_ENUM (dest) |= SPEC_ENUM (src);
669 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
670 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
672 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
673 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
675 /* these are the only function attributes that will be set
676 in a specifier while parsing */
677 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
678 FUNC_BANKED(dest) |= FUNC_BANKED(src);
679 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
680 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
681 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
682 FUNC_ISISR(dest) |= FUNC_ISISR(src);
683 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
684 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
685 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
686 FUNC_INTNO(dest) |= FUNC_INTNO(src);
687 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
688 FUNC_ISINLINE (dest) |= FUNC_ISINLINE (src);
693 /*------------------------------------------------------------------*/
694 /* genSymName - generates and returns a name used for anonymous vars */
695 /*------------------------------------------------------------------*/
697 genSymName (int level)
699 static int gCount = 0;
700 static char gname[SDCC_NAME_MAX + 1];
702 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
706 /*------------------------------------------------------------------*/
707 /* getSpec - returns the specifier part from a declaration chain */
708 /*------------------------------------------------------------------*/
710 getSpec (sym_link * p)
715 while (p && !(IS_SPEC (p)))
721 /*------------------------------------------------------------------*/
722 /* newCharLink() - creates an char type */
723 /*------------------------------------------------------------------*/
729 p = newLink (SPECIFIER);
730 SPEC_NOUN (p) = V_CHAR;
735 /*------------------------------------------------------------------*/
736 /* newFloatLink - a new Float type */
737 /*------------------------------------------------------------------*/
743 p = newLink (SPECIFIER);
744 SPEC_NOUN (p) = V_FLOAT;
749 /*------------------------------------------------------------------*/
750 /* newFixed16x16Link - a new Float type */
751 /*------------------------------------------------------------------*/
757 p = newLink (SPECIFIER);
758 SPEC_NOUN (p) = V_FIXED16X16;
763 /*------------------------------------------------------------------*/
764 /* newLongLink() - new long type */
765 /*------------------------------------------------------------------*/
771 p = newLink (SPECIFIER);
772 SPEC_NOUN (p) = V_INT;
778 /*------------------------------------------------------------------*/
779 /* newIntLink() - creates an int type */
780 /*------------------------------------------------------------------*/
786 p = newLink (SPECIFIER);
787 SPEC_NOUN (p) = V_INT;
792 /*------------------------------------------------------------------*/
793 /* newBoolLink() - creates an bool type */
794 /*------------------------------------------------------------------*/
800 p = newLink (SPECIFIER);
801 SPEC_NOUN (p) = V_BIT;
806 /*------------------------------------------------------------------*/
807 /* getSize - returns size of a type chain in bytes */
808 /*------------------------------------------------------------------*/
810 getSize (sym_link * p)
812 /* if nothing return 0 */
816 { /* if this is the specifier then */
817 switch (SPEC_NOUN (p))
818 { /* depending on the specifier type */
820 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
830 return SPEC_STRUCT (p)->size;
837 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
843 /* this is a declarator */
844 switch (DCL_TYPE (p))
848 return DCL_ELEM (p) * getSize (p->next);
850 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
851 // "can not tell the size of an array[]");
862 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
871 /*------------------------------------------------------------------*/
872 /* checkStructFlexArray - check tree behind a struct */
873 /*------------------------------------------------------------------*/
875 checkStructFlexArray (symbol *sym, sym_link *p)
877 /* if nothing return FALSE */
883 /* (nested) struct with flexible array member? */
884 if (IS_STRUCT (p) && SPEC_STRUCT (p)->b_flexArrayMember)
886 werror (W_INVALID_FLEXARRAY);
892 /* this is a declarator */
895 /* flexible array member? */
898 if (!options.std_c99)
899 werror (W_C89_NO_FLEXARRAY);
903 return checkStructFlexArray (sym, p->next);
908 /*------------------------------------------------------------------*/
909 /* bitsForType - returns # of bits required to store this type */
910 /*------------------------------------------------------------------*/
912 bitsForType (sym_link * p)
914 /* if nothing return 0 */
919 { /* if this is the specifier then */
921 switch (SPEC_NOUN (p))
922 { /* depending on the specifier type */
924 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
926 return FLOATSIZE * 8;
934 return SPEC_STRUCT (p)->size * 8;
941 return SPEC_BLEN (p);
947 /* this is a specifier */
948 switch (DCL_TYPE (p))
951 return DCL_ELEM (p) * getSize (p->next) * 8;
955 return (PTRSIZE * 8);
960 return (FPTRSIZE * 8);
962 return (GPTRSIZE * 8);
969 /*------------------------------------------------------------------*/
970 /* copySymbolChain - copies a symbol chain */
971 /*------------------------------------------------------------------*/
973 copySymbolChain (symbol * src)
980 dest = copySymbol (src);
981 dest->next = copySymbolChain (src->next);
985 /*------------------------------------------------------------------*/
986 /* copySymbol - makes a copy of a symbol */
987 /*------------------------------------------------------------------*/
989 copySymbol (symbol * src)
996 dest = newSymbol (src->name, src->level);
997 memcpy (dest, src, sizeof (symbol));
998 dest->level = src->level;
999 dest->block = src->block;
1000 dest->ival = copyIlist (src->ival);
1001 dest->type = copyLinkChain (src->type);
1002 dest->etype = getSpec (dest->type);
1004 dest->key = src->key;
1005 dest->allocreq = src->allocreq;
1009 /*------------------------------------------------------------------*/
1010 /* reverseSyms - reverses the links for a symbol chain */
1011 /*------------------------------------------------------------------*/
1013 reverseSyms (symbol * sym)
1015 symbol *prev, *curr, *next;
1030 sym->next = (void *) NULL;
1034 /*------------------------------------------------------------------*/
1035 /* reverseLink - reverses the links for a type chain */
1036 /*------------------------------------------------------------------*/
1038 reverseLink (sym_link * type)
1040 sym_link *prev, *curr, *next;
1055 type->next = (void *) NULL;
1059 /*------------------------------------------------------------------*/
1060 /* addSymChain - adds a symbol chain to the symboltable */
1061 /*------------------------------------------------------------------*/
1063 addSymChain (symbol ** symHead)
1065 symbol *sym = *symHead;
1066 symbol *csym = NULL;
1070 for (; sym != NULL; sym = sym->next)
1072 changePointer(sym->type);
1073 checkTypeSanity(sym->etype, sym->name);
1075 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1079 /* if this is an array without any dimension
1080 then update the dimension from the initial value */
1081 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1082 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1085 /* if already exists in the symbol table
1086 then check if the type match;
1087 if yes then if at least one is not extern
1088 set the new symbol to non extern */
1089 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1090 csym->level == sym->level)
1092 /* if not formal parameter and not in file scope
1093 then show symbol redefined error
1094 else check if symbols have conpatible types */
1095 if (!sym->_isparm && sym->level > 0)
1099 /* If the previous definition was for an array with incomplete */
1100 /* type, and the new definition has completed the type, update */
1101 /* the original type to match */
1102 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1103 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1105 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1106 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1110 /* If only one of the definitions used the "at" keyword, copy */
1111 /* the address to the other. */
1112 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1113 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1115 SPEC_ABSA (sym->etype) = 1;
1116 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1118 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1119 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1121 SPEC_ABSA (csym->etype) = 1;
1122 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1127 if (csym->ival && sym->ival)
1129 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1135 /* one definition extern ? */
1136 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1137 werror (E_EXTERN_MISMATCH, sym->name);
1139 werror (E_DUPLICATE, sym->name);
1140 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1142 fprintf (stderr, "from type '");
1143 printTypeChain (csym->type, stderr);
1144 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1145 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1146 fprintf (stderr, "'\nto type '");
1147 printTypeChain (sym->type, stderr);
1148 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1149 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1150 fprintf (stderr, "'\n");
1155 if (csym->ival && !sym->ival)
1156 sym->ival = csym->ival;
1158 if (!sym->cdef && IS_EXTERN (sym->etype))
1160 /* set the new symbol to not extern if not a compiler defined function
1161 and at least one is non extern */
1162 SPEC_EXTR(sym->etype) = SPEC_EXTR(csym->etype);
1163 sym->cdef = csym->cdef;
1166 /* delete current entry */
1167 deleteSym (SymbolTab, csym, csym->name);
1168 deleteFromSeg(csym);
1170 symPtrPtr = symHead;
1171 while (*symPtrPtr && *symPtrPtr != csym)
1172 symPtrPtr = &(*symPtrPtr)->next;
1173 if (*symPtrPtr == csym)
1174 *symPtrPtr = csym->next;
1178 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1183 /*------------------------------------------------------------------*/
1184 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1185 /*------------------------------------------------------------------*/
1187 funcInChain (sym_link * lnk)
1198 /*------------------------------------------------------------------*/
1199 /* structElemType - returns the type info of a struct member */
1200 /*------------------------------------------------------------------*/
1202 structElemType (sym_link * stype, value * id)
1204 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1205 sym_link *type, *etype;
1206 sym_link *petype = getSpec (stype);
1210 /* look for the id */
1213 if (strcmp (fields->rname, id->name) == 0)
1215 type = copyLinkChain (fields->type);
1216 etype = getSpec (type);
1217 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1218 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1220 SPEC_CONST (type) |= SPEC_CONST (stype);
1222 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1225 fields = fields->next;
1229 werror (E_NOT_MEMBER, id->name);
1231 // the show must go on
1232 return newIntLink();
1235 /*------------------------------------------------------------------*/
1236 /* getStructElement - returns element of a tructure definition */
1237 /*------------------------------------------------------------------*/
1239 getStructElement (structdef * sdef, symbol * sym)
1243 for (field = sdef->fields; field; field = field->next)
1244 if (strcmp (field->name, sym->name) == 0)
1247 werror (E_NOT_MEMBER, sym->name);
1249 return sdef->fields;
1252 /*------------------------------------------------------------------*/
1253 /* compStructSize - computes the size of a structure */
1254 /*------------------------------------------------------------------*/
1256 compStructSize (int su, structdef * sdef)
1258 int sum = 0, usum = 0;
1262 /* for the identifiers */
1263 loop = sdef->fields;
1266 /* create the internal name for this variable */
1267 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1272 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1274 /* if this is a bit field */
1277 /* change it to a unsigned bit */
1278 SPEC_NOUN (loop->etype) = V_BITFIELD;
1279 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1280 /* a "plain" int bitfield is unsigned */
1281 if (!loop->etype->select.s.b_signed)
1282 SPEC_USIGN(loop->etype) = 1;
1284 SPEC_BLEN (loop->etype) = loop->bitVar;
1286 if (loop->bitVar == BITVAR_PAD) {
1287 /* A zero length bitfield forces padding */
1288 SPEC_BSTR (loop->etype) = bitOffset;
1289 SPEC_BLEN (loop->etype) = 0;
1294 if (bitOffset == 8) {
1298 /* check if this fit into the remaining */
1299 /* bits of this byte else align it to the */
1300 /* next byte boundary */
1301 if (loop->bitVar <= (8 - bitOffset)) {
1302 /* fits into current byte */
1304 SPEC_BSTR (loop->etype) = bitOffset;
1305 bitOffset += loop->bitVar;
1307 else if (!bitOffset) {
1308 /* does not fit, but is already byte aligned */
1310 SPEC_BSTR (loop->etype) = bitOffset;
1311 bitOffset += loop->bitVar;
1314 if( TARGET_IS_PIC16 && getenv("PIC16_PACKED_BITFIELDS") ) {
1315 /* if PIC16 && enviroment variable is set, then
1316 * tightly pack bitfields, this means that when a
1317 * bitfield goes beyond byte alignment, do not
1318 * automatically start allocatint from next byte,
1319 * but also use the available bits first */
1320 fprintf(stderr, ": packing bitfields in structures\n");
1321 SPEC_BSTR (loop->etype) = bitOffset;
1322 bitOffset += loop->bitVar;
1323 loop->offset = (su == UNION ? sum = 0 : sum);
1325 /* does not fit; need to realign first */
1327 loop->offset = (su == UNION ? sum = 0 : sum);
1329 SPEC_BSTR (loop->etype) = bitOffset;
1330 bitOffset += loop->bitVar;
1333 while (bitOffset>8) {
1340 /* This is a non-bit field. Make sure we are */
1341 /* byte aligned first */
1344 loop->offset = (su == UNION ? sum = 0 : sum);
1348 checkDecl (loop, 1);
1349 sum += getSize (loop->type);
1351 /* search for "flexibel array members" */
1352 /* and do some syntax checks */
1354 && checkStructFlexArray (loop, loop->type))
1356 /* found a "flexible array member" */
1357 sdef->b_flexArrayMember = TRUE;
1358 /* is another struct-member following? */
1360 werror (E_FLEXARRAY_NOTATEND);
1361 /* is it the first struct-member? */
1362 else if (loop == sdef->fields)
1363 werror (E_FLEXARRAY_INEMPTYSTRCT);
1369 /* if union then size = sizeof largest field */
1371 /* For UNION, round up after each field */
1372 sum += ((bitOffset+7)/8);
1373 usum = max (usum, sum);
1378 /* For STRUCT, round up after all fields processed */
1380 sum += ((bitOffset+7)/8);
1382 return (su == UNION ? usum : sum);
1385 /*-------------------------------------------------------------------*/
1386 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1387 /* an enclosing struct/union */
1388 /*-------------------------------------------------------------------*/
1390 promoteAnonStructs (int su, structdef * sdef)
1399 tofield = &sdef->fields;
1400 field = sdef->fields;
1403 nextfield = field->next;
1404 if (!*field->name && IS_STRUCT (field->type))
1406 /* Found an anonymous struct/union. Replace it */
1407 /* with the fields it contains and adjust all */
1410 base = field->offset;
1411 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1413 continue; /* just in case it's empty */
1415 *tofield = subfield;
1418 /* check for field name conflicts resulting from promotion */
1419 dupfield = sdef->fields;
1420 while (dupfield && dupfield != subfield)
1422 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1424 werrorfl (subfield->fileDef, subfield->lineDef,
1426 su==STRUCT ? "struct" : "union",
1428 werrorfl (dupfield->fileDef, dupfield->lineDef,
1431 dupfield = dupfield->next;
1434 subfield->offset += base;
1436 subfield = subfield->next;
1440 subfield->next = nextfield;
1441 tofield = &subfield->next;
1444 tofield = &field->next;
1450 /*------------------------------------------------------------------*/
1451 /* checkSClass - check the storage class specification */
1452 /*------------------------------------------------------------------*/
1454 checkSClass (symbol * sym, int isProto)
1458 if (getenv("DEBUG_SANITY")) {
1459 fprintf (stderr, "checkSClass: %s \n", sym->name);
1462 /* type is literal can happen for enums change to auto */
1463 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1464 SPEC_SCLS (sym->etype) = S_AUTO;
1466 /* if sfr or sbit then must also be volatile */
1467 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1468 SPEC_SCLS (sym->etype) == S_SFR)
1470 SPEC_VOLATILE (sym->etype) = 1;
1473 /* make sure restrict is only used with pointers */
1474 if (SPEC_RESTRICT (sym->etype))
1476 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1477 SPEC_RESTRICT (sym->etype) = 0;
1482 if (IS_DECL (t) && DCL_PTR_RESTRICT (t) && !IS_PTR (t))
1484 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1485 DCL_PTR_RESTRICT (t) = 0;
1491 /* if absolute address given then it mark it as
1492 volatile -- except in the PIC port */
1494 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1495 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1496 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1499 if (IS_ABSOLUTE (sym->etype))
1500 SPEC_VOLATILE (sym->etype) = 1;
1502 if (TARGET_IS_MCS51 &&
1503 IS_ABSOLUTE (sym->etype) &&
1504 SPEC_SCLS (sym->etype) == S_SFR)
1509 if (SPEC_NOUN (sym->etype) == V_CHAR)
1511 else if (SPEC_LONG (sym->etype) == 0)
1516 addr = SPEC_ADDR (sym->etype);
1517 for (n=0; n<size; n+=8)
1518 if (((addr >> n) & 0xFF) < 0x80)
1519 werror (W_SFR_ABSRANGE, sym->name);
1522 /* If code memory is read only, then pointers to code memory */
1523 /* implicitly point to constants -- make this explicit */
1525 while (t && t->next) {
1526 if (IS_CODEPTR(t) && port->mem.code_ro) {
1527 if (IS_SPEC(t->next)) {
1528 SPEC_CONST (t->next) = 1;
1530 DCL_PTR_CONST (t->next) = 1;
1536 /* global variables declared const put into code */
1537 /* if no other storage class specified */
1538 if (sym->level == 0 &&
1539 SPEC_SCLS(sym->etype) == S_FIXED &&
1540 !IS_FUNC(sym->type)) {
1541 /* find the first non-array link */
1545 if (IS_CONSTANT (t)) {
1546 SPEC_SCLS (sym->etype) = S_CODE;
1550 /* global variable in code space is a constant */
1551 if (sym->level == 0 &&
1552 SPEC_SCLS (sym->etype) == S_CODE &&
1553 port->mem.code_ro) {
1554 /* find the first non-array link */
1561 DCL_PTR_CONST (t) = 1;
1565 /* if bit variable then no storage class can be */
1566 /* specified since bit is already a storage */
1567 if (IS_BITVAR (sym->etype) &&
1568 (SPEC_SCLS (sym->etype) != S_FIXED &&
1569 SPEC_SCLS (sym->etype) != S_SBIT &&
1570 SPEC_SCLS (sym->etype) != S_BIT)
1573 werror (E_BITVAR_STORAGE, sym->name);
1574 SPEC_SCLS (sym->etype) = S_FIXED;
1577 /* extern variables cannot be initialized */
1578 if (IS_EXTERN (sym->etype) && sym->ival)
1580 werror (E_EXTERN_INIT, sym->name);
1584 /* if this is an automatic symbol */
1585 if (sym->level && (options.stackAuto || reentrant)) {
1586 if (SPEC_SCLS (sym->etype) != S_BIT) {
1587 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1588 SPEC_SCLS (sym->etype) == S_FIXED ||
1589 SPEC_SCLS (sym->etype) == S_REGISTER ||
1590 SPEC_SCLS (sym->etype) == S_STACK ||
1591 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1592 SPEC_SCLS (sym->etype) = S_AUTO;
1594 /* storage class may only be specified for statics */
1595 if (!IS_STATIC(sym->etype)) {
1596 werror (E_AUTO_ASSUMED, sym->name);
1602 /* automatic symbols cannot be given */
1603 /* an absolute address ignore it */
1604 if (sym->level && !IS_STATIC(sym->etype) &&
1605 SPEC_ABSA (sym->etype) &&
1606 (options.stackAuto || reentrant))
1608 werror (E_AUTO_ABSA, sym->name);
1609 SPEC_ABSA (sym->etype) = 0;
1612 /* arrays & pointers cannot be defined for bits */
1613 /* SBITS or SFRs or BIT */
1614 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1615 !IS_FUNCPTR (sym->type) &&
1616 (SPEC_NOUN (sym->etype) == V_BIT ||
1617 SPEC_NOUN (sym->etype) == V_SBIT ||
1618 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1619 SPEC_SCLS (sym->etype) == S_SFR))
1620 werror (E_BIT_ARRAY, sym->name);
1622 /* if this is a bit|sbit then set length & start */
1623 if (SPEC_NOUN (sym->etype) == V_BIT ||
1624 SPEC_NOUN (sym->etype) == V_SBIT)
1626 SPEC_BLEN (sym->etype) = 1;
1627 SPEC_BSTR (sym->etype) = 0;
1631 /* variables declared in CODE space must have */
1632 /* initializers if not an extern */
1633 if (SPEC_SCLS (sym->etype) == S_CODE &&
1634 sym->ival == NULL &&
1637 port->mem.code_ro &&
1638 !IS_EXTERN (sym->etype) &&
1639 !SPEC_ABSA (sym->etype) &&
1640 !funcInChain (sym->type))
1641 werror (E_CODE_NO_INIT, sym->name);
1644 /* if parameter or local variable then change */
1645 /* the storage class to reflect where the var will go */
1646 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1647 && !IS_STATIC(sym->etype)
1650 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1652 SPEC_SCLS (sym->etype) = (options.useXstack ?
1653 S_XSTACK : S_STACK);
1657 /* hack-o-matic! I see no reason why the useXstack option should ever
1658 * control this allocation, but the code was originally that way, and
1659 * changing it for non-390 ports breaks the compiler badly.
1661 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1662 1 : options.useXstack;
1663 SPEC_SCLS (sym->etype) = (useXdata ?
1669 /*------------------------------------------------------------------*/
1670 /* changePointer - change pointer to functions */
1671 /*------------------------------------------------------------------*/
1673 changePointer (sym_link * p)
1676 /* go thru the chain of declarations */
1677 /* if we find a pointer to a function */
1678 /* change it to a ptr to code area */
1679 /* unless the function is banked. */
1680 for (; p; p = p->next)
1682 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1683 DCL_TYPE (p) = port->unqualified_pointer;
1684 if (IS_PTR (p) && IS_FUNC (p->next))
1685 if (!IFFUNC_BANKED(p->next))
1686 DCL_TYPE (p) = CPOINTER;
1690 /*------------------------------------------------------------------*/
1691 /* checkDecl - does semantic validation of a declaration */
1692 /*------------------------------------------------------------------*/
1694 checkDecl (symbol * sym, int isProto)
1697 checkSClass (sym, isProto); /* check the storage class */
1698 changePointer (sym->type); /* change pointers if required */
1700 /* if this is an array without any dimension
1701 then update the dimension from the initial value */
1702 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1703 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1708 /*------------------------------------------------------------------*/
1709 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1710 /*------------------------------------------------------------------*/
1712 copyLinkChain (sym_link * p)
1714 sym_link *head, *curr, *loop;
1716 /* note: v_struct and v_struct->fields are not copied! */
1718 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1721 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1722 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1730 /*------------------------------------------------------------------*/
1731 /* cleanUpBlock - cleansup the symbol table specified for all the */
1732 /* symbols in the given block */
1733 /*------------------------------------------------------------------*/
1735 cleanUpBlock (bucket ** table, int block)
1740 /* go thru the entire table */
1741 for (i = 0; i < 256; i++)
1743 for (chain = table[i]; chain; chain = chain->next)
1745 if (chain->block >= block)
1747 deleteSym (table, chain->sym, chain->name);
1753 /*------------------------------------------------------------------*/
1754 /* cleanUpLevel - cleansup the symbol table specified for all the */
1755 /* symbols in the given level */
1756 /*------------------------------------------------------------------*/
1758 cleanUpLevel (bucket ** table, int level)
1763 /* go thru the entire table */
1764 for (i = 0; i < 256; i++)
1766 for (chain = table[i]; chain; chain = chain->next)
1768 if (chain->level >= level)
1770 deleteSym (table, chain->sym, chain->name);
1776 /*------------------------------------------------------------------*/
1777 /* computeTypeOr - computes the resultant type from two types */
1778 /*------------------------------------------------------------------*/
1780 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1783 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1784 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1786 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1788 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1792 if (SPEC_USIGN (etype1))
1794 if ( IS_LITERAL (etype2)
1795 && floatFromVal (valFromType (etype2)) >= 0)
1796 SPEC_USIGN (reType) = 1;
1799 /* promote to int */
1800 SPEC_USIGN (reType) = 0;
1801 SPEC_NOUN (reType) = V_INT;
1804 else /* etype1 signed */
1806 if ( IS_LITERAL (etype2)
1807 && floatFromVal (valFromType (etype2)) <= 127)
1808 SPEC_USIGN (reType) = 0;
1811 /* promote to int */
1812 SPEC_USIGN (reType) = 0;
1813 SPEC_NOUN (reType) = V_INT;
1817 if (SPEC_USIGN (etype2))
1819 if ( IS_LITERAL (etype1)
1820 && floatFromVal (valFromType (etype1)) >= 0)
1821 SPEC_USIGN (reType) = 1;
1824 /* promote to int */
1825 SPEC_USIGN (reType) = 0;
1826 SPEC_NOUN (reType) = V_INT;
1829 else /* etype2 signed */
1831 if ( IS_LITERAL (etype1)
1832 && floatFromVal (valFromType (etype1)) <= 127)
1833 SPEC_USIGN (reType) = 0;
1836 /* promote to int */
1837 SPEC_USIGN (reType) = 0;
1838 SPEC_NOUN (reType) = V_INT;
1844 /*------------------------------------------------------------------*/
1845 /* computeType - computes the resultant type from two types */
1846 /*------------------------------------------------------------------*/
1848 computeType (sym_link * type1, sym_link * type2,
1849 RESULT_TYPE resultType, int op)
1853 sym_link *etype1 = getSpec (type1);
1856 etype2 = type2 ? getSpec (type2) : type1;
1858 /* if one of them is a float then result is a float */
1859 /* here we assume that the types passed are okay */
1860 /* and can be cast to one another */
1861 /* which ever is greater in size */
1862 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1863 rType = newFloatLink ();
1864 /* if both are fixed16x16 then result is float */
1865 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1866 rType = newFixed16x16Link();
1867 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1868 rType = newFloatLink ();
1869 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1870 rType = newFloatLink ();
1872 /* if both are bitvars choose the larger one */
1873 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1874 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1875 copyLinkChain (type1) : copyLinkChain (type1);
1877 /* if only one of them is a bit variable then the other one prevails */
1878 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1880 rType = copyLinkChain (type2);
1881 /* bitfield can have up to 16 bits */
1882 if (getSize (etype1) > 1)
1883 SPEC_NOUN (getSpec (rType)) = V_INT;
1885 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1887 rType = copyLinkChain (type1);
1888 /* bitfield can have up to 16 bits */
1889 if (getSize (etype2) > 1)
1890 SPEC_NOUN (getSpec (rType)) = V_INT;
1892 /* if one of them is a pointer or array then that
1894 else if (IS_PTR (type1) || IS_ARRAY (type1))
1895 rType = copyLinkChain (type1);
1896 else if (IS_PTR (type2) || IS_ARRAY (type2))
1897 rType = copyLinkChain (type2);
1898 else if (getSize (type1) > getSize (type2))
1899 rType = copyLinkChain (type1);
1901 rType = copyLinkChain (type2);
1903 reType = getSpec (rType);
1905 /* avoid conflicting types */
1906 reType->select.s.b_signed = 0;
1908 /* if result is a literal then make not so */
1909 if (IS_LITERAL (reType))
1910 SPEC_SCLS (reType) = S_REGISTER;
1914 case RESULT_TYPE_IFX:
1918 case RESULT_TYPE_BIT:
1921 SPEC_NOUN (reType) = V_BIT;
1925 case RESULT_TYPE_CHAR:
1926 if (IS_BITVAR (reType))
1928 SPEC_NOUN (reType) = V_CHAR;
1929 SPEC_SCLS (reType) = 0;
1930 SPEC_USIGN (reType) = 0;
1934 case RESULT_TYPE_INT:
1935 case RESULT_TYPE_NONE:
1936 case RESULT_TYPE_OTHER:
1937 if (IS_BIT (reType))
1939 SPEC_NOUN (reType) = V_CHAR;
1940 SPEC_SCLS (reType) = 0;
1941 SPEC_USIGN (reType) = 0;
1944 else if (IS_BITFIELD (reType))
1946 /* could be smarter, but it depends on the op */
1947 /* this is for the worst case: a multiplication of 4 * 4 bit */
1948 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1949 SPEC_SCLS (reType) = 0;
1950 SPEC_USIGN (reType) = 0;
1953 else if (IS_CHAR (reType))
1955 /* promotion of some special cases */
1960 return computeTypeOr (etype1, etype2, reType);
1962 if (SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1964 SPEC_USIGN (reType) = 1;
1969 SPEC_NOUN (reType) = V_INT;
1970 SPEC_USIGN (reType) = 0;
1973 /* if both are unsigned char then no promotion required */
1974 if (!(SPEC_USIGN (etype1) && SPEC_USIGN (etype2)))
1976 SPEC_NOUN (reType) = V_INT;
1977 SPEC_USIGN (reType) = 0;
1990 /* SDCC's sign promotion:
1991 - if one or both operands are unsigned, the resultant type will be unsigned
1992 (except char, see below)
1993 - if an operand is promoted to a larger type (char -> int, int -> long),
1994 the larger type will be signed
1996 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1997 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1998 the standard. The standard demands, that the result has to be the same
1999 "as if" the promotion would have been performed:
2001 - if the result of an operation with two char's is promoted to a
2002 larger type, the result will be signed.
2004 More sophisticated are these:
2005 - if the result of an operation with two char's is a char again,
2006 the result will only then be unsigned, if both operands are
2007 unsigned. In all other cases the result will be signed.
2009 This seems to be contradictionary to the first two rules, but it makes
2010 real sense (all types are char's):
2012 A signed char can be negative; this must be preserved in the result
2015 Only if both operands are unsigned it's safe to make the result
2016 unsigned; this helps to avoid overflow:
2019 - ToDo: document '|', '^' and '&'
2021 Homework: - why is (200 * 200 < 0) true?
2022 - why is { char l = 200, r = 200; (r * l > 0) } true?
2025 if (!IS_FLOAT (reType)
2026 && ( (SPEC_USIGN (etype1)
2027 /* if this operand is promoted to a larger type,
2028 then it will be promoted to a signed type */
2029 && !(bitsForType (etype1) < bitsForType (reType))
2030 /* char require special handling */
2031 && !IS_CHAR (etype1))
2032 || /* same for 2nd operand */
2033 (SPEC_USIGN (etype2)
2034 && !(bitsForType (etype2) < bitsForType (reType))
2035 && !IS_CHAR (etype2))
2036 || /* if both are 'unsigned char' and not promoted
2037 let the result be unsigned too */
2038 ( SPEC_USIGN (etype1)
2039 && SPEC_USIGN (etype2)
2042 && IS_CHAR (reType))))
2043 SPEC_USIGN (reType) = 1;
2045 SPEC_USIGN (reType) = 0;
2050 /*--------------------------------------------------------------------*/
2051 /* compareType - will do type check return 1 if match, -1 if castable */
2052 /*--------------------------------------------------------------------*/
2054 compareType (sym_link * dest, sym_link * src)
2065 /* if dest is a declarator then */
2070 /* banked function pointer */
2071 if (IS_GENPTR (dest) && IS_GENPTR (src))
2073 if (IS_FUNC (src->next) && IS_VOID(dest->next))
2075 if (IS_FUNC (dest->next) && IS_VOID(src->next))
2077 return compareType (dest->next, src->next);
2080 if (DCL_TYPE (src) == DCL_TYPE (dest))
2084 //checkFunction(src,dest);
2086 return compareType (dest->next, src->next);
2088 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next))
2093 (IS_GENPTR (dest) ||
2094 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2097 if (IS_PTR (dest) && IS_ARRAY (src))
2099 value *val=aggregateToPointer (valFromType(src));
2100 int res=compareType (dest, val->type);
2101 Safe_free(val->type);
2105 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2106 return compareType (dest->next, src);
2109 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2115 /* if one is a specifier and the other is not */
2116 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2117 (IS_SPEC (dest) && !IS_SPEC (src)))
2120 /* if one of them is a void then ok */
2121 if (SPEC_NOUN (dest) == V_VOID &&
2122 SPEC_NOUN (src) != V_VOID)
2125 if (SPEC_NOUN (dest) != V_VOID &&
2126 SPEC_NOUN (src) == V_VOID)
2129 /* if they are both bitfields then if the lengths
2130 and starts don't match */
2131 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2132 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2133 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2136 /* it is a specifier */
2137 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2139 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2140 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2142 bitsForType (dest) == bitsForType (src))
2143 instead of the next two lines, but the regression tests fail with
2144 them; I guess it's a problem with replaceCheaperOp */
2145 getSize (dest) == getSize (src) &&
2146 (IS_BIT (dest) == IS_BIT (src)))
2148 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2153 else if (IS_STRUCT (dest))
2155 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2160 if (SPEC_LONG (dest) != SPEC_LONG (src))
2163 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2169 /*--------------------------------------------------------------------*/
2170 /* compareTypeExact - will do type check return 1 if match exactly */
2171 /*--------------------------------------------------------------------*/
2173 compareTypeExact (sym_link * dest, sym_link * src, int level)
2175 STORAGE_CLASS srcScls, destScls;
2186 /* if dest is a declarator then */
2191 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2192 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2194 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2196 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2200 value *exargs, *acargs, *checkValue;
2202 /* verify function return type */
2203 if (!compareTypeExact (dest->next, src->next, -1))
2205 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2207 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2209 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2212 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2216 /* compare expected args with actual args */
2217 exargs = FUNC_ARGS(dest);
2218 acargs = FUNC_ARGS(src);
2220 /* for all the expected args do */
2221 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2223 //checkTypeSanity(acargs->etype, acargs->name);
2225 if (IS_AGGREGATE (acargs->type))
2227 checkValue = copyValue (acargs);
2228 aggregateToPointer (checkValue);
2231 checkValue = acargs;
2234 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2239 /* if one them ended we have a problem */
2240 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2241 (!exargs && acargs && !IS_VOID (acargs->type)))
2245 return compareTypeExact (dest->next, src->next, level);
2252 /* if one is a specifier and the other is not */
2253 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2254 (IS_SPEC (dest) && !IS_SPEC (src)))
2257 /* if one of them is a void then ok */
2258 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2261 /* if they are both bitfields then if the lengths
2262 and starts don't match */
2263 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2264 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2265 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2268 if (IS_INTEGRAL (dest))
2270 /* signedness must match */
2271 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2273 /* size must match */
2274 if (SPEC_LONG (dest) != SPEC_LONG (src))
2276 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2280 if (IS_STRUCT (dest))
2282 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2286 if (SPEC_CONST (dest) != SPEC_CONST (src))
2288 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2290 if (SPEC_STAT (dest) != SPEC_STAT (src))
2292 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2294 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2297 destScls = SPEC_SCLS (dest);
2298 srcScls = SPEC_SCLS (src);
2300 /* Compensate for const to const code change in checkSClass() */
2301 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2303 if (srcScls == S_CODE && destScls == S_FIXED)
2305 if (destScls == S_CODE && srcScls == S_FIXED)
2309 /* compensate for allocGlobal() */
2310 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2311 && port->mem.default_globl_map == xdata
2315 if (level>0 && !SPEC_STAT (dest))
2317 /* Compensate for hack-o-matic in checkSClass() */
2318 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2320 if (destScls == S_FIXED)
2321 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2322 if (srcScls == S_FIXED)
2323 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2325 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2327 if (destScls == S_FIXED)
2329 if (srcScls == S_FIXED)
2334 if (srcScls != destScls)
2337 printf ("level = %d\n", level);
2338 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2339 SPEC_SCLS (src), SPEC_SCLS (dest));
2340 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2348 /*------------------------------------------------------------------*/
2349 /* inCalleeSaveList - return 1 if found in callee save list */
2350 /*------------------------------------------------------------------*/
2352 calleeCmp(void *p1, void *p2)
2354 return (strcmp((char *)p1, (char *)(p2)) == 0);
2358 inCalleeSaveList(char *s)
2360 if (options.all_callee_saves)
2362 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2365 /*-----------------------------------------------------------------*/
2366 /* aggregateToPointer: change an aggregate type function */
2367 /* argument to a pointer to that type. */
2368 /*-----------------------------------------------------------------*/
2370 aggregateToPointer (value * val)
2372 if (IS_AGGREGATE (val->type))
2374 /* if this is a structure */
2375 /* then we need to add a new link */
2376 if (IS_STRUCT (val->type))
2378 /* first lets add DECLARATOR type */
2379 sym_link *p = val->type;
2381 werror (W_STRUCT_AS_ARG, val->name);
2382 val->type = newLink (DECLARATOR);
2383 val->type->next = p;
2386 /* change to a pointer depending on the */
2387 /* storage class specified */
2388 switch (SPEC_SCLS (val->etype))
2391 DCL_TYPE (val->type) = IPOINTER;
2394 DCL_TYPE (val->type) = PPOINTER;
2397 if (SPEC_OCLS(val->etype)) {
2398 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2400 // this happens for (external) function parameters
2401 DCL_TYPE (val->type) = port->unqualified_pointer;
2407 DCL_TYPE (val->type) = POINTER;
2410 DCL_TYPE (val->type) = CPOINTER;
2413 DCL_TYPE (val->type) = FPOINTER;
2416 DCL_TYPE (val->type) = EEPPOINTER;
2419 DCL_TYPE (val->type) = port->unqualified_pointer;
2422 /* is there is a symbol associated then */
2423 /* change the type of the symbol as well */
2426 val->sym->type = copyLinkChain (val->type);
2427 val->sym->etype = getSpec (val->sym->type);
2432 /*------------------------------------------------------------------*/
2433 /* checkFunction - does all kinds of check on a function */
2434 /*------------------------------------------------------------------*/
2436 checkFunction (symbol * sym, symbol *csym)
2438 value *exargs, *acargs;
2442 if (getenv("DEBUG_SANITY")) {
2443 fprintf (stderr, "checkFunction: %s ", sym->name);
2446 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2448 werror(E_SYNTAX_ERROR, sym->name);
2452 /* move inline specifier from return type to function attributes */
2453 if (IS_INLINE (sym->etype))
2455 SPEC_INLINE (sym->etype) = 0;
2456 FUNC_ISINLINE (sym->type) = 1;
2459 /* make sure the type is complete and sane */
2460 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2462 /* if not type then some kind of error */
2466 /* if the function has no type then make it return int */
2467 if (!sym->type->next)
2468 sym->type->next = sym->etype = newIntLink ();
2470 /* function cannot return aggregate */
2471 if (IS_AGGREGATE (sym->type->next))
2473 werror (E_FUNC_AGGR, sym->name);
2477 /* check if this function is defined as calleeSaves
2478 then mark it as such */
2479 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2481 /* if interrupt service routine */
2482 /* then it cannot have arguments */
2483 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2485 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2486 werror (E_INT_ARGS, sym->name);
2487 FUNC_ARGS(sym->type)=NULL;
2491 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2493 werror (E_SHADOWREGS_NO_ISR, sym->name);
2496 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2498 acargs=acargs->next, argCnt++) {
2500 // this can happen for reentrant functions
2501 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2502 // the show must go on: synthesize a name and symbol
2503 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2504 acargs->sym = newSymbol (acargs->name, 1);
2505 SPEC_OCLS (acargs->etype) = istack;
2506 acargs->sym->type = copyLinkChain (acargs->type);
2507 acargs->sym->etype = getSpec (acargs->sym->type);
2508 acargs->sym->_isparm = 1;
2509 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2510 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2512 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2517 /*JCF: Mark the register bank as used*/
2518 RegBankUsed[FUNC_REGBANK (sym->type)] = 1;
2520 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2521 return 1; /* not defined nothing more to check */
2523 /* check if body already present */
2524 if (csym && IFFUNC_HASBODY(csym->type))
2526 werror (E_FUNC_BODY, sym->name);
2530 /* check the return value type */
2531 if (compareType (csym->type, sym->type) <= 0)
2533 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2534 printFromToType(csym->type, sym->type);
2538 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2540 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2543 /* I don't think this is necessary for interrupts. An isr is a */
2544 /* root in the calling tree. */
2545 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2546 (!FUNC_ISISR (sym->type)))
2548 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2551 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2553 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2556 /* Really, reentrant should match regardless of argCnt, but */
2557 /* this breaks some existing code (the fp lib functions). If */
2558 /* the first argument is always passed the same way, this */
2559 /* lax checking is ok (but may not be true for in future ports) */
2560 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2563 //printf("argCnt = %d\n",argCnt);
2564 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2567 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2569 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2572 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2574 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2578 /* compare expected args with actual args */
2579 exargs = FUNC_ARGS(csym->type);
2580 acargs = FUNC_ARGS(sym->type);
2582 /* for all the expected args do */
2585 exargs = exargs->next, acargs = acargs->next, argCnt++)
2587 if (getenv("DEBUG_SANITY")) {
2588 fprintf (stderr, "checkFunction: %s ", exargs->name);
2590 /* make sure the type is complete and sane */
2591 checkTypeSanity(exargs->etype, exargs->name);
2593 /* If the actual argument is an array, any prototype
2594 * will have modified it to a pointer. Duplicate that
2597 if (IS_AGGREGATE (acargs->type))
2599 checkValue = copyValue (acargs);
2600 aggregateToPointer (checkValue);
2604 checkValue = acargs;
2607 if (compareType (exargs->type, checkValue->type) <= 0)
2609 werror (E_ARG_TYPE, argCnt);
2610 printFromToType(exargs->type, checkValue->type);
2615 /* if one them ended we have a problem */
2616 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2617 (!exargs && acargs && !IS_VOID (acargs->type)))
2618 werror (E_ARG_COUNT);
2620 /* replace with this defition */
2621 sym->cdef = csym->cdef;
2622 deleteSym (SymbolTab, csym, csym->name);
2623 deleteFromSeg(csym);
2624 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2625 if (IS_EXTERN (csym->etype) && !
2626 IS_EXTERN (sym->etype))
2628 addSet (&publics, sym);
2633 /*------------------------------------------------------------------*/
2634 /* cdbStructBlock - calls struct printing for a blcks */
2635 /*------------------------------------------------------------------*/
2636 void cdbStructBlock (int block)
2639 bucket **table = StructTab;
2642 /* go thru the entire table */
2643 for (i = 0; i < 256; i++)
2645 for (chain = table[i]; chain; chain = chain->next)
2647 if (chain->block >= block)
2650 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2656 /*-----------------------------------------------------------------*/
2657 /* processFuncPtrArgs - does some processing with args of func ptrs*/
2658 /*-----------------------------------------------------------------*/
2660 processFuncPtrArgs (sym_link * funcType)
2662 value *val = FUNC_ARGS(funcType);
2664 /* if it is void then remove parameters */
2665 if (val && IS_VOID (val->type))
2667 FUNC_ARGS(funcType) = NULL;
2672 /*-----------------------------------------------------------------*/
2673 /* processFuncArgs - does some processing with function args */
2674 /*-----------------------------------------------------------------*/
2676 processFuncArgs (symbol * func)
2680 sym_link *funcType=func->type;
2682 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2683 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2685 /* find the function declaration within the type */
2686 while (funcType && !IS_FUNC(funcType))
2687 funcType=funcType->next;
2689 /* if this function has variable argument list */
2690 /* then make the function a reentrant one */
2691 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2692 FUNC_ISREENT(funcType)=1;
2694 /* check if this function is defined as calleeSaves
2695 then mark it as such */
2696 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2698 /* loop thru all the arguments */
2699 val = FUNC_ARGS(funcType);
2701 /* if it is void then remove parameters */
2702 if (val && IS_VOID (val->type))
2704 FUNC_ARGS(funcType) = NULL;
2708 /* reset regparm for the port */
2709 (*port->reset_regparms) ();
2711 /* if any of the arguments is an aggregate */
2712 /* change it to pointer to the same type */
2716 char buffer[SDCC_NAME_MAX+1];
2718 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2719 checkTypeSanity (val->etype, buffer);
2721 /* mark it as a register parameter if
2722 the function does not have VA_ARG
2723 and as port dictates */
2724 if (!IFFUNC_HASVARARGS(funcType) &&
2725 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2727 SPEC_REGPARM (val->etype) = 1;
2728 SPEC_ARGREG(val->etype) = argreg;
2729 } else if (IFFUNC_ISREENT(funcType)) {
2730 FUNC_HASSTACKPARM(funcType) = 1;
2733 if (IS_AGGREGATE (val->type))
2735 aggregateToPointer (val);
2742 /* if this is an internal generated function call */
2744 /* ignore --stack-auto for this one, we don't know how it is compiled */
2745 /* simply trust on --int-long-reent or --float-reent */
2746 if (IFFUNC_ISREENT(funcType)) {
2750 /* if this function is reentrant or */
2751 /* automatics r 2b stacked then nothing */
2752 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2756 val = FUNC_ARGS(funcType);
2761 /* if a symbolname is not given */
2762 /* synthesize a variable name */
2765 SNPRINTF (val->name, sizeof(val->name),
2766 "_%s_PARM_%d", func->name, pNum++);
2767 val->sym = newSymbol (val->name, 1);
2768 if (SPEC_SCLS(val->etype) == S_BIT)
2769 SPEC_OCLS (val->etype) = bit;
2771 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2772 val->sym->type = copyLinkChain (val->type);
2773 val->sym->etype = getSpec (val->sym->type);
2774 val->sym->_isparm = 1;
2775 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2777 /* ?? static functions shouldn't imply static parameters - EEP */
2778 if (IS_SPEC(func->etype)) {
2779 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2780 SPEC_STAT (func->etype);
2783 addSymChain (&val->sym);
2786 else /* symbol name given create synth name */
2789 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2790 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2791 val->sym->_isparm = 1;
2792 if (SPEC_SCLS(val->etype) == S_BIT)
2793 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2795 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2796 port->mem.default_local_map;
2799 /* ?? static functions shouldn't imply static parameters - EEP */
2800 if (IS_SPEC(func->etype)) {
2801 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2802 SPEC_STAT (func->etype);
2806 if (SPEC_OCLS (val->sym->etype) == pdata)
2807 val->sym->iaccess = 1;
2808 if (!isinSet(operKeyReset, val->sym)) {
2809 addSet (&operKeyReset, val->sym);
2810 applyToSet (operKeyReset, resetParmKey);
2816 /*-----------------------------------------------------------------*/
2817 /* isSymbolEqual - compares two symbols return 1 if they match */
2818 /*-----------------------------------------------------------------*/
2820 isSymbolEqual (symbol * dest, symbol * src)
2822 /* if pointers match then equal */
2826 /* if one of them is null then don't match */
2830 /* if both of them have rname match on rname */
2831 if (dest->rname[0] && src->rname[0])
2832 return (!strcmp (dest->rname, src->rname));
2834 /* otherwise match on name */
2835 return (!strcmp (dest->name, src->name));
2838 void PT(sym_link *type)
2840 printTypeChain(type,0);
2842 /*-----------------------------------------------------------------*/
2843 /* printTypeChain - prints the type chain in human readable form */
2844 /*-----------------------------------------------------------------*/
2846 printTypeChain (sym_link * start, FILE * of)
2857 dbuf_init (&dbuf, 1024);
2858 dbuf_printTypeChain (start, &dbuf);
2859 dbuf_write_and_destroy (&dbuf, of);
2866 dbuf_printTypeChain (sym_link * start, struct dbuf_s *dbuf)
2869 sym_link * type, * search;
2873 dbuf_append_str (dbuf, "void");
2877 /* Print the chain as it is written in the source: */
2878 /* start with the last entry. */
2879 /* However, the storage class at the end of the */
2880 /* chain reall applies to the first in the chain! */
2882 for (type = start; type && type->next; type = type->next)
2885 scls=SPEC_SCLS(type);
2893 case S_DATA: dbuf_append_str (dbuf, "data-"); break;
2894 case S_XDATA: dbuf_append_str (dbuf, "xdata-"); break;
2895 case S_SFR: dbuf_append_str (dbuf, "sfr-"); break;
2896 case S_SBIT: dbuf_append_str (dbuf, "sbit-"); break;
2897 case S_CODE: dbuf_append_str (dbuf, "code-"); break;
2898 case S_IDATA: dbuf_append_str (dbuf, "idata-"); break;
2899 case S_PDATA: dbuf_append_str (dbuf, "pdata-"); break;
2900 case S_LITERAL: dbuf_append_str (dbuf, "literal-"); break;
2901 case S_STACK: dbuf_append_str (dbuf, "stack-"); break;
2902 case S_XSTACK: dbuf_append_str (dbuf, "xstack-"); break;
2903 case S_BIT: dbuf_append_str (dbuf, "bit-"); break;
2904 case S_EEPROM: dbuf_append_str (dbuf, "eeprom-"); break;
2911 if (!IS_FUNC(type)) {
2912 if (DCL_PTR_VOLATILE (type)) {
2913 dbuf_append_str (dbuf, "volatile-");
2915 if (DCL_PTR_CONST (type)) {
2916 dbuf_append_str (dbuf, "const-");
2918 if (DCL_PTR_RESTRICT (type)) {
2919 dbuf_append_str (dbuf, "restrict-");
2922 switch (DCL_TYPE (type))
2925 dbuf_printf (dbuf, "function %s %s",
2926 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2927 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2928 dbuf_append_str (dbuf, "( ");
2929 for (args = FUNC_ARGS(type);
2932 dbuf_printTypeChain(args->type, dbuf);
2934 dbuf_append_str (dbuf, ", ");
2936 dbuf_append_str (dbuf, ") ");
2939 dbuf_append_str (dbuf, "generic* ");
2942 dbuf_append_str (dbuf, "code* ");
2945 dbuf_append_str (dbuf, "xdata* ");
2948 dbuf_append_str (dbuf, "eeprom* ");
2951 dbuf_append_str (dbuf, "near* ");
2954 dbuf_append_str (dbuf, "idata* ");
2957 dbuf_append_str (dbuf, "pdata* ");
2960 dbuf_append_str (dbuf, "unknown* ");
2963 if (DCL_ELEM(type)) {
2964 dbuf_printf (dbuf, "[%d] ", DCL_ELEM(type));
2966 dbuf_append_str (dbuf, "[] ");
2973 if (SPEC_VOLATILE (type))
2974 dbuf_append_str (dbuf, "volatile-");
2975 if (SPEC_CONST (type))
2976 dbuf_append_str (dbuf, "const-");
2977 if (SPEC_USIGN (type))
2978 dbuf_append_str (dbuf, "unsigned-");
2979 switch (SPEC_NOUN (type))
2983 dbuf_append_str (dbuf, "long-");
2984 dbuf_append_str (dbuf, "int");
2988 dbuf_append_str (dbuf, "char");
2992 dbuf_append_str (dbuf, "void");
2996 dbuf_append_str (dbuf, "float");
3000 dbuf_append_str (dbuf, "fixed16x16");
3004 dbuf_printf (dbuf, "struct %s", SPEC_STRUCT (type)->tag);
3008 dbuf_append_str (dbuf, "sbit");
3012 dbuf_append_str (dbuf, "bit");
3016 dbuf_printf (dbuf, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3020 dbuf_append_str (dbuf, "double");
3024 dbuf_append_str (dbuf, "unknown type");
3028 /* search entry in list before "type" */
3029 for (search = start; search && search->next != type;)
3030 search = search->next;
3033 dbuf_append_char(dbuf, ' ');
3037 /*--------------------------------------------------------------------*/
3038 /* printTypeChainRaw - prints the type chain in human readable form */
3039 /* in the raw data structure ordering */
3040 /*--------------------------------------------------------------------*/
3042 printTypeChainRaw (sym_link * start, FILE * of)
3055 fprintf (of, "void");
3065 if (!IS_FUNC(type)) {
3066 if (DCL_PTR_VOLATILE (type)) {
3067 fprintf (of, "volatile-");
3069 if (DCL_PTR_CONST (type)) {
3070 fprintf (of, "const-");
3072 if (DCL_PTR_RESTRICT (type)) {
3073 fprintf (of, "restrict-");
3076 switch (DCL_TYPE (type))
3079 if (IFFUNC_ISINLINE(type)) {
3080 fprintf (of, "inline-");
3082 fprintf (of, "function %s %s",
3083 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
3084 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
3086 for (args = FUNC_ARGS(type);
3089 printTypeChain(args->type, of);
3096 fprintf (of, "generic* ");
3099 fprintf (of, "code* ");
3102 fprintf (of, "xdata* ");
3105 fprintf (of, "eeprom* ");
3108 fprintf (of, "near* ");
3111 fprintf (of, "idata* ");
3114 fprintf (of, "pdata* ");
3117 fprintf (of, "unknown* ");
3120 if (DCL_ELEM(type)) {
3121 fprintf (of, "[%d] ", DCL_ELEM(type));
3123 fprintf (of, "[] ");
3127 if (DCL_TSPEC(type))
3130 printTypeChainRaw(DCL_TSPEC(type), of);
3134 else if (IS_SPEC (type))
3136 switch (SPEC_SCLS (type))
3138 case S_DATA: fprintf (of, "data-"); break;
3139 case S_XDATA: fprintf (of, "xdata-"); break;
3140 case S_SFR: fprintf (of, "sfr-"); break;
3141 case S_SBIT: fprintf (of, "sbit-"); break;
3142 case S_CODE: fprintf (of, "code-"); break;
3143 case S_IDATA: fprintf (of, "idata-"); break;
3144 case S_PDATA: fprintf (of, "pdata-"); break;
3145 case S_LITERAL: fprintf (of, "literal-"); break;
3146 case S_STACK: fprintf (of, "stack-"); break;
3147 case S_XSTACK: fprintf (of, "xstack-"); break;
3148 case S_BIT: fprintf (of, "bit-"); break;
3149 case S_EEPROM: fprintf (of, "eeprom-"); break;
3152 if (SPEC_VOLATILE (type))
3153 fprintf (of, "volatile-");
3154 if (SPEC_CONST (type))
3155 fprintf (of, "const-");
3156 if (SPEC_USIGN (type))
3157 fprintf (of, "unsigned-");
3158 switch (SPEC_NOUN (type))
3162 fprintf (of, "long-");
3163 fprintf (of, "int");
3167 fprintf (of, "char");
3171 fprintf (of, "void");
3175 fprintf (of, "float");
3179 fprintf (of, "fixed16x16");
3183 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3187 fprintf (of, "sbit");
3191 fprintf (of, "bit");
3195 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3199 fprintf (of, "double");
3203 fprintf (of, "unknown type");
3208 fprintf (of, "NOT_SPEC_OR_DECL");
3218 /*-----------------------------------------------------------------*/
3219 /* powof2 - returns power of two for the number if number is pow 2 */
3220 /*-----------------------------------------------------------------*/
3222 powof2 (TYPE_TARGET_ULONG num)
3235 if (n1s > 1 || nshifts == 0)
3251 symbol *__fps16x16_add;
3252 symbol *__fps16x16_sub;
3253 symbol *__fps16x16_mul;
3254 symbol *__fps16x16_div;
3255 symbol *__fps16x16_eq;
3256 symbol *__fps16x16_neq;
3257 symbol *__fps16x16_lt;
3258 symbol *__fps16x16_lteq;
3259 symbol *__fps16x16_gt;
3260 symbol *__fps16x16_gteq;
3262 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3263 symbol *__muldiv[3][3][2];
3264 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3265 sym_link *__multypes[3][2];
3266 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3267 symbol *__conv[2][3][2];
3268 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3269 symbol *__fp16x16conv[2][4][2];
3270 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3271 symbol *__rlrr[2][3][2];
3273 sym_link *floatType;
3274 sym_link *fixed16x16Type;
3277 _mangleFunctionName(char *in)
3279 if (port->getMangledFunctionName)
3281 return port->getMangledFunctionName(in);
3289 /*-----------------------------------------------------------------*/
3290 /* typeFromStr - create a typechain from an encoded string */
3291 /* basic types - 'c' - char */
3296 /* 'q' - fixed16x16 */
3298 /* '*' - pointer - default (GPOINTER) */
3299 /* modifiers - 'u' - unsigned */
3300 /* pointer modifiers - 'g' - generic */
3304 /* 'F' - function */
3305 /* examples : "ig*" - generic int * */
3306 /* "cx*" - char xdata * */
3307 /* "ui" - unsigned int */
3308 /*-----------------------------------------------------------------*/
3309 sym_link *typeFromStr (char *s)
3311 sym_link *r = newLink(DECLARATOR);
3323 r->class = SPECIFIER;
3324 SPEC_NOUN(r) = V_CHAR;
3328 r->class = SPECIFIER;
3329 SPEC_NOUN(r) = V_INT;
3332 r->class = SPECIFIER;
3333 SPEC_NOUN(r) = V_INT;
3337 r->class = SPECIFIER;
3338 SPEC_NOUN(r) = V_FLOAT;
3341 r->class = SPECIFIER;
3342 SPEC_NOUN(r) = V_FIXED16X16;
3345 r->class = SPECIFIER;
3346 SPEC_NOUN(r) = V_VOID;
3349 DCL_TYPE(r) = port->unqualified_pointer;
3356 assert(*(s+1)=='*');
3357 nr = newLink(DECLARATOR);
3362 DCL_TYPE(r) = GPOINTER;
3365 DCL_TYPE(r) = FPOINTER;
3368 DCL_TYPE(r) = CPOINTER;
3371 DCL_TYPE(r) = POINTER;
3374 DCL_TYPE(r) = FUNCTION;
3375 nr = newLink(DECLARATOR);
3378 DCL_TYPE(r) = CPOINTER;
3384 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3385 "typeFromStr: unknown type");
3388 if (IS_SPEC(r) && usign) {
3397 /*-----------------------------------------------------------------*/
3398 /* initCSupport - create functions for C support routines */
3399 /*-----------------------------------------------------------------*/
3403 const char *smuldivmod[] =
3407 const char *sbwd[] =
3409 "char", "int", "long", "fixed16x16",
3411 const char *fp16x16sbwd[] =
3413 "char", "int", "long", "float",
3419 const char *srlrr[] =
3424 int bwd, su, muldivmod, tofrom, rlrr;
3426 if (getenv("SDCC_NO_C_SUPPORT")) {
3427 /* for debugging only */
3431 floatType = newFloatLink ();
3432 fixed16x16Type = newFixed16x16Link ();
3434 for (bwd = 0; bwd < 3; bwd++)
3451 __multypes[bwd][0] = l;
3452 __multypes[bwd][1] = copyLinkChain (l);
3453 SPEC_USIGN (__multypes[bwd][1]) = 1;
3456 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3457 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3458 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3459 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3460 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3461 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3462 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3463 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3464 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3465 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3467 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3468 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3469 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3470 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3471 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3472 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3473 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3474 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3475 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3476 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3479 for (tofrom = 0; tofrom < 2; tofrom++)
3481 for (bwd = 0; bwd < 3; bwd++)
3483 for (su = 0; su < 2; su++)
3487 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3488 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3492 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3493 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3499 for (tofrom = 0; tofrom < 2; tofrom++)
3501 for (bwd = 0; bwd < 4; bwd++)
3503 for (su = 0; su < 2; su++)
3507 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3509 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3511 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3515 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3517 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3519 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3526 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3528 for (bwd = 0; bwd < 3; bwd++)
3530 for (su = 0; su < 2; su++)
3532 SNPRINTF (buffer, sizeof(buffer),
3534 smuldivmod[muldivmod],
3537 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3538 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3543 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3544 Therefore they've been merged into mulint() and mullong().
3547 for (bwd = 0; bwd < 3; bwd++)
3549 for (su = 0; su < 2; su++)
3551 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3553 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
3554 if (!TARGET_IS_PIC16 || muldivmod != 1 || bwd != 0 || su != 0)
3556 SNPRINTF (buffer, sizeof(buffer),
3558 smuldivmod[muldivmod],
3561 __muldiv[muldivmod][bwd][su] = funcOfType (
3562 _mangleFunctionName(buffer),
3563 __multypes[bwd][su],
3564 __multypes[bwd][su],
3566 options.intlong_rent);
3567 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3573 if (TARGET_IS_PIC16)
3575 /* PIC16 port wants __divschar/__modschar to return an int, so that both
3576 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
3577 * (first one would have to be sign extended, second one must not be).
3578 * Similarly, modschar should be handled, but the iCode introduces cast
3579 * here and forces '% : s8 x s8 -> s8' ... */
3581 for (muldivmod = 1; muldivmod < 2; muldivmod++) {
3582 SNPRINTF (buffer, sizeof(buffer),
3584 smuldivmod[muldivmod],
3587 __muldiv[muldivmod][bwd][su] = funcOfType (
3588 _mangleFunctionName(buffer),
3590 __multypes[bwd][su],
3592 options.intlong_rent);
3593 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3601 for (su = 0; su < 2; su++)
3603 /* muluchar and mulschar are still separate functions, because e.g. the z80
3604 port is sign/zero-extending to int before calling mulint() */
3605 SNPRINTF (buffer, sizeof(buffer),
3607 smuldivmod[muldivmod],
3610 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3611 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3615 /* word and doubleword */
3616 for (bwd = 1; bwd < 3; bwd++)
3619 SNPRINTF (buffer, sizeof(buffer),
3621 smuldivmod[muldivmod],
3623 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3624 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3625 /* signed = unsigned */
3626 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3629 for (rlrr = 0; rlrr < 2; rlrr++)
3631 for (bwd = 0; bwd < 3; bwd++)
3633 for (su = 0; su < 2; su++)
3635 SNPRINTF (buffer, sizeof(buffer),
3640 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3641 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3647 /*-----------------------------------------------------------------*/
3648 /* initBuiltIns - create prototypes for builtin functions */
3649 /*-----------------------------------------------------------------*/
3655 if (!port->builtintable) return ;
3657 for (i = 0 ; port->builtintable[i].name ; i++) {
3658 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3659 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3660 FUNC_ISBUILTIN(sym->type) = 1;
3661 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3665 sym_link *validateLink(sym_link *l,
3672 if (l && l->class==select)
3677 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3678 " expected %s, got %s\n",
3679 macro, args, file, line,
3680 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3682 return l; // never reached, makes compiler happy.
3685 /*--------------------------------------------------------------------*/
3686 /* newEnumType - create an integer type compatible with enumerations */
3687 /*--------------------------------------------------------------------*/
3689 newEnumType (symbol *enumlist)
3697 type = newLink (SPECIFIER);
3698 SPEC_NOUN (type) = V_INT;
3702 /* Determine the range of the enumerated values */
3704 min = max = (int) ulFromVal (valFromType (sym->type));
3705 for (sym = sym->next; sym; sym = sym->next)
3707 v = (int) ulFromVal (valFromType (sym->type));
3714 /* Determine the smallest integer type that is compatible with this range */
3715 type = newLink (SPECIFIER);
3716 if (min>=0 && max<=255)
3718 SPEC_NOUN (type) = V_CHAR;
3719 SPEC_USIGN (type) = 1;
3721 else if (min>=-128 && max<=127)
3723 SPEC_NOUN (type) = V_CHAR;
3725 else if (min>=0 && max<=65535)
3727 SPEC_NOUN (type) = V_INT;
3728 SPEC_USIGN (type) = 1;
3730 else if (min>=-32768 && max<=32767)
3732 SPEC_NOUN (type) = V_INT;
3736 SPEC_NOUN (type) = V_INT;
3737 SPEC_LONG (type) = 1;
3739 SPEC_USIGN (type) = 1;