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 -------------------------------------------------------------------------*/
29 value *aggregateToPointer (value *val);
30 void printTypeChainRaw (sym_link * start, FILE * of);
32 void printFromToType(sym_link *from, sym_link *to) {
33 fprintf (stderr, "from type '");
34 printTypeChain (from, stderr);
35 fprintf (stderr, "'\nto type '");
36 printTypeChain (to, stderr);
37 fprintf (stderr, "'\n");
41 char *nounName(sym_link *sl) {
42 switch (SPEC_NOUN(sl))
45 if (SPEC_LONG(sl)) return "long";
46 if (SPEC_SHORT(sl)) return "short";
49 case V_FLOAT: return "float";
50 case V_FIXED16X16: return "fixed16x16";
51 case V_CHAR: return "char";
52 case V_VOID: return "void";
53 case V_STRUCT: return "struct";
54 case V_LABEL: return "label";
55 case V_BITFIELD: return "bitfield";
56 case V_BIT: return "bit";
57 case V_SBIT: return "sbit";
58 case V_DOUBLE: return "double";
63 bucket *SymbolTab[256]; /* the symbol table */
64 bucket *StructTab[256]; /* the structure table */
65 bucket *TypedefTab[256]; /* the typedef table */
66 bucket *LabelTab[256]; /* the Label table */
67 bucket *enumTab[256]; /* enumerated table */
69 /*------------------------------------------------------------------*/
70 /* initSymt () - initialises symbol table related stuff */
71 /*------------------------------------------------------------------*/
77 for (i = 0; i < 256; i++)
78 SymbolTab[i] = StructTab[i] = (void *) NULL;
82 /*-----------------------------------------------------------------*/
83 /* newBucket - allocates & returns a new bucket */
84 /*-----------------------------------------------------------------*/
90 bp = Safe_alloc ( sizeof (bucket));
95 /*-----------------------------------------------------------------*/
96 /* hashKey - computes the hashkey given a symbol name */
97 /*-----------------------------------------------------------------*/
99 hashKey (const char *s)
101 unsigned long key = 0;
108 /*-----------------------------------------------------------------*/
109 /* addSym - adds a symbol to the hash Table */
110 /*-----------------------------------------------------------------*/
112 addSym (bucket ** stab,
119 int i; /* index into the hash Table */
120 bucket *bp; /* temp bucket * */
123 symbol *csym = (symbol *)sym;
125 if (getenv("DEBUG_SANITY")) {
126 fprintf (stderr, "addSym: %s ", sname);
128 /* make sure the type is complete and sane */
129 checkTypeSanity(csym->etype, csym->name);
132 /* prevent overflow of the (r)name buffers */
133 if (strlen(sname)>SDCC_SYMNAME_MAX) {
134 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
135 sname[SDCC_SYMNAME_MAX]='\0';
138 /* the symbols are always added at the head of the list */
140 /* get a free entry */
141 bp = Safe_alloc ( sizeof (bucket));
143 bp->sym = sym; /* update the symbol pointer */
144 bp->level = level; /* update the nest level */
146 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
148 /* if this is the first entry */
151 bp->prev = bp->next = (void *) NULL; /* point to nothing */
154 /* not first entry then add @ head of list */
164 /*-----------------------------------------------------------------*/
165 /* deleteSym - deletes a symbol from the hash Table entry */
166 /*-----------------------------------------------------------------*/
168 deleteSym (bucket ** stab, void *sym, char *sname)
176 /* find the symbol */
179 if (bp->sym == sym) /* found it then break out */
180 break; /* of the loop */
184 if (!bp) /* did not find it */
186 /* if this is the first one in the chain */
190 if (stab[i]) /* if chain ! empty */
191 stab[i]->prev = (void *) NULL;
193 /* middle || end of chain */
196 if (bp->next) /* if not end of chain */
197 bp->next->prev = bp->prev;
199 bp->prev->next = bp->next;
204 /*-----------------------------------------------------------------*/
205 /* findSym - finds a symbol in a table */
206 /*-----------------------------------------------------------------*/
208 findSym (bucket ** stab, void *sym, const char *sname)
212 bp = stab[hashKey (sname)];
215 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
220 return (bp ? bp->sym : (void *) NULL);
223 /*-----------------------------------------------------------------*/
224 /* findSymWithLevel - finds a symbol with a name & level */
225 /*-----------------------------------------------------------------*/
227 findSymWithLevel (bucket ** stab, symbol * sym)
231 bp = stab[hashKey (sym->name)];
234 ** do the search from the head of the list since the
235 ** elements are added at the head it is ensured that
236 ** we will find the deeper definitions before we find
237 ** the global ones. we need to check for symbols with
238 ** level <= to the level given, if levels match then block
239 ** numbers need to match as well
243 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
245 /* if this is parameter then nothing else need to be checked */
246 if (((symbol *) (bp->sym))->_isparm)
248 /* if levels match then block numbers should also match */
249 if (bp->level && bp->level == sym->level && bp->block == sym->block)
251 /* if levels don't match then we are okay */
252 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
254 /* if this is a global variable then we are ok too */
262 return (void *) NULL;
265 /*-----------------------------------------------------------------*/
266 /* findSymWithBlock - finds a symbol with name in with a block */
267 /*-----------------------------------------------------------------*/
269 findSymWithBlock (bucket ** stab, symbol * sym, int block)
273 bp = stab[hashKey (sym->name)];
276 if (strcmp (bp->name, sym->name) == 0 &&
282 return (bp ? bp->sym : (void *) NULL);
285 /*------------------------------------------------------------------*/
286 /* newSymbol () - returns a new pointer to a symbol */
287 /*------------------------------------------------------------------*/
289 newSymbol (char *name, int scope)
293 sym = Safe_alloc ( sizeof (symbol));
295 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
296 sym->level = scope; /* set the level */
297 sym->block = currBlockno;
298 sym->lineDef = mylineno; /* set the line number */
299 sym->fileDef = currFname;
303 /*------------------------------------------------------------------*/
304 /* newLink - creates a new link (declarator,specifier) */
305 /*------------------------------------------------------------------*/
307 newLink (SYM_LINK_CLASS select)
311 p = Safe_alloc ( sizeof (sym_link));
317 /*------------------------------------------------------------------*/
318 /* newStruct - creats a new structdef from the free list */
319 /*------------------------------------------------------------------*/
321 newStruct (char *tag)
325 s = Safe_alloc ( sizeof (structdef));
327 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
331 /*------------------------------------------------------------------*/
332 /* sclsFromPtr - Return the storage class a pointer points into. */
333 /* S_FIXED is returned for generic pointers or other */
334 /* unexpected cases */
335 /*------------------------------------------------------------------*/
337 sclsFromPtr(sym_link *ptr)
339 switch (DCL_TYPE (ptr))
362 /*------------------------------------------------------------------*/
363 /* pointerTypes - do the computation for the pointer types */
364 /*------------------------------------------------------------------*/
366 pointerTypes (sym_link * ptr, sym_link * type)
371 /* find the first pointer type */
372 while (ptr && !IS_PTR (ptr))
375 /* could not find it */
376 if (!ptr || IS_SPEC (ptr))
379 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
380 pointerTypes (ptr->next, type);
384 /* change the pointer type depending on the
385 storage class of the type */
388 switch (SPEC_SCLS (type))
391 DCL_TYPE (ptr) = FPOINTER;
394 DCL_TYPE (ptr) = IPOINTER;
397 DCL_TYPE (ptr) = PPOINTER;
400 DCL_TYPE (ptr) = POINTER;
403 DCL_TYPE (ptr) = CPOINTER;
406 DCL_TYPE (ptr) = EEPPOINTER;
409 DCL_TYPE (ptr) = port->unqualified_pointer;
412 /* the storage class of type ends here */
413 SPEC_SCLS (type) = 0;
416 /* now change all the remaining unknown pointers
417 to generic pointers */
420 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
421 DCL_TYPE (ptr) = port->unqualified_pointer;
425 /* same for the type although it is highly unlikely that
426 type will have a pointer */
429 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
430 DCL_TYPE (type) = port->unqualified_pointer;
435 /*------------------------------------------------------------------*/
436 /* addDecl - adds a declarator @ the end of a chain */
437 /*------------------------------------------------------------------*/
439 addDecl (symbol * sym, int type, sym_link * p)
441 static sym_link *empty = NULL;
446 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
447 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
450 empty = newLink(SPECIFIER);
452 /* if we are passed a link then set head & tail */
461 head = tail = newLink (DECLARATOR);
462 DCL_TYPE (head) = type;
465 /* if this is the first entry */
471 else if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
473 sym->etype = mergeSpec (sym->etype, head, sym->name);
475 else if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
478 while (t->next != sym->etype)
481 tail->next = sym->etype;
483 else if (IS_FUNC (sym->type) && IS_SPEC (sym->type->next) &&
484 !memcmp(sym->type->next, empty, sizeof(sym_link)))
486 sym->type->next = head;
491 sym->etype->next = head;
495 /* if the type is an unknown pointer and has
496 a tspec then take the storage class const & volatile
497 attribute from the tspec & make it those of this
501 //DCL_TYPE (p) == UPOINTER &&
504 if (!IS_SPEC (sym->etype))
506 sym->etype = sym->etype->next = newLink (SPECIFIER);
508 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
509 DCL_TSPEC (p) = NULL;
512 // if there is a function in this type chain
513 if (p && funcInChain(sym->type)) {
514 processFuncArgs (sym);
520 /*------------------------------------------------------------------
521 checkTypeSanity: prevent the user from doing e.g.:
523 ------------------------------------------------------------------*/
524 void checkTypeSanity(sym_link *etype, char *name) {
528 if (getenv("DEBUG_SANITY")) {
529 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
534 if (!IS_SPEC(etype)) {
535 if (getenv("DEBUG_SANITY")) {
536 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
541 noun=nounName(etype);
543 if (getenv("DEBUG_SANITY")) {
544 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
547 if ((SPEC_NOUN(etype)==V_CHAR ||
548 SPEC_NOUN(etype)==V_FLOAT ||
549 SPEC_NOUN(etype)==V_FIXED16X16 ||
550 SPEC_NOUN(etype)==V_DOUBLE ||
551 SPEC_NOUN(etype)==V_VOID) &&
552 (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
553 // long or short for char float double or void
554 werror (E_LONG_OR_SHORT_INVALID, noun, name);
556 if ((SPEC_NOUN(etype)==V_FLOAT ||
557 SPEC_NOUN(etype)==V_FIXED16X16 ||
558 SPEC_NOUN(etype)==V_DOUBLE ||
559 SPEC_NOUN(etype)==V_VOID) &&
560 (etype->select.s.b_signed || SPEC_USIGN(etype))) {
561 // signed or unsigned for float double or void
562 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
565 // special case for "short"
566 if (SPEC_SHORT(etype)) {
567 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
568 SPEC_SHORT(etype) = 0;
572 "const a;" or "data b;" or "signed s" or "long l"
574 if (!SPEC_NOUN(etype)) {
575 SPEC_NOUN(etype)=V_INT;
578 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
579 /* a "plain" int bitfield is unsigned */
580 if (SPEC_NOUN(etype)==V_BIT ||
581 SPEC_NOUN(etype)==V_SBIT) {
582 if (!etype->select.s.b_signed)
583 SPEC_USIGN(etype) = 1;
586 if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
587 // signed AND unsigned
588 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
590 if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
592 werror (E_LONG_AND_SHORT_INVALID, noun, name);
597 /*------------------------------------------------------------------*/
598 /* mergeSpec - merges two specifiers and returns the new one */
599 /*------------------------------------------------------------------*/
601 mergeSpec (sym_link * dest, sym_link * src, char *name)
603 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
605 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
608 werror (E_SYNTAX_ERROR, yytext);
609 // the show must go on
614 if (SPEC_NOUN(src)) {
615 if (!SPEC_NOUN(dest)) {
616 SPEC_NOUN(dest)=SPEC_NOUN(src);
618 /* we shouldn't redeclare the type */
619 if (getenv("DEBUG_SANITY")) {
620 fprintf (stderr, "mergeSpec: ");
622 werror(E_TWO_OR_MORE_DATA_TYPES, name);
626 if (SPEC_SCLS(src)) {
627 /* if destination has no storage class */
628 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
629 SPEC_SCLS (dest) = SPEC_SCLS (src);
631 if (getenv("DEBUG_SANITY")) {
632 fprintf (stderr, "mergeSpec: ");
634 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
638 /* copy all the specifications */
640 // we really should do:
642 if (SPEC_what(src)) {
643 if (SPEC_what(dest)) {
644 werror(W_DUPLICATE_SPEC, "what");
646 SPEC_what(dst)|=SPEC_what(src);
649 // but there are more important thing right now
651 SPEC_LONG (dest) |= SPEC_LONG (src);
652 SPEC_SHORT(dest) |= SPEC_SHORT(src);
653 SPEC_USIGN (dest) |= SPEC_USIGN (src);
654 dest->select.s.b_signed|=src->select.s.b_signed;
655 SPEC_STAT (dest) |= SPEC_STAT (src);
656 SPEC_EXTR (dest) |= SPEC_EXTR (src);
657 SPEC_CONST(dest) |= SPEC_CONST (src);
658 SPEC_ABSA (dest) |= SPEC_ABSA (src);
659 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
660 SPEC_RESTRICT (dest) |= SPEC_RESTRICT (src);
661 SPEC_ADDR (dest) |= SPEC_ADDR (src);
662 SPEC_OCLS (dest) = SPEC_OCLS (src);
663 SPEC_BLEN (dest) |= SPEC_BLEN (src);
664 SPEC_BSTR (dest) |= SPEC_BSTR (src);
665 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
666 SPEC_ENUM (dest) |= SPEC_ENUM (src);
667 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
668 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
670 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
671 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
673 /* these are the only function attributes that will be set
674 in a specifier while parsing */
675 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
676 FUNC_BANKED(dest) |= FUNC_BANKED(src);
677 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
678 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
679 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
680 FUNC_ISISR(dest) |= FUNC_ISISR(src);
681 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
682 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
683 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
684 FUNC_INTNO(dest) |= FUNC_INTNO(src);
685 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
690 /*------------------------------------------------------------------*/
691 /* genSymName - generates and returns a name used for anonymous vars */
692 /*------------------------------------------------------------------*/
694 genSymName (int level)
696 static int gCount = 0;
697 static char gname[SDCC_NAME_MAX + 1];
699 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
703 /*------------------------------------------------------------------*/
704 /* getSpec - returns the specifier part from a declaration chain */
705 /*------------------------------------------------------------------*/
707 getSpec (sym_link * p)
712 while (p && !(IS_SPEC (p)))
718 /*------------------------------------------------------------------*/
719 /* newCharLink() - creates an char type */
720 /*------------------------------------------------------------------*/
726 p = newLink (SPECIFIER);
727 SPEC_NOUN (p) = V_CHAR;
732 /*------------------------------------------------------------------*/
733 /* newFloatLink - a new Float type */
734 /*------------------------------------------------------------------*/
740 p = newLink (SPECIFIER);
741 SPEC_NOUN (p) = V_FLOAT;
746 /*------------------------------------------------------------------*/
747 /* newFixed16x16Link - a new Float type */
748 /*------------------------------------------------------------------*/
754 p = newLink (SPECIFIER);
755 SPEC_NOUN (p) = V_FIXED16X16;
760 /*------------------------------------------------------------------*/
761 /* newLongLink() - new long type */
762 /*------------------------------------------------------------------*/
768 p = newLink (SPECIFIER);
769 SPEC_NOUN (p) = V_INT;
775 /*------------------------------------------------------------------*/
776 /* newIntLink() - creates an int type */
777 /*------------------------------------------------------------------*/
783 p = newLink (SPECIFIER);
784 SPEC_NOUN (p) = V_INT;
789 /*------------------------------------------------------------------*/
790 /* newBoolLink() - creates an bool type */
791 /*------------------------------------------------------------------*/
797 p = newLink (SPECIFIER);
798 SPEC_NOUN (p) = V_BIT;
803 /*------------------------------------------------------------------*/
804 /* getSize - returns size of a type chain in bytes */
805 /*------------------------------------------------------------------*/
807 getSize (sym_link * p)
809 /* if nothing return 0 */
813 { /* if this is the specifier then */
814 switch (SPEC_NOUN (p))
815 { /* depending on the specifier type */
817 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
827 return SPEC_STRUCT (p)->size;
834 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
840 /* this is a declarator */
841 switch (DCL_TYPE (p))
845 return DCL_ELEM (p) * getSize (p->next);
847 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
848 // "can not tell the size of an array[]");
859 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
868 /*------------------------------------------------------------------*/
869 /* checkStructFlexArray - check tree behind a struct */
870 /*------------------------------------------------------------------*/
872 checkStructFlexArray (symbol *sym, sym_link *p)
874 /* if nothing return FALSE */
880 /* (nested) struct with flexible array member? */
881 if (IS_STRUCT (p) && SPEC_STRUCT (p)->b_flexArrayMember)
883 werror (W_INVALID_FLEXARRAY);
889 /* this is a declarator */
892 /* flexible array member? */
895 if (!options.std_c99)
896 werror (W_C89_NO_FLEXARRAY);
900 return checkStructFlexArray (sym, p->next);
905 /*------------------------------------------------------------------*/
906 /* bitsForType - returns # of bits required to store this type */
907 /*------------------------------------------------------------------*/
909 bitsForType (sym_link * p)
911 /* if nothing return 0 */
916 { /* if this is the specifier then */
918 switch (SPEC_NOUN (p))
919 { /* depending on the specifier type */
921 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
923 return FLOATSIZE * 8;
931 return SPEC_STRUCT (p)->size * 8;
938 return SPEC_BLEN (p);
944 /* this is a specifier */
945 switch (DCL_TYPE (p))
948 return DCL_ELEM (p) * getSize (p->next) * 8;
952 return (PTRSIZE * 8);
957 return (FPTRSIZE * 8);
959 return (GPTRSIZE * 8);
966 /*------------------------------------------------------------------*/
967 /* copySymbolChain - copies a symbol chain */
968 /*------------------------------------------------------------------*/
970 copySymbolChain (symbol * src)
977 dest = copySymbol (src);
978 dest->next = copySymbolChain (src->next);
982 /*------------------------------------------------------------------*/
983 /* copySymbol - makes a copy of a symbol */
984 /*------------------------------------------------------------------*/
986 copySymbol (symbol * src)
993 dest = newSymbol (src->name, src->level);
994 memcpy (dest, src, sizeof (symbol));
995 dest->level = src->level;
996 dest->block = src->block;
997 dest->ival = copyIlist (src->ival);
998 dest->type = copyLinkChain (src->type);
999 dest->etype = getSpec (dest->type);
1001 dest->key = src->key;
1002 dest->allocreq = src->allocreq;
1006 /*------------------------------------------------------------------*/
1007 /* reverseSyms - reverses the links for a symbol chain */
1008 /*------------------------------------------------------------------*/
1010 reverseSyms (symbol * sym)
1012 symbol *prev, *curr, *next;
1027 sym->next = (void *) NULL;
1031 /*------------------------------------------------------------------*/
1032 /* reverseLink - reverses the links for a type chain */
1033 /*------------------------------------------------------------------*/
1035 reverseLink (sym_link * type)
1037 sym_link *prev, *curr, *next;
1052 type->next = (void *) NULL;
1056 /*------------------------------------------------------------------*/
1057 /* addSymChain - adds a symbol chain to the symboltable */
1058 /*------------------------------------------------------------------*/
1060 addSymChain (symbol ** symHead)
1062 symbol *sym = *symHead;
1063 symbol *csym = NULL;
1067 for (; sym != NULL; sym = sym->next)
1069 changePointer(sym->type);
1070 checkTypeSanity(sym->etype, sym->name);
1072 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1075 /* if already exists in the symbol table then check if
1076 one of them is an extern definition if yes then
1077 then check if the type match, if the types match then
1078 delete the current entry and add the new entry */
1079 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1080 csym->level == sym->level) {
1082 /* If the previous definition was for an array with incomplete */
1083 /* type, and the new definition has completed the type, update */
1084 /* the original type to match */
1085 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1086 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1088 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1089 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1093 /* If only one of the definitions used the "at" keyword, copy */
1094 /* the address to the other. */
1095 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1096 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1098 SPEC_ABSA (sym->etype) = 1;
1099 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1101 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1102 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1104 SPEC_ABSA (csym->etype) = 1;
1105 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1110 if (csym->ival && sym->ival)
1112 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1116 /* one definition extern ? */
1117 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1118 werror (E_EXTERN_MISMATCH, sym->name);
1120 werror (E_DUPLICATE, sym->name);
1121 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1123 fprintf (stderr, "from type '");
1124 printTypeChain (csym->type, stderr);
1125 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1126 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1127 fprintf (stderr, "'\nto type '");
1128 printTypeChain (sym->type, stderr);
1129 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1130 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1131 fprintf (stderr, "'\n");
1136 if (csym->ival && !sym->ival)
1137 sym->ival = csym->ival;
1139 /* delete current entry */
1140 deleteSym (SymbolTab, csym, csym->name);
1141 deleteFromSeg(csym);
1143 symPtrPtr = symHead;
1144 while (*symPtrPtr && *symPtrPtr != csym)
1145 symPtrPtr = &(*symPtrPtr)->next;
1146 if (*symPtrPtr == csym)
1147 *symPtrPtr = csym->next;
1152 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1157 /*------------------------------------------------------------------*/
1158 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1159 /*------------------------------------------------------------------*/
1161 funcInChain (sym_link * lnk)
1172 /*------------------------------------------------------------------*/
1173 /* structElemType - returns the type info of a struct member */
1174 /*------------------------------------------------------------------*/
1176 structElemType (sym_link * stype, value * id)
1178 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1179 sym_link *type, *etype;
1180 sym_link *petype = getSpec (stype);
1184 /* look for the id */
1187 if (strcmp (fields->rname, id->name) == 0)
1189 type = copyLinkChain (fields->type);
1190 etype = getSpec (type);
1191 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1192 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1194 SPEC_CONST (type) |= SPEC_CONST (stype);
1196 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1199 fields = fields->next;
1203 werror (E_NOT_MEMBER, id->name);
1205 // the show must go on
1206 return newIntLink();
1209 /*------------------------------------------------------------------*/
1210 /* getStructElement - returns element of a tructure definition */
1211 /*------------------------------------------------------------------*/
1213 getStructElement (structdef * sdef, symbol * sym)
1217 for (field = sdef->fields; field; field = field->next)
1218 if (strcmp (field->name, sym->name) == 0)
1221 werror (E_NOT_MEMBER, sym->name);
1223 return sdef->fields;
1226 /*------------------------------------------------------------------*/
1227 /* compStructSize - computes the size of a structure */
1228 /*------------------------------------------------------------------*/
1230 compStructSize (int su, structdef * sdef)
1232 int sum = 0, usum = 0;
1236 /* for the identifiers */
1237 loop = sdef->fields;
1240 /* create the internal name for this variable */
1241 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1246 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1248 /* if this is a bit field */
1251 /* change it to a unsigned bit */
1252 SPEC_NOUN (loop->etype) = V_BITFIELD;
1253 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1254 /* a "plain" int bitfield is unsigned */
1255 if (!loop->etype->select.s.b_signed)
1256 SPEC_USIGN(loop->etype) = 1;
1258 SPEC_BLEN (loop->etype) = loop->bitVar;
1260 if (loop->bitVar == BITVAR_PAD) {
1261 /* A zero length bitfield forces padding */
1262 SPEC_BSTR (loop->etype) = bitOffset;
1263 SPEC_BLEN (loop->etype) = 0;
1268 if (bitOffset == 8) {
1272 /* check if this fit into the remaining */
1273 /* bits of this byte else align it to the */
1274 /* next byte boundary */
1275 if (loop->bitVar <= (8 - bitOffset)) {
1276 /* fits into current byte */
1278 SPEC_BSTR (loop->etype) = bitOffset;
1279 bitOffset += loop->bitVar;
1281 else if (!bitOffset) {
1282 /* does not fit, but is already byte aligned */
1284 SPEC_BSTR (loop->etype) = bitOffset;
1285 bitOffset += loop->bitVar;
1288 if( TARGET_IS_PIC16 && getenv("PIC16_PACKED_BITFIELDS") ) {
1289 /* if PIC16 && enviroment variable is set, then
1290 * tightly pack bitfields, this means that when a
1291 * bitfield goes beyond byte alignment, do not
1292 * automatically start allocatint from next byte,
1293 * but also use the available bits first */
1294 fprintf(stderr, ": packing bitfields in structures\n");
1295 SPEC_BSTR (loop->etype) = bitOffset;
1296 bitOffset += loop->bitVar;
1297 loop->offset = (su == UNION ? sum = 0 : sum);
1299 /* does not fit; need to realign first */
1301 loop->offset = (su == UNION ? sum = 0 : sum);
1303 SPEC_BSTR (loop->etype) = bitOffset;
1304 bitOffset += loop->bitVar;
1307 while (bitOffset>8) {
1314 /* This is a non-bit field. Make sure we are */
1315 /* byte aligned first */
1318 loop->offset = (su == UNION ? sum = 0 : sum);
1322 checkDecl (loop, 1);
1323 sum += getSize (loop->type);
1325 /* search for "flexibel array members" */
1326 /* and do some syntax checks */
1328 && checkStructFlexArray (loop, loop->type))
1330 /* found a "flexible array member" */
1331 sdef->b_flexArrayMember = TRUE;
1332 /* is another struct-member following? */
1334 werror (E_FLEXARRAY_NOTATEND);
1335 /* is it the first struct-member? */
1336 else if (loop == sdef->fields)
1337 werror (E_FLEXARRAY_INEMPTYSTRCT);
1343 /* if union then size = sizeof largest field */
1345 /* For UNION, round up after each field */
1346 sum += ((bitOffset+7)/8);
1347 usum = max (usum, sum);
1352 /* For STRUCT, round up after all fields processed */
1354 sum += ((bitOffset+7)/8);
1356 return (su == UNION ? usum : sum);
1359 /*-------------------------------------------------------------------*/
1360 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1361 /* an enclosing struct/union */
1362 /*-------------------------------------------------------------------*/
1364 promoteAnonStructs (int su, structdef * sdef)
1373 tofield = &sdef->fields;
1374 field = sdef->fields;
1377 nextfield = field->next;
1378 if (!*field->name && IS_STRUCT (field->type))
1380 /* Found an anonymous struct/union. Replace it */
1381 /* with the fields it contains and adjust all */
1384 base = field->offset;
1385 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1387 continue; /* just in case it's empty */
1389 *tofield = subfield;
1392 /* check for field name conflicts resulting from promotion */
1393 dupfield = sdef->fields;
1394 while (dupfield && dupfield != subfield)
1396 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1398 werrorfl (subfield->fileDef, subfield->lineDef,
1400 su==STRUCT ? "struct" : "union",
1402 werrorfl (dupfield->fileDef, dupfield->lineDef,
1405 dupfield = dupfield->next;
1408 subfield->offset += base;
1410 subfield = subfield->next;
1414 subfield->next = nextfield;
1415 tofield = &subfield->next;
1418 tofield = &field->next;
1424 /*------------------------------------------------------------------*/
1425 /* checkSClass - check the storage class specification */
1426 /*------------------------------------------------------------------*/
1428 checkSClass (symbol * sym, int isProto)
1432 if (getenv("DEBUG_SANITY")) {
1433 fprintf (stderr, "checkSClass: %s \n", sym->name);
1436 /* type is literal can happen for enums change to auto */
1437 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1438 SPEC_SCLS (sym->etype) = S_AUTO;
1440 /* if sfr or sbit then must also be volatile */
1441 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1442 SPEC_SCLS (sym->etype) == S_SFR)
1444 SPEC_VOLATILE (sym->etype) = 1;
1447 /* make sure restrict is only used with pointers */
1448 if (SPEC_RESTRICT (sym->etype))
1450 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1451 SPEC_RESTRICT (sym->etype) = 0;
1456 if (IS_DECL (t) && DCL_PTR_RESTRICT (t) && !IS_PTR (t))
1458 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1459 DCL_PTR_RESTRICT (t) = 0;
1465 /* if absolute address given then it mark it as
1466 volatile -- except in the PIC port */
1468 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1469 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1470 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1473 if (IS_ABSOLUTE (sym->etype))
1474 SPEC_VOLATILE (sym->etype) = 1;
1476 if (TARGET_IS_MCS51 &&
1477 IS_ABSOLUTE (sym->etype) &&
1478 SPEC_SCLS (sym->etype) == S_SFR)
1483 if (SPEC_NOUN (sym->etype) == V_CHAR)
1485 else if (SPEC_LONG (sym->etype) == 0)
1490 addr = SPEC_ADDR (sym->etype);
1491 for (n=0; n<size; n+=8)
1492 if (((addr >> n) & 0xFF) < 0x80)
1493 werror (W_SFR_ABSRANGE, sym->name);
1496 /* If code memory is read only, then pointers to code memory */
1497 /* implicitly point to constants -- make this explicit */
1499 while (t && t->next) {
1500 if (IS_CODEPTR(t) && port->mem.code_ro) {
1501 if (IS_SPEC(t->next)) {
1502 SPEC_CONST (t->next) = 1;
1504 DCL_PTR_CONST (t->next) = 1;
1510 /* global variables declared const put into code */
1511 /* if no other storage class specified */
1512 if (sym->level == 0 &&
1513 SPEC_SCLS(sym->etype) == S_FIXED &&
1514 !IS_FUNC(sym->type)) {
1515 /* find the first non-array link */
1519 if (IS_CONSTANT (t)) {
1520 SPEC_SCLS (sym->etype) = S_CODE;
1524 /* global variable in code space is a constant */
1525 if (sym->level == 0 &&
1526 SPEC_SCLS (sym->etype) == S_CODE &&
1527 port->mem.code_ro) {
1528 /* find the first non-array link */
1535 DCL_PTR_CONST (t) = 1;
1539 /* if bit variable then no storage class can be */
1540 /* specified since bit is already a storage */
1541 if (IS_BITVAR (sym->etype) &&
1542 (SPEC_SCLS (sym->etype) != S_FIXED &&
1543 SPEC_SCLS (sym->etype) != S_SBIT &&
1544 SPEC_SCLS (sym->etype) != S_BIT)
1547 werror (E_BITVAR_STORAGE, sym->name);
1548 SPEC_SCLS (sym->etype) = S_FIXED;
1551 /* extern variables cannot be initialized */
1552 if (IS_EXTERN (sym->etype) && sym->ival)
1554 werror (E_EXTERN_INIT, sym->name);
1558 /* if this is an automatic symbol */
1559 if (sym->level && (options.stackAuto || reentrant)) {
1560 if (SPEC_SCLS (sym->etype) != S_BIT) {
1561 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1562 SPEC_SCLS (sym->etype) == S_FIXED ||
1563 SPEC_SCLS (sym->etype) == S_REGISTER ||
1564 SPEC_SCLS (sym->etype) == S_STACK ||
1565 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1566 SPEC_SCLS (sym->etype) = S_AUTO;
1568 /* storage class may only be specified for statics */
1569 if (!IS_STATIC(sym->etype)) {
1570 werror (E_AUTO_ASSUMED, sym->name);
1576 /* automatic symbols cannot be given */
1577 /* an absolute address ignore it */
1579 SPEC_ABSA (sym->etype) &&
1580 (options.stackAuto || reentrant))
1582 werror (E_AUTO_ABSA, sym->name);
1583 SPEC_ABSA (sym->etype) = 0;
1586 /* arrays & pointers cannot be defined for bits */
1587 /* SBITS or SFRs or BIT */
1588 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1589 !IS_FUNCPTR (sym->type) &&
1590 (SPEC_NOUN (sym->etype) == V_BIT ||
1591 SPEC_NOUN (sym->etype) == V_SBIT ||
1592 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1593 SPEC_SCLS (sym->etype) == S_SFR))
1594 werror (E_BIT_ARRAY, sym->name);
1596 /* if this is a bit|sbit then set length & start */
1597 if (SPEC_NOUN (sym->etype) == V_BIT ||
1598 SPEC_NOUN (sym->etype) == V_SBIT)
1600 SPEC_BLEN (sym->etype) = 1;
1601 SPEC_BSTR (sym->etype) = 0;
1605 /* variables declared in CODE space must have */
1606 /* initializers if not an extern */
1607 if (SPEC_SCLS (sym->etype) == S_CODE &&
1608 sym->ival == NULL &&
1611 port->mem.code_ro &&
1612 !IS_EXTERN (sym->etype) &&
1613 !SPEC_ABSA (sym->etype) &&
1614 !funcInChain (sym->type))
1615 werror (E_CODE_NO_INIT, sym->name);
1618 /* if parameter or local variable then change */
1619 /* the storage class to reflect where the var will go */
1620 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1621 && !IS_STATIC(sym->etype)
1624 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1626 SPEC_SCLS (sym->etype) = (options.useXstack ?
1627 S_XSTACK : S_STACK);
1631 /* hack-o-matic! I see no reason why the useXstack option should ever
1632 * control this allocation, but the code was originally that way, and
1633 * changing it for non-390 ports breaks the compiler badly.
1635 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1636 1 : options.useXstack;
1637 SPEC_SCLS (sym->etype) = (useXdata ?
1643 /*------------------------------------------------------------------*/
1644 /* changePointer - change pointer to functions */
1645 /*------------------------------------------------------------------*/
1647 changePointer (sym_link * p)
1650 /* go thru the chain of declarations */
1651 /* if we find a pointer to a function */
1652 /* change it to a ptr to code area */
1653 /* unless the function is banked. */
1654 for (; p; p = p->next)
1656 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1657 DCL_TYPE (p) = port->unqualified_pointer;
1658 if (IS_PTR (p) && IS_FUNC (p->next))
1659 if (!IFFUNC_BANKED(p->next))
1660 DCL_TYPE (p) = CPOINTER;
1664 /*------------------------------------------------------------------*/
1665 /* checkDecl - does semantic validation of a declaration */
1666 /*------------------------------------------------------------------*/
1668 checkDecl (symbol * sym, int isProto)
1671 checkSClass (sym, isProto); /* check the storage class */
1672 changePointer (sym->type); /* change pointers if required */
1674 /* if this is an array without any dimension
1675 then update the dimension from the initial value */
1676 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1677 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1682 /*------------------------------------------------------------------*/
1683 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1684 /*------------------------------------------------------------------*/
1686 copyLinkChain (sym_link * p)
1688 sym_link *head, *curr, *loop;
1690 /* note: v_struct and v_struct->fields are not copied! */
1692 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1695 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1696 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1704 /*------------------------------------------------------------------*/
1705 /* cleanUpBlock - cleansup the symbol table specified for all the */
1706 /* symbols in the given block */
1707 /*------------------------------------------------------------------*/
1709 cleanUpBlock (bucket ** table, int block)
1714 /* go thru the entire table */
1715 for (i = 0; i < 256; i++)
1717 for (chain = table[i]; chain; chain = chain->next)
1719 if (chain->block >= block)
1721 deleteSym (table, chain->sym, chain->name);
1727 /*------------------------------------------------------------------*/
1728 /* cleanUpLevel - cleansup the symbol table specified for all the */
1729 /* symbols in the given level */
1730 /*------------------------------------------------------------------*/
1732 cleanUpLevel (bucket ** table, int level)
1737 /* go thru the entire table */
1738 for (i = 0; i < 256; i++)
1740 for (chain = table[i]; chain; chain = chain->next)
1742 if (chain->level >= level)
1744 deleteSym (table, chain->sym, chain->name);
1750 /*------------------------------------------------------------------*/
1751 /* computeTypeOr - computes the resultant type from two types */
1752 /*------------------------------------------------------------------*/
1754 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1757 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1758 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1760 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1762 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1766 if (SPEC_USIGN (etype1))
1768 if ( IS_LITERAL (etype2)
1769 && floatFromVal (valFromType (etype2)) >= 0)
1770 SPEC_USIGN (reType) = 1;
1773 /* promote to int */
1774 SPEC_USIGN (reType) = 0;
1775 SPEC_NOUN (reType) = V_INT;
1778 else /* etype1 signed */
1780 if ( IS_LITERAL (etype2)
1781 && floatFromVal (valFromType (etype2)) <= 127)
1782 SPEC_USIGN (reType) = 0;
1785 /* promote to int */
1786 SPEC_USIGN (reType) = 0;
1787 SPEC_NOUN (reType) = V_INT;
1791 if (SPEC_USIGN (etype2))
1793 if ( IS_LITERAL (etype1)
1794 && floatFromVal (valFromType (etype1)) >= 0)
1795 SPEC_USIGN (reType) = 1;
1798 /* promote to int */
1799 SPEC_USIGN (reType) = 0;
1800 SPEC_NOUN (reType) = V_INT;
1803 else /* etype2 signed */
1805 if ( IS_LITERAL (etype1)
1806 && floatFromVal (valFromType (etype1)) <= 127)
1807 SPEC_USIGN (reType) = 0;
1810 /* promote to int */
1811 SPEC_USIGN (reType) = 0;
1812 SPEC_NOUN (reType) = V_INT;
1818 /*------------------------------------------------------------------*/
1819 /* computeType - computes the resultant type from two types */
1820 /*------------------------------------------------------------------*/
1822 computeType (sym_link * type1, sym_link * type2,
1823 RESULT_TYPE resultType, int op)
1827 sym_link *etype1 = getSpec (type1);
1830 etype2 = type2 ? getSpec (type2) : type1;
1832 /* if one of them is a float then result is a float */
1833 /* here we assume that the types passed are okay */
1834 /* and can be cast to one another */
1835 /* which ever is greater in size */
1836 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1837 rType = newFloatLink ();
1838 /* if both are fixed16x16 then result is float */
1839 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1840 rType = newFixed16x16Link();
1841 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1842 rType = newFloatLink ();
1843 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1844 rType = newFloatLink ();
1846 /* if both are bitvars choose the larger one */
1847 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1848 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1849 copyLinkChain (type1) : copyLinkChain (type1);
1851 /* if only one of them is a bit variable then the other one prevails */
1852 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1854 rType = copyLinkChain (type2);
1855 /* bitfield can have up to 16 bits */
1856 if (getSize (etype1) > 1)
1857 SPEC_NOUN (getSpec (rType)) = V_INT;
1859 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1861 rType = copyLinkChain (type1);
1862 /* bitfield can have up to 16 bits */
1863 if (getSize (etype2) > 1)
1864 SPEC_NOUN (getSpec (rType)) = V_INT;
1866 /* if one of them is a pointer or array then that
1868 else if (IS_PTR (type1) || IS_ARRAY (type1))
1869 rType = copyLinkChain (type1);
1870 else if (IS_PTR (type2) || IS_ARRAY (type2))
1871 rType = copyLinkChain (type2);
1872 else if (getSize (type1) > getSize (type2))
1873 rType = copyLinkChain (type1);
1875 rType = copyLinkChain (type2);
1877 reType = getSpec (rType);
1879 /* avoid conflicting types */
1880 reType->select.s.b_signed = 0;
1882 /* if result is a literal then make not so */
1883 if (IS_LITERAL (reType))
1884 SPEC_SCLS (reType) = S_REGISTER;
1888 case RESULT_TYPE_CHAR:
1889 if (IS_BITVAR (reType))
1891 SPEC_NOUN (reType) = V_CHAR;
1892 SPEC_SCLS (reType) = 0;
1893 SPEC_USIGN (reType) = 0;
1897 case RESULT_TYPE_INT:
1898 case RESULT_TYPE_NONE:
1899 case RESULT_TYPE_OTHER:
1900 if (IS_BIT (reType))
1902 SPEC_NOUN (reType) = V_CHAR;
1903 SPEC_SCLS (reType) = 0;
1904 SPEC_USIGN (reType) = 0;
1907 else if (IS_BITFIELD (reType))
1909 /* could be smarter, but it depends on the op */
1910 /* this is for the worst case: a multiplication of 4 * 4 bit */
1911 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1912 SPEC_SCLS (reType) = 0;
1913 SPEC_USIGN (reType) = 0;
1916 else if (IS_CHAR (reType))
1918 /* promotion of some special cases */
1923 return computeTypeOr (etype1, etype2, reType);
1925 if (SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1927 SPEC_USIGN (reType) = 1;
1932 SPEC_NOUN (reType) = V_INT;
1933 SPEC_USIGN (reType) = 0;
1936 /* if both are unsigned char then no promotion required */
1937 if (!(SPEC_USIGN (etype1) && SPEC_USIGN (etype2)))
1939 SPEC_NOUN (reType) = V_INT;
1940 SPEC_USIGN (reType) = 0;
1953 /* SDCC's sign promotion:
1954 - if one or both operands are unsigned, the resultant type will be unsigned
1955 (except char, see below)
1956 - if an operand is promoted to a larger type (char -> int, int -> long),
1957 the larger type will be signed
1959 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1960 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1961 the standard. The standard demands, that the result has to be the same
1962 "as if" the promotion would have been performed:
1964 - if the result of an operation with two char's is promoted to a
1965 larger type, the result will be signed.
1967 More sophisticated are these:
1968 - if the result of an operation with two char's is a char again,
1969 the result will only then be unsigned, if both operands are
1970 unsigned. In all other cases the result will be signed.
1972 This seems to be contradictionary to the first two rules, but it makes
1973 real sense (all types are char's):
1975 A signed char can be negative; this must be preserved in the result
1978 Only if both operands are unsigned it's safe to make the result
1979 unsigned; this helps to avoid overflow:
1982 - ToDo: document '|', '^' and '&'
1984 Homework: - why is (200 * 200 < 0) true?
1985 - why is { char l = 200, r = 200; (r * l > 0) } true?
1988 if (!IS_FLOAT (reType)
1989 && ( (SPEC_USIGN (etype1)
1990 /* if this operand is promoted to a larger type,
1991 then it will be promoted to a signed type */
1992 && !(bitsForType (etype1) < bitsForType (reType))
1993 /* char require special handling */
1994 && !IS_CHAR (etype1))
1995 || /* same for 2nd operand */
1996 (SPEC_USIGN (etype2)
1997 && !(bitsForType (etype2) < bitsForType (reType))
1998 && !IS_CHAR (etype2))
1999 || /* if both are 'unsigned char' and not promoted
2000 let the result be unsigned too */
2001 ( SPEC_USIGN (etype1)
2002 && SPEC_USIGN (etype2)
2005 && IS_CHAR (reType))))
2006 SPEC_USIGN (reType) = 1;
2008 SPEC_USIGN (reType) = 0;
2013 /*--------------------------------------------------------------------*/
2014 /* compareType - will do type check return 1 if match, -1 if castable */
2015 /*--------------------------------------------------------------------*/
2017 compareType (sym_link * dest, sym_link * src)
2028 /* if dest is a declarator then */
2033 /* banked function pointer */
2034 if (IS_GENPTR (dest) && IS_GENPTR (src))
2036 if (IS_FUNC (src->next) && IS_VOID(dest->next))
2038 if (IS_FUNC (dest->next) && IS_VOID(src->next))
2040 return compareType (dest->next, src->next);
2043 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2045 //checkFunction(src,dest);
2047 return compareType (dest->next, src->next);
2049 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
2053 (IS_GENPTR (dest) ||
2054 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2057 if (IS_PTR (dest) && IS_ARRAY (src)) {
2058 value *val=aggregateToPointer (valFromType(src));
2059 int res=compareType (dest, val->type);
2060 Safe_free(val->type);
2064 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2065 return compareType (dest->next, src);
2068 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2074 /* if one is a specifier and the other is not */
2075 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2076 (IS_SPEC (dest) && !IS_SPEC (src)))
2079 /* if one of them is a void then ok */
2080 if (SPEC_NOUN (dest) == V_VOID &&
2081 SPEC_NOUN (src) != V_VOID)
2084 if (SPEC_NOUN (dest) != V_VOID &&
2085 SPEC_NOUN (src) == V_VOID)
2088 /* if they are both bitfields then if the lengths
2089 and starts don't match */
2090 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2091 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2092 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2095 /* it is a specifier */
2096 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2098 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2099 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2101 bitsForType (dest) == bitsForType (src))
2102 instead of the next two lines, but the regression tests fail with
2103 them; I guess it's a problem with replaceCheaperOp */
2104 getSize (dest) == getSize (src) &&
2105 !(!IS_BIT (dest) && IS_BIT (src)))
2107 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2112 else if (IS_STRUCT (dest))
2114 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2119 if (SPEC_LONG (dest) != SPEC_LONG (src))
2122 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2128 /*--------------------------------------------------------------------*/
2129 /* compareTypeExact - will do type check return 1 if match exactly */
2130 /*--------------------------------------------------------------------*/
2132 compareTypeExact (sym_link * dest, sym_link * src, int level)
2134 STORAGE_CLASS srcScls, destScls;
2145 /* if dest is a declarator then */
2150 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2151 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2153 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2155 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2159 value *exargs, *acargs, *checkValue;
2161 /* verify function return type */
2162 if (!compareTypeExact (dest->next, src->next, -1))
2164 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2166 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2168 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2171 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2175 /* compare expected args with actual args */
2176 exargs = FUNC_ARGS(dest);
2177 acargs = FUNC_ARGS(src);
2179 /* for all the expected args do */
2180 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2182 //checkTypeSanity(acargs->etype, acargs->name);
2184 if (IS_AGGREGATE (acargs->type))
2186 checkValue = copyValue (acargs);
2187 aggregateToPointer (checkValue);
2190 checkValue = acargs;
2193 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2198 /* if one them ended we have a problem */
2199 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2200 (!exargs && acargs && !IS_VOID (acargs->type)))
2204 return compareTypeExact (dest->next, src->next, level);
2211 /* if one is a specifier and the other is not */
2212 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2213 (IS_SPEC (dest) && !IS_SPEC (src)))
2216 /* if one of them is a void then ok */
2217 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2220 /* if they are both bitfields then if the lengths
2221 and starts don't match */
2222 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2223 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2224 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2227 if (IS_INTEGRAL (dest))
2229 /* signedness must match */
2230 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2232 /* size must match */
2233 if (SPEC_LONG (dest) != SPEC_LONG (src))
2235 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2239 if (IS_STRUCT (dest))
2241 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2245 if (SPEC_CONST (dest) != SPEC_CONST (src))
2247 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2249 if (SPEC_STAT (dest) != SPEC_STAT (src))
2251 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2253 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2256 destScls = SPEC_SCLS (dest);
2257 srcScls = SPEC_SCLS (src);
2259 /* Compensate for const to const code change in checkSClass() */
2260 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2262 if (srcScls == S_CODE && destScls == S_FIXED)
2264 if (destScls == S_CODE && srcScls == S_FIXED)
2268 /* compensate for allocGlobal() */
2269 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2270 && port->mem.default_globl_map == xdata
2274 if (level>0 && !SPEC_STAT (dest))
2276 /* Compensate for hack-o-matic in checkSClass() */
2277 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2279 if (destScls == S_FIXED)
2280 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2281 if (srcScls == S_FIXED)
2282 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2284 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2286 if (destScls == S_FIXED)
2288 if (srcScls == S_FIXED)
2293 if (srcScls != destScls)
2296 printf ("level = %d\n", level);
2297 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2298 SPEC_SCLS (src), SPEC_SCLS (dest));
2299 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2307 /*------------------------------------------------------------------*/
2308 /* inCalleeSaveList - return 1 if found in callee save list */
2309 /*------------------------------------------------------------------*/
2311 calleeCmp(void *p1, void *p2)
2313 return (strcmp((char *)p1, (char *)(p2)) == 0);
2317 inCalleeSaveList(char *s)
2319 if (options.all_callee_saves)
2321 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2324 /*-----------------------------------------------------------------*/
2325 /* aggregateToPointer: change an agggregate type function */
2326 /* argument to a pointer to that type. */
2327 /*-----------------------------------------------------------------*/
2329 aggregateToPointer (value * val)
2331 if (IS_AGGREGATE (val->type))
2333 /* if this is a structure */
2334 /* then we need to add a new link */
2335 if (IS_STRUCT (val->type))
2337 /* first lets add DECLARATOR type */
2338 sym_link *p = val->type;
2340 werror (W_STRUCT_AS_ARG, val->name);
2341 val->type = newLink (DECLARATOR);
2342 val->type->next = p;
2345 /* change to a pointer depending on the */
2346 /* storage class specified */
2347 switch (SPEC_SCLS (val->etype))
2350 DCL_TYPE (val->type) = IPOINTER;
2353 DCL_TYPE (val->type) = PPOINTER;
2356 if (SPEC_OCLS(val->etype)) {
2357 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2359 // this happens for (external) function parameters
2360 DCL_TYPE (val->type) = port->unqualified_pointer;
2366 DCL_TYPE (val->type) = POINTER;
2369 DCL_TYPE (val->type) = CPOINTER;
2372 DCL_TYPE (val->type) = FPOINTER;
2375 DCL_TYPE (val->type) = EEPPOINTER;
2378 DCL_TYPE (val->type) = port->unqualified_pointer;
2381 /* is there is a symbol associated then */
2382 /* change the type of the symbol as well */
2385 val->sym->type = copyLinkChain (val->type);
2386 val->sym->etype = getSpec (val->sym->type);
2391 /*------------------------------------------------------------------*/
2392 /* checkFunction - does all kinds of check on a function */
2393 /*------------------------------------------------------------------*/
2395 checkFunction (symbol * sym, symbol *csym)
2397 value *exargs, *acargs;
2401 if (getenv("DEBUG_SANITY")) {
2402 fprintf (stderr, "checkFunction: %s ", sym->name);
2405 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2407 werror(E_SYNTAX_ERROR, sym->name);
2411 /* make sure the type is complete and sane */
2412 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2414 /* if not type then some kind of error */
2418 /* if the function has no type then make it return int */
2419 if (!sym->type->next)
2420 sym->type->next = sym->etype = newIntLink ();
2422 /* function cannot return aggregate */
2423 if (IS_AGGREGATE (sym->type->next))
2425 werror (E_FUNC_AGGR, sym->name);
2429 /* check if this function is defined as calleeSaves
2430 then mark it as such */
2431 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2433 /* if interrupt service routine */
2434 /* then it cannot have arguments */
2435 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2437 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2438 werror (E_INT_ARGS, sym->name);
2439 FUNC_ARGS(sym->type)=NULL;
2443 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2445 werror (E_SHADOWREGS_NO_ISR, sym->name);
2449 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2451 acargs=acargs->next, argCnt++) {
2453 // this can happen for reentrant functions
2454 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2455 // the show must go on: synthesize a name and symbol
2456 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2457 acargs->sym = newSymbol (acargs->name, 1);
2458 SPEC_OCLS (acargs->etype) = istack;
2459 acargs->sym->type = copyLinkChain (acargs->type);
2460 acargs->sym->etype = getSpec (acargs->sym->type);
2461 acargs->sym->_isparm = 1;
2462 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2463 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2465 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2470 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2471 return 1; /* not defined nothing more to check */
2473 /* check if body already present */
2474 if (csym && IFFUNC_HASBODY(csym->type))
2476 werror (E_FUNC_BODY, sym->name);
2480 /* check the return value type */
2481 if (compareType (csym->type, sym->type) <= 0)
2483 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2484 printFromToType(csym->type, sym->type);
2488 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2490 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2493 /* I don't think this is necessary for interrupts. An isr is a */
2494 /* root in the calling tree. */
2495 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2496 (!FUNC_ISISR (sym->type)))
2498 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2501 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2503 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2506 /* Really, reentrant should match regardless of argCnt, but */
2507 /* this breaks some existing code (the fp lib functions). If */
2508 /* the first argument is always passed the same way, this */
2509 /* lax checking is ok (but may not be true for in future ports) */
2510 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2513 //printf("argCnt = %d\n",argCnt);
2514 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2517 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2519 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2522 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2524 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2528 /* compare expected args with actual args */
2529 exargs = FUNC_ARGS(csym->type);
2530 acargs = FUNC_ARGS(sym->type);
2532 /* for all the expected args do */
2535 exargs = exargs->next, acargs = acargs->next, argCnt++)
2537 if (getenv("DEBUG_SANITY")) {
2538 fprintf (stderr, "checkFunction: %s ", exargs->name);
2540 /* make sure the type is complete and sane */
2541 checkTypeSanity(exargs->etype, exargs->name);
2543 /* If the actual argument is an array, any prototype
2544 * will have modified it to a pointer. Duplicate that
2547 if (IS_AGGREGATE (acargs->type))
2549 checkValue = copyValue (acargs);
2550 aggregateToPointer (checkValue);
2554 checkValue = acargs;
2557 if (compareType (exargs->type, checkValue->type) <= 0)
2559 werror (E_ARG_TYPE, argCnt);
2560 printFromToType(exargs->type, checkValue->type);
2565 /* if one them ended we have a problem */
2566 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2567 (!exargs && acargs && !IS_VOID (acargs->type)))
2568 werror (E_ARG_COUNT);
2570 /* replace with this defition */
2571 sym->cdef = csym->cdef;
2572 deleteSym (SymbolTab, csym, csym->name);
2573 deleteFromSeg(csym);
2574 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2575 if (IS_EXTERN (csym->etype) && !
2576 IS_EXTERN (sym->etype))
2578 addSet (&publics, sym);
2583 /*------------------------------------------------------------------*/
2584 /* cdbStructBlock - calls struct printing for a blcks */
2585 /*------------------------------------------------------------------*/
2586 void cdbStructBlock (int block)
2589 bucket **table = StructTab;
2592 /* go thru the entire table */
2593 for (i = 0; i < 256; i++)
2595 for (chain = table[i]; chain; chain = chain->next)
2597 if (chain->block >= block)
2600 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2606 /*-----------------------------------------------------------------*/
2607 /* processFuncPtrArgs - does some processing with args of func ptrs*/
2608 /*-----------------------------------------------------------------*/
2610 processFuncPtrArgs (sym_link * funcType)
2612 value *val = FUNC_ARGS(funcType);
2614 /* if it is void then remove parameters */
2615 if (val && IS_VOID (val->type))
2617 FUNC_ARGS(funcType) = NULL;
2622 /*-----------------------------------------------------------------*/
2623 /* processFuncArgs - does some processing with function args */
2624 /*-----------------------------------------------------------------*/
2626 processFuncArgs (symbol * func)
2630 sym_link *funcType=func->type;
2632 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2633 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2635 /* find the function declaration within the type */
2636 while (funcType && !IS_FUNC(funcType))
2637 funcType=funcType->next;
2639 /* if this function has variable argument list */
2640 /* then make the function a reentrant one */
2641 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2642 FUNC_ISREENT(funcType)=1;
2644 /* check if this function is defined as calleeSaves
2645 then mark it as such */
2646 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2648 /* loop thru all the arguments */
2649 val = FUNC_ARGS(funcType);
2651 /* if it is void then remove parameters */
2652 if (val && IS_VOID (val->type))
2654 FUNC_ARGS(funcType) = NULL;
2658 /* reset regparm for the port */
2659 (*port->reset_regparms) ();
2661 /* if any of the arguments is an aggregate */
2662 /* change it to pointer to the same type */
2666 char buffer[SDCC_NAME_MAX+1];
2668 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2669 checkTypeSanity (val->etype, buffer);
2671 /* mark it as a register parameter if
2672 the function does not have VA_ARG
2673 and as port dictates */
2674 if (!IFFUNC_HASVARARGS(funcType) &&
2675 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2677 SPEC_REGPARM (val->etype) = 1;
2678 SPEC_ARGREG(val->etype) = argreg;
2679 } else if (IFFUNC_ISREENT(funcType)) {
2680 FUNC_HASSTACKPARM(funcType) = 1;
2683 if (IS_AGGREGATE (val->type))
2685 aggregateToPointer (val);
2692 /* if this is an internal generated function call */
2694 /* ignore --stack-auto for this one, we don't know how it is compiled */
2695 /* simply trust on --int-long-reent or --float-reent */
2696 if (IFFUNC_ISREENT(funcType)) {
2700 /* if this function is reentrant or */
2701 /* automatics r 2b stacked then nothing */
2702 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2706 val = FUNC_ARGS(funcType);
2711 /* if a symbolname is not given */
2712 /* synthesize a variable name */
2715 SNPRINTF (val->name, sizeof(val->name),
2716 "_%s_PARM_%d", func->name, pNum++);
2717 val->sym = newSymbol (val->name, 1);
2718 if (SPEC_SCLS(val->etype) == S_BIT)
2719 SPEC_OCLS (val->etype) = bit;
2721 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2722 val->sym->type = copyLinkChain (val->type);
2723 val->sym->etype = getSpec (val->sym->type);
2724 val->sym->_isparm = 1;
2725 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2727 /* ?? static functions shouldn't imply static parameters - EEP */
2728 if (IS_SPEC(func->etype)) {
2729 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2730 SPEC_STAT (func->etype);
2733 addSymChain (&val->sym);
2736 else /* symbol name given create synth name */
2739 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2740 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2741 val->sym->_isparm = 1;
2742 if (SPEC_SCLS(val->etype) == S_BIT)
2743 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2745 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2746 port->mem.default_local_map;
2749 /* ?? static functions shouldn't imply static parameters - EEP */
2750 if (IS_SPEC(func->etype)) {
2751 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2752 SPEC_STAT (func->etype);
2756 if (SPEC_OCLS (val->sym->etype) == pdata)
2757 val->sym->iaccess = 1;
2758 if (!isinSet(operKeyReset, val->sym)) {
2759 addSet (&operKeyReset, val->sym);
2760 applyToSet (operKeyReset, resetParmKey);
2766 /*-----------------------------------------------------------------*/
2767 /* isSymbolEqual - compares two symbols return 1 if they match */
2768 /*-----------------------------------------------------------------*/
2770 isSymbolEqual (symbol * dest, symbol * src)
2772 /* if pointers match then equal */
2776 /* if one of them is null then don't match */
2780 /* if both of them have rname match on rname */
2781 if (dest->rname[0] && src->rname[0])
2782 return (!strcmp (dest->rname, src->rname));
2784 /* otherwise match on name */
2785 return (!strcmp (dest->name, src->name));
2788 void PT(sym_link *type)
2790 printTypeChain(type,0);
2792 /*-----------------------------------------------------------------*/
2793 /* printTypeChain - prints the type chain in human readable form */
2794 /*-----------------------------------------------------------------*/
2796 printTypeChain (sym_link * start, FILE * of)
2800 sym_link * type, * search;
2810 fprintf (of, "void");
2814 /* Print the chain as it is written in the source: */
2815 /* start with the last entry. */
2816 /* However, the storage class at the end of the */
2817 /* chain reall applies to the first in the chain! */
2819 for (type = start; type && type->next; type = type->next)
2822 scls=SPEC_SCLS(type);
2830 case S_DATA: fprintf (of, "data-"); break;
2831 case S_XDATA: fprintf (of, "xdata-"); break;
2832 case S_SFR: fprintf (of, "sfr-"); break;
2833 case S_SBIT: fprintf (of, "sbit-"); break;
2834 case S_CODE: fprintf (of, "code-"); break;
2835 case S_IDATA: fprintf (of, "idata-"); break;
2836 case S_PDATA: fprintf (of, "pdata-"); break;
2837 case S_LITERAL: fprintf (of, "literal-"); break;
2838 case S_STACK: fprintf (of, "stack-"); break;
2839 case S_XSTACK: fprintf (of, "xstack-"); break;
2840 case S_BIT: fprintf (of, "bit-"); break;
2841 case S_EEPROM: fprintf (of, "eeprom-"); break;
2848 if (!IS_FUNC(type)) {
2849 if (DCL_PTR_VOLATILE (type)) {
2850 fprintf (of, "volatile-");
2852 if (DCL_PTR_CONST (type)) {
2853 fprintf (of, "const-");
2856 switch (DCL_TYPE (type))
2859 fprintf (of, "function %s %s",
2860 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2861 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2863 for (args = FUNC_ARGS(type);
2866 printTypeChain(args->type, of);
2873 fprintf (of, "generic* ");
2876 fprintf (of, "code* ");
2879 fprintf (of, "xdata* ");
2882 fprintf (of, "eeprom* ");
2885 fprintf (of, "near* ");
2888 fprintf (of, "idata* ");
2891 fprintf (of, "pdata* ");
2894 fprintf (of, "unknown* ");
2897 if (DCL_ELEM(type)) {
2898 fprintf (of, "[%d] ", DCL_ELEM(type));
2900 fprintf (of, "[] ");
2907 if (SPEC_VOLATILE (type))
2908 fprintf (of, "volatile-");
2909 if (SPEC_CONST (type))
2910 fprintf (of, "const-");
2911 if (SPEC_USIGN (type))
2912 fprintf (of, "unsigned-");
2913 switch (SPEC_NOUN (type))
2917 fprintf (of, "long-");
2918 fprintf (of, "int");
2922 fprintf (of, "char");
2926 fprintf (of, "void");
2930 fprintf (of, "float");
2934 fprintf (of, "fixed16x16");
2938 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2942 fprintf (of, "sbit");
2946 fprintf (of, "bit");
2950 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2954 fprintf (of, "double");
2958 fprintf (of, "unknown type");
2962 /* search entry in list before "type" */
2963 for (search = start; search && search->next != type;)
2964 search = search->next;
2973 /*--------------------------------------------------------------------*/
2974 /* printTypeChainRaw - prints the type chain in human readable form */
2975 /* in the raw data structure ordering */
2976 /*--------------------------------------------------------------------*/
2978 printTypeChainRaw (sym_link * start, FILE * of)
2991 fprintf (of, "void");
3001 if (!IS_FUNC(type)) {
3002 if (DCL_PTR_VOLATILE (type)) {
3003 fprintf (of, "volatile-");
3005 if (DCL_PTR_CONST (type)) {
3006 fprintf (of, "const-");
3009 switch (DCL_TYPE (type))
3012 fprintf (of, "function %s %s",
3013 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
3014 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
3016 for (args = FUNC_ARGS(type);
3019 printTypeChain(args->type, of);
3026 fprintf (of, "generic* ");
3029 fprintf (of, "code* ");
3032 fprintf (of, "xdata* ");
3035 fprintf (of, "eeprom* ");
3038 fprintf (of, "near* ");
3041 fprintf (of, "idata* ");
3044 fprintf (of, "pdata* ");
3047 fprintf (of, "unknown* ");
3050 if (DCL_ELEM(type)) {
3051 fprintf (of, "[%d] ", DCL_ELEM(type));
3053 fprintf (of, "[] ");
3057 if (DCL_TSPEC(type))
3060 printTypeChainRaw(DCL_TSPEC(type), of);
3064 else if (IS_SPEC (type))
3066 switch (SPEC_SCLS (type))
3068 case S_DATA: fprintf (of, "data-"); break;
3069 case S_XDATA: fprintf (of, "xdata-"); break;
3070 case S_SFR: fprintf (of, "sfr-"); break;
3071 case S_SBIT: fprintf (of, "sbit-"); break;
3072 case S_CODE: fprintf (of, "code-"); break;
3073 case S_IDATA: fprintf (of, "idata-"); break;
3074 case S_PDATA: fprintf (of, "pdata-"); break;
3075 case S_LITERAL: fprintf (of, "literal-"); break;
3076 case S_STACK: fprintf (of, "stack-"); break;
3077 case S_XSTACK: fprintf (of, "xstack-"); break;
3078 case S_BIT: fprintf (of, "bit-"); break;
3079 case S_EEPROM: fprintf (of, "eeprom-"); break;
3082 if (SPEC_VOLATILE (type))
3083 fprintf (of, "volatile-");
3084 if (SPEC_CONST (type))
3085 fprintf (of, "const-");
3086 if (SPEC_USIGN (type))
3087 fprintf (of, "unsigned-");
3088 switch (SPEC_NOUN (type))
3092 fprintf (of, "long-");
3093 fprintf (of, "int");
3097 fprintf (of, "char");
3101 fprintf (of, "void");
3105 fprintf (of, "float");
3109 fprintf (of, "fixed16x16");
3113 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3117 fprintf (of, "sbit");
3121 fprintf (of, "bit");
3125 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3129 fprintf (of, "double");
3133 fprintf (of, "unknown type");
3138 fprintf (of, "NOT_SPEC_OR_DECL");
3148 /*-----------------------------------------------------------------*/
3149 /* powof2 - returns power of two for the number if number is pow 2 */
3150 /*-----------------------------------------------------------------*/
3152 powof2 (TYPE_TARGET_ULONG num)
3165 if (n1s > 1 || nshifts == 0)
3181 symbol *__fps16x16_add;
3182 symbol *__fps16x16_sub;
3183 symbol *__fps16x16_mul;
3184 symbol *__fps16x16_div;
3185 symbol *__fps16x16_eq;
3186 symbol *__fps16x16_neq;
3187 symbol *__fps16x16_lt;
3188 symbol *__fps16x16_lteq;
3189 symbol *__fps16x16_gt;
3190 symbol *__fps16x16_gteq;
3192 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3193 symbol *__muldiv[3][3][2];
3194 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3195 sym_link *__multypes[3][2];
3196 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3197 symbol *__conv[2][3][2];
3198 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3199 symbol *__fp16x16conv[2][4][2];
3200 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3201 symbol *__rlrr[2][3][2];
3203 sym_link *floatType;
3204 sym_link *fixed16x16Type;
3207 _mangleFunctionName(char *in)
3209 if (port->getMangledFunctionName)
3211 return port->getMangledFunctionName(in);
3219 /*-----------------------------------------------------------------*/
3220 /* typeFromStr - create a typechain from an encoded string */
3221 /* basic types - 'c' - char */
3226 /* 'q' - fixed16x16 */
3228 /* '*' - pointer - default (GPOINTER) */
3229 /* modifiers - 'u' - unsigned */
3230 /* pointer modifiers - 'g' - generic */
3234 /* 'F' - function */
3235 /* examples : "ig*" - generic int * */
3236 /* "cx*" - char xdata * */
3237 /* "ui" - unsigned int */
3238 /*-----------------------------------------------------------------*/
3239 sym_link *typeFromStr (char *s)
3241 sym_link *r = newLink(DECLARATOR);
3253 r->class = SPECIFIER;
3254 SPEC_NOUN(r) = V_CHAR;
3258 r->class = SPECIFIER;
3259 SPEC_NOUN(r) = V_INT;
3262 r->class = SPECIFIER;
3263 SPEC_NOUN(r) = V_INT;
3267 r->class = SPECIFIER;
3268 SPEC_NOUN(r) = V_FLOAT;
3271 r->class = SPECIFIER;
3272 SPEC_NOUN(r) = V_FIXED16X16;
3275 r->class = SPECIFIER;
3276 SPEC_NOUN(r) = V_VOID;
3279 DCL_TYPE(r) = port->unqualified_pointer;
3286 assert(*(s+1)=='*');
3287 nr = newLink(DECLARATOR);
3292 DCL_TYPE(r) = GPOINTER;
3295 DCL_TYPE(r) = FPOINTER;
3298 DCL_TYPE(r) = CPOINTER;
3301 DCL_TYPE(r) = POINTER;
3304 DCL_TYPE(r) = FUNCTION;
3305 nr = newLink(DECLARATOR);
3308 DCL_TYPE(r) = CPOINTER;
3314 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3315 "typeFromStr: unknown type");
3318 if (IS_SPEC(r) && usign) {
3327 /*-----------------------------------------------------------------*/
3328 /* initCSupport - create functions for C support routines */
3329 /*-----------------------------------------------------------------*/
3333 const char *smuldivmod[] =
3337 const char *sbwd[] =
3339 "char", "int", "long", "fixed16x16",
3341 const char *fp16x16sbwd[] =
3343 "char", "int", "long", "float",
3349 const char *srlrr[] =
3354 int bwd, su, muldivmod, tofrom, rlrr;
3356 if (getenv("SDCC_NO_C_SUPPORT")) {
3357 /* for debugging only */
3361 floatType = newFloatLink ();
3362 fixed16x16Type = newFixed16x16Link ();
3364 for (bwd = 0; bwd < 3; bwd++)
3381 __multypes[bwd][0] = l;
3382 __multypes[bwd][1] = copyLinkChain (l);
3383 SPEC_USIGN (__multypes[bwd][1]) = 1;
3386 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3387 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3388 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3389 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3390 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3391 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3392 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3393 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3394 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3395 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3397 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3398 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3399 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3400 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3401 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3402 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3403 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3404 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3405 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3406 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3409 for (tofrom = 0; tofrom < 2; tofrom++)
3411 for (bwd = 0; bwd < 3; bwd++)
3413 for (su = 0; su < 2; su++)
3417 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3418 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3422 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3423 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3429 for (tofrom = 0; tofrom < 2; tofrom++)
3431 for (bwd = 0; bwd < 4; bwd++)
3433 for (su = 0; su < 2; su++)
3437 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3439 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3441 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3445 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3447 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3449 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3456 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3458 for (bwd = 0; bwd < 3; bwd++)
3460 for (su = 0; su < 2; su++)
3462 SNPRINTF (buffer, sizeof(buffer),
3464 smuldivmod[muldivmod],
3467 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3468 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3473 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3474 Therefore they've been merged into mulint() and mullong().
3477 for (bwd = 0; bwd < 3; bwd++)
3479 for (su = 0; su < 2; su++)
3481 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3483 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
3484 if (!TARGET_IS_PIC16 || muldivmod != 1 || bwd != 0 || su != 0)
3486 SNPRINTF (buffer, sizeof(buffer),
3488 smuldivmod[muldivmod],
3491 __muldiv[muldivmod][bwd][su] = funcOfType (
3492 _mangleFunctionName(buffer),
3493 __multypes[bwd][su],
3494 __multypes[bwd][su],
3496 options.intlong_rent);
3497 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3503 if (TARGET_IS_PIC16)
3505 /* PIC16 port wants __divschar/__modschar to return an int, so that both
3506 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
3507 * (first one would have to be sign extended, second one must not be).
3508 * Similarly, modschar should be handled, but the iCode introduces cast
3509 * here and forces '% : s8 x s8 -> s8' ... */
3511 for (muldivmod = 1; muldivmod < 2; muldivmod++) {
3512 SNPRINTF (buffer, sizeof(buffer),
3514 smuldivmod[muldivmod],
3517 __muldiv[muldivmod][bwd][su] = funcOfType (
3518 _mangleFunctionName(buffer),
3520 __multypes[bwd][su],
3522 options.intlong_rent);
3523 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3531 for (su = 0; su < 2; su++)
3533 /* muluchar and mulschar are still separate functions, because e.g. the z80
3534 port is sign/zero-extending to int before calling mulint() */
3535 SNPRINTF (buffer, sizeof(buffer),
3537 smuldivmod[muldivmod],
3540 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3541 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3545 /* word and doubleword */
3546 for (bwd = 1; bwd < 3; bwd++)
3549 SNPRINTF (buffer, sizeof(buffer),
3551 smuldivmod[muldivmod],
3553 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3554 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3555 /* signed = unsigned */
3556 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3559 for (rlrr = 0; rlrr < 2; rlrr++)
3561 for (bwd = 0; bwd < 3; bwd++)
3563 for (su = 0; su < 2; su++)
3565 SNPRINTF (buffer, sizeof(buffer),
3570 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3571 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3577 /*-----------------------------------------------------------------*/
3578 /* initBuiltIns - create prototypes for builtin functions */
3579 /*-----------------------------------------------------------------*/
3585 if (!port->builtintable) return ;
3587 for (i = 0 ; port->builtintable[i].name ; i++) {
3588 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3589 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3590 FUNC_ISBUILTIN(sym->type) = 1;
3591 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3595 sym_link *validateLink(sym_link *l,
3602 if (l && l->class==select)
3607 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3608 " expected %s, got %s\n",
3609 macro, args, file, line,
3610 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3612 return l; // never reached, makes compiler happy.
3615 /*--------------------------------------------------------------------*/
3616 /* newEnumType - create an integer type compatible with enumerations */
3617 /*--------------------------------------------------------------------*/
3619 newEnumType (symbol *enumlist)
3627 type = newLink (SPECIFIER);
3628 SPEC_NOUN (type) = V_INT;
3632 /* Determine the range of the enumerated values */
3634 min = max = (int) floatFromVal (valFromType (sym->type));
3635 for (sym = sym->next; sym; sym = sym->next)
3637 v = (int) floatFromVal (valFromType (sym->type));
3644 /* Determine the smallest integer type that is compatible with this range */
3645 type = newLink (SPECIFIER);
3646 if (min>=0 && max<=255)
3648 SPEC_NOUN (type) = V_CHAR;
3649 SPEC_USIGN (type) = 1;
3651 else if (min>=-128 && max<=127)
3653 SPEC_NOUN (type) = V_CHAR;
3655 else if (min>=0 && max<=65535)
3657 SPEC_NOUN (type) = V_INT;
3658 SPEC_USIGN (type) = 1;
3660 else if (min>=-32768 && max<=32767)
3662 SPEC_NOUN (type) = V_INT;
3666 SPEC_NOUN (type) = V_INT;
3667 SPEC_LONG (type) = 1;
3669 SPEC_USIGN (type) = 1;