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 (sl->select.s._short) 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 (etype->select.s._short || 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._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 (etype->select.s._short) {
563 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
564 etype->select.s._short = 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._signed)
579 SPEC_USIGN(etype) = 1;
582 if (etype->select.s._signed && SPEC_USIGN(etype)) {
583 // signed AND unsigned
584 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
586 if (etype->select.s._short && 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 dest->select.s._short|=src->select.s._short;
649 SPEC_USIGN (dest) |= SPEC_USIGN (src);
650 dest->select.s._signed|=src->select.s._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 /* getSize - returns size of a type chain in bits */
786 /*------------------------------------------------------------------*/
788 getSize (sym_link * p)
790 /* if nothing return 0 */
794 { /* if this is the specifier then */
795 switch (SPEC_NOUN (p))
796 { /* depending on the specifier type */
798 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
808 return SPEC_STRUCT (p)->size;
815 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
821 /* this is a declarator */
822 switch (DCL_TYPE (p))
826 return DCL_ELEM (p) * getSize (p->next);
828 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
829 // "can not tell the size of an array[]");
840 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
849 /*---------------------------------------------------------------------*/
850 /* getAllocSize - returns size of a type chain in bytes for allocation */
851 /*---------------------------------------------------------------------*/
853 getAllocSize (sym_link *p)
855 if (IS_STRUCT (p) && SPEC_STRUCT (p)->type == STRUCT)
857 /* if this is a struct specifier then */
858 /* calculate the size as it could end */
859 /* with an array of unspecified length */
860 symbol *sflds = SPEC_STRUCT (p)->fields;
862 while (sflds && sflds->next)
865 if (sflds && !IS_BITFIELD (sflds->type))
866 return sflds->offset + getAllocSize (sflds->type);
868 return SPEC_STRUCT (p)->size;
874 /*------------------------------------------------------------------*/
875 /* bitsForType - returns # of bits required to store this type */
876 /*------------------------------------------------------------------*/
878 bitsForType (sym_link * p)
880 /* if nothing return 0 */
885 { /* if this is the specifier then */
887 switch (SPEC_NOUN (p))
888 { /* depending on the specifier type */
890 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
892 return FLOATSIZE * 8;
900 return SPEC_STRUCT (p)->size * 8;
907 return SPEC_BLEN (p);
913 /* this is a specifier */
914 switch (DCL_TYPE (p))
917 return DCL_ELEM (p) * getSize (p->next) * 8;
921 return (PTRSIZE * 8);
926 return (FPTRSIZE * 8);
928 return (GPTRSIZE * 8);
935 /*------------------------------------------------------------------*/
936 /* copySymbolChain - copies a symbol chain */
937 /*------------------------------------------------------------------*/
939 copySymbolChain (symbol * src)
946 dest = copySymbol (src);
947 dest->next = copySymbolChain (src->next);
951 /*------------------------------------------------------------------*/
952 /* copySymbol - makes a copy of a symbol */
953 /*------------------------------------------------------------------*/
955 copySymbol (symbol * src)
962 dest = newSymbol (src->name, src->level);
963 memcpy (dest, src, sizeof (symbol));
964 dest->level = src->level;
965 dest->block = src->block;
966 dest->ival = copyIlist (src->ival);
967 dest->type = copyLinkChain (src->type);
968 dest->etype = getSpec (dest->type);
970 dest->key = src->key;
971 dest->allocreq = src->allocreq;
975 /*------------------------------------------------------------------*/
976 /* reverseSyms - reverses the links for a symbol chain */
977 /*------------------------------------------------------------------*/
979 reverseSyms (symbol * sym)
981 symbol *prev, *curr, *next;
996 sym->next = (void *) NULL;
1000 /*------------------------------------------------------------------*/
1001 /* reverseLink - reverses the links for a type chain */
1002 /*------------------------------------------------------------------*/
1004 reverseLink (sym_link * type)
1006 sym_link *prev, *curr, *next;
1021 type->next = (void *) NULL;
1025 /*------------------------------------------------------------------*/
1026 /* addSymChain - adds a symbol chain to the symboltable */
1027 /*------------------------------------------------------------------*/
1029 addSymChain (symbol ** symHead)
1031 symbol *sym = *symHead;
1032 symbol *csym = NULL;
1036 for (; sym != NULL; sym = sym->next)
1038 changePointer(sym->type);
1039 checkTypeSanity(sym->etype, sym->name);
1041 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1044 /* if already exists in the symbol table then check if
1045 one of them is an extern definition if yes then
1046 then check if the type match, if the types match then
1047 delete the current entry and add the new entry */
1048 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1049 csym->level == sym->level) {
1051 /* If the previous definition was for an array with incomplete */
1052 /* type, and the new definition has completed the type, update */
1053 /* the original type to match */
1054 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1055 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1057 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1058 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1062 /* If only one of the definitions used the "at" keyword, copy */
1063 /* the address to the other. */
1064 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1065 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1067 SPEC_ABSA (sym->etype) = 1;
1068 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1070 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1071 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1073 SPEC_ABSA (csym->etype) = 1;
1074 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1079 if (csym->ival && sym->ival)
1081 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1085 /* one definition extern ? */
1086 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1087 werror (E_EXTERN_MISMATCH, sym->name);
1089 werror (E_DUPLICATE, sym->name);
1090 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1092 fprintf (stderr, "from type '");
1093 printTypeChain (csym->type, stderr);
1094 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1095 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1096 fprintf (stderr, "'\nto type '");
1097 printTypeChain (sym->type, stderr);
1098 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1099 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1100 fprintf (stderr, "'\n");
1105 if (csym->ival && !sym->ival)
1106 sym->ival = csym->ival;
1108 /* delete current entry */
1109 deleteSym (SymbolTab, csym, csym->name);
1110 deleteFromSeg(csym);
1112 symPtrPtr = symHead;
1113 while (*symPtrPtr && *symPtrPtr != csym)
1114 symPtrPtr = &(*symPtrPtr)->next;
1115 if (*symPtrPtr == csym)
1116 *symPtrPtr = csym->next;
1121 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1126 /*------------------------------------------------------------------*/
1127 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1128 /*------------------------------------------------------------------*/
1130 funcInChain (sym_link * lnk)
1141 /*------------------------------------------------------------------*/
1142 /* structElemType - returns the type info of a struct member */
1143 /*------------------------------------------------------------------*/
1145 structElemType (sym_link * stype, value * id)
1147 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1148 sym_link *type, *etype;
1149 sym_link *petype = getSpec (stype);
1153 /* look for the id */
1156 if (strcmp (fields->rname, id->name) == 0)
1158 type = copyLinkChain (fields->type);
1159 etype = getSpec (type);
1160 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1161 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1163 SPEC_CONST (type) |= SPEC_CONST (stype);
1165 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1168 fields = fields->next;
1172 werror (E_NOT_MEMBER, id->name);
1174 // the show must go on
1175 return newIntLink();
1178 /*------------------------------------------------------------------*/
1179 /* getStructElement - returns element of a tructure definition */
1180 /*------------------------------------------------------------------*/
1182 getStructElement (structdef * sdef, symbol * sym)
1186 for (field = sdef->fields; field; field = field->next)
1187 if (strcmp (field->name, sym->name) == 0)
1190 werror (E_NOT_MEMBER, sym->name);
1192 return sdef->fields;
1195 /*------------------------------------------------------------------*/
1196 /* compStructSize - computes the size of a structure */
1197 /*------------------------------------------------------------------*/
1199 compStructSize (int su, structdef * sdef)
1201 int sum = 0, usum = 0;
1205 /* for the identifiers */
1206 loop = sdef->fields;
1209 /* create the internal name for this variable */
1210 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1215 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1217 /* if this is a bit field */
1220 /* change it to a unsigned bit */
1221 SPEC_NOUN (loop->etype) = V_BITFIELD;
1222 SPEC_USIGN (loop->etype) = 1;
1223 SPEC_BLEN (loop->etype) = loop->bitVar;
1225 if (loop->bitVar == BITVAR_PAD) {
1226 /* A zero length bitfield forces padding */
1227 SPEC_BSTR (loop->etype) = bitOffset;
1228 SPEC_BLEN (loop->etype) = 0;
1233 if (bitOffset == 8) {
1237 /* check if this fit into the remaining */
1238 /* bits of this byte else align it to the */
1239 /* next byte boundary */
1240 if (loop->bitVar <= (8 - bitOffset)) {
1241 /* fits into current byte */
1243 SPEC_BSTR (loop->etype) = bitOffset;
1244 bitOffset += loop->bitVar;
1246 else if (!bitOffset) {
1247 /* does not fit, but is already byte aligned */
1249 SPEC_BSTR (loop->etype) = bitOffset;
1250 bitOffset += loop->bitVar;
1253 /* does not fit; need to realign first */
1255 loop->offset = (su == UNION ? sum = 0 : sum);
1257 SPEC_BSTR (loop->etype) = bitOffset;
1258 bitOffset += loop->bitVar;
1260 while (bitOffset>8) {
1267 /* This is a non-bit field. Make sure we are */
1268 /* byte aligned first */
1271 loop->offset = (su == UNION ? sum = 0 : sum);
1275 checkDecl (loop, 1);
1276 sum += getSize (loop->type);
1281 /* if union then size = sizeof largest field */
1283 /* For UNION, round up after each field */
1284 sum += ((bitOffset+7)/8);
1285 usum = max (usum, sum);
1290 /* For STRUCT, round up after all fields processed */
1292 sum += ((bitOffset+7)/8);
1294 return (su == UNION ? usum : sum);
1297 /*-------------------------------------------------------------------*/
1298 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1299 /* an enclosing struct/union */
1300 /*-------------------------------------------------------------------*/
1302 promoteAnonStructs (int su, structdef * sdef)
1311 tofield = &sdef->fields;
1312 field = sdef->fields;
1315 nextfield = field->next;
1316 if (!*field->name && IS_STRUCT (field->type))
1318 /* Found an anonymous struct/union. Replace it */
1319 /* with the fields it contains and adjust all */
1322 base = field->offset;
1323 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1325 continue; /* just in case it's empty */
1327 *tofield = subfield;
1330 /* check for field name conflicts resulting from promotion */
1331 dupfield = sdef->fields;
1332 while (dupfield && dupfield != subfield)
1334 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1336 werrorfl (subfield->fileDef, subfield->lineDef,
1338 su==STRUCT ? "struct" : "union",
1340 werrorfl (dupfield->fileDef, dupfield->lineDef,
1343 dupfield = dupfield->next;
1346 subfield->offset += base;
1348 subfield = subfield->next;
1352 subfield->next = nextfield;
1353 tofield = &subfield->next;
1356 tofield = &field->next;
1362 /*------------------------------------------------------------------*/
1363 /* checkSClass - check the storage class specification */
1364 /*------------------------------------------------------------------*/
1366 checkSClass (symbol * sym, int isProto)
1370 if (getenv("DEBUG_SANITY")) {
1371 fprintf (stderr, "checkSClass: %s \n", sym->name);
1374 /* type is literal can happen for enums change
1376 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1377 SPEC_SCLS (sym->etype) = S_AUTO;
1379 /* if sfr or sbit then must also be volatile */
1380 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1381 SPEC_SCLS (sym->etype) == S_SFR)
1383 SPEC_VOLATILE (sym->etype) = 1;
1386 /* if absolute address given then it mark it as
1387 volatile -- except in the PIC port */
1389 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1390 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1391 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1394 if (IS_ABSOLUTE (sym->etype))
1395 SPEC_VOLATILE (sym->etype) = 1;
1397 if (TARGET_IS_MCS51 &&
1398 IS_ABSOLUTE (sym->etype) &&
1399 SPEC_SCLS (sym->etype) == S_SFR)
1404 if (SPEC_NOUN (sym->etype) == V_CHAR)
1406 else if (SPEC_LONG (sym->etype) == 0)
1411 addr = SPEC_ADDR (sym->etype);
1412 for (n=0; n<size; n+=8)
1413 if (((addr >> n) & 0xFF) < 0x80)
1414 werror (W_SFR_ABSRANGE, sym->name);
1417 /* If code memory is read only, then pointers to code memory */
1418 /* implicitly point to constants -- make this explicit */
1420 while (t && t->next) {
1421 if (IS_CODEPTR(t) && port->mem.code_ro) {
1422 if (IS_SPEC(t->next)) {
1423 SPEC_CONST (t->next) = 1;
1425 DCL_PTR_CONST (t->next) = 1;
1431 /* global variables declared const put into code */
1432 /* if no other storage class specified */
1433 if (sym->level == 0 &&
1434 SPEC_SCLS(sym->etype) == S_FIXED &&
1435 !IS_FUNC(sym->type)) {
1436 /* find the first non-array link */
1440 if (IS_CONSTANT (t)) {
1441 SPEC_SCLS (sym->etype) = S_CODE;
1445 /* global variable in code space is a constant */
1446 if (sym->level == 0 &&
1447 SPEC_SCLS (sym->etype) == S_CODE &&
1448 port->mem.code_ro) {
1449 /* find the first non-array link */
1456 DCL_PTR_CONST (t) = 1;
1460 /* if bit variable then no storage class can be */
1461 /* specified since bit is already a storage */
1462 if (IS_BITVAR (sym->etype) &&
1463 (SPEC_SCLS (sym->etype) != S_FIXED &&
1464 SPEC_SCLS (sym->etype) != S_SBIT &&
1465 SPEC_SCLS (sym->etype) != S_BIT)
1468 werror (E_BITVAR_STORAGE, sym->name);
1469 SPEC_SCLS (sym->etype) = S_FIXED;
1472 /* extern variables cannot be initialized */
1473 if (IS_EXTERN (sym->etype) && sym->ival)
1475 werror (E_EXTERN_INIT, sym->name);
1479 /* if this is an automatic symbol */
1480 if (sym->level && (options.stackAuto || reentrant)) {
1481 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1482 SPEC_SCLS (sym->etype) == S_FIXED ||
1483 SPEC_SCLS (sym->etype) == S_REGISTER ||
1484 SPEC_SCLS (sym->etype) == S_STACK ||
1485 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1486 SPEC_SCLS (sym->etype) = S_AUTO;
1488 /* storage class may only be specified for statics */
1489 if (!IS_STATIC(sym->etype)) {
1490 werror (E_AUTO_ASSUMED, sym->name);
1495 /* automatic symbols cannot be given */
1496 /* an absolute address ignore it */
1498 SPEC_ABSA (sym->etype) &&
1499 (options.stackAuto || reentrant))
1501 werror (E_AUTO_ABSA, sym->name);
1502 SPEC_ABSA (sym->etype) = 0;
1505 /* arrays & pointers cannot be defined for bits */
1506 /* SBITS or SFRs or BIT */
1507 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1508 (SPEC_NOUN (sym->etype) == V_BIT ||
1509 SPEC_NOUN (sym->etype) == V_SBIT ||
1510 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1511 SPEC_SCLS (sym->etype) == S_SFR))
1512 werror (E_BIT_ARRAY, sym->name);
1514 /* if this is a bit|sbit then set length & start */
1515 if (SPEC_NOUN (sym->etype) == V_BIT ||
1516 SPEC_NOUN (sym->etype) == V_SBIT)
1518 SPEC_BLEN (sym->etype) = 1;
1519 SPEC_BSTR (sym->etype) = 0;
1523 /* variables declared in CODE space must have */
1524 /* initializers if not an extern */
1525 if (SPEC_SCLS (sym->etype) == S_CODE &&
1526 sym->ival == NULL &&
1529 port->mem.code_ro &&
1530 !IS_EXTERN (sym->etype) &&
1531 !funcInChain (sym->type))
1532 werror (E_CODE_NO_INIT, sym->name);
1535 /* if parameter or local variable then change */
1536 /* the storage class to reflect where the var will go */
1537 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1538 && !IS_STATIC(sym->etype)
1541 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1543 SPEC_SCLS (sym->etype) = (options.useXstack ?
1544 S_XSTACK : S_STACK);
1548 /* hack-o-matic! I see no reason why the useXstack option should ever
1549 * control this allocation, but the code was originally that way, and
1550 * changing it for non-390 ports breaks the compiler badly.
1552 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1553 1 : options.useXstack;
1554 SPEC_SCLS (sym->etype) = (useXdata ?
1560 /*------------------------------------------------------------------*/
1561 /* changePointer - change pointer to functions */
1562 /*------------------------------------------------------------------*/
1564 changePointer (sym_link * p)
1567 /* go thru the chain of declarations */
1568 /* if we find a pointer to a function */
1569 /* change it to a ptr to code area */
1570 /* unless the function is banked. */
1571 for (; p; p = p->next)
1573 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1574 DCL_TYPE (p) = port->unqualified_pointer;
1575 if (IS_PTR (p) && IS_FUNC (p->next))
1576 if (!IFFUNC_BANKED(p->next))
1577 DCL_TYPE (p) = CPOINTER;
1581 /*------------------------------------------------------------------*/
1582 /* checkDecl - does semantic validation of a declaration */
1583 /*------------------------------------------------------------------*/
1585 checkDecl (symbol * sym, int isProto)
1588 checkSClass (sym, isProto); /* check the storage class */
1589 changePointer (sym->type); /* change pointers if required */
1591 /* if this is an array without any dimension
1592 then update the dimension from the initial value */
1593 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1594 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1599 /*------------------------------------------------------------------*/
1600 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1601 /*------------------------------------------------------------------*/
1603 copyLinkChain (sym_link * p)
1605 sym_link *head, *curr, *loop;
1608 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1611 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1612 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1620 /*------------------------------------------------------------------*/
1621 /* cleanUpBlock - cleansup the symbol table specified for all the */
1622 /* symbols in the given block */
1623 /*------------------------------------------------------------------*/
1625 cleanUpBlock (bucket ** table, int block)
1630 /* go thru the entire table */
1631 for (i = 0; i < 256; i++)
1633 for (chain = table[i]; chain; chain = chain->next)
1635 if (chain->block >= block)
1637 deleteSym (table, chain->sym, chain->name);
1643 /*------------------------------------------------------------------*/
1644 /* cleanUpLevel - cleansup the symbol table specified for all the */
1645 /* symbols in the given level */
1646 /*------------------------------------------------------------------*/
1648 cleanUpLevel (bucket ** table, int level)
1653 /* go thru the entire table */
1654 for (i = 0; i < 256; i++)
1656 for (chain = table[i]; chain; chain = chain->next)
1658 if (chain->level >= level)
1660 deleteSym (table, chain->sym, chain->name);
1666 /*------------------------------------------------------------------*/
1667 /* computeTypeOr - computes the resultant type from two types */
1668 /*------------------------------------------------------------------*/
1670 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1673 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1674 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1676 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1678 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1682 if (SPEC_USIGN (etype1))
1684 if ( IS_LITERAL (etype2)
1685 && floatFromVal (valFromType (etype2)) >= 0)
1686 SPEC_USIGN (reType) = 1;
1689 /* promote to int */
1690 SPEC_USIGN (reType) = 0;
1691 SPEC_NOUN (reType) = V_INT;
1694 else /* etype1 signed */
1696 if ( IS_LITERAL (etype2)
1697 && floatFromVal (valFromType (etype2)) <= 127)
1698 SPEC_USIGN (reType) = 0;
1701 /* promote to int */
1702 SPEC_USIGN (reType) = 0;
1703 SPEC_NOUN (reType) = V_INT;
1707 if (SPEC_USIGN (etype2))
1709 if ( IS_LITERAL (etype1)
1710 && floatFromVal (valFromType (etype1)) >= 0)
1711 SPEC_USIGN (reType) = 1;
1714 /* promote to int */
1715 SPEC_USIGN (reType) = 0;
1716 SPEC_NOUN (reType) = V_INT;
1719 else /* etype2 signed */
1721 if ( IS_LITERAL (etype1)
1722 && floatFromVal (valFromType (etype1)) <= 127)
1723 SPEC_USIGN (reType) = 0;
1726 /* promote to int */
1727 SPEC_USIGN (reType) = 0;
1728 SPEC_NOUN (reType) = V_INT;
1734 /*------------------------------------------------------------------*/
1735 /* computeType - computes the resultant type from two types */
1736 /*------------------------------------------------------------------*/
1738 computeType (sym_link * type1, sym_link * type2,
1739 RESULT_TYPE resultType, int op)
1743 sym_link *etype1 = getSpec (type1);
1746 etype2 = type2 ? getSpec (type2) : type1;
1748 /* if one of them is a float then result is a float */
1749 /* here we assume that the types passed are okay */
1750 /* and can be cast to one another */
1751 /* which ever is greater in size */
1752 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1753 rType = newFloatLink ();
1755 /* if both are fixed16x16 then result is float */
1756 if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1757 rType = newFixed16x16Link();
1759 if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1760 rType = newFloatLink ();
1761 if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1762 rType = newFloatLink ();
1764 /* if both are bitvars choose the larger one */
1765 if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1767 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1768 copyLinkChain (type1) : copyLinkChain (type1);
1770 /* if only one of them is a bit variable
1771 then the other one prevails */
1772 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1774 rType = copyLinkChain (type2);
1775 /* bitfield can have up to 16 bits */
1776 if (getSize (etype1) > 1)
1777 SPEC_NOUN (getSpec (rType)) = V_INT;
1779 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1781 rType = copyLinkChain (type1);
1782 /* bitfield can have up to 16 bits */
1783 if (getSize (etype2) > 1)
1784 SPEC_NOUN (getSpec (rType)) = V_INT;
1787 /* if one of them is a pointer or array then that
1789 if (IS_PTR (type1) || IS_ARRAY (type1))
1790 rType = copyLinkChain (type1);
1791 else if (IS_PTR (type2) || IS_ARRAY (type2))
1792 rType = copyLinkChain (type2);
1793 else if (getSize (type1) > getSize (type2))
1794 rType = copyLinkChain (type1);
1796 rType = copyLinkChain (type2);
1798 reType = getSpec (rType);
1800 /* avoid conflicting types */
1801 reType->select.s._signed = 0;
1803 /* if result is a literal then make not so */
1804 if (IS_LITERAL (reType))
1805 SPEC_SCLS (reType) = S_REGISTER;
1809 case RESULT_TYPE_CHAR:
1810 if (IS_BITVAR (reType))
1812 SPEC_NOUN (reType) = V_CHAR;
1813 SPEC_SCLS (reType) = 0;
1814 SPEC_USIGN (reType) = 0;
1818 case RESULT_TYPE_INT:
1819 case RESULT_TYPE_NONE:
1820 case RESULT_TYPE_OTHER:
1821 if (IS_BIT (reType))
1823 SPEC_NOUN (reType) = V_CHAR;
1824 SPEC_SCLS (reType) = 0;
1825 SPEC_USIGN (reType) = 0;
1828 else if (IS_BITFIELD (reType))
1830 /* could be smarter, but it depends on the op */
1831 /* this is for the worst case: a multiplication of 4 * 4 bit */
1832 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1833 SPEC_SCLS (reType) = 0;
1834 SPEC_USIGN (reType) = 0;
1837 else if (IS_CHAR (reType))
1839 if (op == '|' || op == '^')
1840 return computeTypeOr (etype1, etype2, reType);
1842 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1844 SPEC_USIGN (reType) = 1;
1849 SPEC_NOUN (reType) = V_INT;
1850 SPEC_USIGN (reType) = 0;
1853 /* TODO: should be in SDCCast.c */
1855 && ( !SPEC_USIGN (etype1)
1856 || !SPEC_USIGN (etype2)))
1858 SPEC_NOUN (reType) = V_INT;
1859 SPEC_USIGN (reType) = 0;
1868 /* SDCC's sign promotion:
1869 - if one or both operands are unsigned, the resultant type will be unsigned
1870 (except char, see below)
1871 - if an operand is promoted to a larger type (char -> int, int -> long),
1872 the larger type will be signed
1874 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1875 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1876 the standard. The standard demands, that the result has to be the same
1877 "as if" the promotion would have been performed:
1879 - if the result of an operation with two char's is promoted to a
1880 larger type, the result will be signed.
1882 More sophisticated are these:
1883 - if the result of an operation with two char's is a char again,
1884 the result will only then be unsigned, if both operands are
1885 unsigned. In all other cases the result will be signed.
1887 This seems to be contradictionary to the first two rules, but it makes
1888 real sense (all types are char's):
1890 A signed char can be negative; this must be preserved in the result
1893 Only if both operands are unsigned it's safe to make the result
1894 unsigned; this helps to avoid overflow:
1897 - ToDo: document '|', '^' and '&'
1899 Homework: - why is (200 * 200 < 0) true?
1900 - why is { char l = 200, r = 200; (r * l > 0) } true?
1903 if (!IS_FLOAT (reType)
1904 && ( (SPEC_USIGN (etype1)
1905 /* if this operand is promoted to a larger type,
1906 then it will be promoted to a signed type */
1907 && !(getSize (etype1) < getSize (reType))
1908 /* char require special handling */
1909 && !IS_CHAR (etype1))
1910 || /* same for 2nd operand */
1911 (SPEC_USIGN (etype2)
1912 && !(getSize (etype2) < getSize (reType))
1913 && !IS_CHAR (etype2))
1914 || /* if both are 'unsigned char' and not promoted
1915 let the result be unsigned too */
1916 ( SPEC_USIGN (etype1)
1917 && SPEC_USIGN (etype2)
1920 && IS_CHAR (reType))))
1921 SPEC_USIGN (reType) = 1;
1923 SPEC_USIGN (reType) = 0;
1928 /*--------------------------------------------------------------------*/
1929 /* compareType - will do type check return 1 if match, -1 if castable */
1930 /*--------------------------------------------------------------------*/
1932 compareType (sym_link * dest, sym_link * src)
1943 /* if dest is a declarator then */
1948 /* banked function pointer */
1949 if (IS_GENPTR (dest) && IS_GENPTR (src))
1951 if (IS_FUNC (src->next) && IS_VOID(dest->next))
1953 if (IS_FUNC (dest->next) && IS_VOID(src->next))
1955 return compareType (dest->next, src->next);
1958 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1960 //checkFunction(src,dest);
1962 return compareType (dest->next, src->next);
1964 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1968 (IS_GENPTR (dest) ||
1969 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
1972 if (IS_PTR (dest) && IS_ARRAY (src)) {
1973 value *val=aggregateToPointer (valFromType(src));
1974 int res=compareType (dest, val->type);
1975 Safe_free(val->type);
1979 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1980 return compareType (dest->next, src);
1983 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1989 /* if one is a specifier and the other is not */
1990 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1991 (IS_SPEC (dest) && !IS_SPEC (src)))
1994 /* if one of them is a void then ok */
1995 if (SPEC_NOUN (dest) == V_VOID &&
1996 SPEC_NOUN (src) != V_VOID)
1999 if (SPEC_NOUN (dest) != V_VOID &&
2000 SPEC_NOUN (src) == V_VOID)
2003 /* if they are both bitfields then if the lengths
2004 and starts don't match */
2005 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2006 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2007 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2010 /* it is a specifier */
2011 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2013 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2014 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2016 bitsForType (dest) == bitsForType (src))
2017 instead of the next two lines, but the regression tests fail with
2018 them; I guess it's a problem with replaceCheaperOp */
2019 getSize (dest) == getSize (src) &&
2020 !(!IS_BIT (dest) && IS_BIT (src)))
2022 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2027 else if (IS_STRUCT (dest))
2029 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2034 if (SPEC_LONG (dest) != SPEC_LONG (src))
2037 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2043 /*--------------------------------------------------------------------*/
2044 /* compareTypeExact - will do type check return 1 if match exactly */
2045 /*--------------------------------------------------------------------*/
2047 compareTypeExact (sym_link * dest, sym_link * src, int level)
2049 STORAGE_CLASS srcScls, destScls;
2060 /* if dest is a declarator then */
2065 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2066 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2068 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2070 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2074 value *exargs, *acargs, *checkValue;
2076 /* verify function return type */
2077 if (!compareTypeExact (dest->next, src->next, -1))
2079 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2081 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2083 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2086 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2090 /* compare expected args with actual args */
2091 exargs = FUNC_ARGS(dest);
2092 acargs = FUNC_ARGS(src);
2094 /* for all the expected args do */
2095 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2097 //checkTypeSanity(acargs->etype, acargs->name);
2099 if (IS_AGGREGATE (acargs->type))
2101 checkValue = copyValue (acargs);
2102 aggregateToPointer (checkValue);
2105 checkValue = acargs;
2108 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2113 /* if one them ended we have a problem */
2114 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2115 (!exargs && acargs && !IS_VOID (acargs->type)))
2119 return compareTypeExact (dest->next, src->next, level);
2126 /* if one is a specifier and the other is not */
2127 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2128 (IS_SPEC (dest) && !IS_SPEC (src)))
2131 /* if one of them is a void then ok */
2132 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2135 /* if they are both bitfields then if the lengths
2136 and starts don't match */
2137 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2138 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2139 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2142 if (IS_INTEGRAL (dest))
2144 /* signedness must match */
2145 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2147 /* size must match */
2148 if (SPEC_LONG (dest) != SPEC_LONG (src))
2150 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2154 if (IS_STRUCT (dest))
2156 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2160 if (SPEC_CONST (dest) != SPEC_CONST (src))
2162 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2164 if (SPEC_STAT (dest) != SPEC_STAT (src))
2166 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2168 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2171 destScls = SPEC_SCLS (dest);
2172 srcScls = SPEC_SCLS (src);
2174 /* Compensate for const to const code change in checkSClass() */
2175 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2177 if (srcScls == S_CODE && destScls == S_FIXED)
2179 if (destScls == S_CODE && srcScls == S_FIXED)
2183 /* compensate for allocGlobal() */
2184 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2185 && port->mem.default_globl_map == xdata
2189 if (level>0 && !SPEC_STAT (dest))
2191 /* Compensate for hack-o-matic in checkSClass() */
2192 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2194 if (destScls == S_FIXED)
2195 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2196 if (srcScls == S_FIXED)
2197 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2199 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2201 if (destScls == S_FIXED)
2203 if (srcScls == S_FIXED)
2208 if (srcScls != destScls)
2211 printf ("level = %d\n", level);
2212 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2213 SPEC_SCLS (src), SPEC_SCLS (dest));
2214 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2222 /*------------------------------------------------------------------*/
2223 /* inCalleeSaveList - return 1 if found in callee save list */
2224 /*------------------------------------------------------------------*/
2226 calleeCmp(void *p1, void *p2)
2228 return (strcmp((char *)p1, (char *)(p2)) == 0);
2232 inCalleeSaveList(char *s)
2234 if (options.all_callee_saves)
2236 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2239 /*-----------------------------------------------------------------*/
2240 /* aggregateToPointer: change an agggregate type function */
2241 /* argument to a pointer to that type. */
2242 /*-----------------------------------------------------------------*/
2244 aggregateToPointer (value * val)
2246 if (IS_AGGREGATE (val->type))
2248 /* if this is a structure */
2249 /* then we need to add a new link */
2250 if (IS_STRUCT (val->type))
2252 /* first lets add DECLARATOR type */
2253 sym_link *p = val->type;
2255 werror (W_STRUCT_AS_ARG, val->name);
2256 val->type = newLink (DECLARATOR);
2257 val->type->next = p;
2260 /* change to a pointer depending on the */
2261 /* storage class specified */
2262 switch (SPEC_SCLS (val->etype))
2265 DCL_TYPE (val->type) = IPOINTER;
2268 DCL_TYPE (val->type) = PPOINTER;
2271 if (SPEC_OCLS(val->etype)) {
2272 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2274 // this happens for (external) function parameters
2275 DCL_TYPE (val->type) = port->unqualified_pointer;
2281 DCL_TYPE (val->type) = POINTER;
2284 DCL_TYPE (val->type) = CPOINTER;
2287 DCL_TYPE (val->type) = FPOINTER;
2290 DCL_TYPE (val->type) = EEPPOINTER;
2293 DCL_TYPE (val->type) = port->unqualified_pointer;
2296 /* is there is a symbol associated then */
2297 /* change the type of the symbol as well */
2300 val->sym->type = copyLinkChain (val->type);
2301 val->sym->etype = getSpec (val->sym->type);
2306 /*------------------------------------------------------------------*/
2307 /* checkFunction - does all kinds of check on a function */
2308 /*------------------------------------------------------------------*/
2310 checkFunction (symbol * sym, symbol *csym)
2312 value *exargs, *acargs;
2316 if (getenv("DEBUG_SANITY")) {
2317 fprintf (stderr, "checkFunction: %s ", sym->name);
2320 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2322 werror(E_SYNTAX_ERROR, sym->name);
2326 /* make sure the type is complete and sane */
2327 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2329 /* if not type then some kind of error */
2333 /* if the function has no type then make it return int */
2334 if (!sym->type->next)
2335 sym->type->next = sym->etype = newIntLink ();
2337 /* function cannot return aggregate */
2338 if (IS_AGGREGATE (sym->type->next))
2340 werror (E_FUNC_AGGR, sym->name);
2344 /* function cannot return bit */
2345 if (IS_BITVAR (sym->type->next))
2347 werror (E_FUNC_BIT, sym->name);
2351 /* check if this function is defined as calleeSaves
2352 then mark it as such */
2353 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2355 /* if interrupt service routine */
2356 /* then it cannot have arguments */
2357 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2359 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2360 werror (E_INT_ARGS, sym->name);
2361 FUNC_ARGS(sym->type)=NULL;
2365 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2367 werror (E_SHADOWREGS_NO_ISR, sym->name);
2371 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2373 acargs=acargs->next, argCnt++) {
2375 // this can happen for reentrant functions
2376 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2377 // the show must go on: synthesize a name and symbol
2378 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2379 acargs->sym = newSymbol (acargs->name, 1);
2380 SPEC_OCLS (acargs->etype) = istack;
2381 acargs->sym->type = copyLinkChain (acargs->type);
2382 acargs->sym->etype = getSpec (acargs->sym->type);
2383 acargs->sym->_isparm = 1;
2384 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2385 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2387 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2392 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2393 return 1; /* not defined nothing more to check */
2395 /* check if body already present */
2396 if (csym && IFFUNC_HASBODY(csym->type))
2398 werror (E_FUNC_BODY, sym->name);
2402 /* check the return value type */
2403 if (compareType (csym->type, sym->type) <= 0)
2405 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2406 printFromToType(csym->type, sym->type);
2410 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2412 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2415 /* I don't think this is necessary for interrupts. An isr is a */
2416 /* root in the calling tree. */
2417 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2418 (!FUNC_ISISR (sym->type)))
2420 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2423 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2425 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2428 /* Really, reentrant should match regardless of argCnt, but */
2429 /* this breaks some existing code (the fp lib functions). If */
2430 /* the first argument is always passed the same way, this */
2431 /* lax checking is ok (but may not be true for in future ports) */
2432 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2435 //printf("argCnt = %d\n",argCnt);
2436 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2439 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2441 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2444 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2446 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2450 /* compare expected args with actual args */
2451 exargs = FUNC_ARGS(csym->type);
2452 acargs = FUNC_ARGS(sym->type);
2454 /* for all the expected args do */
2457 exargs = exargs->next, acargs = acargs->next, argCnt++)
2459 if (getenv("DEBUG_SANITY")) {
2460 fprintf (stderr, "checkFunction: %s ", exargs->name);
2462 /* make sure the type is complete and sane */
2463 checkTypeSanity(exargs->etype, exargs->name);
2465 /* If the actual argument is an array, any prototype
2466 * will have modified it to a pointer. Duplicate that
2469 if (IS_AGGREGATE (acargs->type))
2471 checkValue = copyValue (acargs);
2472 aggregateToPointer (checkValue);
2476 checkValue = acargs;
2479 if (compareType (exargs->type, checkValue->type) <= 0)
2481 werror (E_ARG_TYPE, argCnt);
2482 printFromToType(exargs->type, checkValue->type);
2487 /* if one them ended we have a problem */
2488 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2489 (!exargs && acargs && !IS_VOID (acargs->type)))
2490 werror (E_ARG_COUNT);
2492 /* replace with this defition */
2493 sym->cdef = csym->cdef;
2494 deleteSym (SymbolTab, csym, csym->name);
2495 deleteFromSeg(csym);
2496 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2497 if (IS_EXTERN (csym->etype) && !
2498 IS_EXTERN (sym->etype))
2500 addSet (&publics, sym);
2505 /*------------------------------------------------------------------*/
2506 /* cdbStructBlock - calls struct printing for a blcks */
2507 /*------------------------------------------------------------------*/
2508 void cdbStructBlock (int block)
2511 bucket **table = StructTab;
2514 /* go thru the entire table */
2515 for (i = 0; i < 256; i++)
2517 for (chain = table[i]; chain; chain = chain->next)
2519 if (chain->block >= block)
2522 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2528 /*-----------------------------------------------------------------*/
2529 /* processFuncArgs - does some processing with function args */
2530 /*-----------------------------------------------------------------*/
2532 processFuncArgs (symbol * func)
2536 sym_link *funcType=func->type;
2538 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2539 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2541 /* find the function declaration within the type */
2542 while (funcType && !IS_FUNC(funcType))
2543 funcType=funcType->next;
2545 /* if this function has variable argument list */
2546 /* then make the function a reentrant one */
2547 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2548 FUNC_ISREENT(funcType)=1;
2550 /* check if this function is defined as calleeSaves
2551 then mark it as such */
2552 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2554 /* loop thru all the arguments */
2555 val = FUNC_ARGS(funcType);
2557 /* if it is void then remove parameters */
2558 if (val && IS_VOID (val->type))
2560 FUNC_ARGS(funcType) = NULL;
2564 /* reset regparm for the port */
2565 (*port->reset_regparms) ();
2567 /* if any of the arguments is an aggregate */
2568 /* change it to pointer to the same type */
2572 char buffer[SDCC_NAME_MAX+1];
2574 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2575 checkTypeSanity (val->etype, buffer);
2577 /* mark it as a register parameter if
2578 the function does not have VA_ARG
2579 and as port dictates */
2580 if (!IFFUNC_HASVARARGS(funcType) &&
2581 (argreg = (*port->reg_parm) (val->type)))
2583 SPEC_REGPARM (val->etype) = 1;
2584 SPEC_ARGREG(val->etype) = argreg;
2585 } else if (IFFUNC_ISREENT(funcType)) {
2586 FUNC_HASSTACKPARM(funcType) = 1;
2589 if (IS_AGGREGATE (val->type))
2591 aggregateToPointer (val);
2598 /* if this is an internal generated function call */
2600 /* ignore --stack-auto for this one, we don't know how it is compiled */
2601 /* simply trust on --int-long-reent or --float-reent */
2602 if (IFFUNC_ISREENT(funcType)) {
2606 /* if this function is reentrant or */
2607 /* automatics r 2b stacked then nothing */
2608 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2612 val = FUNC_ARGS(funcType);
2617 /* if a symbolname is not given */
2618 /* synthesize a variable name */
2621 SNPRINTF (val->name, sizeof(val->name),
2622 "_%s_PARM_%d", func->name, pNum++);
2623 val->sym = newSymbol (val->name, 1);
2624 if (SPEC_SCLS(val->etype) == S_BIT)
2625 SPEC_OCLS (val->etype) = bit;
2627 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2628 val->sym->type = copyLinkChain (val->type);
2629 val->sym->etype = getSpec (val->sym->type);
2630 val->sym->_isparm = 1;
2631 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2633 /* ?? static functions shouldn't imply static parameters - EEP */
2634 if (IS_SPEC(func->etype)) {
2635 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2636 SPEC_STAT (func->etype);
2639 addSymChain (&val->sym);
2642 else /* symbol name given create synth name */
2645 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2646 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2647 val->sym->_isparm = 1;
2648 if (SPEC_SCLS(val->etype) == S_BIT)
2649 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2651 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2652 (options.model != MODEL_SMALL ? xdata : data);
2655 /* ?? static functions shouldn't imply static parameters - EEP */
2656 if (IS_SPEC(func->etype)) {
2657 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2658 SPEC_STAT (func->etype);
2662 if (!isinSet(operKeyReset, val->sym)) {
2663 addSet (&operKeyReset, val->sym);
2664 applyToSet (operKeyReset, resetParmKey);
2670 /*-----------------------------------------------------------------*/
2671 /* isSymbolEqual - compares two symbols return 1 if they match */
2672 /*-----------------------------------------------------------------*/
2674 isSymbolEqual (symbol * dest, symbol * src)
2676 /* if pointers match then equal */
2680 /* if one of them is null then don't match */
2684 /* if both of them have rname match on rname */
2685 if (dest->rname[0] && src->rname[0])
2686 return (!strcmp (dest->rname, src->rname));
2688 /* otherwise match on name */
2689 return (!strcmp (dest->name, src->name));
2692 void PT(sym_link *type)
2694 printTypeChain(type,0);
2696 /*-----------------------------------------------------------------*/
2697 /* printTypeChain - prints the type chain in human readable form */
2698 /*-----------------------------------------------------------------*/
2700 printTypeChain (sym_link * start, FILE * of)
2704 sym_link * type, * search;
2714 fprintf (of, "void");
2718 /* Print the chain as it is written in the source: */
2719 /* start with the last entry. */
2720 /* However, the storage class at the end of the */
2721 /* chain reall applies to the first in the chain! */
2723 for (type = start; type && type->next; type = type->next)
2726 scls=SPEC_SCLS(type);
2734 case S_DATA: fprintf (of, "data-"); break;
2735 case S_XDATA: fprintf (of, "xdata-"); break;
2736 case S_SFR: fprintf (of, "sfr-"); break;
2737 case S_SBIT: fprintf (of, "sbit-"); break;
2738 case S_CODE: fprintf (of, "code-"); break;
2739 case S_IDATA: fprintf (of, "idata-"); break;
2740 case S_PDATA: fprintf (of, "pdata-"); break;
2741 case S_LITERAL: fprintf (of, "literal-"); break;
2742 case S_STACK: fprintf (of, "stack-"); break;
2743 case S_XSTACK: fprintf (of, "xstack-"); break;
2744 case S_BIT: fprintf (of, "bit-"); break;
2745 case S_EEPROM: fprintf (of, "eeprom-"); break;
2752 if (!IS_FUNC(type)) {
2753 if (DCL_PTR_VOLATILE (type)) {
2754 fprintf (of, "volatile-");
2756 if (DCL_PTR_CONST (type)) {
2757 fprintf (of, "const-");
2760 switch (DCL_TYPE (type))
2763 fprintf (of, "function %s %s",
2764 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2765 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2767 for (args = FUNC_ARGS(type);
2770 printTypeChain(args->type, of);
2777 fprintf (of, "generic* ");
2780 fprintf (of, "code* ");
2783 fprintf (of, "xdata* ");
2786 fprintf (of, "eeprom* ");
2789 fprintf (of, "near* ");
2792 fprintf (of, "idata* ");
2795 fprintf (of, "pdata* ");
2798 fprintf (of, "unknown* ");
2801 if (DCL_ELEM(type)) {
2802 fprintf (of, "[%d] ", DCL_ELEM(type));
2804 fprintf (of, "[] ");
2811 if (SPEC_VOLATILE (type))
2812 fprintf (of, "volatile-");
2813 if (SPEC_CONST (type))
2814 fprintf (of, "const-");
2815 if (SPEC_USIGN (type))
2816 fprintf (of, "unsigned-");
2817 switch (SPEC_NOUN (type))
2821 fprintf (of, "long-");
2822 fprintf (of, "int");
2826 fprintf (of, "char");
2830 fprintf (of, "void");
2834 fprintf (of, "float");
2838 fprintf (of, "fixed16x16");
2842 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2846 fprintf (of, "sbit");
2850 fprintf (of, "bit");
2854 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2858 fprintf (of, "double");
2862 fprintf (of, "unknown type");
2866 /* search entry in list before "type" */
2867 for (search = start; search && search->next != type;)
2868 search = search->next;
2877 /*--------------------------------------------------------------------*/
2878 /* printTypeChainRaw - prints the type chain in human readable form */
2879 /* in the raw data structure ordering */
2880 /*--------------------------------------------------------------------*/
2882 printTypeChainRaw (sym_link * start, FILE * of)
2895 fprintf (of, "void");
2905 if (!IS_FUNC(type)) {
2906 if (DCL_PTR_VOLATILE (type)) {
2907 fprintf (of, "volatile-");
2909 if (DCL_PTR_CONST (type)) {
2910 fprintf (of, "const-");
2913 switch (DCL_TYPE (type))
2916 fprintf (of, "function %s %s",
2917 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2918 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2920 for (args = FUNC_ARGS(type);
2923 printTypeChain(args->type, of);
2930 fprintf (of, "generic* ");
2933 fprintf (of, "code* ");
2936 fprintf (of, "xdata* ");
2939 fprintf (of, "eeprom* ");
2942 fprintf (of, "near* ");
2945 fprintf (of, "idata* ");
2948 fprintf (of, "pdata* ");
2951 fprintf (of, "unknown* ");
2954 if (DCL_ELEM(type)) {
2955 fprintf (of, "[%d] ", DCL_ELEM(type));
2957 fprintf (of, "[] ");
2961 if (DCL_TSPEC(type))
2964 printTypeChainRaw(DCL_TSPEC(type), of);
2968 else if (IS_SPEC (type))
2970 switch (SPEC_SCLS (type))
2972 case S_DATA: fprintf (of, "data-"); break;
2973 case S_XDATA: fprintf (of, "xdata-"); break;
2974 case S_SFR: fprintf (of, "sfr-"); break;
2975 case S_SBIT: fprintf (of, "sbit-"); break;
2976 case S_CODE: fprintf (of, "code-"); break;
2977 case S_IDATA: fprintf (of, "idata-"); break;
2978 case S_PDATA: fprintf (of, "pdata-"); break;
2979 case S_LITERAL: fprintf (of, "literal-"); break;
2980 case S_STACK: fprintf (of, "stack-"); break;
2981 case S_XSTACK: fprintf (of, "xstack-"); break;
2982 case S_BIT: fprintf (of, "bit-"); break;
2983 case S_EEPROM: fprintf (of, "eeprom-"); break;
2986 if (SPEC_VOLATILE (type))
2987 fprintf (of, "volatile-");
2988 if (SPEC_CONST (type))
2989 fprintf (of, "const-");
2990 if (SPEC_USIGN (type))
2991 fprintf (of, "unsigned-");
2992 switch (SPEC_NOUN (type))
2996 fprintf (of, "long-");
2997 fprintf (of, "int");
3001 fprintf (of, "char");
3005 fprintf (of, "void");
3009 fprintf (of, "float");
3013 fprintf (of, "fixed16x16");
3017 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3021 fprintf (of, "sbit");
3025 fprintf (of, "bit");
3029 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3033 fprintf (of, "double");
3037 fprintf (of, "unknown type");
3042 fprintf (of, "NOT_SPEC_OR_DECL");
3052 /*-----------------------------------------------------------------*/
3053 /* powof2 - returns power of two for the number if number is pow 2 */
3054 /*-----------------------------------------------------------------*/
3056 powof2 (TYPE_UDWORD num)
3069 if (n1s > 1 || nshifts == 0)
3085 symbol *__fps16x16_add;
3086 symbol *__fps16x16_sub;
3087 symbol *__fps16x16_mul;
3088 symbol *__fps16x16_div;
3089 symbol *__fps16x16_eq;
3090 symbol *__fps16x16_neq;
3091 symbol *__fps16x16_lt;
3092 symbol *__fps16x16_lteq;
3093 symbol *__fps16x16_gt;
3094 symbol *__fps16x16_gteq;
3096 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3097 symbol *__muldiv[3][3][2];
3098 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3099 sym_link *__multypes[3][2];
3100 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3101 symbol *__conv[2][3][2];
3102 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3103 symbol *__fp16x16conv[2][4][2];
3104 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3105 symbol *__rlrr[2][3][2];
3107 sym_link *floatType;
3108 sym_link *fixed16x16Type;
3111 _mangleFunctionName(char *in)
3113 if (port->getMangledFunctionName)
3115 return port->getMangledFunctionName(in);
3123 /*-----------------------------------------------------------------*/
3124 /* typeFromStr - create a typechain from an encoded string */
3125 /* basic types - 'c' - char */
3130 /* 'q' - fixed16x16 */
3132 /* '*' - pointer - default (GPOINTER) */
3133 /* modifiers - 'u' - unsigned */
3134 /* pointer modifiers - 'g' - generic */
3138 /* 'F' - function */
3139 /* examples : "ig*" - generic int * */
3140 /* "cx*" - char xdata * */
3141 /* "ui" - unsigned int */
3142 /*-----------------------------------------------------------------*/
3143 sym_link *typeFromStr (char *s)
3145 sym_link *r = newLink(DECLARATOR);
3157 r->class = SPECIFIER;
3158 SPEC_NOUN(r) = V_CHAR;
3162 r->class = SPECIFIER;
3163 SPEC_NOUN(r) = V_INT;
3166 r->class = SPECIFIER;
3167 SPEC_NOUN(r) = V_INT;
3171 r->class = SPECIFIER;
3172 SPEC_NOUN(r) = V_FLOAT;
3175 r->class = SPECIFIER;
3176 SPEC_NOUN(r) = V_FIXED16X16;
3179 r->class = SPECIFIER;
3180 SPEC_NOUN(r) = V_VOID;
3183 DCL_TYPE(r) = port->unqualified_pointer;
3190 assert(*(s+1)=='*');
3191 nr = newLink(DECLARATOR);
3196 DCL_TYPE(r) = GPOINTER;
3199 DCL_TYPE(r) = FPOINTER;
3202 DCL_TYPE(r) = CPOINTER;
3205 DCL_TYPE(r) = POINTER;
3208 DCL_TYPE(r) = FUNCTION;
3209 nr = newLink(DECLARATOR);
3212 DCL_TYPE(r) = CPOINTER;
3218 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3219 "typeFromStr: unknown type");
3222 if (IS_SPEC(r) && usign) {
3231 /*-----------------------------------------------------------------*/
3232 /* initCSupport - create functions for C support routines */
3233 /*-----------------------------------------------------------------*/
3237 const char *smuldivmod[] =
3241 const char *sbwd[] =
3243 "char", "int", "long", "fixed16x16",
3245 const char *fp16x16sbwd[] =
3247 "char", "int", "long", "float",
3253 const char *srlrr[] =
3258 int bwd, su, muldivmod, tofrom, rlrr;
3260 if (getenv("SDCC_NO_C_SUPPORT")) {
3261 /* for debugging only */
3265 floatType = newFloatLink ();
3266 fixed16x16Type = newFixed16x16Link ();
3268 for (bwd = 0; bwd < 3; bwd++)
3285 __multypes[bwd][0] = l;
3286 __multypes[bwd][1] = copyLinkChain (l);
3287 SPEC_USIGN (__multypes[bwd][1]) = 1;
3290 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3291 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3292 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3293 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3294 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3295 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3296 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3297 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3298 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3299 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3301 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3302 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3303 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3304 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3305 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3306 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3307 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3308 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3309 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3310 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3313 for (tofrom = 0; tofrom < 2; tofrom++)
3315 for (bwd = 0; bwd < 3; bwd++)
3317 for (su = 0; su < 2; su++)
3321 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3322 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3326 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3327 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3333 for (tofrom = 0; tofrom < 2; tofrom++)
3335 for (bwd = 0; bwd < 4; bwd++)
3337 for (su = 0; su < 2; su++)
3341 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3343 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3345 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3349 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3351 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3353 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3360 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3362 for (bwd = 0; bwd < 3; bwd++)
3364 for (su = 0; su < 2; su++)
3366 SNPRINTF (buffer, sizeof(buffer),
3368 smuldivmod[muldivmod],
3371 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3372 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3377 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3378 Therefore they've been merged into mulint() and mullong().
3381 for (bwd = 0; bwd < 3; bwd++)
3383 for (su = 0; su < 2; su++)
3385 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3388 SNPRINTF (buffer, sizeof(buffer),
3390 smuldivmod[muldivmod],
3393 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3394 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3402 for (su = 0; su < 2; su++)
3404 /* muluchar and mulschar are still separate functions, because e.g. the z80
3405 port is sign/zero-extending to int before calling mulint() */
3406 SNPRINTF (buffer, sizeof(buffer),
3408 smuldivmod[muldivmod],
3411 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3412 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3416 /* word and doubleword */
3417 for (bwd = 1; bwd < 3; bwd++)
3420 SNPRINTF (buffer, sizeof(buffer),
3422 smuldivmod[muldivmod],
3424 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3425 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3426 /* signed = unsigned */
3427 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3430 for (rlrr = 0; rlrr < 2; rlrr++)
3432 for (bwd = 0; bwd < 3; bwd++)
3434 for (su = 0; su < 2; su++)
3436 SNPRINTF (buffer, sizeof(buffer),
3441 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3442 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3448 /*-----------------------------------------------------------------*/
3449 /* initBuiltIns - create prototypes for builtin functions */
3450 /*-----------------------------------------------------------------*/
3456 if (!port->builtintable) return ;
3458 for (i = 0 ; port->builtintable[i].name ; i++) {
3459 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3460 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3461 FUNC_ISBUILTIN(sym->type) = 1;
3462 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3466 sym_link *validateLink(sym_link *l,
3473 if (l && l->class==select)
3478 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3479 " expected %s, got %s\n",
3480 macro, args, file, line,
3481 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3483 return l; // never reached, makes compiler happy.
3486 /*--------------------------------------------------------------------*/
3487 /* newEnumType - create an integer type compatible with enumerations */
3488 /*--------------------------------------------------------------------*/
3490 newEnumType (symbol *enumlist)
3498 type = newLink (SPECIFIER);
3499 SPEC_NOUN (type) = V_INT;
3503 /* Determine the range of the enumerated values */
3505 min = max = (int) floatFromVal (valFromType (sym->type));
3506 for (sym = sym->next; sym; sym = sym->next)
3508 v = (int) floatFromVal (valFromType (sym->type));
3515 /* Determine the smallest integer type that is compatible with this range */
3516 type = newLink (SPECIFIER);
3517 if (min>=0 && max<=255)
3519 SPEC_NOUN (type) = V_CHAR;
3520 SPEC_USIGN (type) = 1;
3522 else if (min>=-128 && max<=127)
3524 SPEC_NOUN (type) = V_CHAR;
3526 else if (min>=0 && max<=65535)
3528 SPEC_NOUN (type) = V_INT;
3529 SPEC_USIGN (type) = 1;
3531 else if (min>=-32768 && max<=32767)
3533 SPEC_NOUN (type) = V_INT;
3537 SPEC_NOUN (type) = V_INT;
3538 SPEC_LONG (type) = 1;
3540 SPEC_USIGN (type) = 1;