1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000-2002 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 " -A\t\t\tStrict ANSI mode\n"
79 " -Cl\t\t\tMake local variables static\n"
80 " -Dsym[=defn]\t\tDefine a symbol\n"
81 " -I dir\t\tSet an include directory search path\n"
82 " -O\t\t\tOptimize code\n"
83 " -Oi\t\t\tOptimize code, inline more code\n"
84 " -Or\t\t\tEnable register variables\n"
85 " -Os\t\t\tInline some known functions\n"
86 " -T\t\t\tInclude source as comment\n"
87 " -V\t\t\tPrint the compiler version number\n"
88 " -W\t\t\tSuppress warnings\n"
89 " -d\t\t\tDebug mode\n"
90 " -g\t\t\tAdd debug info to object file\n"
91 " -h\t\t\tHelp (this text)\n"
92 " -j\t\t\tDefault characters are signed\n"
93 " -o name\t\tName the output file\n"
94 " -t sys\t\tSet the target system\n"
95 " -v\t\t\tIncrease verbosity\n"
98 " --add-source\t\tInclude source as comment\n"
99 " --ansi\t\tStrict ANSI mode\n"
100 " --bss-name seg\tSet the name of the BSS segment\n"
101 " --check-stack\t\tGenerate stack overflow checks\n"
102 " --code-name seg\tSet the name of the CODE segment\n"
103 " --codesize x\t\tAccept larger code by factor x\n"
104 " --cpu type\t\tSet cpu type\n"
105 " --create-dep\t\tCreate a make dependency file\n"
106 " --data-name seg\tSet the name of the DATA segment\n"
107 " --debug\t\tDebug mode\n"
108 " --debug-info\t\tAdd debug info to object file\n"
109 " --debug-opt name\tDebug optimization steps\n"
110 " --disable-opt name\tDisable an optimization step\n"
111 " --enable-opt name\tEnable an optimization step\n"
112 " --help\t\tHelp (this text)\n"
113 " --include-dir dir\tSet an include directory search path\n"
114 " --list-opt-steps\tList all optimizer steps and exit\n"
115 " --rodata-name seg\tSet the name of the RODATA segment\n"
116 " --signed-chars\tDefault characters are signed\n"
117 " --static-locals\tMake local variables static\n"
118 " --target sys\t\tSet the target system\n"
119 " --verbose\t\tIncrease verbosity\n"
120 " --version\t\tPrint the compiler version number\n",
126 static void cbmsys (const char* sys)
127 /* Define a CBM system */
129 DefineNumericMacro ("__CBM__", 1);
130 DefineNumericMacro (sys, 1);
135 static void SetSys (const char* Sys)
136 /* Define a target system */
138 switch (Target = FindTarget (Sys)) {
144 AbEnd ("Cannot use `module' as a target for the compiler");
148 DefineNumericMacro ("__ATARI__", 1);
160 cbmsys ("__VIC20__");
172 cbmsys ("__PLUS4__");
176 cbmsys ("__CBM510__");
180 cbmsys ("__CBM610__");
188 DefineNumericMacro ("__BBC__", 1);
192 DefineNumericMacro ("__APPLE2__", 1);
196 /* Do not handle as a CBM system */
197 DefineNumericMacro ("__GEOS__", 1);
201 DefineNumericMacro ("__LUNIX__", 1);
205 DefineNumericMacro ("__ATMOS__", 1);
209 AbEnd ("Unknown target system type");
212 /* Initialize the translation tables for the target system */
218 static void DoCreateDep (const char* OutputName)
219 /* Create the dependency file */
221 /* Make the dependency file name from the output file name */
222 char* DepName = MakeFilename (OutputName, ".u");
225 FILE* F = fopen (DepName, "w");
227 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
230 /* Write the dependencies to the file */
231 WriteDependencies (F, OutputName);
233 /* Close the file, check for errors */
234 if (fclose (F) != 0) {
236 Fatal ("Cannot write to dependeny file (disk full?)");
245 static void DefineSym (const char* Def)
246 /* Define a symbol on the command line */
250 /* The symbol must start with a character or underline */
251 if (Def [0] != '_' && !IsAlpha (Def [0])) {
255 /* Check the symbol name */
256 while (IsAlNum (*P) || *P == '_') {
260 /* Do we have a value given? */
265 /* No value given. Define the macro with the value 1 */
266 DefineNumericMacro (Def, 1);
268 /* We have a value, P points to the '=' character. Since the argument
269 * is const, create a copy and replace the '=' in the copy by a zero
273 unsigned Len = strlen (Def)+1;
274 char* S = (char*) xmalloc (Len);
275 memcpy (S, Def, Len);
279 /* Define this as a macro */
280 DefineTextMacro (S, Q);
282 /* Release the allocated memory */
289 static void CheckSegName (const char* Seg)
290 /* Abort if the given name is not a valid segment name */
292 /* Print an error and abort if the name is not ok */
293 if (!ValidSegName (Seg)) {
294 AbEnd ("Segment name `%s' is invalid", Seg);
300 static void OptAddSource (const char* Opt attribute ((unused)),
301 const char* Arg attribute ((unused)))
302 /* Add source lines as comments in generated assembler file */
309 static void OptAnsi (const char* Opt attribute ((unused)),
310 const char* Arg attribute ((unused)))
311 /* Compile in strict ANSI mode */
318 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
319 /* Handle the --bss-name option */
321 /* Check for a valid name */
325 NewSegName (SEG_BSS, Arg);
330 static void OptCheckStack (const char* Opt attribute ((unused)),
331 const char* Arg attribute ((unused)))
332 /* Handle the --check-stack option */
339 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
340 /* Handle the --code-name option */
342 /* Check for a valid name */
346 NewSegName (SEG_CODE, Arg);
351 static void OptCodeSize (const char* Opt, const char* Arg)
352 /* Handle the --codesize option */
354 /* Numeric argument expected */
355 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
356 CodeSizeFactor < 100 ||
357 CodeSizeFactor > 1000) {
358 AbEnd ("Argument for %s is invalid", Opt);
364 static void OptCreateDep (const char* Opt attribute ((unused)),
365 const char* Arg attribute ((unused)))
366 /* Handle the --create-dep option */
373 static void OptCPU (const char* Opt, const char* Arg)
374 /* Handle the --cpu option */
376 if (strcmp (Arg, "6502") == 0) {
378 } else if (strcmp (Arg, "65C02") == 0) {
381 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
387 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
388 /* Handle the --data-name option */
390 /* Check for a valid name */
394 NewSegName (SEG_DATA, Arg);
399 static void OptDebug (const char* Opt attribute ((unused)),
400 const char* Arg attribute ((unused)))
401 /* Compiler debug mode */
408 static void OptDebugInfo (const char* Opt attribute ((unused)),
409 const char* Arg attribute ((unused)))
410 /* Add debug info to the object file */
417 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
418 /* Debug optimization steps */
424 FILE* F = fopen (Arg, "r");
426 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
429 /* Read line by line, ignore empty lines and switch optimization
432 while (fgets (Buf, sizeof (Buf), F) != 0) {
434 /* Remove trailing control chars. This will also remove the
437 unsigned Len = strlen (Buf);
438 while (Len > 0 && IsControl (Buf[Len-1])) {
443 /* Get a pointer to the buffer and remove leading white space */
445 while (IsBlank (*Line)) {
449 /* Check the first character and enable/disable the step or
457 /* Empty or comment line */
476 /* Close the file, no error check here since we were just reading and
477 * this is only a debug function.
484 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
485 /* Disable an optimization step */
492 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
493 /* Enable an optimization step */
500 static void OptHelp (const char* Opt attribute ((unused)),
501 const char* Arg attribute ((unused)))
502 /* Print usage information and exit */
510 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
511 /* Add an include search path */
513 AddIncludePath (Arg, INC_SYS | INC_USER);
518 static void OptListOptSteps (const char* Opt attribute ((unused)),
519 const char* Arg attribute ((unused)))
520 /* List all optimizer steps */
522 /* List the optimizer steps */
523 ListOptSteps (stdout);
531 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
532 /* Handle the --rodata-name option */
534 /* Check for a valid name */
538 NewSegName (SEG_RODATA, Arg);
543 static void OptSignedChars (const char* Opt attribute ((unused)),
544 const char* Arg attribute ((unused)))
545 /* Make default characters signed */
552 static void OptStaticLocals (const char* Opt attribute ((unused)),
553 const char* Arg attribute ((unused)))
554 /* Place local variables in static storage */
561 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
562 /* Set the target system */
569 static void OptVerbose (const char* Opt attribute ((unused)),
570 const char* Arg attribute ((unused)))
571 /* Increase verbosity */
578 static void OptVersion (const char* Opt attribute ((unused)),
579 const char* Arg attribute ((unused)))
580 /* Print the assembler version */
584 VER_MAJOR, VER_MINOR, VER_PATCH);
589 int main (int argc, char* argv[])
591 /* Program long options */
592 static const LongOpt OptTab[] = {
593 { "--add-source", 0, OptAddSource },
594 { "--ansi", 0, OptAnsi },
595 { "--bss-name", 1, OptBssName },
596 { "--check-stack", 0, OptCheckStack },
597 { "--code-name", 1, OptCodeName },
598 { "--codesize", 1, OptCodeSize },
599 { "--cpu", 1, OptCPU },
600 { "--create-dep", 0, OptCreateDep },
601 { "--data-name", 1, OptDataName },
602 { "--debug", 0, OptDebug },
603 { "--debug-info", 0, OptDebugInfo },
604 { "--debug-opt", 1, OptDebugOpt },
605 { "--disable-opt", 1, OptDisableOpt },
606 { "--enable-opt", 1, OptEnableOpt, },
607 { "--help", 0, OptHelp },
608 { "--include-dir", 1, OptIncludeDir },
609 { "--list-opt-steps", 0, OptListOptSteps },
610 { "--rodata-name", 1, OptRodataName },
611 { "--signed-chars", 0, OptSignedChars },
612 { "--static-locals", 0, OptStaticLocals },
613 { "--target", 1, OptTarget },
614 { "--verbose", 0, OptVerbose },
615 { "--version", 0, OptVersion },
620 /* Initialize the output file name */
621 const char* OutputFile = 0;
622 const char* InputFile = 0;
624 /* Initialize the cmdline module */
625 InitCmdLine (&argc, &argv, "cc65");
627 /* Initialize the default segment names */
630 /* Parse the command line */
632 while (I < ArgCount) {
636 /* Get the argument */
637 const char* Arg = ArgVec[I];
639 /* Check for an option */
640 if (Arg [0] == '-') {
645 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
658 OptDebugInfo (Arg, 0);
662 OptSignedChars (Arg, 0);
666 OutputFile = GetArg (&I, 2);
670 OptTarget (Arg, GetArg (&I, 2));
674 OptCreateDep (Arg, 0);
690 OptStaticLocals (Arg, 0);
700 DefineSym (GetArg (&I, 2));
704 OptIncludeDir (Arg, GetArg (&I, 2));
713 sscanf (P, "%lx", (long*) &OptDisable);
717 CodeSizeFactor = 200;
730 OptAddSource (Arg, 0);
747 fprintf (stderr, "additional file specs ignored\n");
757 /* Did we have a file spec on the command line? */
758 if (InputFile == 0) {
759 AbEnd ("No input files");
762 /* Create the output file name if it was not explicitly given */
763 if (OutputFile == 0) {
764 OutputFile = MakeFilename (InputFile, ".s");
770 /* Create the output file if we didn't had any errors */
771 if (ErrorCount == 0 || Debug) {
776 /* Optimize the output if requested */
783 F = fopen (OutputFile, "w");
785 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
788 /* Write the output to the file */
791 /* Close the file, check for errors */
792 if (fclose (F) != 0) {
794 Fatal ("Cannot write to output file (disk full?)");
797 /* Create dependencies if requested */
799 DoCreateDep (OutputFile);
804 /* Return an apropriate exit code */
805 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;