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 and exit\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 DefineNumericMacro ("__CBM__", 1);
128 DefineNumericMacro (sys, 1);
133 static void SetSys (const char* Sys)
134 /* Define a target system */
136 switch (Target = FindTarget (Sys)) {
142 DefineNumericMacro ("__ATARI__", 1);
158 cbmsys ("__PLUS4__");
162 cbmsys ("__CBM510__");
166 cbmsys ("__CBM610__");
174 DefineNumericMacro ("__BBC__", 1);
178 DefineNumericMacro ("__APPLE2__", 1);
182 /* Do not handle as a CBM system */
183 DefineNumericMacro ("__GEOS__", 1);
187 AbEnd ("Unknown target system type");
190 /* Initialize the translation tables for the target system */
196 static void DoCreateDep (const char* OutputName)
197 /* Create the dependency file */
199 /* Make the dependency file name from the output file name */
200 char* DepName = MakeFilename (OutputName, ".u");
203 FILE* F = fopen (DepName, "w");
205 Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
208 /* Write the dependencies to the file */
209 WriteDependencies (F, OutputName);
211 /* Close the file, check for errors */
212 if (fclose (F) != 0) {
214 Fatal ("Cannot write to dependeny file (disk full?)");
223 static void DefineSym (const char* Def)
224 /* Define a symbol on the command line */
228 /* The symbol must start with a character or underline */
229 if (Def [0] != '_' && !IsAlpha (Def [0])) {
233 /* Check the symbol name */
234 while (IsAlNum (*P) || *P == '_') {
238 /* Do we have a value given? */
243 /* No value given. Define the macro with the value 1 */
244 DefineNumericMacro (Def, 1);
246 /* We have a value, P points to the '=' character. Since the argument
247 * is const, create a copy and replace the '=' in the copy by a zero
251 unsigned Len = strlen (Def)+1;
252 char* S = (char*) xmalloc (Len);
253 memcpy (S, Def, Len);
257 /* Define this as a macro */
258 DefineTextMacro (S, Q);
260 /* Release the allocated memory */
267 static void CheckSegName (const char* Seg)
268 /* Abort if the given name is not a valid segment name */
270 /* Print an error and abort if the name is not ok */
271 if (!ValidSegName (Seg)) {
272 AbEnd ("Segment name `%s' is invalid", Seg);
278 static void OptAddSource (const char* Opt, const char* Arg)
279 /* Add source lines as comments in generated assembler file */
286 static void OptAnsi (const char* Opt, const char* Arg)
287 /* Compile in strict ANSI mode */
294 static void OptBssName (const char* Opt, const char* Arg)
295 /* Handle the --bss-name option */
297 /* Check for a valid name */
301 NewSegName (SEG_BSS, Arg);
306 static void OptCheckStack (const char* Opt, const char* Arg)
307 /* Handle the --check-stack option */
314 static void OptCodeName (const char* Opt, const char* Arg)
315 /* Handle the --code-name option */
317 /* Check for a valid name */
321 NewSegName (SEG_CODE, Arg);
326 static void OptCodeSize (const char* Opt, const char* Arg)
327 /* Handle the --codesize option */
329 /* Numeric argument expected */
330 if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
331 CodeSizeFactor < 100 ||
332 CodeSizeFactor > 1000) {
333 AbEnd ("Argument for %s is invalid", Opt);
339 static void OptCreateDep (const char* Opt, const char* Arg)
340 /* Handle the --create-dep option */
347 static void OptCPU (const char* Opt, const char* Arg)
348 /* Handle the --cpu option */
350 if (strcmp (Arg, "6502") == 0) {
352 } else if (strcmp (Arg, "65C02") == 0) {
355 AbEnd ("Invalid CPU: `%s'", Arg);
361 static void OptDataName (const char* Opt, const char* Arg)
362 /* Handle the --code-name option */
364 /* Check for a valid name */
368 NewSegName (SEG_DATA, Arg);
373 static void OptDebug (const char* Opt, const char* Arg)
374 /* Compiler debug mode */
381 static void OptDebugInfo (const char* Opt, const char* Arg)
382 /* Add debug info to the object file */
389 static void OptDebugOpt (const char* Opt, const char* Arg)
390 /* Debug optimization steps */
396 FILE* F = fopen (Arg, "r");
398 AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
401 /* Read line by line, ignore empty lines and switch optimization
404 while (fgets (Buf, sizeof (Buf), F) != 0) {
406 /* Remove trailing control chars. This will also remove the
409 unsigned Len = strlen (Buf);
410 while (Len > 0 && IsControl (Buf[Len-1])) {
415 /* Get a pointer to the buffer and remove leading white space */
417 while (IsBlank (*Line)) {
421 /* Check the first character and enable/disable the step or
429 /* Empty or comment line */
448 /* Close the file, no error check here since we were just reading and
449 * this is only a debug function.
456 static void OptDisableOpt (const char* Opt, const char* Arg)
457 /* Disable an optimization step */
464 static void OptEnableOpt (const char* Opt, const char* Arg)
465 /* Enable an optimization step */
472 static void OptHelp (const char* Opt, const char* Arg)
473 /* Print usage information and exit */
481 static void OptIncludeDir (const char* Opt, const char* Arg)
482 /* Add an include search path */
484 AddIncludePath (Arg, INC_SYS | INC_USER);
489 static void OptListOptSteps (const char* Opt, const char* Arg)
490 /* List all optimizer steps */
492 /* List the optimizer steps */
493 ListOptSteps (stdout);
501 static void OptRodataName (const char* Opt, const char* Arg)
502 /* Handle the --rodata-name option */
504 /* Check for a valid name */
508 NewSegName (SEG_RODATA, Arg);
513 static void OptSignedChars (const char* Opt, const char* Arg)
514 /* Make default characters signed */
521 static void OptStaticLocals (const char* Opt, const char* Arg)
522 /* Place local variables in static storage */
529 static void OptTarget (const char* Opt, const char* Arg)
530 /* Set the target system */
537 static void OptVerbose (const char* Opt, const char* Arg)
538 /* Increase verbosity */
545 static void OptVersion (const char* Opt, const char* Arg)
546 /* Print the assembler version */
550 VER_MAJOR, VER_MINOR, VER_PATCH);
555 int main (int argc, char* argv[])
557 /* Program long options */
558 static const LongOpt OptTab[] = {
559 { "--add-source", 0, OptAddSource },
560 { "--ansi", 0, OptAnsi },
561 { "--bss-name", 1, OptBssName },
562 { "--check-stack", 0, OptCheckStack },
563 { "--code-name", 1, OptCodeName },
564 { "--codesize", 1, OptCodeSize },
565 { "--create-dep", 0, OptCreateDep },
566 { "--cpu", 1, OptCPU },
567 { "--data-name", 1, OptDataName },
568 { "--debug", 0, OptDebug },
569 { "--debug-info", 0, OptDebugInfo },
570 { "--debug-opt", 1, OptDebugOpt },
571 { "--disable-opt", 1, OptDisableOpt },
572 { "--enable-opt", 1, OptEnableOpt, },
573 { "--help", 0, OptHelp },
574 { "--include-dir", 1, OptIncludeDir },
575 { "--list-opt-steps", 0, OptListOptSteps },
576 { "--rodata-name", 1, OptRodataName },
577 { "--signed-chars", 0, OptSignedChars },
578 { "--static-locals", 0, OptStaticLocals },
579 { "--target", 1, OptTarget },
580 { "--verbose", 0, OptVerbose },
581 { "--version", 0, OptVersion },
586 /* Initialize the output file name */
587 const char* OutputFile = 0;
588 const char* InputFile = 0;
590 /* Initialize the cmdline module */
591 InitCmdLine (&argc, &argv, "cc65");
593 /* Initialize the default segment names */
596 /* Parse the command line */
598 while (I < (int)ArgCount) {
602 /* Get the argument */
603 const char* Arg = ArgVec[I];
605 /* Check for an option */
606 if (Arg [0] == '-') {
611 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
624 OptDebugInfo (Arg, 0);
628 OptSignedChars (Arg, 0);
632 OutputFile = GetArg (&I, 2);
636 OptTarget (Arg, GetArg (&I, 2));
640 OptCreateDep (Arg, 0);
656 OptStaticLocals (Arg, 0);
666 DefineSym (GetArg (&I, 2));
670 OptIncludeDir (Arg, GetArg (&I, 2));
679 sscanf (P, "%lx", (long*) &OptDisable);
683 CodeSizeFactor = 200;
696 OptAddSource (Arg, 0);
713 fprintf (stderr, "additional file specs ignored\n");
723 /* Did we have a file spec on the command line? */
724 if (InputFile == 0) {
725 AbEnd ("No input files");
728 /* Create the output file name if it was not explicitly given */
729 if (OutputFile == 0) {
730 OutputFile = MakeFilename (InputFile, ".s");
736 /* Create the output file if we didn't had any errors */
737 if (ErrorCount == 0 || Debug) {
742 /* Optimize the output if requested */
749 F = fopen (OutputFile, "w");
751 Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
754 /* Write the output to the file */
757 /* Close the file, check for errors */
758 if (fclose (F) != 0) {
760 Fatal ("Cannot write to output file (disk full?)");
763 /* Create dependencies if requested */
765 DoCreateDep (OutputFile);
770 /* Return an apropriate exit code */
771 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;