11761f6de805ba140a4964afc652777b0d5ea9c0
[fw/sdcc] / src / SDCCglobl.h
1 /* SDCCglobl.h - global macros etc required by all files */
2
3 #ifndef SDCCGLOBL_H
4 #define SDCCGLOBL_H
5
6 #include <memory.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <setjmp.h>
10 #include <stdio.h>
11
12 #include "SDCCset.h"
13
14
15 /*
16  * Define host port dependant constants etc.
17  */
18
19 #define UNIX_DIR_SEPARATOR_CHAR    '/'
20
21 #ifdef WIN32
22 # define NATIVE_WIN32          1
23 # ifndef __MINGW32__
24 #   define  PATH_MAX  _MAX_PATH
25 # endif
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #elif defined(_WIN32) && !defined(__MINGW32__)
31 # include "sdcc_vc.h"
32 #else
33 # include "sdccconf.h"
34 #endif
35
36 #include "SDCCerr.h"
37
38 #define SPACE ' '
39 #define ZERO  0
40
41 #include <limits.h>             /* PATH_MAX                  */
42 #if !defined(PATH_MAX) || (PATH_MAX < 2048)
43 #  undef  PATH_MAX
44 #  define PATH_MAX 2048         /* define a reasonable value */
45 #endif
46
47 #define  MAX_REG_PARMS  1
48 typedef int bool;
49
50 #ifndef max
51 #  define max(a,b) (a > b ? a : b)
52 #endif
53
54 #ifndef min
55 #  define min(a,b) (a < b ? a : b)
56 #endif
57
58 #ifndef THROWS
59 #define THROWS
60 #define THROW_NONE  0
61 #define THROW_SRC   1
62 #define THROW_DEST  2
63 #define THROW_BOTH  3
64 #endif
65
66 /* size's in bytes  */
67 #define CHARSIZE    port->s.char_size
68 #define SHORTSIZE   port->s.short_size
69 #define INTSIZE     port->s.int_size
70 #define LONGSIZE    port->s.long_size
71 #define PTRSIZE     port->s.ptr_size
72 #define FPTRSIZE    port->s.fptr_size
73 #define GPTRSIZE    port->s.gptr_size
74 #define BITSIZE     port->s.bit_size
75 #define FLOATSIZE   port->s.float_size
76 #define MAXBASESIZE port->s.max_base_size
77
78 #define  SMALL_MODEL 0
79 #define  LARGE_MODEL 1
80 #define  TRUE 1
81 #define  FALSE 0
82
83 #define MAX_TVAR 6
84 #define INITIAL_INLINEASM 4*1024
85 #define DEFPOOLSTACK(type,size)     \
86     type       *type##Pool        ; \
87     type *type##FreeStack [size]  ; \
88     int   type##StackPtr = 0      ;
89
90 #define PUSH(x,y)   x##FreeStack[x##StackPtr++] = y
91 #define PEEK(x)     x##FreeStack[x##StackPtr-1]
92 #define POP(type)   type##FreeStack[--type##StackPtr]
93 /* #define POP(x)    (x##StackPtr ? x##FreeStack[--x##StackPtr] :       \
94    (assert(x##StackPtr),0)) */
95 #ifdef UNIX
96 #define EMPTY(x)        (x##StackPtr <= 1 ? 1 : 0)
97 #else
98 #define EMPTY(x)        (x##StackPtr == 0 ? 1 : 0)
99 #endif
100
101
102 #define COPYTYPE(start,end,from) (end = getSpec (start = from))
103
104
105 /* general purpose stack related macros */
106 #define  STACK_DCL(stack,type,size)                                   \
107          typedef  type  t_##stack   ;                                 \
108          t_##stack   stack[size]    ;                                 \
109          t_##stack   (*p_##stack) = stack;
110
111 /* define extern stack */
112 #define EXTERN_STACK_DCL(stack,type,size)                             \
113         typedef type t_##stack     ;                                  \
114         extern t_##stack stack[size] ;                                \
115         extern t_##stack *p_##stack;
116
117 #define  STACK_EMPTY(stack)     ((p_##stack) <= stack )
118 #define  STACK_FULL(stack)      ((p_##stack) >= (stack +              \
119                                 sizeof(stack)/sizeof(*stack))         )
120
121 #define  STACK_PUSH_(stack, x)  (*++p_##stack = (x))
122 #define  STACK_POP_(stack)      (*p_##stack--)
123
124 #define  STACK_PUSH(stack, x)   (STACK_FULL(stack)                    \
125                                 ? (STACK_ERR(1, stack), *p_##stack)   \
126                                 : STACK_PUSH_(stack,x)                )
127
128 #define  STACK_POP(stack)       (STACK_EMPTY(stack)                   \
129                                 ? (STACK_ERR(-1, stack), *p_##stack)  \
130                                 : STACK_POP_(stack)                   )
131
132 #define  STACK_PEEK(stack)      (STACK_EMPTY(stack)                   \
133                                 ? (STACK_ERR(0, stack), *p_##stack)   \
134                                 : *p_##stack                          )
135
136 #define  STACK_ERR(o, stack)    (fatal(1, E_STACK_VIOLATION, #stack,  \
137                                         (o < 0)                       \
138                                         ? "underflow"                 \
139                                         : (o > 0)                     \
140                                           ? "overflow"                \
141                                           : "empty"))
142
143 /* optimization options */
144 /*
145  * cloneOptimize function in SDCC.lex should be updated every time
146  * a new set is added to the optimize structure!
147  */
148 struct optimize
149   {
150     int global_cse;
151     int ptrArithmetic;
152     int label1;
153     int label2;
154     int label3;
155     int label4;
156     int loopInvariant;
157     int loopInduction;
158     int noJTabBoundary;
159     int noLoopReverse;
160     int codeSpeed;
161     int codeSize;
162   };
163
164 /** Build model.
165     Used in options.model.A bit each as port.supported_models is an OR
166     of these.
167 */
168 enum
169   {
170     MODEL_SMALL = 1,
171     MODEL_COMPACT = 2,
172     MODEL_MEDIUM = 4,
173     MODEL_LARGE = 8,
174     MODEL_FLAT24 = 16,
175     MODEL_PAGE0 = 32, /* for the xa51 port */
176     MODEL_HUGE = 64 /* for banked support */
177   };
178
179 /* overlay segment name and the functions
180    that belong to it. used by pragma overlay */
181 typedef struct {
182     char *osname;       /* overlay segment name */
183     int  nfuncs;        /* number of functions in this overlay */
184     char *funcs[128];   /* function name that belong to this */
185 } olay;
186
187 /* other command line options */
188 /*
189  * cloneOptions function in SDCC.lex should be updated every time
190  * a new set is added to the options structure!
191  */
192 struct options
193   {
194     int model;                  /* see MODEL_* defines above */
195     int stackAuto;              /* Stack Automatic  */
196     int useXstack;              /* use Xternal Stack */
197     int stack10bit;             /* use 10 bit stack (flat24 model only) */
198     int dump_raw;               /* dump after intermediate code generation */
199     int dump_gcse;              /* dump after gcse */
200     int dump_loop;              /* dump after loop optimizations */
201     int dump_kill;              /* dump after dead code elimination */
202     int dump_range;             /* dump after live range analysis */
203     int dump_pack;              /* dump after register packing */
204     int dump_rassgn;            /* dump after register assignment */
205     int dump_tree;              /* dump front-end tree before lowering to iCode */
206     int cc_only;                /* compile only flag              */
207     int intlong_rent;           /* integer & long support routines reentrant */
208     int float_rent;             /* floating point routines are reentrant */
209     int out_fmt;                /* 1 = motorola S19 format 0 = intel Hex format */
210     int cyclomatic;             /* print cyclomatic information */
211     int noOverlay;              /* don't overlay local variables & parameters */
212     int mainreturn;             /* issue a return after main */
213     int xram_movc;              /* use movc instead of movx to read xram (mcs51) */
214     int nopeep;                 /* no peep hole optimization */
215     int asmpeep;                /* pass inline assembler thru peep hole */
216     int debug;                  /* generate extra debug info */
217     int c1mode;                 /* Act like c1 - no pre-proc, asm or link */
218     char *peep_file;            /* additional rules for peep hole */
219     int nostdlib;               /* Don't use standard lib files */
220     int nostdinc;               /* Don't use standard include files */
221     int noRegParams;            /* Disable passing some parameters in registers */
222     int verbose;                /* Show what the compiler is doing */
223     int shortis8bits;           /* treat short like int or char */
224     int lessPedantic;           /* disable some warnings */
225     int profile;                /* Turn on extra profiling information */
226     int ommitFramePtr;          /* Turn off the frame pointer. */
227     int useAccelerator;         /* use ds390 Arithmetic Accelerator */
228     int noiv;                   /* do not generate irq vector table entries */
229     int all_callee_saves;       /* callee saves for all functions */
230     int stack_probe;            /* insert call to function __stack_probe */
231     int tini_libid;             /* library ID for TINI */
232     int protect_sp_update;      /* DS390 - will disable interrupts during ESP:SP updates */
233     int parms_in_bank1;         /* DS390 - use reg bank1 to pass parameters */
234     int stack_size;             /* MCS51/DS390 - Tells the linker to allocate this space for stack */
235     int no_pack_iram;           /* MCS51/DS390 - Tells the linker not to pack variables in internal ram */
236     /* starting address of the segments */
237     int xstack_loc;             /* initial location of external stack */
238     int stack_loc;              /* initial value of internal stack pointer */
239     int xdata_loc;              /* xternal ram starts at address */
240     int data_loc;               /* interram start location       */
241     int idata_loc;              /* indirect address space        */
242     int code_loc;               /* code location start           */
243     int iram_size;              /* internal ram size (used only for error checking) */
244     int xram_size;              /* external ram size (used only for error checking) */
245     bool xram_size_set;         /* since xram_size=0 is a possibility */
246     int code_size;              /* code size (used only for error checking) */
247     int verboseExec;            /* show what we are doing */
248     int noXinitOpt;             /* don't optimize initialized xdata */
249     int noCcodeInAsm;           /* hide c-code from asm */
250     int iCodeInAsm;             /* show i-code in asm */
251     int noPeepComments;         /* hide peephole optimizer comments */
252     int printSearchDirs;        /* display the directories in the compiler's search path */
253     int vc_err_style;           /* errors and warnings are compatible with Micro$oft visual studio */
254     int use_stdout;             /* send errors to stdout instead of stderr */
255     int no_std_crt0;            /* for the z80/gbz80 do not link default crt0.o*/
256     int std_c99;                /* enable C99 keywords/constructs */
257     int std_sdcc;               /* enable SDCC extensions to C */
258     int dollars_in_ident;       /* zero means dollar signs are punctuation */
259     const char *code_seg;       /* segment name to use instead of CSEG */
260     const char *const_seg;      /* segment name to use instead of CONST */
261     /* sets */
262     set *calleeSavesSet;        /* list of functions using callee save */
263     set *excludeRegsSet;        /* registers excluded from saving */
264 /*  set *olaysSet;               * not implemented yet: overlay segments used in #pragma OVERLAY */
265   };
266
267 /* forward definition for variables accessed globally */
268 extern int noAssemble;          /* no assembly, stop after code generation */
269 extern char *yytext;
270 extern char *currFname;
271 extern char *fullSrcFileName;   /* full name for the source file; */
272                                 /* can be NULL while linking without compiling */
273 extern char *fullDstFileName;   /* full name for the output file; */
274                                 /* only given by -o, otherwise NULL */
275 extern char *dstFileName;       /* destination file name without extension */
276 extern char *dstPath;           /* path for the output files; */
277                                 /* "" is equivalent with cwd */
278 extern char *moduleName;        /* module name is source file without path and extension */
279                                 /* can be NULL while linking without compiling */
280 extern int seqPointNo;          /* current sequence point */
281 extern FILE *yyin;              /* */
282 extern FILE *asmFile;           /* assembly output file */
283 extern FILE *cdbFile;           /* debugger symbol file */
284 extern int NestLevel;           /* NestLevel                 SDCC.y   */
285 extern int stackPtr;            /* stack pointer             SDCC.y   */
286 extern int xstackPtr;           /* external stack pointer    SDCC.y   */
287 extern int reentrant;           /* /X flag has been sent     SDCC.y */
288 extern char buffer[PATH_MAX * 2];/* general buffer           SDCCmain.c   */
289 extern int currRegBank;         /* register bank being used  SDCCgens.c   */
290 extern int RegBankUsed[4];      /* JCF: register banks used  SDCCmain.c   */
291 extern int BitBankUsed;         /* MB: overlayable bit bank  SDCCmain.c   */
292 extern struct symbol *currFunc; /* current function    SDCCgens.c */
293 extern int cNestLevel;          /* block nest level  SDCCval.c      */
294 extern int currBlockno;         /* sequentail block number */
295 extern struct optimize optimize;
296 extern struct options options;
297 extern unsigned maxInterrupts;
298 extern int ignoreTypedefType;
299
300 /* Visible from SDCCmain.c */
301 extern set *preArgvSet;
302 extern set *relFilesSet;
303 extern set *libFilesSet;
304 extern set *libPathsSet;
305 extern set *libDirsSet;         /* list of lib search directories */
306
307 void setParseWithComma (set **, const char *);
308
309 /** An assert() macro that will go out through sdcc's error
310     system.
311 */
312 #define wassertl(a,s)   ((a) ? 0 : \
313         (werror (E_INTERNAL_ERROR,__FILE__,__LINE__, s), 0))
314
315 #define wassert(a)    wassertl(a,"code generator internal error")
316
317 #define DUMP_RAW0 1
318 #define DUMP_RAW1 DUMP_RAW0+1
319 #define DUMP_CSE DUMP_RAW1+1
320 #define DUMP_DFLOW DUMP_CSE+1
321 #define DUMP_GCSE DUMP_DFLOW+1
322 #define DUMP_DEADCODE DUMP_GCSE+1
323 #define DUMP_LOOP DUMP_DEADCODE+1
324 #define DUMP_LOOPG DUMP_LOOP+1
325 #define DUMP_LOOPD DUMP_LOOPG+1
326 #define DUMP_RANGE DUMP_LOOPD+1
327 #define DUMP_PACK DUMP_RANGE+1
328 #define DUMP_RASSGN DUMP_PACK+1
329 #define DUMP_LRANGE DUMP_RASSGN+1
330
331 struct _dumpFiles {
332   int id;
333   char *ext;
334   FILE *filePtr;
335 };
336
337 extern struct _dumpFiles dumpFiles[];
338
339 /* Buffer which can be used to hold a file name; assume it will
340  * be trashed by any function call within SDCC.
341  */
342 extern char scratchFileName[PATH_MAX];
343
344 /* Define well known filenos if the system does not define them.  */
345 #ifndef STDIN_FILENO
346 # define STDIN_FILENO   0
347 #endif
348 #ifndef STDOUT_FILENO
349 # define STDOUT_FILENO  1
350 #endif
351 #ifndef STDERR_FILENO
352 # define STDERR_FILENO  2
353 #endif
354
355 #endif