1 /*-------------------------------------------------------------------------
2 SDCCsymt.c - Code file for Symbols table related structures and MACRO's.
3 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22 -------------------------------------------------------------------------*/
27 value *aggregateToPointer (value *val);
29 void printFromToType(sym_link *from, sym_link *to) {
30 fprintf (stderr, "from type '");
31 printTypeChain (from, stderr);
32 fprintf (stderr, "'\nto type '");
33 printTypeChain (to, stderr);
34 fprintf (stderr, "'\n");
38 char *nounName(sym_link *sl) {
39 switch (SPEC_NOUN(sl))
42 if (SPEC_LONG(sl)) return "long";
43 if (sl->select.s._short) return "short";
46 case V_FLOAT: return "float";
47 case V_CHAR: return "char";
48 case V_VOID: return "void";
49 case V_STRUCT: return "struct";
50 case V_LABEL: return "label";
51 case V_BIT: return "bit";
52 case V_SBIT: return "sbit";
53 case V_DOUBLE: return "double";
58 bucket *SymbolTab[256]; /* the symbol table */
59 bucket *StructTab[256]; /* the structure table */
60 bucket *TypedefTab[256]; /* the typedef table */
61 bucket *LabelTab[256]; /* the Label table */
62 bucket *enumTab[256]; /* enumerated table */
64 /*------------------------------------------------------------------*/
65 /* initSymt () - initialises symbol table related stuff */
66 /*------------------------------------------------------------------*/
72 for (i = 0; i < 256; i++)
73 SymbolTab[i] = StructTab[i] = (void *) NULL;
77 /*-----------------------------------------------------------------*/
78 /* newBucket - allocates & returns a new bucket */
79 /*-----------------------------------------------------------------*/
85 bp = Safe_alloc ( sizeof (bucket));
90 /*-----------------------------------------------------------------*/
91 /* hashKey - computes the hashkey given a symbol name */
92 /*-----------------------------------------------------------------*/
94 hashKey (const char *s)
96 unsigned long key = 0;
103 /*-----------------------------------------------------------------*/
104 /* addSym - adds a symbol to the hash Table */
105 /*-----------------------------------------------------------------*/
107 addSym (bucket ** stab,
114 int i; /* index into the hash Table */
115 bucket *bp; /* temp bucket * */
118 if (getenv("DEBUG_SANITY")) {
119 fprintf (stderr, "addSym: %s ", sname);
121 /* make sure the type is complete and sane */
122 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
125 /* prevent overflow of the (r)name buffers */
126 if (strlen(sname)>SDCC_SYMNAME_MAX) {
127 werror (W_SYMBOL_NAME_TOO_LONG, SDCC_SYMNAME_MAX);
128 sname[SDCC_SYMNAME_MAX]='\0';
131 /* the symbols are always added at the head of the list */
133 /* get a free entry */
134 bp = Safe_alloc ( sizeof (bucket));
136 bp->sym = sym; /* update the symbol pointer */
137 bp->level = level; /* update the nest level */
139 strcpy (bp->name, sname); /* copy the name into place */
141 /* if this is the first entry */
144 bp->prev = bp->next = (void *) NULL; /* point to nothing */
147 /* not first entry then add @ head of list */
157 /*-----------------------------------------------------------------*/
158 /* deleteSym - deletes a symbol from the hash Table entry */
159 /*-----------------------------------------------------------------*/
161 deleteSym (bucket ** stab, void *sym, char *sname)
169 /* find the symbol */
172 if (bp->sym == sym) /* found it then break out */
173 break; /* of the loop */
177 if (!bp) /* did not find it */
179 /* if this is the first one in the chain */
183 if (stab[i]) /* if chain ! empty */
184 stab[i]->prev = (void *) NULL;
186 /* middle || end of chain */
189 if (bp->next) /* if not end of chain */
190 bp->next->prev = bp->prev;
192 bp->prev->next = bp->next;
197 /*-----------------------------------------------------------------*/
198 /* findSym - finds a symbol in a table */
199 /*-----------------------------------------------------------------*/
201 findSym (bucket ** stab, void *sym, const char *sname)
205 bp = stab[hashKey (sname)];
208 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
213 return (bp ? bp->sym : (void *) NULL);
216 /*-----------------------------------------------------------------*/
217 /* findSymWithLevel - finds a symbol with a name & level */
218 /*-----------------------------------------------------------------*/
220 findSymWithLevel (bucket ** stab, symbol * sym)
224 bp = stab[hashKey (sym->name)];
227 ** do the search from the head of the list since the
228 ** elements are added at the head it is ensured that
229 ** we will find the deeper definitions before we find
230 ** the global ones. we need to check for symbols with
231 ** level <= to the level given, if levels match then block
232 ** numbers need to match as well
236 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
238 /* if this is parameter then nothing else need to be checked */
239 if (((symbol *) (bp->sym))->_isparm)
241 /* if levels match then block numbers should also match */
242 if (bp->level && bp->level == sym->level && bp->block == sym->block)
244 /* if levels don't match then we are okay */
245 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
247 /* if this is a global variable then we are ok too */
255 return (void *) NULL;
258 /*-----------------------------------------------------------------*/
259 /* findSymWithBlock - finds a symbol with name in with a block */
260 /*-----------------------------------------------------------------*/
262 findSymWithBlock (bucket ** stab, symbol * sym, int block)
266 bp = stab[hashKey (sym->name)];
269 if (strcmp (bp->name, sym->name) == 0 &&
275 return (bp ? bp->sym : (void *) NULL);
278 /*------------------------------------------------------------------*/
279 /* newSymbol () - returns a new pointer to a symbol */
280 /*------------------------------------------------------------------*/
282 newSymbol (char *name, int scope)
286 sym = Safe_alloc ( sizeof (symbol));
288 strcpy (sym->name, name); /* copy the name */
289 sym->level = scope; /* set the level */
290 sym->block = currBlockno;
291 sym->lineDef = yylineno; /* set the line number */
295 /*------------------------------------------------------------------*/
296 /* newLink - creates a new link (declarator,specifier) */
297 /*------------------------------------------------------------------*/
303 p = Safe_alloc ( sizeof (sym_link));
308 /*------------------------------------------------------------------*/
309 /* newStruct - creats a new structdef from the free list */
310 /*------------------------------------------------------------------*/
312 newStruct (char *tag)
316 s = Safe_alloc ( sizeof (structdef));
318 strcpy (s->tag, tag); /* copy the tag */
322 /*------------------------------------------------------------------*/
323 /* pointerTypes - do the computation for the pointer types */
324 /*------------------------------------------------------------------*/
326 pointerTypes (sym_link * ptr, sym_link * type)
331 /* find the first pointer type */
332 while (ptr && !IS_PTR (ptr))
335 /* could not find it */
336 if (!ptr || IS_SPEC (ptr))
339 if (IS_PTR(ptr) && DCL_TYPE(ptr)!=UPOINTER) {
340 pointerTypes (ptr->next, type);
344 /* change the pointer type depending on the
345 storage class of the type */
348 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
349 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
350 switch (SPEC_SCLS (type))
353 DCL_TYPE (ptr) = FPOINTER;
356 DCL_TYPE (ptr) = IPOINTER;
359 DCL_TYPE (ptr) = PPOINTER;
362 DCL_TYPE (ptr) = POINTER;
365 DCL_PTR_CONST (ptr) = port->mem.code_ro;
366 DCL_TYPE (ptr) = CPOINTER;
369 DCL_TYPE (ptr) = EEPPOINTER;
372 DCL_TYPE (ptr) = GPOINTER;
375 /* the storage class of type ends here */
378 SPEC_VOLATILE (type) = 0;
381 /* now change all the remaining unknown pointers
382 to generic pointers */
385 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
386 DCL_TYPE (ptr) = GPOINTER;
390 /* same for the type although it is highly unlikely that
391 type will have a pointer */
394 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
395 DCL_TYPE (type) = GPOINTER;
401 /*------------------------------------------------------------------*/
402 /* addDecl - adds a declarator @ the end of a chain */
403 /*------------------------------------------------------------------*/
405 addDecl (symbol * sym, int type, sym_link * p)
411 /* if we are passed a link then set head & tail */
420 head = tail = newLink ();
421 DCL_TYPE (head) = type;
424 /* if this is the first entry */
432 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
434 sym->etype = mergeSpec (sym->etype, head, sym->name);
438 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
441 while (t->next != sym->etype)
444 tail->next = sym->etype;
448 sym->etype->next = head;
454 /* if the type is an unknown pointer and has
455 a tspec then take the storage class const & volatile
456 attribute from the tspec & make it those of this
460 //DCL_TYPE (p) == UPOINTER &&
463 if (!IS_SPEC (sym->etype))
465 sym->etype = sym->etype->next = newLink ();
466 sym->etype->class = SPECIFIER;
468 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
469 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
470 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
471 DCL_TSPEC (p) = NULL;
476 /*------------------------------------------------------------------
477 checkTypeSanity: prevent the user from doing e.g.:
479 ------------------------------------------------------------------*/
480 void checkTypeSanity(sym_link *etype, char *name) {
484 if (getenv("DEBUG_SANITY")) {
485 fprintf (stderr, "sanity check skipped for %s (etype==0)\n", name);
490 if (!IS_SPEC(etype)) {
491 if (getenv("DEBUG_SANITY")) {
492 fprintf (stderr, "sanity check skipped for %s (!IS_SPEC)\n", name);
497 noun=nounName(etype);
499 if (getenv("DEBUG_SANITY")) {
500 fprintf (stderr, "checking sanity for %s %x\n", name, (int)etype);
503 if ((SPEC_NOUN(etype)==V_CHAR ||
504 SPEC_NOUN(etype)==V_FLOAT ||
505 SPEC_NOUN(etype)==V_DOUBLE ||
506 SPEC_NOUN(etype)==V_VOID) &&
507 (etype->select.s._short || SPEC_LONG(etype))) {
508 // long or short for char float double or void
509 werror (E_LONG_OR_SHORT_INVALID, noun, name);
511 if ((SPEC_NOUN(etype)==V_FLOAT ||
512 SPEC_NOUN(etype)==V_DOUBLE ||
513 SPEC_NOUN(etype)==V_VOID) &&
514 (etype->select.s._signed || SPEC_USIGN(etype))) {
515 // signed or unsigned for float double or void
516 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
519 // special case for "short"
520 if (etype->select.s._short) {
521 SPEC_NOUN(etype) = options.shortis8bits ? V_CHAR : V_INT;
522 etype->select.s._short = 0;
526 "const a;" or "data b;" or "signed s" or "long l"
528 if (!SPEC_NOUN(etype)) {
529 SPEC_NOUN(etype)=V_INT;
532 if (etype->select.s._signed && SPEC_USIGN(etype)) {
533 // signed AND unsigned
534 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
536 if (etype->select.s._short && SPEC_LONG(etype)) {
538 werror (E_LONG_AND_SHORT_INVALID, noun, name);
543 /*------------------------------------------------------------------*/
544 /* mergeSpec - merges two specifiers and returns the new one */
545 /*------------------------------------------------------------------*/
547 mergeSpec (sym_link * dest, sym_link * src, char *name)
549 sym_link *symlink=dest;
551 if (!IS_SPEC(dest) || !IS_SPEC(src)) {
552 werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "cannot merge declarator");
556 if (SPEC_NOUN(src)) {
557 if (!SPEC_NOUN(dest)) {
558 SPEC_NOUN(dest)=SPEC_NOUN(src);
560 /* we shouldn't redeclare the type */
561 if (getenv("DEBUG_SANITY")) {
562 fprintf (stderr, "mergeSpec: ");
564 werror(E_TWO_OR_MORE_DATA_TYPES, name);
568 if (SPEC_SCLS(src)) {
569 /* if destination has no storage class */
570 if (!SPEC_SCLS (dest) || SPEC_SCLS(dest)==S_REGISTER) {
571 SPEC_SCLS (dest) = SPEC_SCLS (src);
573 if (getenv("DEBUG_SANITY")) {
574 fprintf (stderr, "mergeSpec: ");
576 werror(E_TWO_OR_MORE_STORAGE_CLASSES, name);
580 /* copy all the specifications */
582 // we really should do:
584 if (SPEC_what(src)) {
585 if (SPEC_what(dest)) {
586 werror(W_DUPLICATE_SPEC, "what");
588 SPEC_what(dst)|=SPEC_what(src);
591 // but there are more important thing right now
593 SPEC_LONG (dest) |= SPEC_LONG (src);
594 dest->select.s._short|=src->select.s._short;
595 SPEC_USIGN (dest) |= SPEC_USIGN (src);
596 dest->select.s._signed|=src->select.s._signed;
597 SPEC_STAT (dest) |= SPEC_STAT (src);
598 SPEC_EXTR (dest) |= SPEC_EXTR (src);
599 SPEC_ABSA (dest) |= SPEC_ABSA (src);
600 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
601 SPEC_ADDR (dest) |= SPEC_ADDR (src);
602 SPEC_OCLS (dest) = SPEC_OCLS (src);
603 SPEC_BLEN (dest) |= SPEC_BLEN (src);
604 SPEC_BSTR (dest) |= SPEC_BSTR (src);
605 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
607 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
608 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
610 /* these are the only function attributes that will be set
611 in a specifier while parsing */
612 FUNC_NONBANKED(dest) |= FUNC_NONBANKED(src);
613 FUNC_BANKED(dest) |= FUNC_BANKED(src);
614 FUNC_ISCRITICAL(dest) |= FUNC_ISCRITICAL(src);
615 FUNC_ISREENT(dest) |= FUNC_ISREENT(src);
616 FUNC_ISNAKED(dest) |= FUNC_ISNAKED(src);
617 FUNC_ISISR(dest) |= FUNC_ISISR(src);
618 FUNC_INTNO(dest) |= FUNC_INTNO(src);
619 FUNC_REGBANK(dest) |= FUNC_REGBANK(src);
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->key = src->key;
882 dest->allocreq = src->allocreq;
886 /*------------------------------------------------------------------*/
887 /* reverseSyms - reverses the links for a symbol chain */
888 /*------------------------------------------------------------------*/
890 reverseSyms (symbol * sym)
892 symbol *prev, *curr, *next;
907 sym->next = (void *) NULL;
911 /*------------------------------------------------------------------*/
912 /* reverseLink - reverses the links for a type chain */
913 /*------------------------------------------------------------------*/
915 reverseLink (sym_link * type)
917 sym_link *prev, *curr, *next;
932 type->next = (void *) NULL;
936 /*------------------------------------------------------------------*/
937 /* addSymChain - adds a symbol chain to the symboltable */
938 /*------------------------------------------------------------------*/
940 addSymChain (symbol * symHead)
942 symbol *sym = symHead;
945 for (; sym != NULL; sym = sym->next)
948 checkTypeSanity(sym->etype, sym->name);
950 /* if already exists in the symbol table then check if
951 one of them is an extern definition if yes then
952 then check if the type match, if the types match then
953 delete the current entry and add the new entry */
954 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
955 csym->level == sym->level) {
957 /* one definition extern ? */
958 if (IS_EXTERN (csym->etype) || IS_EXTERN (sym->etype)) {
959 /* do types match ? */
960 if (compareType (csym->type, sym->type) != 1) {
962 werror (E_EXTERN_MISMATCH, csym->name);
965 /* delete current entry */
966 deleteSym (SymbolTab, csym, csym->name);
969 werror (E_DUPLICATE, sym->name);
975 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
980 /*------------------------------------------------------------------*/
981 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
982 /*------------------------------------------------------------------*/
984 funcInChain (sym_link * lnk)
995 /*------------------------------------------------------------------*/
996 /* structElemType - returns the type info of a sturct member */
997 /*------------------------------------------------------------------*/
999 structElemType (sym_link * stype, value * id)
1001 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
1002 sym_link *type, *etype;
1003 sym_link *petype = getSpec (stype);
1008 /* look for the id */
1011 if (strcmp (fields->rname, id->name) == 0)
1013 type = copyLinkChain (fields->type);
1014 etype = getSpec (type);
1015 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
1016 SPEC_SCLS (etype) : SPEC_SCLS (petype));
1019 fields = fields->next;
1021 werror (E_NOT_MEMBER, id->name);
1026 /*------------------------------------------------------------------*/
1027 /* getStructElement - returns element of a tructure definition */
1028 /*------------------------------------------------------------------*/
1030 getStructElement (structdef * sdef, symbol * sym)
1034 for (field = sdef->fields; field; field = field->next)
1035 if (strcmp (field->name, sym->name) == 0)
1038 werror (E_NOT_MEMBER, sym->name);
1040 return sdef->fields;
1043 /*------------------------------------------------------------------*/
1044 /* compStructSize - computes the size of a structure */
1045 /*------------------------------------------------------------------*/
1047 compStructSize (int su, structdef * sdef)
1049 int sum = 0, usum = 0;
1053 /* for the identifiers */
1054 loop = sdef->fields;
1057 /* create the internal name for this variable */
1058 sprintf (loop->rname, "_%s", loop->name);
1059 loop->offset = (su == UNION ? sum = 0 : sum);
1060 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1062 /* if this is a bit field */
1065 /* change it to a unsigned bit */
1066 SPEC_NOUN (loop->etype) = V_BIT;
1067 SPEC_USIGN (loop->etype) = 1;
1068 /* check if this fit into the remaining */
1069 /* bits of this byte else align it to the */
1070 /* next byte boundary */
1071 if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
1072 SPEC_BSTR (loop->etype) = bitOffset;
1073 if ((bitOffset += (loop->bitVar % 8)) == 8)
1076 else /* does not fit */ {
1078 SPEC_BSTR (loop->etype) = bitOffset;
1079 sum += (loop->bitVar / 8);
1080 bitOffset += (loop->bitVar % 8);
1082 /* if this is the last field then pad */
1083 if (!loop->next && bitOffset && bitOffset != 8) {
1089 checkDecl (loop, 1);
1090 sum += getSize (loop->type);
1093 /* if function then do the arguments for it */
1094 if (funcInChain (loop->type)) {
1095 processFuncArgs (loop, 1);
1100 /* if this is not a bitfield but the */
1101 /* previous one was and did not take */
1102 /* the whole byte then pad the rest */
1103 if ((loop && !loop->bitVar) && bitOffset) {
1108 /* if union then size = sizeof larget field */
1110 usum = max (usum, sum);
1114 return (su == UNION ? usum : sum);
1117 /*------------------------------------------------------------------*/
1118 /* checkSClass - check the storage class specification */
1119 /*------------------------------------------------------------------*/
1121 checkSClass (symbol * sym, int isProto)
1123 if (getenv("DEBUG_SANITY")) {
1124 fprintf (stderr, "checkSClass: %s \n", sym->name);
1127 /* type is literal can happen foe enums change
1129 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1130 SPEC_SCLS (sym->etype) = S_AUTO;
1132 /* if sfr or sbit then must also be */
1133 /* volatile the initial value will be xlated */
1134 /* to an absolute address */
1135 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1136 SPEC_SCLS (sym->etype) == S_SFR)
1138 SPEC_VOLATILE (sym->etype) = 1;
1139 /* if initial value given */
1142 SPEC_ABSA (sym->etype) = 1;
1143 SPEC_ADDR (sym->etype) =
1144 (int) list2int (sym->ival);
1149 /* if absolute address given then it mark it as
1151 if (IS_ABSOLUTE (sym->etype))
1152 SPEC_VOLATILE (sym->etype) = 1;
1154 /* global variables declared const put into code */
1155 if (sym->level == 0 &&
1156 SPEC_CONST (sym->etype)) {
1157 SPEC_SCLS (sym->etype) = S_CODE;
1160 /* global variable in code space is a constant */
1161 if (sym->level == 0 &&
1162 SPEC_SCLS (sym->etype) == S_CODE &&
1164 SPEC_CONST (sym->etype) = 1;
1167 /* if bit variable then no storage class can be */
1168 /* specified since bit is already a storage */
1169 if (IS_BITVAR (sym->etype) &&
1170 (SPEC_SCLS (sym->etype) != S_FIXED &&
1171 SPEC_SCLS (sym->etype) != S_SBIT &&
1172 SPEC_SCLS (sym->etype) != S_BIT)
1175 werror (E_BITVAR_STORAGE, sym->name);
1176 SPEC_SCLS (sym->etype) = S_FIXED;
1179 /* extern variables cannot be initialized */
1180 if (IS_EXTERN (sym->etype) && sym->ival)
1182 werror (E_EXTERN_INIT, sym->name);
1186 /* if this is an atomatic symbol */
1187 if (sym->level && (options.stackAuto || reentrant)) {
1188 if ((SPEC_SCLS (sym->etype) == S_AUTO ||
1189 SPEC_SCLS (sym->etype) == S_FIXED ||
1190 SPEC_SCLS (sym->etype) == S_REGISTER ||
1191 SPEC_SCLS (sym->etype) == S_STACK ||
1192 SPEC_SCLS (sym->etype) == S_XSTACK)) {
1193 SPEC_SCLS (sym->etype) = S_AUTO;
1195 /* storage class may only be specified for statics */
1196 if (!IS_STATIC(sym->etype)) {
1197 werror (E_AUTO_ASSUMED, sym->name);
1202 /* automatic symbols cannot be given */
1203 /* an absolute address ignore it */
1205 SPEC_ABSA (sym->etype) &&
1206 (options.stackAuto || reentrant))
1208 werror (E_AUTO_ABSA, sym->name);
1209 SPEC_ABSA (sym->etype) = 0;
1212 /* arrays & pointers cannot be defined for bits */
1213 /* SBITS or SFRs or BIT */
1214 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1215 (SPEC_NOUN (sym->etype) == V_BIT ||
1216 SPEC_NOUN (sym->etype) == V_SBIT ||
1217 SPEC_SCLS (sym->etype) == S_SFR))
1218 werror (E_BIT_ARRAY, sym->name);
1220 /* if this is a bit|sbit then set length & start */
1221 if (SPEC_NOUN (sym->etype) == V_BIT ||
1222 SPEC_NOUN (sym->etype) == V_SBIT)
1224 SPEC_BLEN (sym->etype) = 1;
1225 SPEC_BSTR (sym->etype) = 0;
1229 /* variables declared in CODE space must have */
1230 /* initializers if not an extern */
1231 if (SPEC_SCLS (sym->etype) == S_CODE &&
1232 sym->ival == NULL &&
1234 port->mem.code_ro &&
1235 !IS_EXTERN (sym->etype) &&
1236 !funcInChain (sym->type))
1237 werror (E_CODE_NO_INIT, sym->name);
1240 /* if parameter or local variable then change */
1241 /* the storage class to reflect where the var will go */
1242 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1243 !IS_STATIC(sym->etype))
1245 if (options.stackAuto || (currFunc && IFFUNC_ISREENT (currFunc->type)))
1247 SPEC_SCLS (sym->etype) = (options.useXstack ?
1248 S_XSTACK : S_STACK);
1252 /* hack-o-matic! I see no reason why the useXstack option should ever
1253 * control this allcoation, but the code was originally that way, and
1254 * changing it for non-390 ports breaks the compiler badly.
1256 bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1257 SPEC_SCLS (sym->etype) = (useXdata ?
1263 /*------------------------------------------------------------------*/
1264 /* changePointer - change pointer to functions */
1265 /*------------------------------------------------------------------*/
1267 changePointer (symbol * sym)
1271 /* go thru the chain of declarations */
1272 /* if we find a pointer to a function */
1273 /* unconditionally change it to a ptr */
1275 for (p = sym->type; p; p = p->next)
1277 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1278 DCL_TYPE (p) = GPOINTER;
1279 if (IS_PTR (p) && IS_FUNC (p->next))
1280 DCL_TYPE (p) = CPOINTER;
1284 /*------------------------------------------------------------------*/
1285 /* checkDecl - does semantic validation of a declaration */
1286 /*------------------------------------------------------------------*/
1288 checkDecl (symbol * sym, int isProto)
1291 checkSClass (sym, isProto); /* check the storage class */
1292 changePointer (sym); /* change pointers if required */
1294 /* if this is an array without any dimension
1295 then update the dimension from the initial value */
1296 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1297 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1302 /*------------------------------------------------------------------*/
1303 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1304 /*------------------------------------------------------------------*/
1306 copyLinkChain (sym_link * p)
1308 sym_link *head, *curr, *loop;
1311 head = loop = (curr ? newLink () : (void *) NULL);
1314 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1315 loop->next = (curr->next ? newLink () : (void *) NULL);
1324 /*------------------------------------------------------------------*/
1325 /* cleanUpBlock - cleansup the symbol table specified for all the */
1326 /* symbols in the given block */
1327 /*------------------------------------------------------------------*/
1329 cleanUpBlock (bucket ** table, int block)
1334 /* go thru the entire table */
1335 for (i = 0; i < 256; i++)
1337 for (chain = table[i]; chain; chain = chain->next)
1339 if (chain->block >= block)
1341 deleteSym (table, chain->sym, chain->name);
1347 /*------------------------------------------------------------------*/
1348 /* cleanUpLevel - cleansup the symbol table specified for all the */
1349 /* symbols in the given level */
1350 /*------------------------------------------------------------------*/
1352 cleanUpLevel (bucket ** table, int level)
1357 /* go thru the entire table */
1358 for (i = 0; i < 256; i++)
1360 for (chain = table[i]; chain; chain = chain->next)
1362 if (chain->level >= level)
1364 deleteSym (table, chain->sym, chain->name);
1370 /*------------------------------------------------------------------*/
1371 /* computeType - computes the resultant type from two types */
1372 /*------------------------------------------------------------------*/
1374 computeType (sym_link * type1, sym_link * type2)
1378 sym_link *etype1 = getSpec (type1);
1379 sym_link *etype2 = getSpec (type2);
1381 /* if one of them is a float then result is a float */
1382 /* here we assume that the types passed are okay */
1383 /* and can be cast to one another */
1384 /* which ever is greater in size */
1385 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1386 rType = newFloatLink ();
1388 /* if only one of them is a bit variable
1389 then the other one prevails */
1390 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1391 rType = copyLinkChain (type2);
1392 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1393 rType = copyLinkChain (type1);
1395 /* if one of them is a pointer then that
1398 rType = copyLinkChain (type1);
1399 else if (IS_PTR (type2))
1400 rType = copyLinkChain (type2);
1401 else if (getSize (type1) > getSize (type2))
1402 rType = copyLinkChain (type1);
1404 rType = copyLinkChain (type2);
1406 reType = getSpec (rType);
1408 /* if either of them unsigned but not val then make this unsigned */
1409 if (((!IS_LITERAL(type1) && SPEC_USIGN (etype1)) ||
1410 (!IS_LITERAL(type2) && SPEC_USIGN (etype2))) &&
1412 SPEC_USIGN (reType) = 1;
1414 SPEC_USIGN (reType) = 0;
1416 /* if result is a literal then make not so */
1417 if (IS_LITERAL (reType))
1418 SPEC_SCLS (reType) = S_REGISTER;
1423 /*--------------------------------------------------------------------*/
1424 /* compareType - will do type check return 1 if match, -1 if castable */
1425 /*--------------------------------------------------------------------*/
1427 compareType (sym_link * dest, sym_link * src)
1438 /* if dest is a declarator then */
1443 if (DCL_TYPE (src) == DCL_TYPE (dest)) {
1445 //checkFunction(src,dest);
1447 return compareType (dest->next, src->next);
1449 if (IS_PTR (src) && IS_GENPTR (dest))
1451 if (IS_PTR (dest) && IS_ARRAY (src)) {
1452 value *val=aggregateToPointer (valFromType(src));
1453 int res=compareType (dest, val->type);
1454 Safe_free(val->type);
1456 //return res ? -1 : 0;
1459 if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1460 return compareType (dest->next, src);
1463 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1469 /* if one is a specifier and the other is not */
1470 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1471 (IS_SPEC (dest) && !IS_SPEC (src)))
1474 /* if one of them is a void then ok */
1475 if (SPEC_NOUN (dest) == V_VOID &&
1476 SPEC_NOUN (src) != V_VOID)
1479 if (SPEC_NOUN (dest) != V_VOID &&
1480 SPEC_NOUN (src) == V_VOID)
1483 /* if they are both bitfields then if the lengths
1484 and starts don't match */
1485 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1486 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1487 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1490 /* it is a specifier */
1491 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1493 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1494 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1495 getSize (dest) == getSize (src))
1497 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1502 else if (IS_STRUCT (dest))
1504 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1509 if (SPEC_LONG (dest) != SPEC_LONG (src))
1512 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1518 /*------------------------------------------------------------------*/
1519 /* inCalleeSaveList - return 1 if found in callee save list */
1520 /*------------------------------------------------------------------*/
1522 inCalleeSaveList (char *s)
1526 for (i = 0; options.calleeSaves[i]; i++)
1527 if (strcmp (options.calleeSaves[i], s) == 0)
1533 /*-----------------------------------------------------------------*/
1534 /* aggregateToPointer: change an agggregate type function */
1535 /* argument to a pointer to that type. */
1536 /*-----------------------------------------------------------------*/
1538 aggregateToPointer (value * val)
1540 if (IS_AGGREGATE (val->type))
1542 /* if this is a structure */
1543 /* then we need to add a new link */
1544 if (IS_STRUCT (val->type))
1546 /* first lets add DECLARATOR type */
1547 sym_link *p = val->type;
1549 werror (W_STRUCT_AS_ARG, val->name);
1550 val->type = newLink ();
1551 val->type->next = p;
1554 /* change to a pointer depending on the */
1555 /* storage class specified */
1556 switch (SPEC_SCLS (val->etype))
1559 DCL_TYPE (val->type) = IPOINTER;
1562 DCL_TYPE (val->type) = PPOINTER;
1565 if (SPEC_OCLS(val->etype)) {
1566 DCL_TYPE(val->type)=PTR_TYPE(SPEC_OCLS(val->etype));
1570 if (TARGET_IS_DS390)
1572 /* The AUTO and REGISTER classes should probably
1573 * also become generic pointers, but I haven't yet
1574 * devised a test case for that.
1576 DCL_TYPE (val->type) = GPOINTER;
1579 if (options.model==MODEL_LARGE) {
1580 DCL_TYPE (val->type) = FPOINTER;
1587 DCL_TYPE (val->type) = POINTER;
1590 DCL_TYPE (val->type) = CPOINTER;
1593 DCL_TYPE (val->type) = FPOINTER;
1596 DCL_TYPE (val->type) = EEPPOINTER;
1599 DCL_TYPE (val->type) = GPOINTER;
1602 /* is there is a symbol associated then */
1603 /* change the type of the symbol as well */
1606 val->sym->type = copyLinkChain (val->type);
1607 val->sym->etype = getSpec (val->sym->type);
1612 /*------------------------------------------------------------------*/
1613 /* checkFunction - does all kinds of check on a function */
1614 /*------------------------------------------------------------------*/
1616 checkFunction (symbol * sym, symbol *csym)
1618 value *exargs, *acargs;
1622 if (getenv("DEBUG_SANITY")) {
1623 fprintf (stderr, "checkFunction: %s ", sym->name);
1626 /* make sure the type is complete and sane */
1627 checkTypeSanity(((symbol *)sym)->etype, ((symbol *)sym)->name);
1629 /* if not type then some kind of error */
1633 /* if the function has no type then make it return int */
1634 if (!sym->type->next)
1635 sym->type->next = sym->etype = newIntLink ();
1637 /* function cannot return aggregate */
1638 if (IS_AGGREGATE (sym->type->next))
1640 werror (E_FUNC_AGGR, sym->name);
1644 /* function cannot return bit */
1645 if (IS_BITVAR (sym->type->next))
1647 werror (E_FUNC_BIT, sym->name);
1651 /* check if this function is defined as calleeSaves
1652 then mark it as such */
1653 FUNC_CALLEESAVES(sym->type) = inCalleeSaveList (sym->name);
1655 /* if interrupt service routine */
1656 /* then it cannot have arguments */
1657 if (IFFUNC_ARGS(sym->type) && FUNC_ISISR (sym->type))
1659 if (!IS_VOID(FUNC_ARGS(sym->type)->type)) {
1660 werror (E_INT_ARGS, sym->name);
1661 FUNC_ARGS(sym->type)=NULL;
1665 if (!csym && !(csym = findSym (SymbolTab, sym, sym->name)))
1666 return 1; /* not defined nothing more to check */
1668 /* check if body already present */
1669 if (csym && IFFUNC_HASBODY(csym->type))
1671 werror (E_FUNC_BODY, sym->name);
1675 /* check the return value type */
1676 if (compareType (csym->type, sym->type) <= 0)
1678 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1679 printFromToType(csym->type, sym->type);
1683 if (FUNC_ISISR (csym->type) != FUNC_ISISR (sym->type))
1685 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1688 if (FUNC_REGBANK (csym->type) != FUNC_REGBANK (sym->type))
1690 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1693 if (IFFUNC_ISNAKED (csym->type) != IFFUNC_ISNAKED (sym->type))
1695 werror (E_PREV_DEF_CONFLICT, csym->name, "_naked");
1698 /* compare expected args with actual args */
1699 exargs = FUNC_ARGS(csym->type);
1700 acargs = FUNC_ARGS(sym->type);
1702 /* for all the expected args do */
1705 exargs = exargs->next, acargs = acargs->next, argCnt++)
1707 if (getenv("DEBUG_SANITY")) {
1708 fprintf (stderr, "checkFunction: %s ", exargs->name);
1710 /* make sure the type is complete and sane */
1711 checkTypeSanity(exargs->etype, exargs->name);
1713 /* If the actual argument is an array, any prototype
1714 * will have modified it to a pointer. Duplicate that
1717 if (IS_AGGREGATE (acargs->type))
1719 checkValue = copyValue (acargs);
1720 aggregateToPointer (checkValue);
1724 checkValue = acargs;
1727 if (compareType (exargs->type, checkValue->type) <= 0)
1729 werror (E_ARG_TYPE, argCnt);
1730 printFromToType(exargs->type, checkValue->type);
1735 /* if one them ended we have a problem */
1736 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1737 (!exargs && acargs && !IS_VOID (acargs->type)))
1738 werror (E_ARG_COUNT);
1740 /* replace with this defition */
1741 sym->cdef = csym->cdef;
1742 deleteSym (SymbolTab, csym, csym->name);
1743 addSym (SymbolTab, sym, sym->name, sym->level, sym->block, 1);
1744 if (IS_EXTERN (csym->etype) && !
1745 IS_EXTERN (sym->etype))
1747 addSet (&publics, sym);
1752 /*-----------------------------------------------------------------*/
1753 /* processFuncArgs - does some processing with function args */
1754 /*-----------------------------------------------------------------*/
1756 processFuncArgs (symbol * func, int ignoreName)
1761 /* if this function has variable argument list */
1762 /* then make the function a reentrant one */
1763 if (IFFUNC_HASVARARGS(func->type))
1764 FUNC_ISREENT(func->type)=1;
1766 /* check if this function is defined as calleeSaves
1767 then mark it as such */
1768 FUNC_CALLEESAVES(func->type) = inCalleeSaveList (func->name);
1770 /* loop thru all the arguments */
1771 val = FUNC_ARGS(func->type);
1773 /* if it is void then remove parameters */
1774 if (val && IS_VOID (val->type))
1776 FUNC_ARGS(func->type) = NULL;
1780 /* reset regparm for the port */
1781 (*port->reset_regparms) ();
1782 /* if any of the arguments is an aggregate */
1783 /* change it to pointer to the same type */
1786 /* mark it as a register parameter if
1787 the function does not have VA_ARG
1788 and as port dictates */
1789 if (!IFFUNC_HASVARARGS(func->type) &&
1790 (*port->reg_parm) (val->type))
1792 SPEC_REGPARM (val->etype) = 1;
1795 if (IS_AGGREGATE (val->type))
1797 aggregateToPointer (val);
1803 /* if this is an internal generated function call */
1805 /* ignore --stack-auto for this one, we don't know how it is compiled */
1806 /* simply trust on --int-long-reent or --float-reent */
1807 if (IFFUNC_ISREENT(func->type)) {
1811 /* if this function is reentrant or */
1812 /* automatics r 2b stacked then nothing */
1813 if (IFFUNC_ISREENT (func->type) || options.stackAuto)
1817 val = FUNC_ARGS(func->type);
1822 /* if a symbolname is not given */
1823 /* synthesize a variable name */
1827 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1828 val->sym = newSymbol (val->name, 1);
1829 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1830 val->sym->type = copyLinkChain (val->type);
1831 val->sym->etype = getSpec (val->sym->type);
1832 val->sym->_isparm = 1;
1833 strcpy (val->sym->rname, val->name);
1834 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1835 SPEC_STAT (func->etype);
1836 addSymChain (val->sym);
1839 else /* symbol name given create synth name */
1842 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1843 strcpy (val->sym->rname, val->name);
1844 val->sym->_isparm = 1;
1845 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1846 (options.model != MODEL_SMALL ? xdata : data);
1847 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1848 SPEC_STAT (func->etype);
1854 /*-----------------------------------------------------------------*/
1855 /* isSymbolEqual - compares two symbols return 1 if they match */
1856 /*-----------------------------------------------------------------*/
1858 isSymbolEqual (symbol * dest, symbol * src)
1860 /* if pointers match then equal */
1864 /* if one of them is null then don't match */
1868 /* if both of them have rname match on rname */
1869 if (dest->rname[0] && src->rname[0])
1870 return (!strcmp (dest->rname, src->rname));
1872 /* otherwise match on name */
1873 return (!strcmp (dest->name, src->name));
1876 void PT(sym_link *type)
1878 printTypeChain(type,0);
1880 /*-----------------------------------------------------------------*/
1881 /* printTypeChain - prints the type chain in human readable form */
1882 /*-----------------------------------------------------------------*/
1884 printTypeChain (sym_link * start, FILE * of)
1887 sym_link * type, * search;
1896 fprintf (of, "**err**");
1900 /* print the chain as it is written in the source: */
1901 /* start with the last entry */
1902 for (type = start; type && type->next; type = type->next)
1908 if (DCL_PTR_VOLATILE (type)) {
1909 fprintf (of, "volatile ");
1911 switch (DCL_TYPE (type))
1914 fprintf (of, "function ");
1917 if (DCL_PTR_CONST (type))
1918 fprintf (of, "const ");
1919 fprintf (of, "* generic ");
1922 if (DCL_PTR_CONST (type))
1923 fprintf (of, "const ");
1924 fprintf (of, "* code ");
1927 if (DCL_PTR_CONST (type))
1928 fprintf (of, "const ");
1929 fprintf (of, "* xdata ");
1932 if (DCL_PTR_CONST (type))
1933 fprintf (of, "const ");
1934 fprintf (of, "* eeprom ");
1938 if (DCL_PTR_CONST (type))
1939 fprintf (of, "const ");
1940 fprintf (of, "* near ");
1943 if (DCL_PTR_CONST (type))
1944 fprintf (of, "const ");
1945 fprintf (of, "* idata ");
1948 if (DCL_PTR_CONST (type))
1949 fprintf (of, "const ");
1950 fprintf (of, "* pdata ");
1953 if (DCL_PTR_CONST (type))
1954 fprintf (of, "const ");
1955 fprintf (of, "* unkown ");
1958 fprintf (of, "[] ");
1964 switch (SPEC_SCLS(type))
1966 case S_DATA: fprintf (of, "data "); break;
1967 case S_XDATA: fprintf (of, "xdata "); break;
1968 case S_SFR: fprintf (of, "sfr "); break;
1969 case S_SBIT: fprintf (of, "sbit "); break;
1970 case S_CODE: fprintf (of, "code "); break;
1971 case S_IDATA: fprintf (of, "idata "); break;
1972 case S_PDATA: fprintf (of, "pdata "); break;
1973 case S_LITERAL: fprintf (of, "literal "); break;
1974 case S_STACK: fprintf (of, "stack "); break;
1975 case S_XSTACK: fprintf (of, "xstack "); break;
1976 case S_BIT: fprintf (of, "bit "); break;
1977 case S_EEPROM: fprintf (of, "eeprom "); break;
1981 if (SPEC_VOLATILE (type))
1982 fprintf (of, "volatile ");
1983 if (SPEC_USIGN (type))
1984 fprintf (of, "unsigned ");
1985 if (SPEC_CONST (type))
1986 fprintf (of, "const ");
1988 switch (SPEC_NOUN (type))
1992 fprintf (of, "long ");
1993 fprintf (of, "int");
1997 fprintf (of, "char");
2001 fprintf (of, "void");
2005 fprintf (of, "float");
2009 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
2013 fprintf (of, "sbit");
2017 fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
2021 fprintf (of, "double");
2025 fprintf (of, "unknown type");
2029 /* search entry in list before "type" */
2030 for (search = start; search && search->next != type;)
2031 search = search->next;
2040 /*-----------------------------------------------------------------*/
2041 /* cdbTypeInfo - print the type information for debugger */
2042 /*-----------------------------------------------------------------*/
2044 cdbTypeInfo (sym_link * type, FILE * of)
2046 fprintf (of, "{%d}", getSize (type));
2051 switch (DCL_TYPE (type))
2054 fprintf (of, "DF,");
2057 fprintf (of, "DG,");
2060 fprintf (of, "DC,");
2063 fprintf (of, "DX,");
2066 fprintf (of, "DD,");
2069 fprintf (of, "DI,");
2072 fprintf (of, "DP,");
2075 fprintf (of, "DA,");
2078 fprintf (of, "DA%d,", DCL_ELEM (type));
2086 switch (SPEC_NOUN (type))
2108 fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
2116 fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
2123 if (SPEC_USIGN (type))
2131 /*-----------------------------------------------------------------*/
2132 /* cdbSymbol - prints a symbol & its type information for debugger */
2133 /*-----------------------------------------------------------------*/
2135 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
2147 fprintf (of, "S:"); /* symbol record */
2148 /* if this is not a structure symbol then
2149 we need to figure out the scope information */
2155 if (IS_STATIC (sym->etype))
2156 fprintf (of, "F%s$", moduleName); /* scope is file */
2158 fprintf (of, "G$"); /* scope is global */
2161 /* symbol is local */
2162 fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
2165 fprintf (of, "S$"); /* scope is structure */
2167 /* print the name, & mangled name */
2168 fprintf (of, "%s$%d$%d(", sym->name,
2169 sym->level, sym->block);
2171 cdbTypeInfo (sym->type, of);
2174 /* print the address space */
2175 map = SPEC_OCLS (sym->etype);
2176 fprintf (of, "%c,%d,%d",
2177 (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
2179 /* if assigned to registers then output register names */
2180 /* if this is a function then print
2181 if is it an interrupt routine & interrupt number
2182 and the register bank it is using */
2184 fprintf (of, ",%d,%d,%d", FUNC_ISISR (sym->type),
2185 FUNC_INTNO (sym->type), FUNC_REGBANK (sym->type));
2186 /* alternate location to find this symbol @ : eg registers
2193 /*-----------------------------------------------------------------*/
2194 /* cdbStruct - print a structure for debugger */
2195 /*-----------------------------------------------------------------*/
2197 cdbStruct (structdef * sdef, int block, FILE * of,
2198 int inStruct, char *tag)
2203 /* if block # then must have function scope */
2204 fprintf (of, "F%s$", moduleName);
2205 fprintf (of, "%s[", (tag ? tag : sdef->tag));
2206 for (sym = sdef->fields; sym; sym = sym->next)
2208 fprintf (of, "({%d}", sym->offset);
2209 cdbSymbol (sym, of, TRUE, FALSE);
2217 /*------------------------------------------------------------------*/
2218 /* cdbStructBlock - calls struct printing for a blcks */
2219 /*------------------------------------------------------------------*/
2221 cdbStructBlock (int block, FILE * of)
2224 bucket **table = StructTab;
2228 /* go thru the entire table */
2229 for (i = 0; i < 256; i++)
2231 for (chain = table[i]; chain; chain = chain->next)
2233 if (chain->block >= block)
2235 cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2241 /*-----------------------------------------------------------------*/
2242 /* powof2 - returns power of two for the number if number is pow 2 */
2243 /*-----------------------------------------------------------------*/
2245 powof2 (unsigned long num)
2258 if (n1s > 1 || nshifts == 0)
2274 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2275 symbol *__muldiv[3][3][2];
2276 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2277 sym_link *__multypes[3][2];
2278 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2279 symbol *__conv[2][3][2];
2280 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2281 symbol *__rlrr[2][3][2];
2283 sym_link *floatType;
2286 _mangleFunctionName(char *in)
2288 if (port->getMangledFunctionName)
2290 return port->getMangledFunctionName(in);
2298 /*-----------------------------------------------------------------*/
2299 /* initCSupport - create functions for C support routines */
2300 /*-----------------------------------------------------------------*/
2304 const char *smuldivmod[] =
2308 const char *sbwd[] =
2310 "char", "int", "long"
2316 const char *srlrr[] =
2321 int bwd, su, muldivmod, tofrom, rlrr;
2323 if (getenv("SDCC_NO_C_SUPPORT")) {
2324 /* for debugging only */
2328 floatType = newFloatLink ();
2330 for (bwd = 0; bwd < 3; bwd++)
2347 __multypes[bwd][0] = l;
2348 __multypes[bwd][1] = copyLinkChain (l);
2349 SPEC_USIGN (__multypes[bwd][1]) = 1;
2352 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2353 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2354 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2355 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2356 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2357 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2358 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2359 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2360 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2361 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2363 for (tofrom = 0; tofrom < 2; tofrom++)
2365 for (bwd = 0; bwd < 3; bwd++)
2367 for (su = 0; su < 2; su++)
2371 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2372 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], floatType, 1, options.float_rent);
2376 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2377 __conv[tofrom][bwd][su] = funcOfType (_mangleFunctionName(buffer), floatType, __multypes[bwd][su], 1, options.float_rent);
2383 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2385 for (bwd = 0; bwd < 3; bwd++)
2387 for (su = 0; su < 2; su++)
2389 sprintf (buffer, "_%s%s%s",
2390 smuldivmod[muldivmod],
2393 __muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2394 FUNC_NONBANKED (__muldiv[muldivmod][bwd][su]->type) = 1;
2399 for (rlrr = 0; rlrr < 2; rlrr++)
2401 for (bwd = 0; bwd < 3; bwd++)
2403 for (su = 0; su < 2; su++)
2405 sprintf (buffer, "_%s%s%s",
2409 __rlrr[rlrr][bwd][su] = funcOfType (_mangleFunctionName(buffer), __multypes[bwd][su], __multypes[0][0], 2, options.intlong_rent);
2410 FUNC_NONBANKED (__rlrr[rlrr][bwd][su]->type) = 1;