1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void Usage (void)
76 "Usage: %s [options] file\n"
78 " -d\t\t\tDebug mode\n"
79 " -g\t\t\tAdd debug info to object file\n"
80 " -h\t\t\tHelp (this text)\n"
81 " -j\t\t\tDefault characters are signed\n"
82 " -o name\t\tName the output file\n"
83 " -t sys\t\tSet the target system\n"
84 " -v\t\t\tIncrease verbosity\n"
85 " -A\t\t\tStrict ANSI mode\n"
86 " -Cl\t\t\tMake local variables static\n"
87 " -Dsym[=defn]\t\tDefine a symbol\n"
88 " -I dir\t\tSet an include directory search path\n"
89 " -O\t\t\tOptimize code\n"
90 " -Oi\t\t\tOptimize code, inline more code\n"
91 " -Or\t\t\tEnable register variables\n"
92 " -Os\t\t\tInline some known functions\n"
93 " -T\t\t\tInclude source as comment\n"
94 " -V\t\t\tPrint the compiler version number\n"
95 " -W\t\t\tSuppress warnings\n"
98 " --ansi\t\tStrict ANSI mode\n"
99 " --bss-name seg\tSet the name of the BSS segment\n"
100 " --check-stack\t\tGenerate stack overflow checks\n"
101 " --code-name seg\tSet the name of the CODE segment\n"
102 " --cpu type\t\tSet cpu type\n"
103 " --data-name seg\tSet the name of the DATA segment\n"
104 " --debug\t\tDebug mode\n"
105 " --debug-info\t\tAdd debug info to object file\n"
106 " --help\t\tHelp (this text)\n"
107 " --include-dir dir\tSet an include directory search path\n"
108 " --rodata-name seg\tSet the name of the RODATA segment\n"
109 " --signed-chars\tDefault characters are signed\n"
110 " --static-locals\tMake local variables static\n"
111 " --target sys\t\tSet the target system\n"
112 " --verbose\t\tIncrease verbosity\n"
113 " --version\t\tPrint the compiler version number\n",
119 static void cbmsys (const char* sys)
120 /* Define a CBM system */
122 AddNumericMacro ("__CBM__", 1);
123 AddNumericMacro (sys, 1);
128 static void SetSys (const char* Sys)
129 /* Define a target system */
131 switch (Target = FindTarget (Sys)) {
137 AddNumericMacro ("__ATARI__", 1);
153 cbmsys ("__PLUS4__");
157 cbmsys ("__CBM610__");
165 AddNumericMacro ("__BBC__", 1);
169 AddNumericMacro ("__APPLE2__", 1);
173 /* Do not handle as a CBM system */
174 AddNumericMacro ("__GEOS__", 1);
178 AbEnd ("Unknown target system type");
181 /* Initialize the translation tables for the target system */
187 static void DoCreateDep (const char* OutputName)
188 /* Create the dependency file */
190 /* Make the dependency file name from the output file name */
191 char* DepName = MakeFilename (OutputName, ".u");
194 FILE* F = fopen (DepName, "w");
196 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
199 /* Write the dependencies to the file */
200 WriteDependencies (F, OutputName);
202 /* Close the file, check for errors */
203 if (fclose (F) != 0) {
205 Fatal ("Cannot write to dependeny file (disk full?)");
214 static void DefineSym (const char* Def)
215 /* Define a symbol on the command line */
219 /* The symbol must start with a character or underline */
220 if (Def [0] != '_' && !IsAlpha (Def [0])) {
224 /* Check the symbol name */
225 while (IsAlNum (*P) || *P == '_') {
229 /* Do we have a value given? */
234 /* No value given. Define the macro with the value 1 */
235 AddNumericMacro (Def, 1);
237 /* We have a value, P points to the '=' character. Since the argument
238 * is const, create a copy and replace the '=' in the copy by a zero
242 unsigned Len = strlen (Def)+1;
243 char* S = xmalloc (Len);
244 memcpy (S, Def, Len);
248 /* Define this as a macro */
251 /* Release the allocated memory */
258 static void CheckSegName (const char* Seg)
259 /* Abort if the given name is not a valid segment name */
261 /* Print an error and abort if the name is not ok */
262 if (!ValidSegName (Seg)) {
263 AbEnd ("Segment name `%s' is invalid", Seg);
269 static void OptAddSource (const char* Opt, const char* Arg)
270 /* Add source lines as comments in generated assembler file */
277 static void OptAnsi (const char* Opt, const char* Arg)
278 /* Compile in strict ANSI mode */
285 static void OptBssName (const char* Opt, const char* Arg)
286 /* Handle the --bss-name option */
288 /* Check for a valid name */
292 NewSegName (SEG_BSS, Arg);
297 static void OptCheckStack (const char* Opt, const char* Arg)
298 /* Handle the --check-stack option */
305 static void OptCodeName (const char* Opt, const char* Arg)
306 /* Handle the --code-name option */
308 /* Check for a valid name */
312 NewSegName (SEG_CODE, Arg);
317 static void OptCreateDep (const char* Opt, const char* Arg)
318 /* Handle the --create-dep option */
325 static void OptCPU (const char* Opt, const char* Arg)
326 /* Handle the --cpu option */
328 if (strcmp (Arg, "6502") == 0) {
330 } else if (strcmp (Arg, "65C02") == 0) {
333 AbEnd ("Invalid CPU: `%s'", Arg);
339 static void OptDataName (const char* Opt, const char* Arg)
340 /* Handle the --code-name option */
342 /* Check for a valid name */
346 NewSegName (SEG_DATA, Arg);
351 static void OptDebug (const char* Opt, const char* Arg)
352 /* Compiler debug mode */
359 static void OptDebugInfo (const char* Opt, const char* Arg)
360 /* Add debug info to the object file */
367 static void OptHelp (const char* Opt, const char* Arg)
368 /* Print usage information and exit */
376 static void OptIncludeDir (const char* Opt, const char* Arg)
377 /* Add an include search path */
379 AddIncludePath (Arg, INC_SYS | INC_USER);
384 static void OptRodataName (const char* Opt, const char* Arg)
385 /* Handle the --rodata-name option */
387 /* Check for a valid name */
391 NewSegName (SEG_RODATA, Arg);
396 static void OptSignedChars (const char* Opt, const char* Arg)
397 /* Make default characters signed */
404 static void OptStaticLocals (const char* Opt, const char* Arg)
405 /* Place local variables in static storage */
412 static void OptTarget (const char* Opt, const char* Arg)
413 /* Set the target system */
420 static void OptVerbose (const char* Opt, const char* Arg)
421 /* Increase verbosity */
428 static void OptVersion (const char* Opt, const char* Arg)
429 /* Print the assembler version */
433 VER_MAJOR, VER_MINOR, VER_PATCH);
438 int main (int argc, char* argv[])
440 /* Program long options */
441 static const LongOpt OptTab[] = {
442 { "--add-source", 0, OptAddSource },
443 { "--ansi", 0, OptAnsi },
444 { "--bss-name", 1, OptBssName },
445 { "--check-stack", 0, OptCheckStack },
446 { "--code-name", 1, OptCodeName },
447 { "--create-dep", 0, OptCreateDep },
448 { "--cpu", 1, OptCPU },
449 { "--data-name", 1, OptDataName },
450 { "--debug", 0, OptDebug },
451 { "--debug-info", 0, OptDebugInfo },
452 { "--help", 0, OptHelp },
453 { "--include-dir", 1, OptIncludeDir },
454 { "--rodata-name", 1, OptRodataName },
455 { "--signed-chars", 0, OptSignedChars },
456 { "--static-locals", 0, OptStaticLocals },
457 { "--target", 1, OptTarget },
458 { "--verbose", 0, OptVerbose },
459 { "--version", 0, OptVersion },
464 /* Initialize the output file name */
465 const char* OutputFile = 0;
466 const char* InputFile = 0;
468 /* Initialize the cmdline module */
469 InitCmdLine (&argc, &argv, "cc65");
471 /* Initialize the default segment names */
474 /* Parse the command line */
476 while (I < ArgCount) {
480 /* Get the argument */
481 const char* Arg = ArgVec[I];
483 /* Check for an option */
484 if (Arg [0] == '-') {
489 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
502 OptDebugInfo (Arg, 0);
506 OptSignedChars (Arg, 0);
510 OutputFile = GetArg (&I, 2);
514 OptTarget (Arg, GetArg (&I, 2));
518 OptCreateDep (Arg, 0);
534 OptStaticLocals (Arg, 0);
544 DefineSym (GetArg (&I, 2));
548 OptIncludeDir (Arg, GetArg (&I, 2));
557 sscanf (P, "%lx", (long*) &OptDisable);
573 OptAddSource (Arg, 0);
590 fprintf (stderr, "additional file specs ignored\n");
600 /* Did we have a file spec on the command line? */
601 if (InputFile == 0) {
602 AbEnd ("No input files");
605 /* Open the input file */
606 OpenMainFile (InputFile);
608 /* Create the output file name if it was not explicitly given */
609 if (OutputFile == 0) {
610 OutputFile = MakeFilename (InputFile, ".s");
616 /* Create the output file if we didn't had any errors */
617 if (ErrorCount == 0 || Debug) {
621 /* Optimize the output if requested */
627 F = fopen (OutputFile, "w");
629 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
632 /* Write the output to the file */
635 /* Close the file, check for errors */
636 if (fclose (F) != 0) {
638 Fatal ("Cannot write to output file (disk full?)");
641 /* Create dependencies if requested */
643 DoCreateDep (OutputFile);
648 /* Return an apropriate exit code */
649 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;