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 (IS_STRUCT (loop))
1648 SPEC_STRUCT (loop) = copyStruct (SPEC_STRUCT (loop));
1649 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1657 /*------------------------------------------------------------------*/
1658 /* cleanUpBlock - cleansup the symbol table specified for all the */
1659 /* symbols in the given block */
1660 /*------------------------------------------------------------------*/
1662 cleanUpBlock (bucket ** table, int block)
1667 /* go thru the entire table */
1668 for (i = 0; i < 256; i++)
1670 for (chain = table[i]; chain; chain = chain->next)
1672 if (chain->block >= block)
1674 deleteSym (table, chain->sym, chain->name);
1680 /*------------------------------------------------------------------*/
1681 /* cleanUpLevel - cleansup the symbol table specified for all the */
1682 /* symbols in the given level */
1683 /*------------------------------------------------------------------*/
1685 cleanUpLevel (bucket ** table, int level)
1690 /* go thru the entire table */
1691 for (i = 0; i < 256; i++)
1693 for (chain = table[i]; chain; chain = chain->next)
1695 if (chain->level >= level)
1697 deleteSym (table, chain->sym, chain->name);
1703 /*------------------------------------------------------------------*/
1704 /* computeTypeOr - computes the resultant type from two types */
1705 /*------------------------------------------------------------------*/
1707 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1710 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1711 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1713 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1715 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1719 if (SPEC_USIGN (etype1))
1721 if ( IS_LITERAL (etype2)
1722 && floatFromVal (valFromType (etype2)) >= 0)
1723 SPEC_USIGN (reType) = 1;
1726 /* promote to int */
1727 SPEC_USIGN (reType) = 0;
1728 SPEC_NOUN (reType) = V_INT;
1731 else /* etype1 signed */
1733 if ( IS_LITERAL (etype2)
1734 && floatFromVal (valFromType (etype2)) <= 127)
1735 SPEC_USIGN (reType) = 0;
1738 /* promote to int */
1739 SPEC_USIGN (reType) = 0;
1740 SPEC_NOUN (reType) = V_INT;
1744 if (SPEC_USIGN (etype2))
1746 if ( IS_LITERAL (etype1)
1747 && floatFromVal (valFromType (etype1)) >= 0)
1748 SPEC_USIGN (reType) = 1;
1751 /* promote to int */
1752 SPEC_USIGN (reType) = 0;
1753 SPEC_NOUN (reType) = V_INT;
1756 else /* etype2 signed */
1758 if ( IS_LITERAL (etype1)
1759 && floatFromVal (valFromType (etype1)) <= 127)
1760 SPEC_USIGN (reType) = 0;
1763 /* promote to int */
1764 SPEC_USIGN (reType) = 0;
1765 SPEC_NOUN (reType) = V_INT;
1771 /*------------------------------------------------------------------*/
1772 /* computeType - computes the resultant type from two types */
1773 /*------------------------------------------------------------------*/
1775 computeType (sym_link * type1, sym_link * type2,
1776 RESULT_TYPE resultType, int op)
1780 sym_link *etype1 = getSpec (type1);
1783 etype2 = type2 ? getSpec (type2) : type1;
1785 /* if one of them is a float then result is a float */
1786 /* here we assume that the types passed are okay */
1787 /* and can be cast to one another */
1788 /* which ever is greater in size */
1789 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1790 rType = newFloatLink ();
1791 /* if both are fixed16x16 then result is float */
1792 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1793 rType = newFixed16x16Link();
1794 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1795 rType = newFloatLink ();
1796 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1797 rType = newFloatLink ();
1799 /* if both are bitvars choose the larger one */
1800 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1801 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1802 copyLinkChain (type1) : copyLinkChain (type1);
1804 /* if only one of them is a bit variable then the other one prevails */
1805 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1807 rType = copyLinkChain (type2);
1808 /* bitfield can have up to 16 bits */
1809 if (getSize (etype1) > 1)
1810 SPEC_NOUN (getSpec (rType)) = V_INT;
1812 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1814 rType = copyLinkChain (type1);
1815 /* bitfield can have up to 16 bits */
1816 if (getSize (etype2) > 1)
1817 SPEC_NOUN (getSpec (rType)) = V_INT;
1819 /* if one of them is a pointer or array then that
1821 else if (IS_PTR (type1) || IS_ARRAY (type1))
1822 rType = copyLinkChain (type1);
1823 else if (IS_PTR (type2) || IS_ARRAY (type2))
1824 rType = copyLinkChain (type2);
1825 else if (getSize (type1) > getSize (type2))
1826 rType = copyLinkChain (type1);
1828 rType = copyLinkChain (type2);
1830 reType = getSpec (rType);
1832 /* avoid conflicting types */
1833 reType->select.s.b_signed = 0;
1835 /* if result is a literal then make not so */
1836 if (IS_LITERAL (reType))
1837 SPEC_SCLS (reType) = S_REGISTER;
1841 case RESULT_TYPE_CHAR:
1842 if (IS_BITVAR (reType))
1844 SPEC_NOUN (reType) = V_CHAR;
1845 SPEC_SCLS (reType) = 0;
1846 SPEC_USIGN (reType) = 0;
1850 case RESULT_TYPE_INT:
1851 case RESULT_TYPE_NONE:
1852 case RESULT_TYPE_OTHER:
1853 if (IS_BIT (reType))
1855 SPEC_NOUN (reType) = V_CHAR;
1856 SPEC_SCLS (reType) = 0;
1857 SPEC_USIGN (reType) = 0;
1860 else if (IS_BITFIELD (reType))
1862 /* could be smarter, but it depends on the op */
1863 /* this is for the worst case: a multiplication of 4 * 4 bit */
1864 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1865 SPEC_SCLS (reType) = 0;
1866 SPEC_USIGN (reType) = 0;
1869 else if (IS_CHAR (reType))
1871 if (op == '|' || op == '^')
1872 return computeTypeOr (etype1, etype2, reType);
1874 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1876 SPEC_USIGN (reType) = 1;
1881 SPEC_NOUN (reType) = V_INT;
1882 SPEC_USIGN (reType) = 0;
1885 /* TODO: should be in SDCCast.c */
1887 && ( !SPEC_USIGN (etype1)
1888 || !SPEC_USIGN (etype2)))
1890 SPEC_NOUN (reType) = V_INT;
1891 SPEC_USIGN (reType) = 0;
1900 /* SDCC's sign promotion:
1901 - if one or both operands are unsigned, the resultant type will be unsigned
1902 (except char, see below)
1903 - if an operand is promoted to a larger type (char -> int, int -> long),
1904 the larger type will be signed
1906 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1907 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1908 the standard. The standard demands, that the result has to be the same
1909 "as if" the promotion would have been performed:
1911 - if the result of an operation with two char's is promoted to a
1912 larger type, the result will be signed.
1914 More sophisticated are these:
1915 - if the result of an operation with two char's is a char again,
1916 the result will only then be unsigned, if both operands are
1917 unsigned. In all other cases the result will be signed.
1919 This seems to be contradictionary to the first two rules, but it makes
1920 real sense (all types are char's):
1922 A signed char can be negative; this must be preserved in the result
1925 Only if both operands are unsigned it's safe to make the result
1926 unsigned; this helps to avoid overflow:
1929 - ToDo: document '|', '^' and '&'
1931 Homework: - why is (200 * 200 < 0) true?
1932 - why is { char l = 200, r = 200; (r * l > 0) } true?
1935 if (!IS_FLOAT (reType)
1936 && ( (SPEC_USIGN (etype1)
1937 /* if this operand is promoted to a larger type,
1938 then it will be promoted to a signed type */
1939 && !(getSize (etype1) < getSize (reType))
1940 /* char require special handling */
1941 && !IS_CHAR (etype1))
1942 || /* same for 2nd operand */
1943 (SPEC_USIGN (etype2)
1944 && !(getSize (etype2) < getSize (reType))
1945 && !IS_CHAR (etype2))
1946 || /* if both are 'unsigned char' and not promoted
1947 let the result be unsigned too */
1948 ( SPEC_USIGN (etype1)
1949 && SPEC_USIGN (etype2)
1952 && IS_CHAR (reType))))
1953 SPEC_USIGN (reType) = 1;
1955 SPEC_USIGN (reType) = 0;
1960 /*--------------------------------------------------------------------*/
1961 /* compareType - will do type check return 1 if match, -1 if castable */
1962 /*--------------------------------------------------------------------*/
1964 compareType (sym_link * dest, sym_link * src)
1975 /* if dest is a declarator then */
1980 /* banked function pointer */
1981 if (IS_GENPTR (dest) && IS_GENPTR (src))
1983 if (IS_FUNC (src->next) && IS_VOID(dest->next))
1985 if (IS_FUNC (dest->next) && IS_VOID(src->next))
1987 return compareType (dest->next, src->next);
1990 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1992 //checkFunction(src,dest);
1994 return compareType (dest->next, src->next);
1996 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
2000 (IS_GENPTR (dest) ||
2001 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2004 if (IS_PTR (dest) && IS_ARRAY (src)) {
2005 value *val=aggregateToPointer (valFromType(src));
2006 int res=compareType (dest, val->type);
2007 Safe_free(val->type);
2011 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2012 return compareType (dest->next, src);
2015 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2021 /* if one is a specifier and the other is not */
2022 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2023 (IS_SPEC (dest) && !IS_SPEC (src)))
2026 /* if one of them is a void then ok */
2027 if (SPEC_NOUN (dest) == V_VOID &&
2028 SPEC_NOUN (src) != V_VOID)
2031 if (SPEC_NOUN (dest) != V_VOID &&
2032 SPEC_NOUN (src) == V_VOID)
2035 /* if they are both bitfields then if the lengths
2036 and starts don't match */
2037 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2038 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2039 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2042 /* it is a specifier */
2043 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2045 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2046 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2048 bitsForType (dest) == bitsForType (src))
2049 instead of the next two lines, but the regression tests fail with
2050 them; I guess it's a problem with replaceCheaperOp */
2051 getSize (dest) == getSize (src) &&
2052 !(!IS_BIT (dest) && IS_BIT (src)))
2054 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2059 else if (IS_STRUCT (dest))
2061 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2066 if (SPEC_LONG (dest) != SPEC_LONG (src))
2069 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2075 /*--------------------------------------------------------------------*/
2076 /* compareTypeExact - will do type check return 1 if match exactly */
2077 /*--------------------------------------------------------------------*/
2079 compareTypeExact (sym_link * dest, sym_link * src, int level)
2081 STORAGE_CLASS srcScls, destScls;
2092 /* if dest is a declarator then */
2097 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2098 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2100 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2102 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2106 value *exargs, *acargs, *checkValue;
2108 /* verify function return type */
2109 if (!compareTypeExact (dest->next, src->next, -1))
2111 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2113 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2115 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2118 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2122 /* compare expected args with actual args */
2123 exargs = FUNC_ARGS(dest);
2124 acargs = FUNC_ARGS(src);
2126 /* for all the expected args do */
2127 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2129 //checkTypeSanity(acargs->etype, acargs->name);
2131 if (IS_AGGREGATE (acargs->type))
2133 checkValue = copyValue (acargs);
2134 aggregateToPointer (checkValue);
2137 checkValue = acargs;
2140 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2145 /* if one them ended we have a problem */
2146 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2147 (!exargs && acargs && !IS_VOID (acargs->type)))
2151 return compareTypeExact (dest->next, src->next, level);
2158 /* if one is a specifier and the other is not */
2159 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2160 (IS_SPEC (dest) && !IS_SPEC (src)))
2163 /* if one of them is a void then ok */
2164 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2167 /* if they are both bitfields then if the lengths
2168 and starts don't match */
2169 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2170 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2171 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2174 if (IS_INTEGRAL (dest))
2176 /* signedness must match */
2177 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2179 /* size must match */
2180 if (SPEC_LONG (dest) != SPEC_LONG (src))
2182 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2186 if (IS_STRUCT (dest))
2188 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2192 if (SPEC_CONST (dest) != SPEC_CONST (src))
2194 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2196 if (SPEC_STAT (dest) != SPEC_STAT (src))
2198 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2200 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2203 destScls = SPEC_SCLS (dest);
2204 srcScls = SPEC_SCLS (src);
2206 /* Compensate for const to const code change in checkSClass() */
2207 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2209 if (srcScls == S_CODE && destScls == S_FIXED)
2211 if (destScls == S_CODE && srcScls == S_FIXED)
2215 /* compensate for allocGlobal() */
2216 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2217 && port->mem.default_globl_map == xdata
2221 if (level>0 && !SPEC_STAT (dest))
2223 /* Compensate for hack-o-matic in checkSClass() */
2224 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2226 if (destScls == S_FIXED)
2227 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2228 if (srcScls == S_FIXED)
2229 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2231 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2233 if (destScls == S_FIXED)
2235 if (srcScls == S_FIXED)
2240 if (srcScls != destScls)
2243 printf ("level = %d\n", level);
2244 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2245 SPEC_SCLS (src), SPEC_SCLS (dest));
2246 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2254 /*------------------------------------------------------------------*/
2255 /* inCalleeSaveList - return 1 if found in callee save list */
2256 /*------------------------------------------------------------------*/
2258 calleeCmp(void *p1, void *p2)
2260 return (strcmp((char *)p1, (char *)(p2)) == 0);
2264 inCalleeSaveList(char *s)
2266 if (options.all_callee_saves)
2268 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2271 /*-----------------------------------------------------------------*/
2272 /* aggregateToPointer: change an agggregate type function */
2273 /* argument to a pointer to that type. */
2274 /*-----------------------------------------------------------------*/
2276 aggregateToPointer (value * val)
2278 if (IS_AGGREGATE (val->type))
2280 /* if this is a structure */
2281 /* then we need to add a new link */
2282 if (IS_STRUCT (val->type))
2284 /* first lets add DECLARATOR type */
2285 sym_link *p = val->type;
2287 werror (W_STRUCT_AS_ARG, val->name);
2288 val->type = newLink (DECLARATOR);
2289 val->type->next = p;
2292 /* change to a pointer depending on the */
2293 /* storage class specified */
2294 switch (SPEC_SCLS (val->etype))
2297 DCL_TYPE (val->type) = IPOINTER;
2300 DCL_TYPE (val->type) = PPOINTER;
2303 if (SPEC_OCLS(val->etype)) {
2304 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2306 // this happens for (external) function parameters
2307 DCL_TYPE (val->type) = port->unqualified_pointer;
2313 DCL_TYPE (val->type) = POINTER;
2316 DCL_TYPE (val->type) = CPOINTER;
2319 DCL_TYPE (val->type) = FPOINTER;
2322 DCL_TYPE (val->type) = EEPPOINTER;
2325 DCL_TYPE (val->type) = port->unqualified_pointer;
2328 /* is there is a symbol associated then */
2329 /* change the type of the symbol as well */
2332 val->sym->type = copyLinkChain (val->type);
2333 val->sym->etype = getSpec (val->sym->type);
2338 /*------------------------------------------------------------------*/
2339 /* checkFunction - does all kinds of check on a function */
2340 /*------------------------------------------------------------------*/
2342 checkFunction (symbol * sym, symbol *csym)
2344 value *exargs, *acargs;
2348 if (getenv("DEBUG_SANITY")) {
2349 fprintf (stderr, "checkFunction: %s ", sym->name);
2352 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2354 werror(E_SYNTAX_ERROR, sym->name);
2358 /* make sure the type is complete and sane */
2359 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2361 /* if not type then some kind of error */
2365 /* if the function has no type then make it return int */
2366 if (!sym->type->next)
2367 sym->type->next = sym->etype = newIntLink ();
2369 /* function cannot return aggregate */
2370 if (IS_AGGREGATE (sym->type->next))
2372 werror (E_FUNC_AGGR, sym->name);
2376 /* check if this function is defined as calleeSaves
2377 then mark it as such */
2378 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2380 /* if interrupt service routine */
2381 /* then it cannot have arguments */
2382 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2384 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2385 werror (E_INT_ARGS, sym->name);
2386 FUNC_ARGS(sym->type)=NULL;
2390 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2392 werror (E_SHADOWREGS_NO_ISR, sym->name);
2396 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2398 acargs=acargs->next, argCnt++) {
2400 // this can happen for reentrant functions
2401 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2402 // the show must go on: synthesize a name and symbol
2403 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2404 acargs->sym = newSymbol (acargs->name, 1);
2405 SPEC_OCLS (acargs->etype) = istack;
2406 acargs->sym->type = copyLinkChain (acargs->type);
2407 acargs->sym->etype = getSpec (acargs->sym->type);
2408 acargs->sym->_isparm = 1;
2409 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2410 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2412 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2417 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2418 return 1; /* not defined nothing more to check */
2420 /* check if body already present */
2421 if (csym && IFFUNC_HASBODY(csym->type))
2423 werror (E_FUNC_BODY, sym->name);
2427 /* check the return value type */
2428 if (compareType (csym->type, sym->type) <= 0)
2430 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2431 printFromToType(csym->type, sym->type);
2435 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2437 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2440 /* I don't think this is necessary for interrupts. An isr is a */
2441 /* root in the calling tree. */
2442 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2443 (!FUNC_ISISR (sym->type)))
2445 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2448 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2450 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2453 /* Really, reentrant should match regardless of argCnt, but */
2454 /* this breaks some existing code (the fp lib functions). If */
2455 /* the first argument is always passed the same way, this */
2456 /* lax checking is ok (but may not be true for in future ports) */
2457 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2460 //printf("argCnt = %d\n",argCnt);
2461 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2464 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2466 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2469 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2471 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2475 /* compare expected args with actual args */
2476 exargs = FUNC_ARGS(csym->type);
2477 acargs = FUNC_ARGS(sym->type);
2479 /* for all the expected args do */
2482 exargs = exargs->next, acargs = acargs->next, argCnt++)
2484 if (getenv("DEBUG_SANITY")) {
2485 fprintf (stderr, "checkFunction: %s ", exargs->name);
2487 /* make sure the type is complete and sane */
2488 checkTypeSanity(exargs->etype, exargs->name);
2490 /* If the actual argument is an array, any prototype
2491 * will have modified it to a pointer. Duplicate that
2494 if (IS_AGGREGATE (acargs->type))
2496 checkValue = copyValue (acargs);
2497 aggregateToPointer (checkValue);
2501 checkValue = acargs;
2504 if (compareType (exargs->type, checkValue->type) <= 0)
2506 werror (E_ARG_TYPE, argCnt);
2507 printFromToType(exargs->type, checkValue->type);
2512 /* if one them ended we have a problem */
2513 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2514 (!exargs && acargs && !IS_VOID (acargs->type)))
2515 werror (E_ARG_COUNT);
2517 /* replace with this defition */
2518 sym->cdef = csym->cdef;
2519 deleteSym (SymbolTab, csym, csym->name);
2520 deleteFromSeg(csym);
2521 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2522 if (IS_EXTERN (csym->etype) && !
2523 IS_EXTERN (sym->etype))
2525 addSet (&publics, sym);
2530 /*------------------------------------------------------------------*/
2531 /* cdbStructBlock - calls struct printing for a blcks */
2532 /*------------------------------------------------------------------*/
2533 void cdbStructBlock (int block)
2536 bucket **table = StructTab;
2539 /* go thru the entire table */
2540 for (i = 0; i < 256; i++)
2542 for (chain = table[i]; chain; chain = chain->next)
2544 if (chain->block >= block)
2547 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2553 /*-----------------------------------------------------------------*/
2554 /* processFuncArgs - does some processing with function args */
2555 /*-----------------------------------------------------------------*/
2557 processFuncArgs (symbol * func)
2561 sym_link *funcType=func->type;
2563 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2564 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2566 /* find the function declaration within the type */
2567 while (funcType && !IS_FUNC(funcType))
2568 funcType=funcType->next;
2570 /* if this function has variable argument list */
2571 /* then make the function a reentrant one */
2572 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2573 FUNC_ISREENT(funcType)=1;
2575 /* check if this function is defined as calleeSaves
2576 then mark it as such */
2577 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2579 /* loop thru all the arguments */
2580 val = FUNC_ARGS(funcType);
2582 /* if it is void then remove parameters */
2583 if (val && IS_VOID (val->type))
2585 FUNC_ARGS(funcType) = NULL;
2589 /* reset regparm for the port */
2590 (*port->reset_regparms) ();
2592 /* if any of the arguments is an aggregate */
2593 /* change it to pointer to the same type */
2597 char buffer[SDCC_NAME_MAX+1];
2599 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2600 checkTypeSanity (val->etype, buffer);
2602 /* mark it as a register parameter if
2603 the function does not have VA_ARG
2604 and as port dictates */
2605 if (!IFFUNC_HASVARARGS(funcType) &&
2606 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2608 SPEC_REGPARM (val->etype) = 1;
2609 SPEC_ARGREG(val->etype) = argreg;
2610 } else if (IFFUNC_ISREENT(funcType)) {
2611 FUNC_HASSTACKPARM(funcType) = 1;
2614 if (IS_AGGREGATE (val->type))
2616 aggregateToPointer (val);
2623 /* if this is an internal generated function call */
2625 /* ignore --stack-auto for this one, we don't know how it is compiled */
2626 /* simply trust on --int-long-reent or --float-reent */
2627 if (IFFUNC_ISREENT(funcType)) {
2631 /* if this function is reentrant or */
2632 /* automatics r 2b stacked then nothing */
2633 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2637 val = FUNC_ARGS(funcType);
2642 /* if a symbolname is not given */
2643 /* synthesize a variable name */
2646 SNPRINTF (val->name, sizeof(val->name),
2647 "_%s_PARM_%d", func->name, pNum++);
2648 val->sym = newSymbol (val->name, 1);
2649 if (SPEC_SCLS(val->etype) == S_BIT)
2650 SPEC_OCLS (val->etype) = bit;
2652 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2653 val->sym->type = copyLinkChain (val->type);
2654 val->sym->etype = getSpec (val->sym->type);
2655 val->sym->_isparm = 1;
2656 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2658 /* ?? static functions shouldn't imply static parameters - EEP */
2659 if (IS_SPEC(func->etype)) {
2660 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2661 SPEC_STAT (func->etype);
2664 addSymChain (&val->sym);
2667 else /* symbol name given create synth name */
2670 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2671 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2672 val->sym->_isparm = 1;
2673 if (SPEC_SCLS(val->etype) == S_BIT)
2674 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2676 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2677 port->mem.default_local_map;
2680 /* ?? static functions shouldn't imply static parameters - EEP */
2681 if (IS_SPEC(func->etype)) {
2682 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2683 SPEC_STAT (func->etype);
2687 if (SPEC_OCLS (val->sym->etype) == pdata)
2688 val->sym->iaccess = 1;
2689 if (!isinSet(operKeyReset, val->sym)) {
2690 addSet (&operKeyReset, val->sym);
2691 applyToSet (operKeyReset, resetParmKey);
2697 /*-----------------------------------------------------------------*/
2698 /* isSymbolEqual - compares two symbols return 1 if they match */
2699 /*-----------------------------------------------------------------*/
2701 isSymbolEqual (symbol * dest, symbol * src)
2703 /* if pointers match then equal */
2707 /* if one of them is null then don't match */
2711 /* if both of them have rname match on rname */
2712 if (dest->rname[0] && src->rname[0])
2713 return (!strcmp (dest->rname, src->rname));
2715 /* otherwise match on name */
2716 return (!strcmp (dest->name, src->name));
2719 void PT(sym_link *type)
2721 printTypeChain(type,0);
2723 /*-----------------------------------------------------------------*/
2724 /* printTypeChain - prints the type chain in human readable form */
2725 /*-----------------------------------------------------------------*/
2727 printTypeChain (sym_link * start, FILE * of)
2731 sym_link * type, * search;
2741 fprintf (of, "void");
2745 /* Print the chain as it is written in the source: */
2746 /* start with the last entry. */
2747 /* However, the storage class at the end of the */
2748 /* chain reall applies to the first in the chain! */
2750 for (type = start; type && type->next; type = type->next)
2753 scls=SPEC_SCLS(type);
2761 case S_DATA: fprintf (of, "data-"); break;
2762 case S_XDATA: fprintf (of, "xdata-"); break;
2763 case S_SFR: fprintf (of, "sfr-"); break;
2764 case S_SBIT: fprintf (of, "sbit-"); break;
2765 case S_CODE: fprintf (of, "code-"); break;
2766 case S_IDATA: fprintf (of, "idata-"); break;
2767 case S_PDATA: fprintf (of, "pdata-"); break;
2768 case S_LITERAL: fprintf (of, "literal-"); break;
2769 case S_STACK: fprintf (of, "stack-"); break;
2770 case S_XSTACK: fprintf (of, "xstack-"); break;
2771 case S_BIT: fprintf (of, "bit-"); break;
2772 case S_EEPROM: fprintf (of, "eeprom-"); break;
2779 if (!IS_FUNC(type)) {
2780 if (DCL_PTR_VOLATILE (type)) {
2781 fprintf (of, "volatile-");
2783 if (DCL_PTR_CONST (type)) {
2784 fprintf (of, "const-");
2787 switch (DCL_TYPE (type))
2790 fprintf (of, "function %s %s",
2791 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2792 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2794 for (args = FUNC_ARGS(type);
2797 printTypeChain(args->type, of);
2804 fprintf (of, "generic* ");
2807 fprintf (of, "code* ");
2810 fprintf (of, "xdata* ");
2813 fprintf (of, "eeprom* ");
2816 fprintf (of, "near* ");
2819 fprintf (of, "idata* ");
2822 fprintf (of, "pdata* ");
2825 fprintf (of, "unknown* ");
2828 if (DCL_ELEM(type)) {
2829 fprintf (of, "[%d] ", DCL_ELEM(type));
2831 fprintf (of, "[] ");
2838 if (SPEC_VOLATILE (type))
2839 fprintf (of, "volatile-");
2840 if (SPEC_CONST (type))
2841 fprintf (of, "const-");
2842 if (SPEC_USIGN (type))
2843 fprintf (of, "unsigned-");
2844 switch (SPEC_NOUN (type))
2848 fprintf (of, "long-");
2849 fprintf (of, "int");
2853 fprintf (of, "char");
2857 fprintf (of, "void");
2861 fprintf (of, "float");
2865 fprintf (of, "fixed16x16");
2869 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2873 fprintf (of, "sbit");
2877 fprintf (of, "bit");
2881 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2885 fprintf (of, "double");
2889 fprintf (of, "unknown type");
2893 /* search entry in list before "type" */
2894 for (search = start; search && search->next != type;)
2895 search = search->next;
2904 /*--------------------------------------------------------------------*/
2905 /* printTypeChainRaw - prints the type chain in human readable form */
2906 /* in the raw data structure ordering */
2907 /*--------------------------------------------------------------------*/
2909 printTypeChainRaw (sym_link * start, FILE * of)
2922 fprintf (of, "void");
2932 if (!IS_FUNC(type)) {
2933 if (DCL_PTR_VOLATILE (type)) {
2934 fprintf (of, "volatile-");
2936 if (DCL_PTR_CONST (type)) {
2937 fprintf (of, "const-");
2940 switch (DCL_TYPE (type))
2943 fprintf (of, "function %s %s",
2944 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2945 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2947 for (args = FUNC_ARGS(type);
2950 printTypeChain(args->type, of);
2957 fprintf (of, "generic* ");
2960 fprintf (of, "code* ");
2963 fprintf (of, "xdata* ");
2966 fprintf (of, "eeprom* ");
2969 fprintf (of, "near* ");
2972 fprintf (of, "idata* ");
2975 fprintf (of, "pdata* ");
2978 fprintf (of, "unknown* ");
2981 if (DCL_ELEM(type)) {
2982 fprintf (of, "[%d] ", DCL_ELEM(type));
2984 fprintf (of, "[] ");
2988 if (DCL_TSPEC(type))
2991 printTypeChainRaw(DCL_TSPEC(type), of);
2995 else if (IS_SPEC (type))
2997 switch (SPEC_SCLS (type))
2999 case S_DATA: fprintf (of, "data-"); break;
3000 case S_XDATA: fprintf (of, "xdata-"); break;
3001 case S_SFR: fprintf (of, "sfr-"); break;
3002 case S_SBIT: fprintf (of, "sbit-"); break;
3003 case S_CODE: fprintf (of, "code-"); break;
3004 case S_IDATA: fprintf (of, "idata-"); break;
3005 case S_PDATA: fprintf (of, "pdata-"); break;
3006 case S_LITERAL: fprintf (of, "literal-"); break;
3007 case S_STACK: fprintf (of, "stack-"); break;
3008 case S_XSTACK: fprintf (of, "xstack-"); break;
3009 case S_BIT: fprintf (of, "bit-"); break;
3010 case S_EEPROM: fprintf (of, "eeprom-"); break;
3013 if (SPEC_VOLATILE (type))
3014 fprintf (of, "volatile-");
3015 if (SPEC_CONST (type))
3016 fprintf (of, "const-");
3017 if (SPEC_USIGN (type))
3018 fprintf (of, "unsigned-");
3019 switch (SPEC_NOUN (type))
3023 fprintf (of, "long-");
3024 fprintf (of, "int");
3028 fprintf (of, "char");
3032 fprintf (of, "void");
3036 fprintf (of, "float");
3040 fprintf (of, "fixed16x16");
3044 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3048 fprintf (of, "sbit");
3052 fprintf (of, "bit");
3056 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3060 fprintf (of, "double");
3064 fprintf (of, "unknown type");
3069 fprintf (of, "NOT_SPEC_OR_DECL");
3079 /*-----------------------------------------------------------------*/
3080 /* powof2 - returns power of two for the number if number is pow 2 */
3081 /*-----------------------------------------------------------------*/
3083 powof2 (TYPE_UDWORD num)
3096 if (n1s > 1 || nshifts == 0)
3112 symbol *__fps16x16_add;
3113 symbol *__fps16x16_sub;
3114 symbol *__fps16x16_mul;
3115 symbol *__fps16x16_div;
3116 symbol *__fps16x16_eq;
3117 symbol *__fps16x16_neq;
3118 symbol *__fps16x16_lt;
3119 symbol *__fps16x16_lteq;
3120 symbol *__fps16x16_gt;
3121 symbol *__fps16x16_gteq;
3123 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3124 symbol *__muldiv[3][3][2];
3125 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3126 sym_link *__multypes[3][2];
3127 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3128 symbol *__conv[2][3][2];
3129 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3130 symbol *__fp16x16conv[2][4][2];
3131 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3132 symbol *__rlrr[2][3][2];
3134 sym_link *floatType;
3135 sym_link *fixed16x16Type;
3138 _mangleFunctionName(char *in)
3140 if (port->getMangledFunctionName)
3142 return port->getMangledFunctionName(in);
3150 /*-----------------------------------------------------------------*/
3151 /* typeFromStr - create a typechain from an encoded string */
3152 /* basic types - 'c' - char */
3157 /* 'q' - fixed16x16 */
3159 /* '*' - pointer - default (GPOINTER) */
3160 /* modifiers - 'u' - unsigned */
3161 /* pointer modifiers - 'g' - generic */
3165 /* 'F' - function */
3166 /* examples : "ig*" - generic int * */
3167 /* "cx*" - char xdata * */
3168 /* "ui" - unsigned int */
3169 /*-----------------------------------------------------------------*/
3170 sym_link *typeFromStr (char *s)
3172 sym_link *r = newLink(DECLARATOR);
3184 r->class = SPECIFIER;
3185 SPEC_NOUN(r) = V_CHAR;
3189 r->class = SPECIFIER;
3190 SPEC_NOUN(r) = V_INT;
3193 r->class = SPECIFIER;
3194 SPEC_NOUN(r) = V_INT;
3198 r->class = SPECIFIER;
3199 SPEC_NOUN(r) = V_FLOAT;
3202 r->class = SPECIFIER;
3203 SPEC_NOUN(r) = V_FIXED16X16;
3206 r->class = SPECIFIER;
3207 SPEC_NOUN(r) = V_VOID;
3210 DCL_TYPE(r) = port->unqualified_pointer;
3217 assert(*(s+1)=='*');
3218 nr = newLink(DECLARATOR);
3223 DCL_TYPE(r) = GPOINTER;
3226 DCL_TYPE(r) = FPOINTER;
3229 DCL_TYPE(r) = CPOINTER;
3232 DCL_TYPE(r) = POINTER;
3235 DCL_TYPE(r) = FUNCTION;
3236 nr = newLink(DECLARATOR);
3239 DCL_TYPE(r) = CPOINTER;
3245 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3246 "typeFromStr: unknown type");
3249 if (IS_SPEC(r) && usign) {
3258 /*-----------------------------------------------------------------*/
3259 /* initCSupport - create functions for C support routines */
3260 /*-----------------------------------------------------------------*/
3264 const char *smuldivmod[] =
3268 const char *sbwd[] =
3270 "char", "int", "long", "fixed16x16",
3272 const char *fp16x16sbwd[] =
3274 "char", "int", "long", "float",
3280 const char *srlrr[] =
3285 int bwd, su, muldivmod, tofrom, rlrr;
3287 if (getenv("SDCC_NO_C_SUPPORT")) {
3288 /* for debugging only */
3292 floatType = newFloatLink ();
3293 fixed16x16Type = newFixed16x16Link ();
3295 for (bwd = 0; bwd < 3; bwd++)
3312 __multypes[bwd][0] = l;
3313 __multypes[bwd][1] = copyLinkChain (l);
3314 SPEC_USIGN (__multypes[bwd][1]) = 1;
3317 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3318 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3319 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3320 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3321 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3322 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3323 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3324 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3325 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3326 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3328 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3329 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3330 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3331 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3332 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3333 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3334 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3335 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3336 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3337 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3340 for (tofrom = 0; tofrom < 2; tofrom++)
3342 for (bwd = 0; bwd < 3; bwd++)
3344 for (su = 0; su < 2; su++)
3348 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3349 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3353 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3354 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3360 for (tofrom = 0; tofrom < 2; tofrom++)
3362 for (bwd = 0; bwd < 4; bwd++)
3364 for (su = 0; su < 2; su++)
3368 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3370 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3372 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3376 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3378 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3380 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3387 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3389 for (bwd = 0; bwd < 3; bwd++)
3391 for (su = 0; su < 2; su++)
3393 SNPRINTF (buffer, sizeof(buffer),
3395 smuldivmod[muldivmod],
3398 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3399 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3404 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3405 Therefore they've been merged into mulint() and mullong().
3408 for (bwd = 0; bwd < 3; bwd++)
3410 for (su = 0; su < 2; su++)
3412 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3415 SNPRINTF (buffer, sizeof(buffer),
3417 smuldivmod[muldivmod],
3420 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3421 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3429 for (su = 0; su < 2; su++)
3431 /* muluchar and mulschar are still separate functions, because e.g. the z80
3432 port is sign/zero-extending to int before calling mulint() */
3433 SNPRINTF (buffer, sizeof(buffer),
3435 smuldivmod[muldivmod],
3438 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3439 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3443 /* word and doubleword */
3444 for (bwd = 1; bwd < 3; bwd++)
3447 SNPRINTF (buffer, sizeof(buffer),
3449 smuldivmod[muldivmod],
3451 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3452 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3453 /* signed = unsigned */
3454 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3457 for (rlrr = 0; rlrr < 2; rlrr++)
3459 for (bwd = 0; bwd < 3; bwd++)
3461 for (su = 0; su < 2; su++)
3463 SNPRINTF (buffer, sizeof(buffer),
3468 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3469 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3475 /*-----------------------------------------------------------------*/
3476 /* initBuiltIns - create prototypes for builtin functions */
3477 /*-----------------------------------------------------------------*/
3483 if (!port->builtintable) return ;
3485 for (i = 0 ; port->builtintable[i].name ; i++) {
3486 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3487 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3488 FUNC_ISBUILTIN(sym->type) = 1;
3489 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3493 sym_link *validateLink(sym_link *l,
3500 if (l && l->class==select)
3505 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3506 " expected %s, got %s\n",
3507 macro, args, file, line,
3508 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3510 return l; // never reached, makes compiler happy.
3513 /*--------------------------------------------------------------------*/
3514 /* newEnumType - create an integer type compatible with enumerations */
3515 /*--------------------------------------------------------------------*/
3517 newEnumType (symbol *enumlist)
3525 type = newLink (SPECIFIER);
3526 SPEC_NOUN (type) = V_INT;
3530 /* Determine the range of the enumerated values */
3532 min = max = (int) floatFromVal (valFromType (sym->type));
3533 for (sym = sym->next; sym; sym = sym->next)
3535 v = (int) floatFromVal (valFromType (sym->type));
3542 /* Determine the smallest integer type that is compatible with this range */
3543 type = newLink (SPECIFIER);
3544 if (min>=0 && max<=255)
3546 SPEC_NOUN (type) = V_CHAR;
3547 SPEC_USIGN (type) = 1;
3549 else if (min>=-128 && max<=127)
3551 SPEC_NOUN (type) = V_CHAR;
3553 else if (min>=0 && max<=65535)
3555 SPEC_NOUN (type) = V_INT;
3556 SPEC_USIGN (type) = 1;
3558 else if (min>=-32768 && max<=32767)
3560 SPEC_NOUN (type) = V_INT;
3564 SPEC_NOUN (type) = V_INT;
3565 SPEC_LONG (type) = 1;
3567 SPEC_USIGN (type) = 1;