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 declarator */
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 /* getAllocSize - returns size of a type chain in bytes for allocation */
832 /*---------------------------------------------------------------------*/
834 getAllocSize (sym_link *p)
836 if (IS_STRUCT (p) && SPEC_STRUCT (p)->type == STRUCT)
838 /* if this is a struct specifier then */
839 /* calculate the size as it could end */
840 /* with an array of unspecified length */
841 symbol *sflds = SPEC_STRUCT (p)->fields;
843 while (sflds && sflds->next)
846 if (sflds && !IS_BITFIELD (sflds->type))
847 return sflds->offset + getAllocSize (sflds->type);
849 return SPEC_STRUCT (p)->size;
855 /*------------------------------------------------------------------*/
856 /* bitsForType - returns # of bits required to store this type */
857 /*------------------------------------------------------------------*/
859 bitsForType (sym_link * p)
861 /* if nothing return 0 */
866 { /* if this is the specifier then */
868 switch (SPEC_NOUN (p))
869 { /* depending on the specifier type */
871 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
873 return FLOATSIZE * 8;
879 return SPEC_STRUCT (p)->size * 8;
886 return SPEC_BLEN (p);
892 /* this is a specifier */
893 switch (DCL_TYPE (p))
896 return DCL_ELEM (p) * getSize (p->next) * 8;
900 return (PTRSIZE * 8);
905 return (FPTRSIZE * 8);
907 return (GPTRSIZE * 8);
914 /*------------------------------------------------------------------*/
915 /* copySymbolChain - copies a symbol chain */
916 /*------------------------------------------------------------------*/
918 copySymbolChain (symbol * src)
925 dest = copySymbol (src);
926 dest->next = copySymbolChain (src->next);
930 /*------------------------------------------------------------------*/
931 /* copySymbol - makes a copy of a symbol */
932 /*------------------------------------------------------------------*/
934 copySymbol (symbol * src)
941 dest = newSymbol (src->name, src->level);
942 memcpy (dest, src, sizeof (symbol));
943 dest->level = src->level;
944 dest->block = src->block;
945 dest->ival = copyIlist (src->ival);
946 dest->type = copyLinkChain (src->type);
947 dest->etype = getSpec (dest->type);
949 dest->key = src->key;
950 dest->allocreq = src->allocreq;
954 /*------------------------------------------------------------------*/
955 /* reverseSyms - reverses the links for a symbol chain */
956 /*------------------------------------------------------------------*/
958 reverseSyms (symbol * sym)
960 symbol *prev, *curr, *next;
975 sym->next = (void *) NULL;
979 /*------------------------------------------------------------------*/
980 /* reverseLink - reverses the links for a type chain */
981 /*------------------------------------------------------------------*/
983 reverseLink (sym_link * type)
985 sym_link *prev, *curr, *next;
1000 type->next = (void *) NULL;
1004 /*------------------------------------------------------------------*/
1005 /* addSymChain - adds a symbol chain to the symboltable */
1006 /*------------------------------------------------------------------*/
1008 addSymChain (symbol * symHead)
1010 symbol *sym = symHead;
1011 symbol *csym = NULL;
1014 for (; sym != NULL; sym = sym->next)
1016 changePointer(sym->type);
1017 checkTypeSanity(sym->etype, sym->name);
1019 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1022 /* if already exists in the symbol table then check if
1023 one of them is an extern definition if yes then
1024 then check if the type match, if the types match then
1025 delete the current entry and add the new entry */
1026 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1027 csym->level == sym->level) {
1029 /* If the previous definition was for an array with incomplete */
1030 /* type, and the new definition has completed the type, update */
1031 /* the original type to match */
1032 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1033 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1035 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1036 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1040 /* If only one of the definitions used the "at" keyword, copy */
1041 /* the address to the other. */
1042 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1043 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1045 SPEC_ABSA (sym->etype) = 1;
1046 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1048 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1049 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1051 SPEC_ABSA (csym->etype) = 1;
1052 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1057 if (csym->ival && sym->ival)
1059 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1063 /* one definition extern ? */
1064 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1065 werror (E_EXTERN_MISMATCH, sym->name);
1067 werror (E_DUPLICATE, sym->name);
1068 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1070 fprintf (stderr, "from type '");
1071 printTypeChain (csym->type, stderr);
1072 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1073 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1074 fprintf (stderr, "'\nto type '");
1075 printTypeChain (sym->type, stderr);
1076 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1077 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1078 fprintf (stderr, "'\n");
1083 if (csym->ival && !sym->ival)
1084 sym->ival = csym->ival;
1086 /* delete current entry */
1087 deleteSym (SymbolTab, csym, csym->name);
1088 deleteFromSeg(csym);
1092 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1097 /*------------------------------------------------------------------*/
1098 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1099 /*------------------------------------------------------------------*/
1101 funcInChain (sym_link * lnk)
1112 /*------------------------------------------------------------------*/
1113 /* structElemType - returns the type info of a struct member */
1114 /*------------------------------------------------------------------*/
1116 structElemType (sym_link * stype, value * id)
1118 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1119 sym_link *type, *etype;
1120 sym_link *petype = getSpec (stype);
1124 /* look for the id */
1127 if (strcmp (fields->rname, id->name) == 0)
1129 type = copyLinkChain (fields->type);
1130 etype = getSpec (type);
1131 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1132 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1134 SPEC_CONST (type) |= SPEC_CONST (stype);
1136 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1139 fields = fields->next;
1143 werror (E_NOT_MEMBER, id->name);
1145 // the show must go on
1146 return newIntLink();
1149 /*------------------------------------------------------------------*/
1150 /* getStructElement - returns element of a tructure definition */
1151 /*------------------------------------------------------------------*/
1153 getStructElement (structdef * sdef, symbol * sym)
1157 for (field = sdef->fields; field; field = field->next)
1158 if (strcmp (field->name, sym->name) == 0)
1161 werror (E_NOT_MEMBER, sym->name);
1163 return sdef->fields;
1166 /*------------------------------------------------------------------*/
1167 /* compStructSize - computes the size of a structure */
1168 /*------------------------------------------------------------------*/
1170 compStructSize (int su, structdef * sdef)
1172 int sum = 0, usum = 0;
1176 /* for the identifiers */
1177 loop = sdef->fields;
1180 /* create the internal name for this variable */
1181 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1186 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1188 /* if this is a bit field */
1191 /* change it to a unsigned bit */
1192 SPEC_NOUN (loop->etype) = V_BITFIELD;
1193 SPEC_USIGN (loop->etype) = 1;
1194 SPEC_BLEN (loop->etype) = loop->bitVar;
1196 if (loop->bitVar == BITVAR_PAD) {
1197 /* A zero length bitfield forces padding */
1198 SPEC_BSTR (loop->etype) = bitOffset;
1199 SPEC_BLEN (loop->etype) = 0;
1204 if (bitOffset == 8) {
1208 /* check if this fit into the remaining */
1209 /* bits of this byte else align it to the */
1210 /* next byte boundary */
1211 if (loop->bitVar <= (8 - bitOffset)) {
1212 /* fits into current byte */
1214 SPEC_BSTR (loop->etype) = bitOffset;
1215 bitOffset += loop->bitVar;
1217 else if (!bitOffset) {
1218 /* does not fit, but is already byte aligned */
1220 SPEC_BSTR (loop->etype) = bitOffset;
1221 bitOffset += loop->bitVar;
1224 /* does not fit; need to realign first */
1226 loop->offset = (su == UNION ? sum = 0 : sum);
1228 SPEC_BSTR (loop->etype) = bitOffset;
1229 bitOffset += loop->bitVar;
1231 while (bitOffset>8) {
1238 /* This is a non-bit field. Make sure we are */
1239 /* byte aligned first */
1242 loop->offset = (su == UNION ? sum = 0 : sum);
1246 checkDecl (loop, 1);
1247 sum += getSize (loop->type);
1252 /* if union then size = sizeof largest field */
1254 /* For UNION, round up after each field */
1255 sum += ((bitOffset+7)/8);
1256 usum = max (usum, sum);
1261 /* For STRUCT, round up after all fields processed */
1263 sum += ((bitOffset+7)/8);
1265 return (su == UNION ? usum : sum);
1268 /*-------------------------------------------------------------------*/
1269 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1270 /* an enclosing struct/union */
1271 /*-------------------------------------------------------------------*/
1273 promoteAnonStructs (int su, structdef * sdef)
1282 tofield = &sdef->fields;
1283 field = sdef->fields;
1286 nextfield = field->next;
1287 if (!*field->name && IS_STRUCT (field->type))
1289 /* Found an anonymous struct/union. Replace it */
1290 /* with the fields it contains and adjust all */
1293 base = field->offset;
1294 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1296 continue; /* just in case it's empty */
1298 *tofield = subfield;
1301 /* check for field name conflicts resulting from promotion */
1302 dupfield = sdef->fields;
1303 while (dupfield && dupfield != subfield)
1305 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1307 werrorfl (subfield->fileDef, subfield->lineDef,
1309 su==STRUCT ? "struct" : "union",
1311 werrorfl (dupfield->fileDef, dupfield->lineDef,
1314 dupfield = dupfield->next;
1317 subfield->offset += base;
1319 subfield = subfield->next;
1323 subfield->next = nextfield;
1324 tofield = &subfield->next;
1327 tofield = &field->next;
1333 /*------------------------------------------------------------------*/
1334 /* checkSClass - check the storage class specification */
1335 /*------------------------------------------------------------------*/
1337 checkSClass (symbol * sym, int isProto)
1341 if (getenv("DEBUG_SANITY")) {
1342 fprintf (stderr, "checkSClass: %s \n", sym->name);
1345 /* type is literal can happen for enums change
1347 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1348 SPEC_SCLS (sym->etype) = S_AUTO;
1350 /* if sfr or sbit then must also be volatile */
1351 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1352 SPEC_SCLS (sym->etype) == S_SFR)
1354 SPEC_VOLATILE (sym->etype) = 1;
1357 /* if absolute address given then it mark it as
1358 volatile -- except in the PIC port */
1360 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1361 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1362 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1365 if (IS_ABSOLUTE (sym->etype))
1366 SPEC_VOLATILE (sym->etype) = 1;
1368 /* If code memory is read only, then pointers to code memory */
1369 /* implicitly point to constants -- make this explicit */
1371 while (t && t->next) {
1372 if (IS_CODEPTR(t) && port->mem.code_ro) {
1373 if (IS_SPEC(t->next)) {
1374 SPEC_CONST (t->next) = 1;
1376 DCL_PTR_CONST (t->next) = 1;
1382 /* global variables declared const put into code */
1383 /* if no other storage class specified */
1384 if (sym->level == 0 &&
1385 SPEC_SCLS(sym->etype) == S_FIXED &&
1386 !IS_FUNC(sym->type)) {
1387 /* find the first non-array link */
1391 if (IS_CONSTANT (t)) {
1392 SPEC_SCLS (sym->etype) = S_CODE;
1396 /* global variable in code space is a constant */
1397 if (sym->level == 0 &&
1398 SPEC_SCLS (sym->etype) == S_CODE &&
1399 port->mem.code_ro) {
1400 /* find the first non-array link */
1407 DCL_PTR_CONST (t) = 1;
1411 /* if bit variable then no storage class can be */
1412 /* specified since bit is already a storage */
1413 if (IS_BITVAR (sym->etype) &&
1414 (SPEC_SCLS (sym->etype) != S_FIXED &&
1415 SPEC_SCLS (sym->etype) != S_SBIT &&
1416 SPEC_SCLS (sym->etype) != S_BIT)
1419 werror (E_BITVAR_STORAGE, sym->name);
1420 SPEC_SCLS (sym->etype) = S_FIXED;
1423 /* extern variables cannot be initialized */
1424 if (IS_EXTERN (sym->etype) && sym->ival)
1426 werror (E_EXTERN_INIT, sym->name);
1430 /* if this is an automatic symbol */
1431 if (sym->level && (options.stackAuto || reentrant)) {
1432 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1433 SPEC_SCLS (sym->etype) == S_FIXED ||
1434 SPEC_SCLS (sym->etype) == S_REGISTER ||
1435 SPEC_SCLS (sym->etype) == S_STACK ||
1436 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1437 SPEC_SCLS (sym->etype) = S_AUTO;
1439 /* storage class may only be specified for statics */
1440 if (!IS_STATIC(sym->etype)) {
1441 werror (E_AUTO_ASSUMED, sym->name);
1446 /* automatic symbols cannot be given */
1447 /* an absolute address ignore it */
1449 SPEC_ABSA (sym->etype) &&
1450 (options.stackAuto || reentrant))
1452 werror (E_AUTO_ABSA, sym->name);
1453 SPEC_ABSA (sym->etype) = 0;
1456 /* arrays & pointers cannot be defined for bits */
1457 /* SBITS or SFRs or BIT */
1458 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1459 (SPEC_NOUN (sym->etype) == V_BIT ||
1460 SPEC_NOUN (sym->etype) == V_SBIT ||
1461 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1462 SPEC_SCLS (sym->etype) == S_SFR))
1463 werror (E_BIT_ARRAY, sym->name);
1465 /* if this is a bit|sbit then set length & start */
1466 if (SPEC_NOUN (sym->etype) == V_BIT ||
1467 SPEC_NOUN (sym->etype) == V_SBIT)
1469 SPEC_BLEN (sym->etype) = 1;
1470 SPEC_BSTR (sym->etype) = 0;
1474 /* variables declared in CODE space must have */
1475 /* initializers if not an extern */
1476 if (SPEC_SCLS (sym->etype) == S_CODE &&
1477 sym->ival == NULL &&
1480 port->mem.code_ro &&
1481 !IS_EXTERN (sym->etype) &&
1482 !funcInChain (sym->type))
1483 werror (E_CODE_NO_INIT, sym->name);
1486 /* if parameter or local variable then change */
1487 /* the storage class to reflect where the var will go */
1488 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1489 !IS_STATIC(sym->etype))
1491 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1493 SPEC_SCLS (sym->etype) = (options.useXstack ?
1494 S_XSTACK : S_STACK);
1498 /* hack-o-matic! I see no reason why the useXstack option should ever
1499 * control this allocation, but the code was originally that way, and
1500 * changing it for non-390 ports breaks the compiler badly.
1502 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1503 1 : options.useXstack;
1504 SPEC_SCLS (sym->etype) = (useXdata ?
1510 /*------------------------------------------------------------------*/
1511 /* changePointer - change pointer to functions */
1512 /*------------------------------------------------------------------*/
1514 changePointer (sym_link * p)
1517 /* go thru the chain of declarations */
1518 /* if we find a pointer to a function */
1519 /* unconditionally change it to a ptr */
1521 for (; p; p = p->next)
1523 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1524 DCL_TYPE (p) = port->unqualified_pointer;
1525 if (IS_PTR (p) && IS_FUNC (p->next))
1526 DCL_TYPE (p) = CPOINTER;
1530 /*------------------------------------------------------------------*/
1531 /* checkDecl - does semantic validation of a declaration */
1532 /*------------------------------------------------------------------*/
1534 checkDecl (symbol * sym, int isProto)
1537 checkSClass (sym, isProto); /* check the storage class */
1538 changePointer (sym->type); /* change pointers if required */
1540 /* if this is an array without any dimension
1541 then update the dimension from the initial value */
1542 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1543 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1548 /*------------------------------------------------------------------*/
1549 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1550 /*------------------------------------------------------------------*/
1552 copyLinkChain (sym_link * p)
1554 sym_link *head, *curr, *loop;
1557 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1560 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1561 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1569 /*------------------------------------------------------------------*/
1570 /* cleanUpBlock - cleansup the symbol table specified for all the */
1571 /* symbols in the given block */
1572 /*------------------------------------------------------------------*/
1574 cleanUpBlock (bucket ** table, int block)
1579 /* go thru the entire table */
1580 for (i = 0; i < 256; i++)
1582 for (chain = table[i]; chain; chain = chain->next)
1584 if (chain->block >= block)
1586 deleteSym (table, chain->sym, chain->name);
1592 /*------------------------------------------------------------------*/
1593 /* cleanUpLevel - cleansup the symbol table specified for all the */
1594 /* symbols in the given level */
1595 /*------------------------------------------------------------------*/
1597 cleanUpLevel (bucket ** table, int level)
1602 /* go thru the entire table */
1603 for (i = 0; i < 256; i++)
1605 for (chain = table[i]; chain; chain = chain->next)
1607 if (chain->level >= level)
1609 deleteSym (table, chain->sym, chain->name);
1615 /*------------------------------------------------------------------*/
1616 /* computeTypeOr - computes the resultant type from two types */
1617 /*------------------------------------------------------------------*/
1619 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1622 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1623 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1625 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1627 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1631 if (SPEC_USIGN (etype1))
1633 if ( IS_LITERAL (etype2)
1634 && floatFromVal (valFromType (etype2)) >= 0)
1635 SPEC_USIGN (reType) = 1;
1638 /* promote to int */
1639 SPEC_USIGN (reType) = 0;
1640 SPEC_NOUN (reType) = V_INT;
1643 else /* etype1 signed */
1645 if ( IS_LITERAL (etype2)
1646 && floatFromVal (valFromType (etype2)) <= 127)
1647 SPEC_USIGN (reType) = 0;
1650 /* promote to int */
1651 SPEC_USIGN (reType) = 0;
1652 SPEC_NOUN (reType) = V_INT;
1656 if (SPEC_USIGN (etype2))
1658 if ( IS_LITERAL (etype1)
1659 && floatFromVal (valFromType (etype1)) >= 0)
1660 SPEC_USIGN (reType) = 1;
1663 /* promote to int */
1664 SPEC_USIGN (reType) = 0;
1665 SPEC_NOUN (reType) = V_INT;
1668 else /* etype2 signed */
1670 if ( IS_LITERAL (etype1)
1671 && floatFromVal (valFromType (etype1)) <= 127)
1672 SPEC_USIGN (reType) = 0;
1675 /* promote to int */
1676 SPEC_USIGN (reType) = 0;
1677 SPEC_NOUN (reType) = V_INT;
1683 /*------------------------------------------------------------------*/
1684 /* computeType - computes the resultant type from two types */
1685 /*------------------------------------------------------------------*/
1687 computeType (sym_link * type1, sym_link * type2,
1688 RESULT_TYPE resultType, int op)
1692 sym_link *etype1 = getSpec (type1);
1695 etype2 = type2 ? getSpec (type2) : type1;
1697 /* if one of them is a float then result is a float */
1698 /* here we assume that the types passed are okay */
1699 /* and can be cast to one another */
1700 /* which ever is greater in size */
1701 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1702 rType = newFloatLink ();
1704 /* if both are bitvars choose the larger one */
1705 if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1707 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1708 copyLinkChain (type1) : copyLinkChain (type1);
1710 /* if only one of them is a bit variable
1711 then the other one prevails */
1712 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1714 rType = copyLinkChain (type2);
1715 /* bitfield can have up to 16 bits */
1716 if (getSize (etype1) > 1)
1717 SPEC_NOUN (getSpec (rType)) = V_INT;
1719 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1721 rType = copyLinkChain (type1);
1722 /* bitfield can have up to 16 bits */
1723 if (getSize (etype2) > 1)
1724 SPEC_NOUN (getSpec (rType)) = V_INT;
1727 /* if one of them is a pointer or array then that
1729 if (IS_PTR (type1) || IS_ARRAY (type1))
1730 rType = copyLinkChain (type1);
1731 else if (IS_PTR (type2) || IS_ARRAY (type2))
1732 rType = copyLinkChain (type2);
1733 else if (getSize (type1) > getSize (type2))
1734 rType = copyLinkChain (type1);
1736 rType = copyLinkChain (type2);
1738 reType = getSpec (rType);
1740 /* avoid conflicting types */
1741 reType->select.s._signed = 0;
1743 /* if result is a literal then make not so */
1744 if (IS_LITERAL (reType))
1745 SPEC_SCLS (reType) = S_REGISTER;
1749 case RESULT_TYPE_CHAR:
1750 if (IS_BITVAR (reType))
1752 SPEC_NOUN (reType) = V_CHAR;
1753 SPEC_SCLS (reType) = 0;
1754 SPEC_USIGN (reType) = 0;
1758 case RESULT_TYPE_INT:
1759 case RESULT_TYPE_NONE:
1760 case RESULT_TYPE_OTHER:
1761 if (IS_BIT (reType))
1763 SPEC_NOUN (reType) = V_CHAR;
1764 SPEC_SCLS (reType) = 0;
1765 SPEC_USIGN (reType) = 0;
1768 else if (IS_BITFIELD (reType))
1770 /* could be smarter, but it depends on the op */
1771 /* this is for the worst case: a multiplication of 4 * 4 bit */
1772 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1773 SPEC_SCLS (reType) = 0;
1774 SPEC_USIGN (reType) = 0;
1777 else if (IS_CHAR (reType))
1779 if (op == '|' || op == '^')
1780 return computeTypeOr (etype1, etype2, reType);
1782 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1784 SPEC_USIGN (reType) = 1;
1789 SPEC_NOUN (reType) = V_INT;
1790 SPEC_USIGN (reType) = 0;
1793 /* TODO: should be in SDCCast.c */
1795 && ( !SPEC_USIGN (etype1)
1796 || !SPEC_USIGN (etype2)))
1798 SPEC_NOUN (reType) = V_INT;
1799 SPEC_USIGN (reType) = 0;
1808 /* SDCC's sign promotion:
1809 - if one or both operands are unsigned, the resultant type will be unsigned
1810 (except char, see below)
1811 - if an operand is promoted to a larger type (char -> int, int -> long),
1812 the larger type will be signed
1814 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1815 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1816 the standard. The standard demands, that the result has to be the same
1817 "as if" the promotion would have been performed:
1819 - if the result of an operation with two char's is promoted to a
1820 larger type, the result will be signed.
1822 More sophisticated are these:
1823 - if the result of an operation with two char's is a char again,
1824 the result will only then be unsigned, if both operands are
1825 unsigned. In all other cases the result will be signed.
1827 This seems to be contradictionary to the first two rules, but it makes
1828 real sense (all types are char's):
1830 A signed char can be negative; this must be preserved in the result
1833 Only if both operands are unsigned it's safe to make the result
1834 unsigned; this helps to avoid overflow:
1837 - ToDo: document '|', '^' and '&'
1839 Homework: - why is (200 * 200 < 0) true?
1840 - why is { char l = 200, r = 200; (r * l > 0) } true?
1843 if (!IS_FLOAT (reType)
1844 && ( (SPEC_USIGN (etype1)
1845 /* if this operand is promoted to a larger type,
1846 then it will be promoted to a signed type */
1847 && !(getSize (etype1) < getSize (reType))
1848 /* char require special handling */
1849 && !IS_CHAR (etype1))
1850 || /* same for 2nd operand */
1851 (SPEC_USIGN (etype2)
1852 && !(getSize (etype2) < getSize (reType))
1853 && !IS_CHAR (etype2))
1854 || /* if both are 'unsigned char' and not promoted
1855 let the result be unsigned too */
1856 ( SPEC_USIGN (etype1)
1857 && SPEC_USIGN (etype2)
1860 && IS_CHAR (reType))))
1861 SPEC_USIGN (reType) = 1;
1863 SPEC_USIGN (reType) = 0;
1868 /*--------------------------------------------------------------------*/
1869 /* compareType - will do type check return 1 if match, -1 if castable */
1870 /*--------------------------------------------------------------------*/
1872 compareType (sym_link * dest, sym_link * src)
1883 /* if dest is a declarator then */
1888 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1890 //checkFunction(src,dest);
1892 return compareType (dest->next, src->next);
1894 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1898 (IS_GENPTR (dest) ||
1899 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
1902 if (IS_PTR (dest) && IS_ARRAY (src)) {
1903 value *val=aggregateToPointer (valFromType(src));
1904 int res=compareType (dest, val->type);
1905 Safe_free(val->type);
1909 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1910 return compareType (dest->next, src);
1913 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1919 /* if one is a specifier and the other is not */
1920 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1921 (IS_SPEC (dest) && !IS_SPEC (src)))
1924 /* if one of them is a void then ok */
1925 if (SPEC_NOUN (dest) == V_VOID &&
1926 SPEC_NOUN (src) != V_VOID)
1929 if (SPEC_NOUN (dest) != V_VOID &&
1930 SPEC_NOUN (src) == V_VOID)
1933 /* if they are both bitfields then if the lengths
1934 and starts don't match */
1935 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1936 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1937 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1940 /* it is a specifier */
1941 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1943 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1944 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1946 bitsForType (dest) == bitsForType (src))
1947 instead of the next two lines, but the regression tests fail with
1948 them; I guess it's a problem with replaceCheaperOp */
1949 getSize (dest) == getSize (src) &&
1950 !(!IS_BIT (dest) && IS_BIT (src)))
1952 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1957 else if (IS_STRUCT (dest))
1959 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1964 if (SPEC_LONG (dest) != SPEC_LONG (src))
1967 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1973 /*--------------------------------------------------------------------*/
1974 /* compareTypeExact - will do type check return 1 if match exactly */
1975 /*--------------------------------------------------------------------*/
1977 compareTypeExact (sym_link * dest, sym_link * src, int level)
1979 STORAGE_CLASS srcScls, destScls;
1990 /* if dest is a declarator then */
1995 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1996 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
1998 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2000 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2004 value *exargs, *acargs, *checkValue;
2006 /* verify function return type */
2007 if (!compareTypeExact (dest->next, src->next, -1))
2009 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2011 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2013 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2016 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2020 /* compare expected args with actual args */
2021 exargs = FUNC_ARGS(dest);
2022 acargs = FUNC_ARGS(src);
2024 /* for all the expected args do */
2025 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2027 //checkTypeSanity(acargs->etype, acargs->name);
2029 if (IS_AGGREGATE (acargs->type))
2031 checkValue = copyValue (acargs);
2032 aggregateToPointer (checkValue);
2035 checkValue = acargs;
2038 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2043 /* if one them ended we have a problem */
2044 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2045 (!exargs && acargs && !IS_VOID (acargs->type)))
2049 return compareTypeExact (dest->next, src->next, level);
2056 /* if one is a specifier and the other is not */
2057 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2058 (IS_SPEC (dest) && !IS_SPEC (src)))
2061 /* if one of them is a void then ok */
2062 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2065 /* if they are both bitfields then if the lengths
2066 and starts don't match */
2067 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2068 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2069 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2072 if (IS_INTEGRAL (dest))
2074 /* signedness must match */
2075 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2077 /* size must match */
2078 if (SPEC_LONG (dest) != SPEC_LONG (src))
2080 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2084 if (IS_STRUCT (dest))
2086 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2090 if (SPEC_CONST (dest) != SPEC_CONST (src))
2092 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2094 if (SPEC_STAT (dest) != SPEC_STAT (src))
2096 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2098 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2101 destScls = SPEC_SCLS (dest);
2102 srcScls = SPEC_SCLS (src);
2104 /* Compensate for const to const code change in checkSClass() */
2105 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2107 if (srcScls == S_CODE && destScls == S_FIXED)
2109 if (destScls == S_CODE && srcScls == S_FIXED)
2113 /* compensate for allocGlobal() */
2114 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2115 && port->mem.default_globl_map == xdata
2119 if (level>0 && !SPEC_STAT (dest))
2121 /* Compensate for hack-o-matic in checkSClass() */
2122 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2124 if (destScls == S_FIXED)
2125 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2126 if (srcScls == S_FIXED)
2127 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2129 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2131 if (destScls == S_FIXED)
2133 if (srcScls == S_FIXED)
2138 if (srcScls != destScls)
2141 printf ("level = %d\n", level);
2142 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2143 SPEC_SCLS (src), SPEC_SCLS (dest));
2144 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2152 /*------------------------------------------------------------------*/
2153 /* inCalleeSaveList - return 1 if found in callee save list */
2154 /*------------------------------------------------------------------*/
2156 calleeCmp(void *p1, void *p2)
2158 return (strcmp((char *)p1, (char *)(p2)) == 0);
2162 inCalleeSaveList(char *s)
2164 if (options.all_callee_saves)
2166 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2169 /*-----------------------------------------------------------------*/
2170 /* aggregateToPointer: change an agggregate type function */
2171 /* argument to a pointer to that type. */
2172 /*-----------------------------------------------------------------*/
2174 aggregateToPointer (value * val)
2176 if (IS_AGGREGATE (val->type))
2178 /* if this is a structure */
2179 /* then we need to add a new link */
2180 if (IS_STRUCT (val->type))
2182 /* first lets add DECLARATOR type */
2183 sym_link *p = val->type;
2185 werror (W_STRUCT_AS_ARG, val->name);
2186 val->type = newLink (DECLARATOR);
2187 val->type->next = p;
2190 /* change to a pointer depending on the */
2191 /* storage class specified */
2192 switch (SPEC_SCLS (val->etype))
2195 DCL_TYPE (val->type) = IPOINTER;
2198 DCL_TYPE (val->type) = PPOINTER;
2201 if (SPEC_OCLS(val->etype)) {
2202 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2204 // this happens for (external) function parameters
2205 DCL_TYPE (val->type) = port->unqualified_pointer;
2211 DCL_TYPE (val->type) = POINTER;
2214 DCL_TYPE (val->type) = CPOINTER;
2217 DCL_TYPE (val->type) = FPOINTER;
2220 DCL_TYPE (val->type) = EEPPOINTER;
2223 DCL_TYPE (val->type) = port->unqualified_pointer;
2226 /* is there is a symbol associated then */
2227 /* change the type of the symbol as well */
2230 val->sym->type = copyLinkChain (val->type);
2231 val->sym->etype = getSpec (val->sym->type);
2236 /*------------------------------------------------------------------*/
2237 /* checkFunction - does all kinds of check on a function */
2238 /*------------------------------------------------------------------*/
2240 checkFunction (symbol * sym, symbol *csym)
2242 value *exargs, *acargs;
2246 if (getenv("DEBUG_SANITY")) {
2247 fprintf (stderr, "checkFunction: %s ", sym->name);
2250 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2252 werror(E_SYNTAX_ERROR, sym->name);
2256 /* make sure the type is complete and sane */
2257 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2259 /* if not type then some kind of error */
2263 /* if the function has no type then make it return int */
2264 if (!sym->type->next)
2265 sym->type->next = sym->etype = newIntLink ();
2267 /* function cannot return aggregate */
2268 if (IS_AGGREGATE (sym->type->next))
2270 werror (E_FUNC_AGGR, sym->name);
2274 /* function cannot return bit */
2275 if (IS_BITVAR (sym->type->next))
2277 werror (E_FUNC_BIT, sym->name);
2281 /* check if this function is defined as calleeSaves
2282 then mark it as such */
2283 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2285 /* if interrupt service routine */
2286 /* then it cannot have arguments */
2287 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2289 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2290 werror (E_INT_ARGS, sym->name);
2291 FUNC_ARGS(sym->type)=NULL;
2295 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2297 acargs=acargs->next, argCnt++) {
2299 // this can happen for reentrant functions
2300 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2301 // the show must go on: synthesize a name and symbol
2302 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2303 acargs->sym = newSymbol (acargs->name, 1);
2304 SPEC_OCLS (acargs->etype) = istack;
2305 acargs->sym->type = copyLinkChain (acargs->type);
2306 acargs->sym->etype = getSpec (acargs->sym->type);
2307 acargs->sym->_isparm = 1;
2308 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2309 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2311 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2316 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2317 return 1; /* not defined nothing more to check */
2319 /* check if body already present */
2320 if (csym && IFFUNC_HASBODY(csym->type))
2322 werror (E_FUNC_BODY, sym->name);
2326 /* check the return value type */
2327 if (compareType (csym->type, sym->type) <= 0)
2329 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2330 printFromToType(csym->type, sym->type);
2334 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2336 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2339 /* I don't think this is necessary for interrupts. An isr is a */
2340 /* root in the calling tree. */
2341 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2342 (!FUNC_ISISR (sym->type)))
2344 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2347 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2349 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2352 /* Really, reentrant should match regardless of argCnt, but */
2353 /* this breaks some existing code (the fp lib functions). If */
2354 /* the first argument is always passed the same way, this */
2355 /* lax checking is ok (but may not be true for in future ports) */
2356 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2359 //printf("argCnt = %d\n",argCnt);
2360 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2363 /* compare expected args with actual args */
2364 exargs = FUNC_ARGS(csym->type);
2365 acargs = FUNC_ARGS(sym->type);
2367 /* for all the expected args do */
2370 exargs = exargs->next, acargs = acargs->next, argCnt++)
2372 if (getenv("DEBUG_SANITY")) {
2373 fprintf (stderr, "checkFunction: %s ", exargs->name);
2375 /* make sure the type is complete and sane */
2376 checkTypeSanity(exargs->etype, exargs->name);
2378 /* If the actual argument is an array, any prototype
2379 * will have modified it to a pointer. Duplicate that
2382 if (IS_AGGREGATE (acargs->type))
2384 checkValue = copyValue (acargs);
2385 aggregateToPointer (checkValue);
2389 checkValue = acargs;
2392 if (compareType (exargs->type, checkValue->type) <= 0)
2394 werror (E_ARG_TYPE, argCnt);
2395 printFromToType(exargs->type, checkValue->type);
2400 /* if one them ended we have a problem */
2401 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2402 (!exargs && acargs && !IS_VOID (acargs->type)))
2403 werror (E_ARG_COUNT);
2405 /* replace with this defition */
2406 sym->cdef = csym->cdef;
2407 deleteSym (SymbolTab, csym, csym->name);
2408 deleteFromSeg(csym);
2409 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2410 if (IS_EXTERN (csym->etype) && !
2411 IS_EXTERN (sym->etype))
2413 addSet (&publics, sym);
2418 /*------------------------------------------------------------------*/
2419 /* cdbStructBlock - calls struct printing for a blcks */
2420 /*------------------------------------------------------------------*/
2421 void cdbStructBlock (int block)
2424 bucket **table = StructTab;
2427 /* go thru the entire table */
2428 for (i = 0; i < 256; i++)
2430 for (chain = table[i]; chain; chain = chain->next)
2432 if (chain->block >= block)
2435 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2441 /*-----------------------------------------------------------------*/
2442 /* processFuncArgs - does some processing with function args */
2443 /*-----------------------------------------------------------------*/
2445 processFuncArgs (symbol * func)
2449 sym_link *funcType=func->type;
2451 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2452 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2454 /* find the function declaration within the type */
2455 while (funcType && !IS_FUNC(funcType))
2456 funcType=funcType->next;
2458 /* if this function has variable argument list */
2459 /* then make the function a reentrant one */
2460 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2461 FUNC_ISREENT(funcType)=1;
2463 /* check if this function is defined as calleeSaves
2464 then mark it as such */
2465 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2467 /* loop thru all the arguments */
2468 val = FUNC_ARGS(funcType);
2470 /* if it is void then remove parameters */
2471 if (val && IS_VOID (val->type))
2473 FUNC_ARGS(funcType) = NULL;
2477 /* reset regparm for the port */
2478 (*port->reset_regparms) ();
2480 /* if any of the arguments is an aggregate */
2481 /* change it to pointer to the same type */
2485 char buffer[SDCC_NAME_MAX+1];
2487 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2488 checkTypeSanity (val->etype, buffer);
2490 /* mark it as a register parameter if
2491 the function does not have VA_ARG
2492 and as port dictates */
2493 if (!IFFUNC_HASVARARGS(funcType) &&
2494 (argreg = (*port->reg_parm) (val->type)))
2496 SPEC_REGPARM (val->etype) = 1;
2497 SPEC_ARGREG(val->etype) = argreg;
2498 } else if (IFFUNC_ISREENT(funcType)) {
2499 FUNC_HASSTACKPARM(funcType) = 1;
2502 if (IS_AGGREGATE (val->type))
2504 aggregateToPointer (val);
2511 /* if this is an internal generated function call */
2513 /* ignore --stack-auto for this one, we don't know how it is compiled */
2514 /* simply trust on --int-long-reent or --float-reent */
2515 if (IFFUNC_ISREENT(funcType)) {
2519 /* if this function is reentrant or */
2520 /* automatics r 2b stacked then nothing */
2521 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2525 val = FUNC_ARGS(funcType);
2530 /* if a symbolname is not given */
2531 /* synthesize a variable name */
2534 SNPRINTF (val->name, sizeof(val->name),
2535 "_%s_PARM_%d", func->name, pNum++);
2536 val->sym = newSymbol (val->name, 1);
2537 if (SPEC_SCLS(val->etype) == S_BIT)
2538 SPEC_OCLS (val->etype) = bit;
2540 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2541 val->sym->type = copyLinkChain (val->type);
2542 val->sym->etype = getSpec (val->sym->type);
2543 val->sym->_isparm = 1;
2544 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2546 /* ?? static functions shouldn't imply static parameters - EEP */
2547 if (IS_SPEC(func->etype)) {
2548 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2549 SPEC_STAT (func->etype);
2552 addSymChain (val->sym);
2555 else /* symbol name given create synth name */
2558 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2559 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2560 val->sym->_isparm = 1;
2561 if (SPEC_SCLS(val->etype) == S_BIT)
2562 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2564 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2565 (options.model != MODEL_SMALL ? xdata : data);
2568 /* ?? static functions shouldn't imply static parameters - EEP */
2569 if (IS_SPEC(func->etype)) {
2570 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2571 SPEC_STAT (func->etype);
2575 if (!isinSet(operKeyReset, val->sym)) {
2576 addSet (&operKeyReset, val->sym);
2577 applyToSet (operKeyReset, resetParmKey);
2583 /*-----------------------------------------------------------------*/
2584 /* isSymbolEqual - compares two symbols return 1 if they match */
2585 /*-----------------------------------------------------------------*/
2587 isSymbolEqual (symbol * dest, symbol * src)
2589 /* if pointers match then equal */
2593 /* if one of them is null then don't match */
2597 /* if both of them have rname match on rname */
2598 if (dest->rname[0] && src->rname[0])
2599 return (!strcmp (dest->rname, src->rname));
2601 /* otherwise match on name */
2602 return (!strcmp (dest->name, src->name));
2605 void PT(sym_link *type)
2607 printTypeChain(type,0);
2609 /*-----------------------------------------------------------------*/
2610 /* printTypeChain - prints the type chain in human readable form */
2611 /*-----------------------------------------------------------------*/
2613 printTypeChain (sym_link * start, FILE * of)
2617 sym_link * type, * search;
2627 fprintf (of, "void");
2631 /* Print the chain as it is written in the source: */
2632 /* start with the last entry. */
2633 /* However, the storage class at the end of the */
2634 /* chain reall applies to the first in the chain! */
2636 for (type = start; type && type->next; type = type->next)
2639 scls=SPEC_SCLS(type);
2647 case S_DATA: fprintf (of, "data-"); break;
2648 case S_XDATA: fprintf (of, "xdata-"); break;
2649 case S_SFR: fprintf (of, "sfr-"); break;
2650 case S_SBIT: fprintf (of, "sbit-"); break;
2651 case S_CODE: fprintf (of, "code-"); break;
2652 case S_IDATA: fprintf (of, "idata-"); break;
2653 case S_PDATA: fprintf (of, "pdata-"); break;
2654 case S_LITERAL: fprintf (of, "literal-"); break;
2655 case S_STACK: fprintf (of, "stack-"); break;
2656 case S_XSTACK: fprintf (of, "xstack-"); break;
2657 case S_BIT: fprintf (of, "bit-"); break;
2658 case S_EEPROM: fprintf (of, "eeprom-"); break;
2665 if (!IS_FUNC(type)) {
2666 if (DCL_PTR_VOLATILE (type)) {
2667 fprintf (of, "volatile-");
2669 if (DCL_PTR_CONST (type)) {
2670 fprintf (of, "const-");
2673 switch (DCL_TYPE (type))
2676 fprintf (of, "function %s %s",
2677 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2678 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2680 for (args = FUNC_ARGS(type);
2683 printTypeChain(args->type, of);
2690 fprintf (of, "generic* ");
2693 fprintf (of, "code* ");
2696 fprintf (of, "xdata* ");
2699 fprintf (of, "eeprom* ");
2702 fprintf (of, "near* ");
2705 fprintf (of, "idata* ");
2708 fprintf (of, "pdata* ");
2711 fprintf (of, "unknown* ");
2714 if (DCL_ELEM(type)) {
2715 fprintf (of, "[%d] ", DCL_ELEM(type));
2717 fprintf (of, "[] ");
2724 if (SPEC_VOLATILE (type))
2725 fprintf (of, "volatile-");
2726 if (SPEC_CONST (type))
2727 fprintf (of, "const-");
2728 if (SPEC_USIGN (type))
2729 fprintf (of, "unsigned-");
2730 switch (SPEC_NOUN (type))
2734 fprintf (of, "long-");
2735 fprintf (of, "int");
2739 fprintf (of, "char");
2743 fprintf (of, "void");
2747 fprintf (of, "float");
2751 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2755 fprintf (of, "sbit");
2759 fprintf (of, "bit");
2763 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2767 fprintf (of, "double");
2771 fprintf (of, "unknown type");
2775 /* search entry in list before "type" */
2776 for (search = start; search && search->next != type;)
2777 search = search->next;
2786 /*--------------------------------------------------------------------*/
2787 /* printTypeChainRaw - prints the type chain in human readable form */
2788 /* in the raw data structure ordering */
2789 /*--------------------------------------------------------------------*/
2791 printTypeChainRaw (sym_link * start, FILE * of)
2804 fprintf (of, "void");
2814 if (!IS_FUNC(type)) {
2815 if (DCL_PTR_VOLATILE (type)) {
2816 fprintf (of, "volatile-");
2818 if (DCL_PTR_CONST (type)) {
2819 fprintf (of, "const-");
2822 switch (DCL_TYPE (type))
2825 fprintf (of, "function %s %s",
2826 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2827 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2829 for (args = FUNC_ARGS(type);
2832 printTypeChain(args->type, of);
2839 fprintf (of, "generic* ");
2842 fprintf (of, "code* ");
2845 fprintf (of, "xdata* ");
2848 fprintf (of, "eeprom* ");
2851 fprintf (of, "near* ");
2854 fprintf (of, "idata* ");
2857 fprintf (of, "pdata* ");
2860 fprintf (of, "unknown* ");
2863 if (DCL_ELEM(type)) {
2864 fprintf (of, "[%d] ", DCL_ELEM(type));
2866 fprintf (of, "[] ");
2870 if (DCL_TSPEC(type))
2873 printTypeChainRaw(DCL_TSPEC(type), of);
2877 else if (IS_SPEC (type))
2879 switch (SPEC_SCLS (type))
2881 case S_DATA: fprintf (of, "data-"); break;
2882 case S_XDATA: fprintf (of, "xdata-"); break;
2883 case S_SFR: fprintf (of, "sfr-"); break;
2884 case S_SBIT: fprintf (of, "sbit-"); break;
2885 case S_CODE: fprintf (of, "code-"); break;
2886 case S_IDATA: fprintf (of, "idata-"); break;
2887 case S_PDATA: fprintf (of, "pdata-"); break;
2888 case S_LITERAL: fprintf (of, "literal-"); break;
2889 case S_STACK: fprintf (of, "stack-"); break;
2890 case S_XSTACK: fprintf (of, "xstack-"); break;
2891 case S_BIT: fprintf (of, "bit-"); break;
2892 case S_EEPROM: fprintf (of, "eeprom-"); break;
2895 if (SPEC_VOLATILE (type))
2896 fprintf (of, "volatile-");
2897 if (SPEC_CONST (type))
2898 fprintf (of, "const-");
2899 if (SPEC_USIGN (type))
2900 fprintf (of, "unsigned-");
2901 switch (SPEC_NOUN (type))
2905 fprintf (of, "long-");
2906 fprintf (of, "int");
2910 fprintf (of, "char");
2914 fprintf (of, "void");
2918 fprintf (of, "float");
2922 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2926 fprintf (of, "sbit");
2930 fprintf (of, "bit");
2934 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2938 fprintf (of, "double");
2942 fprintf (of, "unknown type");
2947 fprintf (of, "NOT_SPEC_OR_DECL");
2957 /*-----------------------------------------------------------------*/
2958 /* powof2 - returns power of two for the number if number is pow 2 */
2959 /*-----------------------------------------------------------------*/
2961 powof2 (TYPE_UDWORD num)
2974 if (n1s > 1 || nshifts == 0)
2990 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2991 symbol *__muldiv[3][3][2];
2992 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2993 sym_link *__multypes[3][2];
2994 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2995 symbol *__conv[2][3][2];
2996 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2997 symbol *__rlrr[2][3][2];
2999 sym_link *floatType;
3002 _mangleFunctionName(char *in)
3004 if (port->getMangledFunctionName)
3006 return port->getMangledFunctionName(in);
3014 /*-----------------------------------------------------------------*/
3015 /* typeFromStr - create a typechain from an encoded string */
3016 /* basic types - 'c' - char */
3022 /* '*' - pointer - default (GPOINTER) */
3023 /* modifiers - 'u' - unsigned */
3024 /* pointer modifiers - 'g' - generic */
3028 /* 'F' - function */
3029 /* examples : "ig*" - generic int * */
3030 /* "cx*" - char xdata * */
3031 /* "ui" - unsigned int */
3032 /*-----------------------------------------------------------------*/
3033 sym_link *typeFromStr (char *s)
3035 sym_link *r = newLink(DECLARATOR);
3047 r->class = SPECIFIER;
3048 SPEC_NOUN(r) = V_CHAR;
3052 r->class = SPECIFIER;
3053 SPEC_NOUN(r) = V_INT;
3056 r->class = SPECIFIER;
3057 SPEC_NOUN(r) = V_INT;
3061 r->class = SPECIFIER;
3062 SPEC_NOUN(r) = V_FLOAT;
3065 r->class = SPECIFIER;
3066 SPEC_NOUN(r) = V_VOID;
3069 DCL_TYPE(r) = port->unqualified_pointer;
3076 assert(*(s+1)=='*');
3077 nr = newLink(DECLARATOR);
3082 DCL_TYPE(r) = GPOINTER;
3085 DCL_TYPE(r) = FPOINTER;
3088 DCL_TYPE(r) = CPOINTER;
3091 DCL_TYPE(r) = POINTER;
3094 DCL_TYPE(r) = FUNCTION;
3095 nr = newLink(DECLARATOR);
3098 DCL_TYPE(r) = CPOINTER;
3104 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3105 "typeFromStr: unknown type");
3108 if (IS_SPEC(r) && usign) {
3117 /*-----------------------------------------------------------------*/
3118 /* initCSupport - create functions for C support routines */
3119 /*-----------------------------------------------------------------*/
3123 const char *smuldivmod[] =
3127 const char *sbwd[] =
3129 "char", "int", "long"
3135 const char *srlrr[] =
3140 int bwd, su, muldivmod, tofrom, rlrr;
3142 if (getenv("SDCC_NO_C_SUPPORT")) {
3143 /* for debugging only */
3147 floatType = newFloatLink ();
3149 for (bwd = 0; bwd < 3; bwd++)
3166 __multypes[bwd][0] = l;
3167 __multypes[bwd][1] = copyLinkChain (l);
3168 SPEC_USIGN (__multypes[bwd][1]) = 1;
3171 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3172 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3173 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3174 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3175 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3176 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3177 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3178 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3179 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3180 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3182 for (tofrom = 0; tofrom < 2; tofrom++)
3184 for (bwd = 0; bwd < 3; bwd++)
3186 for (su = 0; su < 2; su++)
3190 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3191 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3195 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3196 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3203 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3205 for (bwd = 0; bwd < 3; bwd++)
3207 for (su = 0; su < 2; su++)
3209 SNPRINTF (buffer, sizeof(buffer),
3211 smuldivmod[muldivmod],
3214 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3215 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3220 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3221 Therefore they've been merged into mulint() and mullong().
3224 for (bwd = 0; bwd < 3; bwd++)
3226 for (su = 0; su < 2; su++)
3228 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3231 SNPRINTF (buffer, sizeof(buffer),
3233 smuldivmod[muldivmod],
3236 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3237 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3245 for (su = 0; su < 2; su++)
3247 /* muluchar and mulschar are still separate functions, because e.g. the z80
3248 port is sign/zero-extending to int before calling mulint() */
3249 SNPRINTF (buffer, sizeof(buffer),
3251 smuldivmod[muldivmod],
3254 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3255 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3259 /* word and doubleword */
3260 for (bwd = 1; bwd < 3; bwd++)
3263 SNPRINTF (buffer, sizeof(buffer),
3265 smuldivmod[muldivmod],
3267 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3268 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3269 /* signed = unsigned */
3270 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3273 for (rlrr = 0; rlrr < 2; rlrr++)
3275 for (bwd = 0; bwd < 3; bwd++)
3277 for (su = 0; su < 2; su++)
3279 SNPRINTF (buffer, sizeof(buffer),
3284 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3285 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3291 /*-----------------------------------------------------------------*/
3292 /* initBuiltIns - create prototypes for builtin functions */
3293 /*-----------------------------------------------------------------*/
3299 if (!port->builtintable) return ;
3301 for (i = 0 ; port->builtintable[i].name ; i++) {
3302 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3303 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3304 FUNC_ISBUILTIN(sym->type) = 1;
3305 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3309 sym_link *validateLink(sym_link *l,
3316 if (l && l->class==select)
3321 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3322 " expected %s, got %s\n",
3323 macro, args, file, line,
3324 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3326 return l; // never reached, makes compiler happy.
3329 /*--------------------------------------------------------------------*/
3330 /* newEnumType - create an integer type compatible with enumerations */
3331 /*--------------------------------------------------------------------*/
3333 newEnumType (symbol *enumlist)
3341 type = newLink (SPECIFIER);
3342 SPEC_NOUN (type) = V_INT;
3346 /* Determine the range of the enumerated values */
3348 min = max = (int) floatFromVal (valFromType (sym->type));
3349 for (sym = sym->next; sym; sym = sym->next)
3351 v = (int) floatFromVal (valFromType (sym->type));
3358 /* Determine the smallest integer type that is compatible with this range */
3359 type = newLink (SPECIFIER);
3360 if (min>=0 && max<=255)
3362 SPEC_NOUN (type) = V_CHAR;
3363 SPEC_USIGN (type) = 1;
3365 else if (min>=-128 && max<=127)
3367 SPEC_NOUN (type) = V_CHAR;
3369 else if (min>=0 && max<=65535)
3371 SPEC_NOUN (type) = V_INT;
3372 SPEC_USIGN (type) = 1;
3374 else if (min>=-32768 && max<=32767)
3376 SPEC_NOUN (type) = V_INT;
3380 SPEC_NOUN (type) = V_INT;
3381 SPEC_LONG (type) = 1;
3383 SPEC_USIGN (type) = 1;