cleanup; moved extern's from .c to .h files for double check
[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 __BORLANDC__
13 #define NATIVE_WIN32            1
14 #endif
15 #ifdef __MINGW32__
16 #define NATIVE_WIN32            1
17 #endif
18
19 #ifdef _NO_GC
20
21 #define GC_malloc malloc
22 #define GC_free free
23 #define GC_realloc realloc
24 #define GC_malloc_atomic malloc
25 #else
26
27 #include "./gc/gc.h" 
28
29 #endif
30
31 #define SPACE ' '
32 #define ZERO  0
33
34 #define  MAX_FNAME_LEN  128
35 #define  MAX_REG_PARMS  1
36 typedef int bool;
37
38 #ifndef max
39 #define max(a,b) (a > b ? a : b)
40 #endif
41
42 #ifndef min
43 #define min(a,b) (a < b ? a : b)
44 #endif
45
46 #ifndef THROWS
47 #define THROWS
48 #define THROW_NONE  0 
49 #define THROW_SRC   1 
50 #define THROW_DEST  2
51 #define THROW_BOTH  3
52 #endif
53
54 /* size's in bytes  */
55 #define CHARSIZE    port->s.char_size
56 #define SHORTSIZE   port->s.short_size
57 #define INTSIZE     port->s.int_size
58 #define LONGSIZE    port->s.long_size
59 #define PTRSIZE     port->s.ptr_size
60 #define FPTRSIZE    port->s.fptr_size
61 #define GPTRSIZE    port->s.gptr_size
62 #define BITSIZE     port->s.bit_size
63 #define FLOATSIZE   port->s.float_size
64 #define MAXBASESIZE port->s.max_base_size
65
66
67 #define PRAGMA_SAVE        "SAVE"
68 #define PRAGMA_RESTORE     "RESTORE"
69 #define PRAGMA_NOINDUCTION "NOINDUCTION"
70 #define PRAGMA_NOINVARIANT "NOINVARIANT"
71 #define PRAGMA_NOLOOPREV   "NOLOOPREVERSE"
72 #define PRAGMA_INDUCTION   "INDUCTION"
73 #define PRAGMA_STACKAUTO   "STACKAUTO"
74 #define PRAGMA_NOJTBOUND   "NOJTBOUND"
75 #define PRAGMA_NOGCSE      "NOGCSE"
76 #define PRAGMA_NOOVERLAY   "NOOVERLAY"
77 #define PRAGMA_CALLEESAVES "CALLEE-SAVES"
78 #define PRAGMA_EXCLUDE     "EXCLUDE"
79 #define PRAGMA_NOREGPARMS  "NOREGPARMS"
80 #define  SMALL_MODEL 0
81 #define  LARGE_MODEL 1
82 #define  TRUE 1
83 #define  FALSE 0
84
85 #define MAX_TVAR 6
86 #define MAX_INLINEASM 4*1024
87 #define DEFPOOLSTACK(type,size)     \
88     type       *type##Pool        ; \
89     type *type##FreeStack [size]  ; \
90     int   type##StackPtr = 0      ;
91
92 #define ALLOCTYPE(type,size) if (!(type##Pool = GC_malloc(size*sizeof(type))))   \
93          {                                                            \
94             werror(E_OUT_OF_MEM,__FILE__,(long) size);                           \
95             exit (1);                                                \
96          }
97
98 #define PUSH(x,y)   x##FreeStack[x##StackPtr++] = y
99 #define PEEK(x)     x##FreeStack[x##StackPtr-1]
100 #define POP(type)   type##FreeStack[--type##StackPtr]   
101 /* #define POP(x)    (x##StackPtr ? x##FreeStack[--x##StackPtr] :       \
102                   (assert(x##StackPtr),0)) */
103 #ifdef UNIX
104 #define EMPTY(x)        (x##StackPtr <= 1 ? 1 : 0)
105 #else
106 #define EMPTY(x)        (x##StackPtr == 0 ? 1 : 0)
107 #endif
108
109 #define  ALLOC(x,sz) if (!(x = GC_malloc(sz)))      \
110          {                                          \
111             werror(E_OUT_OF_MEM,__FILE__,(long) sz);\
112             exit (1);                               \
113          }
114
115 #define ALLOC_ATOMIC(x,sz)   if (!(x = GC_malloc_atomic(sz)))   \
116          {                                               \
117             werror(E_OUT_OF_MEM,__FILE__,(long) sz);     \
118             exit (1);                                    \
119          }
120
121 #define COPYTYPE(start,end,from) (end = getSpec (start = from))
122
123
124 /* generalpurpose stack related macros */
125 #define  STACK_DCL(stack,type,size)                   \
126          typedef  type  t_##stack   ;                 \
127          t_##stack   stack[size]    ;                 \
128          t_##stack   (*p_##stack) = stack + (size);   \
129
130 /* define extern stack */
131 #define EXTERN_STACK_DCL(stack,type,size)             \
132         typedef type t_##stack     ;                  \
133         extern t_##stack stack[size] ;                \
134         extern t_##stack *p_##stack; 
135
136 #define  STACK_FULL(stack)    ((p_##stack) <= stack )
137 #define  STACK_EMPTY(stack)   ((p_##stack) >= (stack +      \
138                               sizeof(stack)/sizeof(*stack)) )  
139
140 #define  STACK_PUSH_(stack,x) (*--p_##stack = (x))
141 #define  STACK_POP_(stack)    (*p_##stack++)
142
143 #define  STACK_PUSH(stack,x)  (STACK_FULL(stack)                  \
144                               ?((t_##stack)(long)(STACK_ERR(1)))  \
145                               : STACK_PUSH_(stack,x)              )
146
147 #define  STACK_POP(stack)     (STACK_EMPTY(stack)                 \
148                               ?((t_##stack)(long)(STACK_ERR(0)))  \
149                               : STACK_POP_(stack)                 )
150
151 #define  STACK_PEEK(stack)    (STACK_EMPTY(stack)                 \
152                               ?((t_##stack) NULL)                 \
153                               : *p_##stack                        )
154
155 #define  STACK_ERR(o)         ( o                                 \
156                               ? fprintf(stderr,"stack Overflow\n")\
157                               : fprintf(stderr,"stack underflow\n"))
158
159 /* optimization options */
160 struct optimize {
161     unsigned    global_cse : 1  ;
162     unsigned    ptrArithmetic :1;
163     unsigned    label1 : 1      ;
164     unsigned    label2 : 1     ;
165     unsigned    label3 : 1     ;
166     unsigned    label4 : 1     ;   
167     unsigned    loopInvariant: 1;
168     unsigned    loopInduction: 1;
169     unsigned    noJTabBoundary:1;
170     unsigned    noLoopReverse :1;
171 } ;
172
173 /** Build model.
174     Used in options.model.A bit each as port.supported_models is an OR
175     of these. 
176 */
177 enum {
178     MODEL_SMALL       = 1,
179     MODEL_COMPACT     = 2,
180     MODEL_MEDIUM      = 4,
181     MODEL_LARGE       = 8,
182     MODEL_FLAT24      = 16
183 };
184
185 /* other command line options */
186 struct options {
187     int model;           /* see MODEL_* defines above */
188     int stackAuto : 3  ; /* Stack Automatic  */
189     int useXstack : 3  ; /* use Xternal Stack */
190     int stack10bit : 3;  /* use 10 bit stack (flat24 model only) */
191     int genericPtr: 1  ; /* use generic pointers */
192     int regExtend : 1  ; /* don't use extended registers */
193     int dump_raw  : 1  ; /* dump after intermediate code generation */
194     int dump_gcse : 1  ; /* dump after gcse */
195     int dump_loop : 1  ; /* dump after loop optimizations */
196     int dump_kill : 1  ; /* dump after dead code elimination */
197     int dump_range: 1  ; /* dump after live range analysis */
198     int dump_pack : 1  ; /* dump after register packing */
199     int dump_rassgn:1  ; /* dump after register assignment */
200     int cc_only   : 1  ; /* compile only flag              */
201     int intlong_rent:1 ; /* integer & long support routines reentrant */
202     int float_rent: 1  ; /* floating point routines are reentrant */
203     int out_fmt   : 1  ; /* 1 = motorola S19 format 0 = intel Hex format */
204     int cyclomatic: 1  ; /* print cyclomatic information */
205     int noOverlay : 1  ; /* don't overlay local variables & parameters */
206     int mainreturn: 1  ; /* issue a return after main */
207     int nopeep    : 1  ; /* no peep hole optimization */
208     int asmpeep   : 1  ; /* pass inline assembler thru peep hole */
209     int debug     : 1  ; /* generate extra debug info */
210     int nodebug   : 1  ; /* Generate no debug info. */
211     int stackOnData:1  ; /* stack after data segment  */
212     int noregparms: 1  ; /* do not pass parameters in registers */
213     int c1mode    : 1  ; /* Act like c1 - no pre-proc, asm or link */
214     char *peep_file    ; /* additional rules for peep hole */
215     char *out_name     ; /* Asm output name for c1 mode */
216     int nostdlib  : 1  ; /* Don't use standard lib files */
217     int nostdinc  : 1  ; /* Don't use standard include files */
218     int verbose   : 1  ; /* Show what the compiler is doing */
219
220     char *calleeSaves[128]; /* list of functions using callee save */
221     char *excludeRegs[32] ; /* registers excluded from saving */
222
223     /* starting address of the segments */
224     int xstack_loc     ; /* initial location of external stack */
225     int stack_loc      ; /* initial value of internal stack pointer */
226     int xdata_loc      ; /* xternal ram starts at address */
227     int data_loc       ; /* interram start location       */
228     int idata_loc      ; /* indirect address space        */
229     int code_loc       ; /* code location start           */    
230     int iram_size      ; /* internal ram size (used only for error checking) */
231 } ;
232
233 /* forward definition for variables accessed globally */
234 extern char *currFname ;
235 extern char *srcFileName; /* source file name without the extenstion */
236 extern char *moduleName ; /* source file name without path & extension */
237 extern int currLineno ;   /* current line number    */
238 extern int yylineno  ;    /* line number of the current file SDCC.lex */
239 extern FILE *yyin    ;    /* */
240 extern FILE *asmFile ;    /* assembly output file */
241 extern FILE *cdbFile ;    /* debugger symbol file */
242 extern int NestLevel ;    /* NestLevel                 SDCC.y   */
243 extern int stackPtr  ;    /* stack pointer             SDCC.y   */
244 extern int xstackPtr ;    /* external stack pointer    SDCC.y   */
245 extern int reentrant ;    /* /X flag has been sent     SDCC.y */
246 extern char     buffer[]    ;/* general buffer      SDCCgen.c   */
247 extern int currRegBank;   /* register bank being used   SDCCgens.c   */
248 extern struct symbol  *currFunc;   /* current function    SDCCgens.c */
249 extern int      cNestLevel;      /* block nest level  SDCCval.c      */
250 extern int      currBlockno;     /* sequentail block number */
251 extern struct optimize optimize ;
252 extern struct options options;
253 extern int maxInterrupts;
254
255 /* Visible from SDCCmain.c */
256 extern int nrelFiles;
257 extern char *relFiles[128];
258 extern char *libFiles[128] ;
259 extern int nlibFiles;
260 void buildCmdLine(char *into, char **args, const char **cmds, 
261                           const char *p1, const char *p2, 
262                           const char *p3, const char **list);
263 int my_system (const char *cmd, char **cmd_argv);
264
265 void parseWithComma (char **,char *) ;
266
267 /** Creates a temporary file a'la tmpfile which avoids the bugs
268     in cygwin wrt c:\tmp.
269     Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
270 */
271 FILE *tempfile(void);
272
273 /** Creates a duplicate of the string 'sz' a'la strdup but using
274     libgc.
275 */
276 char *gc_strdup(const char *sz);
277
278 /** An assert() macro that will go out through sdcc's error
279     system.
280 */
281 #define wassertl(a,s)   ((a) ? 0 : \
282         (werror (E_INTERNAL_ERROR,__FILE__,__LINE__, s), 0))
283
284 #define wassert(a)    wassertl(a,"code generator internal error")
285
286
287 #endif