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 ("__APPLE2__", 1);
210 DefineNumericMacro ("__APPLE2__", 1);
211 DefineNumericMacro ("__APPLE2ENH__", 1);
215 DefineNumericMacro ("__ATMOS__", 1);
219 DefineNumericMacro ("__BBC__", 1);
222 case TGT_CREATIVISION:
223 DefineNumericMacro ("__CREATIVISION__", 1);
227 DefineNumericMacro ("__GEOS__", 1);
228 DefineNumericMacro ("__GEOS_APPLE__", 1);
232 /* Do not handle as a CBM system */
233 DefineNumericMacro ("__GEOS__", 1);
234 DefineNumericMacro ("__GEOS_CBM__", 1);
238 DefineNumericMacro ("__LUNIX__", 1);
242 DefineNumericMacro ("__LYNX__", 1);
246 DefineNumericMacro ("__NES__", 1);
250 DefineNumericMacro ("__SIM6502__", 1);
254 DefineNumericMacro ("__SIM65C02__", 1);
257 case TGT_SUPERVISION:
258 DefineNumericMacro ("__SUPERVISION__", 1);
262 AbEnd ("Unknown target system type %d", Target);
265 /* Initialize the translation tables for the target system */
271 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
272 /* Handle an option that remembers a file name for later */
274 /* Cannot have the option twice */
275 if (SB_NotEmpty (Name)) {
276 AbEnd ("Cannot use option `%s' twice", Opt);
278 /* Remember the file name for later */
279 SB_CopyStr (Name, Arg);
285 static void DefineSym (const char* Def)
286 /* Define a symbol on the command line */
290 /* The symbol must start with a character or underline */
291 if (Def [0] != '_' && !IsAlpha (Def [0])) {
295 /* Check the symbol name */
296 while (IsAlNum (*P) || *P == '_') {
300 /* Do we have a value given? */
305 /* No value given. Define the macro with the value 1 */
306 DefineNumericMacro (Def, 1);
308 /* We have a value, P points to the '=' character. Since the argument
309 * is const, create a copy and replace the '=' in the copy by a zero
313 unsigned Len = strlen (Def)+1;
314 char* S = (char*) xmalloc (Len);
315 memcpy (S, Def, Len);
319 /* Define this as a macro */
320 DefineTextMacro (S, Q);
322 /* Release the allocated memory */
329 static void CheckSegName (const char* Seg)
330 /* Abort if the given name is not a valid segment name */
332 /* Print an error and abort if the name is not ok */
333 if (!ValidSegName (Seg)) {
334 AbEnd ("Segment name `%s' is invalid", Seg);
340 static void OptAddSource (const char* Opt attribute ((unused)),
341 const char* Arg attribute ((unused)))
342 /* Add source lines as comments in generated assembler file */
349 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
350 /* Handle the --bss-name option */
352 /* Check for a valid name */
356 SetSegName (SEG_BSS, Arg);
361 static void OptCheckStack (const char* Opt attribute ((unused)),
362 const char* Arg attribute ((unused)))
363 /* Handle the --check-stack option */
365 IS_Set (&CheckStack, 1);
370 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
371 /* Handle the --code-name option */
373 /* Check for a valid name */
377 SetSegName (SEG_CODE, Arg);
382 static void OptCodeSize (const char* Opt, const char* Arg)
383 /* Handle the --codesize option */
388 /* Numeric argument expected */
389 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
390 Factor < 10 || Factor > 1000) {
391 AbEnd ("Argument for %s is invalid", Opt);
393 IS_Set (&CodeSizeFactor, Factor);
398 static void OptCreateDep (const char* Opt, const char* Arg)
399 /* Handle the --create-dep option */
401 FileNameOption (Opt, Arg, &DepName);
406 static void OptCreateFullDep (const char* Opt attribute ((unused)),
408 /* Handle the --create-full-dep option */
410 FileNameOption (Opt, Arg, &FullDepName);
415 static void OptCPU (const char* Opt, const char* Arg)
416 /* Handle the --cpu option */
418 /* Find the CPU from the given name */
420 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
421 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
422 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
428 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
429 /* Handle the --data-name option */
431 /* Check for a valid name */
435 SetSegName (SEG_DATA, Arg);
440 static void OptDebug (const char* Opt attribute ((unused)),
441 const char* Arg attribute ((unused)))
442 /* Compiler debug mode */
449 static void OptDebugInfo (const char* Opt attribute ((unused)),
450 const char* Arg attribute ((unused)))
451 /* Add debug info to the object file */
458 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
459 /* Debug optimization steps */
465 FILE* F = fopen (Arg, "r");
467 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
470 /* Read line by line, ignore empty lines and switch optimization
473 while (fgets (Buf, sizeof (Buf), F) != 0) {
475 /* Remove trailing control chars. This will also remove the
478 unsigned Len = strlen (Buf);
479 while (Len > 0 && IsControl (Buf[Len-1])) {
484 /* Get a pointer to the buffer and remove leading white space */
486 while (IsBlank (*Line)) {
490 /* Check the first character and enable/disable the step or
498 /* Empty or comment line */
517 /* Close the file, no error check here since we were just reading and
518 * this is only a debug function.
525 static void OptDebugOptOutput (const char* Opt attribute ((unused)),
526 const char* Arg attribute ((unused)))
527 /* Output optimization steps */
534 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
535 /* Handle the --dep-target option */
537 FileNameOption (Opt, Arg, &DepTarget);
542 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
543 /* Disable an optimization step */
550 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
551 /* Enable an optimization step */
558 static void OptHelp (const char* Opt attribute ((unused)),
559 const char* Arg attribute ((unused)))
560 /* Print usage information and exit */
568 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
569 /* Add an include search path */
571 AddSearchPath (SysIncSearchPath, Arg);
572 AddSearchPath (UsrIncSearchPath, Arg);
577 static void OptListOptSteps (const char* Opt attribute ((unused)),
578 const char* Arg attribute ((unused)))
579 /* List all optimizer steps */
581 /* List the optimizer steps */
582 ListOptSteps (stdout);
590 static void OptListWarnings (const char* Opt attribute ((unused)),
591 const char* Arg attribute ((unused)))
592 /* List all warning types */
594 /* List the warnings */
595 ListWarnings (stdout);
603 static void OptLocalStrings (const char* Opt attribute ((unused)),
604 const char* Arg attribute ((unused)))
605 /* Emit string literals immediately */
607 IS_Set (&LocalStrings, 1);
612 static void OptMemoryModel (const char* Opt, const char* Arg)
613 /* Set the memory model */
617 /* Check the current memory model */
618 if (MemoryModel != MMODEL_UNKNOWN) {
619 AbEnd ("Cannot use option `%s' twice", Opt);
622 /* Translate the memory model name and check it */
623 M = FindMemoryModel (Arg);
624 if (M == MMODEL_UNKNOWN) {
625 AbEnd ("Unknown memory model: %s", Arg);
626 } else if (M == MMODEL_HUGE) {
627 AbEnd ("Unsupported memory model: %s", Arg);
630 /* Set the memory model */
636 static void OptRegisterSpace (const char* Opt, const char* Arg)
637 /* Handle the --register-space option */
639 /* Numeric argument expected */
640 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
641 AbEnd ("Argument for option %s is invalid", Opt);
647 static void OptRegisterVars (const char* Opt attribute ((unused)),
648 const char* Arg attribute ((unused)))
649 /* Handle the --register-vars option */
651 IS_Set (&EnableRegVars, 1);
656 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
657 /* Handle the --rodata-name option */
659 /* Check for a valid name */
663 SetSegName (SEG_RODATA, Arg);
668 static void OptSignedChars (const char* Opt attribute ((unused)),
669 const char* Arg attribute ((unused)))
670 /* Make default characters signed */
672 IS_Set (&SignedChars, 1);
677 static void OptStandard (const char* Opt, const char* Arg)
678 /* Handle the --standard option */
680 /* Find the standard from the given name */
681 standard_t Std = FindStandard (Arg);
682 if (Std == STD_UNKNOWN) {
683 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
684 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
685 AbEnd ("Option %s given more than once", Opt);
687 IS_Set (&Standard, Std);
693 static void OptStaticLocals (const char* Opt attribute ((unused)),
694 const char* Arg attribute ((unused)))
695 /* Place local variables in static storage */
697 IS_Set (&StaticLocals, 1);
702 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
703 /* Set the target system */
710 static void OptVerbose (const char* Opt attribute ((unused)),
711 const char* Arg attribute ((unused)))
712 /* Increase verbosity */
719 static void OptVersion (const char* Opt attribute ((unused)),
720 const char* Arg attribute ((unused)))
721 /* Print the compiler version */
723 fprintf (stderr, "cc65 V%s\n", GetVersionAsString ());
729 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
730 /* Handle the -W option */
732 StrBuf W = AUTO_STRBUF_INITIALIZER;
734 /* Arg is a list of suboptions, separated by commas */
741 /* The suboption may be prefixed with '-' or '+' */
745 } else if (*Arg == '+') {
746 /* This is the default */
750 /* Get the next suboption */
751 Pos = strchr (Arg, ',');
753 SB_CopyBuf (&W, Arg, Pos - Arg);
756 SB_CopyStr (&W, Arg);
761 /* Search for the warning */
762 S = FindWarning (SB_GetConstBuf (&W));
764 InvArg (Opt, SB_GetConstBuf (&W));
769 /* Free allocated memory */
775 static void OptWritableStrings (const char* Opt attribute ((unused)),
776 const char* Arg attribute ((unused)))
777 /* Make string literals writable */
779 IS_Set (&WritableStrings, 1);
784 int main (int argc, char* argv[])
786 /* Program long options */
787 static const LongOpt OptTab[] = {
788 { "--add-source", 0, OptAddSource },
789 { "--bss-name", 1, OptBssName },
790 { "--check-stack", 0, OptCheckStack },
791 { "--code-name", 1, OptCodeName },
792 { "--codesize", 1, OptCodeSize },
793 { "--cpu", 1, OptCPU },
794 { "--create-dep", 1, OptCreateDep },
795 { "--create-full-dep", 1, OptCreateFullDep },
796 { "--data-name", 1, OptDataName },
797 { "--debug", 0, OptDebug },
798 { "--debug-info", 0, OptDebugInfo },
799 { "--debug-opt", 1, OptDebugOpt },
800 { "--debug-opt-output", 0, OptDebugOptOutput },
801 { "--dep-target", 1, OptDepTarget },
802 { "--disable-opt", 1, OptDisableOpt },
803 { "--enable-opt", 1, OptEnableOpt },
804 { "--help", 0, OptHelp },
805 { "--include-dir", 1, OptIncludeDir },
806 { "--list-opt-steps", 0, OptListOptSteps },
807 { "--list-warnings", 0, OptListWarnings },
808 { "--local-strings", 0, OptLocalStrings },
809 { "--memory-model", 1, OptMemoryModel },
810 { "--register-space", 1, OptRegisterSpace },
811 { "--register-vars", 0, OptRegisterVars },
812 { "--rodata-name", 1, OptRodataName },
813 { "--signed-chars", 0, OptSignedChars },
814 { "--standard", 1, OptStandard },
815 { "--static-locals", 0, OptStaticLocals },
816 { "--target", 1, OptTarget },
817 { "--verbose", 0, OptVerbose },
818 { "--version", 0, OptVersion },
819 { "--writable-strings", 0, OptWritableStrings },
824 /* Initialize the input file name */
825 const char* InputFile = 0;
827 /* Initialize the cmdline module */
828 InitCmdLine (&argc, &argv, "cc65");
830 /* Initialize the default segment names */
833 /* Initialize the include search paths */
836 /* Parse the command line */
838 while (I < ArgCount) {
842 /* Get the argument */
843 const char* Arg = ArgVec[I];
845 /* Check for an option */
851 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
864 OptDebugInfo (Arg, 0);
868 OptSignedChars (Arg, 0);
872 SetOutputName (GetArg (&I, 2));
876 OptRegisterVars (Arg, 0);
880 OptTarget (Arg, GetArg (&I, 2));
884 OptCreateDep (Arg, 0);
896 OptStaticLocals (Arg, 0);
906 DefineSym (GetArg (&I, 2));
914 OptIncludeDir (Arg, GetArg (&I, 2));
918 IS_Set (&Optimize, 1);
923 IS_Set (&CodeSizeFactor, 200);
926 IS_Set (&EnableRegVars, 1);
929 IS_Set (&InlineStdFuncs, 1);
936 OptAddSource (Arg, 0);
944 OptWarning (Arg, GetArg (&I, 2));
953 fprintf (stderr, "additional file specs ignored\n");
963 /* Did we have a file spec on the command line? */
964 if (InputFile == 0) {
965 AbEnd ("No input files");
968 /* Add the default include search paths. */
969 FinishIncludePaths ();
971 /* Create the output file name if it was not explicitly given */
972 MakeDefaultOutputName (InputFile);
974 /* If no CPU given, use the default CPU for the target */
975 if (CPU == CPU_UNKNOWN) {
976 if (Target != TGT_UNKNOWN) {
977 CPU = GetTargetProperties (Target)->DefaultCPU;
983 /* If no memory model was given, use the default */
984 if (MemoryModel == MMODEL_UNKNOWN) {
985 SetMemoryModel (MMODEL_NEAR);
988 /* If no language standard was given, use the default one */
989 if (IS_Get (&Standard) == STD_UNKNOWN) {
990 IS_Set (&Standard, STD_DEFAULT);
996 /* Create the output file if we didn't had any errors */
997 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
999 /* Emit literals, externals, do cleanup and optimizations */
1005 /* Write the output to the file */
1007 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
1009 /* Close the file, check for errors */
1012 /* Create dependencies if requested */
1013 CreateDependencies ();
1016 /* Return an apropriate exit code */
1017 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;