a saver error report
[fw/sdcc] / src / SDCCsymt.h
1 /*-------------------------------------------------------------------------
2   SDCCsymt.h - Header file for Symbols table related structures and MACRO's.              
3               Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
4
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
8    later version.
9    
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.
14    
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.
18    
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 -------------------------------------------------------------------------*/
23
24 #ifndef  SDCCSYMT_H
25 #define  SDCCSYMT_H
26
27 #define MAX_NEST_LEVEL  256
28 #define SDCC_NAME_MAX    64
29 #include "SDCChasht.h"
30
31
32 #define HASHTAB_SIZE 256
33
34 /* hash table bucket */
35 typedef struct bucket
36   {
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       */
43   }
44 bucket;
45
46 typedef struct structdef
47   {
48     char tag[SDCC_NAME_MAX + 1];        /* tag part of structure      */
49     unsigned char level;        /* Nesting level         */
50     struct symbol *fields;      /* pointer to fields     */
51     unsigned size;              /* sizeof the table in bytes  */
52   }
53 structdef;
54
55 /* noun definitions */
56 typedef enum
57   {
58     V_INT = 1,
59     V_FLOAT,
60     V_CHAR,
61     V_VOID,
62     V_STRUCT,
63     V_LABEL,
64     V_BIT,
65     V_SBIT,
66     V_DOUBLE
67   }
68 NOUN;
69
70 /* storage class    */
71 typedef enum
72   {
73     S_FIXED = 0,
74     S_AUTO,
75     S_REGISTER,
76     S_SFR,
77     S_SBIT,
78     S_CODE,
79     S_XDATA,
80     S_DATA,
81     S_IDATA,
82     S_PDATA,
83     S_LITERAL,
84     S_STACK,
85     S_XSTACK,
86     S_BIT,
87     S_EEPROM
88   }
89 STORAGE_CLASS;
90
91 /* specifier is the last in the type-chain */
92 typedef struct specifier
93   {
94     NOUN noun;                  /* CHAR INT STRUCTURE LABEL   */
95     STORAGE_CLASS sclass;       /* REGISTER,AUTO,FIX,CONSTANT */
96     struct memmap *oclass;      /* output storage class       */
97     unsigned _long:1;           /* 1=long            */
98     unsigned _short:1;          /* 1=short int    */
99     unsigned _unsigned:1;       /* 1=unsigned, 0=signed       */
100     unsigned _signed:1;         /* just for sanity checks only*/
101     unsigned _static:1;         /* 1=static keyword found     */
102     unsigned _extern:1;         /* 1=extern found             */
103     unsigned _absadr:1;         /* absolute address specfied  */
104     unsigned _reent:1;          /* function is reentrant      */
105     unsigned _intrtn:1;         /* this is an interrupt routin */
106     unsigned _rbank:1;          /* seperate register bank     */
107     unsigned _volatile:1;       /* is marked as volatile      */
108     unsigned _const:1;          /* is a constant              */
109     unsigned _critical:1;       /* critical function          */
110     unsigned _naked:1;          /* naked function             */
111     unsigned _typedef:1;        /* is typedefed               */
112     unsigned _isregparm:1;      /* is the first parameter     */
113     unsigned _isenum:1;         /* is an enumerated type      */
114     unsigned nonbanked:1;       /* function has the nonbanked attribute */
115     unsigned banked:1;          /* function has the banked attribute */
116     unsigned _IntNo;            /* 1=Interrupt svc routine    */
117     short _regbank;             /* register bank 2b used      */
118     unsigned _addr;             /* address of symbol          */
119     unsigned _stack;            /* stack offset for stacked v */
120     unsigned _bitStart;         /* bit start position         */
121     int _bitLength;             /* bit length                 */
122
123     union
124       {                         /* Values if constant or enum */
125         int v_int;              /* int and char values        */
126         char *v_char;           /* character string           */
127         unsigned v_uint;        /* unsigned int const value   */
128         long v_long;            /* long constant value        */
129         unsigned long v_ulong;  /* unsigned long constant val */
130         double v_float;         /* floating point constant value */
131         struct symbol *v_enum;  /* ptr 2 enum_list if enum==1 */
132       }
133     const_val;
134     struct structdef *v_struct; /* structure pointer      */
135   }
136 specifier;
137
138 /* types of declarators */
139 typedef enum
140   {
141     POINTER = 0,                /* pointer to near data */
142     FPOINTER,                   /* pointer to far data  */
143     CPOINTER,                   /* pointer to code space */
144     GPOINTER,                   /* _generic pointer     */
145     PPOINTER,                   /* paged area pointer   */
146     IPOINTER,                   /* pointer to upper 128 bytes */
147     UPOINTER,                   /* unknown pointer used only when parsing */
148     EEPPOINTER,                 /* pointer to eeprom     */
149     ARRAY,
150     FUNCTION
151   }
152 DECLARATOR_TYPE;
153
154 typedef struct declarator
155   {
156     DECLARATOR_TYPE dcl_type;   /* POINTER,ARRAY or FUNCTION  */
157     unsigned int num_elem;      /* # of elems if type==array  */
158     short ptr_const:1;          /* pointer is constant        */
159     short ptr_volatile:1;       /* pointer is volatile        */
160     struct sym_link *tspec;     /* pointer type specifier      */
161   }
162 declarator;
163
164 #define DECLARATOR   0
165 #define SPECIFIER    1
166
167 typedef struct sym_link
168   {
169     unsigned class:1;           /* DECLARATOR or SPECIFIER    */
170     unsigned tdef:1;            /* current link created by    */
171     /* typedef if this flag is set */
172     union
173       {
174         specifier s;            /* if CLASS == SPECIFIER      */
175         declarator d;           /* if CLASS == DECLARATOR     */
176       }
177     select;
178
179     struct sym_link *next;      /* next element on the chain  */
180   }
181 sym_link;
182
183 typedef struct symbol
184   {
185     char name[SDCC_NAME_MAX + 1];       /* Input Variable Name     */
186     char rname[SDCC_NAME_MAX + 1];      /* internal name           */
187
188     short level;                /* declration lev,fld offset */
189     short block;                /* sequential block # of defintion */
190     int key;
191     unsigned fbody:1;           /* function body defined             */
192     unsigned implicit:1;        /* implicit flag                     */
193     unsigned undefined:1;       /* undefined variable                */
194     unsigned ret:1;             /* return statement for a function   */
195     unsigned hasVargs:1;        /* has a variable argument list      */
196     unsigned _isparm:1;         /* is a parameter          */
197     unsigned ismyparm:1;        /* is parameter of the function being generated */
198     unsigned isitmp:1;          /* is an intermediate temp */
199     unsigned islbl:1;           /* is a temporary label */
200     unsigned isref:1;           /* has been referenced  */
201     unsigned isind:1;           /* is a induction variable */
202     unsigned isinvariant:1;     /* is a loop invariant  */
203     unsigned isstrlit:1;        /* is a string literal  */
204     unsigned cdef:1;            /* compiler defined symbol */
205     unsigned allocreq:1;        /* allocation is required for this variable */
206     unsigned addrtaken:1;       /* address of the symbol was taken */
207     unsigned isreqv:1;          /* is the register quivalent of a symbol */
208     unsigned hasFcall:1;        /* for functions does it call other functions */
209     unsigned calleeSave:1;      /* for functions uses callee save paradigm */
210     unsigned udChked:1;         /* use def checking has been already done */
211
212     /* following flags are used by the backend
213        for code generation and can be changed
214        if a better scheme for backend is thought of */
215     unsigned isLiveFcall:1;     /* is live at or across a function call */
216     unsigned isspilt:1;         /* has to be spilt */
217     unsigned remat:1;           /* can be remateriazed */
218     unsigned isptr:1;           /* is a pointer */
219     unsigned uptr:1;            /* used as a pointer */
220     unsigned isFree:1;          /* used by register allocator */
221     unsigned islocal:1;         /* is a local variable        */
222     unsigned blockSpil:1;       /* spilt at block level       */
223     unsigned remainSpil:1;      /* spilt because not used in remainder */
224     unsigned stackSpil:1;       /* has been spilt on temp stack location */
225     unsigned onStack:1;         /* this symbol allocated on the stack */
226     unsigned iaccess:1;         /* indirect access      */
227     unsigned ruonly:1;          /* used in return statement only */
228     unsigned spildir:1;         /* spilt in direct space */
229     unsigned ptrreg:1;          /* this symbol assigned to a ptr reg */
230     unsigned accuse;            /* can be left in the accumulator
231                                    On the Z80 accuse is devided into
232                                    ACCUSE_A and ACCUSE_HL as the idea
233                                    is quite similar.
234                                  */
235
236     int stack;                  /* offset on stack      */
237     int xstack;                 /* offset on xternal stack */
238     short nRegs;                /* number of registers required */
239     short regType;              /* type of register required    */
240
241     struct regs *regs[4];       /* can have at the most 4 registers */
242     struct asmop *aop;          /* asmoperand for this symbol */
243     struct iCode *fuse;         /* furthest use */
244     struct iCode *rematiCode;   /* rematerialse with which instruction */
245     struct operand *reqv;       /* register equivalent of a local variable */
246     union
247       {
248         struct symbol *spillLoc;        /* register spil location */
249         struct set *itmpStack;  /* symbols spilt @ this stack location */
250       }
251     usl;
252     short bitVar;               /* this is a bit variable    */
253     unsigned offset;            /* offset from top if struct */
254
255     int lineDef;                /* defined line number        */
256     int lastLine;               /* for functions the last line */
257     struct sym_link *type;      /* 1st link to declator chain */
258     struct sym_link *etype;     /* last link to declarator chn */
259     struct value *args;         /* arguments if function      */
260     struct symbol *next;        /* crosslink to next symbol   */
261     struct symbol *localof;     /* local variable of which function */
262     struct initList *ival;      /* ptr to initializer if any  */
263     struct bitVect *defs;       /* bit vector for definitions */
264     struct bitVect *uses;       /* bit vector for uses        */
265     struct bitVect *regsUsed;   /* for functions registers used */
266     int liveFrom;               /* live from iCode sequence number */
267     int liveTo;                 /* live to sequence number */
268     int used;                   /* no. of times this was used */
269     int recvSize;               /* size of first argument  */
270   }
271 symbol;
272
273 /* Easy Access Macros */
274 #define DCL_TYPE(l)  l->select.d.dcl_type
275 #define DCL_ELEM(l)  l->select.d.num_elem
276 #define DCL_PTR_CONST(l) l->select.d.ptr_const
277 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
278 #define DCL_TSPEC(l) l->select.d.tspec
279 #define SPEC_NOUN(x) x->select.s.noun
280 #define SPEC_LONG(x) x->select.s._long
281 #define SPEC_USIGN(x) x->select.s._unsigned
282 #define SPEC_SCLS(x) x->select.s.sclass
283 #define SPEC_ENUM(x) x->select.s._isenum
284 #define SPEC_OCLS(x) x->select.s.oclass
285 #define SPEC_STAT(x) x->select.s._static
286 #define SPEC_EXTR(x) x->select.s._extern
287 #define SPEC_CODE(x) x->select.s._codesg
288 #define SPEC_RENT(x) x->select.s._reent
289 #define SPEC_INTN(x) x->select.s._IntNo
290 #define SPEC_ABSA(x) x->select.s._absadr
291 #define SPEC_BANK(x) x->select.s._regbank
292 #define SPEC_ADDR(x) x->select.s._addr
293 #define SPEC_STAK(x) x->select.s._stack
294 #define SPEC_CVAL(x) x->select.s.const_val
295 #define SPEC_BSTR(x) x->select.s._bitStart
296 #define SPEC_BLEN(x) x->select.s._bitLength
297
298 /* Sleaze: SPEC_ISR_SAVED_BANKS is only used on 
299  * function type symbols, which obviously cannot
300  * be of BIT type. Therefore, we recycle the 
301  * _bitStart field instead of defining a new field.
302  */
303 #define SPEC_ISR_SAVED_BANKS(x) x->select.s._bitStart
304 #define SPEC_BNKF(x) x->select.s._rbank
305 #define SPEC_INTRTN(x) x->select.s._intrtn
306 #define SPEC_CRTCL(x) x->select.s._critical
307 #define SPEC_NAKED(x) x->select.s._naked
308 #define SPEC_VOLATILE(x) x->select.s._volatile
309 #define SPEC_CONST(x) x->select.s._const
310 #define SPEC_STRUCT(x) x->select.s.v_struct
311 #define SPEC_TYPEDEF(x) x->select.s._typedef
312 #define SPEC_REGPARM(x) x->select.s._isregparm
313 #define SPEC_NONBANKED(x) x->select.s.nonbanked
314 #define SPEC_BANKED(x) x->select.s.banked
315
316 /* type check macros */
317 #define IS_DECL(x)   ( x && x->class == DECLARATOR      )
318 #define IS_SPEC(x)   ( x && x->class == SPECIFIER  )
319 #define IS_ARRAY(x)  (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
320 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
321 #define IS_PTR(x)    (IS_DECL(x) && (DCL_TYPE(x) == POINTER    ||    \
322                                      DCL_TYPE(x) == FPOINTER   ||    \
323                                      DCL_TYPE(x) == GPOINTER   ||    \
324                                      DCL_TYPE(x) == IPOINTER   ||    \
325                                      DCL_TYPE(x) == PPOINTER   ||    \
326                                      DCL_TYPE(x) == EEPPOINTER ||    \
327                                      DCL_TYPE(x) == CPOINTER   ||    \
328                                      DCL_TYPE(x) == UPOINTER  ))
329 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
330 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
331 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
332 #define IS_FUNC(x)   (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
333 #define IS_LONG(x)   (IS_SPEC(x) && x->select.s._long)
334 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
335 #define IS_CONSTANT(x)  (IS_SPEC(x) && ( x->select.s._const == 1))
336 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
337 #define IS_ABSOLUTE(x)  (IS_SPEC(x) && x->select.s._absadr )
338 #define IS_REGISTER(x)  (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
339 #define IS_RENT(x)   (IS_SPEC(x) && x->select.s._reent )
340 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
341 #define IS_INT(x)    (IS_SPEC(x) && x->select.s.noun == V_INT)
342 #define IS_VOID(x)   (IS_SPEC(x) && x->select.s.noun == V_VOID)
343 #define IS_CHAR(x)   (IS_SPEC(x) && x->select.s.noun == V_CHAR)
344 #define IS_EXTERN(x)    (IS_SPEC(x) && x->select.s._extern)
345 #define IS_VOLATILE(x)  (IS_SPEC(x) && x->select.s._volatile )
346 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT ||  \
347                                        x->select.s.noun == V_CHAR || \
348                                        x->select.s.noun == V_BIT ||  \
349                                        x->select.s.noun == V_SBIT ))
350 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
351 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun  == V_BIT ||   \
352                                      x->select.s.noun == V_SBIT ))
353 #define IS_FLOAT(x)  (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
354 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
355 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
356 #define IS_LITERAL(x)   (IS_SPEC(x)  && x->select.s.sclass == S_LITERAL)
357 #define IS_ISR(x)       (IS_SPEC(x)  && SPEC_INTRTN(x))
358 #define IS_REGPARM(x)   (IS_SPEC(x) && SPEC_REGPARM(x))
359 #define IS_NONBANKED(x) (IS_SPEC(x) && SPEC_NONBANKED(x))
360 #define IS_BANKED(x)    (IS_SPEC(x) && SPEC_BANKED(x))
361 #define IS_BANKEDCALL(x) (IS_SPEC(x) && !SPEC_NONBANKED(x) && !SPEC_STAT(x) && (options.model == MODEL_LARGE || options.model == MODEL_MEDIUM || SPEC_BANKED(x)))
362
363 /* forward declaration for the global vars */
364 extern bucket *SymbolTab[];
365 extern bucket *StructTab[];
366 extern bucket *TypedefTab[];
367 extern bucket *LabelTab[];
368 extern bucket *enumTab[];
369 extern symbol *__fsadd;
370 extern symbol *__fssub;
371 extern symbol *__fsmul;
372 extern symbol *__fsdiv;
373 extern symbol *__fseq;
374 extern symbol *__fsneq;
375 extern symbol *__fslt;
376 extern symbol *__fslteq;
377 extern symbol *__fsgt;
378 extern symbol *__fsgteq;
379
380 /* Dims: mul/div/mod, BYTE/WORD/DWORD, SIGNED/UNSIGNED */
381 extern symbol *__muldiv[3][3][2];
382 /* Dims: BYTE/WORD/DWORD SIGNED/UNSIGNED */
383 extern sym_link *__multypes[3][2];
384 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
385 extern symbol *__conv[2][3][2];
386
387 #define CHARTYPE        __multypes[0][0]
388 #define UCHARTYPE       __multypes[0][1]
389 #define INTTYPE         __multypes[1][0]
390 #define UINTTYPE        __multypes[1][1]
391 #define LONGTYPE        __multypes[2][0]
392 #define ULONGTYPE       __multypes[2][1]
393
394
395 extern sym_link *floatType;
396
397 #include "SDCCval.h"
398
399 /* forward definitions for the symbol table related functions */
400 void initSymt ();
401 symbol *newSymbol (char *, int);
402 sym_link *newLink ();
403 sym_link *newFloatLink ();
404 structdef *newStruct (char *);
405 void addDecl (symbol *, int, sym_link *);
406 sym_link *mergeSpec (sym_link *, sym_link *, char *name);
407 sym_link *cloneSpec (sym_link *);
408 symbol *reverseSyms (symbol *);
409 sym_link *reverseLink (sym_link *);
410 symbol *copySymbol (symbol *);
411 symbol *copySymbolChain (symbol *);
412 void printSymChain (symbol *, int);
413 void printStruct (structdef *, int);
414 char *genSymName (int);
415 sym_link *getSpec (sym_link *);
416 char *genSymName (int);
417 int compStructSize (int, structdef *);
418 sym_link *copyLinkChain (sym_link *);
419 int checkDecl (symbol *);
420 void checkBasic (sym_link *, sym_link *);
421 value *checkPointerIval (sym_link *, value *);
422 value *checkStructIval (symbol *, value *);
423 value *checkArrayIval (sym_link *, value *);
424 value *checkIval (sym_link *, value *);
425 unsigned int getSize (sym_link *);
426 unsigned int bitsForType (sym_link *);
427 sym_link *newIntLink ();
428 sym_link *newCharLink ();
429 sym_link *newLongLink ();
430 int checkType (sym_link *, sym_link *);
431 int checkFunction (symbol *);
432 void cleanUpLevel (bucket **, int);
433 void cleanUpBlock (bucket **, int);
434 int funcInChain (sym_link *);
435 void addSymChain (symbol *);
436 sym_link *structElemType (sym_link *, value *, value **);
437 symbol *getStructElement (structdef *, symbol *);
438 sym_link *computeType (sym_link *, sym_link *);
439 void processFuncArgs (symbol *, int);
440 int isSymbolEqual (symbol *, symbol *);
441 int powof2 (unsigned long);
442 void printTypeChain (sym_link *, FILE *);
443 void initCSupport ();
444 void pointerTypes (sym_link *, sym_link *);
445 void cdbTypeInfo (sym_link *, FILE *);
446 void cdbSymbol (symbol *, FILE *, int, int);
447 void cdbStructBlock (int, FILE *);
448 void initHashT ();
449 bucket *newBucket ();
450 void addSym (bucket **, void *, char *, int, int, int checkType);
451 void deleteSym (bucket **, void *, char *);
452 void *findSym (bucket **, void *, const char *);
453 void *findSymWithLevel (bucket **, struct symbol *);
454 void *findSymWithBlock (bucket **, struct symbol *, int);
455 void changePointer (symbol * sym);
456 void checkTypeSanity(sym_link *etype, char *name);
457 /* noun strings */
458 extern char *nounName(sym_link *);
459
460 #endif