1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 #include "svnversion.h"
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static void Usage (void)
80 /* Print usage information to stderr */
82 printf ("Usage: %s [options] file\n"
84 " -Cl\t\t\tMake local variables static\n"
85 " -Dsym[=defn]\t\tDefine a symbol\n"
86 " -E\t\t\tStop after the preprocessing stage\n"
87 " -I dir\t\tSet an include directory search path\n"
88 " -O\t\t\tOptimize code\n"
89 " -Oi\t\t\tOptimize code, inline more code\n"
90 " -Or\t\t\tEnable register variables\n"
91 " -Os\t\t\tInline some known functions\n"
92 " -T\t\t\tInclude source as comment\n"
93 " -V\t\t\tPrint the compiler version number\n"
94 " -W\t\t\tSuppress warnings\n"
95 " -d\t\t\tDebug mode\n"
96 " -g\t\t\tAdd debug info to object file\n"
97 " -h\t\t\tHelp (this text)\n"
98 " -j\t\t\tDefault characters are signed\n"
99 " -mm model\t\tSet the memory model\n"
100 " -o name\t\tName the output file\n"
101 " -r\t\t\tEnable register variables\n"
102 " -t sys\t\tSet the target system\n"
103 " -v\t\t\tIncrease verbosity\n"
106 " --add-source\t\tInclude source as comment\n"
107 " --bss-name seg\tSet the name of the BSS segment\n"
108 " --check-stack\t\tGenerate stack overflow checks\n"
109 " --code-name seg\tSet the name of the CODE segment\n"
110 " --codesize x\t\tAccept larger code by factor x\n"
111 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
112 " --create-dep\t\tCreate a make dependency file\n"
113 " --data-name seg\tSet the name of the DATA segment\n"
114 " --debug\t\tDebug mode\n"
115 " --debug-info\t\tAdd debug info to object file\n"
116 " --debug-opt name\tDebug optimization steps\n"
117 " --disable-opt name\tDisable an optimization step\n"
118 " --enable-opt name\tEnable an optimization step\n"
119 " --forget-inc-paths\tForget include search paths\n"
120 " --help\t\tHelp (this text)\n"
121 " --include-dir dir\tSet an include directory search path\n"
122 " --list-opt-steps\tList all optimizer steps and exit\n"
123 " --memory-model model\tSet the memory model\n"
124 " --register-space b\tSet space available for register variables\n"
125 " --register-vars\tEnable register variables\n"
126 " --rodata-name seg\tSet the name of the RODATA segment\n"
127 " --signed-chars\tDefault characters are signed\n"
128 " --standard std\tLanguage standard (c89, c99, cc65)\n"
129 " --static-locals\tMake local variables static\n"
130 " --target sys\t\tSet the target system\n"
131 " --verbose\t\tIncrease verbosity\n"
132 " --version\t\tPrint the compiler version number\n"
133 " --writable-strings\tMake string literals writable\n",
139 static void cbmsys (const char* sys)
140 /* Define a CBM system */
142 DefineNumericMacro ("__CBM__", 1);
143 DefineNumericMacro (sys, 1);
148 static void SetSys (const char* Sys)
149 /* Define a target system */
151 switch (Target = FindTarget (Sys)) {
157 AbEnd ("Cannot use `module' as a target for the compiler");
161 DefineNumericMacro ("__ATARI__", 1);
173 cbmsys ("__VIC20__");
181 cbmsys ("__PLUS4__");
185 cbmsys ("__CBM510__");
189 cbmsys ("__CBM610__");
197 DefineNumericMacro ("__BBC__", 1);
201 DefineNumericMacro ("__APPLE2__", 1);
205 DefineNumericMacro ("__APPLE2ENH__", 1);
209 /* Do not handle as a CBM system */
210 DefineNumericMacro ("__GEOS__", 1);
214 DefineNumericMacro ("__LUNIX__", 1);
218 DefineNumericMacro ("__ATMOS__", 1);
222 DefineNumericMacro ("__NES__", 1);
225 case TGT_SUPERVISION:
226 DefineNumericMacro ("__SUPERVISION__", 1);
230 DefineNumericMacro ("__LYNX__", 1);
234 AbEnd ("Unknown target system type %d", Target);
237 /* Initialize the translation tables for the target system */
243 static void DoCreateDep (const char* OutputName)
244 /* Create the dependency file */
246 /* Make the dependency file name from the output file name */
247 char* DepName = MakeFilename (OutputName, ".u");
250 FILE* F = fopen (DepName, "w");
252 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
255 /* Write the dependencies to the file */
256 WriteDependencies (F, OutputName);
258 /* Close the file, check for errors */
259 if (fclose (F) != 0) {
261 Fatal ("Cannot write to dependeny file (disk full?)");
270 static void DefineSym (const char* Def)
271 /* Define a symbol on the command line */
275 /* The symbol must start with a character or underline */
276 if (Def [0] != '_' && !IsAlpha (Def [0])) {
280 /* Check the symbol name */
281 while (IsAlNum (*P) || *P == '_') {
285 /* Do we have a value given? */
290 /* No value given. Define the macro with the value 1 */
291 DefineNumericMacro (Def, 1);
293 /* We have a value, P points to the '=' character. Since the argument
294 * is const, create a copy and replace the '=' in the copy by a zero
298 unsigned Len = strlen (Def)+1;
299 char* S = (char*) xmalloc (Len);
300 memcpy (S, Def, Len);
304 /* Define this as a macro */
305 DefineTextMacro (S, Q);
307 /* Release the allocated memory */
314 static void CheckSegName (const char* Seg)
315 /* Abort if the given name is not a valid segment name */
317 /* Print an error and abort if the name is not ok */
318 if (!ValidSegName (Seg)) {
319 AbEnd ("Segment name `%s' is invalid", Seg);
325 static void OptAddSource (const char* Opt attribute ((unused)),
326 const char* Arg attribute ((unused)))
327 /* Add source lines as comments in generated assembler file */
334 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
335 /* Handle the --bss-name option */
337 /* Check for a valid name */
341 SetSegName (SEG_BSS, Arg);
346 static void OptCheckStack (const char* Opt attribute ((unused)),
347 const char* Arg attribute ((unused)))
348 /* Handle the --check-stack option */
350 IS_Set (&CheckStack, 1);
355 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
356 /* Handle the --code-name option */
358 /* Check for a valid name */
362 SetSegName (SEG_CODE, Arg);
367 static void OptCodeSize (const char* Opt, const char* Arg)
368 /* Handle the --codesize option */
373 /* Numeric argument expected */
374 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
375 Factor < 10 || Factor > 1000) {
376 AbEnd ("Argument for %s is invalid", Opt);
378 IS_Set (&CodeSizeFactor, Factor);
383 static void OptCreateDep (const char* Opt attribute ((unused)),
384 const char* Arg attribute ((unused)))
385 /* Handle the --create-dep option */
392 static void OptCPU (const char* Opt, const char* Arg)
393 /* Handle the --cpu option */
395 /* Find the CPU from the given name */
397 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
398 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
399 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
405 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
406 /* Handle the --data-name option */
408 /* Check for a valid name */
412 SetSegName (SEG_DATA, Arg);
417 static void OptDebug (const char* Opt attribute ((unused)),
418 const char* Arg attribute ((unused)))
419 /* Compiler debug mode */
426 static void OptDebugInfo (const char* Opt attribute ((unused)),
427 const char* Arg attribute ((unused)))
428 /* Add debug info to the object file */
435 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
436 /* Debug optimization steps */
442 FILE* F = fopen (Arg, "r");
444 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
447 /* Read line by line, ignore empty lines and switch optimization
450 while (fgets (Buf, sizeof (Buf), F) != 0) {
452 /* Remove trailing control chars. This will also remove the
455 unsigned Len = strlen (Buf);
456 while (Len > 0 && IsControl (Buf[Len-1])) {
461 /* Get a pointer to the buffer and remove leading white space */
463 while (IsBlank (*Line)) {
467 /* Check the first character and enable/disable the step or
475 /* Empty or comment line */
494 /* Close the file, no error check here since we were just reading and
495 * this is only a debug function.
502 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
503 /* Disable an optimization step */
510 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
511 /* Enable an optimization step */
518 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
519 const char* Arg attribute ((unused)))
520 /* Forget all currently defined include paths */
522 ForgetAllIncludePaths ();
527 static void OptHelp (const char* Opt attribute ((unused)),
528 const char* Arg attribute ((unused)))
529 /* Print usage information and exit */
537 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
538 /* Add an include search path */
540 AddIncludePath (Arg, INC_SYS | INC_USER);
545 static void OptListOptSteps (const char* Opt attribute ((unused)),
546 const char* Arg attribute ((unused)))
547 /* List all optimizer steps */
549 /* List the optimizer steps */
550 ListOptSteps (stdout);
558 static void OptMemoryModel (const char* Opt, const char* Arg)
559 /* Set the memory model */
563 /* Check the current memory model */
564 if (MemoryModel != MMODEL_UNKNOWN) {
565 AbEnd ("Cannot use option `%s' twice", Opt);
568 /* Translate the memory model name and check it */
569 M = FindMemoryModel (Arg);
570 if (M == MMODEL_UNKNOWN) {
571 AbEnd ("Unknown memory model: %s", Arg);
572 } else if (M == MMODEL_HUGE) {
573 AbEnd ("Unsupported memory model: %s", Arg);
576 /* Set the memory model */
582 static void OptRegisterSpace (const char* Opt, const char* Arg)
583 /* Handle the --register-space option */
585 /* Numeric argument expected */
586 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
587 AbEnd ("Argument for option %s is invalid", Opt);
593 static void OptRegisterVars (const char* Opt attribute ((unused)),
594 const char* Arg attribute ((unused)))
595 /* Handle the --register-vars option */
597 IS_Set (&EnableRegVars, 1);
602 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
603 /* Handle the --rodata-name option */
605 /* Check for a valid name */
609 SetSegName (SEG_RODATA, Arg);
614 static void OptSignedChars (const char* Opt attribute ((unused)),
615 const char* Arg attribute ((unused)))
616 /* Make default characters signed */
618 IS_Set (&SignedChars, 1);
623 static void OptStandard (const char* Opt, const char* Arg)
624 /* Handle the --standard option */
626 /* Find the standard from the given name */
627 standard_t Std = FindStandard (Arg);
628 if (Std == STD_UNKNOWN) {
629 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
630 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
631 AbEnd ("Option %s given more than once", Opt);
633 IS_Set (&Standard, Std);
639 static void OptStaticLocals (const char* Opt attribute ((unused)),
640 const char* Arg attribute ((unused)))
641 /* Place local variables in static storage */
643 IS_Set (&StaticLocals, 1);
648 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
649 /* Set the target system */
656 static void OptVerbose (const char* Opt attribute ((unused)),
657 const char* Arg attribute ((unused)))
658 /* Increase verbosity */
665 static void OptVersion (const char* Opt attribute ((unused)),
666 const char* Arg attribute ((unused)))
667 /* Print the compiler version */
670 "cc65 V%s\nSVN version: %s\n",
671 GetVersionAsString (), SVNVersion);
677 static void OptWritableStrings (const char* Opt attribute ((unused)),
678 const char* Arg attribute ((unused)))
679 /* Make string literals writable */
681 IS_Set (&WritableStrings, 1);
686 int main (int argc, char* argv[])
688 /* Program long options */
689 static const LongOpt OptTab[] = {
690 { "--add-source", 0, OptAddSource },
691 { "--bss-name", 1, OptBssName },
692 { "--check-stack", 0, OptCheckStack },
693 { "--code-name", 1, OptCodeName },
694 { "--codesize", 1, OptCodeSize },
695 { "--cpu", 1, OptCPU },
696 { "--create-dep", 0, OptCreateDep },
697 { "--data-name", 1, OptDataName },
698 { "--debug", 0, OptDebug },
699 { "--debug-info", 0, OptDebugInfo },
700 { "--debug-opt", 1, OptDebugOpt },
701 { "--disable-opt", 1, OptDisableOpt },
702 { "--enable-opt", 1, OptEnableOpt },
703 { "--forget-inc-paths", 0, OptForgetIncPaths },
704 { "--help", 0, OptHelp },
705 { "--include-dir", 1, OptIncludeDir },
706 { "--list-opt-steps", 0, OptListOptSteps },
707 { "--memory-model", 1, OptMemoryModel },
708 { "--register-space", 1, OptRegisterSpace },
709 { "--register-vars", 0, OptRegisterVars },
710 { "--rodata-name", 1, OptRodataName },
711 { "--signed-chars", 0, OptSignedChars },
712 { "--standard", 1, OptStandard },
713 { "--static-locals", 0, OptStaticLocals },
714 { "--target", 1, OptTarget },
715 { "--verbose", 0, OptVerbose },
716 { "--version", 0, OptVersion },
717 { "--writable-strings", 0, OptWritableStrings },
722 /* Initialize the input file name */
723 const char* InputFile = 0;
725 /* Initialize the cmdline module */
726 InitCmdLine (&argc, &argv, "cc65");
728 /* Initialize the default segment names */
731 /* Initialize the include search paths */
734 /* Parse the command line */
736 while (I < ArgCount) {
740 /* Get the argument */
741 const char* Arg = ArgVec[I];
743 /* Check for an option */
744 if (Arg [0] == '-') {
749 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
762 OptDebugInfo (Arg, 0);
766 OptSignedChars (Arg, 0);
770 SetOutputName (GetArg (&I, 2));
774 OptRegisterVars (Arg, 0);
778 OptTarget (Arg, GetArg (&I, 2));
782 OptCreateDep (Arg, 0);
794 OptStaticLocals (Arg, 0);
804 DefineSym (GetArg (&I, 2));
812 OptIncludeDir (Arg, GetArg (&I, 2));
816 IS_Set (&Optimize, 1);
821 IS_Set (&CodeSizeFactor, 200);
824 IS_Set (&EnableRegVars, 1);
827 IS_Set (&InlineStdFuncs, 1);
834 OptAddSource (Arg, 0);
842 IS_Set (&WarnDisable, 1);
851 fprintf (stderr, "additional file specs ignored\n");
861 /* Did we have a file spec on the command line? */
862 if (InputFile == 0) {
863 AbEnd ("No input files");
866 /* Create the output file name if it was not explicitly given */
867 MakeDefaultOutputName (InputFile);
869 /* If no CPU given, use the default CPU for the target */
870 if (CPU == CPU_UNKNOWN) {
871 if (Target != TGT_UNKNOWN) {
872 CPU = DefaultCPU[Target];
878 /* If no memory model was given, use the default */
879 if (MemoryModel == MMODEL_UNKNOWN) {
880 SetMemoryModel (MMODEL_NEAR);
883 /* If no language standard was given, use the default one */
884 if (IS_Get (&Standard) == STD_UNKNOWN) {
885 IS_Set (&Standard, STD_DEFAULT);
891 /* Create the output file if we didn't had any errors */
892 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
897 /* Write the output to the file */
899 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
901 /* Close the file, check for errors */
904 /* Create dependencies if requested */
906 DoCreateDep (OutputFilename);
907 Print (stdout, 1, "Creating dependeny file\n");
912 /* Return an apropriate exit code */
913 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;