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 standard 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 " --eagerly-inline-funcs\tEagerly inline some known functions\n"
122 " --enable-opt name\t\tEnable an optimization step\n"
123 " --help\t\t\tHelp (this text)\n"
124 " --include-dir dir\t\tSet an include directory search path\n"
125 " --inline-stdfuncs\t\tInline some standard functions\n"
126 " --list-opt-steps\t\tList all optimizer steps and exit\n"
127 " --list-warnings\t\tList available warning types for -W\n"
128 " --local-strings\t\tEmit string literals immediately\n"
129 " --memory-model model\t\tSet the memory model\n"
130 " --register-space b\t\tSet space available for register variables\n"
131 " --register-vars\t\tEnable register variables\n"
132 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
133 " --signed-chars\t\tDefault characters are signed\n"
134 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
135 " --static-locals\t\tMake local variables static\n"
136 " --target sys\t\t\tSet the target system\n"
137 " --verbose\t\t\tIncrease verbosity\n"
138 " --version\t\t\tPrint the compiler version number\n"
139 " --writable-strings\t\tMake string literals writable\n",
145 static void cbmsys (const char* sys)
146 /* Define a CBM system */
148 DefineNumericMacro ("__CBM__", 1);
149 DefineNumericMacro (sys, 1);
154 static void SetSys (const char* Sys)
155 /* Define a target system */
157 switch (Target = FindTarget (Sys)) {
163 AbEnd ("Cannot use `module' as a target for the compiler");
167 DefineNumericMacro ("__ATARI2600__", 1);
171 DefineNumericMacro ("__ATARI5200__", 1);
175 DefineNumericMacro ("__ATARI__", 1);
179 DefineNumericMacro ("__ATARI__", 1);
180 DefineNumericMacro ("__ATARIXL__", 1);
192 cbmsys ("__VIC20__");
201 DefineNumericMacro ("__PLUS4__", 1);
205 cbmsys ("__CBM510__");
209 cbmsys ("__CBM610__");
217 DefineNumericMacro ("__BBC__", 1);
221 DefineNumericMacro ("__APPLE2__", 1);
225 DefineNumericMacro ("__APPLE2__", 1);
226 DefineNumericMacro ("__APPLE2ENH__", 1);
230 DefineNumericMacro ("__GAMATE__", 1);
234 /* Do not handle as a CBM system */
235 DefineNumericMacro ("__GEOS__", 1);
236 DefineNumericMacro ("__GEOS_CBM__", 1);
239 case TGT_CREATIVISION:
240 DefineNumericMacro ("__CREATIVISION__", 1);
244 DefineNumericMacro ("__GEOS__", 1);
245 DefineNumericMacro ("__GEOS_APPLE__", 1);
249 DefineNumericMacro ("__LUNIX__", 1);
253 DefineNumericMacro ("__ATMOS__", 1);
257 DefineNumericMacro ("__TELESTRAT__", 1);
261 DefineNumericMacro ("__NES__", 1);
264 case TGT_SUPERVISION:
265 DefineNumericMacro ("__SUPERVISION__", 1);
269 DefineNumericMacro ("__LYNX__", 1);
273 DefineNumericMacro ("__SIM6502__", 1);
277 DefineNumericMacro ("__SIM65C02__", 1);
281 DefineNumericMacro ("__OSIC1P__", 1);
285 DefineNumericMacro ("__PCE__", 1);
289 AbEnd ("Unknown target system type %d", Target);
292 /* Initialize the translation tables for the target system */
298 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
299 /* Handle an option that remembers a file name for later */
301 /* Cannot have the option twice */
302 if (SB_NotEmpty (Name)) {
303 AbEnd ("Cannot use option `%s' twice", Opt);
305 /* Remember the file name for later */
306 SB_CopyStr (Name, Arg);
312 static void DefineSym (const char* Def)
313 /* Define a symbol on the command line */
317 /* The symbol must start with a character or underline */
318 if (Def [0] != '_' && !IsAlpha (Def [0])) {
322 /* Check the symbol name */
323 while (IsAlNum (*P) || *P == '_') {
327 /* Do we have a value given? */
332 /* No value given. Define the macro with the value 1 */
333 DefineNumericMacro (Def, 1);
335 /* We have a value, P points to the '=' character. Since the argument
336 ** is const, create a copy and replace the '=' in the copy by a zero
340 unsigned Len = strlen (Def)+1;
341 char* S = (char*) xmalloc (Len);
342 memcpy (S, Def, Len);
346 /* Define this as a macro */
347 DefineTextMacro (S, Q);
349 /* Release the allocated memory */
356 static void CheckSegName (const char* Seg)
357 /* Abort if the given name is not a valid segment name */
359 /* Print an error and abort if the name is not ok */
360 if (!ValidSegName (Seg)) {
361 AbEnd ("Segment name `%s' is invalid", Seg);
367 static void OptAddSource (const char* Opt attribute ((unused)),
368 const char* Arg attribute ((unused)))
369 /* Add source lines as comments in generated assembler file */
376 static void OptAllCDecl (const char* Opt attribute ((unused)),
377 const char* Arg attribute ((unused)))
378 /* Make functions default to cdecl instead of fastcall. */
385 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
386 /* Handle the --bss-name option */
388 /* Check for a valid name */
392 SetSegName (SEG_BSS, Arg);
397 static void OptCheckStack (const char* Opt attribute ((unused)),
398 const char* Arg attribute ((unused)))
399 /* Handle the --check-stack option */
401 IS_Set (&CheckStack, 1);
406 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
407 /* Handle the --code-name option */
409 /* Check for a valid name */
413 SetSegName (SEG_CODE, Arg);
418 static void OptCodeSize (const char* Opt, const char* Arg)
419 /* Handle the --codesize option */
424 /* Numeric argument expected */
425 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
426 Factor < 10 || Factor > 1000) {
427 AbEnd ("Argument for %s is invalid", Opt);
429 IS_Set (&CodeSizeFactor, Factor);
434 static void OptCreateDep (const char* Opt, const char* Arg)
435 /* Handle the --create-dep option */
437 FileNameOption (Opt, Arg, &DepName);
442 static void OptCreateFullDep (const char* Opt attribute ((unused)),
444 /* Handle the --create-full-dep option */
446 FileNameOption (Opt, Arg, &FullDepName);
451 static void OptCPU (const char* Opt, const char* Arg)
452 /* Handle the --cpu option */
454 /* Find the CPU from the given name */
456 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
457 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
458 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
464 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
465 /* Handle the --data-name option */
467 /* Check for a valid name */
471 SetSegName (SEG_DATA, Arg);
476 static void OptDebug (const char* Opt attribute ((unused)),
477 const char* Arg attribute ((unused)))
478 /* Compiler debug mode */
485 static void OptDebugInfo (const char* Opt attribute ((unused)),
486 const char* Arg attribute ((unused)))
487 /* Add debug info to the object file */
494 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
495 /* Debug optimization steps */
501 FILE* F = fopen (Arg, "r");
503 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
506 /* Read line by line, ignore empty lines and switch optimization
509 while (fgets (Buf, sizeof (Buf), F) != 0) {
511 /* Remove trailing control chars. This will also remove the
514 unsigned Len = strlen (Buf);
515 while (Len > 0 && IsControl (Buf[Len-1])) {
520 /* Get a pointer to the buffer and remove leading white space */
522 while (IsBlank (*Line)) {
526 /* Check the first character and enable/disable the step or
534 /* Empty or comment line */
553 /* Close the file, no error check here since we were just reading and
554 ** this is only a debug function.
561 static void OptDebugOptOutput (const char* Opt attribute ((unused)),
562 const char* Arg attribute ((unused)))
563 /* Output optimization steps */
570 static void OptDepTarget (const char* Opt attribute ((unused)), const char* Arg)
571 /* Handle the --dep-target option */
573 FileNameOption (Opt, Arg, &DepTarget);
578 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
579 /* Disable an optimization step */
586 static void OptEagerlyInlineFuncs (const char* Opt attribute((unused)),
587 const char* Arg attribute((unused)))
588 /* Eagerly inline some known functions */
590 IS_Set (&InlineStdFuncs, 1);
591 IS_Set (&EagerlyInlineFuncs, 1);
596 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
597 /* Enable an optimization step */
604 static void OptHelp (const char* Opt attribute ((unused)),
605 const char* Arg attribute ((unused)))
606 /* Print usage information and exit */
614 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
615 /* Add an include search path */
617 AddSearchPath (SysIncSearchPath, Arg);
618 AddSearchPath (UsrIncSearchPath, Arg);
623 static void OptInlineStdFuncs (const char* Opt attribute((unused)),
624 const char* Arg attribute((unused)))
625 /* Inline some standard functions */
627 IS_Set (&InlineStdFuncs, 1);
632 static void OptListOptSteps (const char* Opt attribute ((unused)),
633 const char* Arg attribute ((unused)))
634 /* List all optimizer steps */
636 /* List the optimizer steps */
637 ListOptSteps (stdout);
645 static void OptListWarnings (const char* Opt attribute ((unused)),
646 const char* Arg attribute ((unused)))
647 /* List all warning types */
649 /* List the warnings */
650 ListWarnings (stdout);
658 static void OptLocalStrings (const char* Opt attribute ((unused)),
659 const char* Arg attribute ((unused)))
660 /* Emit string literals immediately */
662 IS_Set (&LocalStrings, 1);
667 static void OptMemoryModel (const char* Opt, const char* Arg)
668 /* Set the memory model */
672 /* Check the current memory model */
673 if (MemoryModel != MMODEL_UNKNOWN) {
674 AbEnd ("Cannot use option `%s' twice", Opt);
677 /* Translate the memory model name and check it */
678 M = FindMemoryModel (Arg);
679 if (M == MMODEL_UNKNOWN) {
680 AbEnd ("Unknown memory model: %s", Arg);
681 } else if (M == MMODEL_HUGE) {
682 AbEnd ("Unsupported memory model: %s", Arg);
685 /* Set the memory model */
691 static void OptRegisterSpace (const char* Opt, const char* Arg)
692 /* Handle the --register-space option */
694 /* Numeric argument expected */
695 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
696 AbEnd ("Argument for option %s is invalid", Opt);
702 static void OptRegisterVars (const char* Opt attribute ((unused)),
703 const char* Arg attribute ((unused)))
704 /* Handle the --register-vars option */
706 IS_Set (&EnableRegVars, 1);
711 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
712 /* Handle the --rodata-name option */
714 /* Check for a valid name */
718 SetSegName (SEG_RODATA, Arg);
723 static void OptSignedChars (const char* Opt attribute ((unused)),
724 const char* Arg attribute ((unused)))
725 /* Make default characters signed */
727 IS_Set (&SignedChars, 1);
732 static void OptStandard (const char* Opt, const char* Arg)
733 /* Handle the --standard option */
735 /* Find the standard from the given name */
736 standard_t Std = FindStandard (Arg);
737 if (Std == STD_UNKNOWN) {
738 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
739 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
740 AbEnd ("Option %s given more than once", Opt);
742 IS_Set (&Standard, Std);
748 static void OptStaticLocals (const char* Opt attribute ((unused)),
749 const char* Arg attribute ((unused)))
750 /* Place local variables in static storage */
752 IS_Set (&StaticLocals, 1);
757 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
758 /* Set the target system */
765 static void OptVerbose (const char* Opt attribute ((unused)),
766 const char* Arg attribute ((unused)))
767 /* Increase verbosity */
774 static void OptVersion (const char* Opt attribute ((unused)),
775 const char* Arg attribute ((unused)))
776 /* Print the compiler version */
778 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
784 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
785 /* Handle the -W option */
787 StrBuf W = AUTO_STRBUF_INITIALIZER;
789 /* Arg is a list of suboptions, separated by commas */
796 /* The suboption may be prefixed with '-' or '+' */
800 } else if (*Arg == '+') {
801 /* This is the default */
805 /* Get the next suboption */
806 Pos = strchr (Arg, ',');
808 SB_CopyBuf (&W, Arg, Pos - Arg);
811 SB_CopyStr (&W, Arg);
816 /* Search for the warning */
817 S = FindWarning (SB_GetConstBuf (&W));
819 InvArg (Opt, SB_GetConstBuf (&W));
824 /* Free allocated memory */
830 static void OptWritableStrings (const char* Opt attribute ((unused)),
831 const char* Arg attribute ((unused)))
832 /* Make string literals writable */
834 IS_Set (&WritableStrings, 1);
839 int main (int argc, char* argv[])
841 /* Program long options */
842 static const LongOpt OptTab[] = {
843 { "--add-source", 0, OptAddSource },
844 { "--all-cdecl", 0, OptAllCDecl },
845 { "--bss-name", 1, OptBssName },
846 { "--check-stack", 0, OptCheckStack },
847 { "--code-name", 1, OptCodeName },
848 { "--codesize", 1, OptCodeSize },
849 { "--cpu", 1, OptCPU },
850 { "--create-dep", 1, OptCreateDep },
851 { "--create-full-dep", 1, OptCreateFullDep },
852 { "--data-name", 1, OptDataName },
853 { "--debug", 0, OptDebug },
854 { "--debug-info", 0, OptDebugInfo },
855 { "--debug-opt", 1, OptDebugOpt },
856 { "--debug-opt-output", 0, OptDebugOptOutput },
857 { "--dep-target", 1, OptDepTarget },
858 { "--disable-opt", 1, OptDisableOpt },
859 { "--eagerly-inline-funcs", 0, OptEagerlyInlineFuncs },
860 { "--enable-opt", 1, OptEnableOpt },
861 { "--help", 0, OptHelp },
862 { "--include-dir", 1, OptIncludeDir },
863 { "--inline-stdfuncs", 0, OptInlineStdFuncs },
864 { "--list-opt-steps", 0, OptListOptSteps },
865 { "--list-warnings", 0, OptListWarnings },
866 { "--local-strings", 0, OptLocalStrings },
867 { "--memory-model", 1, OptMemoryModel },
868 { "--register-space", 1, OptRegisterSpace },
869 { "--register-vars", 0, OptRegisterVars },
870 { "--rodata-name", 1, OptRodataName },
871 { "--signed-chars", 0, OptSignedChars },
872 { "--standard", 1, OptStandard },
873 { "--static-locals", 0, OptStaticLocals },
874 { "--target", 1, OptTarget },
875 { "--verbose", 0, OptVerbose },
876 { "--version", 0, OptVersion },
877 { "--writable-strings", 0, OptWritableStrings },
882 /* Initialize the input file name */
883 const char* InputFile = 0;
885 /* Initialize the cmdline module */
886 InitCmdLine (&argc, &argv, "cc65");
888 /* Initialize the default segment names */
891 /* Initialize the include search paths */
894 /* Parse the command line */
896 while (I < ArgCount) {
900 /* Get the argument */
901 const char* Arg = ArgVec[I];
903 /* Check for an option */
909 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
922 OptDebugInfo (Arg, 0);
926 OptSignedChars (Arg, 0);
930 SetOutputName (GetArg (&I, 2));
934 OptRegisterVars (Arg, 0);
938 OptTarget (Arg, GetArg (&I, 2));
942 OptCreateDep (Arg, 0);
954 OptStaticLocals (Arg, 0);
964 DefineSym (GetArg (&I, 2));
972 OptIncludeDir (Arg, GetArg (&I, 2));
976 IS_Set (&Optimize, 1);
981 IS_Set (&CodeSizeFactor, 200);
984 IS_Set (&EnableRegVars, 1);
987 IS_Set (&InlineStdFuncs, 1);
997 OptAddSource (Arg, 0);
1001 OptVersion (Arg, 0);
1005 OptWarning (Arg, GetArg (&I, 2));
1009 UnknownOption (Arg);
1014 fprintf (stderr, "additional file specs ignored\n");
1024 /* Did we have a file spec on the command line? */
1025 if (InputFile == 0) {
1026 AbEnd ("No input files");
1029 /* Add the default include search paths. */
1030 FinishIncludePaths ();
1032 /* Create the output file name if it was not explicitly given */
1033 MakeDefaultOutputName (InputFile);
1035 /* If no CPU given, use the default CPU for the target */
1036 if (CPU == CPU_UNKNOWN) {
1037 if (Target != TGT_UNKNOWN) {
1038 CPU = GetTargetProperties (Target)->DefaultCPU;
1044 /* If no memory model was given, use the default */
1045 if (MemoryModel == MMODEL_UNKNOWN) {
1046 SetMemoryModel (MMODEL_NEAR);
1049 /* If no language standard was given, use the default one */
1050 if (IS_Get (&Standard) == STD_UNKNOWN) {
1051 IS_Set (&Standard, STD_DEFAULT);
1055 Compile (InputFile);
1057 /* Create the output file if we didn't had any errors */
1058 if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
1060 /* Emit literals, externals, do cleanup and optimizations */
1066 /* Write the output to the file */
1068 Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
1070 /* Close the file, check for errors */
1073 /* Create dependencies if requested */
1074 CreateDependencies ();
1077 /* Return an apropriate exit code */
1078 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;