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