c4e9c977f914947ce581f1498a1e76c3c1c5b9fa
[fw/sdcc] / src / SDCCglobl.h
1 /* SDCCglobl.h - global macros etc required by all files */
2 #ifndef SDCCGLOBL_H
3 #define SDCCGLOBL_H
4 #include <memory.h>
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <setjmp.h>
8 #include <stdio.h>
9 #include "sdccconf.h"
10 #include "SDCCerr.h"
11
12 #ifdef _NO_GC
13
14 #define GC_malloc malloc
15 #define GC_free free
16 #define GC_realloc realloc
17 #define GC_malloc_atomic malloc
18 #else
19
20 #include "./gc/gc.h" 
21
22 #endif
23
24 #define SPACE ' '
25 #define ZERO  0
26
27 #define  MAX_FNAME_LEN  128
28 #define  MAX_REG_PARMS  1
29 typedef int bool;
30
31 #ifndef max
32 #define max(a,b) (a > b ? a : b)
33 #endif
34
35 #ifndef min
36 #define min(a,b) (a < b ? a : b)
37 #endif
38
39 #ifndef THROWS
40 #define THROWS
41 #define THROW_NONE  0 
42 #define THROW_SRC   1 
43 #define THROW_DEST  2
44 #define THROW_BOTH  3
45 #endif
46
47 /* size's in bytes  */
48 #define CHARSIZE    1
49 #define SHORTSIZE   1
50 #define INTSIZE     2
51 #define LONGSIZE    4
52 #define PTRSIZE     1
53 #define FPTRSIZE    2
54 #define GPTRSIZE    3
55 #define BITSIZE     1
56 #define FLOATSIZE   4
57 #define MAXBASESIZE 4
58
59
60 #define PRAGMA_SAVE        "SAVE"
61 #define PRAGMA_RESTORE     "RESTORE"
62 #define PRAGMA_NOINDUCTION "NOINDUCTION"
63 #define PRAGMA_NOINVARIANT "NOINVARIANT"
64 #define PRAGMA_NOLOOPREV   "NOLOOPREVERSE"
65 #define PRAGMA_INDUCTION   "INDUCTION"
66 #define PRAGMA_STACKAUTO   "STACKAUTO"
67 #define PRAGMA_NOJTBOUND   "NOJTBOUND"
68 #define PRAGMA_NOGCSE      "NOGCSE"
69 #define PRAGMA_NOOVERLAY   "NOOVERLAY"
70 #define PRAGMA_CALLEESAVES "CALLEE-SAVES"
71 #define PRAGMA_EXCLUDE     "EXCLUDE"
72 #define PRAGMA_NOREGPARMS  "NOREGPARMS"
73 #define  SMALL_MODEL 0
74 #define  LARGE_MODEL 1
75 #define  TRUE 1
76 #define  FALSE 0
77
78 #define MAX_TVAR 6
79 #define MAX_INLINEASM 4*1024
80 #define DEFPOOLSTACK(type,size)     \
81     type       *type##Pool        ; \
82     type *type##FreeStack [size]  ; \
83     int   type##StackPtr = 0      ;
84
85 #define ALLOCTYPE(type,size) if (!(type##Pool = GC_malloc(size*sizeof(type))))   \
86          {                                                            \
87             werror(E_OUT_OF_MEM,__FILE__,(long) size);                           \
88             exit (1);                                                \
89          }
90
91 #define PUSH(x,y)   x##FreeStack[x##StackPtr++] = y
92 #define PEEK(x)     x##FreeStack[x##StackPtr-1]
93 #define POP(type)   type##FreeStack[--type##StackPtr]   
94 /* #define POP(x)    (x##StackPtr ? x##FreeStack[--x##StackPtr] :       \
95                   (assert(x##StackPtr),0)) */
96 #ifdef UNIX
97 #define EMPTY(x)        (x##StackPtr <= 1 ? 1 : 0)
98 #else
99 #define EMPTY(x)        (x##StackPtr == 0 ? 1 : 0)
100 #endif
101
102 #define  ALLOC(x,sz) if (!(x = GC_malloc(sz)))      \
103          {                                          \
104             werror(E_OUT_OF_MEM,__FILE__,(long) sz);\
105             exit (1);                               \
106          }
107
108 #define ALLOC_ATOMIC(x,sz)   if (!(x = GC_malloc_atomic(sz)))   \
109          {                                               \
110             werror(E_OUT_OF_MEM,__FILE__,(long) sz);     \
111             exit (1);                                    \
112          }
113
114 #define COPYTYPE(start,end,from) (end = getSpec (start = from))
115
116
117 /* generalpurpose stack related macros */
118 #define  STACK_DCL(stack,type,size)                   \
119          typedef  type  t_##stack   ;                 \
120          t_##stack   stack[size]    ;                 \
121          t_##stack   (*p_##stack) = stack + (size);   \
122
123 /* define extern stack */
124 #define EXTERN_STACK_DCL(stack,type,size)             \
125         typedef type t_##stack     ;                  \
126         extern t_##stack stack[size] ;                \
127         extern t_##stack *p_##stack; 
128
129 #define  STACK_FULL(stack)    ((p_##stack) <= stack )
130 #define  STACK_EMPTY(stack)   ((p_##stack) >= (stack +      \
131                               sizeof(stack)/sizeof(*stack)) )  
132
133 #define  STACK_PUSH_(stack,x) (*--p_##stack = (x))
134 #define  STACK_POP_(stack)    (*p_##stack++)
135
136 #define  STACK_PUSH(stack,x)  (STACK_FULL(stack)                  \
137                               ?((t_##stack)(long)(STACK_ERR(1)))  \
138                               : STACK_PUSH_(stack,x)              )
139
140 #define  STACK_POP(stack)     (STACK_EMPTY(stack)                 \
141                               ?((t_##stack)(long)(STACK_ERR(0)))  \
142                               : STACK_POP_(stack)                 )
143
144 #define  STACK_PEEK(stack)    (STACK_EMPTY(stack)                 \
145                               ?((t_##stack) NULL)                 \
146                               : *p_##stack                        )
147
148 #define  STACK_ERR(o)         ( o                                 \
149                               ? fprintf(stderr,"stack Overflow\n")\
150                               : fprintf(stderr,"stack underflow\n"))
151
152 /* optimization options */
153 struct optimize {
154     unsigned    global_cse : 1  ;
155     unsigned    ptrArithmetic :1;
156     unsigned    label1 : 1      ;
157     unsigned    label2 : 1     ;
158     unsigned    label3 : 1     ;
159     unsigned    label4 : 1     ;   
160     unsigned    loopInvariant: 1;
161     unsigned    loopInduction: 1;
162     unsigned    noJTabBoundary:1;
163     unsigned    noLoopReverse :1;
164 } ;
165
166 /* other command line options */
167 struct options {
168     int model  : 1     ; /* LARGE == 1 */
169     int stackAuto : 3  ; /* Stack Automatic  */
170     int useXstack : 3  ; /* use Xternal Stack */
171     int genericPtr: 1  ; /* use generic pointers */
172     int regExtend : 1  ; /* don't use extended registers */
173     int dump_raw  : 1  ; /* dump after intermediate code generation */
174     int dump_gcse : 1  ; /* dump after gcse */
175     int dump_loop : 1  ; /* dump after loop optimizations */
176     int dump_kill : 1  ; /* dump after dead code elimination */
177     int dump_range: 1  ; /* dump after live range analysis */
178     int dump_pack : 1  ; /* dump after register packing */
179     int dump_rassgn:1  ; /* dump after register assignment */
180     int cc_only   : 1  ; /* compile only flag              */
181     int intlong_rent:1 ; /* integer & long support routines reentrant */
182     int float_rent: 1  ; /* floating point routines are reentrant */
183     int out_fmt   : 1  ; /* 1 = motorola S19 format 0 = intel Hex format */
184     int cyclomatic: 1  ; /* print cyclomatic information */
185     int noOverlay : 1  ; /* don't overlay local variables & parameters */
186     int mainreturn: 1  ; /* issue a return after main */
187     int nopeep    : 1  ; /* no peep hole optimization */
188     int asmpeep   : 1  ; /* pass inline assembler thru peep hole */
189     int debug     : 1  ; /* generate extra debug info */
190     int stackOnData:1  ; /* stack after data segment  */
191     int noregparms: 1  ; /* do not pass parameters in registers */
192     char *peep_file    ; /* additional rules for peep hole */    
193
194     char *calleeSaves[128]; /* list of functions using callee save */
195     char *excludeRegs[32] ; /* registers excluded from saving */
196
197     /* starting address of the segments */
198     int xstack_loc     ; /* initial location of external stack */
199     int stack_loc      ; /* initial value of internal stack pointer */
200     int xdata_loc      ; /* xternal ram starts at address */
201     int data_loc       ; /* interram start location       */
202     int idata_loc      ; /* indirect address space        */
203     int code_loc       ; /* code location start           */    
204     int iram_size      ; /* internal ram size (used only for error checking) */
205 } ;
206
207 /* Processor specific names */
208 typedef struct {
209     /** Target name string, used for --help */
210     const char *target_name;
211     struct {
212         /** Command to run (eg as-z80) */
213         const char *exec_name;
214         /** Arguments for debug mode */
215         const char *debug_opts;
216         /** Arguments for normal assembly mode */
217         const char *plain_opts;
218         /** TRUE if the output file name should be pre-pended to the args */
219         bool requires_output_name;
220     } assembler;
221     struct {
222         /** Command to run (eg link-z80) */
223         const char *exec_name;
224     } linker;
225     /** Basic type sizes */
226     struct {
227         int char_size;
228         int short_size;
229         int int_size;
230         int long_size;
231         int ptr_size;
232         int fptr_size;
233         int gptr_size;
234         int bit_size;
235         int float_size;
236         int max_base_size;
237     } s;
238     struct {
239         /** -1 for grows down (z80), +1 for grows up (mcs51) */
240         int direction;
241         /** Extra overhead when calling between banks */
242         int bank_overhead;
243         /** Extra overhead when the function is an ISR */
244         int isr_overhead;
245         /** Standard overhead for a function call */
246         int call_overhead;
247         /** Initial SP offset */
248         int start_sp;
249     } stack;
250     struct {
251         /** One more than the smallest mul/div operation the processor can do nativley 
252             Eg if the processor has an 8 bit mul, nativebelow is 2 */
253         int nativebelow;
254         
255     } muldiv;
256
257 } PROCESSOR_CONSTANTS;
258
259 extern const PROCESSOR_CONSTANTS port;
260
261 /* forward definition for variables accessed globally */
262 extern char *currFname ;
263 extern char *srcFileName; /* source file name without the extenstion */
264 extern char *moduleName ; /* source file name without path & extension */
265 extern int currLineno ;   /* current line number    */
266 extern int yylineno  ;    /* line number of the current file SDCC.lex */
267 extern FILE *yyin    ;    /* */
268 extern FILE *asmFile ;    /* assembly output file */
269 extern FILE *cdbFile ;    /* debugger symbol file */
270 extern int NestLevel ;    /* NestLevel                 SDCC.y   */
271 extern int stackPtr  ;    /* stack pointer             SDCC.y   */
272 extern int xstackPtr ;    /* external stack pointer    SDCC.y   */
273 extern int reentrant ;    /* /X flag has been sent     SDCC.y */
274 extern char     buffer[]    ;/* general buffer      SDCCgen.c   */
275 extern int currRegBank;   /* register bank being used   SDCCgens.c   */
276 extern struct symbol  *currFunc;   /* current function    SDCCgens.c */
277 extern int      cNestLevel;      /* block nest level  SDCCval.c      */
278 extern int      currBlockno;     /* sequentail block number */
279 extern struct optimize optimize ;
280 extern struct options options;
281 extern int maxInterrupts;
282 void parseWithComma (char **,char *) ;
283
284 #endif