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 -------------------------------------------------------------------------*/
31 #include "SDCCmacro.h"
41 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
46 /** Name of the environment variable checked for other instalations. */
47 #define SDCCDIR_NAME "SDCCDIR"
50 extern int yyparse ();
52 FILE *srcFile; /* source file */
53 FILE *cdbFile = NULL; /* debugger information output file */
54 char *fullSrcFileName; /* full name for the source file; */
55 /* can be NULL while 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 *moduleName; /* module name is source file without path and extension */
62 /* can be NULL while linking without compiling */
63 const char *preArgv[128]; /* pre-processor arguments */
65 int RegBankUsed[4]={1, 0, 0, 0}; /*JCF: Reg Bank 0 used by default*/
66 struct optimize optimize;
67 struct options options;
68 char *VersionString = SDCC_VERSION_STR;
71 char *linkOptions[128];
72 const char *asmOptions[128];
79 bool verboseExec = FALSE;
81 bool noXinitOpt = FALSE;
83 /* uncomment JAMIN_DS390 to always override and use ds390 port
84 for mcs51 work. This is temporary, for compatibility testing. */
85 /* #define JAMIN_DS390 */
90 // Globally accessible scratch buffer for file names.
91 char scratchFileName[PATH_MAX];
92 char buffer[PATH_MAX];
94 // In MSC VC6 default search path for exe's to path for this
96 char DefaultExePath[128];
98 #define OPTION_HELP "-help"
100 #define LENGTH(_a) (sizeof(_a)/sizeof(*(_a)))
102 #define OPTION_STACK_8BIT "--stack-8bit"
103 #define OPTION_OUT_FMT_IHX "--out-fmt-ihx"
104 #define OPTION_LARGE_MODEL "--model-large"
105 #define OPTION_MEDIUM_MODEL "--model-medium"
106 #define OPTION_SMALL_MODEL "--model-small"
107 #define OPTION_FLAT24_MODEL "--model-flat24"
108 #define OPTION_DUMP_ALL "--dumpall"
109 #define OPTION_PEEP_FILE "--peep-file"
110 #define OPTION_LIB_PATH "--lib-path"
111 #define OPTION_XSTACK_LOC "--xstack-loc"
112 #define OPTION_CALLEE_SAVES "--callee-saves"
113 #define OPTION_STACK_LOC "--stack-loc"
114 #define OPTION_XRAM_LOC "--xram-loc"
115 #define OPTION_IRAM_SIZE "--iram-size"
116 #define OPTION_VERSION "--version"
117 #define OPTION_DATA_LOC "--data-loc"
118 #define OPTION_CODE_LOC "--code-loc"
119 #define OPTION_IDATA_LOC "--idata-loc"
120 #define OPTION_NO_LOOP_INV "--noinvariant"
121 #define OPTION_NO_LOOP_IND "--noinduction"
122 #define OPTION_LESS_PEDANTIC "--lesspedantic"
123 #define OPTION_NO_GCSE "--nogcse"
124 #define OPTION_SHORT_IS_8BITS "--short-is-8bits"
125 #define OPTION_TINI_LIBID "--tini-libid"
126 #define OPTION_NO_XINIT_OPT "--no-xinit-opt"
130 { 'm', NULL, NULL, "Set the port to use e.g. -mz80." },
131 { 'p', NULL, NULL, "Select port specific processor e.g. -mpic14 -p16f84" },
132 { 'd', NULL, NULL, NULL },
133 { 'D', NULL, NULL, "Define macro as in -Dmacro" },
134 { 'I', NULL, NULL, "Add to the include (*.h) path, as in -Ipath" },
135 { 'A', NULL, NULL, NULL },
136 { 'U', NULL, NULL, NULL },
137 { 'C', NULL, NULL, "Preprocessor option" },
138 { 'M', NULL, NULL, "Preprocessor option" },
139 { 'V', NULL, &verboseExec, "Execute verbosely. Show sub commands as they are run" },
140 { 'S', NULL, &noAssemble, "Compile only; do not assemble or link" },
141 { 'W', NULL, NULL, "Pass through options to the pre-processor (p), assembler (a) or linker (l)" },
142 { 'L', NULL, NULL, "Add the next field to the library search path" },
143 { 'l', NULL, NULL, "Include the given library in the link" },
144 { 0, OPTION_LARGE_MODEL, NULL, "external data space is used" },
145 { 0, OPTION_MEDIUM_MODEL, NULL, "not supported" },
146 { 0, OPTION_SMALL_MODEL, NULL, "internal data space is used (default)" },
147 { 0, OPTION_FLAT24_MODEL, NULL, "use the flat24 model for the ds390 (default)" },
148 { 0, "--stack-auto", &options.stackAuto, "Stack automatic variables" },
149 { 0, OPTION_STACK_8BIT, NULL, "use the 8bit stack for the ds390 (not supported yet)" },
150 { 0, "--stack-10bit", &options.stack10bit, "use the 10bit stack for ds390 (default)" },
151 { 0, "--xstack", &options.useXstack, "Use external stack" },
152 { 0, OPTION_NO_GCSE, NULL, "Disable the GCSE optimisation" },
153 { 0, OPTION_NO_LOOP_INV, NULL, "Disable optimisation of invariants" },
154 { 0, OPTION_NO_LOOP_IND, NULL, NULL },
155 { 0, "--nojtbound", &optimize.noJTabBoundary, "Don't generate boundary check for jump tables" },
156 { 0, "--noloopreverse", &optimize.noLoopReverse, "Disable the loop reverse optimisation" },
157 { 'c', "--compile-only", &options.cc_only, "Compile and assemble, but do not link" },
158 { 'o', NULL, NULL, "Place the output into the given path resp. file" },
159 { 0, "--dumpraw", &options.dump_raw, "Dump the internal structure after the initial parse" },
160 { 0, "--dumpgcse", &options.dump_gcse, NULL },
161 { 0, "--dumploop", &options.dump_loop, NULL },
162 { 0, "--dumpdeadcode", &options.dump_kill, NULL },
163 { 0, "--dumpliverange", &options.dump_range, NULL },
164 { 0, "--dumpregpack", &options.dump_pack, NULL },
165 { 0, "--dumpregassign", &options.dump_rassgn, NULL },
166 { 0, "--dumptree", &options.dump_tree, "dump front-end AST before generating iCode" },
167 { 0, OPTION_DUMP_ALL, NULL, "Dump the internal structure at all stages" },
168 { 0, OPTION_XRAM_LOC, NULL, "<nnnn> External Ram start location" },
169 { 0, OPTION_IRAM_SIZE, NULL, "<nnnn> Internal Ram size" },
170 { 0, OPTION_XSTACK_LOC, NULL, "<nnnn> External Ram start location" },
171 { 0, OPTION_CODE_LOC, NULL, "<nnnn> Code Segment Location" },
172 { 0, OPTION_STACK_LOC, NULL, "<nnnn> Stack pointer initial value" },
173 { 0, OPTION_DATA_LOC, NULL, "<nnnn> Direct data start location" },
174 { 0, OPTION_IDATA_LOC, NULL, NULL },
175 { 0, OPTION_PEEP_FILE, NULL, "<file> use this extra peep-hole file" },
176 { 0, OPTION_LIB_PATH, NULL, "<path> use this path to search for libraries" },
177 { 0, "--int-long-reent", &options.intlong_rent, "Use reenterant calls on the int and long support functions" },
178 { 0, "--float-reent", &options.float_rent, "Use reenterant calls on the floar support functions" },
179 { 0, OPTION_OUT_FMT_IHX, NULL, NULL },
180 { 0, "--out-fmt-s19", &options.out_fmt, NULL },
181 { 0, "--cyclomatic", &options.cyclomatic, NULL },
182 { 0, "--nooverlay", &options.noOverlay, NULL },
183 { 0, "--main-return", &options.mainreturn, "Issue a return after main()" },
184 { 0, "--xram-movc", &options.xram_movc, "Use movc instead of movx to read xram (xdata)" },
185 { 0, "--no-peep", &options.nopeep, "Disable the peephole assembly file optimisation" },
186 { 0, "--no-reg-params", &options.noRegParams, "On some ports, disable passing some parameters in registers" },
187 { 0, "--peep-asm", &options.asmpeep, NULL },
188 { 0, "--debug", &options.debug, "Enable debugging symbol output" },
189 { 'v', OPTION_VERSION, NULL, "Display sdcc's version" },
190 { 'E', "--preprocessonly", &preProcOnly, "Preprocess only, do not compile" },
191 { 0, "--c1mode", &options.c1mode, "Act in c1 mode. The input is preprocessed code, the output is assembly code." },
192 { 0, "--help", NULL, "Display this help" },
193 { 0, OPTION_CALLEE_SAVES, NULL, "<func[,func,...]> Cause the called function to save registers insted of the caller" },
194 { 0, "--nostdlib", &options.nostdlib, "Do not include the standard library directory in the search path" },
195 { 0, "--nostdinc", &options.nostdinc, "Do not include the standard include directory in the search path" },
196 { 0, "--verbose", &options.verbose, "Trace calls to the preprocessor, assembler, and linker" },
197 { 0, OPTION_LESS_PEDANTIC, NULL, "Disable some of the more pedantic warnings" },
198 { 0, OPTION_SHORT_IS_8BITS, NULL, "Make short 8bits (for old times sake)" },
199 { 0, "--profile", &options.profile, "On supported ports, generate extra profiling information" },
200 { 0, "--fommit-frame-pointer", &options.ommitFramePtr, "Leave out the frame pointer." },
201 { 0, "--all-callee-saves", &options.all_callee_saves, "callee will always save registers used" },
202 { 0, "--use-accelerator", &options.useAccelerator,"generate code for DS390 Arithmetic Accelerator"},
203 { 0, "--stack-probe", &options.stack_probe,"insert call to function __stack_probe at each function prologue"},
204 { 0, "--tini-libid", NULL,"<nnnn> LibraryID used in -mTININative"},
205 { 0, "--protect-sp-update", &options.protect_sp_update,"DS390 - will disable interrupts during ESP:SP updates"},
206 { 0, "--parms-in-bank1", &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
207 { 0, OPTION_NO_XINIT_OPT, &noXinitOpt, "don't memcpy initialized xram from code"},
212 /** Table of all unsupported options and help text to display when one
216 /** shortOpt as in OPTIONS. */
218 /** longOpt as in OPTIONS. */
220 /** Message to display inside W_UNSUPPORTED_OPT when this option
225 static const UNSUPPORTEDOPT
226 unsupportedOptTable[] = {
227 { 'X', NULL, "use --xstack-loc instead" },
228 { 'x', NULL, "use --xstack instead" },
229 { 'i', NULL, "use --idata-loc instead" },
230 { 'r', NULL, "use --xdata-loc instead" },
231 { 's', NULL, "use --code-loc instead" },
232 { 'Y', NULL, "use -I instead" }
235 /** List of all default constant macros.
237 static const char *_baseValues[] = {
238 "cpp", "{bindir}{sep}sdcpp",
240 /* Path seperator character */
241 "sep", DIR_SEPARATOR_STRING,
245 static const char *_preCmd = "{cpp} -nostdinc -Wall -lang-c++ -DSDCC=1 {cppextraopts} {fullsrcfilename} {cppoutfilename}";
249 static PORT *_ports[] =
251 #if !OPT_DISABLE_MCS51
254 #if !OPT_DISABLE_GBZ80
263 #if !OPT_DISABLE_DS390
269 #if !OPT_DISABLE_TININative
272 #if !OPT_DISABLE_XA51
277 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
280 extern void picglue ();
283 /** Sets the port to the one given by the command line option.
284 @param The name minus the option (eg 'mcs51')
285 @return 0 on success.
288 _setPort (const char *name)
291 for (i = 0; i < NUM_PORTS; i++)
293 if (!strcmp (_ports[i]->target, name))
299 /* Error - didnt find */
300 werror (E_UNKNOWN_TARGET, name);
304 /* Override the default processor with the one specified
305 * on the command line */
307 _setProcessor (char *_processor)
309 port->processor = _processor;
310 fprintf(stderr,"Processor: %s\n",_processor);
314 _validatePorts (void)
317 for (i = 0; i < NUM_PORTS; i++)
319 if (_ports[i]->magic != PORT_MAGIC)
321 /* Uncomment this line to debug which port is causing the problem
322 * (the target name is close to the beginning of the port struct
323 * and probably can be accessed just fine). */
324 fprintf(stderr,"%s :",_ports[i]->target);
325 wassertl (0, "Port definition structure is incomplete");
330 /* search through the command line options for the port */
332 _findPort (int argc, char **argv)
338 if (!strncmp (*argv, "-m", 2))
340 _setPort (*argv + 2);
345 /* Use the first in the list */
349 /* search through the command line options for the processor */
351 _findProcessor (int argc, char **argv)
355 if (!strncmp (*argv, "-p", 2))
357 _setProcessor (*argv + 2);
363 /* no error if processor was not specified. */
366 /*-----------------------------------------------------------------*/
367 /* printVersionInfo - prints the version info */
368 /*-----------------------------------------------------------------*/
376 for (i = 0; i < NUM_PORTS; i++)
377 fprintf (stderr, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
379 fprintf (stderr, " %s"
380 #ifdef SDCC_SUB_VERSION_STR
381 "/" SDCC_SUB_VERSION_STR
390 #if defined(_MSC_VER)
403 printOptions(const OPTION *optionsTable)
406 for (i = 0; optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL; i++)
408 fprintf(stdout, " %c%c %-20s %s\n",
409 optionsTable[i].shortOpt !=0 ? '-' : ' ',
410 optionsTable[i].shortOpt !=0 ? optionsTable[i].shortOpt : ' ',
411 optionsTable[i].longOpt != NULL ? optionsTable[i].longOpt : "",
412 optionsTable[i].help != NULL ? optionsTable[i].help : ""
417 /*-----------------------------------------------------------------*/
418 /* printUsage - prints command line syntax */
419 /*-----------------------------------------------------------------*/
426 "Usage : sdcc [options] filename\n"
430 printOptions(optionsTable);
432 for (i = 0; i < NUM_PORTS; i++)
434 if (_ports[i]->poptions != NULL)
436 fprintf (stdout, "\nSpecial options for the %s port:\n", _ports[i]->target);
437 printOptions (_ports[i]->poptions);
444 /*-----------------------------------------------------------------*/
445 /* parseWithComma - separates string with comma */
446 /*-----------------------------------------------------------------*/
448 parseWithComma (char **dest, char *src)
452 strtok (src, "\r\n \t");
453 /* skip the initial white spaces */
454 while (isspace (*src))
471 /*-----------------------------------------------------------------*/
472 /* setDefaultOptions - sets the default options */
473 /*-----------------------------------------------------------------*/
479 for (i = 0; i < 128; i++)
480 preArgv[i] = asmOptions[i] =
481 linkOptions[i] = relFiles[i] = libFiles[i] =
484 /* first the options part */
485 options.stack_loc = 0; /* stack pointer initialised to 0 */
486 options.xstack_loc = 0; /* xternal stack starts at 0 */
487 options.code_loc = 0; /* code starts at 0 */
488 options.data_loc = 0; /* JCF: By default let the linker locate data */
489 options.xdata_loc = 0;
490 options.idata_loc = 0x80;
492 options.model = port->general.default_model;
493 options.nostdlib = 0;
494 options.nostdinc = 0;
496 options.shortis8bits = 0;
498 options.stack10bit=0;
500 /* now for the optimizations */
501 /* turn on the everything */
502 optimize.global_cse = 1;
507 optimize.loopInvariant = 1;
508 optimize.loopInduction = 1;
510 /* now for the ports */
511 port->setDefaultOptions ();
514 /*-----------------------------------------------------------------*/
515 /* processFile - determines the type of file from the extension */
516 /*-----------------------------------------------------------------*/
518 processFile (char *s)
522 /* get the file extension */
523 fext = s + strlen (s);
524 while ((fext != s) && *fext != '.')
527 /* now if no '.' then we don't know what the file type is
528 so give a warning and return */
531 werror (W_UNKNOWN_FEXT, s);
535 /* otherwise depending on the file type */
536 if (strcmp (fext, ".c") == 0 || strcmp (fext, ".C") == 0 || options.c1mode)
538 /* source file name : not if we already have a
542 werror (W_TOO_MANY_SRC, s);
546 /* the only source file */
548 if (!(srcFile = fopen (fullSrcFileName, "r")))
550 werror (E_FILE_OPEN_ERR, s);
554 /* copy the file name into the buffer */
557 /* get rid of the "."-extension */
559 /* is there a dot at all? */
560 if (strrchr (buffer, '.') &&
561 /* is the dot in the filename, not in the path? */
562 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
564 *strrchr (buffer, '.') = '\0';
567 /* get rid of any path information
568 for the module name; */
569 fext = buffer + strlen (buffer);
571 /* do this by going backwards till we
572 get '\' or ':' or start of buffer */
573 while (fext != buffer &&
574 *(fext - 1) != DIR_SEPARATOR_CHAR &&
580 /* do this by going backwards till we
581 get '/' or start of buffer */
582 while (fext != buffer &&
583 *(fext - 1) != DIR_SEPARATOR_CHAR)
588 moduleName = Safe_strdup ( fext );
592 /* if the extention is type .rel or .r or .REL or .R
593 addtional object file will be passed to the linker */
594 if (strcmp (fext, ".r") == 0 || strcmp (fext, ".rel") == 0 ||
595 strcmp (fext, ".R") == 0 || strcmp (fext, ".REL") == 0 ||
596 strcmp (fext, port->linker.rel_ext) == 0)
598 relFiles[nrelFiles++] = s;
602 /* if .lib or .LIB */
603 if (strcmp (fext, ".lib") == 0 || strcmp (fext, ".LIB") == 0)
605 libFiles[nlibFiles++] = s;
609 werror (W_UNKNOWN_FEXT, s);
614 _setModel (int model, const char *sz)
616 if (port->general.supported_models & model)
617 options.model = model;
619 werror (W_UNSUPPORTED_MODEL, sz, port->target);
622 /** Gets the string argument to this option. If the option is '--opt'
623 then for input of '--optxyz' or '--opt xyz' returns xyz.
626 getStringArg(const char *szStart, char **argv, int *pi, int argc)
628 if (argv[*pi][strlen(szStart)])
630 return &argv[*pi][strlen(szStart)];
637 werror (E_ARGUMENT_MISSING, szStart);
638 /* Die here rather than checking for errors later. */
648 /** Gets the integer argument to this option using the same rules as
652 getIntArg(const char *szStart, char **argv, int *pi, int argc)
654 return (int)floatFromVal(constVal(getStringArg(szStart, argv, pi, argc)));
658 verifyShortOption(const char *opt)
660 if (strlen(opt) != 2)
662 werror (W_EXCESS_SHORT_OPTIONS, opt);
667 tryHandleUnsupportedOpt(char **argv, int *pi)
669 if (argv[*pi][0] == '-')
671 const char *longOpt = "";
675 if (argv[*pi][1] == '-')
682 shortOpt = argv[*pi][1];
684 for (i = 0; i < LENGTH(unsupportedOptTable); i++)
686 if (unsupportedOptTable[i].shortOpt == shortOpt ||
687 (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt))) {
688 // Found an unsupported opt.
690 sprintf(buffer, "%s%c%c", longOpt ? longOpt : "", shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
691 werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
695 // Didn't find in the table
700 // Not an option, so can't be unsupported :)
706 scanOptionsTable(const OPTION *optionsTable, char shortOpt, const char *longOpt, char **argv, int *pi)
709 for (i = 0; optionsTable[i].shortOpt != 0 || optionsTable[i].longOpt != NULL; i++)
711 if (optionsTable[i].shortOpt == shortOpt ||
712 (longOpt && optionsTable[i].longOpt &&
713 strcmp(optionsTable[i].longOpt, longOpt) == 0))
716 // If it is a flag then we can handle it here
717 if (optionsTable[i].pparameter != NULL)
719 if (optionsTable[i].shortOpt == shortOpt)
721 verifyShortOption(argv[*pi]);
724 (*optionsTable[i].pparameter)++;
728 // Not a flag. Handled manually later.
733 // Didn't find in the table
738 tryHandleSimpleOpt(char **argv, int *pi)
740 if (argv[*pi][0] == '-')
742 const char *longOpt = "";
745 if (argv[*pi][1] == '-')
752 shortOpt = argv[*pi][1];
755 if (scanOptionsTable(optionsTable, shortOpt, longOpt, argv, pi))
759 else if (port && port->poptions &&
760 scanOptionsTable(port->poptions, shortOpt, longOpt, argv, pi))
771 // Not an option, so can't be handled.
776 /*-----------------------------------------------------------------*/
777 /* parseCmdLine - parses the command line and sets the options */
778 /*-----------------------------------------------------------------*/
780 parseCmdLine (int argc, char **argv)
784 /* go thru all whole command line */
785 for (i = 1; i < argc; i++)
790 if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
795 if (tryHandleSimpleOpt(argv, &i) == TRUE)
801 if (argv[i][0] == '-' && argv[i][1] == '-')
803 if (strcmp (argv[i], OPTION_HELP) == 0)
809 if (strcmp (argv[i], OPTION_STACK_8BIT) == 0)
811 options.stack10bit = 0;
815 if (strcmp (argv[i], OPTION_OUT_FMT_IHX) == 0)
821 if (strcmp (argv[i], OPTION_LARGE_MODEL) == 0)
823 _setModel (MODEL_LARGE, argv[i]);
827 if (strcmp (argv[i], OPTION_MEDIUM_MODEL) == 0)
829 _setModel (MODEL_MEDIUM, argv[i]);
833 if (strcmp (argv[i], OPTION_SMALL_MODEL) == 0)
835 _setModel (MODEL_SMALL, argv[i]);
839 if (strcmp (argv[i], OPTION_FLAT24_MODEL) == 0)
841 _setModel (MODEL_FLAT24, argv[i]);
845 if (strcmp (argv[i], OPTION_DUMP_ALL) == 0)
847 options.dump_rassgn =
853 options.dump_raw = 1;
857 if (strcmp (argv[i], OPTION_PEEP_FILE) == 0)
859 options.peep_file = getStringArg(OPTION_PEEP_FILE, argv, &i, argc);
863 if (strcmp (argv[i], OPTION_LIB_PATH) == 0)
865 libPaths[nlibPaths++] = getStringArg(OPTION_LIB_PATH, argv, &i, argc);
869 if (strcmp (argv[i], OPTION_VERSION) == 0)
876 if (strcmp (argv[i], OPTION_CALLEE_SAVES) == 0)
878 parseWithComma (options.calleeSaves, getStringArg(OPTION_CALLEE_SAVES, argv, &i, argc));
882 if (strcmp (argv[i], OPTION_XSTACK_LOC) == 0)
884 options.xstack_loc = getIntArg(OPTION_XSTACK_LOC, argv, &i, argc);
888 if (strcmp (argv[i], OPTION_STACK_LOC) == 0)
890 options.stack_loc = getIntArg(OPTION_STACK_LOC, argv, &i, argc);
894 if (strcmp (argv[i], OPTION_XRAM_LOC) == 0)
896 options.xdata_loc = getIntArg(OPTION_XRAM_LOC, argv, &i, argc);
900 if (strcmp (argv[i], OPTION_IRAM_SIZE) == 0)
902 options.iram_size = getIntArg(OPTION_IRAM_SIZE, argv, &i, argc);
906 if (strcmp (argv[i], OPTION_DATA_LOC) == 0)
908 options.data_loc = getIntArg(OPTION_DATA_LOC, argv, &i, argc);
912 if (strcmp (argv[i], OPTION_IDATA_LOC) == 0)
914 options.idata_loc = getIntArg(OPTION_IDATA_LOC, argv, &i, argc);
918 if (strcmp (argv[i], OPTION_CODE_LOC) == 0)
920 options.code_loc = getIntArg(OPTION_CODE_LOC, argv, &i, argc);
924 if (strcmp (argv[i], OPTION_NO_GCSE) == 0)
926 optimize.global_cse = 0;
930 if (strcmp (argv[i], OPTION_NO_LOOP_INV) == 0)
932 optimize.loopInvariant = 0;
936 if (strcmp (argv[i], OPTION_NO_LOOP_IND) == 0)
938 optimize.loopInduction = 0;
942 if (strcmp (argv[i], OPTION_LESS_PEDANTIC) == 0)
944 options.lessPedantic = 1;
945 setErrorLogLevel(ERROR_LEVEL_WARNING);
949 if (strcmp (&argv[i][1], OPTION_SHORT_IS_8BITS) == 0)
951 options.shortis8bits=1;
955 if (strcmp (argv[i], OPTION_TINI_LIBID) == 0)
957 options.tini_libid = getIntArg(OPTION_TINI_LIBID, argv, &i, argc);
961 if (!port->parseOption (&argc, argv, &i))
963 werror (W_UNKNOWN_OPTION, argv[i]);
971 /* if preceded by '-' then option */
977 verifyShortOption(argv[i]);
984 /* Used to select the port. But this has already been done. */
988 /* Used to select the processor in port. But this has
989 * already been done. */
993 verifyShortOption(argv[i]);
999 libPaths[nlibPaths++] = getStringArg("-L", argv, &i, argc);
1003 libFiles[nlibFiles++] = getStringArg("-l", argv, &i, argc);
1010 /* copy the file name into the buffer */
1011 strcpy (buffer, getStringArg("-o", argv, &i, argc));
1012 /* point to last character */
1013 p = buffer + strlen (buffer) - 1;
1014 if (*p == DIR_SEPARATOR_CHAR)
1016 /* only output path specified */
1017 dstPath = Safe_strdup (buffer);
1018 fullDstFileName = NULL;
1022 fullDstFileName = Safe_strdup (buffer);
1024 /* get rid of the "."-extension */
1026 /* is there a dot at all? */
1027 if (strrchr (buffer, '.') &&
1028 /* is the dot in the filename, not in the path? */
1029 (strrchr (buffer, DIR_SEPARATOR_CHAR) < strrchr (buffer, '.')))
1030 *strrchr (buffer, '.') = '\0';
1032 dstFileName = Safe_strdup (buffer);
1034 /* strip module name to get path */
1035 p = strrchr (buffer, DIR_SEPARATOR_CHAR);
1038 /* path with trailing / */
1040 dstPath = Safe_strdup (buffer);
1047 /* pre-processer options */
1048 if (argv[i][2] == 'p')
1050 parseWithComma ((char **)preArgv, getStringArg("-Wp", argv, &i, argc));
1052 /* linker options */
1053 else if (argv[i][2] == 'l')
1055 parseWithComma(linkOptions, getStringArg("-Wl", argv, &i, argc));
1057 /* assembler options */
1058 else if (argv[i][2] == 'a')
1060 parseWithComma ((char **) asmOptions, getStringArg("-Wa", argv, &i, argc));
1064 werror (W_UNKNOWN_OPTION, argv[i]);
1069 verifyShortOption(argv[i]);
1071 printVersionInfo ();
1075 /* preprocessor options */
1079 addToList (preArgv, "-M");
1084 addToList (preArgv, "-C");
1093 char sOpt = argv[i][1];
1096 if (argv[i][2] == ' ' || argv[i][2] == '\0')
1102 werror(E_ARGUMENT_MISSING, argv[i-1]);
1116 sprintf (buffer, "-%c%s", sOpt, rest);
1117 addToList (preArgv, buffer);
1122 if (!port->parseOption (&argc, argv, &i))
1123 werror (W_UNKNOWN_OPTION, argv[i]);
1128 if (!port->parseOption (&argc, argv, &i))
1130 /* no option must be a filename */
1131 processFile (argv[i]);
1135 /* if no dstFileName given with -o, we've to find one: */
1138 /* use the modulename from the C-source */
1139 if (fullSrcFileName)
1141 dstFileName = Safe_alloc (strlen (dstPath) + strlen (moduleName) + 1);
1142 strcpy (dstFileName, dstPath);
1143 strcat (dstFileName, moduleName);
1145 /* use the modulename from the first object file */
1146 else if (nrelFiles >= 1)
1150 strcpy (buffer, relFiles[0]);
1151 /* remove extension (it must be .rel) */
1152 *strrchr (buffer, '.') = '\0';
1154 objectName = strrchr (buffer, DIR_SEPARATOR_CHAR);
1161 objectName = buffer;
1163 dstFileName = Safe_alloc (strlen (dstPath) + strlen (objectName) + 1);
1164 strcpy (dstFileName, dstPath);
1165 strcat (dstFileName, objectName);
1167 /* else no module given: help text is displayed */
1170 /* set up external stack location if not explicitly specified */
1171 if (!options.xstack_loc)
1172 options.xstack_loc = options.xdata_loc;
1174 /* if debug option is set the open the cdbFile */
1175 if (options.debug && fullSrcFileName)
1177 sprintf (scratchFileName, "%s.adb", dstFileName); //JCF: Nov 30, 2002
1178 if ((cdbFile = fopen (scratchFileName, "w")) == NULL)
1179 werror (E_FILE_OPEN_ERR, scratchFileName);
1182 /* add a module record */
1183 fprintf (cdbFile, "M:%s\n", moduleName);
1189 /*-----------------------------------------------------------------*/
1190 /* linkEdit : - calls the linkage editor with options */
1191 /*-----------------------------------------------------------------*/
1193 linkEdit (char **envp)
1199 /* first we need to create the <filename>.lnk file */
1200 sprintf (scratchFileName, "%s.lnk", dstFileName);
1201 if (!(lnkfile = fopen (scratchFileName, "w")))
1203 werror (E_FILE_OPEN_ERR, scratchFileName);
1207 /* now write the options. JCF: added option 'y' */
1208 fprintf (lnkfile, "-myux%c\n", (options.out_fmt ? 's' : 'i'));
1210 /* if iram size specified */
1211 if (options.iram_size)
1212 fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
1215 fprintf (lnkfile, "-z\n");
1217 #define WRITE_SEG_LOC(N, L) \
1218 segName = Safe_strdup(N); \
1219 c = strtok(segName, " \t"); \
1220 fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
1221 if (segName) { Safe_free(segName); }
1223 /* code segment start */
1224 WRITE_SEG_LOC (CODE_NAME, options.code_loc);
1226 /* data segment start */
1227 if(options.data_loc){ /*JCF: If zero, the linker chooses the best place for data*/
1228 WRITE_SEG_LOC (DATA_NAME, options.data_loc);
1232 WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
1236 WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
1239 /* bit segment start */
1240 WRITE_SEG_LOC (BIT_NAME, 0);
1242 /* JCF: stack start */
1243 if(options.stack_loc) {
1244 WRITE_SEG_LOC ("SSEG", options.stack_loc & 0xff);
1247 /* add the extra linker options */
1248 for (i = 0; linkOptions[i]; i++)
1249 fprintf (lnkfile, "%s\n", linkOptions[i]);
1251 /* other library paths if specified */
1252 for (i = 0; i < nlibPaths; i++)
1253 fprintf (lnkfile, "-k %s\n", libPaths[i]);
1255 /* standard library path */
1256 if (!options.nostdlib)
1258 switch (options.model)
1274 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1278 mfprintf (lnkfile, getRuntimeVariables(), "-k {libdir}{sep}%s\n", c);
1280 /* standard library files */
1281 #if !OPT_DISABLE_DS390
1282 if (options.model == MODEL_FLAT24)
1284 fprintf (lnkfile, "-l %s\n", STD_DS390_LIB);
1288 #if !OPT_DISABLE_XA51
1290 if (options.model == MODEL_PAGE0)
1292 fprintf (lnkfile, "-l %s\n", STD_XA51_LIB);
1296 fprintf (lnkfile, "-l %s\n", STD_LIB);
1297 fprintf (lnkfile, "-l %s\n", STD_INT_LIB);
1298 fprintf (lnkfile, "-l %s\n", STD_LONG_LIB);
1299 fprintf (lnkfile, "-l %s\n", STD_FP_LIB);
1302 /* additional libraries if any */
1303 for (i = 0; i < nlibFiles; i++)
1304 fprintf (lnkfile, "-l %s\n", libFiles[i]);
1306 /* put in the object files */
1307 if (fullSrcFileName)
1308 fprintf (lnkfile, "%s ", dstFileName);
1310 for (i = 0; i < nrelFiles; i++)
1311 fprintf (lnkfile, "%s\n", relFiles[i]);
1313 fprintf (lnkfile, "\n-e\n");
1316 if (options.verbose)
1317 printf ("sdcc: Calling linker...\n");
1319 /* build linker output filename */
1321 /* -o option overrides default name? */
1322 if (fullDstFileName)
1324 strcpy (scratchFileName, fullDstFileName);
1328 /* the linked file gets the name of the first modul */
1329 if (fullSrcFileName)
1331 strcpy (scratchFileName, dstFileName);
1335 strcpy (scratchFileName, relFiles[0]);
1336 /* strip ".rel" extension */
1337 *strrchr (scratchFileName, '.') = '\0';
1339 strcat (scratchFileName, options.out_fmt ? ".S19" : ".ihx");
1342 if (port->linker.cmd)
1344 char buffer2[PATH_MAX];
1345 buildCmdLine (buffer2, port->linker.cmd, dstFileName, scratchFileName, NULL, NULL);
1346 buildCmdLine2 (buffer, buffer2);
1350 buildCmdLine2 (buffer, port->linker.mcmd);
1353 if (my_system (buffer))
1357 /* TODO: most linker don't have a -o parameter */
1358 /* -o option overrides default name? */
1359 if (fullDstFileName)
1362 /* the linked file gets the name of the first modul */
1363 if (fullSrcFileName)
1365 strcpy (scratchFileName, dstFileName);
1366 p = strlen (scratchFileName) + scratchFileName;
1370 strcpy (scratchFileName, relFiles[0]);
1371 /* strip "rel" extension */
1372 p = strrchr (scratchFileName, '.') + 1;
1374 strcpy (p, options.out_fmt ? "S19" : "ihx");
1375 rename (scratchFileName, fullDstFileName);
1377 q = strrchr (fullDstFileName, '.');
1380 /* point after the '.' of the extension */
1385 /* no extension: append new extensions */
1386 q = strlen (fullDstFileName) + fullDstFileName;
1390 rename (scratchFileName, fullDstFileName);
1393 rename (scratchFileName, fullDstFileName);
1397 /*-----------------------------------------------------------------*/
1398 /* assemble - spawns the assembler with arguments */
1399 /*-----------------------------------------------------------------*/
1401 assemble (char **envp)
1403 /* build assembler output filename */
1405 /* -o option overrides default name? */
1406 if (options.cc_only && fullDstFileName) {
1407 strcpy (scratchFileName, fullDstFileName);
1409 /* the assembled file gets the name of the first modul */
1410 strcpy (scratchFileName, dstFileName);
1411 strcat (scratchFileName, port->linker.rel_ext);
1414 if (port->assembler.do_assemble) {
1415 port->assembler.do_assemble(asmOptions);
1417 } else if (port->assembler.cmd) {
1418 buildCmdLine (buffer, port->assembler.cmd, dstFileName, scratchFileName,
1419 options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
1422 buildCmdLine2 (buffer, port->assembler.mcmd);
1425 if (my_system (buffer)) {
1426 /* either system() or the assembler itself has reported an error
1427 perror ("Cannot exec assembler");
1431 /* TODO: most assembler don't have a -o parameter */
1432 /* -o option overrides default name? */
1433 if (options.cc_only && fullDstFileName) {
1434 strcpy (scratchFileName, dstFileName);
1435 strcat (scratchFileName, port->linker.rel_ext);
1436 rename (scratchFileName, fullDstFileName);
1440 /*-----------------------------------------------------------------*/
1441 /* preProcess - spawns the preprocessor with arguments */
1442 /*-----------------------------------------------------------------*/
1444 preProcess (char **envp)
1448 if (!options.c1mode)
1450 /* if using external stack define the macro */
1451 if (options.useXstack)
1452 addToList (preArgv, "-DSDCC_USE_XSTACK");
1454 /* set the macro for stack autos */
1455 if (options.stackAuto)
1456 addToList (preArgv, "-DSDCC_STACK_AUTO");
1458 /* set the macro for stack autos */
1459 if (options.stack10bit)
1460 addToList (preArgv, "-DSDCC_STACK_TENBIT");
1462 /* set the macro for no overlay */
1463 if (options.noOverlay)
1464 addToList (preArgv, "-DSDCC_NOOVERLAY");
1466 /* set the macro for large model */
1467 switch (options.model)
1470 addToList (preArgv, "-DSDCC_MODEL_LARGE");
1473 addToList (preArgv, "-DSDCC_MODEL_SMALL");
1476 addToList (preArgv, "-DSDCC_MODEL_COMPACT");
1479 addToList (preArgv, "-DSDCC_MODEL_MEDIUM");
1482 addToList (preArgv, "-DSDCC_MODEL_FLAT24");
1485 addToList (preArgv, "-DSDCC_MODEL_PAGE0");
1488 werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
1492 /* add port (processor information to processor */
1493 addToList (preArgv, "-DSDCC_{port}");
1494 addToList (preArgv, "-D__{port}");
1496 /* standard include path */
1497 if (!options.nostdinc) {
1498 addToList (preArgv, "-I{includedir}");
1501 setMainValue ("cppextraopts", join(preArgv));
1505 if (fullDstFileName)
1506 preOutName = Safe_strdup (fullDstFileName);
1509 preOutName = Safe_strdup (tempfilename ());
1511 /* Have to set cppoutfilename to something, even if just pre-processing. */
1512 setMainValue ("cppoutfilename", preOutName ? preOutName : "");
1514 if (options.verbose)
1515 printf ("sdcc: Calling preprocessor...\n");
1517 buildCmdLine2 (buffer, _preCmd);
1519 if (my_system (buffer))
1521 // @FIX: Dario Vecchio 03-05-2001
1524 unlink (preOutName);
1525 Safe_free (preOutName);
1538 preOutName = fullSrcFileName;
1541 yyin = fopen (preOutName, "r");
1544 perror ("Preproc file not found\n");
1552 _setPaths (const char *pprefix)
1555 Given the prefix and how the directories were layed out at
1556 configure time, see if the library and include directories are
1557 where expected. If so, set.
1559 getPathDifference (buffer, PREFIX, SDCC_INCLUDE_DIR);
1560 strcpy (scratchFileName, pprefix);
1561 strcat (scratchFileName, buffer);
1563 if (pathExists (scratchFileName))
1565 setMainValue ("includedir", scratchFileName);
1572 getPathDifference (buffer, PREFIX, SDCC_LIB_DIR);
1573 strcpy (scratchFileName, pprefix);
1574 strcat (scratchFileName, buffer);
1576 if (pathExists (scratchFileName))
1578 setMainValue ("libdir", scratchFileName);
1589 _discoverPaths (const char *argv0)
1592 1. Try the SDCCDIR environment variable.
1593 2. If (1) fails, and if the argv[0] includes a path, attempt to find the include
1594 and library paths with respect to that. Note that under win32
1595 argv[0] is always the full path to the program.
1596 3. If (1) and (2) fail, fall back to the compile time defaults.
1598 Detecting assumes the same layout as when configured. If the
1599 directories have been further moved about then discovery will
1603 /* Some input cases:
1604 "c:\fish\sdcc\bin\sdcc"
1606 "/home/fish/bin/sdcc"
1608 Note that ./sdcc is explicitly not supported as there isn't
1611 /* bindir is handled differently to the lib and include directories.
1612 It's rather unfortunate, but required due to the different
1613 install and development layouts. Logic is different as well.
1616 if (strchr (argv0, DIR_SEPARATOR_CHAR))
1618 strcpy (scratchFileName, argv0);
1619 *strrchr (scratchFileName, DIR_SEPARATOR_CHAR) = '\0';
1620 setMainValue ("bindir", scratchFileName);
1621 ExePathList[0] = Safe_strdup (scratchFileName);
1623 else if (getenv (SDCCDIR_NAME) != NULL)
1625 getPathDifference (buffer, PREFIX, BINDIR);
1626 strcpy (scratchFileName, getenv (SDCCDIR_NAME));
1627 strcat (scratchFileName, buffer);
1628 setMainValue ("bindir", scratchFileName);
1629 ExePathList[0] = Safe_strdup (scratchFileName);
1633 setMainValue ("bindir", BINDIR);
1634 ExePathList[0] = BINDIR;
1640 if (getenv (SDCCDIR_NAME) != NULL)
1642 if (_setPaths (getenv (SDCCDIR_NAME)))
1644 /* Successfully set. */
1649 /* Include and lib weren't where expected. */
1653 if (strchr (argv0, DIR_SEPARATOR_CHAR))
1655 char *pbase = getPrefixFromBinPath (argv0);
1659 /* A bad path. Skip. */
1663 if (_setPaths (pbase))
1665 /* Successfully set. */
1670 /* Include and lib weren't where expected. */
1675 setMainValue ("includedir", SDCC_INCLUDE_DIR);
1676 setMainValue ("libdir", SDCC_LIB_DIR);
1683 populateMainValues (_baseValues);
1684 setMainValue ("port", port->target);
1685 setMainValue ("objext", port->linker.rel_ext);
1686 setMainValue ("asmext", port->assembler.file_ext);
1688 setMainValue ("dstfilename", dstFileName);
1689 setMainValue ("fullsrcfilename", fullSrcFileName ? fullSrcFileName : "fullsrcfilename");
1691 if (options.cc_only && fullDstFileName)
1692 /* compile + assemble and -o given: -o specifies name of object file */
1694 setMainValue ("objdstfilename", fullDstFileName);
1698 setMainValue ("objdstfilename", "{stdobjdstfilename}");
1700 if (fullDstFileName)
1701 /* if we're linking, -o gives the final file name */
1703 setMainValue ("linkdstfilename", fullDstFileName);
1707 setMainValue ("linkdstfilename", "{stdlinkdstfilename}");
1714 * initialises and calls the parser
1718 main (int argc, char **argv, char **envp)
1720 /* turn all optimizations off by default */
1721 memset (&optimize, 0, sizeof (struct optimize));
1723 /*printVersionInfo (); */
1726 fprintf (stderr, "Build error: no ports are enabled.\n");
1730 /* install atexit handler */
1731 atexit(rm_tmpfiles);
1733 /* Before parsing the command line options, do a
1734 * search for the port and processor and initialize
1735 * them if they're found. (We can't gurantee that these
1736 * will be the first options specified).
1739 _findPort (argc, argv);
1742 if (strcmp(port->target, "mcs51") == 0) {
1743 printf("DS390 jammed in A\n");
1749 _findProcessor (argc, argv);
1751 /* Initalise the port. */
1755 // Create a default exe search path from the path to the sdcc command
1758 setDefaultOptions ();
1761 options.model = MODEL_SMALL;
1762 options.stack10bit=0;
1765 parseCmdLine (argc, argv);
1767 /* if no input then printUsage & exit */
1768 if ((!options.c1mode && !fullSrcFileName && !nrelFiles) ||
1769 (options.c1mode && !fullSrcFileName))
1776 _discoverPaths (argv[0]);
1778 /* initMem() is expensive, but
1779 initMem() must called before port->finaliseOptions ().
1780 And the z80 port needs port->finaliseOptions(),
1781 even if we're only linking. */
1783 port->finaliseOptions ();
1785 if (fullSrcFileName)
1795 if (options.verbose)
1796 printf ("sdcc: Generating code...\n");
1801 // @FIX: Dario Vecchio 03-05-2001
1803 if (yyin && yyin != stdin)
1805 unlink (preOutName);
1806 Safe_free (preOutName);
1812 if (TARGET_IS_PIC) {
1813 /* TSD PIC port hack - if the PIC port option is enabled
1814 and SDCC is used to generate PIC code, then we will
1815 generate .asm files in gpasm's format instead of SDCC's
1818 #if !OPT_DISABLE_PIC
1826 if (!options.c1mode && !noAssemble)
1828 if (options.verbose)
1829 printf ("sdcc: Calling assembler...\n");
1839 if (yyin && yyin != stdin)
1842 if (preOutName && !options.c1mode)
1844 unlink (preOutName);
1845 Safe_free (preOutName);
1848 if (!options.cc_only &&
1852 (fullSrcFileName || nrelFiles))
1854 if (port->linker.do_link)
1855 port->linker.do_link ();