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