1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2015, 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 " --all-cdecl\t\t\tMake functions default to __cdecl__\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 " --help\t\t\tHelp (this text)\n"
123 " --include-dir dir\t\tSet an include directory search path\n"
124 " --list-opt-steps\t\tList all optimizer steps and exit\n"
125 " --list-warnings\t\tList available warning types for -W\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 ("__ATARI5200__", 1);
169 DefineNumericMacro ("__ATARI__", 1);
173 DefineNumericMacro ("__ATARI__", 1);
174 DefineNumericMacro ("__ATARIXL__", 1);
186 cbmsys ("__VIC20__");
195 DefineNumericMacro ("__PLUS4__", 1);
199 cbmsys ("__CBM510__");
203 cbmsys ("__CBM610__");
211 DefineNumericMacro ("__BBC__", 1);
215 DefineNumericMacro ("__APPLE2__", 1);
219 DefineNumericMacro ("__APPLE2__", 1);
220 DefineNumericMacro ("__APPLE2ENH__", 1);
224 DefineNumericMacro ("__GAMATE__", 1);
228 /* Do not handle as a CBM system */
229 DefineNumericMacro ("__GEOS__", 1);
230 DefineNumericMacro ("__GEOS_CBM__", 1);
234 DefineNumericMacro ("__GEOS__", 1);
235 DefineNumericMacro ("__GEOS_APPLE__", 1);
239 DefineNumericMacro ("__LUNIX__", 1);
243 DefineNumericMacro ("__ATMOS__", 1);
247 DefineNumericMacro ("__TELEMON24__", 1);
251 DefineNumericMacro ("__TELEMON30__", 1);
255 DefineNumericMacro ("__NES__", 1);
258 case TGT_SUPERVISION:
259 DefineNumericMacro ("__SUPERVISION__", 1);
263 DefineNumericMacro ("__LYNX__", 1);
267 DefineNumericMacro ("__SIM6502__", 1);
271 DefineNumericMacro ("__SIM65C02__", 1);
275 DefineNumericMacro ("__OSIC1P__", 1);
279 DefineNumericMacro ("__PCE__", 1);
283 AbEnd ("Unknown target system type %d", Target);
286 /* Initialize the translation tables for the target system */
292 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
293 /* Handle an option that remembers a file name for later */
295 /* Cannot have the option twice */
296 if (SB_NotEmpty (Name)) {
297 AbEnd ("Cannot use option `%s' twice", Opt);
299 /* Remember the file name for later */
300 SB_CopyStr (Name, Arg);
306 static void DefineSym (const char* Def)
307 /* Define a symbol on the command line */
311 /* The symbol must start with a character or underline */
312 if (Def [0] != '_' && !IsAlpha (Def [0])) {
316 /* Check the symbol name */
317 while (IsAlNum (*P) || *P == '_') {
321 /* Do we have a value given? */
326 /* No value given. Define the macro with the value 1 */
327 DefineNumericMacro (Def, 1);
329 /* We have a value, P points to the '=' character. Since the argument
330 ** is const, create a copy and replace the '=' in the copy by a zero
334 unsigned Len = strlen (Def)+1;
335 char* S = (char*) xmalloc (Len);
336 memcpy (S, Def, Len);
340 /* Define this as a macro */
341 DefineTextMacro (S, Q);
343 /* Release the allocated memory */
350 static void CheckSegName (const char* Seg)
351 /* Abort if the given name is not a valid segment name */
353 /* Print an error and abort if the name is not ok */
354 if (!ValidSegName (Seg)) {
355 AbEnd ("Segment name `%s' is invalid", Seg);
361 static void OptAddSource (const char* Opt attribute ((unused)),
362 const char* Arg attribute ((unused)))
363 /* Add source lines as comments in generated assembler file */
370 static void OptAllCDecl (const char* Opt attribute ((unused)),
371 const char* Arg attribute ((unused)))
372 /* Make functions default to cdecl instead of fastcall. */
379 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
380 /* Handle the --bss-name option */
382 /* Check for a valid name */
386 SetSegName (SEG_BSS, Arg);
391 static void OptCheckStack (const char* Opt attribute ((unused)),
392 const char* Arg attribute ((unused)))
393 /* Handle the --check-stack option */
395 IS_Set (&CheckStack, 1);
400 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
401 /* Handle the --code-name option */
403 /* Check for a valid name */
407 SetSegName (SEG_CODE, Arg);
412 static void OptCodeSize (const char* Opt, const char* Arg)
413 /* Handle the --codesize option */
418 /* Numeric argument expected */
419 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
420 Factor < 10 || Factor > 1000) {
421 AbEnd ("Argument for %s is invalid", Opt);
423 IS_Set (&CodeSizeFactor, Factor);
428 static void OptCreateDep (const char* Opt, const char* Arg)
429 /* Handle the --create-dep option */
431 FileNameOption (Opt, Arg, &DepName);
436 static void OptCreateFullDep (const char* Opt attribute ((unused)),
438 /* Handle the --create-full-dep option */
440 FileNameOption (Opt, Arg, &FullDepName);
445 static void OptCPU (const char* Opt, const char* Arg)
446 /* Handle the --cpu option */
448 /* Find the CPU from the given name */
450 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
451 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
452 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
458 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
459 /* Handle the --data-name option */
461 /* Check for a valid name */
465 SetSegName (SEG_DATA, Arg);
470 static void OptDebug (const char* Opt attribute ((unused)),
471 const char* Arg attribute ((unused)))
472 /* Compiler debug mode */
479 static void OptDebugInfo (const char* Opt attribute ((unused)),
480 const char* Arg attribute ((unused)))
481 /* Add debug info to the object file */
488 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
489 /* Debug optimization steps */
495 FILE* F = fopen (Arg, "r");
497 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
500 /* Read line by line, ignore empty lines and switch optimization
503 while (fgets (Buf, sizeof (Buf), F) != 0) {
505 /* Remove trailing control chars. This will also remove the
508 unsigned Len = strlen (Buf);
509 while (Len > 0 && IsControl (Buf[Len-1])) {
514 /* Get a pointer to the buffer and remove leading white space */
516 while (IsBlank (*Line)) {
520 /* Check the first character and enable/disable the step or
528 /* Empty or comment line */
547 /* Close the file, no error check here since we were just reading and
548 ** this is only a debug function.
555 static void OptDebugOptOutput (const char* Opt attribute ((unused)),
556 const char* Arg attribute ((unused)))
557 /* Output optimization steps */
564 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
565 /* Handle the --dep-target option */
567 FileNameOption (Opt, Arg, &DepTarget);
572 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
573 /* Disable an optimization step */
580 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
581 /* Enable an optimization step */
588 static void OptHelp (const char* Opt attribute ((unused)),
589 const char* Arg attribute ((unused)))
590 /* Print usage information and exit */
598 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
599 /* Add an include search path */
601 AddSearchPath (SysIncSearchPath, Arg);
602 AddSearchPath (UsrIncSearchPath, Arg);
607 static void OptListOptSteps (const char* Opt attribute ((unused)),
608 const char* Arg attribute ((unused)))
609 /* List all optimizer steps */
611 /* List the optimizer steps */
612 ListOptSteps (stdout);
620 static void OptListWarnings (const char* Opt attribute ((unused)),
621 const char* Arg attribute ((unused)))
622 /* List all warning types */
624 /* List the warnings */
625 ListWarnings (stdout);
633 static void OptLocalStrings (const char* Opt attribute ((unused)),
634 const char* Arg attribute ((unused)))
635 /* Emit string literals immediately */
637 IS_Set (&LocalStrings, 1);
642 static void OptMemoryModel (const char* Opt, const char* Arg)
643 /* Set the memory model */
647 /* Check the current memory model */
648 if (MemoryModel != MMODEL_UNKNOWN) {
649 AbEnd ("Cannot use option `%s' twice", Opt);
652 /* Translate the memory model name and check it */
653 M = FindMemoryModel (Arg);
654 if (M == MMODEL_UNKNOWN) {
655 AbEnd ("Unknown memory model: %s", Arg);
656 } else if (M == MMODEL_HUGE) {
657 AbEnd ("Unsupported memory model: %s", Arg);
660 /* Set the memory model */
666 static void OptRegisterSpace (const char* Opt, const char* Arg)
667 /* Handle the --register-space option */
669 /* Numeric argument expected */
670 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
671 AbEnd ("Argument for option %s is invalid", Opt);
677 static void OptRegisterVars (const char* Opt attribute ((unused)),
678 const char* Arg attribute ((unused)))
679 /* Handle the --register-vars option */
681 IS_Set (&EnableRegVars, 1);
686 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
687 /* Handle the --rodata-name option */
689 /* Check for a valid name */
693 SetSegName (SEG_RODATA, Arg);
698 static void OptSignedChars (const char* Opt attribute ((unused)),
699 const char* Arg attribute ((unused)))
700 /* Make default characters signed */
702 IS_Set (&SignedChars, 1);
707 static void OptStandard (const char* Opt, const char* Arg)
708 /* Handle the --standard option */
710 /* Find the standard from the given name */
711 standard_t Std = FindStandard (Arg);
712 if (Std == STD_UNKNOWN) {
713 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
714 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
715 AbEnd ("Option %s given more than once", Opt);
717 IS_Set (&Standard, Std);
723 static void OptStaticLocals (const char* Opt attribute ((unused)),
724 const char* Arg attribute ((unused)))
725 /* Place local variables in static storage */
727 IS_Set (&StaticLocals, 1);
732 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
733 /* Set the target system */
740 static void OptVerbose (const char* Opt attribute ((unused)),
741 const char* Arg attribute ((unused)))
742 /* Increase verbosity */
750 static void OptVersion(const char* Opt attribute((unused)),
751 const char* Arg attribute((unused)))
752 /* Print the compiler version */
754 fprintf(stderr, "%s V%s\n", ProgName, GetVersionAsString());
760 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
761 /* Handle the -W option */
763 StrBuf W = AUTO_STRBUF_INITIALIZER;
765 /* Arg is a list of suboptions, separated by commas */
772 /* The suboption may be prefixed with '-' or '+' */
776 } else if (*Arg == '+') {
777 /* This is the default */
781 /* Get the next suboption */
782 Pos = strchr (Arg, ',');
784 SB_CopyBuf (&W, Arg, Pos - Arg);
787 SB_CopyStr (&W, Arg);
792 /* Search for the warning */
793 S = FindWarning (SB_GetConstBuf (&W));
795 InvArg (Opt, SB_GetConstBuf (&W));
800 /* Free allocated memory */
806 static void OptWritableStrings (const char* Opt attribute ((unused)),
807 const char* Arg attribute ((unused)))
808 /* Make string literals writable */
810 IS_Set (&WritableStrings, 1);
815 int main (int argc, char* argv[])
817 /* Program long options */
818 static const LongOpt OptTab[] = {
819 { "--add-source", 0, OptAddSource },
820 { "--all-cdecl", 0, OptAllCDecl },
821 { "--bss-name", 1, OptBssName },
822 { "--check-stack", 0, OptCheckStack },
823 { "--code-name", 1, OptCodeName },
824 { "--codesize", 1, OptCodeSize },
825 { "--cpu", 1, OptCPU },
826 { "--create-dep", 1, OptCreateDep },
827 { "--create-full-dep", 1, OptCreateFullDep },
828 { "--data-name", 1, OptDataName },
829 { "--debug", 0, OptDebug },
830 { "--debug-info", 0, OptDebugInfo },
831 { "--debug-opt", 1, OptDebugOpt },
832 { "--debug-opt-output", 0, OptDebugOptOutput },
833 { "--dep-target", 1, OptDepTarget },
834 { "--disable-opt", 1, OptDisableOpt },
835 { "--enable-opt", 1, OptEnableOpt },
836 { "--help", 0, OptHelp },
837 { "--include-dir", 1, OptIncludeDir },
838 { "--list-opt-steps", 0, OptListOptSteps },
839 { "--list-warnings", 0, OptListWarnings },
840 { "--local-strings", 0, OptLocalStrings },
841 { "--memory-model", 1, OptMemoryModel },
842 { "--register-space", 1, OptRegisterSpace },
843 { "--register-vars", 0, OptRegisterVars },
844 { "--rodata-name", 1, OptRodataName },
845 { "--signed-chars", 0, OptSignedChars },
846 { "--standard", 1, OptStandard },
847 { "--static-locals", 0, OptStaticLocals },
848 { "--target", 1, OptTarget },
849 { "--verbose", 0, OptVerbose },
850 { "--version", 0, OptVersion },
851 { "--writable-strings", 0, OptWritableStrings },
856 /* Initialize the input file name */
857 const char* InputFile = 0;
859 /* Initialize the cmdline module */
860 InitCmdLine (&argc, &argv, "cc65");
862 /* Initialize the default segment names */
865 /* Initialize the include search paths */
868 /* Parse the command line */
870 while (I < ArgCount) {
874 /* Get the argument */
875 const char* Arg = ArgVec[I];
877 /* Check for an option */
883 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
896 OptDebugInfo (Arg, 0);
900 OptSignedChars (Arg, 0);
904 SetOutputName (GetArg (&I, 2));
908 OptRegisterVars (Arg, 0);
912 OptTarget (Arg, GetArg (&I, 2));
916 OptCreateDep (Arg, 0);
928 OptStaticLocals (Arg, 0);
938 DefineSym (GetArg (&I, 2));
946 OptIncludeDir (Arg, GetArg (&I, 2));
950 IS_Set (&Optimize, 1);
955 IS_Set (&CodeSizeFactor, 200);
958 IS_Set (&EnableRegVars, 1);
961 IS_Set (&InlineStdFuncs, 1);
968 OptAddSource (Arg, 0);
976 OptWarning (Arg, GetArg (&I, 2));
985 fprintf (stderr, "additional file specs ignored\n");
995 /* Did we have a file spec on the command line? */
996 if (InputFile == 0) {
997 AbEnd ("No input files");
1000 /* Add the default include search paths. */
1001 FinishIncludePaths ();
1003 /* Create the output file name if it was not explicitly given */
1004 MakeDefaultOutputName (InputFile);
1006 /* If no CPU given, use the default CPU for the target */
1007 if (CPU == CPU_UNKNOWN) {
1008 if (Target != TGT_UNKNOWN) {
1009 CPU = GetTargetProperties (Target)->DefaultCPU;
1015 /* If no memory model was given, use the default */
1016 if (MemoryModel == MMODEL_UNKNOWN) {
1017 SetMemoryModel (MMODEL_NEAR);
1020 /* If no language standard was given, use the default one */
1021 if (IS_Get (&Standard) == STD_UNKNOWN) {
1022 IS_Set (&Standard, STD_DEFAULT);
1026 Compile (InputFile);
1028 /* Create the output file if we didn't had any errors */
1029 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
1031 /* Emit literals, externals, do cleanup and optimizations */
1037 /* Write the output to the file */
1039 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
1041 /* Close the file, check for errors */
1044 /* Create dependencies if requested */
1045 CreateDependencies ();
1048 /* Return an apropriate exit code */
1049 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;