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 */
210 unsigned javaNative; /* is a JavaNative Function (TININative ONLY) */
211 unsigned overlay; /* force parameters & locals into overlay segment */
214 struct sym_link *next; /* next element on the chain */
218 typedef struct symbol
220 char name[SDCC_SYMNAME_MAX + 1]; /* Input Variable Name */
221 char rname[SDCC_NAME_MAX + 1]; /* internal name */
223 short level; /* declration lev,fld offset */
224 short block; /* sequential block # of defintion */
226 unsigned implicit:1; /* implicit flag */
227 unsigned undefined:1; /* undefined variable */
228 unsigned _isparm:1; /* is a parameter */
229 unsigned ismyparm:1; /* is parameter of the function being generated */
230 unsigned isitmp:1; /* is an intermediate temp */
231 unsigned islbl:1; /* is a temporary label */
232 unsigned isref:1; /* has been referenced */
233 unsigned isind:1; /* is a induction variable */
234 unsigned isinvariant:1; /* is a loop invariant */
235 unsigned isstrlit:1; /* is a string literal */
236 unsigned cdef:1; /* compiler defined symbol */
237 unsigned addrtaken:1; /* address of the symbol was taken */
238 unsigned isreqv:1; /* is the register quivalent of a symbol */
239 unsigned udChked:1; /* use def checking has been already done */
241 /* following flags are used by the backend
242 for code generation and can be changed
243 if a better scheme for backend is thought of */
244 unsigned isLiveFcall:1; /* is live at or across a function call */
245 unsigned isspilt:1; /* has to be spilt */
246 unsigned spillA:1; /* spilt be register allocator */
247 unsigned remat:1; /* can be remateriazed */
248 unsigned isptr:1; /* is a pointer */
249 unsigned uptr:1; /* used as a pointer */
250 unsigned isFree:1; /* used by register allocator */
251 unsigned islocal:1; /* is a local variable */
252 unsigned blockSpil:1; /* spilt at block level */
253 unsigned remainSpil:1; /* spilt because not used in remainder */
254 unsigned stackSpil:1; /* has been spilt on temp stack location */
255 unsigned onStack:1; /* this symbol allocated on the stack */
256 unsigned iaccess:1; /* indirect access */
257 unsigned ruonly:1; /* used in return statement only */
258 unsigned spildir:1; /* spilt in direct space */
259 unsigned ptrreg:1; /* this symbol assigned to a ptr reg */
260 unsigned noSpilLoc:1; /* cannot be assigned a spil location */
261 unsigned accuse; /* can be left in the accumulator
262 On the Z80 accuse is devided into
263 ACCUSE_A and ACCUSE_HL as the idea
267 int allocreq ; /* allocation is required for this variable */
268 int stack; /* offset on stack */
269 int xstack; /* offset on xternal stack */
270 short nRegs; /* number of registers required */
271 short regType; /* type of register required */
273 struct regs *regs[4]; /* can have at the most 4 registers */
274 struct asmop *aop; /* asmoperand for this symbol */
275 struct iCode *fuse; /* furthest use */
276 struct iCode *rematiCode; /* rematerialse with which instruction */
277 struct operand *reqv; /* register equivalent of a local variable */
280 struct symbol *spillLoc; /* register spil location */
281 struct set *itmpStack; /* symbols spilt @ this stack location */
284 short bitVar; /* this is a bit variable */
285 unsigned offset; /* offset from top if struct */
287 int lineDef; /* defined line number */
288 int lastLine; /* for functions the last line */
289 struct sym_link *type; /* 1st link to declator chain */
290 struct sym_link *etype; /* last link to declarator chn */
291 struct symbol *next; /* crosslink to next symbol */
292 struct symbol *localof; /* local variable of which function */
293 struct initList *ival; /* ptr to initializer if any */
294 struct bitVect *defs; /* bit vector for definitions */
295 struct bitVect *uses; /* bit vector for uses */
296 struct bitVect *regsUsed; /* for functions registers used */
297 int liveFrom; /* live from iCode sequence number */
298 int liveTo; /* live to sequence number */
299 int used; /* no. of times this was used */
300 int recvSize; /* size of first argument */
301 struct bitVect *clashes; /* overlaps with what other symbols */
305 /* Easy Access Macros */
306 #define DCL_TYPE(l) l->select.d.dcl_type
307 #define DCL_ELEM(l) l->select.d.num_elem
308 #define DCL_PTR_CONST(l) l->select.d.ptr_const
309 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
310 #define DCL_TSPEC(l) l->select.d.tspec
312 #define FUNC_DEBUG //assert(IS_FUNC(x));
313 #define FUNC_HASVARARGS(x) (x->funcAttrs.hasVargs)
314 #define IFFUNC_HASVARARGS(x) (IS_FUNC(x) && FUNC_HASVARARGS(x))
315 #define FUNC_ARGS(x) (x->funcAttrs.args)
316 #define IFFUNC_ARGS(x) (IS_FUNC(x) && FUNC_ARGS(x))
317 #define FUNC_HASFCALL(x) (x->funcAttrs.hasFcall)
318 #define IFFUNC_HASFCALL(x) (IS_FUNC(x) && FUNC_HASFCALL(x))
319 #define FUNC_HASBODY(x) (x->funcAttrs.hasbody)
320 #define IFFUNC_HASBODY(x) (IS_FUNC(x) && FUNC_HASBODY(x))
321 #define FUNC_CALLEESAVES(x) (x->funcAttrs.calleeSaves)
322 #define IFFUNC_CALLEESAVES(x) (IS_FUNC(x) && FUNC_CALLEESAVES(x))
323 #define FUNC_ISISR(x) (x->funcAttrs.intrtn)
324 #define IFFUNC_ISISR(x) (IS_FUNC(x) && FUNC_ISISR(x))
325 #define IFFUNC_RBANK(x) (IS_FUNC(x) && FUNC_RBANK(x))
326 #define FUNC_INTNO(x) (x->funcAttrs.intno)
327 #define FUNC_REGBANK(x) (x->funcAttrs.regbank)
329 #define FUNC_ISREENT(x) (x->funcAttrs.reent)
330 #define IFFUNC_ISREENT(x) (IS_FUNC(x) && FUNC_ISREENT(x))
331 #define FUNC_ISNAKED(x) (x->funcAttrs.naked)
332 #define IFFUNC_ISNAKED(x) (IS_FUNC(x) && FUNC_ISNAKED(x))
333 #define FUNC_NONBANKED(x) (x->funcAttrs.nonbanked)
334 #define IFFUNC_NONBANKED(x) (IS_FUNC(x) && FUNC_NONBANKED(x))
335 #define FUNC_BANKED(x) (x->funcAttrs.banked)
336 #define IFFUNC_BANKED(x) (IS_FUNC(x) && FUNC_BANKED(x))
337 #define FUNC_ISCRITICAL(x) (x->funcAttrs.critical)
338 #define IFFUNC_ISCRITICAL(x) (IS_FUNC(x) && FUNC_ISCRITICAL(x))
339 #define FUNC_ISBUILTIN(x) (x->funcAttrs.builtin)
340 #define IFFUNC_ISBUILTIN(x) (IS_FUNC(x) && FUNC_ISBUILTIN(x))
341 #define FUNC_ISJAVANATIVE(x) (x->funcAttrs.javaNative)
342 #define IFFUNC_ISJAVANATIVE(x) (IS_FUNC(x) && FUNC_ISJAVANATIVE(x))
343 #define FUNC_ISOVERLAY(x) (x->funcAttrs.overlay)
344 #define IFFUNC_ISOVERLAY(x) (IS_FUNC(x) && FUNC_ISOVERLAY(x))
346 // jwk: I am not sure about this
347 #define IFFUNC_ISBANKEDCALL(x) (!IFFUNC_NONBANKED(x) && \
348 (options.model == MODEL_LARGE || \
349 options.model == MODEL_MEDIUM || \
352 #define SPEC_NOUN(x) x->select.s.noun
353 #define SPEC_LONG(x) x->select.s._long
354 #define SPEC_USIGN(x) x->select.s._unsigned
355 #define SPEC_SCLS(x) x->select.s.sclass
356 #define SPEC_ENUM(x) x->select.s._isenum
357 #define SPEC_OCLS(x) x->select.s.oclass
358 #define SPEC_STAT(x) x->select.s._static
359 #define SPEC_EXTR(x) x->select.s._extern
360 #define SPEC_CODE(x) x->select.s._codesg
361 #define SPEC_ABSA(x) x->select.s._absadr
362 #define SPEC_BANK(x) x->select.s._regbank
363 #define SPEC_ADDR(x) x->select.s._addr
364 #define SPEC_STAK(x) x->select.s._stack
365 #define SPEC_CVAL(x) x->select.s.const_val
366 #define SPEC_BSTR(x) x->select.s._bitStart
367 #define SPEC_BLEN(x) x->select.s._bitLength
369 /* Sleaze: SPEC_ISR_SAVED_BANKS is only used on
370 * function type symbols, which obviously cannot
371 * be of BIT type. Therefore, we recycle the
372 * _bitStart field instead of defining a new field.
374 #define SPEC_ISR_SAVED_BANKS(x) x->select.s._bitStart
375 #define SPEC_VOLATILE(x) x->select.s._volatile
376 #define SPEC_CONST(x) x->select.s._const
377 #define SPEC_STRUCT(x) x->select.s.v_struct
378 #define SPEC_TYPEDEF(x) x->select.s._typedef
379 #define SPEC_REGPARM(x) x->select.s._isregparm
381 /* type check macros */
382 #define IS_DECL(x) ( x && x->class == DECLARATOR )
383 #define IS_SPEC(x) ( x && x->class == SPECIFIER )
384 #define IS_ARRAY(x) (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
385 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
386 #define IS_PTR(x) (IS_DECL(x) && (DCL_TYPE(x) == POINTER || \
387 DCL_TYPE(x) == FPOINTER || \
388 DCL_TYPE(x) == GPOINTER || \
389 DCL_TYPE(x) == IPOINTER || \
390 DCL_TYPE(x) == PPOINTER || \
391 DCL_TYPE(x) == EEPPOINTER || \
392 DCL_TYPE(x) == CPOINTER || \
393 DCL_TYPE(x) == UPOINTER ))
394 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
395 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
396 #define IS_CODEPTR(x) (IS_DECL(x) && DCL_TYPE(x) == CPOINTER)
397 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
398 #define IS_FUNC(x) (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
399 #define IS_LONG(x) (IS_SPEC(x) && x->select.s._long)
400 #define IS_UNSIGNED(x) (IS_SPEC(x) && x->select.s._unsigned)
401 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
402 #define IS_CONSTANT(x) (IS_SPEC(x) && ( x->select.s._const == 1))
403 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
404 #define IS_ABSOLUTE(x) (IS_SPEC(x) && x->select.s._absadr )
405 #define IS_REGISTER(x) (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
406 #define IS_RENT(x) (IS_SPEC(x) && x->select.s._reent )
407 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
408 #define IS_INT(x) (IS_SPEC(x) && x->select.s.noun == V_INT)
409 #define IS_VOID(x) (IS_SPEC(x) && x->select.s.noun == V_VOID)
410 #define IS_CHAR(x) (IS_SPEC(x) && x->select.s.noun == V_CHAR)
411 #define IS_EXTERN(x) (IS_SPEC(x) && x->select.s._extern)
412 #define IS_VOLATILE(x) (IS_SPEC(x) && x->select.s._volatile )
413 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT || \
414 x->select.s.noun == V_CHAR || \
415 x->select.s.noun == V_BIT || \
416 x->select.s.noun == V_SBIT ))
417 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
418 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT || \
419 x->select.s.noun == V_SBIT ))
420 #define IS_FLOAT(x) (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
421 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
422 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
423 #define IS_LITERAL(x) (IS_SPEC(x) && x->select.s.sclass == S_LITERAL)
424 #define IS_REGPARM(x) (IS_SPEC(x) && SPEC_REGPARM(x))
426 /* forward declaration for the global vars */
427 extern bucket *SymbolTab[];
428 extern bucket *StructTab[];
429 extern bucket *TypedefTab[];
430 extern bucket *LabelTab[];
431 extern bucket *enumTab[];
432 extern symbol *__fsadd;
433 extern symbol *__fssub;
434 extern symbol *__fsmul;
435 extern symbol *__fsdiv;
436 extern symbol *__fseq;
437 extern symbol *__fsneq;
438 extern symbol *__fslt;
439 extern symbol *__fslteq;
440 extern symbol *__fsgt;
441 extern symbol *__fsgteq;
443 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
444 extern symbol *__muldiv[3][3][2];
445 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
446 extern sym_link *__multypes[3][2];
447 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
448 extern symbol *__conv[2][3][2];
449 /* Dims: shift left/shift right, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
450 extern symbol *__rlrr[2][3][2];
452 #define CHARTYPE __multypes[0][0]
453 #define UCHARTYPE __multypes[0][1]
454 #define INTTYPE __multypes[1][0]
455 #define UINTTYPE __multypes[1][1]
456 #define LONGTYPE __multypes[2][0]
457 #define ULONGTYPE __multypes[2][1]
460 extern sym_link *floatType;
464 /* forward definitions for the symbol table related functions */
466 symbol *newSymbol (char *, int);
467 sym_link *newLink ();
468 sym_link *newFloatLink ();
469 structdef *newStruct (char *);
470 void addDecl (symbol *, int, sym_link *);
471 sym_link *mergeSpec (sym_link *, sym_link *, char *name);
472 sym_link *cloneSpec (sym_link *);
473 symbol *reverseSyms (symbol *);
474 sym_link *reverseLink (sym_link *);
475 symbol *copySymbol (symbol *);
476 symbol *copySymbolChain (symbol *);
477 void printSymChain (symbol *, int);
478 void printStruct (structdef *, int);
479 char *genSymName (int);
480 sym_link *getSpec (sym_link *);
481 char *genSymName (int);
482 int compStructSize (int, structdef *);
483 sym_link *copyLinkChain (sym_link *);
484 int checkDecl (symbol *, int);
485 void checkBasic (sym_link *, sym_link *);
486 value *checkPointerIval (sym_link *, value *);
487 value *checkStructIval (symbol *, value *);
488 value *checkArrayIval (sym_link *, value *);
489 value *checkIval (sym_link *, value *);
490 unsigned int getSize (sym_link *);
491 unsigned int bitsForType (sym_link *);
492 sym_link *newIntLink ();
493 sym_link *newCharLink ();
494 sym_link *newLongLink ();
495 int compareType (sym_link *, sym_link *);
496 int checkFunction (symbol *, symbol *);
497 void cleanUpLevel (bucket **, int);
498 void cleanUpBlock (bucket **, int);
499 int funcInChain (sym_link *);
500 void addSymChain (symbol *);
501 sym_link *structElemType (sym_link *, value *);
502 symbol *getStructElement (structdef *, symbol *);
503 sym_link *computeType (sym_link *, sym_link *);
504 void processFuncArgs (symbol *);
505 int isSymbolEqual (symbol *, symbol *);
506 int powof2 (unsigned long);
507 void printTypeChain (sym_link *, FILE *);
508 void initCSupport ();
509 void initBuiltIns ();
510 void pointerTypes (sym_link *, sym_link *);
511 void cdbTypeInfo (sym_link *, FILE *);
512 void cdbSymbol (symbol *, FILE *, int, int);
513 void cdbStructBlock (int, FILE *);
515 bucket *newBucket ();
516 void addSym (bucket **, void *, char *, int, int, int checkType);
517 void deleteSym (bucket **, void *, char *);
518 void *findSym (bucket **, void *, const char *);
519 void *findSymWithLevel (bucket **, struct symbol *);
520 void *findSymWithBlock (bucket **, struct symbol *, int);
521 void changePointer (symbol * sym);
522 void checkTypeSanity(sym_link *etype, char *name);
523 sym_link *typeFromStr (char *) ;
526 extern char *nounName(sym_link *); /* noun strings */
527 extern void printFromToType (sym_link *, sym_link *);