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
221 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
223 /* if this is parameter then nothing else need to be checked */
224 if (((symbol *) (bp->sym))->_isparm)
226 /* if levels match then block numbers hsould also match */
227 if (bp->level && bp->level == sym->level && bp->block == sym->block)
229 /* if levels don't match then we are okay */
230 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
232 /* if this is a global variable then we are ok too */
240 return (void *) NULL;
243 /*-----------------------------------------------------------------*/
244 /* findSymWithBlock - finds a symbol with name in with a block */
245 /*-----------------------------------------------------------------*/
247 findSymWithBlock (bucket ** stab, symbol * sym, int block)
251 bp = stab[hashKey (sym->name)];
254 if (strcmp (bp->name, sym->name) == 0 &&
260 return (bp ? bp->sym : (void *) NULL);
263 /*------------------------------------------------------------------*/
264 /* newSymbol () - returns a new pointer to a symbol */
265 /*------------------------------------------------------------------*/
267 newSymbol (char *name, int scope)
271 sym = Safe_calloc (1, sizeof (symbol));
273 strcpy (sym->name, name); /* copy the name */
274 sym->level = scope; /* set the level */
275 sym->block = currBlockno;
276 sym->lineDef = yylineno; /* set the line number */
280 /*------------------------------------------------------------------*/
281 /* newLink - creates a new link (declarator,specifier) */
282 /*------------------------------------------------------------------*/
288 p = Safe_calloc (1, sizeof (sym_link));
293 /*------------------------------------------------------------------*/
294 /* newStruct - creats a new structdef from the free list */
295 /*------------------------------------------------------------------*/
297 newStruct (char *tag)
301 s = Safe_calloc (1, sizeof (structdef));
303 strcpy (s->tag, tag); /* copy the tag */
307 /*------------------------------------------------------------------*/
308 /* pointerTypes - do the computation for the pointer types */
309 /*------------------------------------------------------------------*/
311 pointerTypes (sym_link * ptr, sym_link * type)
316 /* find the first pointer type */
317 while (ptr && !IS_PTR (ptr))
320 /* could not find it */
321 if (!ptr || IS_SPEC (ptr))
324 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
325 pointerTypes (ptr->next, type);
329 /* change the pointer type depending on the
330 storage class of the type */
333 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
334 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
335 switch (SPEC_SCLS (type))
338 DCL_TYPE (ptr) = FPOINTER;
341 DCL_TYPE (ptr) = IPOINTER;
344 DCL_TYPE (ptr) = PPOINTER;
347 DCL_TYPE (ptr) = POINTER;
350 DCL_PTR_CONST (ptr) = port->mem.code_ro;
351 DCL_TYPE (ptr) = CPOINTER;
354 DCL_TYPE (ptr) = EEPPOINTER;
357 DCL_TYPE (ptr) = GPOINTER;
360 /* the storage class of type ends here */
363 SPEC_VOLATILE (type) = 0;
366 /* now change all the remaining unknown pointers
367 to generic pointers */
370 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
371 DCL_TYPE (ptr) = GPOINTER;
375 /* same for the type although it is highly unlikely that
376 type will have a pointer */
379 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
380 DCL_TYPE (type) = GPOINTER;
386 /*------------------------------------------------------------------*/
387 /* addDecl - adds a declarator @ the end of a chain */
388 /*------------------------------------------------------------------*/
390 addDecl (symbol * sym, int type, sym_link * p)
396 /* if we are passed a link then set head & tail */
405 head = tail = newLink ();
406 DCL_TYPE (head) = type;
409 /* if this is the first entry */
417 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
419 sym->etype = mergeSpec (sym->etype, head, sym->name);
423 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
426 while (t->next != sym->etype)
429 tail->next = sym->etype;
433 sym->etype->next = head;
439 /* if the type is an unknown pointer and has
440 a tspec then take the storage class const & volatile
441 attribute from the tspec & make it those of this
445 //DCL_TYPE (p) == UPOINTER &&
448 if (!IS_SPEC (sym->etype))
450 sym->etype = sym->etype->next = newLink ();
451 sym->etype->class = SPECIFIER;
453 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
454 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
455 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
456 DCL_TSPEC (p) = NULL;
461 /*------------------------------------------------------------------
462 checkTypeSanity: prevent the user from doing e.g.:
464 ------------------------------------------------------------------*/
465 void checkTypeSanity(sym_link *etype, char *name) {
469 if (getenv("DEBUG_SANITY")) {
470 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
475 if (!IS_SPEC(etype)) {
476 if (getenv("DEBUG_SANITY")) {
477 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
482 noun=nounName(etype);
484 if (getenv("DEBUG_SANITY")) {
485 fprintf (stderr, "checking sanity for %s %x\n", name, (int)etype);
488 if ((SPEC_NOUN(etype)==V_CHAR ||
489 SPEC_NOUN(etype)==V_FLOAT ||
490 SPEC_NOUN(etype)==V_DOUBLE ||
491 SPEC_NOUN(etype)==V_VOID) &&
492 (etype->select.s._short || SPEC_LONG(etype))) {
493 // long or short for char float double or void
494 werror (E_LONG_OR_SHORT_INVALID, noun, name);
496 if ((SPEC_NOUN(etype)==V_FLOAT ||
497 SPEC_NOUN(etype)==V_DOUBLE ||
498 SPEC_NOUN(etype)==V_VOID) &&
499 (etype->select.s._signed || SPEC_USIGN(etype))) {
500 // signed or unsigned for float double or void
501 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
504 // special case for "short"
505 if (etype->select.s._short) {
506 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
507 etype->select.s._short = 0;
511 "const a;" or "data b;" or "signed s" or "long l"
513 if (!SPEC_NOUN(etype)) {
514 SPEC_NOUN(etype)=V_INT;
517 if (etype->select.s._signed && SPEC_USIGN(etype)) {
518 // signed AND unsigned
519 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
521 if (etype->select.s._short && SPEC_LONG(etype)) {
523 werror (E_LONG_AND_SHORT_INVALID, noun, name);
528 /*------------------------------------------------------------------*/
529 /* mergeSpec - merges two specifiers and returns the new one */
530 /*------------------------------------------------------------------*/
532 mergeSpec (sym_link * dest, sym_link * src, char *name)
537 if (!IS_SPEC(dest)) {
538 // This can happen for pointers, find the end type
539 while (dest && !IS_SPEC(dest))
543 // here we have a declarator as source, reverse them
547 while (dest && !IS_SPEC(dest)) {
548 // and find the specifier
555 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
556 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
560 if (getenv("DEBUG_mergeSpec")) {
561 fprintf (stderr, "mergeSpec: \"%s\"\n", name);
564 if (SPEC_NOUN(src)) {
565 if (!SPEC_NOUN(dest)) {
566 SPEC_NOUN(dest)=SPEC_NOUN(src);
568 /* we shouldn't redeclare the type */
569 if (getenv("DEBUG_SANITY")) {
570 fprintf (stderr, "mergeSpec: ");
572 werror(E_TWO_OR_MORE_DATA_TYPES, name);
576 if (SPEC_SCLS(src)) {
577 /* if destination has no storage class */
578 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
579 SPEC_SCLS (dest) = SPEC_SCLS (src);
581 if (getenv("DEBUG_SANITY")) {
582 fprintf (stderr, "mergeSpec: ");
584 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
588 /* copy all the specifications */
590 // we really should do:
592 if (SPEC_what(src)) {
593 if (SPEC_what(dest)) {
594 werror(W_DUPLICATE_SPEC, "what");
596 SPEC_what(dst)|=SPEC_what(src);
599 // but there are more important thing right now
601 SPEC_LONG (dest) |= SPEC_LONG (src);
602 dest->select.s._short|=src->select.s._short;
603 SPEC_USIGN (dest) |= SPEC_USIGN (src);
604 dest->select.s._signed|=src->select.s._signed;
605 SPEC_STAT (dest) |= SPEC_STAT (src);
606 SPEC_EXTR (dest) |= SPEC_EXTR (src);
607 SPEC_ABSA (dest) |= SPEC_ABSA (src);
608 SPEC_RENT (dest) |= SPEC_RENT (src);
609 SPEC_INTN (dest) |= SPEC_INTN (src);
610 SPEC_BANK (dest) |= SPEC_BANK (src);
611 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
612 SPEC_CRTCL (dest) |= SPEC_CRTCL (src);
613 SPEC_ADDR (dest) |= SPEC_ADDR (src);
614 SPEC_OCLS (dest) = SPEC_OCLS (src);
615 SPEC_BLEN (dest) |= SPEC_BLEN (src);
616 SPEC_BSTR (dest) |= SPEC_BSTR (src);
617 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
618 SPEC_NONBANKED (dest) |= SPEC_NONBANKED (src);
619 SPEC_NAKED (dest) |= SPEC_NAKED (src);
621 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
622 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
627 /*------------------------------------------------------------------*/
628 /* cloneSpec - copies the entire spec and returns a new spec */
629 /*------------------------------------------------------------------*/
631 cloneSpec (sym_link * src)
635 /* go thru chain till we find the specifier */
636 while (src && src->class != SPECIFIER)
640 memcpy (spec, src, sizeof (sym_link));
644 /*------------------------------------------------------------------*/
645 /* genSymName - generates and returns a name used for anonymous vars */
646 /*------------------------------------------------------------------*/
648 genSymName (int level)
650 static int gCount = 0;
651 static char gname[SDCC_NAME_MAX + 1];
653 sprintf (gname, "__%04d%04d", level, gCount++);
657 /*------------------------------------------------------------------*/
658 /* getSpec - returns the specifier part from a declaration chain */
659 /*------------------------------------------------------------------*/
661 getSpec (sym_link * p)
666 while (p && !(IS_SPEC (p)))
672 /*------------------------------------------------------------------*/
673 /* newCharLink() - creates an char type */
674 /*------------------------------------------------------------------*/
681 p->class = SPECIFIER;
682 SPEC_NOUN (p) = V_CHAR;
687 /*------------------------------------------------------------------*/
688 /* newFloatLink - a new Float type */
689 /*------------------------------------------------------------------*/
696 p->class = SPECIFIER;
697 SPEC_NOUN (p) = V_FLOAT;
702 /*------------------------------------------------------------------*/
703 /* newLongLink() - new long type */
704 /*------------------------------------------------------------------*/
711 p->class = SPECIFIER;
712 SPEC_NOUN (p) = V_INT;
718 /*------------------------------------------------------------------*/
719 /* newIntLink() - creates an int type */
720 /*------------------------------------------------------------------*/
727 p->class = SPECIFIER;
728 SPEC_NOUN (p) = V_INT;
733 /*------------------------------------------------------------------*/
734 /* getSize - returns size of a type chain in bits */
735 /*------------------------------------------------------------------*/
737 getSize (sym_link * p)
739 /* if nothing return 0 */
743 { /* if this is the specifier then */
744 switch (SPEC_NOUN (p))
745 { /* depending on the specifier type */
747 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
755 return SPEC_STRUCT (p)->size;
761 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
767 /* this is a specifier */
768 switch (DCL_TYPE (p))
773 return DCL_ELEM (p) * getSize (p->next);
790 /*------------------------------------------------------------------*/
791 /* bitsForType - returns # of bits required to store this type */
792 /*------------------------------------------------------------------*/
794 bitsForType (sym_link * p)
796 /* if nothing return 0 */
801 { /* if this is the specifier then */
803 switch (SPEC_NOUN (p))
804 { /* depending on the specifier type */
806 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
808 return FLOATSIZE * 8;
814 return SPEC_STRUCT (p)->size * 8;
820 return SPEC_BLEN (p);
826 /* this is a specifier */
827 switch (DCL_TYPE (p))
832 return DCL_ELEM (p) * getSize (p->next) * 8;
836 return (PTRSIZE * 8);
840 return (FPTRSIZE * 8);
842 return (GPTRSIZE * 8);
849 /*------------------------------------------------------------------*/
850 /* copySymbolChain - copies a symbol chain */
851 /*------------------------------------------------------------------*/
853 copySymbolChain (symbol * src)
860 dest = copySymbol (src);
861 dest->next = copySymbolChain (src->next);
865 /*------------------------------------------------------------------*/
866 /* copySymbol - makes a copy of a symbol */
867 /*------------------------------------------------------------------*/
869 copySymbol (symbol * src)
876 dest = newSymbol (src->name, src->level);
877 memcpy (dest, src, sizeof (symbol));
878 dest->level = src->level;
879 dest->block = src->block;
880 dest->ival = copyIlist (src->ival);
881 dest->type = copyLinkChain (src->type);
882 dest->etype = getSpec (dest->type);
884 dest->args = copyValueChain (src->args);
885 dest->key = src->key;
886 dest->calleeSave = src->calleeSave;
887 dest->allocreq = src->allocreq;
891 /*------------------------------------------------------------------*/
892 /* reverseSyms - reverses the links for a symbol chain */
893 /*------------------------------------------------------------------*/
895 reverseSyms (symbol * sym)
897 symbol *prev, *curr, *next;
912 sym->next = (void *) NULL;
916 /*------------------------------------------------------------------*/
917 /* reverseLink - reverses the links for a type chain */
918 /*------------------------------------------------------------------*/
920 reverseLink (sym_link * type)
922 sym_link *prev, *curr, *next;
937 type->next = (void *) NULL;
941 /*------------------------------------------------------------------*/
942 /* addSymChain - adds a symbol chain to the symboltable */
943 /*------------------------------------------------------------------*/
945 addSymChain (symbol * symHead)
947 symbol *sym = symHead;
950 for (; sym != NULL; sym = sym->next)
953 checkTypeSanity(sym->etype, sym->name);
955 /* if already exists in the symbol table then check if
956 one of them is an extern definition if yes then
957 then check if the type match, if the types match then
958 delete the current entry and add the new entry */
959 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
960 csym->level == sym->level) {
962 /* one definition extern ? */
963 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
964 /* do types match ? */
965 if (compareType (csym->type, sym->type) != 1) {
967 werror (E_EXTERN_MISMATCH, csym->name);
970 /* delete current entry */
971 deleteSym (SymbolTab, csym, csym->name);
974 werror (E_DUPLICATE, sym->name);
980 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
985 /*------------------------------------------------------------------*/
986 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
987 /*------------------------------------------------------------------*/
989 funcInChain (sym_link * lnk)
1000 /*------------------------------------------------------------------*/
1001 /* structElemType - returns the type info of a sturct member */
1002 /*------------------------------------------------------------------*/
1004 structElemType (sym_link * stype, value * id, value ** argsp)
1006 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1007 sym_link *type, *etype;
1008 sym_link *petype = getSpec (stype);
1013 /* look for the id */
1016 if (strcmp (fields->rname, id->name) == 0)
1020 *argsp = fields->args;
1022 type = copyLinkChain (fields->type);
1023 etype = getSpec (type);
1024 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1025 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1028 fields = fields->next;
1030 werror (E_NOT_MEMBER, id->name);
1035 /*------------------------------------------------------------------*/
1036 /* getStructElement - returns element of a tructure definition */
1037 /*------------------------------------------------------------------*/
1039 getStructElement (structdef * sdef, symbol * sym)
1043 for (field = sdef->fields; field; field = field->next)
1044 if (strcmp (field->name, sym->name) == 0)
1047 werror (E_NOT_MEMBER, sym->name);
1049 return sdef->fields;
1052 /*------------------------------------------------------------------*/
1053 /* compStructSize - computes the size of a structure */
1054 /*------------------------------------------------------------------*/
1056 compStructSize (int su, structdef * sdef)
1058 int sum = 0, usum = 0;
1062 /* for the identifiers */
1063 loop = sdef->fields;
1066 /* create the internal name for this variable */
1067 sprintf (loop->rname, "_%s", loop->name);
1068 loop->offset = (su == UNION ? sum = 0 : sum);
1069 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1071 /* if this is a bit field */
1074 /* change it to a unsigned bit */
1075 SPEC_NOUN (loop->etype) = V_BIT;
1076 SPEC_USIGN (loop->etype) = 1;
1077 /* check if this fit into the remaining */
1078 /* bits of this byte else align it to the */
1079 /* next byte boundary */
1080 if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
1081 SPEC_BSTR (loop->etype) = bitOffset;
1082 if ((bitOffset += (loop->bitVar % 8)) == 8)
1085 else /* does not fit */ {
1087 SPEC_BSTR (loop->etype) = bitOffset;
1088 sum += (loop->bitVar / 8);
1089 bitOffset += (loop->bitVar % 8);
1091 /* if this is the last field then pad */
1092 if (!loop->next && bitOffset && bitOffset != 8) {
1098 checkDecl (loop, 1);
1099 sum += getSize (loop->type);
1102 /* if function then do the arguments for it */
1103 if (funcInChain (loop->type)) {
1104 processFuncArgs (loop, 1);
1109 /* if this is not a bitfield but the */
1110 /* previous one was and did not take */
1111 /* the whole byte then pad the rest */
1112 if ((loop && !loop->bitVar) && bitOffset) {
1117 /* if union then size = sizeof larget field */
1119 usum = max (usum, sum);
1123 return (su == UNION ? usum : sum);
1126 /*------------------------------------------------------------------*/
1127 /* checkSClass - check the storage class specification */
1128 /*------------------------------------------------------------------*/
1130 checkSClass (symbol * sym, int isProto)
1132 if (getenv("DEBUG_SANITY")) {
1133 fprintf (stderr, "checkSClass: %s \n", sym->name);
1135 if (strcmp(sym->name, "_testsGlobal")==0) {
1139 /* type is literal can happen foe enums change
1141 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1142 SPEC_SCLS (sym->etype) = S_AUTO;
1144 /* if sfr or sbit then must also be */
1145 /* volatile the initial value will be xlated */
1146 /* to an absolute address */
1147 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1148 SPEC_SCLS (sym->etype) == S_SFR)
1150 SPEC_VOLATILE (sym->etype) = 1;
1151 /* if initial value given */
1154 SPEC_ABSA (sym->etype) = 1;
1155 SPEC_ADDR (sym->etype) =
1156 (int) list2int (sym->ival);
1161 /* if absolute address given then it mark it as
1163 if (IS_ABSOLUTE (sym->etype))
1164 SPEC_VOLATILE (sym->etype) = 1;
1166 /* global variables declared const put into code */
1167 if (sym->level == 0 &&
1168 SPEC_CONST (sym->etype)) {
1169 SPEC_SCLS (sym->etype) = S_CODE;
1172 /* global variable in code space is a constant */
1173 if (sym->level == 0 &&
1174 SPEC_SCLS (sym->etype) == S_CODE &&
1176 SPEC_CONST (sym->etype) = 1;
1179 /* if bit variable then no storage class can be */
1180 /* specified since bit is already a storage */
1181 if (IS_BITVAR (sym->etype) &&
1182 (SPEC_SCLS (sym->etype) != S_FIXED &&
1183 SPEC_SCLS (sym->etype) != S_SBIT &&
1184 SPEC_SCLS (sym->etype) != S_BIT)
1187 werror (E_BITVAR_STORAGE, sym->name);
1188 SPEC_SCLS (sym->etype) = S_FIXED;
1191 /* extern variables cannot be initialized */
1192 if (IS_EXTERN (sym->etype) && sym->ival)
1194 werror (E_EXTERN_INIT, sym->name);
1198 /* if this is an atomatic symbol */
1199 if (sym->level && (options.stackAuto || reentrant)) {
1200 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1201 SPEC_SCLS (sym->etype) == S_FIXED ||
1202 SPEC_SCLS (sym->etype) == S_REGISTER ||
1203 SPEC_SCLS (sym->etype) == S_STACK ||
1204 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1205 SPEC_SCLS (sym->etype) = S_AUTO;
1207 /* storage class may only be specified for statics */
1208 if (!IS_STATIC(sym->etype)) {
1209 werror (E_AUTO_ASSUMED, sym->name);
1214 /* automatic symbols cannot be given */
1215 /* an absolute address ignore it */
1217 SPEC_ABSA (sym->etype) &&
1218 (options.stackAuto || reentrant))
1220 werror (E_AUTO_ABSA, sym->name);
1221 SPEC_ABSA (sym->etype) = 0;
1224 /* arrays & pointers cannot be defined for bits */
1225 /* SBITS or SFRs or BIT */
1226 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1227 (SPEC_NOUN (sym->etype) == V_BIT ||
1228 SPEC_NOUN (sym->etype) == V_SBIT ||
1229 SPEC_SCLS (sym->etype) == S_SFR))
1230 werror (E_BIT_ARRAY, sym->name);
1232 /* if this is a bit|sbit then set length & start */
1233 if (SPEC_NOUN (sym->etype) == V_BIT ||
1234 SPEC_NOUN (sym->etype) == V_SBIT)
1236 SPEC_BLEN (sym->etype) = 1;
1237 SPEC_BSTR (sym->etype) = 0;
1241 /* variables declared in CODE space must have */
1242 /* initializers if not an extern */
1243 if (SPEC_SCLS (sym->etype) == S_CODE &&
1244 sym->ival == NULL &&
1246 port->mem.code_ro &&
1247 !IS_EXTERN (sym->etype) &&
1248 !funcInChain (sym->type))
1249 werror (E_CODE_NO_INIT, sym->name);
1252 /* if parameter or local variable then change */
1253 /* the storage class to reflect where the var will go */
1254 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1255 !IS_STATIC(sym->etype))
1257 if (options.stackAuto || (currFunc && IS_RENT (currFunc->etype)))
1259 SPEC_SCLS (sym->etype) = (options.useXstack ?
1260 S_XSTACK : S_STACK);
1264 /* hack-o-matic! I see no reason why the useXstack option should ever
1265 * control this allcoation, but the code was originally that way, and
1266 * changing it for non-390 ports breaks the compiler badly.
1268 bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1269 SPEC_SCLS (sym->etype) = (useXdata ?
1275 /*------------------------------------------------------------------*/
1276 /* changePointer - change pointer to functions */
1277 /*------------------------------------------------------------------*/
1279 changePointer (symbol * sym)
1283 /* go thru the chain of declarations */
1284 /* if we find a pointer to a function */
1285 /* unconditionally change it to a ptr */
1287 for (p = sym->type; p; p = p->next)
1289 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1290 DCL_TYPE (p) = GPOINTER;
1291 if (IS_PTR (p) && IS_FUNC (p->next))
1292 DCL_TYPE (p) = CPOINTER;
1296 /*------------------------------------------------------------------*/
1297 /* checkDecl - does semantic validation of a declaration */
1298 /*------------------------------------------------------------------*/
1300 checkDecl (symbol * sym, int isProto)
1303 checkSClass (sym, isProto); /* check the storage class */
1304 changePointer (sym); /* change pointers if required */
1306 /* if this is an array without any dimension
1307 then update the dimension from the initial value */
1308 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1309 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1314 /*------------------------------------------------------------------*/
1315 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1316 /*------------------------------------------------------------------*/
1318 copyLinkChain (sym_link * p)
1320 sym_link *head, *curr, *loop;
1323 head = loop = (curr ? newLink () : (void *) NULL);
1326 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1327 loop->next = (curr->next ? newLink () : (void *) NULL);
1336 /*------------------------------------------------------------------*/
1337 /* cleanUpBlock - cleansup the symbol table specified for all the */
1338 /* symbols in the given block */
1339 /*------------------------------------------------------------------*/
1341 cleanUpBlock (bucket ** table, int block)
1346 /* go thru the entire table */
1347 for (i = 0; i < 256; i++)
1349 for (chain = table[i]; chain; chain = chain->next)
1351 if (chain->block >= block)
1353 deleteSym (table, chain->sym, chain->name);
1359 /*------------------------------------------------------------------*/
1360 /* cleanUpLevel - cleansup the symbol table specified for all the */
1361 /* symbols in the given level */
1362 /*------------------------------------------------------------------*/
1364 cleanUpLevel (bucket ** table, int level)
1369 /* go thru the entire table */
1370 for (i = 0; i < 256; i++)
1372 for (chain = table[i]; chain; chain = chain->next)
1374 if (chain->level >= level)
1376 deleteSym (table, chain->sym, chain->name);
1382 /*------------------------------------------------------------------*/
1383 /* computeType - computes the resultant type from two types */
1384 /*------------------------------------------------------------------*/
1386 computeType (sym_link * type1, sym_link * type2)
1390 sym_link *etype1 = getSpec (type1);
1391 sym_link *etype2 = getSpec (type2);
1393 /* if one of them is a float then result is a float */
1394 /* here we assume that the types passed are okay */
1395 /* and can be cast to one another */
1396 /* which ever is greater in size */
1397 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1398 rType = newFloatLink ();
1400 /* if only one of them is a bit variable
1401 then the other one prevails */
1402 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1403 rType = copyLinkChain (type2);
1404 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1405 rType = copyLinkChain (type1);
1407 /* if one of them is a pointer then that
1410 rType = copyLinkChain (type1);
1411 else if (IS_PTR (type2))
1412 rType = copyLinkChain (type2);
1413 else if (getSize (type1) > getSize (type2))
1414 rType = copyLinkChain (type1);
1416 rType = copyLinkChain (type2);
1418 reType = getSpec (rType);
1420 /* if either of them unsigned then make this unsigned */
1421 if ((SPEC_USIGN (etype1) || SPEC_USIGN (etype2)) && !IS_FLOAT (reType))
1422 SPEC_USIGN (reType) = 1;
1424 /* if result is a literal then make not so */
1425 if (IS_LITERAL (reType))
1426 SPEC_SCLS (reType) = S_REGISTER;
1431 /*------------------------------------------------------------------*/
1432 /* compareType - will do type check return 1 if match */
1433 /*------------------------------------------------------------------*/
1435 compareType (sym_link * dest, sym_link * src)
1446 /* if dest is a declarator then */
1451 if (DCL_TYPE (src) == DCL_TYPE (dest))
1452 return compareType (dest->next, src->next);
1453 if (IS_PTR (src) && IS_PTR (dest))
1455 if (IS_PTR (dest) && IS_ARRAY (src))
1457 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1458 return -1 * compareType (dest->next, src);
1461 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1467 /* if one is a specifier and the other is not */
1468 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1469 (IS_SPEC (dest) && !IS_SPEC (src)))
1472 /* if one of them is a void then ok */
1473 if (SPEC_NOUN (dest) == V_VOID &&
1474 SPEC_NOUN (src) != V_VOID)
1477 if (SPEC_NOUN (dest) != V_VOID &&
1478 SPEC_NOUN (src) == V_VOID)
1481 /* if they are both bitfields then if the lengths
1482 and starts don't match */
1483 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1484 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1485 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1488 /* it is a specifier */
1489 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1491 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1492 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1493 getSize (dest) == getSize (src))
1495 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1500 else if (IS_STRUCT (dest))
1502 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1507 if (SPEC_LONG (dest) != SPEC_LONG (src))
1510 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1516 /*------------------------------------------------------------------*/
1517 /* inCalleeSaveList - return 1 if found in callee save list */
1518 /*------------------------------------------------------------------*/
1520 inCalleeSaveList (char *s)
1524 for (i = 0; options.calleeSaves[i]; i++)
1525 if (strcmp (options.calleeSaves[i], s) == 0)
1531 /*-----------------------------------------------------------------*/
1532 /* aggregateArgToPointer: change an agggregate type function */
1533 /* argument to a pointer to that type. */
1534 /*-----------------------------------------------------------------*/
1536 aggregateArgToPointer (value * val)
1538 if (IS_AGGREGATE (val->type))
1540 /* if this is a structure */
1541 /* then we need to add a new link */
1542 if (IS_STRUCT (val->type))
1544 /* first lets add DECLARATOR type */
1545 sym_link *p = val->type;
1547 werror (W_STRUCT_AS_ARG, val->name);
1548 val->type = newLink ();
1549 val->type->next = p;
1552 /* change to a pointer depending on the */
1553 /* storage class specified */
1554 switch (SPEC_SCLS (val->etype))
1557 DCL_TYPE (val->type) = IPOINTER;
1560 DCL_TYPE (val->type) = PPOINTER;
1563 if (TARGET_IS_DS390)
1565 /* The AUTO and REGISTER classes should probably
1566 * also become generic pointers, but I haven't yet
1567 * devised a test case for that.
1569 DCL_TYPE (val->type) = GPOINTER;
1576 DCL_TYPE (val->type) = POINTER;
1579 DCL_TYPE (val->type) = CPOINTER;
1582 DCL_TYPE (val->type) = FPOINTER;
1585 DCL_TYPE (val->type) = EEPPOINTER;
1588 DCL_TYPE (val->type) = GPOINTER;
1591 /* is there is a symbol associated then */
1592 /* change the type of the symbol as well */
1595 val->sym->type = copyLinkChain (val->type);
1596 val->sym->etype = getSpec (val->sym->type);
1600 /*------------------------------------------------------------------*/
1601 /* checkFunction - does all kinds of check on a function */
1602 /*------------------------------------------------------------------*/
1604 checkFunction (symbol * sym)
1607 value *exargs, *acargs;
1611 if (getenv("DEBUG_SANITY")) {
1612 fprintf (stderr, "checkFunction: %s ", sym->name);
1615 /* make sure the type is complete and sane */
1616 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1618 /* if not type then some kind of error */
1622 /* if the function has no type then make it return int */
1623 if (!sym->type->next)
1624 sym->type->next = sym->etype = newIntLink ();
1626 /* function cannot return aggregate */
1627 if (IS_AGGREGATE (sym->type->next))
1629 werror (E_FUNC_AGGR, sym->name);
1633 /* function cannot return bit */
1634 if (IS_BITVAR (sym->type->next))
1636 werror (E_FUNC_BIT, sym->name);
1640 /* check if this function is defined as calleeSaves
1641 then mark it as such */
1642 sym->calleeSave = inCalleeSaveList (sym->name);
1644 /* if interrupt service routine */
1645 /* then it cannot have arguments */
1646 if (sym->args && IS_ISR (sym->etype) && !IS_VOID (sym->args->type))
1648 werror (E_INT_ARGS, sym->name);
1652 if (!(csym = findSym (SymbolTab, sym, sym->name)))
1653 return 1; /* not defined nothing more to check */
1655 /* check if body already present */
1656 if (csym && csym->fbody)
1658 werror (E_FUNC_BODY, sym->name);
1662 /* check the return value type */
1663 if (compareType (csym->type, sym->type) <= 0)
1665 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1666 werror (W_CONTINUE, "previous definition type ");
1667 printTypeChain (csym->type, stderr);
1668 fprintf (stderr, "\n");
1669 werror (W_CONTINUE, "current definition type ");
1670 printTypeChain (sym->type, stderr);
1671 fprintf (stderr, "\n");
1675 if (SPEC_INTRTN (csym->etype) != SPEC_INTRTN (sym->etype))
1677 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1680 if (SPEC_BANK (csym->etype) != SPEC_BANK (sym->etype))
1682 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1685 if (SPEC_NAKED (csym->etype) != SPEC_NAKED (sym->etype))
1687 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1690 /* compare expected agrs with actual args */
1691 exargs = csym->args;
1694 /* for all the expected args do */
1697 exargs = exargs->next, acargs = acargs->next, argCnt++)
1699 if (getenv("DEBUG_SANITY")) {
1700 fprintf (stderr, "checkFunction: %s ", exargs->name);
1702 /* make sure the type is complete and sane */
1703 checkTypeSanity(exargs->etype, exargs->name);
1705 /* If the actual argument is an array, any prototype
1706 * will have modified it to a pointer. Duplicate that
1709 if (IS_AGGREGATE (acargs->type))
1711 checkValue = copyValue (acargs);
1712 aggregateArgToPointer (checkValue);
1716 checkValue = acargs;
1719 if (compareType (exargs->type, checkValue->type) <= 0)
1721 werror (E_ARG_TYPE, argCnt);
1726 /* if one them ended we have a problem */
1727 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1728 (!exargs && acargs && !IS_VOID (acargs->type)))
1729 werror (E_ARG_COUNT);
1731 /* replace with this defition */
1732 sym->cdef = csym->cdef;
1733 deleteSym (SymbolTab, csym, csym->name);
1734 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1735 if (IS_EXTERN (csym->etype) && !
1736 IS_EXTERN (sym->etype))
1738 addSet (&publics, sym);
1743 /*-----------------------------------------------------------------*/
1744 /* processFuncArgs - does some processing with function args */
1745 /*-----------------------------------------------------------------*/
1747 processFuncArgs (symbol * func, int ignoreName)
1752 /* if this function has variable argument list */
1753 /* then make the function a reentrant one */
1755 SPEC_RENT (func->etype) = 1;
1757 /* check if this function is defined as calleeSaves
1758 then mark it as such */
1759 func->calleeSave = inCalleeSaveList (func->name);
1761 val = func->args; /* loop thru all the arguments */
1763 /* if it is void then remove parameters */
1764 if (val && IS_VOID (val->type))
1770 /* reset regparm for the port */
1771 (*port->reset_regparms) ();
1772 /* if any of the arguments is an aggregate */
1773 /* change it to pointer to the same type */
1776 /* mark it as a register parameter if
1777 the function does not have VA_ARG
1778 and as port dictates */
1779 if (!func->hasVargs &&
1780 (*port->reg_parm) (val->type))
1782 SPEC_REGPARM (val->etype) = 1;
1785 if (IS_AGGREGATE (val->type))
1787 aggregateArgToPointer (val);
1793 /* if this is an internal generated function call */
1795 /* ignore --stack-auto for this one, we don't know how it is compiled */
1796 /* simply trust on --int-long-reent or --float-reent */
1797 if (IS_RENT(func->etype)) {
1801 /* if this function is reentrant or */
1802 /* automatics r 2b stacked then nothing */
1803 if (IS_RENT (func->etype) || options.stackAuto)
1812 /* if a symbolname is not given */
1813 /* synthesize a variable name */
1817 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1818 val->sym = newSymbol (val->name, 1);
1819 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1820 val->sym->type = copyLinkChain (val->type);
1821 val->sym->etype = getSpec (val->sym->type);
1822 val->sym->_isparm = 1;
1823 strcpy (val->sym->rname, val->name);
1824 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1825 SPEC_STAT (func->etype);
1826 addSymChain (val->sym);
1829 else /* symbol name given create synth name */
1832 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1833 strcpy (val->sym->rname, val->name);
1834 val->sym->_isparm = 1;
1835 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1836 (options.model != MODEL_SMALL ? xdata : data);
1837 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1838 SPEC_STAT (func->etype);
1844 /*-----------------------------------------------------------------*/
1845 /* isSymbolEqual - compares two symbols return 1 if they match */
1846 /*-----------------------------------------------------------------*/
1848 isSymbolEqual (symbol * dest, symbol * src)
1850 /* if pointers match then equal */
1854 /* if one of them is null then don't match */
1858 /* if both of them have rname match on rname */
1859 if (dest->rname[0] && src->rname[0])
1860 return (!strcmp (dest->rname, src->rname));
1862 /* otherwise match on name */
1863 return (!strcmp (dest->name, src->name));
1866 void PT(sym_link *type)
1868 printTypeChain(type,0);
1870 /*-----------------------------------------------------------------*/
1871 /* printTypeChain - prints the type chain in human readable form */
1872 /*-----------------------------------------------------------------*/
1874 printTypeChain (sym_link * type, FILE * of)
1888 if (DCL_PTR_VOLATILE(type)) {
1889 fprintf (of, "volatile ");
1891 switch (DCL_TYPE (type))
1894 fprintf (of, "function ");
1897 fprintf (of, "_generic * ");
1898 if (DCL_PTR_CONST (type))
1899 fprintf (of, "const ");
1902 fprintf (of, "_code * ");
1903 if (DCL_PTR_CONST (type))
1904 fprintf (of, "const ");
1907 fprintf (of, "_far * ");
1908 if (DCL_PTR_CONST (type))
1909 fprintf (of, "const ");
1912 fprintf (of, "_eeprom * ");
1913 if (DCL_PTR_CONST (type))
1914 fprintf (of, "const ");
1918 fprintf (of, "_near * ");
1919 if (DCL_PTR_CONST (type))
1920 fprintf (of, "const ");
1923 fprintf (of, "_idata *");
1924 if (DCL_PTR_CONST (type))
1925 fprintf (of, "const ");
1928 fprintf (of, "_pdata *");
1929 if (DCL_PTR_CONST (type))
1930 fprintf (of, "const ");
1933 fprintf (of, " _unkown *");
1934 if (DCL_PTR_CONST (type))
1935 fprintf (of, "const ");
1938 fprintf (of, "array of ");
1944 if (SPEC_VOLATILE (type))
1945 fprintf (of, "volatile ");
1946 if (SPEC_USIGN (type))
1947 fprintf (of, "unsigned ");
1948 if (SPEC_CONST (type))
1949 fprintf (of, "const ");
1951 switch (SPEC_NOUN (type))
1955 fprintf (of, "long ");
1956 fprintf (of, "int ");
1960 fprintf (of, "char ");
1964 fprintf (of, "void ");
1968 fprintf (of, "float ");
1972 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
1976 fprintf (of, "sbit ");
1980 fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
1984 fprintf (of, "double ");
1988 fprintf (of, "unknown type ");
1998 /*-----------------------------------------------------------------*/
1999 /* cdbTypeInfo - print the type information for debugger */
2000 /*-----------------------------------------------------------------*/
2002 cdbTypeInfo (sym_link * type, FILE * of)
2004 fprintf (of, "{%d}", getSize (type));
2009 switch (DCL_TYPE (type))
2012 fprintf (of, "DF,");
2015 fprintf (of, "DG,");
2018 fprintf (of, "DC,");
2021 fprintf (of, "DX,");
2024 fprintf (of, "DD,");
2027 fprintf (of, "DI,");
2030 fprintf (of, "DP,");
2033 fprintf (of, "DA,");
2036 fprintf (of, "DA%d,", DCL_ELEM (type));
2044 switch (SPEC_NOUN (type))
2066 fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
2074 fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
2081 if (SPEC_USIGN (type))
2089 /*-----------------------------------------------------------------*/
2090 /* cdbSymbol - prints a symbol & its type information for debugger */
2091 /*-----------------------------------------------------------------*/
2093 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
2105 fprintf (of, "S:"); /* symbol record */
2106 /* if this is not a structure symbol then
2107 we need to figure out the scope information */
2113 if (IS_STATIC (sym->etype))
2114 fprintf (of, "F%s$", moduleName); /* scope is file */
2116 fprintf (of, "G$"); /* scope is global */
2119 /* symbol is local */
2120 fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
2123 fprintf (of, "S$"); /* scope is structure */
2125 /* print the name, & mangled name */
2126 fprintf (of, "%s$%d$%d(", sym->name,
2127 sym->level, sym->block);
2129 cdbTypeInfo (sym->type, of);
2132 /* print the address space */
2133 map = SPEC_OCLS (sym->etype);
2134 fprintf (of, "%c,%d,%d",
2135 (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
2137 /* if assigned to registers then output register names */
2138 /* if this is a function then print
2139 if is it an interrupt routine & interrupt number
2140 and the register bank it is using */
2142 fprintf (of, ",%d,%d,%d", SPEC_INTRTN (sym->etype),
2143 SPEC_INTN (sym->etype), SPEC_BANK (sym->etype));
2144 /* alternate location to find this symbol @ : eg registers
2151 /*-----------------------------------------------------------------*/
2152 /* cdbStruct - print a structure for debugger */
2153 /*-----------------------------------------------------------------*/
2155 cdbStruct (structdef * sdef, int block, FILE * of,
2156 int inStruct, char *tag)
2161 /* if block # then must have function scope */
2162 fprintf (of, "F%s$", moduleName);
2163 fprintf (of, "%s[", (tag ? tag : sdef->tag));
2164 for (sym = sdef->fields; sym; sym = sym->next)
2166 fprintf (of, "({%d}", sym->offset);
2167 cdbSymbol (sym, of, TRUE, FALSE);
2175 /*------------------------------------------------------------------*/
2176 /* cdbStructBlock - calls struct printing for a blcks */
2177 /*------------------------------------------------------------------*/
2179 cdbStructBlock (int block, FILE * of)
2182 bucket **table = StructTab;
2186 /* go thru the entire table */
2187 for (i = 0; i < 256; i++)
2189 for (chain = table[i]; chain; chain = chain->next)
2191 if (chain->block >= block)
2193 cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2199 /*-----------------------------------------------------------------*/
2200 /* powof2 - returns power of two for the number if number is pow 2 */
2201 /*-----------------------------------------------------------------*/
2203 powof2 (unsigned long num)
2216 if (n1s > 1 || nshifts == 0)
2232 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2233 symbol *__muldiv[3][3][2];
2234 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2235 sym_link *__multypes[3][2];
2236 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2237 symbol *__conv[2][3][2];
2238 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2239 symbol *__rlrr[2][3][2];
2241 sym_link *floatType;
2244 _mangleFunctionName(char *in)
2246 if (port->getMangledFunctionName)
2248 return port->getMangledFunctionName(in);
2256 /*-----------------------------------------------------------------*/
2257 /* initCSupport - create functions for C support routines */
2258 /*-----------------------------------------------------------------*/
2262 const char *smuldivmod[] =
2266 const char *sbwd[] =
2268 "char", "int", "long"
2274 const char *srlrr[] =
2279 int bwd, su, muldivmod, tofrom, rlrr;
2281 floatType = newFloatLink ();
2283 for (bwd = 0; bwd < 3; bwd++)
2300 __multypes[bwd][0] = l;
2301 __multypes[bwd][1] = copyLinkChain (l);
2302 SPEC_USIGN (__multypes[bwd][1]) = 1;
2305 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2306 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2307 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2308 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2309 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2310 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2311 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2312 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2313 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2314 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2316 for (tofrom = 0; tofrom < 2; tofrom++)
2318 for (bwd = 0; bwd < 3; bwd++)
2320 for (su = 0; su < 2; su++)
2324 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2325 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], floatType, 1, options.float_rent);
2329 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2330 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), floatType, __multypes[bwd][su], 1, options.float_rent);
2336 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2338 for (bwd = 0; bwd < 3; bwd++)
2340 for (su = 0; su < 2; su++)
2342 sprintf (buffer, "_%s%s%s",
2343 smuldivmod[muldivmod],
2346 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2347 SPEC_NONBANKED (__muldiv[muldivmod][bwd][su]->etype) = 1;
2352 for (rlrr = 0; rlrr < 2; rlrr++)
2354 for (bwd = 0; bwd < 3; bwd++)
2356 for (su = 0; su < 2; su++)
2358 sprintf (buffer, "_%s%s%s",
2362 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2363 SPEC_NONBANKED (__rlrr[rlrr][bwd][su]->etype) = 1;