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