1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2003 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"
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static void Usage (void)
77 /* Print usage information to stderr */
80 "Usage: %s [options] file\n"
82 " -A\t\t\tStrict ANSI mode\n"
83 " -Cl\t\t\tMake local variables static\n"
84 " -Dsym[=defn]\t\tDefine a symbol\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 " --ansi\t\tStrict ANSI mode\n"
106 " --bss-name seg\tSet the name of the BSS segment\n"
107 " --check-stack\t\tGenerate stack overflow checks\n"
108 " --code-name seg\tSet the name of the CODE segment\n"
109 " --codesize x\t\tAccept larger code by factor x\n"
110 " --cpu type\t\tSet cpu type (6502, 65c02)\n"
111 " --create-dep\t\tCreate a make dependency file\n"
112 " --data-name seg\tSet the name of the DATA segment\n"
113 " --debug\t\tDebug mode\n"
114 " --debug-info\t\tAdd debug info to object file\n"
115 " --debug-opt name\tDebug optimization steps\n"
116 " --disable-opt name\tDisable an optimization step\n"
117 " --enable-opt name\tEnable an optimization step\n"
118 " --forget-inc-paths\tForget include search paths\n"
119 " --help\t\tHelp (this text)\n"
120 " --include-dir dir\tSet an include directory search path\n"
121 " --list-opt-steps\tList all optimizer steps and exit\n"
122 " --memory-model model\tSet the memory model\n"
123 " --register-space b\tSet space available for register variables\n"
124 " --register-vars\tEnable register variables\n"
125 " --rodata-name seg\tSet the name of the RODATA segment\n"
126 " --signed-chars\tDefault characters are signed\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",
136 static void cbmsys (const char* sys)
137 /* Define a CBM system */
139 DefineNumericMacro ("__CBM__", 1);
140 DefineNumericMacro (sys, 1);
145 static void SetSys (const char* Sys)
146 /* Define a target system */
148 switch (Target = FindTarget (Sys)) {
154 AbEnd ("Cannot use `module' as a target for the compiler");
158 DefineNumericMacro ("__ATARI__", 1);
170 cbmsys ("__VIC20__");
182 cbmsys ("__PLUS4__");
186 cbmsys ("__CBM510__");
190 cbmsys ("__CBM610__");
198 DefineNumericMacro ("__BBC__", 1);
202 DefineNumericMacro ("__APPLE2__", 1);
206 /* Do not handle as a CBM system */
207 DefineNumericMacro ("__GEOS__", 1);
211 DefineNumericMacro ("__LUNIX__", 1);
215 DefineNumericMacro ("__ATMOS__", 1);
219 DefineNumericMacro ("__NES__", 1);
222 case TGT_SUPERVISION:
223 DefineNumericMacro ("__SUPERVISION__", 1);
227 AbEnd ("Unknown target system type %d", Target);
230 /* Initialize the translation tables for the target system */
236 static void DoCreateDep (const char* OutputName)
237 /* Create the dependency file */
239 /* Make the dependency file name from the output file name */
240 char* DepName = MakeFilename (OutputName, ".u");
243 FILE* F = fopen (DepName, "w");
245 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
248 /* Write the dependencies to the file */
249 WriteDependencies (F, OutputName);
251 /* Close the file, check for errors */
252 if (fclose (F) != 0) {
254 Fatal ("Cannot write to dependeny file (disk full?)");
263 static void DefineSym (const char* Def)
264 /* Define a symbol on the command line */
268 /* The symbol must start with a character or underline */
269 if (Def [0] != '_' && !IsAlpha (Def [0])) {
273 /* Check the symbol name */
274 while (IsAlNum (*P) || *P == '_') {
278 /* Do we have a value given? */
283 /* No value given. Define the macro with the value 1 */
284 DefineNumericMacro (Def, 1);
286 /* We have a value, P points to the '=' character. Since the argument
287 * is const, create a copy and replace the '=' in the copy by a zero
291 unsigned Len = strlen (Def)+1;
292 char* S = (char*) xmalloc (Len);
293 memcpy (S, Def, Len);
297 /* Define this as a macro */
298 DefineTextMacro (S, Q);
300 /* Release the allocated memory */
307 static void CheckSegName (const char* Seg)
308 /* Abort if the given name is not a valid segment name */
310 /* Print an error and abort if the name is not ok */
311 if (!ValidSegName (Seg)) {
312 AbEnd ("Segment name `%s' is invalid", Seg);
318 static void OptAddSource (const char* Opt attribute ((unused)),
319 const char* Arg attribute ((unused)))
320 /* Add source lines as comments in generated assembler file */
327 static void OptAnsi (const char* Opt attribute ((unused)),
328 const char* Arg attribute ((unused)))
329 /* Compile in strict ANSI mode */
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 NewSegName (SEG_BSS, Arg);
348 static void OptCheckStack (const char* Opt attribute ((unused)),
349 const char* Arg attribute ((unused)))
350 /* Handle the --check-stack option */
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 NewSegName (SEG_CODE, Arg);
369 static void OptCodeSize (const char* Opt, const char* Arg)
370 /* Handle the --codesize option */
372 /* Numeric argument expected */
373 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
374 CodeSizeFactor < 100 ||
375 CodeSizeFactor > 1000) {
376 AbEnd ("Argument for %s is invalid", Opt);
382 static void OptCreateDep (const char* Opt attribute ((unused)),
383 const char* Arg attribute ((unused)))
384 /* Handle the --create-dep option */
391 static void OptCPU (const char* Opt, const char* Arg)
392 /* Handle the --cpu option */
394 /* Find the CPU from the given name */
396 if (CPU != CPU_6502 && CPU != CPU_65C02) {
397 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
403 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
404 /* Handle the --data-name option */
406 /* Check for a valid name */
410 NewSegName (SEG_DATA, Arg);
415 static void OptDebug (const char* Opt attribute ((unused)),
416 const char* Arg attribute ((unused)))
417 /* Compiler debug mode */
424 static void OptDebugInfo (const char* Opt attribute ((unused)),
425 const char* Arg attribute ((unused)))
426 /* Add debug info to the object file */
433 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
434 /* Debug optimization steps */
440 FILE* F = fopen (Arg, "r");
442 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
445 /* Read line by line, ignore empty lines and switch optimization
448 while (fgets (Buf, sizeof (Buf), F) != 0) {
450 /* Remove trailing control chars. This will also remove the
453 unsigned Len = strlen (Buf);
454 while (Len > 0 && IsControl (Buf[Len-1])) {
459 /* Get a pointer to the buffer and remove leading white space */
461 while (IsBlank (*Line)) {
465 /* Check the first character and enable/disable the step or
473 /* Empty or comment line */
492 /* Close the file, no error check here since we were just reading and
493 * this is only a debug function.
500 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
501 /* Disable an optimization step */
508 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
509 /* Enable an optimization step */
516 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
517 const char* Arg attribute ((unused)))
518 /* Forget all currently defined include paths */
520 ForgetAllIncludePaths ();
525 static void OptHelp (const char* Opt attribute ((unused)),
526 const char* Arg attribute ((unused)))
527 /* Print usage information and exit */
535 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
536 /* Add an include search path */
538 AddIncludePath (Arg, INC_SYS | INC_USER);
543 static void OptListOptSteps (const char* Opt attribute ((unused)),
544 const char* Arg attribute ((unused)))
545 /* List all optimizer steps */
547 /* List the optimizer steps */
548 ListOptSteps (stdout);
556 static void OptMemoryModel (const char* Opt, const char* Arg)
557 /* Set the memory model */
561 /* Check the current memory model */
562 if (MemoryModel != MMODEL_UNKNOWN) {
563 AbEnd ("Cannot use option `%s' twice", Opt);
566 /* Translate the memory model name and check it */
567 M = FindMemoryModel (Arg);
568 if (M == MMODEL_UNKNOWN) {
569 AbEnd ("Unknown memory model: %s", Arg);
570 } else if (M == MMODEL_HUGE) {
571 AbEnd ("Unsupported memory model: %s", Arg);
574 /* Set the memory model */
580 static void OptRegisterSpace (const char* Opt, const char* Arg)
581 /* Handle the --register-space option */
583 /* Numeric argument expected */
584 if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
585 AbEnd ("Argument for option %s is invalid", Opt);
591 static void OptRegisterVars (const char* Opt attribute ((unused)),
592 const char* Arg attribute ((unused)))
593 /* Handle the --register-vars option */
600 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
601 /* Handle the --rodata-name option */
603 /* Check for a valid name */
607 NewSegName (SEG_RODATA, Arg);
612 static void OptSignedChars (const char* Opt attribute ((unused)),
613 const char* Arg attribute ((unused)))
614 /* Make default characters signed */
621 static void OptStaticLocals (const char* Opt attribute ((unused)),
622 const char* Arg attribute ((unused)))
623 /* Place local variables in static storage */
630 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
631 /* Set the target system */
638 static void OptVerbose (const char* Opt attribute ((unused)),
639 const char* Arg attribute ((unused)))
640 /* Increase verbosity */
647 static void OptVersion (const char* Opt attribute ((unused)),
648 const char* Arg attribute ((unused)))
649 /* Print the assembler version */
653 VER_MAJOR, VER_MINOR, VER_PATCH);
658 int main (int argc, char* argv[])
660 /* Program long options */
661 static const LongOpt OptTab[] = {
662 { "--add-source", 0, OptAddSource },
663 { "--ansi", 0, OptAnsi },
664 { "--bss-name", 1, OptBssName },
665 { "--check-stack", 0, OptCheckStack },
666 { "--code-name", 1, OptCodeName },
667 { "--codesize", 1, OptCodeSize },
668 { "--cpu", 1, OptCPU },
669 { "--create-dep", 0, OptCreateDep },
670 { "--data-name", 1, OptDataName },
671 { "--debug", 0, OptDebug },
672 { "--debug-info", 0, OptDebugInfo },
673 { "--debug-opt", 1, OptDebugOpt },
674 { "--disable-opt", 1, OptDisableOpt },
675 { "--enable-opt", 1, OptEnableOpt },
676 { "--forget-inc-paths", 0, OptForgetIncPaths },
677 { "--help", 0, OptHelp },
678 { "--include-dir", 1, OptIncludeDir },
679 { "--list-opt-steps", 0, OptListOptSteps },
680 { "--memory-model", 1, OptMemoryModel },
681 { "--register-space", 1, OptRegisterSpace },
682 { "--register-vars", 0, OptRegisterVars },
683 { "--rodata-name", 1, OptRodataName },
684 { "--signed-chars", 0, OptSignedChars },
685 { "--static-locals", 0, OptStaticLocals },
686 { "--target", 1, OptTarget },
687 { "--verbose", 0, OptVerbose },
688 { "--version", 0, OptVersion },
693 /* Initialize the output file name */
694 const char* OutputFile = 0;
695 const char* InputFile = 0;
697 /* Initialize the cmdline module */
698 InitCmdLine (&argc, &argv, "cc65");
700 /* Initialize the default segment names */
703 /* Initialize the include search paths */
706 /* Parse the command line */
708 while (I < ArgCount) {
712 /* Get the argument */
713 const char* Arg = ArgVec[I];
715 /* Check for an option */
716 if (Arg [0] == '-') {
721 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
734 OptDebugInfo (Arg, 0);
738 OptSignedChars (Arg, 0);
742 OutputFile = GetArg (&I, 2);
746 OptRegisterVars (Arg, 0);
750 OptTarget (Arg, GetArg (&I, 2));
754 OptCreateDep (Arg, 0);
770 OptStaticLocals (Arg, 0);
780 DefineSym (GetArg (&I, 2));
784 OptIncludeDir (Arg, GetArg (&I, 2));
793 sscanf (P, "%lx", (long*) &OptDisable);
797 CodeSizeFactor = 200;
810 OptAddSource (Arg, 0);
827 fprintf (stderr, "additional file specs ignored\n");
837 /* Did we have a file spec on the command line? */
838 if (InputFile == 0) {
839 AbEnd ("No input files");
842 /* Create the output file name if it was not explicitly given */
843 if (OutputFile == 0) {
844 OutputFile = MakeFilename (InputFile, ".s");
847 /* If no CPU given, use the default CPU for the target */
848 if (CPU == CPU_UNKNOWN) {
849 if (Target != TGT_UNKNOWN) {
850 CPU = DefaultCPU[Target];
859 /* Create the output file if we didn't had any errors */
860 if (ErrorCount == 0 || Debug) {
863 FILE* F = fopen (OutputFile, "w");
865 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
868 /* Write the output to the file */
871 /* Close the file, check for errors */
872 if (fclose (F) != 0) {
874 Fatal ("Cannot write to output file (disk full?)");
877 /* Create dependencies if requested */
879 DoCreateDep (OutputFile);
884 /* Return an apropriate exit code */
885 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;