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 */
296 sym->fileDef = currFname;
300 /*------------------------------------------------------------------*/
301 /* newLink - creates a new link (declarator,specifier) */
302 /*------------------------------------------------------------------*/
304 newLink (SYM_LINK_CLASS select)
308 p = Safe_alloc ( sizeof (sym_link));
314 /*------------------------------------------------------------------*/
315 /* newStruct - creats a new structdef from the free list */
316 /*------------------------------------------------------------------*/
318 newStruct (char *tag)
322 s = Safe_alloc ( sizeof (structdef));
324 strncpyz (s->tag, tag, sizeof(s->tag)); /* copy the tag */
328 /*------------------------------------------------------------------*/
329 /* sclsFromPtr - Return the storage class a pointer points into. */
330 /* S_FIXED is returned for generic pointers or other */
331 /* unexpected cases */
332 /*------------------------------------------------------------------*/
334 sclsFromPtr(sym_link *ptr)
336 switch (DCL_TYPE (ptr))
359 /*------------------------------------------------------------------*/
360 /* pointerTypes - do the computation for the pointer types */
361 /*------------------------------------------------------------------*/
363 pointerTypes (sym_link * ptr, sym_link * type)
368 /* find the first pointer type */
369 while (ptr && !IS_PTR (ptr))
372 /* could not find it */
373 if (!ptr || IS_SPEC (ptr))
376 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
377 pointerTypes (ptr->next, type);
381 /* change the pointer type depending on the
382 storage class of the type */
385 switch (SPEC_SCLS (type))
388 DCL_TYPE (ptr) = FPOINTER;
391 DCL_TYPE (ptr) = IPOINTER;
394 DCL_TYPE (ptr) = PPOINTER;
397 DCL_TYPE (ptr) = POINTER;
400 DCL_TYPE (ptr) = CPOINTER;
403 DCL_TYPE (ptr) = EEPPOINTER;
406 DCL_TYPE (ptr) = port->unqualified_pointer;
409 /* the storage class of type ends here */
410 SPEC_SCLS (type) = 0;
413 /* now change all the remaining unknown pointers
414 to generic pointers */
417 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
418 DCL_TYPE (ptr) = port->unqualified_pointer;
422 /* same for the type although it is highly unlikely that
423 type will have a pointer */
426 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
427 DCL_TYPE (type) = port->unqualified_pointer;
432 /*------------------------------------------------------------------*/
433 /* addDecl - adds a declarator @ the end of a chain */
434 /*------------------------------------------------------------------*/
436 addDecl (symbol * sym, int type, sym_link * p)
442 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
443 fprintf (stderr, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym->name, type, p);
445 /* if we are passed a link then set head & tail */
454 head = tail = newLink (DECLARATOR);
455 DCL_TYPE (head) = type;
458 /* if this is the first entry */
466 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
468 sym->etype = mergeSpec (sym->etype, head, sym->name);
472 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
475 while (t->next != sym->etype)
478 tail->next = sym->etype;
482 sym->etype->next = head;
488 /* if the type is an unknown pointer and has
489 a tspec then take the storage class const & volatile
490 attribute from the tspec & make it those of this
494 //DCL_TYPE (p) == UPOINTER &&
497 if (!IS_SPEC (sym->etype))
499 sym->etype = sym->etype->next = newLink (SPECIFIER);
501 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
502 DCL_TSPEC (p) = NULL;
505 // if there is a function in this type chain
506 if (p && funcInChain(sym->type)) {
507 processFuncArgs (sym);
513 /*------------------------------------------------------------------
514 checkTypeSanity: prevent the user from doing e.g.:
516 ------------------------------------------------------------------*/
517 void checkTypeSanity(sym_link *etype, char *name) {
521 if (getenv("DEBUG_SANITY")) {
522 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
527 if (!IS_SPEC(etype)) {
528 if (getenv("DEBUG_SANITY")) {
529 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
534 noun=nounName(etype);
536 if (getenv("DEBUG_SANITY")) {
537 fprintf (stderr, "checking sanity for %s %p\n", name, etype);
540 if ((SPEC_NOUN(etype)==V_CHAR ||
541 SPEC_NOUN(etype)==V_FLOAT ||
542 SPEC_NOUN(etype)==V_DOUBLE ||
543 SPEC_NOUN(etype)==V_VOID) &&
544 (etype->select.s._short || SPEC_LONG(etype))) {
545 // long or short for char float double or void
546 werror (E_LONG_OR_SHORT_INVALID, noun, name);
548 if ((SPEC_NOUN(etype)==V_FLOAT ||
549 SPEC_NOUN(etype)==V_DOUBLE ||
550 SPEC_NOUN(etype)==V_VOID) &&
551 (etype->select.s._signed || SPEC_USIGN(etype))) {
552 // signed or unsigned for float double or void
553 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
556 // special case for "short"
557 if (etype->select.s._short) {
558 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
559 etype->select.s._short = 0;
563 "const a;" or "data b;" or "signed s" or "long l"
565 if (!SPEC_NOUN(etype)) {
566 SPEC_NOUN(etype)=V_INT;
569 if (etype->select.s._signed && SPEC_USIGN(etype)) {
570 // signed AND unsigned
571 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
573 if (etype->select.s._short && SPEC_LONG(etype)) {
575 werror (E_LONG_AND_SHORT_INVALID, noun, name);
580 /*------------------------------------------------------------------*/
581 /* mergeSpec - merges two specifiers and returns the new one */
582 /*------------------------------------------------------------------*/
584 mergeSpec (sym_link * dest, sym_link * src, char *name)
586 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
588 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
591 werror (E_SYNTAX_ERROR, yytext);
592 // the show must go on
597 if (SPEC_NOUN(src)) {
598 if (!SPEC_NOUN(dest)) {
599 SPEC_NOUN(dest)=SPEC_NOUN(src);
601 /* we shouldn't redeclare the type */
602 if (getenv("DEBUG_SANITY")) {
603 fprintf (stderr, "mergeSpec: ");
605 werror(E_TWO_OR_MORE_DATA_TYPES, name);
609 if (SPEC_SCLS(src)) {
610 /* if destination has no storage class */
611 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
612 SPEC_SCLS (dest) = SPEC_SCLS (src);
614 if (getenv("DEBUG_SANITY")) {
615 fprintf (stderr, "mergeSpec: ");
617 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
621 /* copy all the specifications */
623 // we really should do:
625 if (SPEC_what(src)) {
626 if (SPEC_what(dest)) {
627 werror(W_DUPLICATE_SPEC, "what");
629 SPEC_what(dst)|=SPEC_what(src);
632 // but there are more important thing right now
634 SPEC_LONG (dest) |= SPEC_LONG (src);
635 dest->select.s._short|=src->select.s._short;
636 SPEC_USIGN (dest) |= SPEC_USIGN (src);
637 dest->select.s._signed|=src->select.s._signed;
638 SPEC_STAT (dest) |= SPEC_STAT (src);
639 SPEC_EXTR (dest) |= SPEC_EXTR (src);
640 SPEC_CONST(dest) |= SPEC_CONST (src);
641 SPEC_ABSA (dest) |= SPEC_ABSA (src);
642 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
643 SPEC_ADDR (dest) |= SPEC_ADDR (src);
644 SPEC_OCLS (dest) = SPEC_OCLS (src);
645 SPEC_BLEN (dest) |= SPEC_BLEN (src);
646 SPEC_BSTR (dest) |= SPEC_BSTR (src);
647 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
648 SPEC_ENUM (dest) |= SPEC_ENUM (src);
649 if (SPEC_ARGREG(src) && !SPEC_ARGREG(dest))
650 SPEC_ARGREG(dest) = SPEC_ARGREG(src);
652 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
653 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
655 /* these are the only function attributes that will be set
656 in a specifier while parsing */
657 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
658 FUNC_BANKED(dest) |= FUNC_BANKED(src);
659 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
660 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
661 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
662 FUNC_ISISR(dest) |= FUNC_ISISR(src);
663 FUNC_ISJAVANATIVE(dest) |= FUNC_ISJAVANATIVE(src);
664 FUNC_ISBUILTIN(dest) |= FUNC_ISBUILTIN(src);
665 FUNC_ISOVERLAY(dest) |= FUNC_ISOVERLAY(src);
666 FUNC_INTNO(dest) |= FUNC_INTNO(src);
667 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
672 /*------------------------------------------------------------------*/
673 /* genSymName - generates and returns a name used for anonymous vars */
674 /*------------------------------------------------------------------*/
676 genSymName (int level)
678 static int gCount = 0;
679 static char gname[SDCC_NAME_MAX + 1];
681 SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
685 /*------------------------------------------------------------------*/
686 /* getSpec - returns the specifier part from a declaration chain */
687 /*------------------------------------------------------------------*/
689 getSpec (sym_link * p)
694 while (p && !(IS_SPEC (p)))
700 /*------------------------------------------------------------------*/
701 /* newCharLink() - creates an char type */
702 /*------------------------------------------------------------------*/
708 p = newLink (SPECIFIER);
709 SPEC_NOUN (p) = V_CHAR;
714 /*------------------------------------------------------------------*/
715 /* newFloatLink - a new Float type */
716 /*------------------------------------------------------------------*/
722 p = newLink (SPECIFIER);
723 SPEC_NOUN (p) = V_FLOAT;
728 /*------------------------------------------------------------------*/
729 /* newLongLink() - new long type */
730 /*------------------------------------------------------------------*/
736 p = newLink (SPECIFIER);
737 SPEC_NOUN (p) = V_INT;
743 /*------------------------------------------------------------------*/
744 /* newIntLink() - creates an int type */
745 /*------------------------------------------------------------------*/
751 p = newLink (SPECIFIER);
752 SPEC_NOUN (p) = V_INT;
757 /*------------------------------------------------------------------*/
758 /* getSize - returns size of a type chain in bits */
759 /*------------------------------------------------------------------*/
761 getSize (sym_link * p)
763 /* if nothing return 0 */
767 { /* if this is the specifier then */
768 switch (SPEC_NOUN (p))
769 { /* depending on the specifier type */
771 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
779 return SPEC_STRUCT (p)->size;
786 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
792 /* this is a specifier */
793 switch (DCL_TYPE (p))
797 return DCL_ELEM (p) * getSize (p->next);
799 // werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
800 // "can not tell the size of an array[]");
820 /*------------------------------------------------------------------*/
821 /* bitsForType - returns # of bits required to store this type */
822 /*------------------------------------------------------------------*/
824 bitsForType (sym_link * p)
826 /* if nothing return 0 */
831 { /* if this is the specifier then */
833 switch (SPEC_NOUN (p))
834 { /* depending on the specifier type */
836 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
838 return FLOATSIZE * 8;
844 return SPEC_STRUCT (p)->size * 8;
851 return SPEC_BLEN (p);
857 /* this is a specifier */
858 switch (DCL_TYPE (p))
861 return DCL_ELEM (p) * getSize (p->next) * 8;
865 return (PTRSIZE * 8);
870 return (FPTRSIZE * 8);
872 return (GPTRSIZE * 8);
879 /*------------------------------------------------------------------*/
880 /* copySymbolChain - copies a symbol chain */
881 /*------------------------------------------------------------------*/
883 copySymbolChain (symbol * src)
890 dest = copySymbol (src);
891 dest->next = copySymbolChain (src->next);
895 /*------------------------------------------------------------------*/
896 /* copySymbol - makes a copy of a symbol */
897 /*------------------------------------------------------------------*/
899 copySymbol (symbol * src)
906 dest = newSymbol (src->name, src->level);
907 memcpy (dest, src, sizeof (symbol));
908 dest->level = src->level;
909 dest->block = src->block;
910 dest->ival = copyIlist (src->ival);
911 dest->type = copyLinkChain (src->type);
912 dest->etype = getSpec (dest->type);
914 dest->key = src->key;
915 dest->allocreq = src->allocreq;
919 /*------------------------------------------------------------------*/
920 /* reverseSyms - reverses the links for a symbol chain */
921 /*------------------------------------------------------------------*/
923 reverseSyms (symbol * sym)
925 symbol *prev, *curr, *next;
940 sym->next = (void *) NULL;
944 /*------------------------------------------------------------------*/
945 /* reverseLink - reverses the links for a type chain */
946 /*------------------------------------------------------------------*/
948 reverseLink (sym_link * type)
950 sym_link *prev, *curr, *next;
965 type->next = (void *) NULL;
969 /*------------------------------------------------------------------*/
970 /* addSymChain - adds a symbol chain to the symboltable */
971 /*------------------------------------------------------------------*/
973 addSymChain (symbol * symHead)
975 symbol *sym = symHead;
979 for (; sym != NULL; sym = sym->next)
982 checkTypeSanity(sym->etype, sym->name);
984 if (!sym->level && !(IS_SPEC(sym->etype) && IS_TYPEDEF(sym->etype)))
987 /* if already exists in the symbol table then check if
988 one of them is an extern definition if yes then
989 then check if the type match, if the types match then
990 delete the current entry and add the new entry */
991 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
992 csym->level == sym->level) {
994 /* If the previous definition was for an array with incomplete */
995 /* type, and the new definition has completed the type, update */
996 /* the original type to match */
997 if (IS_DECL(csym->type) && DCL_TYPE(csym->type)==ARRAY
998 && IS_DECL(sym->type) && DCL_TYPE(sym->type)==ARRAY)
1000 if (!DCL_ELEM(csym->type) && DCL_ELEM(sym->type))
1001 DCL_ELEM(csym->type) = DCL_ELEM(sym->type);
1005 /* If only one of the definitions used the "at" keyword, copy */
1006 /* the address to the other. */
1007 if (IS_SPEC(csym->etype) && SPEC_ABSA(csym->etype)
1008 && IS_SPEC(sym->etype) && !SPEC_ABSA(sym->etype))
1010 SPEC_ABSA (sym->etype) = 1;
1011 SPEC_ADDR (sym->etype) = SPEC_ADDR (csym->etype);
1013 if (IS_SPEC(csym->etype) && !SPEC_ABSA(csym->etype)
1014 && IS_SPEC(sym->etype) && SPEC_ABSA(sym->etype))
1016 SPEC_ABSA (csym->etype) = 1;
1017 SPEC_ADDR (csym->etype) = SPEC_ADDR (sym->etype);
1022 if (csym->ival && sym->ival)
1024 if (compareTypeExact (csym->type, sym->type, sym->level) != 1)
1028 /* one definition extern ? */
1029 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype))
1030 werror (E_EXTERN_MISMATCH, sym->name);
1032 werror (E_DUPLICATE, sym->name);
1033 fprintf (stderr, "from type '");
1034 printTypeChain (csym->type, stderr);
1035 if (IS_SPEC (csym->etype) && SPEC_ABSA (csym->etype))
1036 fprintf(stderr, " at 0x%x", SPEC_ADDR (csym->etype));
1037 fprintf (stderr, "'\nto type '");
1038 printTypeChain (sym->type, stderr);
1039 if (IS_SPEC (sym->etype) && SPEC_ABSA (sym->etype))
1040 fprintf(stderr, " at 0x%x", SPEC_ADDR (sym->etype));
1041 fprintf (stderr, "'\n");
1045 if (csym->ival && !sym->ival)
1046 sym->ival = csym->ival;
1048 /* delete current entry */
1049 deleteSym (SymbolTab, csym, csym->name);
1050 deleteFromSeg(csym);
1054 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1059 /*------------------------------------------------------------------*/
1060 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1061 /*------------------------------------------------------------------*/
1063 funcInChain (sym_link * lnk)
1074 /*------------------------------------------------------------------*/
1075 /* structElemType - returns the type info of a struct member */
1076 /*------------------------------------------------------------------*/
1078 structElemType (sym_link * stype, value * id)
1080 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1081 sym_link *type, *etype;
1082 sym_link *petype = getSpec (stype);
1086 /* look for the id */
1089 if (strcmp (fields->rname, id->name) == 0)
1091 type = copyLinkChain (fields->type);
1092 etype = getSpec (type);
1093 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1094 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1096 SPEC_CONST (type) |= SPEC_CONST (stype);
1098 DCL_PTR_CONST (type) |= SPEC_CONST (stype);
1101 fields = fields->next;
1105 werror (E_NOT_MEMBER, id->name);
1107 // the show must go on
1108 return newIntLink();
1111 /*------------------------------------------------------------------*/
1112 /* getStructElement - returns element of a tructure definition */
1113 /*------------------------------------------------------------------*/
1115 getStructElement (structdef * sdef, symbol * sym)
1119 for (field = sdef->fields; field; field = field->next)
1120 if (strcmp (field->name, sym->name) == 0)
1123 werror (E_NOT_MEMBER, sym->name);
1125 return sdef->fields;
1128 /*------------------------------------------------------------------*/
1129 /* compStructSize - computes the size of a structure */
1130 /*------------------------------------------------------------------*/
1132 compStructSize (int su, structdef * sdef)
1134 int sum = 0, usum = 0;
1138 /* for the identifiers */
1139 loop = sdef->fields;
1142 /* create the internal name for this variable */
1143 SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
1148 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1150 /* if this is a bit field */
1153 /* change it to a unsigned bit */
1154 SPEC_NOUN (loop->etype) = V_BITFIELD;
1155 SPEC_USIGN (loop->etype) = 1;
1156 SPEC_BLEN (loop->etype) = loop->bitVar;
1158 if (loop->bitVar == BITVAR_PAD) {
1159 /* A zero length bitfield forces padding */
1160 SPEC_BSTR (loop->etype) = bitOffset;
1161 SPEC_BLEN (loop->etype) = 0;
1166 if (bitOffset == 8) {
1170 /* check if this fit into the remaining */
1171 /* bits of this byte else align it to the */
1172 /* next byte boundary */
1173 if (loop->bitVar <= (8 - bitOffset)) {
1174 /* fits into current byte */
1176 SPEC_BSTR (loop->etype) = bitOffset;
1177 bitOffset += loop->bitVar;
1179 else if (!bitOffset) {
1180 /* does not fit, but is already byte aligned */
1182 SPEC_BSTR (loop->etype) = bitOffset;
1183 bitOffset += loop->bitVar;
1186 /* does not fit; need to realign first */
1188 loop->offset = (su == UNION ? sum = 0 : sum);
1190 SPEC_BSTR (loop->etype) = bitOffset;
1191 bitOffset += loop->bitVar;
1193 while (bitOffset>8) {
1200 /* This is a non-bit field. Make sure we are */
1201 /* byte aligned first */
1204 loop->offset = (su == UNION ? sum = 0 : sum);
1208 checkDecl (loop, 1);
1209 sum += getSize (loop->type);
1214 /* if union then size = sizeof larget field */
1216 /* For UNION, round up after each field */
1217 sum += ((bitOffset+7)/8);
1218 usum = max (usum, sum);
1223 /* For STRUCT, round up after all fields processed */
1225 sum += ((bitOffset+7)/8);
1227 return (su == UNION ? usum : sum);
1230 /*------------------------------------------------------------------*/
1231 /* checkSClass - check the storage class specification */
1232 /*------------------------------------------------------------------*/
1234 checkSClass (symbol * sym, int isProto)
1238 if (getenv("DEBUG_SANITY")) {
1239 fprintf (stderr, "checkSClass: %s \n", sym->name);
1242 /* type is literal can happen for enums change
1244 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1245 SPEC_SCLS (sym->etype) = S_AUTO;
1247 /* if sfr or sbit then must also be volatile */
1248 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1249 SPEC_SCLS (sym->etype) == S_SFR)
1251 SPEC_VOLATILE (sym->etype) = 1;
1254 /* if absolute address given then it mark it as
1255 volatile -- except in the PIC port */
1257 #if !OPT_DISABLE_PIC || !OPT_DISABLE_PIC16
1258 /* The PIC port uses a different peep hole optimizer based on "pCode" */
1259 if (!TARGET_IS_PIC && !TARGET_IS_PIC16)
1262 if (IS_ABSOLUTE (sym->etype))
1263 SPEC_VOLATILE (sym->etype) = 1;
1265 /* If code memory is read only, then pointers to code memory */
1266 /* implicitly point to constants -- make this explicit */
1268 while (t && t->next) {
1269 if (IS_CODEPTR(t) && port->mem.code_ro) {
1270 if (IS_SPEC(t->next)) {
1271 SPEC_CONST (t->next) = 1;
1273 DCL_PTR_CONST (t->next) = 1;
1279 /* global variables declared const put into code */
1280 /* if no other storage class specified */
1281 if (sym->level == 0 &&
1282 SPEC_SCLS(sym->etype) == S_FIXED &&
1283 !IS_FUNC(sym->type)) {
1284 /* find the first non-array link */
1288 if (IS_CONSTANT (t)) {
1289 SPEC_SCLS (sym->etype) = S_CODE;
1293 /* global variable in code space is a constant */
1294 if (sym->level == 0 &&
1295 SPEC_SCLS (sym->etype) == S_CODE &&
1296 port->mem.code_ro) {
1297 /* find the first non-array link */
1304 DCL_PTR_CONST (t) = 1;
1308 /* if bit variable then no storage class can be */
1309 /* specified since bit is already a storage */
1310 if (IS_BITVAR (sym->etype) &&
1311 (SPEC_SCLS (sym->etype) != S_FIXED &&
1312 SPEC_SCLS (sym->etype) != S_SBIT &&
1313 SPEC_SCLS (sym->etype) != S_BIT)
1316 werror (E_BITVAR_STORAGE, sym->name);
1317 SPEC_SCLS (sym->etype) = S_FIXED;
1320 /* extern variables cannot be initialized */
1321 if (IS_EXTERN (sym->etype) && sym->ival)
1323 werror (E_EXTERN_INIT, sym->name);
1327 /* if this is an automatic symbol */
1328 if (sym->level && (options.stackAuto || reentrant)) {
1329 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1330 SPEC_SCLS (sym->etype) == S_FIXED ||
1331 SPEC_SCLS (sym->etype) == S_REGISTER ||
1332 SPEC_SCLS (sym->etype) == S_STACK ||
1333 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1334 SPEC_SCLS (sym->etype) = S_AUTO;
1336 /* storage class may only be specified for statics */
1337 if (!IS_STATIC(sym->etype)) {
1338 werror (E_AUTO_ASSUMED, sym->name);
1343 /* automatic symbols cannot be given */
1344 /* an absolute address ignore it */
1346 SPEC_ABSA (sym->etype) &&
1347 (options.stackAuto || reentrant))
1349 werror (E_AUTO_ABSA, sym->name);
1350 SPEC_ABSA (sym->etype) = 0;
1353 /* arrays & pointers cannot be defined for bits */
1354 /* SBITS or SFRs or BIT */
1355 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1356 (SPEC_NOUN (sym->etype) == V_BIT ||
1357 SPEC_NOUN (sym->etype) == V_SBIT ||
1358 SPEC_NOUN (sym->etype) == V_BITFIELD ||
1359 SPEC_SCLS (sym->etype) == S_SFR))
1360 werror (E_BIT_ARRAY, sym->name);
1362 /* if this is a bit|sbit then set length & start */
1363 if (SPEC_NOUN (sym->etype) == V_BIT ||
1364 SPEC_NOUN (sym->etype) == V_SBIT)
1366 SPEC_BLEN (sym->etype) = 1;
1367 SPEC_BSTR (sym->etype) = 0;
1371 /* variables declared in CODE space must have */
1372 /* initializers if not an extern */
1373 if (SPEC_SCLS (sym->etype) == S_CODE &&
1374 sym->ival == NULL &&
1376 port->mem.code_ro &&
1377 !IS_EXTERN (sym->etype) &&
1378 !funcInChain (sym->type))
1379 werror (E_CODE_NO_INIT, sym->name);
1382 /* if parameter or local variable then change */
1383 /* the storage class to reflect where the var will go */
1384 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1385 !IS_STATIC(sym->etype))
1387 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1389 SPEC_SCLS (sym->etype) = (options.useXstack ?
1390 S_XSTACK : S_STACK);
1394 /* hack-o-matic! I see no reason why the useXstack option should ever
1395 * control this allcoation, but the code was originally that way, and
1396 * changing it for non-390 ports breaks the compiler badly.
1398 bool useXdata = (TARGET_IS_DS390 || TARGET_IS_DS400) ?
1399 1 : options.useXstack;
1400 SPEC_SCLS (sym->etype) = (useXdata ?
1406 /*------------------------------------------------------------------*/
1407 /* changePointer - change pointer to functions */
1408 /*------------------------------------------------------------------*/
1410 changePointer (symbol * sym)
1414 /* go thru the chain of declarations */
1415 /* if we find a pointer to a function */
1416 /* unconditionally change it to a ptr */
1418 for (p = sym->type; p; p = p->next)
1420 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1421 DCL_TYPE (p) = port->unqualified_pointer;
1422 if (IS_PTR (p) && IS_FUNC (p->next))
1423 DCL_TYPE (p) = CPOINTER;
1427 /*------------------------------------------------------------------*/
1428 /* checkDecl - does semantic validation of a declaration */
1429 /*------------------------------------------------------------------*/
1431 checkDecl (symbol * sym, int isProto)
1434 checkSClass (sym, isProto); /* check the storage class */
1435 changePointer (sym); /* change pointers if required */
1437 /* if this is an array without any dimension
1438 then update the dimension from the initial value */
1439 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1440 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1445 /*------------------------------------------------------------------*/
1446 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1447 /*------------------------------------------------------------------*/
1449 copyLinkChain (sym_link * p)
1451 sym_link *head, *curr, *loop;
1454 head = loop = (curr ? newLink (p->class) : (void *) NULL);
1457 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1458 loop->next = (curr->next ? newLink (curr->next->class) : (void *) NULL);
1467 /*------------------------------------------------------------------*/
1468 /* cleanUpBlock - cleansup the symbol table specified for all the */
1469 /* symbols in the given block */
1470 /*------------------------------------------------------------------*/
1472 cleanUpBlock (bucket ** table, int block)
1477 /* go thru the entire table */
1478 for (i = 0; i < 256; i++)
1480 for (chain = table[i]; chain; chain = chain->next)
1482 if (chain->block >= block)
1484 deleteSym (table, chain->sym, chain->name);
1490 /*------------------------------------------------------------------*/
1491 /* cleanUpLevel - cleansup the symbol table specified for all the */
1492 /* symbols in the given level */
1493 /*------------------------------------------------------------------*/
1495 cleanUpLevel (bucket ** table, int level)
1500 /* go thru the entire table */
1501 for (i = 0; i < 256; i++)
1503 for (chain = table[i]; chain; chain = chain->next)
1505 if (chain->level >= level)
1507 deleteSym (table, chain->sym, chain->name);
1513 /*------------------------------------------------------------------*/
1514 /* computeType - computes the resultant type from two types */
1515 /*------------------------------------------------------------------*/
1517 computeType (sym_link * type1, sym_link * type2, bool promoteCharToInt)
1521 sym_link *etype1 = getSpec (type1);
1524 etype2 = type2 ? getSpec (type2) : type1;
1526 /* if one of them is a float then result is a float */
1527 /* here we assume that the types passed are okay */
1528 /* and can be cast to one another */
1529 /* which ever is greater in size */
1530 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1531 rType = newFloatLink ();
1533 /* if only one of them is a bit variable
1534 then the other one prevails */
1535 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1536 rType = copyLinkChain (type2);
1537 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1538 rType = copyLinkChain (type1);
1540 /* if one of them is a pointer or array then that
1542 if (IS_PTR (type1) || IS_ARRAY (type1))
1543 rType = copyLinkChain (type1);
1544 else if (IS_PTR (type2) || IS_ARRAY (type2))
1545 rType = copyLinkChain (type2);
1546 else if (getSize (type1) > getSize (type2))
1547 rType = copyLinkChain (type1);
1549 rType = copyLinkChain (type2);
1551 reType = getSpec (rType);
1553 /* avoid conflicting types */
1554 reType->select.s._signed = 0;
1556 if (IS_CHAR (reType) && promoteCharToInt)
1557 SPEC_NOUN (reType) = V_INT;
1559 /* SDCC's sign promotion:
1560 - if one or both operands are unsigned, the resultant type will be unsigned
1561 (except char, see below)
1562 - if an operand is promoted to a larger type (char -> int, int -> long),
1563 the larger type will be signed
1565 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
1566 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
1567 the standard. The standard demands, that the result has to be the same
1568 "as if" the promotion would have been performed:
1570 - if the result of an operation with two char's is promoted to a
1571 larger type, the result will be signed.
1573 More sophisticated is the last one:
1574 - if the result of an operation with two char's is a char again,
1575 the result will only then be unsigned, if both operands are
1576 unsigned. In all other cases the result will be signed.
1578 This seems to be contradictionary to the first two rules, but it makes
1579 real sense (all types are char's):
1581 A signed char can be negative; this must be preserved in the result
1584 Only if both operands are unsigned it's safe to make the result
1585 unsigned; this helps to avoid overflow:
1588 Homework: - why is (200 * 200 < 0) true?
1589 - why is { char l = 200, r = 200; (r * l > 0) } true?
1592 if (!IS_FLOAT (reType)
1593 && ( (SPEC_USIGN (etype1)
1594 /* if this operand is promoted to a larger type,
1595 then it will be promoted to a signed type */
1596 && !(getSize (etype1) < getSize (reType))
1597 /* char require special handling */
1598 && !IS_CHAR (etype1))
1599 || /* same for 2nd operand */
1600 (SPEC_USIGN (etype2)
1601 && !(getSize (etype2) < getSize (reType))
1602 && !IS_CHAR (etype2))
1603 || /* if both are 'unsigned char' and not promoted
1604 let the result be unsigned too */
1605 ( SPEC_USIGN (etype1)
1606 && SPEC_USIGN (etype2)
1609 && IS_CHAR (reType))))
1610 SPEC_USIGN (reType) = 1;
1612 SPEC_USIGN (reType) = 0;
1614 /* if result is a literal then make not so */
1615 if (IS_LITERAL (reType))
1616 SPEC_SCLS (reType) = S_REGISTER;
1621 /*--------------------------------------------------------------------*/
1622 /* compareType - will do type check return 1 if match, -1 if castable */
1623 /*--------------------------------------------------------------------*/
1625 compareType (sym_link * dest, sym_link * src)
1636 /* if dest is a declarator then */
1641 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1643 //checkFunction(src,dest);
1645 return compareType (dest->next, src->next);
1647 if (IS_PTR (dest) && IS_GENPTR (src) && IS_VOID(src->next)) {
1650 if (IS_PTR (src) && IS_GENPTR (dest))
1652 if (IS_PTR (dest) && IS_ARRAY (src)) {
1653 value *val=aggregateToPointer (valFromType(src));
1654 int res=compareType (dest, val->type);
1655 Safe_free(val->type);
1659 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1660 return compareType (dest->next, src);
1663 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1669 /* if one is a specifier and the other is not */
1670 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1671 (IS_SPEC (dest) && !IS_SPEC (src)))
1674 /* if one of them is a void then ok */
1675 if (SPEC_NOUN (dest) == V_VOID &&
1676 SPEC_NOUN (src) != V_VOID)
1679 if (SPEC_NOUN (dest) != V_VOID &&
1680 SPEC_NOUN (src) == V_VOID)
1683 /* if they are both bitfields then if the lengths
1684 and starts don't match */
1685 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1686 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1687 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1690 /* it is a specifier */
1691 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1693 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1694 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1695 getSize (dest) == getSize (src))
1697 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1702 else if (IS_STRUCT (dest))
1704 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1709 if (SPEC_LONG (dest) != SPEC_LONG (src))
1712 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1718 /*--------------------------------------------------------------------*/
1719 /* compareTypeExact - will do type check return 1 if match exactly */
1720 /*--------------------------------------------------------------------*/
1722 compareTypeExact (sym_link * dest, sym_link * src, int level)
1724 STORAGE_CLASS srcScls, destScls;
1735 /* if dest is a declarator then */
1740 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1741 if ((DCL_TYPE (src) == ARRAY) && (DCL_ELEM (src) != DCL_ELEM (dest)))
1743 if (DCL_PTR_CONST (src) != DCL_PTR_CONST (dest))
1745 if (DCL_PTR_VOLATILE (src) != DCL_PTR_VOLATILE (dest))
1749 value *exargs, *acargs, *checkValue;
1751 /* verify function return type */
1752 if (!compareTypeExact (dest->next, src->next, -1))
1754 if (FUNC_ISISR (dest) != FUNC_ISISR (src))
1756 if (FUNC_REGBANK (dest) != FUNC_REGBANK (src))
1758 if (IFFUNC_ISNAKED (dest) != IFFUNC_ISNAKED (src))
1761 if (IFFUNC_ISREENT (dest) != IFFUNC_ISREENT (src) && argCnt>1)
1765 /* compare expected args with actual args */
1766 exargs = FUNC_ARGS(dest);
1767 acargs = FUNC_ARGS(src);
1769 /* for all the expected args do */
1770 for (; exargs && acargs; exargs = exargs->next, acargs = acargs->next)
1772 //checkTypeSanity(acargs->etype, acargs->name);
1774 if (IS_AGGREGATE (acargs->type))
1776 checkValue = copyValue (acargs);
1777 aggregateToPointer (checkValue);
1780 checkValue = acargs;
1783 if (!compareTypeExact (exargs->type, checkValue->type, -1))
1788 /* if one them ended we have a problem */
1789 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1790 (!exargs && acargs && !IS_VOID (acargs->type)))
1794 return compareTypeExact (dest->next, src->next, level);
1801 /* if one is a specifier and the other is not */
1802 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1803 (IS_SPEC (dest) && !IS_SPEC (src)))
1806 /* if one of them is a void then ok */
1807 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1810 /* if they are both bitfields then if the lengths
1811 and starts don't match */
1812 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1813 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1814 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1817 if (IS_INTEGRAL (dest))
1819 /* signedness must match */
1820 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1822 /* size must match */
1823 if (SPEC_LONG (dest) != SPEC_LONG (src))
1825 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
1829 if (IS_STRUCT (dest))
1831 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1835 if (SPEC_CONST (dest) != SPEC_CONST (src))
1837 if (SPEC_VOLATILE (dest) != SPEC_VOLATILE (src))
1839 if (SPEC_STAT (dest) != SPEC_STAT (src))
1841 if (SPEC_ABSA (dest) != SPEC_ABSA (src))
1843 if (SPEC_ABSA (dest) && SPEC_ADDR (dest) != SPEC_ADDR (src))
1846 destScls = SPEC_SCLS (dest);
1847 srcScls = SPEC_SCLS (src);
1849 /* Compensate for const to const code change in checkSClass() */
1850 if (!level & port->mem.code_ro && SPEC_CONST (dest))
1852 if (srcScls == S_CODE && destScls == S_FIXED)
1854 if (destScls == S_CODE && srcScls == S_FIXED)
1858 /* compensate for allocGlobal() */
1859 if ((srcScls == S_FIXED || srcScls == S_AUTO)
1860 && port->mem.default_globl_map == xdata
1864 if (level>0 && !SPEC_STAT (dest))
1866 /* Compensate for hack-o-matic in checkSClass() */
1867 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1869 if (destScls == S_FIXED)
1870 destScls = (options.useXstack ? S_XSTACK : S_STACK);
1871 if (srcScls == S_FIXED)
1872 srcScls = (options.useXstack ? S_XSTACK : S_STACK);
1874 else if (TARGET_IS_DS390 || TARGET_IS_DS400 || options.useXstack)
1876 if (destScls == S_FIXED)
1878 if (srcScls == S_FIXED)
1883 if (srcScls != destScls)
1886 printf ("level = %d\n", level);
1887 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n",
1888 SPEC_SCLS (src), SPEC_SCLS (dest));
1889 printf ("srcScls = %d, destScls = %d\n",srcScls, destScls);
1897 /*------------------------------------------------------------------*/
1898 /* inCalleeSaveList - return 1 if found in callee save list */
1899 /*------------------------------------------------------------------*/
1901 calleeCmp(void *p1, void *p2)
1903 return (strcmp((char *)p1, (char *)(p2)) == 0);
1907 inCalleeSaveList(char *s)
1909 if (options.all_callee_saves)
1911 return isinSetWith(options.calleeSavesSet, s, calleeCmp);
1914 /*-----------------------------------------------------------------*/
1915 /* aggregateToPointer: change an agggregate type function */
1916 /* argument to a pointer to that type. */
1917 /*-----------------------------------------------------------------*/
1919 aggregateToPointer (value * val)
1921 if (IS_AGGREGATE (val->type))
1923 /* if this is a structure */
1924 /* then we need to add a new link */
1925 if (IS_STRUCT (val->type))
1927 /* first lets add DECLARATOR type */
1928 sym_link *p = val->type;
1930 werror (W_STRUCT_AS_ARG, val->name);
1931 val->type = newLink (DECLARATOR);
1932 val->type->next = p;
1935 /* change to a pointer depending on the */
1936 /* storage class specified */
1937 switch (SPEC_SCLS (val->etype))
1940 DCL_TYPE (val->type) = IPOINTER;
1943 DCL_TYPE (val->type) = PPOINTER;
1946 if (SPEC_OCLS(val->etype)) {
1947 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
1949 // this happens for (external) function parameters
1950 DCL_TYPE (val->type) = port->unqualified_pointer;
1956 DCL_TYPE (val->type) = POINTER;
1959 DCL_TYPE (val->type) = CPOINTER;
1962 DCL_TYPE (val->type) = FPOINTER;
1965 DCL_TYPE (val->type) = EEPPOINTER;
1968 DCL_TYPE (val->type) = port->unqualified_pointer;
1971 /* is there is a symbol associated then */
1972 /* change the type of the symbol as well */
1975 val->sym->type = copyLinkChain (val->type);
1976 val->sym->etype = getSpec (val->sym->type);
1981 /*------------------------------------------------------------------*/
1982 /* checkFunction - does all kinds of check on a function */
1983 /*------------------------------------------------------------------*/
1985 checkFunction (symbol * sym, symbol *csym)
1987 value *exargs, *acargs;
1991 if (getenv("DEBUG_SANITY")) {
1992 fprintf (stderr, "checkFunction: %s ", sym->name);
1995 if (!IS_DECL(sym->type) || DCL_TYPE(sym->type)!=FUNCTION)
1997 werror(E_SYNTAX_ERROR, sym->name);
2001 /* make sure the type is complete and sane */
2002 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
2004 /* if not type then some kind of error */
2008 /* if the function has no type then make it return int */
2009 if (!sym->type->next)
2010 sym->type->next = sym->etype = newIntLink ();
2012 /* function cannot return aggregate */
2013 if (IS_AGGREGATE (sym->type->next))
2015 werror (E_FUNC_AGGR, sym->name);
2019 /* function cannot return bit */
2020 if (IS_BITVAR (sym->type->next))
2022 werror (E_FUNC_BIT, sym->name);
2026 /* check if this function is defined as calleeSaves
2027 then mark it as such */
2028 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
2030 /* if interrupt service routine */
2031 /* then it cannot have arguments */
2032 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
2034 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
2035 werror (E_INT_ARGS, sym->name);
2036 FUNC_ARGS(sym->type)=NULL;
2040 for (argCnt=1, acargs = FUNC_ARGS(sym->type);
2042 acargs=acargs->next, argCnt++) {
2044 // this can happen for reentrant functions
2045 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2046 // the show must go on: synthesize a name and symbol
2047 SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
2048 acargs->sym = newSymbol (acargs->name, 1);
2049 SPEC_OCLS (acargs->etype) = istack;
2050 acargs->sym->type = copyLinkChain (acargs->type);
2051 acargs->sym->etype = getSpec (acargs->sym->type);
2052 acargs->sym->_isparm = 1;
2053 strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
2054 } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) {
2056 werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
2061 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
2062 return 1; /* not defined nothing more to check */
2064 /* check if body already present */
2065 if (csym && IFFUNC_HASBODY(csym->type))
2067 werror (E_FUNC_BODY, sym->name);
2071 /* check the return value type */
2072 if (compareType (csym->type, sym->type) <= 0)
2074 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
2075 printFromToType(csym->type, sym->type);
2079 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
2081 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
2084 if (FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type))
2086 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
2089 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
2091 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
2094 /* Really, reentrant should match regardless of argCnt, but */
2095 /* this breaks some existing code (the fp lib functions). If */
2096 /* the first argument is always passed the same way, this */
2097 /* lax checking is ok (but may not be true for in future ports) */
2098 if (IFFUNC_ISREENT (csym->type) != IFFUNC_ISREENT (sym->type)
2101 //printf("argCnt = %d\n",argCnt);
2102 werror (E_PREV_DEF_CONFLICT, csym->name, "reentrant");
2105 /* compare expected args with actual args */
2106 exargs = FUNC_ARGS(csym->type);
2107 acargs = FUNC_ARGS(sym->type);
2109 /* for all the expected args do */
2112 exargs = exargs->next, acargs = acargs->next, argCnt++)
2114 if (getenv("DEBUG_SANITY")) {
2115 fprintf (stderr, "checkFunction: %s ", exargs->name);
2117 /* make sure the type is complete and sane */
2118 checkTypeSanity(exargs->etype, exargs->name);
2120 /* If the actual argument is an array, any prototype
2121 * will have modified it to a pointer. Duplicate that
2124 if (IS_AGGREGATE (acargs->type))
2126 checkValue = copyValue (acargs);
2127 aggregateToPointer (checkValue);
2131 checkValue = acargs;
2134 if (compareType (exargs->type, checkValue->type) <= 0)
2136 werror (E_ARG_TYPE, argCnt);
2137 printFromToType(exargs->type, checkValue->type);
2142 /* if one them ended we have a problem */
2143 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
2144 (!exargs && acargs && !IS_VOID (acargs->type)))
2145 werror (E_ARG_COUNT);
2147 /* replace with this defition */
2148 sym->cdef = csym->cdef;
2149 deleteSym (SymbolTab, csym, csym->name);
2150 deleteFromSeg(csym);
2151 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
2152 if (IS_EXTERN (csym->etype) && !
2153 IS_EXTERN (sym->etype))
2155 addSet (&publics, sym);
2160 /*------------------------------------------------------------------*/
2161 /* cdbStructBlock - calls struct printing for a blcks */
2162 /*------------------------------------------------------------------*/
2163 void cdbStructBlock (int block)
2166 bucket **table = StructTab;
2169 /* go thru the entire table */
2170 for (i = 0; i < 256; i++)
2172 for (chain = table[i]; chain; chain = chain->next)
2174 if (chain->block >= block)
2177 debugFile->writeType((structdef *)chain->sym, chain->block, 0, NULL);
2183 /*-----------------------------------------------------------------*/
2184 /* processFuncArgs - does some processing with function args */
2185 /*-----------------------------------------------------------------*/
2187 processFuncArgs (symbol * func)
2191 sym_link *funcType=func->type;
2193 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
2194 fprintf (stderr, "SDCCsymt.c:processFuncArgs(%s)\n", func->name);
2196 /* find the function declaration within the type */
2197 while (funcType && !IS_FUNC(funcType))
2198 funcType=funcType->next;
2200 /* if this function has variable argument list */
2201 /* then make the function a reentrant one */
2202 if (IFFUNC_HASVARARGS(funcType) || (options.stackAuto && !func->cdef))
2203 FUNC_ISREENT(funcType)=1;
2205 /* check if this function is defined as calleeSaves
2206 then mark it as such */
2207 FUNC_CALLEESAVES(funcType) = inCalleeSaveList (func->name);
2209 /* loop thru all the arguments */
2210 val = FUNC_ARGS(funcType);
2212 /* if it is void then remove parameters */
2213 if (val && IS_VOID (val->type))
2215 FUNC_ARGS(funcType) = NULL;
2219 /* reset regparm for the port */
2220 (*port->reset_regparms) ();
2221 /* if any of the arguments is an aggregate */
2222 /* change it to pointer to the same type */
2226 /* mark it as a register parameter if
2227 the function does not have VA_ARG
2228 and as port dictates */
2229 if (!IFFUNC_HASVARARGS(funcType) &&
2230 (argreg = (*port->reg_parm) (val->type)))
2232 SPEC_REGPARM (val->etype) = 1;
2233 SPEC_ARGREG(val->etype) = argreg;
2234 } else if (IFFUNC_ISREENT(funcType)) {
2235 FUNC_HASSTACKPARM(funcType) = 1;
2238 if (IS_AGGREGATE (val->type))
2240 aggregateToPointer (val);
2247 /* if this is an internal generated function call */
2249 /* ignore --stack-auto for this one, we don't know how it is compiled */
2250 /* simply trust on --int-long-reent or --float-reent */
2251 if (IFFUNC_ISREENT(funcType)) {
2255 /* if this function is reentrant or */
2256 /* automatics r 2b stacked then nothing */
2257 if (IFFUNC_ISREENT (funcType) || options.stackAuto)
2261 val = FUNC_ARGS(funcType);
2266 /* if a symbolname is not given */
2267 /* synthesize a variable name */
2270 SNPRINTF (val->name, sizeof(val->name),
2271 "_%s_PARM_%d", func->name, pNum++);
2272 val->sym = newSymbol (val->name, 1);
2273 SPEC_OCLS (val->etype) = port->mem.default_local_map;
2274 val->sym->type = copyLinkChain (val->type);
2275 val->sym->etype = getSpec (val->sym->type);
2276 val->sym->_isparm = 1;
2277 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2279 /* ?? static functions shouldn't imply static parameters - EEP */
2280 if (IS_SPEC(func->etype)) {
2281 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2282 SPEC_STAT (func->etype);
2285 addSymChain (val->sym);
2288 else /* symbol name given create synth name */
2291 SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
2292 strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
2293 val->sym->_isparm = 1;
2294 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
2295 (options.model != MODEL_SMALL ? xdata : data);
2298 /* ?? static functions shouldn't imply static parameters - EEP */
2299 if (IS_SPEC(func->etype)) {
2300 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
2301 SPEC_STAT (func->etype);
2305 if (!isinSet(operKeyReset, val->sym)) {
2306 addSet (&operKeyReset, val->sym);
2307 applyToSet (operKeyReset, resetParmKey);
2313 /*-----------------------------------------------------------------*/
2314 /* isSymbolEqual - compares two symbols return 1 if they match */
2315 /*-----------------------------------------------------------------*/
2317 isSymbolEqual (symbol * dest, symbol * src)
2319 /* if pointers match then equal */
2323 /* if one of them is null then don't match */
2327 /* if both of them have rname match on rname */
2328 if (dest->rname[0] && src->rname[0])
2329 return (!strcmp (dest->rname, src->rname));
2331 /* otherwise match on name */
2332 return (!strcmp (dest->name, src->name));
2335 void PT(sym_link *type)
2337 printTypeChain(type,0);
2339 /*-----------------------------------------------------------------*/
2340 /* printTypeChain - prints the type chain in human readable form */
2341 /*-----------------------------------------------------------------*/
2343 printTypeChain (sym_link * start, FILE * of)
2347 sym_link * type, * search;
2357 fprintf (of, "void");
2361 /* Print the chain as it is written in the source: */
2362 /* start with the last entry. */
2363 /* However, the storage class at the end of the */
2364 /* chain reall applies to the first in the chain! */
2366 for (type = start; type && type->next; type = type->next)
2369 scls=SPEC_SCLS(type);
2377 case S_DATA: fprintf (of, "data-"); break;
2378 case S_XDATA: fprintf (of, "xdata-"); break;
2379 case S_SFR: fprintf (of, "sfr-"); break;
2380 case S_SBIT: fprintf (of, "sbit-"); break;
2381 case S_CODE: fprintf (of, "code-"); break;
2382 case S_IDATA: fprintf (of, "idata-"); break;
2383 case S_PDATA: fprintf (of, "pdata-"); break;
2384 case S_LITERAL: fprintf (of, "literal-"); break;
2385 case S_STACK: fprintf (of, "stack-"); break;
2386 case S_XSTACK: fprintf (of, "xstack-"); break;
2387 case S_BIT: fprintf (of, "bit-"); break;
2388 case S_EEPROM: fprintf (of, "eeprom-"); break;
2395 if (!IS_FUNC(type)) {
2396 if (DCL_PTR_VOLATILE (type)) {
2397 fprintf (of, "volatile-");
2399 if (DCL_PTR_CONST (type)) {
2400 fprintf (of, "const-");
2403 switch (DCL_TYPE (type))
2406 fprintf (of, "function %s %s",
2407 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2408 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2410 for (args = FUNC_ARGS(type);
2413 printTypeChain(args->type, of);
2420 fprintf (of, "generic* ");
2423 fprintf (of, "code* ");
2426 fprintf (of, "xdata* ");
2429 fprintf (of, "eeprom* ");
2432 fprintf (of, "near* ");
2435 fprintf (of, "idata* ");
2438 fprintf (of, "pdata* ");
2441 fprintf (of, "unknown* ");
2444 if (DCL_ELEM(type)) {
2445 fprintf (of, "[%d] ", DCL_ELEM(type));
2447 fprintf (of, "[] ");
2454 if (SPEC_VOLATILE (type))
2455 fprintf (of, "volatile-");
2456 if (SPEC_CONST (type))
2457 fprintf (of, "const-");
2458 if (SPEC_USIGN (type))
2459 fprintf (of, "unsigned-");
2460 switch (SPEC_NOUN (type))
2464 fprintf (of, "long-");
2465 fprintf (of, "int");
2469 fprintf (of, "char");
2473 fprintf (of, "void");
2477 fprintf (of, "float");
2481 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2485 fprintf (of, "sbit");
2489 fprintf (of, "bit");
2493 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2497 fprintf (of, "double");
2501 fprintf (of, "unknown type");
2505 /* search entry in list before "type" */
2506 for (search = start; search && search->next != type;)
2507 search = search->next;
2516 /*--------------------------------------------------------------------*/
2517 /* printTypeChainRaw - prints the type chain in human readable form */
2518 /* in the raw data structure ordering */
2519 /*--------------------------------------------------------------------*/
2521 printTypeChainRaw (sym_link * start, FILE * of)
2534 fprintf (of, "void");
2544 if (!IS_FUNC(type)) {
2545 if (DCL_PTR_VOLATILE (type)) {
2546 fprintf (of, "volatile-");
2548 if (DCL_PTR_CONST (type)) {
2549 fprintf (of, "const-");
2552 switch (DCL_TYPE (type))
2555 fprintf (of, "function %s %s",
2556 (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "),
2557 (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " "));
2559 for (args = FUNC_ARGS(type);
2562 printTypeChain(args->type, of);
2569 fprintf (of, "generic* ");
2572 fprintf (of, "code* ");
2575 fprintf (of, "xdata* ");
2578 fprintf (of, "eeprom* ");
2581 fprintf (of, "near* ");
2584 fprintf (of, "idata* ");
2587 fprintf (of, "pdata* ");
2590 fprintf (of, "unknown* ");
2593 if (DCL_ELEM(type)) {
2594 fprintf (of, "[%d] ", DCL_ELEM(type));
2596 fprintf (of, "[] ");
2600 if (DCL_TSPEC(type))
2603 printTypeChainRaw(DCL_TSPEC(type), of);
2607 else if (IS_SPEC (type))
2609 switch (SPEC_SCLS (type))
2611 case S_DATA: fprintf (of, "data-"); break;
2612 case S_XDATA: fprintf (of, "xdata-"); break;
2613 case S_SFR: fprintf (of, "sfr-"); break;
2614 case S_SBIT: fprintf (of, "sbit-"); break;
2615 case S_CODE: fprintf (of, "code-"); break;
2616 case S_IDATA: fprintf (of, "idata-"); break;
2617 case S_PDATA: fprintf (of, "pdata-"); break;
2618 case S_LITERAL: fprintf (of, "literal-"); break;
2619 case S_STACK: fprintf (of, "stack-"); break;
2620 case S_XSTACK: fprintf (of, "xstack-"); break;
2621 case S_BIT: fprintf (of, "bit-"); break;
2622 case S_EEPROM: fprintf (of, "eeprom-"); break;
2625 if (SPEC_VOLATILE (type))
2626 fprintf (of, "volatile-");
2627 if (SPEC_CONST (type))
2628 fprintf (of, "const-");
2629 if (SPEC_USIGN (type))
2630 fprintf (of, "unsigned-");
2631 switch (SPEC_NOUN (type))
2635 fprintf (of, "long-");
2636 fprintf (of, "int");
2640 fprintf (of, "char");
2644 fprintf (of, "void");
2648 fprintf (of, "float");
2652 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2656 fprintf (of, "sbit");
2660 fprintf (of, "bit");
2664 fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2668 fprintf (of, "double");
2672 fprintf (of, "unknown type");
2677 fprintf (of, "NOT_SPEC_OR_DECL");
2687 /*-----------------------------------------------------------------*/
2688 /* powof2 - returns power of two for the number if number is pow 2 */
2689 /*-----------------------------------------------------------------*/
2691 powof2 (TYPE_UDWORD num)
2704 if (n1s > 1 || nshifts == 0)
2720 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2721 symbol *__muldiv[3][3][2];
2722 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2723 sym_link *__multypes[3][2];
2724 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2725 symbol *__conv[2][3][2];
2726 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2727 symbol *__rlrr[2][3][2];
2729 sym_link *floatType;
2732 _mangleFunctionName(char *in)
2734 if (port->getMangledFunctionName)
2736 return port->getMangledFunctionName(in);
2744 /*-----------------------------------------------------------------*/
2745 /* typeFromStr - create a typechain from an encoded string */
2746 /* basic types - 'c' - char */
2752 /* '*' - pointer - default (GPOINTER) */
2753 /* modifiers - 'u' - unsigned */
2754 /* pointer modifiers - 'g' - generic */
2758 /* 'F' - function */
2759 /* examples : "ig*" - generic int * */
2760 /* "cx*" - char xdata * */
2761 /* "ui" - unsigned int */
2762 /*-----------------------------------------------------------------*/
2763 sym_link *typeFromStr (char *s)
2765 sym_link *r = newLink(DECLARATOR);
2777 r->class = SPECIFIER;
2778 SPEC_NOUN(r) = V_CHAR;
2782 r->class = SPECIFIER;
2783 SPEC_NOUN(r) = V_INT;
2786 r->class = SPECIFIER;
2787 SPEC_NOUN(r) = V_INT;
2791 r->class = SPECIFIER;
2792 SPEC_NOUN(r) = V_FLOAT;
2795 r->class = SPECIFIER;
2796 SPEC_NOUN(r) = V_VOID;
2799 DCL_TYPE(r) = port->unqualified_pointer;
2806 assert(*(s+1)=='*');
2807 nr = newLink(DECLARATOR);
2812 DCL_TYPE(r) = GPOINTER;
2815 DCL_TYPE(r) = FPOINTER;
2818 DCL_TYPE(r) = CPOINTER;
2821 DCL_TYPE(r) = POINTER;
2824 DCL_TYPE(r) = FUNCTION;
2825 nr = newLink(DECLARATOR);
2828 DCL_TYPE(r) = CPOINTER;
2834 werror(E_INTERNAL_ERROR, __FILE__, __LINE__,
2835 "typeFromStr: unknown type");
2838 if (IS_SPEC(r) && usign) {
2847 /*-----------------------------------------------------------------*/
2848 /* initCSupport - create functions for C support routines */
2849 /*-----------------------------------------------------------------*/
2853 const char *smuldivmod[] =
2857 const char *sbwd[] =
2859 "char", "int", "long"
2865 const char *srlrr[] =
2870 int bwd, su, muldivmod, tofrom, rlrr;
2872 if (getenv("SDCC_NO_C_SUPPORT")) {
2873 /* for debugging only */
2877 floatType = newFloatLink ();
2879 for (bwd = 0; bwd < 3; bwd++)
2896 __multypes[bwd][0] = l;
2897 __multypes[bwd][1] = copyLinkChain (l);
2898 SPEC_USIGN (__multypes[bwd][1]) = 1;
2901 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2902 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2903 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2904 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2905 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2906 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2907 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2908 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2909 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2910 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2912 for (tofrom = 0; tofrom < 2; tofrom++)
2914 for (bwd = 0; bwd < 3; bwd++)
2916 for (su = 0; su < 2; su++)
2920 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
2921 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
2925 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
2926 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
2933 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2935 for (bwd = 0; bwd < 3; bwd++)
2937 for (su = 0; su < 2; su++)
2939 SNPRINTF (buffer, sizeof(buffer),
2941 smuldivmod[muldivmod],
2944 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2945 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2950 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
2951 Therefore they've been merged into mulint() and mullong().
2954 for (bwd = 0; bwd < 3; bwd++)
2956 for (su = 0; su < 2; su++)
2958 for (muldivmod = 1; muldivmod < 3; muldivmod++)
2961 SNPRINTF (buffer, sizeof(buffer),
2963 smuldivmod[muldivmod],
2966 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2967 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2975 for (su = 0; su < 2; su++)
2977 /* muluchar and mulschar are still separate functions, because e.g. the z80
2978 port is sign/zero-extending to int before calling mulint() */
2979 SNPRINTF (buffer, sizeof(buffer),
2981 smuldivmod[muldivmod],
2984 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2985 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2989 /* word and doubleword */
2990 for (bwd = 1; bwd < 3; bwd++)
2993 SNPRINTF (buffer, sizeof(buffer),
2995 smuldivmod[muldivmod],
2997 __muldiv[muldivmod][bwd][0] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2998 FUNC_NONBANKED (__muldiv[muldivmod][bwd][0]->type) = 1;
2999 /* signed = unsigned */
3000 __muldiv[muldivmod][bwd][1] = __muldiv[muldivmod][bwd][0];
3003 for (rlrr = 0; rlrr < 2; rlrr++)
3005 for (bwd = 0; bwd < 3; bwd++)
3007 for (su = 0; su < 2; su++)
3009 SNPRINTF (buffer, sizeof(buffer),
3014 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
3015 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;
3021 /*-----------------------------------------------------------------*/
3022 /* initBuiltIns - create prototypes for builtin functions */
3023 /*-----------------------------------------------------------------*/
3029 if (!port->builtintable) return ;
3031 for (i = 0 ; port->builtintable[i].name ; i++) {
3032 sym = funcOfTypeVarg(port->builtintable[i].name,port->builtintable[i].rtype,
3033 port->builtintable[i].nParms,port->builtintable[i].parm_types);
3034 FUNC_ISBUILTIN(sym->type) = 1;
3035 FUNC_ISREENT(sym->type) = 0; /* can never be reentrant */
3039 sym_link *validateLink(sym_link *l,
3046 if (l && l->class==select)
3051 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
3052 " expected %s, got %s\n",
3053 macro, args, file, line,
3054 DECLSPEC2TXT(select), l ? DECLSPEC2TXT(l->class) : "null-link");
3056 return l; // never reached, makes compiler happy.
3059 /*--------------------------------------------------------------------*/
3060 /* newEnumType - create an integer type compatible with enumerations */
3061 /*--------------------------------------------------------------------*/
3063 newEnumType (symbol *enumlist)
3071 type = newLink (SPECIFIER);
3072 SPEC_NOUN (type) = V_INT;
3076 /* Determine the range of the enumerated values */
3078 min = max = (int) floatFromVal (valFromType (sym->type));
3079 for (sym = sym->next; sym; sym = sym->next)
3081 v = (int) floatFromVal (valFromType (sym->type));
3088 /* Determine the smallest integer type that is compatible with this range */
3089 type = newLink (SPECIFIER);
3090 if (min>=0 && max<=255)
3092 SPEC_NOUN (type) = V_CHAR;
3093 SPEC_USIGN (type) = 1;
3095 else if (min>=-128 && max<=127)
3097 SPEC_NOUN (type) = V_CHAR;
3099 else if (min>=0 && max<=65535)
3101 SPEC_NOUN (type) = V_INT;
3102 SPEC_USIGN (type) = 1;
3104 else if (min>=-32768 && max<=32767)
3106 SPEC_NOUN (type) = V_INT;
3110 SPEC_NOUN (type) = V_INT;
3111 SPEC_LONG (type) = 1;
3113 SPEC_USIGN (type) = 1;