1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
45 #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\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 " --help\t\tHelp (this text)\n"
116 " --include-dir dir\tSet an include directory search path\n"
117 " --list-opt-steps\tList all optimizer steps and exit\n"
118 " --register-space b\tSet space available for register variables\n"
119 " --register-vars\tEnable register variables\n"
120 " --rodata-name seg\tSet the name of the RODATA segment\n"
121 " --signed-chars\tDefault characters are signed\n"
122 " --static-locals\tMake local variables static\n"
123 " --target sys\t\tSet the target system\n"
124 " --verbose\t\tIncrease verbosity\n"
125 " --version\t\tPrint the compiler version number\n",
131 static void cbmsys (const char* sys)
132 /* Define a CBM system */
134 DefineNumericMacro ("__CBM__", 1);
135 DefineNumericMacro (sys, 1);
140 static void SetSys (const char* Sys)
141 /* Define a target system */
143 switch (Target = FindTarget (Sys)) {
149 AbEnd ("Cannot use `module' as a target for the compiler");
153 DefineNumericMacro ("__ATARI__", 1);
165 cbmsys ("__VIC20__");
177 cbmsys ("__PLUS4__");
181 cbmsys ("__CBM510__");
185 cbmsys ("__CBM610__");
193 DefineNumericMacro ("__BBC__", 1);
197 DefineNumericMacro ("__APPLE2__", 1);
201 /* Do not handle as a CBM system */
202 DefineNumericMacro ("__GEOS__", 1);
206 DefineNumericMacro ("__LUNIX__", 1);
210 DefineNumericMacro ("__ATMOS__", 1);
214 AbEnd ("Unknown target system type");
217 /* Initialize the translation tables for the target system */
223 static void DoCreateDep (const char* OutputName)
224 /* Create the dependency file */
226 /* Make the dependency file name from the output file name */
227 char* DepName = MakeFilename (OutputName, ".u");
230 FILE* F = fopen (DepName, "w");
232 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
235 /* Write the dependencies to the file */
236 WriteDependencies (F, OutputName);
238 /* Close the file, check for errors */
239 if (fclose (F) != 0) {
241 Fatal ("Cannot write to dependeny file (disk full?)");
250 static void DefineSym (const char* Def)
251 /* Define a symbol on the command line */
255 /* The symbol must start with a character or underline */
256 if (Def [0] != '_' && !IsAlpha (Def [0])) {
260 /* Check the symbol name */
261 while (IsAlNum (*P) || *P == '_') {
265 /* Do we have a value given? */
270 /* No value given. Define the macro with the value 1 */
271 DefineNumericMacro (Def, 1);
273 /* We have a value, P points to the '=' character. Since the argument
274 * is const, create a copy and replace the '=' in the copy by a zero
278 unsigned Len = strlen (Def)+1;
279 char* S = (char*) xmalloc (Len);
280 memcpy (S, Def, Len);
284 /* Define this as a macro */
285 DefineTextMacro (S, Q);
287 /* Release the allocated memory */
294 static void CheckSegName (const char* Seg)
295 /* Abort if the given name is not a valid segment name */
297 /* Print an error and abort if the name is not ok */
298 if (!ValidSegName (Seg)) {
299 AbEnd ("Segment name `%s' is invalid", Seg);
305 static void OptAddSource (const char* Opt attribute ((unused)),
306 const char* Arg attribute ((unused)))
307 /* Add source lines as comments in generated assembler file */
314 static void OptAnsi (const char* Opt attribute ((unused)),
315 const char* Arg attribute ((unused)))
316 /* Compile in strict ANSI mode */
323 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
324 /* Handle the --bss-name option */
326 /* Check for a valid name */
330 NewSegName (SEG_BSS, Arg);
335 static void OptCheckStack (const char* Opt attribute ((unused)),
336 const char* Arg attribute ((unused)))
337 /* Handle the --check-stack option */
344 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
345 /* Handle the --code-name option */
347 /* Check for a valid name */
351 NewSegName (SEG_CODE, Arg);
356 static void OptCodeSize (const char* Opt, const char* Arg)
357 /* Handle the --codesize option */
359 /* Numeric argument expected */
360 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
361 CodeSizeFactor < 100 ||
362 CodeSizeFactor > 1000) {
363 AbEnd ("Argument for %s is invalid", Opt);
369 static void OptCreateDep (const char* Opt attribute ((unused)),
370 const char* Arg attribute ((unused)))
371 /* Handle the --create-dep option */
378 static void OptCPU (const char* Opt, const char* Arg)
379 /* Handle the --cpu option */
381 if (strcmp (Arg, "6502") == 0) {
383 } else if (strcmp (Arg, "65C02") == 0) {
386 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
392 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
393 /* Handle the --data-name option */
395 /* Check for a valid name */
399 NewSegName (SEG_DATA, Arg);
404 static void OptDebug (const char* Opt attribute ((unused)),
405 const char* Arg attribute ((unused)))
406 /* Compiler debug mode */
413 static void OptDebugInfo (const char* Opt attribute ((unused)),
414 const char* Arg attribute ((unused)))
415 /* Add debug info to the object file */
422 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
423 /* Debug optimization steps */
429 FILE* F = fopen (Arg, "r");
431 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
434 /* Read line by line, ignore empty lines and switch optimization
437 while (fgets (Buf, sizeof (Buf), F) != 0) {
439 /* Remove trailing control chars. This will also remove the
442 unsigned Len = strlen (Buf);
443 while (Len > 0 && IsControl (Buf[Len-1])) {
448 /* Get a pointer to the buffer and remove leading white space */
450 while (IsBlank (*Line)) {
454 /* Check the first character and enable/disable the step or
462 /* Empty or comment line */
481 /* Close the file, no error check here since we were just reading and
482 * this is only a debug function.
489 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
490 /* Disable an optimization step */
497 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
498 /* Enable an optimization step */
505 static void OptHelp (const char* Opt attribute ((unused)),
506 const char* Arg attribute ((unused)))
507 /* Print usage information and exit */
515 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
516 /* Add an include search path */
518 AddIncludePath (Arg, INC_SYS | INC_USER);
523 static void OptListOptSteps (const char* Opt attribute ((unused)),
524 const char* Arg attribute ((unused)))
525 /* List all optimizer steps */
527 /* List the optimizer steps */
528 ListOptSteps (stdout);
536 static void OptRegisterSpace (const char* Opt, const char* Arg)
537 /* Handle the --register-space option */
539 /* Numeric argument expected */
540 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
541 AbEnd ("Argument for option %s is invalid", Opt);
547 static void OptRegisterVars (const char* Opt attribute ((unused)),
548 const char* Arg attribute ((unused)))
549 /* Handle the --register-vars option */
556 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
557 /* Handle the --rodata-name option */
559 /* Check for a valid name */
563 NewSegName (SEG_RODATA, Arg);
568 static void OptSignedChars (const char* Opt attribute ((unused)),
569 const char* Arg attribute ((unused)))
570 /* Make default characters signed */
577 static void OptStaticLocals (const char* Opt attribute ((unused)),
578 const char* Arg attribute ((unused)))
579 /* Place local variables in static storage */
586 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
587 /* Set the target system */
594 static void OptVerbose (const char* Opt attribute ((unused)),
595 const char* Arg attribute ((unused)))
596 /* Increase verbosity */
603 static void OptVersion (const char* Opt attribute ((unused)),
604 const char* Arg attribute ((unused)))
605 /* Print the assembler version */
609 VER_MAJOR, VER_MINOR, VER_PATCH);
614 int main (int argc, char* argv[])
616 /* Program long options */
617 static const LongOpt OptTab[] = {
618 { "--add-source", 0, OptAddSource },
619 { "--ansi", 0, OptAnsi },
620 { "--bss-name", 1, OptBssName },
621 { "--check-stack", 0, OptCheckStack },
622 { "--code-name", 1, OptCodeName },
623 { "--codesize", 1, OptCodeSize },
624 { "--cpu", 1, OptCPU },
625 { "--create-dep", 0, OptCreateDep },
626 { "--data-name", 1, OptDataName },
627 { "--debug", 0, OptDebug },
628 { "--debug-info", 0, OptDebugInfo },
629 { "--debug-opt", 1, OptDebugOpt },
630 { "--disable-opt", 1, OptDisableOpt },
631 { "--enable-opt", 1, OptEnableOpt, },
632 { "--help", 0, OptHelp },
633 { "--include-dir", 1, OptIncludeDir },
634 { "--list-opt-steps", 0, OptListOptSteps },
635 { "--register-space", 1, OptRegisterSpace },
636 { "--register-vars", 0, OptRegisterVars },
637 { "--rodata-name", 1, OptRodataName },
638 { "--signed-chars", 0, OptSignedChars },
639 { "--static-locals", 0, OptStaticLocals },
640 { "--target", 1, OptTarget },
641 { "--verbose", 0, OptVerbose },
642 { "--version", 0, OptVersion },
647 /* Initialize the output file name */
648 const char* OutputFile = 0;
649 const char* InputFile = 0;
651 /* Initialize the cmdline module */
652 InitCmdLine (&argc, &argv, "cc65");
654 /* Initialize the default segment names */
657 /* Parse the command line */
659 while (I < ArgCount) {
663 /* Get the argument */
664 const char* Arg = ArgVec[I];
666 /* Check for an option */
667 if (Arg [0] == '-') {
672 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
685 OptDebugInfo (Arg, 0);
689 OptSignedChars (Arg, 0);
693 OutputFile = GetArg (&I, 2);
697 OptRegisterVars (Arg, 0);
701 OptTarget (Arg, GetArg (&I, 2));
705 OptCreateDep (Arg, 0);
721 OptStaticLocals (Arg, 0);
731 DefineSym (GetArg (&I, 2));
735 OptIncludeDir (Arg, GetArg (&I, 2));
744 sscanf (P, "%lx", (long*) &OptDisable);
748 CodeSizeFactor = 200;
761 OptAddSource (Arg, 0);
778 fprintf (stderr, "additional file specs ignored\n");
788 /* Did we have a file spec on the command line? */
789 if (InputFile == 0) {
790 AbEnd ("No input files");
793 /* Create the output file name if it was not explicitly given */
794 if (OutputFile == 0) {
795 OutputFile = MakeFilename (InputFile, ".s");
801 /* Create the output file if we didn't had any errors */
802 if (ErrorCount == 0 || Debug) {
807 /* Optimize the output if requested */
814 F = fopen (OutputFile, "w");
816 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
819 /* Write the output to the file */
822 /* Close the file, check for errors */
823 if (fclose (F) != 0) {
825 Fatal ("Cannot write to output file (disk full?)");
828 /* Create dependencies if requested */
830 DoCreateDep (OutputFile);
835 /* Return an apropriate exit code */
836 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;