1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void Usage (void)
76 "Usage: %s [options] file\n"
78 " -d\t\t\tDebug mode\n"
79 " -g\t\t\tAdd debug info to object file\n"
80 " -h\t\t\tHelp (this text)\n"
81 " -j\t\t\tDefault characters are signed\n"
82 " -o name\t\tName the output file\n"
83 " -t sys\t\tSet the target system\n"
84 " -v\t\t\tIncrease verbosity\n"
85 " -A\t\t\tStrict ANSI mode\n"
86 " -Cl\t\t\tMake local variables static\n"
87 " -Dsym[=defn]\t\tDefine a symbol\n"
88 " -I dir\t\tSet an include directory search path\n"
89 " -O\t\t\tOptimize code\n"
90 " -Oi\t\t\tOptimize code, inline more code\n"
91 " -Or\t\t\tEnable register variables\n"
92 " -Os\t\t\tInline some known functions\n"
93 " -T\t\t\tInclude source as comment\n"
94 " -V\t\t\tPrint the compiler version number\n"
95 " -W\t\t\tSuppress warnings\n"
98 " --ansi\t\tStrict ANSI mode\n"
99 " --bss-name seg\tSet the name of the BSS segment\n"
100 " --check-stack\t\tGenerate stack overflow checks\n"
101 " --code-name seg\tSet the name of the CODE segment\n"
102 " --codesize x\t\tAccept larger code by factor x\n"
103 " --cpu type\t\tSet cpu type\n"
104 " --data-name seg\tSet the name of the DATA segment\n"
105 " --debug\t\tDebug mode\n"
106 " --debug-info\t\tAdd debug info to object file\n"
107 " --debug-opt name\tDebug optimization steps\n"
108 " --disable-opt name\tDisable an optimization step\n"
109 " --enable-opt name\tEnable an optimization step\n"
110 " --help\t\tHelp (this text)\n"
111 " --include-dir dir\tSet an include directory search path\n"
112 " --list-opt-steps\tList all optimizer steps\n"
113 " --rodata-name seg\tSet the name of the RODATA segment\n"
114 " --signed-chars\tDefault characters are signed\n"
115 " --static-locals\tMake local variables static\n"
116 " --target sys\t\tSet the target system\n"
117 " --verbose\t\tIncrease verbosity\n"
118 " --version\t\tPrint the compiler version number\n",
124 static void cbmsys (const char* sys)
125 /* Define a CBM system */
127 AddNumericMacro ("__CBM__", 1);
128 AddNumericMacro (sys, 1);
133 static void SetSys (const char* Sys)
134 /* Define a target system */
136 switch (Target = FindTarget (Sys)) {
142 AddNumericMacro ("__ATARI__", 1);
158 cbmsys ("__PLUS4__");
162 cbmsys ("__CBM610__");
170 AddNumericMacro ("__BBC__", 1);
174 AddNumericMacro ("__APPLE2__", 1);
178 /* Do not handle as a CBM system */
179 AddNumericMacro ("__GEOS__", 1);
183 AbEnd ("Unknown target system type");
186 /* Initialize the translation tables for the target system */
192 static void DoCreateDep (const char* OutputName)
193 /* Create the dependency file */
195 /* Make the dependency file name from the output file name */
196 char* DepName = MakeFilename (OutputName, ".u");
199 FILE* F = fopen (DepName, "w");
201 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
204 /* Write the dependencies to the file */
205 WriteDependencies (F, OutputName);
207 /* Close the file, check for errors */
208 if (fclose (F) != 0) {
210 Fatal ("Cannot write to dependeny file (disk full?)");
219 static void DefineSym (const char* Def)
220 /* Define a symbol on the command line */
224 /* The symbol must start with a character or underline */
225 if (Def [0] != '_' && !IsAlpha (Def [0])) {
229 /* Check the symbol name */
230 while (IsAlNum (*P) || *P == '_') {
234 /* Do we have a value given? */
239 /* No value given. Define the macro with the value 1 */
240 AddNumericMacro (Def, 1);
242 /* We have a value, P points to the '=' character. Since the argument
243 * is const, create a copy and replace the '=' in the copy by a zero
247 unsigned Len = strlen (Def)+1;
248 char* S = (char*) xmalloc (Len);
249 memcpy (S, Def, Len);
253 /* Define this as a macro */
256 /* Release the allocated memory */
263 static void CheckSegName (const char* Seg)
264 /* Abort if the given name is not a valid segment name */
266 /* Print an error and abort if the name is not ok */
267 if (!ValidSegName (Seg)) {
268 AbEnd ("Segment name `%s' is invalid", Seg);
274 static void OptAddSource (const char* Opt, const char* Arg)
275 /* Add source lines as comments in generated assembler file */
282 static void OptAnsi (const char* Opt, const char* Arg)
283 /* Compile in strict ANSI mode */
290 static void OptBssName (const char* Opt, const char* Arg)
291 /* Handle the --bss-name option */
293 /* Check for a valid name */
297 NewSegName (SEG_BSS, Arg);
302 static void OptCheckStack (const char* Opt, const char* Arg)
303 /* Handle the --check-stack option */
310 static void OptCodeName (const char* Opt, const char* Arg)
311 /* Handle the --code-name option */
313 /* Check for a valid name */
317 NewSegName (SEG_CODE, Arg);
322 static void OptCodeSize (const char* Opt, const char* Arg)
323 /* Handle the --codesize option */
325 /* Numeric argument expected */
326 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
327 CodeSizeFactor < 100 ||
328 CodeSizeFactor > 1000) {
329 AbEnd ("Argument for %s is invalid", Opt);
335 static void OptCreateDep (const char* Opt, const char* Arg)
336 /* Handle the --create-dep option */
343 static void OptCPU (const char* Opt, const char* Arg)
344 /* Handle the --cpu option */
346 if (strcmp (Arg, "6502") == 0) {
348 } else if (strcmp (Arg, "65C02") == 0) {
351 AbEnd ("Invalid CPU: `%s'", Arg);
357 static void OptDataName (const char* Opt, const char* Arg)
358 /* Handle the --code-name option */
360 /* Check for a valid name */
364 NewSegName (SEG_DATA, Arg);
369 static void OptDebug (const char* Opt, const char* Arg)
370 /* Compiler debug mode */
377 static void OptDebugInfo (const char* Opt, const char* Arg)
378 /* Add debug info to the object file */
385 static void OptDebugOpt (const char* Opt, const char* Arg)
386 /* Debug optimization steps */
392 FILE* F = fopen (Arg, "r");
394 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
397 /* Read line by line, ignore empty lines and switch optimization
400 while (fgets (Buf, sizeof (Buf), F) != 0) {
402 /* Remove trailing control chars. This will also remove the
405 unsigned Len = strlen (Buf);
406 while (Len > 0 && IsControl (Buf[Len-1])) {
411 /* Get a pointer to the buffer and remove leading white space */
413 while (IsBlank (*Line)) {
417 /* Check the first character and enable/disable the step or
425 /* Empty or comment line */
444 /* Close the file, no error check here since we were just reading and
445 * this is only a debug function.
452 static void OptDisableOpt (const char* Opt, const char* Arg)
453 /* Disable an optimization step */
460 static void OptEnableOpt (const char* Opt, const char* Arg)
461 /* Enable an optimization step */
468 static void OptHelp (const char* Opt, const char* Arg)
469 /* Print usage information and exit */
477 static void OptIncludeDir (const char* Opt, const char* Arg)
478 /* Add an include search path */
480 AddIncludePath (Arg, INC_SYS | INC_USER);
485 static void OptListOptSteps (const char* Opt, const char* Arg)
486 /* List all optimizer steps */
488 ListOptSteps (stdout);
493 static void OptRodataName (const char* Opt, const char* Arg)
494 /* Handle the --rodata-name option */
496 /* Check for a valid name */
500 NewSegName (SEG_RODATA, Arg);
505 static void OptSignedChars (const char* Opt, const char* Arg)
506 /* Make default characters signed */
513 static void OptStaticLocals (const char* Opt, const char* Arg)
514 /* Place local variables in static storage */
521 static void OptTarget (const char* Opt, const char* Arg)
522 /* Set the target system */
529 static void OptVerbose (const char* Opt, const char* Arg)
530 /* Increase verbosity */
537 static void OptVersion (const char* Opt, const char* Arg)
538 /* Print the assembler version */
542 VER_MAJOR, VER_MINOR, VER_PATCH);
547 int main (int argc, char* argv[])
549 /* Program long options */
550 static const LongOpt OptTab[] = {
551 { "--add-source", 0, OptAddSource },
552 { "--ansi", 0, OptAnsi },
553 { "--bss-name", 1, OptBssName },
554 { "--check-stack", 0, OptCheckStack },
555 { "--code-name", 1, OptCodeName },
556 { "--codesize", 1, OptCodeSize },
557 { "--create-dep", 0, OptCreateDep },
558 { "--cpu", 1, OptCPU },
559 { "--data-name", 1, OptDataName },
560 { "--debug", 0, OptDebug },
561 { "--debug-info", 0, OptDebugInfo },
562 { "--debug-opt", 1, OptDebugOpt },
563 { "--disable-opt", 1, OptDisableOpt },
564 { "--enable-opt", 1, OptEnableOpt, },
565 { "--help", 0, OptHelp },
566 { "--include-dir", 1, OptIncludeDir },
567 { "--list-opt-steps", 0, OptListOptSteps },
568 { "--rodata-name", 1, OptRodataName },
569 { "--signed-chars", 0, OptSignedChars },
570 { "--static-locals", 0, OptStaticLocals },
571 { "--target", 1, OptTarget },
572 { "--verbose", 0, OptVerbose },
573 { "--version", 0, OptVersion },
578 /* Initialize the output file name */
579 const char* OutputFile = 0;
580 const char* InputFile = 0;
582 /* Initialize the cmdline module */
583 InitCmdLine (&argc, &argv, "cc65");
585 /* Initialize the default segment names */
588 /* Parse the command line */
590 while (I < (int)ArgCount) {
594 /* Get the argument */
595 const char* Arg = ArgVec[I];
597 /* Check for an option */
598 if (Arg [0] == '-') {
603 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
616 OptDebugInfo (Arg, 0);
620 OptSignedChars (Arg, 0);
624 OutputFile = GetArg (&I, 2);
628 OptTarget (Arg, GetArg (&I, 2));
632 OptCreateDep (Arg, 0);
648 OptStaticLocals (Arg, 0);
658 DefineSym (GetArg (&I, 2));
662 OptIncludeDir (Arg, GetArg (&I, 2));
671 sscanf (P, "%lx", (long*) &OptDisable);
675 CodeSizeFactor = 200;
688 OptAddSource (Arg, 0);
705 fprintf (stderr, "additional file specs ignored\n");
715 /* Did we have a file spec on the command line? */
716 if (InputFile == 0) {
717 AbEnd ("No input files");
720 /* Create the output file name if it was not explicitly given */
721 if (OutputFile == 0) {
722 OutputFile = MakeFilename (InputFile, ".s");
728 /* Create the output file if we didn't had any errors */
729 if (ErrorCount == 0 || Debug) {
734 /* Optimize the output if requested */
741 F = fopen (OutputFile, "w");
743 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
746 /* Write the output to the file */
749 /* Close the file, check for errors */
750 if (fclose (F) != 0) {
752 Fatal ("Cannot write to output file (disk full?)");
755 /* Create dependencies if requested */
757 DoCreateDep (OutputFile);
762 /* Return an apropriate exit code */
763 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;