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 -------------------------------------------------------------------------*/
26 #include "dbuf_string.h"
30 value *aggregateToPointer (value *val);
31 void printTypeChainRaw (sym_link * start, FILE * of);
33 void printFromToType(sym_link *from, sym_link *to) {
34 fprintf (stderr, "from type '");
35 printTypeChain (from, stderr);
36 fprintf (stderr, "'\nto type '");
37 printTypeChain (to, stderr);
38 fprintf (stderr, "'\n");
42 char *nounName(sym_link *sl) {
43 switch (SPEC_NOUN(sl))
46 if (SPEC_LONG(sl)) return "long";
47 if (SPEC_SHORT(sl)) return "short";
50 case V_FLOAT: return "float";
51 case V_FIXED16X16: return "fixed16x16";
52 case V_CHAR: return "char";
53 case V_VOID: return "void";
54 case V_STRUCT: return "struct";
55 case V_LABEL: return "label";
56 case V_BITFIELD: return "bitfield";
57 case V_BIT: return "bit";
58 case V_SBIT: return "sbit";
59 case V_DOUBLE: return "double";
64 bucket *SymbolTab[256]; /* the symbol table */
65 bucket *StructTab[256]; /* the structure table */
66 bucket *TypedefTab[256]; /* the typedef table */
67 bucket *LabelTab[256]; /* the Label table */
68 bucket *enumTab[256]; /* enumerated table */
70 /*------------------------------------------------------------------*/
71 /* initSymt () - initialises symbol table related stuff */
72 /*------------------------------------------------------------------*/
78 for (i = 0; i < 256; i++)
79 SymbolTab[i] = StructTab[i] = (void *) NULL;
83 /*-----------------------------------------------------------------*/
84 /* newBucket - allocates & returns a new bucket */
85 /*-----------------------------------------------------------------*/
91 bp = Safe_alloc ( sizeof (bucket));
96 /*-----------------------------------------------------------------*/
97 /* hashKey - computes the hashkey given a symbol name */
98 /*-----------------------------------------------------------------*/
100 hashKey (const char *s)
102 unsigned long key = 0;
109 /*-----------------------------------------------------------------*/
110 /* addSym - adds a symbol to the hash Table */
111 /*-----------------------------------------------------------------*/
113 addSym (bucket ** stab,
120 int i; /* index into the hash Table */
121 bucket *bp; /* temp bucket * */
124 symbol *csym = (symbol *)sym;
126 if (getenv("DEBUG_SANITY")) {
127 fprintf (stderr, "addSym: %s ", sname);
129 /* make sure the type is complete and sane */
130 checkTypeSanity(csym->etype, csym->name);
133 /* prevent overflow of the (r)name buffers */
134 if (strlen(sname)>SDCC_SYMNAME_MAX) {
135 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
136 sname[SDCC_SYMNAME_MAX]='\0';
139 /* the symbols are always added at the head of the list */
141 /* get a free entry */
142 bp = Safe_alloc ( sizeof (bucket));
144 bp->sym = sym; /* update the symbol pointer */
145 bp->level = level; /* update the nest level */
147 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
149 /* if this is the first entry */
152 bp->prev = bp->next = (void *) NULL; /* point to nothing */
155 /* not first entry then add @ head of list */
165 /*-----------------------------------------------------------------*/
166 /* deleteSym - deletes a symbol from the hash Table entry */
167 /*-----------------------------------------------------------------*/
169 deleteSym (bucket ** stab, void *sym, char *sname)
177 /* find the symbol */
180 if (bp->sym == sym) /* found it then break out */
181 break; /* of the loop */
185 if (!bp) /* did not find it */
187 /* if this is the first one in the chain */
191 if (stab[i]) /* if chain ! empty */
192 stab[i]->prev = (void *) NULL;
194 /* middle || end of chain */
197 if (bp->next) /* if not end of chain */
198 bp->next->prev = bp->prev;
200 bp->prev->next = bp->next;
205 /*-----------------------------------------------------------------*/
206 /* findSym - finds a symbol in a table */
207 /*-----------------------------------------------------------------*/
209 findSym (bucket ** stab, void *sym, const char *sname)
213 bp = stab[hashKey (sname)];
216 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
221 return (bp ? bp->sym : (void *) NULL);
224 /*-----------------------------------------------------------------*/
225 /* findSymWithLevel - finds a symbol with a name & level */
226 /*-----------------------------------------------------------------*/
228 findSymWithLevel (bucket ** stab, symbol * sym)
232 bp = stab[hashKey (sym->name)];
235 ** do the search from the head of the list since the
236 ** elements are added at the head it is ensured that
237 ** we will find the deeper definitions before we find
238 ** the global ones. we need to check for symbols with
239 ** level <= to the level given, if levels match then block
240 ** numbers need to match as well
244 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
246 /* if this is parameter then nothing else need to be checked */
247 if (((symbol *) (bp->sym))->_isparm)
249 /* if levels match then block numbers should also match */
250 if (bp->level && bp->level == sym->level && bp->block == sym->block)
252 /* if levels don't match then we are okay */
253 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
255 /* if this is a global variable then we are ok too */
263 return (void *) NULL;
266 /*-----------------------------------------------------------------*/
267 /* findSymWithBlock - finds a symbol with name in with a block */
268 /*-----------------------------------------------------------------*/
270 findSymWithBlock (bucket ** stab, symbol * sym, int block)
274 bp = stab[hashKey (sym->name)];
277 if (strcmp (bp->name, sym->name) == 0 &&
283 return (bp ? bp->sym : (void *) NULL);
286 /*------------------------------------------------------------------*/
287 /* newSymbol () - returns a new pointer to a symbol */
288 /*------------------------------------------------------------------*/
290 newSymbol (char *name, int scope)
294 sym = Safe_alloc ( sizeof (symbol));
296 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
297 sym->level = scope; /* set the level */
298 sym->block = currBlockno;
299 sym->lineDef = lineno; /* set the line number */
300 sym->fileDef = filename;
304 /*------------------------------------------------------------------*/
305 /* newLink - creates a new link (declarator,specifier) */
306 /*------------------------------------------------------------------*/
308 newLink (SYM_LINK_CLASS select)
312 p = Safe_alloc ( sizeof (sym_link));
318 /*------------------------------------------------------------------*/
319 /* newStruct - creats a new structdef from the free list */
320 /*------------------------------------------------------------------*/
322 newStruct (char *tag)
326 s = Safe_alloc ( sizeof (structdef));
328 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
332 /*------------------------------------------------------------------*/
333 /* sclsFromPtr - Return the storage class a pointer points into. */
334 /* S_FIXED is returned for generic pointers or other */
335 /* unexpected cases */
336 /*------------------------------------------------------------------*/
338 sclsFromPtr(sym_link *ptr)
340 switch (DCL_TYPE (ptr))
363 /*------------------------------------------------------------------*/
364 /* pointerTypes - do the computation for the pointer types */
365 /*------------------------------------------------------------------*/
367 pointerTypes (sym_link * ptr, sym_link * type)
372 /* find the first pointer type */
373 while (ptr && !IS_PTR (ptr))
376 /* could not find it */
377 if (!ptr || IS_SPEC (ptr))
380 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
381 pointerTypes (ptr->next, type);
385 /* change the pointer type depending on the
386 storage class of the type */
389 switch (SPEC_SCLS (type))
392 DCL_TYPE (ptr) = FPOINTER;
395 DCL_TYPE (ptr) = IPOINTER;
398 DCL_TYPE (ptr) = PPOINTER;
401 DCL_TYPE (ptr) = POINTER;
404 DCL_TYPE (ptr) = CPOINTER;
407 DCL_TYPE (ptr) = EEPPOINTER;
410 DCL_TYPE (ptr) = port->unqualified_pointer;
413 /* the storage class of type ends here */
414 SPEC_SCLS (type) = 0;
417 /* now change all the remaining unknown pointers
418 to generic pointers */
421 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
422 DCL_TYPE (ptr) = port->unqualified_pointer;
426 /* same for the type although it is highly unlikely that
427 type will have a pointer */
430 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
431 DCL_TYPE (type) = port->unqualified_pointer;
436 /*------------------------------------------------------------------*/
437 /* addDecl - adds a declarator @ the end of a chain */
438 /*------------------------------------------------------------------*/
440 addDecl (symbol * sym, int type, sym_link * p)
442 static sym_link *empty = NULL;
447 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
448 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
451 empty = newLink(SPECIFIER);
453 /* if we are passed a link then set head & tail */
462 head = tail = newLink (DECLARATOR);
463 DCL_TYPE (head) = type;
466 /* if this is the first entry */
472 else if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
474 sym->etype = mergeSpec (sym->etype, head, sym->name);
476 else if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
479 while (t->next != sym->etype)
482 tail->next = sym->etype;
484 else if (IS_FUNC (sym->type) && IS_SPEC (sym->type->next) &&
485 !memcmp(sym->type->next, empty, sizeof(sym_link)))
487 sym->type->next = head;
492 sym->etype->next = head;
496 /* if the type is an unknown pointer and has
497 a tspec then take the storage class const & volatile
498 attribute from the tspec & make it those of this
502 //DCL_TYPE (p) == UPOINTER &&
505 if (!IS_SPEC (sym->etype))
507 sym->etype = sym->etype->next = newLink (SPECIFIER);
509 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
510 DCL_TSPEC (p) = NULL;
513 // if there is a function in this type chain
514 if (p && funcInChain(sym->type)) {
515 processFuncArgs (sym);
521 /*------------------------------------------------------------------
522 checkTypeSanity: prevent the user from doing e.g.:
524 ------------------------------------------------------------------*/
525 void checkTypeSanity(sym_link *etype, char *name) {
529 if (getenv("DEBUG_SANITY")) {
530 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
535 if (!IS_SPEC(etype)) {
536 if (getenv("DEBUG_SANITY")) {
537 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
542 noun=nounName(etype);
544 if (getenv("DEBUG_SANITY")) {
545 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
548 if ((SPEC_NOUN(etype)==V_CHAR ||
549 SPEC_NOUN(etype)==V_FLOAT ||
550 SPEC_NOUN(etype)==V_FIXED16X16 ||
551 SPEC_NOUN(etype)==V_DOUBLE ||
552 SPEC_NOUN(etype)==V_VOID) &&
553 (SPEC_SHORT(etype) || SPEC_LONG(etype))) {
554 // long or short for char float double or void
555 werror (E_LONG_OR_SHORT_INVALID, noun, name);
557 if ((SPEC_NOUN(etype)==V_FLOAT ||
558 SPEC_NOUN(etype)==V_FIXED16X16 ||
559 SPEC_NOUN(etype)==V_DOUBLE ||
560 SPEC_NOUN(etype)==V_VOID) &&
561 (etype->select.s.b_signed || SPEC_USIGN(etype))) {
562 // signed or unsigned for float double or void
563 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
566 // special case for "short"
567 if (SPEC_SHORT(etype)) {
568 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
569 SPEC_SHORT(etype) = 0;
573 "const a;" or "data b;" or "signed s" or "long l"
575 if (!SPEC_NOUN(etype)) {
576 SPEC_NOUN(etype)=V_INT;
579 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
580 /* a "plain" int bitfield is unsigned */
581 if (SPEC_NOUN(etype)==V_BIT ||
582 SPEC_NOUN(etype)==V_SBIT) {
583 if (!etype->select.s.b_signed)
584 SPEC_USIGN(etype) = 1;
587 if (etype->select.s.b_signed && SPEC_USIGN(etype)) {
588 // signed AND unsigned
589 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
591 if (SPEC_SHORT(etype) && SPEC_LONG(etype)) {
593 werror (E_LONG_AND_SHORT_INVALID, noun, name);
598 /*------------------------------------------------------------------*/
599 /* mergeSpec - merges two specifiers and returns the new one */
600 /*------------------------------------------------------------------*/
602 mergeSpec (sym_link * dest, sym_link * src, char *name)
604 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
606 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
609 werror (E_SYNTAX_ERROR, yytext);
610 // the show must go on
615 if (SPEC_NOUN(src)) {
616 if (!SPEC_NOUN(dest)) {
617 SPEC_NOUN(dest)=SPEC_NOUN(src);
619 /* we shouldn't redeclare the type */
620 if (getenv("DEBUG_SANITY")) {
621 fprintf (stderr, "mergeSpec: ");
623 werror(E_TWO_OR_MORE_DATA_TYPES, name);
627 if (SPEC_SCLS(src)) {
628 /* if destination has no storage class */
629 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
630 SPEC_SCLS (dest) = SPEC_SCLS (src);
632 if (getenv("DEBUG_SANITY")) {
633 fprintf (stderr, "mergeSpec: ");
635 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
639 /* copy all the specifications */
641 // we really should do:
643 if (SPEC_what(src)) {
644 if (SPEC_what(dest)) {
645 werror(W_DUPLICATE_SPEC, "what");
647 SPEC_what(dst)|=SPEC_what(src);
650 // but there are more important thing right now
652 SPEC_LONG (dest) |= SPEC_LONG (src);
653 SPEC_SHORT(dest) |= SPEC_SHORT(src);
654 SPEC_USIGN (dest) |= SPEC_USIGN (src);
655 dest->select.s.b_signed|=src->select.s.b_signed;
656 SPEC_STAT (dest) |= SPEC_STAT (src);
657 SPEC_EXTR (dest) |= SPEC_EXTR (src);
658 SPEC_CONST(dest) |= SPEC_CONST (src);
659 SPEC_ABSA (dest) |= SPEC_ABSA (src);
660 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
661 SPEC_RESTRICT (dest) |= SPEC_RESTRICT (src);
662 SPEC_ADDR (dest) |= SPEC_ADDR (src);
663 SPEC_OCLS (dest) = SPEC_OCLS (src);
664 SPEC_BLEN (dest) |= SPEC_BLEN (src);
665 SPEC_BSTR (dest) |= SPEC_BSTR (src);
666 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
667 SPEC_ENUM (dest) |= SPEC_ENUM (src);
668 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
669 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
671 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
672 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
674 /* these are the only function attributes that will be set
675 in a specifier while parsing */
676 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
677 FUNC_BANKED(dest) |= FUNC_BANKED(src);
678 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
679 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
680 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
681 FUNC_ISISR(dest) |= FUNC_ISISR(src);
682 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
683 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
684 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
685 FUNC_INTNO(dest) |= FUNC_INTNO(src);
686 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
691 /*------------------------------------------------------------------*/
692 /* genSymName - generates and returns a name used for anonymous vars */
693 /*------------------------------------------------------------------*/
695 genSymName (int level)
697 static int gCount = 0;
698 static char gname[SDCC_NAME_MAX + 1];
700 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
704 /*------------------------------------------------------------------*/
705 /* getSpec - returns the specifier part from a declaration chain */
706 /*------------------------------------------------------------------*/
708 getSpec (sym_link * p)
713 while (p && !(IS_SPEC (p)))
719 /*------------------------------------------------------------------*/
720 /* newCharLink() - creates an char type */
721 /*------------------------------------------------------------------*/
727 p = newLink (SPECIFIER);
728 SPEC_NOUN (p) = V_CHAR;
733 /*------------------------------------------------------------------*/
734 /* newFloatLink - a new Float type */
735 /*------------------------------------------------------------------*/
741 p = newLink (SPECIFIER);
742 SPEC_NOUN (p) = V_FLOAT;
747 /*------------------------------------------------------------------*/
748 /* newFixed16x16Link - a new Float type */
749 /*------------------------------------------------------------------*/
755 p = newLink (SPECIFIER);
756 SPEC_NOUN (p) = V_FIXED16X16;
761 /*------------------------------------------------------------------*/
762 /* newLongLink() - new long type */
763 /*------------------------------------------------------------------*/
769 p = newLink (SPECIFIER);
770 SPEC_NOUN (p) = V_INT;
776 /*------------------------------------------------------------------*/
777 /* newIntLink() - creates an int type */
778 /*------------------------------------------------------------------*/
784 p = newLink (SPECIFIER);
785 SPEC_NOUN (p) = V_INT;
790 /*------------------------------------------------------------------*/
791 /* newBoolLink() - creates an bool type */
792 /*------------------------------------------------------------------*/
798 p = newLink (SPECIFIER);
799 SPEC_NOUN (p) = V_BIT;
804 /*------------------------------------------------------------------*/
805 /* getSize - returns size of a type chain in bytes */
806 /*------------------------------------------------------------------*/
808 getSize (sym_link * p)
810 /* if nothing return 0 */
814 { /* if this is the specifier then */
815 switch (SPEC_NOUN (p))
816 { /* depending on the specifier type */
818 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
828 return SPEC_STRUCT (p)->size;
835 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
841 /* this is a declarator */
842 switch (DCL_TYPE (p))
846 return DCL_ELEM (p) * getSize (p->next);
848 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
849 // "can not tell the size of an array[]");
860 return (IFFUNC_BANKED (p) ? GPTRSIZE : FPTRSIZE);
869 /*------------------------------------------------------------------*/
870 /* checkStructFlexArray - check tree behind a struct */
871 /*------------------------------------------------------------------*/
873 checkStructFlexArray (symbol *sym, sym_link *p)
875 /* if nothing return FALSE */
881 /* (nested) struct with flexible array member? */
882 if (IS_STRUCT (p) && SPEC_STRUCT (p)->b_flexArrayMember)
884 werror (W_INVALID_FLEXARRAY);
890 /* this is a declarator */
893 /* flexible array member? */
896 if (!options.std_c99)
897 werror (W_C89_NO_FLEXARRAY);
901 return checkStructFlexArray (sym, p->next);
906 /*------------------------------------------------------------------*/
907 /* bitsForType - returns # of bits required to store this type */
908 /*------------------------------------------------------------------*/
910 bitsForType (sym_link * p)
912 /* if nothing return 0 */
917 { /* if this is the specifier then */
919 switch (SPEC_NOUN (p))
920 { /* depending on the specifier type */
922 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
924 return FLOATSIZE * 8;
932 return SPEC_STRUCT (p)->size * 8;
939 return SPEC_BLEN (p);
945 /* this is a specifier */
946 switch (DCL_TYPE (p))
949 return DCL_ELEM (p) * getSize (p->next) * 8;
953 return (PTRSIZE * 8);
958 return (FPTRSIZE * 8);
960 return (GPTRSIZE * 8);
967 /*------------------------------------------------------------------*/
968 /* copySymbolChain - copies a symbol chain */
969 /*------------------------------------------------------------------*/
971 copySymbolChain (symbol * src)
978 dest = copySymbol (src);
979 dest->next = copySymbolChain (src->next);
983 /*------------------------------------------------------------------*/
984 /* copySymbol - makes a copy of a symbol */
985 /*------------------------------------------------------------------*/
987 copySymbol (symbol * src)
994 dest = newSymbol (src->name, src->level);
995 memcpy (dest, src, sizeof (symbol));
996 dest->level = src->level;
997 dest->block = src->block;
998 dest->ival = copyIlist (src->ival);
999 dest->type = copyLinkChain (src->type);
1000 dest->etype = getSpec (dest->type);
1002 dest->key = src->key;
1003 dest->allocreq = src->allocreq;
1007 /*------------------------------------------------------------------*/
1008 /* reverseSyms - reverses the links for a symbol chain */
1009 /*------------------------------------------------------------------*/
1011 reverseSyms (symbol * sym)
1013 symbol *prev, *curr, *next;
1028 sym->next = (void *) NULL;
1032 /*------------------------------------------------------------------*/
1033 /* reverseLink - reverses the links for a type chain */
1034 /*------------------------------------------------------------------*/
1036 reverseLink (sym_link * type)
1038 sym_link *prev, *curr, *next;
1053 type->next = (void *) NULL;
1057 /*------------------------------------------------------------------*/
1058 /* addSymChain - adds a symbol chain to the symboltable */
1059 /*------------------------------------------------------------------*/
1061 addSymChain (symbol ** symHead)
1063 symbol *sym = *symHead;
1064 symbol *csym = NULL;
1068 for (; sym != NULL; sym = sym->next)
1070 changePointer(sym->type);
1071 checkTypeSanity(sym->etype, sym->name);
1073 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1076 /* if already exists in the symbol table then check if
1077 one of them is an extern definition if yes then
1078 then check if the type match, if the types match then
1079 delete the current entry and add the new entry */
1080 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1081 csym->level == sym->level)
1083 /* if not in file scope then show symbol redefined error
1084 else check if symbols have conpatible types */
1089 /* If the previous definition was for an array with incomplete */
1090 /* type, and the new definition has completed the type, update */
1091 /* the original type to match */
1092 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1093 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1095 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1096 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1100 /* If only one of the definitions used the "at" keyword, copy */
1101 /* the address to the other. */
1102 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1103 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1105 SPEC_ABSA (sym->etype) = 1;
1106 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1108 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1109 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1111 SPEC_ABSA (csym->etype) = 1;
1112 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1117 if (csym->ival && sym->ival)
1119 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1125 /* one definition extern ? */
1126 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1127 werror (E_EXTERN_MISMATCH, sym->name);
1129 werror (E_DUPLICATE, sym->name);
1130 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1132 fprintf (stderr, "from type '");
1133 printTypeChain (csym->type, stderr);
1134 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1135 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1136 fprintf (stderr, "'\nto type '");
1137 printTypeChain (sym->type, stderr);
1138 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1139 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1140 fprintf (stderr, "'\n");
1145 if (csym->ival && !sym->ival)
1146 sym->ival = csym->ival;
1148 /* delete current entry */
1149 deleteSym (SymbolTab, csym, csym->name);
1150 deleteFromSeg(csym);
1152 symPtrPtr = symHead;
1153 while (*symPtrPtr && *symPtrPtr != csym)
1154 symPtrPtr = &(*symPtrPtr)->next;
1155 if (*symPtrPtr == csym)
1156 *symPtrPtr = csym->next;
1160 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1165 /*------------------------------------------------------------------*/
1166 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1167 /*------------------------------------------------------------------*/
1169 funcInChain (sym_link * lnk)
1180 /*------------------------------------------------------------------*/
1181 /* structElemType - returns the type info of a struct member */
1182 /*------------------------------------------------------------------*/
1184 structElemType (sym_link * stype, value * id)
1186 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1187 sym_link *type, *etype;
1188 sym_link *petype = getSpec (stype);
1192 /* look for the id */
1195 if (strcmp (fields->rname, id->name) == 0)
1197 type = copyLinkChain (fields->type);
1198 etype = getSpec (type);
1199 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1200 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1202 SPEC_CONST (type) |= SPEC_CONST (stype);
1204 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1207 fields = fields->next;
1211 werror (E_NOT_MEMBER, id->name);
1213 // the show must go on
1214 return newIntLink();
1217 /*------------------------------------------------------------------*/
1218 /* getStructElement - returns element of a tructure definition */
1219 /*------------------------------------------------------------------*/
1221 getStructElement (structdef * sdef, symbol * sym)
1225 for (field = sdef->fields; field; field = field->next)
1226 if (strcmp (field->name, sym->name) == 0)
1229 werror (E_NOT_MEMBER, sym->name);
1231 return sdef->fields;
1234 /*------------------------------------------------------------------*/
1235 /* compStructSize - computes the size of a structure */
1236 /*------------------------------------------------------------------*/
1238 compStructSize (int su, structdef * sdef)
1240 int sum = 0, usum = 0;
1244 /* for the identifiers */
1245 loop = sdef->fields;
1248 /* create the internal name for this variable */
1249 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1254 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1256 /* if this is a bit field */
1259 /* change it to a unsigned bit */
1260 SPEC_NOUN (loop->etype) = V_BITFIELD;
1261 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1262 /* a "plain" int bitfield is unsigned */
1263 if (!loop->etype->select.s.b_signed)
1264 SPEC_USIGN(loop->etype) = 1;
1266 SPEC_BLEN (loop->etype) = loop->bitVar;
1268 if (loop->bitVar == BITVAR_PAD) {
1269 /* A zero length bitfield forces padding */
1270 SPEC_BSTR (loop->etype) = bitOffset;
1271 SPEC_BLEN (loop->etype) = 0;
1276 if (bitOffset == 8) {
1280 /* check if this fit into the remaining */
1281 /* bits of this byte else align it to the */
1282 /* next byte boundary */
1283 if (loop->bitVar <= (8 - bitOffset)) {
1284 /* fits into current byte */
1286 SPEC_BSTR (loop->etype) = bitOffset;
1287 bitOffset += loop->bitVar;
1289 else if (!bitOffset) {
1290 /* does not fit, but is already byte aligned */
1292 SPEC_BSTR (loop->etype) = bitOffset;
1293 bitOffset += loop->bitVar;
1296 if( TARGET_IS_PIC16 && getenv("PIC16_PACKED_BITFIELDS") ) {
1297 /* if PIC16 && enviroment variable is set, then
1298 * tightly pack bitfields, this means that when a
1299 * bitfield goes beyond byte alignment, do not
1300 * automatically start allocatint from next byte,
1301 * but also use the available bits first */
1302 fprintf(stderr, ": packing bitfields in structures\n");
1303 SPEC_BSTR (loop->etype) = bitOffset;
1304 bitOffset += loop->bitVar;
1305 loop->offset = (su == UNION ? sum = 0 : sum);
1307 /* does not fit; need to realign first */
1309 loop->offset = (su == UNION ? sum = 0 : sum);
1311 SPEC_BSTR (loop->etype) = bitOffset;
1312 bitOffset += loop->bitVar;
1315 while (bitOffset>8) {
1322 /* This is a non-bit field. Make sure we are */
1323 /* byte aligned first */
1326 loop->offset = (su == UNION ? sum = 0 : sum);
1330 checkDecl (loop, 1);
1331 sum += getSize (loop->type);
1333 /* search for "flexibel array members" */
1334 /* and do some syntax checks */
1336 && checkStructFlexArray (loop, loop->type))
1338 /* found a "flexible array member" */
1339 sdef->b_flexArrayMember = TRUE;
1340 /* is another struct-member following? */
1342 werror (E_FLEXARRAY_NOTATEND);
1343 /* is it the first struct-member? */
1344 else if (loop == sdef->fields)
1345 werror (E_FLEXARRAY_INEMPTYSTRCT);
1351 /* if union then size = sizeof largest field */
1353 /* For UNION, round up after each field */
1354 sum += ((bitOffset+7)/8);
1355 usum = max (usum, sum);
1360 /* For STRUCT, round up after all fields processed */
1362 sum += ((bitOffset+7)/8);
1364 return (su == UNION ? usum : sum);
1367 /*-------------------------------------------------------------------*/
1368 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1369 /* an enclosing struct/union */
1370 /*-------------------------------------------------------------------*/
1372 promoteAnonStructs (int su, structdef * sdef)
1381 tofield = &sdef->fields;
1382 field = sdef->fields;
1385 nextfield = field->next;
1386 if (!*field->name && IS_STRUCT (field->type))
1388 /* Found an anonymous struct/union. Replace it */
1389 /* with the fields it contains and adjust all */
1392 base = field->offset;
1393 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1395 continue; /* just in case it's empty */
1397 *tofield = subfield;
1400 /* check for field name conflicts resulting from promotion */
1401 dupfield = sdef->fields;
1402 while (dupfield && dupfield != subfield)
1404 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1406 werrorfl (subfield->fileDef, subfield->lineDef,
1408 su==STRUCT ? "struct" : "union",
1410 werrorfl (dupfield->fileDef, dupfield->lineDef,
1413 dupfield = dupfield->next;
1416 subfield->offset += base;
1418 subfield = subfield->next;
1422 subfield->next = nextfield;
1423 tofield = &subfield->next;
1426 tofield = &field->next;
1432 /*------------------------------------------------------------------*/
1433 /* checkSClass - check the storage class specification */
1434 /*------------------------------------------------------------------*/
1436 checkSClass (symbol * sym, int isProto)
1440 if (getenv("DEBUG_SANITY")) {
1441 fprintf (stderr, "checkSClass: %s \n", sym->name);
1444 /* type is literal can happen for enums change to auto */
1445 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1446 SPEC_SCLS (sym->etype) = S_AUTO;
1448 /* if sfr or sbit then must also be volatile */
1449 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1450 SPEC_SCLS (sym->etype) == S_SFR)
1452 SPEC_VOLATILE (sym->etype) = 1;
1455 /* make sure restrict is only used with pointers */
1456 if (SPEC_RESTRICT (sym->etype))
1458 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1459 SPEC_RESTRICT (sym->etype) = 0;
1464 if (IS_DECL (t) && DCL_PTR_RESTRICT (t) && !IS_PTR (t))
1466 werrorfl (sym->fileDef, sym->lineDef, E_BAD_RESTRICT);
1467 DCL_PTR_RESTRICT (t) = 0;
1473 /* if absolute address given then it mark it as
1474 volatile -- except in the PIC port */
1476 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1477 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1478 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1481 if (IS_ABSOLUTE (sym->etype))
1482 SPEC_VOLATILE (sym->etype) = 1;
1484 if (TARGET_IS_MCS51 &&
1485 IS_ABSOLUTE (sym->etype) &&
1486 SPEC_SCLS (sym->etype) == S_SFR)
1491 if (SPEC_NOUN (sym->etype) == V_CHAR)
1493 else if (SPEC_LONG (sym->etype) == 0)
1498 addr = SPEC_ADDR (sym->etype);
1499 for (n=0; n<size; n+=8)
1500 if (((addr >> n) & 0xFF) < 0x80)
1501 werror (W_SFR_ABSRANGE, sym->name);
1504 /* If code memory is read only, then pointers to code memory */
1505 /* implicitly point to constants -- make this explicit */
1507 while (t && t->next) {
1508 if (IS_CODEPTR(t) && port->mem.code_ro) {
1509 if (IS_SPEC(t->next)) {
1510 SPEC_CONST (t->next) = 1;
1512 DCL_PTR_CONST (t->next) = 1;
1518 /* global variables declared const put into code */
1519 /* if no other storage class specified */
1520 if (sym->level == 0 &&
1521 SPEC_SCLS(sym->etype) == S_FIXED &&
1522 !IS_FUNC(sym->type)) {
1523 /* find the first non-array link */
1527 if (IS_CONSTANT (t)) {
1528 SPEC_SCLS (sym->etype) = S_CODE;
1532 /* global variable in code space is a constant */
1533 if (sym->level == 0 &&
1534 SPEC_SCLS (sym->etype) == S_CODE &&
1535 port->mem.code_ro) {
1536 /* find the first non-array link */
1543 DCL_PTR_CONST (t) = 1;
1547 /* if bit variable then no storage class can be */
1548 /* specified since bit is already a storage */
1549 if (IS_BITVAR (sym->etype) &&
1550 (SPEC_SCLS (sym->etype) != S_FIXED &&
1551 SPEC_SCLS (sym->etype) != S_SBIT &&
1552 SPEC_SCLS (sym->etype) != S_BIT)
1555 werror (E_BITVAR_STORAGE, sym->name);
1556 SPEC_SCLS (sym->etype) = S_FIXED;
1559 /* extern variables cannot be initialized */
1560 if (IS_EXTERN (sym->etype) && sym->ival)
1562 werror (E_EXTERN_INIT, sym->name);
1566 /* if this is an automatic symbol */
1567 if (sym->level && (options.stackAuto || reentrant)) {
1568 if (SPEC_SCLS (sym->etype) != S_BIT) {
1569 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1570 SPEC_SCLS (sym->etype) == S_FIXED ||
1571 SPEC_SCLS (sym->etype) == S_REGISTER ||
1572 SPEC_SCLS (sym->etype) == S_STACK ||
1573 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1574 SPEC_SCLS (sym->etype) = S_AUTO;
1576 /* storage class may only be specified for statics */
1577 if (!IS_STATIC(sym->etype)) {
1578 werror (E_AUTO_ASSUMED, sym->name);
1584 /* automatic symbols cannot be given */
1585 /* an absolute address ignore it */
1587 SPEC_ABSA (sym->etype) &&
1588 (options.stackAuto || reentrant))
1590 werror (E_AUTO_ABSA, sym->name);
1591 SPEC_ABSA (sym->etype) = 0;
1594 /* arrays & pointers cannot be defined for bits */
1595 /* SBITS or SFRs or BIT */
1596 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1597 !IS_FUNCPTR (sym->type) &&
1598 (SPEC_NOUN (sym->etype) == V_BIT ||
1599 SPEC_NOUN (sym->etype) == V_SBIT ||
1600 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1601 SPEC_SCLS (sym->etype) == S_SFR))
1602 werror (E_BIT_ARRAY, sym->name);
1604 /* if this is a bit|sbit then set length & start */
1605 if (SPEC_NOUN (sym->etype) == V_BIT ||
1606 SPEC_NOUN (sym->etype) == V_SBIT)
1608 SPEC_BLEN (sym->etype) = 1;
1609 SPEC_BSTR (sym->etype) = 0;
1613 /* variables declared in CODE space must have */
1614 /* initializers if not an extern */
1615 if (SPEC_SCLS (sym->etype) == S_CODE &&
1616 sym->ival == NULL &&
1619 port->mem.code_ro &&
1620 !IS_EXTERN (sym->etype) &&
1621 !SPEC_ABSA (sym->etype) &&
1622 !funcInChain (sym->type))
1623 werror (E_CODE_NO_INIT, sym->name);
1626 /* if parameter or local variable then change */
1627 /* the storage class to reflect where the var will go */
1628 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1629 && !IS_STATIC(sym->etype)
1632 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1634 SPEC_SCLS (sym->etype) = (options.useXstack ?
1635 S_XSTACK : S_STACK);
1639 /* hack-o-matic! I see no reason why the useXstack option should ever
1640 * control this allocation, but the code was originally that way, and
1641 * changing it for non-390 ports breaks the compiler badly.
1643 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1644 1 : options.useXstack;
1645 SPEC_SCLS (sym->etype) = (useXdata ?
1651 /*------------------------------------------------------------------*/
1652 /* changePointer - change pointer to functions */
1653 /*------------------------------------------------------------------*/
1655 changePointer (sym_link * p)
1658 /* go thru the chain of declarations */
1659 /* if we find a pointer to a function */
1660 /* change it to a ptr to code area */
1661 /* unless the function is banked. */
1662 for (; p; p = p->next)
1664 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1665 DCL_TYPE (p) = port->unqualified_pointer;
1666 if (IS_PTR (p) && IS_FUNC (p->next))
1667 if (!IFFUNC_BANKED(p->next))
1668 DCL_TYPE (p) = CPOINTER;
1672 /*------------------------------------------------------------------*/
1673 /* checkDecl - does semantic validation of a declaration */
1674 /*------------------------------------------------------------------*/
1676 checkDecl (symbol * sym, int isProto)
1679 checkSClass (sym, isProto); /* check the storage class */
1680 changePointer (sym->type); /* change pointers if required */
1682 /* if this is an array without any dimension
1683 then update the dimension from the initial value */
1684 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1685 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1690 /*------------------------------------------------------------------*/
1691 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1692 /*------------------------------------------------------------------*/
1694 copyLinkChain (sym_link * p)
1696 sym_link *head, *curr, *loop;
1698 /* note: v_struct and v_struct->fields are not copied! */
1700 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1703 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1704 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1712 /*------------------------------------------------------------------*/
1713 /* cleanUpBlock - cleansup the symbol table specified for all the */
1714 /* symbols in the given block */
1715 /*------------------------------------------------------------------*/
1717 cleanUpBlock (bucket ** table, int block)
1722 /* go thru the entire table */
1723 for (i = 0; i < 256; i++)
1725 for (chain = table[i]; chain; chain = chain->next)
1727 if (chain->block >= block)
1729 deleteSym (table, chain->sym, chain->name);
1735 /*------------------------------------------------------------------*/
1736 /* cleanUpLevel - cleansup the symbol table specified for all the */
1737 /* symbols in the given level */
1738 /*------------------------------------------------------------------*/
1740 cleanUpLevel (bucket ** table, int level)
1745 /* go thru the entire table */
1746 for (i = 0; i < 256; i++)
1748 for (chain = table[i]; chain; chain = chain->next)
1750 if (chain->level >= level)
1752 deleteSym (table, chain->sym, chain->name);
1758 /*------------------------------------------------------------------*/
1759 /* computeTypeOr - computes the resultant type from two types */
1760 /*------------------------------------------------------------------*/
1762 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1765 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1766 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1768 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1770 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1774 if (SPEC_USIGN (etype1))
1776 if ( IS_LITERAL (etype2)
1777 && floatFromVal (valFromType (etype2)) >= 0)
1778 SPEC_USIGN (reType) = 1;
1781 /* promote to int */
1782 SPEC_USIGN (reType) = 0;
1783 SPEC_NOUN (reType) = V_INT;
1786 else /* etype1 signed */
1788 if ( IS_LITERAL (etype2)
1789 && floatFromVal (valFromType (etype2)) <= 127)
1790 SPEC_USIGN (reType) = 0;
1793 /* promote to int */
1794 SPEC_USIGN (reType) = 0;
1795 SPEC_NOUN (reType) = V_INT;
1799 if (SPEC_USIGN (etype2))
1801 if ( IS_LITERAL (etype1)
1802 && floatFromVal (valFromType (etype1)) >= 0)
1803 SPEC_USIGN (reType) = 1;
1806 /* promote to int */
1807 SPEC_USIGN (reType) = 0;
1808 SPEC_NOUN (reType) = V_INT;
1811 else /* etype2 signed */
1813 if ( IS_LITERAL (etype1)
1814 && floatFromVal (valFromType (etype1)) <= 127)
1815 SPEC_USIGN (reType) = 0;
1818 /* promote to int */
1819 SPEC_USIGN (reType) = 0;
1820 SPEC_NOUN (reType) = V_INT;
1826 /*------------------------------------------------------------------*/
1827 /* computeType - computes the resultant type from two types */
1828 /*------------------------------------------------------------------*/
1830 computeType (sym_link * type1, sym_link * type2,
1831 RESULT_TYPE resultType, int op)
1835 sym_link *etype1 = getSpec (type1);
1838 etype2 = type2 ? getSpec (type2) : type1;
1840 /* if one of them is a float then result is a float */
1841 /* here we assume that the types passed are okay */
1842 /* and can be cast to one another */
1843 /* which ever is greater in size */
1844 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1845 rType = newFloatLink ();
1846 /* if both are fixed16x16 then result is float */
1847 else if (IS_FIXED16X16(etype1) && IS_FIXED16X16(etype2))
1848 rType = newFixed16x16Link();
1849 else if (IS_FIXED16X16(etype1) && IS_FLOAT (etype2))
1850 rType = newFloatLink ();
1851 else if (IS_FLOAT (etype1) && IS_FIXED16X16 (etype2) )
1852 rType = newFloatLink ();
1854 /* if both are bitvars choose the larger one */
1855 else if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1856 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1857 copyLinkChain (type1) : copyLinkChain (type1);
1859 /* if only one of them is a bit variable then the other one prevails */
1860 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1862 rType = copyLinkChain (type2);
1863 /* bitfield can have up to 16 bits */
1864 if (getSize (etype1) > 1)
1865 SPEC_NOUN (getSpec (rType)) = V_INT;
1867 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1869 rType = copyLinkChain (type1);
1870 /* bitfield can have up to 16 bits */
1871 if (getSize (etype2) > 1)
1872 SPEC_NOUN (getSpec (rType)) = V_INT;
1874 /* if one of them is a pointer or array then that
1876 else if (IS_PTR (type1) || IS_ARRAY (type1))
1877 rType = copyLinkChain (type1);
1878 else if (IS_PTR (type2) || IS_ARRAY (type2))
1879 rType = copyLinkChain (type2);
1880 else if (getSize (type1) > getSize (type2))
1881 rType = copyLinkChain (type1);
1883 rType = copyLinkChain (type2);
1885 reType = getSpec (rType);
1887 /* avoid conflicting types */
1888 reType->select.s.b_signed = 0;
1890 /* if result is a literal then make not so */
1891 if (IS_LITERAL (reType))
1892 SPEC_SCLS (reType) = S_REGISTER;
1896 case RESULT_TYPE_CHAR:
1897 if (IS_BITVAR (reType))
1899 SPEC_NOUN (reType) = V_CHAR;
1900 SPEC_SCLS (reType) = 0;
1901 SPEC_USIGN (reType) = 0;
1905 case RESULT_TYPE_INT:
1906 case RESULT_TYPE_NONE:
1907 case RESULT_TYPE_OTHER:
1908 if (IS_BIT (reType))
1910 SPEC_NOUN (reType) = V_CHAR;
1911 SPEC_SCLS (reType) = 0;
1912 SPEC_USIGN (reType) = 0;
1915 else if (IS_BITFIELD (reType))
1917 /* could be smarter, but it depends on the op */
1918 /* this is for the worst case: a multiplication of 4 * 4 bit */
1919 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1920 SPEC_SCLS (reType) = 0;
1921 SPEC_USIGN (reType) = 0;
1924 else if (IS_CHAR (reType))
1926 /* promotion of some special cases */
1931 return computeTypeOr (etype1, etype2, reType);
1933 if (SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1935 SPEC_USIGN (reType) = 1;
1940 SPEC_NOUN (reType) = V_INT;
1941 SPEC_USIGN (reType) = 0;
1944 /* if both are unsigned char then no promotion required */
1945 if (!(SPEC_USIGN (etype1) && SPEC_USIGN (etype2)))
1947 SPEC_NOUN (reType) = V_INT;
1948 SPEC_USIGN (reType) = 0;
1961 /* SDCC's sign promotion:
1962 - if one or both operands are unsigned, the resultant type will be unsigned
1963 (except char, see below)
1964 - if an operand is promoted to a larger type (char -> int, int -> long),
1965 the larger type will be signed
1967 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1968 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1969 the standard. The standard demands, that the result has to be the same
1970 "as if" the promotion would have been performed:
1972 - if the result of an operation with two char's is promoted to a
1973 larger type, the result will be signed.
1975 More sophisticated are these:
1976 - if the result of an operation with two char's is a char again,
1977 the result will only then be unsigned, if both operands are
1978 unsigned. In all other cases the result will be signed.
1980 This seems to be contradictionary to the first two rules, but it makes
1981 real sense (all types are char's):
1983 A signed char can be negative; this must be preserved in the result
1986 Only if both operands are unsigned it's safe to make the result
1987 unsigned; this helps to avoid overflow:
1990 - ToDo: document '|', '^' and '&'
1992 Homework: - why is (200 * 200 < 0) true?
1993 - why is { char l = 200, r = 200; (r * l > 0) } true?
1996 if (!IS_FLOAT (reType)
1997 && ( (SPEC_USIGN (etype1)
1998 /* if this operand is promoted to a larger type,
1999 then it will be promoted to a signed type */
2000 && !(bitsForType (etype1) < bitsForType (reType))
2001 /* char require special handling */
2002 && !IS_CHAR (etype1))
2003 || /* same for 2nd operand */
2004 (SPEC_USIGN (etype2)
2005 && !(bitsForType (etype2) < bitsForType (reType))
2006 && !IS_CHAR (etype2))
2007 || /* if both are 'unsigned char' and not promoted
2008 let the result be unsigned too */
2009 ( SPEC_USIGN (etype1)
2010 && SPEC_USIGN (etype2)
2013 && IS_CHAR (reType))))
2014 SPEC_USIGN (reType) = 1;
2016 SPEC_USIGN (reType) = 0;
2021 /*--------------------------------------------------------------------*/
2022 /* compareType - will do type check return 1 if match, -1 if castable */
2023 /*--------------------------------------------------------------------*/
2025 compareType (sym_link * dest, sym_link * src)
2036 /* if dest is a declarator then */
2041 /* banked function pointer */
2042 if (IS_GENPTR (dest) && IS_GENPTR (src))
2044 if (IS_FUNC (src->next) && IS_VOID(dest->next))
2046 if (IS_FUNC (dest->next) && IS_VOID(src->next))
2048 return compareType (dest->next, src->next);
2051 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2053 //checkFunction(src,dest);
2055 return compareType (dest->next, src->next);
2057 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
2061 (IS_GENPTR (dest) ||
2062 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
2065 if (IS_PTR (dest) && IS_ARRAY (src)) {
2066 value *val=aggregateToPointer (valFromType(src));
2067 int res=compareType (dest, val->type);
2068 Safe_free(val->type);
2072 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
2073 return compareType (dest->next, src);
2076 else if (IS_PTR (dest) && IS_INTEGRAL (src))
2082 /* if one is a specifier and the other is not */
2083 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2084 (IS_SPEC (dest) && !IS_SPEC (src)))
2087 /* if one of them is a void then ok */
2088 if (SPEC_NOUN (dest) == V_VOID &&
2089 SPEC_NOUN (src) != V_VOID)
2092 if (SPEC_NOUN (dest) != V_VOID &&
2093 SPEC_NOUN (src) == V_VOID)
2096 /* if they are both bitfields then if the lengths
2097 and starts don't match */
2098 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2099 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2100 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2103 /* it is a specifier */
2104 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2106 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
2107 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
2109 bitsForType (dest) == bitsForType (src))
2110 instead of the next two lines, but the regression tests fail with
2111 them; I guess it's a problem with replaceCheaperOp */
2112 getSize (dest) == getSize (src) &&
2113 !(!IS_BIT (dest) && IS_BIT (src)))
2115 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
2120 else if (IS_STRUCT (dest))
2122 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2127 if (SPEC_LONG (dest) != SPEC_LONG (src))
2130 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2136 /*--------------------------------------------------------------------*/
2137 /* compareTypeExact - will do type check return 1 if match exactly */
2138 /*--------------------------------------------------------------------*/
2140 compareTypeExact (sym_link * dest, sym_link * src, int level)
2142 STORAGE_CLASS srcScls, destScls;
2153 /* if dest is a declarator then */
2158 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2159 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2161 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2163 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2167 value *exargs, *acargs, *checkValue;
2169 /* verify function return type */
2170 if (!compareTypeExact (dest->next, src->next, -1))
2172 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2174 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2176 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2179 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2183 /* compare expected args with actual args */
2184 exargs = FUNC_ARGS(dest);
2185 acargs = FUNC_ARGS(src);
2187 /* for all the expected args do */
2188 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2190 //checkTypeSanity(acargs->etype, acargs->name);
2192 if (IS_AGGREGATE (acargs->type))
2194 checkValue = copyValue (acargs);
2195 aggregateToPointer (checkValue);
2198 checkValue = acargs;
2201 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2206 /* if one them ended we have a problem */
2207 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2208 (!exargs && acargs && !IS_VOID (acargs->type)))
2212 return compareTypeExact (dest->next, src->next, level);
2219 /* if one is a specifier and the other is not */
2220 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2221 (IS_SPEC (dest) && !IS_SPEC (src)))
2224 /* if one of them is a void then ok */
2225 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2228 /* if they are both bitfields then if the lengths
2229 and starts don't match */
2230 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2231 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2232 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2235 if (IS_INTEGRAL (dest))
2237 /* signedness must match */
2238 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2240 /* size must match */
2241 if (SPEC_LONG (dest) != SPEC_LONG (src))
2243 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2247 if (IS_STRUCT (dest))
2249 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2253 if (SPEC_CONST (dest) != SPEC_CONST (src))
2255 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2257 if (SPEC_STAT (dest) != SPEC_STAT (src))
2259 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2261 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2264 destScls = SPEC_SCLS (dest);
2265 srcScls = SPEC_SCLS (src);
2267 /* Compensate for const to const code change in checkSClass() */
2268 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2270 if (srcScls == S_CODE && destScls == S_FIXED)
2272 if (destScls == S_CODE && srcScls == S_FIXED)
2276 /* compensate for allocGlobal() */
2277 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2278 && port->mem.default_globl_map == xdata
2282 if (level>0 && !SPEC_STAT (dest))
2284 /* Compensate for hack-o-matic in checkSClass() */
2285 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2287 if (destScls == S_FIXED)
2288 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2289 if (srcScls == S_FIXED)
2290 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2292 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2294 if (destScls == S_FIXED)
2296 if (srcScls == S_FIXED)
2301 if (srcScls != destScls)
2304 printf ("level = %d\n", level);
2305 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2306 SPEC_SCLS (src), SPEC_SCLS (dest));
2307 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2315 /*------------------------------------------------------------------*/
2316 /* inCalleeSaveList - return 1 if found in callee save list */
2317 /*------------------------------------------------------------------*/
2319 calleeCmp(void *p1, void *p2)
2321 return (strcmp((char *)p1, (char *)(p2)) == 0);
2325 inCalleeSaveList(char *s)
2327 if (options.all_callee_saves)
2329 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2332 /*-----------------------------------------------------------------*/
2333 /* aggregateToPointer: change an agggregate type function */
2334 /* argument to a pointer to that type. */
2335 /*-----------------------------------------------------------------*/
2337 aggregateToPointer (value * val)
2339 if (IS_AGGREGATE (val->type))
2341 /* if this is a structure */
2342 /* then we need to add a new link */
2343 if (IS_STRUCT (val->type))
2345 /* first lets add DECLARATOR type */
2346 sym_link *p = val->type;
2348 werror (W_STRUCT_AS_ARG, val->name);
2349 val->type = newLink (DECLARATOR);
2350 val->type->next = p;
2353 /* change to a pointer depending on the */
2354 /* storage class specified */
2355 switch (SPEC_SCLS (val->etype))
2358 DCL_TYPE (val->type) = IPOINTER;
2361 DCL_TYPE (val->type) = PPOINTER;
2364 if (SPEC_OCLS(val->etype)) {
2365 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2367 // this happens for (external) function parameters
2368 DCL_TYPE (val->type) = port->unqualified_pointer;
2374 DCL_TYPE (val->type) = POINTER;
2377 DCL_TYPE (val->type) = CPOINTER;
2380 DCL_TYPE (val->type) = FPOINTER;
2383 DCL_TYPE (val->type) = EEPPOINTER;
2386 DCL_TYPE (val->type) = port->unqualified_pointer;
2389 /* is there is a symbol associated then */
2390 /* change the type of the symbol as well */
2393 val->sym->type = copyLinkChain (val->type);
2394 val->sym->etype = getSpec (val->sym->type);
2399 /*------------------------------------------------------------------*/
2400 /* checkFunction - does all kinds of check on a function */
2401 /*------------------------------------------------------------------*/
2403 checkFunction (symbol * sym, symbol *csym)
2405 value *exargs, *acargs;
2409 if (getenv("DEBUG_SANITY")) {
2410 fprintf (stderr, "checkFunction: %s ", sym->name);
2413 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2415 werror(E_SYNTAX_ERROR, sym->name);
2419 /* make sure the type is complete and sane */
2420 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2422 /* if not type then some kind of error */
2426 /* if the function has no type then make it return int */
2427 if (!sym->type->next)
2428 sym->type->next = sym->etype = newIntLink ();
2430 /* function cannot return aggregate */
2431 if (IS_AGGREGATE (sym->type->next))
2433 werror (E_FUNC_AGGR, sym->name);
2437 /* check if this function is defined as calleeSaves
2438 then mark it as such */
2439 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2441 /* if interrupt service routine */
2442 /* then it cannot have arguments */
2443 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2445 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2446 werror (E_INT_ARGS, sym->name);
2447 FUNC_ARGS(sym->type)=NULL;
2451 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2453 werror (E_SHADOWREGS_NO_ISR, sym->name);
2457 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2459 acargs=acargs->next, argCnt++) {
2461 // this can happen for reentrant functions
2462 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2463 // the show must go on: synthesize a name and symbol
2464 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2465 acargs->sym = newSymbol (acargs->name, 1);
2466 SPEC_OCLS (acargs->etype) = istack;
2467 acargs->sym->type = copyLinkChain (acargs->type);
2468 acargs->sym->etype = getSpec (acargs->sym->type);
2469 acargs->sym->_isparm = 1;
2470 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2471 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2473 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2478 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2479 return 1; /* not defined nothing more to check */
2481 /* check if body already present */
2482 if (csym && IFFUNC_HASBODY(csym->type))
2484 werror (E_FUNC_BODY, sym->name);
2488 /* check the return value type */
2489 if (compareType (csym->type, sym->type) <= 0)
2491 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2492 printFromToType(csym->type, sym->type);
2496 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2498 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2501 /* I don't think this is necessary for interrupts. An isr is a */
2502 /* root in the calling tree. */
2503 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2504 (!FUNC_ISISR (sym->type)))
2506 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2509 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2511 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2514 /* Really, reentrant should match regardless of argCnt, but */
2515 /* this breaks some existing code (the fp lib functions). If */
2516 /* the first argument is always passed the same way, this */
2517 /* lax checking is ok (but may not be true for in future ports) */
2518 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2521 //printf("argCnt = %d\n",argCnt);
2522 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2525 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2527 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2530 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2532 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2536 /* compare expected args with actual args */
2537 exargs = FUNC_ARGS(csym->type);
2538 acargs = FUNC_ARGS(sym->type);
2540 /* for all the expected args do */
2543 exargs = exargs->next, acargs = acargs->next, argCnt++)
2545 if (getenv("DEBUG_SANITY")) {
2546 fprintf (stderr, "checkFunction: %s ", exargs->name);
2548 /* make sure the type is complete and sane */
2549 checkTypeSanity(exargs->etype, exargs->name);
2551 /* If the actual argument is an array, any prototype
2552 * will have modified it to a pointer. Duplicate that
2555 if (IS_AGGREGATE (acargs->type))
2557 checkValue = copyValue (acargs);
2558 aggregateToPointer (checkValue);
2562 checkValue = acargs;
2565 if (compareType (exargs->type, checkValue->type) <= 0)
2567 werror (E_ARG_TYPE, argCnt);
2568 printFromToType(exargs->type, checkValue->type);
2573 /* if one them ended we have a problem */
2574 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2575 (!exargs && acargs && !IS_VOID (acargs->type)))
2576 werror (E_ARG_COUNT);
2578 /* replace with this defition */
2579 sym->cdef = csym->cdef;
2580 deleteSym (SymbolTab, csym, csym->name);
2581 deleteFromSeg(csym);
2582 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2583 if (IS_EXTERN (csym->etype) && !
2584 IS_EXTERN (sym->etype))
2586 addSet (&publics, sym);
2591 /*------------------------------------------------------------------*/
2592 /* cdbStructBlock - calls struct printing for a blcks */
2593 /*------------------------------------------------------------------*/
2594 void cdbStructBlock (int block)
2597 bucket **table = StructTab;
2600 /* go thru the entire table */
2601 for (i = 0; i < 256; i++)
2603 for (chain = table[i]; chain; chain = chain->next)
2605 if (chain->block >= block)
2608 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2614 /*-----------------------------------------------------------------*/
2615 /* processFuncPtrArgs - does some processing with args of func ptrs*/
2616 /*-----------------------------------------------------------------*/
2618 processFuncPtrArgs (sym_link * funcType)
2620 value *val = FUNC_ARGS(funcType);
2622 /* if it is void then remove parameters */
2623 if (val && IS_VOID (val->type))
2625 FUNC_ARGS(funcType) = NULL;
2630 /*-----------------------------------------------------------------*/
2631 /* processFuncArgs - does some processing with function args */
2632 /*-----------------------------------------------------------------*/
2634 processFuncArgs (symbol * func)
2638 sym_link *funcType=func->type;
2640 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2641 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2643 /* find the function declaration within the type */
2644 while (funcType && !IS_FUNC(funcType))
2645 funcType=funcType->next;
2647 /* if this function has variable argument list */
2648 /* then make the function a reentrant one */
2649 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2650 FUNC_ISREENT(funcType)=1;
2652 /* check if this function is defined as calleeSaves
2653 then mark it as such */
2654 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2656 /* loop thru all the arguments */
2657 val = FUNC_ARGS(funcType);
2659 /* if it is void then remove parameters */
2660 if (val && IS_VOID (val->type))
2662 FUNC_ARGS(funcType) = NULL;
2666 /* reset regparm for the port */
2667 (*port->reset_regparms) ();
2669 /* if any of the arguments is an aggregate */
2670 /* change it to pointer to the same type */
2674 char buffer[SDCC_NAME_MAX+1];
2676 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2677 checkTypeSanity (val->etype, buffer);
2679 /* mark it as a register parameter if
2680 the function does not have VA_ARG
2681 and as port dictates */
2682 if (!IFFUNC_HASVARARGS(funcType) &&
2683 (argreg = (*port->reg_parm) (val->type, FUNC_ISREENT(funcType))))
2685 SPEC_REGPARM (val->etype) = 1;
2686 SPEC_ARGREG(val->etype) = argreg;
2687 } else if (IFFUNC_ISREENT(funcType)) {
2688 FUNC_HASSTACKPARM(funcType) = 1;
2691 if (IS_AGGREGATE (val->type))
2693 aggregateToPointer (val);
2700 /* if this is an internal generated function call */
2702 /* ignore --stack-auto for this one, we don't know how it is compiled */
2703 /* simply trust on --int-long-reent or --float-reent */
2704 if (IFFUNC_ISREENT(funcType)) {
2708 /* if this function is reentrant or */
2709 /* automatics r 2b stacked then nothing */
2710 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2714 val = FUNC_ARGS(funcType);
2719 /* if a symbolname is not given */
2720 /* synthesize a variable name */
2723 SNPRINTF (val->name, sizeof(val->name),
2724 "_%s_PARM_%d", func->name, pNum++);
2725 val->sym = newSymbol (val->name, 1);
2726 if (SPEC_SCLS(val->etype) == S_BIT)
2727 SPEC_OCLS (val->etype) = bit;
2729 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2730 val->sym->type = copyLinkChain (val->type);
2731 val->sym->etype = getSpec (val->sym->type);
2732 val->sym->_isparm = 1;
2733 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2735 /* ?? static functions shouldn't imply static parameters - EEP */
2736 if (IS_SPEC(func->etype)) {
2737 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2738 SPEC_STAT (func->etype);
2741 addSymChain (&val->sym);
2744 else /* symbol name given create synth name */
2747 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2748 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2749 val->sym->_isparm = 1;
2750 if (SPEC_SCLS(val->etype) == S_BIT)
2751 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2753 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2754 port->mem.default_local_map;
2757 /* ?? static functions shouldn't imply static parameters - EEP */
2758 if (IS_SPEC(func->etype)) {
2759 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2760 SPEC_STAT (func->etype);
2764 if (SPEC_OCLS (val->sym->etype) == pdata)
2765 val->sym->iaccess = 1;
2766 if (!isinSet(operKeyReset, val->sym)) {
2767 addSet (&operKeyReset, val->sym);
2768 applyToSet (operKeyReset, resetParmKey);
2774 /*-----------------------------------------------------------------*/
2775 /* isSymbolEqual - compares two symbols return 1 if they match */
2776 /*-----------------------------------------------------------------*/
2778 isSymbolEqual (symbol * dest, symbol * src)
2780 /* if pointers match then equal */
2784 /* if one of them is null then don't match */
2788 /* if both of them have rname match on rname */
2789 if (dest->rname[0] && src->rname[0])
2790 return (!strcmp (dest->rname, src->rname));
2792 /* otherwise match on name */
2793 return (!strcmp (dest->name, src->name));
2796 void PT(sym_link *type)
2798 printTypeChain(type,0);
2800 /*-----------------------------------------------------------------*/
2801 /* printTypeChain - prints the type chain in human readable form */
2802 /*-----------------------------------------------------------------*/
2804 printTypeChain (sym_link * start, FILE * of)
2815 dbuf_init (&dbuf, 1024);
2816 dbuf_printTypeChain (start, &dbuf);
2817 dbuf_write_and_destroy (&dbuf, of);
2824 dbuf_printTypeChain (sym_link * start, struct dbuf_s *dbuf)
2827 sym_link * type, * search;
2831 dbuf_append_str (dbuf, "void");
2835 /* Print the chain as it is written in the source: */
2836 /* start with the last entry. */
2837 /* However, the storage class at the end of the */
2838 /* chain reall applies to the first in the chain! */
2840 for (type = start; type && type->next; type = type->next)
2843 scls=SPEC_SCLS(type);
2851 case S_DATA: dbuf_append_str (dbuf, "data-"); break;
2852 case S_XDATA: dbuf_append_str (dbuf, "xdata-"); break;
2853 case S_SFR: dbuf_append_str (dbuf, "sfr-"); break;
2854 case S_SBIT: dbuf_append_str (dbuf, "sbit-"); break;
2855 case S_CODE: dbuf_append_str (dbuf, "code-"); break;
2856 case S_IDATA: dbuf_append_str (dbuf, "idata-"); break;
2857 case S_PDATA: dbuf_append_str (dbuf, "pdata-"); break;
2858 case S_LITERAL: dbuf_append_str (dbuf, "literal-"); break;
2859 case S_STACK: dbuf_append_str (dbuf, "stack-"); break;
2860 case S_XSTACK: dbuf_append_str (dbuf, "xstack-"); break;
2861 case S_BIT: dbuf_append_str (dbuf, "bit-"); break;
2862 case S_EEPROM: dbuf_append_str (dbuf, "eeprom-"); break;
2869 if (!IS_FUNC(type)) {
2870 if (DCL_PTR_VOLATILE (type)) {
2871 dbuf_append_str (dbuf, "volatile-");
2873 if (DCL_PTR_CONST (type)) {
2874 dbuf_append_str (dbuf, "const-");
2877 switch (DCL_TYPE (type))
2880 dbuf_printf (dbuf, "function %s %s",
2881 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2882 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2883 dbuf_append_str (dbuf, "( ");
2884 for (args = FUNC_ARGS(type);
2887 dbuf_printTypeChain(args->type, dbuf);
2889 dbuf_append_str (dbuf, ", ");
2891 dbuf_append_str (dbuf, ") ");
2894 dbuf_append_str (dbuf, "generic* ");
2897 dbuf_append_str (dbuf, "code* ");
2900 dbuf_append_str (dbuf, "xdata* ");
2903 dbuf_append_str (dbuf, "eeprom* ");
2906 dbuf_append_str (dbuf, "near* ");
2909 dbuf_append_str (dbuf, "idata* ");
2912 dbuf_append_str (dbuf, "pdata* ");
2915 dbuf_append_str (dbuf, "unknown* ");
2918 if (DCL_ELEM(type)) {
2919 dbuf_printf (dbuf, "[%d] ", DCL_ELEM(type));
2921 dbuf_append_str (dbuf, "[] ");
2928 if (SPEC_VOLATILE (type))
2929 dbuf_append_str (dbuf, "volatile-");
2930 if (SPEC_CONST (type))
2931 dbuf_append_str (dbuf, "const-");
2932 if (SPEC_USIGN (type))
2933 dbuf_append_str (dbuf, "unsigned-");
2934 switch (SPEC_NOUN (type))
2938 dbuf_append_str (dbuf, "long-");
2939 dbuf_append_str (dbuf, "int");
2943 dbuf_append_str (dbuf, "char");
2947 dbuf_append_str (dbuf, "void");
2951 dbuf_append_str (dbuf, "float");
2955 dbuf_append_str (dbuf, "fixed16x16");
2959 dbuf_printf (dbuf, "struct %s", SPEC_STRUCT (type)->tag);
2963 dbuf_append_str (dbuf, "sbit");
2967 dbuf_append_str (dbuf, "bit");
2971 dbuf_printf (dbuf, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2975 dbuf_append_str (dbuf, "double");
2979 dbuf_append_str (dbuf, "unknown type");
2983 /* search entry in list before "type" */
2984 for (search = start; search && search->next != type;)
2985 search = search->next;
2988 dbuf_append_char(dbuf, ' ');
2992 /*--------------------------------------------------------------------*/
2993 /* printTypeChainRaw - prints the type chain in human readable form */
2994 /* in the raw data structure ordering */
2995 /*--------------------------------------------------------------------*/
2997 printTypeChainRaw (sym_link * start, FILE * of)
3010 fprintf (of, "void");
3020 if (!IS_FUNC(type)) {
3021 if (DCL_PTR_VOLATILE (type)) {
3022 fprintf (of, "volatile-");
3024 if (DCL_PTR_CONST (type)) {
3025 fprintf (of, "const-");
3028 switch (DCL_TYPE (type))
3031 fprintf (of, "function %s %s",
3032 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
3033 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
3035 for (args = FUNC_ARGS(type);
3038 printTypeChain(args->type, of);
3045 fprintf (of, "generic* ");
3048 fprintf (of, "code* ");
3051 fprintf (of, "xdata* ");
3054 fprintf (of, "eeprom* ");
3057 fprintf (of, "near* ");
3060 fprintf (of, "idata* ");
3063 fprintf (of, "pdata* ");
3066 fprintf (of, "unknown* ");
3069 if (DCL_ELEM(type)) {
3070 fprintf (of, "[%d] ", DCL_ELEM(type));
3072 fprintf (of, "[] ");
3076 if (DCL_TSPEC(type))
3079 printTypeChainRaw(DCL_TSPEC(type), of);
3083 else if (IS_SPEC (type))
3085 switch (SPEC_SCLS (type))
3087 case S_DATA: fprintf (of, "data-"); break;
3088 case S_XDATA: fprintf (of, "xdata-"); break;
3089 case S_SFR: fprintf (of, "sfr-"); break;
3090 case S_SBIT: fprintf (of, "sbit-"); break;
3091 case S_CODE: fprintf (of, "code-"); break;
3092 case S_IDATA: fprintf (of, "idata-"); break;
3093 case S_PDATA: fprintf (of, "pdata-"); break;
3094 case S_LITERAL: fprintf (of, "literal-"); break;
3095 case S_STACK: fprintf (of, "stack-"); break;
3096 case S_XSTACK: fprintf (of, "xstack-"); break;
3097 case S_BIT: fprintf (of, "bit-"); break;
3098 case S_EEPROM: fprintf (of, "eeprom-"); break;
3101 if (SPEC_VOLATILE (type))
3102 fprintf (of, "volatile-");
3103 if (SPEC_CONST (type))
3104 fprintf (of, "const-");
3105 if (SPEC_USIGN (type))
3106 fprintf (of, "unsigned-");
3107 switch (SPEC_NOUN (type))
3111 fprintf (of, "long-");
3112 fprintf (of, "int");
3116 fprintf (of, "char");
3120 fprintf (of, "void");
3124 fprintf (of, "float");
3128 fprintf (of, "fixed16x16");
3132 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
3136 fprintf (of, "sbit");
3140 fprintf (of, "bit");
3144 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
3148 fprintf (of, "double");
3152 fprintf (of, "unknown type");
3157 fprintf (of, "NOT_SPEC_OR_DECL");
3167 /*-----------------------------------------------------------------*/
3168 /* powof2 - returns power of two for the number if number is pow 2 */
3169 /*-----------------------------------------------------------------*/
3171 powof2 (TYPE_TARGET_ULONG num)
3184 if (n1s > 1 || nshifts == 0)
3200 symbol *__fps16x16_add;
3201 symbol *__fps16x16_sub;
3202 symbol *__fps16x16_mul;
3203 symbol *__fps16x16_div;
3204 symbol *__fps16x16_eq;
3205 symbol *__fps16x16_neq;
3206 symbol *__fps16x16_lt;
3207 symbol *__fps16x16_lteq;
3208 symbol *__fps16x16_gt;
3209 symbol *__fps16x16_gteq;
3211 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3212 symbol *__muldiv[3][3][2];
3213 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3214 sym_link *__multypes[3][2];
3215 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3216 symbol *__conv[2][3][2];
3217 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/FLOAT, SIGNED/USIGNED */
3218 symbol *__fp16x16conv[2][4][2];
3219 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3220 symbol *__rlrr[2][3][2];
3222 sym_link *floatType;
3223 sym_link *fixed16x16Type;
3226 _mangleFunctionName(char *in)
3228 if (port->getMangledFunctionName)
3230 return port->getMangledFunctionName(in);
3238 /*-----------------------------------------------------------------*/
3239 /* typeFromStr - create a typechain from an encoded string */
3240 /* basic types - 'c' - char */
3245 /* 'q' - fixed16x16 */
3247 /* '*' - pointer - default (GPOINTER) */
3248 /* modifiers - 'u' - unsigned */
3249 /* pointer modifiers - 'g' - generic */
3253 /* 'F' - function */
3254 /* examples : "ig*" - generic int * */
3255 /* "cx*" - char xdata * */
3256 /* "ui" - unsigned int */
3257 /*-----------------------------------------------------------------*/
3258 sym_link *typeFromStr (char *s)
3260 sym_link *r = newLink(DECLARATOR);
3272 r->class = SPECIFIER;
3273 SPEC_NOUN(r) = V_CHAR;
3277 r->class = SPECIFIER;
3278 SPEC_NOUN(r) = V_INT;
3281 r->class = SPECIFIER;
3282 SPEC_NOUN(r) = V_INT;
3286 r->class = SPECIFIER;
3287 SPEC_NOUN(r) = V_FLOAT;
3290 r->class = SPECIFIER;
3291 SPEC_NOUN(r) = V_FIXED16X16;
3294 r->class = SPECIFIER;
3295 SPEC_NOUN(r) = V_VOID;
3298 DCL_TYPE(r) = port->unqualified_pointer;
3305 assert(*(s+1)=='*');
3306 nr = newLink(DECLARATOR);
3311 DCL_TYPE(r) = GPOINTER;
3314 DCL_TYPE(r) = FPOINTER;
3317 DCL_TYPE(r) = CPOINTER;
3320 DCL_TYPE(r) = POINTER;
3323 DCL_TYPE(r) = FUNCTION;
3324 nr = newLink(DECLARATOR);
3327 DCL_TYPE(r) = CPOINTER;
3333 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3334 "typeFromStr: unknown type");
3337 if (IS_SPEC(r) && usign) {
3346 /*-----------------------------------------------------------------*/
3347 /* initCSupport - create functions for C support routines */
3348 /*-----------------------------------------------------------------*/
3352 const char *smuldivmod[] =
3356 const char *sbwd[] =
3358 "char", "int", "long", "fixed16x16",
3360 const char *fp16x16sbwd[] =
3362 "char", "int", "long", "float",
3368 const char *srlrr[] =
3373 int bwd, su, muldivmod, tofrom, rlrr;
3375 if (getenv("SDCC_NO_C_SUPPORT")) {
3376 /* for debugging only */
3380 floatType = newFloatLink ();
3381 fixed16x16Type = newFixed16x16Link ();
3383 for (bwd = 0; bwd < 3; bwd++)
3400 __multypes[bwd][0] = l;
3401 __multypes[bwd][1] = copyLinkChain (l);
3402 SPEC_USIGN (__multypes[bwd][1]) = 1;
3405 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3406 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3407 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3408 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3409 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3410 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3411 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3412 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3413 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3414 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3416 __fps16x16_add = funcOfType ("__fps16x16_add", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3417 __fps16x16_sub = funcOfType ("__fps16x16_sub", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3418 __fps16x16_mul = funcOfType ("__fps16x16_mul", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3419 __fps16x16_div = funcOfType ("__fps16x16_div", fixed16x16Type, fixed16x16Type, 2, options.float_rent);
3420 __fps16x16_eq = funcOfType ("__fps16x16_eq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3421 __fps16x16_neq = funcOfType ("__fps16x16_neq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3422 __fps16x16_lt = funcOfType ("__fps16x16_lt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3423 __fps16x16_lteq = funcOfType ("__fps16x16_lteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3424 __fps16x16_gt = funcOfType ("__fps16x16_gt", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3425 __fps16x16_gteq = funcOfType ("__fps16x16_gteq", CHARTYPE, fixed16x16Type, 2, options.float_rent);
3428 for (tofrom = 0; tofrom < 2; tofrom++)
3430 for (bwd = 0; bwd < 3; bwd++)
3432 for (su = 0; su < 2; su++)
3436 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3437 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3441 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3442 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3448 for (tofrom = 0; tofrom < 2; tofrom++)
3450 for (bwd = 0; bwd < 4; bwd++)
3452 for (su = 0; su < 2; su++)
3456 SNPRINTF (buffer, sizeof(buffer), "__fps16x162%s%s", ssu[su], fp16x16sbwd[bwd]);
3458 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, floatType, fixed16x16Type, 1, options.float_rent);
3460 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], fixed16x16Type, 1, options.float_rent);
3464 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fps16x16", ssu[su], fp16x16sbwd[bwd]);
3466 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, floatType, 1, options.float_rent);
3468 __fp16x16conv[tofrom][bwd][su] = funcOfType (buffer, fixed16x16Type, __multypes[bwd][su], 1, options.float_rent);
3475 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3477 for (bwd = 0; bwd < 3; bwd++)
3479 for (su = 0; su < 2; su++)
3481 SNPRINTF (buffer, sizeof(buffer),
3483 smuldivmod[muldivmod],
3486 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3487 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3492 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3493 Therefore they've been merged into mulint() and mullong().
3496 for (bwd = 0; bwd < 3; bwd++)
3498 for (su = 0; su < 2; su++)
3500 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3502 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
3503 if (!TARGET_IS_PIC16 || muldivmod != 1 || bwd != 0 || su != 0)
3505 SNPRINTF (buffer, sizeof(buffer),
3507 smuldivmod[muldivmod],
3510 __muldiv[muldivmod][bwd][su] = funcOfType (
3511 _mangleFunctionName(buffer),
3512 __multypes[bwd][su],
3513 __multypes[bwd][su],
3515 options.intlong_rent);
3516 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3522 if (TARGET_IS_PIC16)
3524 /* PIC16 port wants __divschar/__modschar to return an int, so that both
3525 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
3526 * (first one would have to be sign extended, second one must not be).
3527 * Similarly, modschar should be handled, but the iCode introduces cast
3528 * here and forces '% : s8 x s8 -> s8' ... */
3530 for (muldivmod = 1; muldivmod < 2; muldivmod++) {
3531 SNPRINTF (buffer, sizeof(buffer),
3533 smuldivmod[muldivmod],
3536 __muldiv[muldivmod][bwd][su] = funcOfType (
3537 _mangleFunctionName(buffer),
3539 __multypes[bwd][su],
3541 options.intlong_rent);
3542 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3550 for (su = 0; su < 2; su++)
3552 /* muluchar and mulschar are still separate functions, because e.g. the z80
3553 port is sign/zero-extending to int before calling mulint() */
3554 SNPRINTF (buffer, sizeof(buffer),
3556 smuldivmod[muldivmod],
3559 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3560 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3564 /* word and doubleword */
3565 for (bwd = 1; bwd < 3; bwd++)
3568 SNPRINTF (buffer, sizeof(buffer),
3570 smuldivmod[muldivmod],
3572 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3573 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3574 /* signed = unsigned */
3575 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3578 for (rlrr = 0; rlrr < 2; rlrr++)
3580 for (bwd = 0; bwd < 3; bwd++)
3582 for (su = 0; su < 2; su++)
3584 SNPRINTF (buffer, sizeof(buffer),
3589 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3590 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3596 /*-----------------------------------------------------------------*/
3597 /* initBuiltIns - create prototypes for builtin functions */
3598 /*-----------------------------------------------------------------*/
3604 if (!port->builtintable) return ;
3606 for (i = 0 ; port->builtintable[i].name ; i++) {
3607 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3608 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3609 FUNC_ISBUILTIN(sym->type) = 1;
3610 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3614 sym_link *validateLink(sym_link *l,
3621 if (l && l->class==select)
3626 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3627 " expected %s, got %s\n",
3628 macro, args, file, line,
3629 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3631 return l; // never reached, makes compiler happy.
3634 /*--------------------------------------------------------------------*/
3635 /* newEnumType - create an integer type compatible with enumerations */
3636 /*--------------------------------------------------------------------*/
3638 newEnumType (symbol *enumlist)
3646 type = newLink (SPECIFIER);
3647 SPEC_NOUN (type) = V_INT;
3651 /* Determine the range of the enumerated values */
3653 min = max = (int) floatFromVal (valFromType (sym->type));
3654 for (sym = sym->next; sym; sym = sym->next)
3656 v = (int) floatFromVal (valFromType (sym->type));
3663 /* Determine the smallest integer type that is compatible with this range */
3664 type = newLink (SPECIFIER);
3665 if (min>=0 && max<=255)
3667 SPEC_NOUN (type) = V_CHAR;
3668 SPEC_USIGN (type) = 1;
3670 else if (min>=-128 && max<=127)
3672 SPEC_NOUN (type) = V_CHAR;
3674 else if (min>=0 && max<=65535)
3676 SPEC_NOUN (type) = V_INT;
3677 SPEC_USIGN (type) = 1;
3679 else if (min>=-32768 && max<=32767)
3681 SPEC_NOUN (type) = V_INT;
3685 SPEC_NOUN (type) = V_INT;
3686 SPEC_LONG (type) = 1;
3688 SPEC_USIGN (type) = 1;