1 /*-------------------------------------------------------------------------
2 SDCCsymt.c - Code file for Symbols table related structures and MACRO's.
3 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22 -------------------------------------------------------------------------*/
29 value *aggregateToPointer (value *val);
30 void printTypeChainRaw (sym_link * start, FILE * of);
32 void printFromToType(sym_link *from, sym_link *to) {
33 fprintf (stderr, "from type '");
34 printTypeChain (from, stderr);
35 fprintf (stderr, "'\nto type '");
36 printTypeChain (to, stderr);
37 fprintf (stderr, "'\n");
41 char *nounName(sym_link *sl) {
42 switch (SPEC_NOUN(sl))
45 if (SPEC_LONG(sl)) return "long";
46 if (SPEC_SHORT(sl)) return "short";
49 case V_FLOAT: return "float";
50 case V_FIXED16X16: return "fixed16x16";
51 case V_CHAR: return "char";
52 case V_VOID: return "void";
53 case V_STRUCT: return "struct";
54 case V_LABEL: return "label";
55 case V_BITFIELD: return "bitfield";
56 case V_BIT: return "bit";
57 case V_SBIT: return "sbit";
58 case V_DOUBLE: return "double";
63 bucket *SymbolTab[256]; /* the symbol table */
64 bucket *StructTab[256]; /* the structure table */
65 bucket *TypedefTab[256]; /* the typedef table */
66 bucket *LabelTab[256]; /* the Label table */
67 bucket *enumTab[256]; /* enumerated table */
69 /*------------------------------------------------------------------*/
70 /* initSymt () - initialises symbol table related stuff */
71 /*------------------------------------------------------------------*/
77 for (i = 0; i < 256; i++)
78 SymbolTab[i] = StructTab[i] = (void *) NULL;
82 /*-----------------------------------------------------------------*/
83 /* newBucket - allocates & returns a new bucket */
84 /*-----------------------------------------------------------------*/
90 bp = Safe_alloc ( sizeof (bucket));
95 /*-----------------------------------------------------------------*/
96 /* hashKey - computes the hashkey given a symbol name */
97 /*-----------------------------------------------------------------*/
99 hashKey (const char *s)
101 unsigned long key = 0;
108 /*-----------------------------------------------------------------*/
109 /* addSym - adds a symbol to the hash Table */
110 /*-----------------------------------------------------------------*/
112 addSym (bucket ** stab,
119 int i; /* index into the hash Table */
120 bucket *bp; /* temp bucket * */
123 symbol *csym = (symbol *)sym;
125 if (getenv("DEBUG_SANITY")) {
126 fprintf (stderr, "addSym: %s ", sname);
128 /* make sure the type is complete and sane */
129 checkTypeSanity(csym->etype, csym->name);
132 /* prevent overflow of the (r)name buffers */
133 if (strlen(sname)>SDCC_SYMNAME_MAX) {
134 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
135 sname[SDCC_SYMNAME_MAX]='\0';
138 /* the symbols are always added at the head of the list */
140 /* get a free entry */
141 bp = Safe_alloc ( sizeof (bucket));
143 bp->sym = sym; /* update the symbol pointer */
144 bp->level = level; /* update the nest level */
146 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
148 /* if this is the first entry */
151 bp->prev = bp->next = (void *) NULL; /* point to nothing */
154 /* not first entry then add @ head of list */
164 /*-----------------------------------------------------------------*/
165 /* deleteSym - deletes a symbol from the hash Table entry */
166 /*-----------------------------------------------------------------*/
168 deleteSym (bucket ** stab, void *sym, char *sname)
176 /* find the symbol */
179 if (bp->sym == sym) /* found it then break out */
180 break; /* of the loop */
184 if (!bp) /* did not find it */
186 /* if this is the first one in the chain */
190 if (stab[i]) /* if chain ! empty */
191 stab[i]->prev = (void *) NULL;
193 /* middle || end of chain */
196 if (bp->next) /* if not end of chain */
197 bp->next->prev = bp->prev;
199 bp->prev->next = bp->next;
204 /*-----------------------------------------------------------------*/
205 /* findSym - finds a symbol in a table */
206 /*-----------------------------------------------------------------*/
208 findSym (bucket ** stab, void *sym, const char *sname)
212 bp = stab[hashKey (sname)];
215 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
220 return (bp ? bp->sym : (void *) NULL);
223 /*-----------------------------------------------------------------*/
224 /* findSymWithLevel - finds a symbol with a name & level */
225 /*-----------------------------------------------------------------*/
227 findSymWithLevel (bucket ** stab, symbol * sym)
231 bp = stab[hashKey (sym->name)];
234 ** do the search from the head of the list since the
235 ** elements are added at the head it is ensured that
236 ** we will find the deeper definitions before we find
237 ** the global ones. we need to check for symbols with
238 ** level <= to the level given, if levels match then block
239 ** numbers need to match as well
243 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
245 /* if this is parameter then nothing else need to be checked */
246 if (((symbol *) (bp->sym))->_isparm)
248 /* if levels match then block numbers should also match */
249 if (bp->level && bp->level == sym->level && bp->block == sym->block)
251 /* if levels don't match then we are okay */
252 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
254 /* if this is a global variable then we are ok too */
262 return (void *) NULL;
265 /*-----------------------------------------------------------------*/
266 /* findSymWithBlock - finds a symbol with name in with a block */
267 /*-----------------------------------------------------------------*/
269 findSymWithBlock (bucket ** stab, symbol * sym, int block)
273 bp = stab[hashKey (sym->name)];
276 if (strcmp (bp->name, sym->name) == 0 &&
282 return (bp ? bp->sym : (void *) NULL);
285 /*------------------------------------------------------------------*/
286 /* newSymbol () - returns a new pointer to a symbol */
287 /*------------------------------------------------------------------*/
289 newSymbol (char *name, int scope)
293 sym = Safe_alloc ( sizeof (symbol));
295 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
296 sym->level = scope; /* set the level */
297 sym->block = currBlockno;
298 sym->lineDef = mylineno; /* set the line number */
299 sym->fileDef = currFname;
303 /*------------------------------------------------------------------*/
304 /* newLink - creates a new link (declarator,specifier) */
305 /*------------------------------------------------------------------*/
307 newLink (SYM_LINK_CLASS select)
311 p = Safe_alloc ( sizeof (sym_link));
317 /*------------------------------------------------------------------*/
318 /* newStruct - creats a new structdef from the free list */
319 /*------------------------------------------------------------------*/
321 newStruct (char *tag)
325 s = Safe_alloc ( sizeof (structdef));
327 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
331 /*------------------------------------------------------------------*/
332 /* sclsFromPtr - Return the storage class a pointer points into. */
333 /* S_FIXED is returned for generic pointers or other */
334 /* unexpected cases */
335 /*------------------------------------------------------------------*/
337 sclsFromPtr(sym_link *ptr)
339 switch (DCL_TYPE (ptr))
362 /*------------------------------------------------------------------*/
363 /* pointerTypes - do the computation for the pointer types */
364 /*------------------------------------------------------------------*/
366 pointerTypes (sym_link * ptr, sym_link * type)
371 /* find the first pointer type */
372 while (ptr && !IS_PTR (ptr))
375 /* could not find it */
376 if (!ptr || IS_SPEC (ptr))
379 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
380 pointerTypes (ptr->next, type);
384 /* change the pointer type depending on the
385 storage class of the type */
388 switch (SPEC_SCLS (type))
391 DCL_TYPE (ptr) = FPOINTER;
394 DCL_TYPE (ptr) = IPOINTER;
397 DCL_TYPE (ptr) = PPOINTER;
400 DCL_TYPE (ptr) = POINTER;
403 DCL_TYPE (ptr) = CPOINTER;
406 DCL_TYPE (ptr) = EEPPOINTER;
409 DCL_TYPE (ptr) = port->unqualified_pointer;
412 /* the storage class of type ends here */
413 SPEC_SCLS (type) = 0;
416 /* now change all the remaining unknown pointers
417 to generic pointers */
420 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
421 DCL_TYPE (ptr) = port->unqualified_pointer;
425 /* same for the type although it is highly unlikely that
426 type will have a pointer */
429 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
430 DCL_TYPE (type) = port->unqualified_pointer;
435 /*------------------------------------------------------------------*/
436 /* addDecl - adds a declarator @ the end of a chain */
437 /*------------------------------------------------------------------*/
439 addDecl (symbol * sym, int type, sym_link * p)
445 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
446 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
448 /* if we are passed a link then set head & tail */
457 head = tail = newLink (DECLARATOR);
458 DCL_TYPE (head) = type;
461 /* if this is the first entry */
469 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
471 sym->etype = mergeSpec (sym->etype, head, sym->name);
475 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
478 while (t->next != sym->etype)
481 tail->next = sym->etype;
485 sym->etype->next = head;
491 /* if the type is an unknown pointer and has
492 a tspec then take the storage class const & volatile
493 attribute from the tspec & make it those of this
497 //DCL_TYPE (p) == UPOINTER &&
500 if (!IS_SPEC (sym->etype))
502 sym->etype = sym->etype->next = newLink (SPECIFIER);
504 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
505 DCL_TSPEC (p) = NULL;
508 // if there is a function in this type chain
509 if (p && funcInChain(sym->type)) {
510 processFuncArgs (sym);
516 /*------------------------------------------------------------------
517 checkTypeSanity: prevent the user from doing e.g.:
519 ------------------------------------------------------------------*/
520 void checkTypeSanity(sym_link *etype, char *name) {
524 if (getenv("DEBUG_SANITY")) {
525 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
530 if (!IS_SPEC(etype)) {
531 if (getenv("DEBUG_SANITY")) {
532 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
537 noun=nounName(etype);
539 if (getenv("DEBUG_SANITY")) {
540 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
543 if ((SPEC_NOUN(etype)==V_CHAR ||
544 SPEC_NOUN(etype)==V_FLOAT ||
545 SPEC_NOUN(etype)==V_FIXED16X16 ||
546 SPEC_NOUN(etype)==V_DOUBLE ||
547 SPEC_NOUN(etype)==V_VOID) &&
548 (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
549 // long or short for char float double or void
550 werror (E_LONG_OR_SHORT_INVALID, noun, name);
552 if ((SPEC_NOUN(etype)==V_FLOAT ||
553 SPEC_NOUN(etype)==V_FIXED16X16 ||
554 SPEC_NOUN(etype)==V_DOUBLE ||
555 SPEC_NOUN(etype)==V_VOID) &&
556 (etype->select.s.b_signed || SPEC_USIGN(etype))) {
557 // signed or unsigned for float double or void
558 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
561 // special case for "short"
562 if (SPEC_SHORT(etype)) {
563 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
564 SPEC_SHORT(etype) = 0;
568 "const a;" or "data b;" or "signed s" or "long l"
570 if (!SPEC_NOUN(etype)) {
571 SPEC_NOUN(etype)=V_INT;
574 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
575 /* a "plain" int bitfield is unsigned */
576 if (SPEC_NOUN(etype)==V_BIT ||
577 SPEC_NOUN(etype)==V_SBIT) {
578 if (!etype->select.s.b_signed)
579 SPEC_USIGN(etype) = 1;
582 if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
583 // signed AND unsigned
584 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
586 if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
588 werror (E_LONG_AND_SHORT_INVALID, noun, name);
593 /*------------------------------------------------------------------*/
594 /* mergeSpec - merges two specifiers and returns the new one */
595 /*------------------------------------------------------------------*/
597 mergeSpec (sym_link * dest, sym_link * src, char *name)
599 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
601 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
604 werror (E_SYNTAX_ERROR, yytext);
605 // the show must go on
610 if (SPEC_NOUN(src)) {
611 if (!SPEC_NOUN(dest)) {
612 SPEC_NOUN(dest)=SPEC_NOUN(src);
614 /* we shouldn't redeclare the type */
615 if (getenv("DEBUG_SANITY")) {
616 fprintf (stderr, "mergeSpec: ");
618 werror(E_TWO_OR_MORE_DATA_TYPES, name);
622 if (SPEC_SCLS(src)) {
623 /* if destination has no storage class */
624 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
625 SPEC_SCLS (dest) = SPEC_SCLS (src);
627 if (getenv("DEBUG_SANITY")) {
628 fprintf (stderr, "mergeSpec: ");
630 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
634 /* copy all the specifications */
636 // we really should do:
638 if (SPEC_what(src)) {
639 if (SPEC_what(dest)) {
640 werror(W_DUPLICATE_SPEC, "what");
642 SPEC_what(dst)|=SPEC_what(src);
645 // but there are more important thing right now
647 SPEC_LONG (dest) |= SPEC_LONG (src);
648 SPEC_SHORT(dest) |= SPEC_SHORT(src);
649 SPEC_USIGN (dest) |= SPEC_USIGN (src);
650 dest->select.s.b_signed|=src->select.s.b_signed;
651 SPEC_STAT (dest) |= SPEC_STAT (src);
652 SPEC_EXTR (dest) |= SPEC_EXTR (src);
653 SPEC_CONST(dest) |= SPEC_CONST (src);
654 SPEC_ABSA (dest) |= SPEC_ABSA (src);
655 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
656 SPEC_ADDR (dest) |= SPEC_ADDR (src);
657 SPEC_OCLS (dest) = SPEC_OCLS (src);
658 SPEC_BLEN (dest) |= SPEC_BLEN (src);
659 SPEC_BSTR (dest) |= SPEC_BSTR (src);
660 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
661 SPEC_ENUM (dest) |= SPEC_ENUM (src);
662 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
663 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
665 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
666 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
668 /* these are the only function attributes that will be set
669 in a specifier while parsing */
670 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
671 FUNC_BANKED(dest) |= FUNC_BANKED(src);
672 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
673 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
674 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
675 FUNC_ISISR(dest) |= FUNC_ISISR(src);
676 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
677 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
678 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
679 FUNC_INTNO(dest) |= FUNC_INTNO(src);
680 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
685 /*------------------------------------------------------------------*/
686 /* genSymName - generates and returns a name used for anonymous vars */
687 /*------------------------------------------------------------------*/
689 genSymName (int level)
691 static int gCount = 0;
692 static char gname[SDCC_NAME_MAX + 1];
694 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
698 /*------------------------------------------------------------------*/
699 /* getSpec - returns the specifier part from a declaration chain */
700 /*------------------------------------------------------------------*/
702 getSpec (sym_link * p)
707 while (p && !(IS_SPEC (p)))
713 /*------------------------------------------------------------------*/
714 /* newCharLink() - creates an char type */
715 /*------------------------------------------------------------------*/
721 p = newLink (SPECIFIER);
722 SPEC_NOUN (p) = V_CHAR;
727 /*------------------------------------------------------------------*/
728 /* newFloatLink - a new Float type */
729 /*------------------------------------------------------------------*/
735 p = newLink (SPECIFIER);
736 SPEC_NOUN (p) = V_FLOAT;
741 /*------------------------------------------------------------------*/
742 /* newFixed16x16Link - a new Float type */
743 /*------------------------------------------------------------------*/
749 p = newLink (SPECIFIER);
750 SPEC_NOUN (p) = V_FIXED16X16;
755 /*------------------------------------------------------------------*/
756 /* newLongLink() - new long type */
757 /*------------------------------------------------------------------*/
763 p = newLink (SPECIFIER);
764 SPEC_NOUN (p) = V_INT;
770 /*------------------------------------------------------------------*/
771 /* newIntLink() - creates an int type */
772 /*------------------------------------------------------------------*/
778 p = newLink (SPECIFIER);
779 SPEC_NOUN (p) = V_INT;
784 /*------------------------------------------------------------------*/
785 /* newBoolLink() - creates an bool type */
786 /*------------------------------------------------------------------*/
792 p = newLink (SPECIFIER);
793 SPEC_NOUN (p) = V_BIT;
798 /*------------------------------------------------------------------*/
799 /* getSize - returns size of a type chain in bits */
800 /*------------------------------------------------------------------*/
802 getSize (sym_link * p)
804 /* if nothing return 0 */
808 { /* if this is the specifier then */
809 switch (SPEC_NOUN (p))
810 { /* depending on the specifier type */
812 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
822 return SPEC_STRUCT (p)->size;
829 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
835 /* this is a declarator */
836 switch (DCL_TYPE (p))
840 return DCL_ELEM (p) * getSize (p->next);
842 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
843 // "can not tell the size of an array[]");
854 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
863 /*---------------------------------------------------------------------*/
864 /* getAllocSize - returns size of a type chain in bytes for allocation */
865 /*---------------------------------------------------------------------*/
867 getAllocSize (sym_link *p)
869 if (IS_STRUCT (p) && SPEC_STRUCT (p)->type == STRUCT)
871 /* if this is a struct specifier then */
872 /* calculate the size as it could end */
873 /* with an array of unspecified length */
874 symbol *sflds = SPEC_STRUCT (p)->fields;
876 while (sflds && sflds->next)
879 if (sflds && !IS_BITFIELD (sflds->type))
880 return sflds->offset + getAllocSize (sflds->type);
882 return SPEC_STRUCT (p)->size;
888 /*------------------------------------------------------------------*/
889 /* bitsForType - returns # of bits required to store this type */
890 /*------------------------------------------------------------------*/
892 bitsForType (sym_link * p)
894 /* if nothing return 0 */
899 { /* if this is the specifier then */
901 switch (SPEC_NOUN (p))
902 { /* depending on the specifier type */
904 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
906 return FLOATSIZE * 8;
914 return SPEC_STRUCT (p)->size * 8;
921 return SPEC_BLEN (p);
927 /* this is a specifier */
928 switch (DCL_TYPE (p))
931 return DCL_ELEM (p) * getSize (p->next) * 8;
935 return (PTRSIZE * 8);
940 return (FPTRSIZE * 8);
942 return (GPTRSIZE * 8);
949 /*------------------------------------------------------------------*/
950 /* copySymbolChain - copies a symbol chain */
951 /*------------------------------------------------------------------*/
953 copySymbolChain (symbol * src)
960 dest = copySymbol (src);
961 dest->next = copySymbolChain (src->next);
965 /*------------------------------------------------------------------*/
966 /* copySymbol - makes a copy of a symbol */
967 /*------------------------------------------------------------------*/
969 copySymbol (symbol * src)
976 dest = newSymbol (src->name, src->level);
977 memcpy (dest, src, sizeof (symbol));
978 dest->level = src->level;
979 dest->block = src->block;
980 dest->ival = copyIlist (src->ival);
981 dest->type = copyLinkChain (src->type);
982 dest->etype = getSpec (dest->type);
984 dest->key = src->key;
985 dest->allocreq = src->allocreq;
989 /*------------------------------------------------------------------*/
990 /* reverseSyms - reverses the links for a symbol chain */
991 /*------------------------------------------------------------------*/
993 reverseSyms (symbol * sym)
995 symbol *prev, *curr, *next;
1010 sym->next = (void *) NULL;
1014 /*------------------------------------------------------------------*/
1015 /* reverseLink - reverses the links for a type chain */
1016 /*------------------------------------------------------------------*/
1018 reverseLink (sym_link * type)
1020 sym_link *prev, *curr, *next;
1035 type->next = (void *) NULL;
1039 /*------------------------------------------------------------------*/
1040 /* addSymChain - adds a symbol chain to the symboltable */
1041 /*------------------------------------------------------------------*/
1043 addSymChain (symbol ** symHead)
1045 symbol *sym = *symHead;
1046 symbol *csym = NULL;
1050 for (; sym != NULL; sym = sym->next)
1052 changePointer(sym->type);
1053 checkTypeSanity(sym->etype, sym->name);
1055 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1058 /* if already exists in the symbol table then check if
1059 one of them is an extern definition if yes then
1060 then check if the type match, if the types match then
1061 delete the current entry and add the new entry */
1062 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1063 csym->level == sym->level) {
1065 /* If the previous definition was for an array with incomplete */
1066 /* type, and the new definition has completed the type, update */
1067 /* the original type to match */
1068 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1069 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1071 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1072 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1076 /* If only one of the definitions used the "at" keyword, copy */
1077 /* the address to the other. */
1078 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1079 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1081 SPEC_ABSA (sym->etype) = 1;
1082 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1084 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1085 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1087 SPEC_ABSA (csym->etype) = 1;
1088 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1093 if (csym->ival && sym->ival)
1095 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1099 /* one definition extern ? */
1100 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1101 werror (E_EXTERN_MISMATCH, sym->name);
1103 werror (E_DUPLICATE, sym->name);
1104 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1106 fprintf (stderr, "from type '");
1107 printTypeChain (csym->type, stderr);
1108 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1109 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1110 fprintf (stderr, "'\nto type '");
1111 printTypeChain (sym->type, stderr);
1112 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1113 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1114 fprintf (stderr, "'\n");
1119 if (csym->ival && !sym->ival)
1120 sym->ival = csym->ival;
1122 /* delete current entry */
1123 deleteSym (SymbolTab, csym, csym->name);
1124 deleteFromSeg(csym);
1126 symPtrPtr = symHead;
1127 while (*symPtrPtr && *symPtrPtr != csym)
1128 symPtrPtr = &(*symPtrPtr)->next;
1129 if (*symPtrPtr == csym)
1130 *symPtrPtr = csym->next;
1135 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1140 /*------------------------------------------------------------------*/
1141 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1142 /*------------------------------------------------------------------*/
1144 funcInChain (sym_link * lnk)
1155 /*------------------------------------------------------------------*/
1156 /* structElemType - returns the type info of a struct member */
1157 /*------------------------------------------------------------------*/
1159 structElemType (sym_link * stype, value * id)
1161 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1162 sym_link *type, *etype;
1163 sym_link *petype = getSpec (stype);
1167 /* look for the id */
1170 if (strcmp (fields->rname, id->name) == 0)
1172 type = copyLinkChain (fields->type);
1173 etype = getSpec (type);
1174 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1175 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1177 SPEC_CONST (type) |= SPEC_CONST (stype);
1179 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1182 fields = fields->next;
1186 werror (E_NOT_MEMBER, id->name);
1188 // the show must go on
1189 return newIntLink();
1192 /*------------------------------------------------------------------*/
1193 /* getStructElement - returns element of a tructure definition */
1194 /*------------------------------------------------------------------*/
1196 getStructElement (structdef * sdef, symbol * sym)
1200 for (field = sdef->fields; field; field = field->next)
1201 if (strcmp (field->name, sym->name) == 0)
1204 werror (E_NOT_MEMBER, sym->name);
1206 return sdef->fields;
1209 /*------------------------------------------------------------------*/
1210 /* compStructSize - computes the size of a structure */
1211 /*------------------------------------------------------------------*/
1213 compStructSize (int su, structdef * sdef)
1215 int sum = 0, usum = 0;
1219 /* for the identifiers */
1220 loop = sdef->fields;
1223 /* create the internal name for this variable */
1224 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1229 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1231 /* if this is a bit field */
1234 /* change it to a unsigned bit */
1235 SPEC_NOUN (loop->etype) = V_BITFIELD;
1236 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1237 /* a "plain" int bitfield is unsigned */
1238 if (!loop->etype->select.s.b_signed)
1239 SPEC_USIGN(loop->etype) = 1;
1241 SPEC_BLEN (loop->etype) = loop->bitVar;
1243 if (loop->bitVar == BITVAR_PAD) {
1244 /* A zero length bitfield forces padding */
1245 SPEC_BSTR (loop->etype) = bitOffset;
1246 SPEC_BLEN (loop->etype) = 0;
1251 if (bitOffset == 8) {
1255 /* check if this fit into the remaining */
1256 /* bits of this byte else align it to the */
1257 /* next byte boundary */
1258 if (loop->bitVar <= (8 - bitOffset)) {
1259 /* fits into current byte */
1261 SPEC_BSTR (loop->etype) = bitOffset;
1262 bitOffset += loop->bitVar;
1264 else if (!bitOffset) {
1265 /* does not fit, but is already byte aligned */
1267 SPEC_BSTR (loop->etype) = bitOffset;
1268 bitOffset += loop->bitVar;
1271 /* does not fit; need to realign first */
1273 loop->offset = (su == UNION ? sum = 0 : sum);
1275 SPEC_BSTR (loop->etype) = bitOffset;
1276 bitOffset += loop->bitVar;
1278 while (bitOffset>8) {
1285 /* This is a non-bit field. Make sure we are */
1286 /* byte aligned first */
1289 loop->offset = (su == UNION ? sum = 0 : sum);
1293 checkDecl (loop, 1);
1294 sum += getSize (loop->type);
1299 /* if union then size = sizeof largest field */
1301 /* For UNION, round up after each field */
1302 sum += ((bitOffset+7)/8);
1303 usum = max (usum, sum);
1308 /* For STRUCT, round up after all fields processed */
1310 sum += ((bitOffset+7)/8);
1312 return (su == UNION ? usum : sum);
1315 /*-------------------------------------------------------------------*/
1316 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1317 /* an enclosing struct/union */
1318 /*-------------------------------------------------------------------*/
1320 promoteAnonStructs (int su, structdef * sdef)
1329 tofield = &sdef->fields;
1330 field = sdef->fields;
1333 nextfield = field->next;
1334 if (!*field->name && IS_STRUCT (field->type))
1336 /* Found an anonymous struct/union. Replace it */
1337 /* with the fields it contains and adjust all */
1340 base = field->offset;
1341 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1343 continue; /* just in case it's empty */
1345 *tofield = subfield;
1348 /* check for field name conflicts resulting from promotion */
1349 dupfield = sdef->fields;
1350 while (dupfield && dupfield != subfield)
1352 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1354 werrorfl (subfield->fileDef, subfield->lineDef,
1356 su==STRUCT ? "struct" : "union",
1358 werrorfl (dupfield->fileDef, dupfield->lineDef,
1361 dupfield = dupfield->next;
1364 subfield->offset += base;
1366 subfield = subfield->next;
1370 subfield->next = nextfield;
1371 tofield = &subfield->next;
1374 tofield = &field->next;
1380 /*------------------------------------------------------------------*/
1381 /* checkSClass - check the storage class specification */
1382 /*------------------------------------------------------------------*/
1384 checkSClass (symbol * sym, int isProto)
1388 if (getenv("DEBUG_SANITY")) {
1389 fprintf (stderr, "checkSClass: %s \n", sym->name);
1392 /* type is literal can happen for enums change
1394 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1395 SPEC_SCLS (sym->etype) = S_AUTO;
1397 /* if sfr or sbit then must also be volatile */
1398 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1399 SPEC_SCLS (sym->etype) == S_SFR)
1401 SPEC_VOLATILE (sym->etype) = 1;
1404 /* if absolute address given then it mark it as
1405 volatile -- except in the PIC port */
1407 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1408 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1409 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1412 if (IS_ABSOLUTE (sym->etype))
1413 SPEC_VOLATILE (sym->etype) = 1;
1415 if (TARGET_IS_MCS51 &&
1416 IS_ABSOLUTE (sym->etype) &&
1417 SPEC_SCLS (sym->etype) == S_SFR)
1422 if (SPEC_NOUN (sym->etype) == V_CHAR)
1424 else if (SPEC_LONG (sym->etype) == 0)
1429 addr = SPEC_ADDR (sym->etype);
1430 for (n=0; n<size; n+=8)
1431 if (((addr >> n) & 0xFF) < 0x80)
1432 werror (W_SFR_ABSRANGE, sym->name);
1435 /* If code memory is read only, then pointers to code memory */
1436 /* implicitly point to constants -- make this explicit */
1438 while (t && t->next) {
1439 if (IS_CODEPTR(t) && port->mem.code_ro) {
1440 if (IS_SPEC(t->next)) {
1441 SPEC_CONST (t->next) = 1;
1443 DCL_PTR_CONST (t->next) = 1;
1449 /* global variables declared const put into code */
1450 /* if no other storage class specified */
1451 if (sym->level == 0 &&
1452 SPEC_SCLS(sym->etype) == S_FIXED &&
1453 !IS_FUNC(sym->type)) {
1454 /* find the first non-array link */
1458 if (IS_CONSTANT (t)) {
1459 SPEC_SCLS (sym->etype) = S_CODE;
1463 /* global variable in code space is a constant */
1464 if (sym->level == 0 &&
1465 SPEC_SCLS (sym->etype) == S_CODE &&
1466 port->mem.code_ro) {
1467 /* find the first non-array link */
1474 DCL_PTR_CONST (t) = 1;
1478 /* if bit variable then no storage class can be */
1479 /* specified since bit is already a storage */
1480 if (IS_BITVAR (sym->etype) &&
1481 (SPEC_SCLS (sym->etype) != S_FIXED &&
1482 SPEC_SCLS (sym->etype) != S_SBIT &&
1483 SPEC_SCLS (sym->etype) != S_BIT)
1486 werror (E_BITVAR_STORAGE, sym->name);
1487 SPEC_SCLS (sym->etype) = S_FIXED;
1490 /* extern variables cannot be initialized */
1491 if (IS_EXTERN (sym->etype) && sym->ival)
1493 werror (E_EXTERN_INIT, sym->name);
1497 /* if this is an automatic symbol */
1498 if (sym->level && (options.stackAuto || reentrant)) {
1499 if (SPEC_SCLS (sym->etype) != S_BIT) {
1500 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1501 SPEC_SCLS (sym->etype) == S_FIXED ||
1502 SPEC_SCLS (sym->etype) == S_REGISTER ||
1503 SPEC_SCLS (sym->etype) == S_STACK ||
1504 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1505 SPEC_SCLS (sym->etype) = S_AUTO;
1507 /* storage class may only be specified for statics */
1508 if (!IS_STATIC(sym->etype)) {
1509 werror (E_AUTO_ASSUMED, sym->name);
1515 /* automatic symbols cannot be given */
1516 /* an absolute address ignore it */
1518 SPEC_ABSA (sym->etype) &&
1519 (options.stackAuto || reentrant))
1521 werror (E_AUTO_ABSA, sym->name);
1522 SPEC_ABSA (sym->etype) = 0;
1525 /* arrays & pointers cannot be defined for bits */
1526 /* SBITS or SFRs or BIT */
1527 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1528 !IS_FUNCPTR (sym->type) &&
1529 (SPEC_NOUN (sym->etype) == V_BIT ||
1530 SPEC_NOUN (sym->etype) == V_SBIT ||
1531 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1532 SPEC_SCLS (sym->etype) == S_SFR))
1533 werror (E_BIT_ARRAY, sym->name);
1535 /* if this is a bit|sbit then set length & start */
1536 if (SPEC_NOUN (sym->etype) == V_BIT ||
1537 SPEC_NOUN (sym->etype) == V_SBIT)
1539 SPEC_BLEN (sym->etype) = 1;
1540 SPEC_BSTR (sym->etype) = 0;
1544 /* variables declared in CODE space must have */
1545 /* initializers if not an extern */
1546 if (SPEC_SCLS (sym->etype) == S_CODE &&
1547 sym->ival == NULL &&
1550 port->mem.code_ro &&
1551 !IS_EXTERN (sym->etype) &&
1552 !funcInChain (sym->type))
1553 werror (E_CODE_NO_INIT, sym->name);
1556 /* if parameter or local variable then change */
1557 /* the storage class to reflect where the var will go */
1558 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1559 && !IS_STATIC(sym->etype)
1562 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1564 SPEC_SCLS (sym->etype) = (options.useXstack ?
1565 S_XSTACK : S_STACK);
1569 /* hack-o-matic! I see no reason why the useXstack option should ever
1570 * control this allocation, but the code was originally that way, and
1571 * changing it for non-390 ports breaks the compiler badly.
1573 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1574 1 : options.useXstack;
1575 SPEC_SCLS (sym->etype) = (useXdata ?
1581 /*------------------------------------------------------------------*/
1582 /* changePointer - change pointer to functions */
1583 /*------------------------------------------------------------------*/
1585 changePointer (sym_link * p)
1588 /* go thru the chain of declarations */
1589 /* if we find a pointer to a function */
1590 /* change it to a ptr to code area */
1591 /* unless the function is banked. */
1592 for (; p; p = p->next)
1594 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1595 DCL_TYPE (p) = port->unqualified_pointer;
1596 if (IS_PTR (p) && IS_FUNC (p->next))
1597 if (!IFFUNC_BANKED(p->next))
1598 DCL_TYPE (p) = CPOINTER;
1602 /*------------------------------------------------------------------*/
1603 /* checkDecl - does semantic validation of a declaration */
1604 /*------------------------------------------------------------------*/
1606 checkDecl (symbol * sym, int isProto)
1609 checkSClass (sym, isProto); /* check the storage class */
1610 changePointer (sym->type); /* change pointers if required */
1612 /* if this is an array without any dimension
1613 then update the dimension from the initial value */
1614 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1615 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1620 /*------------------------------------------------------------------*/
1621 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1622 /*------------------------------------------------------------------*/
1624 copyLinkChain (sym_link * p)
1626 sym_link *head, *curr, *loop;
1629 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1632 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1633 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1641 /*------------------------------------------------------------------*/
1642 /* cleanUpBlock - cleansup the symbol table specified for all the */
1643 /* symbols in the given block */
1644 /*------------------------------------------------------------------*/
1646 cleanUpBlock (bucket ** table, int block)
1651 /* go thru the entire table */
1652 for (i = 0; i < 256; i++)
1654 for (chain = table[i]; chain; chain = chain->next)
1656 if (chain->block >= block)
1658 deleteSym (table, chain->sym, chain->name);
1664 /*------------------------------------------------------------------*/
1665 /* cleanUpLevel - cleansup the symbol table specified for all the */
1666 /* symbols in the given level */
1667 /*------------------------------------------------------------------*/
1669 cleanUpLevel (bucket ** table, int level)
1674 /* go thru the entire table */
1675 for (i = 0; i < 256; i++)
1677 for (chain = table[i]; chain; chain = chain->next)
1679 if (chain->level >= level)
1681 deleteSym (table, chain->sym, chain->name);
1687 /*------------------------------------------------------------------*/
1688 /* computeTypeOr - computes the resultant type from two types */
1689 /*------------------------------------------------------------------*/
1691 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1694 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1695 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1697 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1699 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1703 if (SPEC_USIGN (etype1))
1705 if ( IS_LITERAL (etype2)
1706 && floatFromVal (valFromType (etype2)) >= 0)
1707 SPEC_USIGN (reType) = 1;
1710 /* promote to int */
1711 SPEC_USIGN (reType) = 0;
1712 SPEC_NOUN (reType) = V_INT;
1715 else /* etype1 signed */
1717 if ( IS_LITERAL (etype2)
1718 && floatFromVal (valFromType (etype2)) <= 127)
1719 SPEC_USIGN (reType) = 0;
1722 /* promote to int */
1723 SPEC_USIGN (reType) = 0;
1724 SPEC_NOUN (reType) = V_INT;
1728 if (SPEC_USIGN (etype2))
1730 if ( IS_LITERAL (etype1)
1731 && floatFromVal (valFromType (etype1)) >= 0)
1732 SPEC_USIGN (reType) = 1;
1735 /* promote to int */
1736 SPEC_USIGN (reType) = 0;
1737 SPEC_NOUN (reType) = V_INT;
1740 else /* etype2 signed */
1742 if ( IS_LITERAL (etype1)
1743 && floatFromVal (valFromType (etype1)) <= 127)
1744 SPEC_USIGN (reType) = 0;
1747 /* promote to int */
1748 SPEC_USIGN (reType) = 0;
1749 SPEC_NOUN (reType) = V_INT;
1755 /*------------------------------------------------------------------*/
1756 /* computeType - computes the resultant type from two types */
1757 /*------------------------------------------------------------------*/
1759 computeType (sym_link * type1, sym_link * type2,
1760 RESULT_TYPE resultType, int op)
1764 sym_link *etype1 = getSpec (type1);
1767 etype2 = type2 ? getSpec (type2) : type1;
1769 /* if one of them is a float then result is a float */
1770 /* here we assume that the types passed are okay */
1771 /* and can be cast to one another */
1772 /* which ever is greater in size */
1773 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1774 rType = newFloatLink ();
1775 /* if both are fixed16x16 then result is float */
1776 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1777 rType = newFixed16x16Link();
1778 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1779 rType = newFloatLink ();
1780 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1781 rType = newFloatLink ();
1783 /* if both are bitvars choose the larger one */
1784 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1785 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1786 copyLinkChain (type1) : copyLinkChain (type1);
1788 /* if only one of them is a bit variable then the other one prevails */
1789 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1791 rType = copyLinkChain (type2);
1792 /* bitfield can have up to 16 bits */
1793 if (getSize (etype1) > 1)
1794 SPEC_NOUN (getSpec (rType)) = V_INT;
1796 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1798 rType = copyLinkChain (type1);
1799 /* bitfield can have up to 16 bits */
1800 if (getSize (etype2) > 1)
1801 SPEC_NOUN (getSpec (rType)) = V_INT;
1803 /* if one of them is a pointer or array then that
1805 else if (IS_PTR (type1) || IS_ARRAY (type1))
1806 rType = copyLinkChain (type1);
1807 else if (IS_PTR (type2) || IS_ARRAY (type2))
1808 rType = copyLinkChain (type2);
1809 else if (getSize (type1) > getSize (type2))
1810 rType = copyLinkChain (type1);
1812 rType = copyLinkChain (type2);
1814 reType = getSpec (rType);
1816 /* avoid conflicting types */
1817 reType->select.s.b_signed = 0;
1819 /* if result is a literal then make not so */
1820 if (IS_LITERAL (reType))
1821 SPEC_SCLS (reType) = S_REGISTER;
1825 case RESULT_TYPE_CHAR:
1826 if (IS_BITVAR (reType))
1828 SPEC_NOUN (reType) = V_CHAR;
1829 SPEC_SCLS (reType) = 0;
1830 SPEC_USIGN (reType) = 0;
1834 case RESULT_TYPE_INT:
1835 case RESULT_TYPE_NONE:
1836 case RESULT_TYPE_OTHER:
1837 if (IS_BIT (reType))
1839 SPEC_NOUN (reType) = V_CHAR;
1840 SPEC_SCLS (reType) = 0;
1841 SPEC_USIGN (reType) = 0;
1844 else if (IS_BITFIELD (reType))
1846 /* could be smarter, but it depends on the op */
1847 /* this is for the worst case: a multiplication of 4 * 4 bit */
1848 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1849 SPEC_SCLS (reType) = 0;
1850 SPEC_USIGN (reType) = 0;
1853 else if (IS_CHAR (reType))
1855 if (op == '|' || op == '^')
1856 return computeTypeOr (etype1, etype2, reType);
1858 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1860 SPEC_USIGN (reType) = 1;
1865 SPEC_NOUN (reType) = V_INT;
1866 SPEC_USIGN (reType) = 0;
1869 /* TODO: should be in SDCCast.c */
1871 && ( !SPEC_USIGN (etype1)
1872 || !SPEC_USIGN (etype2)))
1874 SPEC_NOUN (reType) = V_INT;
1875 SPEC_USIGN (reType) = 0;
1884 /* SDCC's sign promotion:
1885 - if one or both operands are unsigned, the resultant type will be unsigned
1886 (except char, see below)
1887 - if an operand is promoted to a larger type (char -> int, int -> long),
1888 the larger type will be signed
1890 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1891 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1892 the standard. The standard demands, that the result has to be the same
1893 "as if" the promotion would have been performed:
1895 - if the result of an operation with two char's is promoted to a
1896 larger type, the result will be signed.
1898 More sophisticated are these:
1899 - if the result of an operation with two char's is a char again,
1900 the result will only then be unsigned, if both operands are
1901 unsigned. In all other cases the result will be signed.
1903 This seems to be contradictionary to the first two rules, but it makes
1904 real sense (all types are char's):
1906 A signed char can be negative; this must be preserved in the result
1909 Only if both operands are unsigned it's safe to make the result
1910 unsigned; this helps to avoid overflow:
1913 - ToDo: document '|', '^' and '&'
1915 Homework: - why is (200 * 200 < 0) true?
1916 - why is { char l = 200, r = 200; (r * l > 0) } true?
1919 if (!IS_FLOAT (reType)
1920 && ( (SPEC_USIGN (etype1)
1921 /* if this operand is promoted to a larger type,
1922 then it will be promoted to a signed type */
1923 && !(getSize (etype1) < getSize (reType))
1924 /* char require special handling */
1925 && !IS_CHAR (etype1))
1926 || /* same for 2nd operand */
1927 (SPEC_USIGN (etype2)
1928 && !(getSize (etype2) < getSize (reType))
1929 && !IS_CHAR (etype2))
1930 || /* if both are 'unsigned char' and not promoted
1931 let the result be unsigned too */
1932 ( SPEC_USIGN (etype1)
1933 && SPEC_USIGN (etype2)
1936 && IS_CHAR (reType))))
1937 SPEC_USIGN (reType) = 1;
1939 SPEC_USIGN (reType) = 0;
1944 /*--------------------------------------------------------------------*/
1945 /* compareType - will do type check return 1 if match, -1 if castable */
1946 /*--------------------------------------------------------------------*/
1948 compareType (sym_link * dest, sym_link * src)
1959 /* if dest is a declarator then */
1964 /* banked function pointer */
1965 if (IS_GENPTR (dest) && IS_GENPTR (src))
1967 if (IS_FUNC (src->next) && IS_VOID(dest->next))
1969 if (IS_FUNC (dest->next) && IS_VOID(src->next))
1971 return compareType (dest->next, src->next);
1974 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1976 //checkFunction(src,dest);
1978 return compareType (dest->next, src->next);
1980 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1984 (IS_GENPTR (dest) ||
1985 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
1988 if (IS_PTR (dest) && IS_ARRAY (src)) {
1989 value *val=aggregateToPointer (valFromType(src));
1990 int res=compareType (dest, val->type);
1991 Safe_free(val->type);
1995 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1996 return compareType (dest->next, src);
1999 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2005 /* if one is a specifier and the other is not */
2006 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2007 (IS_SPEC (dest) && !IS_SPEC (src)))
2010 /* if one of them is a void then ok */
2011 if (SPEC_NOUN (dest) == V_VOID &&
2012 SPEC_NOUN (src) != V_VOID)
2015 if (SPEC_NOUN (dest) != V_VOID &&
2016 SPEC_NOUN (src) == V_VOID)
2019 /* if they are both bitfields then if the lengths
2020 and starts don't match */
2021 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2022 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2023 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2026 /* it is a specifier */
2027 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2029 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2030 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2032 bitsForType (dest) == bitsForType (src))
2033 instead of the next two lines, but the regression tests fail with
2034 them; I guess it's a problem with replaceCheaperOp */
2035 getSize (dest) == getSize (src) &&
2036 !(!IS_BIT (dest) && IS_BIT (src)))
2038 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2043 else if (IS_STRUCT (dest))
2045 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2050 if (SPEC_LONG (dest) != SPEC_LONG (src))
2053 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2059 /*--------------------------------------------------------------------*/
2060 /* compareTypeExact - will do type check return 1 if match exactly */
2061 /*--------------------------------------------------------------------*/
2063 compareTypeExact (sym_link * dest, sym_link * src, int level)
2065 STORAGE_CLASS srcScls, destScls;
2076 /* if dest is a declarator then */
2081 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2082 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2084 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2086 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2090 value *exargs, *acargs, *checkValue;
2092 /* verify function return type */
2093 if (!compareTypeExact (dest->next, src->next, -1))
2095 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2097 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2099 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2102 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2106 /* compare expected args with actual args */
2107 exargs = FUNC_ARGS(dest);
2108 acargs = FUNC_ARGS(src);
2110 /* for all the expected args do */
2111 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2113 //checkTypeSanity(acargs->etype, acargs->name);
2115 if (IS_AGGREGATE (acargs->type))
2117 checkValue = copyValue (acargs);
2118 aggregateToPointer (checkValue);
2121 checkValue = acargs;
2124 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2129 /* if one them ended we have a problem */
2130 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2131 (!exargs && acargs && !IS_VOID (acargs->type)))
2135 return compareTypeExact (dest->next, src->next, level);
2142 /* if one is a specifier and the other is not */
2143 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2144 (IS_SPEC (dest) && !IS_SPEC (src)))
2147 /* if one of them is a void then ok */
2148 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2151 /* if they are both bitfields then if the lengths
2152 and starts don't match */
2153 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2154 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2155 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2158 if (IS_INTEGRAL (dest))
2160 /* signedness must match */
2161 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2163 /* size must match */
2164 if (SPEC_LONG (dest) != SPEC_LONG (src))
2166 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2170 if (IS_STRUCT (dest))
2172 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2176 if (SPEC_CONST (dest) != SPEC_CONST (src))
2178 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2180 if (SPEC_STAT (dest) != SPEC_STAT (src))
2182 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2184 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2187 destScls = SPEC_SCLS (dest);
2188 srcScls = SPEC_SCLS (src);
2190 /* Compensate for const to const code change in checkSClass() */
2191 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2193 if (srcScls == S_CODE && destScls == S_FIXED)
2195 if (destScls == S_CODE && srcScls == S_FIXED)
2199 /* compensate for allocGlobal() */
2200 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2201 && port->mem.default_globl_map == xdata
2205 if (level>0 && !SPEC_STAT (dest))
2207 /* Compensate for hack-o-matic in checkSClass() */
2208 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2210 if (destScls == S_FIXED)
2211 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2212 if (srcScls == S_FIXED)
2213 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2215 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2217 if (destScls == S_FIXED)
2219 if (srcScls == S_FIXED)
2224 if (srcScls != destScls)
2227 printf ("level = %d\n", level);
2228 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2229 SPEC_SCLS (src), SPEC_SCLS (dest));
2230 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2238 /*------------------------------------------------------------------*/
2239 /* inCalleeSaveList - return 1 if found in callee save list */
2240 /*------------------------------------------------------------------*/
2242 calleeCmp(void *p1, void *p2)
2244 return (strcmp((char *)p1, (char *)(p2)) == 0);
2248 inCalleeSaveList(char *s)
2250 if (options.all_callee_saves)
2252 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2255 /*-----------------------------------------------------------------*/
2256 /* aggregateToPointer: change an agggregate type function */
2257 /* argument to a pointer to that type. */
2258 /*-----------------------------------------------------------------*/
2260 aggregateToPointer (value * val)
2262 if (IS_AGGREGATE (val->type))
2264 /* if this is a structure */
2265 /* then we need to add a new link */
2266 if (IS_STRUCT (val->type))
2268 /* first lets add DECLARATOR type */
2269 sym_link *p = val->type;
2271 werror (W_STRUCT_AS_ARG, val->name);
2272 val->type = newLink (DECLARATOR);
2273 val->type->next = p;
2276 /* change to a pointer depending on the */
2277 /* storage class specified */
2278 switch (SPEC_SCLS (val->etype))
2281 DCL_TYPE (val->type) = IPOINTER;
2284 DCL_TYPE (val->type) = PPOINTER;
2287 if (SPEC_OCLS(val->etype)) {
2288 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2290 // this happens for (external) function parameters
2291 DCL_TYPE (val->type) = port->unqualified_pointer;
2297 DCL_TYPE (val->type) = POINTER;
2300 DCL_TYPE (val->type) = CPOINTER;
2303 DCL_TYPE (val->type) = FPOINTER;
2306 DCL_TYPE (val->type) = EEPPOINTER;
2309 DCL_TYPE (val->type) = port->unqualified_pointer;
2312 /* is there is a symbol associated then */
2313 /* change the type of the symbol as well */
2316 val->sym->type = copyLinkChain (val->type);
2317 val->sym->etype = getSpec (val->sym->type);
2322 /*------------------------------------------------------------------*/
2323 /* checkFunction - does all kinds of check on a function */
2324 /*------------------------------------------------------------------*/
2326 checkFunction (symbol * sym, symbol *csym)
2328 value *exargs, *acargs;
2332 if (getenv("DEBUG_SANITY")) {
2333 fprintf (stderr, "checkFunction: %s ", sym->name);
2336 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2338 werror(E_SYNTAX_ERROR, sym->name);
2342 /* make sure the type is complete and sane */
2343 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2345 /* if not type then some kind of error */
2349 /* if the function has no type then make it return int */
2350 if (!sym->type->next)
2351 sym->type->next = sym->etype = newIntLink ();
2353 /* function cannot return aggregate */
2354 if (IS_AGGREGATE (sym->type->next))
2356 werror (E_FUNC_AGGR, sym->name);
2360 /* check if this function is defined as calleeSaves
2361 then mark it as such */
2362 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2364 /* if interrupt service routine */
2365 /* then it cannot have arguments */
2366 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2368 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2369 werror (E_INT_ARGS, sym->name);
2370 FUNC_ARGS(sym->type)=NULL;
2374 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2376 werror (E_SHADOWREGS_NO_ISR, sym->name);
2380 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2382 acargs=acargs->next, argCnt++) {
2384 // this can happen for reentrant functions
2385 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2386 // the show must go on: synthesize a name and symbol
2387 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2388 acargs->sym = newSymbol (acargs->name, 1);
2389 SPEC_OCLS (acargs->etype) = istack;
2390 acargs->sym->type = copyLinkChain (acargs->type);
2391 acargs->sym->etype = getSpec (acargs->sym->type);
2392 acargs->sym->_isparm = 1;
2393 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2394 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2396 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2401 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2402 return 1; /* not defined nothing more to check */
2404 /* check if body already present */
2405 if (csym && IFFUNC_HASBODY(csym->type))
2407 werror (E_FUNC_BODY, sym->name);
2411 /* check the return value type */
2412 if (compareType (csym->type, sym->type) <= 0)
2414 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2415 printFromToType(csym->type, sym->type);
2419 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2421 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2424 /* I don't think this is necessary for interrupts. An isr is a */
2425 /* root in the calling tree. */
2426 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2427 (!FUNC_ISISR (sym->type)))
2429 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2432 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2434 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2437 /* Really, reentrant should match regardless of argCnt, but */
2438 /* this breaks some existing code (the fp lib functions). If */
2439 /* the first argument is always passed the same way, this */
2440 /* lax checking is ok (but may not be true for in future ports) */
2441 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2444 //printf("argCnt = %d\n",argCnt);
2445 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2448 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2450 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2453 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2455 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2459 /* compare expected args with actual args */
2460 exargs = FUNC_ARGS(csym->type);
2461 acargs = FUNC_ARGS(sym->type);
2463 /* for all the expected args do */
2466 exargs = exargs->next, acargs = acargs->next, argCnt++)
2468 if (getenv("DEBUG_SANITY")) {
2469 fprintf (stderr, "checkFunction: %s ", exargs->name);
2471 /* make sure the type is complete and sane */
2472 checkTypeSanity(exargs->etype, exargs->name);
2474 /* If the actual argument is an array, any prototype
2475 * will have modified it to a pointer. Duplicate that
2478 if (IS_AGGREGATE (acargs->type))
2480 checkValue = copyValue (acargs);
2481 aggregateToPointer (checkValue);
2485 checkValue = acargs;
2488 if (compareType (exargs->type, checkValue->type) <= 0)
2490 werror (E_ARG_TYPE, argCnt);
2491 printFromToType(exargs->type, checkValue->type);
2496 /* if one them ended we have a problem */
2497 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2498 (!exargs && acargs && !IS_VOID (acargs->type)))
2499 werror (E_ARG_COUNT);
2501 /* replace with this defition */
2502 sym->cdef = csym->cdef;
2503 deleteSym (SymbolTab, csym, csym->name);
2504 deleteFromSeg(csym);
2505 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2506 if (IS_EXTERN (csym->etype) && !
2507 IS_EXTERN (sym->etype))
2509 addSet (&publics, sym);
2514 /*------------------------------------------------------------------*/
2515 /* cdbStructBlock - calls struct printing for a blcks */
2516 /*------------------------------------------------------------------*/
2517 void cdbStructBlock (int block)
2520 bucket **table = StructTab;
2523 /* go thru the entire table */
2524 for (i = 0; i < 256; i++)
2526 for (chain = table[i]; chain; chain = chain->next)
2528 if (chain->block >= block)
2531 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2537 /*-----------------------------------------------------------------*/
2538 /* processFuncArgs - does some processing with function args */
2539 /*-----------------------------------------------------------------*/
2541 processFuncArgs (symbol * func)
2545 sym_link *funcType=func->type;
2547 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2548 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2550 /* find the function declaration within the type */
2551 while (funcType && !IS_FUNC(funcType))
2552 funcType=funcType->next;
2554 /* if this function has variable argument list */
2555 /* then make the function a reentrant one */
2556 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2557 FUNC_ISREENT(funcType)=1;
2559 /* check if this function is defined as calleeSaves
2560 then mark it as such */
2561 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2563 /* loop thru all the arguments */
2564 val = FUNC_ARGS(funcType);
2566 /* if it is void then remove parameters */
2567 if (val && IS_VOID (val->type))
2569 FUNC_ARGS(funcType) = NULL;
2573 /* reset regparm for the port */
2574 (*port->reset_regparms) ();
2576 /* if any of the arguments is an aggregate */
2577 /* change it to pointer to the same type */
2581 char buffer[SDCC_NAME_MAX+1];
2583 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2584 checkTypeSanity (val->etype, buffer);
2586 /* mark it as a register parameter if
2587 the function does not have VA_ARG
2588 and as port dictates */
2589 if (!IFFUNC_HASVARARGS(funcType) &&
2590 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2592 SPEC_REGPARM (val->etype) = 1;
2593 SPEC_ARGREG(val->etype) = argreg;
2594 } else if (IFFUNC_ISREENT(funcType)) {
2595 FUNC_HASSTACKPARM(funcType) = 1;
2598 if (IS_AGGREGATE (val->type))
2600 aggregateToPointer (val);
2607 /* if this is an internal generated function call */
2609 /* ignore --stack-auto for this one, we don't know how it is compiled */
2610 /* simply trust on --int-long-reent or --float-reent */
2611 if (IFFUNC_ISREENT(funcType)) {
2615 /* if this function is reentrant or */
2616 /* automatics r 2b stacked then nothing */
2617 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2621 val = FUNC_ARGS(funcType);
2626 /* if a symbolname is not given */
2627 /* synthesize a variable name */
2630 SNPRINTF (val->name, sizeof(val->name),
2631 "_%s_PARM_%d", func->name, pNum++);
2632 val->sym = newSymbol (val->name, 1);
2633 if (SPEC_SCLS(val->etype) == S_BIT)
2634 SPEC_OCLS (val->etype) = bit;
2636 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2637 val->sym->type = copyLinkChain (val->type);
2638 val->sym->etype = getSpec (val->sym->type);
2639 val->sym->_isparm = 1;
2640 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2642 /* ?? static functions shouldn't imply static parameters - EEP */
2643 if (IS_SPEC(func->etype)) {
2644 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2645 SPEC_STAT (func->etype);
2648 addSymChain (&val->sym);
2651 else /* symbol name given create synth name */
2654 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2655 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2656 val->sym->_isparm = 1;
2657 if (SPEC_SCLS(val->etype) == S_BIT)
2658 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2660 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2661 port->mem.default_local_map;
2664 /* ?? static functions shouldn't imply static parameters - EEP */
2665 if (IS_SPEC(func->etype)) {
2666 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2667 SPEC_STAT (func->etype);
2671 if (SPEC_OCLS (val->sym->etype) == pdata)
2672 val->sym->iaccess = 1;
2673 if (!isinSet(operKeyReset, val->sym)) {
2674 addSet (&operKeyReset, val->sym);
2675 applyToSet (operKeyReset, resetParmKey);
2681 /*-----------------------------------------------------------------*/
2682 /* isSymbolEqual - compares two symbols return 1 if they match */
2683 /*-----------------------------------------------------------------*/
2685 isSymbolEqual (symbol * dest, symbol * src)
2687 /* if pointers match then equal */
2691 /* if one of them is null then don't match */
2695 /* if both of them have rname match on rname */
2696 if (dest->rname[0] && src->rname[0])
2697 return (!strcmp (dest->rname, src->rname));
2699 /* otherwise match on name */
2700 return (!strcmp (dest->name, src->name));
2703 void PT(sym_link *type)
2705 printTypeChain(type,0);
2707 /*-----------------------------------------------------------------*/
2708 /* printTypeChain - prints the type chain in human readable form */
2709 /*-----------------------------------------------------------------*/
2711 printTypeChain (sym_link * start, FILE * of)
2715 sym_link * type, * search;
2725 fprintf (of, "void");
2729 /* Print the chain as it is written in the source: */
2730 /* start with the last entry. */
2731 /* However, the storage class at the end of the */
2732 /* chain reall applies to the first in the chain! */
2734 for (type = start; type && type->next; type = type->next)
2737 scls=SPEC_SCLS(type);
2745 case S_DATA: fprintf (of, "data-"); break;
2746 case S_XDATA: fprintf (of, "xdata-"); break;
2747 case S_SFR: fprintf (of, "sfr-"); break;
2748 case S_SBIT: fprintf (of, "sbit-"); break;
2749 case S_CODE: fprintf (of, "code-"); break;
2750 case S_IDATA: fprintf (of, "idata-"); break;
2751 case S_PDATA: fprintf (of, "pdata-"); break;
2752 case S_LITERAL: fprintf (of, "literal-"); break;
2753 case S_STACK: fprintf (of, "stack-"); break;
2754 case S_XSTACK: fprintf (of, "xstack-"); break;
2755 case S_BIT: fprintf (of, "bit-"); break;
2756 case S_EEPROM: fprintf (of, "eeprom-"); break;
2763 if (!IS_FUNC(type)) {
2764 if (DCL_PTR_VOLATILE (type)) {
2765 fprintf (of, "volatile-");
2767 if (DCL_PTR_CONST (type)) {
2768 fprintf (of, "const-");
2771 switch (DCL_TYPE (type))
2774 fprintf (of, "function %s %s",
2775 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2776 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2778 for (args = FUNC_ARGS(type);
2781 printTypeChain(args->type, of);
2788 fprintf (of, "generic* ");
2791 fprintf (of, "code* ");
2794 fprintf (of, "xdata* ");
2797 fprintf (of, "eeprom* ");
2800 fprintf (of, "near* ");
2803 fprintf (of, "idata* ");
2806 fprintf (of, "pdata* ");
2809 fprintf (of, "unknown* ");
2812 if (DCL_ELEM(type)) {
2813 fprintf (of, "[%d] ", DCL_ELEM(type));
2815 fprintf (of, "[] ");
2822 if (SPEC_VOLATILE (type))
2823 fprintf (of, "volatile-");
2824 if (SPEC_CONST (type))
2825 fprintf (of, "const-");
2826 if (SPEC_USIGN (type))
2827 fprintf (of, "unsigned-");
2828 switch (SPEC_NOUN (type))
2832 fprintf (of, "long-");
2833 fprintf (of, "int");
2837 fprintf (of, "char");
2841 fprintf (of, "void");
2845 fprintf (of, "float");
2849 fprintf (of, "fixed16x16");
2853 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2857 fprintf (of, "sbit");
2861 fprintf (of, "bit");
2865 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2869 fprintf (of, "double");
2873 fprintf (of, "unknown type");
2877 /* search entry in list before "type" */
2878 for (search = start; search && search->next != type;)
2879 search = search->next;
2888 /*--------------------------------------------------------------------*/
2889 /* printTypeChainRaw - prints the type chain in human readable form */
2890 /* in the raw data structure ordering */
2891 /*--------------------------------------------------------------------*/
2893 printTypeChainRaw (sym_link * start, FILE * of)
2906 fprintf (of, "void");
2916 if (!IS_FUNC(type)) {
2917 if (DCL_PTR_VOLATILE (type)) {
2918 fprintf (of, "volatile-");
2920 if (DCL_PTR_CONST (type)) {
2921 fprintf (of, "const-");
2924 switch (DCL_TYPE (type))
2927 fprintf (of, "function %s %s",
2928 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2929 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2931 for (args = FUNC_ARGS(type);
2934 printTypeChain(args->type, of);
2941 fprintf (of, "generic* ");
2944 fprintf (of, "code* ");
2947 fprintf (of, "xdata* ");
2950 fprintf (of, "eeprom* ");
2953 fprintf (of, "near* ");
2956 fprintf (of, "idata* ");
2959 fprintf (of, "pdata* ");
2962 fprintf (of, "unknown* ");
2965 if (DCL_ELEM(type)) {
2966 fprintf (of, "[%d] ", DCL_ELEM(type));
2968 fprintf (of, "[] ");
2972 if (DCL_TSPEC(type))
2975 printTypeChainRaw(DCL_TSPEC(type), of);
2979 else if (IS_SPEC (type))
2981 switch (SPEC_SCLS (type))
2983 case S_DATA: fprintf (of, "data-"); break;
2984 case S_XDATA: fprintf (of, "xdata-"); break;
2985 case S_SFR: fprintf (of, "sfr-"); break;
2986 case S_SBIT: fprintf (of, "sbit-"); break;
2987 case S_CODE: fprintf (of, "code-"); break;
2988 case S_IDATA: fprintf (of, "idata-"); break;
2989 case S_PDATA: fprintf (of, "pdata-"); break;
2990 case S_LITERAL: fprintf (of, "literal-"); break;
2991 case S_STACK: fprintf (of, "stack-"); break;
2992 case S_XSTACK: fprintf (of, "xstack-"); break;
2993 case S_BIT: fprintf (of, "bit-"); break;
2994 case S_EEPROM: fprintf (of, "eeprom-"); break;
2997 if (SPEC_VOLATILE (type))
2998 fprintf (of, "volatile-");
2999 if (SPEC_CONST (type))
3000 fprintf (of, "const-");
3001 if (SPEC_USIGN (type))
3002 fprintf (of, "unsigned-");
3003 switch (SPEC_NOUN (type))
3007 fprintf (of, "long-");
3008 fprintf (of, "int");
3012 fprintf (of, "char");
3016 fprintf (of, "void");
3020 fprintf (of, "float");
3024 fprintf (of, "fixed16x16");
3028 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3032 fprintf (of, "sbit");
3036 fprintf (of, "bit");
3040 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3044 fprintf (of, "double");
3048 fprintf (of, "unknown type");
3053 fprintf (of, "NOT_SPEC_OR_DECL");
3063 /*-----------------------------------------------------------------*/
3064 /* powof2 - returns power of two for the number if number is pow 2 */
3065 /*-----------------------------------------------------------------*/
3067 powof2 (TYPE_UDWORD num)
3080 if (n1s > 1 || nshifts == 0)
3096 symbol *__fps16x16_add;
3097 symbol *__fps16x16_sub;
3098 symbol *__fps16x16_mul;
3099 symbol *__fps16x16_div;
3100 symbol *__fps16x16_eq;
3101 symbol *__fps16x16_neq;
3102 symbol *__fps16x16_lt;
3103 symbol *__fps16x16_lteq;
3104 symbol *__fps16x16_gt;
3105 symbol *__fps16x16_gteq;
3107 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3108 symbol *__muldiv[3][3][2];
3109 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3110 sym_link *__multypes[3][2];
3111 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3112 symbol *__conv[2][3][2];
3113 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3114 symbol *__fp16x16conv[2][4][2];
3115 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3116 symbol *__rlrr[2][3][2];
3118 sym_link *floatType;
3119 sym_link *fixed16x16Type;
3122 _mangleFunctionName(char *in)
3124 if (port->getMangledFunctionName)
3126 return port->getMangledFunctionName(in);
3134 /*-----------------------------------------------------------------*/
3135 /* typeFromStr - create a typechain from an encoded string */
3136 /* basic types - 'c' - char */
3141 /* 'q' - fixed16x16 */
3143 /* '*' - pointer - default (GPOINTER) */
3144 /* modifiers - 'u' - unsigned */
3145 /* pointer modifiers - 'g' - generic */
3149 /* 'F' - function */
3150 /* examples : "ig*" - generic int * */
3151 /* "cx*" - char xdata * */
3152 /* "ui" - unsigned int */
3153 /*-----------------------------------------------------------------*/
3154 sym_link *typeFromStr (char *s)
3156 sym_link *r = newLink(DECLARATOR);
3168 r->class = SPECIFIER;
3169 SPEC_NOUN(r) = V_CHAR;
3173 r->class = SPECIFIER;
3174 SPEC_NOUN(r) = V_INT;
3177 r->class = SPECIFIER;
3178 SPEC_NOUN(r) = V_INT;
3182 r->class = SPECIFIER;
3183 SPEC_NOUN(r) = V_FLOAT;
3186 r->class = SPECIFIER;
3187 SPEC_NOUN(r) = V_FIXED16X16;
3190 r->class = SPECIFIER;
3191 SPEC_NOUN(r) = V_VOID;
3194 DCL_TYPE(r) = port->unqualified_pointer;
3201 assert(*(s+1)=='*');
3202 nr = newLink(DECLARATOR);
3207 DCL_TYPE(r) = GPOINTER;
3210 DCL_TYPE(r) = FPOINTER;
3213 DCL_TYPE(r) = CPOINTER;
3216 DCL_TYPE(r) = POINTER;
3219 DCL_TYPE(r) = FUNCTION;
3220 nr = newLink(DECLARATOR);
3223 DCL_TYPE(r) = CPOINTER;
3229 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3230 "typeFromStr: unknown type");
3233 if (IS_SPEC(r) && usign) {
3242 /*-----------------------------------------------------------------*/
3243 /* initCSupport - create functions for C support routines */
3244 /*-----------------------------------------------------------------*/
3248 const char *smuldivmod[] =
3252 const char *sbwd[] =
3254 "char", "int", "long", "fixed16x16",
3256 const char *fp16x16sbwd[] =
3258 "char", "int", "long", "float",
3264 const char *srlrr[] =
3269 int bwd, su, muldivmod, tofrom, rlrr;
3271 if (getenv("SDCC_NO_C_SUPPORT")) {
3272 /* for debugging only */
3276 floatType = newFloatLink ();
3277 fixed16x16Type = newFixed16x16Link ();
3279 for (bwd = 0; bwd < 3; bwd++)
3296 __multypes[bwd][0] = l;
3297 __multypes[bwd][1] = copyLinkChain (l);
3298 SPEC_USIGN (__multypes[bwd][1]) = 1;
3301 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3302 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3303 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3304 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3305 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3306 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3307 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3308 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3309 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3310 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3312 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3313 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3314 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3315 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3316 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3317 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3318 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3319 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3320 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3321 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3324 for (tofrom = 0; tofrom < 2; tofrom++)
3326 for (bwd = 0; bwd < 3; bwd++)
3328 for (su = 0; su < 2; su++)
3332 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3333 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3337 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3338 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3344 for (tofrom = 0; tofrom < 2; tofrom++)
3346 for (bwd = 0; bwd < 4; bwd++)
3348 for (su = 0; su < 2; su++)
3352 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3354 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3356 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3360 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3362 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3364 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3371 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3373 for (bwd = 0; bwd < 3; bwd++)
3375 for (su = 0; su < 2; su++)
3377 SNPRINTF (buffer, sizeof(buffer),
3379 smuldivmod[muldivmod],
3382 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3383 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3388 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3389 Therefore they've been merged into mulint() and mullong().
3392 for (bwd = 0; bwd < 3; bwd++)
3394 for (su = 0; su < 2; su++)
3396 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3399 SNPRINTF (buffer, sizeof(buffer),
3401 smuldivmod[muldivmod],
3404 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3405 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3413 for (su = 0; su < 2; su++)
3415 /* muluchar and mulschar are still separate functions, because e.g. the z80
3416 port is sign/zero-extending to int before calling mulint() */
3417 SNPRINTF (buffer, sizeof(buffer),
3419 smuldivmod[muldivmod],
3422 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3423 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3427 /* word and doubleword */
3428 for (bwd = 1; bwd < 3; bwd++)
3431 SNPRINTF (buffer, sizeof(buffer),
3433 smuldivmod[muldivmod],
3435 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3436 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3437 /* signed = unsigned */
3438 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3441 for (rlrr = 0; rlrr < 2; rlrr++)
3443 for (bwd = 0; bwd < 3; bwd++)
3445 for (su = 0; su < 2; su++)
3447 SNPRINTF (buffer, sizeof(buffer),
3452 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3453 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3459 /*-----------------------------------------------------------------*/
3460 /* initBuiltIns - create prototypes for builtin functions */
3461 /*-----------------------------------------------------------------*/
3467 if (!port->builtintable) return ;
3469 for (i = 0 ; port->builtintable[i].name ; i++) {
3470 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3471 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3472 FUNC_ISBUILTIN(sym->type) = 1;
3473 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3477 sym_link *validateLink(sym_link *l,
3484 if (l && l->class==select)
3489 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3490 " expected %s, got %s\n",
3491 macro, args, file, line,
3492 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3494 return l; // never reached, makes compiler happy.
3497 /*--------------------------------------------------------------------*/
3498 /* newEnumType - create an integer type compatible with enumerations */
3499 /*--------------------------------------------------------------------*/
3501 newEnumType (symbol *enumlist)
3509 type = newLink (SPECIFIER);
3510 SPEC_NOUN (type) = V_INT;
3514 /* Determine the range of the enumerated values */
3516 min = max = (int) floatFromVal (valFromType (sym->type));
3517 for (sym = sym->next; sym; sym = sym->next)
3519 v = (int) floatFromVal (valFromType (sym->type));
3526 /* Determine the smallest integer type that is compatible with this range */
3527 type = newLink (SPECIFIER);
3528 if (min>=0 && max<=255)
3530 SPEC_NOUN (type) = V_CHAR;
3531 SPEC_USIGN (type) = 1;
3533 else if (min>=-128 && max<=127)
3535 SPEC_NOUN (type) = V_CHAR;
3537 else if (min>=0 && max<=65535)
3539 SPEC_NOUN (type) = V_INT;
3540 SPEC_USIGN (type) = 1;
3542 else if (min>=-32768 && max<=32767)
3544 SPEC_NOUN (type) = V_INT;
3548 SPEC_NOUN (type) = V_INT;
3549 SPEC_LONG (type) = 1;
3551 SPEC_USIGN (type) = 1;