1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2012, 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"
70 #include "svnversion.h"
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static void Usage (void)
81 /* Print usage information to stderr */
83 printf ("Usage: %s [options] file\n"
85 " -Cl\t\t\t\tMake local variables static\n"
86 " -Dsym[=defn]\t\t\tDefine a symbol\n"
87 " -E\t\t\t\tStop after the preprocessing stage\n"
88 " -I dir\t\t\tSet an include directory search path\n"
89 " -O\t\t\t\tOptimize code\n"
90 " -Oi\t\t\t\tOptimize code, inline more code\n"
91 " -Or\t\t\t\tEnable register variables\n"
92 " -Os\t\t\t\tInline some known functions\n"
93 " -T\t\t\t\tInclude source as comment\n"
94 " -V\t\t\t\tPrint the compiler version number\n"
95 " -W warning[,...]\t\tSuppress warnings\n"
96 " -d\t\t\t\tDebug mode\n"
97 " -g\t\t\t\tAdd debug info to object file\n"
98 " -h\t\t\t\tHelp (this text)\n"
99 " -j\t\t\t\tDefault characters are signed\n"
100 " -mm model\t\t\tSet the memory model\n"
101 " -o name\t\t\tName the output file\n"
102 " -r\t\t\t\tEnable register variables\n"
103 " -t sys\t\t\tSet the target system\n"
104 " -v\t\t\t\tIncrease verbosity\n"
107 " --add-source\t\t\tInclude source as comment\n"
108 " --bss-name seg\t\tSet the name of the BSS segment\n"
109 " --check-stack\t\t\tGenerate stack overflow checks\n"
110 " --code-name seg\t\tSet the name of the CODE segment\n"
111 " --codesize x\t\t\tAccept larger code by factor x\n"
112 " --cpu type\t\t\tSet cpu type (6502, 65c02)\n"
113 " --create-dep name\t\tCreate a make dependency file\n"
114 " --create-full-dep name\tCreate a full make dependency file\n"
115 " --data-name seg\t\tSet the name of the DATA segment\n"
116 " --debug\t\t\tDebug mode\n"
117 " --debug-info\t\t\tAdd debug info to object file\n"
118 " --debug-opt name\t\tDebug optimization steps\n"
119 " --dep-target target\t\tUse this dependency target\n"
120 " --disable-opt name\t\tDisable an optimization step\n"
121 " --enable-opt name\t\tEnable an optimization step\n"
122 " --forget-inc-paths\t\tForget include search paths\n"
123 " --help\t\t\tHelp (this text)\n"
124 " --include-dir dir\t\tSet an include directory search path\n"
125 " --list-opt-steps\t\tList all optimizer steps and exit\n"
126 " --list-warnings\t\tList available warning types for -W\n"
127 " --local-strings\t\tEmit string literals immediately\n"
128 " --memory-model model\t\tSet the memory model\n"
129 " --register-space b\t\tSet space available for register variables\n"
130 " --register-vars\t\tEnable register variables\n"
131 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
132 " --signed-chars\t\tDefault characters are signed\n"
133 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
134 " --static-locals\t\tMake local variables static\n"
135 " --target sys\t\t\tSet the target system\n"
136 " --verbose\t\t\tIncrease verbosity\n"
137 " --version\t\t\tPrint the compiler version number\n"
138 " --writable-strings\t\tMake string literals writable\n",
144 static void cbmsys (const char* sys)
145 /* Define a CBM system */
147 DefineNumericMacro ("__CBM__", 1);
148 DefineNumericMacro (sys, 1);
153 static void SetSys (const char* Sys)
154 /* Define a target system */
156 switch (Target = FindTarget (Sys)) {
162 AbEnd ("Cannot use `module' as a target for the compiler");
166 DefineNumericMacro ("__ATARI__", 1);
178 cbmsys ("__VIC20__");
186 cbmsys ("__PLUS4__");
190 cbmsys ("__CBM510__");
194 cbmsys ("__CBM610__");
202 DefineNumericMacro ("__BBC__", 1);
206 DefineNumericMacro ("__APPLE2__", 1);
210 DefineNumericMacro ("__APPLE2ENH__", 1);
214 /* Do not handle as a CBM system */
215 DefineNumericMacro ("__GEOS__", 1);
216 DefineNumericMacro ("__GEOS_CBM__", 1);
220 DefineNumericMacro ("__GEOS__", 1);
221 DefineNumericMacro ("__GEOS_APPLE__", 1);
225 DefineNumericMacro ("__LUNIX__", 1);
229 DefineNumericMacro ("__ATMOS__", 1);
233 DefineNumericMacro ("__NES__", 1);
236 case TGT_SUPERVISION:
237 DefineNumericMacro ("__SUPERVISION__", 1);
241 DefineNumericMacro ("__LYNX__", 1);
245 AbEnd ("Unknown target system type %d", Target);
248 /* Initialize the translation tables for the target system */
254 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
255 /* Handle an option that remembers a file name for later */
257 /* Cannot have the option twice */
258 if (SB_NotEmpty (Name)) {
259 AbEnd ("Cannot use option `%s' twice", Opt);
261 /* Remember the file name for later */
262 SB_CopyStr (Name, Arg);
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 OptBssName (const char* Opt attribute ((unused)), const char* Arg)
333 /* Handle the --bss-name option */
335 /* Check for a valid name */
339 SetSegName (SEG_BSS, Arg);
344 static void OptCheckStack (const char* Opt attribute ((unused)),
345 const char* Arg attribute ((unused)))
346 /* Handle the --check-stack option */
348 IS_Set (&CheckStack, 1);
353 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
354 /* Handle the --code-name option */
356 /* Check for a valid name */
360 SetSegName (SEG_CODE, Arg);
365 static void OptCodeSize (const char* Opt, const char* Arg)
366 /* Handle the --codesize option */
371 /* Numeric argument expected */
372 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
373 Factor < 10 || Factor > 1000) {
374 AbEnd ("Argument for %s is invalid", Opt);
376 IS_Set (&CodeSizeFactor, Factor);
381 static void OptCreateDep (const char* Opt, const char* Arg)
382 /* Handle the --create-dep option */
384 FileNameOption (Opt, Arg, &DepName);
389 static void OptCreateFullDep (const char* Opt attribute ((unused)),
391 /* Handle the --create-full-dep option */
393 FileNameOption (Opt, Arg, &FullDepName);
398 static void OptCPU (const char* Opt, const char* Arg)
399 /* Handle the --cpu option */
401 /* Find the CPU from the given name */
403 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
404 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
405 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
411 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
412 /* Handle the --data-name option */
414 /* Check for a valid name */
418 SetSegName (SEG_DATA, Arg);
423 static void OptDebug (const char* Opt attribute ((unused)),
424 const char* Arg attribute ((unused)))
425 /* Compiler debug mode */
432 static void OptDebugInfo (const char* Opt attribute ((unused)),
433 const char* Arg attribute ((unused)))
434 /* Add debug info to the object file */
441 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
442 /* Debug optimization steps */
448 FILE* F = fopen (Arg, "r");
450 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
453 /* Read line by line, ignore empty lines and switch optimization
456 while (fgets (Buf, sizeof (Buf), F) != 0) {
458 /* Remove trailing control chars. This will also remove the
461 unsigned Len = strlen (Buf);
462 while (Len > 0 && IsControl (Buf[Len-1])) {
467 /* Get a pointer to the buffer and remove leading white space */
469 while (IsBlank (*Line)) {
473 /* Check the first character and enable/disable the step or
481 /* Empty or comment line */
500 /* Close the file, no error check here since we were just reading and
501 * this is only a debug function.
508 static void OptDebugOptOutput (const char* Opt attribute ((unused)),
509 const char* Arg attribute ((unused)))
510 /* Output optimization steps */
517 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
518 /* Handle the --dep-target option */
520 FileNameOption (Opt, Arg, &DepTarget);
525 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
526 /* Disable an optimization step */
533 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
534 /* Enable an optimization step */
541 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
542 const char* Arg attribute ((unused)))
543 /* Forget all currently defined include paths */
545 ForgetAllIncludePaths ();
550 static void OptHelp (const char* Opt attribute ((unused)),
551 const char* Arg attribute ((unused)))
552 /* Print usage information and exit */
560 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
561 /* Add an include search path */
563 AddSearchPath (SysIncSearchPath, Arg);
564 AddSearchPath (UsrIncSearchPath, Arg);
569 static void OptListOptSteps (const char* Opt attribute ((unused)),
570 const char* Arg attribute ((unused)))
571 /* List all optimizer steps */
573 /* List the optimizer steps */
574 ListOptSteps (stdout);
582 static void OptListWarnings (const char* Opt attribute ((unused)),
583 const char* Arg attribute ((unused)))
584 /* List all warning types */
586 /* List the warnings */
587 ListWarnings (stdout);
595 static void OptLocalStrings (const char* Opt attribute ((unused)),
596 const char* Arg attribute ((unused)))
597 /* Emit string literals immediately */
599 IS_Set (&LocalStrings, 1);
604 static void OptMemoryModel (const char* Opt, const char* Arg)
605 /* Set the memory model */
609 /* Check the current memory model */
610 if (MemoryModel != MMODEL_UNKNOWN) {
611 AbEnd ("Cannot use option `%s' twice", Opt);
614 /* Translate the memory model name and check it */
615 M = FindMemoryModel (Arg);
616 if (M == MMODEL_UNKNOWN) {
617 AbEnd ("Unknown memory model: %s", Arg);
618 } else if (M == MMODEL_HUGE) {
619 AbEnd ("Unsupported memory model: %s", Arg);
622 /* Set the memory model */
628 static void OptRegisterSpace (const char* Opt, const char* Arg)
629 /* Handle the --register-space option */
631 /* Numeric argument expected */
632 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
633 AbEnd ("Argument for option %s is invalid", Opt);
639 static void OptRegisterVars (const char* Opt attribute ((unused)),
640 const char* Arg attribute ((unused)))
641 /* Handle the --register-vars option */
643 IS_Set (&EnableRegVars, 1);
648 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
649 /* Handle the --rodata-name option */
651 /* Check for a valid name */
655 SetSegName (SEG_RODATA, Arg);
660 static void OptSignedChars (const char* Opt attribute ((unused)),
661 const char* Arg attribute ((unused)))
662 /* Make default characters signed */
664 IS_Set (&SignedChars, 1);
669 static void OptStandard (const char* Opt, const char* Arg)
670 /* Handle the --standard option */
672 /* Find the standard from the given name */
673 standard_t Std = FindStandard (Arg);
674 if (Std == STD_UNKNOWN) {
675 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
676 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
677 AbEnd ("Option %s given more than once", Opt);
679 IS_Set (&Standard, Std);
685 static void OptStaticLocals (const char* Opt attribute ((unused)),
686 const char* Arg attribute ((unused)))
687 /* Place local variables in static storage */
689 IS_Set (&StaticLocals, 1);
694 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
695 /* Set the target system */
702 static void OptVerbose (const char* Opt attribute ((unused)),
703 const char* Arg attribute ((unused)))
704 /* Increase verbosity */
711 static void OptVersion (const char* Opt attribute ((unused)),
712 const char* Arg attribute ((unused)))
713 /* Print the compiler version */
716 "cc65 V%s\nSVN version: %s\n",
717 GetVersionAsString (), SVNVersion);
723 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
724 /* Handle the -W option */
726 StrBuf W = AUTO_STRBUF_INITIALIZER;
728 /* Arg is a list of suboptions, separated by commas */
735 /* The suboption may be prefixed with '-' or '+' */
739 } else if (*Arg == '+') {
740 /* This is the default */
744 /* Get the next suboption */
745 Pos = strchr (Arg, ',');
747 SB_CopyBuf (&W, Arg, Pos - Arg);
750 SB_CopyStr (&W, Arg);
755 /* Search for the warning */
756 S = FindWarning (SB_GetConstBuf (&W));
758 InvArg (Opt, SB_GetConstBuf (&W));
763 /* Free allocated memory */
769 static void OptWritableStrings (const char* Opt attribute ((unused)),
770 const char* Arg attribute ((unused)))
771 /* Make string literals writable */
773 IS_Set (&WritableStrings, 1);
778 int main (int argc, char* argv[])
780 /* Program long options */
781 static const LongOpt OptTab[] = {
782 { "--add-source", 0, OptAddSource },
783 { "--bss-name", 1, OptBssName },
784 { "--check-stack", 0, OptCheckStack },
785 { "--code-name", 1, OptCodeName },
786 { "--codesize", 1, OptCodeSize },
787 { "--cpu", 1, OptCPU },
788 { "--create-dep", 1, OptCreateDep },
789 { "--create-full-dep", 1, OptCreateFullDep },
790 { "--data-name", 1, OptDataName },
791 { "--debug", 0, OptDebug },
792 { "--debug-info", 0, OptDebugInfo },
793 { "--debug-opt", 1, OptDebugOpt },
794 { "--debug-opt-output", 0, OptDebugOptOutput },
795 { "--dep-target", 1, OptDepTarget },
796 { "--disable-opt", 1, OptDisableOpt },
797 { "--enable-opt", 1, OptEnableOpt },
798 { "--forget-inc-paths", 0, OptForgetIncPaths },
799 { "--help", 0, OptHelp },
800 { "--include-dir", 1, OptIncludeDir },
801 { "--list-opt-steps", 0, OptListOptSteps },
802 { "--list-warnings", 0, OptListWarnings },
803 { "--local-strings", 0, OptLocalStrings },
804 { "--memory-model", 1, OptMemoryModel },
805 { "--register-space", 1, OptRegisterSpace },
806 { "--register-vars", 0, OptRegisterVars },
807 { "--rodata-name", 1, OptRodataName },
808 { "--signed-chars", 0, OptSignedChars },
809 { "--standard", 1, OptStandard },
810 { "--static-locals", 0, OptStaticLocals },
811 { "--target", 1, OptTarget },
812 { "--verbose", 0, OptVerbose },
813 { "--version", 0, OptVersion },
814 { "--writable-strings", 0, OptWritableStrings },
819 /* Initialize the input file name */
820 const char* InputFile = 0;
822 /* Initialize the cmdline module */
823 InitCmdLine (&argc, &argv, "cc65");
825 /* Initialize the default segment names */
828 /* Initialize the include search paths */
831 /* Parse the command line */
833 while (I < ArgCount) {
837 /* Get the argument */
838 const char* Arg = ArgVec[I];
840 /* Check for an option */
846 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
859 OptDebugInfo (Arg, 0);
863 OptSignedChars (Arg, 0);
867 SetOutputName (GetArg (&I, 2));
871 OptRegisterVars (Arg, 0);
875 OptTarget (Arg, GetArg (&I, 2));
879 OptCreateDep (Arg, 0);
891 OptStaticLocals (Arg, 0);
901 DefineSym (GetArg (&I, 2));
909 OptIncludeDir (Arg, GetArg (&I, 2));
913 IS_Set (&Optimize, 1);
918 IS_Set (&CodeSizeFactor, 200);
921 IS_Set (&EnableRegVars, 1);
924 IS_Set (&InlineStdFuncs, 1);
931 OptAddSource (Arg, 0);
939 OptWarning (Arg, GetArg (&I, 2));
948 fprintf (stderr, "additional file specs ignored\n");
958 /* Did we have a file spec on the command line? */
959 if (InputFile == 0) {
960 AbEnd ("No input files");
963 /* Create the output file name if it was not explicitly given */
964 MakeDefaultOutputName (InputFile);
966 /* If no CPU given, use the default CPU for the target */
967 if (CPU == CPU_UNKNOWN) {
968 if (Target != TGT_UNKNOWN) {
969 CPU = GetTargetProperties (Target)->DefaultCPU;
975 /* If no memory model was given, use the default */
976 if (MemoryModel == MMODEL_UNKNOWN) {
977 SetMemoryModel (MMODEL_NEAR);
980 /* If no language standard was given, use the default one */
981 if (IS_Get (&Standard) == STD_UNKNOWN) {
982 IS_Set (&Standard, STD_DEFAULT);
988 /* Create the output file if we didn't had any errors */
989 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
991 /* Emit literals, externals, do cleanup and optimizations */
997 /* Write the output to the file */
999 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
1001 /* Close the file, check for errors */
1004 /* Create dependencies if requested */
1005 CreateDependencies ();
1008 /* Return an apropriate exit code */
1009 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;