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 reenterant calls on the int and long support functions" },
190 { 0, "--float-reent", &options.float_rent, "Use reenterant 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 -std=c99 {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
429 " #%s (" __DATE__ ") (%s)\n",
430 getBuildNumber(), 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);
1313 /* if no dstFileName given with -o, we've to find one: */
1318 /* use the modulename from the C-source */
1319 if (fullSrcFileName)
1323 if (*dstPath != '\0')
1325 dbuf_init(&path, 128);
1326 dbuf_makePath (&path, dstPath, moduleNameBase);
1328 dstFileName = dbuf_detach (&path);
1331 dstFileName = Safe_strdup(moduleNameBase);
1333 /* use the modulename from the first object file */
1334 else if ((s = peekSet(relFilesSet)) != NULL)
1338 dbuf_init(&file, 128);
1340 /* get rid of the "."-extension */
1341 dbuf_splitFile (s, &file, NULL);
1344 s = dbuf_detach (&file);
1346 dbuf_init (&file, 128);
1348 dbuf_splitPath (s, NULL, &file);
1350 if (*dstPath != '\0')
1354 dbuf_init(&path, 128);
1355 dbuf_makePath (&path, dstPath, dbuf_c_str (&file));
1356 dbuf_destroy (&file);
1358 dstFileName = dbuf_detach (&path);
1363 dstFileName = dbuf_detach (&file);
1366 /* else no module given: help text is displayed */
1369 /* set int, long and float reentrancy based on stack-auto */
1370 if (options.stackAuto)
1372 options.intlong_rent++;
1373 options.float_rent++;
1376 /* mcs51 has an assembly coded float library that's always reentrant */
1377 if (TARGET_IS_MCS51)
1379 options.float_rent++;
1381 /* set up external stack location if not explicitly specified */
1382 if (!options.xstack_loc)
1383 options.xstack_loc = options.xdata_loc;
1386 /* if debug option is set then open the cdbFile */
1387 if (options.debug && fullSrcFileName)
1389 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1390 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1391 if(debugFile->openFile(scratchFileName))
1392 debugFile->writeModule(moduleName);
1394 werror (E_FILE_OPEN_ERR, scratchFileName);
1396 MSVC_style(options.vc_err_style);
1401 /*-----------------------------------------------------------------*/
1402 /* finalizeOptions - finalize (post-process( options */
1403 /*-----------------------------------------------------------------*/
1405 finalizeOptions (void)
1407 /* no peephole comments if not verbose asm */
1408 if (!options.verboseAsm)
1409 options.noPeepComments = 1;
1412 /*-----------------------------------------------------------------*/
1413 /* linkEdit : - calls the linkage editor with options */
1414 /*-----------------------------------------------------------------*/
1416 linkEdit (char **envp)
1422 char linkerScriptFileName[PATH_MAX];
1424 linkerScriptFileName[0] = 0;
1427 if (port->linker.needLinkerScript)
1431 switch (options.out_fmt)
1434 out_fmt = 'i'; /* Intel hex */
1437 out_fmt = 's'; /* Motorola S19 */
1440 out_fmt = 't'; /* Elf */
1446 /* first we need to create the <filename>.lnk file */
1447 SNPRINTF (linkerScriptFileName, sizeof(linkerScriptFileName),
1448 "%s.lnk", dstFileName);
1449 if (!(lnkfile = fopen (linkerScriptFileName, "w")))
1451 werror (E_FILE_OPEN_ERR, linkerScriptFileName);
1452 exit (EXIT_FAILURE);
1455 if (TARGET_Z80_LIKE)
1457 fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1458 out_fmt, dstFileName);
1460 else /*For all the other ports. Including pics???*/
1462 fprintf (lnkfile, "-myux%c\n", out_fmt);
1463 if(!options.no_pack_iram)
1464 fprintf (lnkfile, "-Y\n");
1467 if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1469 /* if iram size specified */
1470 if (options.iram_size)
1471 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1473 /* if stack size specified*/
1474 if(options.stack_size)
1475 fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1477 /* if xram size specified */
1478 if (options.xram_size_set)
1479 fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1481 /* if code size specified */
1482 if (options.code_size)
1483 fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1486 fprintf (lnkfile, "-z\n");
1489 #define WRITE_SEG_LOC(N, L) \
1492 segName = Safe_strdup(N); \
1493 c = strtok(segName, " \t"); \
1494 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1495 if (segName) { Safe_free(segName); } \
1498 if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
1501 /* code segment start */
1502 WRITE_SEG_LOC (HOME_NAME, options.code_loc);
1504 /* data segment start. If zero, the linker chooses
1505 the best place for data */
1506 if (options.data_loc)
1508 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1511 /* xdata segment start. If zero, the linker chooses
1512 the best place for xdata */
1513 if (options.xdata_loc)
1515 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1518 /* pdata/xstack segment start. If zero, the linker
1519 chooses the best place for them */
1520 if (options.xstack_loc)
1522 WRITE_SEG_LOC (PDATA_NAME, options.xstack_loc);
1528 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1531 /* bit segment start */
1532 WRITE_SEG_LOC (BIT_NAME, 0);
1535 if ( (options.stack_loc) && (options.stack_loc<0x100) &&
1538 WRITE_SEG_LOC ("SSEG", options.stack_loc);
1541 else /*For the z80, gbz80*/
1543 WRITE_SEG_LOC ("_CODE", options.code_loc);
1544 WRITE_SEG_LOC ("_DATA", options.data_loc);
1547 /* If the port has any special linker area declarations, get 'em */
1548 if (port->extraAreas.genExtraAreaLinkOptions)
1550 port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1553 /* add the extra linker options */
1554 fputStrSet(lnkfile, linkOptionsSet);
1556 /* command line defined library paths if specified */
1557 for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1558 fprintf (lnkfile, "-k %s\n", s);
1560 /* standard library path */
1561 if (!options.nostdlib)
1563 if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1565 switch (options.model)
1568 if (options.stackAuto)
1569 c = "small-stack-auto";
1574 if (options.stackAuto)
1575 c = "medium-stack-auto";
1580 if (options.stackAuto)
1581 c = "large-stack-auto";
1587 if (TARGET_IS_DS390)
1591 else if (TARGET_IS_DS400)
1598 "Add support for your FLAT24 target in %s @ line %d\n",
1599 __FILE__, __LINE__);
1600 exit (EXIT_FAILURE);
1607 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1612 else /*for the z80, gbz80*/
1616 else if (TARGET_IS_Z80)
1621 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1622 mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1625 /* command line defined library files if specified */
1626 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1627 fprintf (lnkfile, "-l %s\n", s);
1629 /* standard library files */
1630 if (!options.nostdlib)
1632 #if !OPT_DISABLE_DS390
1633 if (options.model == MODEL_FLAT24)
1635 if (TARGET_IS_DS390)
1637 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1639 else if (TARGET_IS_DS400)
1641 fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1646 "Add support for your FLAT24 target in %s @ line %d\n",
1647 __FILE__, __LINE__);
1648 exit (EXIT_FAILURE);
1653 #if !OPT_DISABLE_XA51
1655 if (options.model == MODEL_PAGE0)
1657 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1661 if (TARGET_IS_MCS51)
1663 fprintf (lnkfile, "-l mcs51\n");
1665 if (!(TARGET_Z80_LIKE || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1666 { /*Why the z80 port is not using the standard libraries?*/
1667 fprintf (lnkfile, "-l %s\n", STD_LIB);
1668 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1669 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1670 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1672 else if (TARGET_IS_HC08)
1674 fprintf (lnkfile, "-l hc08\n");
1676 else if (TARGET_IS_Z80)
1678 fprintf (lnkfile, "-l z80\n");
1680 else if (TARGET_IS_GBZ80)
1682 fprintf (lnkfile, "-l gbz80\n");
1686 /*For the z80 and gbz80 ports, try to find where crt0.o is...
1687 It is very important for this file to be first on the linking proccess
1688 so the areas are set in the correct order, expecially _GSINIT*/
1689 if ((TARGET_Z80_LIKE) && !options.no_std_crt0) /*For the z80, gbz80*/
1691 char crt0path[PATH_MAX];
1695 tempSet = appendStrSet(libDirsSet, NULL, DIR_SEPARATOR_STRING);
1696 tempSet = appendStrSet(tempSet, NULL, c);
1697 mergeSets(&tempSet, libPathsSet);
1699 for (s = setFirstItem(tempSet); s != NULL; s = setNextItem(tempSet))
1701 sprintf (crt0path, "%s%scrt0.o",
1702 s, DIR_SEPARATOR_STRING);
1704 crt0fp=fopen(crt0path, "r");
1705 if(crt0fp!=NULL)/*Found it!*/
1710 /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1711 windows paths, so convert them to the CYGWIN format*/
1712 char posix_path[PATH_MAX];
1713 void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1714 cygwin_conv_to_full_posix_path(crt0path, posix_path);
1715 strcpy(crt0path, posix_path);
1718 fprintf (lnkfile, "%s\n", crt0path);
1722 if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1725 /* put in the object files */
1726 if (fullSrcFileName)
1727 fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1729 fputStrSet(lnkfile, relFilesSet);
1731 fprintf (lnkfile, "\n-e\n");
1733 } /* if(port->linker.needLinkerScript) */
1735 if (options.verbose)
1736 printf ("sdcc: Calling linker...\n");
1738 /* build linker output filename */
1740 /* -o option overrides default name? */
1741 if (fullDstFileName)
1743 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1747 /* the linked file gets the name of the first modul */
1748 if (fullSrcFileName)
1750 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1754 s = peekSet(relFilesSet);
1758 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1759 /* strip ".rel" extension */
1760 *strrchr (scratchFileName, '.') = '\0';
1762 strncatz (scratchFileName,
1763 options.out_fmt ? ".S19" : ".ihx",
1764 sizeof(scratchFileName));
1767 if (port->linker.cmd)
1769 char buffer2[PATH_MAX];
1770 char buffer3[PATH_MAX];
1771 set *tempSet=NULL, *libSet=NULL;
1773 strcpy(buffer3, linkerScriptFileName);
1774 if(/*TARGET_IS_PIC16 ||*/ TARGET_IS_PIC) {
1776 /* use $l to set the linker include directories */
1777 tempSet = appendStrSet(libDirsSet, "-I\"", "\"");
1778 mergeSets(&linkOptionsSet, tempSet);
1780 tempSet = appendStrSet(libPathsSet, "-I\"", "\"");
1781 mergeSets(&linkOptionsSet, tempSet);
1783 /* use $3 for libraries from command line --> libSet */
1784 mergeSets(&libSet, libFilesSet);
1786 tempSet = appendStrSet(relFilesSet, "", "");
1787 mergeSets(&libSet, tempSet);
1788 // libSet = reverseSet(libSet);
1790 if(fullSrcFileName) {
1791 // strcpy(buffer3, strrchr(fullSrcFileName, DIR_SEPARATOR_CHAR)+1);
1792 /* if it didn't work, revert to old behaviour */
1793 if(!strlen(buffer3))strcpy(buffer3, dstFileName);
1794 strcat(buffer3, port->linker.rel_ext);
1796 } else strcpy(buffer3, "");
1799 buildCmdLine (buffer2, port->linker.cmd, buffer3, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1801 buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1805 buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1808 /* if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1810 system_ret = my_system (buffer);
1812 /* TODO: most linker don't have a -o parameter */
1813 /* -o option overrides default name? */
1814 if (fullDstFileName)
1817 /* the linked file gets the name of the first modul */
1818 if (fullSrcFileName)
1820 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1821 p = strlen (scratchFileName) + scratchFileName;
1825 s = peekSet(relFilesSet);
1829 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1830 /* strip ".rel" extension */
1831 p = strrchr (scratchFileName, '.');
1837 strncatz (scratchFileName,
1838 options.out_fmt ? ".S19" : ".ihx",
1839 sizeof(scratchFileName));
1840 if (FILENAME_CMP (fullDstFileName, scratchFileName))
1841 remove (fullDstFileName);
1842 rename (scratchFileName, fullDstFileName);
1844 strncpyz (buffer, fullDstFileName, sizeof(buffer));
1845 q = strrchr (buffer, '.');
1848 /* no extension: append new extensions */
1849 q = strlen (buffer) + buffer;
1853 strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1855 strncatz(buffer, ".map", sizeof(buffer));
1856 if (FILENAME_CMP (scratchFileName, buffer))
1858 rename (scratchFileName, buffer);
1860 strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1862 strncatz(buffer, ".mem", sizeof(buffer));
1863 if (FILENAME_CMP (scratchFileName, buffer))
1865 rename (scratchFileName, buffer);
1869 strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1871 strncatz(buffer, ".cdb", sizeof(buffer));
1872 if (FILENAME_CMP (scratchFileName, buffer))
1874 rename (scratchFileName, buffer);
1875 /* and the OMF file without extension: */
1878 if (FILENAME_CMP (scratchFileName, buffer))
1880 rename (scratchFileName, buffer);
1885 exit (EXIT_FAILURE);
1889 /*-----------------------------------------------------------------*/
1890 /* assemble - spawns the assembler with arguments */
1891 /*-----------------------------------------------------------------*/
1893 assemble (char **envp)
1895 /* build assembler output filename */
1897 /* -o option overrides default name? */
1898 if (options.cc_only && fullDstFileName) {
1899 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1901 /* the assembled file gets the name of the first modul */
1902 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1903 strncatz (scratchFileName, port->linker.rel_ext,
1904 sizeof(scratchFileName));
1907 if (port->assembler.do_assemble) {
1908 port->assembler.do_assemble(asmOptionsSet);
1910 } else if (port->assembler.cmd) {
1911 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1912 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1915 buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1918 if (my_system (buffer)) {
1919 /* either system() or the assembler itself has reported an error
1920 perror ("Cannot exec assembler");
1922 exit (EXIT_FAILURE);
1924 /* TODO: most assembler don't have a -o parameter */
1925 /* -o option overrides default name? */
1926 if (options.cc_only && fullDstFileName) {
1927 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1928 strncatz (scratchFileName,
1929 port->linker.rel_ext,
1930 sizeof(scratchFileName));
1931 if (strcmp (scratchFileName, fullDstFileName))
1932 remove (fullDstFileName);
1933 rename (scratchFileName, fullDstFileName);
1937 /*-----------------------------------------------------------------*/
1938 /* preProcess - spawns the preprocessor with arguments */
1939 /*-----------------------------------------------------------------*/
1941 preProcess (char **envp)
1950 set *inclList = NULL;
1952 if (NULL != port->linker.rel_ext)
1954 #define OBJ_EXT_STR "-obj-ext="
1955 #define OBJ_EXT_LEN ((sizeof OBJ_EXT_STR) - 1)
1956 char *buf = Safe_alloc(strlen(port->linker.rel_ext) + (OBJ_EXT_LEN + 1));
1957 strcpy(buf, OBJ_EXT_STR);
1958 strcpy(&buf[OBJ_EXT_LEN], port->linker.rel_ext);
1959 addSet(&preArgvSet, buf);
1962 /* if using dollar signs in identifiers */
1963 if (options.dollars_in_ident)
1964 addSet(&preArgvSet, Safe_strdup("-fdollars-in-identifiers"));
1966 /* if using external stack define the macro */
1967 if (options.useXstack)
1968 addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
1970 /* set the macro for stack autos */
1971 if (options.stackAuto)
1972 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
1974 /* set the macro for stack autos */
1975 if (options.stack10bit)
1976 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
1978 /* set the macro for no overlay */
1979 if (options.noOverlay)
1980 addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
1982 /* set the macro for large model */
1983 switch (options.model)
1986 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
1989 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
1992 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
1995 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
1998 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
2001 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
2004 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
2008 /* add SDCC version number */
2011 SNPRINTF(buf, sizeof(buf), "-DSDCC=%d%d%d",
2012 SDCC_VERSION_HI, SDCC_VERSION_LO, SDCC_VERSION_P);
2013 addSet(&preArgvSet, Safe_strdup(buf));
2016 /* add port (processor information to processor */
2017 addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
2018 addSet(&preArgvSet, Safe_strdup("-D__{port}"));
2020 if (port && port->processor && TARGET_IS_PIC) {
2022 SNPRINTF(&proc[0], 512, "-DSDCC_PROCESSOR=\"%s\"", port->processor);
2023 addSet(&preArgvSet, Safe_strdup(proc));
2026 /* standard include path */
2027 if (!options.nostdinc) {
2028 inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
2029 mergeSets(&preArgvSet, inclList);
2032 setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
2033 Safe_free((void *)s);
2034 if (inclList != NULL)
2035 deleteSet(&inclList);
2037 if (preProcOnly && fullDstFileName)
2039 /* -E and -o given */
2040 setMainValue ("cppoutfilename", fullDstFileName);
2044 /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
2045 setMainValue ("cppoutfilename", NULL);
2048 if (options.verbose)
2049 printf ("sdcc: Calling preprocessor...\n");
2050 buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
2053 if (my_system (buffer)) {
2054 exit (EXIT_FAILURE);
2057 exit (EXIT_SUCCESS);
2060 yyin = my_popen (buffer);
2062 perror ("Preproc file not found");
2063 exit (EXIT_FAILURE);
2072 setBinPaths(const char *argv0)
2080 * 1. - $SDCCDIR/PREFIX2BIN_DIR
2081 * 2. - path(argv[0])
2085 /* do it in reverse mode, so that addSetHead() can be used
2086 instead of slower addSet() */
2088 if ((p = getBinPath(argv0)) != NULL)
2089 addSetHead(&binPathSet, (void *)p);
2091 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2092 SNPRINTF(buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
2093 addSetHead(&binPathSet, Safe_strdup(buf));
2097 /* Set system include path */
2099 setIncludePath(void)
2108 * 1. - $SDCC_INCLUDE/target
2109 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2110 * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2111 * 4. - DATADIR/INCLUDE_DIR_SUFFIX/target (only on *nix)
2112 * 5. - $SDCC_INCLUDE
2113 * 6. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
2114 * 7. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
2115 * 8. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
2118 if (options.nostdinc)
2121 tempSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
2122 includeDirsSet = appendStrSet(tempSet, NULL, DIR_SEPARATOR_STRING);
2123 includeDirsSet = appendStrSet(includeDirsSet, NULL, port->target);
2124 mergeSets(&includeDirsSet, tempSet);
2126 if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
2128 addSetHead(&includeDirsSet, p);
2129 p2=Safe_alloc(strlen(p)+strlen(DIR_SEPARATOR_STRING)+strlen(port->target)+1);
2133 strcat(p2, DIR_SEPARATOR_STRING);
2134 strcat(p2, port->target);
2135 addSetHead(&includeDirsSet, p2);
2140 /* Set system lib path */
2150 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
2151 * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
2152 * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
2155 if (options.nostdlib)
2158 libDirsSet = appendStrSet(dataDirsSet, NULL, LIB_DIR_SUFFIX);
2160 if ((p = getenv(SDCC_LIB_NAME)) != NULL)
2161 addSetHead(&libDirsSet, p);
2166 setDataPaths(const char *argv0)
2174 * 1. - $SDCC_HOME/PREFIX2DATA_DIR
2175 * 2. - path(argv[0])/BIN2DATA_DIR
2176 * 3. - DATADIR (only on *nix)
2179 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2180 SNPRINTF(buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
2181 addSet(&dataDirsSet, Safe_strdup(buf));
2184 if ((p = getBinPath(argv0)) != NULL) {
2185 SNPRINTF(buf, sizeof buf, "%s" BIN2DATA_DIR, p);
2187 addSet(&dataDirsSet, Safe_strdup(buf));
2191 if (peekSet(dataDirsSet) == NULL) {
2192 /* this should never happen... */
2193 wassertl(0, "Can't get binary path");
2196 addSet(&dataDirsSet, Safe_strdup(DATADIR));
2206 populateMainValues (_baseValues);
2207 setMainValue ("port", port->target);
2208 setMainValue ("objext", port->linker.rel_ext);
2209 setMainValue ("asmext", port->assembler.file_ext);
2211 setMainValue ("dstfilename", dstFileName);
2212 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2214 if (options.cc_only && fullDstFileName)
2215 /* compile + assemble and -o given: -o specifies name of object file */
2217 setMainValue ("objdstfilename", fullDstFileName);
2221 setMainValue ("objdstfilename", "{stdobjdstfilename}");
2223 if (fullDstFileName)
2224 /* if we're linking, -o gives the final file name */
2226 setMainValue ("linkdstfilename", fullDstFileName);
2230 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2235 static void doPrintSearchDirs(void)
2237 printf("programs:\n");
2238 fputStrSet(stdout, binPathSet);
2240 printf("datadir:\n");
2241 fputStrSet(stdout, dataDirsSet);
2243 printf("includedir:\n");
2244 fputStrSet(stdout, includeDirsSet);
2246 printf("libdir:\n");
2247 fputStrSet(stdout, libDirsSet);
2248 fputStrSet(stdout, libPathsSet);
2253 sig_handler (int signal)
2260 sig_string = "SIGABRT";
2263 sig_string = "SIGTERM";
2266 sig_string = "SIGINT";
2269 sig_string = "SIGSEGV";
2272 sig_string = "Unknown?";
2275 fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2276 exit (EXIT_FAILURE);
2281 * initialises and calls the parser
2285 main (int argc, char **argv, char **envp)
2287 /* turn all optimizations off by default */
2288 memset (&optimize, 0, sizeof (struct optimize));
2292 fprintf (stderr, "Build error: no ports are enabled.\n");
2293 exit (EXIT_FAILURE);
2296 /* install signal handler;
2297 it's only purpose is to call exit() to remove temp files */
2298 if (!getenv("SDCC_LEAVE_SIGNALS"))
2300 signal (SIGABRT, sig_handler);
2301 signal (SIGTERM, sig_handler);
2302 signal (SIGINT, sig_handler);
2303 signal (SIGSEGV, sig_handler);
2306 /* Before parsing the command line options, do a
2307 * search for the port and processor and initialize
2308 * them if they're found. (We can't gurantee that these
2309 * will be the first options specified).
2312 _findPort (argc, argv);
2315 if (strcmp(port->target, "mcs51") == 0) {
2316 printf("DS390 jammed in A\n");
2322 _findProcessor (argc, argv);
2324 /* Initalise the port. */
2328 setDefaultOptions ();
2331 options.model = MODEL_SMALL;
2332 options.stack10bit=0;
2336 parseCmdLine (argc, argv);
2338 if (options.verbose && NULL != port->processor)
2339 printf("Processor: %s\n", port->processor);
2343 setBinPaths(argv[0]);
2344 setDataPaths(argv[0]);
2346 if (port->initPaths)
2349 if (options.printSearchDirs)
2350 doPrintSearchDirs();
2352 /* if no input then printUsage & exit */
2353 if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL)
2355 if (options.printSearchDirs)
2356 exit (EXIT_SUCCESS);
2358 exit (EXIT_FAILURE);
2361 /* initMem() is expensive, but
2362 initMem() must called before port->finaliseOptions ().
2363 And the z80 port needs port->finaliseOptions(),
2364 even if we're only linking. */
2367 /* finalize target specific options */
2368 port->finaliseOptions ();
2370 /* finalize common options */
2373 if (fullSrcFileName || options.c1mode)
2383 if (options.verbose)
2384 printf ("sdcc: Generating code...\n");
2392 exit (EXIT_FAILURE);
2395 if (port->general.do_glue != NULL)
2396 (*port->general.do_glue) ();
2399 /* this shouldn't happen */
2401 /* in case of NDEBUG */
2406 exit (EXIT_FAILURE);
2409 if (!options.c1mode && !noAssemble)
2411 if (options.verbose)
2412 printf ("sdcc: Calling assembler...\n");
2418 if (options.debug && debugFile)
2419 debugFile->closeFile();
2421 if (!options.cc_only &&
2425 (fullSrcFileName || peekSet(relFilesSet) != NULL))
2427 if (options.verbose)
2428 printf ("sdcc: Calling linker...\n");
2430 if (port->linker.do_link)
2431 port->linker.do_link ();