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 (SPEC_SHORT(sl)) 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,
103 int i; /* index into the hash Table */
104 bucket *bp; /* temp bucket * */
106 if (getenv("DEBUG_SANITY")) {
107 fprintf (stderr, "addSym: %s\n", sname);
109 /* Make sure sym is a symbol and not a structdef */
110 if (StructTab!=stab) checkTypeSanity(((symbol *)sym)->etype, sname);
112 /* the symbols are always added at the head of the list */
114 /* get a free entry */
115 bp = Safe_calloc (1, sizeof (bucket));
117 bp->sym = sym; /* update the symbol pointer */
118 bp->level = level; /* update the nest level */
120 strcpy (bp->name, sname); /* copy the name into place */
122 /* if this is the first entry */
125 bp->prev = bp->next = (void *) NULL; /* point to nothing */
128 /* not first entry then add @ head of list */
138 /*-----------------------------------------------------------------*/
139 /* deleteSym - deletes a symbol from the hash Table entry */
140 /*-----------------------------------------------------------------*/
142 deleteSym (bucket ** stab, void *sym, char *sname)
150 /* find the symbol */
153 if (bp->sym == sym) /* found it then break out */
154 break; /* of the loop */
158 if (!bp) /* did not find it */
160 /* if this is the first one in the chain */
164 if (stab[i]) /* if chain ! empty */
165 stab[i]->prev = (void *) NULL;
167 /* middle || end of chain */
170 if (bp->next) /* if not end of chain */
171 bp->next->prev = bp->prev;
173 bp->prev->next = bp->next;
178 /*-----------------------------------------------------------------*/
179 /* findSym - finds a symbol in a table */
180 /*-----------------------------------------------------------------*/
182 findSym (bucket ** stab, void *sym, const char *sname)
186 bp = stab[hashKey (sname)];
189 if (bp->sym == sym || strcmp (bp->name, sname) == 0)
194 return (bp ? bp->sym : (void *) NULL);
197 /*-----------------------------------------------------------------*/
198 /* findSymWithLevel - finds a symbol with a name & level */
199 /*-----------------------------------------------------------------*/
201 findSymWithLevel (bucket ** stab, symbol * sym)
205 bp = stab[hashKey (sym->name)];
208 ** do the search from the head of the list since the
209 ** elements are added at the head it is ensured that
210 ** we will find the deeper definitions before we find
211 ** the global ones. we need to check for symbols with
212 ** level <= to the level given, if levels match then block
213 ** numbers need to match as well
218 if (strcmp (bp->name, sym->name) == 0 && bp->level <= sym->level)
220 /* if this is parameter then nothing else need to be checked */
221 if (((symbol *) (bp->sym))->_isparm)
223 /* if levels match then block numbers hsould also match */
224 if (bp->level && bp->level == sym->level && bp->block == sym->block)
226 /* if levels don't match then we are okay */
227 if (bp->level && bp->level != sym->level && bp->block <= sym->block)
229 /* if this is a global variable then we are ok too */
237 return (void *) NULL;
240 /*-----------------------------------------------------------------*/
241 /* findSymWithBlock - finds a symbol with name in with a block */
242 /*-----------------------------------------------------------------*/
244 findSymWithBlock (bucket ** stab, symbol * sym, int block)
248 bp = stab[hashKey (sym->name)];
251 if (strcmp (bp->name, sym->name) == 0 &&
257 return (bp ? bp->sym : (void *) NULL);
260 /*------------------------------------------------------------------*/
261 /* newSymbol () - returns a new pointer to a symbol */
262 /*------------------------------------------------------------------*/
264 newSymbol (char *name, int scope)
268 sym = Safe_calloc (1, sizeof (symbol));
270 strcpy (sym->name, name); /* copy the name */
271 sym->level = scope; /* set the level */
272 sym->block = currBlockno;
273 sym->lineDef = yylineno; /* set the line number */
277 /*------------------------------------------------------------------*/
278 /* newLink - creates a new link (declarator,specifier) */
279 /*------------------------------------------------------------------*/
285 p = Safe_calloc (1, sizeof (sym_link));
290 /*------------------------------------------------------------------*/
291 /* newStruct - creats a new structdef from the free list */
292 /*------------------------------------------------------------------*/
294 newStruct (char *tag)
298 s = Safe_calloc (1, sizeof (structdef));
300 strcpy (s->tag, tag); /* copy the tag */
304 /*------------------------------------------------------------------*/
305 /* pointerTypes - do the computation for the pointer types */
306 /*------------------------------------------------------------------*/
308 pointerTypes (sym_link * ptr, sym_link * type)
313 /* find the first pointer type */
314 while (ptr && !IS_PTR (ptr))
317 /* could not find it */
318 if (!ptr || IS_SPEC (ptr) ||
319 DCL_TYPE (ptr) != UPOINTER)
322 /* change the pointer type depending on the
323 storage class of the type */
326 DCL_PTR_CONST (ptr) = SPEC_CONST (type);
327 DCL_PTR_VOLATILE (ptr) = SPEC_VOLATILE (type);
328 switch (SPEC_SCLS (type))
331 DCL_TYPE (ptr) = FPOINTER;
334 DCL_TYPE (ptr) = IPOINTER;
337 DCL_TYPE (ptr) = PPOINTER;
340 DCL_TYPE (ptr) = POINTER;
343 DCL_PTR_CONST (ptr) = port->mem.code_ro;
344 DCL_TYPE (ptr) = CPOINTER;
347 DCL_TYPE (ptr) = EEPPOINTER;
350 DCL_TYPE (ptr) = GPOINTER;
353 /* the storage class of type ends here */
356 SPEC_VOLATILE (type) = 0;
359 /* now change all the remaining unknown pointers
360 to generic pointers */
363 if (!IS_SPEC (ptr) && DCL_TYPE (ptr) == UPOINTER)
364 DCL_TYPE (ptr) = GPOINTER;
368 /* same for the type although it is highly unlikely that
369 type will have a pointer */
372 if (!IS_SPEC (type) && DCL_TYPE (type) == UPOINTER)
373 DCL_TYPE (type) = GPOINTER;
379 /*------------------------------------------------------------------*/
380 /* addDecl - adds a declarator @ the end of a chain */
381 /*------------------------------------------------------------------*/
383 addDecl (symbol * sym, int type, sym_link * p)
389 /* if we are passed a link then set head & tail */
398 head = tail = newLink ();
399 DCL_TYPE (head) = type;
402 /* if this is the first entry */
410 if (IS_SPEC (sym->etype) && IS_SPEC (head) && head == tail)
412 sym->etype = mergeSpec (sym->etype, head);
416 if (IS_SPEC (sym->etype) && !IS_SPEC (head) && head == tail)
419 while (t->next != sym->etype)
422 tail->next = sym->etype;
426 sym->etype->next = head;
432 /* if the type is a unknown pointer and has
433 a tspec then take the storage class const & volatile
434 attribute from the tspec & make it those of this
438 DCL_TYPE (p) == UPOINTER &&
441 if (!IS_SPEC (sym->etype))
443 sym->etype = sym->etype->next = newLink ();
444 sym->etype->class = SPECIFIER;
446 SPEC_SCLS (sym->etype) = SPEC_SCLS (DCL_TSPEC (p));
447 SPEC_CONST (sym->etype) = SPEC_CONST (DCL_TSPEC (p));
448 SPEC_VOLATILE (sym->etype) = SPEC_VOLATILE (DCL_TSPEC (p));
449 DCL_TSPEC (p) = NULL;
454 /*------------------------------------------------------------------
455 checkTypeSanity: prevent the user from doing e.g.:
457 ------------------------------------------------------------------*/
458 void checkTypeSanity(sym_link *dest, char *name) {
462 if (getenv("DEBUG_SANITY")) {
463 printf ("sanity check skipped for %s\n", name);
470 if (getenv("DEBUG_SANITY")) {
471 printf ("checking sanity for %s\n", name);
474 if ((SPEC_NOUN(dest)==V_CHAR ||
475 SPEC_NOUN(dest)==V_FLOAT ||
476 SPEC_NOUN(dest)==V_DOUBLE ||
477 SPEC_NOUN(dest)==V_VOID) &&
478 (SPEC_SHORT(dest) || SPEC_LONG(dest))) {
479 // long or short for char float double or void
480 werror (E_LONG_OR_SHORT_INVALID, noun, name);
482 if ((SPEC_NOUN(dest)==V_FLOAT ||
483 SPEC_NOUN(dest)==V_DOUBLE ||
484 SPEC_NOUN(dest)==V_VOID) &&
485 (SPEC_SIGNED(dest) || SPEC_USIGN(dest))) {
486 // signed or unsigned for float double or void
487 werror (E_SIGNED_OR_UNSIGNED_INVALID, noun, name);
490 if (!SPEC_NOUN(dest)) {
491 if (SPEC_SIGNED(dest) || SPEC_USIGN(dest)) {
492 SPEC_NOUN(dest)=V_INT;
496 if (SPEC_SIGNED(dest) && SPEC_USIGN(dest)) {
497 // signed AND unsigned
498 werror (E_SIGNED_AND_UNSIGNED_INVALID, noun, name);
500 if (SPEC_SHORT(dest) && SPEC_LONG(dest)) {
502 werror (E_LONG_AND_SHORT_INVALID, noun, name);
507 /*------------------------------------------------------------------*/
508 /* mergeSpec - merges two specifiers and returns the new one */
509 /*------------------------------------------------------------------*/
511 mergeSpec (sym_link * dest, sym_link * src)
514 /* we shouldn't redeclare the type */
515 if ((SPEC_NOUN (dest) && SPEC_NOUN (src)) &&
516 (SPEC_NOUN(dest) != SPEC_NOUN(src))) {
517 if (getenv("DEBUG_SANITY")) {
518 fprintf (stderr, "mergeSpec: ");
520 werror(E_TWO_OR_MORE_DATA_TYPES, yylval.yychar);
523 /* if noun different then src overrides */
524 if (SPEC_NOUN (dest) != SPEC_NOUN (src) && !SPEC_NOUN (dest))
525 SPEC_NOUN (dest) = SPEC_NOUN (src);
527 /* if destination has no storage class */
528 if (!SPEC_SCLS (dest) ||
529 ((SPEC_SCLS(dest) == S_CONSTANT || SPEC_SCLS(dest) == S_REGISTER) &&
531 SPEC_SCLS (dest) = SPEC_SCLS (src);
532 /* special case for const */
533 /* copy all the specifications */
534 SPEC_LONG (dest) |= SPEC_LONG (src);
535 SPEC_SHORT (dest) |= SPEC_SHORT (src);
536 SPEC_USIGN (dest) |= SPEC_USIGN (src);
537 SPEC_SIGNED (dest) |= SPEC_SIGNED (src);
538 SPEC_STAT (dest) |= SPEC_STAT (src);
539 SPEC_EXTR (dest) |= SPEC_EXTR (src);
540 SPEC_ABSA (dest) |= SPEC_ABSA (src);
541 SPEC_RENT (dest) |= SPEC_RENT (src);
542 SPEC_INTN (dest) |= SPEC_INTN (src);
543 SPEC_BANK (dest) |= SPEC_BANK (src);
544 SPEC_VOLATILE (dest) |= SPEC_VOLATILE (src);
545 SPEC_CRTCL (dest) |= SPEC_CRTCL (src);
546 SPEC_ADDR (dest) |= SPEC_ADDR (src);
547 SPEC_OCLS (dest) = SPEC_OCLS (src);
548 SPEC_BLEN (dest) |= SPEC_BLEN (src);
549 SPEC_BSTR (dest) |= SPEC_BSTR (src);
550 SPEC_TYPEDEF (dest) |= SPEC_TYPEDEF (src);
551 SPEC_NONBANKED (dest) |= SPEC_NONBANKED (src);
552 SPEC_NAKED (dest) |= SPEC_NAKED (src);
554 if (IS_STRUCT (dest) && SPEC_STRUCT (dest) == NULL)
555 SPEC_STRUCT (dest) = SPEC_STRUCT (src);
560 /*------------------------------------------------------------------*/
561 /* cloneSpec - copies the entire spec and returns a new spec */
562 /*------------------------------------------------------------------*/
564 cloneSpec (sym_link * src)
568 /* go thru chain till we find the specifier */
569 while (src && src->class != SPECIFIER)
573 memcpy (spec, src, sizeof (sym_link));
577 /*------------------------------------------------------------------*/
578 /* genSymName - generates and returns a name used for anonymous vars */
579 /*------------------------------------------------------------------*/
581 genSymName (int level)
583 static int gCount = 0;
584 static char gname[SDCC_NAME_MAX + 1];
586 sprintf (gname, "__%04d%04d", level, gCount++);
590 /*------------------------------------------------------------------*/
591 /* getSpec - returns the specifier part from a declaration chain */
592 /*------------------------------------------------------------------*/
594 getSpec (sym_link * p)
599 while (p && !(IS_SPEC (p)))
605 /*------------------------------------------------------------------*/
606 /* newCharLink() - creates an char type */
607 /*------------------------------------------------------------------*/
614 p->class = SPECIFIER;
615 SPEC_NOUN (p) = V_CHAR;
620 /*------------------------------------------------------------------*/
621 /* newFloatLink - a new Float type */
622 /*------------------------------------------------------------------*/
629 p->class = SPECIFIER;
630 SPEC_NOUN (p) = V_FLOAT;
635 /*------------------------------------------------------------------*/
636 /* newLongLink() - new long type */
637 /*------------------------------------------------------------------*/
644 p->class = SPECIFIER;
645 SPEC_NOUN (p) = V_INT;
651 /*------------------------------------------------------------------*/
652 /* newIntLink() - creates an int type */
653 /*------------------------------------------------------------------*/
660 p->class = SPECIFIER;
661 SPEC_NOUN (p) = V_INT;
666 /*------------------------------------------------------------------*/
667 /* getSize - returns size of a type chain in bits */
668 /*------------------------------------------------------------------*/
670 getSize (sym_link * p)
672 /* if nothing return 0 */
676 { /* if this is the specifier then */
677 switch (SPEC_NOUN (p))
678 { /* depending on the specifier type */
680 return (IS_LONG (p) ? LONGSIZE : (IS_SHORT (p) ? SHORTSIZE : INTSIZE));
688 return SPEC_STRUCT (p)->size;
694 return ((SPEC_BLEN (p) / 8) + (SPEC_BLEN (p) % 8 ? 1 : 0));
700 /* this is a specifier */
701 switch (DCL_TYPE (p))
706 return DCL_ELEM (p) * getSize (p->next);
723 /*------------------------------------------------------------------*/
724 /* bitsForType - returns # of bits required to store this type */
725 /*------------------------------------------------------------------*/
727 bitsForType (sym_link * p)
729 /* if nothing return 0 */
734 { /* if this is the specifier then */
736 switch (SPEC_NOUN (p))
737 { /* depending on the specifier type */
739 return (IS_LONG (p) ? LONGSIZE * 8 : (IS_SHORT (p) ? SHORTSIZE * 8 : INTSIZE * 8));
741 return FLOATSIZE * 8;
747 return SPEC_STRUCT (p)->size * 8;
753 return SPEC_BLEN (p);
759 /* this is a specifier */
760 switch (DCL_TYPE (p))
765 return DCL_ELEM (p) * getSize (p->next) * 8;
769 return (PTRSIZE * 8);
773 return (FPTRSIZE * 8);
775 return (GPTRSIZE * 8);
782 /*------------------------------------------------------------------*/
783 /* copySymbolChain - copies a symbol chain */
784 /*------------------------------------------------------------------*/
786 copySymbolChain (symbol * src)
793 dest = copySymbol (src);
794 dest->next = copySymbolChain (src->next);
798 /*------------------------------------------------------------------*/
799 /* copySymbol - makes a copy of a symbol */
800 /*------------------------------------------------------------------*/
802 copySymbol (symbol * src)
809 dest = newSymbol (src->name, src->level);
810 memcpy (dest, src, sizeof (symbol));
811 dest->level = src->level;
812 dest->block = src->block;
813 dest->ival = copyIlist (src->ival);
814 dest->type = copyLinkChain (src->type);
815 dest->etype = getSpec (dest->type);
817 dest->args = copyValueChain (src->args);
818 dest->key = src->key;
819 dest->calleeSave = src->calleeSave;
820 dest->allocreq = src->allocreq;
824 /*------------------------------------------------------------------*/
825 /* reverseSyms - reverses the links for a symbol chain */
826 /*------------------------------------------------------------------*/
828 reverseSyms (symbol * sym)
830 symbol *prev, *curr, *next;
845 sym->next = (void *) NULL;
849 /*------------------------------------------------------------------*/
850 /* reverseLink - reverses the links for a type chain */
851 /*------------------------------------------------------------------*/
853 reverseLink (sym_link * type)
855 sym_link *prev, *curr, *next;
870 type->next = (void *) NULL;
874 /*------------------------------------------------------------------*/
875 /* addSymChain - adds a symbol chain to the symboltable */
876 /*------------------------------------------------------------------*/
878 addSymChain (symbol * symHead)
880 symbol *sym = symHead;
884 for (; sym != NULL; sym = sym->next)
888 /* if already exists in the symbol table then check if
889 the previous was an extern definition if yes then
890 then check if the type match, if the types match then
891 delete the current entry and add the new entry */
892 if ((csym = findSymWithLevel (SymbolTab, sym)) &&
893 csym->level == sym->level)
896 /* previous definition extern ? */
897 if (1 || IS_EXTERN (csym->etype))
899 /* do types match ? */
900 if (checkType (csym->type, sym->type) != 1)
902 werror (E_DUPLICATE, csym->name);
904 /* delete current entry */
905 deleteSym (SymbolTab, csym, csym->name);
907 addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
909 else /* not extern */
910 werror (E_DUPLICATE, sym->name);
914 /* check if previously defined */
915 if (csym && csym->level == sym->level)
917 /* if the previous one was declared as extern */
918 /* then check the type with the current one */
919 if (IS_EXTERN (csym->etype))
921 if (checkType (csym->type, sym->type) <= 0)
922 werror (W_EXTERN_MISMATCH, csym->name);
925 addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
930 /*------------------------------------------------------------------*/
931 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
932 /*------------------------------------------------------------------*/
934 funcInChain (sym_link * lnk)
945 /*------------------------------------------------------------------*/
946 /* structElemType - returns the type info of a sturct member */
947 /*------------------------------------------------------------------*/
949 structElemType (sym_link * stype, value * id, value ** argsp)
951 symbol *fields = (SPEC_STRUCT (stype) ? SPEC_STRUCT (stype)->fields : NULL);
952 sym_link *type, *etype;
953 sym_link *petype = getSpec (stype);
958 /* look for the id */
961 if (strcmp (fields->rname, id->name) == 0)
965 *argsp = fields->args;
967 type = copyLinkChain (fields->type);
968 etype = getSpec (type);
969 SPEC_SCLS (etype) = (SPEC_SCLS (petype) == S_REGISTER ?
970 SPEC_SCLS (etype) : SPEC_SCLS (petype));
973 fields = fields->next;
975 werror (E_NOT_MEMBER, id->name);
980 /*------------------------------------------------------------------*/
981 /* getStructElement - returns element of a tructure definition */
982 /*------------------------------------------------------------------*/
984 getStructElement (structdef * sdef, symbol * sym)
988 for (field = sdef->fields; field; field = field->next)
989 if (strcmp (field->name, sym->name) == 0)
992 werror (E_NOT_MEMBER, sym->name);
997 /*------------------------------------------------------------------*/
998 /* compStructSize - computes the size of a structure */
999 /*------------------------------------------------------------------*/
1001 compStructSize (int su, structdef * sdef)
1003 int sum = 0, usum = 0;
1007 /* for the identifiers */
1008 loop = sdef->fields;
1011 /* create the internal name for this variable */
1012 sprintf (loop->rname, "_%s", loop->name);
1013 loop->offset = (su == UNION ? sum = 0 : sum);
1014 SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
1016 /* if this is a bit field */
1019 /* change it to a unsigned bit */
1020 SPEC_NOUN (loop->etype) = V_BIT;
1021 SPEC_USIGN (loop->etype) = 1;
1022 /* check if this fit into the remaining */
1023 /* bits of this byte else align it to the */
1024 /* next byte boundary */
1025 if ((SPEC_BLEN (loop->etype) = loop->bitVar) <= (8 - bitOffset)) {
1026 SPEC_BSTR (loop->etype) = bitOffset;
1027 if ((bitOffset += (loop->bitVar % 8)) == 8)
1030 else /* does not fit */ {
1032 SPEC_BSTR (loop->etype) = bitOffset;
1033 sum += (loop->bitVar / 8);
1034 bitOffset += (loop->bitVar % 8);
1036 /* if this is the last field then pad */
1037 if (!loop->next && bitOffset && bitOffset != 8) {
1044 sum += getSize (loop->type);
1047 /* if function then do the arguments for it */
1048 if (funcInChain (loop->type)) {
1049 processFuncArgs (loop, 1);
1054 /* if this is not a bitfield but the */
1055 /* previous one was and did not take */
1056 /* the whole byte then pad the rest */
1057 if ((loop && !loop->bitVar) && bitOffset) {
1062 /* if union then size = sizeof larget field */
1064 usum = max (usum, sum);
1068 return (su == UNION ? usum : sum);
1071 /*------------------------------------------------------------------*/
1072 /* checkSClass - check the storage class specification */
1073 /*------------------------------------------------------------------*/
1075 checkSClass (symbol * sym)
1077 /* type is literal can happen foe enums change
1079 if (SPEC_SCLS (sym->etype) == S_LITERAL && !SPEC_ENUM (sym->etype))
1080 SPEC_SCLS (sym->etype) = S_AUTO;
1082 /* if sfr or sbit then must also be */
1083 /* volatile the initial value will be xlated */
1084 /* to an absolute address */
1085 if (SPEC_SCLS (sym->etype) == S_SBIT ||
1086 SPEC_SCLS (sym->etype) == S_SFR)
1088 SPEC_VOLATILE (sym->etype) = 1;
1089 /* if initial value given */
1092 SPEC_ABSA (sym->etype) = 1;
1093 SPEC_ADDR (sym->etype) =
1094 (int) list2int (sym->ival);
1099 /* if absolute address given then it mark it as
1101 if (IS_ABSOLUTE (sym->etype))
1102 SPEC_VOLATILE (sym->etype) = 1;
1104 /* global variables declared const put into code */
1105 if (sym->level == 0 &&
1106 SPEC_SCLS (sym->etype) == S_CONSTANT)
1108 SPEC_SCLS (sym->etype) = S_CODE;
1109 SPEC_CONST (sym->etype) = 1;
1112 /* global variable in code space is a constant */
1113 if (sym->level == 0 &&
1114 SPEC_SCLS (sym->etype) == S_CODE &&
1116 SPEC_CONST (sym->etype) = 1;
1119 /* if bit variable then no storage class can be */
1120 /* specified since bit is already a storage */
1121 if (IS_BITVAR (sym->etype) &&
1122 (SPEC_SCLS (sym->etype) != S_FIXED &&
1123 SPEC_SCLS (sym->etype) != S_SBIT &&
1124 SPEC_SCLS (sym->etype) != S_BIT)
1127 werror (E_BITVAR_STORAGE, sym->name);
1128 SPEC_SCLS (sym->etype) = S_FIXED;
1131 /* extern variables cannot be initialized */
1132 if (IS_EXTERN (sym->etype) && sym->ival)
1134 werror (E_EXTERN_INIT, sym->name);
1138 /* if this is an automatic symbol then */
1139 /* storage class will be ignored and */
1140 /* symbol will be allocated on stack/ */
1141 /* data depending on flag */
1143 (options.stackAuto || reentrant) &&
1144 (SPEC_SCLS (sym->etype) != S_AUTO &&
1145 SPEC_SCLS (sym->etype) != S_FIXED &&
1146 SPEC_SCLS (sym->etype) != S_REGISTER &&
1147 SPEC_SCLS (sym->etype) != S_STACK &&
1148 SPEC_SCLS (sym->etype) != S_XSTACK &&
1149 SPEC_SCLS (sym->etype) != S_CONSTANT))
1151 werror (E_AUTO_ASSUMED, sym->name);
1152 SPEC_SCLS (sym->etype) = S_AUTO;
1155 /* automatic symbols cannot be given */
1156 /* an absolute address ignore it */
1158 SPEC_ABSA (sym->etype) &&
1159 (options.stackAuto || reentrant))
1161 werror (E_AUTO_ABSA, sym->name);
1162 SPEC_ABSA (sym->etype) = 0;
1165 /* arrays & pointers cannot be defined for bits */
1166 /* SBITS or SFRs or BIT */
1167 if ((IS_ARRAY (sym->type) || IS_PTR (sym->type)) &&
1168 (SPEC_NOUN (sym->etype) == V_BIT ||
1169 SPEC_NOUN (sym->etype) == V_SBIT ||
1170 SPEC_SCLS (sym->etype) == S_SFR))
1171 werror (E_BIT_ARRAY, sym->name);
1173 /* if this is a bit|sbit then set length & start */
1174 if (SPEC_NOUN (sym->etype) == V_BIT ||
1175 SPEC_NOUN (sym->etype) == V_SBIT)
1177 SPEC_BLEN (sym->etype) = 1;
1178 SPEC_BSTR (sym->etype) = 0;
1181 /* variables declared in CODE space must have */
1182 /* initializers if not an extern */
1183 if (SPEC_SCLS (sym->etype) == S_CODE &&
1184 sym->ival == NULL &&
1186 port->mem.code_ro &&
1187 !IS_EXTERN (sym->etype) &&
1188 !funcInChain (sym->type))
1189 werror (E_CODE_NO_INIT, sym->name);
1191 /* if parameter or local variable then change */
1192 /* the storage class to reflect where the var will go */
1193 if (sym->level && SPEC_SCLS (sym->etype) == S_FIXED &&
1194 !IS_STATIC(sym->etype))
1196 if (options.stackAuto || (currFunc && IS_RENT (currFunc->etype)))
1198 SPEC_SCLS (sym->etype) = (options.useXstack ?
1199 S_XSTACK : S_STACK);
1203 /* hack-o-matic! I see no reason why the useXstack option should ever
1204 * control this allcoation, but the code was originally that way, and
1205 * changing it for non-390 ports breaks the compiler badly.
1207 bool useXdata = TARGET_IS_DS390 ? 1 : options.useXstack;
1208 SPEC_SCLS (sym->etype) = (useXdata ?
1214 /*------------------------------------------------------------------*/
1215 /* changePointer - change pointer to functions */
1216 /*------------------------------------------------------------------*/
1218 changePointer (symbol * sym)
1222 /* go thru the chain of declarations */
1223 /* if we find a pointer to a function */
1224 /* unconditionally change it to a ptr */
1226 for (p = sym->type; p; p = p->next)
1228 if (!IS_SPEC (p) && DCL_TYPE (p) == UPOINTER)
1229 DCL_TYPE (p) = GPOINTER;
1230 if (IS_PTR (p) && IS_FUNC (p->next))
1231 DCL_TYPE (p) = CPOINTER;
1235 /*------------------------------------------------------------------*/
1236 /* checkDecl - does semantic validation of a declaration */
1237 /*------------------------------------------------------------------*/
1239 checkDecl (symbol * sym)
1242 checkSClass (sym); /* check the storage class */
1243 changePointer (sym); /* change pointers if required */
1245 /* if this is an array without any dimension
1246 then update the dimension from the initial value */
1247 if (IS_ARRAY (sym->type) && !DCL_ELEM (sym->type))
1248 DCL_ELEM (sym->type) = getNelements (sym->type, sym->ival);
1253 /*------------------------------------------------------------------*/
1254 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
1255 /*------------------------------------------------------------------*/
1257 copyLinkChain (sym_link * p)
1259 sym_link *head, *curr, *loop;
1262 head = loop = (curr ? newLink () : (void *) NULL);
1265 memcpy (loop, curr, sizeof (sym_link)); /* copy it */
1266 loop->next = (curr->next ? newLink () : (void *) NULL);
1275 /*------------------------------------------------------------------*/
1276 /* cleanUpBlock - cleansup the symbol table specified for all the */
1277 /* symbols in the given block */
1278 /*------------------------------------------------------------------*/
1280 cleanUpBlock (bucket ** table, int block)
1285 /* go thru the entire table */
1286 for (i = 0; i < 256; i++)
1288 for (chain = table[i]; chain; chain = chain->next)
1290 if (chain->block >= block)
1292 deleteSym (table, chain->sym, chain->name);
1298 /*------------------------------------------------------------------*/
1299 /* cleanUpLevel - cleansup the symbol table specified for all the */
1300 /* symbols in the given level */
1301 /*------------------------------------------------------------------*/
1303 cleanUpLevel (bucket ** table, int level)
1308 /* go thru the entire table */
1309 for (i = 0; i < 256; i++)
1311 for (chain = table[i]; chain; chain = chain->next)
1313 if (chain->level >= level)
1315 deleteSym (table, chain->sym, chain->name);
1321 /*------------------------------------------------------------------*/
1322 /* computeType - computes the resultant type from two types */
1323 /*------------------------------------------------------------------*/
1325 computeType (sym_link * type1, sym_link * type2)
1329 sym_link *etype1 = getSpec (type1);
1330 sym_link *etype2 = getSpec (type2);
1332 /* if one of them is a float then result is a float */
1333 /* here we assume that the types passed are okay */
1334 /* and can be cast to one another */
1335 /* which ever is greater in size */
1336 if (IS_FLOAT (etype1) || IS_FLOAT (etype2))
1337 rType = newFloatLink ();
1339 /* if only one of them is a bit variable
1340 then the other one prevails */
1341 if (IS_BITVAR (etype1) && !IS_BITVAR (etype2))
1342 rType = copyLinkChain (type2);
1343 else if (IS_BITVAR (etype2) && !IS_BITVAR (etype1))
1344 rType = copyLinkChain (type1);
1346 /* if one of them is a pointer then that
1349 rType = copyLinkChain (type1);
1350 else if (IS_PTR (type2))
1351 rType = copyLinkChain (type2);
1352 else if (getSize (type1) > getSize (type2))
1353 rType = copyLinkChain (type1);
1355 rType = copyLinkChain (type2);
1357 reType = getSpec (rType);
1359 /* if either of them unsigned then make this unsigned */
1360 if ((SPEC_USIGN (etype1) || SPEC_USIGN (etype2)) && !IS_FLOAT (reType))
1361 SPEC_USIGN (reType) = 1;
1363 /* if result is a literal then make not so */
1364 if (IS_LITERAL (reType))
1365 SPEC_SCLS (reType) = S_REGISTER;
1370 /*------------------------------------------------------------------*/
1371 /* checkType - will do type check return 1 if match */
1372 /*------------------------------------------------------------------*/
1374 checkType (sym_link * dest, sym_link * src)
1385 /* if dest is a declarator then */
1390 if (DCL_TYPE (src) == DCL_TYPE (dest))
1391 return checkType (dest->next, src->next);
1392 else if (IS_PTR (src) && IS_PTR (dest))
1394 else if (IS_PTR (dest) && IS_ARRAY (src))
1396 else if (IS_PTR (dest) && IS_FUNC (dest->next) && IS_FUNC (src))
1397 return -1 * checkType (dest->next, src);
1401 else if (IS_PTR (dest) && IS_INTEGRAL (src))
1407 /* if one is a specifier and the other is not */
1408 if ((IS_SPEC (src) && !IS_SPEC (dest)) ||
1409 (IS_SPEC (dest) && !IS_SPEC (src)))
1412 /* if one of them is a void then ok */
1413 if (SPEC_NOUN (dest) == V_VOID &&
1414 SPEC_NOUN (src) != V_VOID)
1417 if (SPEC_NOUN (dest) != V_VOID &&
1418 SPEC_NOUN (src) == V_VOID)
1421 /* char === to short */
1422 if (SPEC_NOUN (dest) == V_CHAR &&
1423 SPEC_NOUN (src) == V_INT &&
1425 return (SPEC_USIGN (src) == SPEC_USIGN (dest) ? 1 : -2);
1427 if (SPEC_NOUN (src) == V_CHAR &&
1428 SPEC_NOUN (dest) == V_INT &&
1430 return (SPEC_USIGN (src) == SPEC_USIGN (dest) ? 1 : -2);
1432 /* if they are both bitfields then if the lengths
1433 and starts don't match */
1434 if (IS_BITFIELD (dest) && IS_BITFIELD (src) &&
1435 (SPEC_BLEN (dest) != SPEC_BLEN (src) ||
1436 SPEC_BSTR (dest) != SPEC_BSTR (src)))
1439 /* it is a specifier */
1440 if (SPEC_NOUN (dest) != SPEC_NOUN (src))
1442 if (SPEC_USIGN (dest) == SPEC_USIGN (src) &&
1443 IS_INTEGRAL (dest) && IS_INTEGRAL (src) &&
1444 getSize (dest) == getSize (src))
1446 else if (IS_ARITHMETIC (dest) && IS_ARITHMETIC (src))
1451 else if (IS_STRUCT (dest))
1453 if (SPEC_STRUCT (dest) != SPEC_STRUCT (src))
1458 if (SPEC_LONG (dest) != SPEC_LONG (src))
1461 if (SPEC_SHORT (dest) != SPEC_SHORT (src))
1464 if (SPEC_USIGN (dest) != SPEC_USIGN (src))
1470 /*------------------------------------------------------------------*/
1471 /* inCalleeSaveList - return 1 if found in calle save list */
1472 /*------------------------------------------------------------------*/
1474 inCalleeSaveList (char *s)
1478 for (i = 0; options.calleeSaves[i]; i++)
1479 if (strcmp (options.calleeSaves[i], s) == 0)
1485 /*-----------------------------------------------------------------*/
1486 /* aggregateArgToPointer: change an agggregate type function */
1487 /* argument to a pointer to that type. */
1488 /*-----------------------------------------------------------------*/
1490 aggregateArgToPointer (value * val)
1492 if (IS_AGGREGATE (val->type))
1494 /* if this is a structure */
1495 /* then we need to add a new link */
1496 if (IS_STRUCT (val->type))
1498 /* first lets add DECLARATOR type */
1499 sym_link *p = val->type;
1501 werror (W_STRUCT_AS_ARG, val->name);
1502 val->type = newLink ();
1503 val->type->next = p;
1506 /* change to a pointer depending on the */
1507 /* storage class specified */
1508 switch (SPEC_SCLS (val->etype))
1511 DCL_TYPE (val->type) = IPOINTER;
1514 DCL_TYPE (val->type) = PPOINTER;
1517 if (TARGET_IS_DS390)
1519 /* The AUTO and REGISTER classes should probably
1520 * also become generic pointers, but I haven't yet
1521 * devised a test case for that.
1523 DCL_TYPE (val->type) = GPOINTER;
1530 DCL_TYPE (val->type) = POINTER;
1533 DCL_TYPE (val->type) = CPOINTER;
1536 DCL_TYPE (val->type) = FPOINTER;
1539 DCL_TYPE (val->type) = EEPPOINTER;
1542 DCL_TYPE (val->type) = GPOINTER;
1545 /* is there is a symbol associated then */
1546 /* change the type of the symbol as well */
1549 val->sym->type = copyLinkChain (val->type);
1550 val->sym->etype = getSpec (val->sym->type);
1554 /*------------------------------------------------------------------*/
1555 /* checkFunction - does all kinds of check on a function */
1556 /*------------------------------------------------------------------*/
1558 checkFunction (symbol * sym)
1561 value *exargs, *acargs;
1564 /* if not type then some kind of error */
1568 /* if the function has no type then make it return int */
1569 if (!sym->type->next)
1570 sym->type->next = sym->etype = newIntLink ();
1572 /* function cannot return aggregate */
1573 if (IS_AGGREGATE (sym->type->next))
1575 werror (E_FUNC_AGGR, sym->name);
1579 /* function cannot return bit */
1580 if (IS_BITVAR (sym->type->next))
1582 werror (E_FUNC_BIT, sym->name);
1586 /* check if this function is defined as calleeSaves
1587 then mark it as such */
1588 sym->calleeSave = inCalleeSaveList (sym->name);
1590 /* if interrupt service routine */
1591 /* then it cannot have arguments */
1592 if (sym->args && IS_ISR (sym->etype) && !IS_VOID (sym->args->type))
1594 werror (E_INT_ARGS, sym->name);
1598 if (!(csym = findSym (SymbolTab, sym, sym->name)))
1599 return 1; /* not defined nothing more to check */
1601 /* check if body already present */
1602 if (csym && csym->fbody)
1604 werror (E_FUNC_BODY, sym->name);
1608 /* check the return value type */
1609 if (checkType (csym->type, sym->type) <= 0)
1611 werror (E_PREV_DEF_CONFLICT, csym->name, "type");
1612 werror (E_CONTINUE, "previous definition type ");
1613 printTypeChain (csym->type, stderr);
1614 fprintf (stderr, "\n");
1615 werror (E_CONTINUE, "current definition type ");
1616 printTypeChain (sym->type, stderr);
1617 fprintf (stderr, "\n");
1621 if (SPEC_INTRTN (csym->etype) != SPEC_INTRTN (sym->etype))
1623 werror (E_PREV_DEF_CONFLICT, csym->name, "interrupt");
1627 if (SPEC_BANK (csym->etype) != SPEC_BANK (sym->etype))
1629 werror (E_PREV_DEF_CONFLICT, csym->name, "using");
1633 /* compare expected agrs with actual args */
1634 exargs = csym->args;
1637 /* for all the expected args do */
1640 exargs = exargs->next, acargs = acargs->next, argCnt++)
1643 /* If the actual argument is an array, any prototype
1644 * will have modified it to a pointer. Duplicate that
1647 if (IS_AGGREGATE (acargs->type))
1649 checkValue = copyValue (acargs);
1650 aggregateArgToPointer (checkValue);
1654 checkValue = acargs;
1657 if (checkType (exargs->type, checkValue->type) <= 0)
1659 werror (E_ARG_TYPE, argCnt);
1664 /* if one them ended we have a problem */
1665 if ((exargs && !acargs && !IS_VOID (exargs->type)) ||
1666 (!exargs && acargs && !IS_VOID (acargs->type)))
1667 werror (E_ARG_COUNT);
1669 /* replace with this defition */
1670 sym->cdef = csym->cdef;
1671 deleteSym (SymbolTab, csym, csym->name);
1672 addSym (SymbolTab, sym, sym->name, sym->level, sym->block);
1673 if (IS_EXTERN (csym->etype) && !
1674 IS_EXTERN (sym->etype))
1676 addSet (&publics, sym);
1681 /*-----------------------------------------------------------------*/
1682 /* processFuncArgs - does some processing with function args */
1683 /*-----------------------------------------------------------------*/
1685 processFuncArgs (symbol * func, int ignoreName)
1690 /* if this function has variable argument list */
1691 /* then make the function a reentrant one */
1693 SPEC_RENT (func->etype) = 1;
1695 /* check if this function is defined as calleeSaves
1696 then mark it as such */
1697 func->calleeSave = inCalleeSaveList (func->name);
1699 val = func->args; /* loop thru all the arguments */
1701 /* if it is void then remove parameters */
1702 if (val && IS_VOID (val->type))
1708 /* reset regparm for the port */
1709 (*port->reset_regparms) ();
1710 /* if any of the arguments is an aggregate */
1711 /* change it to pointer to the same type */
1714 /* mark it as a register parameter if
1715 the function does not have VA_ARG
1716 and as port dictates */
1717 if (!func->hasVargs &&
1718 (*port->reg_parm) (val->type))
1720 SPEC_REGPARM (val->etype) = 1;
1723 if (IS_AGGREGATE (val->type))
1725 aggregateArgToPointer (val);
1731 /* if this is an internal generated function call */
1733 /* ignore --stack-auto for this one, we don't know how it is compiled */
1734 /* simply trust on --int-long-reent or --float-reent */
1735 if (IS_RENT(func->etype)) {
1739 /* if this function is reentrant or */
1740 /* automatics r 2b stacked then nothing */
1741 if (IS_RENT (func->etype) || options.stackAuto)
1750 /* if a symbolname is not given */
1751 /* synthesize a variable name */
1755 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1756 val->sym = newSymbol (val->name, 1);
1757 SPEC_OCLS (val->etype) = port->mem.default_local_map;
1758 val->sym->type = copyLinkChain (val->type);
1759 val->sym->etype = getSpec (val->sym->type);
1760 val->sym->_isparm = 1;
1761 strcpy (val->sym->rname, val->name);
1762 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1763 SPEC_STAT (func->etype);
1764 addSymChain (val->sym);
1767 else /* symbol name given create synth name */
1770 sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
1771 strcpy (val->sym->rname, val->name);
1772 val->sym->_isparm = 1;
1773 SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
1774 (options.model != MODEL_SMALL ? xdata : data);
1775 SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
1776 SPEC_STAT (func->etype);
1782 /*-----------------------------------------------------------------*/
1783 /* isSymbolEqual - compares two symbols return 1 if they match */
1784 /*-----------------------------------------------------------------*/
1786 isSymbolEqual (symbol * dest, symbol * src)
1788 /* if pointers match then equal */
1792 /* if one of them is null then don't match */
1796 /* if both of them have rname match on rname */
1797 if (dest->rname[0] && src->rname[0])
1798 return (!strcmp (dest->rname, src->rname));
1800 /* otherwise match on name */
1801 return (!strcmp (dest->name, src->name));
1804 void PT(sym_link *type)
1806 printTypeChain(type,0);
1808 /*-----------------------------------------------------------------*/
1809 /* printTypeChain - prints the type chain in human readable form */
1810 /*-----------------------------------------------------------------*/
1812 printTypeChain (sym_link * type, FILE * of)
1826 if (DCL_PTR_VOLATILE(type)) {
1827 fprintf (of, "volatile ");
1829 switch (DCL_TYPE (type))
1832 fprintf (of, "function ");
1835 fprintf (of, "_generic * ");
1836 if (DCL_PTR_CONST (type))
1837 fprintf (of, "const ");
1840 fprintf (of, "_code * ");
1841 if (DCL_PTR_CONST (type))
1842 fprintf (of, "const ");
1845 fprintf (of, "_far * ");
1846 if (DCL_PTR_CONST (type))
1847 fprintf (of, "const ");
1850 fprintf (of, "_eeprom * ");
1851 if (DCL_PTR_CONST (type))
1852 fprintf (of, "const ");
1856 fprintf (of, "_near * ");
1857 if (DCL_PTR_CONST (type))
1858 fprintf (of, "const ");
1861 fprintf (of, "_idata *");
1862 if (DCL_PTR_CONST (type))
1863 fprintf (of, "const ");
1866 fprintf (of, "_pdata *");
1867 if (DCL_PTR_CONST (type))
1868 fprintf (of, "const ");
1871 fprintf (of, " _unkown *");
1872 if (DCL_PTR_CONST (type))
1873 fprintf (of, "const ");
1876 fprintf (of, "array of ");
1882 if (SPEC_VOLATILE (type))
1883 fprintf (of, "volatile ");
1884 if (SPEC_USIGN (type))
1885 fprintf (of, "unsigned ");
1886 if (SPEC_CONST (type))
1887 fprintf (of, "const ");
1889 switch (SPEC_NOUN (type))
1893 fprintf (of, "long ");
1894 if (IS_SHORT (type))
1895 fprintf (of, "short ");
1896 fprintf (of, "int ");
1900 fprintf (of, "char ");
1904 fprintf (of, "void ");
1908 fprintf (of, "float ");
1912 fprintf (of, "struct %s", SPEC_STRUCT (type)->tag);
1916 fprintf (of, "sbit ");
1920 fprintf (of, "bit {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type));
1924 fprintf (of, "double ");
1928 fprintf (of, "unknown type ");
1938 /*-----------------------------------------------------------------*/
1939 /* cdbTypeInfo - print the type information for debugger */
1940 /*-----------------------------------------------------------------*/
1942 cdbTypeInfo (sym_link * type, FILE * of)
1944 fprintf (of, "{%d}", getSize (type));
1949 switch (DCL_TYPE (type))
1952 fprintf (of, "DF,");
1955 fprintf (of, "DG,");
1958 fprintf (of, "DC,");
1961 fprintf (of, "DX,");
1964 fprintf (of, "DD,");
1967 fprintf (of, "DI,");
1970 fprintf (of, "DP,");
1973 fprintf (of, "DA,");
1976 fprintf (of, "DA%d,", DCL_ELEM (type));
1984 switch (SPEC_NOUN (type))
1989 else if (IS_SHORT (type))
2008 fprintf (of, "ST%s", SPEC_STRUCT (type)->tag);
2016 fprintf (of, "SB%d$%d", SPEC_BSTR (type), SPEC_BLEN (type));
2023 if (SPEC_USIGN (type))
2031 /*-----------------------------------------------------------------*/
2032 /* cdbSymbol - prints a symbol & its type information for debugger */
2033 /*-----------------------------------------------------------------*/
2035 cdbSymbol (symbol * sym, FILE * of, int isStructSym, int isFunc)
2047 fprintf (of, "S:"); /* symbol record */
2048 /* if this is not a structure symbol then
2049 we need to figure out the scope information */
2055 if (IS_STATIC (sym->etype))
2056 fprintf (of, "F%s$", moduleName); /* scope is file */
2058 fprintf (of, "G$"); /* scope is global */
2061 /* symbol is local */
2062 fprintf (of, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
2065 fprintf (of, "S$"); /* scope is structure */
2067 /* print the name, & mangled name */
2068 fprintf (of, "%s$%d$%d(", sym->name,
2069 sym->level, sym->block);
2071 cdbTypeInfo (sym->type, of);
2074 /* print the address space */
2075 map = SPEC_OCLS (sym->etype);
2076 fprintf (of, "%c,%d,%d",
2077 (map ? map->dbName : 'Z'), sym->onStack, SPEC_STAK (sym->etype));
2079 /* if assigned to registers then output register names */
2080 /* if this is a function then print
2081 if is it an interrupt routine & interrupt number
2082 and the register bank it is using */
2084 fprintf (of, ",%d,%d,%d", SPEC_INTRTN (sym->etype),
2085 SPEC_INTN (sym->etype), SPEC_BANK (sym->etype));
2086 /* alternate location to find this symbol @ : eg registers
2093 /*-----------------------------------------------------------------*/
2094 /* cdbStruct - print a structure for debugger */
2095 /*-----------------------------------------------------------------*/
2097 cdbStruct (structdef * sdef, int block, FILE * of,
2098 int inStruct, char *tag)
2103 /* if block # then must have function scope */
2104 fprintf (of, "F%s$", moduleName);
2105 fprintf (of, "%s[", (tag ? tag : sdef->tag));
2106 for (sym = sdef->fields; sym; sym = sym->next)
2108 fprintf (of, "({%d}", sym->offset);
2109 cdbSymbol (sym, of, TRUE, FALSE);
2117 /*------------------------------------------------------------------*/
2118 /* cdbStructBlock - calls struct printing for a blcks */
2119 /*------------------------------------------------------------------*/
2121 cdbStructBlock (int block, FILE * of)
2124 bucket **table = StructTab;
2128 /* go thru the entire table */
2129 for (i = 0; i < 256; i++)
2131 for (chain = table[i]; chain; chain = chain->next)
2133 if (chain->block >= block)
2135 cdbStruct ((structdef *) chain->sym, chain->block, of, 0, NULL);
2141 /*-----------------------------------------------------------------*/
2142 /* powof2 - returns power of two for the number if number is pow 2 */
2143 /*-----------------------------------------------------------------*/
2145 powof2 (unsigned long num)
2158 if (n1s > 1 || nshifts == 0)
2174 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
2175 symbol *__muldiv[3][3][2];
2176 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
2177 sym_link *__multypes[3][2];
2178 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
2179 symbol *__conv[2][3][2];
2181 sym_link *floatType;
2184 _makeRegParam (symbol * sym)
2188 val = sym->args; /* loop thru all the arguments */
2190 /* reset regparm for the port */
2191 (*port->reset_regparms) ();
2194 SPEC_REGPARM (val->etype) = 1;
2199 /*-----------------------------------------------------------------*/
2200 /* initCSupport - create functions for C support routines */
2201 /*-----------------------------------------------------------------*/
2205 const char *smuldivmod[] =
2209 const char *sbwd[] =
2211 "char", "int", "long"
2218 int bwd, su, muldivmod, tofrom;
2220 floatType = newFloatLink ();
2222 for (bwd = 0; bwd < 3; bwd++)
2239 __multypes[bwd][0] = l;
2240 __multypes[bwd][1] = copyLinkChain (l);
2241 SPEC_USIGN (__multypes[bwd][1]) = 1;
2244 __fsadd = funcOfType ("__fsadd", floatType, floatType, 2, options.float_rent);
2245 __fssub = funcOfType ("__fssub", floatType, floatType, 2, options.float_rent);
2246 __fsmul = funcOfType ("__fsmul", floatType, floatType, 2, options.float_rent);
2247 __fsdiv = funcOfType ("__fsdiv", floatType, floatType, 2, options.float_rent);
2248 __fseq = funcOfType ("__fseq", CHARTYPE, floatType, 2, options.float_rent);
2249 __fsneq = funcOfType ("__fsneq", CHARTYPE, floatType, 2, options.float_rent);
2250 __fslt = funcOfType ("__fslt", CHARTYPE, floatType, 2, options.float_rent);
2251 __fslteq = funcOfType ("__fslteq", CHARTYPE, floatType, 2, options.float_rent);
2252 __fsgt = funcOfType ("__fsgt", CHARTYPE, floatType, 2, options.float_rent);
2253 __fsgteq = funcOfType ("__fsgteq", CHARTYPE, floatType, 2, options.float_rent);
2255 for (tofrom = 0; tofrom < 2; tofrom++)
2257 for (bwd = 0; bwd < 3; bwd++)
2259 for (su = 0; su < 2; su++)
2263 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
2264 __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
2268 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
2269 __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
2275 for (muldivmod = 0; muldivmod < 3; muldivmod++)
2277 for (bwd = 0; bwd < 3; bwd++)
2279 for (su = 0; su < 2; su++)
2281 sprintf (buffer, "_%s%s%s",
2282 smuldivmod[muldivmod],
2285 __muldiv[muldivmod][bwd][su] = funcOfType (buffer, __multypes[bwd][su], __multypes[bwd][su], 2, options.intlong_rent);
2286 SPEC_NONBANKED (__muldiv[muldivmod][bwd][su]->etype) = 1;
2287 if (bwd < port->muldiv.force_reg_param_below)
2288 _makeRegParam (__muldiv[muldivmod][bwd][su]);