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_XSTACK_LOC "--xstack-loc"
107 #define OPTION_CALLEE_SAVES "--callee-saves"
108 #define OPTION_STACK_LOC "--stack-loc"
109 #define OPTION_XRAM_LOC "--xram-loc"
110 #define OPTION_IRAM_SIZE "--iram-size"
111 #define OPTION_VERSION "--version"
112 #define OPTION_DATA_LOC "--data-loc"
113 #define OPTION_CODE_LOC "--code-loc"
114 #define OPTION_IDATA_LOC "--idata-loc"
115 #define OPTION_NO_LABEL_OPT "--nolabelopt"
116 #define OPTION_NO_LOOP_INV "--noinvariant"
117 #define OPTION_NO_LOOP_IND "--noinduction"
118 #define OPTION_LESS_PEDANTIC "--less-pedantic"
119 #define OPTION_DISABLE_WARNING "--disable-warning"
120 #define OPTION_NO_GCSE "--nogcse"
121 #define OPTION_SHORT_IS_8BITS "--short-is-8bits"
122 #define OPTION_TINI_LIBID "--tini-libid"
123 #define OPTION_NO_XINIT_OPT "--no-xinit-opt"
124 #define OPTION_XRAM_SIZE "--xram-size"
125 #define OPTION_CODE_SIZE "--code-size"
126 #define OPTION_NO_CCODE_IN_ASM "--no-c-code-in-asm"
127 #define OPTION_ICODE_IN_ASM "--i-code-in-asm"
128 #define OPTION_PRINT_SEARCH_DIRS "--print-search-dirs"
129 #define OPTION_MSVC_ERROR_STYLE "--vc"
130 #define OPTION_USE_STDOUT "--use-stdout"
131 #define OPTION_STACK_SIZE "--stack-size"
132 #define OPTION_PACK_IRAM "--pack-iram"
133 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
134 #define OPTION_OPT_CODE_SPEED "--opt-code-speed"
135 #define OPTION_OPT_CODE_SIZE "--opt-code-size"
139 { 0, NULL, NULL, "General options" },
140 { 0, "--help", NULL, "Display this help" },
141 { 'v', OPTION_VERSION, NULL, "Display sdcc's version" },
142 { 0, "--verbose", &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
143 { 'V', NULL, &options.verboseExec, "Execute verbosely. Show sub commands as they are run" },
144 { 'd', NULL, NULL, NULL },
145 { 'D', NULL, NULL, "Define macro as in -Dmacro" },
146 { 'I', NULL, NULL, "Add to the include (*.h) path, as in -Ipath" },
147 { 'A', NULL, NULL, NULL },
148 { 'U', NULL, NULL, NULL },
149 { 'C', NULL, NULL, "Preprocessor option" },
150 { 'M', NULL, NULL, "Preprocessor option" },
151 { 'W', NULL, NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
152 { 'S', NULL, &noAssemble, "Compile only; do not assemble or link" },
153 { 'c', "--compile-only", &options.cc_only, "Compile and assemble, but do not link" },
154 { 'E', "--preprocessonly", &preProcOnly, "Preprocess only, do not compile" },
155 { 0, "--c1mode", &options.c1mode, "Act in c1 mode. The standard input is preprocessed code, the output is assembly code." },
156 { 'o', NULL, NULL, "Place the output into the given path resp. file" },
157 { 0, OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
158 { 0, OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
159 { 0, OPTION_USE_STDOUT, &options.use_stdout, "send errors to stdout instead of stderr"},
160 { 0, "--nostdlib", &options.nostdlib, "Do not include the standard library directory in the search path" },
161 { 0, "--nostdinc", &options.nostdinc, "Do not include the standard include directory in the search path" },
162 { 0, OPTION_LESS_PEDANTIC, NULL, "Disable some of the more pedantic warnings" },
163 { 0, OPTION_DISABLE_WARNING, NULL, "<nnnn> Disable specific warning" },
164 { 0, "--debug", &options.debug, "Enable debugging symbol output" },
165 { 0, "--cyclomatic", &options.cyclomatic, "Display complexity of compiled functions" },
167 { 0, NULL, NULL, "Code generation options"},
168 { 'm', NULL, NULL, "Set the port to use e.g. -mz80." },
169 { 'p', NULL, NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
170 { 0, OPTION_LARGE_MODEL, NULL, "external data space is used" },
171 { 0, OPTION_MEDIUM_MODEL, NULL, "not supported" },
172 { 0, OPTION_SMALL_MODEL, NULL, "internal data space is used (default)" },
173 #if !OPT_DISABLE_DS390
174 { 0, OPTION_FLAT24_MODEL, NULL, "use the flat24 model for the ds390 (default)" },
176 { 0, "--stack-auto", &options.stackAuto, "Stack automatic variables" },
177 #if !OPT_DISABLE_DS390
178 { 0, OPTION_STACK_8BIT, NULL, "use the 8bit stack for the ds390 (not supported yet)" },
179 { 0, "--stack-10bit", &options.stack10bit, "use the 10bit stack for ds390 (default)" },
181 { 0, "--xstack", &options.useXstack, "Use external stack" },
182 { 0, "--int-long-reent", &options.intlong_rent, "Use reenterant calls on the int and long support functions" },
183 { 0, "--float-reent", &options.float_rent, "Use reenterant calls on the float support functions" },
184 { 0, "--main-return", &options.mainreturn, "Issue a return after main()" },
185 { 0, "--xram-movc", &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
186 { 0, OPTION_CALLEE_SAVES, NULL, "<func[,func,...]> Cause the called function to save registers insted of the caller" },
187 { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" },
188 { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
189 { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" },
190 #if !OPT_DISABLE_DS390
191 { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"},
193 { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
194 #if !OPT_DISABLE_TININative
195 { 0, "--tini-libid", NULL,"<nnnn> LibraryID used in -mTININative"},
197 #if !OPT_DISABLE_DS390
198 { 0, "--protect-sp-update", &options.protect_sp_update,"DS390 - will disable interrupts during ESP:SP updates"},
200 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
201 { 0, "--parms-in-bank1", &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
203 { 0, OPTION_NO_XINIT_OPT, &options.noXinitOpt, "don't memcpy initialized xram from code"},
204 { 0, OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
205 { 0, OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
206 #if !OPT_DISABLE_Z80 || !OPT_DISABLE_GBZ80
207 { 0, "--no-std-crt0", &options.no_std_crt0, "For the z80/gbz80 do not link default crt0.o"},
209 { 0, OPTION_SHORT_IS_8BITS, NULL, "Make short 8 bits (for old times sake)" },
211 { 0, NULL, NULL, "Optimization options"},
212 { 0, "--nooverlay", &options.noOverlay, "Disable overlaying leaf function auto variables" },
213 { 0, OPTION_NO_GCSE, NULL, "Disable the GCSE optimisation" },
214 { 0, OPTION_NO_LABEL_OPT, NULL, "Disable label optimisation" },
215 { 0, OPTION_NO_LOOP_INV, NULL, "Disable optimisation of invariants" },
216 { 0, OPTION_NO_LOOP_IND, NULL, "Disable loop variable induction" },
217 { 0, "--nojtbound", &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
218 { 0, "--noloopreverse", &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
219 { 0, "--no-peep", &options.nopeep, "Disable the peephole assembly file optimisation" },
220 { 0, "--no-reg-params", &options.noRegParams, "On some ports, disable passing some parameters in registers" },
221 { 0, "--peep-asm", &options.asmpeep, "Enable peephole optimization on inline assembly" },
222 { 0, OPTION_PEEP_FILE, NULL, "<file> use this extra peephole file" },
223 { 0, OPTION_OPT_CODE_SPEED, NULL, "Optimize for code speed rather than size" },
224 { 0, OPTION_OPT_CODE_SIZE, NULL, "Optimize for code size rather than speed" },
226 { 0, NULL, NULL, "Internal debugging options"},
227 { 0, "--dumpraw", &options.dump_raw, "Dump the internal structure after the initial parse" },
228 { 0, "--dumpgcse", &options.dump_gcse, NULL },
229 { 0, "--dumploop", &options.dump_loop, NULL },
230 { 0, "--dumpdeadcode", &options.dump_kill, NULL },
231 { 0, "--dumpliverange", &options.dump_range, NULL },
232 { 0, "--dumpregpack", &options.dump_pack, NULL },
233 { 0, "--dumpregassign", &options.dump_rassgn, NULL },
234 { 0, "--dumptree", &options.dump_tree, "dump front-end AST before generating iCode" },
235 { 0, OPTION_DUMP_ALL, NULL, "Dump the internal structure at all stages" },
236 { 0, OPTION_ICODE_IN_ASM, &options.iCodeInAsm, "include i-code as comments in the asm file"},
238 { 0, NULL, NULL, "Linker options" },
239 { 'l', NULL, NULL, "Include the given library in the link" },
240 { 'L', NULL, NULL, "Add the next field to the library search path" },
241 { 0, OPTION_LIB_PATH, NULL, "<path> use this path to search for libraries" },
242 { 0, OPTION_OUT_FMT_IHX, NULL, "Output in Intel hex format" },
243 { 0, "--out-fmt-s19", &options.out_fmt, "Output in S19 hex format" },
244 { 0, OPTION_XRAM_LOC, NULL, "<nnnn> External Ram start location" },
245 { 0, OPTION_XRAM_SIZE, NULL, "<nnnn> External Ram size" },
246 { 0, OPTION_IRAM_SIZE, NULL, "<nnnn> Internal Ram size" },
247 { 0, OPTION_XSTACK_LOC, NULL, "<nnnn> External Ram start location" },
248 { 0, OPTION_CODE_LOC, NULL, "<nnnn> Code Segment Location" },
249 { 0, OPTION_CODE_SIZE, NULL, "<nnnn> Code Segment size" },
250 { 0, OPTION_STACK_LOC, NULL, "<nnnn> Stack pointer initial value" },
251 { 0, OPTION_DATA_LOC, NULL, "<nnnn> Direct data start location" },
252 { 0, OPTION_IDATA_LOC, NULL, NULL },
253 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
254 { 0, OPTION_STACK_SIZE, NULL,"MCS51/DS390 - Tells the linker to allocate this space for stack"},
255 { 0, OPTION_PACK_IRAM, &options.pack_iram,"MCS51/DS390 - Tells the linker 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 up external stack location if not explicitly specified */
1341 if (!options.xstack_loc)
1342 options.xstack_loc = options.xdata_loc;
1344 /* if debug option is set then open the cdbFile */
1345 if (options.debug && fullSrcFileName)
1347 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1348 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1349 if(debugFile->openFile(scratchFileName))
1350 debugFile->writeModule(moduleName);
1352 werror (E_FILE_OPEN_ERR, scratchFileName);
1354 MSVC_style(options.vc_err_style);
1355 if(options.use_stdout) dup2(STDOUT_FILENO, STDERR_FILENO);
1360 /*-----------------------------------------------------------------*/
1361 /* linkEdit : - calls the linkage editor with options */
1362 /*-----------------------------------------------------------------*/
1364 linkEdit (char **envp)
1372 if(port->linker.needLinkerScript)
1376 switch (options.out_fmt)
1379 out_fmt = 'i'; /* Intel hex */
1382 out_fmt = 's'; /* Motorola S19 */
1385 out_fmt = 't'; /* Elf */
1391 /* first we need to create the <filename>.lnk file */
1392 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1393 "%s.lnk", dstFileName);
1394 if (!(lnkfile = fopen (scratchFileName, "w")))
1396 werror (E_FILE_OPEN_ERR, scratchFileName);
1400 if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
1402 fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1403 out_fmt, dstFileName);
1405 else /*For all the other ports. Including pics???*/
1407 fprintf (lnkfile, "-myux%c\n", out_fmt);
1408 if(options.pack_iram)
1409 fprintf (lnkfile, "-Y\n");
1412 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1414 /* if iram size specified */
1415 if (options.iram_size)
1416 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1418 /* if stack size specified*/
1419 if(options.stack_size)
1420 fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1422 /* if xram size specified */
1423 if (options.xram_size_set)
1424 fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1426 /* if code size specified */
1427 if (options.code_size)
1428 fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1431 fprintf (lnkfile, "-z\n");
1434 #define WRITE_SEG_LOC(N, L) \
1435 segName = Safe_strdup(N); \
1436 c = strtok(segName, " \t"); \
1437 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1438 if (segName) { Safe_free(segName); }
1440 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1443 /* code segment start */
1444 WRITE_SEG_LOC (CODE_NAME, options.code_loc);
1446 /* data segment start. If zero, the linker chooses
1447 the best place for data */
1448 if(options.data_loc)
1450 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1454 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1459 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1462 /* bit segment start */
1463 WRITE_SEG_LOC (BIT_NAME, 0);
1466 if ( (options.stack_loc) && (options.stack_loc<0x100) )
1468 WRITE_SEG_LOC ("SSEG", options.stack_loc);
1471 else /*For the z80, gbz80*/
1473 WRITE_SEG_LOC ("_CODE", options.code_loc);
1474 WRITE_SEG_LOC ("_DATA", options.data_loc);
1477 /* If the port has any special linker area declarations, get 'em */
1478 if (port->extraAreas.genExtraAreaLinkOptions)
1480 port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1483 /* add the extra linker options */
1484 fputStrSet(lnkfile, linkOptionsSet);
1486 /* command line defined library paths if specified */
1487 for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1488 fprintf (lnkfile, "-k %s\n", s);
1490 /* standard library path */
1491 if (!options.nostdlib)
1493 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1495 switch (options.model)
1505 if (TARGET_IS_DS390)
1509 else if (TARGET_IS_DS400)
1516 "Add support for your FLAT24 target in %s @ line %d\n",
1517 __FILE__, __LINE__);
1525 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1530 else /*for the z80, gbz80*/
1534 else if (TARGET_IS_Z80)
1539 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1540 mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1543 /* command line defined library files if specified */
1544 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1545 fprintf (lnkfile, "-l %s\n", s);
1547 /* standard library files */
1548 if (!options.nostdlib)
1550 #if !OPT_DISABLE_DS390
1551 if (options.model == MODEL_FLAT24)
1553 if (TARGET_IS_DS390)
1555 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1557 else if (TARGET_IS_DS400)
1559 fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1564 "Add support for your FLAT24 target in %s @ line %d\n",
1565 __FILE__, __LINE__);
1571 #if !OPT_DISABLE_XA51
1573 if (options.model == MODEL_PAGE0)
1575 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1579 if (TARGET_IS_MCS51)
1581 fprintf (lnkfile, "-l mcs51\n");
1583 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80
1584 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1585 { /*Why the z80 port is not using the standard libraries?*/
1586 fprintf (lnkfile, "-l %s\n", STD_LIB);
1587 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1588 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1589 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1591 else if (TARGET_IS_HC08)
1593 fprintf (lnkfile, "-l hc08\n");
1595 else if (TARGET_IS_Z80)
1597 fprintf (lnkfile, "-l z80\n");
1599 else if (TARGET_IS_GBZ80)
1601 fprintf (lnkfile, "-l gbz80\n");
1605 /*For the z80 and gbz80 ports, try to find where crt0.o is...
1606 It is very important for this file to be first on the linking proccess
1607 so the areas are set in the correct order, expecially _GSINIT*/
1608 if ((TARGET_IS_Z80 || TARGET_IS_GBZ80) &&
1609 !options.no_std_crt0 && !options.nostdlib) /*For the z80, gbz80*/
1611 char crt0path[PATH_MAX];
1613 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1615 sprintf (crt0path, "%s%s%s%scrt0.o",
1616 s, DIR_SEPARATOR_STRING, c, DIR_SEPARATOR_STRING);
1618 crt0fp=fopen(crt0path, "r");
1619 if(crt0fp!=NULL)/*Found it!*/
1624 /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1625 windows paths, so convert them to the CYGWIN format*/
1626 char posix_path[PATH_MAX];
1627 void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1628 cygwin_conv_to_full_posix_path(crt0path, posix_path);
1629 strcpy(crt0path, posix_path);
1632 fprintf (lnkfile, "%s\n", crt0path);
1636 if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1639 /* put in the object files */
1640 if (fullSrcFileName)
1641 fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1643 fputStrSet(lnkfile, relFilesSet);
1645 fprintf (lnkfile, "\n-e\n");
1647 } /* if(port->linker.needLinkerScript) */
1649 if (options.verbose)
1650 printf ("sdcc: Calling linker...\n");
1652 /* build linker output filename */
1654 /* -o option overrides default name? */
1655 if (fullDstFileName)
1657 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1661 /* the linked file gets the name of the first modul */
1662 if (fullSrcFileName)
1664 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1668 s = peekSet(relFilesSet);
1672 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1673 /* strip ".rel" extension */
1674 *strrchr (scratchFileName, '.') = '\0';
1676 strncatz (scratchFileName,
1677 options.out_fmt ? ".S19" : ".ihx",
1678 sizeof(scratchFileName));
1681 if (port->linker.cmd)
1683 char buffer2[PATH_MAX];
1684 char buffer3[PATH_MAX];
1685 set *tempSet=NULL, *libSet=NULL;
1687 strcpy(buffer3, dstFileName);
1688 if(TARGET_IS_PIC16) {
1690 /* use $l to set the linker include directories */
1691 tempSet = appendStrSet(libDirsSet, "-I\"", "\"");
1692 mergeSets(&linkOptionsSet, tempSet);
1694 tempSet = appendStrSet(libPathsSet, "-I\"", "\"");
1695 mergeSets(&linkOptionsSet, tempSet);
1697 /* use $3 for libraries from command line --> libSet */
1698 mergeSets(&libSet, libFilesSet);
1700 tempSet = appendStrSet(relFilesSet, "", "");
1701 mergeSets(&libSet, tempSet);
1702 // libSet = reverseSet(libSet);
1704 if(fullSrcFileName) {
1705 // strcpy(buffer3, strrchr(fullSrcFileName, DIR_SEPARATOR_CHAR)+1);
1706 /* if it didn't work, revert to old behaviour */
1707 if(!strlen(buffer3))strcpy(buffer3, dstFileName);
1708 strcat(buffer3, port->linker.rel_ext);
1710 } else strcpy(buffer3, "");
1713 buildCmdLine (buffer2, port->linker.cmd, buffer3, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1715 buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1719 buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1722 /* if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1724 system_ret = my_system (buffer);
1725 /* TODO: most linker don't have a -o parameter */
1726 /* -o option overrides default name? */
1727 if (fullDstFileName)
1730 /* the linked file gets the name of the first modul */
1731 if (fullSrcFileName)
1733 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1734 p = strlen (scratchFileName) + scratchFileName;
1738 s = peekSet(relFilesSet);
1742 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1743 /* strip ".rel" extension */
1744 p = strrchr (scratchFileName, '.');
1750 strncatz (scratchFileName,
1751 options.out_fmt ? ".S19" : ".ihx",
1752 sizeof(scratchFileName));
1753 if (strcmp (fullDstFileName, scratchFileName))
1754 unlink (fullDstFileName);
1755 rename (scratchFileName, fullDstFileName);
1757 strncpyz (buffer, fullDstFileName, sizeof(buffer));
1758 q = strrchr (buffer, '.');
1761 /* no extension: append new extensions */
1762 q = strlen (buffer) + buffer;
1766 strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1768 strncatz(buffer, ".map", sizeof(buffer));
1769 if (strcmp (scratchFileName, buffer))
1771 rename (scratchFileName, buffer);
1773 strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1775 strncatz(buffer, ".mem", sizeof(buffer));
1776 if (strcmp (scratchFileName, buffer))
1778 rename (scratchFileName, buffer);
1782 strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1784 strncatz(buffer, ".cdb", sizeof(buffer));
1785 if (strcmp (scratchFileName, buffer))
1787 rename (scratchFileName, buffer);
1788 /* and the OMF file without extension: */
1791 if (strcmp (scratchFileName, buffer))
1793 rename (scratchFileName, buffer);
1802 /*-----------------------------------------------------------------*/
1803 /* assemble - spawns the assembler with arguments */
1804 /*-----------------------------------------------------------------*/
1806 assemble (char **envp)
1808 /* build assembler output filename */
1810 /* -o option overrides default name? */
1811 if (options.cc_only && fullDstFileName) {
1812 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1814 /* the assembled file gets the name of the first modul */
1815 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1816 strncatz (scratchFileName, port->linker.rel_ext,
1817 sizeof(scratchFileName));
1820 if (port->assembler.do_assemble) {
1821 port->assembler.do_assemble(asmOptionsSet);
1823 } else if (port->assembler.cmd) {
1824 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1825 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1828 buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1831 if (my_system (buffer)) {
1832 /* either system() or the assembler itself has reported an error
1833 perror ("Cannot exec assembler");
1837 /* TODO: most assembler don't have a -o parameter */
1838 /* -o option overrides default name? */
1839 if (options.cc_only && fullDstFileName) {
1840 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1841 strncatz (scratchFileName,
1842 port->linker.rel_ext,
1843 sizeof(scratchFileName));
1844 if (strcmp (scratchFileName, fullDstFileName))
1845 unlink (fullDstFileName);
1846 rename (scratchFileName, fullDstFileName);
1850 /*-----------------------------------------------------------------*/
1851 /* preProcess - spawns the preprocessor with arguments */
1852 /*-----------------------------------------------------------------*/
1854 preProcess (char **envp)
1863 set *inclList = NULL;
1865 /* if using external stack define the macro */
1866 if (options.useXstack)
1867 addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
1869 /* set the macro for stack autos */
1870 if (options.stackAuto)
1871 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
1873 /* set the macro for stack autos */
1874 if (options.stack10bit)
1875 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
1877 /* set the macro for no overlay */
1878 if (options.noOverlay)
1879 addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
1881 /* set the macro for large model */
1882 switch (options.model)
1885 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
1888 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
1891 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
1894 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
1897 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
1900 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
1903 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1907 /* add port (processor information to processor */
1908 addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
1909 addSet(&preArgvSet, Safe_strdup("-D__{port}"));
1911 /* standard include path */
1912 if (!options.nostdinc) {
1913 inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
1914 mergeSets(&preArgvSet, inclList);
1917 setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
1918 Safe_free((void *)s);
1919 if (inclList != NULL)
1920 deleteSet(&inclList);
1922 if (preProcOnly && fullDstFileName)
1924 /* -E and -o given */
1925 setMainValue ("cppoutfilename", fullDstFileName);
1929 /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
1930 setMainValue ("cppoutfilename", NULL);
1933 if (options.verbose)
1934 printf ("sdcc: Calling preprocessor...\n");
1936 buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
1939 if (my_system (buffer)) {
1946 yyin = my_popen (buffer);
1948 perror ("Preproc file not found");
1951 addSetHead (&pipeSet, yyin);
1959 setBinPaths(const char *argv0)
1967 * 1. - $SDCCDIR/PREFIX2BIN_DIR
1968 * 2. - path(argv[0])
1972 /* do it in reverse mode, so that addSetHead() can be used
1973 instead of slower addSet() */
1975 if ((p = getBinPath(argv0)) != NULL)
1976 addSetHead(&binPathSet, Safe_strdup(p));
1978 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
1979 SNPRINTF(buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
1980 addSetHead(&binPathSet, Safe_strdup(buf));
1984 if (options.printSearchDirs) {
1985 printf("programs:\n");
1986 fputStrSet(stdout, binPathSet);
1991 /* Set system include path */
1993 setIncludePath(void)
2000 * 1. - $SDCC_INCLUDE
2001 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
2002 * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
2003 * 4. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
2006 includeDirsSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
2008 if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
2009 addSetHead(&includeDirsSet, p);
2012 if (options.printSearchDirs) {
2013 printf("includedir:\n");
2014 fputStrSet(stdout, includeDirsSet);
2019 /* Set system lib path */
2029 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
2030 * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
2031 * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
2034 libDirsSet = appendStrSet(dataDirsSet, NULL, LIB_DIR_SUFFIX);
2036 if ((p = getenv(SDCC_LIB_NAME)) != NULL)
2037 addSetHead(&libDirsSet, p);
2040 if (options.printSearchDirs) {
2041 printf("libdir:\n");
2042 fputStrSet(stdout, libDirsSet);
2049 setDataPaths(const char *argv0)
2057 * 1. - $SDCC_HOME/PREFIX2DATA_DIR
2058 * 2. - path(argv[0])/BIN2DATA_DIR
2059 * 3. - DATADIR (only on *nix)
2062 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
2063 SNPRINTF(buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
2064 addSet(&dataDirsSet, Safe_strdup(buf));
2067 if ((p = getBinPath(argv0)) != NULL) {
2068 SNPRINTF(buf, sizeof buf, "%s" BIN2DATA_DIR, p);
2069 addSet(&dataDirsSet, Safe_strdup(buf));
2073 if (peekSet(dataDirsSet) == NULL) {
2074 /* this should never happen... */
2075 wassertl(0, "Can't get binary path");
2078 addSet(&dataDirsSet, Safe_strdup(DATADIR));
2082 if (options.printSearchDirs) {
2083 printf("datadir:\n");
2084 fputStrSet(stdout, dataDirsSet);
2095 populateMainValues (_baseValues);
2096 setMainValue ("port", port->target);
2097 setMainValue ("objext", port->linker.rel_ext);
2098 setMainValue ("asmext", port->assembler.file_ext);
2100 setMainValue ("dstfilename", dstFileName);
2101 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2103 if (options.cc_only && fullDstFileName)
2104 /* compile + assemble and -o given: -o specifies name of object file */
2106 setMainValue ("objdstfilename", fullDstFileName);
2110 setMainValue ("objdstfilename", "{stdobjdstfilename}");
2112 if (fullDstFileName)
2113 /* if we're linking, -o gives the final file name */
2115 setMainValue ("linkdstfilename", fullDstFileName);
2119 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2124 static void doPrintSearchDirs(void)
2126 printf("programs:\n");
2127 fputStrSet(stdout, binPathSet);
2129 printf("datadir:\n");
2130 fputStrSet(stdout, dataDirsSet);
2132 printf("includedir:\n");
2133 fputStrSet(stdout, includeDirsSet);
2135 printf("libdir:\n");
2136 fputStrSet(stdout, libDirsSet);
2137 fputStrSet(stdout, libPathsSet);
2142 sig_handler (int signal)
2149 sig_string = "SIGABRT";
2152 sig_string = "SIGTERM";
2155 sig_string = "SIGINT";
2158 sig_string = "SIGSEGV";
2161 sig_string = "Unknown?";
2164 fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2170 * initialises and calls the parser
2174 main (int argc, char **argv, char **envp)
2176 /* turn all optimizations off by default */
2177 memset (&optimize, 0, sizeof (struct optimize));
2179 /*printVersionInfo (); */
2182 fprintf (stderr, "Build error: no ports are enabled.\n");
2186 /* install atexit handler */
2187 atexit(rm_tmpfiles);
2189 /* install signal handler;
2190 it's only purpuse is to call exit() to remove temp files */
2191 if (!getenv("SDCC_LEAVE_SIGNALS"))
2193 signal (SIGABRT, sig_handler);
2194 signal (SIGTERM, sig_handler);
2195 signal (SIGINT , sig_handler);
2196 signal (SIGSEGV, sig_handler);
2199 /* Before parsing the command line options, do a
2200 * search for the port and processor and initialize
2201 * them if they're found. (We can't gurantee that these
2202 * will be the first options specified).
2205 _findPort (argc, argv);
2208 if (strcmp(port->target, "mcs51") == 0) {
2209 printf("DS390 jammed in A\n");
2215 _findProcessor (argc, argv);
2217 /* Initalise the port. */
2221 setDefaultOptions ();
2224 options.model = MODEL_SMALL;
2225 options.stack10bit=0;
2228 parseCmdLine (argc, argv);
2232 setBinPaths(argv[0]);
2233 setDataPaths(argv[0]);
2238 if(options.printSearchDirs)
2239 doPrintSearchDirs();
2241 /* if no input then printUsage & exit */
2242 if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL) {
2243 if (!options.printSearchDirs)
2249 /* initMem() is expensive, but
2250 initMem() must called before port->finaliseOptions ().
2251 And the z80 port needs port->finaliseOptions(),
2252 even if we're only linking. */
2254 port->finaliseOptions ();
2256 if (fullSrcFileName || options.c1mode)
2266 if (options.verbose)
2267 printf ("sdcc: Generating code...\n");
2273 deleteSetItem(&pipeSet, yyin);
2279 if (port->general.do_glue != NULL)
2280 (*port->general.do_glue)();
2283 /* this shouldn't happen */
2285 /* in case of NDEBUG */
2293 if (!options.c1mode && !noAssemble)
2295 if (options.verbose)
2296 printf ("sdcc: Calling assembler...\n");
2302 if (options.debug && debugFile)
2303 debugFile->closeFile();
2305 if (!options.cc_only &&
2309 (fullSrcFileName || peekSet(relFilesSet) != NULL))
2311 if (port->linker.do_link)
2312 port->linker.do_link ();