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;
1015 for (; sym != NULL; sym = sym->next)
1017 changePointer(sym->type);
1018 checkTypeSanity(sym->etype, sym->name);
1020 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
1023 /* if already exists in the symbol table then check if
1024 one of them is an extern definition if yes then
1025 then check if the type match, if the types match then
1026 delete the current entry and add the new entry */
1027 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
1028 csym->level == sym->level) {
1030 /* If the previous definition was for an array with incomplete */
1031 /* type, and the new definition has completed the type, update */
1032 /* the original type to match */
1033 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
1034 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1036 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1037 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1041 /* If only one of the definitions used the "at" keyword, copy */
1042 /* the address to the other. */
1043 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1044 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1046 SPEC_ABSA (sym->etype) = 1;
1047 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1049 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1050 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1052 SPEC_ABSA (csym->etype) = 1;
1053 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1058 if (csym->ival && sym->ival)
1060 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1064 /* one definition extern ? */
1065 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1066 werror (E_EXTERN_MISMATCH, sym->name);
1068 werror (E_DUPLICATE, sym->name);
1069 werrorfl (csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
1071 fprintf (stderr, "from type '");
1072 printTypeChain (csym->type, stderr);
1073 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1074 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1075 fprintf (stderr, "'\nto type '");
1076 printTypeChain (sym->type, stderr);
1077 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1078 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1079 fprintf (stderr, "'\n");
1084 if (csym->ival && !sym->ival)
1085 sym->ival = csym->ival;
1087 /* delete current entry */
1088 deleteSym (SymbolTab, csym, csym->name);
1089 deleteFromSeg(csym);
1091 symPtrPtr = symHead;
1092 while (*symPtrPtr && *symPtrPtr != csym)
1093 symPtrPtr = &(*symPtrPtr)->next;
1094 if (*symPtrPtr == csym)
1095 *symPtrPtr = csym->next;
1100 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1105 /*------------------------------------------------------------------*/
1106 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1107 /*------------------------------------------------------------------*/
1109 funcInChain (sym_link * lnk)
1120 /*------------------------------------------------------------------*/
1121 /* structElemType - returns the type info of a struct member */
1122 /*------------------------------------------------------------------*/
1124 structElemType (sym_link * stype, value * id)
1126 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1127 sym_link *type, *etype;
1128 sym_link *petype = getSpec (stype);
1132 /* look for the id */
1135 if (strcmp (fields->rname, id->name) == 0)
1137 type = copyLinkChain (fields->type);
1138 etype = getSpec (type);
1139 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1140 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1142 SPEC_CONST (type) |= SPEC_CONST (stype);
1144 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1147 fields = fields->next;
1151 werror (E_NOT_MEMBER, id->name);
1153 // the show must go on
1154 return newIntLink();
1157 /*------------------------------------------------------------------*/
1158 /* getStructElement - returns element of a tructure definition */
1159 /*------------------------------------------------------------------*/
1161 getStructElement (structdef * sdef, symbol * sym)
1165 for (field = sdef->fields; field; field = field->next)
1166 if (strcmp (field->name, sym->name) == 0)
1169 werror (E_NOT_MEMBER, sym->name);
1171 return sdef->fields;
1174 /*------------------------------------------------------------------*/
1175 /* compStructSize - computes the size of a structure */
1176 /*------------------------------------------------------------------*/
1178 compStructSize (int su, structdef * sdef)
1180 int sum = 0, usum = 0;
1184 /* for the identifiers */
1185 loop = sdef->fields;
1188 /* create the internal name for this variable */
1189 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1194 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1196 /* if this is a bit field */
1199 /* change it to a unsigned bit */
1200 SPEC_NOUN (loop->etype) = V_BITFIELD;
1201 SPEC_USIGN (loop->etype) = 1;
1202 SPEC_BLEN (loop->etype) = loop->bitVar;
1204 if (loop->bitVar == BITVAR_PAD) {
1205 /* A zero length bitfield forces padding */
1206 SPEC_BSTR (loop->etype) = bitOffset;
1207 SPEC_BLEN (loop->etype) = 0;
1212 if (bitOffset == 8) {
1216 /* check if this fit into the remaining */
1217 /* bits of this byte else align it to the */
1218 /* next byte boundary */
1219 if (loop->bitVar <= (8 - bitOffset)) {
1220 /* fits into current byte */
1222 SPEC_BSTR (loop->etype) = bitOffset;
1223 bitOffset += loop->bitVar;
1225 else if (!bitOffset) {
1226 /* does not fit, but is already byte aligned */
1228 SPEC_BSTR (loop->etype) = bitOffset;
1229 bitOffset += loop->bitVar;
1232 /* does not fit; need to realign first */
1234 loop->offset = (su == UNION ? sum = 0 : sum);
1236 SPEC_BSTR (loop->etype) = bitOffset;
1237 bitOffset += loop->bitVar;
1239 while (bitOffset>8) {
1246 /* This is a non-bit field. Make sure we are */
1247 /* byte aligned first */
1250 loop->offset = (su == UNION ? sum = 0 : sum);
1254 checkDecl (loop, 1);
1255 sum += getSize (loop->type);
1260 /* if union then size = sizeof largest field */
1262 /* For UNION, round up after each field */
1263 sum += ((bitOffset+7)/8);
1264 usum = max (usum, sum);
1269 /* For STRUCT, round up after all fields processed */
1271 sum += ((bitOffset+7)/8);
1273 return (su == UNION ? usum : sum);
1276 /*-------------------------------------------------------------------*/
1277 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1278 /* an enclosing struct/union */
1279 /*-------------------------------------------------------------------*/
1281 promoteAnonStructs (int su, structdef * sdef)
1290 tofield = &sdef->fields;
1291 field = sdef->fields;
1294 nextfield = field->next;
1295 if (!*field->name && IS_STRUCT (field->type))
1297 /* Found an anonymous struct/union. Replace it */
1298 /* with the fields it contains and adjust all */
1301 base = field->offset;
1302 subfield = copySymbolChain (SPEC_STRUCT (field->type)->fields);
1304 continue; /* just in case it's empty */
1306 *tofield = subfield;
1309 /* check for field name conflicts resulting from promotion */
1310 dupfield = sdef->fields;
1311 while (dupfield && dupfield != subfield)
1313 if (*subfield->name && !strcmp (dupfield->name, subfield->name))
1315 werrorfl (subfield->fileDef, subfield->lineDef,
1317 su==STRUCT ? "struct" : "union",
1319 werrorfl (dupfield->fileDef, dupfield->lineDef,
1322 dupfield = dupfield->next;
1325 subfield->offset += base;
1327 subfield = subfield->next;
1331 subfield->next = nextfield;
1332 tofield = &subfield->next;
1335 tofield = &field->next;
1341 /*------------------------------------------------------------------*/
1342 /* checkSClass - check the storage class specification */
1343 /*------------------------------------------------------------------*/
1345 checkSClass (symbol * sym, int isProto)
1349 if (getenv("DEBUG_SANITY")) {
1350 fprintf (stderr, "checkSClass: %s \n", sym->name);
1353 /* type is literal can happen for enums change
1355 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1356 SPEC_SCLS (sym->etype) = S_AUTO;
1358 /* if sfr or sbit then must also be volatile */
1359 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1360 SPEC_SCLS (sym->etype) == S_SFR)
1362 SPEC_VOLATILE (sym->etype) = 1;
1365 /* if absolute address given then it mark it as
1366 volatile -- except in the PIC port */
1368 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1369 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1370 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1373 if (IS_ABSOLUTE (sym->etype))
1374 SPEC_VOLATILE (sym->etype) = 1;
1376 if (TARGET_IS_MCS51 &&
1377 IS_ABSOLUTE (sym->etype) &&
1378 SPEC_SCLS (sym->etype) == S_SFR)
1383 if (SPEC_NOUN (sym->etype) == V_CHAR)
1385 else if (SPEC_LONG (sym->etype) == 0)
1390 addr = SPEC_ADDR (sym->etype);
1391 for (n=0; n<size; n+=8)
1392 if (((addr >> n) & 0xFF) < 0x80)
1393 werror (W_SFR_ABSRANGE, sym->name);
1396 /* If code memory is read only, then pointers to code memory */
1397 /* implicitly point to constants -- make this explicit */
1399 while (t && t->next) {
1400 if (IS_CODEPTR(t) && port->mem.code_ro) {
1401 if (IS_SPEC(t->next)) {
1402 SPEC_CONST (t->next) = 1;
1404 DCL_PTR_CONST (t->next) = 1;
1410 /* global variables declared const put into code */
1411 /* if no other storage class specified */
1412 if (sym->level == 0 &&
1413 SPEC_SCLS(sym->etype) == S_FIXED &&
1414 !IS_FUNC(sym->type)) {
1415 /* find the first non-array link */
1419 if (IS_CONSTANT (t)) {
1420 SPEC_SCLS (sym->etype) = S_CODE;
1424 /* global variable in code space is a constant */
1425 if (sym->level == 0 &&
1426 SPEC_SCLS (sym->etype) == S_CODE &&
1427 port->mem.code_ro) {
1428 /* find the first non-array link */
1435 DCL_PTR_CONST (t) = 1;
1439 /* if bit variable then no storage class can be */
1440 /* specified since bit is already a storage */
1441 if (IS_BITVAR (sym->etype) &&
1442 (SPEC_SCLS (sym->etype) != S_FIXED &&
1443 SPEC_SCLS (sym->etype) != S_SBIT &&
1444 SPEC_SCLS (sym->etype) != S_BIT)
1447 werror (E_BITVAR_STORAGE, sym->name);
1448 SPEC_SCLS (sym->etype) = S_FIXED;
1451 /* extern variables cannot be initialized */
1452 if (IS_EXTERN (sym->etype) && sym->ival)
1454 werror (E_EXTERN_INIT, sym->name);
1458 /* if this is an automatic symbol */
1459 if (sym->level && (options.stackAuto || reentrant)) {
1460 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1461 SPEC_SCLS (sym->etype) == S_FIXED ||
1462 SPEC_SCLS (sym->etype) == S_REGISTER ||
1463 SPEC_SCLS (sym->etype) == S_STACK ||
1464 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1465 SPEC_SCLS (sym->etype) = S_AUTO;
1467 /* storage class may only be specified for statics */
1468 if (!IS_STATIC(sym->etype)) {
1469 werror (E_AUTO_ASSUMED, sym->name);
1474 /* automatic symbols cannot be given */
1475 /* an absolute address ignore it */
1477 SPEC_ABSA (sym->etype) &&
1478 (options.stackAuto || reentrant))
1480 werror (E_AUTO_ABSA, sym->name);
1481 SPEC_ABSA (sym->etype) = 0;
1484 /* arrays & pointers cannot be defined for bits */
1485 /* SBITS or SFRs or BIT */
1486 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1487 (SPEC_NOUN (sym->etype) == V_BIT ||
1488 SPEC_NOUN (sym->etype) == V_SBIT ||
1489 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1490 SPEC_SCLS (sym->etype) == S_SFR))
1491 werror (E_BIT_ARRAY, sym->name);
1493 /* if this is a bit|sbit then set length & start */
1494 if (SPEC_NOUN (sym->etype) == V_BIT ||
1495 SPEC_NOUN (sym->etype) == V_SBIT)
1497 SPEC_BLEN (sym->etype) = 1;
1498 SPEC_BSTR (sym->etype) = 0;
1502 /* variables declared in CODE space must have */
1503 /* initializers if not an extern */
1504 if (SPEC_SCLS (sym->etype) == S_CODE &&
1505 sym->ival == NULL &&
1508 port->mem.code_ro &&
1509 !IS_EXTERN (sym->etype) &&
1510 !funcInChain (sym->type))
1511 werror (E_CODE_NO_INIT, sym->name);
1514 /* if parameter or local variable then change */
1515 /* the storage class to reflect where the var will go */
1516 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED
1517 && !IS_STATIC(sym->etype)
1520 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1522 SPEC_SCLS (sym->etype) = (options.useXstack ?
1523 S_XSTACK : S_STACK);
1527 /* hack-o-matic! I see no reason why the useXstack option should ever
1528 * control this allocation, but the code was originally that way, and
1529 * changing it for non-390 ports breaks the compiler badly.
1531 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1532 1 : options.useXstack;
1533 SPEC_SCLS (sym->etype) = (useXdata ?
1539 /*------------------------------------------------------------------*/
1540 /* changePointer - change pointer to functions */
1541 /*------------------------------------------------------------------*/
1543 changePointer (sym_link * p)
1546 /* go thru the chain of declarations */
1547 /* if we find a pointer to a function */
1548 /* unconditionally change it to a ptr */
1550 for (; p; p = p->next)
1552 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1553 DCL_TYPE (p) = port->unqualified_pointer;
1554 if (IS_PTR (p) && IS_FUNC (p->next))
1555 DCL_TYPE (p) = CPOINTER;
1559 /*------------------------------------------------------------------*/
1560 /* checkDecl - does semantic validation of a declaration */
1561 /*------------------------------------------------------------------*/
1563 checkDecl (symbol * sym, int isProto)
1566 checkSClass (sym, isProto); /* check the storage class */
1567 changePointer (sym->type); /* change pointers if required */
1569 /* if this is an array without any dimension
1570 then update the dimension from the initial value */
1571 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1572 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1577 /*------------------------------------------------------------------*/
1578 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1579 /*------------------------------------------------------------------*/
1581 copyLinkChain (sym_link * p)
1583 sym_link *head, *curr, *loop;
1586 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1589 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1590 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1598 /*------------------------------------------------------------------*/
1599 /* cleanUpBlock - cleansup the symbol table specified for all the */
1600 /* symbols in the given block */
1601 /*------------------------------------------------------------------*/
1603 cleanUpBlock (bucket ** table, int block)
1608 /* go thru the entire table */
1609 for (i = 0; i < 256; i++)
1611 for (chain = table[i]; chain; chain = chain->next)
1613 if (chain->block >= block)
1615 deleteSym (table, chain->sym, chain->name);
1621 /*------------------------------------------------------------------*/
1622 /* cleanUpLevel - cleansup the symbol table specified for all the */
1623 /* symbols in the given level */
1624 /*------------------------------------------------------------------*/
1626 cleanUpLevel (bucket ** table, int level)
1631 /* go thru the entire table */
1632 for (i = 0; i < 256; i++)
1634 for (chain = table[i]; chain; chain = chain->next)
1636 if (chain->level >= level)
1638 deleteSym (table, chain->sym, chain->name);
1644 /*------------------------------------------------------------------*/
1645 /* computeTypeOr - computes the resultant type from two types */
1646 /*------------------------------------------------------------------*/
1648 computeTypeOr (sym_link * etype1, sym_link * etype2, sym_link * reType)
1651 assert ( (IS_CHAR (etype1) || IS_BIT (etype1))
1652 && (IS_CHAR (etype2) || IS_BIT (etype2)));
1654 if (SPEC_USIGN (etype1) == SPEC_USIGN (etype2))
1656 SPEC_USIGN (reType) = SPEC_USIGN (etype1);
1660 if (SPEC_USIGN (etype1))
1662 if ( IS_LITERAL (etype2)
1663 && floatFromVal (valFromType (etype2)) >= 0)
1664 SPEC_USIGN (reType) = 1;
1667 /* promote to int */
1668 SPEC_USIGN (reType) = 0;
1669 SPEC_NOUN (reType) = V_INT;
1672 else /* etype1 signed */
1674 if ( IS_LITERAL (etype2)
1675 && floatFromVal (valFromType (etype2)) <= 127)
1676 SPEC_USIGN (reType) = 0;
1679 /* promote to int */
1680 SPEC_USIGN (reType) = 0;
1681 SPEC_NOUN (reType) = V_INT;
1685 if (SPEC_USIGN (etype2))
1687 if ( IS_LITERAL (etype1)
1688 && floatFromVal (valFromType (etype1)) >= 0)
1689 SPEC_USIGN (reType) = 1;
1692 /* promote to int */
1693 SPEC_USIGN (reType) = 0;
1694 SPEC_NOUN (reType) = V_INT;
1697 else /* etype2 signed */
1699 if ( IS_LITERAL (etype1)
1700 && floatFromVal (valFromType (etype1)) <= 127)
1701 SPEC_USIGN (reType) = 0;
1704 /* promote to int */
1705 SPEC_USIGN (reType) = 0;
1706 SPEC_NOUN (reType) = V_INT;
1712 /*------------------------------------------------------------------*/
1713 /* computeType - computes the resultant type from two types */
1714 /*------------------------------------------------------------------*/
1716 computeType (sym_link * type1, sym_link * type2,
1717 RESULT_TYPE resultType, int op)
1721 sym_link *etype1 = getSpec (type1);
1724 etype2 = type2 ? getSpec (type2) : type1;
1726 /* if one of them is a float then result is a float */
1727 /* here we assume that the types passed are okay */
1728 /* and can be cast to one another */
1729 /* which ever is greater in size */
1730 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1731 rType = newFloatLink ();
1733 /* if both are bitvars choose the larger one */
1734 if (IS_BITVAR (etype1) && IS_BITVAR (etype2))
1736 rType = SPEC_BLEN (etype1) >= SPEC_BLEN (etype2) ?
1737 copyLinkChain (type1) : copyLinkChain (type1);
1739 /* if only one of them is a bit variable
1740 then the other one prevails */
1741 else if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1743 rType = copyLinkChain (type2);
1744 /* bitfield can have up to 16 bits */
1745 if (getSize (etype1) > 1)
1746 SPEC_NOUN (getSpec (rType)) = V_INT;
1748 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1750 rType = copyLinkChain (type1);
1751 /* bitfield can have up to 16 bits */
1752 if (getSize (etype2) > 1)
1753 SPEC_NOUN (getSpec (rType)) = V_INT;
1756 /* if one of them is a pointer or array then that
1758 if (IS_PTR (type1) || IS_ARRAY (type1))
1759 rType = copyLinkChain (type1);
1760 else if (IS_PTR (type2) || IS_ARRAY (type2))
1761 rType = copyLinkChain (type2);
1762 else if (getSize (type1) > getSize (type2))
1763 rType = copyLinkChain (type1);
1765 rType = copyLinkChain (type2);
1767 reType = getSpec (rType);
1769 /* avoid conflicting types */
1770 reType->select.s._signed = 0;
1772 /* if result is a literal then make not so */
1773 if (IS_LITERAL (reType))
1774 SPEC_SCLS (reType) = S_REGISTER;
1778 case RESULT_TYPE_CHAR:
1779 if (IS_BITVAR (reType))
1781 SPEC_NOUN (reType) = V_CHAR;
1782 SPEC_SCLS (reType) = 0;
1783 SPEC_USIGN (reType) = 0;
1787 case RESULT_TYPE_INT:
1788 case RESULT_TYPE_NONE:
1789 case RESULT_TYPE_OTHER:
1790 if (IS_BIT (reType))
1792 SPEC_NOUN (reType) = V_CHAR;
1793 SPEC_SCLS (reType) = 0;
1794 SPEC_USIGN (reType) = 0;
1797 else if (IS_BITFIELD (reType))
1799 /* could be smarter, but it depends on the op */
1800 /* this is for the worst case: a multiplication of 4 * 4 bit */
1801 SPEC_NOUN (reType) = SPEC_BLEN (reType) <= 4 ? V_CHAR : V_INT;
1802 SPEC_SCLS (reType) = 0;
1803 SPEC_USIGN (reType) = 0;
1806 else if (IS_CHAR (reType))
1808 if (op == '|' || op == '^')
1809 return computeTypeOr (etype1, etype2, reType);
1811 && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
1813 SPEC_USIGN (reType) = 1;
1818 SPEC_NOUN (reType) = V_INT;
1819 SPEC_USIGN (reType) = 0;
1822 /* TODO: should be in SDCCast.c */
1824 && ( !SPEC_USIGN (etype1)
1825 || !SPEC_USIGN (etype2)))
1827 SPEC_NOUN (reType) = V_INT;
1828 SPEC_USIGN (reType) = 0;
1837 /* SDCC's sign promotion:
1838 - if one or both operands are unsigned, the resultant type will be unsigned
1839 (except char, see below)
1840 - if an operand is promoted to a larger type (char -> int, int -> long),
1841 the larger type will be signed
1843 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1844 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1845 the standard. The standard demands, that the result has to be the same
1846 "as if" the promotion would have been performed:
1848 - if the result of an operation with two char's is promoted to a
1849 larger type, the result will be signed.
1851 More sophisticated are these:
1852 - if the result of an operation with two char's is a char again,
1853 the result will only then be unsigned, if both operands are
1854 unsigned. In all other cases the result will be signed.
1856 This seems to be contradictionary to the first two rules, but it makes
1857 real sense (all types are char's):
1859 A signed char can be negative; this must be preserved in the result
1862 Only if both operands are unsigned it's safe to make the result
1863 unsigned; this helps to avoid overflow:
1866 - ToDo: document '|', '^' and '&'
1868 Homework: - why is (200 * 200 < 0) true?
1869 - why is { char l = 200, r = 200; (r * l > 0) } true?
1872 if (!IS_FLOAT (reType)
1873 && ( (SPEC_USIGN (etype1)
1874 /* if this operand is promoted to a larger type,
1875 then it will be promoted to a signed type */
1876 && !(getSize (etype1) < getSize (reType))
1877 /* char require special handling */
1878 && !IS_CHAR (etype1))
1879 || /* same for 2nd operand */
1880 (SPEC_USIGN (etype2)
1881 && !(getSize (etype2) < getSize (reType))
1882 && !IS_CHAR (etype2))
1883 || /* if both are 'unsigned char' and not promoted
1884 let the result be unsigned too */
1885 ( SPEC_USIGN (etype1)
1886 && SPEC_USIGN (etype2)
1889 && IS_CHAR (reType))))
1890 SPEC_USIGN (reType) = 1;
1892 SPEC_USIGN (reType) = 0;
1897 /*--------------------------------------------------------------------*/
1898 /* compareType - will do type check return 1 if match, -1 if castable */
1899 /*--------------------------------------------------------------------*/
1901 compareType (sym_link * dest, sym_link * src)
1912 /* if dest is a declarator then */
1917 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1919 //checkFunction(src,dest);
1921 return compareType (dest->next, src->next);
1923 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1927 (IS_GENPTR (dest) ||
1928 ((DCL_TYPE(src) == POINTER) && (DCL_TYPE(dest) == IPOINTER))
1931 if (IS_PTR (dest) && IS_ARRAY (src)) {
1932 value *val=aggregateToPointer (valFromType(src));
1933 int res=compareType (dest, val->type);
1934 Safe_free(val->type);
1938 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1939 return compareType (dest->next, src);
1942 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1948 /* if one is a specifier and the other is not */
1949 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1950 (IS_SPEC (dest) && !IS_SPEC (src)))
1953 /* if one of them is a void then ok */
1954 if (SPEC_NOUN (dest) == V_VOID &&
1955 SPEC_NOUN (src) != V_VOID)
1958 if (SPEC_NOUN (dest) != V_VOID &&
1959 SPEC_NOUN (src) == V_VOID)
1962 /* if they are both bitfields then if the lengths
1963 and starts don't match */
1964 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1965 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1966 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1969 /* it is a specifier */
1970 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1972 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1973 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1975 bitsForType (dest) == bitsForType (src))
1976 instead of the next two lines, but the regression tests fail with
1977 them; I guess it's a problem with replaceCheaperOp */
1978 getSize (dest) == getSize (src) &&
1979 !(!IS_BIT (dest) && IS_BIT (src)))
1981 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1986 else if (IS_STRUCT (dest))
1988 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1993 if (SPEC_LONG (dest) != SPEC_LONG (src))
1996 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2002 /*--------------------------------------------------------------------*/
2003 /* compareTypeExact - will do type check return 1 if match exactly */
2004 /*--------------------------------------------------------------------*/
2006 compareTypeExact (sym_link * dest, sym_link * src, int level)
2008 STORAGE_CLASS srcScls, destScls;
2019 /* if dest is a declarator then */
2024 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
2025 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
2027 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
2029 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
2033 value *exargs, *acargs, *checkValue;
2035 /* verify function return type */
2036 if (!compareTypeExact (dest->next, src->next, -1))
2038 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
2040 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
2042 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
2045 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
2049 /* compare expected args with actual args */
2050 exargs = FUNC_ARGS(dest);
2051 acargs = FUNC_ARGS(src);
2053 /* for all the expected args do */
2054 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
2056 //checkTypeSanity(acargs->etype, acargs->name);
2058 if (IS_AGGREGATE (acargs->type))
2060 checkValue = copyValue (acargs);
2061 aggregateToPointer (checkValue);
2064 checkValue = acargs;
2067 if (!compareTypeExact (exargs->type, checkValue->type, -1))
2072 /* if one them ended we have a problem */
2073 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2074 (!exargs && acargs && !IS_VOID (acargs->type)))
2078 return compareTypeExact (dest->next, src->next, level);
2085 /* if one is a specifier and the other is not */
2086 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
2087 (IS_SPEC (dest) && !IS_SPEC (src)))
2090 /* if one of them is a void then ok */
2091 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
2094 /* if they are both bitfields then if the lengths
2095 and starts don't match */
2096 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
2097 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
2098 SPEC_BSTR (dest) != SPEC_BSTR (src)))
2101 if (IS_INTEGRAL (dest))
2103 /* signedness must match */
2104 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
2106 /* size must match */
2107 if (SPEC_LONG (dest) != SPEC_LONG (src))
2109 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
2113 if (IS_STRUCT (dest))
2115 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
2119 if (SPEC_CONST (dest) != SPEC_CONST (src))
2121 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
2123 if (SPEC_STAT (dest) != SPEC_STAT (src))
2125 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
2127 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
2130 destScls = SPEC_SCLS (dest);
2131 srcScls = SPEC_SCLS (src);
2133 /* Compensate for const to const code change in checkSClass() */
2134 if (!level & port->mem.code_ro && SPEC_CONST (dest))
2136 if (srcScls == S_CODE && destScls == S_FIXED)
2138 if (destScls == S_CODE && srcScls == S_FIXED)
2142 /* compensate for allocGlobal() */
2143 if ((srcScls == S_FIXED || srcScls == S_AUTO)
2144 && port->mem.default_globl_map == xdata
2148 if (level>0 && !SPEC_STAT (dest))
2150 /* Compensate for hack-o-matic in checkSClass() */
2151 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
2153 if (destScls == S_FIXED)
2154 destScls = (options.useXstack ? S_XSTACK : S_STACK);
2155 if (srcScls == S_FIXED)
2156 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
2158 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
2160 if (destScls == S_FIXED)
2162 if (srcScls == S_FIXED)
2167 if (srcScls != destScls)
2170 printf ("level = %d\n", level);
2171 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
2172 SPEC_SCLS (src), SPEC_SCLS (dest));
2173 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
2181 /*------------------------------------------------------------------*/
2182 /* inCalleeSaveList - return 1 if found in callee save list */
2183 /*------------------------------------------------------------------*/
2185 calleeCmp(void *p1, void *p2)
2187 return (strcmp((char *)p1, (char *)(p2)) == 0);
2191 inCalleeSaveList(char *s)
2193 if (options.all_callee_saves)
2195 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
2198 /*-----------------------------------------------------------------*/
2199 /* aggregateToPointer: change an agggregate type function */
2200 /* argument to a pointer to that type. */
2201 /*-----------------------------------------------------------------*/
2203 aggregateToPointer (value * val)
2205 if (IS_AGGREGATE (val->type))
2207 /* if this is a structure */
2208 /* then we need to add a new link */
2209 if (IS_STRUCT (val->type))
2211 /* first lets add DECLARATOR type */
2212 sym_link *p = val->type;
2214 werror (W_STRUCT_AS_ARG, val->name);
2215 val->type = newLink (DECLARATOR);
2216 val->type->next = p;
2219 /* change to a pointer depending on the */
2220 /* storage class specified */
2221 switch (SPEC_SCLS (val->etype))
2224 DCL_TYPE (val->type) = IPOINTER;
2227 DCL_TYPE (val->type) = PPOINTER;
2230 if (SPEC_OCLS(val->etype)) {
2231 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
2233 // this happens for (external) function parameters
2234 DCL_TYPE (val->type) = port->unqualified_pointer;
2240 DCL_TYPE (val->type) = POINTER;
2243 DCL_TYPE (val->type) = CPOINTER;
2246 DCL_TYPE (val->type) = FPOINTER;
2249 DCL_TYPE (val->type) = EEPPOINTER;
2252 DCL_TYPE (val->type) = port->unqualified_pointer;
2255 /* is there is a symbol associated then */
2256 /* change the type of the symbol as well */
2259 val->sym->type = copyLinkChain (val->type);
2260 val->sym->etype = getSpec (val->sym->type);
2265 /*------------------------------------------------------------------*/
2266 /* checkFunction - does all kinds of check on a function */
2267 /*------------------------------------------------------------------*/
2269 checkFunction (symbol * sym, symbol *csym)
2271 value *exargs, *acargs;
2275 if (getenv("DEBUG_SANITY")) {
2276 fprintf (stderr, "checkFunction: %s ", sym->name);
2279 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
2281 werror(E_SYNTAX_ERROR, sym->name);
2285 /* make sure the type is complete and sane */
2286 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2288 /* if not type then some kind of error */
2292 /* if the function has no type then make it return int */
2293 if (!sym->type->next)
2294 sym->type->next = sym->etype = newIntLink ();
2296 /* function cannot return aggregate */
2297 if (IS_AGGREGATE (sym->type->next))
2299 werror (E_FUNC_AGGR, sym->name);
2303 /* function cannot return bit */
2304 if (IS_BITVAR (sym->type->next))
2306 werror (E_FUNC_BIT, sym->name);
2310 /* check if this function is defined as calleeSaves
2311 then mark it as such */
2312 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2314 /* if interrupt service routine */
2315 /* then it cannot have arguments */
2316 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2318 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2319 werror (E_INT_ARGS, sym->name);
2320 FUNC_ARGS(sym->type)=NULL;
2324 if (IFFUNC_ISSHADOWREGS(sym->type) && !FUNC_ISISR (sym->type))
2326 werror (E_SHADOWREGS_NO_ISR, sym->name);
2330 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2332 acargs=acargs->next, argCnt++) {
2334 // this can happen for reentrant functions
2335 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2336 // the show must go on: synthesize a name and symbol
2337 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2338 acargs->sym = newSymbol (acargs->name, 1);
2339 SPEC_OCLS (acargs->etype) = istack;
2340 acargs->sym->type = copyLinkChain (acargs->type);
2341 acargs->sym->etype = getSpec (acargs->sym->type);
2342 acargs->sym->_isparm = 1;
2343 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2344 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2346 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2351 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2352 return 1; /* not defined nothing more to check */
2354 /* check if body already present */
2355 if (csym && IFFUNC_HASBODY(csym->type))
2357 werror (E_FUNC_BODY, sym->name);
2361 /* check the return value type */
2362 if (compareType (csym->type, sym->type) <= 0)
2364 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2365 printFromToType(csym->type, sym->type);
2369 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2371 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2374 /* I don't think this is necessary for interrupts. An isr is a */
2375 /* root in the calling tree. */
2376 if ((FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type)) &&
2377 (!FUNC_ISISR (sym->type)))
2379 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2382 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2384 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2387 /* Really, reentrant should match regardless of argCnt, but */
2388 /* this breaks some existing code (the fp lib functions). If */
2389 /* the first argument is always passed the same way, this */
2390 /* lax checking is ok (but may not be true for in future ports) */
2391 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2394 //printf("argCnt = %d\n",argCnt);
2395 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2398 if (IFFUNC_ISWPARAM (csym->type) != IFFUNC_ISWPARAM (sym->type))
2400 werror (E_PREV_DEF_CONFLICT, csym->name, "wparam");
2403 if (IFFUNC_ISSHADOWREGS (csym->type) != IFFUNC_ISSHADOWREGS (sym->type))
2405 werror (E_PREV_DEF_CONFLICT, csym->name, "shadowregs");
2409 /* compare expected args with actual args */
2410 exargs = FUNC_ARGS(csym->type);
2411 acargs = FUNC_ARGS(sym->type);
2413 /* for all the expected args do */
2416 exargs = exargs->next, acargs = acargs->next, argCnt++)
2418 if (getenv("DEBUG_SANITY")) {
2419 fprintf (stderr, "checkFunction: %s ", exargs->name);
2421 /* make sure the type is complete and sane */
2422 checkTypeSanity(exargs->etype, exargs->name);
2424 /* If the actual argument is an array, any prototype
2425 * will have modified it to a pointer. Duplicate that
2428 if (IS_AGGREGATE (acargs->type))
2430 checkValue = copyValue (acargs);
2431 aggregateToPointer (checkValue);
2435 checkValue = acargs;
2438 if (compareType (exargs->type, checkValue->type) <= 0)
2440 werror (E_ARG_TYPE, argCnt);
2441 printFromToType(exargs->type, checkValue->type);
2446 /* if one them ended we have a problem */
2447 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2448 (!exargs && acargs && !IS_VOID (acargs->type)))
2449 werror (E_ARG_COUNT);
2451 /* replace with this defition */
2452 sym->cdef = csym->cdef;
2453 deleteSym (SymbolTab, csym, csym->name);
2454 deleteFromSeg(csym);
2455 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2456 if (IS_EXTERN (csym->etype) && !
2457 IS_EXTERN (sym->etype))
2459 addSet (&publics, sym);
2464 /*------------------------------------------------------------------*/
2465 /* cdbStructBlock - calls struct printing for a blcks */
2466 /*------------------------------------------------------------------*/
2467 void cdbStructBlock (int block)
2470 bucket **table = StructTab;
2473 /* go thru the entire table */
2474 for (i = 0; i < 256; i++)
2476 for (chain = table[i]; chain; chain = chain->next)
2478 if (chain->block >= block)
2481 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2487 /*-----------------------------------------------------------------*/
2488 /* processFuncArgs - does some processing with function args */
2489 /*-----------------------------------------------------------------*/
2491 processFuncArgs (symbol * func)
2495 sym_link *funcType=func->type;
2497 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2498 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2500 /* find the function declaration within the type */
2501 while (funcType && !IS_FUNC(funcType))
2502 funcType=funcType->next;
2504 /* if this function has variable argument list */
2505 /* then make the function a reentrant one */
2506 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2507 FUNC_ISREENT(funcType)=1;
2509 /* check if this function is defined as calleeSaves
2510 then mark it as such */
2511 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2513 /* loop thru all the arguments */
2514 val = FUNC_ARGS(funcType);
2516 /* if it is void then remove parameters */
2517 if (val && IS_VOID (val->type))
2519 FUNC_ARGS(funcType) = NULL;
2523 /* reset regparm for the port */
2524 (*port->reset_regparms) ();
2526 /* if any of the arguments is an aggregate */
2527 /* change it to pointer to the same type */
2531 char buffer[SDCC_NAME_MAX+1];
2533 SNPRINTF (buffer, sizeof(buffer), "%s parameter %d", func->name, pNum);
2534 checkTypeSanity (val->etype, buffer);
2536 /* mark it as a register parameter if
2537 the function does not have VA_ARG
2538 and as port dictates */
2539 if (!IFFUNC_HASVARARGS(funcType) &&
2540 (argreg = (*port->reg_parm) (val->type)))
2542 SPEC_REGPARM (val->etype) = 1;
2543 SPEC_ARGREG(val->etype) = argreg;
2544 } else if (IFFUNC_ISREENT(funcType)) {
2545 FUNC_HASSTACKPARM(funcType) = 1;
2548 if (IS_AGGREGATE (val->type))
2550 aggregateToPointer (val);
2557 /* if this is an internal generated function call */
2559 /* ignore --stack-auto for this one, we don't know how it is compiled */
2560 /* simply trust on --int-long-reent or --float-reent */
2561 if (IFFUNC_ISREENT(funcType)) {
2565 /* if this function is reentrant or */
2566 /* automatics r 2b stacked then nothing */
2567 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2571 val = FUNC_ARGS(funcType);
2576 /* if a symbolname is not given */
2577 /* synthesize a variable name */
2580 SNPRINTF (val->name, sizeof(val->name),
2581 "_%s_PARM_%d", func->name, pNum++);
2582 val->sym = newSymbol (val->name, 1);
2583 if (SPEC_SCLS(val->etype) == S_BIT)
2584 SPEC_OCLS (val->etype) = bit;
2586 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2587 val->sym->type = copyLinkChain (val->type);
2588 val->sym->etype = getSpec (val->sym->type);
2589 val->sym->_isparm = 1;
2590 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2592 /* ?? static functions shouldn't imply static parameters - EEP */
2593 if (IS_SPEC(func->etype)) {
2594 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2595 SPEC_STAT (func->etype);
2598 addSymChain (&val->sym);
2601 else /* symbol name given create synth name */
2604 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2605 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2606 val->sym->_isparm = 1;
2607 if (SPEC_SCLS(val->etype) == S_BIT)
2608 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) = bit;
2610 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2611 (options.model != MODEL_SMALL ? xdata : data);
2614 /* ?? static functions shouldn't imply static parameters - EEP */
2615 if (IS_SPEC(func->etype)) {
2616 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2617 SPEC_STAT (func->etype);
2621 if (!isinSet(operKeyReset, val->sym)) {
2622 addSet (&operKeyReset, val->sym);
2623 applyToSet (operKeyReset, resetParmKey);
2629 /*-----------------------------------------------------------------*/
2630 /* isSymbolEqual - compares two symbols return 1 if they match */
2631 /*-----------------------------------------------------------------*/
2633 isSymbolEqual (symbol * dest, symbol * src)
2635 /* if pointers match then equal */
2639 /* if one of them is null then don't match */
2643 /* if both of them have rname match on rname */
2644 if (dest->rname[0] && src->rname[0])
2645 return (!strcmp (dest->rname, src->rname));
2647 /* otherwise match on name */
2648 return (!strcmp (dest->name, src->name));
2651 void PT(sym_link *type)
2653 printTypeChain(type,0);
2655 /*-----------------------------------------------------------------*/
2656 /* printTypeChain - prints the type chain in human readable form */
2657 /*-----------------------------------------------------------------*/
2659 printTypeChain (sym_link * start, FILE * of)
2663 sym_link * type, * search;
2673 fprintf (of, "void");
2677 /* Print the chain as it is written in the source: */
2678 /* start with the last entry. */
2679 /* However, the storage class at the end of the */
2680 /* chain reall applies to the first in the chain! */
2682 for (type = start; type && type->next; type = type->next)
2685 scls=SPEC_SCLS(type);
2693 case S_DATA: fprintf (of, "data-"); break;
2694 case S_XDATA: fprintf (of, "xdata-"); break;
2695 case S_SFR: fprintf (of, "sfr-"); break;
2696 case S_SBIT: fprintf (of, "sbit-"); break;
2697 case S_CODE: fprintf (of, "code-"); break;
2698 case S_IDATA: fprintf (of, "idata-"); break;
2699 case S_PDATA: fprintf (of, "pdata-"); break;
2700 case S_LITERAL: fprintf (of, "literal-"); break;
2701 case S_STACK: fprintf (of, "stack-"); break;
2702 case S_XSTACK: fprintf (of, "xstack-"); break;
2703 case S_BIT: fprintf (of, "bit-"); break;
2704 case S_EEPROM: fprintf (of, "eeprom-"); break;
2711 if (!IS_FUNC(type)) {
2712 if (DCL_PTR_VOLATILE (type)) {
2713 fprintf (of, "volatile-");
2715 if (DCL_PTR_CONST (type)) {
2716 fprintf (of, "const-");
2719 switch (DCL_TYPE (type))
2722 fprintf (of, "function %s %s",
2723 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2724 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2726 for (args = FUNC_ARGS(type);
2729 printTypeChain(args->type, of);
2736 fprintf (of, "generic* ");
2739 fprintf (of, "code* ");
2742 fprintf (of, "xdata* ");
2745 fprintf (of, "eeprom* ");
2748 fprintf (of, "near* ");
2751 fprintf (of, "idata* ");
2754 fprintf (of, "pdata* ");
2757 fprintf (of, "unknown* ");
2760 if (DCL_ELEM(type)) {
2761 fprintf (of, "[%d] ", DCL_ELEM(type));
2763 fprintf (of, "[] ");
2770 if (SPEC_VOLATILE (type))
2771 fprintf (of, "volatile-");
2772 if (SPEC_CONST (type))
2773 fprintf (of, "const-");
2774 if (SPEC_USIGN (type))
2775 fprintf (of, "unsigned-");
2776 switch (SPEC_NOUN (type))
2780 fprintf (of, "long-");
2781 fprintf (of, "int");
2785 fprintf (of, "char");
2789 fprintf (of, "void");
2793 fprintf (of, "float");
2797 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2801 fprintf (of, "sbit");
2805 fprintf (of, "bit");
2809 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2813 fprintf (of, "double");
2817 fprintf (of, "unknown type");
2821 /* search entry in list before "type" */
2822 for (search = start; search && search->next != type;)
2823 search = search->next;
2832 /*--------------------------------------------------------------------*/
2833 /* printTypeChainRaw - prints the type chain in human readable form */
2834 /* in the raw data structure ordering */
2835 /*--------------------------------------------------------------------*/
2837 printTypeChainRaw (sym_link * start, FILE * of)
2850 fprintf (of, "void");
2860 if (!IS_FUNC(type)) {
2861 if (DCL_PTR_VOLATILE (type)) {
2862 fprintf (of, "volatile-");
2864 if (DCL_PTR_CONST (type)) {
2865 fprintf (of, "const-");
2868 switch (DCL_TYPE (type))
2871 fprintf (of, "function %s %s",
2872 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2873 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2875 for (args = FUNC_ARGS(type);
2878 printTypeChain(args->type, of);
2885 fprintf (of, "generic* ");
2888 fprintf (of, "code* ");
2891 fprintf (of, "xdata* ");
2894 fprintf (of, "eeprom* ");
2897 fprintf (of, "near* ");
2900 fprintf (of, "idata* ");
2903 fprintf (of, "pdata* ");
2906 fprintf (of, "unknown* ");
2909 if (DCL_ELEM(type)) {
2910 fprintf (of, "[%d] ", DCL_ELEM(type));
2912 fprintf (of, "[] ");
2916 if (DCL_TSPEC(type))
2919 printTypeChainRaw(DCL_TSPEC(type), of);
2923 else if (IS_SPEC (type))
2925 switch (SPEC_SCLS (type))
2927 case S_DATA: fprintf (of, "data-"); break;
2928 case S_XDATA: fprintf (of, "xdata-"); break;
2929 case S_SFR: fprintf (of, "sfr-"); break;
2930 case S_SBIT: fprintf (of, "sbit-"); break;
2931 case S_CODE: fprintf (of, "code-"); break;
2932 case S_IDATA: fprintf (of, "idata-"); break;
2933 case S_PDATA: fprintf (of, "pdata-"); break;
2934 case S_LITERAL: fprintf (of, "literal-"); break;
2935 case S_STACK: fprintf (of, "stack-"); break;
2936 case S_XSTACK: fprintf (of, "xstack-"); break;
2937 case S_BIT: fprintf (of, "bit-"); break;
2938 case S_EEPROM: fprintf (of, "eeprom-"); break;
2941 if (SPEC_VOLATILE (type))
2942 fprintf (of, "volatile-");
2943 if (SPEC_CONST (type))
2944 fprintf (of, "const-");
2945 if (SPEC_USIGN (type))
2946 fprintf (of, "unsigned-");
2947 switch (SPEC_NOUN (type))
2951 fprintf (of, "long-");
2952 fprintf (of, "int");
2956 fprintf (of, "char");
2960 fprintf (of, "void");
2964 fprintf (of, "float");
2968 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2972 fprintf (of, "sbit");
2976 fprintf (of, "bit");
2980 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2984 fprintf (of, "double");
2988 fprintf (of, "unknown type");
2993 fprintf (of, "NOT_SPEC_OR_DECL");
3003 /*-----------------------------------------------------------------*/
3004 /* powof2 - returns power of two for the number if number is pow 2 */
3005 /*-----------------------------------------------------------------*/
3007 powof2 (TYPE_UDWORD num)
3020 if (n1s > 1 || nshifts == 0)
3036 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3037 symbol *__muldiv[3][3][2];
3038 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
3039 sym_link *__multypes[3][2];
3040 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
3041 symbol *__conv[2][3][2];
3042 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
3043 symbol *__rlrr[2][3][2];
3045 sym_link *floatType;
3048 _mangleFunctionName(char *in)
3050 if (port->getMangledFunctionName)
3052 return port->getMangledFunctionName(in);
3060 /*-----------------------------------------------------------------*/
3061 /* typeFromStr - create a typechain from an encoded string */
3062 /* basic types - 'c' - char */
3068 /* '*' - pointer - default (GPOINTER) */
3069 /* modifiers - 'u' - unsigned */
3070 /* pointer modifiers - 'g' - generic */
3074 /* 'F' - function */
3075 /* examples : "ig*" - generic int * */
3076 /* "cx*" - char xdata * */
3077 /* "ui" - unsigned int */
3078 /*-----------------------------------------------------------------*/
3079 sym_link *typeFromStr (char *s)
3081 sym_link *r = newLink(DECLARATOR);
3093 r->class = SPECIFIER;
3094 SPEC_NOUN(r) = V_CHAR;
3098 r->class = SPECIFIER;
3099 SPEC_NOUN(r) = V_INT;
3102 r->class = SPECIFIER;
3103 SPEC_NOUN(r) = V_INT;
3107 r->class = SPECIFIER;
3108 SPEC_NOUN(r) = V_FLOAT;
3111 r->class = SPECIFIER;
3112 SPEC_NOUN(r) = V_VOID;
3115 DCL_TYPE(r) = port->unqualified_pointer;
3122 assert(*(s+1)=='*');
3123 nr = newLink(DECLARATOR);
3128 DCL_TYPE(r) = GPOINTER;
3131 DCL_TYPE(r) = FPOINTER;
3134 DCL_TYPE(r) = CPOINTER;
3137 DCL_TYPE(r) = POINTER;
3140 DCL_TYPE(r) = FUNCTION;
3141 nr = newLink(DECLARATOR);
3144 DCL_TYPE(r) = CPOINTER;
3150 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
3151 "typeFromStr: unknown type");
3154 if (IS_SPEC(r) && usign) {
3163 /*-----------------------------------------------------------------*/
3164 /* initCSupport - create functions for C support routines */
3165 /*-----------------------------------------------------------------*/
3169 const char *smuldivmod[] =
3173 const char *sbwd[] =
3175 "char", "int", "long"
3181 const char *srlrr[] =
3186 int bwd, su, muldivmod, tofrom, rlrr;
3188 if (getenv("SDCC_NO_C_SUPPORT")) {
3189 /* for debugging only */
3193 floatType = newFloatLink ();
3195 for (bwd = 0; bwd < 3; bwd++)
3212 __multypes[bwd][0] = l;
3213 __multypes[bwd][1] = copyLinkChain (l);
3214 SPEC_USIGN (__multypes[bwd][1]) = 1;
3217 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
3218 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
3219 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
3220 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
3221 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
3222 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
3223 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
3224 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
3225 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
3226 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
3228 for (tofrom = 0; tofrom < 2; tofrom++)
3230 for (bwd = 0; bwd < 3; bwd++)
3232 for (su = 0; su < 2; su++)
3236 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
3237 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
3241 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
3242 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
3249 for (muldivmod = 0; muldivmod < 3; muldivmod++)
3251 for (bwd = 0; bwd < 3; bwd++)
3253 for (su = 0; su < 2; su++)
3255 SNPRINTF (buffer, sizeof(buffer),
3257 smuldivmod[muldivmod],
3260 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3261 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3266 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
3267 Therefore they've been merged into mulint() and mullong().
3270 for (bwd = 0; bwd < 3; bwd++)
3272 for (su = 0; su < 2; su++)
3274 for (muldivmod = 1; muldivmod < 3; muldivmod++)
3277 SNPRINTF (buffer, sizeof(buffer),
3279 smuldivmod[muldivmod],
3282 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3283 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3291 for (su = 0; su < 2; su++)
3293 /* muluchar and mulschar are still separate functions, because e.g. the z80
3294 port is sign/zero-extending to int before calling mulint() */
3295 SNPRINTF (buffer, sizeof(buffer),
3297 smuldivmod[muldivmod],
3300 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3301 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
3305 /* word and doubleword */
3306 for (bwd = 1; bwd < 3; bwd++)
3309 SNPRINTF (buffer, sizeof(buffer),
3311 smuldivmod[muldivmod],
3313 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
3314 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
3315 /* signed = unsigned */
3316 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3319 for (rlrr = 0; rlrr < 2; rlrr++)
3321 for (bwd = 0; bwd < 3; bwd++)
3323 for (su = 0; su < 2; su++)
3325 SNPRINTF (buffer, sizeof(buffer),
3330 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3331 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3337 /*-----------------------------------------------------------------*/
3338 /* initBuiltIns - create prototypes for builtin functions */
3339 /*-----------------------------------------------------------------*/
3345 if (!port->builtintable) return ;
3347 for (i = 0 ; port->builtintable[i].name ; i++) {
3348 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3349 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3350 FUNC_ISBUILTIN(sym->type) = 1;
3351 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3355 sym_link *validateLink(sym_link *l,
3362 if (l && l->class==select)
3367 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3368 " expected %s, got %s\n",
3369 macro, args, file, line,
3370 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3372 return l; // never reached, makes compiler happy.
3375 /*--------------------------------------------------------------------*/
3376 /* newEnumType - create an integer type compatible with enumerations */
3377 /*--------------------------------------------------------------------*/
3379 newEnumType (symbol *enumlist)
3387 type = newLink (SPECIFIER);
3388 SPEC_NOUN (type) = V_INT;
3392 /* Determine the range of the enumerated values */
3394 min = max = (int) floatFromVal (valFromType (sym->type));
3395 for (sym = sym->next; sym; sym = sym->next)
3397 v = (int) floatFromVal (valFromType (sym->type));
3404 /* Determine the smallest integer type that is compatible with this range */
3405 type = newLink (SPECIFIER);
3406 if (min>=0 && max<=255)
3408 SPEC_NOUN (type) = V_CHAR;
3409 SPEC_USIGN (type) = 1;
3411 else if (min>=-128 && max<=127)
3413 SPEC_NOUN (type) = V_CHAR;
3415 else if (min>=0 && max<=65535)
3417 SPEC_NOUN (type) = V_INT;
3418 SPEC_USIGN (type) = 1;
3420 else if (min>=-32768 && max<=32767)
3422 SPEC_NOUN (type) = V_INT;
3426 SPEC_NOUN (type) = V_INT;
3427 SPEC_LONG (type) = 1;
3429 SPEC_USIGN (type) = 1;