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_NAME_MAX 64
29 #include "SDCChasht.h"
32 #define HASHTAB_SIZE 256
34 /* hash table bucket */
37 void *sym; /* pointer to the object */
38 char name[SDCC_NAME_MAX + 1]; /* name of this symbol */
39 int level; /* nest level for this symbol */
40 int block; /* belongs to which block */
41 struct bucket *prev; /* ptr 2 previous bucket */
42 struct bucket *next; /* ptr 2 next bucket */
46 typedef struct structdef
48 char tag[SDCC_NAME_MAX + 1]; /* tag part of structure */
49 unsigned char level; /* Nesting level */
50 struct symbol *fields; /* pointer to fields */
51 unsigned size; /* sizeof the table in bytes */
55 /* noun definitions */
91 #define TF_LONG 0x00000001 /* type long int */
92 #define TF_SHORT 0x00000002 /* type short */
93 #define TF_UNSIGNED 0x00000004 /* type is unsigned */
94 #define TF_STATIC 0x00000008 /* type is static */
95 #define TF_EXTERN 0x00000010 /* type is extern */
96 #define TF_ABSADDR 0x00000020 /* type has absolute address */
97 #define TF_REENT 0x00000040 /* type of func is reentrant func */
98 #define TF_INTRRNT 0x00000080 /* is an interrupt routine */
100 /* specifier is the last in the type-chain */
101 typedef struct specifier
103 NOUN noun; /* CHAR INT STRUCTURE LABEL */
104 STORAGE_CLASS sclass; /* REGISTER,AUTO,FIX,CONSTANT */
105 struct memmap *oclass; /* output storage class */
106 unsigned _long:1; /* 1=long */
107 unsigned _short:1; /* 1=short int */
108 unsigned _unsigned:1; /* 1=unsigned, 0=signed */
109 unsigned _static:1; /* 1=static keyword found */
110 unsigned _extern:1; /* 1=extern found */
111 unsigned _absadr:1; /* absolute address specfied */
112 unsigned _reent:1; /* function is reentrant */
113 unsigned _intrtn:1; /* this is an interrupt routin */
114 unsigned _rbank:1; /* seperate register bank */
115 unsigned _volatile:1; /* is marked as volatile */
116 unsigned _const:1; /* is a constant */
117 unsigned _critical:1; /* critical function */
118 unsigned _naked:1; /* naked function */
119 unsigned _typedef:1; /* is typedefed */
120 unsigned _isregparm:1; /* is the first parameter */
121 unsigned _isenum:1; /* is an enumerated type */
122 unsigned nonbanked:1; /* function has the nonbanked attribute */
123 unsigned banked:1; /* function has the banked attribute */
124 unsigned _IntNo; /* 1=Interrupt svc routine */
125 short _regbank; /* register bank 2b used */
126 unsigned _addr; /* address of symbol */
127 unsigned _stack; /* stack offset for stacked v */
128 unsigned _bitStart; /* bit start position */
129 int _bitLength; /* bit length */
132 { /* Values if constant or enum */
133 int v_int; /* int and char values */
134 char *v_char; /* character string */
135 unsigned v_uint; /* unsigned int const value */
136 long v_long; /* long constant value */
137 unsigned long v_ulong; /* unsigned long constant val */
138 double v_float; /* floating point constant value */
139 struct symbol *v_enum; /* ptr 2 enum_list if enum==1 */
142 struct structdef *v_struct; /* structure pointer */
146 /* types of declarators */
149 POINTER = 0, /* pointer to near data */
150 FPOINTER, /* pointer to far data */
151 CPOINTER, /* pointer to code space */
152 GPOINTER, /* _generic pointer */
153 PPOINTER, /* paged area pointer */
154 IPOINTER, /* pointer to upper 128 bytes */
155 UPOINTER, /* unknown pointer used only when parsing */
156 EEPPOINTER, /* pointer to eeprom */
162 typedef struct declarator
164 DECLARATOR_TYPE dcl_type; /* POINTER,ARRAY or FUNCTION */
165 unsigned int num_elem; /* # of elems if type==array */
166 short ptr_const:1; /* pointer is constant */
167 short ptr_volatile:1; /* pointer is volatile */
168 struct sym_link *tspec; /* pointer type specifier */
175 typedef struct sym_link
177 unsigned class:1; /* DECLARATOR or SPECIFIER */
178 unsigned tdef:1; /* current link created by */
179 /* typedef if this flag is set */
182 specifier s; /* if CLASS == SPECIFIER */
183 declarator d; /* if CLASS == DECLARATOR */
187 struct sym_link *next; /* next element on the chain */
191 typedef struct symbol
193 char name[SDCC_NAME_MAX + 1]; /* Input Variable Name */
194 char rname[SDCC_NAME_MAX + 1]; /* internal name */
196 short level; /* declration lev,fld offset */
197 short block; /* sequential block # of defintion */
199 unsigned fbody:1; /* function body defined */
200 unsigned implicit:1; /* implicit flag */
201 unsigned undefined:1; /* undefined variable */
202 unsigned ret:1; /* return statement for a function */
203 unsigned hasVargs:1; /* has a variable argument list */
204 unsigned _isparm:1; /* is a parameter */
205 unsigned ismyparm:1; /* is parameter of the function being generated */
206 unsigned isitmp:1; /* is an intermediate temp */
207 unsigned islbl:1; /* is a temporary label */
208 unsigned isref:1; /* has been referenced */
209 unsigned isind:1; /* is a induction variable */
210 unsigned isinvariant:1; /* is a loop invariant */
211 unsigned isstrlit:1; /* is a string literal */
212 unsigned cdef:1; /* compiler defined symbol */
213 unsigned allocreq:1; /* allocation is required for this variable */
214 unsigned addrtaken:1; /* address of the symbol was taken */
215 unsigned isreqv:1; /* is the register quivalent of a symbol */
216 unsigned hasFcall:1; /* for functions does it call other functions */
217 unsigned calleeSave:1; /* for functions uses callee save paradigm */
218 unsigned udChked:1; /* use def checking has been already done */
220 /* following flags are used by the backend
221 for code generation and can be changed
222 if a better scheme for backend is thought of */
223 unsigned isLiveFcall:1; /* is live at or across a function call */
224 unsigned isspilt:1; /* has to be spilt */
225 unsigned remat:1; /* can be remateriazed */
226 unsigned isptr:1; /* is a pointer */
227 unsigned uptr:1; /* used as a pointer */
228 unsigned isFree:1; /* used by register allocator */
229 unsigned islocal:1; /* is a local variable */
230 unsigned blockSpil:1; /* spilt at block level */
231 unsigned remainSpil:1; /* spilt because not used in remainder */
232 unsigned stackSpil:1; /* has been spilt on temp stack location */
233 unsigned onStack:1; /* this symbol allocated on the stack */
234 unsigned iaccess:1; /* indirect access */
235 unsigned ruonly:1; /* used in return statement only */
236 unsigned spildir:1; /* spilt in direct space */
237 unsigned ptrreg:1; /* this symbol assigned to a ptr reg */
238 unsigned accuse; /* can be left in the accumulator
239 On the Z80 accuse is devided into
240 ACCUSE_A and ACCUSE_HL as the idea
244 int stack; /* offset on stack */
245 int xstack; /* offset on xternal stack */
246 short nRegs; /* number of registers required */
247 short regType; /* type of register required */
249 struct regs *regs[4]; /* can have at the most 4 registers */
250 struct asmop *aop; /* asmoperand for this symbol */
251 struct iCode *fuse; /* furthest use */
252 struct iCode *rematiCode; /* rematerialse with which instruction */
253 struct operand *reqv; /* register equivalent of a local variable */
256 struct symbol *spillLoc; /* register spil location */
257 struct set *itmpStack; /* symbols spilt @ this stack location */
260 short bitVar; /* this is a bit variable */
261 unsigned offset; /* offset from top if struct */
263 int lineDef; /* defined line number */
264 int lastLine; /* for functions the last line */
265 struct sym_link *type; /* 1st link to declator chain */
266 struct sym_link *etype; /* last link to declarator chn */
267 struct value *args; /* arguments if function */
268 struct symbol *next; /* crosslink to next symbol */
269 struct symbol *localof; /* local variable of which function */
270 struct initList *ival; /* ptr to initializer if any */
271 struct bitVect *defs; /* bit vector for definitions */
272 struct bitVect *uses; /* bit vector for uses */
273 struct bitVect *regsUsed; /* for functions registers used */
274 int liveFrom; /* live from iCode sequence number */
275 int liveTo; /* live to sequence number */
276 int used; /* no. of times this was used */
277 int recvSize; /* size of first argument */
281 /* Easy Access Macros */
282 #define DCL_TYPE(l) l->select.d.dcl_type
283 #define DCL_ELEM(l) l->select.d.num_elem
284 #define DCL_PTR_CONST(l) l->select.d.ptr_const
285 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
286 #define DCL_TSPEC(l) l->select.d.tspec
287 #define SPEC_NOUN(x) x->select.s.noun
288 #define SPEC_LONG(x) x->select.s._long
289 #define SPEC_SHORT(x) x->select.s._short
290 #define SPEC_USIGN(x) x->select.s._unsigned
291 #define SPEC_SCLS(x) x->select.s.sclass
292 #define SPEC_ENUM(x) x->select.s._isenum
293 #define SPEC_OCLS(x) x->select.s.oclass
294 #define SPEC_STAT(x) x->select.s._static
295 #define SPEC_EXTR(x) x->select.s._extern
296 #define SPEC_CODE(x) x->select.s._codesg
297 #define SPEC_RENT(x) x->select.s._reent
298 #define SPEC_INTN(x) x->select.s._IntNo
299 #define SPEC_ABSA(x) x->select.s._absadr
300 #define SPEC_BANK(x) x->select.s._regbank
301 #define SPEC_ADDR(x) x->select.s._addr
302 #define SPEC_STAK(x) x->select.s._stack
303 #define SPEC_CVAL(x) x->select.s.const_val
304 #define SPEC_BSTR(x) x->select.s._bitStart
305 #define SPEC_BLEN(x) x->select.s._bitLength
307 /* Sleaze: SPEC_ISR_SAVED_BANKS is only used on
308 * function type symbols, which obviously cannot
309 * be of BIT type. Therefore, we recycle the
310 * _bitStart field instead of defining a new field.
312 #define SPEC_ISR_SAVED_BANKS(x) x->select.s._bitStart
313 #define SPEC_BNKF(x) x->select.s._rbank
314 #define SPEC_INTRTN(x) x->select.s._intrtn
315 #define SPEC_CRTCL(x) x->select.s._critical
316 #define SPEC_NAKED(x) x->select.s._naked
317 #define SPEC_VOLATILE(x) x->select.s._volatile
318 #define SPEC_CONST(x) x->select.s._const
319 #define SPEC_STRUCT(x) x->select.s.v_struct
320 #define SPEC_TYPEDEF(x) x->select.s._typedef
321 #define SPEC_REGPARM(x) x->select.s._isregparm
322 #define SPEC_NONBANKED(x) x->select.s.nonbanked
323 #define SPEC_BANKED(x) x->select.s.banked
325 /* type check macros */
326 #define IS_DECL(x) ( x && x->class == DECLARATOR )
327 #define IS_SPEC(x) ( x && x->class == SPECIFIER )
328 #define IS_ARRAY(x) (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
329 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
330 #define IS_PTR(x) (IS_DECL(x) && (DCL_TYPE(x) == POINTER || \
331 DCL_TYPE(x) == FPOINTER || \
332 DCL_TYPE(x) == GPOINTER || \
333 DCL_TYPE(x) == IPOINTER || \
334 DCL_TYPE(x) == PPOINTER || \
335 DCL_TYPE(x) == EEPPOINTER || \
336 DCL_TYPE(x) == CPOINTER || \
337 DCL_TYPE(x) == UPOINTER ))
338 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
339 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
340 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
341 #define IS_FUNC(x) (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
342 #define IS_LONG(x) (IS_SPEC(x) && x->select.s._long)
343 #define IS_SHORT(x) (IS_SPEC(x) && x->select.s._short)
344 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
345 #define IS_CONSTANT(x) (IS_SPEC(x) && ( x->select.s._const == 1))
346 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
347 #define IS_ABSOLUTE(x) (IS_SPEC(x) && x->select.s._absadr )
348 #define IS_REGISTER(x) (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
349 #define IS_RENT(x) (IS_SPEC(x) && x->select.s._reent )
350 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
351 #define IS_INT(x) (IS_SPEC(x) && x->select.s.noun == V_INT)
352 #define IS_VOID(x) (IS_SPEC(x) && x->select.s.noun == V_VOID)
353 #define IS_CHAR(x) (IS_SPEC(x) && x->select.s.noun == V_CHAR)
354 #define IS_EXTERN(x) (IS_SPEC(x) && x->select.s._extern)
355 #define IS_VOLATILE(x) (IS_SPEC(x) && x->select.s._volatile )
356 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT || \
357 x->select.s.noun == V_CHAR || \
358 x->select.s.noun == V_BIT || \
359 x->select.s.noun == V_SBIT ))
360 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
361 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT || \
362 x->select.s.noun == V_SBIT ))
363 #define IS_FLOAT(x) (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
364 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
365 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
366 #define IS_LITERAL(x) (IS_SPEC(x) && x->select.s.sclass == S_LITERAL)
367 #define IS_ISR(x) (IS_SPEC(x) && SPEC_INTRTN(x))
368 #define IS_REGPARM(x) (IS_SPEC(x) && SPEC_REGPARM(x))
369 #define IS_NONBANKED(x) (IS_SPEC(x) && SPEC_NONBANKED(x))
370 #define IS_BANKED(x) (IS_SPEC(x) && SPEC_BANKED(x))
371 #define IS_BANKEDCALL(x) (IS_SPEC(x) && !SPEC_NONBANKED(x) && !SPEC_STAT(x) && (options.model == MODEL_LARGE || options.model == MODEL_MEDIUM || SPEC_BANKED(x)))
373 /* forward declaration for the global vars */
374 extern bucket *SymbolTab[];
375 extern bucket *StructTab[];
376 extern bucket *TypedefTab[];
377 extern bucket *LabelTab[];
378 extern bucket *enumTab[];
379 extern symbol *__fsadd;
380 extern symbol *__fssub;
381 extern symbol *__fsmul;
382 extern symbol *__fsdiv;
383 extern symbol *__fseq;
384 extern symbol *__fsneq;
385 extern symbol *__fslt;
386 extern symbol *__fslteq;
387 extern symbol *__fsgt;
388 extern symbol *__fsgteq;
390 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
391 extern symbol *__muldiv[3][3][2];
392 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
393 extern sym_link *__multypes[3][2];
394 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
395 extern symbol *__conv[2][3][2];
397 #define CHARTYPE __multypes[0][0]
398 #define UCHARTYPE __multypes[0][1]
399 #define INTTYPE __multypes[1][0]
400 #define UINTTYPE __multypes[1][1]
401 #define LONGTYPE __multypes[2][0]
402 #define ULONGTYPE __multypes[2][1]
405 extern sym_link *floatType;
409 /* forward definitions for the symbol table related functions */
411 symbol *newSymbol (char *, int);
412 sym_link *newLink ();
413 sym_link *newFloatLink ();
414 structdef *newStruct (char *);
415 void addDecl (symbol *, int, sym_link *);
416 sym_link *mergeSpec (sym_link *, sym_link *);
417 sym_link *cloneSpec (sym_link *);
418 symbol *reverseSyms (symbol *);
419 sym_link *reverseLink (sym_link *);
420 symbol *copySymbol (symbol *);
421 symbol *copySymbolChain (symbol *);
422 void printSymChain (symbol *, int);
423 void printStruct (structdef *, int);
424 char *genSymName (int);
425 sym_link *getSpec (sym_link *);
426 char *genSymName (int);
427 int compStructSize (int, structdef *);
428 sym_link *copyLinkChain (sym_link *);
429 int checkDecl (symbol *);
430 void checkBasic (sym_link *, sym_link *);
431 value *checkPointerIval (sym_link *, value *);
432 value *checkStructIval (symbol *, value *);
433 value *checkArrayIval (sym_link *, value *);
434 value *checkIval (sym_link *, value *);
435 unsigned int getSize (sym_link *);
436 unsigned int bitsForType (sym_link *);
437 sym_link *newIntLink ();
438 sym_link *newCharLink ();
439 sym_link *newLongLink ();
440 int checkType (sym_link *, sym_link *);
441 int checkFunction (symbol *);
442 void cleanUpLevel (bucket **, int);
443 void cleanUpBlock (bucket **, int);
444 int funcInChain (sym_link *);
445 void addSymChain (symbol *);
446 sym_link *structElemType (sym_link *, value *, value **);
447 symbol *getStructElement (structdef *, symbol *);
448 sym_link *computeType (sym_link *, sym_link *);
449 void processFuncArgs (symbol *, int);
450 int isSymbolEqual (symbol *, symbol *);
451 int powof2 (unsigned long);
452 void printTypeChain (sym_link *, FILE *);
453 void initCSupport ();
454 void pointerTypes (sym_link *, sym_link *);
455 void cdbTypeInfo (sym_link *, FILE *);
456 void cdbSymbol (symbol *, FILE *, int, int);
457 void cdbStructBlock (int, FILE *);
459 bucket *newBucket ();
460 void addSym (bucket **, void *, char *, int, int);
461 void deleteSym (bucket **, void *, char *);
462 void *findSym (bucket **, void *, const char *);
463 void *findSymWithLevel (bucket **, struct symbol *);
464 void *findSymWithBlock (bucket **, struct symbol *, int);
465 void changePointer (symbol * sym);