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 */
45 typedef struct structdef {
46 char tag[SDCC_NAME_MAX+1]; /* tag part of structure */
47 unsigned char level ; /* Nesting level */
48 struct symbol *fields ; /* pointer to fields */
49 unsigned size ; /* sizeof the table in bytes */
52 /* noun definitions */
84 /* specifier is the last in the type-chain */
85 typedef struct specifier {
86 NOUN noun ; /* CHAR INT STRUCTURE LABEL */
87 STORAGE_CLASS sclass ; /* REGISTER,AUTO,FIX,CONSTANT */
88 struct memmap *oclass ; /* output storage class */
89 unsigned _long : 1 ; /* 1=long */
90 unsigned _short: 1 ; /* 1=short int */
91 unsigned _unsigned: 1 ; /* 1=unsigned, 0=signed */
92 unsigned _static: 1 ; /* 1=static keyword found */
93 unsigned _extern: 1 ; /* 1=extern found */
94 unsigned _absadr: 1 ; /* absolute address specfied */
95 unsigned _reent : 1 ; /* function is reentrant */
96 unsigned _intrtn: 1 ; /* this is an interrupt routin*/
97 unsigned _rbank : 1 ; /* seperate register bank */
98 unsigned _volatile : 1; /* is marked as volatile */
99 unsigned _const:1 ; /* is a constant */
100 unsigned _critical:1 ; /* critical function */
101 unsigned _typedef :1 ; /* is typedefed */
102 unsigned _isregparm:1 ; /* is the first parameter */
103 unsigned _isenum :1 ; /* is an enumerated type */
104 unsigned banked :1 ; /* function has banked attribute */
105 unsigned _IntNo ; /* 1=Interrupt svc routine */
106 short _regbank ; /* register bank 2b used */
107 unsigned _addr ; /* address of symbol */
108 unsigned _stack ; /* stack offset for stacked v */
109 unsigned _bitStart ; /* bit start position */
110 int _bitLength ; /* bit length */
112 union { /* Values if constant or enum */
113 int v_int ; /* int and char values */
114 char *v_char; /* character string */
115 unsigned v_uint; /* unsigned int const value */
116 long v_long; /* long constant value */
117 unsigned long v_ulong; /* unsigned long constant val */
118 double v_float; /* floating point constant value */
119 struct symbol *v_enum; /* ptr 2 enum_list if enum==1 */
121 struct structdef *v_struct; /* structure pointer */
124 /* types of declarators */
126 POINTER = 0, /* pointer to near data */
127 FPOINTER , /* pointer to far data */
128 CPOINTER , /* pointer to code space */
129 GPOINTER , /* _generic pointer */
130 PPOINTER , /* paged area pointer */
131 IPOINTER , /* pointer to upper 128 bytes */
132 UPOINTER , /* unknown pointer used only when parsing */
133 EEPPOINTER , /* pointer to eeprom */
138 typedef struct declarator {
139 DECLARATOR_TYPE dcl_type; /* POINTER,ARRAY or FUNCTION */
140 short num_elem; /* # of elems if type==array */
141 short ptr_const :1; /* pointer is constant */
142 short ptr_volatile:1; /* pointer is volatile */
143 struct link *tspec; /* pointer type specifier */
149 typedef struct link {
150 unsigned class : 1 ; /* DECLARATOR or SPECIFIER */
151 unsigned tdef : 1 ; /* current link created by */
152 /* typedef if this flag is set*/
154 specifier s ; /* if CLASS == SPECIFIER */
155 declarator d ; /* if CLASS == DECLARATOR */
158 struct link *next ; /* next element on the chain */
161 typedef struct symbol {
162 char name [SDCC_NAME_MAX+1] ; /* Input Variable Name */
163 char rname[SDCC_NAME_MAX+1] ; /* internal name */
165 short level ; /* declration lev,fld offset */
166 short block ; /* sequential block # of defintion */
168 unsigned fbody :1 ; /* function body defined */
169 unsigned implicit :1 ; /* implicit flag */
170 unsigned undefined :1 ; /* undefined variable */
171 unsigned ret :1 ; /* return statement for a function */
172 unsigned hasVargs :1 ; /* has a variable argument list */
173 unsigned _isparm :1 ; /* is a parameter */
174 unsigned ismyparm :1 ; /* is parameter of the function being generated */
175 unsigned isitmp :1 ; /* is an intermediate temp */
176 unsigned islbl :1 ; /* is a temporary label */
177 unsigned isref :1 ; /* has been referenced */
178 unsigned isind :1 ; /* is a induction variable */
179 unsigned isinvariant:1 ; /* is a loop invariant */
180 unsigned isstrlit :1 ; /* is a string literal */
181 unsigned cdef :1 ; /* compiler defined symbol */
182 unsigned allocreq :1 ; /* allocation is required for this variable */
183 unsigned addrtaken :1 ; /* address of the symbol was taken */
184 unsigned isreqv :1 ; /* is the register quivalent of a symbol */
185 unsigned hasFcall :1 ; /* for functions does it call other functions */
186 unsigned calleeSave :1 ; /* for functions uses callee save paradigm */
187 unsigned udChked :1 ; /* use def checking has been already done */
189 /* following flags are used by the backend
190 for code generation and can be changed
191 if a better scheme for backend is thought of */
193 unsigned isspilt :1 ; /* has to be spilt */
194 unsigned remat :1 ; /* can be remateriazed */
195 unsigned isptr :1 ; /* is a pointer */
196 unsigned uptr :1 ; /* used as a pointer */
197 unsigned isFree :1 ; /* used by register allocator */
198 unsigned islocal :1 ; /* is a local variable */
199 unsigned blockSpil :1 ; /* spilt at block level */
200 unsigned remainSpil :1 ; /* spilt because not used in remainder */
201 unsigned stackSpil :1 ; /* has been spilt on temp stack location */
202 unsigned onStack :1 ; /* this symbol allocated on the stack */
203 unsigned iaccess :1 ; /* indirect access */
204 unsigned ruonly :1 ; /* used in return statement only */
205 unsigned spildir :1 ; /* spilt in direct space */
206 unsigned ptrreg :1 ; /* this symbol assigned to a ptr reg */
207 unsigned accuse ; /* can be left in the accumulator
208 On the Z80 accuse is devided into
209 ACCUSE_A and ACCUSE_HL as the idea
213 int stack ; /* offset on stack */
214 int xstack ; /* offset on xternal stack */
215 short nRegs ; /* number of registers required */
216 short regType ; /* type of register required */
218 struct regs *regs[4] ; /* can have at the most 4 registers */
219 struct asmop *aop ; /* asmoperand for this symbol */
220 struct iCode *fuse ; /* furthest use */
221 struct iCode *rematiCode ; /* rematerialse with which instruction */
222 struct operand *reqv ; /* register equivalent of a local variable */
224 struct symbol *spillLoc; /* register spil location */
225 struct set *itmpStack; /* symbols spilt @ this stack location */
227 short bitVar ; /* this is a bit variable */
228 unsigned offset ; /* offset from top if struct */
230 int lineDef ; /* defined line number */
231 int lastLine ; /* for functions the last line*/
232 struct link *type ; /* 1st link to declator chain */
233 struct link *etype ; /* last link to declarator chn*/
234 struct value *args ; /* arguments if function */
235 struct symbol *next ; /* crosslink to next symbol */
236 struct symbol *localof ; /* local variable of which function */
237 struct initList *ival ; /* ptr to initializer if any */
238 struct bitVect *defs ; /* bit vector for definitions */
239 struct bitVect *uses ; /* bit vector for uses */
240 struct bitVect *regsUsed ; /* for functions registers used */
241 int liveFrom ; /* live from iCode sequence number */
242 int liveTo ; /* live to sequence number */
243 int used ; /* no. of times this was used */
244 int recvSize ; /* size of first argument */
245 int argStack ; /* stacks used by parameters */
249 /* Easy Access Macros */
250 #define DCL_TYPE(l) l->select.d.dcl_type
251 #define DCL_ELEM(l) l->select.d.num_elem
252 #define DCL_PTR_CONST(l) l->select.d.ptr_const
253 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
254 #define DCL_TSPEC(l) l->select.d.tspec
255 #define SPEC_NOUN(x) x->select.s.noun
256 #define SPEC_LONG(x) x->select.s._long
257 #define SPEC_SHORT(x) x->select.s._short
258 #define SPEC_USIGN(x) x->select.s._unsigned
259 #define SPEC_SCLS(x) x->select.s.sclass
260 #define SPEC_ENUM(x) x->select.s._isenum
261 #define SPEC_OCLS(x) x->select.s.oclass
262 #define SPEC_STAT(x) x->select.s._static
263 #define SPEC_EXTR(x) x->select.s._extern
264 #define SPEC_CODE(x) x->select.s._codesg
265 #define SPEC_RENT(x) x->select.s._reent
266 #define SPEC_INTN(x) x->select.s._IntNo
267 #define SPEC_ABSA(x) x->select.s._absadr
268 #define SPEC_BANK(x) x->select.s._regbank
269 #define SPEC_ADDR(x) x->select.s._addr
270 #define SPEC_STAK(x) x->select.s._stack
271 #define SPEC_CVAL(x) x->select.s.const_val
272 #define SPEC_BSTR(x) x->select.s._bitStart
273 #define SPEC_BLEN(x) x->select.s._bitLength
274 #define SPEC_BNKF(x) x->select.s._rbank
275 #define SPEC_INTRTN(x) x->select.s._intrtn
276 #define SPEC_CRTCL(x) x->select.s._critical
277 #define SPEC_VOLATILE(x) x->select.s._volatile
278 #define SPEC_CONST(x) x->select.s._const
279 #define SPEC_STRUCT(x) x->select.s.v_struct
280 #define SPEC_TYPEDEF(x) x->select.s._typedef
281 #define SPEC_REGPARM(x) x->select.s._isregparm
282 #define SPEC_BANKED(x) x->select.s.banked
284 /* type check macros */
285 #define IS_DECL(x) ( x && x->class == DECLARATOR )
286 #define IS_SPEC(x) ( x && x->class == SPECIFIER )
287 #define IS_ARRAY(x) (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
288 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
289 #define IS_PTR(x) (IS_DECL(x) && (DCL_TYPE(x) == POINTER || \
290 DCL_TYPE(x) == FPOINTER || \
291 DCL_TYPE(x) == GPOINTER || \
292 DCL_TYPE(x) == IPOINTER || \
293 DCL_TYPE(x) == PPOINTER || \
294 DCL_TYPE(x) == EEPPOINTER || \
295 DCL_TYPE(x) == CPOINTER || \
296 DCL_TYPE(x) == UPOINTER ))
297 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
298 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
299 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
300 #define IS_FUNC(x) (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
301 #define IS_LONG(x) (IS_SPEC(x) && x->select.s._long)
302 #define IS_SHORT(x) (IS_SPEC(x) && x->select.s._short)
303 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
304 #define IS_CONSTANT(x) (IS_SPEC(x) && (x->select.s.sclass == S_CONSTANT ||\
305 x->select.s._const == 1))
306 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
307 #define IS_ABSOLUTE(x) (IS_SPEC(x) && x->select.s._absadr )
308 #define IS_REGISTER(x) (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
309 #define IS_RENT(x) (IS_SPEC(x) && x->select.s._reent )
310 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
311 #define IS_INT(x) (IS_SPEC(x) && x->select.s.noun == V_INT)
312 #define IS_VOID(x) (IS_SPEC(x) && x->select.s.noun == V_VOID)
313 #define IS_CHAR(x) (IS_SPEC(x) && x->select.s.noun == V_CHAR)
314 #define IS_EXTERN(x) (IS_SPEC(x) && x->select.s._extern)
315 #define IS_VOLATILE(x) (IS_SPEC(x) && x->select.s._volatile )
316 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT || \
317 x->select.s.noun == V_CHAR || \
318 x->select.s.noun == V_BIT || \
319 x->select.s.noun == V_SBIT ))
320 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
321 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT || \
322 x->select.s.noun == V_SBIT ))
323 #define IS_FLOAT(x) (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
324 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
325 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
326 #define IS_LITERAL(x) (IS_SPEC(x) && x->select.s.sclass == S_LITERAL)
327 #define IS_ISR(x) (IS_SPEC(x) && SPEC_INTRTN(x))
328 #define IS_REGPARM(x) (IS_SPEC(x) && SPEC_REGPARM(x))
329 #define IS_BANKED(x) (IS_SPEC(x) && SPEC_BANKED(x))
331 /* forward declaration for the global vars */
332 extern bucket *SymbolTab[] ;
333 extern bucket *StructTab[] ;
334 extern bucket *TypedefTab[];
335 extern bucket *LabelTab[] ;
336 extern bucket *enumTab[];
337 extern symbol *__fsadd ;
338 extern symbol *__fssub ;
339 extern symbol *__fsmul ;
340 extern symbol *__fsdiv ;
341 extern symbol *__fseq ;
342 extern symbol *__fsneq ;
343 extern symbol *__fslt ;
344 extern symbol *__fslteq;
345 extern symbol *__fsgt ;
346 extern symbol *__fsgteq;
348 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
349 extern symbol *__muldiv[3][3][2];
350 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
351 extern link *__multypes[3][2];
352 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
353 extern symbol *__conv[2][3][2];
355 #define CHARTYPE __multypes[0][0]
356 #define INTTYPE __multypes[1][0]
357 #define UCHARTYPE __multypes[0][1]
359 extern link *floatType;
363 /* forward definitions for the symbol table related functions */
365 symbol *newSymbol ( char *, int );
367 structdef *newStruct ( char * );
368 void addDecl ( symbol *, int , link * );
369 link *mergeSpec ( link *, link * );
370 link *cloneSpec ( link * );
371 symbol *reverseSyms ( symbol * );
372 link *reverseLink ( link * );
373 symbol *copySymbol ( symbol * );
374 symbol *copySymbolChain ( symbol * );
375 void printSymChain ( symbol *, int );
376 void printStruct ( structdef *, int );
377 char *genSymName ( int );
378 link *getSpec ( link * );
379 char *genSymName ( int );
380 int compStructSize ( int ,structdef * );
381 link *copyLinkChain ( link * );
382 int checkDecl ( symbol * );
383 void checkBasic ( link *, link * );
384 value *checkPointerIval ( link *, value * );
385 value *checkStructIval ( symbol *, value * );
386 value *checkArrayIval ( link *, value * );
387 value *checkIval ( link *, value * );
388 unsigned int getSize ( link * );
389 unsigned int bitsForType ( link * );
390 link *newIntLink ( );
391 link *newCharLink ( );
392 link *newLongLink ( );
393 int checkType ( link *, link * );
394 int checkFunction ( symbol * );
395 void cleanUpLevel ( bucket **,int );
396 void cleanUpBlock ( bucket **,int );
397 int funcInChain ( link * );
398 void addSymChain ( symbol * );
399 link *structElemType ( link *, value * , value ** );
400 symbol *getStructElement ( structdef *, symbol *) ;
401 link *computeType ( link *, link *);
402 void processFuncArgs (symbol *,int);
403 int isSymbolEqual (symbol *, symbol *);
404 int powof2 (unsigned long );
405 void printTypeChain (link *,FILE *);
406 void initCSupport ();
407 void pointerTypes (link *, link * );
408 void cdbTypeInfo (link *,FILE *);
409 void cdbSymbol (symbol *,FILE *,int,int);
410 void cdbStructBlock (int ,FILE *);
412 bucket *newBucket ( );
413 void addSym ( bucket ** , void *, char *, int, int);
414 void deleteSym ( bucket ** , void *, char *);
415 void *findSym ( bucket ** , void *, char *);
416 void *findSymWithLevel ( bucket ** , struct symbol * );
417 void *findSymWithBlock ( bucket ** , struct symbol *,int );