1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2004 Ullrich von Bassewitz */
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"
71 /*****************************************************************************/
73 /*****************************************************************************/
77 static void Usage (void)
78 /* Print usage information to stderr */
80 printf ("Usage: %s [options] file\n"
82 " -Cl\t\t\tMake local variables static\n"
83 " -Dsym[=defn]\t\tDefine a symbol\n"
84 " -E\t\t\tStop after the preprocessing stage\n"
85 " -I dir\t\tSet an include directory search path\n"
86 " -O\t\t\tOptimize code\n"
87 " -Oi\t\t\tOptimize code, inline more code\n"
88 " -Or\t\t\tEnable register variables\n"
89 " -Os\t\t\tInline some known functions\n"
90 " -T\t\t\tInclude source as comment\n"
91 " -V\t\t\tPrint the compiler version number\n"
92 " -W\t\t\tSuppress warnings\n"
93 " -d\t\t\tDebug mode\n"
94 " -g\t\t\tAdd debug info to object file\n"
95 " -h\t\t\tHelp (this text)\n"
96 " -j\t\t\tDefault characters are signed\n"
97 " -mm model\t\tSet the memory model\n"
98 " -o name\t\tName the output file\n"
99 " -r\t\t\tEnable register variables\n"
100 " -t sys\t\tSet the target system\n"
101 " -v\t\t\tIncrease verbosity\n"
104 " --add-source\t\tInclude source as comment\n"
105 " --bss-name seg\tSet the name of the BSS segment\n"
106 " --check-stack\t\tGenerate stack overflow checks\n"
107 " --code-name seg\tSet the name of the CODE segment\n"
108 " --codesize x\t\tAccept larger code by factor x\n"
109 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
110 " --create-dep\t\tCreate a make dependency file\n"
111 " --data-name seg\tSet the name of the DATA segment\n"
112 " --debug\t\tDebug mode\n"
113 " --debug-info\t\tAdd debug info to object file\n"
114 " --debug-opt name\tDebug optimization steps\n"
115 " --disable-opt name\tDisable an optimization step\n"
116 " --enable-opt name\tEnable an optimization step\n"
117 " --forget-inc-paths\tForget include search paths\n"
118 " --help\t\tHelp (this text)\n"
119 " --include-dir dir\tSet an include directory search path\n"
120 " --list-opt-steps\tList all optimizer steps and exit\n"
121 " --memory-model model\tSet the memory model\n"
122 " --register-space b\tSet space available for register variables\n"
123 " --register-vars\tEnable register variables\n"
124 " --rodata-name seg\tSet the name of the RODATA segment\n"
125 " --signed-chars\tDefault characters are signed\n"
126 " --standard std\tLanguage standard (c89, c99, cc65)\n"
127 " --static-locals\tMake local variables static\n"
128 " --target sys\t\tSet the target system\n"
129 " --verbose\t\tIncrease verbosity\n"
130 " --version\t\tPrint the compiler version number\n"
131 " --writable-strings\tMake string literals writable\n",
137 static void cbmsys (const char* sys)
138 /* Define a CBM system */
140 DefineNumericMacro ("__CBM__", 1);
141 DefineNumericMacro (sys, 1);
146 static void SetSys (const char* Sys)
147 /* Define a target system */
149 switch (Target = FindTarget (Sys)) {
155 AbEnd ("Cannot use `module' as a target for the compiler");
159 DefineNumericMacro ("__ATARI__", 1);
171 cbmsys ("__VIC20__");
183 cbmsys ("__PLUS4__");
187 cbmsys ("__CBM510__");
191 cbmsys ("__CBM610__");
199 DefineNumericMacro ("__BBC__", 1);
203 DefineNumericMacro ("__APPLE2__", 1);
207 DefineNumericMacro ("__APPLE2ENH__", 1);
211 /* Do not handle as a CBM system */
212 DefineNumericMacro ("__GEOS__", 1);
216 DefineNumericMacro ("__LUNIX__", 1);
220 DefineNumericMacro ("__ATMOS__", 1);
224 DefineNumericMacro ("__NES__", 1);
227 case TGT_SUPERVISION:
228 DefineNumericMacro ("__SUPERVISION__", 1);
232 DefineNumericMacro ("__LYNX__", 1);
236 AbEnd ("Unknown target system type %d", Target);
239 /* Initialize the translation tables for the target system */
245 static void DoCreateDep (const char* OutputName)
246 /* Create the dependency file */
248 /* Make the dependency file name from the output file name */
249 char* DepName = MakeFilename (OutputName, ".u");
252 FILE* F = fopen (DepName, "w");
254 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
257 /* Write the dependencies to the file */
258 WriteDependencies (F, OutputName);
260 /* Close the file, check for errors */
261 if (fclose (F) != 0) {
263 Fatal ("Cannot write to dependeny file (disk full?)");
272 static void DefineSym (const char* Def)
273 /* Define a symbol on the command line */
277 /* The symbol must start with a character or underline */
278 if (Def [0] != '_' && !IsAlpha (Def [0])) {
282 /* Check the symbol name */
283 while (IsAlNum (*P) || *P == '_') {
287 /* Do we have a value given? */
292 /* No value given. Define the macro with the value 1 */
293 DefineNumericMacro (Def, 1);
295 /* We have a value, P points to the '=' character. Since the argument
296 * is const, create a copy and replace the '=' in the copy by a zero
300 unsigned Len = strlen (Def)+1;
301 char* S = (char*) xmalloc (Len);
302 memcpy (S, Def, Len);
306 /* Define this as a macro */
307 DefineTextMacro (S, Q);
309 /* Release the allocated memory */
316 static void CheckSegName (const char* Seg)
317 /* Abort if the given name is not a valid segment name */
319 /* Print an error and abort if the name is not ok */
320 if (!ValidSegName (Seg)) {
321 AbEnd ("Segment name `%s' is invalid", Seg);
327 static void OptAddSource (const char* Opt attribute ((unused)),
328 const char* Arg attribute ((unused)))
329 /* Add source lines as comments in generated assembler file */
336 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
337 /* Handle the --bss-name option */
339 /* Check for a valid name */
343 SetSegName (SEG_BSS, Arg);
348 static void OptCheckStack (const char* Opt attribute ((unused)),
349 const char* Arg attribute ((unused)))
350 /* Handle the --check-stack option */
352 IS_Set (&CheckStack, 1);
357 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
358 /* Handle the --code-name option */
360 /* Check for a valid name */
364 SetSegName (SEG_CODE, Arg);
369 static void OptCodeSize (const char* Opt, const char* Arg)
370 /* Handle the --codesize option */
375 /* Numeric argument expected */
376 if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
377 Factor < 10 || Factor > 1000) {
378 AbEnd ("Argument for %s is invalid", Opt);
380 IS_Set (&CodeSizeFactor, Factor);
385 static void OptCreateDep (const char* Opt attribute ((unused)),
386 const char* Arg attribute ((unused)))
387 /* Handle the --create-dep option */
394 static void OptCPU (const char* Opt, const char* Arg)
395 /* Handle the --cpu option */
397 /* Find the CPU from the given name */
399 if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
400 CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
401 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
407 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
408 /* Handle the --data-name option */
410 /* Check for a valid name */
414 SetSegName (SEG_DATA, Arg);
419 static void OptDebug (const char* Opt attribute ((unused)),
420 const char* Arg attribute ((unused)))
421 /* Compiler debug mode */
428 static void OptDebugInfo (const char* Opt attribute ((unused)),
429 const char* Arg attribute ((unused)))
430 /* Add debug info to the object file */
437 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
438 /* Debug optimization steps */
444 FILE* F = fopen (Arg, "r");
446 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
449 /* Read line by line, ignore empty lines and switch optimization
452 while (fgets (Buf, sizeof (Buf), F) != 0) {
454 /* Remove trailing control chars. This will also remove the
457 unsigned Len = strlen (Buf);
458 while (Len > 0 && IsControl (Buf[Len-1])) {
463 /* Get a pointer to the buffer and remove leading white space */
465 while (IsBlank (*Line)) {
469 /* Check the first character and enable/disable the step or
477 /* Empty or comment line */
496 /* Close the file, no error check here since we were just reading and
497 * this is only a debug function.
504 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
505 /* Disable an optimization step */
512 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
513 /* Enable an optimization step */
520 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
521 const char* Arg attribute ((unused)))
522 /* Forget all currently defined include paths */
524 ForgetAllIncludePaths ();
529 static void OptHelp (const char* Opt attribute ((unused)),
530 const char* Arg attribute ((unused)))
531 /* Print usage information and exit */
539 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
540 /* Add an include search path */
542 AddIncludePath (Arg, INC_SYS | INC_USER);
547 static void OptListOptSteps (const char* Opt attribute ((unused)),
548 const char* Arg attribute ((unused)))
549 /* List all optimizer steps */
551 /* List the optimizer steps */
552 ListOptSteps (stdout);
560 static void OptMemoryModel (const char* Opt, const char* Arg)
561 /* Set the memory model */
565 /* Check the current memory model */
566 if (MemoryModel != MMODEL_UNKNOWN) {
567 AbEnd ("Cannot use option `%s' twice", Opt);
570 /* Translate the memory model name and check it */
571 M = FindMemoryModel (Arg);
572 if (M == MMODEL_UNKNOWN) {
573 AbEnd ("Unknown memory model: %s", Arg);
574 } else if (M == MMODEL_HUGE) {
575 AbEnd ("Unsupported memory model: %s", Arg);
578 /* Set the memory model */
584 static void OptRegisterSpace (const char* Opt, const char* Arg)
585 /* Handle the --register-space option */
587 /* Numeric argument expected */
588 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
589 AbEnd ("Argument for option %s is invalid", Opt);
595 static void OptRegisterVars (const char* Opt attribute ((unused)),
596 const char* Arg attribute ((unused)))
597 /* Handle the --register-vars option */
599 IS_Set (&EnableRegVars, 1);
604 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
605 /* Handle the --rodata-name option */
607 /* Check for a valid name */
611 SetSegName (SEG_RODATA, Arg);
616 static void OptSignedChars (const char* Opt attribute ((unused)),
617 const char* Arg attribute ((unused)))
618 /* Make default characters signed */
620 IS_Set (&SignedChars, 1);
625 static void OptStandard (const char* Opt, const char* Arg)
626 /* Handle the --standard option */
628 /* Find the standard from the given name */
629 standard_t Std = FindStandard (Arg);
630 if (Std == STD_UNKNOWN) {
631 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
632 } else if (IS_Get (&Standard) != STD_UNKNOWN) {
633 AbEnd ("Option %s given more than once", Opt);
635 IS_Set (&Standard, Std);
641 static void OptStaticLocals (const char* Opt attribute ((unused)),
642 const char* Arg attribute ((unused)))
643 /* Place local variables in static storage */
645 IS_Set (&StaticLocals, 1);
650 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
651 /* Set the target system */
658 static void OptVerbose (const char* Opt attribute ((unused)),
659 const char* Arg attribute ((unused)))
660 /* Increase verbosity */
667 static void OptVersion (const char* Opt attribute ((unused)),
668 const char* Arg attribute ((unused)))
669 /* Print the assembler version */
673 VER_MAJOR, VER_MINOR, VER_PATCH);
678 static void OptWritableStrings (const char* Opt attribute ((unused)),
679 const char* Arg attribute ((unused)))
680 /* Make string literals writable */
682 IS_Set (&WritableStrings, 1);
687 int main (int argc, char* argv[])
689 /* Program long options */
690 static const LongOpt OptTab[] = {
691 { "--add-source", 0, OptAddSource },
692 { "--bss-name", 1, OptBssName },
693 { "--check-stack", 0, OptCheckStack },
694 { "--code-name", 1, OptCodeName },
695 { "--codesize", 1, OptCodeSize },
696 { "--cpu", 1, OptCPU },
697 { "--create-dep", 0, OptCreateDep },
698 { "--data-name", 1, OptDataName },
699 { "--debug", 0, OptDebug },
700 { "--debug-info", 0, OptDebugInfo },
701 { "--debug-opt", 1, OptDebugOpt },
702 { "--disable-opt", 1, OptDisableOpt },
703 { "--enable-opt", 1, OptEnableOpt },
704 { "--forget-inc-paths", 0, OptForgetIncPaths },
705 { "--help", 0, OptHelp },
706 { "--include-dir", 1, OptIncludeDir },
707 { "--list-opt-steps", 0, OptListOptSteps },
708 { "--memory-model", 1, OptMemoryModel },
709 { "--register-space", 1, OptRegisterSpace },
710 { "--register-vars", 0, OptRegisterVars },
711 { "--rodata-name", 1, OptRodataName },
712 { "--signed-chars", 0, OptSignedChars },
713 { "--standard", 1, OptStandard },
714 { "--static-locals", 0, OptStaticLocals },
715 { "--target", 1, OptTarget },
716 { "--verbose", 0, OptVerbose },
717 { "--version", 0, OptVersion },
718 { "--writable-strings", 0, OptWritableStrings },
723 /* Initialize the output file name */
724 const char* OutputFile = 0;
725 const char* InputFile = 0;
727 /* Initialize the cmdline module */
728 InitCmdLine (&argc, &argv, "cc65");
730 /* Initialize the default segment names */
733 /* Initialize the include search paths */
736 /* Parse the command line */
738 while (I < ArgCount) {
742 /* Get the argument */
743 const char* Arg = ArgVec[I];
745 /* Check for an option */
746 if (Arg [0] == '-') {
751 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
764 OptDebugInfo (Arg, 0);
768 OptSignedChars (Arg, 0);
772 OutputFile = GetArg (&I, 2);
776 OptRegisterVars (Arg, 0);
780 OptTarget (Arg, GetArg (&I, 2));
784 OptCreateDep (Arg, 0);
796 OptStaticLocals (Arg, 0);
806 DefineSym (GetArg (&I, 2));
814 OptIncludeDir (Arg, GetArg (&I, 2));
818 IS_Set (&Optimize, 1);
823 IS_Set (&CodeSizeFactor, 200);
826 IS_Set (&EnableRegVars, 1);
829 IS_Set (&InlineStdFuncs, 1);
836 OptAddSource (Arg, 0);
844 IS_Set (&WarnDisable, 1);
853 fprintf (stderr, "additional file specs ignored\n");
863 /* Did we have a file spec on the command line? */
864 if (InputFile == 0) {
865 AbEnd ("No input files");
868 /* Create the output file name if it was not explicitly given */
869 if (OutputFile == 0) {
870 OutputFile = MakeFilename (InputFile, ".s");
873 /* If no CPU given, use the default CPU for the target */
874 if (CPU == CPU_UNKNOWN) {
875 if (Target != TGT_UNKNOWN) {
876 CPU = DefaultCPU[Target];
882 /* If no memory model was given, use the default */
883 if (MemoryModel == MMODEL_UNKNOWN) {
884 SetMemoryModel (MMODEL_NEAR);
887 /* If no language standard was given, use the default one */
888 if (IS_Get (&Standard) == STD_UNKNOWN) {
889 IS_Set (&Standard, STD_DEFAULT);
895 /* Create the output file if we didn't had any errors */
896 if (ErrorCount == 0 || Debug) {
899 FILE* F = fopen (OutputFile, "w");
901 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
903 Print (stdout, 1, "Opened output file `%s'\n", OutputFile);
905 /* Write the output to the file */
907 Print (stdout, 1, "Wrote output to `%s'\n", OutputFile);
909 /* Close the file, check for errors */
910 if (fclose (F) != 0) {
912 Fatal ("Cannot write to output file (disk full?)");
914 Print (stdout, 1, "Closed output file `%s'\n", OutputFile);
916 /* Create dependencies if requested */
918 DoCreateDep (OutputFile);
919 Print (stdout, 1, "Creating dependeny file\n");
924 /* Return an apropriate exit code */
925 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;