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