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