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) ||
322 DCL_TYPE(ptr)!=UPOINTER)
325 /* change the pointer type depending on the
326 storage class of the type */
329 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
330 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
331 switch (SPEC_SCLS (type))
334 DCL_TYPE (ptr) = FPOINTER;
337 DCL_TYPE (ptr) = IPOINTER;
340 DCL_TYPE (ptr) = PPOINTER;
343 DCL_TYPE (ptr) = POINTER;
346 DCL_PTR_CONST (ptr) = port->mem.code_ro;
347 DCL_TYPE (ptr) = CPOINTER;
350 DCL_TYPE (ptr) = EEPPOINTER;
353 DCL_TYPE (ptr) = GPOINTER;
356 /* the storage class of type ends here */
359 SPEC_VOLATILE (type) = 0;
362 /* now change all the remaining unknown pointers
363 to generic pointers */
366 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
367 DCL_TYPE (ptr) = GPOINTER;
371 /* same for the type although it is highly unlikely that
372 type will have a pointer */
375 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
376 DCL_TYPE (type) = GPOINTER;
382 /*------------------------------------------------------------------*/
383 /* addDecl - adds a declarator @ the end of a chain */
384 /*------------------------------------------------------------------*/
386 addDecl (symbol * sym, int type, sym_link * p)
392 /* if we are passed a link then set head & tail */
401 head = tail = newLink ();
402 DCL_TYPE (head) = type;
405 /* if this is the first entry */
413 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
415 sym->etype = mergeSpec (sym->etype, head, sym->name);
419 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
422 while (t->next != sym->etype)
425 tail->next = sym->etype;
429 sym->etype->next = head;
435 /* if the type is a unknown pointer and has
436 a tspec then take the storage class const & volatile
437 attribute from the tspec & make it those of this
441 DCL_TYPE (p) == UPOINTER &&
444 if (!IS_SPEC (sym->etype))
446 sym->etype = sym->etype->next = newLink ();
447 sym->etype->class = SPECIFIER;
449 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
450 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
451 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
452 DCL_TSPEC (p) = NULL;
457 /*------------------------------------------------------------------
458 checkTypeSanity: prevent the user from doing e.g.:
460 ------------------------------------------------------------------*/
461 void checkTypeSanity(sym_link *etype, char *name) {
465 if (getenv("DEBUG_SANITY")) {
466 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
471 if (!IS_SPEC(etype)) {
472 if (getenv("DEBUG_SANITY")) {
473 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
478 noun=nounName(etype);
480 if (getenv("DEBUG_SANITY")) {
481 fprintf (stderr, "checking sanity for %s %x\n", name, (int)etype);
484 if ((SPEC_NOUN(etype)==V_CHAR ||
485 SPEC_NOUN(etype)==V_FLOAT ||
486 SPEC_NOUN(etype)==V_DOUBLE ||
487 SPEC_NOUN(etype)==V_VOID) &&
488 (etype->select.s._short || SPEC_LONG(etype))) {
489 // long or short for char float double or void
490 werror (E_LONG_OR_SHORT_INVALID, noun, name);
492 if ((SPEC_NOUN(etype)==V_FLOAT ||
493 SPEC_NOUN(etype)==V_DOUBLE ||
494 SPEC_NOUN(etype)==V_VOID) &&
495 (etype->select.s._signed || SPEC_USIGN(etype))) {
496 // signed or unsigned for float double or void
497 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
500 // special case for "short"
501 if (etype->select.s._short) {
502 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
503 etype->select.s._short = 0;
507 "const a;" or "data b;" or "signed s" or "long l"
509 if (!SPEC_NOUN(etype)) {
510 SPEC_NOUN(etype)=V_INT;
513 if (etype->select.s._signed && SPEC_USIGN(etype)) {
514 // signed AND unsigned
515 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
517 if (etype->select.s._short && SPEC_LONG(etype)) {
519 werror (E_LONG_AND_SHORT_INVALID, noun, name);
524 /*------------------------------------------------------------------*/
525 /* mergeSpec - merges two specifiers and returns the new one */
526 /*------------------------------------------------------------------*/
527 #define LAST_MINUTE_2_3_0_FIX
529 mergeSpec (sym_link * dest, sym_link * src, char *name)
532 #ifdef LAST_MINUTE_2_3_0_FIX
535 if (!IS_SPEC(dest)) {
536 // This should not happen
537 fprintf (stderr, "*** internal error: can't merge declarators\n");
540 // here we have a declarator as source, reverse them
544 while (!IS_SPEC(dest)) {
545 // and find the specifier
553 if (getenv("DEBUG_mergeSpec")) {
554 fprintf (stderr, "mergeSpec: \"%s\"\n", name);
557 if (SPEC_NOUN(src)) {
558 if (!SPEC_NOUN(dest)) {
559 SPEC_NOUN(dest)=SPEC_NOUN(src);
561 /* we shouldn't redeclare the type */
562 if (getenv("DEBUG_SANITY")) {
563 fprintf (stderr, "mergeSpec: ");
565 werror(E_TWO_OR_MORE_DATA_TYPES, name);
569 if (SPEC_SCLS(src)) {
570 /* if destination has no storage class */
571 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
572 SPEC_SCLS (dest) = SPEC_SCLS (src);
574 if (getenv("DEBUG_SANITY")) {
575 fprintf (stderr, "mergeSpec: ");
577 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
581 /* copy all the specifications */
583 // we really should do:
585 if (SPEC_what(src)) {
586 if (SPEC_what(dest)) {
587 werror(W_DUPLICATE_SPEC, "what");
589 SPEC_what(dst)|=SPEC_what(src);
592 // but there are more important thing right now
594 SPEC_LONG (dest) |= SPEC_LONG (src);
595 dest->select.s._short|=src->select.s._short;
596 SPEC_USIGN (dest) |= SPEC_USIGN (src);
597 dest->select.s._signed|=src->select.s._signed;
598 SPEC_STAT (dest) |= SPEC_STAT (src);
599 SPEC_EXTR (dest) |= SPEC_EXTR (src);
600 SPEC_ABSA (dest) |= SPEC_ABSA (src);
601 SPEC_RENT (dest) |= SPEC_RENT (src);
602 SPEC_INTN (dest) |= SPEC_INTN (src);
603 SPEC_BANK (dest) |= SPEC_BANK (src);
604 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
605 SPEC_CRTCL (dest) |= SPEC_CRTCL (src);
606 SPEC_ADDR (dest) |= SPEC_ADDR (src);
607 SPEC_OCLS (dest) = SPEC_OCLS (src);
608 SPEC_BLEN (dest) |= SPEC_BLEN (src);
609 SPEC_BSTR (dest) |= SPEC_BSTR (src);
610 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
611 SPEC_NONBANKED (dest) |= SPEC_NONBANKED (src);
612 SPEC_NAKED (dest) |= SPEC_NAKED (src);
614 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
615 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
617 #ifdef LAST_MINUTE_2_3_0_FIX
624 /*------------------------------------------------------------------*/
625 /* cloneSpec - copies the entire spec and returns a new spec */
626 /*------------------------------------------------------------------*/
628 cloneSpec (sym_link * src)
632 /* go thru chain till we find the specifier */
633 while (src && src->class != SPECIFIER)
637 memcpy (spec, src, sizeof (sym_link));
641 /*------------------------------------------------------------------*/
642 /* genSymName - generates and returns a name used for anonymous vars */
643 /*------------------------------------------------------------------*/
645 genSymName (int level)
647 static int gCount = 0;
648 static char gname[SDCC_NAME_MAX + 1];
650 sprintf (gname, "__%04d%04d", level, gCount++);
654 /*------------------------------------------------------------------*/
655 /* getSpec - returns the specifier part from a declaration chain */
656 /*------------------------------------------------------------------*/
658 getSpec (sym_link * p)
663 while (p && !(IS_SPEC (p)))
669 /*------------------------------------------------------------------*/
670 /* newCharLink() - creates an char type */
671 /*------------------------------------------------------------------*/
678 p->class = SPECIFIER;
679 SPEC_NOUN (p) = V_CHAR;
684 /*------------------------------------------------------------------*/
685 /* newFloatLink - a new Float type */
686 /*------------------------------------------------------------------*/
693 p->class = SPECIFIER;
694 SPEC_NOUN (p) = V_FLOAT;
699 /*------------------------------------------------------------------*/
700 /* newLongLink() - new long type */
701 /*------------------------------------------------------------------*/
708 p->class = SPECIFIER;
709 SPEC_NOUN (p) = V_INT;
715 /*------------------------------------------------------------------*/
716 /* newIntLink() - creates an int type */
717 /*------------------------------------------------------------------*/
724 p->class = SPECIFIER;
725 SPEC_NOUN (p) = V_INT;
730 /*------------------------------------------------------------------*/
731 /* getSize - returns size of a type chain in bits */
732 /*------------------------------------------------------------------*/
734 getSize (sym_link * p)
736 /* if nothing return 0 */
740 { /* if this is the specifier then */
741 switch (SPEC_NOUN (p))
742 { /* depending on the specifier type */
744 return (IS_LONG (p) ? LONGSIZE : INTSIZE);
752 return SPEC_STRUCT (p)->size;
758 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
764 /* this is a specifier */
765 switch (DCL_TYPE (p))
770 return DCL_ELEM (p) * getSize (p->next);
787 /*------------------------------------------------------------------*/
788 /* bitsForType - returns # of bits required to store this type */
789 /*------------------------------------------------------------------*/
791 bitsForType (sym_link * p)
793 /* if nothing return 0 */
798 { /* if this is the specifier then */
800 switch (SPEC_NOUN (p))
801 { /* depending on the specifier type */
803 return (IS_LONG (p) ? LONGSIZE * 8 : INTSIZE * 8);
805 return FLOATSIZE * 8;
811 return SPEC_STRUCT (p)->size * 8;
817 return SPEC_BLEN (p);
823 /* this is a specifier */
824 switch (DCL_TYPE (p))
829 return DCL_ELEM (p) * getSize (p->next) * 8;
833 return (PTRSIZE * 8);
837 return (FPTRSIZE * 8);
839 return (GPTRSIZE * 8);
846 /*------------------------------------------------------------------*/
847 /* copySymbolChain - copies a symbol chain */
848 /*------------------------------------------------------------------*/
850 copySymbolChain (symbol * src)
857 dest = copySymbol (src);
858 dest->next = copySymbolChain (src->next);
862 /*------------------------------------------------------------------*/
863 /* copySymbol - makes a copy of a symbol */
864 /*------------------------------------------------------------------*/
866 copySymbol (symbol * src)
873 dest = newSymbol (src->name, src->level);
874 memcpy (dest, src, sizeof (symbol));
875 dest->level = src->level;
876 dest->block = src->block;
877 dest->ival = copyIlist (src->ival);
878 dest->type = copyLinkChain (src->type);
879 dest->etype = getSpec (dest->type);
881 dest->args = copyValueChain (src->args);
882 dest->key = src->key;
883 dest->calleeSave = src->calleeSave;
884 dest->allocreq = src->allocreq;
888 /*------------------------------------------------------------------*/
889 /* reverseSyms - reverses the links for a symbol chain */
890 /*------------------------------------------------------------------*/
892 reverseSyms (symbol * sym)
894 symbol *prev, *curr, *next;
909 sym->next = (void *) NULL;
913 /*------------------------------------------------------------------*/
914 /* reverseLink - reverses the links for a type chain */
915 /*------------------------------------------------------------------*/
917 reverseLink (sym_link * type)
919 sym_link *prev, *curr, *next;
934 type->next = (void *) NULL;
938 /*------------------------------------------------------------------*/
939 /* addSymChain - adds a symbol chain to the symboltable */
940 /*------------------------------------------------------------------*/
942 addSymChain (symbol * symHead)
944 symbol *sym = symHead;
947 for (; sym != NULL; sym = sym->next)
950 checkTypeSanity(sym->etype, sym->name);
952 /* if already exists in the symbol table then check if
953 one of them is an extern definition if yes then
954 then check if the type match, if the types match then
955 delete the current entry and add the new entry */
956 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
957 csym->level == sym->level) {
959 /* one definition extern ? */
960 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
961 /* do types match ? */
962 if (compareType (csym->type, sym->type) != 1) {
964 werror (E_EXTERN_MISMATCH, csym->name);
967 /* delete current entry */
968 deleteSym (SymbolTab, csym, csym->name);
971 werror (E_DUPLICATE, sym->name);
977 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
982 /*------------------------------------------------------------------*/
983 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
984 /*------------------------------------------------------------------*/
986 funcInChain (sym_link * lnk)
997 /*------------------------------------------------------------------*/
998 /* structElemType - returns the type info of a sturct member */
999 /*------------------------------------------------------------------*/
1001 structElemType (sym_link * stype, value * id, value ** argsp)
1003 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1004 sym_link *type, *etype;
1005 sym_link *petype = getSpec (stype);
1010 /* look for the id */
1013 if (strcmp (fields->rname, id->name) == 0)
1017 *argsp = fields->args;
1019 type = copyLinkChain (fields->type);
1020 etype = getSpec (type);
1021 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1022 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1025 fields = fields->next;
1027 werror (E_NOT_MEMBER, id->name);
1032 /*------------------------------------------------------------------*/
1033 /* getStructElement - returns element of a tructure definition */
1034 /*------------------------------------------------------------------*/
1036 getStructElement (structdef * sdef, symbol * sym)
1040 for (field = sdef->fields; field; field = field->next)
1041 if (strcmp (field->name, sym->name) == 0)
1044 werror (E_NOT_MEMBER, sym->name);
1046 return sdef->fields;
1049 /*------------------------------------------------------------------*/
1050 /* compStructSize - computes the size of a structure */
1051 /*------------------------------------------------------------------*/
1053 compStructSize (int su, structdef * sdef)
1055 int sum = 0, usum = 0;
1059 /* for the identifiers */
1060 loop = sdef->fields;
1063 /* create the internal name for this variable */
1064 sprintf (loop->rname, "_%s", loop->name);
1065 loop->offset = (su == UNION ? sum = 0 : sum);
1066 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1068 /* if this is a bit field */
1071 /* change it to a unsigned bit */
1072 SPEC_NOUN (loop->etype) = V_BIT;
1073 SPEC_USIGN (loop->etype) = 1;
1074 /* check if this fit into the remaining */
1075 /* bits of this byte else align it to the */
1076 /* next byte boundary */
1077 if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
1078 SPEC_BSTR (loop->etype) = bitOffset;
1079 if ((bitOffset += (loop->bitVar % 8)) == 8)
1082 else /* does not fit */ {
1084 SPEC_BSTR (loop->etype) = bitOffset;
1085 sum += (loop->bitVar / 8);
1086 bitOffset += (loop->bitVar % 8);
1088 /* if this is the last field then pad */
1089 if (!loop->next && bitOffset && bitOffset != 8) {
1095 checkDecl (loop, 1);
1096 sum += getSize (loop->type);
1099 /* if function then do the arguments for it */
1100 if (funcInChain (loop->type)) {
1101 processFuncArgs (loop, 1);
1106 /* if this is not a bitfield but the */
1107 /* previous one was and did not take */
1108 /* the whole byte then pad the rest */
1109 if ((loop && !loop->bitVar) && bitOffset) {
1114 /* if union then size = sizeof larget field */
1116 usum = max (usum, sum);
1120 return (su == UNION ? usum : sum);
1123 /*------------------------------------------------------------------*/
1124 /* checkSClass - check the storage class specification */
1125 /*------------------------------------------------------------------*/
1127 checkSClass (symbol * sym, int isProto)
1129 if (getenv("DEBUG_SANITY")) {
1130 fprintf (stderr, "checkSClass: %s \n", sym->name);
1132 if (strcmp(sym->name, "_testsGlobal")==0) {
1136 /* type is literal can happen foe enums change
1138 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1139 SPEC_SCLS (sym->etype) = S_AUTO;
1141 /* if sfr or sbit then must also be */
1142 /* volatile the initial value will be xlated */
1143 /* to an absolute address */
1144 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1145 SPEC_SCLS (sym->etype) == S_SFR)
1147 SPEC_VOLATILE (sym->etype) = 1;
1148 /* if initial value given */
1151 SPEC_ABSA (sym->etype) = 1;
1152 SPEC_ADDR (sym->etype) =
1153 (int) list2int (sym->ival);
1158 /* if absolute address given then it mark it as
1160 if (IS_ABSOLUTE (sym->etype))
1161 SPEC_VOLATILE (sym->etype) = 1;
1163 /* global variables declared const put into code */
1164 if (sym->level == 0 &&
1165 SPEC_CONST (sym->etype)) {
1166 SPEC_SCLS (sym->etype) = S_CODE;
1169 /* global variable in code space is a constant */
1170 if (sym->level == 0 &&
1171 SPEC_SCLS (sym->etype) == S_CODE &&
1173 SPEC_CONST (sym->etype) = 1;
1176 /* if bit variable then no storage class can be */
1177 /* specified since bit is already a storage */
1178 if (IS_BITVAR (sym->etype) &&
1179 (SPEC_SCLS (sym->etype) != S_FIXED &&
1180 SPEC_SCLS (sym->etype) != S_SBIT &&
1181 SPEC_SCLS (sym->etype) != S_BIT)
1184 werror (E_BITVAR_STORAGE, sym->name);
1185 SPEC_SCLS (sym->etype) = S_FIXED;
1188 /* extern variables cannot be initialized */
1189 if (IS_EXTERN (sym->etype) && sym->ival)
1191 werror (E_EXTERN_INIT, sym->name);
1196 /* if this is an automatic symbol then */
1197 /* storage class will be ignored and */
1198 /* symbol will be allocated on stack/ */
1199 /* data depending on flag */
1201 (options.stackAuto || reentrant) &&
1202 (SPEC_SCLS (sym->etype) != S_AUTO &&
1203 SPEC_SCLS (sym->etype) != S_FIXED &&
1204 SPEC_SCLS (sym->etype) != S_REGISTER &&
1205 SPEC_SCLS (sym->etype) != S_STACK &&
1206 SPEC_SCLS (sym->etype) != S_XSTACK))
1208 werror (E_AUTO_ASSUMED, sym->name);
1209 SPEC_SCLS (sym->etype) = S_AUTO;
1212 /* if this is an atomatic symbol */
1213 if (sym->level && (options.stackAuto || reentrant)) {
1214 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1215 SPEC_SCLS (sym->etype) == S_FIXED ||
1216 SPEC_SCLS (sym->etype) == S_REGISTER ||
1217 SPEC_SCLS (sym->etype) == S_STACK ||
1218 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1219 SPEC_SCLS (sym->etype) = S_AUTO;
1221 /* storage class may only be specified for statics */
1222 if (!IS_STATIC(sym->etype)) {
1223 werror (E_AUTO_ASSUMED, sym->name);
1229 /* automatic symbols cannot be given */
1230 /* an absolute address ignore it */
1232 SPEC_ABSA (sym->etype) &&
1233 (options.stackAuto || reentrant))
1235 werror (E_AUTO_ABSA, sym->name);
1236 SPEC_ABSA (sym->etype) = 0;
1239 /* arrays & pointers cannot be defined for bits */
1240 /* SBITS or SFRs or BIT */
1241 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1242 (SPEC_NOUN (sym->etype) == V_BIT ||
1243 SPEC_NOUN (sym->etype) == V_SBIT ||
1244 SPEC_SCLS (sym->etype) == S_SFR))
1245 werror (E_BIT_ARRAY, sym->name);
1247 /* if this is a bit|sbit then set length & start */
1248 if (SPEC_NOUN (sym->etype) == V_BIT ||
1249 SPEC_NOUN (sym->etype) == V_SBIT)
1251 SPEC_BLEN (sym->etype) = 1;
1252 SPEC_BSTR (sym->etype) = 0;
1256 /* variables declared in CODE space must have */
1257 /* initializers if not an extern */
1258 if (SPEC_SCLS (sym->etype) == S_CODE &&
1259 sym->ival == NULL &&
1261 port->mem.code_ro &&
1262 !IS_EXTERN (sym->etype) &&
1263 !funcInChain (sym->type))
1264 werror (E_CODE_NO_INIT, sym->name);
1267 /* if parameter or local variable then change */
1268 /* the storage class to reflect where the var will go */
1269 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1270 !IS_STATIC(sym->etype))
1272 if (options.stackAuto || (currFunc && IS_RENT (currFunc->etype)))
1274 SPEC_SCLS (sym->etype) = (options.useXstack ?
1275 S_XSTACK : S_STACK);
1279 /* hack-o-matic! I see no reason why the useXstack option should ever
1280 * control this allcoation, but the code was originally that way, and
1281 * changing it for non-390 ports breaks the compiler badly.
1283 bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1284 SPEC_SCLS (sym->etype) = (useXdata ?
1290 /*------------------------------------------------------------------*/
1291 /* changePointer - change pointer to functions */
1292 /*------------------------------------------------------------------*/
1294 changePointer (symbol * sym)
1298 /* go thru the chain of declarations */
1299 /* if we find a pointer to a function */
1300 /* unconditionally change it to a ptr */
1302 for (p = sym->type; p; p = p->next)
1304 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1305 DCL_TYPE (p) = GPOINTER;
1306 if (IS_PTR (p) && IS_FUNC (p->next))
1307 DCL_TYPE (p) = CPOINTER;
1311 /*------------------------------------------------------------------*/
1312 /* checkDecl - does semantic validation of a declaration */
1313 /*------------------------------------------------------------------*/
1315 checkDecl (symbol * sym, int isProto)
1318 checkSClass (sym, isProto); /* check the storage class */
1319 changePointer (sym); /* change pointers if required */
1321 /* if this is an array without any dimension
1322 then update the dimension from the initial value */
1323 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1324 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1329 /*------------------------------------------------------------------*/
1330 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1331 /*------------------------------------------------------------------*/
1333 copyLinkChain (sym_link * p)
1335 sym_link *head, *curr, *loop;
1338 head = loop = (curr ? newLink () : (void *) NULL);
1341 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1342 loop->next = (curr->next ? newLink () : (void *) NULL);
1351 /*------------------------------------------------------------------*/
1352 /* cleanUpBlock - cleansup the symbol table specified for all the */
1353 /* symbols in the given block */
1354 /*------------------------------------------------------------------*/
1356 cleanUpBlock (bucket ** table, int block)
1361 /* go thru the entire table */
1362 for (i = 0; i < 256; i++)
1364 for (chain = table[i]; chain; chain = chain->next)
1366 if (chain->block >= block)
1368 deleteSym (table, chain->sym, chain->name);
1374 /*------------------------------------------------------------------*/
1375 /* cleanUpLevel - cleansup the symbol table specified for all the */
1376 /* symbols in the given level */
1377 /*------------------------------------------------------------------*/
1379 cleanUpLevel (bucket ** table, int level)
1384 /* go thru the entire table */
1385 for (i = 0; i < 256; i++)
1387 for (chain = table[i]; chain; chain = chain->next)
1389 if (chain->level >= level)
1391 deleteSym (table, chain->sym, chain->name);
1397 /*------------------------------------------------------------------*/
1398 /* computeType - computes the resultant type from two types */
1399 /*------------------------------------------------------------------*/
1401 computeType (sym_link * type1, sym_link * type2)
1405 sym_link *etype1 = getSpec (type1);
1406 sym_link *etype2 = getSpec (type2);
1408 /* if one of them is a float then result is a float */
1409 /* here we assume that the types passed are okay */
1410 /* and can be cast to one another */
1411 /* which ever is greater in size */
1412 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1413 rType = newFloatLink ();
1415 /* if only one of them is a bit variable
1416 then the other one prevails */
1417 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1418 rType = copyLinkChain (type2);
1419 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1420 rType = copyLinkChain (type1);
1422 /* if one of them is a pointer then that
1425 rType = copyLinkChain (type1);
1426 else if (IS_PTR (type2))
1427 rType = copyLinkChain (type2);
1428 else if (getSize (type1) > getSize (type2))
1429 rType = copyLinkChain (type1);
1431 rType = copyLinkChain (type2);
1433 reType = getSpec (rType);
1435 /* if either of them unsigned then make this unsigned */
1436 if ((SPEC_USIGN (etype1) || SPEC_USIGN (etype2)) && !IS_FLOAT (reType))
1437 SPEC_USIGN (reType) = 1;
1439 /* if result is a literal then make not so */
1440 if (IS_LITERAL (reType))
1441 SPEC_SCLS (reType) = S_REGISTER;
1446 /*------------------------------------------------------------------*/
1447 /* compareType - will do type check return 1 if match */
1448 /*------------------------------------------------------------------*/
1450 compareType (sym_link * dest, sym_link * src)
1461 /* if dest is a declarator then */
1466 if (DCL_TYPE (src) == DCL_TYPE (dest))
1467 return compareType (dest->next, src->next);
1468 else if (IS_PTR (src) && IS_PTR (dest))
1470 else if (IS_PTR (dest) && IS_ARRAY (src))
1472 else if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1473 return -1 * compareType (dest->next, src);
1477 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1483 /* if one is a specifier and the other is not */
1484 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1485 (IS_SPEC (dest) && !IS_SPEC (src)))
1488 /* if one of them is a void then ok */
1489 if (SPEC_NOUN (dest) == V_VOID &&
1490 SPEC_NOUN (src) != V_VOID)
1493 if (SPEC_NOUN (dest) != V_VOID &&
1494 SPEC_NOUN (src) == V_VOID)
1497 /* if they are both bitfields then if the lengths
1498 and starts don't match */
1499 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1500 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1501 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1504 /* it is a specifier */
1505 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1507 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1508 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1509 getSize (dest) == getSize (src))
1511 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1516 else if (IS_STRUCT (dest))
1518 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1523 if (SPEC_LONG (dest) != SPEC_LONG (src))
1526 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1532 /*------------------------------------------------------------------*/
1533 /* inCalleeSaveList - return 1 if found in callee save list */
1534 /*------------------------------------------------------------------*/
1536 inCalleeSaveList (char *s)
1540 for (i = 0; options.calleeSaves[i]; i++)
1541 if (strcmp (options.calleeSaves[i], s) == 0)
1547 /*-----------------------------------------------------------------*/
1548 /* aggregateArgToPointer: change an agggregate type function */
1549 /* argument to a pointer to that type. */
1550 /*-----------------------------------------------------------------*/
1552 aggregateArgToPointer (value * val)
1554 if (IS_AGGREGATE (val->type))
1556 /* if this is a structure */
1557 /* then we need to add a new link */
1558 if (IS_STRUCT (val->type))
1560 /* first lets add DECLARATOR type */
1561 sym_link *p = val->type;
1563 werror (W_STRUCT_AS_ARG, val->name);
1564 val->type = newLink ();
1565 val->type->next = p;
1568 /* change to a pointer depending on the */
1569 /* storage class specified */
1570 switch (SPEC_SCLS (val->etype))
1573 DCL_TYPE (val->type) = IPOINTER;
1576 DCL_TYPE (val->type) = PPOINTER;
1579 if (TARGET_IS_DS390)
1581 /* The AUTO and REGISTER classes should probably
1582 * also become generic pointers, but I haven't yet
1583 * devised a test case for that.
1585 DCL_TYPE (val->type) = GPOINTER;
1592 DCL_TYPE (val->type) = POINTER;
1595 DCL_TYPE (val->type) = CPOINTER;
1598 DCL_TYPE (val->type) = FPOINTER;
1601 DCL_TYPE (val->type) = EEPPOINTER;
1604 DCL_TYPE (val->type) = GPOINTER;
1607 /* is there is a symbol associated then */
1608 /* change the type of the symbol as well */
1611 val->sym->type = copyLinkChain (val->type);
1612 val->sym->etype = getSpec (val->sym->type);
1616 /*------------------------------------------------------------------*/
1617 /* checkFunction - does all kinds of check on a function */
1618 /*------------------------------------------------------------------*/
1620 checkFunction (symbol * sym)
1623 value *exargs, *acargs;
1627 if (getenv("DEBUG_SANITY")) {
1628 fprintf (stderr, "checkFunction: %s ", sym->name);
1631 /* make sure the type is complete and sane */
1632 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1634 /* if not type then some kind of error */
1638 /* if the function has no type then make it return int */
1639 if (!sym->type->next)
1640 sym->type->next = sym->etype = newIntLink ();
1642 /* function cannot return aggregate */
1643 if (IS_AGGREGATE (sym->type->next))
1645 werror (E_FUNC_AGGR, sym->name);
1649 /* function cannot return bit */
1650 if (IS_BITVAR (sym->type->next))
1652 werror (E_FUNC_BIT, sym->name);
1656 /* check if this function is defined as calleeSaves
1657 then mark it as such */
1658 sym->calleeSave = inCalleeSaveList (sym->name);
1660 /* if interrupt service routine */
1661 /* then it cannot have arguments */
1662 if (sym->args && IS_ISR (sym->etype) && !IS_VOID (sym->args->type))
1664 werror (E_INT_ARGS, sym->name);
1668 if (!(csym = findSym (SymbolTab, sym, sym->name)))
1669 return 1; /* not defined nothing more to check */
1671 /* check if body already present */
1672 if (csym && csym->fbody)
1674 werror (E_FUNC_BODY, sym->name);
1678 /* check the return value type */
1679 if (compareType (csym->type, sym->type) <= 0)
1681 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1682 werror (E_CONTINUE, "previous definition type ");
1683 printTypeChain (csym->type, stderr);
1684 fprintf (stderr, "\n");
1685 werror (E_CONTINUE, "current definition type ");
1686 printTypeChain (sym->type, stderr);
1687 fprintf (stderr, "\n");
1691 if (SPEC_INTRTN (csym->etype) != SPEC_INTRTN (sym->etype))
1693 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1696 if (SPEC_BANK (csym->etype) != SPEC_BANK (sym->etype))
1698 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1701 if (SPEC_NAKED (csym->etype) != SPEC_NAKED (sym->etype))
1703 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1706 /* compare expected agrs with actual args */
1707 exargs = csym->args;
1710 /* for all the expected args do */
1713 exargs = exargs->next, acargs = acargs->next, argCnt++)
1715 if (getenv("DEBUG_SANITY")) {
1716 fprintf (stderr, "checkFunction: %s ", exargs->name);
1718 /* make sure the type is complete and sane */
1719 checkTypeSanity(exargs->etype, exargs->name);
1721 /* If the actual argument is an array, any prototype
1722 * will have modified it to a pointer. Duplicate that
1725 if (IS_AGGREGATE (acargs->type))
1727 checkValue = copyValue (acargs);
1728 aggregateArgToPointer (checkValue);
1732 checkValue = acargs;
1735 if (compareType (exargs->type, checkValue->type) <= 0)
1737 werror (E_ARG_TYPE, argCnt);
1742 /* if one them ended we have a problem */
1743 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1744 (!exargs && acargs && !IS_VOID (acargs->type)))
1745 werror (E_ARG_COUNT);
1747 /* replace with this defition */
1748 sym->cdef = csym->cdef;
1749 deleteSym (SymbolTab, csym, csym->name);
1750 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1751 if (IS_EXTERN (csym->etype) && !
1752 IS_EXTERN (sym->etype))
1754 addSet (&publics, sym);
1759 /*-----------------------------------------------------------------*/
1760 /* processFuncArgs - does some processing with function args */
1761 /*-----------------------------------------------------------------*/
1763 processFuncArgs (symbol * func, int ignoreName)
1768 /* if this function has variable argument list */
1769 /* then make the function a reentrant one */
1771 SPEC_RENT (func->etype) = 1;
1773 /* check if this function is defined as calleeSaves
1774 then mark it as such */
1775 func->calleeSave = inCalleeSaveList (func->name);
1777 val = func->args; /* loop thru all the arguments */
1779 /* if it is void then remove parameters */
1780 if (val && IS_VOID (val->type))
1786 /* reset regparm for the port */
1787 (*port->reset_regparms) ();
1788 /* if any of the arguments is an aggregate */
1789 /* change it to pointer to the same type */
1792 /* mark it as a register parameter if
1793 the function does not have VA_ARG
1794 and as port dictates */
1795 if (!func->hasVargs &&
1796 (*port->reg_parm) (val->type))
1798 SPEC_REGPARM (val->etype) = 1;
1801 if (IS_AGGREGATE (val->type))
1803 aggregateArgToPointer (val);
1809 /* if this is an internal generated function call */
1811 /* ignore --stack-auto for this one, we don't know how it is compiled */
1812 /* simply trust on --int-long-reent or --float-reent */
1813 if (IS_RENT(func->etype)) {
1817 /* if this function is reentrant or */
1818 /* automatics r 2b stacked then nothing */
1819 if (IS_RENT (func->etype) || options.stackAuto)
1828 /* if a symbolname is not given */
1829 /* synthesize a variable name */
1833 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1834 val->sym = newSymbol (val->name, 1);
1835 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1836 val->sym->type = copyLinkChain (val->type);
1837 val->sym->etype = getSpec (val->sym->type);
1838 val->sym->_isparm = 1;
1839 strcpy (val->sym->rname, val->name);
1840 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1841 SPEC_STAT (func->etype);
1842 addSymChain (val->sym);
1845 else /* symbol name given create synth name */
1848 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1849 strcpy (val->sym->rname, val->name);
1850 val->sym->_isparm = 1;
1851 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1852 (options.model != MODEL_SMALL ? xdata : data);
1853 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1854 SPEC_STAT (func->etype);
1860 /*-----------------------------------------------------------------*/
1861 /* isSymbolEqual - compares two symbols return 1 if they match */
1862 /*-----------------------------------------------------------------*/
1864 isSymbolEqual (symbol * dest, symbol * src)
1866 /* if pointers match then equal */
1870 /* if one of them is null then don't match */
1874 /* if both of them have rname match on rname */
1875 if (dest->rname[0] && src->rname[0])
1876 return (!strcmp (dest->rname, src->rname));
1878 /* otherwise match on name */
1879 return (!strcmp (dest->name, src->name));
1882 void PT(sym_link *type)
1884 printTypeChain(type,0);
1886 /*-----------------------------------------------------------------*/
1887 /* printTypeChain - prints the type chain in human readable form */
1888 /*-----------------------------------------------------------------*/
1890 printTypeChain (sym_link * type, FILE * of)
1904 if (DCL_PTR_VOLATILE(type)) {
1905 fprintf (of, "volatile ");
1907 switch (DCL_TYPE (type))
1910 fprintf (of, "function ");
1913 fprintf (of, "_generic * ");
1914 if (DCL_PTR_CONST (type))
1915 fprintf (of, "const ");
1918 fprintf (of, "_code * ");
1919 if (DCL_PTR_CONST (type))
1920 fprintf (of, "const ");
1923 fprintf (of, "_far * ");
1924 if (DCL_PTR_CONST (type))
1925 fprintf (of, "const ");
1928 fprintf (of, "_eeprom * ");
1929 if (DCL_PTR_CONST (type))
1930 fprintf (of, "const ");
1934 fprintf (of, "_near * ");
1935 if (DCL_PTR_CONST (type))
1936 fprintf (of, "const ");
1939 fprintf (of, "_idata *");
1940 if (DCL_PTR_CONST (type))
1941 fprintf (of, "const ");
1944 fprintf (of, "_pdata *");
1945 if (DCL_PTR_CONST (type))
1946 fprintf (of, "const ");
1949 fprintf (of, " _unkown *");
1950 if (DCL_PTR_CONST (type))
1951 fprintf (of, "const ");
1954 fprintf (of, "array of ");
1960 if (SPEC_VOLATILE (type))
1961 fprintf (of, "volatile ");
1962 if (SPEC_USIGN (type))
1963 fprintf (of, "unsigned ");
1964 if (SPEC_CONST (type))
1965 fprintf (of, "const ");
1967 switch (SPEC_NOUN (type))
1971 fprintf (of, "long ");
1972 fprintf (of, "int ");
1976 fprintf (of, "char ");
1980 fprintf (of, "void ");
1984 fprintf (of, "float ");
1988 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
1992 fprintf (of, "sbit ");
1996 fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2000 fprintf (of, "double ");
2004 fprintf (of, "unknown type ");
2014 /*-----------------------------------------------------------------*/
2015 /* cdbTypeInfo - print the type information for debugger */
2016 /*-----------------------------------------------------------------*/
2018 cdbTypeInfo (sym_link * type, FILE * of)
2020 fprintf (of, "{%d}", getSize (type));
2025 switch (DCL_TYPE (type))
2028 fprintf (of, "DF,");
2031 fprintf (of, "DG,");
2034 fprintf (of, "DC,");
2037 fprintf (of, "DX,");
2040 fprintf (of, "DD,");
2043 fprintf (of, "DI,");
2046 fprintf (of, "DP,");
2049 fprintf (of, "DA,");
2052 fprintf (of, "DA%d,", DCL_ELEM (type));
2060 switch (SPEC_NOUN (type))
2082 fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
2090 fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
2097 if (SPEC_USIGN (type))
2105 /*-----------------------------------------------------------------*/
2106 /* cdbSymbol - prints a symbol & its type information for debugger */
2107 /*-----------------------------------------------------------------*/
2109 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
2121 fprintf (of, "S:"); /* symbol record */
2122 /* if this is not a structure symbol then
2123 we need to figure out the scope information */
2129 if (IS_STATIC (sym->etype))
2130 fprintf (of, "F%s$", moduleName); /* scope is file */
2132 fprintf (of, "G$"); /* scope is global */
2135 /* symbol is local */
2136 fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
2139 fprintf (of, "S$"); /* scope is structure */
2141 /* print the name, & mangled name */
2142 fprintf (of, "%s$%d$%d(", sym->name,
2143 sym->level, sym->block);
2145 cdbTypeInfo (sym->type, of);
2148 /* print the address space */
2149 map = SPEC_OCLS (sym->etype);
2150 fprintf (of, "%c,%d,%d",
2151 (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
2153 /* if assigned to registers then output register names */
2154 /* if this is a function then print
2155 if is it an interrupt routine & interrupt number
2156 and the register bank it is using */
2158 fprintf (of, ",%d,%d,%d", SPEC_INTRTN (sym->etype),
2159 SPEC_INTN (sym->etype), SPEC_BANK (sym->etype));
2160 /* alternate location to find this symbol @ : eg registers
2167 /*-----------------------------------------------------------------*/
2168 /* cdbStruct - print a structure for debugger */
2169 /*-----------------------------------------------------------------*/
2171 cdbStruct (structdef * sdef, int block, FILE * of,
2172 int inStruct, char *tag)
2177 /* if block # then must have function scope */
2178 fprintf (of, "F%s$", moduleName);
2179 fprintf (of, "%s[", (tag ? tag : sdef->tag));
2180 for (sym = sdef->fields; sym; sym = sym->next)
2182 fprintf (of, "({%d}", sym->offset);
2183 cdbSymbol (sym, of, TRUE, FALSE);
2191 /*------------------------------------------------------------------*/
2192 /* cdbStructBlock - calls struct printing for a blcks */
2193 /*------------------------------------------------------------------*/
2195 cdbStructBlock (int block, FILE * of)
2198 bucket **table = StructTab;
2202 /* go thru the entire table */
2203 for (i = 0; i < 256; i++)
2205 for (chain = table[i]; chain; chain = chain->next)
2207 if (chain->block >= block)
2209 cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2215 /*-----------------------------------------------------------------*/
2216 /* powof2 - returns power of two for the number if number is pow 2 */
2217 /*-----------------------------------------------------------------*/
2219 powof2 (unsigned long num)
2232 if (n1s > 1 || nshifts == 0)
2248 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2249 symbol *__muldiv[3][3][2];
2250 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2251 sym_link *__multypes[3][2];
2252 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2253 symbol *__conv[2][3][2];
2254 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2255 symbol *__rlrr[2][3][2];
2257 sym_link *floatType;
2260 _mangleFunctionName(char *in)
2262 if (port->getMangledFunctionName)
2264 return port->getMangledFunctionName(in);
2272 /*-----------------------------------------------------------------*/
2273 /* initCSupport - create functions for C support routines */
2274 /*-----------------------------------------------------------------*/
2278 const char *smuldivmod[] =
2282 const char *sbwd[] =
2284 "char", "int", "long"
2290 const char *srlrr[] =
2295 int bwd, su, muldivmod, tofrom, rlrr;
2297 floatType = newFloatLink ();
2299 for (bwd = 0; bwd < 3; bwd++)
2316 __multypes[bwd][0] = l;
2317 __multypes[bwd][1] = copyLinkChain (l);
2318 SPEC_USIGN (__multypes[bwd][1]) = 1;
2321 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2322 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2323 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2324 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2325 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2326 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2327 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2328 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2329 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2330 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2332 for (tofrom = 0; tofrom < 2; tofrom++)
2334 for (bwd = 0; bwd < 3; bwd++)
2336 for (su = 0; su < 2; su++)
2340 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2341 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], floatType, 1, options.float_rent);
2345 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2346 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), floatType, __multypes[bwd][su], 1, options.float_rent);
2352 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2354 for (bwd = 0; bwd < 3; bwd++)
2356 for (su = 0; su < 2; su++)
2358 sprintf (buffer, "_%s%s%s",
2359 smuldivmod[muldivmod],
2362 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2363 SPEC_NONBANKED (__muldiv[muldivmod][bwd][su]->etype) = 1;
2368 for (rlrr = 0; rlrr < 2; rlrr++)
2370 for (bwd = 0; bwd < 3; bwd++)
2372 for (su = 0; su < 2; su++)
2374 sprintf (buffer, "_%s%s%s",
2378 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2379 SPEC_NONBANKED (__rlrr[rlrr][bwd][su]->etype) = 1;