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