* src/Makefile.in, sim/ucsim/avr.src/Makefile.in,
[fw/sdcc] / src / SDCCmain.c
1 /*-------------------------------------------------------------------------
2   SDCCmain.c - main file
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #ifdef _WIN32
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30
31 #include <signal.h>
32 #include "common.h"
33 #include <ctype.h>
34 #include "newalloc.h"
35 #include "dbuf_string.h"
36 #include "SDCCerr.h"
37 #include "BuildCmd.h"
38 #include "MySystem.h"
39 #include "SDCCmacro.h"
40 #include "SDCCutil.h"
41 #include "SDCCdebug.h"
42 #include "SDCCargs.h"
43
44 #ifdef _WIN32
45 #include <process.h>
46 #else
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #endif
50
51 /* REMOVE ME!!! */
52 extern int yyparse (void);
53
54 FILE *srcFile;                  /* source file          */
55 char *fullSrcFileName;          /* full name for the source file; */
56                                 /* can be NULL while c1mode or linking without compiling */
57 char *fullDstFileName;          /* full name for the output file; */
58                                 /* only given by -o, otherwise NULL */
59 char *dstFileName;              /* destination file name without extension */
60 char *dstPath = "";             /* path for the output files; */
61                                 /* "" is equivalent with cwd */
62 char *moduleNameBase;           /* module name base is source file without path and extension */
63                                 /* can be NULL while linking without compiling */
64 char *moduleName;               /* module name is same as module name base, but with all */
65                                 /* non-alphanumeric characters replaced with underscore */
66 int currRegBank = 0;
67 int RegBankUsed[4] = {1, 0, 0, 0}; /*JCF: Reg Bank 0 used by default*/
68 int BitBankUsed;                /* MB: overlayable bit bank */
69 struct optimize optimize;
70 struct options options;
71 int preProcOnly = 0;
72 int noAssemble = 0;
73 set *preArgvSet = NULL;         /* pre-processor arguments  */
74 set *asmOptionsSet = NULL;      /* set of assembler options */
75 set *linkOptionsSet = NULL;     /* set of linker options */
76 set *libFilesSet = NULL;
77 set *libPathsSet = NULL;
78 set *relFilesSet = NULL;
79 set *dataDirsSet = NULL;        /* list of data search directories */
80 set *includeDirsSet = NULL;     /* list of include search directories */
81 set *userIncDirsSet = NULL;     /* list of user include directories */
82 set *libDirsSet = NULL;         /* list of lib search directories */
83
84 /* uncomment JAMIN_DS390 to always override and use ds390 port
85   for mcs51 work.  This is temporary, for compatibility testing. */
86 /* #define JAMIN_DS390 */
87 #ifdef JAMIN_DS390
88 int ds390_jammed = 0;
89 #endif
90
91 /* Globally accessible scratch buffer for file names.
92    TODO: replace them with local buffers */
93 char scratchFileName[PATH_MAX];
94 char buffer[PATH_MAX * 2];
95
96 #define LENGTH(_a)      (sizeof(_a)/sizeof(*(_a)))
97
98 #define OPTION_HELP             "--help"
99 #define OPTION_STACK_8BIT       "--stack-8bit"
100 #define OPTION_OUT_FMT_IHX      "--out-fmt-ihx"
101 #define OPTION_OUT_FMT_S19      "--out-fmt-s19"
102 #define OPTION_LARGE_MODEL      "--model-large"
103 #define OPTION_MEDIUM_MODEL     "--model-medium"
104 #define OPTION_SMALL_MODEL      "--model-small"
105 #define OPTION_FLAT24_MODEL     "--model-flat24"
106 #define OPTION_DUMP_ALL         "--dumpall"
107 #define OPTION_PEEP_FILE        "--peep-file"
108 #define OPTION_LIB_PATH         "--lib-path"
109 #define OPTION_CALLEE_SAVES     "--callee-saves"
110 #define OPTION_STACK_LOC        "--stack-loc"
111 #define OPTION_XSTACK_LOC       "--xstack-loc"
112 #define OPTION_DATA_LOC         "--data-loc"
113 #define OPTION_IDATA_LOC        "--idata-loc"
114 #define OPTION_XRAM_LOC         "--xram-loc"
115 #define OPTION_CODE_LOC         "--code-loc"
116 #define OPTION_STACK_SIZE       "--stack-size"
117 #define OPTION_IRAM_SIZE        "--iram-size"
118 #define OPTION_XRAM_SIZE        "--xram-size"
119 #define OPTION_CODE_SIZE        "--code-size"
120 #define OPTION_VERSION          "--version"
121 #define OPTION_NO_LABEL_OPT     "--nolabelopt"
122 #define OPTION_NO_LOOP_INV      "--noinvariant"
123 #define OPTION_NO_LOOP_IND      "--noinduction"
124 #define OPTION_LESS_PEDANTIC    "--less-pedantic"
125 #define OPTION_DISABLE_WARNING  "--disable-warning"
126 #define OPTION_NO_GCSE          "--nogcse"
127 #define OPTION_SHORT_IS_8BITS   "--short-is-8bits"
128 #define OPTION_TINI_LIBID       "--tini-libid"
129 #define OPTION_NO_XINIT_OPT     "--no-xinit-opt"
130 #define OPTION_NO_CCODE_IN_ASM  "--no-c-code-in-asm"
131 #define OPTION_ICODE_IN_ASM     "--i-code-in-asm"
132 #define OPTION_PRINT_SEARCH_DIRS "--print-search-dirs"
133 #define OPTION_MSVC_ERROR_STYLE "--vc"
134 #define OPTION_USE_STDOUT       "--use-stdout"
135 #define OPTION_PACK_IRAM        "--pack-iram"
136 #define OPTION_NO_PACK_IRAM     "--no-pack-iram"
137 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
138 #define OPTION_NO_GEN_COMMENTS  "--no-gen-comments"
139 #define OPTION_OPT_CODE_SPEED   "--opt-code-speed"
140 #define OPTION_OPT_CODE_SIZE    "--opt-code-size"
141 #define OPTION_STD_C89          "--std-c89"
142 #define OPTION_STD_C99          "--std-c99"
143 #define OPTION_STD_SDCC89       "--std-sdcc89"
144 #define OPTION_STD_SDCC99       "--std-sdcc99"
145 #define OPTION_CODE_SEG         "--codeseg"
146 #define OPTION_CONST_SEG        "--constseg"
147 #define OPTION_DOLLARS_IN_IDENT "--fdollars-in-identifiers"
148 #define OPTION_UNSIGNED_CHAR    "--funsigned-char"
149
150 static const OPTION
151 optionsTable[] = {
152     { 0,    NULL,                   NULL, "General options" },
153     { 0,    OPTION_HELP,            NULL, "Display this help" },
154     { 'v',  OPTION_VERSION,         NULL, "Display sdcc's version" },
155     { 0,    "--verbose",            &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
156     { 'V',  NULL,                   &options.verboseExec, "Execute verbosely.  Show sub commands as they are run" },
157     { 'd',  NULL,                   NULL, NULL },
158     { 'D',  NULL,                   NULL, "Define macro as in -Dmacro" },
159     { 'I',  NULL,                   NULL, "Add to the include (*.h) path, as in -Ipath" },
160     { 'A',  NULL,                   NULL, NULL },
161     { 'U',  NULL,                   NULL, NULL },
162     { 'C',  NULL,                   NULL, "Preprocessor option" },
163     { 'M',  NULL,                   NULL, "Preprocessor option" },
164     { 'W',  NULL,                   NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
165     { 'S',  NULL,                   &noAssemble, "Compile only; do not assemble or link" },
166     { 'c',  "--compile-only",       &options.cc_only, "Compile and assemble, but do not link" },
167     { 'E',  "--preprocessonly",     &preProcOnly, "Preprocess only, do not compile" },
168     { 0,    "--c1mode",             &options.c1mode, "Act in c1 mode.  The standard input is preprocessed code, the output is assembly code." },
169     { 'o',  NULL,                   NULL, "Place the output into the given path resp. file" },
170     { 0,    OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
171     { 0,    OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
172     { 0,    OPTION_USE_STDOUT,      NULL, "send errors to stdout instead of stderr"},
173     { 0,    "--nostdlib",           &options.nostdlib, "Do not include the standard library directory in the search path" },
174     { 0,    "--nostdinc",           &options.nostdinc, "Do not include the standard include directory in the search path" },
175     { 0,    OPTION_LESS_PEDANTIC,   NULL, "Disable some of the more pedantic warnings" },
176     { 0,    OPTION_DISABLE_WARNING, NULL, "<nnnn> Disable specific warning" },
177     { 0,    "--debug",              &options.debug, "Enable debugging symbol output" },
178     { 0,    "--cyclomatic",         &options.cyclomatic, "Display complexity of compiled functions" },
179     { 0,    OPTION_STD_C89,         NULL, "Use C89 standard only" },
180     { 0,    OPTION_STD_SDCC89,      NULL, "Use C89 standard with SDCC extensions (default)" },
181     { 0,    OPTION_STD_C99,         NULL, "Use C99 standard only (incomplete)" },
182     { 0,    OPTION_STD_SDCC99,      NULL, "Use C99 standard with SDCC extensions (incomplete)" },
183     { 0,    OPTION_DOLLARS_IN_IDENT, &options.dollars_in_ident, "Permit '$' as an identifier character" },
184     { 0,    OPTION_UNSIGNED_CHAR,   &options.unsigned_char, "Make \"char\" unsigned by default" },
185
186     { 0,    NULL,                   NULL, "Code generation options"},
187     { 'm',  NULL,                   NULL, "Set the port to use e.g. -mz80." },
188     { 'p',  NULL,                   NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
189     { 0,    OPTION_LARGE_MODEL,     NULL, "external data space is used" },
190     { 0,    OPTION_MEDIUM_MODEL,    NULL, "external paged data space is used" },
191     { 0,    OPTION_SMALL_MODEL,     NULL, "internal data space is used (default)" },
192 #if !OPT_DISABLE_DS390
193     { 0,    OPTION_FLAT24_MODEL,    NULL, "use the flat24 model for the ds390 (default)" },
194     { 0,    OPTION_STACK_8BIT,      NULL, "use the 8bit stack for the ds390 (not supported yet)" },
195     { 0,    "--stack-10bit",        &options.stack10bit, "use the 10bit stack for ds390 (default)" },
196 #endif
197     { 0,    "--stack-auto",         &options.stackAuto, "Stack automatic variables" },
198     { 0,    "--xstack",             &options.useXstack, "Use external stack" },
199     { 0,    "--int-long-reent",     &options.intlong_rent, "Use reenterant calls on the int and long support functions" },
200     { 0,    "--float-reent",        &options.float_rent, "Use reenterant calls on the float support functions" },
201     { 0,    "--main-return",        &options.mainreturn, "Issue a return after main()" },
202     { 0,    "--xram-movc",          &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
203     { 0,    OPTION_CALLEE_SAVES,    NULL, "<func[,func,...]> Cause the called function to save registers insted of the caller" },
204     { 0,    "--profile",            &options.profile, "On supported ports, generate extra profiling information" },
205     { 0,    "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
206     { 0,    "--all-callee-saves",   &options.all_callee_saves, "callee will always save registers used" },
207 #if !OPT_DISABLE_DS390
208     { 0,    "--use-accelerator",    &options.useAccelerator,"generate code for  DS390 Arithmetic Accelerator"},
209 #endif
210     { 0,    "--stack-probe",        &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
211 #if !OPT_DISABLE_TININative
212     { 0,    "--tini-libid",         NULL,"<nnnn> LibraryID used in -mTININative"},
213 #endif
214 #if !OPT_DISABLE_DS390
215     { 0,    "--protect-sp-update",  &options.protect_sp_update,"DS390 - will disable interrupts during ESP:SP updates"},
216 #endif
217 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
218     { 0,    "--parms-in-bank1",     &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
219 #endif
220     { 0,    OPTION_NO_XINIT_OPT,    &options.noXinitOpt, "don't memcpy initialized xram from code"},
221     { 0,    OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
222     { 0,    OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
223     { 0,    OPTION_NO_GEN_COMMENTS, &options.noGenComments, "don't include code generator comments"},
224 #if !OPT_DISABLE_Z80 || !OPT_DISABLE_GBZ80
225     { 0,    "--no-std-crt0", &options.no_std_crt0, "For the z80/gbz80 do not link default crt0.o"},
226 #endif
227     { 0,    OPTION_SHORT_IS_8BITS,  NULL, "Make short 8 bits (for old times sake)" },
228     { 0,    OPTION_CODE_SEG,        NULL, "<name> use this name for the code segment" },
229     { 0,    OPTION_CONST_SEG,       NULL, "<name> use this name for the const segment" },
230
231     { 0,    NULL,                   NULL, "Optimization options"},
232     { 0,    "--nooverlay",          &options.noOverlay, "Disable overlaying leaf function auto variables" },
233     { 0,    OPTION_NO_GCSE,         NULL, "Disable the GCSE optimisation" },
234     { 0,    OPTION_NO_LABEL_OPT,    NULL, "Disable label optimisation" },
235     { 0,    OPTION_NO_LOOP_INV,     NULL, "Disable optimisation of invariants" },
236     { 0,    OPTION_NO_LOOP_IND,     NULL, "Disable loop variable induction" },
237     { 0,    "--nojtbound",          &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
238     { 0,    "--noloopreverse",      &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
239     { 0,    "--no-peep",            &options.nopeep, "Disable the peephole assembly file optimisation" },
240     { 0,    "--no-reg-params",      &options.noRegParams, "On some ports, disable passing some parameters in registers" },
241     { 0,    "--peep-asm",           &options.asmpeep, "Enable peephole optimization on inline assembly" },
242     { 0,    OPTION_PEEP_FILE,       NULL, "<file> use this extra peephole file" },
243     { 0,    OPTION_OPT_CODE_SPEED,  NULL, "Optimize for code speed rather than size" },
244     { 0,    OPTION_OPT_CODE_SIZE,   NULL, "Optimize for code size rather than speed" },
245
246     { 0,    NULL,                   NULL, "Internal debugging options"},
247     { 0,    "--dumpraw",            &options.dump_raw, "Dump the internal structure after the initial parse" },
248     { 0,    "--dumpgcse",           &options.dump_gcse, NULL },
249     { 0,    "--dumploop",           &options.dump_loop, NULL },
250     { 0,    "--dumpdeadcode",       &options.dump_kill, NULL },
251     { 0,    "--dumpliverange",      &options.dump_range, NULL },
252     { 0,    "--dumpregpack",        &options.dump_pack, NULL },
253     { 0,    "--dumpregassign",      &options.dump_rassgn, NULL },
254     { 0,    "--dumptree",           &options.dump_tree, "dump front-end AST before generating iCode" },
255     { 0,    OPTION_DUMP_ALL,        NULL, "Dump the internal structure at all stages" },
256     { 0,    OPTION_ICODE_IN_ASM,    &options.iCodeInAsm, "include i-code as comments in the asm file"},
257
258     { 0,    NULL,                   NULL, "Linker options" },
259     { 'l',  NULL,                   NULL, "Include the given library in the link" },
260     { 'L',  NULL,                   NULL, "Add the next field to the library search path" },
261     { 0,    OPTION_LIB_PATH,        NULL, "<path> use this path to search for libraries" },
262     { 0,    OPTION_OUT_FMT_IHX,     NULL, "Output in Intel hex format" },
263     { 0,    OPTION_OUT_FMT_S19,     NULL, "Output in S19 hex format" },
264     { 0,    OPTION_XRAM_LOC,        NULL, "<nnnn> External Ram start location" },
265     { 0,    OPTION_XRAM_SIZE,       NULL, "<nnnn> External Ram size" },
266     { 0,    OPTION_IRAM_SIZE,       NULL, "<nnnn> Internal Ram size" },
267     { 0,    OPTION_XSTACK_LOC,      NULL, "<nnnn> External Stack start location" },
268     { 0,    OPTION_CODE_LOC,        NULL, "<nnnn> Code Segment Location" },
269     { 0,    OPTION_CODE_SIZE,       NULL, "<nnnn> Code Segment size" },
270     { 0,    OPTION_STACK_LOC,       NULL, "<nnnn> Stack pointer initial value" },
271     { 0,    OPTION_DATA_LOC,        NULL, "<nnnn> Direct data start location" },
272     { 0,    OPTION_IDATA_LOC,       NULL, NULL },
273 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51 || !OPT_DISABLE_PIC
274     { 0,    OPTION_STACK_SIZE,      NULL,"MCS51/DS390/PIC - Tells the linker to allocate this space for stack"},
275 #endif
276 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
277     { 0,    OPTION_PACK_IRAM,       NULL,"MCS51/DS390 - Tells the linker to pack variables in internal ram (default)"},
278     { 0,    OPTION_NO_PACK_IRAM,    &options.no_pack_iram,"MCS51/DS390 - Tells the linker not to pack variables in internal ram"},
279 #endif
280
281     /* End of options */
282     { 0,    NULL }
283 };
284
285 /** Table of all unsupported options and help text to display when one
286     is used.
287 */
288 typedef struct {
289     /** shortOpt as in OPTIONS. */
290     char shortOpt;
291     /** longOpt as in OPTIONS. */
292     const char *longOpt;
293     /** Message to display inside W_UNSUPPORTED_OPT when this option
294         is used. */
295     const char *message;
296 } UNSUPPORTEDOPT;
297
298 static const UNSUPPORTEDOPT
299 unsupportedOptTable[] = {
300     { 'X',  NULL,       "use --xstack-loc instead" },
301     { 'x',  NULL,       "use --xstack instead" },
302     { 'i',  NULL,       "use --idata-loc instead" },
303     { 'r',  NULL,       "use --xdata-loc instead" },
304     { 's',  NULL,       "use --code-loc instead" },
305     { 'Y',  NULL,       "use -I instead" }
306 };
307
308 /** List of all default constant macros.
309  */
310 static const char *_baseValues[] = {
311   "cpp", "sdcpp",
312   "cppextraopts", "",
313   /* Path seperator character */
314   "sep", DIR_SEPARATOR_STRING,
315   NULL
316 };
317
318 static const char *_preCmd = "{cpp} -nostdinc -Wall -std=c99 {cppextraopts} \"{fullsrcfilename}\" \"{cppoutfilename}\"";
319
320 PORT *port;
321
322 static PORT *_ports[] =
323 {
324 #if !OPT_DISABLE_MCS51
325   &mcs51_port,
326 #endif
327 #if !OPT_DISABLE_GBZ80
328   &gbz80_port,
329 #endif
330 #if !OPT_DISABLE_Z80
331   &z80_port,
332 #endif
333 #if !OPT_DISABLE_AVR
334   &avr_port,
335 #endif
336 #if !OPT_DISABLE_DS390
337   &ds390_port,
338 #endif
339 #if !OPT_DISABLE_PIC16
340   &pic16_port,
341 #endif
342 #if !OPT_DISABLE_PIC
343   &pic_port,
344 #endif
345 #if !OPT_DISABLE_TININative
346   &tininative_port,
347 #endif
348 #if !OPT_DISABLE_XA51
349   &xa51_port,
350 #endif
351 #if !OPT_DISABLE_DS400
352   &ds400_port,
353 #endif
354 #if !OPT_DISABLE_HC08
355   &hc08_port,
356 #endif
357 };
358
359 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
360
361 /** Sets the port to the one given by the command line option.
362     @param    The name minus the option (eg 'mcs51')
363     @return     0 on success.
364 */
365 static void
366 _setPort (const char *name)
367 {
368   int i;
369   for (i = 0; i < NUM_PORTS; i++)
370     {
371       if (!strcmp (_ports[i]->target, name))
372         {
373           port = _ports[i];
374           return;
375         }
376     }
377   /* Error - didnt find */
378   werror (E_UNKNOWN_TARGET, name);
379   exit (1);
380 }
381
382 /* Override the default processor with the one specified
383  * on the command line */
384 static void
385 _setProcessor (char *_processor)
386 {
387   port->processor = _processor;
388 }
389
390 static void
391 _validatePorts (void)
392 {
393   int i;
394   for (i = 0; i < NUM_PORTS; i++)
395     {
396       if (_ports[i]->magic != PORT_MAGIC)
397         {
398           /* Uncomment this line to debug which port is causing the problem
399            * (the target name is close to the beginning of the port struct
400            * and probably can be accessed just fine). */
401           fprintf(stderr,"%s :",_ports[i]->target);
402           wassertl (0, "Port definition structure is incomplete");
403         }
404     }
405 }
406
407 /* search through the command line options for the port */
408 static void
409 _findPort (int argc, char **argv)
410 {
411   _validatePorts ();
412
413   while (argc--)
414     {
415       if (!strncmp (*argv, "-m", 2))
416         {
417           _setPort (*argv + 2);
418           return;
419         }
420       argv++;
421     }
422
423   /* Use the first in the list */
424   port = _ports[0];
425 }
426
427 /* search through the command line options for the processor */
428 static void
429 _findProcessor (int argc, char **argv)
430 {
431   while (argc--)
432     {
433       if (!strncmp (*argv, "-p", 2))
434         {
435           _setProcessor (*argv + 2);
436           return;
437         }
438       argv++;
439     }
440
441   /* no error if processor was not specified. */
442 }
443
444 /*-----------------------------------------------------------------*/
445 /* printVersionInfo - prints the version info        */
446 /*-----------------------------------------------------------------*/
447 void
448 printVersionInfo (FILE *stream)
449 {
450   int i;
451
452   fprintf (stream,
453            "SDCC : ");
454   for (i = 0; i < NUM_PORTS; i++)
455     fprintf (stream, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
456
457   fprintf (stream, " " SDCC_VERSION_STR
458 #ifdef SDCC_SUB_VERSION_STR
459            "/" SDCC_SUB_VERSION_STR
460 #endif
461            " #%s (" __DATE__ ")"
462 #ifdef __CYGWIN__
463            " (CYGWIN)\n"
464 #elif defined __MINGW32__
465            " (MINGW32)\n"
466 #elif defined __DJGPP__
467            " (DJGPP)\n"
468 #elif defined(_MSC_VER)
469            " (MSVC)\n"
470 #elif defined(__BORLANDC__)
471            " (BORLANDC)\n"
472 #else
473            " (UNIX) \n"
474 #endif
475     , getBuildNumber() );
476 }
477
478 static void
479 printOptions(const OPTION *optionsTable, FILE *stream)
480 {
481   int i;
482   for (i = 0;
483        optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
484        || optionsTable[i].help != NULL;
485        i++)
486     {
487       if (!optionsTable[i].shortOpt && !optionsTable[i].longOpt
488           && optionsTable[i].help)
489         {
490           fprintf (stream, "\n%s:\n", optionsTable[i].help);
491         }
492       else
493         {
494           fprintf(stream, "  %c%c  %-20s  %s\n",
495                   optionsTable[i].shortOpt !=0 ? '-' : ' ',
496                   optionsTable[i].shortOpt !=0 ? optionsTable[i].shortOpt : ' ',
497                   optionsTable[i].longOpt != NULL ? optionsTable[i].longOpt : "",
498                   optionsTable[i].help != NULL ? optionsTable[i].help : ""
499                   );
500         }
501     }
502 }
503
504 /*-----------------------------------------------------------------*/
505 /* printUsage - prints command line syntax         */
506 /*-----------------------------------------------------------------*/
507 static void
508 printUsage (void)
509 {
510     int i;
511     FILE *stream = stderr;
512
513     printVersionInfo (stream);
514     fprintf (stream,
515              "Usage : sdcc [options] filename\n"
516              "Options :-\n"
517              );
518
519     printOptions (optionsTable, stream);
520
521     for (i = 0; i < NUM_PORTS; i++)
522       {
523         if (_ports[i]->poptions != NULL)
524           {
525             fprintf (stream, "\nSpecial options for the %s port:\n", _ports[i]->target);
526             printOptions (_ports[i]->poptions, stream);
527           }
528       }
529 }
530
531 /*-----------------------------------------------------------------*/
532 /* setParseWithComma - separates string with comma to a set        */
533 /*-----------------------------------------------------------------*/
534 void
535 setParseWithComma (set **dest, const char *src)
536 {
537   const char *p, *end;
538   struct dbuf_s dbuf;
539
540   /* skip the initial white spaces */
541   while (isspace((unsigned char)*src))
542     ++src;
543
544   /* skip the trailing white spaces */
545   end = &src[strlen(src) - 1];
546   while (end >= src && isspace((unsigned char)*end))
547     --end;
548   ++end;
549
550   p = src;
551   while (src < end)
552     {
553       dbuf_init (&dbuf, 16);
554
555       while (p < end && ',' != *p)
556         ++p;
557       dbuf_append (&dbuf, src, p - src);
558
559       /* null terminate the buffer */
560       dbuf_c_str (&dbuf);
561       addSet(dest, dbuf_detach (&dbuf));
562
563       src = ++p;
564     }
565 }
566
567 /*-----------------------------------------------------------------*/
568 /* setDefaultOptions - sets the default options                    */
569 /*-----------------------------------------------------------------*/
570 static void
571 setDefaultOptions (void)
572 {
573   /* first the options part */
574   options.stack_loc = 0;          /* stack pointer initialised to 0 */
575   options.xstack_loc = 0;         /* xternal stack starts at 0 */
576   options.code_loc = 0;           /* code starts at 0 */
577   options.data_loc = 0;           /* JCF: By default let the linker locate data */
578   options.xdata_loc = 0;
579   options.idata_loc = 0;          /* MB: No need to limit idata to 0x80-0xFF */
580   options.nopeep = 0;
581   options.model = port->general.default_model;
582   options.nostdlib = 0;
583   options.nostdinc = 0;
584   options.verbose = 0;
585   options.shortis8bits = 0;
586   options.std_sdcc = 1;           /* enable SDCC language extensions */
587   options.std_c99 = 0;            /* default to C89 until more C99 support */
588   options.code_seg = CODE_NAME;   /* default to CSEG for generated code */
589   options.const_seg = CONST_NAME; /* default to CONST for generated code */
590
591   options.stack10bit=0;
592
593   /* now for the optimizations */
594   /* turn on the everything */
595   optimize.global_cse = 1;
596   optimize.label1 = 1;
597   optimize.label2 = 1;
598   optimize.label3 = 1;
599   optimize.label4 = 1;
600   optimize.loopInvariant = 1;
601   optimize.loopInduction = 1;
602
603   /* now for the ports */
604   port->setDefaultOptions ();
605 }
606
607 /*-----------------------------------------------------------------*/
608 /* processFile - determines the type of file from the extension    */
609 /*-----------------------------------------------------------------*/
610 static void
611 processFile (char *s)
612 {
613   const char *extp;
614   struct dbuf_s ext;
615   struct dbuf_s path;
616
617   dbuf_init (&ext, 128);
618   dbuf_init (&path, 128);
619
620   /* get the file extension.
621      If no '.' then we don't know what the file type is
622      so give a warning and return */
623   if (!dbuf_splitFile (s, &path, &ext))
624     {
625       werror (W_UNKNOWN_FEXT, s);
626
627       dbuf_destroy (&ext);
628       dbuf_destroy (&path);
629
630       return;
631     }
632
633   /* otherwise depending on the file type */
634   extp = dbuf_c_str (&ext);
635   if (STRCASECMP (extp, ".c") == 0)
636     {
637       char *p;
638
639       dbuf_destroy (&ext);
640
641       /* source file name : not if we already have a
642          source file */
643       if (fullSrcFileName)
644         {
645           werror (W_TOO_MANY_SRC, s);
646
647           dbuf_destroy (&path);
648
649           return;
650         }
651
652       /* the only source file */
653       fullSrcFileName = s;
654       if (!(srcFile = fopen (fullSrcFileName, "r")))
655         {
656           werror (E_FILE_OPEN_ERR, s);
657
658           dbuf_destroy (&path);
659
660           exit (1);
661         }
662
663       /* get rid of any path information
664          for the module name; */
665       dbuf_init (&ext, 128);
666
667       dbuf_splitPath (dbuf_c_str (&path), NULL, &ext);
668       dbuf_destroy (&path);
669
670       moduleNameBase = Safe_strdup (dbuf_c_str (&ext));
671       moduleName = dbuf_detach (&ext);
672
673       for (p = moduleName; *p; ++p)
674         if (!isalnum ((unsigned char)*p))
675           *p = '_';
676       return;
677     }
678
679   /* if the extention is type .rel or .r or .REL or .R
680      additional object file will be passed to the linker */
681   if (STRCASECMP (extp, ".r") == 0 || STRCASECMP (extp, ".rel") == 0 ||
682       strcmp (extp, port->linker.rel_ext) == 0)
683     {
684       dbuf_destroy (&ext);
685       dbuf_destroy (&path);
686
687       addSet (&relFilesSet, Safe_strdup (s));
688       return;
689     }
690
691   /* if .lib or .LIB */
692   if (STRCASECMP (extp, ".lib") == 0)
693     {
694       dbuf_destroy (&ext);
695       dbuf_destroy (&path);
696
697       addSet (&libFilesSet, Safe_strdup (s));
698       return;
699     }
700
701   dbuf_destroy (&ext);
702   dbuf_destroy (&path);
703
704   werror (W_UNKNOWN_FEXT, s);
705 }
706
707 static void
708 _setModel (int model, const char *sz)
709 {
710   if (port->general.supported_models & model)
711     options.model = model;
712   else
713     werror (W_UNSUPPORTED_MODEL, sz, port->target);
714 }
715
716 /** Gets the string argument to this option.  If the option is '--opt'
717     then for input of '--optxyz' or '--opt xyz' returns xyz.
718 */
719 char *
720 getStringArg(const char *szStart, char **argv, int *pi, int argc)
721 {
722   if (argv[*pi][strlen(szStart)])
723     {
724       return &argv[*pi][strlen(szStart)];
725     }
726   else
727     {
728       ++(*pi);
729       if (*pi >= argc)
730         {
731           werror (E_ARGUMENT_MISSING, szStart);
732           /* Die here rather than checking for errors later. */
733           exit (EXIT_FAILURE);
734         }
735       else
736         {
737           return argv[*pi];
738         }
739     }
740 }
741
742 /** Gets the integer argument to this option using the same rules as
743     getStringArg.
744 */
745 int
746 getIntArg(const char *szStart, char **argv, int *pi, int argc)
747 {
748   return (int)floatFromVal(constVal(getStringArg(szStart, argv, pi, argc)));
749 }
750
751 static void
752 verifyShortOption(const char *opt)
753 {
754   if (strlen(opt) != 2)
755     {
756       werror (W_EXCESS_SHORT_OPTIONS, opt);
757     }
758 }
759
760 static bool
761 tryHandleUnsupportedOpt(char **argv, int *pi)
762 {
763   if (argv[*pi][0] == '-')
764     {
765       const char *longOpt = "";
766       char shortOpt = -1;
767       int i;
768
769       if (argv[*pi][1] == '-')
770         {
771           /* Long option. */
772           longOpt = argv[*pi];
773         }
774       else
775         {
776           shortOpt = argv[*pi][1];
777         }
778       for (i = 0; i < LENGTH(unsupportedOptTable); i++)
779         {
780           if (unsupportedOptTable[i].shortOpt == shortOpt ||
781               (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt)))
782             {
783               /* Found an unsupported opt. */
784               char buffer[100];
785               SNPRINTF(buffer, sizeof(buffer),
786                 "%s%c%c",
787                 longOpt ? longOpt : "",
788                 shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
789               werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
790               return 1;
791             }
792         }
793       /* Didn't find in the table */
794       return 0;
795     }
796   else
797     {
798       /* Not an option, so can't be unsupported :) */
799       return 0;
800     }
801 }
802
803 static bool
804 scanOptionsTable(const OPTION *optionsTable, char shortOpt, const char *longOpt, char **argv, int *pi)
805 {
806   int i;
807   for (i = 0;
808        optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
809        || optionsTable[i].help != NULL;
810        i++)
811     {
812       if (optionsTable[i].shortOpt == shortOpt ||
813           (longOpt && optionsTable[i].longOpt &&
814            strcmp(optionsTable[i].longOpt, longOpt) == 0))
815         {
816
817           /* If it is a flag then we can handle it here */
818           if (optionsTable[i].pparameter != NULL)
819             {
820               if (optionsTable[i].shortOpt == shortOpt)
821                 {
822                   verifyShortOption(argv[*pi]);
823                 }
824
825               (*optionsTable[i].pparameter)++;
826               return 1;
827             }
828           else {
829             /* Not a flag.  Handled manually later. */
830             return 0;
831           }
832         }
833     }
834   /* Didn't find in the table */
835   return 0;
836 }
837
838 static bool
839 tryHandleSimpleOpt(char **argv, int *pi)
840 {
841     if (argv[*pi][0] == '-')
842         {
843             const char *longOpt = "";
844             char shortOpt = -1;
845
846             if (argv[*pi][1] == '-')
847                 {
848                     /* Long option. */
849                     longOpt = argv[*pi];
850                 }
851             else
852                 {
853                     shortOpt = argv[*pi][1];
854                 }
855
856             if (scanOptionsTable(optionsTable, shortOpt, longOpt, argv, pi))
857               {
858                 return 1;
859               }
860             else if (port && port->poptions &&
861                      scanOptionsTable(port->poptions, shortOpt, longOpt, argv, pi))
862               {
863                 return 1;
864               }
865             else
866               {
867                 return 0;
868               }
869         }
870     else
871         {
872             /* Not an option, so can't be handled. */
873             return 0;
874         }
875 }
876
877 /*-----------------------------------------------------------------*/
878 /* parseCmdLine - parses the command line and sets the options     */
879 /*-----------------------------------------------------------------*/
880 static int
881 parseCmdLine (int argc, char **argv)
882 {
883   int i;
884
885   /* go thru all whole command line */
886   for (i = 1; i < argc; i++)
887     {
888       if (i >= argc)
889         break;
890
891       if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
892         {
893           continue;
894         }
895
896       if (tryHandleSimpleOpt(argv, &i) == TRUE)
897         {
898           continue;
899         }
900
901       /* options */
902       if (argv[i][0] == '-' && argv[i][1] == '-')
903         {
904           if (strcmp (argv[i], OPTION_USE_STDOUT) == 0)
905             {
906               if (options.use_stdout == 0)
907                 {
908                   options.use_stdout = 1;
909                   dup2(STDOUT_FILENO, STDERR_FILENO);
910                 }
911               continue;
912             }
913           if (strcmp (argv[i], OPTION_HELP) == 0)
914             {
915               printUsage ();
916               exit (EXIT_SUCCESS);
917             }
918
919           if (strcmp (argv[i], OPTION_STACK_8BIT) == 0)
920             {
921               options.stack10bit = 0;
922               continue;
923             }
924
925           if (strcmp (argv[i], OPTION_OUT_FMT_IHX) == 0)
926             {
927               options.out_fmt = 0;
928               continue;
929             }
930
931           if (strcmp (argv[i], OPTION_OUT_FMT_S19) == 0)
932             {
933               options.out_fmt = 1;
934               continue;
935             }
936
937           if (strcmp (argv[i], OPTION_LARGE_MODEL) == 0)
938             {
939               _setModel (MODEL_LARGE, argv[i]);
940               continue;
941             }
942
943           if (strcmp (argv[i], OPTION_MEDIUM_MODEL) == 0)
944             {
945               _setModel (MODEL_MEDIUM, argv[i]);
946               continue;
947             }
948
949           if (strcmp (argv[i], OPTION_SMALL_MODEL) == 0)
950             {
951               _setModel (MODEL_SMALL, argv[i]);
952               continue;
953             }
954
955           if (strcmp (argv[i], OPTION_FLAT24_MODEL) == 0)
956             {
957               _setModel (MODEL_FLAT24, argv[i]);
958               continue;
959             }
960
961           if (strcmp (argv[i], OPTION_DUMP_ALL) == 0)
962             {
963               options.dump_rassgn =
964                 options.dump_pack =
965                 options.dump_range =
966                 options.dump_kill =
967                 options.dump_loop =
968                 options.dump_gcse =
969                 options.dump_raw = 1;
970               continue;
971             }
972
973           if (strcmp (argv[i], OPTION_PEEP_FILE) == 0)
974             {
975               options.peep_file = getStringArg(OPTION_PEEP_FILE, argv, &i, argc);
976               continue;
977             }
978
979           if (strcmp (argv[i], OPTION_LIB_PATH) == 0)
980             {
981               addSet(&libPathsSet, Safe_strdup(getStringArg(OPTION_LIB_PATH, argv, &i, argc)));
982               continue;
983             }
984
985           if (strcmp (argv[i], OPTION_VERSION) == 0)
986             {
987               printVersionInfo (stdout);
988               exit (EXIT_SUCCESS);
989               continue;
990             }
991
992           if (strcmp (argv[i], OPTION_CALLEE_SAVES) == 0)
993             {
994               setParseWithComma(&options.calleeSavesSet, getStringArg(OPTION_CALLEE_SAVES, argv, &i, argc));
995               continue;
996             }
997
998           if (strcmp (argv[i], OPTION_XSTACK_LOC) == 0)
999             {
1000               options.xstack_loc = getIntArg(OPTION_XSTACK_LOC, argv, &i, argc);
1001               continue;
1002             }
1003
1004           if (strcmp (argv[i], OPTION_STACK_LOC) == 0)
1005             {
1006               options.stack_loc = getIntArg(OPTION_STACK_LOC, argv, &i, argc);
1007               continue;
1008             }
1009
1010           if (strcmp (argv[i], OPTION_STACK_SIZE) == 0)
1011             {
1012               options.stack_size = getIntArg(OPTION_STACK_SIZE, argv, &i, argc);
1013               continue;
1014             }
1015
1016           if (strcmp (argv[i], OPTION_XRAM_LOC) == 0)
1017             {
1018               options.xdata_loc = getIntArg(OPTION_XRAM_LOC, argv, &i, argc);
1019               continue;
1020             }
1021
1022           if (strcmp (argv[i], OPTION_IRAM_SIZE) == 0)
1023             {
1024               options.iram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
1025               continue;
1026             }
1027
1028           if (strcmp (argv[i], OPTION_XRAM_SIZE) == 0)
1029             {
1030               options.xram_size = getIntArg(OPTION_XRAM_SIZE, argv, &i, argc);
1031               options.xram_size_set = TRUE;
1032               continue;
1033             }
1034
1035           if (strcmp (argv[i], OPTION_CODE_SIZE) == 0)
1036             {
1037               options.code_size = getIntArg(OPTION_CODE_SIZE, argv, &i, argc);
1038               continue;
1039             }
1040
1041           if (strcmp (argv[i], OPTION_DATA_LOC) == 0)
1042             {
1043               options.data_loc = getIntArg(OPTION_DATA_LOC, argv, &i, argc);
1044               continue;
1045             }
1046
1047           if (strcmp (argv[i], OPTION_IDATA_LOC) == 0)
1048             {
1049               options.idata_loc = getIntArg(OPTION_IDATA_LOC, argv, &i, argc);
1050               continue;
1051             }
1052
1053           if (strcmp (argv[i], OPTION_CODE_LOC) == 0)
1054             {
1055               options.code_loc = getIntArg(OPTION_CODE_LOC, argv, &i, argc);
1056               continue;
1057             }
1058
1059           if (strcmp (argv[i], OPTION_NO_GCSE) == 0)
1060             {
1061               optimize.global_cse = 0;
1062               continue;
1063             }
1064
1065           if (strcmp (argv[i], OPTION_NO_LOOP_INV) == 0)
1066             {
1067               optimize.loopInvariant = 0;
1068               continue;
1069             }
1070
1071           if (strcmp (argv[i], OPTION_NO_LABEL_OPT) == 0)
1072             {
1073               optimize.label4 = 0;
1074               continue;
1075             }
1076
1077           if (strcmp (argv[i], OPTION_NO_LOOP_IND) == 0)
1078             {
1079               optimize.loopInduction = 0;
1080               continue;
1081             }
1082
1083           if (strcmp (argv[i], OPTION_OPT_CODE_SPEED) == 0)
1084             {
1085               optimize.codeSpeed = 1;
1086               optimize.codeSize = 0;
1087               continue;
1088             }
1089
1090           if (strcmp (argv[i], OPTION_OPT_CODE_SIZE) == 0)
1091             {
1092               optimize.codeSpeed = 0;
1093               optimize.codeSize = 1;
1094               continue;
1095             }
1096
1097           if (strcmp (argv[i], OPTION_LESS_PEDANTIC) == 0)
1098             {
1099               options.lessPedantic = 1;
1100               setErrorLogLevel(ERROR_LEVEL_WARNING);
1101               continue;
1102             }
1103
1104           if (strcmp (argv[i], OPTION_DISABLE_WARNING) == 0)
1105             {
1106               int w = getIntArg(OPTION_DISABLE_WARNING, argv, &i, argc);
1107               if (w < MAX_ERROR_WARNING)
1108                 {
1109                   setWarningDisabled(w);
1110                 }
1111               continue;
1112             }
1113
1114           if (strcmp (&argv[i][1], OPTION_SHORT_IS_8BITS) == 0)
1115             {
1116               options.shortis8bits=1;
1117               continue;
1118             }
1119
1120           if (strcmp (argv[i], OPTION_TINI_LIBID) == 0)
1121             {
1122               options.tini_libid = getIntArg(OPTION_TINI_LIBID, argv, &i, argc);
1123               continue;
1124             }
1125
1126           if (strcmp (argv[i], OPTION_STD_C89) == 0)
1127             {
1128               options.std_c99 = 0;
1129               options.std_sdcc = 0;
1130               continue;
1131             }
1132
1133           if (strcmp (argv[i], OPTION_STD_C99) == 0)
1134             {
1135               options.std_c99 = 1;
1136               options.std_sdcc = 0;
1137               continue;
1138             }
1139
1140           if (strcmp (argv[i], OPTION_STD_SDCC89) == 0)
1141             {
1142               options.std_c99 = 0;
1143               options.std_sdcc = 1;
1144               continue;
1145             }
1146
1147           if (strcmp (argv[i], OPTION_STD_SDCC99) == 0)
1148             {
1149               options.std_c99 = 1;
1150               options.std_sdcc = 1;
1151               continue;
1152             }
1153
1154           if (strcmp (argv[i], OPTION_CODE_SEG) == 0)
1155             {
1156               struct dbuf_s segname;
1157
1158               dbuf_init (&segname, 16);
1159               dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CODE_SEG, argv, &i, argc));
1160               options.code_seg = dbuf_detach (&segname);
1161               continue;
1162             }
1163
1164           if (strcmp (argv[i], OPTION_CONST_SEG) == 0)
1165             {
1166               struct dbuf_s segname;
1167
1168               dbuf_init (&segname, 16);
1169               dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CONST_SEG, argv, &i, argc));
1170               options.const_seg = dbuf_detach (&segname);
1171               continue;
1172             }
1173
1174           if (!port->parseOption (&argc, argv, &i))
1175             {
1176               werror (W_UNKNOWN_OPTION, argv[i]);
1177               continue;
1178             }
1179           else
1180             {
1181               continue;
1182             }
1183         }
1184
1185       /* if preceded by  '-' then option */
1186       if (*argv[i] == '-')
1187         {
1188           switch (argv[i][1])
1189             {
1190             case 'h':
1191               verifyShortOption(argv[i]);
1192
1193               printUsage ();
1194               exit (EXIT_SUCCESS);
1195               break;
1196
1197             case 'm':
1198               /* Used to select the port. But this has already been done. */
1199               break;
1200
1201             case 'p':
1202               /* Used to select the processor in port. But this has
1203                * already been done. */
1204               break;
1205
1206             case 'c':
1207               verifyShortOption(argv[i]);
1208
1209               options.cc_only = 1;
1210               break;
1211
1212             case 'L':
1213                 addSet(&libPathsSet, Safe_strdup(getStringArg("-L", argv, &i, argc)));
1214                 break;
1215
1216             case 'l':
1217                 addSet(&libFilesSet, Safe_strdup(getStringArg("-l", argv, &i, argc)));
1218                 break;
1219
1220             case 'o':
1221               {
1222                 char *outName = getStringArg("-o", argv, &i, argc);
1223                 size_t len = strlen(outName);
1224
1225                 /* point to last character */
1226                 if (IS_DIR_SEPARATOR(outName[len - 1]))
1227                   {
1228                     /* only output path specified */
1229                     dstPath = Safe_malloc(len);
1230                     memcpy(dstPath, outName, len - 1);
1231                     dstPath[len - 1] = '\0';
1232                     fullDstFileName = NULL;
1233                   }
1234                 else
1235                   {
1236                     struct dbuf_s path;
1237
1238                     dbuf_init (&path, 128);
1239                     fullDstFileName = Safe_strdup (outName);
1240
1241                     /* get rid of the "."-extension */
1242                     dbuf_splitFile (outName, &path, NULL);
1243
1244                     dbuf_c_str (&path);
1245                     dstFileName = dbuf_detach (&path);
1246
1247                     dbuf_init (&path, 128);
1248                     /* strip module name to get path */
1249                     if (dbuf_splitPath (dstFileName, &path, NULL))
1250                       {
1251                         dbuf_c_str (&path);
1252                         dstPath = dbuf_detach (&path);
1253                       }
1254                     else
1255                       dbuf_destroy (&path);
1256                   }
1257                 break;
1258               }
1259
1260             case 'W':
1261               /* pre-processer options */
1262               if (argv[i][2] == 'p')
1263                 {
1264                   setParseWithComma(&preArgvSet, getStringArg("-Wp", argv, &i, argc));
1265                 }
1266               /* linker options */
1267               else if (argv[i][2] == 'l')
1268                 {
1269                   setParseWithComma(&linkOptionsSet, getStringArg("-Wl", argv, &i, argc));
1270                 }
1271               /* assembler options */
1272               else if (argv[i][2] == 'a')
1273                 {
1274                   setParseWithComma(&asmOptionsSet, getStringArg("-Wa", argv, &i, argc));
1275                 }
1276               else
1277                 {
1278                   werror (W_UNKNOWN_OPTION, argv[i]);
1279                 }
1280               break;
1281
1282             case 'v':
1283               verifyShortOption(argv[i]);
1284
1285               printVersionInfo (stdout);
1286               exit (0);
1287               break;
1288
1289               /* preprocessor options */
1290             case 'M':
1291               {
1292                 preProcOnly = 1;
1293                 if (argv[i][2] == 'M')
1294                   addSet(&preArgvSet, Safe_strdup("-MM"));
1295                 else
1296                   addSet(&preArgvSet, Safe_strdup("-M"));
1297                 break;
1298               }
1299             case 'C':
1300               {
1301                 addSet(&preArgvSet, Safe_strdup("-C"));
1302                 break;
1303               }
1304
1305             case 'd':
1306             case 'D':
1307             case 'I':
1308             case 'A':
1309             case 'U':
1310               {
1311                 char sOpt = argv[i][1];
1312                 char *rest;
1313
1314                 if (argv[i][2] == ' ' || argv[i][2] == '\0')
1315                   {
1316                     i++;
1317                     if (i >= argc)
1318                       {
1319                         /* No argument. */
1320                         werror(E_ARGUMENT_MISSING, argv[i-1]);
1321                         break;
1322                       }
1323                     else
1324                       {
1325                         rest = argv[i];
1326                       }
1327                   }
1328                 else
1329                   rest = &argv[i][2];
1330
1331                 if (sOpt == 'Y')
1332                   sOpt = 'I';
1333
1334                 SNPRINTF (buffer, sizeof(buffer),
1335                   ((sOpt == 'I') ? "-%c\"%s\"": "-%c%s"), sOpt, rest);
1336                 addSet(&preArgvSet, Safe_strdup(buffer));
1337                 if(sOpt == 'I') {
1338                   addSet(&includeDirsSet, Safe_strdup(rest));
1339                   addSet(&userIncDirsSet, Safe_strdup(rest));
1340                 }
1341               }
1342               break;
1343
1344             default:
1345               if (!port->parseOption (&argc, argv, &i))
1346                 werror (W_UNKNOWN_OPTION, argv[i]);
1347             }
1348           continue;
1349         }
1350
1351       if (!port->parseOption (&argc, argv, &i))
1352         {
1353           /* no option must be a filename */
1354           if (options.c1mode)
1355             {
1356               werror (W_NO_FILE_ARG_IN_C1, argv[i]);
1357             }
1358           else
1359             {
1360               processFile (argv[i]);
1361             }
1362         }
1363     }
1364
1365   /* some sanity checks in c1 mode */
1366   if (options.c1mode)
1367     {
1368       const char *s;
1369
1370       if (fullSrcFileName)
1371         {
1372           fclose (srcFile);
1373           werror (W_NO_FILE_ARG_IN_C1, fullSrcFileName);
1374         }
1375       fullSrcFileName = NULL;
1376       for (s = setFirstItem(relFilesSet); s != NULL; s = setNextItem(relFilesSet))
1377         {
1378           werror (W_NO_FILE_ARG_IN_C1, s);
1379         }
1380       for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1381         {
1382           werror (W_NO_FILE_ARG_IN_C1, s);
1383         }
1384       deleteSet(&relFilesSet);
1385       deleteSet(&libFilesSet);
1386
1387       if (options.cc_only || noAssemble || preProcOnly)
1388         {
1389           werror (W_ILLEGAL_OPT_COMBINATION);
1390         }
1391       options.cc_only = noAssemble = preProcOnly = 0;
1392       if (!dstFileName)
1393         {
1394           werror (E_NEED_OPT_O_IN_C1);
1395           exit (1);
1396         }
1397     }
1398   /* if no dstFileName given with -o, we've to find one: */
1399   if (!dstFileName)
1400     {
1401       const char *s;
1402
1403       /* use the modulename from the C-source */
1404       if (fullSrcFileName)
1405         {
1406           struct dbuf_s path;
1407
1408                   if (*dstPath != '\0')
1409             {
1410               dbuf_init(&path, 128);
1411               dbuf_makePath (&path, dstPath, moduleNameBase);
1412               dbuf_c_str (&path);
1413               dstFileName = dbuf_detach (&path);
1414             }
1415           else
1416             dstFileName = Safe_strdup(moduleNameBase);
1417         }
1418       /* use the modulename from the first object file */
1419       else if ((s = peekSet(relFilesSet)) != NULL)
1420         {
1421           struct dbuf_s file;
1422
1423           dbuf_init(&file, 128);
1424
1425           dbuf_splitPath (s, NULL, &file);
1426
1427           if (*dstPath != '\0')
1428             {
1429               struct dbuf_s path;
1430
1431               dbuf_init(&path, 128);
1432               dbuf_makePath (&path, dstPath, dbuf_c_str (&file));
1433               dbuf_destroy (&file);
1434               dbuf_c_str (&path);
1435               dstFileName = dbuf_detach (&path);
1436             }
1437           else
1438             {
1439               dbuf_c_str (&file);
1440               dstFileName = dbuf_detach (&file);
1441             }
1442         }
1443       /* else no module given: help text is displayed */
1444     }
1445
1446   /* set int, long and float reentrancy based on stack-auto */
1447   if (options.stackAuto)
1448     {
1449       options.intlong_rent++;
1450       options.float_rent++;
1451     }
1452
1453   /* mcs51 has an assembly coded float library that's always reentrant */
1454   if (TARGET_IS_MCS51)
1455     {
1456       options.float_rent++;
1457
1458       /* set up external stack location if not explicitly specified */
1459       if (!options.xstack_loc)
1460         options.xstack_loc = options.xdata_loc;
1461     }
1462
1463   /* if debug option is set then open the cdbFile */
1464   if (options.debug && fullSrcFileName)
1465     {
1466       SNPRINTF (scratchFileName, sizeof(scratchFileName),
1467                 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1468       if(debugFile->openFile(scratchFileName))
1469         debugFile->writeModule(moduleName);
1470       else
1471         werror (E_FILE_OPEN_ERR, scratchFileName);
1472     }
1473   MSVC_style(options.vc_err_style);
1474
1475   return 0;
1476 }
1477
1478 /*-----------------------------------------------------------------*/
1479 /* linkEdit : - calls the linkage editor  with options             */
1480 /*-----------------------------------------------------------------*/
1481 static void
1482 linkEdit (char **envp)
1483 {
1484   FILE *lnkfile;
1485   char *segName, *c;
1486   int system_ret;
1487   const char *s;
1488   char linkerScriptFileName[PATH_MAX];
1489
1490   linkerScriptFileName[0] = 0;
1491   c = NULL;
1492
1493   if(port->linker.needLinkerScript)
1494     {
1495       char out_fmt;
1496
1497       switch (options.out_fmt)
1498         {
1499         case 0:
1500           out_fmt = 'i';        /* Intel hex */
1501           break;
1502         case 1:
1503           out_fmt = 's';        /* Motorola S19 */
1504           break;
1505         case 2:
1506           out_fmt = 't';        /* Elf */
1507           break;
1508         default:
1509           out_fmt = 'i';
1510         }
1511
1512       /* first we need to create the <filename>.lnk file */
1513       SNPRINTF (linkerScriptFileName, sizeof(scratchFileName),
1514         "%s.lnk", dstFileName);
1515       if (!(lnkfile = fopen (linkerScriptFileName, "w")))
1516         {
1517           werror (E_FILE_OPEN_ERR, linkerScriptFileName);
1518           exit (1);
1519         }
1520
1521       if (TARGET_Z80_LIKE)
1522         {
1523           fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1524             out_fmt, dstFileName);
1525         }
1526       else /*For all the other ports.  Including pics???*/
1527         {
1528           fprintf (lnkfile, "-myux%c\n", out_fmt);
1529           if(!options.no_pack_iram)
1530               fprintf (lnkfile, "-Y\n");
1531         }
1532
1533       if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1534         {
1535           /* if iram size specified */
1536           if (options.iram_size)
1537             fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1538
1539           /* if stack size specified*/
1540           if(options.stack_size)
1541               fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1542
1543           /* if xram size specified */
1544           if (options.xram_size_set)
1545             fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1546
1547           /* if code size specified */
1548           if (options.code_size)
1549             fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1550
1551           if (options.debug)
1552             fprintf (lnkfile, "-z\n");
1553         }
1554
1555 #define WRITE_SEG_LOC(N, L) \
1556   if (N) \
1557   { \
1558     segName = Safe_strdup(N); \
1559     c = strtok(segName, " \t"); \
1560     fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1561     if (segName) { Safe_free(segName); } \
1562   }
1563
1564       if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1565         {
1566
1567           /* code segment start */
1568           WRITE_SEG_LOC (HOME_NAME, options.code_loc);
1569
1570           /* data segment start. If zero, the linker chooses
1571              the best place for data */
1572           if (options.data_loc)
1573             {
1574               WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1575             }
1576
1577           /* xdata segment start. If zero, the linker chooses
1578              the best place for xdata */
1579           if (options.xdata_loc)
1580             {
1581               WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1582             }
1583
1584           /* pdata/xstack segment start. If zero, the linker
1585              chooses the best place for them */
1586           if (options.xstack_loc)
1587             {
1588               WRITE_SEG_LOC (PDATA_NAME, options.xstack_loc);
1589             }
1590
1591           /* indirect data */
1592           if (IDATA_NAME)
1593             {
1594               WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1595             }
1596
1597           /* bit segment start */
1598           WRITE_SEG_LOC (BIT_NAME, 0);
1599
1600           /* stack start */
1601           if ( (options.stack_loc) && (options.stack_loc<0x100) &&
1602                !TARGET_IS_HC08)
1603             {
1604               WRITE_SEG_LOC ("SSEG", options.stack_loc);
1605             }
1606         }
1607       else /*For the z80, gbz80*/
1608         {
1609           WRITE_SEG_LOC ("_CODE", options.code_loc);
1610           WRITE_SEG_LOC ("_DATA", options.data_loc);
1611         }
1612
1613       /* If the port has any special linker area declarations, get 'em */
1614       if (port->extraAreas.genExtraAreaLinkOptions)
1615         {
1616           port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1617         }
1618
1619       /* add the extra linker options */
1620       fputStrSet(lnkfile, linkOptionsSet);
1621
1622       /* command line defined library paths if specified */
1623       for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1624         fprintf (lnkfile, "-k %s\n", s);
1625
1626       /* standard library path */
1627       if (!options.nostdlib)
1628         {
1629           if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1630             {
1631               switch (options.model)
1632                 {
1633                 case MODEL_SMALL:
1634                   if (options.stackAuto)
1635                     c = "small-stack-auto";
1636                   else
1637                     c = "small";
1638                   break;
1639                 case MODEL_MEDIUM:
1640                   if (options.stackAuto)
1641                     c = "medium-stack-auto";
1642                   else
1643                     c = "medium";
1644                   break;
1645                 case MODEL_LARGE:
1646                   if (options.stackAuto)
1647                     c = "large-stack-auto";
1648                   else
1649                     c = "large";
1650                   break;
1651                 case MODEL_FLAT24:
1652                   /* c = "flat24"; */
1653                   if (TARGET_IS_DS390)
1654                     {
1655                       c = "ds390";
1656                     }
1657                   else if (TARGET_IS_DS400)
1658                     {
1659                       c = "ds400";
1660                     }
1661                   else
1662                     {
1663                       fprintf(stderr,
1664                         "Add support for your FLAT24 target in %s @ line %d\n",
1665                         __FILE__, __LINE__);
1666                       exit (EXIT_FAILURE);
1667                     }
1668                   break;
1669                 case MODEL_PAGE0:
1670                   c = "xa51";
1671                   break;
1672                 default:
1673                   werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1674                   c = "unknown";
1675                   break;
1676                 }
1677             }
1678           else /*for the z80, gbz80*/
1679             {
1680               if (TARGET_IS_HC08)
1681                 c = "hc08";
1682               else if (TARGET_IS_Z80)
1683                 c = "z80";
1684               else
1685                 c = "gbz80";
1686             }
1687           for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1688             mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1689         }
1690
1691       /* command line defined library files if specified */
1692       for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1693         fprintf (lnkfile, "-l %s\n", s);
1694
1695       /* standard library files */
1696       if (!options.nostdlib)
1697         {
1698 #if !OPT_DISABLE_DS390
1699           if (options.model == MODEL_FLAT24)
1700             {
1701               if (TARGET_IS_DS390)
1702                 {
1703                   fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1704                 }
1705               else if (TARGET_IS_DS400)
1706                 {
1707                   fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1708                 }
1709               else
1710                 {
1711                   fprintf(stderr,
1712                     "Add support for your FLAT24 target in %s @ line %d\n",
1713                     __FILE__, __LINE__);
1714                   exit (EXIT_FAILURE);
1715                 }
1716               }
1717 #endif
1718
1719 #if !OPT_DISABLE_XA51
1720 #ifdef STD_XA51_LIB
1721           if (options.model == MODEL_PAGE0)
1722             {
1723               fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1724             }
1725 #endif
1726 #endif
1727           if (TARGET_IS_MCS51)
1728             {
1729               fprintf (lnkfile, "-l mcs51\n");
1730             }
1731           if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1732             { /*Why the z80 port is not using the standard libraries?*/
1733               fprintf (lnkfile, "-l %s\n", STD_LIB);
1734               fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1735               fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1736               fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1737             }
1738           else if (TARGET_IS_HC08)
1739             {
1740               fprintf (lnkfile, "-l hc08\n");
1741             }
1742           else if (TARGET_IS_Z80)
1743             {
1744               fprintf (lnkfile, "-l z80\n");
1745             }
1746           else if (TARGET_IS_GBZ80)
1747             {
1748               fprintf (lnkfile, "-l gbz80\n");
1749             }
1750         }
1751
1752       /*For the z80 and gbz80 ports, try to find where crt0.o is...
1753       It is very important for this file to be first on the linking proccess
1754       so the areas are set in the correct order, expecially _GSINIT*/
1755       if ((TARGET_Z80_LIKE) && !options.no_std_crt0) /*For the z80, gbz80*/
1756         {
1757           char crt0path[PATH_MAX];
1758           FILE * crt0fp;
1759           set *tempSet=NULL;
1760
1761           tempSet = appendStrSet(libDirsSet, NULL, DIR_SEPARATOR_STRING);
1762           tempSet = appendStrSet(tempSet, NULL, c);
1763           mergeSets(&tempSet, libPathsSet);
1764
1765           for (s = setFirstItem(tempSet); s != NULL; s = setNextItem(tempSet))
1766             {
1767               sprintf (crt0path, "%s%scrt0.o",
1768                 s, DIR_SEPARATOR_STRING);
1769
1770               crt0fp=fopen(crt0path, "r");
1771               if(crt0fp!=NULL)/*Found it!*/
1772                 {
1773                   fclose(crt0fp);
1774                   #ifdef __CYGWIN__
1775                   {
1776                     /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1777                     windows paths, so convert them to the CYGWIN format*/
1778                     char posix_path[PATH_MAX];
1779                     void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1780                     cygwin_conv_to_full_posix_path(crt0path, posix_path);
1781                     strcpy(crt0path, posix_path);
1782                   }
1783                   #endif
1784                   fprintf (lnkfile, "%s\n", crt0path);
1785                   break;
1786                 }
1787             }
1788           if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1789         }
1790
1791       /* put in the object files */
1792       if (fullSrcFileName)
1793         fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1794
1795       fputStrSet(lnkfile, relFilesSet);
1796
1797       fprintf (lnkfile, "\n-e\n");
1798       fclose (lnkfile);
1799     } /* if(port->linker.needLinkerScript) */
1800
1801   if (options.verbose)
1802     printf ("sdcc: Calling linker...\n");
1803
1804   /* build linker output filename */
1805
1806   /* -o option overrides default name? */
1807   if (fullDstFileName)
1808     {
1809       strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1810     }
1811   else
1812     {
1813       /* the linked file gets the name of the first modul */
1814       if (fullSrcFileName)
1815         {
1816           strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1817         }
1818       else
1819         {
1820           s = peekSet(relFilesSet);
1821
1822           assert(s);
1823
1824           strncpyz (scratchFileName, s, sizeof(scratchFileName));
1825           /* strip ".rel" extension */
1826           *strrchr (scratchFileName, '.') = '\0';
1827         }
1828       strncatz (scratchFileName,
1829         options.out_fmt ? ".S19" : ".ihx",
1830         sizeof(scratchFileName));
1831     }
1832
1833   if (port->linker.cmd)
1834     {
1835       char buffer2[PATH_MAX];
1836       char buffer3[PATH_MAX];
1837       set *tempSet=NULL, *libSet=NULL;
1838
1839       strcpy(buffer3, linkerScriptFileName);
1840       if(/*TARGET_IS_PIC16 ||*/ TARGET_IS_PIC) {
1841
1842          /* use $l to set the linker include directories */
1843          tempSet = appendStrSet(libDirsSet, "-I\"", "\"");
1844          mergeSets(&linkOptionsSet, tempSet);
1845
1846          tempSet = appendStrSet(libPathsSet, "-I\"", "\"");
1847          mergeSets(&linkOptionsSet, tempSet);
1848
1849          /* use $3 for libraries from command line --> libSet */
1850          mergeSets(&libSet, libFilesSet);
1851
1852          tempSet = appendStrSet(relFilesSet, "", "");
1853          mergeSets(&libSet, tempSet);
1854 //         libSet = reverseSet(libSet);
1855
1856         if(fullSrcFileName) {
1857 //              strcpy(buffer3, strrchr(fullSrcFileName, DIR_SEPARATOR_CHAR)+1);
1858                 /* if it didn't work, revert to old behaviour */
1859                 if(!strlen(buffer3))strcpy(buffer3, dstFileName);
1860                 strcat(buffer3, port->linker.rel_ext);
1861
1862         } else strcpy(buffer3, "");
1863       }
1864
1865       buildCmdLine (buffer2, port->linker.cmd, buffer3, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1866
1867       buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1868     }
1869   else
1870     {
1871       buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1872     }
1873
1874   /*  if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1875
1876   system_ret = my_system (buffer);
1877
1878   /* TODO: most linker don't have a -o parameter */
1879   /* -o option overrides default name? */
1880   if (fullDstFileName)
1881     {
1882       char *p, *q;
1883       /* the linked file gets the name of the first modul */
1884       if (fullSrcFileName)
1885         {
1886           strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1887           p = strlen (scratchFileName) + scratchFileName;
1888         }
1889       else
1890         {
1891           s = peekSet(relFilesSet);
1892
1893           assert(s);
1894
1895           strncpyz (scratchFileName, s, sizeof(scratchFileName));
1896           /* strip ".rel" extension */
1897           p = strrchr (scratchFileName, '.');
1898           if (p)
1899             {
1900               *p = 0;
1901             }
1902         }
1903       strncatz (scratchFileName,
1904         options.out_fmt ? ".S19" : ".ihx",
1905         sizeof(scratchFileName));
1906       if (FILENAME_CMP (fullDstFileName, scratchFileName))
1907         remove (fullDstFileName);
1908       rename (scratchFileName, fullDstFileName);
1909
1910       strncpyz (buffer, fullDstFileName, sizeof(buffer));
1911       q = strrchr (buffer, '.');
1912       if (!q)
1913         {
1914           /* no extension: append new extensions */
1915           q = strlen (buffer) + buffer;
1916         }
1917
1918       *p = 0;
1919       strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1920       *q = 0;
1921       strncatz(buffer, ".map", sizeof(buffer));
1922       if (FILENAME_CMP (scratchFileName, buffer))
1923         remove (buffer);
1924       rename (scratchFileName, buffer);
1925       *p = 0;
1926       strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1927       *q = 0;
1928       strncatz(buffer, ".mem", sizeof(buffer));
1929       if (FILENAME_CMP (scratchFileName, buffer))
1930         remove (buffer);
1931       rename (scratchFileName, buffer);
1932       if (options.debug)
1933         {
1934           *p = 0;
1935           strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1936           *q = 0;
1937           strncatz(buffer, ".cdb", sizeof(buffer));
1938           if (FILENAME_CMP (scratchFileName, buffer))
1939             remove (buffer);
1940           rename (scratchFileName, buffer);
1941           /* and the OMF file without extension: */
1942           *p = 0;
1943           *q = 0;
1944           if (FILENAME_CMP (scratchFileName, buffer))
1945             remove (buffer);
1946           rename (scratchFileName, buffer);
1947         }
1948     }
1949   if (system_ret)
1950     {
1951       exit (1);
1952     }
1953 }
1954
1955 /*-----------------------------------------------------------------*/
1956 /* assemble - spawns the assembler with arguments                  */
1957 /*-----------------------------------------------------------------*/
1958 static void
1959 assemble (char **envp)
1960 {
1961     /* build assembler output filename */
1962
1963     /* -o option overrides default name? */
1964     if (options.cc_only && fullDstFileName) {
1965         strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1966     } else {
1967         /* the assembled file gets the name of the first modul */
1968         strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1969         strncatz (scratchFileName, port->linker.rel_ext,
1970                   sizeof(scratchFileName));
1971     }
1972
1973     if (port->assembler.do_assemble) {
1974         port->assembler.do_assemble(asmOptionsSet);
1975         return ;
1976     } else if (port->assembler.cmd) {
1977         buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1978                       options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1979                       asmOptionsSet);
1980     } else {
1981         buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1982     }
1983
1984     if (my_system (buffer)) {
1985         /* either system() or the assembler itself has reported an error
1986            perror ("Cannot exec assembler");
1987         */
1988         exit (1);
1989     }
1990     /* TODO: most assembler don't have a -o parameter */
1991     /* -o option overrides default name? */
1992     if (options.cc_only && fullDstFileName) {
1993         strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1994         strncatz (scratchFileName,
1995                   port->linker.rel_ext,
1996                   sizeof(scratchFileName));
1997         if (strcmp (scratchFileName, fullDstFileName))
1998           remove (fullDstFileName);
1999         rename (scratchFileName, fullDstFileName);
2000     }
2001 }
2002
2003 /*-----------------------------------------------------------------*/
2004 /* preProcess - spawns the preprocessor with arguments       */
2005 /*-----------------------------------------------------------------*/
2006 static int
2007 preProcess (char **envp)
2008 {
2009   if (options.c1mode)
2010     {
2011       yyin = stdin;
2012     }
2013   else
2014     {
2015       const char *s;
2016       set *inclList = NULL;
2017
2018       if (NULL != port->linker.rel_ext)
2019         {
2020 #define OBJ_EXT_STR     "-obj-ext="
2021 #define OBJ_EXT_LEN     ((sizeof OBJ_EXT_STR) - 1)
2022           char *buf = Safe_alloc(strlen(port->linker.rel_ext) + (OBJ_EXT_LEN + 1));
2023           strcpy(buf, OBJ_EXT_STR);
2024           strcpy(&buf[OBJ_EXT_LEN], port->linker.rel_ext);
2025           addSet(&preArgvSet, buf);
2026         }
2027
2028       /* if using dollar signs in identifiers */
2029       if (options.dollars_in_ident)
2030         addSet(&preArgvSet, Safe_strdup("--fdollars-in-identifiers"));
2031
2032       /* if using external stack define the macro */
2033       if (options.useXstack)
2034         addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
2035
2036       /* set the macro for stack autos  */
2037       if (options.stackAuto)
2038         addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
2039
2040       /* set the macro for stack autos  */
2041       if (options.stack10bit)
2042         addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
2043
2044       /* set the macro for no overlay  */
2045       if (options.noOverlay)
2046         addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
2047
2048       /* set the macro for large model  */
2049       switch (options.model)
2050         {
2051         case MODEL_LARGE:
2052           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
2053           break;
2054         case MODEL_SMALL:
2055           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
2056           break;
2057         case MODEL_COMPACT:
2058           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
2059           break;
2060         case MODEL_MEDIUM:
2061           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
2062           break;
2063         case MODEL_FLAT24:
2064           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
2065           break;
2066         case MODEL_PAGE0:
2067           addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
2068           break;
2069         default:
2070           werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
2071           break;
2072         }
2073
2074       /* add SDCC version number */
2075       {
2076         char buf[20];
2077         SNPRINTF(buf, sizeof(buf), "-DSDCC=%d%d%d",
2078                  SDCC_VERSION_HI, SDCC_VERSION_LO, SDCC_VERSION_P);
2079         addSet(&preArgvSet, Safe_strdup(buf));
2080       }
2081
2082       /* add port (processor information to processor */
2083       addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
2084       addSet(&preArgvSet, Safe_strdup("-D__{port}"));
2085
2086       if (port && port->processor && TARGET_IS_PIC) {
2087         char proc[512];
2088         SNPRINTF(&proc[0], 512, "-DSDCC_PROCESSOR=\"%s\"", port->processor);
2089         addSet(&preArgvSet, Safe_strdup(proc));
2090       }
2091
2092       /* standard include path */
2093       if (!options.nostdinc) {
2094         inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
2095         mergeSets(&preArgvSet, inclList);
2096       }
2097
2098       setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
2099       Safe_free((void *)s);
2100       if (inclList != NULL)
2101         deleteSet(&inclList);
2102
2103       if (preProcOnly && fullDstFileName)
2104         {
2105           /* -E and -o given */
2106           setMainValue ("cppoutfilename", fullDstFileName);
2107         }
2108       else
2109         {
2110           /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
2111           setMainValue ("cppoutfilename", NULL);
2112         }
2113
2114       if (options.verbose)
2115         printf ("sdcc: Calling preprocessor...\n");
2116       buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
2117
2118       if (preProcOnly) {
2119         if (my_system (buffer)) {
2120           exit (1);
2121         }
2122
2123         exit (0);
2124       }
2125
2126       yyin = my_popen (buffer);
2127       if (yyin == NULL) {
2128           perror ("Preproc file not found");
2129           exit (1);
2130       }
2131     }
2132
2133   return 0;
2134 }
2135
2136 /* Set bin paths */
2137 static void
2138 setBinPaths(const char *argv0)
2139 {
2140   const char *p;
2141   char buf[PATH_MAX];
2142
2143   /*
2144    * Search logic:
2145    *
2146    * 1. - $SDCCDIR/PREFIX2BIN_DIR
2147    * 2. - path(argv[0])
2148    * 3. - $PATH
2149    */
2150
2151   /* do it in reverse mode, so that addSetHead() can be used
2152      instead of slower addSet() */
2153
2154   if ((p = getBinPath(argv0)) != NULL)
2155     addSetHead(&binPathSet, (void *)p);
2156
2157   if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2158     SNPRINTF(buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
2159     addSetHead(&binPathSet, Safe_strdup(buf));
2160   }
2161 }
2162
2163 /* Set system include path */
2164 static void
2165 setIncludePath(void)
2166 {
2167   char *p;
2168   char *p2=NULL;
2169   set *tempSet=NULL;
2170
2171   /*
2172    * Search logic:
2173    *
2174    * 1. - $SDCC_INCLUDE/target
2175    * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2176    * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2177    * 4. - DATADIR/INCLUDE_DIR_SUFFIX/target (only on *nix)
2178    * 5. - $SDCC_INCLUDE
2179    * 6. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
2180    * 7. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
2181    * 8. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
2182    */
2183
2184   if (options.nostdinc)
2185       return;
2186
2187   tempSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
2188   includeDirsSet = appendStrSet(tempSet, NULL, DIR_SEPARATOR_STRING);
2189   includeDirsSet = appendStrSet(includeDirsSet, NULL, port->target);
2190   mergeSets(&includeDirsSet, tempSet);
2191
2192   if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
2193   {
2194     addSetHead(&includeDirsSet, p);
2195     p2=Safe_alloc(strlen(p)+strlen(DIR_SEPARATOR_STRING)+strlen(port->target)+1);
2196     if(p2!=NULL)
2197     {
2198         strcpy(p2, p);
2199         strcat(p2, DIR_SEPARATOR_STRING);
2200         strcat(p2, port->target);
2201         addSetHead(&includeDirsSet, p2);
2202     }
2203   }
2204 }
2205
2206 /* Set system lib path */
2207 static void
2208 setLibPath(void)
2209 {
2210   char *p;
2211
2212   /*
2213    * Search logic:
2214    *
2215    * 1. - $SDCC_LIB
2216    * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
2217    * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
2218    * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
2219    */
2220
2221   if (options.nostdlib)
2222       return;
2223
2224   libDirsSet = appendStrSet(dataDirsSet, NULL, LIB_DIR_SUFFIX);
2225
2226   if ((p = getenv(SDCC_LIB_NAME)) != NULL)
2227     addSetHead(&libDirsSet, p);
2228 }
2229
2230 /* Set data path */
2231 static void
2232 setDataPaths(const char *argv0)
2233 {
2234   const char *p;
2235   char buf[PATH_MAX];
2236
2237   /*
2238    * Search logic:
2239    *
2240    * 1. - $SDCC_HOME/PREFIX2DATA_DIR
2241    * 2. - path(argv[0])/BIN2DATA_DIR
2242    * 3. - DATADIR (only on *nix)
2243    */
2244
2245   if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2246     SNPRINTF(buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
2247     addSet(&dataDirsSet, Safe_strdup(buf));
2248   }
2249
2250   if ((p = getBinPath(argv0)) != NULL) {
2251     SNPRINTF(buf, sizeof buf, "%s" BIN2DATA_DIR, p);
2252     free((void *)p);
2253     addSet(&dataDirsSet, Safe_strdup(buf));
2254   }
2255
2256 #ifdef _WIN32
2257   if (peekSet(dataDirsSet) == NULL) {
2258     /* this should never happen... */
2259     wassertl(0, "Can't get binary path");
2260   }
2261 #else
2262   addSet(&dataDirsSet, Safe_strdup(DATADIR));
2263 #endif
2264
2265   setIncludePath();
2266   setLibPath();
2267 }
2268
2269 static void
2270 initValues (void)
2271 {
2272   populateMainValues (_baseValues);
2273   setMainValue ("port", port->target);
2274   setMainValue ("objext", port->linker.rel_ext);
2275   setMainValue ("asmext", port->assembler.file_ext);
2276
2277   setMainValue ("dstfilename", dstFileName);
2278   setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2279
2280   if (options.cc_only && fullDstFileName)
2281     /* compile + assemble and -o given: -o specifies name of object file */
2282     {
2283       setMainValue ("objdstfilename", fullDstFileName);
2284     }
2285   else
2286     {
2287       setMainValue ("objdstfilename", "{stdobjdstfilename}");
2288     }
2289   if (fullDstFileName)
2290     /* if we're linking, -o gives the final file name */
2291     {
2292       setMainValue ("linkdstfilename", fullDstFileName);
2293     }
2294   else
2295     {
2296       setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2297     }
2298
2299 }
2300
2301 static void doPrintSearchDirs(void)
2302 {
2303     printf("programs:\n");
2304     fputStrSet(stdout, binPathSet);
2305
2306     printf("datadir:\n");
2307     fputStrSet(stdout, dataDirsSet);
2308
2309     printf("includedir:\n");
2310     fputStrSet(stdout, includeDirsSet);
2311
2312     printf("libdir:\n");
2313     fputStrSet(stdout, libDirsSet);
2314     fputStrSet(stdout, libPathsSet);
2315 }
2316
2317
2318 static void
2319 sig_handler (int signal)
2320 {
2321   char *sig_string;
2322
2323   switch (signal)
2324     {
2325     case SIGABRT:
2326       sig_string = "SIGABRT";
2327       break;
2328     case SIGTERM:
2329       sig_string = "SIGTERM";
2330       break;
2331     case SIGINT:
2332       sig_string = "SIGINT";
2333       break;
2334     case SIGSEGV:
2335       sig_string = "SIGSEGV";
2336       break;
2337     default:
2338       sig_string = "Unknown?";
2339       break;
2340     }
2341   fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2342   exit (1);
2343 }
2344
2345 /*
2346  * main routine
2347  * initialises and calls the parser
2348  */
2349
2350 int
2351 main (int argc, char **argv, char **envp)
2352 {
2353   /* turn all optimizations off by default */
2354   memset (&optimize, 0, sizeof (struct optimize));
2355
2356   if (NUM_PORTS==0) {
2357     fprintf (stderr, "Build error: no ports are enabled.\n");
2358     exit (1);
2359   }
2360
2361   /* install signal handler;
2362      it's only purpose is to call exit() to remove temp files */
2363   if (!getenv("SDCC_LEAVE_SIGNALS"))
2364     {
2365       signal (SIGABRT, sig_handler);
2366       signal (SIGTERM, sig_handler);
2367       signal (SIGINT , sig_handler);
2368       signal (SIGSEGV, sig_handler);
2369     }
2370
2371   /* Before parsing the command line options, do a
2372    * search for the port and processor and initialize
2373    * them if they're found. (We can't gurantee that these
2374    * will be the first options specified).
2375    */
2376
2377   _findPort (argc, argv);
2378
2379 #ifdef JAMIN_DS390
2380   if (strcmp(port->target, "mcs51") == 0) {
2381     printf("DS390 jammed in A\n");
2382     _setPort ("ds390");
2383     ds390_jammed = 1;
2384   }
2385 #endif
2386
2387   _findProcessor (argc, argv);
2388
2389   /* Initalise the port. */
2390   if (port->init)
2391     port->init ();
2392
2393   setDefaultOptions ();
2394 #ifdef JAMIN_DS390
2395   if (ds390_jammed) {
2396     options.model = MODEL_SMALL;
2397     options.stack10bit=0;
2398   }
2399 #endif
2400
2401   parseCmdLine (argc, argv);
2402
2403   if (options.verbose && NULL != port->processor)
2404     printf("Processor: %s\n", port->processor);
2405
2406   initValues ();
2407
2408   setBinPaths(argv[0]);
2409   setDataPaths(argv[0]);
2410
2411   if(port->initPaths)
2412         port->initPaths();
2413
2414   if(options.printSearchDirs)
2415         doPrintSearchDirs();
2416
2417   /* if no input then printUsage & exit */
2418   if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL)
2419     {
2420       if (options.printSearchDirs)
2421         exit (EXIT_SUCCESS);
2422       printUsage();
2423       exit (EXIT_FAILURE);
2424     }
2425
2426   /* initMem() is expensive, but
2427      initMem() must called before port->finaliseOptions ().
2428      And the z80 port needs port->finaliseOptions(),
2429      even if we're only linking. */
2430   initMem ();
2431   port->finaliseOptions ();
2432
2433   if (fullSrcFileName || options.c1mode)
2434     {
2435       preProcess (envp);
2436
2437       initSymt ();
2438       initiCode ();
2439       initCSupport ();
2440       initBuiltIns();
2441       initPeepHole ();
2442
2443       if (options.verbose)
2444         printf ("sdcc: Generating code...\n");
2445
2446       yyparse ();
2447
2448       if (pclose(yyin))
2449         fatalError = 1;
2450
2451       if (fatalError) {
2452         exit (EXIT_FAILURE);
2453       }
2454
2455       if (port->general.do_glue != NULL)
2456         (*port->general.do_glue)();
2457       else
2458         {
2459           /* this shouldn't happen */
2460           assert(FALSE);
2461           /* in case of NDEBUG */
2462           glue();
2463         }
2464
2465       if (fatalError) {
2466         exit (1);
2467       }
2468
2469       if (!options.c1mode && !noAssemble)
2470         {
2471           if (options.verbose)
2472             printf ("sdcc: Calling assembler...\n");
2473           assemble (envp);
2474         }
2475     }
2476   closeDumpFiles();
2477
2478   if (options.debug && debugFile)
2479     debugFile->closeFile();
2480
2481   if (!options.cc_only &&
2482       !fatalError &&
2483       !noAssemble &&
2484       !options.c1mode &&
2485       (fullSrcFileName || peekSet(relFilesSet) != NULL))
2486     {
2487       if (options.verbose)
2488         printf ("sdcc: Calling linker...\n");
2489
2490       if (port->linker.do_link)
2491         port->linker.do_link ();
2492       else
2493         linkEdit (envp);
2494     }
2495
2496   return 0;
2497 }