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