* src/SDCCglobl.h: ensure that PATH_MAX >= 2048 to guarantee
[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   };
176
177 /* overlay segment name and the functions
178    that belong to it. used by pragma overlay */
179 typedef struct {
180     char *osname;       /* overlay segment name */
181     int  nfuncs;        /* number of functions in this overlay */
182     char *funcs[128];   /* function name that belong to this */
183 } olay;
184
185 /* other command line options */
186 /*
187  * cloneOptions function in SDCC.lex should be updated every time
188  * a new set is added to the options structure!
189  */
190 struct options
191   {
192     int model;                  /* see MODEL_* defines above */
193     int stackAuto;              /* Stack Automatic  */
194     int useXstack;              /* use Xternal Stack */
195     int stack10bit;             /* use 10 bit stack (flat24 model only) */
196     int dump_raw;               /* dump after intermediate code generation */
197     int dump_gcse;              /* dump after gcse */
198     int dump_loop;              /* dump after loop optimizations */
199     int dump_kill;              /* dump after dead code elimination */
200     int dump_range;             /* dump after live range analysis */
201     int dump_pack;              /* dump after register packing */
202     int dump_rassgn;            /* dump after register assignment */
203     int dump_tree;              /* dump front-end tree before lowering to iCode */
204     int cc_only;                /* compile only flag              */
205     int intlong_rent;           /* integer & long support routines reentrant */
206     int float_rent;             /* floating point routines are reentrant */
207     int out_fmt;                /* 1 = motorola S19 format 0 = intel Hex format */
208     int cyclomatic;             /* print cyclomatic information */
209     int noOverlay;              /* don't overlay local variables & parameters */
210     int mainreturn;             /* issue a return after main */
211     int xram_movc;              /* use movc instead of movx to read xram (mcs51) */
212     int nopeep;                 /* no peep hole optimization */
213     int asmpeep;                /* pass inline assembler thru peep hole */
214     int debug;                  /* generate extra debug info */
215     int c1mode;                 /* Act like c1 - no pre-proc, asm or link */
216     char *peep_file;            /* additional rules for peep hole */
217     int nostdlib;               /* Don't use standard lib files */
218     int nostdinc;               /* Don't use standard include files */
219     int noRegParams;            /* Disable passing some parameters in registers */
220     int verbose;                /* Show what the compiler is doing */
221     int shortis8bits;           /* treat short like int or char */
222     int lessPedantic;           /* disable some warnings */
223     int profile;                /* Turn on extra profiling information */
224     int ommitFramePtr;          /* Turn off the frame pointer. */
225     int useAccelerator;         /* use ds390 Arithmetic Accelerator */
226     int noiv;                   /* do not generate irq vector table entries */
227     int all_callee_saves;       /* callee saves for all functions */
228     int stack_probe;            /* insert call to function __stack_probe */
229     int tini_libid;             /* library ID for TINI */
230     int protect_sp_update;      /* DS390 - will disable interrupts during ESP:SP updates */
231     int parms_in_bank1;         /* DS390 - use reg bank1 to pass parameters */
232     int stack_size;             /* MCS51/DS390 - Tells the linker to allocate this space for stack */
233     int no_pack_iram;           /* MCS51/DS390 - Tells the linker not to pack variables in internal ram */
234     /* starting address of the segments */
235     int xstack_loc;             /* initial location of external stack */
236     int stack_loc;              /* initial value of internal stack pointer */
237     int xdata_loc;              /* xternal ram starts at address */
238     int data_loc;               /* interram start location       */
239     int idata_loc;              /* indirect address space        */
240     int code_loc;               /* code location start           */
241     int iram_size;              /* internal ram size (used only for error checking) */
242     int xram_size;              /* external ram size (used only for error checking) */
243     bool xram_size_set;         /* since xram_size=0 is a possibility */
244     int code_size;              /* code size (used only for error checking) */
245     int verboseExec;            /* show what we are doing */
246     int noXinitOpt;             /* don't optimize initialized xdata */
247     int noCcodeInAsm;           /* hide c-code from asm */
248     int iCodeInAsm;             /* show i-code in asm */
249     int noPeepComments;         /* hide peephole optimizer comments */
250     int printSearchDirs;        /* display the directories in the compiler's search path */
251     int vc_err_style;           /* errors and warnings are compatible with Micro$oft visual studio */
252     int use_stdout;             /* send errors to stdout instead of stderr */
253     int no_std_crt0;            /*For the z80/gbz80 do not link default crt0.o*/
254     /* sets */
255     set *calleeSavesSet;        /* list of functions using callee save */
256     set *excludeRegsSet;        /* registers excluded from saving */
257 /*  set *olaysSet;               * not implemented yet: overlay segments used in #pragma OVERLAY */
258   };
259
260 /* forward definition for variables accessed globally */
261 extern int noAssemble;         /* no assembly, stop after code generation */
262 extern char *yytext;
263 extern char *currFname;
264 extern char *fullSrcFileName;   /* full name for the source file; */
265                                 /* can be NULL while linking without compiling */
266 extern char *fullDstFileName;   /* full name for the output file; */
267                                 /* only given by -o, otherwise NULL */
268 extern char *dstFileName;       /* destination file name without extension */
269 extern char *dstPath;           /* path for the output files; */
270                                 /* "" is equivalent with cwd */
271 extern char *moduleName;        /* module name is source file without path and extension */
272                                 /* can be NULL while linking without compiling */
273 extern int seqPointNo;          /* current sequence point */
274 extern int currLineno;          /* current line number    */
275 extern int mylineno;            /* line number of the current file SDCC.lex */
276 extern FILE *yyin;              /* */
277 extern FILE *asmFile;           /* assembly output file */
278 extern FILE *cdbFile;           /* debugger symbol file */
279 extern int NestLevel;           /* NestLevel                 SDCC.y   */
280 extern int stackPtr;            /* stack pointer             SDCC.y   */
281 extern int xstackPtr;           /* external stack pointer    SDCC.y   */
282 extern int reentrant;           /* /X flag has been sent     SDCC.y */
283 extern char buffer[PATH_MAX * 2];/* general buffer           SDCCmain.c   */
284 extern int currRegBank;         /* register bank being used  SDCCgens.c   */
285 extern int RegBankUsed[4];      /* JCF: register banks used  SDCCmain.c   */
286 extern struct symbol *currFunc; /* current function    SDCCgens.c */
287 extern int cNestLevel;          /* block nest level  SDCCval.c      */
288 extern int currBlockno;         /* sequentail block number */
289 extern struct optimize optimize;
290 extern struct options options;
291 extern unsigned maxInterrupts;
292 extern int ignoreTypedefType;
293
294 /* Visible from SDCCmain.c */
295 extern set *preArgvSet;
296 extern set *relFilesSet;
297 extern set *libFilesSet;
298 extern set *libPathsSet;
299 extern set *libDirsSet;         /* list of lib search directories */
300
301 void setParseWithComma (set **, char *);
302
303 /** Creates a temporary file a'la tmpfile which avoids the bugs
304     in cygwin wrt c:\tmp.
305     Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
306 */
307 FILE *tempfile (void);
308
309 /** Creates a temporary file name a'la tmpnam which avoids the bugs
310     in cygwin wrt c:\tmp.
311     Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
312 */
313 char *tempfilename (void);
314
315 /** An assert() macro that will go out through sdcc's error
316     system.
317 */
318 #define wassertl(a,s)   ((a) ? 0 : \
319         (werror (E_INTERNAL_ERROR,__FILE__,__LINE__, s), 0))
320
321 #define wassert(a)    wassertl(a,"code generator internal error")
322
323 #define DUMP_RAW0 1
324 #define DUMP_RAW1 DUMP_RAW0+1
325 #define DUMP_CSE DUMP_RAW1+1
326 #define DUMP_DFLOW DUMP_CSE+1
327 #define DUMP_GCSE DUMP_DFLOW+1
328 #define DUMP_DEADCODE DUMP_GCSE+1
329 #define DUMP_LOOP DUMP_DEADCODE+1
330 #define DUMP_LOOPG DUMP_LOOP+1
331 #define DUMP_LOOPD DUMP_LOOPG+1
332 #define DUMP_RANGE DUMP_LOOPD+1
333 #define DUMP_PACK DUMP_RANGE+1
334 #define DUMP_RASSGN DUMP_PACK+1
335 #define DUMP_LRANGE DUMP_RASSGN+1
336
337 struct _dumpFiles {
338   int id;
339   char *ext;
340   FILE *filePtr;
341 };
342
343 extern struct _dumpFiles dumpFiles[];
344
345 /* Buffer which can be used to hold a file name; assume it will
346  * be trashed by any function call within SDCC.
347  */
348 extern char scratchFileName[PATH_MAX];
349
350 /* Define well known filenos if the system does not define them.  */
351 #ifndef STDIN_FILENO
352 # define STDIN_FILENO   0
353 #endif
354 #ifndef STDOUT_FILENO
355 # define STDOUT_FILENO  1
356 #endif
357 #ifndef STDERR_FILENO
358 # define STDERR_FILENO  2
359 #endif
360
361 #endif