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 = lineno; /* set the line number */
300 sym->fileDef = filename;
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_CONST(dest) |= SPEC_CONST (src);
659 SPEC_ABSA (dest) |= SPEC_ABSA (src);
660 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
661 SPEC_RESTRICT (dest) |= SPEC_RESTRICT (src);
662 SPEC_ADDR (dest) |= SPEC_ADDR (src);
663 SPEC_OCLS (dest) = SPEC_OCLS (src);
664 SPEC_BLEN (dest) |= SPEC_BLEN (src);
665 SPEC_BSTR (dest) |= SPEC_BSTR (src);
666 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
667 SPEC_ENUM (dest) |= SPEC_ENUM (src);
668 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
669 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
671 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
672 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
674 /* these are the only function attributes that will be set
675 in a specifier while parsing */
676 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
677 FUNC_BANKED(dest) |= FUNC_BANKED(src);
678 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
679 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
680 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
681 FUNC_ISISR(dest) |= FUNC_ISISR(src);
682 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
683 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
684 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
685 FUNC_INTNO(dest) |= FUNC_INTNO(src);
686 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
691 /*------------------------------------------------------------------*/
692 /* genSymName - generates and returns a name used for anonymous vars */
693 /*------------------------------------------------------------------*/
695 genSymName (int level)
697 static int gCount = 0;
698 static char gname[SDCC_NAME_MAX + 1];
700 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
704 /*------------------------------------------------------------------*/
705 /* getSpec - returns the specifier part from a declaration chain */
706 /*------------------------------------------------------------------*/
708 getSpec (sym_link * p)
713 while (p && !(IS_SPEC (p)))
719 /*------------------------------------------------------------------*/
720 /* newCharLink() - creates an char type */
721 /*------------------------------------------------------------------*/
727 p = newLink (SPECIFIER);
728 SPEC_NOUN (p) = V_CHAR;
733 /*------------------------------------------------------------------*/
734 /* newFloatLink - a new Float type */
735 /*------------------------------------------------------------------*/
741 p = newLink (SPECIFIER);
742 SPEC_NOUN (p) = V_FLOAT;
747 /*------------------------------------------------------------------*/
748 /* newFixed16x16Link - a new Float type */
749 /*------------------------------------------------------------------*/
755 p = newLink (SPECIFIER);
756 SPEC_NOUN (p) = V_FIXED16X16;
761 /*------------------------------------------------------------------*/
762 /* newLongLink() - new long type */
763 /*------------------------------------------------------------------*/
769 p = newLink (SPECIFIER);
770 SPEC_NOUN (p) = V_INT;
776 /*------------------------------------------------------------------*/
777 /* newIntLink() - creates an int type */
778 /*------------------------------------------------------------------*/
784 p = newLink (SPECIFIER);
785 SPEC_NOUN (p) = V_INT;
790 /*------------------------------------------------------------------*/
791 /* newBoolLink() - creates an bool type */
792 /*------------------------------------------------------------------*/
798 p = newLink (SPECIFIER);
799 SPEC_NOUN (p) = V_BIT;
804 /*------------------------------------------------------------------*/
805 /* getSize - returns size of a type chain in bytes */
806 /*------------------------------------------------------------------*/
808 getSize (sym_link * p)
810 /* if nothing return 0 */
814 { /* if this is the specifier then */
815 switch (SPEC_NOUN (p))
816 { /* depending on the specifier type */
818 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
828 return SPEC_STRUCT (p)->size;
835 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
841 /* this is a declarator */
842 switch (DCL_TYPE (p))
846 return DCL_ELEM (p) * getSize (p->next);
848 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
849 // "can not tell the size of an array[]");
860 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
869 /*------------------------------------------------------------------*/
870 /* checkStructFlexArray - check tree behind a struct */
871 /*------------------------------------------------------------------*/
873 checkStructFlexArray (symbol *sym, sym_link *p)
875 /* if nothing return FALSE */
881 /* (nested) struct with flexible array member? */
882 if (IS_STRUCT (p) && SPEC_STRUCT (p)->b_flexArrayMember)
884 werror (W_INVALID_FLEXARRAY);
890 /* this is a declarator */
893 /* flexible array member? */
896 if (!options.std_c99)
897 werror (W_C89_NO_FLEXARRAY);
901 return checkStructFlexArray (sym, p->next);
906 /*------------------------------------------------------------------*/
907 /* bitsForType - returns # of bits required to store this type */
908 /*------------------------------------------------------------------*/
910 bitsForType (sym_link * p)
912 /* if nothing return 0 */
917 { /* if this is the specifier then */
919 switch (SPEC_NOUN (p))
920 { /* depending on the specifier type */
922 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
924 return FLOATSIZE * 8;
932 return SPEC_STRUCT (p)->size * 8;
939 return SPEC_BLEN (p);
945 /* this is a specifier */
946 switch (DCL_TYPE (p))
949 return DCL_ELEM (p) * getSize (p->next) * 8;
953 return (PTRSIZE * 8);
958 return (FPTRSIZE * 8);
960 return (GPTRSIZE * 8);
967 /*------------------------------------------------------------------*/
968 /* copySymbolChain - copies a symbol chain */
969 /*------------------------------------------------------------------*/
971 copySymbolChain (symbol * src)
978 dest = copySymbol (src);
979 dest->next = copySymbolChain (src->next);
983 /*------------------------------------------------------------------*/
984 /* copySymbol - makes a copy of a symbol */
985 /*------------------------------------------------------------------*/
987 copySymbol (symbol * src)
994 dest = newSymbol (src->name, src->level);
995 memcpy (dest, src, sizeof (symbol));
996 dest->level = src->level;
997 dest->block = src->block;
998 dest->ival = copyIlist (src->ival);
999 dest->type = copyLinkChain (src->type);
1000 dest->etype = getSpec (dest->type);
1002 dest->key = src->key;
1003 dest->allocreq = src->allocreq;
1007 /*------------------------------------------------------------------*/
1008 /* reverseSyms - reverses the links for a symbol chain */
1009 /*------------------------------------------------------------------*/
1011 reverseSyms (symbol * sym)
1013 symbol *prev, *curr, *next;
1028 sym->next = (void *) NULL;
1032 /*------------------------------------------------------------------*/
1033 /* reverseLink - reverses the links for a type chain */
1034 /*------------------------------------------------------------------*/
1036 reverseLink (sym_link * type)
1038 sym_link *prev, *curr, *next;
1053 type->next = (void *) NULL;
1057 /*------------------------------------------------------------------*/
1058 /* addSymChain - adds a symbol chain to the symboltable */
1059 /*------------------------------------------------------------------*/
1061 addSymChain (symbol ** symHead)
1063 symbol *sym = *symHead;
1064 symbol *csym = NULL;
1068 for (; sym != NULL; sym = sym->next)
1070 changePointer(sym->type);
1071 checkTypeSanity(sym->etype, sym->name);
1073 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1076 /* if already exists in the symbol table then check if
1077 one of them is an extern definition if yes then
1078 then check if the type match, if the types match then
1079 delete the current entry and add the new entry */
1080 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1081 csym->level == sym->level) {
1083 /* If the previous definition was for an array with incomplete */
1084 /* type, and the new definition has completed the type, update */
1085 /* the original type to match */
1086 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1087 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1089 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1090 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1094 /* If only one of the definitions used the "at" keyword, copy */
1095 /* the address to the other. */
1096 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1097 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1099 SPEC_ABSA (sym->etype) = 1;
1100 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1102 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1103 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1105 SPEC_ABSA (csym->etype) = 1;
1106 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1111 if (csym->ival && sym->ival)
1113 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1117 /* one definition extern ? */
1118 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1119 werror (E_EXTERN_MISMATCH, sym->name);
1121 werror (E_DUPLICATE, sym->name);
1122 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1124 fprintf (stderr, "from type '");
1125 printTypeChain (csym->type, stderr);
1126 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1127 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1128 fprintf (stderr, "'\nto type '");
1129 printTypeChain (sym->type, stderr);
1130 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1131 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1132 fprintf (stderr, "'\n");
1137 if (csym->ival && !sym->ival)
1138 sym->ival = csym->ival;
1140 /* delete current entry */
1141 deleteSym (SymbolTab, csym, csym->name);
1142 deleteFromSeg(csym);
1144 symPtrPtr = symHead;
1145 while (*symPtrPtr && *symPtrPtr != csym)
1146 symPtrPtr = &(*symPtrPtr)->next;
1147 if (*symPtrPtr == csym)
1148 *symPtrPtr = csym->next;
1153 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1158 /*------------------------------------------------------------------*/
1159 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1160 /*------------------------------------------------------------------*/
1162 funcInChain (sym_link * lnk)
1173 /*------------------------------------------------------------------*/
1174 /* structElemType - returns the type info of a struct member */
1175 /*------------------------------------------------------------------*/
1177 structElemType (sym_link * stype, value * id)
1179 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1180 sym_link *type, *etype;
1181 sym_link *petype = getSpec (stype);
1185 /* look for the id */
1188 if (strcmp (fields->rname, id->name) == 0)
1190 type = copyLinkChain (fields->type);
1191 etype = getSpec (type);
1192 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1193 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1195 SPEC_CONST (type) |= SPEC_CONST (stype);
1197 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1200 fields = fields->next;
1204 werror (E_NOT_MEMBER, id->name);
1206 // the show must go on
1207 return newIntLink();
1210 /*------------------------------------------------------------------*/
1211 /* getStructElement - returns element of a tructure definition */
1212 /*------------------------------------------------------------------*/
1214 getStructElement (structdef * sdef, symbol * sym)
1218 for (field = sdef->fields; field; field = field->next)
1219 if (strcmp (field->name, sym->name) == 0)
1222 werror (E_NOT_MEMBER, sym->name);
1224 return sdef->fields;
1227 /*------------------------------------------------------------------*/
1228 /* compStructSize - computes the size of a structure */
1229 /*------------------------------------------------------------------*/
1231 compStructSize (int su, structdef * sdef)
1233 int sum = 0, usum = 0;
1237 /* for the identifiers */
1238 loop = sdef->fields;
1241 /* create the internal name for this variable */
1242 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1247 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1249 /* if this is a bit field */
1252 /* change it to a unsigned bit */
1253 SPEC_NOUN (loop->etype) = V_BITFIELD;
1254 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1255 /* a "plain" int bitfield is unsigned */
1256 if (!loop->etype->select.s.b_signed)
1257 SPEC_USIGN(loop->etype) = 1;
1259 SPEC_BLEN (loop->etype) = loop->bitVar;
1261 if (loop->bitVar == BITVAR_PAD) {
1262 /* A zero length bitfield forces padding */
1263 SPEC_BSTR (loop->etype) = bitOffset;
1264 SPEC_BLEN (loop->etype) = 0;
1269 if (bitOffset == 8) {
1273 /* check if this fit into the remaining */
1274 /* bits of this byte else align it to the */
1275 /* next byte boundary */
1276 if (loop->bitVar <= (8 - bitOffset)) {
1277 /* fits into current byte */
1279 SPEC_BSTR (loop->etype) = bitOffset;
1280 bitOffset += loop->bitVar;
1282 else if (!bitOffset) {
1283 /* does not fit, but is already byte aligned */
1285 SPEC_BSTR (loop->etype) = bitOffset;
1286 bitOffset += loop->bitVar;
1289 if( TARGET_IS_PIC16 && getenv("PIC16_PACKED_BITFIELDS") ) {
1290 /* if PIC16 && enviroment variable is set, then
1291 * tightly pack bitfields, this means that when a
1292 * bitfield goes beyond byte alignment, do not
1293 * automatically start allocatint from next byte,
1294 * but also use the available bits first */
1295 fprintf(stderr, ": packing bitfields in structures\n");
1296 SPEC_BSTR (loop->etype) = bitOffset;
1297 bitOffset += loop->bitVar;
1298 loop->offset = (su == UNION ? sum = 0 : sum);
1300 /* does not fit; need to realign first */
1302 loop->offset = (su == UNION ? sum = 0 : sum);
1304 SPEC_BSTR (loop->etype) = bitOffset;
1305 bitOffset += loop->bitVar;
1308 while (bitOffset>8) {
1315 /* This is a non-bit field. Make sure we are */
1316 /* byte aligned first */
1319 loop->offset = (su == UNION ? sum = 0 : sum);
1323 checkDecl (loop, 1);
1324 sum += getSize (loop->type);
1326 /* search for "flexibel array members" */
1327 /* and do some syntax checks */
1329 && checkStructFlexArray (loop, loop->type))
1331 /* found a "flexible array member" */
1332 sdef->b_flexArrayMember = TRUE;
1333 /* is another struct-member following? */
1335 werror (E_FLEXARRAY_NOTATEND);
1336 /* is it the first struct-member? */
1337 else if (loop == sdef->fields)
1338 werror (E_FLEXARRAY_INEMPTYSTRCT);
1344 /* if union then size = sizeof largest field */
1346 /* For UNION, round up after each field */
1347 sum += ((bitOffset+7)/8);
1348 usum = max (usum, sum);
1353 /* For STRUCT, round up after all fields processed */
1355 sum += ((bitOffset+7)/8);
1357 return (su == UNION ? usum : sum);
1360 /*-------------------------------------------------------------------*/
1361 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1362 /* an enclosing struct/union */
1363 /*-------------------------------------------------------------------*/
1365 promoteAnonStructs (int su, structdef * sdef)
1374 tofield = &sdef->fields;
1375 field = sdef->fields;
1378 nextfield = field->next;
1379 if (!*field->name && IS_STRUCT (field->type))
1381 /* Found an anonymous struct/union. Replace it */
1382 /* with the fields it contains and adjust all */
1385 base = field->offset;
1386 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1388 continue; /* just in case it's empty */
1390 *tofield = subfield;
1393 /* check for field name conflicts resulting from promotion */
1394 dupfield = sdef->fields;
1395 while (dupfield && dupfield != subfield)
1397 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1399 werrorfl (subfield->fileDef, subfield->lineDef,
1401 su==STRUCT ? "struct" : "union",
1403 werrorfl (dupfield->fileDef, dupfield->lineDef,
1406 dupfield = dupfield->next;
1409 subfield->offset += base;
1411 subfield = subfield->next;
1415 subfield->next = nextfield;
1416 tofield = &subfield->next;
1419 tofield = &field->next;
1425 /*------------------------------------------------------------------*/
1426 /* checkSClass - check the storage class specification */
1427 /*------------------------------------------------------------------*/
1429 checkSClass (symbol * sym, int isProto)
1433 if (getenv("DEBUG_SANITY")) {
1434 fprintf (stderr, "checkSClass: %s \n", sym->name);
1437 /* type is literal can happen for enums change to auto */
1438 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1439 SPEC_SCLS (sym->etype) = S_AUTO;
1441 /* if sfr or sbit then must also be volatile */
1442 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1443 SPEC_SCLS (sym->etype) == S_SFR)
1445 SPEC_VOLATILE (sym->etype) = 1;
1448 /* make sure restrict is only used with pointers */
1449 if (SPEC_RESTRICT (sym->etype))
1451 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1452 SPEC_RESTRICT (sym->etype) = 0;
1457 if (IS_DECL (t) && DCL_PTR_RESTRICT (t) && !IS_PTR (t))
1459 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1460 DCL_PTR_RESTRICT (t) = 0;
1466 /* if absolute address given then it mark it as
1467 volatile -- except in the PIC port */
1469 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1470 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1471 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1474 if (IS_ABSOLUTE (sym->etype))
1475 SPEC_VOLATILE (sym->etype) = 1;
1477 if (TARGET_IS_MCS51 &&
1478 IS_ABSOLUTE (sym->etype) &&
1479 SPEC_SCLS (sym->etype) == S_SFR)
1484 if (SPEC_NOUN (sym->etype) == V_CHAR)
1486 else if (SPEC_LONG (sym->etype) == 0)
1491 addr = SPEC_ADDR (sym->etype);
1492 for (n=0; n<size; n+=8)
1493 if (((addr >> n) & 0xFF) < 0x80)
1494 werror (W_SFR_ABSRANGE, sym->name);
1497 /* If code memory is read only, then pointers to code memory */
1498 /* implicitly point to constants -- make this explicit */
1500 while (t && t->next) {
1501 if (IS_CODEPTR(t) && port->mem.code_ro) {
1502 if (IS_SPEC(t->next)) {
1503 SPEC_CONST (t->next) = 1;
1505 DCL_PTR_CONST (t->next) = 1;
1511 /* global variables declared const put into code */
1512 /* if no other storage class specified */
1513 if (sym->level == 0 &&
1514 SPEC_SCLS(sym->etype) == S_FIXED &&
1515 !IS_FUNC(sym->type)) {
1516 /* find the first non-array link */
1520 if (IS_CONSTANT (t)) {
1521 SPEC_SCLS (sym->etype) = S_CODE;
1525 /* global variable in code space is a constant */
1526 if (sym->level == 0 &&
1527 SPEC_SCLS (sym->etype) == S_CODE &&
1528 port->mem.code_ro) {
1529 /* find the first non-array link */
1536 DCL_PTR_CONST (t) = 1;
1540 /* if bit variable then no storage class can be */
1541 /* specified since bit is already a storage */
1542 if (IS_BITVAR (sym->etype) &&
1543 (SPEC_SCLS (sym->etype) != S_FIXED &&
1544 SPEC_SCLS (sym->etype) != S_SBIT &&
1545 SPEC_SCLS (sym->etype) != S_BIT)
1548 werror (E_BITVAR_STORAGE, sym->name);
1549 SPEC_SCLS (sym->etype) = S_FIXED;
1552 /* extern variables cannot be initialized */
1553 if (IS_EXTERN (sym->etype) && sym->ival)
1555 werror (E_EXTERN_INIT, sym->name);
1559 /* if this is an automatic symbol */
1560 if (sym->level && (options.stackAuto || reentrant)) {
1561 if (SPEC_SCLS (sym->etype) != S_BIT) {
1562 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1563 SPEC_SCLS (sym->etype) == S_FIXED ||
1564 SPEC_SCLS (sym->etype) == S_REGISTER ||
1565 SPEC_SCLS (sym->etype) == S_STACK ||
1566 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1567 SPEC_SCLS (sym->etype) = S_AUTO;
1569 /* storage class may only be specified for statics */
1570 if (!IS_STATIC(sym->etype)) {
1571 werror (E_AUTO_ASSUMED, sym->name);
1577 /* automatic symbols cannot be given */
1578 /* an absolute address ignore it */
1580 SPEC_ABSA (sym->etype) &&
1581 (options.stackAuto || reentrant))
1583 werror (E_AUTO_ABSA, sym->name);
1584 SPEC_ABSA (sym->etype) = 0;
1587 /* arrays & pointers cannot be defined for bits */
1588 /* SBITS or SFRs or BIT */
1589 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1590 !IS_FUNCPTR (sym->type) &&
1591 (SPEC_NOUN (sym->etype) == V_BIT ||
1592 SPEC_NOUN (sym->etype) == V_SBIT ||
1593 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1594 SPEC_SCLS (sym->etype) == S_SFR))
1595 werror (E_BIT_ARRAY, sym->name);
1597 /* if this is a bit|sbit then set length & start */
1598 if (SPEC_NOUN (sym->etype) == V_BIT ||
1599 SPEC_NOUN (sym->etype) == V_SBIT)
1601 SPEC_BLEN (sym->etype) = 1;
1602 SPEC_BSTR (sym->etype) = 0;
1606 /* variables declared in CODE space must have */
1607 /* initializers if not an extern */
1608 if (SPEC_SCLS (sym->etype) == S_CODE &&
1609 sym->ival == NULL &&
1612 port->mem.code_ro &&
1613 !IS_EXTERN (sym->etype) &&
1614 !SPEC_ABSA (sym->etype) &&
1615 !funcInChain (sym->type))
1616 werror (E_CODE_NO_INIT, sym->name);
1619 /* if parameter or local variable then change */
1620 /* the storage class to reflect where the var will go */
1621 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1622 && !IS_STATIC(sym->etype)
1625 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1627 SPEC_SCLS (sym->etype) = (options.useXstack ?
1628 S_XSTACK : S_STACK);
1632 /* hack-o-matic! I see no reason why the useXstack option should ever
1633 * control this allocation, but the code was originally that way, and
1634 * changing it for non-390 ports breaks the compiler badly.
1636 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1637 1 : options.useXstack;
1638 SPEC_SCLS (sym->etype) = (useXdata ?
1644 /*------------------------------------------------------------------*/
1645 /* changePointer - change pointer to functions */
1646 /*------------------------------------------------------------------*/
1648 changePointer (sym_link * p)
1651 /* go thru the chain of declarations */
1652 /* if we find a pointer to a function */
1653 /* change it to a ptr to code area */
1654 /* unless the function is banked. */
1655 for (; p; p = p->next)
1657 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1658 DCL_TYPE (p) = port->unqualified_pointer;
1659 if (IS_PTR (p) && IS_FUNC (p->next))
1660 if (!IFFUNC_BANKED(p->next))
1661 DCL_TYPE (p) = CPOINTER;
1665 /*------------------------------------------------------------------*/
1666 /* checkDecl - does semantic validation of a declaration */
1667 /*------------------------------------------------------------------*/
1669 checkDecl (symbol * sym, int isProto)
1672 checkSClass (sym, isProto); /* check the storage class */
1673 changePointer (sym->type); /* change pointers if required */
1675 /* if this is an array without any dimension
1676 then update the dimension from the initial value */
1677 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1678 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1683 /*------------------------------------------------------------------*/
1684 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1685 /*------------------------------------------------------------------*/
1687 copyLinkChain (sym_link * p)
1689 sym_link *head, *curr, *loop;
1691 /* note: v_struct and v_struct->fields are not copied! */
1693 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1696 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1697 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1705 /*------------------------------------------------------------------*/
1706 /* cleanUpBlock - cleansup the symbol table specified for all the */
1707 /* symbols in the given block */
1708 /*------------------------------------------------------------------*/
1710 cleanUpBlock (bucket ** table, int block)
1715 /* go thru the entire table */
1716 for (i = 0; i < 256; i++)
1718 for (chain = table[i]; chain; chain = chain->next)
1720 if (chain->block >= block)
1722 deleteSym (table, chain->sym, chain->name);
1728 /*------------------------------------------------------------------*/
1729 /* cleanUpLevel - cleansup the symbol table specified for all the */
1730 /* symbols in the given level */
1731 /*------------------------------------------------------------------*/
1733 cleanUpLevel (bucket ** table, int level)
1738 /* go thru the entire table */
1739 for (i = 0; i < 256; i++)
1741 for (chain = table[i]; chain; chain = chain->next)
1743 if (chain->level >= level)
1745 deleteSym (table, chain->sym, chain->name);
1751 /*------------------------------------------------------------------*/
1752 /* computeTypeOr - computes the resultant type from two types */
1753 /*------------------------------------------------------------------*/
1755 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1758 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1759 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1761 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1763 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1767 if (SPEC_USIGN (etype1))
1769 if ( IS_LITERAL (etype2)
1770 && floatFromVal (valFromType (etype2)) >= 0)
1771 SPEC_USIGN (reType) = 1;
1774 /* promote to int */
1775 SPEC_USIGN (reType) = 0;
1776 SPEC_NOUN (reType) = V_INT;
1779 else /* etype1 signed */
1781 if ( IS_LITERAL (etype2)
1782 && floatFromVal (valFromType (etype2)) <= 127)
1783 SPEC_USIGN (reType) = 0;
1786 /* promote to int */
1787 SPEC_USIGN (reType) = 0;
1788 SPEC_NOUN (reType) = V_INT;
1792 if (SPEC_USIGN (etype2))
1794 if ( IS_LITERAL (etype1)
1795 && floatFromVal (valFromType (etype1)) >= 0)
1796 SPEC_USIGN (reType) = 1;
1799 /* promote to int */
1800 SPEC_USIGN (reType) = 0;
1801 SPEC_NOUN (reType) = V_INT;
1804 else /* etype2 signed */
1806 if ( IS_LITERAL (etype1)
1807 && floatFromVal (valFromType (etype1)) <= 127)
1808 SPEC_USIGN (reType) = 0;
1811 /* promote to int */
1812 SPEC_USIGN (reType) = 0;
1813 SPEC_NOUN (reType) = V_INT;
1819 /*------------------------------------------------------------------*/
1820 /* computeType - computes the resultant type from two types */
1821 /*------------------------------------------------------------------*/
1823 computeType (sym_link * type1, sym_link * type2,
1824 RESULT_TYPE resultType, int op)
1828 sym_link *etype1 = getSpec (type1);
1831 etype2 = type2 ? getSpec (type2) : type1;
1833 /* if one of them is a float then result is a float */
1834 /* here we assume that the types passed are okay */
1835 /* and can be cast to one another */
1836 /* which ever is greater in size */
1837 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1838 rType = newFloatLink ();
1839 /* if both are fixed16x16 then result is float */
1840 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1841 rType = newFixed16x16Link();
1842 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1843 rType = newFloatLink ();
1844 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1845 rType = newFloatLink ();
1847 /* if both are bitvars choose the larger one */
1848 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1849 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1850 copyLinkChain (type1) : copyLinkChain (type1);
1852 /* if only one of them is a bit variable then the other one prevails */
1853 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1855 rType = copyLinkChain (type2);
1856 /* bitfield can have up to 16 bits */
1857 if (getSize (etype1) > 1)
1858 SPEC_NOUN (getSpec (rType)) = V_INT;
1860 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1862 rType = copyLinkChain (type1);
1863 /* bitfield can have up to 16 bits */
1864 if (getSize (etype2) > 1)
1865 SPEC_NOUN (getSpec (rType)) = V_INT;
1867 /* if one of them is a pointer or array then that
1869 else if (IS_PTR (type1) || IS_ARRAY (type1))
1870 rType = copyLinkChain (type1);
1871 else if (IS_PTR (type2) || IS_ARRAY (type2))
1872 rType = copyLinkChain (type2);
1873 else if (getSize (type1) > getSize (type2))
1874 rType = copyLinkChain (type1);
1876 rType = copyLinkChain (type2);
1878 reType = getSpec (rType);
1880 /* avoid conflicting types */
1881 reType->select.s.b_signed = 0;
1883 /* if result is a literal then make not so */
1884 if (IS_LITERAL (reType))
1885 SPEC_SCLS (reType) = S_REGISTER;
1889 case RESULT_TYPE_CHAR:
1890 if (IS_BITVAR (reType))
1892 SPEC_NOUN (reType) = V_CHAR;
1893 SPEC_SCLS (reType) = 0;
1894 SPEC_USIGN (reType) = 0;
1898 case RESULT_TYPE_INT:
1899 case RESULT_TYPE_NONE:
1900 case RESULT_TYPE_OTHER:
1901 if (IS_BIT (reType))
1903 SPEC_NOUN (reType) = V_CHAR;
1904 SPEC_SCLS (reType) = 0;
1905 SPEC_USIGN (reType) = 0;
1908 else if (IS_BITFIELD (reType))
1910 /* could be smarter, but it depends on the op */
1911 /* this is for the worst case: a multiplication of 4 * 4 bit */
1912 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1913 SPEC_SCLS (reType) = 0;
1914 SPEC_USIGN (reType) = 0;
1917 else if (IS_CHAR (reType))
1919 /* promotion of some special cases */
1924 return computeTypeOr (etype1, etype2, reType);
1926 if (SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1928 SPEC_USIGN (reType) = 1;
1933 SPEC_NOUN (reType) = V_INT;
1934 SPEC_USIGN (reType) = 0;
1937 /* if both are unsigned char then no promotion required */
1938 if (!(SPEC_USIGN (etype1) && SPEC_USIGN (etype2)))
1940 SPEC_NOUN (reType) = V_INT;
1941 SPEC_USIGN (reType) = 0;
1954 /* SDCC's sign promotion:
1955 - if one or both operands are unsigned, the resultant type will be unsigned
1956 (except char, see below)
1957 - if an operand is promoted to a larger type (char -> int, int -> long),
1958 the larger type will be signed
1960 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1961 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1962 the standard. The standard demands, that the result has to be the same
1963 "as if" the promotion would have been performed:
1965 - if the result of an operation with two char's is promoted to a
1966 larger type, the result will be signed.
1968 More sophisticated are these:
1969 - if the result of an operation with two char's is a char again,
1970 the result will only then be unsigned, if both operands are
1971 unsigned. In all other cases the result will be signed.
1973 This seems to be contradictionary to the first two rules, but it makes
1974 real sense (all types are char's):
1976 A signed char can be negative; this must be preserved in the result
1979 Only if both operands are unsigned it's safe to make the result
1980 unsigned; this helps to avoid overflow:
1983 - ToDo: document '|', '^' and '&'
1985 Homework: - why is (200 * 200 < 0) true?
1986 - why is { char l = 200, r = 200; (r * l > 0) } true?
1989 if (!IS_FLOAT (reType)
1990 && ( (SPEC_USIGN (etype1)
1991 /* if this operand is promoted to a larger type,
1992 then it will be promoted to a signed type */
1993 && !(bitsForType (etype1) < bitsForType (reType))
1994 /* char require special handling */
1995 && !IS_CHAR (etype1))
1996 || /* same for 2nd operand */
1997 (SPEC_USIGN (etype2)
1998 && !(bitsForType (etype2) < bitsForType (reType))
1999 && !IS_CHAR (etype2))
2000 || /* if both are 'unsigned char' and not promoted
2001 let the result be unsigned too */
2002 ( SPEC_USIGN (etype1)
2003 && SPEC_USIGN (etype2)
2006 && IS_CHAR (reType))))
2007 SPEC_USIGN (reType) = 1;
2009 SPEC_USIGN (reType) = 0;
2014 /*--------------------------------------------------------------------*/
2015 /* compareType - will do type check return 1 if match, -1 if castable */
2016 /*--------------------------------------------------------------------*/
2018 compareType (sym_link * dest, sym_link * src)
2029 /* if dest is a declarator then */
2034 /* banked function pointer */
2035 if (IS_GENPTR (dest) && IS_GENPTR (src))
2037 if (IS_FUNC (src->next) && IS_VOID(dest->next))
2039 if (IS_FUNC (dest->next) && IS_VOID(src->next))
2041 return compareType (dest->next, src->next);
2044 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2046 //checkFunction(src,dest);
2048 return compareType (dest->next, src->next);
2050 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
2054 (IS_GENPTR (dest) ||
2055 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2058 if (IS_PTR (dest) && IS_ARRAY (src)) {
2059 value *val=aggregateToPointer (valFromType(src));
2060 int res=compareType (dest, val->type);
2061 Safe_free(val->type);
2065 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2066 return compareType (dest->next, src);
2069 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2075 /* if one is a specifier and the other is not */
2076 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2077 (IS_SPEC (dest) && !IS_SPEC (src)))
2080 /* if one of them is a void then ok */
2081 if (SPEC_NOUN (dest) == V_VOID &&
2082 SPEC_NOUN (src) != V_VOID)
2085 if (SPEC_NOUN (dest) != V_VOID &&
2086 SPEC_NOUN (src) == V_VOID)
2089 /* if they are both bitfields then if the lengths
2090 and starts don't match */
2091 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2092 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2093 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2096 /* it is a specifier */
2097 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2099 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2100 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2102 bitsForType (dest) == bitsForType (src))
2103 instead of the next two lines, but the regression tests fail with
2104 them; I guess it's a problem with replaceCheaperOp */
2105 getSize (dest) == getSize (src) &&
2106 !(!IS_BIT (dest) && IS_BIT (src)))
2108 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2113 else if (IS_STRUCT (dest))
2115 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2120 if (SPEC_LONG (dest) != SPEC_LONG (src))
2123 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2129 /*--------------------------------------------------------------------*/
2130 /* compareTypeExact - will do type check return 1 if match exactly */
2131 /*--------------------------------------------------------------------*/
2133 compareTypeExact (sym_link * dest, sym_link * src, int level)
2135 STORAGE_CLASS srcScls, destScls;
2146 /* if dest is a declarator then */
2151 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2152 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2154 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2156 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2160 value *exargs, *acargs, *checkValue;
2162 /* verify function return type */
2163 if (!compareTypeExact (dest->next, src->next, -1))
2165 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2167 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2169 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2172 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2176 /* compare expected args with actual args */
2177 exargs = FUNC_ARGS(dest);
2178 acargs = FUNC_ARGS(src);
2180 /* for all the expected args do */
2181 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2183 //checkTypeSanity(acargs->etype, acargs->name);
2185 if (IS_AGGREGATE (acargs->type))
2187 checkValue = copyValue (acargs);
2188 aggregateToPointer (checkValue);
2191 checkValue = acargs;
2194 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2199 /* if one them ended we have a problem */
2200 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2201 (!exargs && acargs && !IS_VOID (acargs->type)))
2205 return compareTypeExact (dest->next, src->next, level);
2212 /* if one is a specifier and the other is not */
2213 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2214 (IS_SPEC (dest) && !IS_SPEC (src)))
2217 /* if one of them is a void then ok */
2218 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2221 /* if they are both bitfields then if the lengths
2222 and starts don't match */
2223 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2224 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2225 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2228 if (IS_INTEGRAL (dest))
2230 /* signedness must match */
2231 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2233 /* size must match */
2234 if (SPEC_LONG (dest) != SPEC_LONG (src))
2236 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2240 if (IS_STRUCT (dest))
2242 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2246 if (SPEC_CONST (dest) != SPEC_CONST (src))
2248 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2250 if (SPEC_STAT (dest) != SPEC_STAT (src))
2252 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2254 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2257 destScls = SPEC_SCLS (dest);
2258 srcScls = SPEC_SCLS (src);
2260 /* Compensate for const to const code change in checkSClass() */
2261 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2263 if (srcScls == S_CODE && destScls == S_FIXED)
2265 if (destScls == S_CODE && srcScls == S_FIXED)
2269 /* compensate for allocGlobal() */
2270 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2271 && port->mem.default_globl_map == xdata
2275 if (level>0 && !SPEC_STAT (dest))
2277 /* Compensate for hack-o-matic in checkSClass() */
2278 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2280 if (destScls == S_FIXED)
2281 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2282 if (srcScls == S_FIXED)
2283 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2285 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2287 if (destScls == S_FIXED)
2289 if (srcScls == S_FIXED)
2294 if (srcScls != destScls)
2297 printf ("level = %d\n", level);
2298 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2299 SPEC_SCLS (src), SPEC_SCLS (dest));
2300 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2308 /*------------------------------------------------------------------*/
2309 /* inCalleeSaveList - return 1 if found in callee save list */
2310 /*------------------------------------------------------------------*/
2312 calleeCmp(void *p1, void *p2)
2314 return (strcmp((char *)p1, (char *)(p2)) == 0);
2318 inCalleeSaveList(char *s)
2320 if (options.all_callee_saves)
2322 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2325 /*-----------------------------------------------------------------*/
2326 /* aggregateToPointer: change an agggregate type function */
2327 /* argument to a pointer to that type. */
2328 /*-----------------------------------------------------------------*/
2330 aggregateToPointer (value * val)
2332 if (IS_AGGREGATE (val->type))
2334 /* if this is a structure */
2335 /* then we need to add a new link */
2336 if (IS_STRUCT (val->type))
2338 /* first lets add DECLARATOR type */
2339 sym_link *p = val->type;
2341 werror (W_STRUCT_AS_ARG, val->name);
2342 val->type = newLink (DECLARATOR);
2343 val->type->next = p;
2346 /* change to a pointer depending on the */
2347 /* storage class specified */
2348 switch (SPEC_SCLS (val->etype))
2351 DCL_TYPE (val->type) = IPOINTER;
2354 DCL_TYPE (val->type) = PPOINTER;
2357 if (SPEC_OCLS(val->etype)) {
2358 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2360 // this happens for (external) function parameters
2361 DCL_TYPE (val->type) = port->unqualified_pointer;
2367 DCL_TYPE (val->type) = POINTER;
2370 DCL_TYPE (val->type) = CPOINTER;
2373 DCL_TYPE (val->type) = FPOINTER;
2376 DCL_TYPE (val->type) = EEPPOINTER;
2379 DCL_TYPE (val->type) = port->unqualified_pointer;
2382 /* is there is a symbol associated then */
2383 /* change the type of the symbol as well */
2386 val->sym->type = copyLinkChain (val->type);
2387 val->sym->etype = getSpec (val->sym->type);
2392 /*------------------------------------------------------------------*/
2393 /* checkFunction - does all kinds of check on a function */
2394 /*------------------------------------------------------------------*/
2396 checkFunction (symbol * sym, symbol *csym)
2398 value *exargs, *acargs;
2402 if (getenv("DEBUG_SANITY")) {
2403 fprintf (stderr, "checkFunction: %s ", sym->name);
2406 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2408 werror(E_SYNTAX_ERROR, sym->name);
2412 /* make sure the type is complete and sane */
2413 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2415 /* if not type then some kind of error */
2419 /* if the function has no type then make it return int */
2420 if (!sym->type->next)
2421 sym->type->next = sym->etype = newIntLink ();
2423 /* function cannot return aggregate */
2424 if (IS_AGGREGATE (sym->type->next))
2426 werror (E_FUNC_AGGR, sym->name);
2430 /* check if this function is defined as calleeSaves
2431 then mark it as such */
2432 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2434 /* if interrupt service routine */
2435 /* then it cannot have arguments */
2436 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2438 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2439 werror (E_INT_ARGS, sym->name);
2440 FUNC_ARGS(sym->type)=NULL;
2444 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2446 werror (E_SHADOWREGS_NO_ISR, sym->name);
2450 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2452 acargs=acargs->next, argCnt++) {
2454 // this can happen for reentrant functions
2455 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2456 // the show must go on: synthesize a name and symbol
2457 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2458 acargs->sym = newSymbol (acargs->name, 1);
2459 SPEC_OCLS (acargs->etype) = istack;
2460 acargs->sym->type = copyLinkChain (acargs->type);
2461 acargs->sym->etype = getSpec (acargs->sym->type);
2462 acargs->sym->_isparm = 1;
2463 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2464 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2466 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2471 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2472 return 1; /* not defined nothing more to check */
2474 /* check if body already present */
2475 if (csym && IFFUNC_HASBODY(csym->type))
2477 werror (E_FUNC_BODY, sym->name);
2481 /* check the return value type */
2482 if (compareType (csym->type, sym->type) <= 0)
2484 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2485 printFromToType(csym->type, sym->type);
2489 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2491 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2494 /* I don't think this is necessary for interrupts. An isr is a */
2495 /* root in the calling tree. */
2496 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2497 (!FUNC_ISISR (sym->type)))
2499 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2502 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2504 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2507 /* Really, reentrant should match regardless of argCnt, but */
2508 /* this breaks some existing code (the fp lib functions). If */
2509 /* the first argument is always passed the same way, this */
2510 /* lax checking is ok (but may not be true for in future ports) */
2511 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2514 //printf("argCnt = %d\n",argCnt);
2515 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2518 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2520 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2523 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2525 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2529 /* compare expected args with actual args */
2530 exargs = FUNC_ARGS(csym->type);
2531 acargs = FUNC_ARGS(sym->type);
2533 /* for all the expected args do */
2536 exargs = exargs->next, acargs = acargs->next, argCnt++)
2538 if (getenv("DEBUG_SANITY")) {
2539 fprintf (stderr, "checkFunction: %s ", exargs->name);
2541 /* make sure the type is complete and sane */
2542 checkTypeSanity(exargs->etype, exargs->name);
2544 /* If the actual argument is an array, any prototype
2545 * will have modified it to a pointer. Duplicate that
2548 if (IS_AGGREGATE (acargs->type))
2550 checkValue = copyValue (acargs);
2551 aggregateToPointer (checkValue);
2555 checkValue = acargs;
2558 if (compareType (exargs->type, checkValue->type) <= 0)
2560 werror (E_ARG_TYPE, argCnt);
2561 printFromToType(exargs->type, checkValue->type);
2566 /* if one them ended we have a problem */
2567 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2568 (!exargs && acargs && !IS_VOID (acargs->type)))
2569 werror (E_ARG_COUNT);
2571 /* replace with this defition */
2572 sym->cdef = csym->cdef;
2573 deleteSym (SymbolTab, csym, csym->name);
2574 deleteFromSeg(csym);
2575 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2576 if (IS_EXTERN (csym->etype) && !
2577 IS_EXTERN (sym->etype))
2579 addSet (&publics, sym);
2584 /*------------------------------------------------------------------*/
2585 /* cdbStructBlock - calls struct printing for a blcks */
2586 /*------------------------------------------------------------------*/
2587 void cdbStructBlock (int block)
2590 bucket **table = StructTab;
2593 /* go thru the entire table */
2594 for (i = 0; i < 256; i++)
2596 for (chain = table[i]; chain; chain = chain->next)
2598 if (chain->block >= block)
2601 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2607 /*-----------------------------------------------------------------*/
2608 /* processFuncPtrArgs - does some processing with args of func ptrs*/
2609 /*-----------------------------------------------------------------*/
2611 processFuncPtrArgs (sym_link * funcType)
2613 value *val = FUNC_ARGS(funcType);
2615 /* if it is void then remove parameters */
2616 if (val && IS_VOID (val->type))
2618 FUNC_ARGS(funcType) = NULL;
2623 /*-----------------------------------------------------------------*/
2624 /* processFuncArgs - does some processing with function args */
2625 /*-----------------------------------------------------------------*/
2627 processFuncArgs (symbol * func)
2631 sym_link *funcType=func->type;
2633 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2634 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2636 /* find the function declaration within the type */
2637 while (funcType && !IS_FUNC(funcType))
2638 funcType=funcType->next;
2640 /* if this function has variable argument list */
2641 /* then make the function a reentrant one */
2642 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2643 FUNC_ISREENT(funcType)=1;
2645 /* check if this function is defined as calleeSaves
2646 then mark it as such */
2647 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2649 /* loop thru all the arguments */
2650 val = FUNC_ARGS(funcType);
2652 /* if it is void then remove parameters */
2653 if (val && IS_VOID (val->type))
2655 FUNC_ARGS(funcType) = NULL;
2659 /* reset regparm for the port */
2660 (*port->reset_regparms) ();
2662 /* if any of the arguments is an aggregate */
2663 /* change it to pointer to the same type */
2667 char buffer[SDCC_NAME_MAX+1];
2669 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2670 checkTypeSanity (val->etype, buffer);
2672 /* mark it as a register parameter if
2673 the function does not have VA_ARG
2674 and as port dictates */
2675 if (!IFFUNC_HASVARARGS(funcType) &&
2676 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2678 SPEC_REGPARM (val->etype) = 1;
2679 SPEC_ARGREG(val->etype) = argreg;
2680 } else if (IFFUNC_ISREENT(funcType)) {
2681 FUNC_HASSTACKPARM(funcType) = 1;
2684 if (IS_AGGREGATE (val->type))
2686 aggregateToPointer (val);
2693 /* if this is an internal generated function call */
2695 /* ignore --stack-auto for this one, we don't know how it is compiled */
2696 /* simply trust on --int-long-reent or --float-reent */
2697 if (IFFUNC_ISREENT(funcType)) {
2701 /* if this function is reentrant or */
2702 /* automatics r 2b stacked then nothing */
2703 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2707 val = FUNC_ARGS(funcType);
2712 /* if a symbolname is not given */
2713 /* synthesize a variable name */
2716 SNPRINTF (val->name, sizeof(val->name),
2717 "_%s_PARM_%d", func->name, pNum++);
2718 val->sym = newSymbol (val->name, 1);
2719 if (SPEC_SCLS(val->etype) == S_BIT)
2720 SPEC_OCLS (val->etype) = bit;
2722 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2723 val->sym->type = copyLinkChain (val->type);
2724 val->sym->etype = getSpec (val->sym->type);
2725 val->sym->_isparm = 1;
2726 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2728 /* ?? static functions shouldn't imply static parameters - EEP */
2729 if (IS_SPEC(func->etype)) {
2730 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2731 SPEC_STAT (func->etype);
2734 addSymChain (&val->sym);
2737 else /* symbol name given create synth name */
2740 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2741 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2742 val->sym->_isparm = 1;
2743 if (SPEC_SCLS(val->etype) == S_BIT)
2744 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2746 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2747 port->mem.default_local_map;
2750 /* ?? static functions shouldn't imply static parameters - EEP */
2751 if (IS_SPEC(func->etype)) {
2752 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2753 SPEC_STAT (func->etype);
2757 if (SPEC_OCLS (val->sym->etype) == pdata)
2758 val->sym->iaccess = 1;
2759 if (!isinSet(operKeyReset, val->sym)) {
2760 addSet (&operKeyReset, val->sym);
2761 applyToSet (operKeyReset, resetParmKey);
2767 /*-----------------------------------------------------------------*/
2768 /* isSymbolEqual - compares two symbols return 1 if they match */
2769 /*-----------------------------------------------------------------*/
2771 isSymbolEqual (symbol * dest, symbol * src)
2773 /* if pointers match then equal */
2777 /* if one of them is null then don't match */
2781 /* if both of them have rname match on rname */
2782 if (dest->rname[0] && src->rname[0])
2783 return (!strcmp (dest->rname, src->rname));
2785 /* otherwise match on name */
2786 return (!strcmp (dest->name, src->name));
2789 void PT(sym_link *type)
2791 printTypeChain(type,0);
2793 /*-----------------------------------------------------------------*/
2794 /* printTypeChain - prints the type chain in human readable form */
2795 /*-----------------------------------------------------------------*/
2797 printTypeChain (sym_link * start, FILE * of)
2808 dbuf_init (&dbuf, 1024);
2809 dbuf_printTypeChain (start, &dbuf);
2810 dbuf_write_and_destroy (&dbuf, of);
2817 dbuf_printTypeChain (sym_link * start, struct dbuf_s *dbuf)
2820 sym_link * type, * search;
2824 dbuf_append_str (dbuf, "void");
2828 /* Print the chain as it is written in the source: */
2829 /* start with the last entry. */
2830 /* However, the storage class at the end of the */
2831 /* chain reall applies to the first in the chain! */
2833 for (type = start; type && type->next; type = type->next)
2836 scls=SPEC_SCLS(type);
2844 case S_DATA: dbuf_append_str (dbuf, "data-"); break;
2845 case S_XDATA: dbuf_append_str (dbuf, "xdata-"); break;
2846 case S_SFR: dbuf_append_str (dbuf, "sfr-"); break;
2847 case S_SBIT: dbuf_append_str (dbuf, "sbit-"); break;
2848 case S_CODE: dbuf_append_str (dbuf, "code-"); break;
2849 case S_IDATA: dbuf_append_str (dbuf, "idata-"); break;
2850 case S_PDATA: dbuf_append_str (dbuf, "pdata-"); break;
2851 case S_LITERAL: dbuf_append_str (dbuf, "literal-"); break;
2852 case S_STACK: dbuf_append_str (dbuf, "stack-"); break;
2853 case S_XSTACK: dbuf_append_str (dbuf, "xstack-"); break;
2854 case S_BIT: dbuf_append_str (dbuf, "bit-"); break;
2855 case S_EEPROM: dbuf_append_str (dbuf, "eeprom-"); break;
2862 if (!IS_FUNC(type)) {
2863 if (DCL_PTR_VOLATILE (type)) {
2864 dbuf_append_str (dbuf, "volatile-");
2866 if (DCL_PTR_CONST (type)) {
2867 dbuf_append_str (dbuf, "const-");
2870 switch (DCL_TYPE (type))
2873 dbuf_printf (dbuf, "function %s %s",
2874 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2875 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2876 dbuf_append_str (dbuf, "( ");
2877 for (args = FUNC_ARGS(type);
2880 dbuf_printTypeChain(args->type, dbuf);
2882 dbuf_append_str (dbuf, ", ");
2884 dbuf_append_str (dbuf, ") ");
2887 dbuf_append_str (dbuf, "generic* ");
2890 dbuf_append_str (dbuf, "code* ");
2893 dbuf_append_str (dbuf, "xdata* ");
2896 dbuf_append_str (dbuf, "eeprom* ");
2899 dbuf_append_str (dbuf, "near* ");
2902 dbuf_append_str (dbuf, "idata* ");
2905 dbuf_append_str (dbuf, "pdata* ");
2908 dbuf_append_str (dbuf, "unknown* ");
2911 if (DCL_ELEM(type)) {
2912 dbuf_printf (dbuf, "[%d] ", DCL_ELEM(type));
2914 dbuf_append_str (dbuf, "[] ");
2921 if (SPEC_VOLATILE (type))
2922 dbuf_append_str (dbuf, "volatile-");
2923 if (SPEC_CONST (type))
2924 dbuf_append_str (dbuf, "const-");
2925 if (SPEC_USIGN (type))
2926 dbuf_append_str (dbuf, "unsigned-");
2927 switch (SPEC_NOUN (type))
2931 dbuf_append_str (dbuf, "long-");
2932 dbuf_append_str (dbuf, "int");
2936 dbuf_append_str (dbuf, "char");
2940 dbuf_append_str (dbuf, "void");
2944 dbuf_append_str (dbuf, "float");
2948 dbuf_append_str (dbuf, "fixed16x16");
2952 dbuf_printf (dbuf, "struct %s", SPEC_STRUCT (type)->tag);
2956 dbuf_append_str (dbuf, "sbit");
2960 dbuf_append_str (dbuf, "bit");
2964 dbuf_printf (dbuf, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2968 dbuf_append_str (dbuf, "double");
2972 dbuf_append_str (dbuf, "unknown type");
2976 /* search entry in list before "type" */
2977 for (search = start; search && search->next != type;)
2978 search = search->next;
2981 dbuf_append_char(dbuf, ' ');
2985 /*--------------------------------------------------------------------*/
2986 /* printTypeChainRaw - prints the type chain in human readable form */
2987 /* in the raw data structure ordering */
2988 /*--------------------------------------------------------------------*/
2990 printTypeChainRaw (sym_link * start, FILE * of)
3003 fprintf (of, "void");
3013 if (!IS_FUNC(type)) {
3014 if (DCL_PTR_VOLATILE (type)) {
3015 fprintf (of, "volatile-");
3017 if (DCL_PTR_CONST (type)) {
3018 fprintf (of, "const-");
3021 switch (DCL_TYPE (type))
3024 fprintf (of, "function %s %s",
3025 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
3026 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
3028 for (args = FUNC_ARGS(type);
3031 printTypeChain(args->type, of);
3038 fprintf (of, "generic* ");
3041 fprintf (of, "code* ");
3044 fprintf (of, "xdata* ");
3047 fprintf (of, "eeprom* ");
3050 fprintf (of, "near* ");
3053 fprintf (of, "idata* ");
3056 fprintf (of, "pdata* ");
3059 fprintf (of, "unknown* ");
3062 if (DCL_ELEM(type)) {
3063 fprintf (of, "[%d] ", DCL_ELEM(type));
3065 fprintf (of, "[] ");
3069 if (DCL_TSPEC(type))
3072 printTypeChainRaw(DCL_TSPEC(type), of);
3076 else if (IS_SPEC (type))
3078 switch (SPEC_SCLS (type))
3080 case S_DATA: fprintf (of, "data-"); break;
3081 case S_XDATA: fprintf (of, "xdata-"); break;
3082 case S_SFR: fprintf (of, "sfr-"); break;
3083 case S_SBIT: fprintf (of, "sbit-"); break;
3084 case S_CODE: fprintf (of, "code-"); break;
3085 case S_IDATA: fprintf (of, "idata-"); break;
3086 case S_PDATA: fprintf (of, "pdata-"); break;
3087 case S_LITERAL: fprintf (of, "literal-"); break;
3088 case S_STACK: fprintf (of, "stack-"); break;
3089 case S_XSTACK: fprintf (of, "xstack-"); break;
3090 case S_BIT: fprintf (of, "bit-"); break;
3091 case S_EEPROM: fprintf (of, "eeprom-"); break;
3094 if (SPEC_VOLATILE (type))
3095 fprintf (of, "volatile-");
3096 if (SPEC_CONST (type))
3097 fprintf (of, "const-");
3098 if (SPEC_USIGN (type))
3099 fprintf (of, "unsigned-");
3100 switch (SPEC_NOUN (type))
3104 fprintf (of, "long-");
3105 fprintf (of, "int");
3109 fprintf (of, "char");
3113 fprintf (of, "void");
3117 fprintf (of, "float");
3121 fprintf (of, "fixed16x16");
3125 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3129 fprintf (of, "sbit");
3133 fprintf (of, "bit");
3137 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3141 fprintf (of, "double");
3145 fprintf (of, "unknown type");
3150 fprintf (of, "NOT_SPEC_OR_DECL");
3160 /*-----------------------------------------------------------------*/
3161 /* powof2 - returns power of two for the number if number is pow 2 */
3162 /*-----------------------------------------------------------------*/
3164 powof2 (TYPE_TARGET_ULONG num)
3177 if (n1s > 1 || nshifts == 0)
3193 symbol *__fps16x16_add;
3194 symbol *__fps16x16_sub;
3195 symbol *__fps16x16_mul;
3196 symbol *__fps16x16_div;
3197 symbol *__fps16x16_eq;
3198 symbol *__fps16x16_neq;
3199 symbol *__fps16x16_lt;
3200 symbol *__fps16x16_lteq;
3201 symbol *__fps16x16_gt;
3202 symbol *__fps16x16_gteq;
3204 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3205 symbol *__muldiv[3][3][2];
3206 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3207 sym_link *__multypes[3][2];
3208 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3209 symbol *__conv[2][3][2];
3210 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3211 symbol *__fp16x16conv[2][4][2];
3212 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3213 symbol *__rlrr[2][3][2];
3215 sym_link *floatType;
3216 sym_link *fixed16x16Type;
3219 _mangleFunctionName(char *in)
3221 if (port->getMangledFunctionName)
3223 return port->getMangledFunctionName(in);
3231 /*-----------------------------------------------------------------*/
3232 /* typeFromStr - create a typechain from an encoded string */
3233 /* basic types - 'c' - char */
3238 /* 'q' - fixed16x16 */
3240 /* '*' - pointer - default (GPOINTER) */
3241 /* modifiers - 'u' - unsigned */
3242 /* pointer modifiers - 'g' - generic */
3246 /* 'F' - function */
3247 /* examples : "ig*" - generic int * */
3248 /* "cx*" - char xdata * */
3249 /* "ui" - unsigned int */
3250 /*-----------------------------------------------------------------*/
3251 sym_link *typeFromStr (char *s)
3253 sym_link *r = newLink(DECLARATOR);
3265 r->class = SPECIFIER;
3266 SPEC_NOUN(r) = V_CHAR;
3270 r->class = SPECIFIER;
3271 SPEC_NOUN(r) = V_INT;
3274 r->class = SPECIFIER;
3275 SPEC_NOUN(r) = V_INT;
3279 r->class = SPECIFIER;
3280 SPEC_NOUN(r) = V_FLOAT;
3283 r->class = SPECIFIER;
3284 SPEC_NOUN(r) = V_FIXED16X16;
3287 r->class = SPECIFIER;
3288 SPEC_NOUN(r) = V_VOID;
3291 DCL_TYPE(r) = port->unqualified_pointer;
3298 assert(*(s+1)=='*');
3299 nr = newLink(DECLARATOR);
3304 DCL_TYPE(r) = GPOINTER;
3307 DCL_TYPE(r) = FPOINTER;
3310 DCL_TYPE(r) = CPOINTER;
3313 DCL_TYPE(r) = POINTER;
3316 DCL_TYPE(r) = FUNCTION;
3317 nr = newLink(DECLARATOR);
3320 DCL_TYPE(r) = CPOINTER;
3326 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3327 "typeFromStr: unknown type");
3330 if (IS_SPEC(r) && usign) {
3339 /*-----------------------------------------------------------------*/
3340 /* initCSupport - create functions for C support routines */
3341 /*-----------------------------------------------------------------*/
3345 const char *smuldivmod[] =
3349 const char *sbwd[] =
3351 "char", "int", "long", "fixed16x16",
3353 const char *fp16x16sbwd[] =
3355 "char", "int", "long", "float",
3361 const char *srlrr[] =
3366 int bwd, su, muldivmod, tofrom, rlrr;
3368 if (getenv("SDCC_NO_C_SUPPORT")) {
3369 /* for debugging only */
3373 floatType = newFloatLink ();
3374 fixed16x16Type = newFixed16x16Link ();
3376 for (bwd = 0; bwd < 3; bwd++)
3393 __multypes[bwd][0] = l;
3394 __multypes[bwd][1] = copyLinkChain (l);
3395 SPEC_USIGN (__multypes[bwd][1]) = 1;
3398 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3399 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3400 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3401 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3402 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3403 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3404 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3405 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3406 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3407 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3409 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3410 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3411 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3412 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3413 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3414 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3415 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3416 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3417 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3418 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3421 for (tofrom = 0; tofrom < 2; tofrom++)
3423 for (bwd = 0; bwd < 3; bwd++)
3425 for (su = 0; su < 2; su++)
3429 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3430 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3434 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3435 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3441 for (tofrom = 0; tofrom < 2; tofrom++)
3443 for (bwd = 0; bwd < 4; bwd++)
3445 for (su = 0; su < 2; su++)
3449 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3451 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3453 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3457 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3459 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3461 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3468 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3470 for (bwd = 0; bwd < 3; bwd++)
3472 for (su = 0; su < 2; su++)
3474 SNPRINTF (buffer, sizeof(buffer),
3476 smuldivmod[muldivmod],
3479 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3480 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3485 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3486 Therefore they've been merged into mulint() and mullong().
3489 for (bwd = 0; bwd < 3; bwd++)
3491 for (su = 0; su < 2; su++)
3493 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3495 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
3496 if (!TARGET_IS_PIC16 || muldivmod != 1 || bwd != 0 || su != 0)
3498 SNPRINTF (buffer, sizeof(buffer),
3500 smuldivmod[muldivmod],
3503 __muldiv[muldivmod][bwd][su] = funcOfType (
3504 _mangleFunctionName(buffer),
3505 __multypes[bwd][su],
3506 __multypes[bwd][su],
3508 options.intlong_rent);
3509 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3515 if (TARGET_IS_PIC16)
3517 /* PIC16 port wants __divschar/__modschar to return an int, so that both
3518 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
3519 * (first one would have to be sign extended, second one must not be).
3520 * Similarly, modschar should be handled, but the iCode introduces cast
3521 * here and forces '% : s8 x s8 -> s8' ... */
3523 for (muldivmod = 1; muldivmod < 2; muldivmod++) {
3524 SNPRINTF (buffer, sizeof(buffer),
3526 smuldivmod[muldivmod],
3529 __muldiv[muldivmod][bwd][su] = funcOfType (
3530 _mangleFunctionName(buffer),
3532 __multypes[bwd][su],
3534 options.intlong_rent);
3535 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3543 for (su = 0; su < 2; su++)
3545 /* muluchar and mulschar are still separate functions, because e.g. the z80
3546 port is sign/zero-extending to int before calling mulint() */
3547 SNPRINTF (buffer, sizeof(buffer),
3549 smuldivmod[muldivmod],
3552 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3553 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3557 /* word and doubleword */
3558 for (bwd = 1; bwd < 3; bwd++)
3561 SNPRINTF (buffer, sizeof(buffer),
3563 smuldivmod[muldivmod],
3565 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3566 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3567 /* signed = unsigned */
3568 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3571 for (rlrr = 0; rlrr < 2; rlrr++)
3573 for (bwd = 0; bwd < 3; bwd++)
3575 for (su = 0; su < 2; su++)
3577 SNPRINTF (buffer, sizeof(buffer),
3582 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3583 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3589 /*-----------------------------------------------------------------*/
3590 /* initBuiltIns - create prototypes for builtin functions */
3591 /*-----------------------------------------------------------------*/
3597 if (!port->builtintable) return ;
3599 for (i = 0 ; port->builtintable[i].name ; i++) {
3600 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3601 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3602 FUNC_ISBUILTIN(sym->type) = 1;
3603 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3607 sym_link *validateLink(sym_link *l,
3614 if (l && l->class==select)
3619 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3620 " expected %s, got %s\n",
3621 macro, args, file, line,
3622 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3624 return l; // never reached, makes compiler happy.
3627 /*--------------------------------------------------------------------*/
3628 /* newEnumType - create an integer type compatible with enumerations */
3629 /*--------------------------------------------------------------------*/
3631 newEnumType (symbol *enumlist)
3639 type = newLink (SPECIFIER);
3640 SPEC_NOUN (type) = V_INT;
3644 /* Determine the range of the enumerated values */
3646 min = max = (int) floatFromVal (valFromType (sym->type));
3647 for (sym = sym->next; sym; sym = sym->next)
3649 v = (int) floatFromVal (valFromType (sym->type));
3656 /* Determine the smallest integer type that is compatible with this range */
3657 type = newLink (SPECIFIER);
3658 if (min>=0 && max<=255)
3660 SPEC_NOUN (type) = V_CHAR;
3661 SPEC_USIGN (type) = 1;
3663 else if (min>=-128 && max<=127)
3665 SPEC_NOUN (type) = V_CHAR;
3667 else if (min>=0 && max<=65535)
3669 SPEC_NOUN (type) = V_INT;
3670 SPEC_USIGN (type) = 1;
3672 else if (min>=-32768 && max<=32767)
3674 SPEC_NOUN (type) = V_INT;
3678 SPEC_NOUN (type) = V_INT;
3679 SPEC_LONG (type) = 1;
3681 SPEC_USIGN (type) = 1;