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 *libDirsSet = NULL; /* list of lib search directories */
81 /* uncomment JAMIN_DS390 to always override and use ds390 port
82 for mcs51 work. This is temporary, for compatibility testing. */
83 /* #define JAMIN_DS390 */
88 /* Globally accessible scratch buffer for file names. */
89 char scratchFileName[PATH_MAX];
90 char buffer[PATH_MAX * 2];
92 #define OPTION_HELP "-help"
94 #define LENGTH(_a) (sizeof(_a)/sizeof(*(_a)))
96 #define OPTION_STACK_8BIT "--stack-8bit"
97 #define OPTION_OUT_FMT_IHX "--out-fmt-ihx"
98 #define OPTION_LARGE_MODEL "--model-large"
99 #define OPTION_MEDIUM_MODEL "--model-medium"
100 #define OPTION_SMALL_MODEL "--model-small"
101 #define OPTION_FLAT24_MODEL "--model-flat24"
102 #define OPTION_DUMP_ALL "--dumpall"
103 #define OPTION_PEEP_FILE "--peep-file"
104 #define OPTION_LIB_PATH "--lib-path"
105 #define OPTION_XSTACK_LOC "--xstack-loc"
106 #define OPTION_CALLEE_SAVES "--callee-saves"
107 #define OPTION_STACK_LOC "--stack-loc"
108 #define OPTION_XRAM_LOC "--xram-loc"
109 #define OPTION_IRAM_SIZE "--iram-size"
110 #define OPTION_VERSION "--version"
111 #define OPTION_DATA_LOC "--data-loc"
112 #define OPTION_CODE_LOC "--code-loc"
113 #define OPTION_IDATA_LOC "--idata-loc"
114 #define OPTION_NO_LABEL_OPT "--nolabelopt"
115 #define OPTION_NO_LOOP_INV "--noinvariant"
116 #define OPTION_NO_LOOP_IND "--noinduction"
117 #define OPTION_LESS_PEDANTIC "--less-pedantic"
118 #define OPTION_NO_GCSE "--nogcse"
119 #define OPTION_SHORT_IS_8BITS "--short-is-8bits"
120 #define OPTION_TINI_LIBID "--tini-libid"
121 #define OPTION_NO_XINIT_OPT "--no-xinit-opt"
122 #define OPTION_XRAM_SIZE "--xram-size"
123 #define OPTION_CODE_SIZE "--code-size"
124 #define OPTION_NO_CCODE_IN_ASM "--no-c-code-in-asm"
125 #define OPTION_ICODE_IN_ASM "--i-code-in-asm"
126 #define OPTION_PRINT_SEARCH_DIRS "--print-search-dirs"
127 #define OPTION_MSVC_ERROR_STYLE "--vc"
128 #define OPTION_USE_STDOUT "--use-stdout"
129 #define OPTION_STACK_SIZE "--stack-size"
130 #define OPTION_PACK_IRAM "--pack-iram"
131 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
135 { 'm', NULL, NULL, "Set the port to use e.g. -mz80." },
136 { 'p', NULL, NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
137 { 'd', NULL, NULL, NULL },
138 { 'D', NULL, NULL, "Define macro as in -Dmacro" },
139 { 'I', NULL, NULL, "Add to the include (*.h) path, as in -Ipath" },
140 { 'A', NULL, NULL, NULL },
141 { 'U', NULL, NULL, NULL },
142 { 'C', NULL, NULL, "Preprocessor option" },
143 { 'M', NULL, NULL, "Preprocessor option" },
144 { 'V', NULL, &options.verboseExec, "Execute verbosely. Show sub commands as they are run" },
145 { 'S', NULL, &noAssemble, "Compile only; do not assemble or link" },
146 { 'W', NULL, NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
147 { 'L', NULL, NULL, "Add the next field to the library search path" },
148 { 'l', NULL, NULL, "Include the given library in the link" },
149 { 0, OPTION_LARGE_MODEL, NULL, "external data space is used" },
150 { 0, OPTION_MEDIUM_MODEL, NULL, "not supported" },
151 { 0, OPTION_SMALL_MODEL, NULL, "internal data space is used (default)" },
152 #if !OPT_DISABLE_DS390
153 { 0, OPTION_FLAT24_MODEL, NULL, "use the flat24 model for the ds390 (default)" },
155 { 0, "--stack-auto", &options.stackAuto, "Stack automatic variables" },
156 #if !OPT_DISABLE_DS390
157 { 0, OPTION_STACK_8BIT, NULL, "use the 8bit stack for the ds390 (not supported yet)" },
158 { 0, "--stack-10bit", &options.stack10bit, "use the 10bit stack for ds390 (default)" },
160 { 0, "--xstack", &options.useXstack, "Use external stack" },
161 { 0, OPTION_NO_GCSE, NULL, "Disable the GCSE optimisation" },
162 { 0, OPTION_NO_LABEL_OPT, NULL, "Disable label optimisation" },
163 { 0, OPTION_NO_LOOP_INV, NULL, "Disable optimisation of invariants" },
164 { 0, OPTION_NO_LOOP_IND, NULL, NULL },
165 { 0, "--nojtbound", &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
166 { 0, "--noloopreverse", &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
167 { 'c', "--compile-only", &options.cc_only, "Compile and assemble, but do not link" },
168 { 'o', NULL, NULL, "Place the output into the given path resp. file" },
169 { 0, "--dumpraw", &options.dump_raw, "Dump the internal structure after the initial parse" },
170 { 0, "--dumpgcse", &options.dump_gcse, NULL },
171 { 0, "--dumploop", &options.dump_loop, NULL },
172 { 0, "--dumpdeadcode", &options.dump_kill, NULL },
173 { 0, "--dumpliverange", &options.dump_range, NULL },
174 { 0, "--dumpregpack", &options.dump_pack, NULL },
175 { 0, "--dumpregassign", &options.dump_rassgn, NULL },
176 { 0, "--dumptree", &options.dump_tree, "dump front-end AST before generating iCode" },
177 { 0, OPTION_DUMP_ALL, NULL, "Dump the internal structure at all stages" },
178 { 0, OPTION_XRAM_LOC, NULL, "<nnnn> External Ram start location" },
179 { 0, OPTION_XRAM_SIZE, NULL, "<nnnn> External Ram size" },
180 { 0, OPTION_IRAM_SIZE, NULL, "<nnnn> Internal Ram size" },
181 { 0, OPTION_XSTACK_LOC, NULL, "<nnnn> External Ram start location" },
182 { 0, OPTION_CODE_LOC, NULL, "<nnnn> Code Segment Location" },
183 { 0, OPTION_CODE_SIZE, NULL, "<nnnn> Code Segment size" },
184 { 0, OPTION_STACK_LOC, NULL, "<nnnn> Stack pointer initial value" },
185 { 0, OPTION_DATA_LOC, NULL, "<nnnn> Direct data start location" },
186 { 0, OPTION_IDATA_LOC, NULL, NULL },
187 { 0, OPTION_PEEP_FILE, NULL, "<file> use this extra peep-hole file" },
188 { 0, OPTION_LIB_PATH, NULL, "<path> use this path to search for libraries" },
189 { 0, "--int-long-reent", &options.intlong_rent, "Use reenterant calls on the int and long support functions" },
190 { 0, "--float-reent", &options.float_rent, "Use reenterant calls on the float support functions" },
191 { 0, OPTION_OUT_FMT_IHX, NULL, NULL },
192 { 0, "--out-fmt-s19", &options.out_fmt, NULL },
193 { 0, "--cyclomatic", &options.cyclomatic, NULL },
194 { 0, "--nooverlay", &options.noOverlay, NULL },
195 { 0, "--main-return", &options.mainreturn, "Issue a return after main()" },
196 { 0, "--xram-movc", &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
197 { 0, "--no-peep", &options.nopeep, "Disable the peephole assembly file optimisation" },
198 { 0, "--no-reg-params", &options.noRegParams, "On some ports, disable passing some parameters in registers" },
199 { 0, "--peep-asm", &options.asmpeep, NULL },
200 { 0, "--debug", &options.debug, "Enable debugging symbol output" },
201 { 'v', OPTION_VERSION, NULL, "Display sdcc's version" },
202 { 'E', "--preprocessonly", &preProcOnly, "Preprocess only, do not compile" },
203 { 0, "--c1mode", &options.c1mode, "Act in c1 mode. The standard input is preprocessed code, the output is assembly code." },
204 { 0, "--help", NULL, "Display this help" },
205 { 0, OPTION_CALLEE_SAVES, NULL, "<func[,func,...]> Cause the called function to save registers insted of the caller" },
206 { 0, "--nostdlib", &options.nostdlib, "Do not include the standard library directory in the search path" },
207 { 0, "--nostdinc", &options.nostdinc, "Do not include the standard include directory in the search path" },
208 { 0, "--verbose", &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
209 { 0, OPTION_LESS_PEDANTIC, NULL, "Disable some of the more pedantic warnings" },
210 { 0, OPTION_SHORT_IS_8BITS, NULL, "Make short 8bits (for old times sake)" },
211 { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" },
212 { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
213 { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" },
214 #if !OPT_DISABLE_DS390
215 { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"},
217 { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
218 #if !OPT_DISABLE_TININative
219 { 0, "--tini-libid", NULL,"<nnnn> LibraryID used in -mTININative"},
221 #if !OPT_DISABLE_DS390
222 { 0, "--protect-sp-update", &options.protect_sp_update,"DS390 - will disable interrupts during ESP:SP updates"},
224 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
225 { 0, "--parms-in-bank1", &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
226 { 0, OPTION_STACK_SIZE, NULL,"MCS51/DS390 - Tells the linker to allocate this space for stack"},
227 { 0, OPTION_PACK_IRAM, &options.pack_iram,"MCS51/DS390 - Tells the linker to pack variables in internal ram"},
229 { 0, OPTION_NO_XINIT_OPT, &options.noXinitOpt, "don't memcpy initialized xram from code"},
230 { 0, OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
231 { 0, OPTION_ICODE_IN_ASM, &options.iCodeInAsm, "include i-code as comments in the asm file"},
232 { 0, OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
233 { 0, OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
234 { 0, OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
235 { 0, OPTION_USE_STDOUT, &options.use_stdout, "send errors to stdout instead of stderr"},
236 #if !OPT_DISABLE_Z80 || !OPT_DISABLE_GBZ80
237 { 0, "--no-std-crt0", &options.no_std_crt0, "For the z80/gbz80 do not link default crt0.o"},
243 /** Table of all unsupported options and help text to display when one
247 /** shortOpt as in OPTIONS. */
249 /** longOpt as in OPTIONS. */
251 /** Message to display inside W_UNSUPPORTED_OPT when this option
256 static const UNSUPPORTEDOPT
257 unsupportedOptTable[] = {
258 { 'X', NULL, "use --xstack-loc instead" },
259 { 'x', NULL, "use --xstack instead" },
260 { 'i', NULL, "use --idata-loc instead" },
261 { 'r', NULL, "use --xdata-loc instead" },
262 { 's', NULL, "use --code-loc instead" },
263 { 'Y', NULL, "use -I instead" }
266 /** List of all default constant macros.
268 static const char *_baseValues[] = {
271 /* Path seperator character */
272 "sep", DIR_SEPARATOR_STRING,
276 static const char *_preCmd = "{cpp} -nostdinc -Wall -std=c99 -DSDCC=1 {cppextraopts} \"{fullsrcfilename}\" \"{cppoutfilename}\"";
280 static PORT *_ports[] =
282 #if !OPT_DISABLE_MCS51
285 #if !OPT_DISABLE_GBZ80
294 #if !OPT_DISABLE_DS390
297 #if !OPT_DISABLE_PIC16
303 #if !OPT_DISABLE_TININative
306 #if !OPT_DISABLE_XA51
309 #if !OPT_DISABLE_DS400
312 #if !OPT_DISABLE_HC08
317 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
319 /** Sets the port to the one given by the command line option.
320 @param The name minus the option (eg 'mcs51')
321 @return 0 on success.
324 _setPort (const char *name)
327 for (i = 0; i < NUM_PORTS; i++)
329 if (!strcmp (_ports[i]->target, name))
335 /* Error - didnt find */
336 werror (E_UNKNOWN_TARGET, name);
340 /* Override the default processor with the one specified
341 * on the command line */
343 _setProcessor (char *_processor)
345 port->processor = _processor;
346 fprintf(stderr,"Processor: %s\n",_processor);
350 _validatePorts (void)
353 for (i = 0; i < NUM_PORTS; i++)
355 if (_ports[i]->magic != PORT_MAGIC)
357 /* Uncomment this line to debug which port is causing the problem
358 * (the target name is close to the beginning of the port struct
359 * and probably can be accessed just fine). */
360 fprintf(stderr,"%s :",_ports[i]->target);
361 wassertl (0, "Port definition structure is incomplete");
366 /* search through the command line options for the port */
368 _findPort (int argc, char **argv)
374 if (!strncmp (*argv, "-m", 2))
376 _setPort (*argv + 2);
382 /* Use the first in the list */
386 /* search through the command line options for the processor */
388 _findProcessor (int argc, char **argv)
392 if (!strncmp (*argv, "-p", 2))
394 _setProcessor (*argv + 2);
400 /* no error if processor was not specified. */
403 /*-----------------------------------------------------------------*/
404 /* printVersionInfo - prints the version info */
405 /*-----------------------------------------------------------------*/
407 printVersionInfo (void)
413 for (i = 0; i < NUM_PORTS; i++)
414 fprintf (stderr, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
416 fprintf (stderr, " " SDCC_VERSION_STR
417 #ifdef SDCC_SUB_VERSION_STR
418 "/" SDCC_SUB_VERSION_STR
423 #elif defined __MINGW32__
425 #elif defined __DJGPP__
427 #elif defined(_MSC_VER)
429 #elif defined(__BORLANDC__)
438 printOptions(const OPTION *optionsTable)
441 for (i = 0; optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL; i++)
443 fprintf(stdout, " %c%c %-20s %s\n",
444 optionsTable[i].shortOpt !=0 ? '-' : ' ',
445 optionsTable[i].shortOpt !=0 ? optionsTable[i].shortOpt : ' ',
446 optionsTable[i].longOpt != NULL ? optionsTable[i].longOpt : "",
447 optionsTable[i].help != NULL ? optionsTable[i].help : ""
452 /*-----------------------------------------------------------------*/
453 /* printUsage - prints command line syntax */
454 /*-----------------------------------------------------------------*/
461 "Usage : sdcc [options] filename\n"
465 printOptions(optionsTable);
467 for (i = 0; i < NUM_PORTS; i++)
469 if (_ports[i]->poptions != NULL)
471 fprintf (stdout, "\nSpecial options for the %s port:\n", _ports[i]->target);
472 printOptions (_ports[i]->poptions);
479 /*-----------------------------------------------------------------*/
480 /* setParseWithComma - separates string with comma to a set */
481 /*-----------------------------------------------------------------*/
483 setParseWithComma (set **dest, char *src)
488 /* skip the initial white spaces */
489 while (isspace(*src))
492 /* skip the trailing white spaces */
493 length = strlen(src);
494 while (length && isspace(src[length-1]))
495 src[--length] = '\0';
497 for (p = strtok(src, ","); p != NULL; p = strtok(NULL, ","))
498 addSet(dest, Safe_strdup(p));
501 /*-----------------------------------------------------------------*/
502 /* setDefaultOptions - sets the default options */
503 /*-----------------------------------------------------------------*/
505 setDefaultOptions (void)
507 /* first the options part */
508 options.stack_loc = 0; /* stack pointer initialised to 0 */
509 options.xstack_loc = 0; /* xternal stack starts at 0 */
510 options.code_loc = 0; /* code starts at 0 */
511 options.data_loc = 0; /* JCF: By default let the linker locate data */
512 options.xdata_loc = 0;
513 options.idata_loc = 0x80;
515 options.model = port->general.default_model;
516 options.nostdlib = 0;
517 options.nostdinc = 0;
519 options.shortis8bits = 0;
521 options.stack10bit=0;
523 /* now for the optimizations */
524 /* turn on the everything */
525 optimize.global_cse = 1;
530 optimize.loopInvariant = 1;
531 optimize.loopInduction = 1;
533 /* now for the ports */
534 port->setDefaultOptions ();
537 /*-----------------------------------------------------------------*/
538 /* processFile - determines the type of file from the extension */
539 /*-----------------------------------------------------------------*/
541 processFile (char *s)
545 /* get the file extension */
546 fext = s + strlen (s);
547 while ((fext != s) && *fext != '.')
550 /* now if no '.' then we don't know what the file type is
551 so give a warning and return */
554 werror (W_UNKNOWN_FEXT, s);
558 /* otherwise depending on the file type */
559 if (strcmp (fext, ".c") == 0 || strcmp (fext, ".C") == 0)
561 /* source file name : not if we already have a
565 werror (W_TOO_MANY_SRC, s);
569 /* the only source file */
571 if (!(srcFile = fopen (fullSrcFileName, "r")))
573 werror (E_FILE_OPEN_ERR, s);
577 /* copy the file name into the buffer */
578 strncpyz (buffer, s, sizeof(buffer));
580 /* get rid of the "."-extension */
582 /* is there a dot at all? */
583 if (strrchr (buffer, '.') &&
584 /* is the dot in the filename, not in the path? */
585 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
587 *strrchr (buffer, '.') = '\0';
590 /* get rid of any path information
591 for the module name; */
592 fext = buffer + strlen (buffer);
594 /* do this by going backwards till we
595 get '\' or ':' or start of buffer */
596 while (fext != buffer &&
597 *(fext - 1) != DIR_SEPARATOR_CHAR &&
603 /* do this by going backwards till we
604 get '/' or start of buffer */
605 while (fext != buffer &&
606 *(fext - 1) != DIR_SEPARATOR_CHAR)
611 moduleNameBase = Safe_strdup ( fext );
612 moduleName = Safe_strdup ( fext );
614 for (fext = moduleName; *fext; fext++)
615 if (!isalnum (*fext))
620 /* if the extention is type .rel or .r or .REL or .R
621 addtional object file will be passed to the linker */
622 if (strcmp (fext, ".r") == 0 || strcmp (fext, ".rel") == 0 ||
623 strcmp (fext, ".R") == 0 || strcmp (fext, ".REL") == 0 ||
624 strcmp (fext, port->linker.rel_ext) == 0)
626 addSet(&relFilesSet, Safe_strdup(s));
630 /* if .lib or .LIB */
631 if (strcmp (fext, ".lib") == 0 || strcmp (fext, ".LIB") == 0)
633 addSet(&libFilesSet, Safe_strdup(s));
637 werror (W_UNKNOWN_FEXT, s);
642 _setModel (int model, const char *sz)
644 if (port->general.supported_models & model)
645 options.model = model;
647 werror (W_UNSUPPORTED_MODEL, sz, port->target);
650 /** Gets the string argument to this option. If the option is '--opt'
651 then for input of '--optxyz' or '--opt xyz' returns xyz.
654 getStringArg(const char *szStart, char **argv, int *pi, int argc)
656 if (argv[*pi][strlen(szStart)])
658 return &argv[*pi][strlen(szStart)];
665 werror (E_ARGUMENT_MISSING, szStart);
666 /* Die here rather than checking for errors later. */
676 /** Gets the integer argument to this option using the same rules as
680 getIntArg(const char *szStart, char **argv, int *pi, int argc)
682 return (int)floatFromVal(constVal(getStringArg(szStart, argv, pi, argc)));
686 verifyShortOption(const char *opt)
688 if (strlen(opt) != 2)
690 werror (W_EXCESS_SHORT_OPTIONS, opt);
695 tryHandleUnsupportedOpt(char **argv, int *pi)
697 if (argv[*pi][0] == '-')
699 const char *longOpt = "";
703 if (argv[*pi][1] == '-')
710 shortOpt = argv[*pi][1];
712 for (i = 0; i < LENGTH(unsupportedOptTable); i++)
714 if (unsupportedOptTable[i].shortOpt == shortOpt ||
715 (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt))) {
716 /* Found an unsupported opt. */
718 SNPRINTF(buffer, sizeof(buffer),
720 longOpt ? longOpt : "",
721 shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
722 werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
726 /* Didn't find in the table */
731 /* Not an option, so can't be unsupported :) */
737 scanOptionsTable(const OPTION *optionsTable, char shortOpt, const char *longOpt, char **argv, int *pi)
740 for (i = 0; optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL; i++)
742 if (optionsTable[i].shortOpt == shortOpt ||
743 (longOpt && optionsTable[i].longOpt &&
744 strcmp(optionsTable[i].longOpt, longOpt) == 0))
747 /* If it is a flag then we can handle it here */
748 if (optionsTable[i].pparameter != NULL)
750 if (optionsTable[i].shortOpt == shortOpt)
752 verifyShortOption(argv[*pi]);
755 (*optionsTable[i].pparameter)++;
759 /* Not a flag. Handled manually later. */
764 /* Didn't find in the table */
769 tryHandleSimpleOpt(char **argv, int *pi)
771 if (argv[*pi][0] == '-')
773 const char *longOpt = "";
776 if (argv[*pi][1] == '-')
783 shortOpt = argv[*pi][1];
786 if (scanOptionsTable(optionsTable, shortOpt, longOpt, argv, pi))
790 else if (port && port->poptions &&
791 scanOptionsTable(port->poptions, shortOpt, longOpt, argv, pi))
802 /* Not an option, so can't be handled. */
807 /*-----------------------------------------------------------------*/
808 /* parseCmdLine - parses the command line and sets the options */
809 /*-----------------------------------------------------------------*/
811 parseCmdLine (int argc, char **argv)
815 /* go thru all whole command line */
816 for (i = 1; i < argc; i++)
821 if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
826 if (tryHandleSimpleOpt(argv, &i) == TRUE)
832 if (argv[i][0] == '-' && argv[i][1] == '-')
834 if (strcmp (argv[i], OPTION_HELP) == 0)
840 if (strcmp (argv[i], OPTION_STACK_8BIT) == 0)
842 options.stack10bit = 0;
846 if (strcmp (argv[i], OPTION_OUT_FMT_IHX) == 0)
852 if (strcmp (argv[i], OPTION_LARGE_MODEL) == 0)
854 _setModel (MODEL_LARGE, argv[i]);
858 if (strcmp (argv[i], OPTION_MEDIUM_MODEL) == 0)
860 _setModel (MODEL_MEDIUM, argv[i]);
864 if (strcmp (argv[i], OPTION_SMALL_MODEL) == 0)
866 _setModel (MODEL_SMALL, argv[i]);
870 if (strcmp (argv[i], OPTION_FLAT24_MODEL) == 0)
872 _setModel (MODEL_FLAT24, argv[i]);
876 if (strcmp (argv[i], OPTION_DUMP_ALL) == 0)
878 options.dump_rassgn =
884 options.dump_raw = 1;
888 if (strcmp (argv[i], OPTION_PEEP_FILE) == 0)
890 options.peep_file = getStringArg(OPTION_PEEP_FILE, argv, &i, argc);
894 if (strcmp (argv[i], OPTION_LIB_PATH) == 0)
896 addSet(&libPathsSet, Safe_strdup(getStringArg(OPTION_LIB_PATH, argv, &i, argc)));
900 if (strcmp (argv[i], OPTION_VERSION) == 0)
907 if (strcmp (argv[i], OPTION_CALLEE_SAVES) == 0)
909 setParseWithComma(&options.calleeSavesSet, getStringArg(OPTION_CALLEE_SAVES, argv, &i, argc));
913 if (strcmp (argv[i], OPTION_XSTACK_LOC) == 0)
915 options.xstack_loc = getIntArg(OPTION_XSTACK_LOC, argv, &i, argc);
919 if (strcmp (argv[i], OPTION_STACK_LOC) == 0)
921 options.stack_loc = getIntArg(OPTION_STACK_LOC, argv, &i, argc);
925 if (strcmp (argv[i], OPTION_STACK_SIZE) == 0)
927 options.stack_size = getIntArg(OPTION_STACK_SIZE, argv, &i, argc);
931 if (strcmp (argv[i], OPTION_XRAM_LOC) == 0)
933 options.xdata_loc = getIntArg(OPTION_XRAM_LOC, argv, &i, argc);
937 if (strcmp (argv[i], OPTION_IRAM_SIZE) == 0)
939 options.iram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
943 if (strcmp (argv[i], OPTION_XRAM_SIZE) == 0)
945 options.xram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
946 options.xram_size_set = TRUE;
950 if (strcmp (argv[i], OPTION_CODE_SIZE) == 0)
952 options.code_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
956 if (strcmp (argv[i], OPTION_DATA_LOC) == 0)
958 options.data_loc = getIntArg(OPTION_DATA_LOC, argv, &i, argc);
962 if (strcmp (argv[i], OPTION_IDATA_LOC) == 0)
964 options.idata_loc = getIntArg(OPTION_IDATA_LOC, argv, &i, argc);
968 if (strcmp (argv[i], OPTION_CODE_LOC) == 0)
970 options.code_loc = getIntArg(OPTION_CODE_LOC, argv, &i, argc);
974 if (strcmp (argv[i], OPTION_NO_GCSE) == 0)
976 optimize.global_cse = 0;
980 if (strcmp (argv[i], OPTION_NO_LOOP_INV) == 0)
982 optimize.loopInvariant = 0;
986 if (strcmp (argv[i], OPTION_NO_LABEL_OPT) == 0)
992 if (strcmp (argv[i], OPTION_NO_LOOP_IND) == 0)
994 optimize.loopInduction = 0;
998 if (strcmp (argv[i], OPTION_LESS_PEDANTIC) == 0)
1000 options.lessPedantic = 1;
1001 setErrorLogLevel(ERROR_LEVEL_WARNING);
1005 if (strcmp (&argv[i][1], OPTION_SHORT_IS_8BITS) == 0)
1007 options.shortis8bits=1;
1011 if (strcmp (argv[i], OPTION_TINI_LIBID) == 0)
1013 options.tini_libid = getIntArg(OPTION_TINI_LIBID, argv, &i, argc);
1017 if (!port->parseOption (&argc, argv, &i))
1019 werror (W_UNKNOWN_OPTION, argv[i]);
1027 /* if preceded by '-' then option */
1028 if (*argv[i] == '-')
1033 verifyShortOption(argv[i]);
1040 /* Used to select the port. But this has already been done. */
1044 /* Used to select the processor in port. But this has
1045 * already been done. */
1049 verifyShortOption(argv[i]);
1051 options.cc_only = 1;
1055 addSet(&libPathsSet, Safe_strdup(getStringArg("-L", argv, &i, argc)));
1059 addSet(&libFilesSet, Safe_strdup(getStringArg("-l", argv, &i, argc)));
1066 /* copy the file name into the buffer */
1067 strncpyz(buffer, getStringArg("-o", argv, &i, argc),
1069 /* point to last character */
1070 p = buffer + strlen (buffer) - 1;
1071 if (*p == DIR_SEPARATOR_CHAR)
1073 /* only output path specified */
1074 dstPath = Safe_strdup (buffer);
1075 fullDstFileName = NULL;
1079 fullDstFileName = Safe_strdup (buffer);
1081 /* get rid of the "."-extension */
1083 /* is there a dot at all? */
1084 if (strrchr (buffer, '.') &&
1085 /* is the dot in the filename, not in the path? */
1086 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
1087 *strrchr (buffer, '.') = '\0';
1089 dstFileName = Safe_strdup (buffer);
1091 /* strip module name to get path */
1092 p = strrchr (buffer, DIR_SEPARATOR_CHAR);
1095 /* path with trailing / */
1097 dstPath = Safe_strdup (buffer);
1104 /* pre-processer options */
1105 if (argv[i][2] == 'p')
1107 setParseWithComma(&preArgvSet, getStringArg("-Wp", argv, &i, argc));
1109 /* linker options */
1110 else if (argv[i][2] == 'l')
1112 setParseWithComma(&linkOptionsSet, getStringArg("-Wl", argv, &i, argc));
1114 /* assembler options */
1115 else if (argv[i][2] == 'a')
1117 setParseWithComma(&asmOptionsSet, getStringArg("-Wa", argv, &i, argc));
1121 werror (W_UNKNOWN_OPTION, argv[i]);
1126 verifyShortOption(argv[i]);
1128 printVersionInfo ();
1132 /* preprocessor options */
1136 if (argv[i][2] == 'M')
1137 addSet(&preArgvSet, Safe_strdup("-MM"));
1139 addSet(&preArgvSet, Safe_strdup("-M"));
1144 addSet(&preArgvSet, Safe_strdup("-C"));
1154 char sOpt = argv[i][1];
1157 if (argv[i][2] == ' ' || argv[i][2] == '\0')
1163 werror(E_ARGUMENT_MISSING, argv[i-1]);
1177 SNPRINTF (buffer, sizeof(buffer),
1178 ((sOpt == 'I') ? "-%c\"%s\"": "-%c%s"), sOpt, rest);
1179 addSet(&preArgvSet, Safe_strdup(buffer));
1184 if (!port->parseOption (&argc, argv, &i))
1185 werror (W_UNKNOWN_OPTION, argv[i]);
1190 if (!port->parseOption (&argc, argv, &i))
1192 /* no option must be a filename */
1195 werror (W_NO_FILE_ARG_IN_C1, argv[i]);
1199 processFile (argv[i]);
1204 /* some sanity checks in c1 mode */
1209 if (fullSrcFileName)
1212 werror (W_NO_FILE_ARG_IN_C1, fullSrcFileName);
1214 fullSrcFileName = NULL;
1215 for (s = setFirstItem(relFilesSet); s != NULL; s = setNextItem(relFilesSet))
1217 werror (W_NO_FILE_ARG_IN_C1, s);
1219 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1221 werror (W_NO_FILE_ARG_IN_C1, s);
1223 deleteSet(&relFilesSet);
1224 deleteSet(&libFilesSet);
1226 if (options.cc_only || noAssemble || preProcOnly)
1228 werror (W_ILLEGAL_OPT_COMBINATION);
1230 options.cc_only = noAssemble = preProcOnly = 0;
1233 werror (E_NEED_OPT_O_IN_C1);
1237 /* if no dstFileName given with -o, we've to find one: */
1242 /* use the modulename from the C-source */
1243 if (fullSrcFileName)
1245 size_t bufSize = strlen (dstPath) + strlen (moduleNameBase) + 1;
1247 dstFileName = Safe_alloc (bufSize);
1248 strncpyz (dstFileName, dstPath, bufSize);
1249 strncatz (dstFileName, moduleNameBase, bufSize);
1251 /* use the modulename from the first object file */
1252 else if ((s = peekSet(relFilesSet)) != NULL)
1257 strncpyz (buffer, s, sizeof(buffer));
1258 /* remove extension (it must be .rel) */
1259 *strrchr (buffer, '.') = '\0';
1261 objectName = strrchr (buffer, DIR_SEPARATOR_CHAR);
1268 objectName = buffer;
1270 bufSize = strlen (dstPath) + strlen (objectName) + 1;
1271 dstFileName = Safe_alloc (bufSize);
1272 strncpyz (dstFileName, dstPath, bufSize);
1273 strncatz (dstFileName, objectName, bufSize);
1275 /* else no module given: help text is displayed */
1278 /* set up external stack location if not explicitly specified */
1279 if (!options.xstack_loc)
1280 options.xstack_loc = options.xdata_loc;
1282 /* if debug option is set then open the cdbFile */
1283 if (options.debug && fullSrcFileName)
1285 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1286 "%s.adb", dstFileName); /*JCF: Nov 30, 2002*/
1287 if(debugFile->openFile(scratchFileName))
1288 debugFile->writeModule(moduleName);
1290 werror (E_FILE_OPEN_ERR, scratchFileName);
1292 MSVC_style(options.vc_err_style);
1293 if(options.use_stdout) dup2(STDOUT_FILENO, STDERR_FILENO);
1298 /*-----------------------------------------------------------------*/
1299 /* linkEdit : - calls the linkage editor with options */
1300 /*-----------------------------------------------------------------*/
1302 linkEdit (char **envp)
1310 if(port->linker.needLinkerScript)
1314 switch (options.out_fmt)
1317 out_fmt = 'i'; /* Intel hex */
1320 out_fmt = 's'; /* Motorola S19 */
1323 out_fmt = 't'; /* Elf */
1329 /* first we need to create the <filename>.lnk file */
1330 SNPRINTF (scratchFileName, sizeof(scratchFileName),
1331 "%s.lnk", dstFileName);
1332 if (!(lnkfile = fopen (scratchFileName, "w")))
1334 werror (E_FILE_OPEN_ERR, scratchFileName);
1338 if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
1340 fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
1341 out_fmt, dstFileName);
1343 else /*For all the other ports. Including pics???*/
1345 fprintf (lnkfile, "-myux%c\n", out_fmt);
1346 if(options.pack_iram)
1347 fprintf (lnkfile, "-Y\n");
1350 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1352 /* if iram size specified */
1353 if (options.iram_size)
1354 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1356 /* if stack size specified*/
1357 if(options.stack_size)
1358 fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
1360 /* if xram size specified */
1361 if (options.xram_size_set)
1362 fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
1364 /* if code size specified */
1365 if (options.code_size)
1366 fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
1369 fprintf (lnkfile, "-z\n");
1372 #define WRITE_SEG_LOC(N, L) \
1373 segName = Safe_strdup(N); \
1374 c = strtok(segName, " \t"); \
1375 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1376 if (segName) { Safe_free(segName); }
1378 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
1381 /* code segment start */
1382 WRITE_SEG_LOC (CODE_NAME, options.code_loc);
1384 /* data segment start. If zero, the linker chooses
1385 the best place for data */
1386 if(options.data_loc)
1388 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1392 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1397 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1400 /* bit segment start */
1401 WRITE_SEG_LOC (BIT_NAME, 0);
1404 if ( (options.stack_loc) && (options.stack_loc<0x100) )
1406 WRITE_SEG_LOC ("SSEG", options.stack_loc);
1409 else /*For the z80, gbz80*/
1411 WRITE_SEG_LOC ("_CODE", options.code_loc);
1412 WRITE_SEG_LOC ("_DATA", options.data_loc);
1415 /* If the port has any special linker area declarations, get 'em */
1416 if (port->extraAreas.genExtraAreaLinkOptions)
1418 port->extraAreas.genExtraAreaLinkOptions(lnkfile);
1421 /* add the extra linker options */
1422 fputStrSet(lnkfile, linkOptionsSet);
1424 /* command line defined library paths if specified */
1425 for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
1426 fprintf (lnkfile, "-k %s\n", s);
1428 /* standard library path */
1429 if (!options.nostdlib)
1431 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1433 switch (options.model)
1443 if (TARGET_IS_DS390)
1447 else if (TARGET_IS_DS400)
1454 "Add support for your FLAT24 target in %s @ line %d\n",
1455 __FILE__, __LINE__);
1463 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1468 else /*for the z80, gbz80*/
1472 else if (TARGET_IS_Z80)
1477 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1478 mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
1481 /* command line defined library files if specified */
1482 for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
1483 fprintf (lnkfile, "-l %s\n", s);
1485 /* standard library files */
1486 if (!options.nostdlib)
1488 #if !OPT_DISABLE_DS390
1489 if (options.model == MODEL_FLAT24)
1491 if (TARGET_IS_DS390)
1493 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1495 else if (TARGET_IS_DS400)
1497 fprintf (lnkfile, "-l %s\n", STD_DS400_LIB);
1502 "Add support for your FLAT24 target in %s @ line %d\n",
1503 __FILE__, __LINE__);
1509 #if !OPT_DISABLE_XA51
1511 if (options.model == MODEL_PAGE0)
1513 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1517 if (TARGET_IS_MCS51)
1519 fprintf (lnkfile, "-l mcs51\n");
1521 if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80
1522 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
1523 { /*Why the z80 port is not using the standard libraries?*/
1524 fprintf (lnkfile, "-l %s\n", STD_LIB);
1525 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1526 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1527 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1529 else if (TARGET_IS_HC08)
1531 fprintf (lnkfile, "-l hc08\n");
1533 else if (TARGET_IS_Z80)
1535 fprintf (lnkfile, "-l z80\n");
1537 else if (TARGET_IS_GBZ80)
1539 fprintf (lnkfile, "-l gbz80\n");
1543 /*For the z80 and gbz80 ports, try to find where crt0.o is...
1544 It is very important for this file to be first on the linking proccess
1545 so the areas are set in the correct order, expecially _GSINIT*/
1546 if ((TARGET_IS_Z80 || TARGET_IS_GBZ80) &&
1547 !options.no_std_crt0 && !options.nostdlib) /*For the z80, gbz80*/
1549 char crt0path[PATH_MAX];
1551 for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
1553 sprintf (crt0path, "%s%s%s%scrt0.o",
1554 s, DIR_SEPARATOR_STRING, c, DIR_SEPARATOR_STRING);
1556 crt0fp=fopen(crt0path, "r");
1557 if(crt0fp!=NULL)/*Found it!*/
1562 /*The CYGWIN version of the z80-gbz80 linker is getting confused with
1563 windows paths, so convert them to the CYGWIN format*/
1564 char posix_path[PATH_MAX];
1565 void cygwin_conv_to_full_posix_path(char * win_path, char * posix_path);
1566 cygwin_conv_to_full_posix_path(crt0path, posix_path);
1567 strcpy(crt0path, posix_path);
1570 fprintf (lnkfile, "%s\n", crt0path);
1574 if(s==NULL) fprintf (stderr, "Warning: couldn't find crt0.o\n");
1577 /* put in the object files */
1578 if (fullSrcFileName)
1579 fprintf (lnkfile, "%s%s\n", dstFileName, port->linker.rel_ext);
1581 fputStrSet(lnkfile, relFilesSet);
1583 fprintf (lnkfile, "\n-e\n");
1585 } /* if(port->linker.needLinkerScript) */
1587 if (options.verbose)
1588 printf ("sdcc: Calling linker...\n");
1590 /* build linker output filename */
1592 /* -o option overrides default name? */
1593 if (fullDstFileName)
1595 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1599 /* the linked file gets the name of the first modul */
1600 if (fullSrcFileName)
1602 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1606 s = peekSet(relFilesSet);
1610 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1611 /* strip ".rel" extension */
1612 *strrchr (scratchFileName, '.') = '\0';
1614 strncatz (scratchFileName,
1615 options.out_fmt ? ".S19" : ".ihx",
1616 sizeof(scratchFileName));
1619 if (port->linker.cmd)
1621 char buffer2[PATH_MAX];
1624 if(TARGET_IS_PIC16) {
1625 /* use $3 to set the linker include directories */
1626 libSet = appendStrSet(libDirsSet, "-I\"", "\"");
1628 /* now add the libraries from command line */
1629 mergeSets(&libSet, libFilesSet);
1632 buildCmdLine (buffer2, port->linker.cmd, dstFileName, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
1633 if(libSet)deleteSet(&libSet);
1635 buildCmdLine2 (buffer, sizeof(buffer), buffer2);
1639 buildCmdLine2 (buffer, sizeof(buffer), port->linker.mcmd);
1642 /* if (options.verbose)fprintf(stderr, "linker command line: %s\n", buffer); */
1644 system_ret = my_system (buffer);
1645 /* TODO: most linker don't have a -o parameter */
1646 /* -o option overrides default name? */
1647 if (fullDstFileName)
1650 /* the linked file gets the name of the first modul */
1651 if (fullSrcFileName)
1653 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1654 p = strlen (scratchFileName) + scratchFileName;
1658 s = peekSet(relFilesSet);
1662 strncpyz (scratchFileName, s, sizeof(scratchFileName));
1663 /* strip ".rel" extension */
1664 p = strrchr (scratchFileName, '.');
1670 strncatz (scratchFileName,
1671 options.out_fmt ? ".S19" : ".ihx",
1672 sizeof(scratchFileName));
1673 if (strcmp (fullDstFileName, scratchFileName))
1674 unlink (fullDstFileName);
1675 rename (scratchFileName, fullDstFileName);
1677 strncpyz (buffer, fullDstFileName, sizeof(buffer));
1678 q = strrchr (buffer, '.');
1681 /* no extension: append new extensions */
1682 q = strlen (buffer) + buffer;
1686 strncatz (scratchFileName, ".map", sizeof(scratchFileName));
1688 strncatz(buffer, ".map", sizeof(buffer));
1689 if (strcmp (scratchFileName, buffer))
1691 rename (scratchFileName, buffer);
1693 strncatz (scratchFileName, ".mem", sizeof(scratchFileName));
1695 strncatz(buffer, ".mem", sizeof(buffer));
1696 if (strcmp (scratchFileName, buffer))
1698 rename (scratchFileName, buffer);
1702 strncatz (scratchFileName, ".cdb", sizeof(scratchFileName));
1704 strncatz(buffer, ".cdb", sizeof(buffer));
1705 if (strcmp (scratchFileName, buffer))
1707 rename (scratchFileName, buffer);
1708 /* and the OMF file without extension: */
1711 if (strcmp (scratchFileName, buffer))
1713 rename (scratchFileName, buffer);
1722 /*-----------------------------------------------------------------*/
1723 /* assemble - spawns the assembler with arguments */
1724 /*-----------------------------------------------------------------*/
1726 assemble (char **envp)
1728 /* build assembler output filename */
1730 /* -o option overrides default name? */
1731 if (options.cc_only && fullDstFileName) {
1732 strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
1734 /* the assembled file gets the name of the first modul */
1735 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1736 strncatz (scratchFileName, port->linker.rel_ext,
1737 sizeof(scratchFileName));
1740 if (port->assembler.do_assemble) {
1741 port->assembler.do_assemble(asmOptionsSet);
1743 } else if (port->assembler.cmd) {
1744 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1745 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1748 buildCmdLine2 (buffer, sizeof(buffer), port->assembler.mcmd);
1751 if (my_system (buffer)) {
1752 /* either system() or the assembler itself has reported an error
1753 perror ("Cannot exec assembler");
1757 /* TODO: most assembler don't have a -o parameter */
1758 /* -o option overrides default name? */
1759 if (options.cc_only && fullDstFileName) {
1760 strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
1761 strncatz (scratchFileName,
1762 port->linker.rel_ext,
1763 sizeof(scratchFileName));
1764 if (strcmp (scratchFileName, fullDstFileName))
1765 unlink (fullDstFileName);
1766 rename (scratchFileName, fullDstFileName);
1770 /*-----------------------------------------------------------------*/
1771 /* preProcess - spawns the preprocessor with arguments */
1772 /*-----------------------------------------------------------------*/
1774 preProcess (char **envp)
1783 set *inclList = NULL;
1785 /* if using external stack define the macro */
1786 if (options.useXstack)
1787 addSet(&preArgvSet, Safe_strdup("-DSDCC_USE_XSTACK"));
1789 /* set the macro for stack autos */
1790 if (options.stackAuto)
1791 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_AUTO"));
1793 /* set the macro for stack autos */
1794 if (options.stack10bit)
1795 addSet(&preArgvSet, Safe_strdup("-DSDCC_STACK_TENBIT"));
1797 /* set the macro for no overlay */
1798 if (options.noOverlay)
1799 addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY"));
1801 /* set the macro for large model */
1802 switch (options.model)
1805 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_LARGE"));
1808 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_SMALL"));
1811 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_COMPACT"));
1814 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_MEDIUM"));
1817 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_FLAT24"));
1820 addSet(&preArgvSet, Safe_strdup("-DSDCC_MODEL_PAGE0"));
1823 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1827 /* add port (processor information to processor */
1828 addSet(&preArgvSet, Safe_strdup("-DSDCC_{port}"));
1829 addSet(&preArgvSet, Safe_strdup("-D__{port}"));
1831 /* standard include path */
1832 if (!options.nostdinc) {
1833 inclList = appendStrSet(includeDirsSet, "-I\"", "\"");
1834 mergeSets(&preArgvSet, inclList);
1837 setMainValue("cppextraopts", (s = joinStrSet(preArgvSet)));
1838 Safe_free((void *)s);
1839 if (inclList != NULL)
1840 deleteSet(&inclList);
1842 if (preProcOnly && fullDstFileName)
1844 /* -E and -o given */
1845 setMainValue ("cppoutfilename", fullDstFileName);
1849 /* Piping: set cppoutfilename to NULL, to avoid empty quotes */
1850 setMainValue ("cppoutfilename", NULL);
1853 if (options.verbose)
1854 printf ("sdcc: Calling preprocessor...\n");
1856 buildCmdLine2 (buffer, sizeof(buffer), _preCmd);
1859 if (my_system (buffer)) {
1866 yyin = my_popen (buffer);
1868 perror ("Preproc file not found");
1871 addSetHead (&pipeSet, yyin);
1879 setBinPaths(const char *argv0)
1887 * 1. - $SDCCDIR/PREFIX2BIN_DIR
1888 * 2. - path(argv[0])
1892 /* do it in reverse mode, so that addSetHead() can be used
1893 instead of slower addSet() */
1895 if ((p = getBinPath(argv0)) != NULL)
1896 addSetHead(&binPathSet, Safe_strdup(p));
1898 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
1899 SNPRINTF(buf, sizeof buf, "%s" PREFIX2BIN_DIR, p);
1900 addSetHead(&binPathSet, Safe_strdup(buf));
1904 if (options.printSearchDirs) {
1905 printf("programs:\n");
1906 fputStrSet(stdout, binPathSet);
1911 /* Set system include path */
1913 setIncludePath(void)
1920 * 1. - $SDCC_INCLUDE
1921 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/INCLUDE_DIR_SUFFIX
1922 * 3. - path(argv[0])/BIN2DATA_DIR/INCLUDE_DIR_SUFFIX
1923 * 4. - DATADIR/INCLUDE_DIR_SUFFIX (only on *nix)
1926 includeDirsSet = appendStrSet(dataDirsSet, NULL, INCLUDE_DIR_SUFFIX);
1928 if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
1929 addSetHead(&includeDirsSet, p);
1932 if (options.printSearchDirs) {
1933 printf("includedir:\n");
1934 fputStrSet(stdout, includeDirsSet);
1939 /* Set system lib path */
1949 * 2. - $SDCC_HOME/PREFIX2DATA_DIR/LIB_DIR_SUFFIX/<model>
1950 * 3. - path(argv[0])/BIN2DATA_DIR/LIB_DIR_SUFFIX/<model>
1951 * 4. - DATADIR/LIB_DIR_SUFFIX/<model> (only on *nix)
1954 libDirsSet = appendStrSet(dataDirsSet, NULL, LIB_DIR_SUFFIX);
1956 if ((p = getenv(SDCC_LIB_NAME)) != NULL)
1957 addSetHead(&libDirsSet, p);
1960 if (options.printSearchDirs) {
1961 printf("libdir:\n");
1962 fputStrSet(stdout, libDirsSet);
1969 setDataPaths(const char *argv0)
1977 * 1. - $SDCC_HOME/PREFIX2DATA_DIR
1978 * 2. - path(argv[0])/BIN2DATA_DIR
1979 * 3. - DATADIR (only on *nix)
1982 if ((p = getenv(SDCC_DIR_NAME)) != NULL) {
1983 SNPRINTF(buf, sizeof buf, "%s" PREFIX2DATA_DIR, p);
1984 addSet(&dataDirsSet, Safe_strdup(buf));
1987 if ((p = getBinPath(argv0)) != NULL) {
1988 SNPRINTF(buf, sizeof buf, "%s" BIN2DATA_DIR, p);
1989 addSet(&dataDirsSet, Safe_strdup(buf));
1993 if (peekSet(dataDirsSet) == NULL) {
1994 /* this should never happen... */
1995 wassertl(0, "Can't get binary path");
1998 addSet(&dataDirsSet, Safe_strdup(DATADIR));
2002 if (options.printSearchDirs) {
2003 printf("datadir:\n");
2004 fputStrSet(stdout, dataDirsSet);
2015 populateMainValues (_baseValues);
2016 setMainValue ("port", port->target);
2017 setMainValue ("objext", port->linker.rel_ext);
2018 setMainValue ("asmext", port->assembler.file_ext);
2020 setMainValue ("dstfilename", dstFileName);
2021 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
2023 if (options.cc_only && fullDstFileName)
2024 /* compile + assemble and -o given: -o specifies name of object file */
2026 setMainValue ("objdstfilename", fullDstFileName);
2030 setMainValue ("objdstfilename", "{stdobjdstfilename}");
2032 if (fullDstFileName)
2033 /* if we're linking, -o gives the final file name */
2035 setMainValue ("linkdstfilename", fullDstFileName);
2039 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
2044 static void doPrintSearchDirs(void)
2046 printf("programs:\n");
2047 fputStrSet(stdout, binPathSet);
2049 printf("datadir:\n");
2050 fputStrSet(stdout, dataDirsSet);
2052 printf("includedir:\n");
2053 fputStrSet(stdout, includeDirsSet);
2055 printf("libdir:\n");
2056 fputStrSet(stdout, libDirsSet);
2061 sig_handler (int signal)
2068 sig_string = "SIGABRT";
2071 sig_string = "SIGTERM";
2074 sig_string = "SIGINT";
2077 sig_string = "SIGSEGV";
2080 sig_string = "Unknown?";
2083 fprintf (stderr, "Caught signal %d: %s\n", signal, sig_string);
2089 * initialises and calls the parser
2093 main (int argc, char **argv, char **envp)
2095 /* turn all optimizations off by default */
2096 memset (&optimize, 0, sizeof (struct optimize));
2098 /*printVersionInfo (); */
2101 fprintf (stderr, "Build error: no ports are enabled.\n");
2105 /* install atexit handler */
2106 atexit(rm_tmpfiles);
2108 /* install signal handler;
2109 it's only purpuse is to call exit() to remove temp files */
2110 if (!getenv("SDCC_LEAVE_SIGNALS"))
2112 signal (SIGABRT, sig_handler);
2113 signal (SIGTERM, sig_handler);
2114 signal (SIGINT , sig_handler);
2115 signal (SIGSEGV, sig_handler);
2118 /* Before parsing the command line options, do a
2119 * search for the port and processor and initialize
2120 * them if they're found. (We can't gurantee that these
2121 * will be the first options specified).
2124 _findPort (argc, argv);
2127 if (strcmp(port->target, "mcs51") == 0) {
2128 printf("DS390 jammed in A\n");
2134 _findProcessor (argc, argv);
2136 /* Initalise the port. */
2140 setDefaultOptions ();
2143 options.model = MODEL_SMALL;
2144 options.stack10bit=0;
2147 parseCmdLine (argc, argv);
2150 setBinPaths(argv[0]);
2151 setDataPaths(argv[0]);
2156 if(options.printSearchDirs)
2157 doPrintSearchDirs();
2159 /* if no input then printUsage & exit */
2160 if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL) {
2161 if (!options.printSearchDirs)
2167 /* initMem() is expensive, but
2168 initMem() must called before port->finaliseOptions ().
2169 And the z80 port needs port->finaliseOptions(),
2170 even if we're only linking. */
2172 port->finaliseOptions ();
2174 if (fullSrcFileName || options.c1mode)
2184 if (options.verbose)
2185 printf ("sdcc: Generating code...\n");
2191 deleteSetItem(&pipeSet, yyin);
2197 if (port->general.do_glue != NULL)
2198 (*port->general.do_glue)();
2201 /* this shouldn't happen */
2203 /* in case of NDEBUG */
2211 if (!options.c1mode && !noAssemble)
2213 if (options.verbose)
2214 printf ("sdcc: Calling assembler...\n");
2220 if (options.debug && debugFile)
2221 debugFile->closeFile();
2223 if (!options.cc_only &&
2227 (fullSrcFileName || peekSet(relFilesSet) != NULL))
2229 if (port->linker.do_link)
2230 port->linker.do_link ();