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