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