1 /*-------------------------------------------------------------------------
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
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
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.
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.
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 -------------------------------------------------------------------------*/
35 #include "dbuf_string.h"
39 #include "SDCCmacro.h"
41 #include "SDCCdebug.h"
52 extern int yyparse (void);
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 */
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;
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 */
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 */
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];
96 #define LENGTH(_a) (sizeof(_a)/sizeof(*(_a)))
98 #define OPTION_HELP "--help"
99 #define OPTION_OUT_FMT_IHX "--out-fmt-ihx"
100 #define OPTION_OUT_FMT_S19 "--out-fmt-s19"
101 #define OPTION_LARGE_MODEL "--model-large"
102 #define OPTION_MEDIUM_MODEL "--model-medium"
103 #define OPTION_SMALL_MODEL "--model-small"
104 #define OPTION_DUMP_ALL "--dumpall"
105 #define OPTION_PEEP_FILE "--peep-file"
106 #define OPTION_LIB_PATH "--lib-path"
107 #define OPTION_CALLEE_SAVES "--callee-saves"
108 #define OPTION_STACK_LOC "--stack-loc"
109 #define OPTION_XSTACK_LOC "--xstack-loc"
110 #define OPTION_DATA_LOC "--data-loc"
111 #define OPTION_IDATA_LOC "--idata-loc"
112 #define OPTION_XRAM_LOC "--xram-loc"
113 #define OPTION_CODE_LOC "--code-loc"
114 #define OPTION_IRAM_SIZE "--iram-size"
115 #define OPTION_XRAM_SIZE "--xram-size"
116 #define OPTION_CODE_SIZE "--code-size"
117 #define OPTION_VERSION "--version"
118 #define OPTION_NO_LABEL_OPT "--nolabelopt"
119 #define OPTION_NO_LOOP_INV "--noinvariant"
120 #define OPTION_NO_LOOP_IND "--noinduction"
121 #define OPTION_LESS_PEDANTIC "--less-pedantic"
122 #define OPTION_DISABLE_WARNING "--disable-warning"
123 #define OPTION_WERROR "--Werror"
124 #define OPTION_NO_GCSE "--nogcse"
125 #define OPTION_SHORT_IS_8BITS "--short-is-8bits"
126 #define OPTION_NO_XINIT_OPT "--no-xinit-opt"
127 #define OPTION_NO_CCODE_IN_ASM "--no-c-code-in-asm"
128 #define OPTION_ICODE_IN_ASM "--i-code-in-asm"
129 #define OPTION_PRINT_SEARCH_DIRS "--print-search-dirs"
130 #define OPTION_MSVC_ERROR_STYLE "--vc"
131 #define OPTION_USE_STDOUT "--use-stdout"
132 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
133 #define OPTION_VERBOSE_ASM "--fverbose-asm"
134 #define OPTION_OPT_CODE_SPEED "--opt-code-speed"
135 #define OPTION_OPT_CODE_SIZE "--opt-code-size"
136 #define OPTION_STD_C89 "--std-c89"
137 #define OPTION_STD_C99 "--std-c99"
138 #define OPTION_STD_SDCC89 "--std-sdcc89"
139 #define OPTION_STD_SDCC99 "--std-sdcc99"
140 #define OPTION_CODE_SEG "--codeseg"
141 #define OPTION_CONST_SEG "--constseg"
142 #define OPTION_DOLLARS_IN_IDENT "--fdollars-in-identifiers"
143 #define OPTION_UNSIGNED_CHAR "--funsigned-char"
147 { 0, NULL, NULL, "General options" },
148 { 0, OPTION_HELP, NULL, "Display this help" },
149 { 'v', OPTION_VERSION, NULL, "Display sdcc's version" },
150 { 0, "--verbose", &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
151 { 'V', NULL, &options.verboseExec, "Execute verbosely. Show sub commands as they are run" },
152 { 'd', NULL, NULL, NULL },
153 { 'D', NULL, NULL, "Define macro as in -Dmacro" },
154 { 'I', NULL, NULL, "Add to the include (*.h) path, as in -Ipath" },
155 { 'A', NULL, NULL, NULL },
156 { 'U', NULL, NULL, NULL },
157 { 'M', NULL, NULL, "Preprocessor option" },
158 { 'W', NULL, NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
159 { 'S', NULL, &noAssemble, "Compile only; do not assemble or link" },
160 { 'c', "--compile-only", &options.cc_only, "Compile and assemble, but do not link" },
161 { 'E', "--preprocessonly", &preProcOnly, "Preprocess only, do not compile" },
162 { 0, "--c1mode", &options.c1mode, "Act in c1 mode. The standard input is preprocessed code, the output is assembly code." },
163 { 'o', NULL, NULL, "Place the output into the given path resp. file" },
164 { 0, OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
165 { 0, OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
166 { 0, OPTION_USE_STDOUT, NULL, "send errors to stdout instead of stderr"},
167 { 0, "--nostdlib", &options.nostdlib, "Do not include the standard library directory in the search path" },
168 { 0, "--nostdinc", &options.nostdinc, "Do not include the standard include directory in the search path" },
169 { 0, OPTION_LESS_PEDANTIC, NULL, "Disable some of the more pedantic warnings" },
170 { 0, OPTION_DISABLE_WARNING, NULL, "<nnnn> Disable specific warning" },
171 { 0, OPTION_WERROR, NULL, "Treat the warnings as errors" },
172 { 0, "--debug", &options.debug, "Enable debugging symbol output" },
173 { 0, "--cyclomatic", &options.cyclomatic, "Display complexity of compiled functions" },
174 { 0, OPTION_STD_C89, NULL, "Use C89 standard only" },
175 { 0, OPTION_STD_SDCC89, NULL, "Use C89 standard with SDCC extensions (default)" },
176 { 0, OPTION_STD_C99, NULL, "Use C99 standard only (incomplete)" },
177 { 0, OPTION_STD_SDCC99, NULL, "Use C99 standard with SDCC extensions (incomplete)" },
178 { 0, OPTION_DOLLARS_IN_IDENT, &options.dollars_in_ident, "Permit '$' as an identifier character" },
179 { 0, OPTION_UNSIGNED_CHAR, &options.unsigned_char, "Make \"char\" unsigned by default" },
181 { 0, NULL, NULL, "Code generation options"},
182 { 'm', NULL, NULL, "Set the port to use e.g. -mz80." },
183 { 'p', NULL, NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
184 { 0, OPTION_LARGE_MODEL, NULL, "external data space is used" },
185 { 0, OPTION_MEDIUM_MODEL, NULL, "external paged data space is used" },
186 { 0, OPTION_SMALL_MODEL, NULL, "internal data space is used (default)" },
187 { 0, "--stack-auto", &options.stackAuto, "Stack automatic variables" },
188 { 0, "--xstack", &options.useXstack, "Use external stack" },
189 { 0, "--int-long-reent", &options.intlong_rent, "Use reentrant calls on the int and long support functions" },
190 { 0, "--float-reent", &options.float_rent, "Use reentrant calls on the float support functions" },
191 { 0, "--main-return", &options.mainreturn, "Issue a return after main()" },
192 { 0, "--xram-movc", &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
193 { 0, OPTION_CALLEE_SAVES, &options.calleeSavesSet, "<func[,func,...]> Cause the called function to save registers insted of the caller", CLAT_SET },
194 { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" },
195 { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
196 { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" },
197 { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
198 { 0, OPTION_NO_XINIT_OPT, &options.noXinitOpt, "don't memcpy initialized xram from code"},
199 { 0, OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
200 { 0, OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
201 { 0, OPTION_VERBOSE_ASM, &options.verboseAsm, "include code generator comments"},
202 { 0, OPTION_SHORT_IS_8BITS, NULL, "Make short 8 bits (for old times sake)" },
203 { 0, OPTION_CODE_SEG, NULL, "<name> use this name for the code segment" },
204 { 0, OPTION_CONST_SEG, NULL, "<name> use this name for the const segment" },
206 { 0, NULL, NULL, "Optimization options"},
207 { 0, "--nooverlay", &options.noOverlay, "Disable overlaying leaf function auto variables" },
208 { 0, OPTION_NO_GCSE, NULL, "Disable the GCSE optimisation" },
209 { 0, OPTION_NO_LABEL_OPT, NULL, "Disable label optimisation" },
210 { 0, OPTION_NO_LOOP_INV, NULL, "Disable optimisation of invariants" },
211 { 0, OPTION_NO_LOOP_IND, NULL, "Disable loop variable induction" },
212 { 0, "--nojtbound", &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
213 { 0, "--noloopreverse", &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
214 { 0, "--no-peep", &options.nopeep, "Disable the peephole assembly file optimisation" },
215 { 0, "--no-reg-params", &options.noRegParams, "On some ports, disable passing some parameters in registers" },
216 { 0, "--peep-asm", &options.asmpeep, "Enable peephole optimization on inline assembly" },
217 { 0, OPTION_PEEP_FILE, &options.peep_file, "<file> use this extra peephole file", CLAT_STRING },
218 { 0, OPTION_OPT_CODE_SPEED, NULL, "Optimize for code speed rather than size" },
219 { 0, OPTION_OPT_CODE_SIZE, NULL, "Optimize for code size rather than speed" },
221 { 0, NULL, NULL, "Internal debugging options"},
222 { 0, "--dumpraw", &options.dump_raw, "Dump the internal structure after the initial parse" },
223 { 0, "--dumpgcse", &options.dump_gcse, NULL },
224 { 0, "--dumploop", &options.dump_loop, NULL },
225 { 0, "--dumpdeadcode", &options.dump_kill, NULL },
226 { 0, "--dumpliverange", &options.dump_range, NULL },
227 { 0, "--dumpregpack", &options.dump_pack, NULL },
228 { 0, "--dumpregassign", &options.dump_rassgn, NULL },
229 { 0, "--dumptree", &options.dump_tree, "dump front-end AST before generating iCode" },
230 { 0, OPTION_DUMP_ALL, NULL, "Dump the internal structure at all stages" },
231 { 0, OPTION_ICODE_IN_ASM, &options.iCodeInAsm, "include i-code as comments in the asm file"},
233 { 0, NULL, NULL, "Linker options" },
234 { 'l', NULL, NULL, "Include the given library in the link" },
235 { 'L', NULL, NULL, "Add the next field to the library search path" },
236 { 0, OPTION_LIB_PATH, &libPathsSet, "<path> use this path to search for libraries", CLAT_ADD_SET },
237 { 0, OPTION_OUT_FMT_IHX, NULL, "Output in Intel hex format" },
238 { 0, OPTION_OUT_FMT_S19, NULL, "Output in S19 hex format" },
239 { 0, OPTION_XRAM_LOC, &options.xdata_loc, "<nnnn> External Ram start location", CLAT_INTEGER },
240 { 0, OPTION_XRAM_SIZE, NULL, "<nnnn> External Ram size" },
241 { 0, OPTION_IRAM_SIZE, &options.iram_size, "<nnnn> Internal Ram size", CLAT_INTEGER },
242 { 0, OPTION_XSTACK_LOC, &options.xstack_loc, "<nnnn> External Stack start location", CLAT_INTEGER },
243 { 0, OPTION_CODE_LOC, &options.code_loc, "<nnnn> Code Segment Location", CLAT_INTEGER },
244 { 0, OPTION_CODE_SIZE, &options.code_size, "<nnnn> Code Segment size", CLAT_INTEGER },
245 { 0, OPTION_STACK_LOC, &options.stack_loc, "<nnnn> Stack pointer initial value", CLAT_INTEGER },
246 { 0, OPTION_DATA_LOC, &options.data_loc, "<nnnn> Direct data start location", CLAT_INTEGER },
247 { 0, OPTION_IDATA_LOC, &options.idata_loc, NULL, CLAT_INTEGER },
253 /** Table of all unsupported options and help text to display when one
257 /** shortOpt as in OPTIONS. */
259 /** longOpt as in OPTIONS. */
261 /** Message to display inside W_UNSUPPORTED_OPT when this option
266 static const UNSUPPORTEDOPT
267 unsupportedOptTable[] = {
268 { 'X', NULL, "use --xstack-loc instead" },
269 { 'x', NULL, "use --xstack instead" },
270 { 'i', NULL, "use --idata-loc instead" },
271 { 'r', NULL, "use --xdata-loc instead" },
272 { 's', NULL, "use --code-loc instead" },
273 { 'Y', NULL, "use -I instead" }
276 /** List of all default constant macros.
278 static const char *_baseValues[] = {
281 /* Path seperator character */
282 "sep", DIR_SEPARATOR_STRING,
286 static const char *_preCmd = "{cpp} -nostdinc -Wall {cppstd}{cppextraopts} \"{fullsrcfilename}\" \"{cppoutfilename}\"";
290 static PORT *_ports[] =
292 #if !OPT_DISABLE_MCS51
295 #if !OPT_DISABLE_GBZ80
304 #if !OPT_DISABLE_DS390
307 #if !OPT_DISABLE_PIC16
313 #if !OPT_DISABLE_TININative
316 #if !OPT_DISABLE_XA51
319 #if !OPT_DISABLE_DS400
322 #if !OPT_DISABLE_HC08
327 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
329 /** Sets the port to the one given by the command line option.
330 @param The name minus the option (eg 'mcs51')
331 @return 0 on success.
334 _setPort (const char *name)
337 for (i = 0; i < NUM_PORTS; i++)
339 if (!strcmp (_ports[i]->target, name))
345 /* Error - didnt find */
346 werror (E_UNKNOWN_TARGET, name);
350 /* Override the default processor with the one specified
351 * on the command line */
353 _setProcessor (char *_processor)
355 port->processor = _processor;
359 _validatePorts (void)
362 for (i = 0; i < NUM_PORTS; i++)
364 if (_ports[i]->magic != PORT_MAGIC)
366 /* Uncomment this line to debug which port is causing the problem
367 * (the target name is close to the beginning of the port struct
368 * and probably can be accessed just fine). */
369 fprintf(stderr,"%s :",_ports[i]->target);
370 wassertl (0, "Port definition structure is incomplete");
375 /* search through the command line options for the port */
377 _findPort (int argc, char **argv)
383 if (!strncmp (*argv, "-m", 2))
385 _setPort (*argv + 2);
391 /* Use the first in the list */
395 /* search through the command line options for the processor */
397 _findProcessor (int argc, char **argv)
401 if (!strncmp (*argv, "-p", 2))
403 _setProcessor (*argv + 2);
409 /* no error if processor was not specified. */
412 /*-----------------------------------------------------------------*/
413 /* printVersionInfo - prints the version info */
414 /*-----------------------------------------------------------------*/
416 printVersionInfo (FILE *stream)
422 for (i = 0; i < NUM_PORTS; i++)
423 fprintf (stream, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
425 fprintf (stream, " " SDCC_VERSION_STR
426 #ifdef SDCC_SUB_VERSION_STR
427 "/" SDCC_SUB_VERSION_STR
430 getBuildNumber(), getBuildDate(), getBuildEnvironment() );
434 printOptions(const OPTION *optionsTable, FILE *stream)
438 optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
439 || optionsTable[i].help != NULL;
442 if (!optionsTable[i].shortOpt && !optionsTable[i].longOpt
443 && optionsTable[i].help)
445 fprintf (stream, "\n%s:\n", optionsTable[i].help);
449 fprintf(stream, " %c%c %-20s %s\n",
450 optionsTable[i].shortOpt !=0 ? '-' : ' ',
451 optionsTable[i].shortOpt !=0 ? optionsTable[i].shortOpt : ' ',
452 optionsTable[i].longOpt != NULL ? optionsTable[i].longOpt : "",
453 optionsTable[i].help != NULL ? optionsTable[i].help : ""
459 /*-----------------------------------------------------------------*/
460 /* printUsage - prints command line syntax */
461 /*-----------------------------------------------------------------*/
466 FILE *stream = stderr;
468 printVersionInfo (stream);
470 "Usage : sdcc [options] filename\n"
474 printOptions (optionsTable, stream);
476 for (i = 0; i < NUM_PORTS; i++)
478 if (_ports[i]->poptions != NULL)
480 fprintf (stream, "\nSpecial options for the %s port:\n", _ports[i]->target);
481 printOptions (_ports[i]->poptions, stream);
486 /*-----------------------------------------------------------------*/
487 /* setParseWithComma - separates string with comma to a set */
488 /*-----------------------------------------------------------------*/
490 setParseWithComma (set **dest, const char *src)
495 /* skip the initial white spaces */
496 while (isspace((unsigned char)*src))
499 /* skip the trailing white spaces */
500 end = &src[strlen(src) - 1];
501 while (end >= src && isspace((unsigned char)*end))
508 dbuf_init (&dbuf, 16);
510 while (p < end && ',' != *p)
512 dbuf_append (&dbuf, src, p - src);
514 /* null terminate the buffer */
516 addSet(dest, dbuf_detach (&dbuf));
522 /*-----------------------------------------------------------------*/
523 /* setDefaultOptions - sets the default options */
524 /*-----------------------------------------------------------------*/
526 setDefaultOptions (void)
528 /* first the options part */
529 options.stack_loc = 0; /* stack pointer initialised to 0 */
530 options.xstack_loc = 0; /* xternal stack starts at 0 */
531 options.code_loc = 0; /* code starts at 0 */
532 options.data_loc = 0; /* JCF: By default let the linker locate data */
533 options.xdata_loc = 0;
534 options.idata_loc = 0; /* MB: No need to limit idata to 0x80-0xFF */
536 options.model = port->general.default_model;
537 options.nostdlib = 0;
538 options.nostdinc = 0;
540 options.shortis8bits = 0;
541 options.std_sdcc = 1; /* enable SDCC language extensions */
542 options.std_c99 = 0; /* default to C89 until more C99 support */
543 options.code_seg = CODE_NAME ? Safe_strdup(CODE_NAME) : NULL; /* default to CSEG for generated code */
544 options.const_seg = CONST_NAME ? Safe_strdup(CONST_NAME) : NULL; /* default to CONST for generated code */
546 options.stack10bit=0;
548 /* now for the optimizations */
549 /* turn on the everything */
550 optimize.global_cse = 1;
555 optimize.loopInvariant = 1;
556 optimize.loopInduction = 1;
558 /* now for the ports */
559 port->setDefaultOptions ();
562 /*-----------------------------------------------------------------*/
563 /* processFile - determines the type of file from the extension */
564 /*-----------------------------------------------------------------*/
566 processFile (char *s)
572 dbuf_init (&ext, 128);
573 dbuf_init (&path, 128);
575 /* get the file extension.
576 If no '.' then we don't know what the file type is
577 so give a warning and return */
578 if (!dbuf_splitFile (s, &path, &ext))
580 werror (W_UNKNOWN_FEXT, s);
583 dbuf_destroy (&path);
588 /* otherwise depending on the file type */
589 extp = dbuf_c_str (&ext);
590 if (STRCASECMP (extp, ".c") == 0)
596 /* source file name : not if we already have a
600 werror (W_TOO_MANY_SRC, s);
602 dbuf_destroy (&path);
607 /* the only source file */
609 if (!(srcFile = fopen (fullSrcFileName, "r")))
611 werror (E_FILE_OPEN_ERR, s);
613 dbuf_destroy (&path);
618 /* get rid of any path information
619 for the module name; */
620 dbuf_init (&ext, 128);
622 dbuf_splitPath (dbuf_c_str (&path), NULL, &ext);
623 dbuf_destroy (&path);
625 moduleNameBase = Safe_strdup (dbuf_c_str (&ext));
626 moduleName = dbuf_detach (&ext);
628 for (p = moduleName; *p; ++p)
629 if (!isalnum ((unsigned char)*p))
634 /* if the extention is type .rel or .r or .REL or .R
635 additional object file will be passed to the linker */
636 if (STRCASECMP (extp, ".r") == 0 || STRCASECMP (extp, ".rel") == 0 ||
637 strcmp (extp, port->linker.rel_ext) == 0)
640 dbuf_destroy (&path);
642 addSet (&relFilesSet, Safe_strdup (s));
646 /* if .lib or .LIB */
647 if (STRCASECMP (extp, ".lib") == 0)
650 dbuf_destroy (&path);
652 addSet (&libFilesSet, Safe_strdup (s));
657 dbuf_destroy (&path);
659 werror (W_UNKNOWN_FEXT, s);
663 _setModel (int model, const char *sz)
665 if (port->general.supported_models & model)
666 options.model = model;
668 werror (W_UNSUPPORTED_MODEL, sz, port->target);
671 /** Gets the string argument to this option. If the option is '--opt'
672 then for input of '--optxyz' or '--opt xyz' returns xyz.
675 getStringArg(const char *szStart, char **argv, int *pi, int argc)
677 if (argv[*pi][strlen(szStart)])
679 return &argv[*pi][strlen(szStart)];
686 werror (E_ARGUMENT_MISSING, szStart);
687 /* Die here rather than checking for errors later. */
697 /** Gets the integer argument to this option using the same rules as
701 getIntArg(const char *szStart, char **argv, int *pi, int argc)
705 char *str = getStringArg(szStart, argv, pi, argc);
707 val = strtol(str, &p, 0);
708 if (p == str || *p != '\0')
710 werror (E_BAD_INT_ARGUMENT, szStart);
711 /* Die here rather than checking for errors later. */
718 verifyShortOption(const char *opt)
720 if (strlen(opt) != 2)
722 werror (W_EXCESS_SHORT_OPTIONS, opt);
727 tryHandleUnsupportedOpt(char **argv, int *pi)
729 if (argv[*pi][0] == '-')
731 const char *longOpt = "";
735 if (argv[*pi][1] == '-')
742 shortOpt = argv[*pi][1];
744 for (i = 0; i < LENGTH(unsupportedOptTable); i++)
746 if (unsupportedOptTable[i].shortOpt == shortOpt ||
747 (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt)))
749 /* Found an unsupported opt. */
751 SNPRINTF(buffer, sizeof(buffer),
753 longOpt ? longOpt : "",
754 shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
755 werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
759 /* Didn't find in the table */
764 /* Not an option, so can't be unsupported :) */
770 scanOptionsTable(const OPTION *optionsTable, char shortOpt, const char *longOpt, char **argv, int *pi, int argc)
775 optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
776 || optionsTable[i].help != NULL;
779 if (optionsTable[i].shortOpt == shortOpt)
781 if (optionsTable[i].pparameter != NULL)
783 verifyShortOption(argv[*pi]);
785 (*(int *)optionsTable[i].pparameter)++;
792 size_t len = optionsTable[i].longOpt ? strlen (optionsTable[i].longOpt) : 0;
795 (optionsTable[i].arg_type != CLAT_BOOLEAN ||
796 (optionsTable[i].arg_type == CLAT_BOOLEAN && len == strlen (longOpt) && optionsTable[i].longOpt)) &&
797 strncmp (optionsTable[i].longOpt, longOpt, len) == 0)
799 /* If it is a flag then we can handle it here */
800 if (optionsTable[i].pparameter != NULL)
802 switch (optionsTable[i].arg_type)
805 (*(int *)optionsTable[i].pparameter)++;
809 *(int *)optionsTable[i].pparameter = getIntArg (optionsTable[i].longOpt, argv, pi, argc);
813 if (*(char **)optionsTable[i].pparameter)
814 Safe_free(*(char **)optionsTable[i].pparameter);
815 *(char **)optionsTable[i].pparameter = Safe_strdup (getStringArg (optionsTable[i].longOpt, argv, pi, argc));
819 if (*(set **)optionsTable[i].pparameter)
821 deleteSet ((set **)optionsTable[i].pparameter);
823 setParseWithComma ((set **)optionsTable[i].pparameter, getStringArg (optionsTable[i].longOpt, argv, pi, argc));
827 addSet((set **)optionsTable[i].pparameter, Safe_strdup (getStringArg (optionsTable[i].longOpt, argv, pi, argc)));
834 /* Not a flag. Handled manually later. */
840 /* Didn't find in the table */
845 tryHandleSimpleOpt(char **argv, int *pi, int argc)
847 if (argv[*pi][0] == '-')
849 const char *longOpt = "";
852 if (argv[*pi][1] == '-')
859 shortOpt = argv[*pi][1];
862 if (scanOptionsTable(optionsTable, shortOpt, longOpt, argv, pi, argc))
866 else if (port && port->poptions &&
867 scanOptionsTable(port->poptions, shortOpt, longOpt, argv, pi, argc))
878 /* Not an option, so can't be handled. */
883 /*-----------------------------------------------------------------*/
884 /* parseCmdLine - parses the command line and sets the options */
885 /*-----------------------------------------------------------------*/
887 parseCmdLine (int argc, char **argv)
891 /* go thru all whole command line */
892 for (i = 1; i < argc; i++)
897 /* check port specific options before general ones */
898 if (port->parseOption (&argc, argv, &i) == TRUE)
903 if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
908 if (tryHandleSimpleOpt(argv, &i, argc) == TRUE)
914 if (argv[i][0] == '-' && argv[i][1] == '-')
916 if (strcmp (argv[i], OPTION_USE_STDOUT) == 0)
918 if (options.use_stdout == 0)
920 options.use_stdout = 1;
921 dup2(STDOUT_FILENO, STDERR_FILENO);
925 if (strcmp (argv[i], OPTION_HELP) == 0)
931 if (strcmp (argv[i], OPTION_OUT_FMT_IHX) == 0)
937 if (strcmp (argv[i], OPTION_OUT_FMT_S19) == 0)
943 if (strcmp (argv[i], OPTION_LARGE_MODEL) == 0)
945 _setModel (MODEL_LARGE, argv[i]);
949 if (strcmp (argv[i], OPTION_MEDIUM_MODEL) == 0)
951 _setModel (MODEL_MEDIUM, argv[i]);
955 if (strcmp (argv[i], OPTION_SMALL_MODEL) == 0)
957 _setModel (MODEL_SMALL, argv[i]);
961 if (strcmp (argv[i], OPTION_DUMP_ALL) == 0)
963 options.dump_rassgn =
969 options.dump_raw = 1;
973 if (strcmp (argv[i], OPTION_VERSION) == 0)
975 printVersionInfo (stdout);
980 if (strcmp (argv[i], OPTION_XRAM_SIZE) == 0)
982 options.xram_size = getIntArg(OPTION_XRAM_SIZE, argv, &i, argc);
983 options.xram_size_set = TRUE;
987 if (strcmp (argv[i], OPTION_NO_GCSE) == 0)
989 optimize.global_cse = 0;
993 if (strcmp (argv[i], OPTION_NO_LOOP_INV) == 0)
995 optimize.loopInvariant = 0;
999 if (strcmp (argv[i], OPTION_NO_LABEL_OPT) == 0)
1001 optimize.label4 = 0;
1005 if (strcmp (argv[i], OPTION_NO_LOOP_IND) == 0)
1007 optimize.loopInduction = 0;
1011 if (strcmp (argv[i], OPTION_OPT_CODE_SPEED) == 0)
1013 optimize.codeSpeed = 1;
1014 optimize.codeSize = 0;
1018 if (strcmp (argv[i], OPTION_OPT_CODE_SIZE) == 0)
1020 optimize.codeSpeed = 0;
1021 optimize.codeSize = 1;
1025 if (strcmp (argv[i], OPTION_LESS_PEDANTIC) == 0)
1027 options.lessPedantic = 1;
1028 setErrorLogLevel(ERROR_LEVEL_WARNING);
1032 if (strcmp (argv[i], OPTION_DISABLE_WARNING) == 0)
1034 int w = getIntArg(OPTION_DISABLE_WARNING, argv, &i, argc);
1035 if (w < MAX_ERROR_WARNING)
1037 setWarningDisabled(w);
1042 if (strcmp (argv[i], OPTION_WERROR) == 0)
1045 addSet(&preArgvSet, Safe_strdup("-Werror"));
1049 if (strcmp (&argv[i][1], OPTION_SHORT_IS_8BITS) == 0)
1051 options.shortis8bits=1;
1055 if (strcmp (argv[i], OPTION_STD_C89) == 0)
1057 options.std_c99 = 0;
1058 options.std_sdcc = 0;
1062 if (strcmp (argv[i], OPTION_STD_C99) == 0)
1064 options.std_c99 = 1;
1065 options.std_sdcc = 0;
1069 if (strcmp (argv[i], OPTION_STD_SDCC89) == 0)
1071 options.std_c99 = 0;
1072 options.std_sdcc = 1;
1076 if (strcmp (argv[i], OPTION_STD_SDCC99) == 0)
1078 options.std_c99 = 1;
1079 options.std_sdcc = 1;
1083 if (strcmp (argv[i], OPTION_CODE_SEG) == 0)
1085 struct dbuf_s segname;
1087 dbuf_init (&segname, 16);
1088 dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CODE_SEG, argv, &i, argc));
1089 if (options.code_seg) Safe_free(options.code_seg);
1090 options.code_seg = dbuf_detach (&segname);
1094 if (strcmp (argv[i], OPTION_CONST_SEG) == 0)
1096 struct dbuf_s segname;
1098 dbuf_init (&segname, 16);
1099 dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CONST_SEG, argv, &i, argc));
1100 if (options.const_seg) Safe_free(options.const_seg);
1101 options.const_seg = dbuf_detach (&segname);
1105 werror (W_UNKNOWN_OPTION, argv[i]);
1109 /* if preceded by '-' then option */
1110 if (*argv[i] == '-')
1115 verifyShortOption(argv[i]);
1118 exit (EXIT_SUCCESS);
1122 /* Used to select the port. But this has already been done. */
1126 /* Used to select the processor in port. But this has
1127 * already been done. */
1131 verifyShortOption(argv[i]);
1133 options.cc_only = 1;
1137 addSet(&libPathsSet, Safe_strdup(getStringArg("-L", argv, &i, argc)));
1141 addSet(&libFilesSet, Safe_strdup(getStringArg("-l", argv, &i, argc)));
1146 char *outName = getStringArg("-o", argv, &i, argc);
1147 size_t len = strlen(outName);
1149 /* point to last character */
1150 if (IS_DIR_SEPARATOR(outName[len - 1]))
1152 /* only output path specified */
1153 dstPath = Safe_malloc(len);
1154 memcpy(dstPath, outName, len - 1);
1155 dstPath[len - 1] = '\0';
1156 fullDstFileName = NULL;
1162 dbuf_init (&path, 128);
1163 fullDstFileName = Safe_strdup (outName);
1165 /* get rid of the "."-extension */
1166 dbuf_splitFile (outName, &path, NULL);
1169 dstFileName = dbuf_detach (&path);
1171 dbuf_init (&path, 128);
1172 /* strip module name to get path */
1173 if (dbuf_splitPath (dstFileName, &path, NULL))
1176 dstPath = dbuf_detach (&path);
1179 dbuf_destroy (&path);
1185 /* pre-processer options */
1186 if (argv[i][2] == 'p')
1188 setParseWithComma(&preArgvSet, getStringArg("-Wp", argv, &i, argc));
1190 /* linker options */
1191 else if (argv[i][2] == 'l')
1193 setParseWithComma(&linkOptionsSet, getStringArg("-Wl", argv, &i, argc));
1195 /* assembler options */
1196 else if (argv[i][2] == 'a')
1198 setParseWithComma(&asmOptionsSet, getStringArg("-Wa", argv, &i, argc));
1202 werror (W_UNKNOWN_OPTION, argv[i]);
1207 verifyShortOption(argv[i]);
1209 printVersionInfo (stdout);
1210 exit (EXIT_SUCCESS);
1213 /* preprocessor options */
1217 if (argv[i][2] == 'M')
1218 addSet(&preArgvSet, Safe_strdup("-MM"));
1220 addSet(&preArgvSet, Safe_strdup("-M"));
1230 char sOpt = argv[i][1];
1233 if (argv[i][2] == ' ' || argv[i][2] == '\0')
1239 werror(E_ARGUMENT_MISSING, argv[i-1]);
1253 SNPRINTF (buffer, sizeof(buffer),
1254 ((sOpt == 'I') ? "-%c\"%s\"": "-%c%s"), sOpt, rest);
1255 addSet(&preArgvSet, Safe_strdup(buffer));
1257 addSet(&includeDirsSet, Safe_strdup(rest));
1258 addSet(&userIncDirsSet, Safe_strdup(rest));
1264 werror (W_UNKNOWN_OPTION, argv[i]);
1269 /* no option must be a filename */
1272 werror (W_NO_FILE_ARG_IN_C1, argv[i]);
1276 processFile (argv[i]);
1280 /* some sanity checks in c1 mode */
1285 if (fullSrcFileName)
1288 werror (W_NO_FILE_ARG_IN_C1, fullSrcFileName);
1290 fullSrcFileName = NULL;
1291 for (s = setFirstItem(relFilesSet); s != NULL; s = setNextItem(relFilesSet))
1293 werror (W_NO_FILE_ARG_IN_C1, s);
1295 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1297 werror (W_NO_FILE_ARG_IN_C1, s);
1299 deleteSet(&relFilesSet);
1300 deleteSet(&libFilesSet);
1302 if (options.cc_only || noAssemble || preProcOnly)
1304 werror (W_ILLEGAL_OPT_COMBINATION);
1306 options.cc_only = noAssemble = preProcOnly = 0;
1309 werror (E_NEED_OPT_O_IN_C1);
1310 exit (EXIT_FAILURE);
1316 moduleName = Safe_strdup(dstFileName);
1317 for (p = moduleName; *p; ++p)
1318 if (!isalnum ((unsigned char)*p))
1322 /* if no dstFileName given with -o, we've to find one: */
1327 /* use the modulename from the C-source */
1328 if (fullSrcFileName)
1332 if (*dstPath != '\0')
1334 dbuf_init(&path, 128);
1335 dbuf_makePath (&path, dstPath, moduleNameBase);
1337 dstFileName = dbuf_detach (&path);
1340 dstFileName = Safe_strdup(moduleNameBase);
1342 /* use the modulename from the first object file */
1343 else if ((s = peekSet(relFilesSet)) != NULL)
1347 dbuf_init(&file, 128);
1349 /* get rid of the "."-extension */
1350 dbuf_splitFile (s, &file, NULL);
1353 s = dbuf_detach (&file);
1355 dbuf_init (&file, 128);
1357 dbuf_splitPath (s, NULL, &file);
1359 if (*dstPath != '\0')
1363 dbuf_init(&path, 128);
1364 dbuf_makePath (&path, dstPath, dbuf_c_str (&file));
1365 dbuf_destroy (&file);
1367 dstFileName = dbuf_detach (&path);
1372 dstFileName = dbuf_detach (&file);
1375 /* else no module given: help text is displayed */
1378 /* set int, long and float reentrancy based on stack-auto */
1379 if (options.stackAuto)
1381 options.intlong_rent++;
1382 options.float_rent++;
1385 /* mcs51 has an assembly coded float library that's always reentrant */
1386 if (TARGET_IS_MCS51)
1388 options.float_rent++;
1390 /* set up external stack location if not explicitly specified */
1391 if (!options.xstack_loc)
1392 options.xstack_loc = options.xdata_loc;
1395 /* if debug option is set then open the cdbFile */
1396 if (options.debug && fullSrcFileName)
1398 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1399 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1400 if(debugFile->openFile(scratchFileName))
1401 debugFile->writeModule(moduleName);
1403 werror (E_FILE_OPEN_ERR, scratchFileName);
1405 MSVC_style(options.vc_err_style);
1410 /*-----------------------------------------------------------------*/
1411 /* finalizeOptions - finalize (post-process( options */
1412 /*-----------------------------------------------------------------*/
1414 finalizeOptions (void)
1416 /* no peephole comments if not verbose asm */
1417 if (!options.verboseAsm)
1418 options.noPeepComments = 1;
1421 /*-----------------------------------------------------------------*/
1422 /* linkEdit : - calls the linkage editor with options */
1423 /*-----------------------------------------------------------------*/
1425 linkEdit (char **envp)
1431 char linkerScriptFileName[PATH_MAX];
1433 linkerScriptFileName[0] = 0;
1436 if (port->linker.needLinkerScript)
1440 switch (options.out_fmt)
1443 out_fmt = 'i'; /* Intel hex */
1446 out_fmt = 's'; /* Motorola S19 */
1449 out_fmt = 't'; /* Elf */
1455 /* first we need to create the <filename>.lnk file */
1456 SNPRINTF (linkerScriptFileName, sizeof(linkerScriptFileName),
1457 "%s.lnk", dstFileName);
1458 if (!(lnkfile = fopen (linkerScriptFileName, "w")))
1460 werror (E_FILE_OPEN_ERR, linkerScriptFileName);
1461 exit (EXIT_FAILURE);
1464 if (TARGET_Z80_LIKE)
1466 fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1467 out_fmt, dstFileName);
1469 else /*For all the other ports. Including pics???*/
1471 fprintf (lnkfile, "-myux%c\n", out_fmt);
1472 if(!options.no_pack_iram)
1473 fprintf (lnkfile, "-Y\n");
1476 if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1478 /* if iram size specified */
1479 if (options.iram_size)
1480 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1482 /* if stack size specified*/
1483 if(options.stack_size)
1484 fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1486 /* if xram size specified */
1487 if (options.xram_size_set)
1488 fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1490 /* if code size specified */
1491 if (options.code_size)
1492 fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1496 fprintf (lnkfile, "-z\n");
1498 #define WRITE_SEG_LOC(N, L) \
1501 segName = Safe_strdup(N); \
1502 c = strtok(segName, " \t"); \
1503 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1504 if (segName) { Safe_free(segName); } \
1507 if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1510 /* code segment start */
1511 WRITE_SEG_LOC (HOME_NAME, options.code_loc);
1513 /* data segment start. If zero, the linker chooses
1514 the best place for data */
1515 if (options.data_loc)
1517 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1520 /* xdata segment start. If zero, the linker chooses
1521 the best place for xdata */
1522 if (options.xdata_loc)
1524 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1527 /* pdata/xstack segment start. If zero, the linker
1528 chooses the best place for them */
1529 if (options.xstack_loc)
1531 WRITE_SEG_LOC (PDATA_NAME, options.xstack_loc);
1537 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1540 /* bit segment start */
1541 WRITE_SEG_LOC (BIT_NAME, 0);
1544 if ( (options.stack_loc) && (options.stack_loc<0x100) &&
1547 WRITE_SEG_LOC ("SSEG", options.stack_loc);
1550 else /*For the z80, gbz80*/
1552 WRITE_SEG_LOC ("_CODE", options.code_loc);
1553 WRITE_SEG_LOC ("_DATA", options.data_loc);
1556 /* If the port has any special linker area declarations, get 'em */
1557 if (port->extraAreas.genExtraAreaLinkOptions)
1559 port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1562 /* add the extra linker options */
1563 fputStrSet(lnkfile, linkOptionsSet);
1565 /* command line defined library paths if specified */
1566 for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1567 fprintf (lnkfile, "-k %s\n", s);
1569 /* standard library path */
1570 if (!options.nostdlib)
1572 if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1574 switch (options.model)
1577 if (options.stackAuto)
1578 c = "small-stack-auto";
1583 if (options.stackAuto)
1584 c = "medium-stack-auto";
1589 if (options.stackAuto)
1590 c = "large-stack-auto";
1596 if (TARGET_IS_DS390)
1600 else if (TARGET_IS_DS400)
1607 "Add support for your FLAT24 target in %s @ line %d\n",
1608 __FILE__, __LINE__);
1609 exit (EXIT_FAILURE);
1616 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1621 else /*for the z80, gbz80*/
1625 else if (TARGET_IS_Z80)
1630 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1631 mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1634 /* command line defined library files if specified */
1635 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1636 fprintf (lnkfile, "-l %s\n", s);
1638 /* standard library files */
1639 if (!options.nostdlib)
1641 #if !OPT_DISABLE_DS390
1642 if (options.model == MODEL_FLAT24)
1644 if (TARGET_IS_DS390)
1646 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1648 else if (TARGET_IS_DS400)
1650 fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1655 "Add support for your FLAT24 target in %s @ line %d\n",
1656 __FILE__, __LINE__);
1657 exit (EXIT_FAILURE);
1662 #if !OPT_DISABLE_XA51
1664 if (options.model == MODEL_PAGE0)
1666 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1670 if (TARGET_IS_MCS51)
1672 fprintf (lnkfile, "-l mcs51\n");
1674 if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1675 { /*Why the z80 port is not using the standard libraries?*/
1676 fprintf (lnkfile, "-l %s\n", STD_LIB);
1677 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1678 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1679 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1681 else if (TARGET_IS_HC08)
1683 fprintf (lnkfile, "-l hc08\n");
1685 else if (TARGET_IS_Z80)
1687 fprintf (lnkfile, "-l z80\n");
1689 else if (TARGET_IS_GBZ80)
1691 fprintf (lnkfile, "-l gbz80\n");
1695 /*For the z80 and gbz80 ports, try to find where crt0.o is...
1696 It is very important for this file to be first on the linking proccess
1697 so the areas are set in the correct order, expecially _GSINIT*/
1698 if ((TARGET_Z80_LIKE) && !options.no_std_crt0) /*For the z80, gbz80*/
1700 char crt0path[PATH_MAX];
1704 tempSet = appendStrSet(libDirsSet, NULL, DIR_SEPARATOR_STRING);
1705 tempSet = appendStrSet(tempSet, NULL, c);
1706 mergeSets(&tempSet, libPathsSet);
1708 for (s = setFirstItem(tempSet); s != NULL; s = setNextItem(tempSet))
1710 sprintf (crt0path, "%s%scrt0.o",
1711 s, DIR_SEPARATOR_STRING);
1713 crt0fp=fopen(crt0path, "r");
1714 if(crt0fp!=NULL)/*Found it!*/
1719 /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1720 windows paths, so convert them to the CYGWIN format*/
1721 char posix_path[PATH_MAX];
1722 void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1723 cygwin_conv_to_full_posix_path(crt0path, posix_path);
1724 strcpy(crt0path, posix_path);
1727 fprintf (lnkfile, "%s\n", crt0path);
1731 if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1734 /* put in the object files */
1735 if (fullSrcFileName)
1736 fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1738 fputStrSet(lnkfile, relFilesSet);
1740 fprintf (lnkfile, "\n-e\n");
1742 } /* if(port->linker.needLinkerScript) */
1744 if (options.verbose)
1745 printf ("sdcc: Calling linker...\n");
1747 /* build linker output filename */
1749 /* -o option overrides default name? */
1750 if (fullDstFileName)
1752 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1756 /* the linked file gets the name of the first modul */
1757 if (fullSrcFileName)
1759 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1763 s = peekSet(relFilesSet);
1767 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1768 /* strip ".rel" extension */
1769 *strrchr (scratchFileName, '.') = '\0';
1771 strncatz (scratchFileName,
1772 options.out_fmt ? ".S19" : ".ihx",
1773 sizeof(scratchFileName));
1776 if (port->linker.cmd)
1778 char buffer2[PATH_MAX];
1779 char buffer3[PATH_MAX];
1780 set *tempSet=NULL, *libSet=NULL;
1782 strcpy(buffer3, linkerScriptFileName);
1783 if(/*TARGET_IS_PIC16 ||*/ TARGET_IS_PIC) {
1785 /* use $l to set the linker include directories */
1786 tempSet = appendStrSet(libDirsSet, "-I\"", "\"");
1787 mergeSets(&linkOptionsSet, tempSet);
1789 tempSet = appendStrSet(libPathsSet, "-I\"", "\"");
1790 mergeSets(&linkOptionsSet, tempSet);
1792 /* use $3 for libraries from command line --> libSet */
1793 mergeSets(&libSet, libFilesSet);
1795 tempSet = appendStrSet(relFilesSet, "", "");
1796 mergeSets(&libSet, tempSet);
1797 // libSet = reverseSet(libSet);
1799 if(fullSrcFileName) {
1800 // strcpy(buffer3, strrchr(fullSrcFileName, DIR_SEPARATOR_CHAR)+1);
1801 /* if it didn't work, revert to old behaviour */
1802 if(!strlen(buffer3))strcpy(buffer3, dstFileName);
1803 strcat(buffer3, port->linker.rel_ext);
1805 } else strcpy(buffer3, "");
1808 buildCmdLine (buffer2, port->linker.cmd, buffer3, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1810 buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1814 buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1817 /* if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1819 system_ret = my_system (buffer);
1821 /* TODO: most linker don't have a -o parameter */
1822 /* -o option overrides default name? */
1823 if (fullDstFileName)
1826 /* the linked file gets the name of the first modul */
1827 if (fullSrcFileName)
1829 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1830 p = strlen (scratchFileName) + scratchFileName;
1834 s = peekSet(relFilesSet);
1838 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1839 /* strip ".rel" extension */
1840 p = strrchr (scratchFileName, '.');
1846 strncatz (scratchFileName,
1847 options.out_fmt ? ".S19" : ".ihx",
1848 sizeof(scratchFileName));
1849 if (FILENAME_CMP (fullDstFileName, scratchFileName))
1850 remove (fullDstFileName);
1851 rename (scratchFileName, fullDstFileName);
1853 strncpyz (buffer, fullDstFileName, sizeof(buffer));
1854 q = strrchr (buffer, '.');
1857 /* no extension: append new extensions */
1858 q = strlen (buffer) + buffer;
1862 strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1864 strncatz(buffer, ".map", sizeof(buffer));
1865 if (FILENAME_CMP (scratchFileName, buffer))
1867 rename (scratchFileName, buffer);
1869 strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1871 strncatz(buffer, ".mem", sizeof(buffer));
1872 if (FILENAME_CMP (scratchFileName, buffer))
1874 rename (scratchFileName, buffer);
1878 strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1880 strncatz(buffer, ".cdb", sizeof(buffer));
1881 if (FILENAME_CMP (scratchFileName, buffer))
1883 rename (scratchFileName, buffer);
1884 /* and the OMF file without extension: */
1887 if (FILENAME_CMP (scratchFileName, buffer))
1889 rename (scratchFileName, buffer);
1894 exit (EXIT_FAILURE);
1898 /*-----------------------------------------------------------------*/
1899 /* assemble - spawns the assembler with arguments */
1900 /*-----------------------------------------------------------------*/
1902 assemble (char **envp)
1904 /* build assembler output filename */
1906 /* -o option overrides default name? */
1907 if (options.cc_only && fullDstFileName) {
1908 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1910 /* the assembled file gets the name of the first modul */
1911 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1912 strncatz (scratchFileName, port->linker.rel_ext,
1913 sizeof(scratchFileName));
1916 if (port->assembler.do_assemble) {
1917 port->assembler.do_assemble(asmOptionsSet);
1919 } else if (port->assembler.cmd) {
1920 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1921 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1924 buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1927 if (my_system (buffer)) {
1928 /* either system() or the assembler itself has reported an error
1929 perror ("Cannot exec assembler");
1931 exit (EXIT_FAILURE);
1933 /* TODO: most assembler don't have a -o parameter */
1934 /* -o option overrides default name? */
1935 if (options.cc_only && fullDstFileName) {
1936 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1937 strncatz (scratchFileName,
1938 port->linker.rel_ext,
1939 sizeof(scratchFileName));
1940 if (strcmp (scratchFileName, fullDstFileName))
1941 remove (fullDstFileName);
1942 rename (scratchFileName, fullDstFileName);
1946 /*-----------------------------------------------------------------*/
1947 /* preProcess - spawns the preprocessor with arguments */
1948 /*-----------------------------------------------------------------*/
1950 preProcess (char **envp)
1959 set *inclList = NULL;
1961 if (NULL != port->linker.rel_ext)
1963 #define OBJ_EXT_STR "-obj-ext="
1964 #define OBJ_EXT_LEN ((sizeof OBJ_EXT_STR) - 1)
1965 char *buf = Safe_alloc(strlen(port->linker.rel_ext) + (OBJ_EXT_LEN + 1));
1966 strcpy(buf, OBJ_EXT_STR);
1967 strcpy(&buf[OBJ_EXT_LEN], port->linker.rel_ext);
1968 addSet(&preArgvSet, buf);
1971 /* if using dollar signs in identifiers */
1972 if (options.dollars_in_ident)
1973 addSet(&preArgvSet, Safe_strdup("-fdollars-in-identifiers"));
1975 /* if using external stack define the macro */
1976 if (options.useXstack)
1977 addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
1979 /* set the macro for stack autos */
1980 if (options.stackAuto)
1981 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
1983 /* set the macro for stack autos */
1984 if (options.stack10bit)
1985 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
1987 /* set the macro for no overlay */
1988 if (options.noOverlay)
1989 addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
1991 /* set the macro for large model */
1992 switch (options.model)
1995 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
1998 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
2001 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
2004 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
2007 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
2010 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
2013 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
2017 /* set macro corresponding to compiler option */
2018 if (options.intlong_rent)
2019 addSet(&preArgvSet, Safe_strdup("-DSDCC_INT_LONG_REENT"));
2021 /* set macro corresponding to compiler option */
2022 if (options.float_rent)
2023 addSet(&preArgvSet, Safe_strdup("-DSDCC_FLOAT_REENT"));
2025 /* add SDCC version number */
2028 SNPRINTF(buf, sizeof(buf), "-DSDCC=%d%d%d",
2029 SDCC_VERSION_HI, SDCC_VERSION_LO, SDCC_VERSION_P);
2030 addSet(&preArgvSet, Safe_strdup(buf));
2033 /* add SDCC revision number */
2036 SNPRINTF(buf, sizeof(buf), "-DSDCC_REVISION=%s", getBuildNumber());
2037 addSet(&preArgvSet, Safe_strdup(buf));
2040 /* add port (processor information to processor */
2041 addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
2042 addSet(&preArgvSet, Safe_strdup("-D__{port}"));
2044 if (port && port->processor && TARGET_IS_PIC) {
2046 SNPRINTF(&proc[0], 512, "-DSDCC_PROCESSOR=\"%s\"", port->processor);
2047 addSet(&preArgvSet, Safe_strdup(proc));
2050 /* standard include path */
2051 if (!options.nostdinc) {
2052 inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
2053 mergeSets(&preArgvSet, inclList);
2056 setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
2057 Safe_free((void *)s);
2058 if (inclList != NULL)
2059 deleteSet(&inclList);
2061 if (preProcOnly && fullDstFileName)
2063 /* -E and -o given */
2064 setMainValue ("cppoutfilename", fullDstFileName);
2068 /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
2069 setMainValue ("cppoutfilename", NULL);
2072 if (options.verbose)
2073 printf ("sdcc: Calling preprocessor...\n");
2074 buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
2077 if (my_system (buffer)) {
2078 exit (EXIT_FAILURE);
2081 exit (EXIT_SUCCESS);
2084 yyin = my_popen (buffer);
2086 perror ("Preproc file not found");
2087 exit (EXIT_FAILURE);
2096 setBinPaths (const char *argv0)
2104 * 1. - $SDCCDIR/PREFIX2BIN_DIR
2105 * 2. - path(argv[0])
2109 /* do it in reverse mode, so that addSetHead() can be used
2110 instead of slower addSet() */
2112 if ((p = getBinPath (argv0)) != NULL)
2113 addSetHead (&binPathSet, (void *)p);
2115 if ((p = getenv (SDCC_DIR_NAME)) != NULL) {
2116 SNPRINTF (buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
2117 addSetHead (&binPathSet, Safe_strdup (buf));
2121 /* Set system include path */
2123 setIncludePath (void)
2127 set *tempSet = NULL;
2132 * 1. - $SDCC_INCLUDE/target
2133 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2134 * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2135 * 4. - DATADIR/INCLUDE_DIR_SUFFIX/target (only on *nix)
2136 * 5. - $SDCC_INCLUDE
2137 * 6. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
2138 * 7. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
2139 * 8. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
2142 if (options.nostdinc)
2145 tempSet = appendStrSet (dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
2146 includeDirsSet = appendStrSet (tempSet, NULL, DIR_SEPARATOR_STRING);
2147 includeDirsSet = appendStrSet (includeDirsSet, NULL, port->target);
2148 mergeSets (&includeDirsSet, tempSet);
2150 if ((p = getenv (SDCC_INCLUDE_NAME)) != NULL)
2152 addSetHead(&includeDirsSet, p);
2153 p2=Safe_alloc(strlen(p)+strlen(DIR_SEPARATOR_STRING)+strlen(port->target)+1);
2157 strcat (p2, DIR_SEPARATOR_STRING);
2158 strcat (p2, port->target);
2159 addSetHead (&includeDirsSet, p2);
2164 /* Set system lib path */
2174 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
2175 * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
2176 * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
2179 if (options.nostdlib)
2182 libDirsSet = appendStrSet (dataDirsSet, NULL, LIB_DIR_SUFFIX);
2184 if ((p = getenv (SDCC_LIB_NAME)) != NULL)
2185 addSetHead (&libDirsSet, p);
2190 setDataPaths (const char *argv0)
2198 * 1. - $SDCC_HOME/PREFIX2DATA_DIR
2199 * 2. - path(argv[0])/BIN2DATA_DIR
2200 * 3. - DATADIR (only on *nix)
2203 if ((p = getenv (SDCC_DIR_NAME)) != NULL) {
2204 SNPRINTF (buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
2205 addSet (&dataDirsSet, Safe_strdup (buf));
2208 if ((p = getBinPath (argv0)) != NULL) {
2209 SNPRINTF (buf, sizeof buf, "%s" BIN2DATA_DIR, p);
2210 Safe_free ((void *)p);
2211 addSet (&dataDirsSet, Safe_strdup(buf));
2215 if (peekSet (dataDirsSet) == NULL) {
2216 /* this should never happen... */
2217 wassertl (0, "Can't get binary path");
2220 addSet (&dataDirsSet, Safe_strdup (DATADIR));
2230 populateMainValues (_baseValues);
2231 setMainValue ("port", port->target);
2232 setMainValue ("objext", port->linker.rel_ext);
2233 setMainValue ("asmext", port->assembler.file_ext);
2235 setMainValue ("dstfilename", dstFileName);
2236 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2238 if (options.cc_only && fullDstFileName)
2239 /* compile + assemble and -o given: -o specifies name of object file */
2241 setMainValue ("objdstfilename", fullDstFileName);
2245 setMainValue ("objdstfilename", "{stdobjdstfilename}");
2247 if (fullDstFileName)
2248 /* if we're linking, -o gives the final file name */
2250 setMainValue ("linkdstfilename", fullDstFileName);
2254 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2258 * Make sure the preprocessor is called with the "-std" option
2259 * corresponding to the --std used to start sdcc
2261 if (options.std_c99)
2263 if (!options.std_sdcc)
2264 setMainValue ("cppstd", "-std=c99 ");
2268 if (!options.std_sdcc)
2269 setMainValue ("cppstd", "-std=c89 ");
2273 static void doPrintSearchDirs(void)
2275 printf("programs:\n");
2276 fputStrSet(stdout, binPathSet);
2278 printf("datadir:\n");
2279 fputStrSet(stdout, dataDirsSet);
2281 printf("includedir:\n");
2282 fputStrSet(stdout, includeDirsSet);
2284 printf("libdir:\n");
2285 fputStrSet(stdout, libDirsSet);
2286 fputStrSet(stdout, libPathsSet);
2291 sig_handler (int signal)
2298 sig_string = "SIGABRT";
2301 sig_string = "SIGTERM";
2304 sig_string = "SIGINT";
2307 sig_string = "SIGSEGV";
2310 sig_string = "Unknown?";
2313 fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2314 exit (EXIT_FAILURE);
2319 * initialises and calls the parser
2323 main (int argc, char **argv, char **envp)
2325 /* turn all optimizations off by default */
2326 memset (&optimize, 0, sizeof (struct optimize));
2330 fprintf (stderr, "Build error: no ports are enabled.\n");
2331 exit (EXIT_FAILURE);
2334 /* install signal handler;
2335 it's only purpose is to call exit() to remove temp files */
2336 if (!getenv ("SDCC_LEAVE_SIGNALS"))
2338 signal (SIGABRT, sig_handler);
2339 signal (SIGTERM, sig_handler);
2340 signal (SIGINT, sig_handler);
2341 signal (SIGSEGV, sig_handler);
2344 /* Before parsing the command line options, do a
2345 * search for the port and processor and initialize
2346 * them if they're found. (We can't gurantee that these
2347 * will be the first options specified).
2350 _findPort (argc, argv);
2353 if (strcmp (port->target, "mcs51") == 0) {
2354 printf ("DS390 jammed in A\n");
2360 _findProcessor (argc, argv);
2362 /* Initalise the port. */
2366 setDefaultOptions ();
2369 options.model = MODEL_SMALL;
2370 options.stack10bit=0;
2374 parseCmdLine (argc, argv);
2376 if (options.verbose && NULL != port->processor)
2377 printf("Processor: %s\n", port->processor);
2381 setBinPaths (argv[0]);
2382 setDataPaths (argv[0]);
2384 if (port->initPaths)
2387 if (options.printSearchDirs)
2388 doPrintSearchDirs();
2390 /* if no input then printUsage & exit */
2391 if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL)
2393 if (options.printSearchDirs)
2394 exit (EXIT_SUCCESS);
2396 exit (EXIT_FAILURE);
2399 /* initMem() is expensive, but
2400 initMem() must called before port->finaliseOptions ().
2401 And the z80 port needs port->finaliseOptions(),
2402 even if we're only linking. */
2405 /* finalize target specific options */
2406 port->finaliseOptions ();
2408 /* finalize common options */
2411 if (fullSrcFileName || options.c1mode)
2421 if (options.verbose)
2422 printf ("sdcc: Generating code...\n");
2426 if (!options.c1mode)
2431 exit (EXIT_FAILURE);
2433 if (port->general.do_glue != NULL)
2434 (*port->general.do_glue) ();
2437 /* this shouldn't happen */
2439 /* in case of NDEBUG */
2444 exit (EXIT_FAILURE);
2446 if (!options.c1mode && !noAssemble)
2448 if (options.verbose)
2449 printf ("sdcc: Calling assembler...\n");
2455 if (options.debug && debugFile)
2456 debugFile->closeFile ();
2458 if (!options.cc_only &&
2462 (fullSrcFileName || peekSet (relFilesSet) != NULL))
2464 if (options.verbose)
2465 printf ("sdcc: Calling linker...\n");
2467 if (port->linker.do_link)
2468 port->linker.do_link ();