1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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"
69 /*****************************************************************************/
71 /*****************************************************************************/
75 static void Usage (void)
78 "Usage: %s [options] file\n"
80 " -A\t\t\tStrict ANSI mode\n"
81 " -Cl\t\t\tMake local variables static\n"
82 " -Dsym[=defn]\t\tDefine a symbol\n"
83 " -I dir\t\tSet an include directory search path\n"
84 " -O\t\t\tOptimize code\n"
85 " -Oi\t\t\tOptimize code, inline more code\n"
86 " -Or\t\t\tEnable register variables\n"
87 " -Os\t\t\tInline some known functions\n"
88 " -T\t\t\tInclude source as comment\n"
89 " -V\t\t\tPrint the compiler version number\n"
90 " -W\t\t\tSuppress warnings\n"
91 " -d\t\t\tDebug mode\n"
92 " -g\t\t\tAdd debug info to object file\n"
93 " -h\t\t\tHelp (this text)\n"
94 " -j\t\t\tDefault characters are signed\n"
95 " -o name\t\tName the output file\n"
96 " -r\t\t\tEnable register variables\n"
97 " -t sys\t\tSet the target system\n"
98 " -v\t\t\tIncrease verbosity\n"
101 " --add-source\t\tInclude source as comment\n"
102 " --ansi\t\tStrict ANSI mode\n"
103 " --bss-name seg\tSet the name of the BSS segment\n"
104 " --check-stack\t\tGenerate stack overflow checks\n"
105 " --code-name seg\tSet the name of the CODE segment\n"
106 " --codesize x\t\tAccept larger code by factor x\n"
107 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
108 " --create-dep\t\tCreate a make dependency file\n"
109 " --data-name seg\tSet the name of the DATA segment\n"
110 " --debug\t\tDebug mode\n"
111 " --debug-info\t\tAdd debug info to object file\n"
112 " --debug-opt name\tDebug optimization steps\n"
113 " --disable-opt name\tDisable an optimization step\n"
114 " --enable-opt name\tEnable an optimization step\n"
115 " --help\t\tHelp (this text)\n"
116 " --include-dir dir\tSet an include directory search path\n"
117 " --list-opt-steps\tList all optimizer steps and exit\n"
118 " --register-space b\tSet space available for register variables\n"
119 " --register-vars\tEnable register variables\n"
120 " --rodata-name seg\tSet the name of the RODATA segment\n"
121 " --signed-chars\tDefault characters are signed\n"
122 " --static-locals\tMake local variables static\n"
123 " --target sys\t\tSet the target system\n"
124 " --verbose\t\tIncrease verbosity\n"
125 " --version\t\tPrint the compiler version number\n",
131 static void cbmsys (const char* sys)
132 /* Define a CBM system */
134 DefineNumericMacro ("__CBM__", 1);
135 DefineNumericMacro (sys, 1);
140 static void SetSys (const char* Sys)
141 /* Define a target system */
143 switch (Target = FindTarget (Sys)) {
149 AbEnd ("Cannot use `module' as a target for the compiler");
153 DefineNumericMacro ("__ATARI__", 1);
165 cbmsys ("__VIC20__");
177 cbmsys ("__PLUS4__");
181 cbmsys ("__CBM510__");
185 cbmsys ("__CBM610__");
193 DefineNumericMacro ("__BBC__", 1);
197 DefineNumericMacro ("__APPLE2__", 1);
201 /* Do not handle as a CBM system */
202 DefineNumericMacro ("__GEOS__", 1);
206 DefineNumericMacro ("__LUNIX__", 1);
210 DefineNumericMacro ("__ATMOS__", 1);
214 DefineNumericMacro ("__NES__", 1);
218 AbEnd ("Unknown target system type %d", Target);
221 /* Initialize the translation tables for the target system */
227 static void DoCreateDep (const char* OutputName)
228 /* Create the dependency file */
230 /* Make the dependency file name from the output file name */
231 char* DepName = MakeFilename (OutputName, ".u");
234 FILE* F = fopen (DepName, "w");
236 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
239 /* Write the dependencies to the file */
240 WriteDependencies (F, OutputName);
242 /* Close the file, check for errors */
243 if (fclose (F) != 0) {
245 Fatal ("Cannot write to dependeny file (disk full?)");
254 static void DefineSym (const char* Def)
255 /* Define a symbol on the command line */
259 /* The symbol must start with a character or underline */
260 if (Def [0] != '_' && !IsAlpha (Def [0])) {
264 /* Check the symbol name */
265 while (IsAlNum (*P) || *P == '_') {
269 /* Do we have a value given? */
274 /* No value given. Define the macro with the value 1 */
275 DefineNumericMacro (Def, 1);
277 /* We have a value, P points to the '=' character. Since the argument
278 * is const, create a copy and replace the '=' in the copy by a zero
282 unsigned Len = strlen (Def)+1;
283 char* S = (char*) xmalloc (Len);
284 memcpy (S, Def, Len);
288 /* Define this as a macro */
289 DefineTextMacro (S, Q);
291 /* Release the allocated memory */
298 static void CheckSegName (const char* Seg)
299 /* Abort if the given name is not a valid segment name */
301 /* Print an error and abort if the name is not ok */
302 if (!ValidSegName (Seg)) {
303 AbEnd ("Segment name `%s' is invalid", Seg);
309 static void OptAddSource (const char* Opt attribute ((unused)),
310 const char* Arg attribute ((unused)))
311 /* Add source lines as comments in generated assembler file */
318 static void OptAnsi (const char* Opt attribute ((unused)),
319 const char* Arg attribute ((unused)))
320 /* Compile in strict ANSI mode */
327 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
328 /* Handle the --bss-name option */
330 /* Check for a valid name */
334 NewSegName (SEG_BSS, Arg);
339 static void OptCheckStack (const char* Opt attribute ((unused)),
340 const char* Arg attribute ((unused)))
341 /* Handle the --check-stack option */
348 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
349 /* Handle the --code-name option */
351 /* Check for a valid name */
355 NewSegName (SEG_CODE, Arg);
360 static void OptCodeSize (const char* Opt, const char* Arg)
361 /* Handle the --codesize option */
363 /* Numeric argument expected */
364 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
365 CodeSizeFactor < 100 ||
366 CodeSizeFactor > 1000) {
367 AbEnd ("Argument for %s is invalid", Opt);
373 static void OptCreateDep (const char* Opt attribute ((unused)),
374 const char* Arg attribute ((unused)))
375 /* Handle the --create-dep option */
382 static void OptCPU (const char* Opt, const char* Arg)
383 /* Handle the --cpu option */
386 if (CPU != CPU_6502 && CPU != CPU_65C02) {
387 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
393 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
394 /* Handle the --data-name option */
396 /* Check for a valid name */
400 NewSegName (SEG_DATA, Arg);
405 static void OptDebug (const char* Opt attribute ((unused)),
406 const char* Arg attribute ((unused)))
407 /* Compiler debug mode */
414 static void OptDebugInfo (const char* Opt attribute ((unused)),
415 const char* Arg attribute ((unused)))
416 /* Add debug info to the object file */
423 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
424 /* Debug optimization steps */
430 FILE* F = fopen (Arg, "r");
432 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
435 /* Read line by line, ignore empty lines and switch optimization
438 while (fgets (Buf, sizeof (Buf), F) != 0) {
440 /* Remove trailing control chars. This will also remove the
443 unsigned Len = strlen (Buf);
444 while (Len > 0 && IsControl (Buf[Len-1])) {
449 /* Get a pointer to the buffer and remove leading white space */
451 while (IsBlank (*Line)) {
455 /* Check the first character and enable/disable the step or
463 /* Empty or comment line */
482 /* Close the file, no error check here since we were just reading and
483 * this is only a debug function.
490 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
491 /* Disable an optimization step */
498 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
499 /* Enable an optimization step */
506 static void OptHelp (const char* Opt attribute ((unused)),
507 const char* Arg attribute ((unused)))
508 /* Print usage information and exit */
516 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
517 /* Add an include search path */
519 AddIncludePath (Arg, INC_SYS | INC_USER);
524 static void OptListOptSteps (const char* Opt attribute ((unused)),
525 const char* Arg attribute ((unused)))
526 /* List all optimizer steps */
528 /* List the optimizer steps */
529 ListOptSteps (stdout);
537 static void OptRegisterSpace (const char* Opt, const char* Arg)
538 /* Handle the --register-space option */
540 /* Numeric argument expected */
541 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
542 AbEnd ("Argument for option %s is invalid", Opt);
548 static void OptRegisterVars (const char* Opt attribute ((unused)),
549 const char* Arg attribute ((unused)))
550 /* Handle the --register-vars option */
557 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
558 /* Handle the --rodata-name option */
560 /* Check for a valid name */
564 NewSegName (SEG_RODATA, Arg);
569 static void OptSignedChars (const char* Opt attribute ((unused)),
570 const char* Arg attribute ((unused)))
571 /* Make default characters signed */
578 static void OptStaticLocals (const char* Opt attribute ((unused)),
579 const char* Arg attribute ((unused)))
580 /* Place local variables in static storage */
587 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
588 /* Set the target system */
595 static void OptVerbose (const char* Opt attribute ((unused)),
596 const char* Arg attribute ((unused)))
597 /* Increase verbosity */
604 static void OptVersion (const char* Opt attribute ((unused)),
605 const char* Arg attribute ((unused)))
606 /* Print the assembler version */
610 VER_MAJOR, VER_MINOR, VER_PATCH);
615 int main (int argc, char* argv[])
617 /* Program long options */
618 static const LongOpt OptTab[] = {
619 { "--add-source", 0, OptAddSource },
620 { "--ansi", 0, OptAnsi },
621 { "--bss-name", 1, OptBssName },
622 { "--check-stack", 0, OptCheckStack },
623 { "--code-name", 1, OptCodeName },
624 { "--codesize", 1, OptCodeSize },
625 { "--cpu", 1, OptCPU },
626 { "--create-dep", 0, OptCreateDep },
627 { "--data-name", 1, OptDataName },
628 { "--debug", 0, OptDebug },
629 { "--debug-info", 0, OptDebugInfo },
630 { "--debug-opt", 1, OptDebugOpt },
631 { "--disable-opt", 1, OptDisableOpt },
632 { "--enable-opt", 1, OptEnableOpt, },
633 { "--help", 0, OptHelp },
634 { "--include-dir", 1, OptIncludeDir },
635 { "--list-opt-steps", 0, OptListOptSteps },
636 { "--register-space", 1, OptRegisterSpace },
637 { "--register-vars", 0, OptRegisterVars },
638 { "--rodata-name", 1, OptRodataName },
639 { "--signed-chars", 0, OptSignedChars },
640 { "--static-locals", 0, OptStaticLocals },
641 { "--target", 1, OptTarget },
642 { "--verbose", 0, OptVerbose },
643 { "--version", 0, OptVersion },
648 /* Initialize the output file name */
649 const char* OutputFile = 0;
650 const char* InputFile = 0;
652 /* Initialize the cmdline module */
653 InitCmdLine (&argc, &argv, "cc65");
655 /* Initialize the default segment names */
658 /* Initialize the include search paths */
661 /* Parse the command line */
663 while (I < ArgCount) {
667 /* Get the argument */
668 const char* Arg = ArgVec[I];
670 /* Check for an option */
671 if (Arg [0] == '-') {
676 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
689 OptDebugInfo (Arg, 0);
693 OptSignedChars (Arg, 0);
697 OutputFile = GetArg (&I, 2);
701 OptRegisterVars (Arg, 0);
705 OptTarget (Arg, GetArg (&I, 2));
709 OptCreateDep (Arg, 0);
725 OptStaticLocals (Arg, 0);
735 DefineSym (GetArg (&I, 2));
739 OptIncludeDir (Arg, GetArg (&I, 2));
748 sscanf (P, "%lx", (long*) &OptDisable);
752 CodeSizeFactor = 200;
765 OptAddSource (Arg, 0);
782 fprintf (stderr, "additional file specs ignored\n");
792 /* Did we have a file spec on the command line? */
793 if (InputFile == 0) {
794 AbEnd ("No input files");
797 /* Create the output file name if it was not explicitly given */
798 if (OutputFile == 0) {
799 OutputFile = MakeFilename (InputFile, ".s");
802 /* If no CPU given, use the default CPU for the target */
803 if (CPU == CPU_UNKNOWN) {
804 if (Target != TGT_UNKNOWN) {
805 CPU = DefaultCPU[Target];
814 /* Create the output file if we didn't had any errors */
815 if (ErrorCount == 0 || Debug) {
818 FILE* F = fopen (OutputFile, "w");
820 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
823 /* Write the output to the file */
826 /* Close the file, check for errors */
827 if (fclose (F) != 0) {
829 Fatal ("Cannot write to output file (disk full?)");
832 /* Create dependencies if requested */
834 DoCreateDep (OutputFile);
839 /* Return an apropriate exit code */
840 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;