1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
46 #include "debugflag.h"
69 /*****************************************************************************/
71 /*****************************************************************************/
75 static void Usage (void)
78 "Usage: %s [options] file\n"
80 " -A\t\t\tStrict ANSI mode\n"
81 " -Cl\t\t\tMake local variables static\n"
82 " -Dsym[=defn]\t\tDefine a symbol\n"
83 " -I dir\t\tSet an include directory search path\n"
84 " -O\t\t\tOptimize code\n"
85 " -Oi\t\t\tOptimize code, inline more code\n"
86 " -Or\t\t\tEnable register variables\n"
87 " -Os\t\t\tInline some known functions\n"
88 " -T\t\t\tInclude source as comment\n"
89 " -V\t\t\tPrint the compiler version number\n"
90 " -W\t\t\tSuppress warnings\n"
91 " -d\t\t\tDebug mode\n"
92 " -g\t\t\tAdd debug info to object file\n"
93 " -h\t\t\tHelp (this text)\n"
94 " -j\t\t\tDefault characters are signed\n"
95 " -o name\t\tName the output file\n"
96 " -r\t\t\tEnable register variables\n"
97 " -t sys\t\tSet the target system\n"
98 " -v\t\t\tIncrease verbosity\n"
101 " --add-source\t\tInclude source as comment\n"
102 " --ansi\t\tStrict ANSI mode\n"
103 " --bss-name seg\tSet the name of the BSS segment\n"
104 " --check-stack\t\tGenerate stack overflow checks\n"
105 " --code-name seg\tSet the name of the CODE segment\n"
106 " --codesize x\t\tAccept larger code by factor x\n"
107 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
108 " --create-dep\t\tCreate a make dependency file\n"
109 " --data-name seg\tSet the name of the DATA segment\n"
110 " --debug\t\tDebug mode\n"
111 " --debug-info\t\tAdd debug info to object file\n"
112 " --debug-opt name\tDebug optimization steps\n"
113 " --disable-opt name\tDisable an optimization step\n"
114 " --enable-opt name\tEnable an optimization step\n"
115 " --forget-inc-paths\tForget include search paths\n"
116 " --help\t\tHelp (this text)\n"
117 " --include-dir dir\tSet an include directory search path\n"
118 " --list-opt-steps\tList all optimizer steps and exit\n"
119 " --register-space b\tSet space available for register variables\n"
120 " --register-vars\tEnable register variables\n"
121 " --rodata-name seg\tSet the name of the RODATA segment\n"
122 " --signed-chars\tDefault characters are signed\n"
123 " --static-locals\tMake local variables static\n"
124 " --target sys\t\tSet the target system\n"
125 " --verbose\t\tIncrease verbosity\n"
126 " --version\t\tPrint the compiler version number\n",
132 static void cbmsys (const char* sys)
133 /* Define a CBM system */
135 DefineNumericMacro ("__CBM__", 1);
136 DefineNumericMacro (sys, 1);
141 static void SetSys (const char* Sys)
142 /* Define a target system */
144 switch (Target = FindTarget (Sys)) {
150 AbEnd ("Cannot use `module' as a target for the compiler");
154 DefineNumericMacro ("__ATARI__", 1);
166 cbmsys ("__VIC20__");
178 cbmsys ("__PLUS4__");
182 cbmsys ("__CBM510__");
186 cbmsys ("__CBM610__");
194 DefineNumericMacro ("__BBC__", 1);
198 DefineNumericMacro ("__APPLE2__", 1);
202 /* Do not handle as a CBM system */
203 DefineNumericMacro ("__GEOS__", 1);
207 DefineNumericMacro ("__LUNIX__", 1);
211 DefineNumericMacro ("__ATMOS__", 1);
215 DefineNumericMacro ("__NES__", 1);
219 AbEnd ("Unknown target system type %d", Target);
222 /* Initialize the translation tables for the target system */
228 static void DoCreateDep (const char* OutputName)
229 /* Create the dependency file */
231 /* Make the dependency file name from the output file name */
232 char* DepName = MakeFilename (OutputName, ".u");
235 FILE* F = fopen (DepName, "w");
237 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
240 /* Write the dependencies to the file */
241 WriteDependencies (F, OutputName);
243 /* Close the file, check for errors */
244 if (fclose (F) != 0) {
246 Fatal ("Cannot write to dependeny file (disk full?)");
255 static void DefineSym (const char* Def)
256 /* Define a symbol on the command line */
260 /* The symbol must start with a character or underline */
261 if (Def [0] != '_' && !IsAlpha (Def [0])) {
265 /* Check the symbol name */
266 while (IsAlNum (*P) || *P == '_') {
270 /* Do we have a value given? */
275 /* No value given. Define the macro with the value 1 */
276 DefineNumericMacro (Def, 1);
278 /* We have a value, P points to the '=' character. Since the argument
279 * is const, create a copy and replace the '=' in the copy by a zero
283 unsigned Len = strlen (Def)+1;
284 char* S = (char*) xmalloc (Len);
285 memcpy (S, Def, Len);
289 /* Define this as a macro */
290 DefineTextMacro (S, Q);
292 /* Release the allocated memory */
299 static void CheckSegName (const char* Seg)
300 /* Abort if the given name is not a valid segment name */
302 /* Print an error and abort if the name is not ok */
303 if (!ValidSegName (Seg)) {
304 AbEnd ("Segment name `%s' is invalid", Seg);
310 static void OptAddSource (const char* Opt attribute ((unused)),
311 const char* Arg attribute ((unused)))
312 /* Add source lines as comments in generated assembler file */
319 static void OptAnsi (const char* Opt attribute ((unused)),
320 const char* Arg attribute ((unused)))
321 /* Compile in strict ANSI mode */
328 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
329 /* Handle the --bss-name option */
331 /* Check for a valid name */
335 NewSegName (SEG_BSS, Arg);
340 static void OptCheckStack (const char* Opt attribute ((unused)),
341 const char* Arg attribute ((unused)))
342 /* Handle the --check-stack option */
349 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
350 /* Handle the --code-name option */
352 /* Check for a valid name */
356 NewSegName (SEG_CODE, Arg);
361 static void OptCodeSize (const char* Opt, const char* Arg)
362 /* Handle the --codesize option */
364 /* Numeric argument expected */
365 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
366 CodeSizeFactor < 100 ||
367 CodeSizeFactor > 1000) {
368 AbEnd ("Argument for %s is invalid", Opt);
374 static void OptCreateDep (const char* Opt attribute ((unused)),
375 const char* Arg attribute ((unused)))
376 /* Handle the --create-dep option */
383 static void OptCPU (const char* Opt, const char* Arg)
384 /* Handle the --cpu option */
386 /* Find the CPU from the given name */
388 if (CPU != CPU_6502 && CPU != CPU_65C02) {
389 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
395 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
396 /* Handle the --data-name option */
398 /* Check for a valid name */
402 NewSegName (SEG_DATA, Arg);
407 static void OptDebug (const char* Opt attribute ((unused)),
408 const char* Arg attribute ((unused)))
409 /* Compiler debug mode */
416 static void OptDebugInfo (const char* Opt attribute ((unused)),
417 const char* Arg attribute ((unused)))
418 /* Add debug info to the object file */
425 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
426 /* Debug optimization steps */
432 FILE* F = fopen (Arg, "r");
434 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
437 /* Read line by line, ignore empty lines and switch optimization
440 while (fgets (Buf, sizeof (Buf), F) != 0) {
442 /* Remove trailing control chars. This will also remove the
445 unsigned Len = strlen (Buf);
446 while (Len > 0 && IsControl (Buf[Len-1])) {
451 /* Get a pointer to the buffer and remove leading white space */
453 while (IsBlank (*Line)) {
457 /* Check the first character and enable/disable the step or
465 /* Empty or comment line */
484 /* Close the file, no error check here since we were just reading and
485 * this is only a debug function.
492 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
493 /* Disable an optimization step */
500 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
501 /* Enable an optimization step */
508 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
509 const char* Arg attribute ((unused)))
510 /* Forget all currently defined include paths */
512 ForgetAllIncludePaths ();
517 static void OptHelp (const char* Opt attribute ((unused)),
518 const char* Arg attribute ((unused)))
519 /* Print usage information and exit */
527 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
528 /* Add an include search path */
530 AddIncludePath (Arg, INC_SYS | INC_USER);
535 static void OptListOptSteps (const char* Opt attribute ((unused)),
536 const char* Arg attribute ((unused)))
537 /* List all optimizer steps */
539 /* List the optimizer steps */
540 ListOptSteps (stdout);
548 static void OptRegisterSpace (const char* Opt, const char* Arg)
549 /* Handle the --register-space option */
551 /* Numeric argument expected */
552 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
553 AbEnd ("Argument for option %s is invalid", Opt);
559 static void OptRegisterVars (const char* Opt attribute ((unused)),
560 const char* Arg attribute ((unused)))
561 /* Handle the --register-vars option */
568 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
569 /* Handle the --rodata-name option */
571 /* Check for a valid name */
575 NewSegName (SEG_RODATA, Arg);
580 static void OptSignedChars (const char* Opt attribute ((unused)),
581 const char* Arg attribute ((unused)))
582 /* Make default characters signed */
589 static void OptStaticLocals (const char* Opt attribute ((unused)),
590 const char* Arg attribute ((unused)))
591 /* Place local variables in static storage */
598 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
599 /* Set the target system */
606 static void OptVerbose (const char* Opt attribute ((unused)),
607 const char* Arg attribute ((unused)))
608 /* Increase verbosity */
615 static void OptVersion (const char* Opt attribute ((unused)),
616 const char* Arg attribute ((unused)))
617 /* Print the assembler version */
621 VER_MAJOR, VER_MINOR, VER_PATCH);
626 int main (int argc, char* argv[])
628 /* Program long options */
629 static const LongOpt OptTab[] = {
630 { "--add-source", 0, OptAddSource },
631 { "--ansi", 0, OptAnsi },
632 { "--bss-name", 1, OptBssName },
633 { "--check-stack", 0, OptCheckStack },
634 { "--code-name", 1, OptCodeName },
635 { "--codesize", 1, OptCodeSize },
636 { "--cpu", 1, OptCPU },
637 { "--create-dep", 0, OptCreateDep },
638 { "--data-name", 1, OptDataName },
639 { "--debug", 0, OptDebug },
640 { "--debug-info", 0, OptDebugInfo },
641 { "--debug-opt", 1, OptDebugOpt },
642 { "--disable-opt", 1, OptDisableOpt },
643 { "--enable-opt", 1, OptEnableOpt },
644 { "--forget-inc-paths", 0, OptForgetIncPaths },
645 { "--help", 0, OptHelp },
646 { "--include-dir", 1, OptIncludeDir },
647 { "--list-opt-steps", 0, OptListOptSteps },
648 { "--register-space", 1, OptRegisterSpace },
649 { "--register-vars", 0, OptRegisterVars },
650 { "--rodata-name", 1, OptRodataName },
651 { "--signed-chars", 0, OptSignedChars },
652 { "--static-locals", 0, OptStaticLocals },
653 { "--target", 1, OptTarget },
654 { "--verbose", 0, OptVerbose },
655 { "--version", 0, OptVersion },
660 /* Initialize the output file name */
661 const char* OutputFile = 0;
662 const char* InputFile = 0;
664 /* Initialize the cmdline module */
665 InitCmdLine (&argc, &argv, "cc65");
667 /* Initialize the default segment names */
670 /* Initialize the include search paths */
673 /* Parse the command line */
675 while (I < ArgCount) {
679 /* Get the argument */
680 const char* Arg = ArgVec[I];
682 /* Check for an option */
683 if (Arg [0] == '-') {
688 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
701 OptDebugInfo (Arg, 0);
705 OptSignedChars (Arg, 0);
709 OutputFile = GetArg (&I, 2);
713 OptRegisterVars (Arg, 0);
717 OptTarget (Arg, GetArg (&I, 2));
721 OptCreateDep (Arg, 0);
737 OptStaticLocals (Arg, 0);
747 DefineSym (GetArg (&I, 2));
751 OptIncludeDir (Arg, GetArg (&I, 2));
760 sscanf (P, "%lx", (long*) &OptDisable);
764 CodeSizeFactor = 200;
777 OptAddSource (Arg, 0);
794 fprintf (stderr, "additional file specs ignored\n");
804 /* Did we have a file spec on the command line? */
805 if (InputFile == 0) {
806 AbEnd ("No input files");
809 /* Create the output file name if it was not explicitly given */
810 if (OutputFile == 0) {
811 OutputFile = MakeFilename (InputFile, ".s");
814 /* If no CPU given, use the default CPU for the target */
815 if (CPU == CPU_UNKNOWN) {
816 if (Target != TGT_UNKNOWN) {
817 CPU = DefaultCPU[Target];
826 /* Create the output file if we didn't had any errors */
827 if (ErrorCount == 0 || Debug) {
830 FILE* F = fopen (OutputFile, "w");
832 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
835 /* Write the output to the file */
838 /* Close the file, check for errors */
839 if (fclose (F) != 0) {
841 Fatal ("Cannot write to output file (disk full?)");
844 /* Create dependencies if requested */
846 DoCreateDep (OutputFile);
851 /* Return an apropriate exit code */
852 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;