1 /*-------------------------------------------------------------------------
2 SDCCsymt.h - Header 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 #define MAX_NEST_LEVEL 256
28 #define SDCC_SYMNAME_MAX 64
29 #define SDCC_NAME_MAX 3*SDCC_SYMNAME_MAX // big enough for _<func>_<var>_etc
30 #include "SDCChasht.h"
54 #define HASHTAB_SIZE 256
56 /* hash table bucket */
59 void *sym; /* pointer to the object */
60 char name[SDCC_NAME_MAX + 1]; /* name of this symbol */
61 int level; /* nest level for this symbol */
62 int block; /* belongs to which block */
63 struct bucket *prev; /* ptr 2 previous bucket */
64 struct bucket *next; /* ptr 2 next bucket */
68 typedef struct structdef
70 char tag[SDCC_NAME_MAX + 1]; /* tag part of structure */
71 unsigned char level; /* Nesting level */
72 struct symbol *fields; /* pointer to fields */
73 unsigned size; /* sizeof the table in bytes */
77 /* noun definitions */
113 /* specifier is the last in the type-chain */
114 typedef struct specifier
116 NOUN noun; /* CHAR INT STRUCTURE LABEL */
117 STORAGE_CLASS sclass; /* REGISTER,AUTO,FIX,CONSTANT */
118 struct memmap *oclass; /* output storage class */
119 unsigned _long:1; /* 1=long */
120 unsigned _short:1; /* 1=short int */
121 unsigned _unsigned:1; /* 1=unsigned, 0=signed */
122 unsigned _signed:1; /* just for sanity checks only*/
123 unsigned _static:1; /* 1=static keyword found */
124 unsigned _extern:1; /* 1=extern found */
125 unsigned _absadr:1; /* absolute address specfied */
126 unsigned _volatile:1; /* is marked as volatile */
127 unsigned _const:1; /* is a constant */
128 unsigned _typedef:1; /* is typedefed */
129 unsigned _isregparm:1; /* is the first parameter */
130 unsigned _isenum:1; /* is an enumerated type */
131 unsigned _addr; /* address of symbol */
132 unsigned _stack; /* stack offset for stacked v */
133 unsigned _bitStart; /* bit start position */
134 int _bitLength; /* bit length */
137 { /* Values if constant or enum */
138 short int v_int; /* int and char values */
139 char *v_char; /* character string */
140 unsigned short v_uint; /* unsigned int const value */
141 long v_long; /* long constant value */
142 unsigned long v_ulong; /* unsigned long constant val */
143 double v_float; /* floating point constant value */
144 struct symbol *v_enum; /* ptr 2 enum_list if enum==1 */
147 struct structdef *v_struct; /* structure pointer */
151 /* types of declarators */
154 POINTER = 0, /* pointer to near data */
155 FPOINTER, /* pointer to far data */
156 CPOINTER, /* pointer to code space */
157 GPOINTER, /* _generic pointer */
158 PPOINTER, /* paged area pointer */
159 IPOINTER, /* pointer to upper 128 bytes */
160 UPOINTER, /* unknown pointer used only when parsing */
161 EEPPOINTER, /* pointer to eeprom */
167 typedef struct declarator
169 DECLARATOR_TYPE dcl_type; /* POINTER,ARRAY or FUNCTION */
170 unsigned int num_elem; /* # of elems if type==array */
171 short ptr_const:1; /* pointer is constant */
172 short ptr_volatile:1; /* pointer is volatile */
173 struct sym_link *tspec; /* pointer type specifier */
180 typedef struct sym_link
182 unsigned class:1; /* DECLARATOR or SPECIFIER */
183 unsigned tdef:1; /* current link created by */
184 /* typedef if this flag is set */
187 specifier s; /* if CLASS == SPECIFIER */
188 declarator d; /* if CLASS == DECLARATOR */
191 /* function attributes */
193 struct value *args; /* the defined arguments */
194 unsigned hasVargs:1; /* functions has varargs */
195 unsigned calleeSaves:1; /* functions uses callee save */
196 unsigned hasbody:1; /* function body defined */
197 //unsigned ret:1; /* return statement for a function */
198 unsigned hasFcall:1; /* does it call other functions */
199 unsigned reent:1; /* function is reentrant */
200 unsigned naked:1; /* naked function */
202 unsigned nonbanked:1; /* function has the nonbanked attribute */
203 unsigned banked:1; /* function has the banked attribute */
204 unsigned critical:1; /* critical function */
205 unsigned intrtn:1; /* this is an interrupt routin */
206 unsigned rbank:1; /* seperate register bank */
207 unsigned intno; /* 1=Interrupt svc routine */
208 unsigned regbank; /* register bank 2b used */
209 unsigned builtin; /* is a builtin function */
212 struct sym_link *next; /* next element on the chain */
216 typedef struct symbol
218 char name[SDCC_SYMNAME_MAX + 1]; /* Input Variable Name */
219 char rname[SDCC_NAME_MAX + 1]; /* internal name */
221 short level; /* declration lev,fld offset */
222 short block; /* sequential block # of defintion */
224 unsigned implicit:1; /* implicit flag */
225 unsigned undefined:1; /* undefined variable */
226 unsigned _isparm:1; /* is a parameter */
227 unsigned ismyparm:1; /* is parameter of the function being generated */
228 unsigned isitmp:1; /* is an intermediate temp */
229 unsigned islbl:1; /* is a temporary label */
230 unsigned isref:1; /* has been referenced */
231 unsigned isind:1; /* is a induction variable */
232 unsigned isinvariant:1; /* is a loop invariant */
233 unsigned isstrlit:1; /* is a string literal */
234 unsigned cdef:1; /* compiler defined symbol */
235 unsigned addrtaken:1; /* address of the symbol was taken */
236 unsigned isreqv:1; /* is the register quivalent of a symbol */
237 unsigned udChked:1; /* use def checking has been already done */
239 /* following flags are used by the backend
240 for code generation and can be changed
241 if a better scheme for backend is thought of */
242 unsigned isLiveFcall:1; /* is live at or across a function call */
243 unsigned isspilt:1; /* has to be spilt */
244 unsigned spillA:1; /* spilt be register allocator */
245 unsigned remat:1; /* can be remateriazed */
246 unsigned isptr:1; /* is a pointer */
247 unsigned uptr:1; /* used as a pointer */
248 unsigned isFree:1; /* used by register allocator */
249 unsigned islocal:1; /* is a local variable */
250 unsigned blockSpil:1; /* spilt at block level */
251 unsigned remainSpil:1; /* spilt because not used in remainder */
252 unsigned stackSpil:1; /* has been spilt on temp stack location */
253 unsigned onStack:1; /* this symbol allocated on the stack */
254 unsigned iaccess:1; /* indirect access */
255 unsigned ruonly:1; /* used in return statement only */
256 unsigned spildir:1; /* spilt in direct space */
257 unsigned ptrreg:1; /* this symbol assigned to a ptr reg */
258 unsigned noSpilLoc:1; /* cannot be assigned a spil location */
259 unsigned accuse; /* can be left in the accumulator
260 On the Z80 accuse is devided into
261 ACCUSE_A and ACCUSE_HL as the idea
265 int allocreq ; /* allocation is required for this variable */
266 int stack; /* offset on stack */
267 int xstack; /* offset on xternal stack */
268 short nRegs; /* number of registers required */
269 short regType; /* type of register required */
271 struct regs *regs[4]; /* can have at the most 4 registers */
272 struct asmop *aop; /* asmoperand for this symbol */
273 struct iCode *fuse; /* furthest use */
274 struct iCode *rematiCode; /* rematerialse with which instruction */
275 struct operand *reqv; /* register equivalent of a local variable */
278 struct symbol *spillLoc; /* register spil location */
279 struct set *itmpStack; /* symbols spilt @ this stack location */
282 short bitVar; /* this is a bit variable */
283 unsigned offset; /* offset from top if struct */
285 int lineDef; /* defined line number */
286 int lastLine; /* for functions the last line */
287 struct sym_link *type; /* 1st link to declator chain */
288 struct sym_link *etype; /* last link to declarator chn */
289 struct symbol *next; /* crosslink to next symbol */
290 struct symbol *localof; /* local variable of which function */
291 struct initList *ival; /* ptr to initializer if any */
292 struct bitVect *defs; /* bit vector for definitions */
293 struct bitVect *uses; /* bit vector for uses */
294 struct bitVect *regsUsed; /* for functions registers used */
295 int liveFrom; /* live from iCode sequence number */
296 int liveTo; /* live to sequence number */
297 int used; /* no. of times this was used */
298 int recvSize; /* size of first argument */
299 struct bitVect *clashes; /* overlaps with what other symbols */
303 /* Easy Access Macros */
304 #define DCL_TYPE(l) l->select.d.dcl_type
305 #define DCL_ELEM(l) l->select.d.num_elem
306 #define DCL_PTR_CONST(l) l->select.d.ptr_const
307 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
308 #define DCL_TSPEC(l) l->select.d.tspec
310 #define FUNC_DEBUG //assert(IS_FUNC(x));
311 #define FUNC_HASVARARGS(x) (x->funcAttrs.hasVargs)
312 #define IFFUNC_HASVARARGS(x) (IS_FUNC(x) && FUNC_HASVARARGS(x))
313 #define FUNC_ARGS(x) (x->funcAttrs.args)
314 #define IFFUNC_ARGS(x) (IS_FUNC(x) && FUNC_ARGS(x))
315 #define FUNC_HASFCALL(x) (x->funcAttrs.hasFcall)
316 #define IFFUNC_HASFCALL(x) (IS_FUNC(x) && FUNC_HASFCALL(x))
317 #define FUNC_HASBODY(x) (x->funcAttrs.hasbody)
318 #define IFFUNC_HASBODY(x) (IS_FUNC(x) && FUNC_HASBODY(x))
319 #define FUNC_CALLEESAVES(x) (x->funcAttrs.calleeSaves)
320 #define IFFUNC_CALLEESAVES(x) (IS_FUNC(x) && FUNC_CALLEESAVES(x))
321 #define FUNC_ISISR(x) (x->funcAttrs.intrtn)
322 #define IFFUNC_ISISR(x) (IS_FUNC(x) && FUNC_ISISR(x))
323 #define IFFUNC_RBANK(x) (IS_FUNC(x) && FUNC_RBANK(x))
324 #define FUNC_INTNO(x) (x->funcAttrs.intno)
325 #define FUNC_REGBANK(x) (x->funcAttrs.regbank)
327 #define FUNC_ISREENT(x) (x->funcAttrs.reent)
328 #define IFFUNC_ISREENT(x) (IS_FUNC(x) && FUNC_ISREENT(x))
329 #define FUNC_ISNAKED(x) (x->funcAttrs.naked)
330 #define IFFUNC_ISNAKED(x) (IS_FUNC(x) && FUNC_ISNAKED(x))
331 #define FUNC_NONBANKED(x) (x->funcAttrs.nonbanked)
332 #define IFFUNC_NONBANKED(x) (IS_FUNC(x) && FUNC_NONBANKED(x))
333 #define FUNC_BANKED(x) (x->funcAttrs.banked)
334 #define IFFUNC_BANKED(x) (IS_FUNC(x) && FUNC_BANKED(x))
335 #define FUNC_ISCRITICAL(x) (x->funcAttrs.critical)
336 #define IFFUNC_ISCRITICAL(x) (IS_FUNC(x) && FUNC_ISCRITICAL(x))
337 #define FUNC_ISBUILTIN(x) (x->funcAttrs.builtin)
338 #define IFFUNC_ISBUILTIN(x) (IS_FUNC(x) && FUNC_ISBUILTIN(x))
340 // jwk: I am not sure about this
341 #define IFFUNC_ISBANKEDCALL(x) (!IFFUNC_NONBANKED(x) && \
342 (options.model == MODEL_LARGE || \
343 options.model == MODEL_MEDIUM || \
346 #define SPEC_NOUN(x) x->select.s.noun
347 #define SPEC_LONG(x) x->select.s._long
348 #define SPEC_USIGN(x) x->select.s._unsigned
349 #define SPEC_SCLS(x) x->select.s.sclass
350 #define SPEC_ENUM(x) x->select.s._isenum
351 #define SPEC_OCLS(x) x->select.s.oclass
352 #define SPEC_STAT(x) x->select.s._static
353 #define SPEC_EXTR(x) x->select.s._extern
354 #define SPEC_CODE(x) x->select.s._codesg
355 #define SPEC_ABSA(x) x->select.s._absadr
356 #define SPEC_BANK(x) x->select.s._regbank
357 #define SPEC_ADDR(x) x->select.s._addr
358 #define SPEC_STAK(x) x->select.s._stack
359 #define SPEC_CVAL(x) x->select.s.const_val
360 #define SPEC_BSTR(x) x->select.s._bitStart
361 #define SPEC_BLEN(x) x->select.s._bitLength
363 /* Sleaze: SPEC_ISR_SAVED_BANKS is only used on
364 * function type symbols, which obviously cannot
365 * be of BIT type. Therefore, we recycle the
366 * _bitStart field instead of defining a new field.
368 #define SPEC_ISR_SAVED_BANKS(x) x->select.s._bitStart
369 #define SPEC_VOLATILE(x) x->select.s._volatile
370 #define SPEC_CONST(x) x->select.s._const
371 #define SPEC_STRUCT(x) x->select.s.v_struct
372 #define SPEC_TYPEDEF(x) x->select.s._typedef
373 #define SPEC_REGPARM(x) x->select.s._isregparm
375 /* type check macros */
376 #define IS_DECL(x) ( x && x->class == DECLARATOR )
377 #define IS_SPEC(x) ( x && x->class == SPECIFIER )
378 #define IS_ARRAY(x) (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
379 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
380 #define IS_PTR(x) (IS_DECL(x) && (DCL_TYPE(x) == POINTER || \
381 DCL_TYPE(x) == FPOINTER || \
382 DCL_TYPE(x) == GPOINTER || \
383 DCL_TYPE(x) == IPOINTER || \
384 DCL_TYPE(x) == PPOINTER || \
385 DCL_TYPE(x) == EEPPOINTER || \
386 DCL_TYPE(x) == CPOINTER || \
387 DCL_TYPE(x) == UPOINTER ))
388 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
389 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
390 #define IS_CODEPTR(x) (IS_DECL(x) && DCL_TYPE(x) == CPOINTER)
391 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
392 #define IS_FUNC(x) (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
393 #define IS_LONG(x) (IS_SPEC(x) && x->select.s._long)
394 #define IS_UNSIGNED(x) (IS_SPEC(x) && x->select.s._unsigned)
395 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
396 #define IS_CONSTANT(x) (IS_SPEC(x) && ( x->select.s._const == 1))
397 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
398 #define IS_ABSOLUTE(x) (IS_SPEC(x) && x->select.s._absadr )
399 #define IS_REGISTER(x) (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
400 #define IS_RENT(x) (IS_SPEC(x) && x->select.s._reent )
401 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
402 #define IS_INT(x) (IS_SPEC(x) && x->select.s.noun == V_INT)
403 #define IS_VOID(x) (IS_SPEC(x) && x->select.s.noun == V_VOID)
404 #define IS_CHAR(x) (IS_SPEC(x) && x->select.s.noun == V_CHAR)
405 #define IS_EXTERN(x) (IS_SPEC(x) && x->select.s._extern)
406 #define IS_VOLATILE(x) (IS_SPEC(x) && x->select.s._volatile )
407 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT || \
408 x->select.s.noun == V_CHAR || \
409 x->select.s.noun == V_BIT || \
410 x->select.s.noun == V_SBIT ))
411 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
412 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT || \
413 x->select.s.noun == V_SBIT ))
414 #define IS_FLOAT(x) (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
415 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
416 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
417 #define IS_LITERAL(x) (IS_SPEC(x) && x->select.s.sclass == S_LITERAL)
418 #define IS_REGPARM(x) (IS_SPEC(x) && SPEC_REGPARM(x))
420 /* forward declaration for the global vars */
421 extern bucket *SymbolTab[];
422 extern bucket *StructTab[];
423 extern bucket *TypedefTab[];
424 extern bucket *LabelTab[];
425 extern bucket *enumTab[];
426 extern symbol *__fsadd;
427 extern symbol *__fssub;
428 extern symbol *__fsmul;
429 extern symbol *__fsdiv;
430 extern symbol *__fseq;
431 extern symbol *__fsneq;
432 extern symbol *__fslt;
433 extern symbol *__fslteq;
434 extern symbol *__fsgt;
435 extern symbol *__fsgteq;
437 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
438 extern symbol *__muldiv[3][3][2];
439 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
440 extern sym_link *__multypes[3][2];
441 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
442 extern symbol *__conv[2][3][2];
443 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
444 extern symbol *__rlrr[2][3][2];
446 #define CHARTYPE __multypes[0][0]
447 #define UCHARTYPE __multypes[0][1]
448 #define INTTYPE __multypes[1][0]
449 #define UINTTYPE __multypes[1][1]
450 #define LONGTYPE __multypes[2][0]
451 #define ULONGTYPE __multypes[2][1]
454 extern sym_link *floatType;
458 /* forward definitions for the symbol table related functions */
460 symbol *newSymbol (char *, int);
461 sym_link *newLink ();
462 sym_link *newFloatLink ();
463 structdef *newStruct (char *);
464 void addDecl (symbol *, int, sym_link *);
465 sym_link *mergeSpec (sym_link *, sym_link *, char *name);
466 sym_link *cloneSpec (sym_link *);
467 symbol *reverseSyms (symbol *);
468 sym_link *reverseLink (sym_link *);
469 symbol *copySymbol (symbol *);
470 symbol *copySymbolChain (symbol *);
471 void printSymChain (symbol *, int);
472 void printStruct (structdef *, int);
473 char *genSymName (int);
474 sym_link *getSpec (sym_link *);
475 char *genSymName (int);
476 int compStructSize (int, structdef *);
477 sym_link *copyLinkChain (sym_link *);
478 int checkDecl (symbol *, int);
479 void checkBasic (sym_link *, sym_link *);
480 value *checkPointerIval (sym_link *, value *);
481 value *checkStructIval (symbol *, value *);
482 value *checkArrayIval (sym_link *, value *);
483 value *checkIval (sym_link *, value *);
484 unsigned int getSize (sym_link *);
485 unsigned int bitsForType (sym_link *);
486 sym_link *newIntLink ();
487 sym_link *newCharLink ();
488 sym_link *newLongLink ();
489 int compareType (sym_link *, sym_link *);
490 int checkFunction (symbol *, symbol *);
491 void cleanUpLevel (bucket **, int);
492 void cleanUpBlock (bucket **, int);
493 int funcInChain (sym_link *);
494 void addSymChain (symbol *);
495 sym_link *structElemType (sym_link *, value *);
496 symbol *getStructElement (structdef *, symbol *);
497 sym_link *computeType (sym_link *, sym_link *);
498 void processFuncArgs (symbol *);
499 int isSymbolEqual (symbol *, symbol *);
500 int powof2 (unsigned long);
501 void printTypeChain (sym_link *, FILE *);
502 void initCSupport ();
503 void initBuiltIns ();
504 void pointerTypes (sym_link *, sym_link *);
505 void cdbTypeInfo (sym_link *, FILE *);
506 void cdbSymbol (symbol *, FILE *, int, int);
507 void cdbStructBlock (int, FILE *);
509 bucket *newBucket ();
510 void addSym (bucket **, void *, char *, int, int, int checkType);
511 void deleteSym (bucket **, void *, char *);
512 void *findSym (bucket **, void *, const char *);
513 void *findSymWithLevel (bucket **, struct symbol *);
514 void *findSymWithBlock (bucket **, struct symbol *, int);
515 void changePointer (symbol * sym);
516 void checkTypeSanity(sym_link *etype, char *name);
517 sym_link *typeFromStr (char *) ;
520 extern char *nounName(sym_link *); /* noun strings */
521 extern void printFromToType (sym_link *, sym_link *);