1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2004 Ullrich von Bassewitz */
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"
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static void Usage (void)
77 /* Print usage information to stderr */
80 "Usage: %s [options] file\n"
82 " -A\t\t\tStrict ANSI mode\n"
83 " -Cl\t\t\tMake local variables static\n"
84 " -Dsym[=defn]\t\tDefine a symbol\n"
85 " -I dir\t\tSet an include directory search path\n"
86 " -O\t\t\tOptimize code\n"
87 " -Oi\t\t\tOptimize code, inline more code\n"
88 " -Or\t\t\tEnable register variables\n"
89 " -Os\t\t\tInline some known functions\n"
90 " -T\t\t\tInclude source as comment\n"
91 " -V\t\t\tPrint the compiler version number\n"
92 " -W\t\t\tSuppress warnings\n"
93 " -d\t\t\tDebug mode\n"
94 " -g\t\t\tAdd debug info to object file\n"
95 " -h\t\t\tHelp (this text)\n"
96 " -j\t\t\tDefault characters are signed\n"
97 " -mm model\t\tSet the memory model\n"
98 " -o name\t\tName the output file\n"
99 " -r\t\t\tEnable register variables\n"
100 " -t sys\t\tSet the target system\n"
101 " -v\t\t\tIncrease verbosity\n"
104 " --add-source\t\tInclude source as comment\n"
105 " --ansi\t\tStrict ANSI mode\n"
106 " --bss-name seg\tSet the name of the BSS segment\n"
107 " --check-stack\t\tGenerate stack overflow checks\n"
108 " --code-name seg\tSet the name of the CODE segment\n"
109 " --codesize x\t\tAccept larger code by factor x\n"
110 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
111 " --create-dep\t\tCreate a make dependency file\n"
112 " --data-name seg\tSet the name of the DATA segment\n"
113 " --debug\t\tDebug mode\n"
114 " --debug-info\t\tAdd debug info to object file\n"
115 " --debug-opt name\tDebug optimization steps\n"
116 " --disable-opt name\tDisable an optimization step\n"
117 " --enable-opt name\tEnable an optimization step\n"
118 " --forget-inc-paths\tForget include search paths\n"
119 " --help\t\tHelp (this text)\n"
120 " --include-dir dir\tSet an include directory search path\n"
121 " --list-opt-steps\tList all optimizer steps and exit\n"
122 " --memory-model model\tSet the memory model\n"
123 " --register-space b\tSet space available for register variables\n"
124 " --register-vars\tEnable register variables\n"
125 " --rodata-name seg\tSet the name of the RODATA segment\n"
126 " --signed-chars\tDefault characters are signed\n"
127 " --static-locals\tMake local variables static\n"
128 " --target sys\t\tSet the target system\n"
129 " --verbose\t\tIncrease verbosity\n"
130 " --version\t\tPrint the compiler version number\n"
131 " --writable-strings\tMake string literals writable\n",
137 static void cbmsys (const char* sys)
138 /* Define a CBM system */
140 DefineNumericMacro ("__CBM__", 1);
141 DefineNumericMacro (sys, 1);
146 static void SetSys (const char* Sys)
147 /* Define a target system */
149 switch (Target = FindTarget (Sys)) {
155 AbEnd ("Cannot use `module' as a target for the compiler");
159 DefineNumericMacro ("__ATARI__", 1);
171 cbmsys ("__VIC20__");
183 cbmsys ("__PLUS4__");
187 cbmsys ("__CBM510__");
191 cbmsys ("__CBM610__");
199 DefineNumericMacro ("__BBC__", 1);
203 DefineNumericMacro ("__APPLE2__", 1);
207 DefineNumericMacro ("__APPLE2ENH__", 1);
211 /* Do not handle as a CBM system */
212 DefineNumericMacro ("__GEOS__", 1);
216 DefineNumericMacro ("__LUNIX__", 1);
220 DefineNumericMacro ("__ATMOS__", 1);
224 DefineNumericMacro ("__NES__", 1);
227 case TGT_SUPERVISION:
228 DefineNumericMacro ("__SUPERVISION__", 1);
232 AbEnd ("Unknown target system type %d", Target);
235 /* Initialize the translation tables for the target system */
241 static void DoCreateDep (const char* OutputName)
242 /* Create the dependency file */
244 /* Make the dependency file name from the output file name */
245 char* DepName = MakeFilename (OutputName, ".u");
248 FILE* F = fopen (DepName, "w");
250 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
253 /* Write the dependencies to the file */
254 WriteDependencies (F, OutputName);
256 /* Close the file, check for errors */
257 if (fclose (F) != 0) {
259 Fatal ("Cannot write to dependeny file (disk full?)");
268 static void DefineSym (const char* Def)
269 /* Define a symbol on the command line */
273 /* The symbol must start with a character or underline */
274 if (Def [0] != '_' && !IsAlpha (Def [0])) {
278 /* Check the symbol name */
279 while (IsAlNum (*P) || *P == '_') {
283 /* Do we have a value given? */
288 /* No value given. Define the macro with the value 1 */
289 DefineNumericMacro (Def, 1);
291 /* We have a value, P points to the '=' character. Since the argument
292 * is const, create a copy and replace the '=' in the copy by a zero
296 unsigned Len = strlen (Def)+1;
297 char* S = (char*) xmalloc (Len);
298 memcpy (S, Def, Len);
302 /* Define this as a macro */
303 DefineTextMacro (S, Q);
305 /* Release the allocated memory */
312 static void CheckSegName (const char* Seg)
313 /* Abort if the given name is not a valid segment name */
315 /* Print an error and abort if the name is not ok */
316 if (!ValidSegName (Seg)) {
317 AbEnd ("Segment name `%s' is invalid", Seg);
323 static void OptAddSource (const char* Opt attribute ((unused)),
324 const char* Arg attribute ((unused)))
325 /* Add source lines as comments in generated assembler file */
332 static void OptAnsi (const char* Opt attribute ((unused)),
333 const char* Arg attribute ((unused)))
334 /* Compile in strict ANSI mode */
341 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
342 /* Handle the --bss-name option */
344 /* Check for a valid name */
348 SetSegName (SEG_BSS, Arg);
353 static void OptCheckStack (const char* Opt attribute ((unused)),
354 const char* Arg attribute ((unused)))
355 /* Handle the --check-stack option */
357 IS_Set (&CheckStack, 1);
362 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
363 /* Handle the --code-name option */
365 /* Check for a valid name */
369 SetSegName (SEG_CODE, Arg);
374 static void OptCodeSize (const char* Opt, const char* Arg)
375 /* Handle the --codesize option */
377 /* Numeric argument expected */
378 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
379 CodeSizeFactor < 100 ||
380 CodeSizeFactor > 1000) {
381 AbEnd ("Argument for %s is invalid", Opt);
387 static void OptCreateDep (const char* Opt attribute ((unused)),
388 const char* Arg attribute ((unused)))
389 /* Handle the --create-dep option */
396 static void OptCPU (const char* Opt, const char* Arg)
397 /* Handle the --cpu option */
399 /* Find the CPU from the given name */
401 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
402 CPU != CPU_65C02 && CPU != CPU_65816) {
403 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
409 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
410 /* Handle the --data-name option */
412 /* Check for a valid name */
416 SetSegName (SEG_DATA, Arg);
421 static void OptDebug (const char* Opt attribute ((unused)),
422 const char* Arg attribute ((unused)))
423 /* Compiler debug mode */
430 static void OptDebugInfo (const char* Opt attribute ((unused)),
431 const char* Arg attribute ((unused)))
432 /* Add debug info to the object file */
439 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
440 /* Debug optimization steps */
446 FILE* F = fopen (Arg, "r");
448 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
451 /* Read line by line, ignore empty lines and switch optimization
454 while (fgets (Buf, sizeof (Buf), F) != 0) {
456 /* Remove trailing control chars. This will also remove the
459 unsigned Len = strlen (Buf);
460 while (Len > 0 && IsControl (Buf[Len-1])) {
465 /* Get a pointer to the buffer and remove leading white space */
467 while (IsBlank (*Line)) {
471 /* Check the first character and enable/disable the step or
479 /* Empty or comment line */
498 /* Close the file, no error check here since we were just reading and
499 * this is only a debug function.
506 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
507 /* Disable an optimization step */
514 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
515 /* Enable an optimization step */
522 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
523 const char* Arg attribute ((unused)))
524 /* Forget all currently defined include paths */
526 ForgetAllIncludePaths ();
531 static void OptHelp (const char* Opt attribute ((unused)),
532 const char* Arg attribute ((unused)))
533 /* Print usage information and exit */
541 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
542 /* Add an include search path */
544 AddIncludePath (Arg, INC_SYS | INC_USER);
549 static void OptListOptSteps (const char* Opt attribute ((unused)),
550 const char* Arg attribute ((unused)))
551 /* List all optimizer steps */
553 /* List the optimizer steps */
554 ListOptSteps (stdout);
562 static void OptMemoryModel (const char* Opt, const char* Arg)
563 /* Set the memory model */
567 /* Check the current memory model */
568 if (MemoryModel != MMODEL_UNKNOWN) {
569 AbEnd ("Cannot use option `%s' twice", Opt);
572 /* Translate the memory model name and check it */
573 M = FindMemoryModel (Arg);
574 if (M == MMODEL_UNKNOWN) {
575 AbEnd ("Unknown memory model: %s", Arg);
576 } else if (M == MMODEL_HUGE) {
577 AbEnd ("Unsupported memory model: %s", Arg);
580 /* Set the memory model */
586 static void OptRegisterSpace (const char* Opt, const char* Arg)
587 /* Handle the --register-space option */
589 /* Numeric argument expected */
590 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
591 AbEnd ("Argument for option %s is invalid", Opt);
597 static void OptRegisterVars (const char* Opt attribute ((unused)),
598 const char* Arg attribute ((unused)))
599 /* Handle the --register-vars option */
601 IS_Set (&EnableRegVars, 1);
606 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
607 /* Handle the --rodata-name option */
609 /* Check for a valid name */
613 SetSegName (SEG_RODATA, Arg);
618 static void OptSignedChars (const char* Opt attribute ((unused)),
619 const char* Arg attribute ((unused)))
620 /* Make default characters signed */
622 IS_Set (&SignedChars, 1);
627 static void OptStaticLocals (const char* Opt attribute ((unused)),
628 const char* Arg attribute ((unused)))
629 /* Place local variables in static storage */
631 IS_Set (&StaticLocals, 1);
636 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
637 /* Set the target system */
644 static void OptVerbose (const char* Opt attribute ((unused)),
645 const char* Arg attribute ((unused)))
646 /* Increase verbosity */
653 static void OptVersion (const char* Opt attribute ((unused)),
654 const char* Arg attribute ((unused)))
655 /* Print the assembler version */
659 VER_MAJOR, VER_MINOR, VER_PATCH);
664 static void OptWritableStrings (const char* Opt attribute ((unused)),
665 const char* Arg attribute ((unused)))
666 /* Make string literals writable */
668 IS_Set (&WritableStrings, 1);
673 int main (int argc, char* argv[])
675 /* Program long options */
676 static const LongOpt OptTab[] = {
677 { "--add-source", 0, OptAddSource },
678 { "--ansi", 0, OptAnsi },
679 { "--bss-name", 1, OptBssName },
680 { "--check-stack", 0, OptCheckStack },
681 { "--code-name", 1, OptCodeName },
682 { "--codesize", 1, OptCodeSize },
683 { "--cpu", 1, OptCPU },
684 { "--create-dep", 0, OptCreateDep },
685 { "--data-name", 1, OptDataName },
686 { "--debug", 0, OptDebug },
687 { "--debug-info", 0, OptDebugInfo },
688 { "--debug-opt", 1, OptDebugOpt },
689 { "--disable-opt", 1, OptDisableOpt },
690 { "--enable-opt", 1, OptEnableOpt },
691 { "--forget-inc-paths", 0, OptForgetIncPaths },
692 { "--help", 0, OptHelp },
693 { "--include-dir", 1, OptIncludeDir },
694 { "--list-opt-steps", 0, OptListOptSteps },
695 { "--memory-model", 1, OptMemoryModel },
696 { "--register-space", 1, OptRegisterSpace },
697 { "--register-vars", 0, OptRegisterVars },
698 { "--rodata-name", 1, OptRodataName },
699 { "--signed-chars", 0, OptSignedChars },
700 { "--static-locals", 0, OptStaticLocals },
701 { "--target", 1, OptTarget },
702 { "--verbose", 0, OptVerbose },
703 { "--version", 0, OptVersion },
704 { "--writable-strings", 0, OptWritableStrings },
709 /* Initialize the output file name */
710 const char* OutputFile = 0;
711 const char* InputFile = 0;
713 /* Initialize the cmdline module */
714 InitCmdLine (&argc, &argv, "cc65");
716 /* Initialize the default segment names */
719 /* Initialize the include search paths */
722 /* Parse the command line */
724 while (I < ArgCount) {
728 /* Get the argument */
729 const char* Arg = ArgVec[I];
731 /* Check for an option */
732 if (Arg [0] == '-') {
737 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
750 OptDebugInfo (Arg, 0);
754 OptSignedChars (Arg, 0);
758 OutputFile = GetArg (&I, 2);
762 OptRegisterVars (Arg, 0);
766 OptTarget (Arg, GetArg (&I, 2));
770 OptCreateDep (Arg, 0);
786 OptStaticLocals (Arg, 0);
796 DefineSym (GetArg (&I, 2));
800 OptIncludeDir (Arg, GetArg (&I, 2));
809 sscanf (P, "%lx", (long*) &OptDisable);
813 CodeSizeFactor = 200;
816 IS_Set (&EnableRegVars, 1);
819 IS_Set (&InlineStdFuncs, 1);
826 OptAddSource (Arg, 0);
843 fprintf (stderr, "additional file specs ignored\n");
853 /* Did we have a file spec on the command line? */
854 if (InputFile == 0) {
855 AbEnd ("No input files");
858 /* Create the output file name if it was not explicitly given */
859 if (OutputFile == 0) {
860 OutputFile = MakeFilename (InputFile, ".s");
863 /* If no CPU given, use the default CPU for the target */
864 if (CPU == CPU_UNKNOWN) {
865 if (Target != TGT_UNKNOWN) {
866 CPU = DefaultCPU[Target];
872 /* If no memory model was given, use the default */
873 if (MemoryModel == MMODEL_UNKNOWN) {
874 SetMemoryModel (MMODEL_NEAR);
880 /* Create the output file if we didn't had any errors */
881 if (ErrorCount == 0 || Debug) {
884 FILE* F = fopen (OutputFile, "w");
886 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
888 Print (stdout, 1, "Opened output file `%s'\n", OutputFile);
890 /* Write the output to the file */
892 Print (stdout, 1, "Wrote output to `%s'\n", OutputFile);
894 /* Close the file, check for errors */
895 if (fclose (F) != 0) {
897 Fatal ("Cannot write to output file (disk full?)");
899 Print (stdout, 1, "Closed output file `%s'\n", OutputFile);
901 /* Create dependencies if requested */
903 DoCreateDep (OutputFile);
904 Print (stdout, 1, "Creating dependeny file");
909 /* Return an apropriate exit code */
910 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;