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);
29 void printFromToType(sym_link *from, sym_link *to) {
30 fprintf (stderr, "from type '");
31 printTypeChain (from, stderr);
32 fprintf (stderr, "'\nto type '");
33 printTypeChain (to, stderr);
34 fprintf (stderr, "'\n");
38 char *nounName(sym_link *sl) {
39 switch (SPEC_NOUN(sl))
42 if (SPEC_LONG(sl)) return "long";
43 if (sl->select.s._short) return "short";
46 case V_FLOAT: return "float";
47 case V_CHAR: return "char";
48 case V_VOID: return "void";
49 case V_STRUCT: return "struct";
50 case V_LABEL: return "label";
51 case V_BITFIELD: return "bitfield";
52 case V_BIT: return "bit";
53 case V_SBIT: return "sbit";
54 case V_DOUBLE: return "double";
59 bucket *SymbolTab[256]; /* the symbol table */
60 bucket *StructTab[256]; /* the structure table */
61 bucket *TypedefTab[256]; /* the typedef table */
62 bucket *LabelTab[256]; /* the Label table */
63 bucket *enumTab[256]; /* enumerated table */
65 /*------------------------------------------------------------------*/
66 /* initSymt () - initialises symbol table related stuff */
67 /*------------------------------------------------------------------*/
73 for (i = 0; i < 256; i++)
74 SymbolTab[i] = StructTab[i] = (void *) NULL;
78 /*-----------------------------------------------------------------*/
79 /* newBucket - allocates & returns a new bucket */
80 /*-----------------------------------------------------------------*/
86 bp = Safe_alloc ( sizeof (bucket));
91 /*-----------------------------------------------------------------*/
92 /* hashKey - computes the hashkey given a symbol name */
93 /*-----------------------------------------------------------------*/
95 hashKey (const char *s)
97 unsigned long key = 0;
104 /*-----------------------------------------------------------------*/
105 /* addSym - adds a symbol to the hash Table */
106 /*-----------------------------------------------------------------*/
108 addSym (bucket ** stab,
115 int i; /* index into the hash Table */
116 bucket *bp; /* temp bucket * */
119 symbol *csym = (symbol *)sym;
121 if (getenv("DEBUG_SANITY")) {
122 fprintf (stderr, "addSym: %s ", sname);
124 /* make sure the type is complete and sane */
125 checkTypeSanity(csym->etype, csym->name);
128 /* prevent overflow of the (r)name buffers */
129 if (strlen(sname)>SDCC_SYMNAME_MAX) {
130 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
131 sname[SDCC_SYMNAME_MAX]='\0';
134 /* the symbols are always added at the head of the list */
136 /* get a free entry */
137 bp = Safe_alloc ( sizeof (bucket));
139 bp->sym = sym; /* update the symbol pointer */
140 bp->level = level; /* update the nest level */
142 strncpyz (bp->name, sname, sizeof(bp->name)); /* copy the name into place */
144 /* if this is the first entry */
147 bp->prev = bp->next = (void *) NULL; /* point to nothing */
150 /* not first entry then add @ head of list */
160 /*-----------------------------------------------------------------*/
161 /* deleteSym - deletes a symbol from the hash Table entry */
162 /*-----------------------------------------------------------------*/
164 deleteSym (bucket ** stab, void *sym, char *sname)
172 /* find the symbol */
175 if (bp->sym == sym) /* found it then break out */
176 break; /* of the loop */
180 if (!bp) /* did not find it */
182 /* if this is the first one in the chain */
186 if (stab[i]) /* if chain ! empty */
187 stab[i]->prev = (void *) NULL;
189 /* middle || end of chain */
192 if (bp->next) /* if not end of chain */
193 bp->next->prev = bp->prev;
195 bp->prev->next = bp->next;
200 /*-----------------------------------------------------------------*/
201 /* findSym - finds a symbol in a table */
202 /*-----------------------------------------------------------------*/
204 findSym (bucket ** stab, void *sym, const char *sname)
208 bp = stab[hashKey (sname)];
211 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
216 return (bp ? bp->sym : (void *) NULL);
219 /*-----------------------------------------------------------------*/
220 /* findSymWithLevel - finds a symbol with a name & level */
221 /*-----------------------------------------------------------------*/
223 findSymWithLevel (bucket ** stab, symbol * sym)
227 bp = stab[hashKey (sym->name)];
230 ** do the search from the head of the list since the
231 ** elements are added at the head it is ensured that
232 ** we will find the deeper definitions before we find
233 ** the global ones. we need to check for symbols with
234 ** level <= to the level given, if levels match then block
235 ** numbers need to match as well
239 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
241 /* if this is parameter then nothing else need to be checked */
242 if (((symbol *) (bp->sym))->_isparm)
244 /* if levels match then block numbers should also match */
245 if (bp->level && bp->level == sym->level && bp->block == sym->block)
247 /* if levels don't match then we are okay */
248 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
250 /* if this is a global variable then we are ok too */
258 return (void *) NULL;
261 /*-----------------------------------------------------------------*/
262 /* findSymWithBlock - finds a symbol with name in with a block */
263 /*-----------------------------------------------------------------*/
265 findSymWithBlock (bucket ** stab, symbol * sym, int block)
269 bp = stab[hashKey (sym->name)];
272 if (strcmp (bp->name, sym->name) == 0 &&
278 return (bp ? bp->sym : (void *) NULL);
281 /*------------------------------------------------------------------*/
282 /* newSymbol () - returns a new pointer to a symbol */
283 /*------------------------------------------------------------------*/
285 newSymbol (char *name, int scope)
289 sym = Safe_alloc ( sizeof (symbol));
291 strncpyz (sym->name, name, sizeof(sym->name)); /* copy the name */
292 sym->level = scope; /* set the level */
293 sym->block = currBlockno;
294 sym->lineDef = mylineno; /* set the line number */
298 /*------------------------------------------------------------------*/
299 /* newLink - creates a new link (declarator,specifier) */
300 /*------------------------------------------------------------------*/
302 newLink (SYM_LINK_CLASS select)
306 p = Safe_alloc ( sizeof (sym_link));
312 /*------------------------------------------------------------------*/
313 /* newStruct - creats a new structdef from the free list */
314 /*------------------------------------------------------------------*/
316 newStruct (char *tag)
320 s = Safe_alloc ( sizeof (structdef));
322 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
326 /*------------------------------------------------------------------*/
327 /* pointerTypes - do the computation for the pointer types */
328 /*------------------------------------------------------------------*/
330 pointerTypes (sym_link * ptr, sym_link * type)
335 /* find the first pointer type */
336 while (ptr && !IS_PTR (ptr))
339 /* could not find it */
340 if (!ptr || IS_SPEC (ptr))
343 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
344 pointerTypes (ptr->next, type);
348 /* change the pointer type depending on the
349 storage class of the type */
352 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
353 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
354 switch (SPEC_SCLS (type))
357 DCL_TYPE (ptr) = FPOINTER;
360 DCL_TYPE (ptr) = IPOINTER;
363 DCL_TYPE (ptr) = PPOINTER;
366 DCL_TYPE (ptr) = POINTER;
369 DCL_PTR_CONST (ptr) = port->mem.code_ro;
370 DCL_TYPE (ptr) = CPOINTER;
373 DCL_TYPE (ptr) = EEPPOINTER;
376 DCL_TYPE (ptr) = port->unqualified_pointer;
379 /* the storage class of type ends here */
382 SPEC_VOLATILE (type) = 0;
385 /* now change all the remaining unknown pointers
386 to generic pointers */
389 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
390 DCL_TYPE (ptr) = port->unqualified_pointer;
394 /* same for the type although it is highly unlikely that
395 type will have a pointer */
398 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
399 DCL_TYPE (type) = port->unqualified_pointer;
405 /*------------------------------------------------------------------*/
406 /* addDecl - adds a declarator @ the end of a chain */
407 /*------------------------------------------------------------------*/
409 addDecl (symbol * sym, int type, sym_link * p)
415 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
416 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
418 /* if we are passed a link then set head & tail */
427 head = tail = newLink (DECLARATOR);
428 DCL_TYPE (head) = type;
431 /* if this is the first entry */
439 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
441 sym->etype = mergeSpec (sym->etype, head, sym->name);
445 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
448 while (t->next != sym->etype)
451 tail->next = sym->etype;
455 sym->etype->next = head;
461 /* if the type is an unknown pointer and has
462 a tspec then take the storage class const & volatile
463 attribute from the tspec & make it those of this
467 //DCL_TYPE (p) == UPOINTER &&
470 if (!IS_SPEC (sym->etype))
472 sym->etype = sym->etype->next = newLink (SPECIFIER);
474 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
475 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
476 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
477 DCL_TSPEC (p) = NULL;
480 // if there is a function in this type chain
481 if (p && funcInChain(sym->type)) {
482 processFuncArgs (sym);
488 /*------------------------------------------------------------------
489 checkTypeSanity: prevent the user from doing e.g.:
491 ------------------------------------------------------------------*/
492 void checkTypeSanity(sym_link *etype, char *name) {
496 if (getenv("DEBUG_SANITY")) {
497 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
502 if (!IS_SPEC(etype)) {
503 if (getenv("DEBUG_SANITY")) {
504 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
509 noun=nounName(etype);
511 if (getenv("DEBUG_SANITY")) {
512 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
515 if ((SPEC_NOUN(etype)==V_CHAR ||
516 SPEC_NOUN(etype)==V_FLOAT ||
517 SPEC_NOUN(etype)==V_DOUBLE ||
518 SPEC_NOUN(etype)==V_VOID) &&
519 (etype->select.s._short || SPEC_LONG(etype))) {
520 // long or short for char float double or void
521 werror (E_LONG_OR_SHORT_INVALID, noun, name);
523 if ((SPEC_NOUN(etype)==V_FLOAT ||
524 SPEC_NOUN(etype)==V_DOUBLE ||
525 SPEC_NOUN(etype)==V_VOID) &&
526 (etype->select.s._signed || SPEC_USIGN(etype))) {
527 // signed or unsigned for float double or void
528 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
531 // special case for "short"
532 if (etype->select.s._short) {
533 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
534 etype->select.s._short = 0;
538 "const a;" or "data b;" or "signed s" or "long l"
540 if (!SPEC_NOUN(etype)) {
541 SPEC_NOUN(etype)=V_INT;
544 if (etype->select.s._signed && SPEC_USIGN(etype)) {
545 // signed AND unsigned
546 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
548 if (etype->select.s._short && SPEC_LONG(etype)) {
550 werror (E_LONG_AND_SHORT_INVALID, noun, name);
555 /*------------------------------------------------------------------*/
556 /* mergeSpec - merges two specifiers and returns the new one */
557 /*------------------------------------------------------------------*/
559 mergeSpec (sym_link * dest, sym_link * src, char *name)
561 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
563 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
566 werror (E_SYNTAX_ERROR, yytext);
567 // the show must go on
572 if (SPEC_NOUN(src)) {
573 if (!SPEC_NOUN(dest)) {
574 SPEC_NOUN(dest)=SPEC_NOUN(src);
576 /* we shouldn't redeclare the type */
577 if (getenv("DEBUG_SANITY")) {
578 fprintf (stderr, "mergeSpec: ");
580 werror(E_TWO_OR_MORE_DATA_TYPES, name);
584 if (SPEC_SCLS(src)) {
585 /* if destination has no storage class */
586 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
587 SPEC_SCLS (dest) = SPEC_SCLS (src);
589 if (getenv("DEBUG_SANITY")) {
590 fprintf (stderr, "mergeSpec: ");
592 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
596 /* copy all the specifications */
598 // we really should do:
600 if (SPEC_what(src)) {
601 if (SPEC_what(dest)) {
602 werror(W_DUPLICATE_SPEC, "what");
604 SPEC_what(dst)|=SPEC_what(src);
607 // but there are more important thing right now
609 SPEC_LONG (dest) |= SPEC_LONG (src);
610 dest->select.s._short|=src->select.s._short;
611 SPEC_USIGN (dest) |= SPEC_USIGN (src);
612 dest->select.s._signed|=src->select.s._signed;
613 SPEC_STAT (dest) |= SPEC_STAT (src);
614 SPEC_EXTR (dest) |= SPEC_EXTR (src);
615 SPEC_CONST(dest) |= SPEC_CONST (src);
616 SPEC_ABSA (dest) |= SPEC_ABSA (src);
617 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
618 SPEC_ADDR (dest) |= SPEC_ADDR (src);
619 SPEC_OCLS (dest) = SPEC_OCLS (src);
620 SPEC_BLEN (dest) |= SPEC_BLEN (src);
621 SPEC_BSTR (dest) |= SPEC_BSTR (src);
622 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
623 SPEC_ENUM (dest) |= SPEC_ENUM (src);
624 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
625 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
627 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
628 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
630 /* these are the only function attributes that will be set
631 in a specifier while parsing */
632 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
633 FUNC_BANKED(dest) |= FUNC_BANKED(src);
634 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
635 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
636 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
637 FUNC_ISISR(dest) |= FUNC_ISISR(src);
638 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
639 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
640 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
641 FUNC_INTNO(dest) |= FUNC_INTNO(src);
642 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
647 /*------------------------------------------------------------------*/
648 /* genSymName - generates and returns a name used for anonymous vars */
649 /*------------------------------------------------------------------*/
651 genSymName (int level)
653 static int gCount = 0;
654 static char gname[SDCC_NAME_MAX + 1];
656 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
660 /*------------------------------------------------------------------*/
661 /* getSpec - returns the specifier part from a declaration chain */
662 /*------------------------------------------------------------------*/
664 getSpec (sym_link * p)
669 while (p && !(IS_SPEC (p)))
675 /*------------------------------------------------------------------*/
676 /* newCharLink() - creates an char type */
677 /*------------------------------------------------------------------*/
683 p = newLink (SPECIFIER);
684 SPEC_NOUN (p) = V_CHAR;
689 /*------------------------------------------------------------------*/
690 /* newFloatLink - a new Float type */
691 /*------------------------------------------------------------------*/
697 p = newLink (SPECIFIER);
698 SPEC_NOUN (p) = V_FLOAT;
703 /*------------------------------------------------------------------*/
704 /* newLongLink() - new long type */
705 /*------------------------------------------------------------------*/
711 p = newLink (SPECIFIER);
712 SPEC_NOUN (p) = V_INT;
718 /*------------------------------------------------------------------*/
719 /* newIntLink() - creates an int type */
720 /*------------------------------------------------------------------*/
726 p = newLink (SPECIFIER);
727 SPEC_NOUN (p) = V_INT;
732 /*------------------------------------------------------------------*/
733 /* getSize - returns size of a type chain in bits */
734 /*------------------------------------------------------------------*/
736 getSize (sym_link * p)
738 /* if nothing return 0 */
742 { /* if this is the specifier then */
743 switch (SPEC_NOUN (p))
744 { /* depending on the specifier type */
746 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
754 return SPEC_STRUCT (p)->size;
761 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
767 /* this is a specifier */
768 switch (DCL_TYPE (p))
772 return DCL_ELEM (p) * getSize (p->next);
774 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
775 // "can not tell the size of an array[]");
795 /*------------------------------------------------------------------*/
796 /* bitsForType - returns # of bits required to store this type */
797 /*------------------------------------------------------------------*/
799 bitsForType (sym_link * p)
801 /* if nothing return 0 */
806 { /* if this is the specifier then */
808 switch (SPEC_NOUN (p))
809 { /* depending on the specifier type */
811 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
813 return FLOATSIZE * 8;
819 return SPEC_STRUCT (p)->size * 8;
826 return SPEC_BLEN (p);
832 /* this is a specifier */
833 switch (DCL_TYPE (p))
836 return DCL_ELEM (p) * getSize (p->next) * 8;
840 return (PTRSIZE * 8);
845 return (FPTRSIZE * 8);
847 return (GPTRSIZE * 8);
854 /*------------------------------------------------------------------*/
855 /* copySymbolChain - copies a symbol chain */
856 /*------------------------------------------------------------------*/
858 copySymbolChain (symbol * src)
865 dest = copySymbol (src);
866 dest->next = copySymbolChain (src->next);
870 /*------------------------------------------------------------------*/
871 /* copySymbol - makes a copy of a symbol */
872 /*------------------------------------------------------------------*/
874 copySymbol (symbol * src)
881 dest = newSymbol (src->name, src->level);
882 memcpy (dest, src, sizeof (symbol));
883 dest->level = src->level;
884 dest->block = src->block;
885 dest->ival = copyIlist (src->ival);
886 dest->type = copyLinkChain (src->type);
887 dest->etype = getSpec (dest->type);
889 dest->key = src->key;
890 dest->allocreq = src->allocreq;
894 /*------------------------------------------------------------------*/
895 /* reverseSyms - reverses the links for a symbol chain */
896 /*------------------------------------------------------------------*/
898 reverseSyms (symbol * sym)
900 symbol *prev, *curr, *next;
915 sym->next = (void *) NULL;
919 /*------------------------------------------------------------------*/
920 /* reverseLink - reverses the links for a type chain */
921 /*------------------------------------------------------------------*/
923 reverseLink (sym_link * type)
925 sym_link *prev, *curr, *next;
940 type->next = (void *) NULL;
944 /*------------------------------------------------------------------*/
945 /* addSymChain - adds a symbol chain to the symboltable */
946 /*------------------------------------------------------------------*/
948 addSymChain (symbol * symHead)
950 symbol *sym = symHead;
953 for (; sym != NULL; sym = sym->next)
956 checkTypeSanity(sym->etype, sym->name);
958 /* if already exists in the symbol table then check if
959 one of them is an extern definition if yes then
960 then check if the type match, if the types match then
961 delete the current entry and add the new entry */
962 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
963 csym->level == sym->level) {
965 /* one definition extern ? */
966 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
967 /* do types match ? */
968 if (compareType (csym->type, sym->type) != 1) {
970 werror (E_EXTERN_MISMATCH, csym->name);
975 if (compareType (csym->type, sym->type) != 1) {
976 werror (E_DUPLICATE, sym->name);
980 /* delete current entry */
981 deleteSym (SymbolTab, csym, csym->name);
986 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
991 /*------------------------------------------------------------------*/
992 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
993 /*------------------------------------------------------------------*/
995 funcInChain (sym_link * lnk)
1006 /*------------------------------------------------------------------*/
1007 /* structElemType - returns the type info of a sturct member */
1008 /*------------------------------------------------------------------*/
1010 structElemType (sym_link * stype, value * id)
1012 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1013 sym_link *type, *etype;
1014 sym_link *petype = getSpec (stype);
1018 /* look for the id */
1021 if (strcmp (fields->rname, id->name) == 0)
1023 type = copyLinkChain (fields->type);
1024 etype = getSpec (type);
1025 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1026 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1029 fields = fields->next;
1033 werror (E_NOT_MEMBER, id->name);
1035 // the show must go on
1036 return newIntLink();
1039 /*------------------------------------------------------------------*/
1040 /* getStructElement - returns element of a tructure definition */
1041 /*------------------------------------------------------------------*/
1043 getStructElement (structdef * sdef, symbol * sym)
1047 for (field = sdef->fields; field; field = field->next)
1048 if (strcmp (field->name, sym->name) == 0)
1051 werror (E_NOT_MEMBER, sym->name);
1053 return sdef->fields;
1056 /*------------------------------------------------------------------*/
1057 /* compStructSize - computes the size of a structure */
1058 /*------------------------------------------------------------------*/
1060 compStructSize (int su, structdef * sdef)
1062 int sum = 0, usum = 0;
1066 /* for the identifiers */
1067 loop = sdef->fields;
1070 /* create the internal name for this variable */
1071 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1076 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1078 /* if this is a bit field */
1081 /* change it to a unsigned bit */
1082 SPEC_NOUN (loop->etype) = V_BITFIELD;
1083 SPEC_USIGN (loop->etype) = 1;
1084 SPEC_BLEN (loop->etype) = loop->bitVar;
1086 if (loop->bitVar == BITVAR_PAD) {
1087 /* A zero length bitfield forces padding */
1088 SPEC_BSTR (loop->etype) = bitOffset;
1089 SPEC_BLEN (loop->etype) = 0;
1094 if (bitOffset == 8) {
1098 /* check if this fit into the remaining */
1099 /* bits of this byte else align it to the */
1100 /* next byte boundary */
1101 if (loop->bitVar <= (8 - bitOffset)) {
1102 /* fits into current byte */
1104 SPEC_BSTR (loop->etype) = bitOffset;
1105 bitOffset += loop->bitVar;
1107 else if (!bitOffset) {
1108 /* does not fit, but is already byte aligned */
1110 SPEC_BSTR (loop->etype) = bitOffset;
1111 bitOffset += loop->bitVar;
1114 /* does not fit; need to realign first */
1116 loop->offset = (su == UNION ? sum = 0 : sum);
1118 SPEC_BSTR (loop->etype) = bitOffset;
1119 bitOffset += loop->bitVar;
1121 while (bitOffset>8) {
1128 /* This is a non-bit field. Make sure we are */
1129 /* byte aligned first */
1132 loop->offset = (su == UNION ? sum = 0 : sum);
1136 checkDecl (loop, 1);
1137 sum += getSize (loop->type);
1142 /* if union then size = sizeof larget field */
1144 /* For UNION, round up after each field */
1145 sum += ((bitOffset+7)/8);
1146 usum = max (usum, sum);
1151 /* For STRUCT, round up after all fields processed */
1153 sum += ((bitOffset+7)/8);
1155 return (su == UNION ? usum : sum);
1158 /*------------------------------------------------------------------*/
1159 /* checkSClass - check the storage class specification */
1160 /*------------------------------------------------------------------*/
1162 checkSClass (symbol * sym, int isProto)
1164 if (getenv("DEBUG_SANITY")) {
1165 fprintf (stderr, "checkSClass: %s \n", sym->name);
1168 /* type is literal can happen foe enums change
1170 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1171 SPEC_SCLS (sym->etype) = S_AUTO;
1173 /* if sfr or sbit then must also be volatile */
1174 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1175 SPEC_SCLS (sym->etype) == S_SFR)
1177 SPEC_VOLATILE (sym->etype) = 1;
1180 /* if absolute address given then it mark it as
1181 volatile -- except in the PIC port */
1183 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1184 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1185 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1188 if (IS_ABSOLUTE (sym->etype))
1189 SPEC_VOLATILE (sym->etype) = 1;
1192 /* global variables declared const put into code */
1193 /* if no other storage class specified */
1194 if (sym->level == 0 &&
1195 SPEC_CONST (sym->etype) &&
1196 SPEC_SCLS(sym->etype) == S_FIXED &&
1197 !IS_FUNC(sym->type)) {
1198 SPEC_SCLS (sym->etype) = S_CODE;
1201 /* global variable in code space is a constant */
1202 if (sym->level == 0 &&
1203 SPEC_SCLS (sym->etype) == S_CODE &&
1204 port->mem.code_ro) {
1205 if (IS_SPEC(sym->type)) {
1206 SPEC_CONST (sym->type) = 1;
1208 DCL_PTR_CONST (sym->type) = 1;
1212 /* if bit variable then no storage class can be */
1213 /* specified since bit is already a storage */
1214 if (IS_BITVAR (sym->etype) &&
1215 (SPEC_SCLS (sym->etype) != S_FIXED &&
1216 SPEC_SCLS (sym->etype) != S_SBIT &&
1217 SPEC_SCLS (sym->etype) != S_BIT)
1220 werror (E_BITVAR_STORAGE, sym->name);
1221 SPEC_SCLS (sym->etype) = S_FIXED;
1224 /* extern variables cannot be initialized */
1225 if (IS_EXTERN (sym->etype) && sym->ival)
1227 werror (E_EXTERN_INIT, sym->name);
1231 /* if this is an atomatic symbol */
1232 if (sym->level && (options.stackAuto || reentrant)) {
1233 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1234 SPEC_SCLS (sym->etype) == S_FIXED ||
1235 SPEC_SCLS (sym->etype) == S_REGISTER ||
1236 SPEC_SCLS (sym->etype) == S_STACK ||
1237 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1238 SPEC_SCLS (sym->etype) = S_AUTO;
1240 /* storage class may only be specified for statics */
1241 if (!IS_STATIC(sym->etype)) {
1242 werror (E_AUTO_ASSUMED, sym->name);
1247 /* automatic symbols cannot be given */
1248 /* an absolute address ignore it */
1250 SPEC_ABSA (sym->etype) &&
1251 (options.stackAuto || reentrant))
1253 werror (E_AUTO_ABSA, sym->name);
1254 SPEC_ABSA (sym->etype) = 0;
1257 /* arrays & pointers cannot be defined for bits */
1258 /* SBITS or SFRs or BIT */
1259 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1260 (SPEC_NOUN (sym->etype) == V_BIT ||
1261 SPEC_NOUN (sym->etype) == V_SBIT ||
1262 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1263 SPEC_SCLS (sym->etype) == S_SFR))
1264 werror (E_BIT_ARRAY, sym->name);
1266 /* if this is a bit|sbit then set length & start */
1267 if (SPEC_NOUN (sym->etype) == V_BIT ||
1268 SPEC_NOUN (sym->etype) == V_SBIT)
1270 SPEC_BLEN (sym->etype) = 1;
1271 SPEC_BSTR (sym->etype) = 0;
1275 /* variables declared in CODE space must have */
1276 /* initializers if not an extern */
1277 if (SPEC_SCLS (sym->etype) == S_CODE &&
1278 sym->ival == NULL &&
1280 port->mem.code_ro &&
1281 !IS_EXTERN (sym->etype) &&
1282 !funcInChain (sym->type))
1283 werror (E_CODE_NO_INIT, sym->name);
1286 /* if parameter or local variable then change */
1287 /* the storage class to reflect where the var will go */
1288 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1289 !IS_STATIC(sym->etype))
1291 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1293 SPEC_SCLS (sym->etype) = (options.useXstack ?
1294 S_XSTACK : S_STACK);
1298 /* hack-o-matic! I see no reason why the useXstack option should ever
1299 * control this allcoation, but the code was originally that way, and
1300 * changing it for non-390 ports breaks the compiler badly.
1302 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1303 1 : options.useXstack;
1304 SPEC_SCLS (sym->etype) = (useXdata ?
1310 /*------------------------------------------------------------------*/
1311 /* changePointer - change pointer to functions */
1312 /*------------------------------------------------------------------*/
1314 changePointer (symbol * sym)
1318 /* go thru the chain of declarations */
1319 /* if we find a pointer to a function */
1320 /* unconditionally change it to a ptr */
1322 for (p = sym->type; p; p = p->next)
1324 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1325 DCL_TYPE (p) = port->unqualified_pointer;
1326 if (IS_PTR (p) && IS_FUNC (p->next))
1327 DCL_TYPE (p) = CPOINTER;
1331 /*------------------------------------------------------------------*/
1332 /* checkDecl - does semantic validation of a declaration */
1333 /*------------------------------------------------------------------*/
1335 checkDecl (symbol * sym, int isProto)
1338 checkSClass (sym, isProto); /* check the storage class */
1339 changePointer (sym); /* change pointers if required */
1341 /* if this is an array without any dimension
1342 then update the dimension from the initial value */
1343 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1344 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1349 /*------------------------------------------------------------------*/
1350 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1351 /*------------------------------------------------------------------*/
1353 copyLinkChain (sym_link * p)
1355 sym_link *head, *curr, *loop;
1358 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1361 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1362 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1371 /*------------------------------------------------------------------*/
1372 /* cleanUpBlock - cleansup the symbol table specified for all the */
1373 /* symbols in the given block */
1374 /*------------------------------------------------------------------*/
1376 cleanUpBlock (bucket ** table, int block)
1381 /* go thru the entire table */
1382 for (i = 0; i < 256; i++)
1384 for (chain = table[i]; chain; chain = chain->next)
1386 if (chain->block >= block)
1388 deleteSym (table, chain->sym, chain->name);
1394 /*------------------------------------------------------------------*/
1395 /* cleanUpLevel - cleansup the symbol table specified for all the */
1396 /* symbols in the given level */
1397 /*------------------------------------------------------------------*/
1399 cleanUpLevel (bucket ** table, int level)
1404 /* go thru the entire table */
1405 for (i = 0; i < 256; i++)
1407 for (chain = table[i]; chain; chain = chain->next)
1409 if (chain->level >= level)
1411 deleteSym (table, chain->sym, chain->name);
1417 /*------------------------------------------------------------------*/
1418 /* computeType - computes the resultant type from two types */
1419 /*------------------------------------------------------------------*/
1421 computeType (sym_link * type1, sym_link * type2)
1425 sym_link *etype1 = getSpec (type1);
1426 sym_link *etype2 = getSpec (type2);
1428 /* if one of them is a float then result is a float */
1429 /* here we assume that the types passed are okay */
1430 /* and can be cast to one another */
1431 /* which ever is greater in size */
1432 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1433 rType = newFloatLink ();
1435 /* if only one of them is a bit variable
1436 then the other one prevails */
1437 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1438 rType = copyLinkChain (type2);
1439 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1440 rType = copyLinkChain (type1);
1442 /* if one of them is a pointer or array then that
1444 if (IS_PTR (type1) || IS_ARRAY (type1))
1445 rType = copyLinkChain (type1);
1446 else if (IS_PTR (type2) || IS_ARRAY (type2))
1447 rType = copyLinkChain (type2);
1448 else if (getSize (type1) > getSize (type2))
1449 rType = copyLinkChain (type1);
1451 rType = copyLinkChain (type2);
1453 reType = getSpec (rType);
1455 /* if either of them unsigned but not val then make this unsigned */
1456 if (((/*!IS_LITERAL(type1) &&*/ SPEC_USIGN (etype1)) ||
1457 (/*!IS_LITERAL(type2) &&*/ SPEC_USIGN (etype2))) &&
1459 SPEC_USIGN (reType) = 1;
1461 SPEC_USIGN (reType) = 0;
1463 /* if result is a literal then make not so */
1464 if (IS_LITERAL (reType))
1465 SPEC_SCLS (reType) = S_REGISTER;
1470 /*--------------------------------------------------------------------*/
1471 /* compareType - will do type check return 1 if match, -1 if castable */
1472 /*--------------------------------------------------------------------*/
1474 compareType (sym_link * dest, sym_link * src)
1485 /* if dest is a declarator then */
1490 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1492 //checkFunction(src,dest);
1494 return compareType (dest->next, src->next);
1496 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1499 if (IS_PTR (src) && IS_GENPTR (dest))
1501 if (IS_PTR (dest) && IS_ARRAY (src)) {
1502 value *val=aggregateToPointer (valFromType(src));
1503 int res=compareType (dest, val->type);
1504 Safe_free(val->type);
1508 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1509 return compareType (dest->next, src);
1512 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1518 /* if one is a specifier and the other is not */
1519 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1520 (IS_SPEC (dest) && !IS_SPEC (src)))
1523 /* if one of them is a void then ok */
1524 if (SPEC_NOUN (dest) == V_VOID &&
1525 SPEC_NOUN (src) != V_VOID)
1528 if (SPEC_NOUN (dest) != V_VOID &&
1529 SPEC_NOUN (src) == V_VOID)
1532 /* if they are both bitfields then if the lengths
1533 and starts don't match */
1534 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1535 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1536 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1539 /* it is a specifier */
1540 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1542 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1543 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1544 getSize (dest) == getSize (src))
1546 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1551 else if (IS_STRUCT (dest))
1553 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1558 if (SPEC_LONG (dest) != SPEC_LONG (src))
1561 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1567 /*------------------------------------------------------------------*/
1568 /* inCalleeSaveList - return 1 if found in callee save list */
1569 /*------------------------------------------------------------------*/
1571 calleeCmp(void *p1, void *p2)
1573 return (strcmp((char *)p1, (char *)(p2)) == 0);
1577 inCalleeSaveList(char *s)
1579 if (options.all_callee_saves)
1581 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
1584 /*-----------------------------------------------------------------*/
1585 /* aggregateToPointer: change an agggregate type function */
1586 /* argument to a pointer to that type. */
1587 /*-----------------------------------------------------------------*/
1589 aggregateToPointer (value * val)
1591 if (IS_AGGREGATE (val->type))
1593 /* if this is a structure */
1594 /* then we need to add a new link */
1595 if (IS_STRUCT (val->type))
1597 /* first lets add DECLARATOR type */
1598 sym_link *p = val->type;
1600 werror (W_STRUCT_AS_ARG, val->name);
1601 val->type = newLink (DECLARATOR);
1602 val->type->next = p;
1605 /* change to a pointer depending on the */
1606 /* storage class specified */
1607 switch (SPEC_SCLS (val->etype))
1610 DCL_TYPE (val->type) = IPOINTER;
1613 DCL_TYPE (val->type) = PPOINTER;
1616 if (SPEC_OCLS(val->etype)) {
1617 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
1619 // this happens for (external) function parameters
1620 DCL_TYPE (val->type) = port->unqualified_pointer;
1626 DCL_TYPE (val->type) = POINTER;
1629 DCL_TYPE (val->type) = CPOINTER;
1632 DCL_TYPE (val->type) = FPOINTER;
1635 DCL_TYPE (val->type) = EEPPOINTER;
1638 DCL_TYPE (val->type) = port->unqualified_pointer;
1641 /* is there is a symbol associated then */
1642 /* change the type of the symbol as well */
1645 val->sym->type = copyLinkChain (val->type);
1646 val->sym->etype = getSpec (val->sym->type);
1651 /*------------------------------------------------------------------*/
1652 /* checkFunction - does all kinds of check on a function */
1653 /*------------------------------------------------------------------*/
1655 checkFunction (symbol * sym, symbol *csym)
1657 value *exargs, *acargs;
1661 if (getenv("DEBUG_SANITY")) {
1662 fprintf (stderr, "checkFunction: %s ", sym->name);
1665 /* make sure the type is complete and sane */
1666 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1668 /* if not type then some kind of error */
1672 /* if the function has no type then make it return int */
1673 if (!sym->type->next)
1674 sym->type->next = sym->etype = newIntLink ();
1676 /* function cannot return aggregate */
1677 if (IS_AGGREGATE (sym->type->next))
1679 werror (E_FUNC_AGGR, sym->name);
1683 /* function cannot return bit */
1684 if (IS_BITVAR (sym->type->next))
1686 werror (E_FUNC_BIT, sym->name);
1690 /* check if this function is defined as calleeSaves
1691 then mark it as such */
1692 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
1694 /* if interrupt service routine */
1695 /* then it cannot have arguments */
1696 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
1698 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
1699 werror (E_INT_ARGS, sym->name);
1700 FUNC_ARGS(sym->type)=NULL;
1704 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
1706 acargs=acargs->next, argCnt++) {
1708 // this can happen for reentrant functions
1709 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
1710 // the show must go on: synthesize a name and symbol
1711 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
1712 acargs->sym = newSymbol (acargs->name, 1);
1713 SPEC_OCLS (acargs->etype) = istack;
1714 acargs->sym->type = copyLinkChain (acargs->type);
1715 acargs->sym->etype = getSpec (acargs->sym->type);
1716 acargs->sym->_isparm = 1;
1717 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
1718 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
1720 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
1724 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
1725 return 1; /* not defined nothing more to check */
1727 /* check if body already present */
1728 if (csym && IFFUNC_HASBODY(csym->type))
1730 werror (E_FUNC_BODY, sym->name);
1734 /* check the return value type */
1735 if (compareType (csym->type, sym->type) <= 0)
1737 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1738 printFromToType(csym->type, sym->type);
1742 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
1744 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1747 if (FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type))
1749 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1752 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
1754 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1757 /* compare expected args with actual args */
1758 exargs = FUNC_ARGS(csym->type);
1759 acargs = FUNC_ARGS(sym->type);
1761 /* for all the expected args do */
1764 exargs = exargs->next, acargs = acargs->next, argCnt++)
1766 if (getenv("DEBUG_SANITY")) {
1767 fprintf (stderr, "checkFunction: %s ", exargs->name);
1769 /* make sure the type is complete and sane */
1770 checkTypeSanity(exargs->etype, exargs->name);
1772 /* If the actual argument is an array, any prototype
1773 * will have modified it to a pointer. Duplicate that
1776 if (IS_AGGREGATE (acargs->type))
1778 checkValue = copyValue (acargs);
1779 aggregateToPointer (checkValue);
1783 checkValue = acargs;
1786 if (compareType (exargs->type, checkValue->type) <= 0)
1788 werror (E_ARG_TYPE, argCnt);
1789 printFromToType(exargs->type, checkValue->type);
1794 /* if one them ended we have a problem */
1795 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1796 (!exargs && acargs && !IS_VOID (acargs->type)))
1797 werror (E_ARG_COUNT);
1799 /* replace with this defition */
1800 sym->cdef = csym->cdef;
1801 deleteSym (SymbolTab, csym, csym->name);
1802 deleteFromSeg(csym);
1803 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1804 if (IS_EXTERN (csym->etype) && !
1805 IS_EXTERN (sym->etype))
1807 addSet (&publics, sym);
1812 /*------------------------------------------------------------------*/
1813 /* cdbStructBlock - calls struct printing for a blcks */
1814 /*------------------------------------------------------------------*/
1815 void cdbStructBlock (int block)
1818 bucket **table = StructTab;
1821 /* go thru the entire table */
1822 for (i = 0; i < 256; i++)
1824 for (chain = table[i]; chain; chain = chain->next)
1826 if (chain->block >= block)
1829 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
1835 /*-----------------------------------------------------------------*/
1836 /* processFuncArgs - does some processing with function args */
1837 /*-----------------------------------------------------------------*/
1839 processFuncArgs (symbol * func)
1843 sym_link *funcType=func->type;
1845 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
1846 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
1848 // if this is a pointer to a function
1849 if (IS_PTR(funcType)) {
1850 funcType=funcType->next;
1853 /* if this function has variable argument list */
1854 /* then make the function a reentrant one */
1855 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
1856 FUNC_ISREENT(funcType)=1;
1858 /* check if this function is defined as calleeSaves
1859 then mark it as such */
1860 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
1862 /* loop thru all the arguments */
1863 val = FUNC_ARGS(funcType);
1865 /* if it is void then remove parameters */
1866 if (val && IS_VOID (val->type))
1868 FUNC_ARGS(funcType) = NULL;
1872 /* reset regparm for the port */
1873 (*port->reset_regparms) ();
1874 /* if any of the arguments is an aggregate */
1875 /* change it to pointer to the same type */
1879 /* mark it as a register parameter if
1880 the function does not have VA_ARG
1881 and as port dictates */
1882 if (!IFFUNC_HASVARARGS(funcType) &&
1883 (argreg = (*port->reg_parm) (val->type)))
1885 SPEC_REGPARM (val->etype) = 1;
1886 SPEC_ARGREG(val->etype) = argreg;
1887 } else if (IFFUNC_ISREENT(funcType)) {
1888 FUNC_HASSTACKPARM(funcType) = 1;
1891 if (IS_AGGREGATE (val->type))
1893 aggregateToPointer (val);
1900 /* if this is an internal generated function call */
1902 /* ignore --stack-auto for this one, we don't know how it is compiled */
1903 /* simply trust on --int-long-reent or --float-reent */
1904 if (IFFUNC_ISREENT(funcType)) {
1908 /* if this function is reentrant or */
1909 /* automatics r 2b stacked then nothing */
1910 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
1914 val = FUNC_ARGS(funcType);
1919 /* if a symbolname is not given */
1920 /* synthesize a variable name */
1923 SNPRINTF (val->name, sizeof(val->name),
1924 "_%s_PARM_%d", func->name, pNum++);
1925 val->sym = newSymbol (val->name, 1);
1926 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1927 val->sym->type = copyLinkChain (val->type);
1928 val->sym->etype = getSpec (val->sym->type);
1929 val->sym->_isparm = 1;
1930 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
1931 if (IS_SPEC(func->etype)) {
1932 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1933 SPEC_STAT (func->etype);
1935 addSymChain (val->sym);
1938 else /* symbol name given create synth name */
1941 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
1942 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
1943 val->sym->_isparm = 1;
1944 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1945 (options.model != MODEL_SMALL ? xdata : data);
1946 if (IS_SPEC(func->etype)) {
1947 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1948 SPEC_STAT (func->etype);
1951 if (!isinSet(operKeyReset, val->sym)) {
1952 addSet (&operKeyReset, val->sym);
1953 applyToSet (operKeyReset, resetParmKey);
1959 /*-----------------------------------------------------------------*/
1960 /* isSymbolEqual - compares two symbols return 1 if they match */
1961 /*-----------------------------------------------------------------*/
1963 isSymbolEqual (symbol * dest, symbol * src)
1965 /* if pointers match then equal */
1969 /* if one of them is null then don't match */
1973 /* if both of them have rname match on rname */
1974 if (dest->rname[0] && src->rname[0])
1975 return (!strcmp (dest->rname, src->rname));
1977 /* otherwise match on name */
1978 return (!strcmp (dest->name, src->name));
1981 void PT(sym_link *type)
1983 printTypeChain(type,0);
1985 /*-----------------------------------------------------------------*/
1986 /* printTypeChain - prints the type chain in human readable form */
1987 /*-----------------------------------------------------------------*/
1989 printTypeChain (sym_link * start, FILE * of)
1993 sym_link * type, * search;
2003 fprintf (of, "void");
2007 /* Print the chain as it is written in the source: */
2008 /* start with the last entry. */
2009 /* However, the storage class at the end of the */
2010 /* chain reall applies to the first in the chain! */
2012 for (type = start; type && type->next; type = type->next)
2014 scls=SPEC_SCLS(type);
2020 case S_DATA: fprintf (of, "data-"); break;
2021 case S_XDATA: fprintf (of, "xdata-"); break;
2022 case S_SFR: fprintf (of, "sfr-"); break;
2023 case S_SBIT: fprintf (of, "sbit-"); break;
2024 case S_CODE: fprintf (of, "code-"); break;
2025 case S_IDATA: fprintf (of, "idata-"); break;
2026 case S_PDATA: fprintf (of, "pdata-"); break;
2027 case S_LITERAL: fprintf (of, "literal-"); break;
2028 case S_STACK: fprintf (of, "stack-"); break;
2029 case S_XSTACK: fprintf (of, "xstack-"); break;
2030 case S_BIT: fprintf (of, "bit-"); break;
2031 case S_EEPROM: fprintf (of, "eeprom-"); break;
2038 if (!IS_FUNC(type)) {
2039 if (DCL_PTR_VOLATILE (type)) {
2040 fprintf (of, "volatile-");
2042 if (DCL_PTR_CONST (type)) {
2043 fprintf (of, "const-");
2046 switch (DCL_TYPE (type))
2049 fprintf (of, "function %s %s",
2050 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2051 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2053 for (args = FUNC_ARGS(type);
2056 printTypeChain(args->type, of);
2063 fprintf (of, "generic* ");
2066 fprintf (of, "code* ");
2069 fprintf (of, "xdata* ");
2072 fprintf (of, "eeprom* ");
2075 fprintf (of, "near* ");
2078 fprintf (of, "idata* ");
2081 fprintf (of, "pdata* ");
2084 fprintf (of, "unkown* ");
2087 if (DCL_ELEM(type)) {
2088 fprintf (of, "[%d] ", DCL_ELEM(type));
2090 fprintf (of, "[] ");
2097 if (SPEC_VOLATILE (type))
2098 fprintf (of, "volatile-");
2099 if (SPEC_CONST (type))
2100 fprintf (of, "const-");
2101 if (SPEC_USIGN (type))
2102 fprintf (of, "unsigned-");
2103 switch (SPEC_NOUN (type))
2107 fprintf (of, "long-");
2108 fprintf (of, "int");
2112 fprintf (of, "char");
2116 fprintf (of, "void");
2120 fprintf (of, "float");
2124 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2128 fprintf (of, "sbit");
2132 fprintf (of, "bit");
2136 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2140 fprintf (of, "double");
2144 fprintf (of, "unknown type");
2148 /* search entry in list before "type" */
2149 for (search = start; search && search->next != type;)
2150 search = search->next;
2160 /*-----------------------------------------------------------------*/
2161 /* powof2 - returns power of two for the number if number is pow 2 */
2162 /*-----------------------------------------------------------------*/
2164 powof2 (unsigned long num)
2177 if (n1s > 1 || nshifts == 0)
2193 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2194 symbol *__muldiv[3][3][2];
2195 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2196 sym_link *__multypes[3][2];
2197 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2198 symbol *__conv[2][3][2];
2199 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2200 symbol *__rlrr[2][3][2];
2202 sym_link *floatType;
2205 _mangleFunctionName(char *in)
2207 if (port->getMangledFunctionName)
2209 return port->getMangledFunctionName(in);
2217 /*-----------------------------------------------------------------*/
2218 /* typeFromStr - create a typechain from an encoded string */
2219 /* basic types - 'c' - char */
2225 /* '*' - pointer - default (GPOINTER) */
2226 /* modifiers - 'u' - unsigned */
2227 /* pointer modifiers - 'g' - generic */
2231 /* 'F' - function */
2232 /* examples : "ig*" - generic int * */
2233 /* "cx*" - char xdata * */
2234 /* "ui" - unsigned int */
2235 /*-----------------------------------------------------------------*/
2236 sym_link *typeFromStr (char *s)
2238 sym_link *r = newLink(DECLARATOR);
2250 r->class = SPECIFIER;
2251 SPEC_NOUN(r) = V_CHAR;
2255 r->class = SPECIFIER;
2256 SPEC_NOUN(r) = V_INT;
2259 r->class = SPECIFIER;
2260 SPEC_NOUN(r) = V_INT;
2264 r->class = SPECIFIER;
2265 SPEC_NOUN(r) = V_FLOAT;
2268 r->class = SPECIFIER;
2269 SPEC_NOUN(r) = V_VOID;
2272 DCL_TYPE(r) = port->unqualified_pointer;
2279 assert(*(s+1)=='*');
2280 nr = newLink(DECLARATOR);
2285 DCL_TYPE(r) = GPOINTER;
2288 DCL_TYPE(r) = FPOINTER;
2291 DCL_TYPE(r) = CPOINTER;
2294 DCL_TYPE(r) = POINTER;
2297 DCL_TYPE(r) = FUNCTION;
2298 nr = newLink(DECLARATOR);
2301 DCL_TYPE(r) = CPOINTER;
2307 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
2308 "typeFromStr: unknown type");
2311 if (IS_SPEC(r) && usign) {
2320 /*-----------------------------------------------------------------*/
2321 /* initCSupport - create functions for C support routines */
2322 /*-----------------------------------------------------------------*/
2326 const char *smuldivmod[] =
2330 const char *sbwd[] =
2332 "char", "int", "long"
2338 const char *srlrr[] =
2343 int bwd, su, muldivmod, tofrom, rlrr;
2345 if (getenv("SDCC_NO_C_SUPPORT")) {
2346 /* for debugging only */
2350 floatType = newFloatLink ();
2352 for (bwd = 0; bwd < 3; bwd++)
2369 __multypes[bwd][0] = l;
2370 __multypes[bwd][1] = copyLinkChain (l);
2371 SPEC_USIGN (__multypes[bwd][1]) = 1;
2374 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2375 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2376 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2377 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2378 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2379 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2380 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2381 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2382 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2383 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2385 for (tofrom = 0; tofrom < 2; tofrom++)
2387 for (bwd = 0; bwd < 3; bwd++)
2389 for (su = 0; su < 2; su++)
2393 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
2394 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
2398 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
2399 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
2406 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2408 for (bwd = 0; bwd < 3; bwd++)
2410 for (su = 0; su < 2; su++)
2412 SNPRINTF (buffer, sizeof(buffer),
2414 smuldivmod[muldivmod],
2417 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2418 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2423 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
2424 Therefore they've been merged into mulint() and mullong().
2427 for (bwd = 0; bwd < 3; bwd++)
2429 for (su = 0; su < 2; su++)
2431 for (muldivmod = 1; muldivmod < 3; muldivmod++)
2434 SNPRINTF (buffer, sizeof(buffer),
2436 smuldivmod[muldivmod],
2439 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2440 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2448 for (su = 0; su < 2; su++)
2450 /* muluchar and mulschar are still separate functions, because e.g. the z80
2451 port is sign/zero-extending to int before calling mulint() */
2452 SNPRINTF (buffer, sizeof(buffer),
2454 smuldivmod[muldivmod],
2457 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2458 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2462 /* word and doubleword */
2463 for (bwd = 1; bwd < 3; bwd++)
2466 SNPRINTF (buffer, sizeof(buffer),
2468 smuldivmod[muldivmod],
2470 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2471 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
2472 /* signed = unsigned */
2473 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
2476 for (rlrr = 0; rlrr < 2; rlrr++)
2478 for (bwd = 0; bwd < 3; bwd++)
2480 for (su = 0; su < 2; su++)
2482 SNPRINTF (buffer, sizeof(buffer),
2487 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2488 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
2494 /*-----------------------------------------------------------------*/
2495 /* initBuiltIns - create prototypes for builtin functions */
2496 /*-----------------------------------------------------------------*/
2502 if (!port->builtintable) return ;
2504 for (i = 0 ; port->builtintable[i].name ; i++) {
2505 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
2506 port->builtintable[i].nParms,port->builtintable[i].parm_types);
2507 FUNC_ISBUILTIN(sym->type) = 1;
2508 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
2512 sym_link *validateLink(sym_link *l,
2519 if (l && l->class==select)
2524 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
2525 " expected %s, got %s\n",
2526 macro, args, file, line,
2527 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
2529 return l; // never reached, makes compiler happy.