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 -------------------------------------------------------------------------*/
38 #include "SDCCmacro.h"
40 #include "SDCCdebug.h"
51 extern int yyparse (void);
53 FILE *srcFile; /* source file */
54 char *fullSrcFileName; /* full name for the source file; */
55 /* can be NULL while c1mode or linking without compiling */
56 char *fullDstFileName; /* full name for the output file; */
57 /* only given by -o, otherwise NULL */
58 char *dstFileName; /* destination file name without extension */
59 char *dstPath = ""; /* path for the output files; */
60 /* "" is equivalent with cwd */
61 char *moduleNameBase; /* module name base is source file without path and extension */
62 /* can be NULL while linking without compiling */
63 char *moduleName; /* module name is same as module name base, but with all */
64 /* non-alphanumeric characters replaced with underscore */
66 int RegBankUsed[4] = {1, 0, 0, 0}; /*JCF: Reg Bank 0 used by default*/
67 struct optimize optimize;
68 struct options options;
71 set *preArgvSet = NULL; /* pre-processor arguments */
72 set *asmOptionsSet = NULL; /* set of assembler options */
73 set *linkOptionsSet = NULL; /* set of linker options */
74 set *libFilesSet = NULL;
75 set *libPathsSet = NULL;
76 set *relFilesSet = NULL;
77 set *dataDirsSet = NULL; /* list of data search directories */
78 set *includeDirsSet = NULL; /* list of include search directories */
79 set *userIncDirsSet = NULL; /* list of user include directories */
80 set *libDirsSet = NULL; /* list of lib search directories */
82 /* uncomment JAMIN_DS390 to always override and use ds390 port
83 for mcs51 work. This is temporary, for compatibility testing. */
84 /* #define JAMIN_DS390 */
89 /* Globally accessible scratch buffer for file names. */
90 char scratchFileName[PATH_MAX];
91 char buffer[PATH_MAX * 2];
93 #define OPTION_HELP "-help"
95 #define LENGTH(_a) (sizeof(_a)/sizeof(*(_a)))
97 #define OPTION_STACK_8BIT "--stack-8bit"
98 #define OPTION_OUT_FMT_IHX "--out-fmt-ihx"
99 #define OPTION_LARGE_MODEL "--model-large"
100 #define OPTION_MEDIUM_MODEL "--model-medium"
101 #define OPTION_SMALL_MODEL "--model-small"
102 #define OPTION_FLAT24_MODEL "--model-flat24"
103 #define OPTION_DUMP_ALL "--dumpall"
104 #define OPTION_PEEP_FILE "--peep-file"
105 #define OPTION_LIB_PATH "--lib-path"
106 #define OPTION_CALLEE_SAVES "--callee-saves"
107 #define OPTION_STACK_LOC "--stack-loc"
108 #define OPTION_XSTACK_LOC "--xstack-loc"
109 #define OPTION_DATA_LOC "--data-loc"
110 #define OPTION_IDATA_LOC "--idata-loc"
111 #define OPTION_XRAM_LOC "--xram-loc"
112 #define OPTION_CODE_LOC "--code-loc"
113 #define OPTION_STACK_SIZE "--stack-size"
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_NO_GCSE "--nogcse"
124 #define OPTION_SHORT_IS_8BITS "--short-is-8bits"
125 #define OPTION_TINI_LIBID "--tini-libid"
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_PACK_IRAM "--pack-iram"
133 #define OPTION_NO_PACK_IRAM "--no-pack-iram"
134 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
135 #define OPTION_OPT_CODE_SPEED "--opt-code-speed"
136 #define OPTION_OPT_CODE_SIZE "--opt-code-size"
140 { 0, NULL, NULL, "General options" },
141 { 0, "--help", NULL, "Display this help" },
142 { 'v', OPTION_VERSION, NULL, "Display sdcc's version" },
143 { 0, "--verbose", &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
144 { 'V', NULL, &options.verboseExec, "Execute verbosely. Show sub commands as they are run" },
145 { 'd', NULL, NULL, NULL },
146 { 'D', NULL, NULL, "Define macro as in -Dmacro" },
147 { 'I', NULL, NULL, "Add to the include (*.h) path, as in -Ipath" },
148 { 'A', NULL, NULL, NULL },
149 { 'U', NULL, NULL, NULL },
150 { 'C', NULL, NULL, "Preprocessor option" },
151 { 'M', NULL, NULL, "Preprocessor option" },
152 { 'W', NULL, NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
153 { 'S', NULL, &noAssemble, "Compile only; do not assemble or link" },
154 { 'c', "--compile-only", &options.cc_only, "Compile and assemble, but do not link" },
155 { 'E', "--preprocessonly", &preProcOnly, "Preprocess only, do not compile" },
156 { 0, "--c1mode", &options.c1mode, "Act in c1 mode. The standard input is preprocessed code, the output is assembly code." },
157 { 'o', NULL, NULL, "Place the output into the given path resp. file" },
158 { 0, OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
159 { 0, OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
160 { 0, OPTION_USE_STDOUT, &options.use_stdout, "send errors to stdout instead of stderr"},
161 { 0, "--nostdlib", &options.nostdlib, "Do not include the standard library directory in the search path" },
162 { 0, "--nostdinc", &options.nostdinc, "Do not include the standard include directory in the search path" },
163 { 0, OPTION_LESS_PEDANTIC, NULL, "Disable some of the more pedantic warnings" },
164 { 0, OPTION_DISABLE_WARNING, NULL, "<nnnn> Disable specific warning" },
165 { 0, "--debug", &options.debug, "Enable debugging symbol output" },
166 { 0, "--cyclomatic", &options.cyclomatic, "Display complexity of compiled functions" },
168 { 0, NULL, NULL, "Code generation options"},
169 { 'm', NULL, NULL, "Set the port to use e.g. -mz80." },
170 { 'p', NULL, NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
171 { 0, OPTION_LARGE_MODEL, NULL, "external data space is used" },
172 { 0, OPTION_MEDIUM_MODEL, NULL, "not supported" },
173 { 0, OPTION_SMALL_MODEL, NULL, "internal data space is used (default)" },
174 #if !OPT_DISABLE_DS390
175 { 0, OPTION_FLAT24_MODEL, NULL, "use the flat24 model for the ds390 (default)" },
176 { 0, OPTION_STACK_8BIT, NULL, "use the 8bit stack for the ds390 (not supported yet)" },
177 { 0, "--stack-10bit", &options.stack10bit, "use the 10bit stack for ds390 (default)" },
179 { 0, "--stack-auto", &options.stackAuto, "Stack automatic variables" },
180 { 0, "--xstack", &options.useXstack, "Use external stack" },
181 { 0, "--int-long-reent", &options.intlong_rent, "Use reenterant calls on the int and long support functions" },
182 { 0, "--float-reent", &options.float_rent, "Use reenterant calls on the float support functions" },
183 { 0, "--main-return", &options.mainreturn, "Issue a return after main()" },
184 { 0, "--xram-movc", &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
185 { 0, OPTION_CALLEE_SAVES, NULL, "<func[,func,...]> Cause the called function to save registers insted of the caller" },
186 { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" },
187 { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
188 { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" },
189 #if !OPT_DISABLE_DS390
190 { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"},
192 { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
193 #if !OPT_DISABLE_TININative
194 { 0, "--tini-libid", NULL,"<nnnn> LibraryID used in -mTININative"},
196 #if !OPT_DISABLE_DS390
197 { 0, "--protect-sp-update", &options.protect_sp_update,"DS390 - will disable interrupts during ESP:SP updates"},
199 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
200 { 0, "--parms-in-bank1", &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
202 { 0, OPTION_NO_XINIT_OPT, &options.noXinitOpt, "don't memcpy initialized xram from code"},
203 { 0, OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
204 { 0, OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
205 #if !OPT_DISABLE_Z80 || !OPT_DISABLE_GBZ80
206 { 0, "--no-std-crt0", &options.no_std_crt0, "For the z80/gbz80 do not link default crt0.o"},
208 { 0, OPTION_SHORT_IS_8BITS, NULL, "Make short 8 bits (for old times sake)" },
210 { 0, NULL, NULL, "Optimization options"},
211 { 0, "--nooverlay", &options.noOverlay, "Disable overlaying leaf function auto variables" },
212 { 0, OPTION_NO_GCSE, NULL, "Disable the GCSE optimisation" },
213 { 0, OPTION_NO_LABEL_OPT, NULL, "Disable label optimisation" },
214 { 0, OPTION_NO_LOOP_INV, NULL, "Disable optimisation of invariants" },
215 { 0, OPTION_NO_LOOP_IND, NULL, "Disable loop variable induction" },
216 { 0, "--nojtbound", &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
217 { 0, "--noloopreverse", &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
218 { 0, "--no-peep", &options.nopeep, "Disable the peephole assembly file optimisation" },
219 { 0, "--no-reg-params", &options.noRegParams, "On some ports, disable passing some parameters in registers" },
220 { 0, "--peep-asm", &options.asmpeep, "Enable peephole optimization on inline assembly" },
221 { 0, OPTION_PEEP_FILE, NULL, "<file> use this extra peephole file" },
222 { 0, OPTION_OPT_CODE_SPEED, NULL, "Optimize for code speed rather than size" },
223 { 0, OPTION_OPT_CODE_SIZE, NULL, "Optimize for code size rather than speed" },
225 { 0, NULL, NULL, "Internal debugging options"},
226 { 0, "--dumpraw", &options.dump_raw, "Dump the internal structure after the initial parse" },
227 { 0, "--dumpgcse", &options.dump_gcse, NULL },
228 { 0, "--dumploop", &options.dump_loop, NULL },
229 { 0, "--dumpdeadcode", &options.dump_kill, NULL },
230 { 0, "--dumpliverange", &options.dump_range, NULL },
231 { 0, "--dumpregpack", &options.dump_pack, NULL },
232 { 0, "--dumpregassign", &options.dump_rassgn, NULL },
233 { 0, "--dumptree", &options.dump_tree, "dump front-end AST before generating iCode" },
234 { 0, OPTION_DUMP_ALL, NULL, "Dump the internal structure at all stages" },
235 { 0, OPTION_ICODE_IN_ASM, &options.iCodeInAsm, "include i-code as comments in the asm file"},
237 { 0, NULL, NULL, "Linker options" },
238 { 'l', NULL, NULL, "Include the given library in the link" },
239 { 'L', NULL, NULL, "Add the next field to the library search path" },
240 { 0, OPTION_LIB_PATH, NULL, "<path> use this path to search for libraries" },
241 { 0, OPTION_OUT_FMT_IHX, NULL, "Output in Intel hex format" },
242 { 0, "--out-fmt-s19", &options.out_fmt, "Output in S19 hex format" },
243 { 0, OPTION_XRAM_LOC, NULL, "<nnnn> External Ram start location" },
244 { 0, OPTION_XRAM_SIZE, NULL, "<nnnn> External Ram size" },
245 { 0, OPTION_IRAM_SIZE, NULL, "<nnnn> Internal Ram size" },
246 { 0, OPTION_XSTACK_LOC, NULL, "<nnnn> External Ram start location" },
247 { 0, OPTION_CODE_LOC, NULL, "<nnnn> Code Segment Location" },
248 { 0, OPTION_CODE_SIZE, NULL, "<nnnn> Code Segment size" },
249 { 0, OPTION_STACK_LOC, NULL, "<nnnn> Stack pointer initial value" },
250 { 0, OPTION_DATA_LOC, NULL, "<nnnn> Direct data start location" },
251 { 0, OPTION_IDATA_LOC, NULL, NULL },
252 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
253 { 0, OPTION_STACK_SIZE, NULL,"MCS51/DS390 - Tells the linker to allocate this space for stack"},
254 { 0, OPTION_PACK_IRAM, NULL,"MCS51/DS390 - Tells the linker to pack variables in internal ram (default)"},
255 { 0, OPTION_NO_PACK_IRAM, &options.no_pack_iram,"MCS51/DS390 - Tells the linker not to pack variables in internal ram"},
262 /** Table of all unsupported options and help text to display when one
266 /** shortOpt as in OPTIONS. */
268 /** longOpt as in OPTIONS. */
270 /** Message to display inside W_UNSUPPORTED_OPT when this option
275 static const UNSUPPORTEDOPT
276 unsupportedOptTable[] = {
277 { 'X', NULL, "use --xstack-loc instead" },
278 { 'x', NULL, "use --xstack instead" },
279 { 'i', NULL, "use --idata-loc instead" },
280 { 'r', NULL, "use --xdata-loc instead" },
281 { 's', NULL, "use --code-loc instead" },
282 { 'Y', NULL, "use -I instead" }
285 /** List of all default constant macros.
287 static const char *_baseValues[] = {
290 /* Path seperator character */
291 "sep", DIR_SEPARATOR_STRING,
295 static const char *_preCmd = "{cpp} -nostdinc -Wall -std=c99 -DSDCC=1 {cppextraopts} \"{fullsrcfilename}\" \"{cppoutfilename}\"";
299 static PORT *_ports[] =
301 #if !OPT_DISABLE_MCS51
304 #if !OPT_DISABLE_GBZ80
313 #if !OPT_DISABLE_DS390
316 #if !OPT_DISABLE_PIC16
322 #if !OPT_DISABLE_TININative
325 #if !OPT_DISABLE_XA51
328 #if !OPT_DISABLE_DS400
331 #if !OPT_DISABLE_HC08
336 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
338 /** Sets the port to the one given by the command line option.
339 @param The name minus the option (eg 'mcs51')
340 @return 0 on success.
343 _setPort (const char *name)
346 for (i = 0; i < NUM_PORTS; i++)
348 if (!strcmp (_ports[i]->target, name))
354 /* Error - didnt find */
355 werror (E_UNKNOWN_TARGET, name);
359 /* Override the default processor with the one specified
360 * on the command line */
362 _setProcessor (char *_processor)
364 port->processor = _processor;
365 fprintf(stderr,"Processor: %s\n",_processor);
369 _validatePorts (void)
372 for (i = 0; i < NUM_PORTS; i++)
374 if (_ports[i]->magic != PORT_MAGIC)
376 /* Uncomment this line to debug which port is causing the problem
377 * (the target name is close to the beginning of the port struct
378 * and probably can be accessed just fine). */
379 fprintf(stderr,"%s :",_ports[i]->target);
380 wassertl (0, "Port definition structure is incomplete");
385 /* search through the command line options for the port */
387 _findPort (int argc, char **argv)
393 if (!strncmp (*argv, "-m", 2))
395 _setPort (*argv + 2);
401 /* Use the first in the list */
405 /* search through the command line options for the processor */
407 _findProcessor (int argc, char **argv)
411 if (!strncmp (*argv, "-p", 2))
413 _setProcessor (*argv + 2);
419 /* no error if processor was not specified. */
422 /*-----------------------------------------------------------------*/
423 /* printVersionInfo - prints the version info */
424 /*-----------------------------------------------------------------*/
426 printVersionInfo (void)
432 for (i = 0; i < NUM_PORTS; i++)
433 fprintf (stderr, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
435 fprintf (stderr, " " SDCC_VERSION_STR
436 #ifdef SDCC_SUB_VERSION_STR
437 "/" SDCC_SUB_VERSION_STR
439 " #%s (" __DATE__ ")"
442 #elif defined __MINGW32__
444 #elif defined __DJGPP__
446 #elif defined(_MSC_VER)
448 #elif defined(__BORLANDC__)
453 , getBuildNumber() );
457 printOptions(const OPTION *optionsTable)
461 optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
462 || optionsTable[i].help != NULL;
465 if (!optionsTable[i].shortOpt && !optionsTable[i].longOpt
466 && optionsTable[i].help)
468 fprintf (stdout, "\n%s:\n", optionsTable[i].help);
472 fprintf(stdout, " %c%c %-20s %s\n",
473 optionsTable[i].shortOpt !=0 ? '-' : ' ',
474 optionsTable[i].shortOpt !=0 ? optionsTable[i].shortOpt : ' ',
475 optionsTable[i].longOpt != NULL ? optionsTable[i].longOpt : "",
476 optionsTable[i].help != NULL ? optionsTable[i].help : ""
482 /*-----------------------------------------------------------------*/
483 /* printUsage - prints command line syntax */
484 /*-----------------------------------------------------------------*/
491 "Usage : sdcc [options] filename\n"
495 printOptions(optionsTable);
497 for (i = 0; i < NUM_PORTS; i++)
499 if (_ports[i]->poptions != NULL)
501 fprintf (stdout, "\nSpecial options for the %s port:\n", _ports[i]->target);
502 printOptions (_ports[i]->poptions);
509 /*-----------------------------------------------------------------*/
510 /* setParseWithComma - separates string with comma to a set */
511 /*-----------------------------------------------------------------*/
513 setParseWithComma (set **dest, char *src)
518 /* skip the initial white spaces */
519 while (isspace(*src))
522 /* skip the trailing white spaces */
523 length = strlen(src);
524 while (length && isspace(src[length-1]))
525 src[--length] = '\0';
527 for (p = strtok(src, ","); p != NULL; p = strtok(NULL, ","))
528 addSet(dest, Safe_strdup(p));
531 /*-----------------------------------------------------------------*/
532 /* setDefaultOptions - sets the default options */
533 /*-----------------------------------------------------------------*/
535 setDefaultOptions (void)
537 /* first the options part */
538 options.stack_loc = 0; /* stack pointer initialised to 0 */
539 options.xstack_loc = 0; /* xternal stack starts at 0 */
540 options.code_loc = 0; /* code starts at 0 */
541 options.data_loc = 0; /* JCF: By default let the linker locate data */
542 options.xdata_loc = 0;
543 options.idata_loc = 0x80;
545 options.model = port->general.default_model;
546 options.nostdlib = 0;
547 options.nostdinc = 0;
549 options.shortis8bits = 0;
551 options.stack10bit=0;
553 /* now for the optimizations */
554 /* turn on the everything */
555 optimize.global_cse = 1;
560 optimize.loopInvariant = 1;
561 optimize.loopInduction = 1;
563 /* now for the ports */
564 port->setDefaultOptions ();
567 /*-----------------------------------------------------------------*/
568 /* processFile - determines the type of file from the extension */
569 /*-----------------------------------------------------------------*/
571 processFile (char *s)
575 /* get the file extension */
576 fext = s + strlen (s);
577 while ((fext != s) && *fext != '.')
580 /* now if no '.' then we don't know what the file type is
581 so give a warning and return */
584 werror (W_UNKNOWN_FEXT, s);
588 /* otherwise depending on the file type */
589 if (strcmp (fext, ".c") == 0 || strcmp (fext, ".C") == 0)
591 /* source file name : not if we already have a
595 werror (W_TOO_MANY_SRC, s);
599 /* the only source file */
601 if (!(srcFile = fopen (fullSrcFileName, "r")))
603 werror (E_FILE_OPEN_ERR, s);
607 /* copy the file name into the buffer */
608 strncpyz (buffer, s, sizeof(buffer));
610 /* get rid of the "."-extension */
612 /* is there a dot at all? */
613 if (strrchr (buffer, '.') &&
614 /* is the dot in the filename, not in the path? */
615 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
617 *strrchr (buffer, '.') = '\0';
620 /* get rid of any path information
621 for the module name; */
622 fext = buffer + strlen (buffer);
624 /* do this by going backwards till we
625 get '\' or ':' or start of buffer */
626 while (fext != buffer &&
627 *(fext - 1) != DIR_SEPARATOR_CHAR &&
633 /* do this by going backwards till we
634 get '/' or start of buffer */
635 while (fext != buffer &&
636 *(fext - 1) != DIR_SEPARATOR_CHAR)
641 moduleNameBase = Safe_strdup ( fext );
642 moduleName = Safe_strdup ( fext );
644 for (fext = moduleName; *fext; fext++)
645 if (!isalnum (*fext))
650 /* if the extention is type .rel or .r or .REL or .R
651 addtional object file will be passed to the linker */
652 if (strcmp (fext, ".r") == 0 || strcmp (fext, ".rel") == 0 ||
653 strcmp (fext, ".R") == 0 || strcmp (fext, ".REL") == 0 ||
654 strcmp (fext, port->linker.rel_ext) == 0)
656 addSet(&relFilesSet, Safe_strdup(s));
660 /* if .lib or .LIB */
661 if (strcmp (fext, ".lib") == 0 || strcmp (fext, ".LIB") == 0)
663 addSet(&libFilesSet, Safe_strdup(s));
667 werror (W_UNKNOWN_FEXT, s);
672 _setModel (int model, const char *sz)
674 if (port->general.supported_models & model)
675 options.model = model;
677 werror (W_UNSUPPORTED_MODEL, sz, port->target);
680 /** Gets the string argument to this option. If the option is '--opt'
681 then for input of '--optxyz' or '--opt xyz' returns xyz.
684 getStringArg(const char *szStart, char **argv, int *pi, int argc)
686 if (argv[*pi][strlen(szStart)])
688 return &argv[*pi][strlen(szStart)];
695 werror (E_ARGUMENT_MISSING, szStart);
696 /* Die here rather than checking for errors later. */
706 /** Gets the integer argument to this option using the same rules as
710 getIntArg(const char *szStart, char **argv, int *pi, int argc)
712 return (int)floatFromVal(constVal(getStringArg(szStart, argv, pi, argc)));
716 verifyShortOption(const char *opt)
718 if (strlen(opt) != 2)
720 werror (W_EXCESS_SHORT_OPTIONS, opt);
725 tryHandleUnsupportedOpt(char **argv, int *pi)
727 if (argv[*pi][0] == '-')
729 const char *longOpt = "";
733 if (argv[*pi][1] == '-')
740 shortOpt = argv[*pi][1];
742 for (i = 0; i < LENGTH(unsupportedOptTable); i++)
744 if (unsupportedOptTable[i].shortOpt == shortOpt ||
745 (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt))) {
746 /* Found an unsupported opt. */
748 SNPRINTF(buffer, sizeof(buffer),
750 longOpt ? longOpt : "",
751 shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
752 werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
756 /* Didn't find in the table */
761 /* Not an option, so can't be unsupported :) */
767 scanOptionsTable(const OPTION *optionsTable, char shortOpt, const char *longOpt, char **argv, int *pi)
771 optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL
772 || optionsTable[i].help != NULL;
775 if (optionsTable[i].shortOpt == shortOpt ||
776 (longOpt && optionsTable[i].longOpt &&
777 strcmp(optionsTable[i].longOpt, longOpt) == 0))
780 /* If it is a flag then we can handle it here */
781 if (optionsTable[i].pparameter != NULL)
783 if (optionsTable[i].shortOpt == shortOpt)
785 verifyShortOption(argv[*pi]);
788 (*optionsTable[i].pparameter)++;
792 /* Not a flag. Handled manually later. */
797 /* Didn't find in the table */
802 tryHandleSimpleOpt(char **argv, int *pi)
804 if (argv[*pi][0] == '-')
806 const char *longOpt = "";
809 if (argv[*pi][1] == '-')
816 shortOpt = argv[*pi][1];
819 if (scanOptionsTable(optionsTable, shortOpt, longOpt, argv, pi))
823 else if (port && port->poptions &&
824 scanOptionsTable(port->poptions, shortOpt, longOpt, argv, pi))
835 /* Not an option, so can't be handled. */
840 /*-----------------------------------------------------------------*/
841 /* parseCmdLine - parses the command line and sets the options */
842 /*-----------------------------------------------------------------*/
844 parseCmdLine (int argc, char **argv)
848 /* go thru all whole command line */
849 for (i = 1; i < argc; i++)
854 if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
859 if (tryHandleSimpleOpt(argv, &i) == TRUE)
865 if (argv[i][0] == '-' && argv[i][1] == '-')
867 if (strcmp (argv[i], OPTION_HELP) == 0)
873 if (strcmp (argv[i], OPTION_STACK_8BIT) == 0)
875 options.stack10bit = 0;
879 if (strcmp (argv[i], OPTION_OUT_FMT_IHX) == 0)
885 if (strcmp (argv[i], OPTION_LARGE_MODEL) == 0)
887 _setModel (MODEL_LARGE, argv[i]);
891 if (strcmp (argv[i], OPTION_MEDIUM_MODEL) == 0)
893 _setModel (MODEL_MEDIUM, argv[i]);
897 if (strcmp (argv[i], OPTION_SMALL_MODEL) == 0)
899 _setModel (MODEL_SMALL, argv[i]);
903 if (strcmp (argv[i], OPTION_FLAT24_MODEL) == 0)
905 _setModel (MODEL_FLAT24, argv[i]);
909 if (strcmp (argv[i], OPTION_DUMP_ALL) == 0)
911 options.dump_rassgn =
917 options.dump_raw = 1;
921 if (strcmp (argv[i], OPTION_PEEP_FILE) == 0)
923 options.peep_file = getStringArg(OPTION_PEEP_FILE, argv, &i, argc);
927 if (strcmp (argv[i], OPTION_LIB_PATH) == 0)
929 addSet(&libPathsSet, Safe_strdup(getStringArg(OPTION_LIB_PATH, argv, &i, argc)));
933 if (strcmp (argv[i], OPTION_VERSION) == 0)
940 if (strcmp (argv[i], OPTION_CALLEE_SAVES) == 0)
942 setParseWithComma(&options.calleeSavesSet, getStringArg(OPTION_CALLEE_SAVES, argv, &i, argc));
946 if (strcmp (argv[i], OPTION_XSTACK_LOC) == 0)
948 options.xstack_loc = getIntArg(OPTION_XSTACK_LOC, argv, &i, argc);
952 if (strcmp (argv[i], OPTION_STACK_LOC) == 0)
954 options.stack_loc = getIntArg(OPTION_STACK_LOC, argv, &i, argc);
958 if (strcmp (argv[i], OPTION_STACK_SIZE) == 0)
960 options.stack_size = getIntArg(OPTION_STACK_SIZE, argv, &i, argc);
964 if (strcmp (argv[i], OPTION_XRAM_LOC) == 0)
966 options.xdata_loc = getIntArg(OPTION_XRAM_LOC, argv, &i, argc);
970 if (strcmp (argv[i], OPTION_IRAM_SIZE) == 0)
972 options.iram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
976 if (strcmp (argv[i], OPTION_XRAM_SIZE) == 0)
978 options.xram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
979 options.xram_size_set = TRUE;
983 if (strcmp (argv[i], OPTION_CODE_SIZE) == 0)
985 options.code_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
989 if (strcmp (argv[i], OPTION_DATA_LOC) == 0)
991 options.data_loc = getIntArg(OPTION_DATA_LOC, argv, &i, argc);
995 if (strcmp (argv[i], OPTION_IDATA_LOC) == 0)
997 options.idata_loc = getIntArg(OPTION_IDATA_LOC, argv, &i, argc);
1001 if (strcmp (argv[i], OPTION_CODE_LOC) == 0)
1003 options.code_loc = getIntArg(OPTION_CODE_LOC, argv, &i, argc);
1007 if (strcmp (argv[i], OPTION_NO_GCSE) == 0)
1009 optimize.global_cse = 0;
1013 if (strcmp (argv[i], OPTION_NO_LOOP_INV) == 0)
1015 optimize.loopInvariant = 0;
1019 if (strcmp (argv[i], OPTION_NO_LABEL_OPT) == 0)
1021 optimize.label4 = 0;
1025 if (strcmp (argv[i], OPTION_NO_LOOP_IND) == 0)
1027 optimize.loopInduction = 0;
1031 if (strcmp (argv[i], OPTION_OPT_CODE_SPEED) == 0)
1033 optimize.codeSpeed = 1;
1034 optimize.codeSize = 0;
1038 if (strcmp (argv[i], OPTION_OPT_CODE_SIZE) == 0)
1040 optimize.codeSpeed = 0;
1041 optimize.codeSize = 1;
1045 if (strcmp (argv[i], OPTION_LESS_PEDANTIC) == 0)
1047 options.lessPedantic = 1;
1048 setErrorLogLevel(ERROR_LEVEL_WARNING);
1052 if (strcmp (argv[i], OPTION_DISABLE_WARNING) == 0)
1054 int w = getIntArg(OPTION_DISABLE_WARNING, argv, &i, argc);
1055 if (w < MAX_ERROR_WARNING)
1057 setWarningDisabled(w);
1062 if (strcmp (&argv[i][1], OPTION_SHORT_IS_8BITS) == 0)
1064 options.shortis8bits=1;
1068 if (strcmp (argv[i], OPTION_TINI_LIBID) == 0)
1070 options.tini_libid = getIntArg(OPTION_TINI_LIBID, argv, &i, argc);
1074 if (!port->parseOption (&argc, argv, &i))
1076 werror (W_UNKNOWN_OPTION, argv[i]);
1085 /* if preceded by '-' then option */
1086 if (*argv[i] == '-')
1091 verifyShortOption(argv[i]);
1098 /* Used to select the port. But this has already been done. */
1102 /* Used to select the processor in port. But this has
1103 * already been done. */
1107 verifyShortOption(argv[i]);
1109 options.cc_only = 1;
1113 addSet(&libPathsSet, Safe_strdup(getStringArg("-L", argv, &i, argc)));
1117 addSet(&libFilesSet, Safe_strdup(getStringArg("-l", argv, &i, argc)));
1124 /* copy the file name into the buffer */
1125 strncpyz(buffer, getStringArg("-o", argv, &i, argc),
1127 /* point to last character */
1128 p = buffer + strlen (buffer) - 1;
1129 if (*p == DIR_SEPARATOR_CHAR)
1131 /* only output path specified */
1132 dstPath = Safe_strdup (buffer);
1133 fullDstFileName = NULL;
1137 fullDstFileName = Safe_strdup (buffer);
1139 /* get rid of the "."-extension */
1141 /* is there a dot at all? */
1142 if (strrchr (buffer, '.') &&
1143 /* is the dot in the filename, not in the path? */
1144 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
1145 *strrchr (buffer, '.') = '\0';
1147 dstFileName = Safe_strdup (buffer);
1149 /* strip module name to get path */
1150 p = strrchr (buffer, DIR_SEPARATOR_CHAR);
1153 /* path with trailing / */
1155 dstPath = Safe_strdup (buffer);
1162 /* pre-processer options */
1163 if (argv[i][2] == 'p')
1165 setParseWithComma(&preArgvSet, getStringArg("-Wp", argv, &i, argc));
1167 /* linker options */
1168 else if (argv[i][2] == 'l')
1170 setParseWithComma(&linkOptionsSet, getStringArg("-Wl", argv, &i, argc));
1172 /* assembler options */
1173 else if (argv[i][2] == 'a')
1175 setParseWithComma(&asmOptionsSet, getStringArg("-Wa", argv, &i, argc));
1179 werror (W_UNKNOWN_OPTION, argv[i]);
1184 verifyShortOption(argv[i]);
1186 printVersionInfo ();
1190 /* preprocessor options */
1194 if (argv[i][2] == 'M')
1195 addSet(&preArgvSet, Safe_strdup("-MM"));
1197 addSet(&preArgvSet, Safe_strdup("-M"));
1202 addSet(&preArgvSet, Safe_strdup("-C"));
1212 char sOpt = argv[i][1];
1215 if (argv[i][2] == ' ' || argv[i][2] == '\0')
1221 werror(E_ARGUMENT_MISSING, argv[i-1]);
1235 SNPRINTF (buffer, sizeof(buffer),
1236 ((sOpt == 'I') ? "-%c\"%s\"": "-%c%s"), sOpt, rest);
1237 addSet(&preArgvSet, Safe_strdup(buffer));
1239 addSet(&includeDirsSet, Safe_strdup(rest));
1240 addSet(&userIncDirsSet, Safe_strdup(rest));
1246 if (!port->parseOption (&argc, argv, &i))
1247 werror (W_UNKNOWN_OPTION, argv[i]);
1252 if (!port->parseOption (&argc, argv, &i))
1254 /* no option must be a filename */
1257 werror (W_NO_FILE_ARG_IN_C1, argv[i]);
1261 processFile (argv[i]);
1266 /* some sanity checks in c1 mode */
1271 if (fullSrcFileName)
1274 werror (W_NO_FILE_ARG_IN_C1, fullSrcFileName);
1276 fullSrcFileName = NULL;
1277 for (s = setFirstItem(relFilesSet); s != NULL; s = setNextItem(relFilesSet))
1279 werror (W_NO_FILE_ARG_IN_C1, s);
1281 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1283 werror (W_NO_FILE_ARG_IN_C1, s);
1285 deleteSet(&relFilesSet);
1286 deleteSet(&libFilesSet);
1288 if (options.cc_only || noAssemble || preProcOnly)
1290 werror (W_ILLEGAL_OPT_COMBINATION);
1292 options.cc_only = noAssemble = preProcOnly = 0;
1295 werror (E_NEED_OPT_O_IN_C1);
1299 /* if no dstFileName given with -o, we've to find one: */
1304 /* use the modulename from the C-source */
1305 if (fullSrcFileName)
1307 size_t bufSize = strlen (dstPath) + strlen (moduleNameBase) + 1;
1309 dstFileName = Safe_alloc (bufSize);
1310 strncpyz (dstFileName, dstPath, bufSize);
1311 strncatz (dstFileName, moduleNameBase, bufSize);
1313 /* use the modulename from the first object file */
1314 else if ((s = peekSet(relFilesSet)) != NULL)
1319 strncpyz (buffer, s, sizeof(buffer));
1320 /* remove extension (it must be .rel) */
1321 *strrchr (buffer, '.') = '\0';
1323 objectName = strrchr (buffer, DIR_SEPARATOR_CHAR);
1330 objectName = buffer;
1332 bufSize = strlen (dstPath) + strlen (objectName) + 1;
1333 dstFileName = Safe_alloc (bufSize);
1334 strncpyz (dstFileName, dstPath, bufSize);
1335 strncatz (dstFileName, objectName, bufSize);
1337 /* else no module given: help text is displayed */
1340 /* set int, long and float reentrancy based on stack-auto */
1341 if (options.stackAuto)
1343 options.intlong_rent++;
1344 options.float_rent++;
1347 /* set up external stack location if not explicitly specified */
1348 if (!options.xstack_loc)
1349 options.xstack_loc = options.xdata_loc;
1351 /* if debug option is set then open the cdbFile */
1352 if (options.debug && fullSrcFileName)
1354 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1355 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1356 if(debugFile->openFile(scratchFileName))
1357 debugFile->writeModule(moduleName);
1359 werror (E_FILE_OPEN_ERR, scratchFileName);
1361 MSVC_style(options.vc_err_style);
1362 if(options.use_stdout) dup2(STDOUT_FILENO, STDERR_FILENO);
1367 /*-----------------------------------------------------------------*/
1368 /* linkEdit : - calls the linkage editor with options */
1369 /*-----------------------------------------------------------------*/
1371 linkEdit (char **envp)
1379 if(port->linker.needLinkerScript)
1383 switch (options.out_fmt)
1386 out_fmt = 'i'; /* Intel hex */
1389 out_fmt = 's'; /* Motorola S19 */
1392 out_fmt = 't'; /* Elf */
1398 /* first we need to create the <filename>.lnk file */
1399 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1400 "%s.lnk", dstFileName);
1401 if (!(lnkfile = fopen (scratchFileName, "w")))
1403 werror (E_FILE_OPEN_ERR, scratchFileName);
1407 if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
1409 fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1410 out_fmt, dstFileName);
1412 else /*For all the other ports. Including pics???*/
1414 fprintf (lnkfile, "-myux%c\n", out_fmt);
1415 if(!options.no_pack_iram)
1416 fprintf (lnkfile, "-Y\n");
1419 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1421 /* if iram size specified */
1422 if (options.iram_size)
1423 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1425 /* if stack size specified*/
1426 if(options.stack_size)
1427 fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1429 /* if xram size specified */
1430 if (options.xram_size_set)
1431 fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1433 /* if code size specified */
1434 if (options.code_size)
1435 fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1438 fprintf (lnkfile, "-z\n");
1441 #define WRITE_SEG_LOC(N, L) \
1442 segName = Safe_strdup(N); \
1443 c = strtok(segName, " \t"); \
1444 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1445 if (segName) { Safe_free(segName); }
1447 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1450 /* code segment start */
1451 WRITE_SEG_LOC (CODE_NAME, options.code_loc);
1453 /* data segment start. If zero, the linker chooses
1454 the best place for data */
1455 if(options.data_loc)
1457 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1460 /* xdata segment start. If zero, the linker chooses
1461 the best place for xdata */
1462 if(options.xdata_loc)
1464 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1470 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1473 /* bit segment start */
1474 WRITE_SEG_LOC (BIT_NAME, 0);
1477 if ( (options.stack_loc) && (options.stack_loc<0x100) )
1479 WRITE_SEG_LOC ("SSEG", options.stack_loc);
1482 else /*For the z80, gbz80*/
1484 WRITE_SEG_LOC ("_CODE", options.code_loc);
1485 WRITE_SEG_LOC ("_DATA", options.data_loc);
1488 /* If the port has any special linker area declarations, get 'em */
1489 if (port->extraAreas.genExtraAreaLinkOptions)
1491 port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1494 /* add the extra linker options */
1495 fputStrSet(lnkfile, linkOptionsSet);
1497 /* command line defined library paths if specified */
1498 for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1499 fprintf (lnkfile, "-k %s\n", s);
1501 /* standard library path */
1502 if (!options.nostdlib)
1504 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1506 switch (options.model)
1516 if (TARGET_IS_DS390)
1520 else if (TARGET_IS_DS400)
1527 "Add support for your FLAT24 target in %s @ line %d\n",
1528 __FILE__, __LINE__);
1536 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1541 else /*for the z80, gbz80*/
1545 else if (TARGET_IS_Z80)
1550 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1551 mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1554 /* command line defined library files if specified */
1555 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1556 fprintf (lnkfile, "-l %s\n", s);
1558 /* standard library files */
1559 if (!options.nostdlib)
1561 #if !OPT_DISABLE_DS390
1562 if (options.model == MODEL_FLAT24)
1564 if (TARGET_IS_DS390)
1566 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1568 else if (TARGET_IS_DS400)
1570 fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1575 "Add support for your FLAT24 target in %s @ line %d\n",
1576 __FILE__, __LINE__);
1582 #if !OPT_DISABLE_XA51
1584 if (options.model == MODEL_PAGE0)
1586 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1590 if (TARGET_IS_MCS51)
1592 fprintf (lnkfile, "-l mcs51\n");
1594 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80
1595 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1596 { /*Why the z80 port is not using the standard libraries?*/
1597 fprintf (lnkfile, "-l %s\n", STD_LIB);
1598 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1599 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1600 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1602 else if (TARGET_IS_HC08)
1604 fprintf (lnkfile, "-l hc08\n");
1606 else if (TARGET_IS_Z80)
1608 fprintf (lnkfile, "-l z80\n");
1610 else if (TARGET_IS_GBZ80)
1612 fprintf (lnkfile, "-l gbz80\n");
1616 /*For the z80 and gbz80 ports, try to find where crt0.o is...
1617 It is very important for this file to be first on the linking proccess
1618 so the areas are set in the correct order, expecially _GSINIT*/
1619 if ((TARGET_IS_Z80 || TARGET_IS_GBZ80) &&
1620 !options.no_std_crt0) /*For the z80, gbz80*/
1622 char crt0path[PATH_MAX];
1626 tempSet = appendStrSet(libDirsSet, NULL, DIR_SEPARATOR_STRING);
1627 tempSet = appendStrSet(libDirsSet, NULL, c);
1628 mergeSets(&tempSet, libPathsSet);
1630 for (s = setFirstItem(tempSet); s != NULL; s = setNextItem(tempSet))
1632 sprintf (crt0path, "%s%scrt0.o",
1633 s, DIR_SEPARATOR_STRING);
1635 crt0fp=fopen(crt0path, "r");
1636 if(crt0fp!=NULL)/*Found it!*/
1641 /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1642 windows paths, so convert them to the CYGWIN format*/
1643 char posix_path[PATH_MAX];
1644 void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1645 cygwin_conv_to_full_posix_path(crt0path, posix_path);
1646 strcpy(crt0path, posix_path);
1649 fprintf (lnkfile, "%s\n", crt0path);
1653 if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1656 /* put in the object files */
1657 if (fullSrcFileName)
1658 fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1660 fputStrSet(lnkfile, relFilesSet);
1662 fprintf (lnkfile, "\n-e\n");
1664 } /* if(port->linker.needLinkerScript) */
1666 if (options.verbose)
1667 printf ("sdcc: Calling linker...\n");
1669 /* build linker output filename */
1671 /* -o option overrides default name? */
1672 if (fullDstFileName)
1674 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1678 /* the linked file gets the name of the first modul */
1679 if (fullSrcFileName)
1681 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1685 s = peekSet(relFilesSet);
1689 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1690 /* strip ".rel" extension */
1691 *strrchr (scratchFileName, '.') = '\0';
1693 strncatz (scratchFileName,
1694 options.out_fmt ? ".S19" : ".ihx",
1695 sizeof(scratchFileName));
1698 if (port->linker.cmd)
1700 char buffer2[PATH_MAX];
1701 char buffer3[PATH_MAX];
1702 set *tempSet=NULL, *libSet=NULL;
1704 strcpy(buffer3, dstFileName);
1705 if(TARGET_IS_PIC16) {
1707 /* use $l to set the linker include directories */
1708 tempSet = appendStrSet(libDirsSet, "-I\"", "\"");
1709 mergeSets(&linkOptionsSet, tempSet);
1711 tempSet = appendStrSet(libPathsSet, "-I\"", "\"");
1712 mergeSets(&linkOptionsSet, tempSet);
1714 /* use $3 for libraries from command line --> libSet */
1715 mergeSets(&libSet, libFilesSet);
1717 tempSet = appendStrSet(relFilesSet, "", "");
1718 mergeSets(&libSet, tempSet);
1719 // libSet = reverseSet(libSet);
1721 if(fullSrcFileName) {
1722 // strcpy(buffer3, strrchr(fullSrcFileName, DIR_SEPARATOR_CHAR)+1);
1723 /* if it didn't work, revert to old behaviour */
1724 if(!strlen(buffer3))strcpy(buffer3, dstFileName);
1725 strcat(buffer3, port->linker.rel_ext);
1727 } else strcpy(buffer3, "");
1730 buildCmdLine (buffer2, port->linker.cmd, buffer3, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1732 buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1736 buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1739 /* if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1741 system_ret = my_system (buffer);
1742 /* TODO: most linker don't have a -o parameter */
1743 /* -o option overrides default name? */
1744 if (fullDstFileName)
1747 /* the linked file gets the name of the first modul */
1748 if (fullSrcFileName)
1750 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1751 p = strlen (scratchFileName) + scratchFileName;
1755 s = peekSet(relFilesSet);
1759 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1760 /* strip ".rel" extension */
1761 p = strrchr (scratchFileName, '.');
1767 strncatz (scratchFileName,
1768 options.out_fmt ? ".S19" : ".ihx",
1769 sizeof(scratchFileName));
1770 if (strcmp (fullDstFileName, scratchFileName))
1771 unlink (fullDstFileName);
1772 rename (scratchFileName, fullDstFileName);
1774 strncpyz (buffer, fullDstFileName, sizeof(buffer));
1775 q = strrchr (buffer, '.');
1778 /* no extension: append new extensions */
1779 q = strlen (buffer) + buffer;
1783 strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1785 strncatz(buffer, ".map", sizeof(buffer));
1786 if (strcmp (scratchFileName, buffer))
1788 rename (scratchFileName, buffer);
1790 strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1792 strncatz(buffer, ".mem", sizeof(buffer));
1793 if (strcmp (scratchFileName, buffer))
1795 rename (scratchFileName, buffer);
1799 strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1801 strncatz(buffer, ".cdb", sizeof(buffer));
1802 if (strcmp (scratchFileName, buffer))
1804 rename (scratchFileName, buffer);
1805 /* and the OMF file without extension: */
1808 if (strcmp (scratchFileName, buffer))
1810 rename (scratchFileName, buffer);
1819 /*-----------------------------------------------------------------*/
1820 /* assemble - spawns the assembler with arguments */
1821 /*-----------------------------------------------------------------*/
1823 assemble (char **envp)
1825 /* build assembler output filename */
1827 /* -o option overrides default name? */
1828 if (options.cc_only && fullDstFileName) {
1829 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1831 /* the assembled file gets the name of the first modul */
1832 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1833 strncatz (scratchFileName, port->linker.rel_ext,
1834 sizeof(scratchFileName));
1837 if (port->assembler.do_assemble) {
1838 port->assembler.do_assemble(asmOptionsSet);
1840 } else if (port->assembler.cmd) {
1841 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1842 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1845 buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1848 if (my_system (buffer)) {
1849 /* either system() or the assembler itself has reported an error
1850 perror ("Cannot exec assembler");
1854 /* TODO: most assembler don't have a -o parameter */
1855 /* -o option overrides default name? */
1856 if (options.cc_only && fullDstFileName) {
1857 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1858 strncatz (scratchFileName,
1859 port->linker.rel_ext,
1860 sizeof(scratchFileName));
1861 if (strcmp (scratchFileName, fullDstFileName))
1862 unlink (fullDstFileName);
1863 rename (scratchFileName, fullDstFileName);
1867 /*-----------------------------------------------------------------*/
1868 /* preProcess - spawns the preprocessor with arguments */
1869 /*-----------------------------------------------------------------*/
1871 preProcess (char **envp)
1880 set *inclList = NULL;
1882 /* if using external stack define the macro */
1883 if (options.useXstack)
1884 addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
1886 /* set the macro for stack autos */
1887 if (options.stackAuto)
1888 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
1890 /* set the macro for stack autos */
1891 if (options.stack10bit)
1892 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
1894 /* set the macro for no overlay */
1895 if (options.noOverlay)
1896 addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
1898 /* set the macro for large model */
1899 switch (options.model)
1902 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
1905 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
1908 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
1911 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
1914 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
1917 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
1920 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1924 /* add port (processor information to processor */
1925 addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
1926 addSet(&preArgvSet, Safe_strdup("-D__{port}"));
1928 /* standard include path */
1929 if (!options.nostdinc) {
1930 inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
1931 mergeSets(&preArgvSet, inclList);
1934 setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
1935 Safe_free((void *)s);
1936 if (inclList != NULL)
1937 deleteSet(&inclList);
1939 if (preProcOnly && fullDstFileName)
1941 /* -E and -o given */
1942 setMainValue ("cppoutfilename", fullDstFileName);
1946 /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
1947 setMainValue ("cppoutfilename", NULL);
1950 if (options.verbose)
1951 printf ("sdcc: Calling preprocessor...\n");
1953 buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
1956 if (my_system (buffer)) {
1963 yyin = my_popen (buffer);
1965 perror ("Preproc file not found");
1968 addSetHead (&pipeSet, yyin);
1976 setBinPaths(const char *argv0)
1984 * 1. - $SDCCDIR/PREFIX2BIN_DIR
1985 * 2. - path(argv[0])
1989 /* do it in reverse mode, so that addSetHead() can be used
1990 instead of slower addSet() */
1992 if ((p = getBinPath(argv0)) != NULL)
1993 addSetHead(&binPathSet, Safe_strdup(p));
1995 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
1996 SNPRINTF(buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
1997 addSetHead(&binPathSet, Safe_strdup(buf));
2001 if (options.printSearchDirs) {
2002 printf("programs:\n");
2003 fputStrSet(stdout, binPathSet);
2008 /* Set system include path */
2010 setIncludePath(void)
2018 * 1. - $SDCC_INCLUDE/target
2019 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2020 * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX/target
2021 * 4. - DATADIR/INCLUDE_DIR_SUFFIX/target (only on *nix)
2022 * 5. - $SDCC_INCLUDE
2023 * 6. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
2024 * 7. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
2025 * 8. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
2028 if (options.nostdinc)
2031 tempSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
2032 includeDirsSet = appendStrSet(tempSet, NULL, DIR_SEPARATOR_STRING);
2033 includeDirsSet = appendStrSet(includeDirsSet, NULL, port->target);
2034 mergeSets(&includeDirsSet, tempSet);
2036 if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
2037 addSetHead(&includeDirsSet, p);
2040 if (options.printSearchDirs) {
2041 printf("includedir:\n");
2042 fputStrSet(stdout, includeDirsSet);
2047 /* Set system lib path */
2057 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
2058 * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
2059 * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
2062 if (options.nostdlib)
2065 libDirsSet = appendStrSet(dataDirsSet, NULL, LIB_DIR_SUFFIX);
2067 if ((p = getenv(SDCC_LIB_NAME)) != NULL)
2068 addSetHead(&libDirsSet, p);
2071 if (options.printSearchDirs) {
2072 printf("libdir:\n");
2073 fputStrSet(stdout, libDirsSet);
2080 setDataPaths(const char *argv0)
2088 * 1. - $SDCC_HOME/PREFIX2DATA_DIR
2089 * 2. - path(argv[0])/BIN2DATA_DIR
2090 * 3. - DATADIR (only on *nix)
2093 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2094 SNPRINTF(buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
2095 addSet(&dataDirsSet, Safe_strdup(buf));
2098 if ((p = getBinPath(argv0)) != NULL) {
2099 SNPRINTF(buf, sizeof buf, "%s" BIN2DATA_DIR, p);
2100 addSet(&dataDirsSet, Safe_strdup(buf));
2104 if (peekSet(dataDirsSet) == NULL) {
2105 /* this should never happen... */
2106 wassertl(0, "Can't get binary path");
2109 addSet(&dataDirsSet, Safe_strdup(DATADIR));
2113 if (options.printSearchDirs) {
2114 printf("datadir:\n");
2115 fputStrSet(stdout, dataDirsSet);
2126 populateMainValues (_baseValues);
2127 setMainValue ("port", port->target);
2128 setMainValue ("objext", port->linker.rel_ext);
2129 setMainValue ("asmext", port->assembler.file_ext);
2131 setMainValue ("dstfilename", dstFileName);
2132 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2134 if (options.cc_only && fullDstFileName)
2135 /* compile + assemble and -o given: -o specifies name of object file */
2137 setMainValue ("objdstfilename", fullDstFileName);
2141 setMainValue ("objdstfilename", "{stdobjdstfilename}");
2143 if (fullDstFileName)
2144 /* if we're linking, -o gives the final file name */
2146 setMainValue ("linkdstfilename", fullDstFileName);
2150 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2155 static void doPrintSearchDirs(void)
2157 printf("programs:\n");
2158 fputStrSet(stdout, binPathSet);
2160 printf("datadir:\n");
2161 fputStrSet(stdout, dataDirsSet);
2163 printf("includedir:\n");
2164 fputStrSet(stdout, includeDirsSet);
2166 printf("libdir:\n");
2167 fputStrSet(stdout, libDirsSet);
2168 fputStrSet(stdout, libPathsSet);
2173 sig_handler (int signal)
2180 sig_string = "SIGABRT";
2183 sig_string = "SIGTERM";
2186 sig_string = "SIGINT";
2189 sig_string = "SIGSEGV";
2192 sig_string = "Unknown?";
2195 fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2201 * initialises and calls the parser
2205 main (int argc, char **argv, char **envp)
2207 /* turn all optimizations off by default */
2208 memset (&optimize, 0, sizeof (struct optimize));
2210 /*printVersionInfo (); */
2213 fprintf (stderr, "Build error: no ports are enabled.\n");
2217 /* install atexit handler */
2218 atexit(rm_tmpfiles);
2220 /* install signal handler;
2221 it's only purpuse is to call exit() to remove temp files */
2222 if (!getenv("SDCC_LEAVE_SIGNALS"))
2224 signal (SIGABRT, sig_handler);
2225 signal (SIGTERM, sig_handler);
2226 signal (SIGINT , sig_handler);
2227 signal (SIGSEGV, sig_handler);
2230 /* Before parsing the command line options, do a
2231 * search for the port and processor and initialize
2232 * them if they're found. (We can't gurantee that these
2233 * will be the first options specified).
2236 _findPort (argc, argv);
2239 if (strcmp(port->target, "mcs51") == 0) {
2240 printf("DS390 jammed in A\n");
2246 _findProcessor (argc, argv);
2248 /* Initalise the port. */
2252 setDefaultOptions ();
2255 options.model = MODEL_SMALL;
2256 options.stack10bit=0;
2259 parseCmdLine (argc, argv);
2263 setBinPaths(argv[0]);
2264 setDataPaths(argv[0]);
2269 if(options.printSearchDirs)
2270 doPrintSearchDirs();
2272 /* if no input then printUsage & exit */
2273 if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL) {
2274 if (!options.printSearchDirs)
2280 /* initMem() is expensive, but
2281 initMem() must called before port->finaliseOptions ().
2282 And the z80 port needs port->finaliseOptions(),
2283 even if we're only linking. */
2285 port->finaliseOptions ();
2287 if (fullSrcFileName || options.c1mode)
2297 if (options.verbose)
2298 printf ("sdcc: Generating code...\n");
2304 deleteSetItem(&pipeSet, yyin);
2310 if (port->general.do_glue != NULL)
2311 (*port->general.do_glue)();
2314 /* this shouldn't happen */
2316 /* in case of NDEBUG */
2324 if (!options.c1mode && !noAssemble)
2326 if (options.verbose)
2327 printf ("sdcc: Calling assembler...\n");
2333 if (options.debug && debugFile)
2334 debugFile->closeFile();
2336 if (!options.cc_only &&
2340 (fullSrcFileName || peekSet(relFilesSet) != NULL))
2342 if (port->linker.do_link)
2343 port->linker.do_link ();