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 on the same level */
1086 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1087 csym->level == sym->level)
1089 /* if not formal parameter and not in file scope
1090 then show symbol redefined error
1091 else check if symbols have conpatible types */
1092 if (!sym->_isparm && sym->level > 0)
1096 /* If the previous definition was for an array with incomplete
1097 type, and the new definition has completed the type, update
1098 the original type to match */
1099 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1100 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1102 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1103 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1107 /* If only one of the definitions used the "at" keyword, copy */
1108 /* the address to the other. */
1109 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1110 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1112 SPEC_ABSA (sym->etype) = 1;
1113 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1115 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1116 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1118 SPEC_ABSA (csym->etype) = 1;
1119 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1124 if (csym->ival && sym->ival)
1126 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1132 /* one definition extern ? */
1133 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1134 werror (E_EXTERN_MISMATCH, sym->name);
1136 werror (E_DUPLICATE, sym->name);
1137 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1139 fprintf (stderr, "from type '");
1140 printTypeChain (csym->type, stderr);
1141 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1142 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1143 fprintf (stderr, "'\nto type '");
1144 printTypeChain (sym->type, stderr);
1145 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1146 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1147 fprintf (stderr, "'\n");
1152 if (csym->ival && !sym->ival)
1153 sym->ival = csym->ival;
1155 if (!csym->cdef && !sym->cdef && IS_EXTERN (sym->etype))
1157 /* if none of symbols is a compiler defined function
1158 and at least one is not extern
1159 then set the new symbol to non extern */
1160 SPEC_EXTR(sym->etype) = SPEC_EXTR(csym->etype);
1163 /* delete current entry */
1164 deleteSym (SymbolTab, csym, csym->name);
1165 deleteFromSeg(csym);
1167 symPtrPtr = symHead;
1168 while (*symPtrPtr && *symPtrPtr != csym)
1169 symPtrPtr = &(*symPtrPtr)->next;
1170 if (*symPtrPtr == csym)
1171 *symPtrPtr = csym->next;
1175 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1180 /*------------------------------------------------------------------*/
1181 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1182 /*------------------------------------------------------------------*/
1184 funcInChain (sym_link * lnk)
1195 /*------------------------------------------------------------------*/
1196 /* structElemType - returns the type info of a struct member */
1197 /*------------------------------------------------------------------*/
1199 structElemType (sym_link * stype, value * id)
1201 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1202 sym_link *type, *etype;
1203 sym_link *petype = getSpec (stype);
1207 /* look for the id */
1210 if (strcmp (fields->rname, id->name) == 0)
1212 type = copyLinkChain (fields->type);
1213 etype = getSpec (type);
1214 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1215 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1216 SPEC_OCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1217 SPEC_OCLS (etype) : SPEC_OCLS (petype));
1219 SPEC_CONST (type) |= SPEC_CONST (stype);
1221 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1224 fields = fields->next;
1228 werror (E_NOT_MEMBER, id->name);
1230 // the show must go on
1231 return newIntLink();
1234 /*------------------------------------------------------------------*/
1235 /* getStructElement - returns element of a tructure definition */
1236 /*------------------------------------------------------------------*/
1238 getStructElement (structdef * sdef, symbol * sym)
1242 for (field = sdef->fields; field; field = field->next)
1243 if (strcmp (field->name, sym->name) == 0)
1246 werror (E_NOT_MEMBER, sym->name);
1248 return sdef->fields;
1251 /*------------------------------------------------------------------*/
1252 /* compStructSize - computes the size of a structure */
1253 /*------------------------------------------------------------------*/
1255 compStructSize (int su, structdef * sdef)
1257 int sum = 0, usum = 0;
1261 /* for the identifiers */
1262 loop = sdef->fields;
1265 /* create the internal name for this variable */
1266 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1271 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1273 /* if this is a bit field */
1276 /* change it to a unsigned bit */
1277 SPEC_NOUN (loop->etype) = V_BITFIELD;
1278 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1279 /* a "plain" int bitfield is unsigned */
1280 if (!loop->etype->select.s.b_signed)
1281 SPEC_USIGN(loop->etype) = 1;
1283 if (loop->bitVar == BITVAR_PAD) {
1284 /* A zero length bitfield forces padding */
1285 SPEC_BLEN (loop->etype) = 0;
1286 SPEC_BSTR (loop->etype) = bitOffset;
1288 bitOffset = 8; /* padding is not needed when at bit 0 */
1292 SPEC_BLEN (loop->etype) = loop->bitVar;
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 (type2);
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;
2051 comparePtrType (sym_link * dest, sym_link * src, bool bMustCast)
2055 if (IS_VOID (src->next) && IS_VOID (dest->next))
2056 return bMustCast ? -1 : 1;
2057 if ((IS_VOID (src->next) && !IS_VOID (dest->next)) ||
2058 (!IS_VOID (src->next) && IS_VOID (dest->next)) )
2060 res = compareType (dest->next, src->next);
2062 return bMustCast ? -1 : 1;
2069 /*--------------------------------------------------------------------*/
2070 /* compareType - will do type check return 1 if match, 0 if no match, */
2071 /* -1 if castable, -2 if only signedness differs */
2072 /*--------------------------------------------------------------------*/
2074 compareType (sym_link * dest, sym_link * src)
2085 /* if dest is a declarator then */
2090 /* banked function pointer */
2091 if (IS_GENPTR (dest) && IS_GENPTR (src))
2093 if (IS_FUNC (src->next) && IS_VOID(dest->next))
2095 if (IS_FUNC (dest->next) && IS_VOID(src->next))
2097 return comparePtrType(dest, src, FALSE);
2100 if (DCL_TYPE (src) == DCL_TYPE (dest))
2104 //checkFunction(src,dest);
2106 return comparePtrType(dest, src, FALSE);
2108 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next))
2113 (IS_GENPTR (dest) ||
2114 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2117 return comparePtrType(dest, src, TRUE);
2119 if (IS_PTR (dest) && IS_ARRAY (src))
2121 value *val=aggregateToPointer (valFromType(src));
2122 int res=compareType (dest, val->type);
2123 Safe_free(val->type);
2127 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2128 return compareType (dest->next, src);
2131 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2137 if (IS_PTR (src) && IS_VOID (dest))
2140 /* if one is a specifier and the other is not */
2141 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2142 (IS_SPEC (dest) && !IS_SPEC (src)))
2145 /* if one of them is a void then ok */
2146 if (SPEC_NOUN (dest) == V_VOID &&
2147 SPEC_NOUN (src) != V_VOID)
2150 if (SPEC_NOUN (dest) != V_VOID &&
2151 SPEC_NOUN (src) == V_VOID)
2154 /* if they are both bitfields then if the lengths
2155 and starts don't match */
2156 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2157 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2158 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2161 /* it is a specifier */
2162 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2164 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2165 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2167 bitsForType (dest) == bitsForType (src))
2168 instead of the next two lines, but the regression tests fail with
2169 them; I guess it's a problem with replaceCheaperOp */
2170 getSize (dest) == getSize (src) &&
2171 (IS_BIT (dest) == IS_BIT (src)))
2173 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2178 else if (IS_STRUCT (dest))
2180 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2185 if (SPEC_LONG (dest) != SPEC_LONG (src))
2188 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2194 /*--------------------------------------------------------------------*/
2195 /* compareTypeExact - will do type check return 1 if match exactly */
2196 /*--------------------------------------------------------------------*/
2198 compareTypeExact (sym_link * dest, sym_link * src, int level)
2200 STORAGE_CLASS srcScls, destScls;
2211 /* if dest is a declarator then */
2216 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2217 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2219 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2221 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2225 value *exargs, *acargs, *checkValue;
2227 /* verify function return type */
2228 if (!compareTypeExact (dest->next, src->next, -1))
2230 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2232 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2234 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2237 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2241 /* compare expected args with actual args */
2242 exargs = FUNC_ARGS(dest);
2243 acargs = FUNC_ARGS(src);
2245 /* for all the expected args do */
2246 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2248 //checkTypeSanity(acargs->etype, acargs->name);
2250 if (IS_AGGREGATE (acargs->type))
2252 checkValue = copyValue (acargs);
2253 aggregateToPointer (checkValue);
2256 checkValue = acargs;
2259 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2264 /* if one them ended we have a problem */
2265 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2266 (!exargs && acargs && !IS_VOID (acargs->type)))
2270 return compareTypeExact (dest->next, src->next, level);
2277 /* if one is a specifier and the other is not */
2278 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2279 (IS_SPEC (dest) && !IS_SPEC (src)))
2282 /* if one of them is a void then ok */
2283 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2286 /* if they are both bitfields then if the lengths
2287 and starts don't match */
2288 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2289 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2290 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2293 if (IS_INTEGRAL (dest))
2295 /* signedness must match */
2296 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2298 /* size must match */
2299 if (SPEC_LONG (dest) != SPEC_LONG (src))
2301 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2305 if (IS_STRUCT (dest))
2307 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2311 if (SPEC_CONST (dest) != SPEC_CONST (src))
2313 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2315 if (SPEC_STAT (dest) != SPEC_STAT (src))
2317 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2319 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2322 destScls = SPEC_SCLS (dest);
2323 srcScls = SPEC_SCLS (src);
2325 /* Compensate for const to const code change in checkSClass() */
2326 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2328 if (srcScls == S_CODE && destScls == S_FIXED)
2330 if (destScls == S_CODE && srcScls == S_FIXED)
2334 /* compensate for allocGlobal() */
2335 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2336 && port->mem.default_globl_map == xdata
2340 if (level>0 && !SPEC_STAT (dest))
2342 /* Compensate for hack-o-matic in checkSClass() */
2343 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2345 if (destScls == S_FIXED)
2346 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2347 if (srcScls == S_FIXED)
2348 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2350 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2352 if (destScls == S_FIXED)
2354 if (srcScls == S_FIXED)
2359 if (srcScls != destScls)
2362 printf ("level = %d\n", level);
2363 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2364 SPEC_SCLS (src), SPEC_SCLS (dest));
2365 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2373 /*------------------------------------------------------------------*/
2374 /* inCalleeSaveList - return 1 if found in callee save list */
2375 /*------------------------------------------------------------------*/
2377 calleeCmp(void *p1, void *p2)
2379 return (strcmp((char *)p1, (char *)(p2)) == 0);
2383 inCalleeSaveList(char *s)
2385 if (options.all_callee_saves)
2387 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2390 /*-----------------------------------------------------------------*/
2391 /* aggregateToPointer: change an aggregate type function */
2392 /* argument to a pointer to that type. */
2393 /*-----------------------------------------------------------------*/
2395 aggregateToPointer (value * val)
2397 if (IS_AGGREGATE (val->type))
2399 /* if this is a structure */
2400 /* then we need to add a new link */
2401 if (IS_STRUCT (val->type))
2403 /* first lets add DECLARATOR type */
2404 sym_link *p = val->type;
2406 werror (W_STRUCT_AS_ARG, val->name);
2407 val->type = newLink (DECLARATOR);
2408 val->type->next = p;
2411 /* change to a pointer depending on the */
2412 /* storage class specified */
2413 switch (SPEC_SCLS (val->etype))
2416 DCL_TYPE (val->type) = IPOINTER;
2419 DCL_TYPE (val->type) = PPOINTER;
2422 if (SPEC_OCLS(val->etype)) {
2423 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2425 // this happens for (external) function parameters
2426 DCL_TYPE (val->type) = port->unqualified_pointer;
2430 DCL_TYPE (val->type) = PTR_TYPE(SPEC_OCLS(val->etype));
2434 DCL_TYPE (val->type) = POINTER;
2437 DCL_TYPE (val->type) = CPOINTER;
2440 DCL_TYPE (val->type) = FPOINTER;
2443 DCL_TYPE (val->type) = EEPPOINTER;
2446 DCL_TYPE (val->type) = port->unqualified_pointer;
2449 /* is there is a symbol associated then */
2450 /* change the type of the symbol as well */
2453 val->sym->type = copyLinkChain (val->type);
2454 val->sym->etype = getSpec (val->sym->type);
2459 /*------------------------------------------------------------------*/
2460 /* checkFunction - does all kinds of check on a function */
2461 /*------------------------------------------------------------------*/
2463 checkFunction (symbol * sym, symbol *csym)
2465 value *exargs, *acargs;
2469 if (getenv("DEBUG_SANITY")) {
2470 fprintf (stderr, "checkFunction: %s ", sym->name);
2473 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2475 werror(E_SYNTAX_ERROR, sym->name);
2479 /* move inline specifier from return type to function attributes */
2480 if (IS_INLINE (sym->etype))
2482 SPEC_INLINE (sym->etype) = 0;
2483 FUNC_ISINLINE (sym->type) = 1;
2486 /* make sure the type is complete and sane */
2487 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2489 /* if not type then some kind of error */
2493 /* if the function has no type then make it return int */
2494 if (!sym->type->next)
2495 sym->type->next = sym->etype = newIntLink ();
2497 /* function cannot return aggregate */
2498 if (IS_AGGREGATE (sym->type->next))
2500 werror (E_FUNC_AGGR, sym->name);
2504 /* check if this function is defined as calleeSaves
2505 then mark it as such */
2506 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2508 /* if interrupt service routine */
2509 /* then it cannot have arguments */
2510 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2512 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2513 werror (E_INT_ARGS, sym->name);
2514 FUNC_ARGS(sym->type)=NULL;
2518 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2520 werror (E_SHADOWREGS_NO_ISR, sym->name);
2523 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2525 acargs=acargs->next, argCnt++) {
2527 // this can happen for reentrant functions
2528 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2529 // the show must go on: synthesize a name and symbol
2530 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2531 acargs->sym = newSymbol (acargs->name, 1);
2532 SPEC_OCLS (acargs->etype) = istack;
2533 acargs->sym->type = copyLinkChain (acargs->type);
2534 acargs->sym->etype = getSpec (acargs->sym->type);
2535 acargs->sym->_isparm = 1;
2536 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2537 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2539 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2544 /*JCF: Mark the register bank as used*/
2545 RegBankUsed[FUNC_REGBANK (sym->type)] = 1;
2547 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2548 return 1; /* not defined nothing more to check */
2550 /* check if body already present */
2551 if (csym && IFFUNC_HASBODY(csym->type))
2553 werror (E_FUNC_BODY, sym->name);
2557 /* check the return value type */
2558 if (compareType (csym->type, sym->type) <= 0)
2560 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2561 printFromToType(csym->type, sym->type);
2565 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2567 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2570 /* I don't think this is necessary for interrupts. An isr is a */
2571 /* root in the calling tree. */
2572 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2573 (!FUNC_ISISR (sym->type)))
2575 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2578 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2580 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2583 /* Really, reentrant should match regardless of argCnt, but */
2584 /* this breaks some existing code (the fp lib functions). If */
2585 /* the first argument is always passed the same way, this */
2586 /* lax checking is ok (but may not be true for in future ports) */
2587 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2590 //printf("argCnt = %d\n",argCnt);
2591 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2594 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2596 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2599 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2601 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2605 /* compare expected args with actual args */
2606 exargs = FUNC_ARGS(csym->type);
2607 acargs = FUNC_ARGS(sym->type);
2609 /* for all the expected args do */
2612 exargs = exargs->next, acargs = acargs->next, argCnt++)
2614 if (getenv("DEBUG_SANITY")) {
2615 fprintf (stderr, "checkFunction: %s ", exargs->name);
2617 /* make sure the type is complete and sane */
2618 checkTypeSanity(exargs->etype, exargs->name);
2620 /* If the actual argument is an array, any prototype
2621 * will have modified it to a pointer. Duplicate that
2624 if (IS_AGGREGATE (acargs->type))
2626 checkValue = copyValue (acargs);
2627 aggregateToPointer (checkValue);
2631 checkValue = acargs;
2634 if (compareType (exargs->type, checkValue->type) <= 0)
2636 werror (E_ARG_TYPE, argCnt);
2637 printFromToType(exargs->type, checkValue->type);
2642 /* if one them ended we have a problem */
2643 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2644 (!exargs && acargs && !IS_VOID (acargs->type)))
2645 werror (E_ARG_COUNT);
2647 /* replace with this defition */
2648 sym->cdef = csym->cdef;
2649 deleteSym (SymbolTab, csym, csym->name);
2650 deleteFromSeg(csym);
2651 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2652 if (IS_EXTERN (csym->etype) && !
2653 IS_EXTERN (sym->etype))
2655 addSet (&publics, sym);
2660 /*------------------------------------------------------------------*/
2661 /* cdbStructBlock - calls struct printing for a blcks */
2662 /*------------------------------------------------------------------*/
2663 void cdbStructBlock (int block)
2666 bucket **table = StructTab;
2669 /* go thru the entire table */
2670 for (i = 0; i < 256; i++)
2672 for (chain = table[i]; chain; chain = chain->next)
2674 if (chain->block >= block)
2677 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2683 /*-----------------------------------------------------------------*/
2684 /* processFuncPtrArgs - does some processing with args of func ptrs*/
2685 /*-----------------------------------------------------------------*/
2687 processFuncPtrArgs (sym_link * funcType)
2689 value *val = FUNC_ARGS(funcType);
2691 /* if it is void then remove parameters */
2692 if (val && IS_VOID (val->type))
2694 FUNC_ARGS(funcType) = NULL;
2699 /*-----------------------------------------------------------------*/
2700 /* processFuncArgs - does some processing with function args */
2701 /*-----------------------------------------------------------------*/
2703 processFuncArgs (symbol * func)
2707 sym_link *funcType=func->type;
2709 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2710 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2712 /* find the function declaration within the type */
2713 while (funcType && !IS_FUNC(funcType))
2714 funcType=funcType->next;
2716 /* if this function has variable argument list */
2717 /* then make the function a reentrant one */
2718 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2719 FUNC_ISREENT(funcType)=1;
2721 /* check if this function is defined as calleeSaves
2722 then mark it as such */
2723 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2725 /* loop thru all the arguments */
2726 val = FUNC_ARGS(funcType);
2728 /* if it is void then remove parameters */
2729 if (val && IS_VOID (val->type))
2731 FUNC_ARGS(funcType) = NULL;
2735 /* reset regparm for the port */
2736 (*port->reset_regparms) ();
2738 /* if any of the arguments is an aggregate */
2739 /* change it to pointer to the same type */
2743 char buffer[SDCC_NAME_MAX+1];
2745 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2746 checkTypeSanity (val->etype, buffer);
2748 /* mark it as a register parameter if
2749 the function does not have VA_ARG
2750 and as port dictates */
2751 if (!IFFUNC_HASVARARGS(funcType) &&
2752 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2754 SPEC_REGPARM (val->etype) = 1;
2755 SPEC_ARGREG(val->etype) = argreg;
2756 } else if (IFFUNC_ISREENT(funcType)) {
2757 FUNC_HASSTACKPARM(funcType) = 1;
2760 if (IS_AGGREGATE (val->type))
2762 aggregateToPointer (val);
2769 /* if this is an internal generated function call */
2771 /* ignore --stack-auto for this one, we don't know how it is compiled */
2772 /* simply trust on --int-long-reent or --float-reent */
2773 if (IFFUNC_ISREENT(funcType)) {
2777 /* if this function is reentrant or */
2778 /* automatics r 2b stacked then nothing */
2779 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2783 val = FUNC_ARGS(funcType);
2788 /* if a symbolname is not given */
2789 /* synthesize a variable name */
2792 SNPRINTF (val->name, sizeof(val->name),
2793 "_%s_PARM_%d", func->name, pNum++);
2794 val->sym = newSymbol (val->name, 1);
2795 if (SPEC_SCLS(val->etype) == S_BIT)
2796 SPEC_OCLS (val->etype) = bit;
2798 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2799 val->sym->type = copyLinkChain (val->type);
2800 val->sym->etype = getSpec (val->sym->type);
2801 val->sym->_isparm = 1;
2802 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2804 /* ?? static functions shouldn't imply static parameters - EEP */
2805 if (IS_SPEC(func->etype)) {
2806 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2807 SPEC_STAT (func->etype);
2810 addSymChain (&val->sym);
2813 else /* symbol name given create synth name */
2816 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2817 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2818 val->sym->_isparm = 1;
2819 if (SPEC_SCLS(val->etype) == S_BIT)
2820 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2822 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2823 port->mem.default_local_map;
2826 /* ?? static functions shouldn't imply static parameters - EEP */
2827 if (IS_SPEC(func->etype)) {
2828 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2829 SPEC_STAT (func->etype);
2833 if (SPEC_OCLS (val->sym->etype) == pdata)
2834 val->sym->iaccess = 1;
2835 if (!isinSet(operKeyReset, val->sym)) {
2836 addSet (&operKeyReset, val->sym);
2837 applyToSet (operKeyReset, resetParmKey);
2843 /*-----------------------------------------------------------------*/
2844 /* isSymbolEqual - compares two symbols return 1 if they match */
2845 /*-----------------------------------------------------------------*/
2847 isSymbolEqual (symbol * dest, symbol * src)
2849 /* if pointers match then equal */
2853 /* if one of them is null then don't match */
2857 /* if both of them have rname match on rname */
2858 if (dest->rname[0] && src->rname[0])
2859 return (!strcmp (dest->rname, src->rname));
2861 /* otherwise match on name */
2862 return (!strcmp (dest->name, src->name));
2865 void PT(sym_link *type)
2867 printTypeChain(type,0);
2869 /*-----------------------------------------------------------------*/
2870 /* printTypeChain - prints the type chain in human readable form */
2871 /*-----------------------------------------------------------------*/
2873 printTypeChain (sym_link * start, FILE * of)
2884 dbuf_init (&dbuf, 1024);
2885 dbuf_printTypeChain (start, &dbuf);
2886 dbuf_write_and_destroy (&dbuf, of);
2893 dbuf_printTypeChain (sym_link * start, struct dbuf_s *dbuf)
2896 sym_link * type, * search;
2900 dbuf_append_str (dbuf, "void");
2904 /* Print the chain as it is written in the source: */
2905 /* start with the last entry. */
2906 /* However, the storage class at the end of the */
2907 /* chain reall applies to the first in the chain! */
2909 for (type = start; type && type->next; type = type->next)
2912 scls=SPEC_SCLS(type);
2920 case S_DATA: dbuf_append_str (dbuf, "data-"); break;
2921 case S_XDATA: dbuf_append_str (dbuf, "xdata-"); break;
2922 case S_SFR: dbuf_append_str (dbuf, "sfr-"); break;
2923 case S_SBIT: dbuf_append_str (dbuf, "sbit-"); break;
2924 case S_CODE: dbuf_append_str (dbuf, "code-"); break;
2925 case S_IDATA: dbuf_append_str (dbuf, "idata-"); break;
2926 case S_PDATA: dbuf_append_str (dbuf, "pdata-"); break;
2927 case S_LITERAL: dbuf_append_str (dbuf, "literal-"); break;
2928 case S_STACK: dbuf_append_str (dbuf, "stack-"); break;
2929 case S_XSTACK: dbuf_append_str (dbuf, "xstack-"); break;
2930 case S_BIT: dbuf_append_str (dbuf, "bit-"); break;
2931 case S_EEPROM: dbuf_append_str (dbuf, "eeprom-"); break;
2938 if (!IS_FUNC(type)) {
2939 if (DCL_PTR_VOLATILE (type)) {
2940 dbuf_append_str (dbuf, "volatile-");
2942 if (DCL_PTR_CONST (type)) {
2943 dbuf_append_str (dbuf, "const-");
2945 if (DCL_PTR_RESTRICT (type)) {
2946 dbuf_append_str (dbuf, "restrict-");
2949 switch (DCL_TYPE (type))
2952 dbuf_printf (dbuf, "function %s %s",
2953 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2954 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2955 dbuf_append_str (dbuf, "( ");
2956 for (args = FUNC_ARGS(type);
2959 dbuf_printTypeChain(args->type, dbuf);
2961 dbuf_append_str (dbuf, ", ");
2963 dbuf_append_str (dbuf, ") ");
2966 dbuf_append_str (dbuf, "generic* ");
2969 dbuf_append_str (dbuf, "code* ");
2972 dbuf_append_str (dbuf, "xdata* ");
2975 dbuf_append_str (dbuf, "eeprom* ");
2978 dbuf_append_str (dbuf, "near* ");
2981 dbuf_append_str (dbuf, "idata* ");
2984 dbuf_append_str (dbuf, "pdata* ");
2987 dbuf_append_str (dbuf, "unknown* ");
2990 if (DCL_ELEM(type)) {
2991 dbuf_printf (dbuf, "[%d] ", DCL_ELEM(type));
2993 dbuf_append_str (dbuf, "[] ");
3000 if (SPEC_VOLATILE (type))
3001 dbuf_append_str (dbuf, "volatile-");
3002 if (SPEC_CONST (type))
3003 dbuf_append_str (dbuf, "const-");
3004 if (SPEC_USIGN (type))
3005 dbuf_append_str (dbuf, "unsigned-");
3006 switch (SPEC_NOUN (type))
3010 dbuf_append_str (dbuf, "long-");
3011 dbuf_append_str (dbuf, "int");
3015 dbuf_append_str (dbuf, "char");
3019 dbuf_append_str (dbuf, "void");
3023 dbuf_append_str (dbuf, "float");
3027 dbuf_append_str (dbuf, "fixed16x16");
3031 dbuf_printf (dbuf, "struct %s", SPEC_STRUCT (type)->tag);
3035 dbuf_append_str (dbuf, "sbit");
3039 dbuf_append_str (dbuf, "bit");
3043 dbuf_printf (dbuf, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3047 dbuf_append_str (dbuf, "double");
3051 dbuf_append_str (dbuf, "unknown type");
3055 /* search entry in list before "type" */
3056 for (search = start; search && search->next != type;)
3057 search = search->next;
3060 dbuf_append_char(dbuf, ' ');
3064 /*--------------------------------------------------------------------*/
3065 /* printTypeChainRaw - prints the type chain in human readable form */
3066 /* in the raw data structure ordering */
3067 /*--------------------------------------------------------------------*/
3069 printTypeChainRaw (sym_link * start, FILE * of)
3082 fprintf (of, "void");
3092 if (!IS_FUNC(type)) {
3093 if (DCL_PTR_VOLATILE (type)) {
3094 fprintf (of, "volatile-");
3096 if (DCL_PTR_CONST (type)) {
3097 fprintf (of, "const-");
3099 if (DCL_PTR_RESTRICT (type)) {
3100 fprintf (of, "restrict-");
3103 switch (DCL_TYPE (type))
3106 if (IFFUNC_ISINLINE(type)) {
3107 fprintf (of, "inline-");
3109 fprintf (of, "function %s %s",
3110 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
3111 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
3113 for (args = FUNC_ARGS(type);
3116 printTypeChain(args->type, of);
3123 fprintf (of, "generic* ");
3126 fprintf (of, "code* ");
3129 fprintf (of, "xdata* ");
3132 fprintf (of, "eeprom* ");
3135 fprintf (of, "near* ");
3138 fprintf (of, "idata* ");
3141 fprintf (of, "pdata* ");
3144 fprintf (of, "unknown* ");
3147 if (DCL_ELEM(type)) {
3148 fprintf (of, "[%d] ", DCL_ELEM(type));
3150 fprintf (of, "[] ");
3154 if (DCL_TSPEC(type))
3157 printTypeChainRaw(DCL_TSPEC(type), of);
3161 else if (IS_SPEC (type))
3163 switch (SPEC_SCLS (type))
3165 case S_DATA: fprintf (of, "data-"); break;
3166 case S_XDATA: fprintf (of, "xdata-"); break;
3167 case S_SFR: fprintf (of, "sfr-"); break;
3168 case S_SBIT: fprintf (of, "sbit-"); break;
3169 case S_CODE: fprintf (of, "code-"); break;
3170 case S_IDATA: fprintf (of, "idata-"); break;
3171 case S_PDATA: fprintf (of, "pdata-"); break;
3172 case S_LITERAL: fprintf (of, "literal-"); break;
3173 case S_STACK: fprintf (of, "stack-"); break;
3174 case S_XSTACK: fprintf (of, "xstack-"); break;
3175 case S_BIT: fprintf (of, "bit-"); break;
3176 case S_EEPROM: fprintf (of, "eeprom-"); break;
3179 if (SPEC_VOLATILE (type))
3180 fprintf (of, "volatile-");
3181 if (SPEC_CONST (type))
3182 fprintf (of, "const-");
3183 if (SPEC_USIGN (type))
3184 fprintf (of, "unsigned-");
3185 switch (SPEC_NOUN (type))
3189 fprintf (of, "long-");
3190 fprintf (of, "int");
3194 fprintf (of, "char");
3198 fprintf (of, "void");
3202 fprintf (of, "float");
3206 fprintf (of, "fixed16x16");
3210 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3214 fprintf (of, "sbit");
3218 fprintf (of, "bit");
3222 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3226 fprintf (of, "double");
3230 fprintf (of, "unknown type");
3235 fprintf (of, "NOT_SPEC_OR_DECL");
3245 /*-----------------------------------------------------------------*/
3246 /* powof2 - returns power of two for the number if number is pow 2 */
3247 /*-----------------------------------------------------------------*/
3249 powof2 (TYPE_TARGET_ULONG num)
3262 if (n1s > 1 || nshifts == 0)
3278 symbol *__fps16x16_add;
3279 symbol *__fps16x16_sub;
3280 symbol *__fps16x16_mul;
3281 symbol *__fps16x16_div;
3282 symbol *__fps16x16_eq;
3283 symbol *__fps16x16_neq;
3284 symbol *__fps16x16_lt;
3285 symbol *__fps16x16_lteq;
3286 symbol *__fps16x16_gt;
3287 symbol *__fps16x16_gteq;
3289 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3290 symbol *__muldiv[3][3][2];
3291 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3292 sym_link *__multypes[3][2];
3293 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3294 symbol *__conv[2][3][2];
3295 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3296 symbol *__fp16x16conv[2][4][2];
3297 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3298 symbol *__rlrr[2][3][2];
3300 sym_link *floatType;
3301 sym_link *fixed16x16Type;
3304 _mangleFunctionName(char *in)
3306 if (port->getMangledFunctionName)
3308 return port->getMangledFunctionName(in);
3316 /*-----------------------------------------------------------------*/
3317 /* typeFromStr - create a typechain from an encoded string */
3318 /* basic types - 'c' - char */
3323 /* 'q' - fixed16x16 */
3325 /* '*' - pointer - default (GPOINTER) */
3326 /* modifiers - 'u' - unsigned */
3327 /* pointer modifiers - 'g' - generic */
3331 /* 'F' - function */
3332 /* examples : "ig*" - generic int * */
3333 /* "cx*" - char xdata * */
3334 /* "ui" - unsigned int */
3335 /*-----------------------------------------------------------------*/
3336 sym_link *typeFromStr (char *s)
3338 sym_link *r = newLink(DECLARATOR);
3350 r->class = SPECIFIER;
3351 SPEC_NOUN(r) = V_CHAR;
3355 r->class = SPECIFIER;
3356 SPEC_NOUN(r) = V_INT;
3359 r->class = SPECIFIER;
3360 SPEC_NOUN(r) = V_INT;
3364 r->class = SPECIFIER;
3365 SPEC_NOUN(r) = V_FLOAT;
3368 r->class = SPECIFIER;
3369 SPEC_NOUN(r) = V_FIXED16X16;
3372 r->class = SPECIFIER;
3373 SPEC_NOUN(r) = V_VOID;
3376 DCL_TYPE(r) = port->unqualified_pointer;
3383 assert(*(s+1)=='*');
3384 nr = newLink(DECLARATOR);
3389 DCL_TYPE(r) = GPOINTER;
3392 DCL_TYPE(r) = FPOINTER;
3395 DCL_TYPE(r) = CPOINTER;
3398 DCL_TYPE(r) = POINTER;
3401 DCL_TYPE(r) = FUNCTION;
3402 nr = newLink(DECLARATOR);
3405 DCL_TYPE(r) = CPOINTER;
3411 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3412 "typeFromStr: unknown type");
3415 if (IS_SPEC(r) && usign) {
3424 /*-----------------------------------------------------------------*/
3425 /* initCSupport - create functions for C support routines */
3426 /*-----------------------------------------------------------------*/
3430 const char *smuldivmod[] =
3434 const char *sbwd[] =
3436 "char", "int", "long", "fixed16x16",
3438 const char *fp16x16sbwd[] =
3440 "char", "int", "long", "float",
3446 const char *srlrr[] =
3451 int bwd, su, muldivmod, tofrom, rlrr;
3453 if (getenv("SDCC_NO_C_SUPPORT")) {
3454 /* for debugging only */
3458 floatType = newFloatLink ();
3459 fixed16x16Type = newFixed16x16Link ();
3461 for (bwd = 0; bwd < 3; bwd++)
3478 __multypes[bwd][0] = l;
3479 __multypes[bwd][1] = copyLinkChain (l);
3480 SPEC_USIGN (__multypes[bwd][1]) = 1;
3483 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3484 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3485 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3486 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3487 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3488 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3489 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3490 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3491 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3492 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3494 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3495 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3496 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3497 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3498 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3499 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3500 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3501 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3502 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3503 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3506 for (tofrom = 0; tofrom < 2; tofrom++)
3508 for (bwd = 0; bwd < 3; bwd++)
3510 for (su = 0; su < 2; su++)
3514 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3515 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3519 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3520 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3526 for (tofrom = 0; tofrom < 2; tofrom++)
3528 for (bwd = 0; bwd < 4; bwd++)
3530 for (su = 0; su < 2; su++)
3534 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3536 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3538 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3542 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3544 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3546 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3553 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3555 for (bwd = 0; bwd < 3; bwd++)
3557 for (su = 0; su < 2; su++)
3559 SNPRINTF (buffer, sizeof(buffer),
3561 smuldivmod[muldivmod],
3564 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3565 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3570 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3571 Therefore they've been merged into mulint() and mullong().
3574 for (bwd = 0; bwd < 3; bwd++)
3576 for (su = 0; su < 2; su++)
3578 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3580 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
3581 if (!TARGET_IS_PIC16 || muldivmod != 1 || bwd != 0 || su != 0)
3583 SNPRINTF (buffer, sizeof(buffer),
3585 smuldivmod[muldivmod],
3588 __muldiv[muldivmod][bwd][su] = funcOfType (
3589 _mangleFunctionName(buffer),
3590 __multypes[bwd][su],
3591 __multypes[bwd][su],
3593 options.intlong_rent);
3594 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3600 if (TARGET_IS_PIC16)
3602 /* PIC16 port wants __divschar/__modschar to return an int, so that both
3603 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
3604 * (first one would have to be sign extended, second one must not be).
3605 * Similarly, modschar should be handled, but the iCode introduces cast
3606 * here and forces '% : s8 x s8 -> s8' ... */
3608 for (muldivmod = 1; muldivmod < 2; muldivmod++) {
3609 SNPRINTF (buffer, sizeof(buffer),
3611 smuldivmod[muldivmod],
3614 __muldiv[muldivmod][bwd][su] = funcOfType (
3615 _mangleFunctionName(buffer),
3617 __multypes[bwd][su],
3619 options.intlong_rent);
3620 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3628 for (su = 0; su < 2; su++)
3630 /* muluchar and mulschar are still separate functions, because e.g. the z80
3631 port is sign/zero-extending to int before calling mulint() */
3632 SNPRINTF (buffer, sizeof(buffer),
3634 smuldivmod[muldivmod],
3637 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3638 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3642 /* word and doubleword */
3643 for (bwd = 1; bwd < 3; bwd++)
3646 SNPRINTF (buffer, sizeof(buffer),
3648 smuldivmod[muldivmod],
3650 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3651 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3652 /* signed = unsigned */
3653 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3656 for (rlrr = 0; rlrr < 2; rlrr++)
3658 for (bwd = 0; bwd < 3; bwd++)
3660 for (su = 0; su < 2; su++)
3662 SNPRINTF (buffer, sizeof(buffer),
3667 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3668 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3674 /*-----------------------------------------------------------------*/
3675 /* initBuiltIns - create prototypes for builtin functions */
3676 /*-----------------------------------------------------------------*/
3682 if (!port->builtintable) return ;
3684 for (i = 0 ; port->builtintable[i].name ; i++) {
3685 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3686 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3687 FUNC_ISBUILTIN(sym->type) = 1;
3688 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3692 sym_link *validateLink(sym_link *l,
3699 if (l && l->class==select)
3704 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3705 " expected %s, got %s\n",
3706 macro, args, file, line,
3707 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3709 return l; // never reached, makes compiler happy.
3712 /*--------------------------------------------------------------------*/
3713 /* newEnumType - create an integer type compatible with enumerations */
3714 /*--------------------------------------------------------------------*/
3716 newEnumType (symbol *enumlist)
3724 type = newLink (SPECIFIER);
3725 SPEC_NOUN (type) = V_INT;
3729 /* Determine the range of the enumerated values */
3731 min = max = (int) ulFromVal (valFromType (sym->type));
3732 for (sym = sym->next; sym; sym = sym->next)
3734 v = (int) ulFromVal (valFromType (sym->type));
3741 /* Determine the smallest integer type that is compatible with this range */
3742 type = newLink (SPECIFIER);
3743 if (min>=0 && max<=255)
3745 SPEC_NOUN (type) = V_CHAR;
3746 SPEC_USIGN (type) = 1;
3748 else if (min>=-128 && max<=127)
3750 SPEC_NOUN (type) = V_CHAR;
3752 else if (min>=0 && max<=65535)
3754 SPEC_NOUN (type) = V_INT;
3755 SPEC_USIGN (type) = 1;
3757 else if (min>=-32768 && max<=32767)
3759 SPEC_NOUN (type) = V_INT;
3763 SPEC_NOUN (type) = V_INT;
3764 SPEC_LONG (type) = 1;
3766 SPEC_USIGN (type) = 1;