1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2010, 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\t\t\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 " --local-strings\t\tEmit string literals immediately\n"
127 " --memory-model model\t\tSet the memory model\n"
128 " --register-space b\t\tSet space available for register variables\n"
129 " --register-vars\t\tEnable register variables\n"
130 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
131 " --signed-chars\t\tDefault characters are signed\n"
132 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
133 " --static-locals\t\tMake local variables static\n"
134 " --target sys\t\t\tSet the target system\n"
135 " --verbose\t\t\tIncrease verbosity\n"
136 " --version\t\t\tPrint the compiler version number\n"
137 " --writable-strings\t\tMake string literals writable\n",
143 static void cbmsys (const char* sys)
144 /* Define a CBM system */
146 DefineNumericMacro ("__CBM__", 1);
147 DefineNumericMacro (sys, 1);
152 static void SetSys (const char* Sys)
153 /* Define a target system */
155 switch (Target = FindTarget (Sys)) {
161 AbEnd ("Cannot use `module' as a target for the compiler");
165 DefineNumericMacro ("__ATARI__", 1);
177 cbmsys ("__VIC20__");
185 cbmsys ("__PLUS4__");
189 cbmsys ("__CBM510__");
193 cbmsys ("__CBM610__");
201 DefineNumericMacro ("__BBC__", 1);
205 DefineNumericMacro ("__APPLE2__", 1);
209 DefineNumericMacro ("__APPLE2ENH__", 1);
213 /* Do not handle as a CBM system */
214 DefineNumericMacro ("__GEOS__", 1);
218 DefineNumericMacro ("__LUNIX__", 1);
222 DefineNumericMacro ("__ATMOS__", 1);
226 DefineNumericMacro ("__NES__", 1);
229 case TGT_SUPERVISION:
230 DefineNumericMacro ("__SUPERVISION__", 1);
234 DefineNumericMacro ("__LYNX__", 1);
238 AbEnd ("Unknown target system type %d", Target);
241 /* Initialize the translation tables for the target system */
247 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
248 /* Handle an option that remembers a file name for later */
250 /* Cannot have the option twice */
251 if (SB_NotEmpty (Name)) {
252 AbEnd ("Cannot use option `%s' twice", Opt);
254 /* Remember the file name for later */
255 SB_CopyStr (Name, Arg);
261 static void DefineSym (const char* Def)
262 /* Define a symbol on the command line */
266 /* The symbol must start with a character or underline */
267 if (Def [0] != '_' && !IsAlpha (Def [0])) {
271 /* Check the symbol name */
272 while (IsAlNum (*P) || *P == '_') {
276 /* Do we have a value given? */
281 /* No value given. Define the macro with the value 1 */
282 DefineNumericMacro (Def, 1);
284 /* We have a value, P points to the '=' character. Since the argument
285 * is const, create a copy and replace the '=' in the copy by a zero
289 unsigned Len = strlen (Def)+1;
290 char* S = (char*) xmalloc (Len);
291 memcpy (S, Def, Len);
295 /* Define this as a macro */
296 DefineTextMacro (S, Q);
298 /* Release the allocated memory */
305 static void CheckSegName (const char* Seg)
306 /* Abort if the given name is not a valid segment name */
308 /* Print an error and abort if the name is not ok */
309 if (!ValidSegName (Seg)) {
310 AbEnd ("Segment name `%s' is invalid", Seg);
316 static void OptAddSource (const char* Opt attribute ((unused)),
317 const char* Arg attribute ((unused)))
318 /* Add source lines as comments in generated assembler file */
325 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
326 /* Handle the --bss-name option */
328 /* Check for a valid name */
332 SetSegName (SEG_BSS, Arg);
337 static void OptCheckStack (const char* Opt attribute ((unused)),
338 const char* Arg attribute ((unused)))
339 /* Handle the --check-stack option */
341 IS_Set (&CheckStack, 1);
346 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
347 /* Handle the --code-name option */
349 /* Check for a valid name */
353 SetSegName (SEG_CODE, Arg);
358 static void OptCodeSize (const char* Opt, const char* Arg)
359 /* Handle the --codesize option */
364 /* Numeric argument expected */
365 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
366 Factor < 10 || Factor > 1000) {
367 AbEnd ("Argument for %s is invalid", Opt);
369 IS_Set (&CodeSizeFactor, Factor);
374 static void OptCreateDep (const char* Opt, const char* Arg)
375 /* Handle the --create-dep option */
377 FileNameOption (Opt, Arg, &DepName);
382 static void OptCreateFullDep (const char* Opt attribute ((unused)),
384 /* Handle the --create-full-dep option */
386 FileNameOption (Opt, Arg, &FullDepName);
391 static void OptCPU (const char* Opt, const char* Arg)
392 /* Handle the --cpu option */
394 /* Find the CPU from the given name */
396 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
397 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
398 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
404 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
405 /* Handle the --data-name option */
407 /* Check for a valid name */
411 SetSegName (SEG_DATA, Arg);
416 static void OptDebug (const char* Opt attribute ((unused)),
417 const char* Arg attribute ((unused)))
418 /* Compiler debug mode */
425 static void OptDebugInfo (const char* Opt attribute ((unused)),
426 const char* Arg attribute ((unused)))
427 /* Add debug info to the object file */
434 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
435 /* Debug optimization steps */
441 FILE* F = fopen (Arg, "r");
443 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
446 /* Read line by line, ignore empty lines and switch optimization
449 while (fgets (Buf, sizeof (Buf), F) != 0) {
451 /* Remove trailing control chars. This will also remove the
454 unsigned Len = strlen (Buf);
455 while (Len > 0 && IsControl (Buf[Len-1])) {
460 /* Get a pointer to the buffer and remove leading white space */
462 while (IsBlank (*Line)) {
466 /* Check the first character and enable/disable the step or
474 /* Empty or comment line */
493 /* Close the file, no error check here since we were just reading and
494 * this is only a debug function.
501 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
502 /* Handle the --dep-target option */
504 FileNameOption (Opt, Arg, &DepTarget);
509 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
510 /* Disable an optimization step */
517 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
518 /* Enable an optimization step */
525 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
526 const char* Arg attribute ((unused)))
527 /* Forget all currently defined include paths */
529 ForgetAllIncludePaths ();
534 static void OptHelp (const char* Opt attribute ((unused)),
535 const char* Arg attribute ((unused)))
536 /* Print usage information and exit */
544 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
545 /* Add an include search path */
547 AddSearchPath (SysIncSearchPath, Arg);
548 AddSearchPath (UsrIncSearchPath, Arg);
553 static void OptListOptSteps (const char* Opt attribute ((unused)),
554 const char* Arg attribute ((unused)))
555 /* List all optimizer steps */
557 /* List the optimizer steps */
558 ListOptSteps (stdout);
566 static void OptLocalStrings (const char* Opt attribute ((unused)),
567 const char* Arg attribute ((unused)))
568 /* Emit string literals immediately */
570 IS_Set (&LocalStrings, 1);
575 static void OptMemoryModel (const char* Opt, const char* Arg)
576 /* Set the memory model */
580 /* Check the current memory model */
581 if (MemoryModel != MMODEL_UNKNOWN) {
582 AbEnd ("Cannot use option `%s' twice", Opt);
585 /* Translate the memory model name and check it */
586 M = FindMemoryModel (Arg);
587 if (M == MMODEL_UNKNOWN) {
588 AbEnd ("Unknown memory model: %s", Arg);
589 } else if (M == MMODEL_HUGE) {
590 AbEnd ("Unsupported memory model: %s", Arg);
593 /* Set the memory model */
599 static void OptRegisterSpace (const char* Opt, const char* Arg)
600 /* Handle the --register-space option */
602 /* Numeric argument expected */
603 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
604 AbEnd ("Argument for option %s is invalid", Opt);
610 static void OptRegisterVars (const char* Opt attribute ((unused)),
611 const char* Arg attribute ((unused)))
612 /* Handle the --register-vars option */
614 IS_Set (&EnableRegVars, 1);
619 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
620 /* Handle the --rodata-name option */
622 /* Check for a valid name */
626 SetSegName (SEG_RODATA, Arg);
631 static void OptSignedChars (const char* Opt attribute ((unused)),
632 const char* Arg attribute ((unused)))
633 /* Make default characters signed */
635 IS_Set (&SignedChars, 1);
640 static void OptStandard (const char* Opt, const char* Arg)
641 /* Handle the --standard option */
643 /* Find the standard from the given name */
644 standard_t Std = FindStandard (Arg);
645 if (Std == STD_UNKNOWN) {
646 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
647 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
648 AbEnd ("Option %s given more than once", Opt);
650 IS_Set (&Standard, Std);
656 static void OptStaticLocals (const char* Opt attribute ((unused)),
657 const char* Arg attribute ((unused)))
658 /* Place local variables in static storage */
660 IS_Set (&StaticLocals, 1);
665 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
666 /* Set the target system */
673 static void OptVerbose (const char* Opt attribute ((unused)),
674 const char* Arg attribute ((unused)))
675 /* Increase verbosity */
682 static void OptVersion (const char* Opt attribute ((unused)),
683 const char* Arg attribute ((unused)))
684 /* Print the compiler version */
687 "cc65 V%s\nSVN version: %s\n",
688 GetVersionAsString (), SVNVersion);
694 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
695 /* Handle the -W option */
697 StrBuf W = AUTO_STRBUF_INITIALIZER;
699 /* Arg is a list of suboptions, separated by commas */
706 /* The suboption may be prefixed with '-' or '+' */
710 } else if (*Arg == '+') {
711 /* This is the default */
715 /* Get the next suboption */
716 Pos = strchr (Arg, ',');
718 SB_CopyBuf (&W, Arg, Pos - Arg);
721 SB_CopyStr (&W, Arg);
726 /* Search for the warning */
727 S = FindWarning (SB_GetConstBuf (&W));
729 InvArg (Opt, SB_GetConstBuf (&W));
734 /* Free allocated memory */
740 static void OptWritableStrings (const char* Opt attribute ((unused)),
741 const char* Arg attribute ((unused)))
742 /* Make string literals writable */
744 IS_Set (&WritableStrings, 1);
749 int main (int argc, char* argv[])
751 /* Program long options */
752 static const LongOpt OptTab[] = {
753 { "--add-source", 0, OptAddSource },
754 { "--bss-name", 1, OptBssName },
755 { "--check-stack", 0, OptCheckStack },
756 { "--code-name", 1, OptCodeName },
757 { "--codesize", 1, OptCodeSize },
758 { "--cpu", 1, OptCPU },
759 { "--create-dep", 1, OptCreateDep },
760 { "--create-full-dep", 1, OptCreateFullDep },
761 { "--data-name", 1, OptDataName },
762 { "--debug", 0, OptDebug },
763 { "--debug-info", 0, OptDebugInfo },
764 { "--debug-opt", 1, OptDebugOpt },
765 { "--dep-target", 1, OptDepTarget },
766 { "--disable-opt", 1, OptDisableOpt },
767 { "--enable-opt", 1, OptEnableOpt },
768 { "--forget-inc-paths", 0, OptForgetIncPaths },
769 { "--help", 0, OptHelp },
770 { "--include-dir", 1, OptIncludeDir },
771 { "--list-opt-steps", 0, OptListOptSteps },
772 { "--local-strings", 0, OptLocalStrings },
773 { "--memory-model", 1, OptMemoryModel },
774 { "--register-space", 1, OptRegisterSpace },
775 { "--register-vars", 0, OptRegisterVars },
776 { "--rodata-name", 1, OptRodataName },
777 { "--signed-chars", 0, OptSignedChars },
778 { "--standard", 1, OptStandard },
779 { "--static-locals", 0, OptStaticLocals },
780 { "--target", 1, OptTarget },
781 { "--verbose", 0, OptVerbose },
782 { "--version", 0, OptVersion },
783 { "--writable-strings", 0, OptWritableStrings },
788 /* Initialize the input file name */
789 const char* InputFile = 0;
791 /* Initialize the cmdline module */
792 InitCmdLine (&argc, &argv, "cc65");
794 /* Initialize the default segment names */
797 /* Initialize the include search paths */
800 /* Parse the command line */
802 while (I < ArgCount) {
806 /* Get the argument */
807 const char* Arg = ArgVec[I];
809 /* Check for an option */
815 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
828 OptDebugInfo (Arg, 0);
832 OptSignedChars (Arg, 0);
836 SetOutputName (GetArg (&I, 2));
840 OptRegisterVars (Arg, 0);
844 OptTarget (Arg, GetArg (&I, 2));
848 OptCreateDep (Arg, 0);
860 OptStaticLocals (Arg, 0);
870 DefineSym (GetArg (&I, 2));
878 OptIncludeDir (Arg, GetArg (&I, 2));
882 IS_Set (&Optimize, 1);
887 IS_Set (&CodeSizeFactor, 200);
890 IS_Set (&EnableRegVars, 1);
893 IS_Set (&InlineStdFuncs, 1);
900 OptAddSource (Arg, 0);
908 OptWarning (Arg, GetArg (&I, 2));
917 fprintf (stderr, "additional file specs ignored\n");
927 /* Did we have a file spec on the command line? */
928 if (InputFile == 0) {
929 AbEnd ("No input files");
932 /* Create the output file name if it was not explicitly given */
933 MakeDefaultOutputName (InputFile);
935 /* If no CPU given, use the default CPU for the target */
936 if (CPU == CPU_UNKNOWN) {
937 if (Target != TGT_UNKNOWN) {
938 CPU = DefaultCPU[Target];
944 /* If no memory model was given, use the default */
945 if (MemoryModel == MMODEL_UNKNOWN) {
946 SetMemoryModel (MMODEL_NEAR);
949 /* If no language standard was given, use the default one */
950 if (IS_Get (&Standard) == STD_UNKNOWN) {
951 IS_Set (&Standard, STD_DEFAULT);
957 /* Create the output file if we didn't had any errors */
958 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
960 /* Emit literals, externals, do cleanup and optimizations */
966 /* Write the output to the file */
968 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
970 /* Close the file, check for errors */
973 /* Create dependencies if requested */
974 CreateDependencies ();
977 /* Return an apropriate exit code */
978 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;