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 /* copyStruct - copies a structdef including the fields-list */
333 /*------------------------------------------------------------------*/
335 copyStruct (structdef *src)
339 dest = newStruct ("");
340 memcpy (dest, src, sizeof (structdef));
341 dest->fields = copySymbolChain (src->fields);
345 /*------------------------------------------------------------------*/
346 /* sclsFromPtr - Return the storage class a pointer points into. */
347 /* S_FIXED is returned for generic pointers or other */
348 /* unexpected cases */
349 /*------------------------------------------------------------------*/
351 sclsFromPtr(sym_link *ptr)
353 switch (DCL_TYPE (ptr))
376 /*------------------------------------------------------------------*/
377 /* pointerTypes - do the computation for the pointer types */
378 /*------------------------------------------------------------------*/
380 pointerTypes (sym_link * ptr, sym_link * type)
385 /* find the first pointer type */
386 while (ptr && !IS_PTR (ptr))
389 /* could not find it */
390 if (!ptr || IS_SPEC (ptr))
393 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
394 pointerTypes (ptr->next, type);
398 /* change the pointer type depending on the
399 storage class of the type */
402 switch (SPEC_SCLS (type))
405 DCL_TYPE (ptr) = FPOINTER;
408 DCL_TYPE (ptr) = IPOINTER;
411 DCL_TYPE (ptr) = PPOINTER;
414 DCL_TYPE (ptr) = POINTER;
417 DCL_TYPE (ptr) = CPOINTER;
420 DCL_TYPE (ptr) = EEPPOINTER;
423 DCL_TYPE (ptr) = port->unqualified_pointer;
426 /* the storage class of type ends here */
427 SPEC_SCLS (type) = 0;
430 /* now change all the remaining unknown pointers
431 to generic pointers */
434 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
435 DCL_TYPE (ptr) = port->unqualified_pointer;
439 /* same for the type although it is highly unlikely that
440 type will have a pointer */
443 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
444 DCL_TYPE (type) = port->unqualified_pointer;
449 /*------------------------------------------------------------------*/
450 /* addDecl - adds a declarator @ the end of a chain */
451 /*------------------------------------------------------------------*/
453 addDecl (symbol * sym, int type, sym_link * p)
459 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
460 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
462 /* if we are passed a link then set head & tail */
471 head = tail = newLink (DECLARATOR);
472 DCL_TYPE (head) = type;
475 /* if this is the first entry */
483 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
485 sym->etype = mergeSpec (sym->etype, head, sym->name);
489 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
492 while (t->next != sym->etype)
495 tail->next = sym->etype;
499 sym->etype->next = head;
505 /* if the type is an unknown pointer and has
506 a tspec then take the storage class const & volatile
507 attribute from the tspec & make it those of this
511 //DCL_TYPE (p) == UPOINTER &&
514 if (!IS_SPEC (sym->etype))
516 sym->etype = sym->etype->next = newLink (SPECIFIER);
518 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
519 DCL_TSPEC (p) = NULL;
522 // if there is a function in this type chain
523 if (p && funcInChain(sym->type)) {
524 processFuncArgs (sym);
530 /*------------------------------------------------------------------
531 checkTypeSanity: prevent the user from doing e.g.:
533 ------------------------------------------------------------------*/
534 void checkTypeSanity(sym_link *etype, char *name) {
538 if (getenv("DEBUG_SANITY")) {
539 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
544 if (!IS_SPEC(etype)) {
545 if (getenv("DEBUG_SANITY")) {
546 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
551 noun=nounName(etype);
553 if (getenv("DEBUG_SANITY")) {
554 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
557 if ((SPEC_NOUN(etype)==V_CHAR ||
558 SPEC_NOUN(etype)==V_FLOAT ||
559 SPEC_NOUN(etype)==V_FIXED16X16 ||
560 SPEC_NOUN(etype)==V_DOUBLE ||
561 SPEC_NOUN(etype)==V_VOID) &&
562 (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
563 // long or short for char float double or void
564 werror (E_LONG_OR_SHORT_INVALID, noun, name);
566 if ((SPEC_NOUN(etype)==V_FLOAT ||
567 SPEC_NOUN(etype)==V_FIXED16X16 ||
568 SPEC_NOUN(etype)==V_DOUBLE ||
569 SPEC_NOUN(etype)==V_VOID) &&
570 (etype->select.s.b_signed || SPEC_USIGN(etype))) {
571 // signed or unsigned for float double or void
572 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
575 // special case for "short"
576 if (SPEC_SHORT(etype)) {
577 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
578 SPEC_SHORT(etype) = 0;
582 "const a;" or "data b;" or "signed s" or "long l"
584 if (!SPEC_NOUN(etype)) {
585 SPEC_NOUN(etype)=V_INT;
588 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
589 /* a "plain" int bitfield is unsigned */
590 if (SPEC_NOUN(etype)==V_BIT ||
591 SPEC_NOUN(etype)==V_SBIT) {
592 if (!etype->select.s.b_signed)
593 SPEC_USIGN(etype) = 1;
596 if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
597 // signed AND unsigned
598 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
600 if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
602 werror (E_LONG_AND_SHORT_INVALID, noun, name);
607 /*------------------------------------------------------------------*/
608 /* mergeSpec - merges two specifiers and returns the new one */
609 /*------------------------------------------------------------------*/
611 mergeSpec (sym_link * dest, sym_link * src, char *name)
613 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
615 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
618 werror (E_SYNTAX_ERROR, yytext);
619 // the show must go on
624 if (SPEC_NOUN(src)) {
625 if (!SPEC_NOUN(dest)) {
626 SPEC_NOUN(dest)=SPEC_NOUN(src);
628 /* we shouldn't redeclare the type */
629 if (getenv("DEBUG_SANITY")) {
630 fprintf (stderr, "mergeSpec: ");
632 werror(E_TWO_OR_MORE_DATA_TYPES, name);
636 if (SPEC_SCLS(src)) {
637 /* if destination has no storage class */
638 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
639 SPEC_SCLS (dest) = SPEC_SCLS (src);
641 if (getenv("DEBUG_SANITY")) {
642 fprintf (stderr, "mergeSpec: ");
644 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
648 /* copy all the specifications */
650 // we really should do:
652 if (SPEC_what(src)) {
653 if (SPEC_what(dest)) {
654 werror(W_DUPLICATE_SPEC, "what");
656 SPEC_what(dst)|=SPEC_what(src);
659 // but there are more important thing right now
661 SPEC_LONG (dest) |= SPEC_LONG (src);
662 SPEC_SHORT(dest) |= SPEC_SHORT(src);
663 SPEC_USIGN (dest) |= SPEC_USIGN (src);
664 dest->select.s.b_signed|=src->select.s.b_signed;
665 SPEC_STAT (dest) |= SPEC_STAT (src);
666 SPEC_EXTR (dest) |= SPEC_EXTR (src);
667 SPEC_CONST(dest) |= SPEC_CONST (src);
668 SPEC_ABSA (dest) |= SPEC_ABSA (src);
669 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
670 SPEC_ADDR (dest) |= SPEC_ADDR (src);
671 SPEC_OCLS (dest) = SPEC_OCLS (src);
672 SPEC_BLEN (dest) |= SPEC_BLEN (src);
673 SPEC_BSTR (dest) |= SPEC_BSTR (src);
674 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
675 SPEC_ENUM (dest) |= SPEC_ENUM (src);
676 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
677 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
679 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
680 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
682 /* these are the only function attributes that will be set
683 in a specifier while parsing */
684 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
685 FUNC_BANKED(dest) |= FUNC_BANKED(src);
686 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
687 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
688 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
689 FUNC_ISISR(dest) |= FUNC_ISISR(src);
690 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
691 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
692 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
693 FUNC_INTNO(dest) |= FUNC_INTNO(src);
694 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
699 /*------------------------------------------------------------------*/
700 /* genSymName - generates and returns a name used for anonymous vars */
701 /*------------------------------------------------------------------*/
703 genSymName (int level)
705 static int gCount = 0;
706 static char gname[SDCC_NAME_MAX + 1];
708 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
712 /*------------------------------------------------------------------*/
713 /* getSpec - returns the specifier part from a declaration chain */
714 /*------------------------------------------------------------------*/
716 getSpec (sym_link * p)
721 while (p && !(IS_SPEC (p)))
727 /*------------------------------------------------------------------*/
728 /* newCharLink() - creates an char type */
729 /*------------------------------------------------------------------*/
735 p = newLink (SPECIFIER);
736 SPEC_NOUN (p) = V_CHAR;
741 /*------------------------------------------------------------------*/
742 /* newFloatLink - a new Float type */
743 /*------------------------------------------------------------------*/
749 p = newLink (SPECIFIER);
750 SPEC_NOUN (p) = V_FLOAT;
755 /*------------------------------------------------------------------*/
756 /* newFixed16x16Link - a new Float type */
757 /*------------------------------------------------------------------*/
763 p = newLink (SPECIFIER);
764 SPEC_NOUN (p) = V_FIXED16X16;
769 /*------------------------------------------------------------------*/
770 /* newLongLink() - new long type */
771 /*------------------------------------------------------------------*/
777 p = newLink (SPECIFIER);
778 SPEC_NOUN (p) = V_INT;
784 /*------------------------------------------------------------------*/
785 /* newIntLink() - creates an int type */
786 /*------------------------------------------------------------------*/
792 p = newLink (SPECIFIER);
793 SPEC_NOUN (p) = V_INT;
798 /*------------------------------------------------------------------*/
799 /* newBoolLink() - creates an bool type */
800 /*------------------------------------------------------------------*/
806 p = newLink (SPECIFIER);
807 SPEC_NOUN (p) = V_BIT;
812 /*------------------------------------------------------------------*/
813 /* getSize - returns size of a type chain in bits */
814 /*------------------------------------------------------------------*/
816 getSize (sym_link * p)
818 /* if nothing return 0 */
822 { /* if this is the specifier then */
823 switch (SPEC_NOUN (p))
824 { /* depending on the specifier type */
826 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
836 return SPEC_STRUCT (p)->size;
843 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
849 /* this is a declarator */
850 switch (DCL_TYPE (p))
854 return DCL_ELEM (p) * getSize (p->next);
856 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
857 // "can not tell the size of an array[]");
868 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
877 /*---------------------------------------------------------------------*/
878 /* getAllocSize - returns size of a type chain in bytes for allocation */
879 /*---------------------------------------------------------------------*/
881 getAllocSize (sym_link *p)
883 if (IS_STRUCT (p) && SPEC_STRUCT (p)->type == STRUCT)
885 /* if this is a struct specifier then */
886 /* calculate the size as it could end */
887 /* with an array of unspecified length */
888 symbol *sflds = SPEC_STRUCT (p)->fields;
890 while (sflds && sflds->next)
893 if (sflds && !IS_BITFIELD (sflds->type))
894 return sflds->offset + getAllocSize (sflds->type);
896 return SPEC_STRUCT (p)->size;
902 /*------------------------------------------------------------------*/
903 /* bitsForType - returns # of bits required to store this type */
904 /*------------------------------------------------------------------*/
906 bitsForType (sym_link * p)
908 /* if nothing return 0 */
913 { /* if this is the specifier then */
915 switch (SPEC_NOUN (p))
916 { /* depending on the specifier type */
918 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
920 return FLOATSIZE * 8;
928 return SPEC_STRUCT (p)->size * 8;
935 return SPEC_BLEN (p);
941 /* this is a specifier */
942 switch (DCL_TYPE (p))
945 return DCL_ELEM (p) * getSize (p->next) * 8;
949 return (PTRSIZE * 8);
954 return (FPTRSIZE * 8);
956 return (GPTRSIZE * 8);
963 /*------------------------------------------------------------------*/
964 /* copySymbolChain - copies a symbol chain */
965 /*------------------------------------------------------------------*/
967 copySymbolChain (symbol * src)
974 dest = copySymbol (src);
975 dest->next = copySymbolChain (src->next);
979 /*------------------------------------------------------------------*/
980 /* copySymbol - makes a copy of a symbol */
981 /*------------------------------------------------------------------*/
983 copySymbol (symbol * src)
990 dest = newSymbol (src->name, src->level);
991 memcpy (dest, src, sizeof (symbol));
992 dest->level = src->level;
993 dest->block = src->block;
994 dest->ival = copyIlist (src->ival);
995 dest->type = copyLinkChain (src->type);
996 dest->etype = getSpec (dest->type);
998 dest->key = src->key;
999 dest->allocreq = src->allocreq;
1003 /*------------------------------------------------------------------*/
1004 /* reverseSyms - reverses the links for a symbol chain */
1005 /*------------------------------------------------------------------*/
1007 reverseSyms (symbol * sym)
1009 symbol *prev, *curr, *next;
1024 sym->next = (void *) NULL;
1028 /*------------------------------------------------------------------*/
1029 /* reverseLink - reverses the links for a type chain */
1030 /*------------------------------------------------------------------*/
1032 reverseLink (sym_link * type)
1034 sym_link *prev, *curr, *next;
1049 type->next = (void *) NULL;
1053 /*------------------------------------------------------------------*/
1054 /* addSymChain - adds a symbol chain to the symboltable */
1055 /*------------------------------------------------------------------*/
1057 addSymChain (symbol ** symHead)
1059 symbol *sym = *symHead;
1060 symbol *csym = NULL;
1064 for (; sym != NULL; sym = sym->next)
1066 changePointer(sym->type);
1067 checkTypeSanity(sym->etype, sym->name);
1069 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1072 /* if already exists in the symbol table then check if
1073 one of them is an extern definition if yes then
1074 then check if the type match, if the types match then
1075 delete the current entry and add the new entry */
1076 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1077 csym->level == sym->level) {
1079 /* If the previous definition was for an array with incomplete */
1080 /* type, and the new definition has completed the type, update */
1081 /* the original type to match */
1082 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1083 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1085 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1086 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1090 /* If only one of the definitions used the "at" keyword, copy */
1091 /* the address to the other. */
1092 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1093 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1095 SPEC_ABSA (sym->etype) = 1;
1096 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1098 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1099 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1101 SPEC_ABSA (csym->etype) = 1;
1102 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1107 if (csym->ival && sym->ival)
1109 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1113 /* one definition extern ? */
1114 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1115 werror (E_EXTERN_MISMATCH, sym->name);
1117 werror (E_DUPLICATE, sym->name);
1118 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1120 fprintf (stderr, "from type '");
1121 printTypeChain (csym->type, stderr);
1122 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1123 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1124 fprintf (stderr, "'\nto type '");
1125 printTypeChain (sym->type, stderr);
1126 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1127 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1128 fprintf (stderr, "'\n");
1133 if (csym->ival && !sym->ival)
1134 sym->ival = csym->ival;
1136 /* delete current entry */
1137 deleteSym (SymbolTab, csym, csym->name);
1138 deleteFromSeg(csym);
1140 symPtrPtr = symHead;
1141 while (*symPtrPtr && *symPtrPtr != csym)
1142 symPtrPtr = &(*symPtrPtr)->next;
1143 if (*symPtrPtr == csym)
1144 *symPtrPtr = csym->next;
1149 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1154 /*------------------------------------------------------------------*/
1155 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1156 /*------------------------------------------------------------------*/
1158 funcInChain (sym_link * lnk)
1169 /*------------------------------------------------------------------*/
1170 /* structElemType - returns the type info of a struct member */
1171 /*------------------------------------------------------------------*/
1173 structElemType (sym_link * stype, value * id)
1175 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1176 sym_link *type, *etype;
1177 sym_link *petype = getSpec (stype);
1181 /* look for the id */
1184 if (strcmp (fields->rname, id->name) == 0)
1186 type = copyLinkChain (fields->type);
1187 etype = getSpec (type);
1188 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1189 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1191 SPEC_CONST (type) |= SPEC_CONST (stype);
1193 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1196 fields = fields->next;
1200 werror (E_NOT_MEMBER, id->name);
1202 // the show must go on
1203 return newIntLink();
1206 /*------------------------------------------------------------------*/
1207 /* getStructElement - returns element of a tructure definition */
1208 /*------------------------------------------------------------------*/
1210 getStructElement (structdef * sdef, symbol * sym)
1214 for (field = sdef->fields; field; field = field->next)
1215 if (strcmp (field->name, sym->name) == 0)
1218 werror (E_NOT_MEMBER, sym->name);
1220 return sdef->fields;
1223 /*------------------------------------------------------------------*/
1224 /* compStructSize - computes the size of a structure */
1225 /*------------------------------------------------------------------*/
1227 compStructSize (int su, structdef * sdef)
1229 int sum = 0, usum = 0;
1233 /* for the identifiers */
1234 loop = sdef->fields;
1237 /* create the internal name for this variable */
1238 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1243 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1245 /* if this is a bit field */
1248 /* change it to a unsigned bit */
1249 SPEC_NOUN (loop->etype) = V_BITFIELD;
1250 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1251 /* a "plain" int bitfield is unsigned */
1252 if (!loop->etype->select.s.b_signed)
1253 SPEC_USIGN(loop->etype) = 1;
1255 SPEC_BLEN (loop->etype) = loop->bitVar;
1257 if (loop->bitVar == BITVAR_PAD) {
1258 /* A zero length bitfield forces padding */
1259 SPEC_BSTR (loop->etype) = bitOffset;
1260 SPEC_BLEN (loop->etype) = 0;
1265 if (bitOffset == 8) {
1269 /* check if this fit into the remaining */
1270 /* bits of this byte else align it to the */
1271 /* next byte boundary */
1272 if (loop->bitVar <= (8 - bitOffset)) {
1273 /* fits into current byte */
1275 SPEC_BSTR (loop->etype) = bitOffset;
1276 bitOffset += loop->bitVar;
1278 else if (!bitOffset) {
1279 /* does not fit, but is already byte aligned */
1281 SPEC_BSTR (loop->etype) = bitOffset;
1282 bitOffset += loop->bitVar;
1285 /* does not fit; need to realign first */
1287 loop->offset = (su == UNION ? sum = 0 : sum);
1289 SPEC_BSTR (loop->etype) = bitOffset;
1290 bitOffset += loop->bitVar;
1292 while (bitOffset>8) {
1299 /* This is a non-bit field. Make sure we are */
1300 /* byte aligned first */
1303 loop->offset = (su == UNION ? sum = 0 : sum);
1307 checkDecl (loop, 1);
1308 sum += getSize (loop->type);
1313 /* if union then size = sizeof largest field */
1315 /* For UNION, round up after each field */
1316 sum += ((bitOffset+7)/8);
1317 usum = max (usum, sum);
1322 /* For STRUCT, round up after all fields processed */
1324 sum += ((bitOffset+7)/8);
1326 return (su == UNION ? usum : sum);
1329 /*-------------------------------------------------------------------*/
1330 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1331 /* an enclosing struct/union */
1332 /*-------------------------------------------------------------------*/
1334 promoteAnonStructs (int su, structdef * sdef)
1343 tofield = &sdef->fields;
1344 field = sdef->fields;
1347 nextfield = field->next;
1348 if (!*field->name && IS_STRUCT (field->type))
1350 /* Found an anonymous struct/union. Replace it */
1351 /* with the fields it contains and adjust all */
1354 base = field->offset;
1355 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1357 continue; /* just in case it's empty */
1359 *tofield = subfield;
1362 /* check for field name conflicts resulting from promotion */
1363 dupfield = sdef->fields;
1364 while (dupfield && dupfield != subfield)
1366 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1368 werrorfl (subfield->fileDef, subfield->lineDef,
1370 su==STRUCT ? "struct" : "union",
1372 werrorfl (dupfield->fileDef, dupfield->lineDef,
1375 dupfield = dupfield->next;
1378 subfield->offset += base;
1380 subfield = subfield->next;
1384 subfield->next = nextfield;
1385 tofield = &subfield->next;
1388 tofield = &field->next;
1394 /*------------------------------------------------------------------*/
1395 /* checkSClass - check the storage class specification */
1396 /*------------------------------------------------------------------*/
1398 checkSClass (symbol * sym, int isProto)
1402 if (getenv("DEBUG_SANITY")) {
1403 fprintf (stderr, "checkSClass: %s \n", sym->name);
1406 /* type is literal can happen for enums change
1408 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1409 SPEC_SCLS (sym->etype) = S_AUTO;
1411 /* if sfr or sbit then must also be volatile */
1412 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1413 SPEC_SCLS (sym->etype) == S_SFR)
1415 SPEC_VOLATILE (sym->etype) = 1;
1418 /* if absolute address given then it mark it as
1419 volatile -- except in the PIC port */
1421 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1422 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1423 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1426 if (IS_ABSOLUTE (sym->etype))
1427 SPEC_VOLATILE (sym->etype) = 1;
1429 if (TARGET_IS_MCS51 &&
1430 IS_ABSOLUTE (sym->etype) &&
1431 SPEC_SCLS (sym->etype) == S_SFR)
1436 if (SPEC_NOUN (sym->etype) == V_CHAR)
1438 else if (SPEC_LONG (sym->etype) == 0)
1443 addr = SPEC_ADDR (sym->etype);
1444 for (n=0; n<size; n+=8)
1445 if (((addr >> n) & 0xFF) < 0x80)
1446 werror (W_SFR_ABSRANGE, sym->name);
1449 /* If code memory is read only, then pointers to code memory */
1450 /* implicitly point to constants -- make this explicit */
1452 while (t && t->next) {
1453 if (IS_CODEPTR(t) && port->mem.code_ro) {
1454 if (IS_SPEC(t->next)) {
1455 SPEC_CONST (t->next) = 1;
1457 DCL_PTR_CONST (t->next) = 1;
1463 /* global variables declared const put into code */
1464 /* if no other storage class specified */
1465 if (sym->level == 0 &&
1466 SPEC_SCLS(sym->etype) == S_FIXED &&
1467 !IS_FUNC(sym->type)) {
1468 /* find the first non-array link */
1472 if (IS_CONSTANT (t)) {
1473 SPEC_SCLS (sym->etype) = S_CODE;
1477 /* global variable in code space is a constant */
1478 if (sym->level == 0 &&
1479 SPEC_SCLS (sym->etype) == S_CODE &&
1480 port->mem.code_ro) {
1481 /* find the first non-array link */
1488 DCL_PTR_CONST (t) = 1;
1492 /* if bit variable then no storage class can be */
1493 /* specified since bit is already a storage */
1494 if (IS_BITVAR (sym->etype) &&
1495 (SPEC_SCLS (sym->etype) != S_FIXED &&
1496 SPEC_SCLS (sym->etype) != S_SBIT &&
1497 SPEC_SCLS (sym->etype) != S_BIT)
1500 werror (E_BITVAR_STORAGE, sym->name);
1501 SPEC_SCLS (sym->etype) = S_FIXED;
1504 /* extern variables cannot be initialized */
1505 if (IS_EXTERN (sym->etype) && sym->ival)
1507 werror (E_EXTERN_INIT, sym->name);
1511 /* if this is an automatic symbol */
1512 if (sym->level && (options.stackAuto || reentrant)) {
1513 if (SPEC_SCLS (sym->etype) != S_BIT) {
1514 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1515 SPEC_SCLS (sym->etype) == S_FIXED ||
1516 SPEC_SCLS (sym->etype) == S_REGISTER ||
1517 SPEC_SCLS (sym->etype) == S_STACK ||
1518 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1519 SPEC_SCLS (sym->etype) = S_AUTO;
1521 /* storage class may only be specified for statics */
1522 if (!IS_STATIC(sym->etype)) {
1523 werror (E_AUTO_ASSUMED, sym->name);
1529 /* automatic symbols cannot be given */
1530 /* an absolute address ignore it */
1532 SPEC_ABSA (sym->etype) &&
1533 (options.stackAuto || reentrant))
1535 werror (E_AUTO_ABSA, sym->name);
1536 SPEC_ABSA (sym->etype) = 0;
1539 /* arrays & pointers cannot be defined for bits */
1540 /* SBITS or SFRs or BIT */
1541 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1542 !IS_FUNCPTR (sym->type) &&
1543 (SPEC_NOUN (sym->etype) == V_BIT ||
1544 SPEC_NOUN (sym->etype) == V_SBIT ||
1545 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1546 SPEC_SCLS (sym->etype) == S_SFR))
1547 werror (E_BIT_ARRAY, sym->name);
1549 /* if this is a bit|sbit then set length & start */
1550 if (SPEC_NOUN (sym->etype) == V_BIT ||
1551 SPEC_NOUN (sym->etype) == V_SBIT)
1553 SPEC_BLEN (sym->etype) = 1;
1554 SPEC_BSTR (sym->etype) = 0;
1558 /* variables declared in CODE space must have */
1559 /* initializers if not an extern */
1560 if (SPEC_SCLS (sym->etype) == S_CODE &&
1561 sym->ival == NULL &&
1564 port->mem.code_ro &&
1565 !IS_EXTERN (sym->etype) &&
1566 !funcInChain (sym->type))
1567 werror (E_CODE_NO_INIT, sym->name);
1570 /* if parameter or local variable then change */
1571 /* the storage class to reflect where the var will go */
1572 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1573 && !IS_STATIC(sym->etype)
1576 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1578 SPEC_SCLS (sym->etype) = (options.useXstack ?
1579 S_XSTACK : S_STACK);
1583 /* hack-o-matic! I see no reason why the useXstack option should ever
1584 * control this allocation, but the code was originally that way, and
1585 * changing it for non-390 ports breaks the compiler badly.
1587 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1588 1 : options.useXstack;
1589 SPEC_SCLS (sym->etype) = (useXdata ?
1595 /*------------------------------------------------------------------*/
1596 /* changePointer - change pointer to functions */
1597 /*------------------------------------------------------------------*/
1599 changePointer (sym_link * p)
1602 /* go thru the chain of declarations */
1603 /* if we find a pointer to a function */
1604 /* change it to a ptr to code area */
1605 /* unless the function is banked. */
1606 for (; p; p = p->next)
1608 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1609 DCL_TYPE (p) = port->unqualified_pointer;
1610 if (IS_PTR (p) && IS_FUNC (p->next))
1611 if (!IFFUNC_BANKED(p->next))
1612 DCL_TYPE (p) = CPOINTER;
1616 /*------------------------------------------------------------------*/
1617 /* checkDecl - does semantic validation of a declaration */
1618 /*------------------------------------------------------------------*/
1620 checkDecl (symbol * sym, int isProto)
1623 checkSClass (sym, isProto); /* check the storage class */
1624 changePointer (sym->type); /* change pointers if required */
1626 /* if this is an array without any dimension
1627 then update the dimension from the initial value */
1628 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1629 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1634 /*------------------------------------------------------------------*/
1635 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1636 /*------------------------------------------------------------------*/
1638 copyLinkChain (sym_link * p)
1640 sym_link *head, *curr, *loop;
1643 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1646 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1647 if (getenv ("SDCCCOPYSTRUCT")) // this breaks regression test bug-221220??
1648 if (IS_STRUCT (loop))
1649 SPEC_STRUCT (loop) = copyStruct (SPEC_STRUCT (loop));
1650 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1658 /*------------------------------------------------------------------*/
1659 /* cleanUpBlock - cleansup the symbol table specified for all the */
1660 /* symbols in the given block */
1661 /*------------------------------------------------------------------*/
1663 cleanUpBlock (bucket ** table, int block)
1668 /* go thru the entire table */
1669 for (i = 0; i < 256; i++)
1671 for (chain = table[i]; chain; chain = chain->next)
1673 if (chain->block >= block)
1675 deleteSym (table, chain->sym, chain->name);
1681 /*------------------------------------------------------------------*/
1682 /* cleanUpLevel - cleansup the symbol table specified for all the */
1683 /* symbols in the given level */
1684 /*------------------------------------------------------------------*/
1686 cleanUpLevel (bucket ** table, int level)
1691 /* go thru the entire table */
1692 for (i = 0; i < 256; i++)
1694 for (chain = table[i]; chain; chain = chain->next)
1696 if (chain->level >= level)
1698 deleteSym (table, chain->sym, chain->name);
1704 /*------------------------------------------------------------------*/
1705 /* computeTypeOr - computes the resultant type from two types */
1706 /*------------------------------------------------------------------*/
1708 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1711 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1712 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1714 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1716 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1720 if (SPEC_USIGN (etype1))
1722 if ( IS_LITERAL (etype2)
1723 && floatFromVal (valFromType (etype2)) >= 0)
1724 SPEC_USIGN (reType) = 1;
1727 /* promote to int */
1728 SPEC_USIGN (reType) = 0;
1729 SPEC_NOUN (reType) = V_INT;
1732 else /* etype1 signed */
1734 if ( IS_LITERAL (etype2)
1735 && floatFromVal (valFromType (etype2)) <= 127)
1736 SPEC_USIGN (reType) = 0;
1739 /* promote to int */
1740 SPEC_USIGN (reType) = 0;
1741 SPEC_NOUN (reType) = V_INT;
1745 if (SPEC_USIGN (etype2))
1747 if ( IS_LITERAL (etype1)
1748 && floatFromVal (valFromType (etype1)) >= 0)
1749 SPEC_USIGN (reType) = 1;
1752 /* promote to int */
1753 SPEC_USIGN (reType) = 0;
1754 SPEC_NOUN (reType) = V_INT;
1757 else /* etype2 signed */
1759 if ( IS_LITERAL (etype1)
1760 && floatFromVal (valFromType (etype1)) <= 127)
1761 SPEC_USIGN (reType) = 0;
1764 /* promote to int */
1765 SPEC_USIGN (reType) = 0;
1766 SPEC_NOUN (reType) = V_INT;
1772 /*------------------------------------------------------------------*/
1773 /* computeType - computes the resultant type from two types */
1774 /*------------------------------------------------------------------*/
1776 computeType (sym_link * type1, sym_link * type2,
1777 RESULT_TYPE resultType, int op)
1781 sym_link *etype1 = getSpec (type1);
1784 etype2 = type2 ? getSpec (type2) : type1;
1786 /* if one of them is a float then result is a float */
1787 /* here we assume that the types passed are okay */
1788 /* and can be cast to one another */
1789 /* which ever is greater in size */
1790 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1791 rType = newFloatLink ();
1792 /* if both are fixed16x16 then result is float */
1793 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1794 rType = newFixed16x16Link();
1795 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1796 rType = newFloatLink ();
1797 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1798 rType = newFloatLink ();
1800 /* if both are bitvars choose the larger one */
1801 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1802 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1803 copyLinkChain (type1) : copyLinkChain (type1);
1805 /* if only one of them is a bit variable then the other one prevails */
1806 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1808 rType = copyLinkChain (type2);
1809 /* bitfield can have up to 16 bits */
1810 if (getSize (etype1) > 1)
1811 SPEC_NOUN (getSpec (rType)) = V_INT;
1813 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1815 rType = copyLinkChain (type1);
1816 /* bitfield can have up to 16 bits */
1817 if (getSize (etype2) > 1)
1818 SPEC_NOUN (getSpec (rType)) = V_INT;
1820 /* if one of them is a pointer or array then that
1822 else if (IS_PTR (type1) || IS_ARRAY (type1))
1823 rType = copyLinkChain (type1);
1824 else if (IS_PTR (type2) || IS_ARRAY (type2))
1825 rType = copyLinkChain (type2);
1826 else if (getSize (type1) > getSize (type2))
1827 rType = copyLinkChain (type1);
1829 rType = copyLinkChain (type2);
1831 reType = getSpec (rType);
1833 /* avoid conflicting types */
1834 reType->select.s.b_signed = 0;
1836 /* if result is a literal then make not so */
1837 if (IS_LITERAL (reType))
1838 SPEC_SCLS (reType) = S_REGISTER;
1842 case RESULT_TYPE_CHAR:
1843 if (IS_BITVAR (reType))
1845 SPEC_NOUN (reType) = V_CHAR;
1846 SPEC_SCLS (reType) = 0;
1847 SPEC_USIGN (reType) = 0;
1851 case RESULT_TYPE_INT:
1852 case RESULT_TYPE_NONE:
1853 case RESULT_TYPE_OTHER:
1854 if (IS_BIT (reType))
1856 SPEC_NOUN (reType) = V_CHAR;
1857 SPEC_SCLS (reType) = 0;
1858 SPEC_USIGN (reType) = 0;
1861 else if (IS_BITFIELD (reType))
1863 /* could be smarter, but it depends on the op */
1864 /* this is for the worst case: a multiplication of 4 * 4 bit */
1865 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1866 SPEC_SCLS (reType) = 0;
1867 SPEC_USIGN (reType) = 0;
1870 else if (IS_CHAR (reType))
1872 if (op == '|' || op == '^')
1873 return computeTypeOr (etype1, etype2, reType);
1875 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1877 SPEC_USIGN (reType) = 1;
1882 SPEC_NOUN (reType) = V_INT;
1883 SPEC_USIGN (reType) = 0;
1886 /* TODO: should be in SDCCast.c */
1888 && ( !SPEC_USIGN (etype1)
1889 || !SPEC_USIGN (etype2)))
1891 SPEC_NOUN (reType) = V_INT;
1892 SPEC_USIGN (reType) = 0;
1901 /* SDCC's sign promotion:
1902 - if one or both operands are unsigned, the resultant type will be unsigned
1903 (except char, see below)
1904 - if an operand is promoted to a larger type (char -> int, int -> long),
1905 the larger type will be signed
1907 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1908 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1909 the standard. The standard demands, that the result has to be the same
1910 "as if" the promotion would have been performed:
1912 - if the result of an operation with two char's is promoted to a
1913 larger type, the result will be signed.
1915 More sophisticated are these:
1916 - if the result of an operation with two char's is a char again,
1917 the result will only then be unsigned, if both operands are
1918 unsigned. In all other cases the result will be signed.
1920 This seems to be contradictionary to the first two rules, but it makes
1921 real sense (all types are char's):
1923 A signed char can be negative; this must be preserved in the result
1926 Only if both operands are unsigned it's safe to make the result
1927 unsigned; this helps to avoid overflow:
1930 - ToDo: document '|', '^' and '&'
1932 Homework: - why is (200 * 200 < 0) true?
1933 - why is { char l = 200, r = 200; (r * l > 0) } true?
1936 if (!IS_FLOAT (reType)
1937 && ( (SPEC_USIGN (etype1)
1938 /* if this operand is promoted to a larger type,
1939 then it will be promoted to a signed type */
1940 && !(getSize (etype1) < getSize (reType))
1941 /* char require special handling */
1942 && !IS_CHAR (etype1))
1943 || /* same for 2nd operand */
1944 (SPEC_USIGN (etype2)
1945 && !(getSize (etype2) < getSize (reType))
1946 && !IS_CHAR (etype2))
1947 || /* if both are 'unsigned char' and not promoted
1948 let the result be unsigned too */
1949 ( SPEC_USIGN (etype1)
1950 && SPEC_USIGN (etype2)
1953 && IS_CHAR (reType))))
1954 SPEC_USIGN (reType) = 1;
1956 SPEC_USIGN (reType) = 0;
1961 /*--------------------------------------------------------------------*/
1962 /* compareType - will do type check return 1 if match, -1 if castable */
1963 /*--------------------------------------------------------------------*/
1965 compareType (sym_link * dest, sym_link * src)
1976 /* if dest is a declarator then */
1981 /* banked function pointer */
1982 if (IS_GENPTR (dest) && IS_GENPTR (src))
1984 if (IS_FUNC (src->next) && IS_VOID(dest->next))
1986 if (IS_FUNC (dest->next) && IS_VOID(src->next))
1988 return compareType (dest->next, src->next);
1991 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1993 //checkFunction(src,dest);
1995 return compareType (dest->next, src->next);
1997 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
2001 (IS_GENPTR (dest) ||
2002 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2005 if (IS_PTR (dest) && IS_ARRAY (src)) {
2006 value *val=aggregateToPointer (valFromType(src));
2007 int res=compareType (dest, val->type);
2008 Safe_free(val->type);
2012 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2013 return compareType (dest->next, src);
2016 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2022 /* if one is a specifier and the other is not */
2023 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2024 (IS_SPEC (dest) && !IS_SPEC (src)))
2027 /* if one of them is a void then ok */
2028 if (SPEC_NOUN (dest) == V_VOID &&
2029 SPEC_NOUN (src) != V_VOID)
2032 if (SPEC_NOUN (dest) != V_VOID &&
2033 SPEC_NOUN (src) == V_VOID)
2036 /* if they are both bitfields then if the lengths
2037 and starts don't match */
2038 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2039 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2040 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2043 /* it is a specifier */
2044 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2046 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2047 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2049 bitsForType (dest) == bitsForType (src))
2050 instead of the next two lines, but the regression tests fail with
2051 them; I guess it's a problem with replaceCheaperOp */
2052 getSize (dest) == getSize (src) &&
2053 !(!IS_BIT (dest) && IS_BIT (src)))
2055 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2060 else if (IS_STRUCT (dest))
2062 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2067 if (SPEC_LONG (dest) != SPEC_LONG (src))
2070 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2076 /*--------------------------------------------------------------------*/
2077 /* compareTypeExact - will do type check return 1 if match exactly */
2078 /*--------------------------------------------------------------------*/
2080 compareTypeExact (sym_link * dest, sym_link * src, int level)
2082 STORAGE_CLASS srcScls, destScls;
2093 /* if dest is a declarator then */
2098 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2099 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2101 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2103 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2107 value *exargs, *acargs, *checkValue;
2109 /* verify function return type */
2110 if (!compareTypeExact (dest->next, src->next, -1))
2112 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2114 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2116 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2119 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2123 /* compare expected args with actual args */
2124 exargs = FUNC_ARGS(dest);
2125 acargs = FUNC_ARGS(src);
2127 /* for all the expected args do */
2128 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2130 //checkTypeSanity(acargs->etype, acargs->name);
2132 if (IS_AGGREGATE (acargs->type))
2134 checkValue = copyValue (acargs);
2135 aggregateToPointer (checkValue);
2138 checkValue = acargs;
2141 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2146 /* if one them ended we have a problem */
2147 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2148 (!exargs && acargs && !IS_VOID (acargs->type)))
2152 return compareTypeExact (dest->next, src->next, level);
2159 /* if one is a specifier and the other is not */
2160 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2161 (IS_SPEC (dest) && !IS_SPEC (src)))
2164 /* if one of them is a void then ok */
2165 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2168 /* if they are both bitfields then if the lengths
2169 and starts don't match */
2170 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2171 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2172 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2175 if (IS_INTEGRAL (dest))
2177 /* signedness must match */
2178 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2180 /* size must match */
2181 if (SPEC_LONG (dest) != SPEC_LONG (src))
2183 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2187 if (IS_STRUCT (dest))
2189 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2193 if (SPEC_CONST (dest) != SPEC_CONST (src))
2195 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2197 if (SPEC_STAT (dest) != SPEC_STAT (src))
2199 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2201 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2204 destScls = SPEC_SCLS (dest);
2205 srcScls = SPEC_SCLS (src);
2207 /* Compensate for const to const code change in checkSClass() */
2208 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2210 if (srcScls == S_CODE && destScls == S_FIXED)
2212 if (destScls == S_CODE && srcScls == S_FIXED)
2216 /* compensate for allocGlobal() */
2217 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2218 && port->mem.default_globl_map == xdata
2222 if (level>0 && !SPEC_STAT (dest))
2224 /* Compensate for hack-o-matic in checkSClass() */
2225 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2227 if (destScls == S_FIXED)
2228 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2229 if (srcScls == S_FIXED)
2230 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2232 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2234 if (destScls == S_FIXED)
2236 if (srcScls == S_FIXED)
2241 if (srcScls != destScls)
2244 printf ("level = %d\n", level);
2245 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2246 SPEC_SCLS (src), SPEC_SCLS (dest));
2247 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2255 /*------------------------------------------------------------------*/
2256 /* inCalleeSaveList - return 1 if found in callee save list */
2257 /*------------------------------------------------------------------*/
2259 calleeCmp(void *p1, void *p2)
2261 return (strcmp((char *)p1, (char *)(p2)) == 0);
2265 inCalleeSaveList(char *s)
2267 if (options.all_callee_saves)
2269 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2272 /*-----------------------------------------------------------------*/
2273 /* aggregateToPointer: change an agggregate type function */
2274 /* argument to a pointer to that type. */
2275 /*-----------------------------------------------------------------*/
2277 aggregateToPointer (value * val)
2279 if (IS_AGGREGATE (val->type))
2281 /* if this is a structure */
2282 /* then we need to add a new link */
2283 if (IS_STRUCT (val->type))
2285 /* first lets add DECLARATOR type */
2286 sym_link *p = val->type;
2288 werror (W_STRUCT_AS_ARG, val->name);
2289 val->type = newLink (DECLARATOR);
2290 val->type->next = p;
2293 /* change to a pointer depending on the */
2294 /* storage class specified */
2295 switch (SPEC_SCLS (val->etype))
2298 DCL_TYPE (val->type) = IPOINTER;
2301 DCL_TYPE (val->type) = PPOINTER;
2304 if (SPEC_OCLS(val->etype)) {
2305 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2307 // this happens for (external) function parameters
2308 DCL_TYPE (val->type) = port->unqualified_pointer;
2314 DCL_TYPE (val->type) = POINTER;
2317 DCL_TYPE (val->type) = CPOINTER;
2320 DCL_TYPE (val->type) = FPOINTER;
2323 DCL_TYPE (val->type) = EEPPOINTER;
2326 DCL_TYPE (val->type) = port->unqualified_pointer;
2329 /* is there is a symbol associated then */
2330 /* change the type of the symbol as well */
2333 val->sym->type = copyLinkChain (val->type);
2334 val->sym->etype = getSpec (val->sym->type);
2339 /*------------------------------------------------------------------*/
2340 /* checkFunction - does all kinds of check on a function */
2341 /*------------------------------------------------------------------*/
2343 checkFunction (symbol * sym, symbol *csym)
2345 value *exargs, *acargs;
2349 if (getenv("DEBUG_SANITY")) {
2350 fprintf (stderr, "checkFunction: %s ", sym->name);
2353 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2355 werror(E_SYNTAX_ERROR, sym->name);
2359 /* make sure the type is complete and sane */
2360 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2362 /* if not type then some kind of error */
2366 /* if the function has no type then make it return int */
2367 if (!sym->type->next)
2368 sym->type->next = sym->etype = newIntLink ();
2370 /* function cannot return aggregate */
2371 if (IS_AGGREGATE (sym->type->next))
2373 werror (E_FUNC_AGGR, sym->name);
2377 /* check if this function is defined as calleeSaves
2378 then mark it as such */
2379 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2381 /* if interrupt service routine */
2382 /* then it cannot have arguments */
2383 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2385 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2386 werror (E_INT_ARGS, sym->name);
2387 FUNC_ARGS(sym->type)=NULL;
2391 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2393 werror (E_SHADOWREGS_NO_ISR, sym->name);
2397 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2399 acargs=acargs->next, argCnt++) {
2401 // this can happen for reentrant functions
2402 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2403 // the show must go on: synthesize a name and symbol
2404 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2405 acargs->sym = newSymbol (acargs->name, 1);
2406 SPEC_OCLS (acargs->etype) = istack;
2407 acargs->sym->type = copyLinkChain (acargs->type);
2408 acargs->sym->etype = getSpec (acargs->sym->type);
2409 acargs->sym->_isparm = 1;
2410 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2411 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2413 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2418 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2419 return 1; /* not defined nothing more to check */
2421 /* check if body already present */
2422 if (csym && IFFUNC_HASBODY(csym->type))
2424 werror (E_FUNC_BODY, sym->name);
2428 /* check the return value type */
2429 if (compareType (csym->type, sym->type) <= 0)
2431 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2432 printFromToType(csym->type, sym->type);
2436 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2438 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2441 /* I don't think this is necessary for interrupts. An isr is a */
2442 /* root in the calling tree. */
2443 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2444 (!FUNC_ISISR (sym->type)))
2446 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2449 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2451 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2454 /* Really, reentrant should match regardless of argCnt, but */
2455 /* this breaks some existing code (the fp lib functions). If */
2456 /* the first argument is always passed the same way, this */
2457 /* lax checking is ok (but may not be true for in future ports) */
2458 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2461 //printf("argCnt = %d\n",argCnt);
2462 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2465 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2467 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2470 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2472 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2476 /* compare expected args with actual args */
2477 exargs = FUNC_ARGS(csym->type);
2478 acargs = FUNC_ARGS(sym->type);
2480 /* for all the expected args do */
2483 exargs = exargs->next, acargs = acargs->next, argCnt++)
2485 if (getenv("DEBUG_SANITY")) {
2486 fprintf (stderr, "checkFunction: %s ", exargs->name);
2488 /* make sure the type is complete and sane */
2489 checkTypeSanity(exargs->etype, exargs->name);
2491 /* If the actual argument is an array, any prototype
2492 * will have modified it to a pointer. Duplicate that
2495 if (IS_AGGREGATE (acargs->type))
2497 checkValue = copyValue (acargs);
2498 aggregateToPointer (checkValue);
2502 checkValue = acargs;
2505 if (compareType (exargs->type, checkValue->type) <= 0)
2507 werror (E_ARG_TYPE, argCnt);
2508 printFromToType(exargs->type, checkValue->type);
2513 /* if one them ended we have a problem */
2514 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2515 (!exargs && acargs && !IS_VOID (acargs->type)))
2516 werror (E_ARG_COUNT);
2518 /* replace with this defition */
2519 sym->cdef = csym->cdef;
2520 deleteSym (SymbolTab, csym, csym->name);
2521 deleteFromSeg(csym);
2522 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2523 if (IS_EXTERN (csym->etype) && !
2524 IS_EXTERN (sym->etype))
2526 addSet (&publics, sym);
2531 /*------------------------------------------------------------------*/
2532 /* cdbStructBlock - calls struct printing for a blcks */
2533 /*------------------------------------------------------------------*/
2534 void cdbStructBlock (int block)
2537 bucket **table = StructTab;
2540 /* go thru the entire table */
2541 for (i = 0; i < 256; i++)
2543 for (chain = table[i]; chain; chain = chain->next)
2545 if (chain->block >= block)
2548 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2554 /*-----------------------------------------------------------------*/
2555 /* processFuncArgs - does some processing with function args */
2556 /*-----------------------------------------------------------------*/
2558 processFuncArgs (symbol * func)
2562 sym_link *funcType=func->type;
2564 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2565 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2567 /* find the function declaration within the type */
2568 while (funcType && !IS_FUNC(funcType))
2569 funcType=funcType->next;
2571 /* if this function has variable argument list */
2572 /* then make the function a reentrant one */
2573 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2574 FUNC_ISREENT(funcType)=1;
2576 /* check if this function is defined as calleeSaves
2577 then mark it as such */
2578 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2580 /* loop thru all the arguments */
2581 val = FUNC_ARGS(funcType);
2583 /* if it is void then remove parameters */
2584 if (val && IS_VOID (val->type))
2586 FUNC_ARGS(funcType) = NULL;
2590 /* reset regparm for the port */
2591 (*port->reset_regparms) ();
2593 /* if any of the arguments is an aggregate */
2594 /* change it to pointer to the same type */
2598 char buffer[SDCC_NAME_MAX+1];
2600 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2601 checkTypeSanity (val->etype, buffer);
2603 /* mark it as a register parameter if
2604 the function does not have VA_ARG
2605 and as port dictates */
2606 if (!IFFUNC_HASVARARGS(funcType) &&
2607 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2609 SPEC_REGPARM (val->etype) = 1;
2610 SPEC_ARGREG(val->etype) = argreg;
2611 } else if (IFFUNC_ISREENT(funcType)) {
2612 FUNC_HASSTACKPARM(funcType) = 1;
2615 if (IS_AGGREGATE (val->type))
2617 aggregateToPointer (val);
2624 /* if this is an internal generated function call */
2626 /* ignore --stack-auto for this one, we don't know how it is compiled */
2627 /* simply trust on --int-long-reent or --float-reent */
2628 if (IFFUNC_ISREENT(funcType)) {
2632 /* if this function is reentrant or */
2633 /* automatics r 2b stacked then nothing */
2634 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2638 val = FUNC_ARGS(funcType);
2643 /* if a symbolname is not given */
2644 /* synthesize a variable name */
2647 SNPRINTF (val->name, sizeof(val->name),
2648 "_%s_PARM_%d", func->name, pNum++);
2649 val->sym = newSymbol (val->name, 1);
2650 if (SPEC_SCLS(val->etype) == S_BIT)
2651 SPEC_OCLS (val->etype) = bit;
2653 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2654 val->sym->type = copyLinkChain (val->type);
2655 val->sym->etype = getSpec (val->sym->type);
2656 val->sym->_isparm = 1;
2657 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2659 /* ?? static functions shouldn't imply static parameters - EEP */
2660 if (IS_SPEC(func->etype)) {
2661 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2662 SPEC_STAT (func->etype);
2665 addSymChain (&val->sym);
2668 else /* symbol name given create synth name */
2671 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2672 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2673 val->sym->_isparm = 1;
2674 if (SPEC_SCLS(val->etype) == S_BIT)
2675 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2677 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2678 port->mem.default_local_map;
2681 /* ?? static functions shouldn't imply static parameters - EEP */
2682 if (IS_SPEC(func->etype)) {
2683 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2684 SPEC_STAT (func->etype);
2688 if (SPEC_OCLS (val->sym->etype) == pdata)
2689 val->sym->iaccess = 1;
2690 if (!isinSet(operKeyReset, val->sym)) {
2691 addSet (&operKeyReset, val->sym);
2692 applyToSet (operKeyReset, resetParmKey);
2698 /*-----------------------------------------------------------------*/
2699 /* isSymbolEqual - compares two symbols return 1 if they match */
2700 /*-----------------------------------------------------------------*/
2702 isSymbolEqual (symbol * dest, symbol * src)
2704 /* if pointers match then equal */
2708 /* if one of them is null then don't match */
2712 /* if both of them have rname match on rname */
2713 if (dest->rname[0] && src->rname[0])
2714 return (!strcmp (dest->rname, src->rname));
2716 /* otherwise match on name */
2717 return (!strcmp (dest->name, src->name));
2720 void PT(sym_link *type)
2722 printTypeChain(type,0);
2724 /*-----------------------------------------------------------------*/
2725 /* printTypeChain - prints the type chain in human readable form */
2726 /*-----------------------------------------------------------------*/
2728 printTypeChain (sym_link * start, FILE * of)
2732 sym_link * type, * search;
2742 fprintf (of, "void");
2746 /* Print the chain as it is written in the source: */
2747 /* start with the last entry. */
2748 /* However, the storage class at the end of the */
2749 /* chain reall applies to the first in the chain! */
2751 for (type = start; type && type->next; type = type->next)
2754 scls=SPEC_SCLS(type);
2762 case S_DATA: fprintf (of, "data-"); break;
2763 case S_XDATA: fprintf (of, "xdata-"); break;
2764 case S_SFR: fprintf (of, "sfr-"); break;
2765 case S_SBIT: fprintf (of, "sbit-"); break;
2766 case S_CODE: fprintf (of, "code-"); break;
2767 case S_IDATA: fprintf (of, "idata-"); break;
2768 case S_PDATA: fprintf (of, "pdata-"); break;
2769 case S_LITERAL: fprintf (of, "literal-"); break;
2770 case S_STACK: fprintf (of, "stack-"); break;
2771 case S_XSTACK: fprintf (of, "xstack-"); break;
2772 case S_BIT: fprintf (of, "bit-"); break;
2773 case S_EEPROM: fprintf (of, "eeprom-"); break;
2780 if (!IS_FUNC(type)) {
2781 if (DCL_PTR_VOLATILE (type)) {
2782 fprintf (of, "volatile-");
2784 if (DCL_PTR_CONST (type)) {
2785 fprintf (of, "const-");
2788 switch (DCL_TYPE (type))
2791 fprintf (of, "function %s %s",
2792 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2793 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2795 for (args = FUNC_ARGS(type);
2798 printTypeChain(args->type, of);
2805 fprintf (of, "generic* ");
2808 fprintf (of, "code* ");
2811 fprintf (of, "xdata* ");
2814 fprintf (of, "eeprom* ");
2817 fprintf (of, "near* ");
2820 fprintf (of, "idata* ");
2823 fprintf (of, "pdata* ");
2826 fprintf (of, "unknown* ");
2829 if (DCL_ELEM(type)) {
2830 fprintf (of, "[%d] ", DCL_ELEM(type));
2832 fprintf (of, "[] ");
2839 if (SPEC_VOLATILE (type))
2840 fprintf (of, "volatile-");
2841 if (SPEC_CONST (type))
2842 fprintf (of, "const-");
2843 if (SPEC_USIGN (type))
2844 fprintf (of, "unsigned-");
2845 switch (SPEC_NOUN (type))
2849 fprintf (of, "long-");
2850 fprintf (of, "int");
2854 fprintf (of, "char");
2858 fprintf (of, "void");
2862 fprintf (of, "float");
2866 fprintf (of, "fixed16x16");
2870 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2874 fprintf (of, "sbit");
2878 fprintf (of, "bit");
2882 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2886 fprintf (of, "double");
2890 fprintf (of, "unknown type");
2894 /* search entry in list before "type" */
2895 for (search = start; search && search->next != type;)
2896 search = search->next;
2905 /*--------------------------------------------------------------------*/
2906 /* printTypeChainRaw - prints the type chain in human readable form */
2907 /* in the raw data structure ordering */
2908 /*--------------------------------------------------------------------*/
2910 printTypeChainRaw (sym_link * start, FILE * of)
2923 fprintf (of, "void");
2933 if (!IS_FUNC(type)) {
2934 if (DCL_PTR_VOLATILE (type)) {
2935 fprintf (of, "volatile-");
2937 if (DCL_PTR_CONST (type)) {
2938 fprintf (of, "const-");
2941 switch (DCL_TYPE (type))
2944 fprintf (of, "function %s %s",
2945 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2946 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2948 for (args = FUNC_ARGS(type);
2951 printTypeChain(args->type, of);
2958 fprintf (of, "generic* ");
2961 fprintf (of, "code* ");
2964 fprintf (of, "xdata* ");
2967 fprintf (of, "eeprom* ");
2970 fprintf (of, "near* ");
2973 fprintf (of, "idata* ");
2976 fprintf (of, "pdata* ");
2979 fprintf (of, "unknown* ");
2982 if (DCL_ELEM(type)) {
2983 fprintf (of, "[%d] ", DCL_ELEM(type));
2985 fprintf (of, "[] ");
2989 if (DCL_TSPEC(type))
2992 printTypeChainRaw(DCL_TSPEC(type), of);
2996 else if (IS_SPEC (type))
2998 switch (SPEC_SCLS (type))
3000 case S_DATA: fprintf (of, "data-"); break;
3001 case S_XDATA: fprintf (of, "xdata-"); break;
3002 case S_SFR: fprintf (of, "sfr-"); break;
3003 case S_SBIT: fprintf (of, "sbit-"); break;
3004 case S_CODE: fprintf (of, "code-"); break;
3005 case S_IDATA: fprintf (of, "idata-"); break;
3006 case S_PDATA: fprintf (of, "pdata-"); break;
3007 case S_LITERAL: fprintf (of, "literal-"); break;
3008 case S_STACK: fprintf (of, "stack-"); break;
3009 case S_XSTACK: fprintf (of, "xstack-"); break;
3010 case S_BIT: fprintf (of, "bit-"); break;
3011 case S_EEPROM: fprintf (of, "eeprom-"); break;
3014 if (SPEC_VOLATILE (type))
3015 fprintf (of, "volatile-");
3016 if (SPEC_CONST (type))
3017 fprintf (of, "const-");
3018 if (SPEC_USIGN (type))
3019 fprintf (of, "unsigned-");
3020 switch (SPEC_NOUN (type))
3024 fprintf (of, "long-");
3025 fprintf (of, "int");
3029 fprintf (of, "char");
3033 fprintf (of, "void");
3037 fprintf (of, "float");
3041 fprintf (of, "fixed16x16");
3045 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3049 fprintf (of, "sbit");
3053 fprintf (of, "bit");
3057 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3061 fprintf (of, "double");
3065 fprintf (of, "unknown type");
3070 fprintf (of, "NOT_SPEC_OR_DECL");
3080 /*-----------------------------------------------------------------*/
3081 /* powof2 - returns power of two for the number if number is pow 2 */
3082 /*-----------------------------------------------------------------*/
3084 powof2 (TYPE_UDWORD num)
3097 if (n1s > 1 || nshifts == 0)
3113 symbol *__fps16x16_add;
3114 symbol *__fps16x16_sub;
3115 symbol *__fps16x16_mul;
3116 symbol *__fps16x16_div;
3117 symbol *__fps16x16_eq;
3118 symbol *__fps16x16_neq;
3119 symbol *__fps16x16_lt;
3120 symbol *__fps16x16_lteq;
3121 symbol *__fps16x16_gt;
3122 symbol *__fps16x16_gteq;
3124 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3125 symbol *__muldiv[3][3][2];
3126 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3127 sym_link *__multypes[3][2];
3128 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3129 symbol *__conv[2][3][2];
3130 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3131 symbol *__fp16x16conv[2][4][2];
3132 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3133 symbol *__rlrr[2][3][2];
3135 sym_link *floatType;
3136 sym_link *fixed16x16Type;
3139 _mangleFunctionName(char *in)
3141 if (port->getMangledFunctionName)
3143 return port->getMangledFunctionName(in);
3151 /*-----------------------------------------------------------------*/
3152 /* typeFromStr - create a typechain from an encoded string */
3153 /* basic types - 'c' - char */
3158 /* 'q' - fixed16x16 */
3160 /* '*' - pointer - default (GPOINTER) */
3161 /* modifiers - 'u' - unsigned */
3162 /* pointer modifiers - 'g' - generic */
3166 /* 'F' - function */
3167 /* examples : "ig*" - generic int * */
3168 /* "cx*" - char xdata * */
3169 /* "ui" - unsigned int */
3170 /*-----------------------------------------------------------------*/
3171 sym_link *typeFromStr (char *s)
3173 sym_link *r = newLink(DECLARATOR);
3185 r->class = SPECIFIER;
3186 SPEC_NOUN(r) = V_CHAR;
3190 r->class = SPECIFIER;
3191 SPEC_NOUN(r) = V_INT;
3194 r->class = SPECIFIER;
3195 SPEC_NOUN(r) = V_INT;
3199 r->class = SPECIFIER;
3200 SPEC_NOUN(r) = V_FLOAT;
3203 r->class = SPECIFIER;
3204 SPEC_NOUN(r) = V_FIXED16X16;
3207 r->class = SPECIFIER;
3208 SPEC_NOUN(r) = V_VOID;
3211 DCL_TYPE(r) = port->unqualified_pointer;
3218 assert(*(s+1)=='*');
3219 nr = newLink(DECLARATOR);
3224 DCL_TYPE(r) = GPOINTER;
3227 DCL_TYPE(r) = FPOINTER;
3230 DCL_TYPE(r) = CPOINTER;
3233 DCL_TYPE(r) = POINTER;
3236 DCL_TYPE(r) = FUNCTION;
3237 nr = newLink(DECLARATOR);
3240 DCL_TYPE(r) = CPOINTER;
3246 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3247 "typeFromStr: unknown type");
3250 if (IS_SPEC(r) && usign) {
3259 /*-----------------------------------------------------------------*/
3260 /* initCSupport - create functions for C support routines */
3261 /*-----------------------------------------------------------------*/
3265 const char *smuldivmod[] =
3269 const char *sbwd[] =
3271 "char", "int", "long", "fixed16x16",
3273 const char *fp16x16sbwd[] =
3275 "char", "int", "long", "float",
3281 const char *srlrr[] =
3286 int bwd, su, muldivmod, tofrom, rlrr;
3288 if (getenv("SDCC_NO_C_SUPPORT")) {
3289 /* for debugging only */
3293 floatType = newFloatLink ();
3294 fixed16x16Type = newFixed16x16Link ();
3296 for (bwd = 0; bwd < 3; bwd++)
3313 __multypes[bwd][0] = l;
3314 __multypes[bwd][1] = copyLinkChain (l);
3315 SPEC_USIGN (__multypes[bwd][1]) = 1;
3318 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3319 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3320 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3321 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3322 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3323 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3324 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3325 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3326 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3327 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3329 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3330 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3331 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3332 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3333 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3334 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3335 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3336 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3337 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3338 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3341 for (tofrom = 0; tofrom < 2; tofrom++)
3343 for (bwd = 0; bwd < 3; bwd++)
3345 for (su = 0; su < 2; su++)
3349 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3350 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3354 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3355 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3361 for (tofrom = 0; tofrom < 2; tofrom++)
3363 for (bwd = 0; bwd < 4; bwd++)
3365 for (su = 0; su < 2; su++)
3369 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3371 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3373 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3377 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3379 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3381 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3388 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3390 for (bwd = 0; bwd < 3; bwd++)
3392 for (su = 0; su < 2; su++)
3394 SNPRINTF (buffer, sizeof(buffer),
3396 smuldivmod[muldivmod],
3399 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3400 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3405 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3406 Therefore they've been merged into mulint() and mullong().
3409 for (bwd = 0; bwd < 3; bwd++)
3411 for (su = 0; su < 2; su++)
3413 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3416 SNPRINTF (buffer, sizeof(buffer),
3418 smuldivmod[muldivmod],
3421 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3422 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3430 for (su = 0; su < 2; su++)
3432 /* muluchar and mulschar are still separate functions, because e.g. the z80
3433 port is sign/zero-extending to int before calling mulint() */
3434 SNPRINTF (buffer, sizeof(buffer),
3436 smuldivmod[muldivmod],
3439 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3440 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3444 /* word and doubleword */
3445 for (bwd = 1; bwd < 3; bwd++)
3448 SNPRINTF (buffer, sizeof(buffer),
3450 smuldivmod[muldivmod],
3452 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3453 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3454 /* signed = unsigned */
3455 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3458 for (rlrr = 0; rlrr < 2; rlrr++)
3460 for (bwd = 0; bwd < 3; bwd++)
3462 for (su = 0; su < 2; su++)
3464 SNPRINTF (buffer, sizeof(buffer),
3469 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3470 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3476 /*-----------------------------------------------------------------*/
3477 /* initBuiltIns - create prototypes for builtin functions */
3478 /*-----------------------------------------------------------------*/
3484 if (!port->builtintable) return ;
3486 for (i = 0 ; port->builtintable[i].name ; i++) {
3487 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3488 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3489 FUNC_ISBUILTIN(sym->type) = 1;
3490 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3494 sym_link *validateLink(sym_link *l,
3501 if (l && l->class==select)
3506 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3507 " expected %s, got %s\n",
3508 macro, args, file, line,
3509 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3511 return l; // never reached, makes compiler happy.
3514 /*--------------------------------------------------------------------*/
3515 /* newEnumType - create an integer type compatible with enumerations */
3516 /*--------------------------------------------------------------------*/
3518 newEnumType (symbol *enumlist)
3526 type = newLink (SPECIFIER);
3527 SPEC_NOUN (type) = V_INT;
3531 /* Determine the range of the enumerated values */
3533 min = max = (int) floatFromVal (valFromType (sym->type));
3534 for (sym = sym->next; sym; sym = sym->next)
3536 v = (int) floatFromVal (valFromType (sym->type));
3543 /* Determine the smallest integer type that is compatible with this range */
3544 type = newLink (SPECIFIER);
3545 if (min>=0 && max<=255)
3547 SPEC_NOUN (type) = V_CHAR;
3548 SPEC_USIGN (type) = 1;
3550 else if (min>=-128 && max<=127)
3552 SPEC_NOUN (type) = V_CHAR;
3554 else if (min>=0 && max<=65535)
3556 SPEC_NOUN (type) = V_INT;
3557 SPEC_USIGN (type) = 1;
3559 else if (min>=-32768 && max<=32767)
3561 SPEC_NOUN (type) = V_INT;
3565 SPEC_NOUN (type) = V_INT;
3566 SPEC_LONG (type) = 1;
3568 SPEC_USIGN (type) = 1;