Add support for ANSI integer promotion rules
[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 } bucket ;
44
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  */
50 } structdef ;
51
52 /* noun definitions */
53 typedef enum { 
54         V_INT   =  0,
55         V_FLOAT     ,
56         V_CHAR      ,
57         V_VOID      ,
58         V_STRUCT    ,
59         V_LABEL     ,
60         V_BIT       ,
61         V_SBIT      
62 } NOUN;
63
64 /* storage class    */
65 typedef enum { 
66         S_FIXED  =  0,
67         S_AUTO       ,
68         S_REGISTER   ,        
69         S_CONSTANT   ,
70         S_SFR        ,
71         S_SBIT       ,
72         S_CODE       ,
73         S_XDATA      ,
74         S_DATA       ,
75         S_IDATA      ,
76         S_PDATA      ,
77         S_LITERAL    ,
78         S_STACK      ,
79         S_XSTACK     ,
80         S_BIT        ,
81         S_EEPROM      
82 } STORAGE_CLASS;
83
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                 */        
112     
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 */      
121     } const_val ;
122     struct structdef *v_struct; /* structure pointer      */
123 } specifier ;
124
125 /* types of declarators */
126 typedef enum {
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     */
135         ARRAY        ,
136         FUNCTION     
137 } DECLARATOR_TYPE;
138
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 sym_link *tspec;         /* pointer type specifier      */
145 } declarator ;
146
147 #define DECLARATOR   0
148 #define SPECIFIER    1
149
150 typedef struct sym_link {
151         unsigned class : 1      ;  /* DECLARATOR or SPECIFIER    */
152         unsigned tdef  : 1      ;  /* current link created by    */
153                                    /* typedef if this flag is set*/
154         union {
155                 specifier      s     ;  /* if CLASS == SPECIFIER      */
156                 declarator     d     ;  /* if CLASS == DECLARATOR     */
157         } select ;
158
159         struct sym_link *next    ;  /* next element on the chain  */
160 } sym_link ;
161
162 typedef struct symbol {
163     char name [SDCC_NAME_MAX+1] ;  /* Input Variable Name     */
164     char rname[SDCC_NAME_MAX+1]  ;  /* internal name           */
165     
166     short    level              ;  /* declration lev,fld offset */
167     short    block              ;  /* sequential block # of defintion */
168     int      key;
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 */
189
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 */
193     unsigned isLiveFcall:1      ;  /* is live at or across a function call */
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
211                                       is quite similar.
212                                    */
213
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    */
218     
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 */
224     union {
225         struct  symbol *spillLoc;  /* register spil location */
226         struct  set    *itmpStack; /* symbols spilt @ this stack location */
227     } usl;
228     short      bitVar           ;  /* this is a bit variable    */
229     unsigned offset             ;  /* offset from top if struct */
230     
231     int      lineDef            ;  /* defined line number        */
232     int      lastLine           ;  /* for functions the last line*/
233     struct   sym_link  *type    ;  /* 1st link to declator chain */
234     struct   sym_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 */
247
248 } symbol ;
249
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
285
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)))
334
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;
351
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 sym_link *__multypes[3][2];
356 /* Dims: to/from float, BYTE/WORD/DWORD, SIGNED/USIGNED */
357 extern symbol *__conv[2][3][2];
358
359 #define CHARTYPE        __multypes[0][0]
360 #define UCHARTYPE       __multypes[0][1]
361 #define INTTYPE         __multypes[1][0]
362 #define UINTTYPE        __multypes[1][1]
363 #define LONGTYPE        __multypes[2][0]
364 #define ULONGTYPE       __multypes[2][1]
365
366
367 extern sym_link *floatType;
368
369 #include "SDCCval.h"
370
371 /* forward definitions for the symbol table related functions */
372 void       initSymt           (                                );
373 symbol      *newSymbol        ( char      *, int               );
374 sym_link    *newLink          (                                );
375 sym_link    *newFloatLink     (                                );
376 structdef   *newStruct        ( char      *                    );
377 void         addDecl          ( symbol   *, int   , sym_link * );
378 sym_link    *mergeSpec        ( sym_link      *, sym_link *    );
379 sym_link    *cloneSpec        ( sym_link  *                    );
380 symbol      *reverseSyms      ( symbol     *                   );
381 sym_link    *reverseLink      ( sym_link *                     );
382 symbol      *copySymbol       ( symbol     *                   );
383 symbol      *copySymbolChain  ( symbol     *                   );
384 void         printSymChain    ( symbol     *, int              );
385 void         printStruct      ( structdef *, int               );
386 char        *genSymName       ( int                            );
387 sym_link    *getSpec          ( sym_link     *                 );
388 char        *genSymName       ( int                            );
389 int          compStructSize   ( int    ,structdef *            );
390 sym_link    *copyLinkChain    ( sym_link    *                  );
391 int          checkDecl        ( symbol *                       );
392 void         checkBasic       ( sym_link   *, sym_link  *      );
393 value       *checkPointerIval ( sym_link   *, value *          );
394 value       *checkStructIval  ( symbol *, value *              );
395 value       *checkArrayIval   ( sym_link   *, value *          );
396 value       *checkIval        ( sym_link   *, value *          );
397 unsigned int getSize          ( sym_link   *                   );
398 unsigned int bitsForType      ( sym_link   *                   );
399 sym_link    *newIntLink       (                                );
400 sym_link    *newCharLink      (                                );
401 sym_link    *newLongLink      (                                );
402 int          checkType        ( sym_link   *, sym_link  *      );
403 int          checkFunction    ( symbol *                       );
404 void         cleanUpLevel     ( bucket **,int                  );
405 void         cleanUpBlock     ( bucket **,int                  );
406 int          funcInChain      ( sym_link   *                   );
407 void         addSymChain      ( symbol *                       );
408 sym_link    *structElemType   ( sym_link *, value * , value ** );
409 symbol      *getStructElement ( structdef *, symbol *) ;
410 sym_link    *computeType      ( sym_link *, sym_link *);
411 void         processFuncArgs  (symbol *,int);
412 int          isSymbolEqual    (symbol *, symbol *);
413 int          powof2           (unsigned long );
414 void         printTypeChain   (sym_link *,FILE *);
415 void         initCSupport     ();
416 void         pointerTypes     (sym_link *, sym_link * );
417 void         cdbTypeInfo      (sym_link *,FILE *);
418 void         cdbSymbol        (symbol *,FILE *,int,int);
419 void         cdbStructBlock   (int ,FILE *);
420 void           initHashT            (                              );
421 bucket        *newBucket            (                              );
422 void           addSym               ( bucket ** , void   *, char  *, int, int);
423 void           deleteSym            ( bucket ** , void   *, char  *);
424 void          *findSym              ( bucket ** , void   *, const char  *);
425 void          *findSymWithLevel     ( bucket ** , struct symbol *  );
426 void          *findSymWithBlock     ( bucket ** , struct symbol *,int   );
427 #include "SDCCmem.h"
428
429 #endif