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 -------------------------------------------------------------------------*/
27 value *aggregateToPointer (value *val);
28 void printTypeChainRaw (sym_link * start, FILE * of);
30 void printFromToType(sym_link *from, sym_link *to) {
31 fprintf (stderr, "from type '");
32 printTypeChain (from, stderr);
33 fprintf (stderr, "'\nto type '");
34 printTypeChain (to, stderr);
35 fprintf (stderr, "'\n");
39 char *nounName(sym_link *sl) {
40 switch (SPEC_NOUN(sl))
43 if (SPEC_LONG(sl)) return "long";
44 if (sl->select.s._short) return "short";
47 case V_FLOAT: return "float";
48 case V_CHAR: return "char";
49 case V_VOID: return "void";
50 case V_STRUCT: return "struct";
51 case V_LABEL: return "label";
52 case V_BITFIELD: return "bitfield";
53 case V_BIT: return "bit";
54 case V_SBIT: return "sbit";
55 case V_DOUBLE: return "double";
60 bucket *SymbolTab[256]; /* the symbol table */
61 bucket *StructTab[256]; /* the structure table */
62 bucket *TypedefTab[256]; /* the typedef table */
63 bucket *LabelTab[256]; /* the Label table */
64 bucket *enumTab[256]; /* enumerated table */
66 /*------------------------------------------------------------------*/
67 /* initSymt () - initialises symbol table related stuff */
68 /*------------------------------------------------------------------*/
74 for (i = 0; i < 256; i++)
75 SymbolTab[i] = StructTab[i] = (void *) NULL;
79 /*-----------------------------------------------------------------*/
80 /* newBucket - allocates & returns a new bucket */
81 /*-----------------------------------------------------------------*/
87 bp = Safe_alloc ( sizeof (bucket));
92 /*-----------------------------------------------------------------*/
93 /* hashKey - computes the hashkey given a symbol name */
94 /*-----------------------------------------------------------------*/
96 hashKey (const char *s)
98 unsigned long key = 0;
105 /*-----------------------------------------------------------------*/
106 /* addSym - adds a symbol to the hash Table */
107 /*-----------------------------------------------------------------*/
109 addSym (bucket ** stab,
116 int i; /* index into the hash Table */
117 bucket *bp; /* temp bucket * */
120 symbol *csym = (symbol *)sym;
122 if (getenv("DEBUG_SANITY")) {
123 fprintf (stderr, "addSym: %s ", sname);
125 /* make sure the type is complete and sane */
126 checkTypeSanity(csym->etype, csym->name);
129 /* prevent overflow of the (r)name buffers */
130 if (strlen(sname)>SDCC_SYMNAME_MAX) {
131 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
132 sname[SDCC_SYMNAME_MAX]='\0';
135 /* the symbols are always added at the head of the list */
137 /* get a free entry */
138 bp = Safe_alloc ( sizeof (bucket));
140 bp->sym = sym; /* update the symbol pointer */
141 bp->level = level; /* update the nest level */
143 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
145 /* if this is the first entry */
148 bp->prev = bp->next = (void *) NULL; /* point to nothing */
151 /* not first entry then add @ head of list */
161 /*-----------------------------------------------------------------*/
162 /* deleteSym - deletes a symbol from the hash Table entry */
163 /*-----------------------------------------------------------------*/
165 deleteSym (bucket ** stab, void *sym, char *sname)
173 /* find the symbol */
176 if (bp->sym == sym) /* found it then break out */
177 break; /* of the loop */
181 if (!bp) /* did not find it */
183 /* if this is the first one in the chain */
187 if (stab[i]) /* if chain ! empty */
188 stab[i]->prev = (void *) NULL;
190 /* middle || end of chain */
193 if (bp->next) /* if not end of chain */
194 bp->next->prev = bp->prev;
196 bp->prev->next = bp->next;
201 /*-----------------------------------------------------------------*/
202 /* findSym - finds a symbol in a table */
203 /*-----------------------------------------------------------------*/
205 findSym (bucket ** stab, void *sym, const char *sname)
209 bp = stab[hashKey (sname)];
212 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
217 return (bp ? bp->sym : (void *) NULL);
220 /*-----------------------------------------------------------------*/
221 /* findSymWithLevel - finds a symbol with a name & level */
222 /*-----------------------------------------------------------------*/
224 findSymWithLevel (bucket ** stab, symbol * sym)
228 bp = stab[hashKey (sym->name)];
231 ** do the search from the head of the list since the
232 ** elements are added at the head it is ensured that
233 ** we will find the deeper definitions before we find
234 ** the global ones. we need to check for symbols with
235 ** level <= to the level given, if levels match then block
236 ** numbers need to match as well
240 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
242 /* if this is parameter then nothing else need to be checked */
243 if (((symbol *) (bp->sym))->_isparm)
245 /* if levels match then block numbers should also match */
246 if (bp->level && bp->level == sym->level && bp->block == sym->block)
248 /* if levels don't match then we are okay */
249 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
251 /* if this is a global variable then we are ok too */
259 return (void *) NULL;
262 /*-----------------------------------------------------------------*/
263 /* findSymWithBlock - finds a symbol with name in with a block */
264 /*-----------------------------------------------------------------*/
266 findSymWithBlock (bucket ** stab, symbol * sym, int block)
270 bp = stab[hashKey (sym->name)];
273 if (strcmp (bp->name, sym->name) == 0 &&
279 return (bp ? bp->sym : (void *) NULL);
282 /*------------------------------------------------------------------*/
283 /* newSymbol () - returns a new pointer to a symbol */
284 /*------------------------------------------------------------------*/
286 newSymbol (char *name, int scope)
290 sym = Safe_alloc ( sizeof (symbol));
292 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
293 sym->level = scope; /* set the level */
294 sym->block = currBlockno;
295 sym->lineDef = mylineno; /* set the line number */
299 /*------------------------------------------------------------------*/
300 /* newLink - creates a new link (declarator,specifier) */
301 /*------------------------------------------------------------------*/
303 newLink (SYM_LINK_CLASS select)
307 p = Safe_alloc ( sizeof (sym_link));
313 /*------------------------------------------------------------------*/
314 /* newStruct - creats a new structdef from the free list */
315 /*------------------------------------------------------------------*/
317 newStruct (char *tag)
321 s = Safe_alloc ( sizeof (structdef));
323 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
327 /*------------------------------------------------------------------*/
328 /* sclsFromPtr - Return the storage class a pointer points into. */
329 /* S_FIXED is returned for generic pointers or other */
330 /* unexpected cases */
331 /*------------------------------------------------------------------*/
333 sclsFromPtr(sym_link *ptr)
335 switch (DCL_TYPE (ptr))
358 /*------------------------------------------------------------------*/
359 /* pointerTypes - do the computation for the pointer types */
360 /*------------------------------------------------------------------*/
362 pointerTypes (sym_link * ptr, sym_link * type)
367 /* find the first pointer type */
368 while (ptr && !IS_PTR (ptr))
371 /* could not find it */
372 if (!ptr || IS_SPEC (ptr))
375 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
376 pointerTypes (ptr->next, type);
380 /* change the pointer type depending on the
381 storage class of the type */
384 switch (SPEC_SCLS (type))
387 DCL_TYPE (ptr) = FPOINTER;
390 DCL_TYPE (ptr) = IPOINTER;
393 DCL_TYPE (ptr) = PPOINTER;
396 DCL_TYPE (ptr) = POINTER;
399 DCL_TYPE (ptr) = CPOINTER;
402 DCL_TYPE (ptr) = EEPPOINTER;
405 DCL_TYPE (ptr) = port->unqualified_pointer;
408 /* the storage class of type ends here */
409 SPEC_SCLS (type) = 0;
412 /* now change all the remaining unknown pointers
413 to generic pointers */
416 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
417 DCL_TYPE (ptr) = port->unqualified_pointer;
421 /* same for the type although it is highly unlikely that
422 type will have a pointer */
425 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
426 DCL_TYPE (type) = port->unqualified_pointer;
431 /*------------------------------------------------------------------*/
432 /* addDecl - adds a declarator @ the end of a chain */
433 /*------------------------------------------------------------------*/
435 addDecl (symbol * sym, int type, sym_link * p)
441 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
442 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
444 /* if we are passed a link then set head & tail */
453 head = tail = newLink (DECLARATOR);
454 DCL_TYPE (head) = type;
457 /* if this is the first entry */
465 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
467 sym->etype = mergeSpec (sym->etype, head, sym->name);
471 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
474 while (t->next != sym->etype)
477 tail->next = sym->etype;
481 sym->etype->next = head;
487 /* if the type is an unknown pointer and has
488 a tspec then take the storage class const & volatile
489 attribute from the tspec & make it those of this
493 //DCL_TYPE (p) == UPOINTER &&
496 if (!IS_SPEC (sym->etype))
498 sym->etype = sym->etype->next = newLink (SPECIFIER);
500 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
501 DCL_TSPEC (p) = NULL;
504 // if there is a function in this type chain
505 if (p && funcInChain(sym->type)) {
506 processFuncArgs (sym);
512 /*------------------------------------------------------------------
513 checkTypeSanity: prevent the user from doing e.g.:
515 ------------------------------------------------------------------*/
516 void checkTypeSanity(sym_link *etype, char *name) {
520 if (getenv("DEBUG_SANITY")) {
521 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
526 if (!IS_SPEC(etype)) {
527 if (getenv("DEBUG_SANITY")) {
528 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
533 noun=nounName(etype);
535 if (getenv("DEBUG_SANITY")) {
536 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
539 if ((SPEC_NOUN(etype)==V_CHAR ||
540 SPEC_NOUN(etype)==V_FLOAT ||
541 SPEC_NOUN(etype)==V_DOUBLE ||
542 SPEC_NOUN(etype)==V_VOID) &&
543 (etype->select.s._short || SPEC_LONG(etype))) {
544 // long or short for char float double or void
545 werror (E_LONG_OR_SHORT_INVALID, noun, name);
547 if ((SPEC_NOUN(etype)==V_FLOAT ||
548 SPEC_NOUN(etype)==V_DOUBLE ||
549 SPEC_NOUN(etype)==V_VOID) &&
550 (etype->select.s._signed || SPEC_USIGN(etype))) {
551 // signed or unsigned for float double or void
552 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
555 // special case for "short"
556 if (etype->select.s._short) {
557 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
558 etype->select.s._short = 0;
562 "const a;" or "data b;" or "signed s" or "long l"
564 if (!SPEC_NOUN(etype)) {
565 SPEC_NOUN(etype)=V_INT;
568 if (etype->select.s._signed && SPEC_USIGN(etype)) {
569 // signed AND unsigned
570 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
572 if (etype->select.s._short && SPEC_LONG(etype)) {
574 werror (E_LONG_AND_SHORT_INVALID, noun, name);
579 /*------------------------------------------------------------------*/
580 /* mergeSpec - merges two specifiers and returns the new one */
581 /*------------------------------------------------------------------*/
583 mergeSpec (sym_link * dest, sym_link * src, char *name)
585 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
587 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
590 werror (E_SYNTAX_ERROR, yytext);
591 // the show must go on
596 if (SPEC_NOUN(src)) {
597 if (!SPEC_NOUN(dest)) {
598 SPEC_NOUN(dest)=SPEC_NOUN(src);
600 /* we shouldn't redeclare the type */
601 if (getenv("DEBUG_SANITY")) {
602 fprintf (stderr, "mergeSpec: ");
604 werror(E_TWO_OR_MORE_DATA_TYPES, name);
608 if (SPEC_SCLS(src)) {
609 /* if destination has no storage class */
610 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
611 SPEC_SCLS (dest) = SPEC_SCLS (src);
613 if (getenv("DEBUG_SANITY")) {
614 fprintf (stderr, "mergeSpec: ");
616 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
620 /* copy all the specifications */
622 // we really should do:
624 if (SPEC_what(src)) {
625 if (SPEC_what(dest)) {
626 werror(W_DUPLICATE_SPEC, "what");
628 SPEC_what(dst)|=SPEC_what(src);
631 // but there are more important thing right now
633 SPEC_LONG (dest) |= SPEC_LONG (src);
634 dest->select.s._short|=src->select.s._short;
635 SPEC_USIGN (dest) |= SPEC_USIGN (src);
636 dest->select.s._signed|=src->select.s._signed;
637 SPEC_STAT (dest) |= SPEC_STAT (src);
638 SPEC_EXTR (dest) |= SPEC_EXTR (src);
639 SPEC_CONST(dest) |= SPEC_CONST (src);
640 SPEC_ABSA (dest) |= SPEC_ABSA (src);
641 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
642 SPEC_ADDR (dest) |= SPEC_ADDR (src);
643 SPEC_OCLS (dest) = SPEC_OCLS (src);
644 SPEC_BLEN (dest) |= SPEC_BLEN (src);
645 SPEC_BSTR (dest) |= SPEC_BSTR (src);
646 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
647 SPEC_ENUM (dest) |= SPEC_ENUM (src);
648 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
649 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
651 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
652 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
654 /* these are the only function attributes that will be set
655 in a specifier while parsing */
656 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
657 FUNC_BANKED(dest) |= FUNC_BANKED(src);
658 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
659 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
660 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
661 FUNC_ISISR(dest) |= FUNC_ISISR(src);
662 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
663 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
664 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
665 FUNC_INTNO(dest) |= FUNC_INTNO(src);
666 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
671 /*------------------------------------------------------------------*/
672 /* genSymName - generates and returns a name used for anonymous vars */
673 /*------------------------------------------------------------------*/
675 genSymName (int level)
677 static int gCount = 0;
678 static char gname[SDCC_NAME_MAX + 1];
680 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
684 /*------------------------------------------------------------------*/
685 /* getSpec - returns the specifier part from a declaration chain */
686 /*------------------------------------------------------------------*/
688 getSpec (sym_link * p)
693 while (p && !(IS_SPEC (p)))
699 /*------------------------------------------------------------------*/
700 /* newCharLink() - creates an char type */
701 /*------------------------------------------------------------------*/
707 p = newLink (SPECIFIER);
708 SPEC_NOUN (p) = V_CHAR;
713 /*------------------------------------------------------------------*/
714 /* newFloatLink - a new Float type */
715 /*------------------------------------------------------------------*/
721 p = newLink (SPECIFIER);
722 SPEC_NOUN (p) = V_FLOAT;
727 /*------------------------------------------------------------------*/
728 /* newLongLink() - new long type */
729 /*------------------------------------------------------------------*/
735 p = newLink (SPECIFIER);
736 SPEC_NOUN (p) = V_INT;
742 /*------------------------------------------------------------------*/
743 /* newIntLink() - creates an int type */
744 /*------------------------------------------------------------------*/
750 p = newLink (SPECIFIER);
751 SPEC_NOUN (p) = V_INT;
756 /*------------------------------------------------------------------*/
757 /* getSize - returns size of a type chain in bits */
758 /*------------------------------------------------------------------*/
760 getSize (sym_link * p)
762 /* if nothing return 0 */
766 { /* if this is the specifier then */
767 switch (SPEC_NOUN (p))
768 { /* depending on the specifier type */
770 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
778 return SPEC_STRUCT (p)->size;
785 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
791 /* this is a specifier */
792 switch (DCL_TYPE (p))
796 return DCL_ELEM (p) * getSize (p->next);
798 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
799 // "can not tell the size of an array[]");
819 /*------------------------------------------------------------------*/
820 /* bitsForType - returns # of bits required to store this type */
821 /*------------------------------------------------------------------*/
823 bitsForType (sym_link * p)
825 /* if nothing return 0 */
830 { /* if this is the specifier then */
832 switch (SPEC_NOUN (p))
833 { /* depending on the specifier type */
835 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
837 return FLOATSIZE * 8;
843 return SPEC_STRUCT (p)->size * 8;
850 return SPEC_BLEN (p);
856 /* this is a specifier */
857 switch (DCL_TYPE (p))
860 return DCL_ELEM (p) * getSize (p->next) * 8;
864 return (PTRSIZE * 8);
869 return (FPTRSIZE * 8);
871 return (GPTRSIZE * 8);
878 /*------------------------------------------------------------------*/
879 /* copySymbolChain - copies a symbol chain */
880 /*------------------------------------------------------------------*/
882 copySymbolChain (symbol * src)
889 dest = copySymbol (src);
890 dest->next = copySymbolChain (src->next);
894 /*------------------------------------------------------------------*/
895 /* copySymbol - makes a copy of a symbol */
896 /*------------------------------------------------------------------*/
898 copySymbol (symbol * src)
905 dest = newSymbol (src->name, src->level);
906 memcpy (dest, src, sizeof (symbol));
907 dest->level = src->level;
908 dest->block = src->block;
909 dest->ival = copyIlist (src->ival);
910 dest->type = copyLinkChain (src->type);
911 dest->etype = getSpec (dest->type);
913 dest->key = src->key;
914 dest->allocreq = src->allocreq;
918 /*------------------------------------------------------------------*/
919 /* reverseSyms - reverses the links for a symbol chain */
920 /*------------------------------------------------------------------*/
922 reverseSyms (symbol * sym)
924 symbol *prev, *curr, *next;
939 sym->next = (void *) NULL;
943 /*------------------------------------------------------------------*/
944 /* reverseLink - reverses the links for a type chain */
945 /*------------------------------------------------------------------*/
947 reverseLink (sym_link * type)
949 sym_link *prev, *curr, *next;
964 type->next = (void *) NULL;
968 /*------------------------------------------------------------------*/
969 /* addSymChain - adds a symbol chain to the symboltable */
970 /*------------------------------------------------------------------*/
972 addSymChain (symbol * symHead)
974 symbol *sym = symHead;
977 for (; sym != NULL; sym = sym->next)
980 checkTypeSanity(sym->etype, sym->name);
982 /* if already exists in the symbol table then check if
983 one of them is an extern definition if yes then
984 then check if the type match, if the types match then
985 delete the current entry and add the new entry */
986 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
987 csym->level == sym->level) {
989 /* one definition extern ? */
990 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
991 /* do types match ? */
992 if (compareType (csym->type, sym->type) != 1) {
994 werror (E_EXTERN_MISMATCH, csym->name);
999 if (compareType (csym->type, sym->type) != 1) {
1000 werror (E_DUPLICATE, sym->name);
1004 /* delete current entry */
1005 deleteSym (SymbolTab, csym, csym->name);
1006 deleteFromSeg(csym);
1010 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1015 /*------------------------------------------------------------------*/
1016 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1017 /*------------------------------------------------------------------*/
1019 funcInChain (sym_link * lnk)
1030 /*------------------------------------------------------------------*/
1031 /* structElemType - returns the type info of a sturct member */
1032 /*------------------------------------------------------------------*/
1034 structElemType (sym_link * stype, value * id)
1036 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1037 sym_link *type, *etype;
1038 sym_link *petype = getSpec (stype);
1042 /* look for the id */
1045 if (strcmp (fields->rname, id->name) == 0)
1047 type = copyLinkChain (fields->type);
1048 etype = getSpec (type);
1049 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1050 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1052 SPEC_CONST (type) |= SPEC_CONST (stype);
1054 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1057 fields = fields->next;
1061 werror (E_NOT_MEMBER, id->name);
1063 // the show must go on
1064 return newIntLink();
1067 /*------------------------------------------------------------------*/
1068 /* getStructElement - returns element of a tructure definition */
1069 /*------------------------------------------------------------------*/
1071 getStructElement (structdef * sdef, symbol * sym)
1075 for (field = sdef->fields; field; field = field->next)
1076 if (strcmp (field->name, sym->name) == 0)
1079 werror (E_NOT_MEMBER, sym->name);
1081 return sdef->fields;
1084 /*------------------------------------------------------------------*/
1085 /* compStructSize - computes the size of a structure */
1086 /*------------------------------------------------------------------*/
1088 compStructSize (int su, structdef * sdef)
1090 int sum = 0, usum = 0;
1094 /* for the identifiers */
1095 loop = sdef->fields;
1098 /* create the internal name for this variable */
1099 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1104 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1106 /* if this is a bit field */
1109 /* change it to a unsigned bit */
1110 SPEC_NOUN (loop->etype) = V_BITFIELD;
1111 SPEC_USIGN (loop->etype) = 1;
1112 SPEC_BLEN (loop->etype) = loop->bitVar;
1114 if (loop->bitVar == BITVAR_PAD) {
1115 /* A zero length bitfield forces padding */
1116 SPEC_BSTR (loop->etype) = bitOffset;
1117 SPEC_BLEN (loop->etype) = 0;
1122 if (bitOffset == 8) {
1126 /* check if this fit into the remaining */
1127 /* bits of this byte else align it to the */
1128 /* next byte boundary */
1129 if (loop->bitVar <= (8 - bitOffset)) {
1130 /* fits into current byte */
1132 SPEC_BSTR (loop->etype) = bitOffset;
1133 bitOffset += loop->bitVar;
1135 else if (!bitOffset) {
1136 /* does not fit, but is already byte aligned */
1138 SPEC_BSTR (loop->etype) = bitOffset;
1139 bitOffset += loop->bitVar;
1142 /* does not fit; need to realign first */
1144 loop->offset = (su == UNION ? sum = 0 : sum);
1146 SPEC_BSTR (loop->etype) = bitOffset;
1147 bitOffset += loop->bitVar;
1149 while (bitOffset>8) {
1156 /* This is a non-bit field. Make sure we are */
1157 /* byte aligned first */
1160 loop->offset = (su == UNION ? sum = 0 : sum);
1164 checkDecl (loop, 1);
1165 sum += getSize (loop->type);
1170 /* if union then size = sizeof larget field */
1172 /* For UNION, round up after each field */
1173 sum += ((bitOffset+7)/8);
1174 usum = max (usum, sum);
1179 /* For STRUCT, round up after all fields processed */
1181 sum += ((bitOffset+7)/8);
1183 return (su == UNION ? usum : sum);
1186 /*------------------------------------------------------------------*/
1187 /* checkSClass - check the storage class specification */
1188 /*------------------------------------------------------------------*/
1190 checkSClass (symbol * sym, int isProto)
1194 if (getenv("DEBUG_SANITY")) {
1195 fprintf (stderr, "checkSClass: %s \n", sym->name);
1198 /* type is literal can happen for enums change
1200 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1201 SPEC_SCLS (sym->etype) = S_AUTO;
1203 /* if sfr or sbit then must also be volatile */
1204 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1205 SPEC_SCLS (sym->etype) == S_SFR)
1207 SPEC_VOLATILE (sym->etype) = 1;
1210 /* if absolute address given then it mark it as
1211 volatile -- except in the PIC port */
1213 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1214 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1215 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1218 if (IS_ABSOLUTE (sym->etype))
1219 SPEC_VOLATILE (sym->etype) = 1;
1221 /* If code memory is read only, then pointers to code memory */
1222 /* implicitly point to constants -- make this explicit */
1224 while (t && t->next) {
1225 if (IS_CODEPTR(t) && port->mem.code_ro) {
1226 if (IS_SPEC(t->next)) {
1227 SPEC_CONST (t->next) = 1;
1229 DCL_PTR_CONST (t->next) = 1;
1235 /* global variables declared const put into code */
1236 /* if no other storage class specified */
1237 if (sym->level == 0 &&
1238 SPEC_SCLS(sym->etype) == S_FIXED &&
1239 !IS_FUNC(sym->type)) {
1240 /* find the first non-array link */
1244 if (IS_CONSTANT (t)) {
1245 SPEC_SCLS (sym->etype) = S_CODE;
1249 /* global variable in code space is a constant */
1250 if (sym->level == 0 &&
1251 SPEC_SCLS (sym->etype) == S_CODE &&
1252 port->mem.code_ro) {
1253 /* find the first non-array link */
1260 DCL_PTR_CONST (t) = 1;
1264 /* if bit variable then no storage class can be */
1265 /* specified since bit is already a storage */
1266 if (IS_BITVAR (sym->etype) &&
1267 (SPEC_SCLS (sym->etype) != S_FIXED &&
1268 SPEC_SCLS (sym->etype) != S_SBIT &&
1269 SPEC_SCLS (sym->etype) != S_BIT)
1272 werror (E_BITVAR_STORAGE, sym->name);
1273 SPEC_SCLS (sym->etype) = S_FIXED;
1276 /* extern variables cannot be initialized */
1277 if (IS_EXTERN (sym->etype) && sym->ival)
1279 werror (E_EXTERN_INIT, sym->name);
1283 /* if this is an automatic symbol */
1284 if (sym->level && (options.stackAuto || reentrant)) {
1285 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1286 SPEC_SCLS (sym->etype) == S_FIXED ||
1287 SPEC_SCLS (sym->etype) == S_REGISTER ||
1288 SPEC_SCLS (sym->etype) == S_STACK ||
1289 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1290 SPEC_SCLS (sym->etype) = S_AUTO;
1292 /* storage class may only be specified for statics */
1293 if (!IS_STATIC(sym->etype)) {
1294 werror (E_AUTO_ASSUMED, sym->name);
1299 /* automatic symbols cannot be given */
1300 /* an absolute address ignore it */
1302 SPEC_ABSA (sym->etype) &&
1303 (options.stackAuto || reentrant))
1305 werror (E_AUTO_ABSA, sym->name);
1306 SPEC_ABSA (sym->etype) = 0;
1309 /* arrays & pointers cannot be defined for bits */
1310 /* SBITS or SFRs or BIT */
1311 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1312 (SPEC_NOUN (sym->etype) == V_BIT ||
1313 SPEC_NOUN (sym->etype) == V_SBIT ||
1314 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1315 SPEC_SCLS (sym->etype) == S_SFR))
1316 werror (E_BIT_ARRAY, sym->name);
1318 /* if this is a bit|sbit then set length & start */
1319 if (SPEC_NOUN (sym->etype) == V_BIT ||
1320 SPEC_NOUN (sym->etype) == V_SBIT)
1322 SPEC_BLEN (sym->etype) = 1;
1323 SPEC_BSTR (sym->etype) = 0;
1327 /* variables declared in CODE space must have */
1328 /* initializers if not an extern */
1329 if (SPEC_SCLS (sym->etype) == S_CODE &&
1330 sym->ival == NULL &&
1332 port->mem.code_ro &&
1333 !IS_EXTERN (sym->etype) &&
1334 !funcInChain (sym->type))
1335 werror (E_CODE_NO_INIT, sym->name);
1338 /* if parameter or local variable then change */
1339 /* the storage class to reflect where the var will go */
1340 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1341 !IS_STATIC(sym->etype))
1343 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1345 SPEC_SCLS (sym->etype) = (options.useXstack ?
1346 S_XSTACK : S_STACK);
1350 /* hack-o-matic! I see no reason why the useXstack option should ever
1351 * control this allcoation, but the code was originally that way, and
1352 * changing it for non-390 ports breaks the compiler badly.
1354 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1355 1 : options.useXstack;
1356 SPEC_SCLS (sym->etype) = (useXdata ?
1362 /*------------------------------------------------------------------*/
1363 /* changePointer - change pointer to functions */
1364 /*------------------------------------------------------------------*/
1366 changePointer (symbol * sym)
1370 /* go thru the chain of declarations */
1371 /* if we find a pointer to a function */
1372 /* unconditionally change it to a ptr */
1374 for (p = sym->type; p; p = p->next)
1376 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1377 DCL_TYPE (p) = port->unqualified_pointer;
1378 if (IS_PTR (p) && IS_FUNC (p->next))
1379 DCL_TYPE (p) = CPOINTER;
1383 /*------------------------------------------------------------------*/
1384 /* checkDecl - does semantic validation of a declaration */
1385 /*------------------------------------------------------------------*/
1387 checkDecl (symbol * sym, int isProto)
1390 checkSClass (sym, isProto); /* check the storage class */
1391 changePointer (sym); /* change pointers if required */
1393 /* if this is an array without any dimension
1394 then update the dimension from the initial value */
1395 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1396 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1401 /*------------------------------------------------------------------*/
1402 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1403 /*------------------------------------------------------------------*/
1405 copyLinkChain (sym_link * p)
1407 sym_link *head, *curr, *loop;
1410 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1413 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1414 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1423 /*------------------------------------------------------------------*/
1424 /* cleanUpBlock - cleansup the symbol table specified for all the */
1425 /* symbols in the given block */
1426 /*------------------------------------------------------------------*/
1428 cleanUpBlock (bucket ** table, int block)
1433 /* go thru the entire table */
1434 for (i = 0; i < 256; i++)
1436 for (chain = table[i]; chain; chain = chain->next)
1438 if (chain->block >= block)
1440 deleteSym (table, chain->sym, chain->name);
1446 /*------------------------------------------------------------------*/
1447 /* cleanUpLevel - cleansup the symbol table specified for all the */
1448 /* symbols in the given level */
1449 /*------------------------------------------------------------------*/
1451 cleanUpLevel (bucket ** table, int level)
1456 /* go thru the entire table */
1457 for (i = 0; i < 256; i++)
1459 for (chain = table[i]; chain; chain = chain->next)
1461 if (chain->level >= level)
1463 deleteSym (table, chain->sym, chain->name);
1469 /*------------------------------------------------------------------*/
1470 /* computeType - computes the resultant type from two types */
1471 /*------------------------------------------------------------------*/
1473 computeType (sym_link * type1, sym_link * type2)
1477 sym_link *etype1 = getSpec (type1);
1478 sym_link *etype2 = getSpec (type2);
1480 /* if one of them is a float then result is a float */
1481 /* here we assume that the types passed are okay */
1482 /* and can be cast to one another */
1483 /* which ever is greater in size */
1484 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1485 rType = newFloatLink ();
1487 /* if only one of them is a bit variable
1488 then the other one prevails */
1489 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1490 rType = copyLinkChain (type2);
1491 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1492 rType = copyLinkChain (type1);
1494 /* if one of them is a pointer or array then that
1496 if (IS_PTR (type1) || IS_ARRAY (type1))
1497 rType = copyLinkChain (type1);
1498 else if (IS_PTR (type2) || IS_ARRAY (type2))
1499 rType = copyLinkChain (type2);
1500 else if (getSize (type1) > getSize (type2))
1501 rType = copyLinkChain (type1);
1503 rType = copyLinkChain (type2);
1505 reType = getSpec (rType);
1507 if (SPEC_NOUN (reType) == V_CHAR)
1508 SPEC_NOUN (reType) = V_INT;
1511 /* if either of them unsigned but not val then make this unsigned */
1512 if (((/*!IS_LITERAL(type1) &&*/ SPEC_USIGN (etype1)) ||
1513 (/*!IS_LITERAL(type2) &&*/ SPEC_USIGN (etype2))) &&
1515 SPEC_USIGN (reType) = 1;
1517 SPEC_USIGN (reType) = 0;
1519 /* if result is a literal then make not so */
1520 if (IS_LITERAL (reType))
1521 SPEC_SCLS (reType) = S_REGISTER;
1526 /*--------------------------------------------------------------------*/
1527 /* compareType - will do type check return 1 if match, -1 if castable */
1528 /*--------------------------------------------------------------------*/
1530 compareType (sym_link * dest, sym_link * src)
1541 /* if dest is a declarator then */
1546 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1548 //checkFunction(src,dest);
1550 return compareType (dest->next, src->next);
1552 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1555 if (IS_PTR (src) && IS_GENPTR (dest))
1557 if (IS_PTR (dest) && IS_ARRAY (src)) {
1558 value *val=aggregateToPointer (valFromType(src));
1559 int res=compareType (dest, val->type);
1560 Safe_free(val->type);
1564 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1565 return compareType (dest->next, src);
1568 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1574 /* if one is a specifier and the other is not */
1575 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1576 (IS_SPEC (dest) && !IS_SPEC (src)))
1579 /* if one of them is a void then ok */
1580 if (SPEC_NOUN (dest) == V_VOID &&
1581 SPEC_NOUN (src) != V_VOID)
1584 if (SPEC_NOUN (dest) != V_VOID &&
1585 SPEC_NOUN (src) == V_VOID)
1588 /* if they are both bitfields then if the lengths
1589 and starts don't match */
1590 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1591 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1592 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1595 /* it is a specifier */
1596 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1598 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1599 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1600 getSize (dest) == getSize (src))
1602 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1607 else if (IS_STRUCT (dest))
1609 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1614 if (SPEC_LONG (dest) != SPEC_LONG (src))
1617 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1623 /*------------------------------------------------------------------*/
1624 /* inCalleeSaveList - return 1 if found in callee save list */
1625 /*------------------------------------------------------------------*/
1627 calleeCmp(void *p1, void *p2)
1629 return (strcmp((char *)p1, (char *)(p2)) == 0);
1633 inCalleeSaveList(char *s)
1635 if (options.all_callee_saves)
1637 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
1640 /*-----------------------------------------------------------------*/
1641 /* aggregateToPointer: change an agggregate type function */
1642 /* argument to a pointer to that type. */
1643 /*-----------------------------------------------------------------*/
1645 aggregateToPointer (value * val)
1647 if (IS_AGGREGATE (val->type))
1649 /* if this is a structure */
1650 /* then we need to add a new link */
1651 if (IS_STRUCT (val->type))
1653 /* first lets add DECLARATOR type */
1654 sym_link *p = val->type;
1656 werror (W_STRUCT_AS_ARG, val->name);
1657 val->type = newLink (DECLARATOR);
1658 val->type->next = p;
1661 /* change to a pointer depending on the */
1662 /* storage class specified */
1663 switch (SPEC_SCLS (val->etype))
1666 DCL_TYPE (val->type) = IPOINTER;
1669 DCL_TYPE (val->type) = PPOINTER;
1672 if (SPEC_OCLS(val->etype)) {
1673 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
1675 // this happens for (external) function parameters
1676 DCL_TYPE (val->type) = port->unqualified_pointer;
1682 DCL_TYPE (val->type) = POINTER;
1685 DCL_TYPE (val->type) = CPOINTER;
1688 DCL_TYPE (val->type) = FPOINTER;
1691 DCL_TYPE (val->type) = EEPPOINTER;
1694 DCL_TYPE (val->type) = port->unqualified_pointer;
1697 /* is there is a symbol associated then */
1698 /* change the type of the symbol as well */
1701 val->sym->type = copyLinkChain (val->type);
1702 val->sym->etype = getSpec (val->sym->type);
1707 /*------------------------------------------------------------------*/
1708 /* checkFunction - does all kinds of check on a function */
1709 /*------------------------------------------------------------------*/
1711 checkFunction (symbol * sym, symbol *csym)
1713 value *exargs, *acargs;
1717 if (getenv("DEBUG_SANITY")) {
1718 fprintf (stderr, "checkFunction: %s ", sym->name);
1721 /* make sure the type is complete and sane */
1722 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1724 /* if not type then some kind of error */
1728 /* if the function has no type then make it return int */
1729 if (!sym->type->next)
1730 sym->type->next = sym->etype = newIntLink ();
1732 /* function cannot return aggregate */
1733 if (IS_AGGREGATE (sym->type->next))
1735 werror (E_FUNC_AGGR, sym->name);
1739 /* function cannot return bit */
1740 if (IS_BITVAR (sym->type->next))
1742 werror (E_FUNC_BIT, sym->name);
1746 /* check if this function is defined as calleeSaves
1747 then mark it as such */
1748 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
1750 /* if interrupt service routine */
1751 /* then it cannot have arguments */
1752 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
1754 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
1755 werror (E_INT_ARGS, sym->name);
1756 FUNC_ARGS(sym->type)=NULL;
1760 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
1762 acargs=acargs->next, argCnt++) {
1764 // this can happen for reentrant functions
1765 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
1766 // the show must go on: synthesize a name and symbol
1767 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
1768 acargs->sym = newSymbol (acargs->name, 1);
1769 SPEC_OCLS (acargs->etype) = istack;
1770 acargs->sym->type = copyLinkChain (acargs->type);
1771 acargs->sym->etype = getSpec (acargs->sym->type);
1772 acargs->sym->_isparm = 1;
1773 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
1774 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
1776 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
1780 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
1781 return 1; /* not defined nothing more to check */
1783 /* check if body already present */
1784 if (csym && IFFUNC_HASBODY(csym->type))
1786 werror (E_FUNC_BODY, sym->name);
1790 /* check the return value type */
1791 if (compareType (csym->type, sym->type) <= 0)
1793 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1794 printFromToType(csym->type, sym->type);
1798 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
1800 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1803 if (FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type))
1805 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1808 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
1810 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1813 /* compare expected args with actual args */
1814 exargs = FUNC_ARGS(csym->type);
1815 acargs = FUNC_ARGS(sym->type);
1817 /* for all the expected args do */
1820 exargs = exargs->next, acargs = acargs->next, argCnt++)
1822 if (getenv("DEBUG_SANITY")) {
1823 fprintf (stderr, "checkFunction: %s ", exargs->name);
1825 /* make sure the type is complete and sane */
1826 checkTypeSanity(exargs->etype, exargs->name);
1828 /* If the actual argument is an array, any prototype
1829 * will have modified it to a pointer. Duplicate that
1832 if (IS_AGGREGATE (acargs->type))
1834 checkValue = copyValue (acargs);
1835 aggregateToPointer (checkValue);
1839 checkValue = acargs;
1842 if (compareType (exargs->type, checkValue->type) <= 0)
1844 werror (E_ARG_TYPE, argCnt);
1845 printFromToType(exargs->type, checkValue->type);
1850 /* if one them ended we have a problem */
1851 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1852 (!exargs && acargs && !IS_VOID (acargs->type)))
1853 werror (E_ARG_COUNT);
1855 /* replace with this defition */
1856 sym->cdef = csym->cdef;
1857 deleteSym (SymbolTab, csym, csym->name);
1858 deleteFromSeg(csym);
1859 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1860 if (IS_EXTERN (csym->etype) && !
1861 IS_EXTERN (sym->etype))
1863 addSet (&publics, sym);
1868 /*------------------------------------------------------------------*/
1869 /* cdbStructBlock - calls struct printing for a blcks */
1870 /*------------------------------------------------------------------*/
1871 void cdbStructBlock (int block)
1874 bucket **table = StructTab;
1877 /* go thru the entire table */
1878 for (i = 0; i < 256; i++)
1880 for (chain = table[i]; chain; chain = chain->next)
1882 if (chain->block >= block)
1885 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
1891 /*-----------------------------------------------------------------*/
1892 /* processFuncArgs - does some processing with function args */
1893 /*-----------------------------------------------------------------*/
1895 processFuncArgs (symbol * func)
1899 sym_link *funcType=func->type;
1901 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
1902 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
1904 // if this is a pointer to a function
1905 if (IS_PTR(funcType)) {
1906 funcType=funcType->next;
1909 /* if this function has variable argument list */
1910 /* then make the function a reentrant one */
1911 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
1912 FUNC_ISREENT(funcType)=1;
1914 /* check if this function is defined as calleeSaves
1915 then mark it as such */
1916 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
1918 /* loop thru all the arguments */
1919 val = FUNC_ARGS(funcType);
1921 /* if it is void then remove parameters */
1922 if (val && IS_VOID (val->type))
1924 FUNC_ARGS(funcType) = NULL;
1928 /* reset regparm for the port */
1929 (*port->reset_regparms) ();
1930 /* if any of the arguments is an aggregate */
1931 /* change it to pointer to the same type */
1935 /* mark it as a register parameter if
1936 the function does not have VA_ARG
1937 and as port dictates */
1938 if (!IFFUNC_HASVARARGS(funcType) &&
1939 (argreg = (*port->reg_parm) (val->type)))
1941 SPEC_REGPARM (val->etype) = 1;
1942 SPEC_ARGREG(val->etype) = argreg;
1943 } else if (IFFUNC_ISREENT(funcType)) {
1944 FUNC_HASSTACKPARM(funcType) = 1;
1947 if (IS_AGGREGATE (val->type))
1949 aggregateToPointer (val);
1956 /* if this is an internal generated function call */
1958 /* ignore --stack-auto for this one, we don't know how it is compiled */
1959 /* simply trust on --int-long-reent or --float-reent */
1960 if (IFFUNC_ISREENT(funcType)) {
1964 /* if this function is reentrant or */
1965 /* automatics r 2b stacked then nothing */
1966 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
1970 val = FUNC_ARGS(funcType);
1975 /* if a symbolname is not given */
1976 /* synthesize a variable name */
1979 SNPRINTF (val->name, sizeof(val->name),
1980 "_%s_PARM_%d", func->name, pNum++);
1981 val->sym = newSymbol (val->name, 1);
1982 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1983 val->sym->type = copyLinkChain (val->type);
1984 val->sym->etype = getSpec (val->sym->type);
1985 val->sym->_isparm = 1;
1986 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
1987 if (IS_SPEC(func->etype)) {
1988 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1989 SPEC_STAT (func->etype);
1991 addSymChain (val->sym);
1994 else /* symbol name given create synth name */
1997 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
1998 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
1999 val->sym->_isparm = 1;
2000 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2001 (options.model != MODEL_SMALL ? xdata : data);
2002 if (IS_SPEC(func->etype)) {
2003 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2004 SPEC_STAT (func->etype);
2007 if (!isinSet(operKeyReset, val->sym)) {
2008 addSet (&operKeyReset, val->sym);
2009 applyToSet (operKeyReset, resetParmKey);
2015 /*-----------------------------------------------------------------*/
2016 /* isSymbolEqual - compares two symbols return 1 if they match */
2017 /*-----------------------------------------------------------------*/
2019 isSymbolEqual (symbol * dest, symbol * src)
2021 /* if pointers match then equal */
2025 /* if one of them is null then don't match */
2029 /* if both of them have rname match on rname */
2030 if (dest->rname[0] && src->rname[0])
2031 return (!strcmp (dest->rname, src->rname));
2033 /* otherwise match on name */
2034 return (!strcmp (dest->name, src->name));
2037 void PT(sym_link *type)
2039 printTypeChain(type,0);
2041 /*-----------------------------------------------------------------*/
2042 /* printTypeChain - prints the type chain in human readable form */
2043 /*-----------------------------------------------------------------*/
2045 printTypeChain (sym_link * start, FILE * of)
2049 sym_link * type, * search;
2059 fprintf (of, "void");
2063 /* Print the chain as it is written in the source: */
2064 /* start with the last entry. */
2065 /* However, the storage class at the end of the */
2066 /* chain reall applies to the first in the chain! */
2068 for (type = start; type && type->next; type = type->next)
2071 scls=SPEC_SCLS(type);
2079 case S_DATA: fprintf (of, "data-"); break;
2080 case S_XDATA: fprintf (of, "xdata-"); break;
2081 case S_SFR: fprintf (of, "sfr-"); break;
2082 case S_SBIT: fprintf (of, "sbit-"); break;
2083 case S_CODE: fprintf (of, "code-"); break;
2084 case S_IDATA: fprintf (of, "idata-"); break;
2085 case S_PDATA: fprintf (of, "pdata-"); break;
2086 case S_LITERAL: fprintf (of, "literal-"); break;
2087 case S_STACK: fprintf (of, "stack-"); break;
2088 case S_XSTACK: fprintf (of, "xstack-"); break;
2089 case S_BIT: fprintf (of, "bit-"); break;
2090 case S_EEPROM: fprintf (of, "eeprom-"); break;
2097 if (!IS_FUNC(type)) {
2098 if (DCL_PTR_VOLATILE (type)) {
2099 fprintf (of, "volatile-");
2101 if (DCL_PTR_CONST (type)) {
2102 fprintf (of, "const-");
2105 switch (DCL_TYPE (type))
2108 fprintf (of, "function %s %s",
2109 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2110 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2112 for (args = FUNC_ARGS(type);
2115 printTypeChain(args->type, of);
2122 fprintf (of, "generic* ");
2125 fprintf (of, "code* ");
2128 fprintf (of, "xdata* ");
2131 fprintf (of, "eeprom* ");
2134 fprintf (of, "near* ");
2137 fprintf (of, "idata* ");
2140 fprintf (of, "pdata* ");
2143 fprintf (of, "unknown* ");
2146 if (DCL_ELEM(type)) {
2147 fprintf (of, "[%d] ", DCL_ELEM(type));
2149 fprintf (of, "[] ");
2156 if (SPEC_VOLATILE (type))
2157 fprintf (of, "volatile-");
2158 if (SPEC_CONST (type))
2159 fprintf (of, "const-");
2160 if (SPEC_USIGN (type))
2161 fprintf (of, "unsigned-");
2162 switch (SPEC_NOUN (type))
2166 fprintf (of, "long-");
2167 fprintf (of, "int");
2171 fprintf (of, "char");
2175 fprintf (of, "void");
2179 fprintf (of, "float");
2183 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2187 fprintf (of, "sbit");
2191 fprintf (of, "bit");
2195 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2199 fprintf (of, "double");
2203 fprintf (of, "unknown type");
2207 /* search entry in list before "type" */
2208 for (search = start; search && search->next != type;)
2209 search = search->next;
2218 /*--------------------------------------------------------------------*/
2219 /* printTypeChainRaw - prints the type chain in human readable form */
2220 /* in the raw data structure ordering */
2221 /*--------------------------------------------------------------------*/
2223 printTypeChainRaw (sym_link * start, FILE * of)
2236 fprintf (of, "void");
2246 if (!IS_FUNC(type)) {
2247 if (DCL_PTR_VOLATILE (type)) {
2248 fprintf (of, "volatile-");
2250 if (DCL_PTR_CONST (type)) {
2251 fprintf (of, "const-");
2254 switch (DCL_TYPE (type))
2257 fprintf (of, "function %s %s",
2258 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2259 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2261 for (args = FUNC_ARGS(type);
2264 printTypeChain(args->type, of);
2271 fprintf (of, "generic* ");
2274 fprintf (of, "code* ");
2277 fprintf (of, "xdata* ");
2280 fprintf (of, "eeprom* ");
2283 fprintf (of, "near* ");
2286 fprintf (of, "idata* ");
2289 fprintf (of, "pdata* ");
2292 fprintf (of, "unknown* ");
2295 if (DCL_ELEM(type)) {
2296 fprintf (of, "[%d] ", DCL_ELEM(type));
2298 fprintf (of, "[] ");
2302 if (DCL_TSPEC(type))
2305 printTypeChainRaw(DCL_TSPEC(type), of);
2309 else if (IS_SPEC (type))
2311 switch (SPEC_SCLS (type))
2313 case S_DATA: fprintf (of, "data-"); break;
2314 case S_XDATA: fprintf (of, "xdata-"); break;
2315 case S_SFR: fprintf (of, "sfr-"); break;
2316 case S_SBIT: fprintf (of, "sbit-"); break;
2317 case S_CODE: fprintf (of, "code-"); break;
2318 case S_IDATA: fprintf (of, "idata-"); break;
2319 case S_PDATA: fprintf (of, "pdata-"); break;
2320 case S_LITERAL: fprintf (of, "literal-"); break;
2321 case S_STACK: fprintf (of, "stack-"); break;
2322 case S_XSTACK: fprintf (of, "xstack-"); break;
2323 case S_BIT: fprintf (of, "bit-"); break;
2324 case S_EEPROM: fprintf (of, "eeprom-"); break;
2327 if (SPEC_VOLATILE (type))
2328 fprintf (of, "volatile-");
2329 if (SPEC_CONST (type))
2330 fprintf (of, "const-");
2331 if (SPEC_USIGN (type))
2332 fprintf (of, "unsigned-");
2333 switch (SPEC_NOUN (type))
2337 fprintf (of, "long-");
2338 fprintf (of, "int");
2342 fprintf (of, "char");
2346 fprintf (of, "void");
2350 fprintf (of, "float");
2354 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2358 fprintf (of, "sbit");
2362 fprintf (of, "bit");
2366 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2370 fprintf (of, "double");
2374 fprintf (of, "unknown type");
2379 fprintf (of, "NOT_SPEC_OR_DECL");
2389 /*-----------------------------------------------------------------*/
2390 /* powof2 - returns power of two for the number if number is pow 2 */
2391 /*-----------------------------------------------------------------*/
2393 powof2 (unsigned long num)
2406 if (n1s > 1 || nshifts == 0)
2422 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2423 symbol *__muldiv[3][3][2];
2424 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2425 sym_link *__multypes[3][2];
2426 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2427 symbol *__conv[2][3][2];
2428 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2429 symbol *__rlrr[2][3][2];
2431 sym_link *floatType;
2434 _mangleFunctionName(char *in)
2436 if (port->getMangledFunctionName)
2438 return port->getMangledFunctionName(in);
2446 /*-----------------------------------------------------------------*/
2447 /* typeFromStr - create a typechain from an encoded string */
2448 /* basic types - 'c' - char */
2454 /* '*' - pointer - default (GPOINTER) */
2455 /* modifiers - 'u' - unsigned */
2456 /* pointer modifiers - 'g' - generic */
2460 /* 'F' - function */
2461 /* examples : "ig*" - generic int * */
2462 /* "cx*" - char xdata * */
2463 /* "ui" - unsigned int */
2464 /*-----------------------------------------------------------------*/
2465 sym_link *typeFromStr (char *s)
2467 sym_link *r = newLink(DECLARATOR);
2479 r->class = SPECIFIER;
2480 SPEC_NOUN(r) = V_CHAR;
2484 r->class = SPECIFIER;
2485 SPEC_NOUN(r) = V_INT;
2488 r->class = SPECIFIER;
2489 SPEC_NOUN(r) = V_INT;
2493 r->class = SPECIFIER;
2494 SPEC_NOUN(r) = V_FLOAT;
2497 r->class = SPECIFIER;
2498 SPEC_NOUN(r) = V_VOID;
2501 DCL_TYPE(r) = port->unqualified_pointer;
2508 assert(*(s+1)=='*');
2509 nr = newLink(DECLARATOR);
2514 DCL_TYPE(r) = GPOINTER;
2517 DCL_TYPE(r) = FPOINTER;
2520 DCL_TYPE(r) = CPOINTER;
2523 DCL_TYPE(r) = POINTER;
2526 DCL_TYPE(r) = FUNCTION;
2527 nr = newLink(DECLARATOR);
2530 DCL_TYPE(r) = CPOINTER;
2536 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
2537 "typeFromStr: unknown type");
2540 if (IS_SPEC(r) && usign) {
2549 /*-----------------------------------------------------------------*/
2550 /* initCSupport - create functions for C support routines */
2551 /*-----------------------------------------------------------------*/
2555 const char *smuldivmod[] =
2559 const char *sbwd[] =
2561 "char", "int", "long"
2567 const char *srlrr[] =
2572 int bwd, su, muldivmod, tofrom, rlrr;
2574 if (getenv("SDCC_NO_C_SUPPORT")) {
2575 /* for debugging only */
2579 floatType = newFloatLink ();
2581 for (bwd = 0; bwd < 3; bwd++)
2598 __multypes[bwd][0] = l;
2599 __multypes[bwd][1] = copyLinkChain (l);
2600 SPEC_USIGN (__multypes[bwd][1]) = 1;
2603 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2604 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2605 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2606 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2607 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2608 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2609 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2610 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2611 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2612 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2614 for (tofrom = 0; tofrom < 2; tofrom++)
2616 for (bwd = 0; bwd < 3; bwd++)
2618 for (su = 0; su < 2; su++)
2622 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
2623 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
2627 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
2628 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
2635 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2637 for (bwd = 0; bwd < 3; bwd++)
2639 for (su = 0; su < 2; su++)
2641 SNPRINTF (buffer, sizeof(buffer),
2643 smuldivmod[muldivmod],
2646 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2647 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2652 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
2653 Therefore they've been merged into mulint() and mullong().
2656 for (bwd = 0; bwd < 3; bwd++)
2658 for (su = 0; su < 2; su++)
2660 for (muldivmod = 1; muldivmod < 3; muldivmod++)
2663 SNPRINTF (buffer, sizeof(buffer),
2665 smuldivmod[muldivmod],
2668 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2669 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2677 for (su = 0; su < 2; su++)
2679 /* muluchar and mulschar are still separate functions, because e.g. the z80
2680 port is sign/zero-extending to int before calling mulint() */
2681 SNPRINTF (buffer, sizeof(buffer),
2683 smuldivmod[muldivmod],
2686 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2687 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2691 /* word and doubleword */
2692 for (bwd = 1; bwd < 3; bwd++)
2695 SNPRINTF (buffer, sizeof(buffer),
2697 smuldivmod[muldivmod],
2699 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2700 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
2701 /* signed = unsigned */
2702 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
2705 for (rlrr = 0; rlrr < 2; rlrr++)
2707 for (bwd = 0; bwd < 3; bwd++)
2709 for (su = 0; su < 2; su++)
2711 SNPRINTF (buffer, sizeof(buffer),
2716 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2717 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
2723 /*-----------------------------------------------------------------*/
2724 /* initBuiltIns - create prototypes for builtin functions */
2725 /*-----------------------------------------------------------------*/
2731 if (!port->builtintable) return ;
2733 for (i = 0 ; port->builtintable[i].name ; i++) {
2734 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
2735 port->builtintable[i].nParms,port->builtintable[i].parm_types);
2736 FUNC_ISBUILTIN(sym->type) = 1;
2737 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
2741 sym_link *validateLink(sym_link *l,
2748 if (l && l->class==select)
2753 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
2754 " expected %s, got %s\n",
2755 macro, args, file, line,
2756 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
2758 return l; // never reached, makes compiler happy.