1 /*****************************************************************************/
5 /* cc65 main program */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 /* Names of the target systems sorted by target name */
71 static const char* TargetNames [] = {
87 /*****************************************************************************/
89 /*****************************************************************************/
93 static void Usage (void)
96 "Usage: %s [options] file\n"
98 " -d\t\t\tDebug mode\n"
99 " -g\t\t\tAdd debug info to object file\n"
100 " -h\t\t\tHelp (this text)\n"
101 " -j\t\t\tDefault characters are signed\n"
102 " -o name\t\tName the output file\n"
103 " -t sys\t\tSet the target system\n"
104 " -v\t\t\tIncrease verbosity\n"
105 " -A\t\t\tStrict ANSI mode\n"
106 " -Cl\t\t\tMake local variables static\n"
107 " -Dsym[=defn]\t\tDefine a symbol\n"
108 " -I dir\t\tSet an include directory search path\n"
109 " -O\t\t\tOptimize code\n"
110 " -Oi\t\t\tOptimize code, inline more code\n"
111 " -Or\t\t\tEnable register variables\n"
112 " -Os\t\t\tInline some known functions\n"
113 " -T\t\t\tInclude source as comment\n"
114 " -V\t\t\tPrint the compiler version number\n"
115 " -W\t\t\tSuppress warnings\n"
118 " --ansi\t\tStrict ANSI mode\n"
119 " --bss-name seg\tSet the name of the BSS segment\n"
120 " --code-name seg\tSet the name of the CODE segment\n"
121 " --cpu type\t\tSet cpu type\n"
122 " --data-name seg\tSet the name of the DATA segment\n"
123 " --debug\t\tDebug mode\n"
124 " --debug-info\t\tAdd debug info to object file\n"
125 " --help\t\tHelp (this text)\n"
126 " --include-dir dir\tSet an include directory search path\n"
127 " --rodata-name seg\tSet the name of the RODATA segment\n"
128 " --signed-chars\tDefault characters are signed\n"
129 " --static-locals\tMake local variables static\n"
130 " --target sys\t\tSet the target system\n"
131 " --verbose\t\tIncrease verbosity\n"
132 " --version\t\tPrint the compiler version number\n",
138 static void cbmsys (const char* sys)
139 /* Define a CBM system */
141 AddNumericMacro ("__CBM__", 1);
142 AddNumericMacro (sys, 1);
147 static int MapSys (const char* Name)
148 /* Map a target name to a system code. Return -1 in case of an error */
152 /* Check for a numeric target */
153 if (isdigit (*Name)) {
154 int Target = atoi (Name);
155 if (Target >= 0 && Target < TGT_COUNT) {
160 /* Check for a target string */
161 for (I = 0; I < TGT_COUNT; ++I) {
162 if (strcmp (TargetNames [I], Name) == 0) {
172 static void SetSys (const char* Sys)
173 /* Define a target system */
175 switch (Target = MapSys (Sys)) {
181 AddNumericMacro ("__ATARI__", 1);
197 cbmsys ("__PLUS4__");
201 cbmsys ("__CBM610__");
209 AddNumericMacro ("__NES__", 1);
213 AddNumericMacro ("__APPLE2__", 1);
217 /* Do not handle as a CBM system */
218 AddNumericMacro ("__GEOS__", 1);
222 AbEnd ("Unknown target system type");
228 static void DefineSym (const char* Def)
229 /* Define a symbol on the command line */
233 /* The symbol must start with a character or underline */
234 if (Def [0] != '_' && !isalpha (Def [0])) {
238 /* Check the symbol name */
239 while (isalnum (*P) || *P == '_') {
243 /* Do we have a value given? */
248 /* No value given. Define the macro with the value 1 */
249 AddNumericMacro (Def, 1);
251 /* We have a value, P points to the '=' character. Since the argument
252 * is const, create a copy and replace the '=' in the copy by a zero
256 unsigned Len = strlen (Def)+1;
257 char* S = xmalloc (Len);
258 memcpy (S, Def, Len);
262 /* Define this as a macro */
265 /* Release the allocated memory */
272 static void CheckSegName (const char* Seg)
273 /* Abort if the given name is not a valid segment name */
275 /* Print an error and abort if the name is not ok */
276 if (!ValidSegName (Seg)) {
277 AbEnd ("Segment name `%s' is invalid", Seg);
283 static void OptAddSource (const char* Opt, const char* Arg)
284 /* Add source lines as comments in generated assembler file */
291 static void OptAnsi (const char* Opt, const char* Arg)
292 /* Compile in strict ANSI mode */
299 static void OptBssName (const char* Opt, const char* Arg)
300 /* Handle the --bss-name option */
302 /* Must have a segment name */
307 /* Check for a valid name */
311 NewSegName (SEG_BSS, Arg);
316 static void OptCodeName (const char* Opt, const char* Arg)
317 /* Handle the --code-name option */
319 /* Must have a segment name */
324 /* Check for a valid name */
328 NewSegName (SEG_CODE, Arg);
333 static void OptCPU (const char* Opt, const char* Arg)
334 /* Handle the --cpu option */
339 if (strcmp (Arg, "6502") == 0) {
341 } else if (strcmp (Arg, "65C02") == 0) {
344 AbEnd ("Invalid CPU: `%s'", Arg);
350 static void OptDataName (const char* Opt, const char* Arg)
351 /* Handle the --code-name option */
353 /* Must have a segment name */
358 /* Check for a valid name */
362 NewSegName (SEG_DATA, Arg);
367 static void OptDebug (const char* Opt, const char* Arg)
368 /* Compiler debug mode */
375 static void OptDebugInfo (const char* Opt, const char* Arg)
376 /* Add debug info to the object file */
383 static void OptHelp (const char* Opt, const char* Arg)
384 /* Print usage information and exit */
392 static void OptIncludeDir (const char* Opt, const char* Arg)
393 /* Add an include search path */
398 AddIncludePath (Arg, INC_SYS | INC_USER);
403 static void OptRodataName (const char* Opt, const char* Arg)
404 /* Handle the --rodata-name option */
406 /* Must have a segment name */
411 /* Check for a valid name */
415 NewSegName (SEG_RODATA, Arg);
420 static void OptSignedChars (const char* Opt, const char* Arg)
421 /* Make default characters signed */
428 static void OptStaticLocals (const char* Opt, const char* Arg)
429 /* Place local variables in static storage */
436 static void OptTarget (const char* Opt, const char* Arg)
437 /* Set the target system */
447 static void OptVerbose (const char* Opt, const char* Arg)
448 /* Increase verbosity */
455 static void OptVersion (const char* Opt, const char* Arg)
456 /* Print the assembler version */
460 VER_MAJOR, VER_MINOR, VER_PATCH);
465 int main (int argc, char* argv[])
467 /* Program long options */
468 static const LongOpt OptTab[] = {
469 { "--add-source", 0, OptAddSource },
470 { "--ansi", 0, OptAnsi },
471 { "--bss-name", 1, OptBssName },
472 { "--code-name", 1, OptCodeName },
473 { "--cpu", 1, OptCPU },
474 { "--data-name", 1, OptDataName },
475 { "--debug", 0, OptDebug },
476 { "--debug-info", 0, OptDebugInfo },
477 { "--help", 0, OptHelp },
478 { "--include-dir", 1, OptIncludeDir },
479 { "--rodata-name", 1, OptRodataName },
480 { "--signed-chars", 0, OptSignedChars },
481 { "--static-locals", 0, OptStaticLocals },
482 { "--target", 1, OptTarget },
483 { "--verbose", 0, OptVerbose },
484 { "--version", 0, OptVersion },
489 /* Initialize the output file name */
490 const char* OutputFile = 0;
491 const char* InputFile = 0;
493 /* Initialize the cmdline module */
494 InitCmdLine (argc, argv, "cc65");
496 /* Initialize the default segment names */
499 /* Parse the command line */
505 /* Get the argument */
506 const char* Arg = argv [I];
508 /* Check for an option */
509 if (Arg [0] == '-') {
514 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
527 OptDebugInfo (Arg, 0);
531 OptSignedChars (Arg, 0);
535 OutputFile = GetArg (&I, 2);
539 OptTarget (Arg, GetArg (&I, 2));
555 OptStaticLocals (Arg, 0);
565 DefineSym (GetArg (&I, 2));
569 OptIncludeDir (Arg, GetArg (&I, 2));
578 sscanf (P, "%lx", (long*) &OptDisable);
594 OptAddSource (Arg, 0);
611 fprintf (stderr, "additional file specs ignored\n");
621 /* Did we have a file spec on the command line? */
622 if (InputFile == 0) {
623 AbEnd ("No input files");
626 /* Open the input file */
627 OpenMainFile (InputFile);
629 /* Create the output file name if it was not explicitly given */
630 if (OutputFile == 0) {
631 OutputFile = MakeFilename (InputFile, ".s");
637 /* Create the output file if we didn't had any errors */
638 if (ErrorCount == 0 || Debug) {
642 /* Optimize the output if requested */
648 F = fopen (OutputFile, "w");
650 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
653 /* Write the output to the file */
656 /* Close the file, check for errors */
657 if (fclose (F) != 0) {
659 Fatal (FAT_CANNOT_WRITE_OUTPUT);
663 /* Return an apropriate exit code */
664 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;