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