1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2013, 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"
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static void Usage (void)
80 /* Print usage information to stderr */
82 printf ("Usage: %s [options] file\n"
84 " -Cl\t\t\t\tMake local variables static\n"
85 " -Dsym[=defn]\t\t\tDefine a symbol\n"
86 " -E\t\t\t\tStop after the preprocessing stage\n"
87 " -I dir\t\t\tSet an include directory search path\n"
88 " -O\t\t\t\tOptimize code\n"
89 " -Oi\t\t\t\tOptimize code, inline more code\n"
90 " -Or\t\t\t\tEnable register variables\n"
91 " -Os\t\t\t\tInline some known functions\n"
92 " -T\t\t\t\tInclude source as comment\n"
93 " -V\t\t\t\tPrint the compiler version number\n"
94 " -W warning[,...]\t\tSuppress warnings\n"
95 " -d\t\t\t\tDebug mode\n"
96 " -g\t\t\t\tAdd debug info to object file\n"
97 " -h\t\t\t\tHelp (this text)\n"
98 " -j\t\t\t\tDefault characters are signed\n"
99 " -mm model\t\t\tSet the memory model\n"
100 " -o name\t\t\tName the output file\n"
101 " -r\t\t\t\tEnable register variables\n"
102 " -t sys\t\t\tSet the target system\n"
103 " -v\t\t\t\tIncrease verbosity\n"
106 " --add-source\t\t\tInclude source as comment\n"
107 " --bss-name seg\t\tSet the name of the BSS segment\n"
108 " --check-stack\t\t\tGenerate stack overflow checks\n"
109 " --code-name seg\t\tSet the name of the CODE segment\n"
110 " --codesize x\t\t\tAccept larger code by factor x\n"
111 " --cpu type\t\t\tSet cpu type (6502, 65c02)\n"
112 " --create-dep name\t\tCreate a make dependency file\n"
113 " --create-full-dep name\tCreate a full make dependency file\n"
114 " --data-name seg\t\tSet the name of the DATA segment\n"
115 " --debug\t\t\tDebug mode\n"
116 " --debug-info\t\t\tAdd debug info to object file\n"
117 " --debug-opt name\t\tDebug optimization steps\n"
118 " --dep-target target\t\tUse this dependency target\n"
119 " --disable-opt name\t\tDisable an optimization step\n"
120 " --enable-opt name\t\tEnable an optimization step\n"
121 " --help\t\t\tHelp (this text)\n"
122 " --include-dir dir\t\tSet an include directory search path\n"
123 " --list-opt-steps\t\tList all optimizer steps and exit\n"
124 " --list-warnings\t\tList available warning types for -W\n"
125 " --local-strings\t\tEmit string literals immediately\n"
126 " --memory-model model\t\tSet the memory model\n"
127 " --register-space b\t\tSet space available for register variables\n"
128 " --register-vars\t\tEnable register variables\n"
129 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
130 " --signed-chars\t\tDefault characters are signed\n"
131 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
132 " --static-locals\t\tMake local variables static\n"
133 " --target sys\t\t\tSet the target system\n"
134 " --verbose\t\t\tIncrease verbosity\n"
135 " --version\t\t\tPrint the compiler version number\n"
136 " --writable-strings\t\tMake string literals writable\n",
142 static void cbmsys (const char* sys)
143 /* Define a CBM system */
145 DefineNumericMacro ("__CBM__", 1);
146 DefineNumericMacro (sys, 1);
151 static void SetSys (const char* Sys)
152 /* Define a target system */
154 switch (Target = FindTarget (Sys)) {
160 AbEnd ("Cannot use `module' as a target for the compiler");
164 DefineNumericMacro ("__ATARI__", 1);
168 DefineNumericMacro ("__ATARI__", 1);
169 DefineNumericMacro ("__ATARIXL__", 1);
181 cbmsys ("__VIC20__");
190 DefineNumericMacro ("__PLUS4__", 1);
194 cbmsys ("__CBM510__");
198 cbmsys ("__CBM610__");
206 DefineNumericMacro ("__BBC__", 1);
210 DefineNumericMacro ("__APPLE2__", 1);
214 DefineNumericMacro ("__APPLE2__", 1);
215 DefineNumericMacro ("__APPLE2ENH__", 1);
219 /* Do not handle as a CBM system */
220 DefineNumericMacro ("__GEOS__", 1);
221 DefineNumericMacro ("__GEOS_CBM__", 1);
225 DefineNumericMacro ("__GEOS__", 1);
226 DefineNumericMacro ("__GEOS_APPLE__", 1);
230 DefineNumericMacro ("__LUNIX__", 1);
234 DefineNumericMacro ("__ATMOS__", 1);
238 DefineNumericMacro ("__NES__", 1);
241 case TGT_SUPERVISION:
242 DefineNumericMacro ("__SUPERVISION__", 1);
246 DefineNumericMacro ("__LYNX__", 1);
250 DefineNumericMacro ("__SIM6502__", 1);
254 DefineNumericMacro ("__SIM65C02__", 1);
258 AbEnd ("Unknown target system type %d", Target);
261 /* Initialize the translation tables for the target system */
267 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
268 /* Handle an option that remembers a file name for later */
270 /* Cannot have the option twice */
271 if (SB_NotEmpty (Name)) {
272 AbEnd ("Cannot use option `%s' twice", Opt);
274 /* Remember the file name for later */
275 SB_CopyStr (Name, Arg);
281 static void DefineSym (const char* Def)
282 /* Define a symbol on the command line */
286 /* The symbol must start with a character or underline */
287 if (Def [0] != '_' && !IsAlpha (Def [0])) {
291 /* Check the symbol name */
292 while (IsAlNum (*P) || *P == '_') {
296 /* Do we have a value given? */
301 /* No value given. Define the macro with the value 1 */
302 DefineNumericMacro (Def, 1);
304 /* We have a value, P points to the '=' character. Since the argument
305 * is const, create a copy and replace the '=' in the copy by a zero
309 unsigned Len = strlen (Def)+1;
310 char* S = (char*) xmalloc (Len);
311 memcpy (S, Def, Len);
315 /* Define this as a macro */
316 DefineTextMacro (S, Q);
318 /* Release the allocated memory */
325 static void CheckSegName (const char* Seg)
326 /* Abort if the given name is not a valid segment name */
328 /* Print an error and abort if the name is not ok */
329 if (!ValidSegName (Seg)) {
330 AbEnd ("Segment name `%s' is invalid", Seg);
336 static void OptAddSource (const char* Opt attribute ((unused)),
337 const char* Arg attribute ((unused)))
338 /* Add source lines as comments in generated assembler file */
345 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
346 /* Handle the --bss-name option */
348 /* Check for a valid name */
352 SetSegName (SEG_BSS, Arg);
357 static void OptCheckStack (const char* Opt attribute ((unused)),
358 const char* Arg attribute ((unused)))
359 /* Handle the --check-stack option */
361 IS_Set (&CheckStack, 1);
366 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
367 /* Handle the --code-name option */
369 /* Check for a valid name */
373 SetSegName (SEG_CODE, Arg);
378 static void OptCodeSize (const char* Opt, const char* Arg)
379 /* Handle the --codesize option */
384 /* Numeric argument expected */
385 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
386 Factor < 10 || Factor > 1000) {
387 AbEnd ("Argument for %s is invalid", Opt);
389 IS_Set (&CodeSizeFactor, Factor);
394 static void OptCreateDep (const char* Opt, const char* Arg)
395 /* Handle the --create-dep option */
397 FileNameOption (Opt, Arg, &DepName);
402 static void OptCreateFullDep (const char* Opt attribute ((unused)),
404 /* Handle the --create-full-dep option */
406 FileNameOption (Opt, Arg, &FullDepName);
411 static void OptCPU (const char* Opt, const char* Arg)
412 /* Handle the --cpu option */
414 /* Find the CPU from the given name */
416 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
417 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
418 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
424 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
425 /* Handle the --data-name option */
427 /* Check for a valid name */
431 SetSegName (SEG_DATA, Arg);
436 static void OptDebug (const char* Opt attribute ((unused)),
437 const char* Arg attribute ((unused)))
438 /* Compiler debug mode */
445 static void OptDebugInfo (const char* Opt attribute ((unused)),
446 const char* Arg attribute ((unused)))
447 /* Add debug info to the object file */
454 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
455 /* Debug optimization steps */
461 FILE* F = fopen (Arg, "r");
463 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
466 /* Read line by line, ignore empty lines and switch optimization
469 while (fgets (Buf, sizeof (Buf), F) != 0) {
471 /* Remove trailing control chars. This will also remove the
474 unsigned Len = strlen (Buf);
475 while (Len > 0 && IsControl (Buf[Len-1])) {
480 /* Get a pointer to the buffer and remove leading white space */
482 while (IsBlank (*Line)) {
486 /* Check the first character and enable/disable the step or
494 /* Empty or comment line */
513 /* Close the file, no error check here since we were just reading and
514 * this is only a debug function.
521 static void OptDebugOptOutput (const char* Opt attribute ((unused)),
522 const char* Arg attribute ((unused)))
523 /* Output optimization steps */
530 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
531 /* Handle the --dep-target option */
533 FileNameOption (Opt, Arg, &DepTarget);
538 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
539 /* Disable an optimization step */
546 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
547 /* Enable an optimization step */
554 static void OptHelp (const char* Opt attribute ((unused)),
555 const char* Arg attribute ((unused)))
556 /* Print usage information and exit */
564 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
565 /* Add an include search path */
567 AddSearchPath (SysIncSearchPath, Arg);
568 AddSearchPath (UsrIncSearchPath, Arg);
573 static void OptListOptSteps (const char* Opt attribute ((unused)),
574 const char* Arg attribute ((unused)))
575 /* List all optimizer steps */
577 /* List the optimizer steps */
578 ListOptSteps (stdout);
586 static void OptListWarnings (const char* Opt attribute ((unused)),
587 const char* Arg attribute ((unused)))
588 /* List all warning types */
590 /* List the warnings */
591 ListWarnings (stdout);
599 static void OptLocalStrings (const char* Opt attribute ((unused)),
600 const char* Arg attribute ((unused)))
601 /* Emit string literals immediately */
603 IS_Set (&LocalStrings, 1);
608 static void OptMemoryModel (const char* Opt, const char* Arg)
609 /* Set the memory model */
613 /* Check the current memory model */
614 if (MemoryModel != MMODEL_UNKNOWN) {
615 AbEnd ("Cannot use option `%s' twice", Opt);
618 /* Translate the memory model name and check it */
619 M = FindMemoryModel (Arg);
620 if (M == MMODEL_UNKNOWN) {
621 AbEnd ("Unknown memory model: %s", Arg);
622 } else if (M == MMODEL_HUGE) {
623 AbEnd ("Unsupported memory model: %s", Arg);
626 /* Set the memory model */
632 static void OptRegisterSpace (const char* Opt, const char* Arg)
633 /* Handle the --register-space option */
635 /* Numeric argument expected */
636 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
637 AbEnd ("Argument for option %s is invalid", Opt);
643 static void OptRegisterVars (const char* Opt attribute ((unused)),
644 const char* Arg attribute ((unused)))
645 /* Handle the --register-vars option */
647 IS_Set (&EnableRegVars, 1);
652 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
653 /* Handle the --rodata-name option */
655 /* Check for a valid name */
659 SetSegName (SEG_RODATA, Arg);
664 static void OptSignedChars (const char* Opt attribute ((unused)),
665 const char* Arg attribute ((unused)))
666 /* Make default characters signed */
668 IS_Set (&SignedChars, 1);
673 static void OptStandard (const char* Opt, const char* Arg)
674 /* Handle the --standard option */
676 /* Find the standard from the given name */
677 standard_t Std = FindStandard (Arg);
678 if (Std == STD_UNKNOWN) {
679 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
680 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
681 AbEnd ("Option %s given more than once", Opt);
683 IS_Set (&Standard, Std);
689 static void OptStaticLocals (const char* Opt attribute ((unused)),
690 const char* Arg attribute ((unused)))
691 /* Place local variables in static storage */
693 IS_Set (&StaticLocals, 1);
698 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
699 /* Set the target system */
706 static void OptVerbose (const char* Opt attribute ((unused)),
707 const char* Arg attribute ((unused)))
708 /* Increase verbosity */
715 static void OptVersion (const char* Opt attribute ((unused)),
716 const char* Arg attribute ((unused)))
717 /* Print the compiler version */
719 fprintf (stderr, "cc65 V%s\n", GetVersionAsString ());
725 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
726 /* Handle the -W option */
728 StrBuf W = AUTO_STRBUF_INITIALIZER;
730 /* Arg is a list of suboptions, separated by commas */
737 /* The suboption may be prefixed with '-' or '+' */
741 } else if (*Arg == '+') {
742 /* This is the default */
746 /* Get the next suboption */
747 Pos = strchr (Arg, ',');
749 SB_CopyBuf (&W, Arg, Pos - Arg);
752 SB_CopyStr (&W, Arg);
757 /* Search for the warning */
758 S = FindWarning (SB_GetConstBuf (&W));
760 InvArg (Opt, SB_GetConstBuf (&W));
765 /* Free allocated memory */
771 static void OptWritableStrings (const char* Opt attribute ((unused)),
772 const char* Arg attribute ((unused)))
773 /* Make string literals writable */
775 IS_Set (&WritableStrings, 1);
780 int main (int argc, char* argv[])
782 /* Program long options */
783 static const LongOpt OptTab[] = {
784 { "--add-source", 0, OptAddSource },
785 { "--bss-name", 1, OptBssName },
786 { "--check-stack", 0, OptCheckStack },
787 { "--code-name", 1, OptCodeName },
788 { "--codesize", 1, OptCodeSize },
789 { "--cpu", 1, OptCPU },
790 { "--create-dep", 1, OptCreateDep },
791 { "--create-full-dep", 1, OptCreateFullDep },
792 { "--data-name", 1, OptDataName },
793 { "--debug", 0, OptDebug },
794 { "--debug-info", 0, OptDebugInfo },
795 { "--debug-opt", 1, OptDebugOpt },
796 { "--debug-opt-output", 0, OptDebugOptOutput },
797 { "--dep-target", 1, OptDepTarget },
798 { "--disable-opt", 1, OptDisableOpt },
799 { "--enable-opt", 1, OptEnableOpt },
800 { "--help", 0, OptHelp },
801 { "--include-dir", 1, OptIncludeDir },
802 { "--list-opt-steps", 0, OptListOptSteps },
803 { "--list-warnings", 0, OptListWarnings },
804 { "--local-strings", 0, OptLocalStrings },
805 { "--memory-model", 1, OptMemoryModel },
806 { "--register-space", 1, OptRegisterSpace },
807 { "--register-vars", 0, OptRegisterVars },
808 { "--rodata-name", 1, OptRodataName },
809 { "--signed-chars", 0, OptSignedChars },
810 { "--standard", 1, OptStandard },
811 { "--static-locals", 0, OptStaticLocals },
812 { "--target", 1, OptTarget },
813 { "--verbose", 0, OptVerbose },
814 { "--version", 0, OptVersion },
815 { "--writable-strings", 0, OptWritableStrings },
820 /* Initialize the input file name */
821 const char* InputFile = 0;
823 /* Initialize the cmdline module */
824 InitCmdLine (&argc, &argv, "cc65");
826 /* Initialize the default segment names */
829 /* Initialize the include search paths */
832 /* Parse the command line */
834 while (I < ArgCount) {
838 /* Get the argument */
839 const char* Arg = ArgVec[I];
841 /* Check for an option */
847 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
860 OptDebugInfo (Arg, 0);
864 OptSignedChars (Arg, 0);
868 SetOutputName (GetArg (&I, 2));
872 OptRegisterVars (Arg, 0);
876 OptTarget (Arg, GetArg (&I, 2));
880 OptCreateDep (Arg, 0);
892 OptStaticLocals (Arg, 0);
902 DefineSym (GetArg (&I, 2));
910 OptIncludeDir (Arg, GetArg (&I, 2));
914 IS_Set (&Optimize, 1);
919 IS_Set (&CodeSizeFactor, 200);
922 IS_Set (&EnableRegVars, 1);
925 IS_Set (&InlineStdFuncs, 1);
932 OptAddSource (Arg, 0);
940 OptWarning (Arg, GetArg (&I, 2));
949 fprintf (stderr, "additional file specs ignored\n");
959 /* Did we have a file spec on the command line? */
960 if (InputFile == 0) {
961 AbEnd ("No input files");
964 /* Add the default include search paths. */
965 FinishIncludePaths ();
967 /* Create the output file name if it was not explicitly given */
968 MakeDefaultOutputName (InputFile);
970 /* If no CPU given, use the default CPU for the target */
971 if (CPU == CPU_UNKNOWN) {
972 if (Target != TGT_UNKNOWN) {
973 CPU = GetTargetProperties (Target)->DefaultCPU;
979 /* If no memory model was given, use the default */
980 if (MemoryModel == MMODEL_UNKNOWN) {
981 SetMemoryModel (MMODEL_NEAR);
984 /* If no language standard was given, use the default one */
985 if (IS_Get (&Standard) == STD_UNKNOWN) {
986 IS_Set (&Standard, STD_DEFAULT);
992 /* Create the output file if we didn't had any errors */
993 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
995 /* Emit literals, externals, do cleanup and optimizations */
1001 /* Write the output to the file */
1003 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
1005 /* Close the file, check for errors */
1008 /* Create dependencies if requested */
1009 CreateDependencies ();
1012 /* Return an apropriate exit code */
1013 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;