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 -------------------------------------------------------------------------*/
28 char *nounName(sym_link *sl) {
29 switch (SPEC_NOUN(sl))
32 if (SPEC_LONG(sl)) return "long";
33 if (sl->select.s._short) return "short";
36 case V_FLOAT: return "float";
37 case V_CHAR: return "char";
38 case V_VOID: return "void";
39 case V_STRUCT: return "struct";
40 case V_LABEL: return "label";
41 case V_BIT: return "bit";
42 case V_SBIT: return "sbit";
43 case V_DOUBLE: return "double";
48 bucket *SymbolTab[256]; /* the symbol table */
49 bucket *StructTab[256]; /* the structure table */
50 bucket *TypedefTab[256]; /* the typedef table */
51 bucket *LabelTab[256]; /* the Label table */
52 bucket *enumTab[256]; /* enumerated table */
54 /*------------------------------------------------------------------*/
55 /* initSymt () - initialises symbol table related stuff */
56 /*------------------------------------------------------------------*/
62 for (i = 0; i < 256; i++)
63 SymbolTab[i] = StructTab[i] = (void *) NULL;
67 /*-----------------------------------------------------------------*/
68 /* newBucket - allocates & returns a new bucket */
69 /*-----------------------------------------------------------------*/
75 bp = Safe_calloc (1, sizeof (bucket));
80 /*-----------------------------------------------------------------*/
81 /* hashKey - computes the hashkey given a symbol name */
82 /*-----------------------------------------------------------------*/
84 hashKey (const char *s)
86 unsigned long key = 0;
93 /*-----------------------------------------------------------------*/
94 /* addSym - adds a symbol to the hash Table */
95 /*-----------------------------------------------------------------*/
97 addSym (bucket ** stab,
104 int i; /* index into the hash Table */
105 bucket *bp; /* temp bucket * */
108 if (getenv("DEBUG_SANITY")) {
109 fprintf (stderr, "addSym: %s ", sname);
111 /* make sure the type is complete and sane */
112 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
115 /* the symbols are always added at the head of the list */
117 /* get a free entry */
118 bp = Safe_calloc (1, sizeof (bucket));
120 bp->sym = sym; /* update the symbol pointer */
121 bp->level = level; /* update the nest level */
123 strcpy (bp->name, sname); /* copy the name into place */
125 /* if this is the first entry */
128 bp->prev = bp->next = (void *) NULL; /* point to nothing */
131 /* not first entry then add @ head of list */
141 /*-----------------------------------------------------------------*/
142 /* deleteSym - deletes a symbol from the hash Table entry */
143 /*-----------------------------------------------------------------*/
145 deleteSym (bucket ** stab, void *sym, char *sname)
153 /* find the symbol */
156 if (bp->sym == sym) /* found it then break out */
157 break; /* of the loop */
161 if (!bp) /* did not find it */
163 /* if this is the first one in the chain */
167 if (stab[i]) /* if chain ! empty */
168 stab[i]->prev = (void *) NULL;
170 /* middle || end of chain */
173 if (bp->next) /* if not end of chain */
174 bp->next->prev = bp->prev;
176 bp->prev->next = bp->next;
181 /*-----------------------------------------------------------------*/
182 /* findSym - finds a symbol in a table */
183 /*-----------------------------------------------------------------*/
185 findSym (bucket ** stab, void *sym, const char *sname)
189 bp = stab[hashKey (sname)];
192 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
197 return (bp ? bp->sym : (void *) NULL);
200 /*-----------------------------------------------------------------*/
201 /* findSymWithLevel - finds a symbol with a name & level */
202 /*-----------------------------------------------------------------*/
204 findSymWithLevel (bucket ** stab, symbol * sym)
208 bp = stab[hashKey (sym->name)];
211 ** do the search from the head of the list since the
212 ** elements are added at the head it is ensured that
213 ** we will find the deeper definitions before we find
214 ** the global ones. we need to check for symbols with
215 ** level <= to the level given, if levels match then block
216 ** numbers need to match as well
220 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
222 /* if this is parameter then nothing else need to be checked */
223 if (((symbol *) (bp->sym))->_isparm)
225 /* if levels match then block numbers should also match */
226 if (bp->level && bp->level == sym->level && bp->block == sym->block)
228 /* if levels don't match then we are okay */
229 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
231 /* if this is a global variable then we are ok too */
239 return (void *) NULL;
242 /*-----------------------------------------------------------------*/
243 /* findSymWithBlock - finds a symbol with name in with a block */
244 /*-----------------------------------------------------------------*/
246 findSymWithBlock (bucket ** stab, symbol * sym, int block)
250 bp = stab[hashKey (sym->name)];
253 if (strcmp (bp->name, sym->name) == 0 &&
259 return (bp ? bp->sym : (void *) NULL);
262 /*------------------------------------------------------------------*/
263 /* newSymbol () - returns a new pointer to a symbol */
264 /*------------------------------------------------------------------*/
266 newSymbol (char *name, int scope)
270 sym = Safe_calloc (1, sizeof (symbol));
272 strcpy (sym->name, name); /* copy the name */
273 sym->level = scope; /* set the level */
274 sym->block = currBlockno;
275 sym->lineDef = yylineno; /* set the line number */
279 /*------------------------------------------------------------------*/
280 /* newLink - creates a new link (declarator,specifier) */
281 /*------------------------------------------------------------------*/
287 p = Safe_calloc (1, sizeof (sym_link));
292 /*------------------------------------------------------------------*/
293 /* newStruct - creats a new structdef from the free list */
294 /*------------------------------------------------------------------*/
296 newStruct (char *tag)
300 s = Safe_calloc (1, sizeof (structdef));
302 strcpy (s->tag, tag); /* copy the tag */
306 /*------------------------------------------------------------------*/
307 /* pointerTypes - do the computation for the pointer types */
308 /*------------------------------------------------------------------*/
310 pointerTypes (sym_link * ptr, sym_link * type)
315 /* find the first pointer type */
316 while (ptr && !IS_PTR (ptr))
319 /* could not find it */
320 if (!ptr || IS_SPEC (ptr))
323 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
324 pointerTypes (ptr->next, type);
328 /* change the pointer type depending on the
329 storage class of the type */
332 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
333 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
334 switch (SPEC_SCLS (type))
337 DCL_TYPE (ptr) = FPOINTER;
340 DCL_TYPE (ptr) = IPOINTER;
343 DCL_TYPE (ptr) = PPOINTER;
346 DCL_TYPE (ptr) = POINTER;
349 DCL_PTR_CONST (ptr) = port->mem.code_ro;
350 DCL_TYPE (ptr) = CPOINTER;
353 DCL_TYPE (ptr) = EEPPOINTER;
356 DCL_TYPE (ptr) = GPOINTER;
359 /* the storage class of type ends here */
362 SPEC_VOLATILE (type) = 0;
365 /* now change all the remaining unknown pointers
366 to generic pointers */
369 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
370 DCL_TYPE (ptr) = GPOINTER;
374 /* same for the type although it is highly unlikely that
375 type will have a pointer */
378 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
379 DCL_TYPE (type) = GPOINTER;
385 /*------------------------------------------------------------------*/
386 /* addDecl - adds a declarator @ the end of a chain */
387 /*------------------------------------------------------------------*/
389 addDecl (symbol * sym, int type, sym_link * p)
395 /* if we are passed a link then set head & tail */
404 head = tail = newLink ();
405 DCL_TYPE (head) = type;
408 /* if this is the first entry */
416 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
418 sym->etype = mergeSpec (sym->etype, head, sym->name);
422 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
425 while (t->next != sym->etype)
428 tail->next = sym->etype;
432 sym->etype->next = head;
438 /* if the type is an unknown pointer and has
439 a tspec then take the storage class const & volatile
440 attribute from the tspec & make it those of this
444 //DCL_TYPE (p) == UPOINTER &&
447 if (!IS_SPEC (sym->etype))
449 sym->etype = sym->etype->next = newLink ();
450 sym->etype->class = SPECIFIER;
452 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
453 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
454 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
455 DCL_TSPEC (p) = NULL;
460 /*------------------------------------------------------------------
461 checkTypeSanity: prevent the user from doing e.g.:
463 ------------------------------------------------------------------*/
464 void checkTypeSanity(sym_link *etype, char *name) {
468 if (getenv("DEBUG_SANITY")) {
469 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
474 if (!IS_SPEC(etype)) {
475 if (getenv("DEBUG_SANITY")) {
476 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
481 noun=nounName(etype);
483 if (getenv("DEBUG_SANITY")) {
484 fprintf (stderr, "checking sanity for %s %x\n", name, (int)etype);
487 if ((SPEC_NOUN(etype)==V_CHAR ||
488 SPEC_NOUN(etype)==V_FLOAT ||
489 SPEC_NOUN(etype)==V_DOUBLE ||
490 SPEC_NOUN(etype)==V_VOID) &&
491 (etype->select.s._short || SPEC_LONG(etype))) {
492 // long or short for char float double or void
493 werror (E_LONG_OR_SHORT_INVALID, noun, name);
495 if ((SPEC_NOUN(etype)==V_FLOAT ||
496 SPEC_NOUN(etype)==V_DOUBLE ||
497 SPEC_NOUN(etype)==V_VOID) &&
498 (etype->select.s._signed || SPEC_USIGN(etype))) {
499 // signed or unsigned for float double or void
500 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
503 // special case for "short"
504 if (etype->select.s._short) {
505 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
506 etype->select.s._short = 0;
510 "const a;" or "data b;" or "signed s" or "long l"
512 if (!SPEC_NOUN(etype)) {
513 SPEC_NOUN(etype)=V_INT;
516 if (etype->select.s._signed && SPEC_USIGN(etype)) {
517 // signed AND unsigned
518 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
520 if (etype->select.s._short && SPEC_LONG(etype)) {
522 werror (E_LONG_AND_SHORT_INVALID, noun, name);
527 /*------------------------------------------------------------------*/
528 /* mergeSpec - merges two specifiers and returns the new one */
529 /*------------------------------------------------------------------*/
531 mergeSpec (sym_link * dest, sym_link * src, char *name)
536 if (!IS_SPEC(dest)) {
537 // This can happen for pointers, find the end type
538 while (dest && !IS_SPEC(dest))
542 // here we have a declarator as source, reverse them
546 while (dest && !IS_SPEC(dest)) {
547 // and find the specifier
554 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
555 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
559 if (getenv("DEBUG_mergeSpec")) {
560 fprintf (stderr, "mergeSpec: \"%s\"\n", name);
563 if (SPEC_NOUN(src)) {
564 if (!SPEC_NOUN(dest)) {
565 SPEC_NOUN(dest)=SPEC_NOUN(src);
567 /* we shouldn't redeclare the type */
568 if (getenv("DEBUG_SANITY")) {
569 fprintf (stderr, "mergeSpec: ");
571 werror(E_TWO_OR_MORE_DATA_TYPES, name);
575 if (SPEC_SCLS(src)) {
576 /* if destination has no storage class */
577 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
578 SPEC_SCLS (dest) = SPEC_SCLS (src);
580 if (getenv("DEBUG_SANITY")) {
581 fprintf (stderr, "mergeSpec: ");
583 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
587 /* copy all the specifications */
589 // we really should do:
591 if (SPEC_what(src)) {
592 if (SPEC_what(dest)) {
593 werror(W_DUPLICATE_SPEC, "what");
595 SPEC_what(dst)|=SPEC_what(src);
598 // but there are more important thing right now
600 SPEC_LONG (dest) |= SPEC_LONG (src);
601 dest->select.s._short|=src->select.s._short;
602 SPEC_USIGN (dest) |= SPEC_USIGN (src);
603 dest->select.s._signed|=src->select.s._signed;
604 SPEC_STAT (dest) |= SPEC_STAT (src);
605 SPEC_EXTR (dest) |= SPEC_EXTR (src);
606 SPEC_ABSA (dest) |= SPEC_ABSA (src);
607 SPEC_RENT (dest) |= SPEC_RENT (src);
608 SPEC_INTN (dest) |= SPEC_INTN (src);
609 SPEC_BANK (dest) |= SPEC_BANK (src);
610 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
611 SPEC_CRTCL (dest) |= SPEC_CRTCL (src);
612 SPEC_ADDR (dest) |= SPEC_ADDR (src);
613 SPEC_OCLS (dest) = SPEC_OCLS (src);
614 SPEC_BLEN (dest) |= SPEC_BLEN (src);
615 SPEC_BSTR (dest) |= SPEC_BSTR (src);
616 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
617 SPEC_NONBANKED (dest) |= SPEC_NONBANKED (src);
618 SPEC_NAKED (dest) |= SPEC_NAKED (src);
620 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
621 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
626 /*------------------------------------------------------------------*/
627 /* cloneSpec - copies the entire spec and returns a new spec */
628 /*------------------------------------------------------------------*/
630 cloneSpec (sym_link * src)
634 /* go thru chain till we find the specifier */
635 while (src && src->class != SPECIFIER)
639 memcpy (spec, src, sizeof (sym_link));
643 /*------------------------------------------------------------------*/
644 /* genSymName - generates and returns a name used for anonymous vars */
645 /*------------------------------------------------------------------*/
647 genSymName (int level)
649 static int gCount = 0;
650 static char gname[SDCC_NAME_MAX + 1];
652 sprintf (gname, "__%04d%04d", level, gCount++);
656 /*------------------------------------------------------------------*/
657 /* getSpec - returns the specifier part from a declaration chain */
658 /*------------------------------------------------------------------*/
660 getSpec (sym_link * p)
665 while (p && !(IS_SPEC (p)))
671 /*------------------------------------------------------------------*/
672 /* newCharLink() - creates an char type */
673 /*------------------------------------------------------------------*/
680 p->class = SPECIFIER;
681 SPEC_NOUN (p) = V_CHAR;
686 /*------------------------------------------------------------------*/
687 /* newFloatLink - a new Float type */
688 /*------------------------------------------------------------------*/
695 p->class = SPECIFIER;
696 SPEC_NOUN (p) = V_FLOAT;
701 /*------------------------------------------------------------------*/
702 /* newLongLink() - new long type */
703 /*------------------------------------------------------------------*/
710 p->class = SPECIFIER;
711 SPEC_NOUN (p) = V_INT;
717 /*------------------------------------------------------------------*/
718 /* newIntLink() - creates an int type */
719 /*------------------------------------------------------------------*/
726 p->class = 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;
760 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
766 /* this is a specifier */
767 switch (DCL_TYPE (p))
772 return DCL_ELEM (p) * getSize (p->next);
789 /*------------------------------------------------------------------*/
790 /* bitsForType - returns # of bits required to store this type */
791 /*------------------------------------------------------------------*/
793 bitsForType (sym_link * p)
795 /* if nothing return 0 */
800 { /* if this is the specifier then */
802 switch (SPEC_NOUN (p))
803 { /* depending on the specifier type */
805 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
807 return FLOATSIZE * 8;
813 return SPEC_STRUCT (p)->size * 8;
819 return SPEC_BLEN (p);
825 /* this is a specifier */
826 switch (DCL_TYPE (p))
831 return DCL_ELEM (p) * getSize (p->next) * 8;
835 return (PTRSIZE * 8);
839 return (FPTRSIZE * 8);
841 return (GPTRSIZE * 8);
848 /*------------------------------------------------------------------*/
849 /* copySymbolChain - copies a symbol chain */
850 /*------------------------------------------------------------------*/
852 copySymbolChain (symbol * src)
859 dest = copySymbol (src);
860 dest->next = copySymbolChain (src->next);
864 /*------------------------------------------------------------------*/
865 /* copySymbol - makes a copy of a symbol */
866 /*------------------------------------------------------------------*/
868 copySymbol (symbol * src)
875 dest = newSymbol (src->name, src->level);
876 memcpy (dest, src, sizeof (symbol));
877 dest->level = src->level;
878 dest->block = src->block;
879 dest->ival = copyIlist (src->ival);
880 dest->type = copyLinkChain (src->type);
881 dest->etype = getSpec (dest->type);
883 dest->args = copyValueChain (src->args);
884 dest->key = src->key;
885 dest->calleeSave = src->calleeSave;
886 dest->allocreq = src->allocreq;
890 /*------------------------------------------------------------------*/
891 /* reverseSyms - reverses the links for a symbol chain */
892 /*------------------------------------------------------------------*/
894 reverseSyms (symbol * sym)
896 symbol *prev, *curr, *next;
911 sym->next = (void *) NULL;
915 /*------------------------------------------------------------------*/
916 /* reverseLink - reverses the links for a type chain */
917 /*------------------------------------------------------------------*/
919 reverseLink (sym_link * type)
921 sym_link *prev, *curr, *next;
936 type->next = (void *) NULL;
940 /*------------------------------------------------------------------*/
941 /* addSymChain - adds a symbol chain to the symboltable */
942 /*------------------------------------------------------------------*/
944 addSymChain (symbol * symHead)
946 symbol *sym = symHead;
949 for (; sym != NULL; sym = sym->next)
952 checkTypeSanity(sym->etype, sym->name);
954 /* if already exists in the symbol table then check if
955 one of them is an extern definition if yes then
956 then check if the type match, if the types match then
957 delete the current entry and add the new entry */
958 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
959 csym->level == sym->level) {
961 /* one definition extern ? */
962 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
963 /* do types match ? */
964 if (compareType (csym->type, sym->type) != 1) {
966 werror (E_EXTERN_MISMATCH, csym->name);
969 /* delete current entry */
970 deleteSym (SymbolTab, csym, csym->name);
973 werror (E_DUPLICATE, sym->name);
979 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
984 /*------------------------------------------------------------------*/
985 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
986 /*------------------------------------------------------------------*/
988 funcInChain (sym_link * lnk)
999 /*------------------------------------------------------------------*/
1000 /* structElemType - returns the type info of a sturct member */
1001 /*------------------------------------------------------------------*/
1003 structElemType (sym_link * stype, value * id, value ** argsp)
1005 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1006 sym_link *type, *etype;
1007 sym_link *petype = getSpec (stype);
1012 /* look for the id */
1015 if (strcmp (fields->rname, id->name) == 0)
1019 *argsp = fields->args;
1021 type = copyLinkChain (fields->type);
1022 etype = getSpec (type);
1023 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1024 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1027 fields = fields->next;
1029 werror (E_NOT_MEMBER, id->name);
1034 /*------------------------------------------------------------------*/
1035 /* getStructElement - returns element of a tructure definition */
1036 /*------------------------------------------------------------------*/
1038 getStructElement (structdef * sdef, symbol * sym)
1042 for (field = sdef->fields; field; field = field->next)
1043 if (strcmp (field->name, sym->name) == 0)
1046 werror (E_NOT_MEMBER, sym->name);
1048 return sdef->fields;
1051 /*------------------------------------------------------------------*/
1052 /* compStructSize - computes the size of a structure */
1053 /*------------------------------------------------------------------*/
1055 compStructSize (int su, structdef * sdef)
1057 int sum = 0, usum = 0;
1061 /* for the identifiers */
1062 loop = sdef->fields;
1065 /* create the internal name for this variable */
1066 sprintf (loop->rname, "_%s", loop->name);
1067 loop->offset = (su == UNION ? sum = 0 : sum);
1068 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1070 /* if this is a bit field */
1073 /* change it to a unsigned bit */
1074 SPEC_NOUN (loop->etype) = V_BIT;
1075 SPEC_USIGN (loop->etype) = 1;
1076 /* check if this fit into the remaining */
1077 /* bits of this byte else align it to the */
1078 /* next byte boundary */
1079 if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
1080 SPEC_BSTR (loop->etype) = bitOffset;
1081 if ((bitOffset += (loop->bitVar % 8)) == 8)
1084 else /* does not fit */ {
1086 SPEC_BSTR (loop->etype) = bitOffset;
1087 sum += (loop->bitVar / 8);
1088 bitOffset += (loop->bitVar % 8);
1090 /* if this is the last field then pad */
1091 if (!loop->next && bitOffset && bitOffset != 8) {
1097 checkDecl (loop, 1);
1098 sum += getSize (loop->type);
1101 /* if function then do the arguments for it */
1102 if (funcInChain (loop->type)) {
1103 processFuncArgs (loop, 1);
1108 /* if this is not a bitfield but the */
1109 /* previous one was and did not take */
1110 /* the whole byte then pad the rest */
1111 if ((loop && !loop->bitVar) && bitOffset) {
1116 /* if union then size = sizeof larget field */
1118 usum = max (usum, sum);
1122 return (su == UNION ? usum : sum);
1125 /*------------------------------------------------------------------*/
1126 /* checkSClass - check the storage class specification */
1127 /*------------------------------------------------------------------*/
1129 checkSClass (symbol * sym, int isProto)
1131 if (getenv("DEBUG_SANITY")) {
1132 fprintf (stderr, "checkSClass: %s \n", sym->name);
1134 if (strcmp(sym->name, "_testsGlobal")==0) {
1138 /* type is literal can happen foe enums change
1140 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1141 SPEC_SCLS (sym->etype) = S_AUTO;
1143 /* if sfr or sbit then must also be */
1144 /* volatile the initial value will be xlated */
1145 /* to an absolute address */
1146 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1147 SPEC_SCLS (sym->etype) == S_SFR)
1149 SPEC_VOLATILE (sym->etype) = 1;
1150 /* if initial value given */
1153 SPEC_ABSA (sym->etype) = 1;
1154 SPEC_ADDR (sym->etype) =
1155 (int) list2int (sym->ival);
1160 /* if absolute address given then it mark it as
1162 if (IS_ABSOLUTE (sym->etype))
1163 SPEC_VOLATILE (sym->etype) = 1;
1165 /* global variables declared const put into code */
1166 if (sym->level == 0 &&
1167 SPEC_CONST (sym->etype)) {
1168 SPEC_SCLS (sym->etype) = S_CODE;
1171 /* global variable in code space is a constant */
1172 if (sym->level == 0 &&
1173 SPEC_SCLS (sym->etype) == S_CODE &&
1175 SPEC_CONST (sym->etype) = 1;
1178 /* if bit variable then no storage class can be */
1179 /* specified since bit is already a storage */
1180 if (IS_BITVAR (sym->etype) &&
1181 (SPEC_SCLS (sym->etype) != S_FIXED &&
1182 SPEC_SCLS (sym->etype) != S_SBIT &&
1183 SPEC_SCLS (sym->etype) != S_BIT)
1186 werror (E_BITVAR_STORAGE, sym->name);
1187 SPEC_SCLS (sym->etype) = S_FIXED;
1190 /* extern variables cannot be initialized */
1191 if (IS_EXTERN (sym->etype) && sym->ival)
1193 werror (E_EXTERN_INIT, sym->name);
1197 /* if this is an atomatic symbol */
1198 if (sym->level && (options.stackAuto || reentrant)) {
1199 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1200 SPEC_SCLS (sym->etype) == S_FIXED ||
1201 SPEC_SCLS (sym->etype) == S_REGISTER ||
1202 SPEC_SCLS (sym->etype) == S_STACK ||
1203 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1204 SPEC_SCLS (sym->etype) = S_AUTO;
1206 /* storage class may only be specified for statics */
1207 if (!IS_STATIC(sym->etype)) {
1208 werror (E_AUTO_ASSUMED, sym->name);
1213 /* automatic symbols cannot be given */
1214 /* an absolute address ignore it */
1216 SPEC_ABSA (sym->etype) &&
1217 (options.stackAuto || reentrant))
1219 werror (E_AUTO_ABSA, sym->name);
1220 SPEC_ABSA (sym->etype) = 0;
1223 /* arrays & pointers cannot be defined for bits */
1224 /* SBITS or SFRs or BIT */
1225 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1226 (SPEC_NOUN (sym->etype) == V_BIT ||
1227 SPEC_NOUN (sym->etype) == V_SBIT ||
1228 SPEC_SCLS (sym->etype) == S_SFR))
1229 werror (E_BIT_ARRAY, sym->name);
1231 /* if this is a bit|sbit then set length & start */
1232 if (SPEC_NOUN (sym->etype) == V_BIT ||
1233 SPEC_NOUN (sym->etype) == V_SBIT)
1235 SPEC_BLEN (sym->etype) = 1;
1236 SPEC_BSTR (sym->etype) = 0;
1240 /* variables declared in CODE space must have */
1241 /* initializers if not an extern */
1242 if (SPEC_SCLS (sym->etype) == S_CODE &&
1243 sym->ival == NULL &&
1245 port->mem.code_ro &&
1246 !IS_EXTERN (sym->etype) &&
1247 !funcInChain (sym->type))
1248 werror (E_CODE_NO_INIT, sym->name);
1251 /* if parameter or local variable then change */
1252 /* the storage class to reflect where the var will go */
1253 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1254 !IS_STATIC(sym->etype))
1256 if (options.stackAuto || (currFunc && IS_RENT (currFunc->etype)))
1258 SPEC_SCLS (sym->etype) = (options.useXstack ?
1259 S_XSTACK : S_STACK);
1263 /* hack-o-matic! I see no reason why the useXstack option should ever
1264 * control this allcoation, but the code was originally that way, and
1265 * changing it for non-390 ports breaks the compiler badly.
1267 bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1268 SPEC_SCLS (sym->etype) = (useXdata ?
1274 /*------------------------------------------------------------------*/
1275 /* changePointer - change pointer to functions */
1276 /*------------------------------------------------------------------*/
1278 changePointer (symbol * sym)
1282 /* go thru the chain of declarations */
1283 /* if we find a pointer to a function */
1284 /* unconditionally change it to a ptr */
1286 for (p = sym->type; p; p = p->next)
1288 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1289 DCL_TYPE (p) = GPOINTER;
1290 if (IS_PTR (p) && IS_FUNC (p->next))
1291 DCL_TYPE (p) = CPOINTER;
1295 /*------------------------------------------------------------------*/
1296 /* checkDecl - does semantic validation of a declaration */
1297 /*------------------------------------------------------------------*/
1299 checkDecl (symbol * sym, int isProto)
1302 checkSClass (sym, isProto); /* check the storage class */
1303 changePointer (sym); /* change pointers if required */
1305 /* if this is an array without any dimension
1306 then update the dimension from the initial value */
1307 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1308 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1313 /*------------------------------------------------------------------*/
1314 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1315 /*------------------------------------------------------------------*/
1317 copyLinkChain (sym_link * p)
1319 sym_link *head, *curr, *loop;
1322 head = loop = (curr ? newLink () : (void *) NULL);
1325 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1326 loop->next = (curr->next ? newLink () : (void *) NULL);
1335 /*------------------------------------------------------------------*/
1336 /* cleanUpBlock - cleansup the symbol table specified for all the */
1337 /* symbols in the given block */
1338 /*------------------------------------------------------------------*/
1340 cleanUpBlock (bucket ** table, int block)
1345 /* go thru the entire table */
1346 for (i = 0; i < 256; i++)
1348 for (chain = table[i]; chain; chain = chain->next)
1350 if (chain->block >= block)
1352 deleteSym (table, chain->sym, chain->name);
1358 /*------------------------------------------------------------------*/
1359 /* cleanUpLevel - cleansup the symbol table specified for all the */
1360 /* symbols in the given level */
1361 /*------------------------------------------------------------------*/
1363 cleanUpLevel (bucket ** table, int level)
1368 /* go thru the entire table */
1369 for (i = 0; i < 256; i++)
1371 for (chain = table[i]; chain; chain = chain->next)
1373 if (chain->level >= level)
1375 deleteSym (table, chain->sym, chain->name);
1381 /*------------------------------------------------------------------*/
1382 /* computeType - computes the resultant type from two types */
1383 /*------------------------------------------------------------------*/
1385 computeType (sym_link * type1, sym_link * type2)
1389 sym_link *etype1 = getSpec (type1);
1390 sym_link *etype2 = getSpec (type2);
1392 /* if one of them is a float then result is a float */
1393 /* here we assume that the types passed are okay */
1394 /* and can be cast to one another */
1395 /* which ever is greater in size */
1396 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1397 rType = newFloatLink ();
1399 /* if only one of them is a bit variable
1400 then the other one prevails */
1401 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1402 rType = copyLinkChain (type2);
1403 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1404 rType = copyLinkChain (type1);
1406 /* if one of them is a pointer then that
1409 rType = copyLinkChain (type1);
1410 else if (IS_PTR (type2))
1411 rType = copyLinkChain (type2);
1412 else if (getSize (type1) > getSize (type2))
1413 rType = copyLinkChain (type1);
1415 rType = copyLinkChain (type2);
1417 reType = getSpec (rType);
1419 /* if either of them unsigned then make this unsigned */
1420 if ((SPEC_USIGN (etype1) || SPEC_USIGN (etype2)) && !IS_FLOAT (reType))
1421 SPEC_USIGN (reType) = 1;
1423 /* if result is a literal then make not so */
1424 if (IS_LITERAL (reType))
1425 SPEC_SCLS (reType) = S_REGISTER;
1430 /*--------------------------------------------------------------------*/
1431 /* compareType - will do type check return 1 if match, -1 if castable */
1432 /*--------------------------------------------------------------------*/
1434 compareType (sym_link * dest, sym_link * src)
1445 /* if dest is a declarator then */
1450 if (DCL_TYPE (src) == DCL_TYPE (dest))
1451 return compareType (dest->next, src->next);
1452 if (IS_PTR (src) && IS_PTR (dest))
1454 if (IS_PTR (dest) && IS_ARRAY (src))
1456 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1457 return -1 * compareType (dest->next, src);
1460 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1466 /* if one is a specifier and the other is not */
1467 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1468 (IS_SPEC (dest) && !IS_SPEC (src)))
1471 /* if one of them is a void then ok */
1472 if (SPEC_NOUN (dest) == V_VOID &&
1473 SPEC_NOUN (src) != V_VOID)
1476 if (SPEC_NOUN (dest) != V_VOID &&
1477 SPEC_NOUN (src) == V_VOID)
1480 /* if they are both bitfields then if the lengths
1481 and starts don't match */
1482 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1483 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1484 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1487 /* it is a specifier */
1488 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1490 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1491 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1492 getSize (dest) == getSize (src))
1494 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1499 else if (IS_STRUCT (dest))
1501 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1506 if (SPEC_LONG (dest) != SPEC_LONG (src))
1509 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1515 /*------------------------------------------------------------------*/
1516 /* inCalleeSaveList - return 1 if found in callee save list */
1517 /*------------------------------------------------------------------*/
1519 inCalleeSaveList (char *s)
1523 for (i = 0; options.calleeSaves[i]; i++)
1524 if (strcmp (options.calleeSaves[i], s) == 0)
1530 /*-----------------------------------------------------------------*/
1531 /* aggregateArgToPointer: change an agggregate type function */
1532 /* argument to a pointer to that type. */
1533 /*-----------------------------------------------------------------*/
1535 aggregateArgToPointer (value * val)
1537 if (IS_AGGREGATE (val->type))
1539 /* if this is a structure */
1540 /* then we need to add a new link */
1541 if (IS_STRUCT (val->type))
1543 /* first lets add DECLARATOR type */
1544 sym_link *p = val->type;
1546 werror (W_STRUCT_AS_ARG, val->name);
1547 val->type = newLink ();
1548 val->type->next = p;
1551 /* change to a pointer depending on the */
1552 /* storage class specified */
1553 switch (SPEC_SCLS (val->etype))
1556 DCL_TYPE (val->type) = IPOINTER;
1559 DCL_TYPE (val->type) = PPOINTER;
1562 if (TARGET_IS_DS390)
1564 /* The AUTO and REGISTER classes should probably
1565 * also become generic pointers, but I haven't yet
1566 * devised a test case for that.
1568 DCL_TYPE (val->type) = GPOINTER;
1575 DCL_TYPE (val->type) = POINTER;
1578 DCL_TYPE (val->type) = CPOINTER;
1581 DCL_TYPE (val->type) = FPOINTER;
1584 DCL_TYPE (val->type) = EEPPOINTER;
1587 DCL_TYPE (val->type) = GPOINTER;
1590 /* is there is a symbol associated then */
1591 /* change the type of the symbol as well */
1594 val->sym->type = copyLinkChain (val->type);
1595 val->sym->etype = getSpec (val->sym->type);
1599 /*------------------------------------------------------------------*/
1600 /* checkFunction - does all kinds of check on a function */
1601 /*------------------------------------------------------------------*/
1603 checkFunction (symbol * sym)
1606 value *exargs, *acargs;
1610 if (getenv("DEBUG_SANITY")) {
1611 fprintf (stderr, "checkFunction: %s ", sym->name);
1614 /* make sure the type is complete and sane */
1615 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1617 /* if not type then some kind of error */
1621 /* if the function has no type then make it return int */
1622 if (!sym->type->next)
1623 sym->type->next = sym->etype = newIntLink ();
1625 /* function cannot return aggregate */
1626 if (IS_AGGREGATE (sym->type->next))
1628 werror (E_FUNC_AGGR, sym->name);
1632 /* function cannot return bit */
1633 if (IS_BITVAR (sym->type->next))
1635 werror (E_FUNC_BIT, sym->name);
1639 /* check if this function is defined as calleeSaves
1640 then mark it as such */
1641 sym->calleeSave = inCalleeSaveList (sym->name);
1643 /* if interrupt service routine */
1644 /* then it cannot have arguments */
1645 if (sym->args && IS_ISR (sym->etype) && !IS_VOID (sym->args->type))
1647 werror (E_INT_ARGS, sym->name);
1651 if (!(csym = findSym (SymbolTab, sym, sym->name)))
1652 return 1; /* not defined nothing more to check */
1654 /* check if body already present */
1655 if (csym && csym->fbody)
1657 werror (E_FUNC_BODY, sym->name);
1661 /* check the return value type */
1662 if (compareType (csym->type, sym->type) <= 0)
1664 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1665 werror (W_CONTINUE, "previous definition type ");
1666 printTypeChain (csym->type, stderr);
1667 fprintf (stderr, "\n");
1668 werror (W_CONTINUE, "current definition type ");
1669 printTypeChain (sym->type, stderr);
1670 fprintf (stderr, "\n");
1674 if (SPEC_INTRTN (csym->etype) != SPEC_INTRTN (sym->etype))
1676 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1679 if (SPEC_BANK (csym->etype) != SPEC_BANK (sym->etype))
1681 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1684 if (SPEC_NAKED (csym->etype) != SPEC_NAKED (sym->etype))
1686 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1689 /* compare expected agrs with actual args */
1690 exargs = csym->args;
1693 /* for all the expected args do */
1696 exargs = exargs->next, acargs = acargs->next, argCnt++)
1698 if (getenv("DEBUG_SANITY")) {
1699 fprintf (stderr, "checkFunction: %s ", exargs->name);
1701 /* make sure the type is complete and sane */
1702 checkTypeSanity(exargs->etype, exargs->name);
1704 /* If the actual argument is an array, any prototype
1705 * will have modified it to a pointer. Duplicate that
1708 if (IS_AGGREGATE (acargs->type))
1710 checkValue = copyValue (acargs);
1711 aggregateArgToPointer (checkValue);
1715 checkValue = acargs;
1718 if (compareType (exargs->type, checkValue->type) <= 0)
1720 werror (E_ARG_TYPE, argCnt);
1725 /* if one them ended we have a problem */
1726 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1727 (!exargs && acargs && !IS_VOID (acargs->type)))
1728 werror (E_ARG_COUNT);
1730 /* replace with this defition */
1731 sym->cdef = csym->cdef;
1732 deleteSym (SymbolTab, csym, csym->name);
1733 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1734 if (IS_EXTERN (csym->etype) && !
1735 IS_EXTERN (sym->etype))
1737 addSet (&publics, sym);
1742 /*-----------------------------------------------------------------*/
1743 /* processFuncArgs - does some processing with function args */
1744 /*-----------------------------------------------------------------*/
1746 processFuncArgs (symbol * func, int ignoreName)
1751 /* if this function has variable argument list */
1752 /* then make the function a reentrant one */
1754 SPEC_RENT (func->etype) = 1;
1756 /* check if this function is defined as calleeSaves
1757 then mark it as such */
1758 func->calleeSave = inCalleeSaveList (func->name);
1760 val = func->args; /* loop thru all the arguments */
1762 /* if it is void then remove parameters */
1763 if (val && IS_VOID (val->type))
1769 /* reset regparm for the port */
1770 (*port->reset_regparms) ();
1771 /* if any of the arguments is an aggregate */
1772 /* change it to pointer to the same type */
1775 /* mark it as a register parameter if
1776 the function does not have VA_ARG
1777 and as port dictates */
1778 if (!func->hasVargs &&
1779 (*port->reg_parm) (val->type))
1781 SPEC_REGPARM (val->etype) = 1;
1784 if (IS_AGGREGATE (val->type))
1786 aggregateArgToPointer (val);
1792 /* if this is an internal generated function call */
1794 /* ignore --stack-auto for this one, we don't know how it is compiled */
1795 /* simply trust on --int-long-reent or --float-reent */
1796 if (IS_RENT(func->etype)) {
1800 /* if this function is reentrant or */
1801 /* automatics r 2b stacked then nothing */
1802 if (IS_RENT (func->etype) || options.stackAuto)
1811 /* if a symbolname is not given */
1812 /* synthesize a variable name */
1816 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1817 val->sym = newSymbol (val->name, 1);
1818 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1819 val->sym->type = copyLinkChain (val->type);
1820 val->sym->etype = getSpec (val->sym->type);
1821 val->sym->_isparm = 1;
1822 strcpy (val->sym->rname, val->name);
1823 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1824 SPEC_STAT (func->etype);
1825 addSymChain (val->sym);
1828 else /* symbol name given create synth name */
1831 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1832 strcpy (val->sym->rname, val->name);
1833 val->sym->_isparm = 1;
1834 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1835 (options.model != MODEL_SMALL ? xdata : data);
1836 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1837 SPEC_STAT (func->etype);
1843 /*-----------------------------------------------------------------*/
1844 /* isSymbolEqual - compares two symbols return 1 if they match */
1845 /*-----------------------------------------------------------------*/
1847 isSymbolEqual (symbol * dest, symbol * src)
1849 /* if pointers match then equal */
1853 /* if one of them is null then don't match */
1857 /* if both of them have rname match on rname */
1858 if (dest->rname[0] && src->rname[0])
1859 return (!strcmp (dest->rname, src->rname));
1861 /* otherwise match on name */
1862 return (!strcmp (dest->name, src->name));
1865 void PT(sym_link *type)
1867 printTypeChain(type,0);
1869 /*-----------------------------------------------------------------*/
1870 /* printTypeChain - prints the type chain in human readable form */
1871 /*-----------------------------------------------------------------*/
1873 printTypeChain (sym_link * type, FILE * of)
1887 if (DCL_PTR_VOLATILE(type)) {
1888 fprintf (of, "volatile ");
1890 switch (DCL_TYPE (type))
1893 fprintf (of, "function ");
1896 fprintf (of, "_generic * ");
1897 if (DCL_PTR_CONST (type))
1898 fprintf (of, "const ");
1901 fprintf (of, "_code * ");
1902 if (DCL_PTR_CONST (type))
1903 fprintf (of, "const ");
1906 fprintf (of, "_far * ");
1907 if (DCL_PTR_CONST (type))
1908 fprintf (of, "const ");
1911 fprintf (of, "_eeprom * ");
1912 if (DCL_PTR_CONST (type))
1913 fprintf (of, "const ");
1917 fprintf (of, "_near * ");
1918 if (DCL_PTR_CONST (type))
1919 fprintf (of, "const ");
1922 fprintf (of, "_idata *");
1923 if (DCL_PTR_CONST (type))
1924 fprintf (of, "const ");
1927 fprintf (of, "_pdata *");
1928 if (DCL_PTR_CONST (type))
1929 fprintf (of, "const ");
1932 fprintf (of, " _unkown *");
1933 if (DCL_PTR_CONST (type))
1934 fprintf (of, "const ");
1937 fprintf (of, "array of ");
1943 if (SPEC_VOLATILE (type))
1944 fprintf (of, "volatile ");
1945 if (SPEC_USIGN (type))
1946 fprintf (of, "unsigned ");
1947 if (SPEC_CONST (type))
1948 fprintf (of, "const ");
1950 switch (SPEC_NOUN (type))
1954 fprintf (of, "long ");
1955 fprintf (of, "int ");
1959 fprintf (of, "char ");
1963 fprintf (of, "void ");
1967 fprintf (of, "float ");
1971 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
1975 fprintf (of, "sbit ");
1979 fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
1983 fprintf (of, "double ");
1987 fprintf (of, "unknown type ");
1997 /*-----------------------------------------------------------------*/
1998 /* cdbTypeInfo - print the type information for debugger */
1999 /*-----------------------------------------------------------------*/
2001 cdbTypeInfo (sym_link * type, FILE * of)
2003 fprintf (of, "{%d}", getSize (type));
2008 switch (DCL_TYPE (type))
2011 fprintf (of, "DF,");
2014 fprintf (of, "DG,");
2017 fprintf (of, "DC,");
2020 fprintf (of, "DX,");
2023 fprintf (of, "DD,");
2026 fprintf (of, "DI,");
2029 fprintf (of, "DP,");
2032 fprintf (of, "DA,");
2035 fprintf (of, "DA%d,", DCL_ELEM (type));
2043 switch (SPEC_NOUN (type))
2065 fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
2073 fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
2080 if (SPEC_USIGN (type))
2088 /*-----------------------------------------------------------------*/
2089 /* cdbSymbol - prints a symbol & its type information for debugger */
2090 /*-----------------------------------------------------------------*/
2092 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
2104 fprintf (of, "S:"); /* symbol record */
2105 /* if this is not a structure symbol then
2106 we need to figure out the scope information */
2112 if (IS_STATIC (sym->etype))
2113 fprintf (of, "F%s$", moduleName); /* scope is file */
2115 fprintf (of, "G$"); /* scope is global */
2118 /* symbol is local */
2119 fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
2122 fprintf (of, "S$"); /* scope is structure */
2124 /* print the name, & mangled name */
2125 fprintf (of, "%s$%d$%d(", sym->name,
2126 sym->level, sym->block);
2128 cdbTypeInfo (sym->type, of);
2131 /* print the address space */
2132 map = SPEC_OCLS (sym->etype);
2133 fprintf (of, "%c,%d,%d",
2134 (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
2136 /* if assigned to registers then output register names */
2137 /* if this is a function then print
2138 if is it an interrupt routine & interrupt number
2139 and the register bank it is using */
2141 fprintf (of, ",%d,%d,%d", SPEC_INTRTN (sym->etype),
2142 SPEC_INTN (sym->etype), SPEC_BANK (sym->etype));
2143 /* alternate location to find this symbol @ : eg registers
2150 /*-----------------------------------------------------------------*/
2151 /* cdbStruct - print a structure for debugger */
2152 /*-----------------------------------------------------------------*/
2154 cdbStruct (structdef * sdef, int block, FILE * of,
2155 int inStruct, char *tag)
2160 /* if block # then must have function scope */
2161 fprintf (of, "F%s$", moduleName);
2162 fprintf (of, "%s[", (tag ? tag : sdef->tag));
2163 for (sym = sdef->fields; sym; sym = sym->next)
2165 fprintf (of, "({%d}", sym->offset);
2166 cdbSymbol (sym, of, TRUE, FALSE);
2174 /*------------------------------------------------------------------*/
2175 /* cdbStructBlock - calls struct printing for a blcks */
2176 /*------------------------------------------------------------------*/
2178 cdbStructBlock (int block, FILE * of)
2181 bucket **table = StructTab;
2185 /* go thru the entire table */
2186 for (i = 0; i < 256; i++)
2188 for (chain = table[i]; chain; chain = chain->next)
2190 if (chain->block >= block)
2192 cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2198 /*-----------------------------------------------------------------*/
2199 /* powof2 - returns power of two for the number if number is pow 2 */
2200 /*-----------------------------------------------------------------*/
2202 powof2 (unsigned long num)
2215 if (n1s > 1 || nshifts == 0)
2231 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2232 symbol *__muldiv[3][3][2];
2233 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2234 sym_link *__multypes[3][2];
2235 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2236 symbol *__conv[2][3][2];
2237 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2238 symbol *__rlrr[2][3][2];
2240 sym_link *floatType;
2243 _mangleFunctionName(char *in)
2245 if (port->getMangledFunctionName)
2247 return port->getMangledFunctionName(in);
2255 /*-----------------------------------------------------------------*/
2256 /* initCSupport - create functions for C support routines */
2257 /*-----------------------------------------------------------------*/
2261 const char *smuldivmod[] =
2265 const char *sbwd[] =
2267 "char", "int", "long"
2273 const char *srlrr[] =
2278 int bwd, su, muldivmod, tofrom, rlrr;
2280 if (getenv("SDCC_NO_C_SUPPORT")) {
2281 /* for debugging only */
2285 floatType = newFloatLink ();
2287 for (bwd = 0; bwd < 3; bwd++)
2304 __multypes[bwd][0] = l;
2305 __multypes[bwd][1] = copyLinkChain (l);
2306 SPEC_USIGN (__multypes[bwd][1]) = 1;
2309 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2310 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2311 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2312 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2313 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2314 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2315 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2316 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2317 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2318 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2320 for (tofrom = 0; tofrom < 2; tofrom++)
2322 for (bwd = 0; bwd < 3; bwd++)
2324 for (su = 0; su < 2; su++)
2328 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2329 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], floatType, 1, options.float_rent);
2333 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2334 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), floatType, __multypes[bwd][su], 1, options.float_rent);
2340 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2342 for (bwd = 0; bwd < 3; bwd++)
2344 for (su = 0; su < 2; su++)
2346 sprintf (buffer, "_%s%s%s",
2347 smuldivmod[muldivmod],
2350 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2351 SPEC_NONBANKED (__muldiv[muldivmod][bwd][su]->etype) = 1;
2356 for (rlrr = 0; rlrr < 2; rlrr++)
2358 for (bwd = 0; bwd < 3; bwd++)
2360 for (su = 0; su < 2; su++)
2362 sprintf (buffer, "_%s%s%s",
2366 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2367 SPEC_NONBANKED (__rlrr[rlrr][bwd][su]->etype) = 1;