work on avr port start by defining the new
[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 enum  { V_INT   =  0,
54         V_FLOAT     ,
55         V_CHAR      ,
56         V_VOID      ,
57         V_STRUCT    ,
58         V_LABEL     ,
59         V_BIT       ,
60         V_SBIT      };
61
62 /* storage class    */
63 enum  { S_FIXED  =  0,
64         S_AUTO       ,
65         S_REGISTER   ,        
66         S_CONSTANT   ,
67         S_SFR        ,
68         S_SBIT       ,
69         S_CODE       ,
70         S_XDATA      ,
71         S_DATA       ,
72         S_IDATA      ,
73         S_PDATA      ,
74         S_LITERAL    ,
75         S_STACK      ,
76         S_XSTACK     ,
77         S_BIT        ,
78         S_FLASH      };
79
80 /* specifier is the last in the type-chain */
81 typedef struct specifier {
82     unsigned    noun        ;  /* CHAR INT STRUCTURE LABEL   */
83     unsigned    sclass      ;  /* REGISTER,AUTO,FIX,CONSTANT */
84     struct memmap *oclass   ;  /* output storage class       */
85     unsigned    _long : 1   ;  /* 1=long            */
86     unsigned    _short: 1       ;       /* 1=short int    */
87     unsigned _unsigned: 1   ;  /* 1=unsigned, 0=signed       */
88     unsigned   _static: 1   ;  /* 1=static keyword found     */
89     unsigned   _extern: 1   ;  /* 1=extern found             */
90     unsigned   _absadr: 1   ;  /* absolute address specfied  */
91     unsigned   _reent : 1   ;  /* function is reentrant      */
92     unsigned   _intrtn: 1   ;  /* this is an interrupt routin*/
93     unsigned   _rbank : 1   ;  /* seperate register bank     */
94     unsigned   _volatile : 1;  /* is marked as volatile      */
95     unsigned   _const:1     ;  /* is a constant              */
96     unsigned   _critical:1  ;  /* critical function          */
97     unsigned   _typedef :1  ;  /* is typedefed               */
98     unsigned   _isregparm:1 ;  /* is the first parameter     */
99     unsigned   _isenum   :1 ;  /* is an enumerated type      */
100     unsigned   _IntNo       ;  /* 1=Interrupt svc routine    */
101     short      _regbank     ;  /* register bank 2b used      */
102     unsigned   _addr        ;  /* address of symbol          */
103     unsigned   _stack       ;  /* stack offset for stacked v */
104     unsigned   _bitStart    ;  /* bit start position         */
105     int        _bitLength   ;  /* bit length                 */        
106     
107     union   {                 /* Values if constant or enum */ 
108         int            v_int ;  /* int and char values        */
109         char          *v_char;  /* character string           */
110         unsigned       v_uint;  /* unsigned int const value   */
111         long           v_long;  /* long constant value        */
112         unsigned long v_ulong;  /* unsigned long constant val */
113         double         v_float;  /* floating point constant value */
114         struct symbol *v_enum;  /* ptr 2 enum_list if enum==1 */      
115     } const_val ;
116     struct structdef *v_struct; /* structure pointer      */
117 } specifier ;
118
119 /* types of declarators */
120 enum {  POINTER   = 0,       /* pointer to near data */
121         FPOINTER     ,       /* pointer to far data  */
122         CPOINTER     ,       /* pointer to code space */
123         GPOINTER     ,       /* _generic pointer     */
124         PPOINTER     ,       /* paged area pointer   */
125         IPOINTER     ,       /* pointer to upper 128 bytes */
126         UPOINTER     ,       /* unknown pointer used only when parsing */
127         FLPOINTER    ,       /* pointer to flash     */
128         ARRAY        ,
129         FUNCTION     };
130
131 typedef struct declarator {
132     short    dcl_type;     /* POINTER,ARRAY or FUNCTION  */
133     short    num_elem;     /* # of elems if type==array  */
134     short    ptr_const :1;   /* pointer is constant        */
135     short    ptr_volatile:1; /* pointer is volatile        */
136     struct link *tspec;     /* pointer type specifier      */
137 } declarator ;
138
139 #define DECLARATOR   0
140 #define SPECIFIER    1
141
142 typedef struct link {
143         unsigned class : 1      ;  /* DECLARATOR or SPECIFIER    */
144         unsigned tdef  : 1      ;  /* current link created by    */
145                                    /* typedef if this flag is set*/
146         union {
147                 specifier      s     ;  /* if CLASS == SPECIFIER      */
148                 declarator     d     ;  /* if CLASS == DECLARATOR     */
149         } select ;
150
151         struct link    *next    ;  /* next element on the chain  */
152 } link ;
153
154 typedef struct symbol {
155     char name [SDCC_NAME_MAX+1] ;  /* Input Variable Name     */
156     char rname[SDCC_NAME_MAX+1]  ;  /* internal name           */
157     
158     short    level              ;  /* declration lev,fld offset */
159     short    block              ;  /* sequential block # of defintion */
160     int      key;
161     unsigned fbody      :1      ;  /* function body defined             */
162     unsigned implicit   :1      ;  /* implicit flag                     */
163     unsigned undefined  :1      ;  /* undefined variable                */
164     unsigned ret        :1      ;  /* return statement for a function   */
165     unsigned hasVargs   :1      ;  /* has a variable argument list      */
166     unsigned _isparm    :1      ;  /* is a parameter          */        
167     unsigned ismyparm   :1      ;  /* is parameter of the function being generated */   
168     unsigned isitmp     :1      ;  /* is an intermediate temp */
169     unsigned islbl      :1      ;  /* is a temporary label */
170     unsigned isref      :1      ;  /* has been referenced  */
171     unsigned isind      :1      ;  /* is a induction variable */
172     unsigned isinvariant:1      ;  /* is a loop invariant  */
173     unsigned isstrlit   :1      ;  /* is a string literal  */
174     unsigned cdef       :1      ;  /* compiler defined symbol */
175     unsigned allocreq   :1      ;  /* allocation is required for this variable */
176     unsigned addrtaken  :1      ;  /* address of the symbol was taken */
177     unsigned isreqv     :1      ;  /* is the register quivalent of a symbol */
178     unsigned hasFcall   :1      ;  /* for functions does it call other functions */
179     unsigned calleeSave :1      ;  /* for functions uses callee save paradigm */
180
181     /* following flags are used by the backend
182        for code generation and can be changed
183        if a better scheme for backend is thought of */
184
185     unsigned isspilt    :1      ;  /* has to be spilt */
186     unsigned remat      :1      ;  /* can be remateriazed */
187     unsigned isptr      :1      ;  /* is a pointer */
188     unsigned uptr       :1      ;  /* used as a pointer */
189     unsigned isFree     :1      ;  /* used by register allocator */
190     unsigned islocal    :1      ;  /* is a local variable        */
191     unsigned blockSpil  :1      ;  /* spilt at block level       */
192     unsigned remainSpil :1      ;  /* spilt because not used in remainder */
193     unsigned stackSpil  :1      ;  /* has been spilt on temp stack location */
194     unsigned onStack    :1      ;  /* this symbol allocated on the stack */
195     unsigned iaccess    :1      ;  /* indirect access      */
196     unsigned ruonly     :1      ;  /* used in return statement only */
197     unsigned accuse     :1      ;  /* can be left in the accumulator */
198     unsigned spildir    :1      ;  /* spilt in direct space */
199     unsigned ptrreg     :1      ;  /* this symbol assigned to a ptr reg */
200
201     int      stack              ;  /* offset on stack      */
202     int      xstack             ;  /* offset on xternal stack */
203     short    nRegs              ;  /* number of registers required */
204     short    regType            ;  /* type of register required    */
205     
206     struct regs *regs[4]        ;  /* can have at the most 4 registers */
207     struct asmop *aop           ;  /* asmoperand for this symbol */
208     struct iCode *fuse          ;  /* furthest use */
209     struct iCode *rematiCode    ;  /* rematerialse with which instruction */    
210     struct operand *reqv        ;  /* register equivalent of a local variable */
211     union {
212         struct  symbol *spillLoc;  /* register spil location */
213         struct  set    *itmpStack; /* symbols spilt @ this stack location */
214     } usl;
215     short      bitVar           ;  /* this is a bit variable    */
216     unsigned offset             ;  /* offset from top if struct */
217     
218     int      lineDef            ;  /* defined line number        */
219     int      lastLine           ;  /* for functions the last line*/
220     struct   link  *type        ;  /* 1st link to declator chain */
221     struct   link  *etype       ;  /* last link to declarator chn*/
222     struct   value *args        ;  /* arguments if function      */
223     struct   symbol *next       ;  /* crosslink to next symbol   */
224     struct   symbol *localof    ;  /* local variable of which function */
225     struct   initList *ival       ;  /* ptr to initializer if any  */
226     struct   bitVect *defs      ;  /* bit vector for definitions */
227     struct   bitVect *uses      ;  /* bit vector for uses        */        
228     struct   bitVect *regsUsed  ;  /* for functions registers used */
229     int      liveFrom ;            /* live from iCode sequence number */
230     int      liveTo   ;            /* live to sequence number */    
231     int      used     ;            /* no. of times this was used */
232     int      recvSize ;            /* size of first argument  */
233     int      argStack ;            /* stacks used by parameters */
234
235 } symbol ;
236
237 /* Easy Access Macros */
238 #define DCL_TYPE(l)  l->select.d.dcl_type
239 #define DCL_ELEM(l)  l->select.d.num_elem
240 #define DCL_PTR_CONST(l) l->select.d.ptr_const
241 #define DCL_PTR_VOLATILE(l) l->select.d.ptr_volatile
242 #define DCL_TSPEC(l) l->select.d.tspec
243 #define SPEC_NOUN(x) x->select.s.noun 
244 #define SPEC_LONG(x) x->select.s._long
245 #define SPEC_SHORT(x) x->select.s._short
246 #define SPEC_USIGN(x) x->select.s._unsigned
247 #define SPEC_SCLS(x) x->select.s.sclass
248 #define SPEC_ENUM(x) x->select.s._isenum
249 #define SPEC_OCLS(x) x->select.s.oclass 
250 #define SPEC_STAT(x) x->select.s._static
251 #define SPEC_EXTR(x) x->select.s._extern
252 #define SPEC_CODE(x) x->select.s._codesg
253 #define SPEC_RENT(x) x->select.s._reent
254 #define SPEC_INTN(x) x->select.s._IntNo
255 #define SPEC_ABSA(x) x->select.s._absadr
256 #define SPEC_BANK(x) x->select.s._regbank
257 #define SPEC_ADDR(x) x->select.s._addr
258 #define SPEC_STAK(x) x->select.s._stack
259 #define SPEC_CVAL(x) x->select.s.const_val
260 #define SPEC_BSTR(x) x->select.s._bitStart
261 #define SPEC_BLEN(x) x->select.s._bitLength
262 #define SPEC_BNKF(x) x->select.s._rbank
263 #define SPEC_INTRTN(x) x->select.s._intrtn
264 #define SPEC_CRTCL(x) x->select.s._critical
265 #define SPEC_VOLATILE(x) x->select.s._volatile
266 #define SPEC_CONST(x) x->select.s._const
267 #define SPEC_STRUCT(x) x->select.s.v_struct
268 #define SPEC_TYPEDEF(x) x->select.s._typedef
269 #define SPEC_REGPARM(x) x->select.s._isregparm
270
271 /* type check macros */
272 #define IS_DECL(x)   ( x && x->class == DECLARATOR      )
273 #define IS_SPEC(x)   ( x && x->class == SPECIFIER  )
274 #define IS_ARRAY(x)  (IS_DECL(x) && DCL_TYPE(x) == ARRAY)
275 #define IS_DATA_PTR(x) (IS_DECL(x) && DCL_TYPE(x) == POINTER)
276 #define IS_PTR(x)    (IS_DECL(x) && (DCL_TYPE(x) == POINTER    ||    \
277                                      DCL_TYPE(x) == FPOINTER   ||    \
278                                      DCL_TYPE(x) == GPOINTER   ||    \
279                                      DCL_TYPE(x) == IPOINTER   ||    \
280                                      DCL_TYPE(x) == PPOINTER   ||    \
281                                      DCL_TYPE(x) == CPOINTER   ||    \
282                                      DCL_TYPE(x) == UPOINTER  ))
283 #define IS_PTR_CONST(x) (IS_PTR(x) && DCL_PTR_CONST(x))
284 #define IS_FARPTR(x) (IS_DECL(x) && DCL_TYPE(x) == FPOINTER)
285 #define IS_GENPTR(x) (IS_DECL(x) && DCL_TYPE(x) == GPOINTER)
286 #define IS_FUNC(x)   (IS_DECL(x) && DCL_TYPE(x) == FUNCTION)
287 #define IS_LONG(x)   (IS_SPEC(x) && x->select.s._long)
288 #define IS_SHORT(x)   (IS_SPEC(x) && x->select.s._short)
289 #define IS_TYPEDEF(x)(IS_SPEC(x) && x->select.s._typedef)
290 #define IS_CONSTANT(x)  (IS_SPEC(x) && (x->select.s.sclass == S_CONSTANT ||\
291                                         x->select.s._const == 1))
292 #define IS_STRUCT(x) (IS_SPEC(x) && x->select.s.noun == V_STRUCT)
293 #define IS_ABSOLUTE(x)  (IS_SPEC(x) && x->select.s._absadr )
294 #define IS_REGISTER(x)  (IS_SPEC(x) && SPEC_SCLS(x) == S_REGISTER)
295 #define IS_RENT(x)   (IS_SPEC(x) && x->select.s._reent )
296 #define IS_STATIC(x) (IS_SPEC(x) && SPEC_STAT(x))
297 #define IS_INT(x)    (IS_SPEC(x) && x->select.s.noun == V_INT)
298 #define IS_VOID(x)   (IS_SPEC(x) && x->select.s.noun == V_VOID)
299 #define IS_CHAR(x)   (IS_SPEC(x) && x->select.s.noun == V_CHAR)
300 #define IS_EXTERN(x)    (IS_SPEC(x) && x->select.s._extern)
301 #define IS_VOLATILE(x)  (IS_SPEC(x) && x->select.s._volatile )
302 #define IS_INTEGRAL(x) (IS_SPEC(x) && (x->select.s.noun == V_INT ||  \
303                                        x->select.s.noun == V_CHAR || \
304                                        x->select.s.noun == V_BIT ||  \
305                                        x->select.s.noun == V_SBIT ))
306 #define IS_BITFIELD(x) (IS_SPEC(x) && (x->select.s.noun == V_BIT))
307 #define IS_BITVAR(x) (IS_SPEC(x) && (x->select.s.noun  == V_BIT ||   \
308                                      x->select.s.noun == V_SBIT ))
309 #define IS_FLOAT(x)  (IS_SPEC(x) && x->select.s.noun == V_FLOAT)
310 #define IS_ARITHMETIC(x) (IS_INTEGRAL(x) || IS_FLOAT(x))
311 #define IS_AGGREGATE(x) (IS_ARRAY(x) || IS_STRUCT(x))
312 #define IS_LITERAL(x)   (IS_SPEC(x)  && x->select.s.sclass == S_LITERAL)
313 #define IS_ISR(x)               (IS_SPEC(x)  && SPEC_INTRTN(x))
314 #define IS_REGPARM(x)   (IS_SPEC(x) && SPEC_REGPARM(x))
315
316 /* forward declaration for the global vars */
317 extern bucket *SymbolTab[] ;
318 extern bucket *StructTab[] ;
319 extern bucket *TypedefTab[];
320 extern bucket *LabelTab[]  ;
321 extern bucket *enumTab[];
322 extern symbol *__fsadd ;
323 extern symbol *__fssub ;
324 extern symbol *__fsmul ;
325 extern symbol *__fsdiv ;
326 extern symbol *__fseq  ;
327 extern symbol *__fsneq ;
328 extern symbol *__fslt  ;
329 extern symbol *__fslteq;
330 extern symbol *__fsgt  ;
331 extern symbol *__fsgteq;
332 extern symbol *__fs2uchar;
333 extern symbol *__fs2uint ;
334 extern symbol *__fs2ulong;
335 extern symbol *__fs2char;
336 extern symbol *__fs2int ;
337 extern symbol *__fs2long;
338 extern symbol *__long2fs;
339 extern symbol *__ulong2fs;
340 extern symbol *__int2fs;
341 extern symbol *__uint2fs;
342 extern symbol *__char2fs;
343 extern symbol *__uchar2fs;
344 extern symbol *__muluint;
345 extern symbol *__mulsint;
346 extern symbol *__divuint;
347 extern symbol *__divsint;
348 extern symbol *__mululong;
349 extern symbol *__mulslong;
350 extern symbol *__divulong;
351 extern symbol *__divslong;
352 extern symbol *__moduint;
353 extern symbol *__modsint;
354 extern symbol *__modulong;
355 extern symbol *__modslong;
356
357 extern link *charType ;
358 extern link *intType  ;
359 extern link *floatType;
360 extern link *longType ;
361 extern link *ucharType ;
362 extern link *uintType  ;
363 extern link *ulongType ;
364
365 #include "SDCCval.h"
366
367 /* forward definitions for the symbol table related functions */
368 void       initSymt           (                                );
369 symbol      *newSymbol        ( char      *, int               );
370 link        *newLink          (                                );
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 *);
415 void           initHashT            (                              );
416 bucket        *newBucket            (                              );
417 void           addSym               ( bucket ** , void   *, char  *, int, int);
418 void           deleteSym            ( bucket ** , void   *, char  *);
419 void          *findSym              ( bucket ** , void   *, char  *);
420 void          *findSymWithLevel     ( bucket ** , struct symbol *  );
421 void          *findSymWithBlock     ( bucket ** , struct symbol *,int   );
422 #include "SDCCmem.h"
423
424 #endif