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_CHAR: return "char";
51 case V_VOID: return "void";
52 case V_STRUCT: return "struct";
53 case V_LABEL: return "label";
54 case V_BITFIELD: return "bitfield";
55 case V_BIT: return "bit";
56 case V_SBIT: return "sbit";
57 case V_DOUBLE: return "double";
62 bucket *SymbolTab[256]; /* the symbol table */
63 bucket *StructTab[256]; /* the structure table */
64 bucket *TypedefTab[256]; /* the typedef table */
65 bucket *LabelTab[256]; /* the Label table */
66 bucket *enumTab[256]; /* enumerated table */
68 /*------------------------------------------------------------------*/
69 /* initSymt () - initialises symbol table related stuff */
70 /*------------------------------------------------------------------*/
76 for (i = 0; i < 256; i++)
77 SymbolTab[i] = StructTab[i] = (void *) NULL;
81 /*-----------------------------------------------------------------*/
82 /* newBucket - allocates & returns a new bucket */
83 /*-----------------------------------------------------------------*/
89 bp = Safe_alloc ( sizeof (bucket));
94 /*-----------------------------------------------------------------*/
95 /* hashKey - computes the hashkey given a symbol name */
96 /*-----------------------------------------------------------------*/
98 hashKey (const char *s)
100 unsigned long key = 0;
107 /*-----------------------------------------------------------------*/
108 /* addSym - adds a symbol to the hash Table */
109 /*-----------------------------------------------------------------*/
111 addSym (bucket ** stab,
118 int i; /* index into the hash Table */
119 bucket *bp; /* temp bucket * */
122 symbol *csym = (symbol *)sym;
124 if (getenv("DEBUG_SANITY")) {
125 fprintf (stderr, "addSym: %s ", sname);
127 /* make sure the type is complete and sane */
128 checkTypeSanity(csym->etype, csym->name);
131 /* prevent overflow of the (r)name buffers */
132 if (strlen(sname)>SDCC_SYMNAME_MAX) {
133 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
134 sname[SDCC_SYMNAME_MAX]='\0';
137 /* the symbols are always added at the head of the list */
139 /* get a free entry */
140 bp = Safe_alloc ( sizeof (bucket));
142 bp->sym = sym; /* update the symbol pointer */
143 bp->level = level; /* update the nest level */
145 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
147 /* if this is the first entry */
150 bp->prev = bp->next = (void *) NULL; /* point to nothing */
153 /* not first entry then add @ head of list */
163 /*-----------------------------------------------------------------*/
164 /* deleteSym - deletes a symbol from the hash Table entry */
165 /*-----------------------------------------------------------------*/
167 deleteSym (bucket ** stab, void *sym, char *sname)
175 /* find the symbol */
178 if (bp->sym == sym) /* found it then break out */
179 break; /* of the loop */
183 if (!bp) /* did not find it */
185 /* if this is the first one in the chain */
189 if (stab[i]) /* if chain ! empty */
190 stab[i]->prev = (void *) NULL;
192 /* middle || end of chain */
195 if (bp->next) /* if not end of chain */
196 bp->next->prev = bp->prev;
198 bp->prev->next = bp->next;
203 /*-----------------------------------------------------------------*/
204 /* findSym - finds a symbol in a table */
205 /*-----------------------------------------------------------------*/
207 findSym (bucket ** stab, void *sym, const char *sname)
211 bp = stab[hashKey (sname)];
214 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
219 return (bp ? bp->sym : (void *) NULL);
222 /*-----------------------------------------------------------------*/
223 /* findSymWithLevel - finds a symbol with a name & level */
224 /*-----------------------------------------------------------------*/
226 findSymWithLevel (bucket ** stab, symbol * sym)
230 bp = stab[hashKey (sym->name)];
233 ** do the search from the head of the list since the
234 ** elements are added at the head it is ensured that
235 ** we will find the deeper definitions before we find
236 ** the global ones. we need to check for symbols with
237 ** level <= to the level given, if levels match then block
238 ** numbers need to match as well
242 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
244 /* if this is parameter then nothing else need to be checked */
245 if (((symbol *) (bp->sym))->_isparm)
247 /* if levels match then block numbers should also match */
248 if (bp->level && bp->level == sym->level && bp->block == sym->block)
250 /* if levels don't match then we are okay */
251 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
253 /* if this is a global variable then we are ok too */
261 return (void *) NULL;
264 /*-----------------------------------------------------------------*/
265 /* findSymWithBlock - finds a symbol with name in with a block */
266 /*-----------------------------------------------------------------*/
268 findSymWithBlock (bucket ** stab, symbol * sym, int block)
272 bp = stab[hashKey (sym->name)];
275 if (strcmp (bp->name, sym->name) == 0 &&
281 return (bp ? bp->sym : (void *) NULL);
284 /*------------------------------------------------------------------*/
285 /* newSymbol () - returns a new pointer to a symbol */
286 /*------------------------------------------------------------------*/
288 newSymbol (char *name, int scope)
292 sym = Safe_alloc ( sizeof (symbol));
294 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
295 sym->level = scope; /* set the level */
296 sym->block = currBlockno;
297 sym->lineDef = mylineno; /* set the line number */
298 sym->fileDef = currFname;
302 /*------------------------------------------------------------------*/
303 /* newLink - creates a new link (declarator,specifier) */
304 /*------------------------------------------------------------------*/
306 newLink (SYM_LINK_CLASS select)
310 p = Safe_alloc ( sizeof (sym_link));
316 /*------------------------------------------------------------------*/
317 /* newStruct - creats a new structdef from the free list */
318 /*------------------------------------------------------------------*/
320 newStruct (char *tag)
324 s = Safe_alloc ( sizeof (structdef));
326 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
330 /*------------------------------------------------------------------*/
331 /* sclsFromPtr - Return the storage class a pointer points into. */
332 /* S_FIXED is returned for generic pointers or other */
333 /* unexpected cases */
334 /*------------------------------------------------------------------*/
336 sclsFromPtr(sym_link *ptr)
338 switch (DCL_TYPE (ptr))
361 /*------------------------------------------------------------------*/
362 /* pointerTypes - do the computation for the pointer types */
363 /*------------------------------------------------------------------*/
365 pointerTypes (sym_link * ptr, sym_link * type)
370 /* find the first pointer type */
371 while (ptr && !IS_PTR (ptr))
374 /* could not find it */
375 if (!ptr || IS_SPEC (ptr))
378 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
379 pointerTypes (ptr->next, type);
383 /* change the pointer type depending on the
384 storage class of the type */
387 switch (SPEC_SCLS (type))
390 DCL_TYPE (ptr) = FPOINTER;
393 DCL_TYPE (ptr) = IPOINTER;
396 DCL_TYPE (ptr) = PPOINTER;
399 DCL_TYPE (ptr) = POINTER;
402 DCL_TYPE (ptr) = CPOINTER;
405 DCL_TYPE (ptr) = EEPPOINTER;
408 DCL_TYPE (ptr) = port->unqualified_pointer;
411 /* the storage class of type ends here */
412 SPEC_SCLS (type) = 0;
415 /* now change all the remaining unknown pointers
416 to generic pointers */
419 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
420 DCL_TYPE (ptr) = port->unqualified_pointer;
424 /* same for the type although it is highly unlikely that
425 type will have a pointer */
428 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
429 DCL_TYPE (type) = port->unqualified_pointer;
434 /*------------------------------------------------------------------*/
435 /* addDecl - adds a declarator @ the end of a chain */
436 /*------------------------------------------------------------------*/
438 addDecl (symbol * sym, int type, sym_link * p)
444 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
445 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
447 /* if we are passed a link then set head & tail */
456 head = tail = newLink (DECLARATOR);
457 DCL_TYPE (head) = type;
460 /* if this is the first entry */
468 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
470 sym->etype = mergeSpec (sym->etype, head, sym->name);
474 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
477 while (t->next != sym->etype)
480 tail->next = sym->etype;
484 sym->etype->next = head;
490 /* if the type is an unknown pointer and has
491 a tspec then take the storage class const & volatile
492 attribute from the tspec & make it those of this
496 //DCL_TYPE (p) == UPOINTER &&
499 if (!IS_SPEC (sym->etype))
501 sym->etype = sym->etype->next = newLink (SPECIFIER);
503 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
504 DCL_TSPEC (p) = NULL;
507 // if there is a function in this type chain
508 if (p && funcInChain(sym->type)) {
509 processFuncArgs (sym);
515 /*------------------------------------------------------------------
516 checkTypeSanity: prevent the user from doing e.g.:
518 ------------------------------------------------------------------*/
519 void checkTypeSanity(sym_link *etype, char *name) {
523 if (getenv("DEBUG_SANITY")) {
524 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
529 if (!IS_SPEC(etype)) {
530 if (getenv("DEBUG_SANITY")) {
531 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
536 noun=nounName(etype);
538 if (getenv("DEBUG_SANITY")) {
539 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
542 if ((SPEC_NOUN(etype)==V_CHAR ||
543 SPEC_NOUN(etype)==V_FLOAT ||
544 SPEC_NOUN(etype)==V_DOUBLE ||
545 SPEC_NOUN(etype)==V_VOID) &&
546 (etype->select.s._short || SPEC_LONG(etype))) {
547 // long or short for char float double or void
548 werror (E_LONG_OR_SHORT_INVALID, noun, name);
550 if ((SPEC_NOUN(etype)==V_FLOAT ||
551 SPEC_NOUN(etype)==V_DOUBLE ||
552 SPEC_NOUN(etype)==V_VOID) &&
553 (etype->select.s._signed || SPEC_USIGN(etype))) {
554 // signed or unsigned for float double or void
555 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
558 // special case for "short"
559 if (etype->select.s._short) {
560 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
561 etype->select.s._short = 0;
565 "const a;" or "data b;" or "signed s" or "long l"
567 if (!SPEC_NOUN(etype)) {
568 SPEC_NOUN(etype)=V_INT;
571 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
572 /* a "plain" int bitfield is unsigned */
573 if (SPEC_NOUN(etype)==V_BIT ||
574 SPEC_NOUN(etype)==V_SBIT) {
575 if (!etype->select.s._signed)
576 SPEC_USIGN(etype) = 1;
579 if (etype->select.s._signed && SPEC_USIGN(etype)) {
580 // signed AND unsigned
581 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
583 if (etype->select.s._short && SPEC_LONG(etype)) {
585 werror (E_LONG_AND_SHORT_INVALID, noun, name);
590 /*------------------------------------------------------------------*/
591 /* mergeSpec - merges two specifiers and returns the new one */
592 /*------------------------------------------------------------------*/
594 mergeSpec (sym_link * dest, sym_link * src, char *name)
596 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
598 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
601 werror (E_SYNTAX_ERROR, yytext);
602 // the show must go on
607 if (SPEC_NOUN(src)) {
608 if (!SPEC_NOUN(dest)) {
609 SPEC_NOUN(dest)=SPEC_NOUN(src);
611 /* we shouldn't redeclare the type */
612 if (getenv("DEBUG_SANITY")) {
613 fprintf (stderr, "mergeSpec: ");
615 werror(E_TWO_OR_MORE_DATA_TYPES, name);
619 if (SPEC_SCLS(src)) {
620 /* if destination has no storage class */
621 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
622 SPEC_SCLS (dest) = SPEC_SCLS (src);
624 if (getenv("DEBUG_SANITY")) {
625 fprintf (stderr, "mergeSpec: ");
627 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
631 /* copy all the specifications */
633 // we really should do:
635 if (SPEC_what(src)) {
636 if (SPEC_what(dest)) {
637 werror(W_DUPLICATE_SPEC, "what");
639 SPEC_what(dst)|=SPEC_what(src);
642 // but there are more important thing right now
644 SPEC_LONG (dest) |= SPEC_LONG (src);
645 dest->select.s._short|=src->select.s._short;
646 SPEC_USIGN (dest) |= SPEC_USIGN (src);
647 dest->select.s._signed|=src->select.s._signed;
648 SPEC_STAT (dest) |= SPEC_STAT (src);
649 SPEC_EXTR (dest) |= SPEC_EXTR (src);
650 SPEC_CONST(dest) |= SPEC_CONST (src);
651 SPEC_ABSA (dest) |= SPEC_ABSA (src);
652 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
653 SPEC_ADDR (dest) |= SPEC_ADDR (src);
654 SPEC_OCLS (dest) = SPEC_OCLS (src);
655 SPEC_BLEN (dest) |= SPEC_BLEN (src);
656 SPEC_BSTR (dest) |= SPEC_BSTR (src);
657 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
658 SPEC_ENUM (dest) |= SPEC_ENUM (src);
659 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
660 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
662 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
663 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
665 /* these are the only function attributes that will be set
666 in a specifier while parsing */
667 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
668 FUNC_BANKED(dest) |= FUNC_BANKED(src);
669 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
670 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
671 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
672 FUNC_ISISR(dest) |= FUNC_ISISR(src);
673 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
674 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
675 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
676 FUNC_INTNO(dest) |= FUNC_INTNO(src);
677 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
682 /*------------------------------------------------------------------*/
683 /* genSymName - generates and returns a name used for anonymous vars */
684 /*------------------------------------------------------------------*/
686 genSymName (int level)
688 static int gCount = 0;
689 static char gname[SDCC_NAME_MAX + 1];
691 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
695 /*------------------------------------------------------------------*/
696 /* getSpec - returns the specifier part from a declaration chain */
697 /*------------------------------------------------------------------*/
699 getSpec (sym_link * p)
704 while (p && !(IS_SPEC (p)))
710 /*------------------------------------------------------------------*/
711 /* newCharLink() - creates an char type */
712 /*------------------------------------------------------------------*/
718 p = newLink (SPECIFIER);
719 SPEC_NOUN (p) = V_CHAR;
724 /*------------------------------------------------------------------*/
725 /* newFloatLink - a new Float type */
726 /*------------------------------------------------------------------*/
732 p = newLink (SPECIFIER);
733 SPEC_NOUN (p) = V_FLOAT;
738 /*------------------------------------------------------------------*/
739 /* newLongLink() - new long type */
740 /*------------------------------------------------------------------*/
746 p = newLink (SPECIFIER);
747 SPEC_NOUN (p) = V_INT;
753 /*------------------------------------------------------------------*/
754 /* newIntLink() - creates an int type */
755 /*------------------------------------------------------------------*/
761 p = newLink (SPECIFIER);
762 SPEC_NOUN (p) = V_INT;
767 /*------------------------------------------------------------------*/
768 /* getSize - returns size of a type chain in bits */
769 /*------------------------------------------------------------------*/
771 getSize (sym_link * p)
773 /* if nothing return 0 */
777 { /* if this is the specifier then */
778 switch (SPEC_NOUN (p))
779 { /* depending on the specifier type */
781 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
789 return SPEC_STRUCT (p)->size;
796 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
802 /* this is a specifier */
803 switch (DCL_TYPE (p))
807 return DCL_ELEM (p) * getSize (p->next);
809 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
810 // "can not tell the size of an array[]");
830 /*------------------------------------------------------------------*/
831 /* bitsForType - returns # of bits required to store this type */
832 /*------------------------------------------------------------------*/
834 bitsForType (sym_link * p)
836 /* if nothing return 0 */
841 { /* if this is the specifier then */
843 switch (SPEC_NOUN (p))
844 { /* depending on the specifier type */
846 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
848 return FLOATSIZE * 8;
854 return SPEC_STRUCT (p)->size * 8;
861 return SPEC_BLEN (p);
867 /* this is a specifier */
868 switch (DCL_TYPE (p))
871 return DCL_ELEM (p) * getSize (p->next) * 8;
875 return (PTRSIZE * 8);
880 return (FPTRSIZE * 8);
882 return (GPTRSIZE * 8);
889 /*------------------------------------------------------------------*/
890 /* copySymbolChain - copies a symbol chain */
891 /*------------------------------------------------------------------*/
893 copySymbolChain (symbol * src)
900 dest = copySymbol (src);
901 dest->next = copySymbolChain (src->next);
905 /*------------------------------------------------------------------*/
906 /* copySymbol - makes a copy of a symbol */
907 /*------------------------------------------------------------------*/
909 copySymbol (symbol * src)
916 dest = newSymbol (src->name, src->level);
917 memcpy (dest, src, sizeof (symbol));
918 dest->level = src->level;
919 dest->block = src->block;
920 dest->ival = copyIlist (src->ival);
921 dest->type = copyLinkChain (src->type);
922 dest->etype = getSpec (dest->type);
924 dest->key = src->key;
925 dest->allocreq = src->allocreq;
929 /*------------------------------------------------------------------*/
930 /* reverseSyms - reverses the links for a symbol chain */
931 /*------------------------------------------------------------------*/
933 reverseSyms (symbol * sym)
935 symbol *prev, *curr, *next;
950 sym->next = (void *) NULL;
954 /*------------------------------------------------------------------*/
955 /* reverseLink - reverses the links for a type chain */
956 /*------------------------------------------------------------------*/
958 reverseLink (sym_link * type)
960 sym_link *prev, *curr, *next;
975 type->next = (void *) NULL;
979 /*------------------------------------------------------------------*/
980 /* addSymChain - adds a symbol chain to the symboltable */
981 /*------------------------------------------------------------------*/
983 addSymChain (symbol * symHead)
985 symbol *sym = symHead;
989 for (; sym != NULL; sym = sym->next)
992 checkTypeSanity(sym->etype, sym->name);
994 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
997 /* if already exists in the symbol table then check if
998 one of them is an extern definition if yes then
999 then check if the type match, if the types match then
1000 delete the current entry and add the new entry */
1001 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1002 csym->level == sym->level) {
1004 /* If the previous definition was for an array with incomplete */
1005 /* type, and the new definition has completed the type, update */
1006 /* the original type to match */
1007 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1008 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1010 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1011 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1015 /* If only one of the definitions used the "at" keyword, copy */
1016 /* the address to the other. */
1017 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1018 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1020 SPEC_ABSA (sym->etype) = 1;
1021 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1023 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1024 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1026 SPEC_ABSA (csym->etype) = 1;
1027 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1032 if (csym->ival && sym->ival)
1034 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1038 /* one definition extern ? */
1039 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1040 werror (E_EXTERN_MISMATCH, sym->name);
1042 werror (E_DUPLICATE, sym->name);
1043 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1045 fprintf (stderr, "from type '");
1046 printTypeChain (csym->type, stderr);
1047 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1048 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1049 fprintf (stderr, "'\nto type '");
1050 printTypeChain (sym->type, stderr);
1051 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1052 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1053 fprintf (stderr, "'\n");
1058 if (csym->ival && !sym->ival)
1059 sym->ival = csym->ival;
1061 /* delete current entry */
1062 deleteSym (SymbolTab, csym, csym->name);
1063 deleteFromSeg(csym);
1067 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1072 /*------------------------------------------------------------------*/
1073 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1074 /*------------------------------------------------------------------*/
1076 funcInChain (sym_link * lnk)
1087 /*------------------------------------------------------------------*/
1088 /* structElemType - returns the type info of a struct member */
1089 /*------------------------------------------------------------------*/
1091 structElemType (sym_link * stype, value * id)
1093 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1094 sym_link *type, *etype;
1095 sym_link *petype = getSpec (stype);
1099 /* look for the id */
1102 if (strcmp (fields->rname, id->name) == 0)
1104 type = copyLinkChain (fields->type);
1105 etype = getSpec (type);
1106 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1107 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1109 SPEC_CONST (type) |= SPEC_CONST (stype);
1111 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1114 fields = fields->next;
1118 werror (E_NOT_MEMBER, id->name);
1120 // the show must go on
1121 return newIntLink();
1124 /*------------------------------------------------------------------*/
1125 /* getStructElement - returns element of a tructure definition */
1126 /*------------------------------------------------------------------*/
1128 getStructElement (structdef * sdef, symbol * sym)
1132 for (field = sdef->fields; field; field = field->next)
1133 if (strcmp (field->name, sym->name) == 0)
1136 werror (E_NOT_MEMBER, sym->name);
1138 return sdef->fields;
1141 /*------------------------------------------------------------------*/
1142 /* compStructSize - computes the size of a structure */
1143 /*------------------------------------------------------------------*/
1145 compStructSize (int su, structdef * sdef)
1147 int sum = 0, usum = 0;
1151 /* for the identifiers */
1152 loop = sdef->fields;
1155 /* create the internal name for this variable */
1156 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1161 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1163 /* if this is a bit field */
1166 /* change it to a unsigned bit */
1167 SPEC_NOUN (loop->etype) = V_BITFIELD;
1168 SPEC_USIGN (loop->etype) = 1;
1169 SPEC_BLEN (loop->etype) = loop->bitVar;
1171 if (loop->bitVar == BITVAR_PAD) {
1172 /* A zero length bitfield forces padding */
1173 SPEC_BSTR (loop->etype) = bitOffset;
1174 SPEC_BLEN (loop->etype) = 0;
1179 if (bitOffset == 8) {
1183 /* check if this fit into the remaining */
1184 /* bits of this byte else align it to the */
1185 /* next byte boundary */
1186 if (loop->bitVar <= (8 - bitOffset)) {
1187 /* fits into current byte */
1189 SPEC_BSTR (loop->etype) = bitOffset;
1190 bitOffset += loop->bitVar;
1192 else if (!bitOffset) {
1193 /* does not fit, but is already byte aligned */
1195 SPEC_BSTR (loop->etype) = bitOffset;
1196 bitOffset += loop->bitVar;
1199 /* does not fit; need to realign first */
1201 loop->offset = (su == UNION ? sum = 0 : sum);
1203 SPEC_BSTR (loop->etype) = bitOffset;
1204 bitOffset += loop->bitVar;
1206 while (bitOffset>8) {
1213 /* This is a non-bit field. Make sure we are */
1214 /* byte aligned first */
1217 loop->offset = (su == UNION ? sum = 0 : sum);
1221 checkDecl (loop, 1);
1222 sum += getSize (loop->type);
1227 /* if union then size = sizeof larget field */
1229 /* For UNION, round up after each field */
1230 sum += ((bitOffset+7)/8);
1231 usum = max (usum, sum);
1236 /* For STRUCT, round up after all fields processed */
1238 sum += ((bitOffset+7)/8);
1240 return (su == UNION ? usum : sum);
1243 /*-------------------------------------------------------------------*/
1244 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1245 /* an enclosing struct/union */
1246 /*-------------------------------------------------------------------*/
1248 promoteAnonStructs (int su, structdef * sdef)
1257 tofield = &sdef->fields;
1258 field = sdef->fields;
1261 nextfield = field->next;
1262 if (!*field->name && IS_STRUCT (field->type))
1264 /* Found an anonymous struct/union. Replace it */
1265 /* with the fields it contains and adjust all */
1268 base = field->offset;
1269 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1271 continue; /* just in case it's empty */
1273 *tofield = subfield;
1276 /* check for field name conflicts resulting from promotion */
1277 dupfield = sdef->fields;
1278 while (dupfield && dupfield != subfield)
1280 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1282 werrorfl (subfield->fileDef, subfield->lineDef,
1284 su==STRUCT ? "struct" : "union",
1286 werrorfl (dupfield->fileDef, dupfield->lineDef,
1289 dupfield = dupfield->next;
1292 subfield->offset += base;
1294 subfield = subfield->next;
1298 subfield->next = nextfield;
1299 tofield = &subfield->next;
1302 tofield = &field->next;
1308 /*------------------------------------------------------------------*/
1309 /* checkSClass - check the storage class specification */
1310 /*------------------------------------------------------------------*/
1312 checkSClass (symbol * sym, int isProto)
1316 if (getenv("DEBUG_SANITY")) {
1317 fprintf (stderr, "checkSClass: %s \n", sym->name);
1320 /* type is literal can happen for enums change
1322 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1323 SPEC_SCLS (sym->etype) = S_AUTO;
1325 /* if sfr or sbit then must also be volatile */
1326 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1327 SPEC_SCLS (sym->etype) == S_SFR)
1329 SPEC_VOLATILE (sym->etype) = 1;
1332 /* if absolute address given then it mark it as
1333 volatile -- except in the PIC port */
1335 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1336 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1337 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1340 if (IS_ABSOLUTE (sym->etype))
1341 SPEC_VOLATILE (sym->etype) = 1;
1343 /* If code memory is read only, then pointers to code memory */
1344 /* implicitly point to constants -- make this explicit */
1346 while (t && t->next) {
1347 if (IS_CODEPTR(t) && port->mem.code_ro) {
1348 if (IS_SPEC(t->next)) {
1349 SPEC_CONST (t->next) = 1;
1351 DCL_PTR_CONST (t->next) = 1;
1357 /* global variables declared const put into code */
1358 /* if no other storage class specified */
1359 if (sym->level == 0 &&
1360 SPEC_SCLS(sym->etype) == S_FIXED &&
1361 !IS_FUNC(sym->type)) {
1362 /* find the first non-array link */
1366 if (IS_CONSTANT (t)) {
1367 SPEC_SCLS (sym->etype) = S_CODE;
1371 /* global variable in code space is a constant */
1372 if (sym->level == 0 &&
1373 SPEC_SCLS (sym->etype) == S_CODE &&
1374 port->mem.code_ro) {
1375 /* find the first non-array link */
1382 DCL_PTR_CONST (t) = 1;
1386 /* if bit variable then no storage class can be */
1387 /* specified since bit is already a storage */
1388 if (IS_BITVAR (sym->etype) &&
1389 (SPEC_SCLS (sym->etype) != S_FIXED &&
1390 SPEC_SCLS (sym->etype) != S_SBIT &&
1391 SPEC_SCLS (sym->etype) != S_BIT)
1394 werror (E_BITVAR_STORAGE, sym->name);
1395 SPEC_SCLS (sym->etype) = S_FIXED;
1398 /* extern variables cannot be initialized */
1399 if (IS_EXTERN (sym->etype) && sym->ival)
1401 werror (E_EXTERN_INIT, sym->name);
1405 /* if this is an automatic symbol */
1406 if (sym->level && (options.stackAuto || reentrant)) {
1407 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1408 SPEC_SCLS (sym->etype) == S_FIXED ||
1409 SPEC_SCLS (sym->etype) == S_REGISTER ||
1410 SPEC_SCLS (sym->etype) == S_STACK ||
1411 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1412 SPEC_SCLS (sym->etype) = S_AUTO;
1414 /* storage class may only be specified for statics */
1415 if (!IS_STATIC(sym->etype)) {
1416 werror (E_AUTO_ASSUMED, sym->name);
1421 /* automatic symbols cannot be given */
1422 /* an absolute address ignore it */
1424 SPEC_ABSA (sym->etype) &&
1425 (options.stackAuto || reentrant))
1427 werror (E_AUTO_ABSA, sym->name);
1428 SPEC_ABSA (sym->etype) = 0;
1431 /* arrays & pointers cannot be defined for bits */
1432 /* SBITS or SFRs or BIT */
1433 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1434 (SPEC_NOUN (sym->etype) == V_BIT ||
1435 SPEC_NOUN (sym->etype) == V_SBIT ||
1436 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1437 SPEC_SCLS (sym->etype) == S_SFR))
1438 werror (E_BIT_ARRAY, sym->name);
1440 /* if this is a bit|sbit then set length & start */
1441 if (SPEC_NOUN (sym->etype) == V_BIT ||
1442 SPEC_NOUN (sym->etype) == V_SBIT)
1444 SPEC_BLEN (sym->etype) = 1;
1445 SPEC_BSTR (sym->etype) = 0;
1449 /* variables declared in CODE space must have */
1450 /* initializers if not an extern */
1451 if (SPEC_SCLS (sym->etype) == S_CODE &&
1452 sym->ival == NULL &&
1454 port->mem.code_ro &&
1455 !IS_EXTERN (sym->etype) &&
1456 !funcInChain (sym->type))
1457 werror (E_CODE_NO_INIT, sym->name);
1460 /* if parameter or local variable then change */
1461 /* the storage class to reflect where the var will go */
1462 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1463 !IS_STATIC(sym->etype))
1465 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1467 SPEC_SCLS (sym->etype) = (options.useXstack ?
1468 S_XSTACK : S_STACK);
1472 /* hack-o-matic! I see no reason why the useXstack option should ever
1473 * control this allcoation, but the code was originally that way, and
1474 * changing it for non-390 ports breaks the compiler badly.
1476 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1477 1 : options.useXstack;
1478 SPEC_SCLS (sym->etype) = (useXdata ?
1484 /*------------------------------------------------------------------*/
1485 /* changePointer - change pointer to functions */
1486 /*------------------------------------------------------------------*/
1488 changePointer (symbol * sym)
1492 /* go thru the chain of declarations */
1493 /* if we find a pointer to a function */
1494 /* unconditionally change it to a ptr */
1496 for (p = sym->type; p; p = p->next)
1498 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1499 DCL_TYPE (p) = port->unqualified_pointer;
1500 if (IS_PTR (p) && IS_FUNC (p->next))
1501 DCL_TYPE (p) = CPOINTER;
1505 /*------------------------------------------------------------------*/
1506 /* checkDecl - does semantic validation of a declaration */
1507 /*------------------------------------------------------------------*/
1509 checkDecl (symbol * sym, int isProto)
1512 checkSClass (sym, isProto); /* check the storage class */
1513 changePointer (sym); /* change pointers if required */
1515 /* if this is an array without any dimension
1516 then update the dimension from the initial value */
1517 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1518 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1523 /*------------------------------------------------------------------*/
1524 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1525 /*------------------------------------------------------------------*/
1527 copyLinkChain (sym_link * p)
1529 sym_link *head, *curr, *loop;
1532 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1535 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1536 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1545 /*------------------------------------------------------------------*/
1546 /* cleanUpBlock - cleansup the symbol table specified for all the */
1547 /* symbols in the given block */
1548 /*------------------------------------------------------------------*/
1550 cleanUpBlock (bucket ** table, int block)
1555 /* go thru the entire table */
1556 for (i = 0; i < 256; i++)
1558 for (chain = table[i]; chain; chain = chain->next)
1560 if (chain->block >= block)
1562 deleteSym (table, chain->sym, chain->name);
1568 /*------------------------------------------------------------------*/
1569 /* cleanUpLevel - cleansup the symbol table specified for all the */
1570 /* symbols in the given level */
1571 /*------------------------------------------------------------------*/
1573 cleanUpLevel (bucket ** table, int level)
1578 /* go thru the entire table */
1579 for (i = 0; i < 256; i++)
1581 for (chain = table[i]; chain; chain = chain->next)
1583 if (chain->level >= level)
1585 deleteSym (table, chain->sym, chain->name);
1591 /*------------------------------------------------------------------*/
1592 /* computeTypeOr - computes the resultant type from two types */
1593 /*------------------------------------------------------------------*/
1595 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1598 assert (IS_CHAR (etype1) && IS_CHAR (etype2));
1600 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1602 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1606 if (SPEC_USIGN (etype1))
1608 if ( IS_LITERAL (etype2)
1609 && floatFromVal (valFromType (etype2)) >= 0)
1610 SPEC_USIGN (reType) = 1;
1613 /* promote to int */
1614 SPEC_USIGN (reType) = 0;
1615 SPEC_NOUN (reType) = V_INT;
1618 else /* etype1 signed */
1620 if ( IS_LITERAL (etype2)
1621 && floatFromVal (valFromType (etype2)) <= 127)
1622 SPEC_USIGN (reType) = 0;
1625 /* promote to int */
1626 SPEC_USIGN (reType) = 0;
1627 SPEC_NOUN (reType) = V_INT;
1631 if (SPEC_USIGN (etype2))
1633 if ( IS_LITERAL (etype1)
1634 && floatFromVal (valFromType (etype1)) >= 0)
1635 SPEC_USIGN (reType) = 1;
1638 /* promote to int */
1639 SPEC_USIGN (reType) = 0;
1640 SPEC_NOUN (reType) = V_INT;
1643 else /* etype2 signed */
1645 if ( IS_LITERAL (etype1)
1646 && floatFromVal (valFromType (etype1)) <= 127)
1647 SPEC_USIGN (reType) = 0;
1650 /* promote to int */
1651 SPEC_USIGN (reType) = 0;
1652 SPEC_NOUN (reType) = V_INT;
1658 /*------------------------------------------------------------------*/
1659 /* computeType - computes the resultant type from two types */
1660 /*------------------------------------------------------------------*/
1662 computeType (sym_link * type1, sym_link * type2,
1663 RESULT_TYPE resultType, char op)
1667 sym_link *etype1 = getSpec (type1);
1670 etype2 = type2 ? getSpec (type2) : type1;
1672 /* if one of them is a float then result is a float */
1673 /* here we assume that the types passed are okay */
1674 /* and can be cast to one another */
1675 /* which ever is greater in size */
1676 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1677 rType = newFloatLink ();
1679 /* if both are bitvars choose the larger one */
1680 if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1682 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1683 copyLinkChain (type1) : copyLinkChain (type1);
1685 /* if only one of them is a bit variable
1686 then the other one prevails */
1687 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1689 rType = copyLinkChain (type2);
1690 /* bitfield can have up to 16 bits */
1691 if (getSize (etype1) > 1)
1692 SPEC_NOUN (getSpec (rType)) = V_INT;
1694 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1696 rType = copyLinkChain (type1);
1697 /* bitfield can have up to 16 bits */
1698 if (getSize (etype2) > 1)
1699 SPEC_NOUN (getSpec (rType)) = V_INT;
1702 /* if one of them is a pointer or array then that
1704 if (IS_PTR (type1) || IS_ARRAY (type1))
1705 rType = copyLinkChain (type1);
1706 else if (IS_PTR (type2) || IS_ARRAY (type2))
1707 rType = copyLinkChain (type2);
1708 else if (getSize (type1) > getSize (type2))
1709 rType = copyLinkChain (type1);
1711 rType = copyLinkChain (type2);
1713 reType = getSpec (rType);
1715 /* avoid conflicting types */
1716 reType->select.s._signed = 0;
1718 /* if result is a literal then make not so */
1719 if (IS_LITERAL (reType))
1720 SPEC_SCLS (reType) = S_REGISTER;
1724 case RESULT_TYPE_CHAR:
1725 if (IS_BITVAR (reType))
1727 SPEC_NOUN (reType) = V_CHAR;
1728 SPEC_SCLS (reType) = 0;
1729 SPEC_USIGN (reType) = 0;
1733 case RESULT_TYPE_INT:
1734 case RESULT_TYPE_NONE:
1735 if (IS_BIT (reType))
1737 SPEC_NOUN (reType) = V_CHAR;
1738 SPEC_SCLS (reType) = 0;
1739 SPEC_USIGN (reType) = 0;
1742 else if (IS_BITFIELD (reType))
1744 /* could be smarter, but it depends on the op */
1745 /* this is for the worst case: a multiplication of 4 * 4 bit */
1746 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1747 SPEC_SCLS (reType) = 0;
1748 SPEC_USIGN (reType) = 0;
1751 else if (IS_CHAR (reType))
1753 if (op == '|' || op == '^')
1754 return computeTypeOr (etype1, etype2, reType);
1756 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1758 SPEC_USIGN (reType) = 1;
1763 SPEC_NOUN (reType) = V_INT;
1764 SPEC_USIGN (reType) = 0;
1773 /* SDCC's sign promotion:
1774 - if one or both operands are unsigned, the resultant type will be unsigned
1775 (except char, see below)
1776 - if an operand is promoted to a larger type (char -> int, int -> long),
1777 the larger type will be signed
1779 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1780 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1781 the standard. The standard demands, that the result has to be the same
1782 "as if" the promotion would have been performed:
1784 - if the result of an operation with two char's is promoted to a
1785 larger type, the result will be signed.
1787 More sophisticated are these:
1788 - if the result of an operation with two char's is a char again,
1789 the result will only then be unsigned, if both operands are
1790 unsigned. In all other cases the result will be signed.
1792 This seems to be contradictionary to the first two rules, but it makes
1793 real sense (all types are char's):
1795 A signed char can be negative; this must be preserved in the result
1798 Only if both operands are unsigned it's safe to make the result
1799 unsigned; this helps to avoid overflow:
1802 - ToDo: document '|', '^' and '&'
1804 Homework: - why is (200 * 200 < 0) true?
1805 - why is { char l = 200, r = 200; (r * l > 0) } true?
1808 if (!IS_FLOAT (reType)
1809 && ( (SPEC_USIGN (etype1)
1810 /* if this operand is promoted to a larger type,
1811 then it will be promoted to a signed type */
1812 && !(getSize (etype1) < getSize (reType))
1813 /* char require special handling */
1814 && !IS_CHAR (etype1))
1815 || /* same for 2nd operand */
1816 (SPEC_USIGN (etype2)
1817 && !(getSize (etype2) < getSize (reType))
1818 && !IS_CHAR (etype2))
1819 || /* if both are 'unsigned char' and not promoted
1820 let the result be unsigned too */
1821 ( SPEC_USIGN (etype1)
1822 && SPEC_USIGN (etype2)
1825 && IS_CHAR (reType))))
1826 SPEC_USIGN (reType) = 1;
1828 SPEC_USIGN (reType) = 0;
1833 /*--------------------------------------------------------------------*/
1834 /* compareType - will do type check return 1 if match, -1 if castable */
1835 /*--------------------------------------------------------------------*/
1837 compareType (sym_link * dest, sym_link * src)
1848 /* if dest is a declarator then */
1853 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1855 //checkFunction(src,dest);
1857 return compareType (dest->next, src->next);
1859 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1862 if (IS_PTR (src) && IS_GENPTR (dest))
1864 if (IS_PTR (dest) && IS_ARRAY (src)) {
1865 value *val=aggregateToPointer (valFromType(src));
1866 int res=compareType (dest, val->type);
1867 Safe_free(val->type);
1871 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1872 return compareType (dest->next, src);
1875 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1881 /* if one is a specifier and the other is not */
1882 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1883 (IS_SPEC (dest) && !IS_SPEC (src)))
1886 /* if one of them is a void then ok */
1887 if (SPEC_NOUN (dest) == V_VOID &&
1888 SPEC_NOUN (src) != V_VOID)
1891 if (SPEC_NOUN (dest) != V_VOID &&
1892 SPEC_NOUN (src) == V_VOID)
1895 /* if they are both bitfields then if the lengths
1896 and starts don't match */
1897 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1898 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1899 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1902 /* it is a specifier */
1903 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1905 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1906 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1907 getSize (dest) == getSize (src))
1909 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1914 else if (IS_STRUCT (dest))
1916 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1921 if (SPEC_LONG (dest) != SPEC_LONG (src))
1924 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1930 /*--------------------------------------------------------------------*/
1931 /* compareTypeExact - will do type check return 1 if match exactly */
1932 /*--------------------------------------------------------------------*/
1934 compareTypeExact (sym_link * dest, sym_link * src, int level)
1936 STORAGE_CLASS srcScls, destScls;
1947 /* if dest is a declarator then */
1952 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1953 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
1955 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
1957 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
1961 value *exargs, *acargs, *checkValue;
1963 /* verify function return type */
1964 if (!compareTypeExact (dest->next, src->next, -1))
1966 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
1968 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
1970 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
1973 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
1977 /* compare expected args with actual args */
1978 exargs = FUNC_ARGS(dest);
1979 acargs = FUNC_ARGS(src);
1981 /* for all the expected args do */
1982 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
1984 //checkTypeSanity(acargs->etype, acargs->name);
1986 if (IS_AGGREGATE (acargs->type))
1988 checkValue = copyValue (acargs);
1989 aggregateToPointer (checkValue);
1992 checkValue = acargs;
1995 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2000 /* if one them ended we have a problem */
2001 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2002 (!exargs && acargs && !IS_VOID (acargs->type)))
2006 return compareTypeExact (dest->next, src->next, level);
2013 /* if one is a specifier and the other is not */
2014 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2015 (IS_SPEC (dest) && !IS_SPEC (src)))
2018 /* if one of them is a void then ok */
2019 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2022 /* if they are both bitfields then if the lengths
2023 and starts don't match */
2024 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2025 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2026 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2029 if (IS_INTEGRAL (dest))
2031 /* signedness must match */
2032 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2034 /* size must match */
2035 if (SPEC_LONG (dest) != SPEC_LONG (src))
2037 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2041 if (IS_STRUCT (dest))
2043 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2047 if (SPEC_CONST (dest) != SPEC_CONST (src))
2049 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2051 if (SPEC_STAT (dest) != SPEC_STAT (src))
2053 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2055 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2058 destScls = SPEC_SCLS (dest);
2059 srcScls = SPEC_SCLS (src);
2061 /* Compensate for const to const code change in checkSClass() */
2062 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2064 if (srcScls == S_CODE && destScls == S_FIXED)
2066 if (destScls == S_CODE && srcScls == S_FIXED)
2070 /* compensate for allocGlobal() */
2071 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2072 && port->mem.default_globl_map == xdata
2076 if (level>0 && !SPEC_STAT (dest))
2078 /* Compensate for hack-o-matic in checkSClass() */
2079 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2081 if (destScls == S_FIXED)
2082 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2083 if (srcScls == S_FIXED)
2084 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2086 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2088 if (destScls == S_FIXED)
2090 if (srcScls == S_FIXED)
2095 if (srcScls != destScls)
2098 printf ("level = %d\n", level);
2099 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2100 SPEC_SCLS (src), SPEC_SCLS (dest));
2101 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2109 /*------------------------------------------------------------------*/
2110 /* inCalleeSaveList - return 1 if found in callee save list */
2111 /*------------------------------------------------------------------*/
2113 calleeCmp(void *p1, void *p2)
2115 return (strcmp((char *)p1, (char *)(p2)) == 0);
2119 inCalleeSaveList(char *s)
2121 if (options.all_callee_saves)
2123 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2126 /*-----------------------------------------------------------------*/
2127 /* aggregateToPointer: change an agggregate type function */
2128 /* argument to a pointer to that type. */
2129 /*-----------------------------------------------------------------*/
2131 aggregateToPointer (value * val)
2133 if (IS_AGGREGATE (val->type))
2135 /* if this is a structure */
2136 /* then we need to add a new link */
2137 if (IS_STRUCT (val->type))
2139 /* first lets add DECLARATOR type */
2140 sym_link *p = val->type;
2142 werror (W_STRUCT_AS_ARG, val->name);
2143 val->type = newLink (DECLARATOR);
2144 val->type->next = p;
2147 /* change to a pointer depending on the */
2148 /* storage class specified */
2149 switch (SPEC_SCLS (val->etype))
2152 DCL_TYPE (val->type) = IPOINTER;
2155 DCL_TYPE (val->type) = PPOINTER;
2158 if (SPEC_OCLS(val->etype)) {
2159 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2161 // this happens for (external) function parameters
2162 DCL_TYPE (val->type) = port->unqualified_pointer;
2168 DCL_TYPE (val->type) = POINTER;
2171 DCL_TYPE (val->type) = CPOINTER;
2174 DCL_TYPE (val->type) = FPOINTER;
2177 DCL_TYPE (val->type) = EEPPOINTER;
2180 DCL_TYPE (val->type) = port->unqualified_pointer;
2183 /* is there is a symbol associated then */
2184 /* change the type of the symbol as well */
2187 val->sym->type = copyLinkChain (val->type);
2188 val->sym->etype = getSpec (val->sym->type);
2193 /*------------------------------------------------------------------*/
2194 /* checkFunction - does all kinds of check on a function */
2195 /*------------------------------------------------------------------*/
2197 checkFunction (symbol * sym, symbol *csym)
2199 value *exargs, *acargs;
2203 if (getenv("DEBUG_SANITY")) {
2204 fprintf (stderr, "checkFunction: %s ", sym->name);
2207 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2209 werror(E_SYNTAX_ERROR, sym->name);
2213 /* make sure the type is complete and sane */
2214 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2216 /* if not type then some kind of error */
2220 /* if the function has no type then make it return int */
2221 if (!sym->type->next)
2222 sym->type->next = sym->etype = newIntLink ();
2224 /* function cannot return aggregate */
2225 if (IS_AGGREGATE (sym->type->next))
2227 werror (E_FUNC_AGGR, sym->name);
2231 /* function cannot return bit */
2232 if (IS_BITVAR (sym->type->next))
2234 werror (E_FUNC_BIT, sym->name);
2238 /* check if this function is defined as calleeSaves
2239 then mark it as such */
2240 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2242 /* if interrupt service routine */
2243 /* then it cannot have arguments */
2244 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2246 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2247 werror (E_INT_ARGS, sym->name);
2248 FUNC_ARGS(sym->type)=NULL;
2252 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2254 acargs=acargs->next, argCnt++) {
2256 // this can happen for reentrant functions
2257 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2258 // the show must go on: synthesize a name and symbol
2259 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2260 acargs->sym = newSymbol (acargs->name, 1);
2261 SPEC_OCLS (acargs->etype) = istack;
2262 acargs->sym->type = copyLinkChain (acargs->type);
2263 acargs->sym->etype = getSpec (acargs->sym->type);
2264 acargs->sym->_isparm = 1;
2265 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2266 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2268 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2273 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2274 return 1; /* not defined nothing more to check */
2276 /* check if body already present */
2277 if (csym && IFFUNC_HASBODY(csym->type))
2279 werror (E_FUNC_BODY, sym->name);
2283 /* check the return value type */
2284 if (compareType (csym->type, sym->type) <= 0)
2286 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2287 printFromToType(csym->type, sym->type);
2291 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2293 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2296 if (FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type))
2298 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2301 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2303 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2306 /* Really, reentrant should match regardless of argCnt, but */
2307 /* this breaks some existing code (the fp lib functions). If */
2308 /* the first argument is always passed the same way, this */
2309 /* lax checking is ok (but may not be true for in future ports) */
2310 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2313 //printf("argCnt = %d\n",argCnt);
2314 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2317 /* compare expected args with actual args */
2318 exargs = FUNC_ARGS(csym->type);
2319 acargs = FUNC_ARGS(sym->type);
2321 /* for all the expected args do */
2324 exargs = exargs->next, acargs = acargs->next, argCnt++)
2326 if (getenv("DEBUG_SANITY")) {
2327 fprintf (stderr, "checkFunction: %s ", exargs->name);
2329 /* make sure the type is complete and sane */
2330 checkTypeSanity(exargs->etype, exargs->name);
2332 /* If the actual argument is an array, any prototype
2333 * will have modified it to a pointer. Duplicate that
2336 if (IS_AGGREGATE (acargs->type))
2338 checkValue = copyValue (acargs);
2339 aggregateToPointer (checkValue);
2343 checkValue = acargs;
2346 if (compareType (exargs->type, checkValue->type) <= 0)
2348 werror (E_ARG_TYPE, argCnt);
2349 printFromToType(exargs->type, checkValue->type);
2354 /* if one them ended we have a problem */
2355 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2356 (!exargs && acargs && !IS_VOID (acargs->type)))
2357 werror (E_ARG_COUNT);
2359 /* replace with this defition */
2360 sym->cdef = csym->cdef;
2361 deleteSym (SymbolTab, csym, csym->name);
2362 deleteFromSeg(csym);
2363 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2364 if (IS_EXTERN (csym->etype) && !
2365 IS_EXTERN (sym->etype))
2367 addSet (&publics, sym);
2372 /*------------------------------------------------------------------*/
2373 /* cdbStructBlock - calls struct printing for a blcks */
2374 /*------------------------------------------------------------------*/
2375 void cdbStructBlock (int block)
2378 bucket **table = StructTab;
2381 /* go thru the entire table */
2382 for (i = 0; i < 256; i++)
2384 for (chain = table[i]; chain; chain = chain->next)
2386 if (chain->block >= block)
2389 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2395 /*-----------------------------------------------------------------*/
2396 /* processFuncArgs - does some processing with function args */
2397 /*-----------------------------------------------------------------*/
2399 processFuncArgs (symbol * func)
2403 sym_link *funcType=func->type;
2405 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2406 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2408 /* find the function declaration within the type */
2409 while (funcType && !IS_FUNC(funcType))
2410 funcType=funcType->next;
2412 /* if this function has variable argument list */
2413 /* then make the function a reentrant one */
2414 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2415 FUNC_ISREENT(funcType)=1;
2417 /* check if this function is defined as calleeSaves
2418 then mark it as such */
2419 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2421 /* loop thru all the arguments */
2422 val = FUNC_ARGS(funcType);
2424 /* if it is void then remove parameters */
2425 if (val && IS_VOID (val->type))
2427 FUNC_ARGS(funcType) = NULL;
2431 /* reset regparm for the port */
2432 (*port->reset_regparms) ();
2433 /* if any of the arguments is an aggregate */
2434 /* change it to pointer to the same type */
2438 /* mark it as a register parameter if
2439 the function does not have VA_ARG
2440 and as port dictates */
2441 if (!IFFUNC_HASVARARGS(funcType) &&
2442 (argreg = (*port->reg_parm) (val->type)))
2444 SPEC_REGPARM (val->etype) = 1;
2445 SPEC_ARGREG(val->etype) = argreg;
2446 } else if (IFFUNC_ISREENT(funcType)) {
2447 FUNC_HASSTACKPARM(funcType) = 1;
2450 if (IS_AGGREGATE (val->type))
2452 aggregateToPointer (val);
2459 /* if this is an internal generated function call */
2461 /* ignore --stack-auto for this one, we don't know how it is compiled */
2462 /* simply trust on --int-long-reent or --float-reent */
2463 if (IFFUNC_ISREENT(funcType)) {
2467 /* if this function is reentrant or */
2468 /* automatics r 2b stacked then nothing */
2469 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2473 val = FUNC_ARGS(funcType);
2478 /* if a symbolname is not given */
2479 /* synthesize a variable name */
2482 SNPRINTF (val->name, sizeof(val->name),
2483 "_%s_PARM_%d", func->name, pNum++);
2484 val->sym = newSymbol (val->name, 1);
2485 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2486 val->sym->type = copyLinkChain (val->type);
2487 val->sym->etype = getSpec (val->sym->type);
2488 val->sym->_isparm = 1;
2489 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2491 /* ?? static functions shouldn't imply static parameters - EEP */
2492 if (IS_SPEC(func->etype)) {
2493 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2494 SPEC_STAT (func->etype);
2497 addSymChain (val->sym);
2500 else /* symbol name given create synth name */
2503 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2504 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2505 val->sym->_isparm = 1;
2506 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2507 (options.model != MODEL_SMALL ? xdata : data);
2510 /* ?? static functions shouldn't imply static parameters - EEP */
2511 if (IS_SPEC(func->etype)) {
2512 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2513 SPEC_STAT (func->etype);
2517 if (!isinSet(operKeyReset, val->sym)) {
2518 addSet (&operKeyReset, val->sym);
2519 applyToSet (operKeyReset, resetParmKey);
2525 /*-----------------------------------------------------------------*/
2526 /* isSymbolEqual - compares two symbols return 1 if they match */
2527 /*-----------------------------------------------------------------*/
2529 isSymbolEqual (symbol * dest, symbol * src)
2531 /* if pointers match then equal */
2535 /* if one of them is null then don't match */
2539 /* if both of them have rname match on rname */
2540 if (dest->rname[0] && src->rname[0])
2541 return (!strcmp (dest->rname, src->rname));
2543 /* otherwise match on name */
2544 return (!strcmp (dest->name, src->name));
2547 void PT(sym_link *type)
2549 printTypeChain(type,0);
2551 /*-----------------------------------------------------------------*/
2552 /* printTypeChain - prints the type chain in human readable form */
2553 /*-----------------------------------------------------------------*/
2555 printTypeChain (sym_link * start, FILE * of)
2559 sym_link * type, * search;
2569 fprintf (of, "void");
2573 /* Print the chain as it is written in the source: */
2574 /* start with the last entry. */
2575 /* However, the storage class at the end of the */
2576 /* chain reall applies to the first in the chain! */
2578 for (type = start; type && type->next; type = type->next)
2581 scls=SPEC_SCLS(type);
2589 case S_DATA: fprintf (of, "data-"); break;
2590 case S_XDATA: fprintf (of, "xdata-"); break;
2591 case S_SFR: fprintf (of, "sfr-"); break;
2592 case S_SBIT: fprintf (of, "sbit-"); break;
2593 case S_CODE: fprintf (of, "code-"); break;
2594 case S_IDATA: fprintf (of, "idata-"); break;
2595 case S_PDATA: fprintf (of, "pdata-"); break;
2596 case S_LITERAL: fprintf (of, "literal-"); break;
2597 case S_STACK: fprintf (of, "stack-"); break;
2598 case S_XSTACK: fprintf (of, "xstack-"); break;
2599 case S_BIT: fprintf (of, "bit-"); break;
2600 case S_EEPROM: fprintf (of, "eeprom-"); break;
2607 if (!IS_FUNC(type)) {
2608 if (DCL_PTR_VOLATILE (type)) {
2609 fprintf (of, "volatile-");
2611 if (DCL_PTR_CONST (type)) {
2612 fprintf (of, "const-");
2615 switch (DCL_TYPE (type))
2618 fprintf (of, "function %s %s",
2619 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2620 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2622 for (args = FUNC_ARGS(type);
2625 printTypeChain(args->type, of);
2632 fprintf (of, "generic* ");
2635 fprintf (of, "code* ");
2638 fprintf (of, "xdata* ");
2641 fprintf (of, "eeprom* ");
2644 fprintf (of, "near* ");
2647 fprintf (of, "idata* ");
2650 fprintf (of, "pdata* ");
2653 fprintf (of, "unknown* ");
2656 if (DCL_ELEM(type)) {
2657 fprintf (of, "[%d] ", DCL_ELEM(type));
2659 fprintf (of, "[] ");
2666 if (SPEC_VOLATILE (type))
2667 fprintf (of, "volatile-");
2668 if (SPEC_CONST (type))
2669 fprintf (of, "const-");
2670 if (SPEC_USIGN (type))
2671 fprintf (of, "unsigned-");
2672 switch (SPEC_NOUN (type))
2676 fprintf (of, "long-");
2677 fprintf (of, "int");
2681 fprintf (of, "char");
2685 fprintf (of, "void");
2689 fprintf (of, "float");
2693 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2697 fprintf (of, "sbit");
2701 fprintf (of, "bit");
2705 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2709 fprintf (of, "double");
2713 fprintf (of, "unknown type");
2717 /* search entry in list before "type" */
2718 for (search = start; search && search->next != type;)
2719 search = search->next;
2728 /*--------------------------------------------------------------------*/
2729 /* printTypeChainRaw - prints the type chain in human readable form */
2730 /* in the raw data structure ordering */
2731 /*--------------------------------------------------------------------*/
2733 printTypeChainRaw (sym_link * start, FILE * of)
2746 fprintf (of, "void");
2756 if (!IS_FUNC(type)) {
2757 if (DCL_PTR_VOLATILE (type)) {
2758 fprintf (of, "volatile-");
2760 if (DCL_PTR_CONST (type)) {
2761 fprintf (of, "const-");
2764 switch (DCL_TYPE (type))
2767 fprintf (of, "function %s %s",
2768 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2769 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2771 for (args = FUNC_ARGS(type);
2774 printTypeChain(args->type, of);
2781 fprintf (of, "generic* ");
2784 fprintf (of, "code* ");
2787 fprintf (of, "xdata* ");
2790 fprintf (of, "eeprom* ");
2793 fprintf (of, "near* ");
2796 fprintf (of, "idata* ");
2799 fprintf (of, "pdata* ");
2802 fprintf (of, "unknown* ");
2805 if (DCL_ELEM(type)) {
2806 fprintf (of, "[%d] ", DCL_ELEM(type));
2808 fprintf (of, "[] ");
2812 if (DCL_TSPEC(type))
2815 printTypeChainRaw(DCL_TSPEC(type), of);
2819 else if (IS_SPEC (type))
2821 switch (SPEC_SCLS (type))
2823 case S_DATA: fprintf (of, "data-"); break;
2824 case S_XDATA: fprintf (of, "xdata-"); break;
2825 case S_SFR: fprintf (of, "sfr-"); break;
2826 case S_SBIT: fprintf (of, "sbit-"); break;
2827 case S_CODE: fprintf (of, "code-"); break;
2828 case S_IDATA: fprintf (of, "idata-"); break;
2829 case S_PDATA: fprintf (of, "pdata-"); break;
2830 case S_LITERAL: fprintf (of, "literal-"); break;
2831 case S_STACK: fprintf (of, "stack-"); break;
2832 case S_XSTACK: fprintf (of, "xstack-"); break;
2833 case S_BIT: fprintf (of, "bit-"); break;
2834 case S_EEPROM: fprintf (of, "eeprom-"); break;
2837 if (SPEC_VOLATILE (type))
2838 fprintf (of, "volatile-");
2839 if (SPEC_CONST (type))
2840 fprintf (of, "const-");
2841 if (SPEC_USIGN (type))
2842 fprintf (of, "unsigned-");
2843 switch (SPEC_NOUN (type))
2847 fprintf (of, "long-");
2848 fprintf (of, "int");
2852 fprintf (of, "char");
2856 fprintf (of, "void");
2860 fprintf (of, "float");
2864 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2868 fprintf (of, "sbit");
2872 fprintf (of, "bit");
2876 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2880 fprintf (of, "double");
2884 fprintf (of, "unknown type");
2889 fprintf (of, "NOT_SPEC_OR_DECL");
2899 /*-----------------------------------------------------------------*/
2900 /* powof2 - returns power of two for the number if number is pow 2 */
2901 /*-----------------------------------------------------------------*/
2903 powof2 (TYPE_UDWORD num)
2916 if (n1s > 1 || nshifts == 0)
2932 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2933 symbol *__muldiv[3][3][2];
2934 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2935 sym_link *__multypes[3][2];
2936 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2937 symbol *__conv[2][3][2];
2938 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2939 symbol *__rlrr[2][3][2];
2941 sym_link *floatType;
2944 _mangleFunctionName(char *in)
2946 if (port->getMangledFunctionName)
2948 return port->getMangledFunctionName(in);
2956 /*-----------------------------------------------------------------*/
2957 /* typeFromStr - create a typechain from an encoded string */
2958 /* basic types - 'c' - char */
2964 /* '*' - pointer - default (GPOINTER) */
2965 /* modifiers - 'u' - unsigned */
2966 /* pointer modifiers - 'g' - generic */
2970 /* 'F' - function */
2971 /* examples : "ig*" - generic int * */
2972 /* "cx*" - char xdata * */
2973 /* "ui" - unsigned int */
2974 /*-----------------------------------------------------------------*/
2975 sym_link *typeFromStr (char *s)
2977 sym_link *r = newLink(DECLARATOR);
2989 r->class = SPECIFIER;
2990 SPEC_NOUN(r) = V_CHAR;
2994 r->class = SPECIFIER;
2995 SPEC_NOUN(r) = V_INT;
2998 r->class = SPECIFIER;
2999 SPEC_NOUN(r) = V_INT;
3003 r->class = SPECIFIER;
3004 SPEC_NOUN(r) = V_FLOAT;
3007 r->class = SPECIFIER;
3008 SPEC_NOUN(r) = V_VOID;
3011 DCL_TYPE(r) = port->unqualified_pointer;
3018 assert(*(s+1)=='*');
3019 nr = newLink(DECLARATOR);
3024 DCL_TYPE(r) = GPOINTER;
3027 DCL_TYPE(r) = FPOINTER;
3030 DCL_TYPE(r) = CPOINTER;
3033 DCL_TYPE(r) = POINTER;
3036 DCL_TYPE(r) = FUNCTION;
3037 nr = newLink(DECLARATOR);
3040 DCL_TYPE(r) = CPOINTER;
3046 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3047 "typeFromStr: unknown type");
3050 if (IS_SPEC(r) && usign) {
3059 /*-----------------------------------------------------------------*/
3060 /* initCSupport - create functions for C support routines */
3061 /*-----------------------------------------------------------------*/
3065 const char *smuldivmod[] =
3069 const char *sbwd[] =
3071 "char", "int", "long"
3077 const char *srlrr[] =
3082 int bwd, su, muldivmod, tofrom, rlrr;
3084 if (getenv("SDCC_NO_C_SUPPORT")) {
3085 /* for debugging only */
3089 floatType = newFloatLink ();
3091 for (bwd = 0; bwd < 3; bwd++)
3108 __multypes[bwd][0] = l;
3109 __multypes[bwd][1] = copyLinkChain (l);
3110 SPEC_USIGN (__multypes[bwd][1]) = 1;
3113 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3114 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3115 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3116 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3117 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3118 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3119 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3120 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3121 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3122 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3124 for (tofrom = 0; tofrom < 2; tofrom++)
3126 for (bwd = 0; bwd < 3; bwd++)
3128 for (su = 0; su < 2; su++)
3132 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3133 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3137 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3138 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3145 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3147 for (bwd = 0; bwd < 3; bwd++)
3149 for (su = 0; su < 2; su++)
3151 SNPRINTF (buffer, sizeof(buffer),
3153 smuldivmod[muldivmod],
3156 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3157 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3162 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3163 Therefore they've been merged into mulint() and mullong().
3166 for (bwd = 0; bwd < 3; bwd++)
3168 for (su = 0; su < 2; su++)
3170 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3173 SNPRINTF (buffer, sizeof(buffer),
3175 smuldivmod[muldivmod],
3178 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3179 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3187 for (su = 0; su < 2; su++)
3189 /* muluchar and mulschar are still separate functions, because e.g. the z80
3190 port is sign/zero-extending to int before calling mulint() */
3191 SNPRINTF (buffer, sizeof(buffer),
3193 smuldivmod[muldivmod],
3196 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3197 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3201 /* word and doubleword */
3202 for (bwd = 1; bwd < 3; bwd++)
3205 SNPRINTF (buffer, sizeof(buffer),
3207 smuldivmod[muldivmod],
3209 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3210 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3211 /* signed = unsigned */
3212 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3215 for (rlrr = 0; rlrr < 2; rlrr++)
3217 for (bwd = 0; bwd < 3; bwd++)
3219 for (su = 0; su < 2; su++)
3221 SNPRINTF (buffer, sizeof(buffer),
3226 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3227 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3233 /*-----------------------------------------------------------------*/
3234 /* initBuiltIns - create prototypes for builtin functions */
3235 /*-----------------------------------------------------------------*/
3241 if (!port->builtintable) return ;
3243 for (i = 0 ; port->builtintable[i].name ; i++) {
3244 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3245 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3246 FUNC_ISBUILTIN(sym->type) = 1;
3247 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3251 sym_link *validateLink(sym_link *l,
3258 if (l && l->class==select)
3263 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3264 " expected %s, got %s\n",
3265 macro, args, file, line,
3266 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3268 return l; // never reached, makes compiler happy.
3271 /*--------------------------------------------------------------------*/
3272 /* newEnumType - create an integer type compatible with enumerations */
3273 /*--------------------------------------------------------------------*/
3275 newEnumType (symbol *enumlist)
3283 type = newLink (SPECIFIER);
3284 SPEC_NOUN (type) = V_INT;
3288 /* Determine the range of the enumerated values */
3290 min = max = (int) floatFromVal (valFromType (sym->type));
3291 for (sym = sym->next; sym; sym = sym->next)
3293 v = (int) floatFromVal (valFromType (sym->type));
3300 /* Determine the smallest integer type that is compatible with this range */
3301 type = newLink (SPECIFIER);
3302 if (min>=0 && max<=255)
3304 SPEC_NOUN (type) = V_CHAR;
3305 SPEC_USIGN (type) = 1;
3307 else if (min>=-128 && max<=127)
3309 SPEC_NOUN (type) = V_CHAR;
3311 else if (min>=0 && max<=65535)
3313 SPEC_NOUN (type) = V_INT;
3314 SPEC_USIGN (type) = 1;
3316 else if (min>=-32768 && max<=32767)
3318 SPEC_NOUN (type) = V_INT;
3322 SPEC_NOUN (type) = V_INT;
3323 SPEC_LONG (type) = 1;
3325 SPEC_USIGN (type) = 1;