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 */
187 /* following flags are used by the backend
188 for code generation and can be changed
189 if a better scheme for backend is thought of */
191 unsigned isspilt :1 ; /* has to be spilt */
192 unsigned remat :1 ; /* can be remateriazed */
193 unsigned isptr :1 ; /* is a pointer */
194 unsigned uptr :1 ; /* used as a pointer */
195 unsigned isFree :1 ; /* used by register allocator */
196 unsigned islocal :1 ; /* is a local variable */
197 unsigned blockSpil :1 ; /* spilt at block level */
198 unsigned remainSpil :1 ; /* spilt because not used in remainder */
199 unsigned stackSpil :1 ; /* has been spilt on temp stack location */
200 unsigned onStack :1 ; /* this symbol allocated on the stack */
201 unsigned iaccess :1 ; /* indirect access */
202 unsigned ruonly :1 ; /* used in return statement only */
203 unsigned accuse :1 ; /* can be left in the accumulator */
204 unsigned spildir :1 ; /* spilt in direct space */
205 unsigned ptrreg :1 ; /* this symbol assigned to a ptr reg */
207 int stack ; /* offset on stack */
208 int xstack ; /* offset on xternal stack */
209 short nRegs ; /* number of registers required */
210 short regType ; /* type of register required */
212 struct regs *regs[4] ; /* can have at the most 4 registers */
213 struct asmop *aop ; /* asmoperand for this symbol */
214 struct iCode *fuse ; /* furthest use */
215 struct iCode *rematiCode ; /* rematerialse with which instruction */
216 struct operand *reqv ; /* register equivalent of a local variable */
218 struct symbol *spillLoc; /* register spil location */
219 struct set *itmpStack; /* symbols spilt @ this stack location */
221 short bitVar ; /* this is a bit variable */
222 unsigned offset ; /* offset from top if struct */
224 int lineDef ; /* defined line number */
225 int lastLine ; /* for functions the last line*/
226 struct link *type ; /* 1st link to declator chain */
227 struct link *etype ; /* last link to declarator chn*/
228 struct value *args ; /* arguments if function */
229 struct symbol *next ; /* crosslink to next symbol */
230 struct symbol *localof ; /* local variable of which function */
231 struct initList *ival ; /* ptr to initializer if any */
232 struct bitVect *defs ; /* bit vector for definitions */
233 struct bitVect *uses ; /* bit vector for uses */
234 struct bitVect *regsUsed ; /* for functions registers used */
235 int liveFrom ; /* live from iCode sequence number */
236 int liveTo ; /* live to sequence number */
237 int used ; /* no. of times this was used */
238 int recvSize ; /* size of first argument */
239 int argStack ; /* stacks used by parameters */
243 /* Easy Access Macros */
244 #define DCL_TYPE(l) l->select.d.dcl_type
245 #define DCL_ELEM(l) l->select.d.num_elem
246 #define DCL_PTR_CONST(l) l->select.d.ptr_const
247 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
248 #define DCL_TSPEC(l) l->select.d.tspec
249 #define SPEC_NOUN(x) x->select.s.noun
250 #define SPEC_LONG(x) x->select.s._long
251 #define SPEC_SHORT(x) x->select.s._short
252 #define SPEC_USIGN(x) x->select.s._unsigned
253 #define SPEC_SCLS(x) x->select.s.sclass
254 #define SPEC_ENUM(x) x->select.s._isenum
255 #define SPEC_OCLS(x) x->select.s.oclass
256 #define SPEC_STAT(x) x->select.s._static
257 #define SPEC_EXTR(x) x->select.s._extern
258 #define SPEC_CODE(x) x->select.s._codesg
259 #define SPEC_RENT(x) x->select.s._reent
260 #define SPEC_INTN(x) x->select.s._IntNo
261 #define SPEC_ABSA(x) x->select.s._absadr
262 #define SPEC_BANK(x) x->select.s._regbank
263 #define SPEC_ADDR(x) x->select.s._addr
264 #define SPEC_STAK(x) x->select.s._stack
265 #define SPEC_CVAL(x) x->select.s.const_val
266 #define SPEC_BSTR(x) x->select.s._bitStart
267 #define SPEC_BLEN(x) x->select.s._bitLength
268 #define SPEC_BNKF(x) x->select.s._rbank
269 #define SPEC_INTRTN(x) x->select.s._intrtn
270 #define SPEC_CRTCL(x) x->select.s._critical
271 #define SPEC_VOLATILE(x) x->select.s._volatile
272 #define SPEC_CONST(x) x->select.s._const
273 #define SPEC_STRUCT(x) x->select.s.v_struct
274 #define SPEC_TYPEDEF(x) x->select.s._typedef
275 #define SPEC_REGPARM(x) x->select.s._isregparm
277 /* type check macros */
278 #define IS_DECL(x) ( x && x->class == DECLARATOR )
279 #define IS_SPEC(x) ( x && x->class == SPECIFIER )
280 #define IS_ARRAY(x) (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
281 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
282 #define IS_PTR(x) (IS_DECL(x) && (DCL_TYPE(x) == POINTER || \
283 DCL_TYPE(x) == FPOINTER || \
284 DCL_TYPE(x) == GPOINTER || \
285 DCL_TYPE(x) == IPOINTER || \
286 DCL_TYPE(x) == PPOINTER || \
287 DCL_TYPE(x) == EEPPOINTER || \
288 DCL_TYPE(x) == CPOINTER || \
289 DCL_TYPE(x) == UPOINTER ))
290 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
291 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
292 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
293 #define IS_FUNC(x) (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
294 #define IS_LONG(x) (IS_SPEC(x) && x->select.s._long)
295 #define IS_SHORT(x) (IS_SPEC(x) && x->select.s._short)
296 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
297 #define IS_CONSTANT(x) (IS_SPEC(x) && (x->select.s.sclass == S_CONSTANT ||\
298 x->select.s._const == 1))
299 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
300 #define IS_ABSOLUTE(x) (IS_SPEC(x) && x->select.s._absadr )
301 #define IS_REGISTER(x) (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
302 #define IS_RENT(x) (IS_SPEC(x) && x->select.s._reent )
303 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
304 #define IS_INT(x) (IS_SPEC(x) && x->select.s.noun == V_INT)
305 #define IS_VOID(x) (IS_SPEC(x) && x->select.s.noun == V_VOID)
306 #define IS_CHAR(x) (IS_SPEC(x) && x->select.s.noun == V_CHAR)
307 #define IS_EXTERN(x) (IS_SPEC(x) && x->select.s._extern)
308 #define IS_VOLATILE(x) (IS_SPEC(x) && x->select.s._volatile )
309 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT || \
310 x->select.s.noun == V_CHAR || \
311 x->select.s.noun == V_BIT || \
312 x->select.s.noun == V_SBIT ))
313 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
314 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT || \
315 x->select.s.noun == V_SBIT ))
316 #define IS_FLOAT(x) (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
317 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
318 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
319 #define IS_LITERAL(x) (IS_SPEC(x) && x->select.s.sclass == S_LITERAL)
320 #define IS_ISR(x) (IS_SPEC(x) && SPEC_INTRTN(x))
321 #define IS_REGPARM(x) (IS_SPEC(x) && SPEC_REGPARM(x))
323 /* forward declaration for the global vars */
324 extern bucket *SymbolTab[] ;
325 extern bucket *StructTab[] ;
326 extern bucket *TypedefTab[];
327 extern bucket *LabelTab[] ;
328 extern bucket *enumTab[];
329 extern symbol *__fsadd ;
330 extern symbol *__fssub ;
331 extern symbol *__fsmul ;
332 extern symbol *__fsdiv ;
333 extern symbol *__fseq ;
334 extern symbol *__fsneq ;
335 extern symbol *__fslt ;
336 extern symbol *__fslteq;
337 extern symbol *__fsgt ;
338 extern symbol *__fsgteq;
340 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
341 extern symbol *__muldiv[3][3][2];
342 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
343 extern link *__multypes[3][2];
344 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
345 extern symbol *__conv[2][3][2];
347 #define CHARTYPE __multypes[0][0]
348 #define INTTYPE __multypes[1][0]
349 #define UCHARTYPE __multypes[0][1]
351 extern link *floatType;
355 /* forward definitions for the symbol table related functions */
357 symbol *newSymbol ( char *, int );
359 structdef *newStruct ( char * );
360 void addDecl ( symbol *, int , link * );
361 link *mergeSpec ( link *, link * );
362 link *cloneSpec ( link * );
363 symbol *reverseSyms ( symbol * );
364 link *reverseLink ( link * );
365 symbol *copySymbol ( symbol * );
366 symbol *copySymbolChain ( symbol * );
367 void printSymChain ( symbol *, int );
368 void printStruct ( structdef *, int );
369 char *genSymName ( int );
370 link *getSpec ( link * );
371 char *genSymName ( int );
372 int compStructSize ( int ,structdef * );
373 link *copyLinkChain ( link * );
374 int checkDecl ( symbol * );
375 void checkBasic ( link *, link * );
376 value *checkPointerIval ( link *, value * );
377 value *checkStructIval ( symbol *, value * );
378 value *checkArrayIval ( link *, value * );
379 value *checkIval ( link *, value * );
380 unsigned int getSize ( link * );
381 unsigned int bitsForType ( link * );
382 link *newIntLink ( );
383 link *newCharLink ( );
384 link *newLongLink ( );
385 int checkType ( link *, link * );
386 int checkFunction ( symbol * );
387 void cleanUpLevel ( bucket **,int );
388 void cleanUpBlock ( bucket **,int );
389 int funcInChain ( link * );
390 void addSymChain ( symbol * );
391 link *structElemType ( link *, value * , value ** );
392 symbol *getStructElement ( structdef *, symbol *) ;
393 link *computeType ( link *, link *);
394 void processFuncArgs (symbol *,int);
395 int isSymbolEqual (symbol *, symbol *);
396 int powof2 (unsigned long );
397 void printTypeChain (link *,FILE *);
398 void initCSupport ();
399 void pointerTypes (link *, link * );
400 void cdbTypeInfo (link *,FILE *);
401 void cdbSymbol (symbol *,FILE *,int,int);
402 void cdbStructBlock (int ,FILE *);
404 bucket *newBucket ( );
405 void addSym ( bucket ** , void *, char *, int, int);
406 void deleteSym ( bucket ** , void *, char *);
407 void *findSym ( bucket ** , void *, char *);
408 void *findSymWithLevel ( bucket ** , struct symbol * );
409 void *findSymWithBlock ( bucket ** , struct symbol *,int );