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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Usage (void)
75 "Usage: %s [options] file\n"
77 " -d\t\t\tDebug mode\n"
78 " -g\t\t\tAdd debug info to object file\n"
79 " -h\t\t\tHelp (this text)\n"
80 " -j\t\t\tDefault characters are signed\n"
81 " -o name\t\tName the output file\n"
82 " -t sys\t\tSet the target system\n"
83 " -v\t\t\tIncrease verbosity\n"
84 " -A\t\t\tStrict ANSI mode\n"
85 " -Cl\t\t\tMake local variables static\n"
86 " -Dsym[=defn]\t\tDefine a symbol\n"
87 " -I dir\t\tSet an include directory search path\n"
88 " -O\t\t\tOptimize code\n"
89 " -Oi\t\t\tOptimize code, inline more code\n"
90 " -Or\t\t\tEnable register variables\n"
91 " -Os\t\t\tInline some known functions\n"
92 " -T\t\t\tInclude source as comment\n"
93 " -V\t\t\tPrint the compiler version number\n"
94 " -W\t\t\tSuppress warnings\n"
97 " --ansi\t\tStrict ANSI mode\n"
98 " --bss-name seg\tSet the name of the BSS segment\n"
99 " --code-name seg\tSet the name of the CODE segment\n"
100 " --cpu type\t\tSet cpu type\n"
101 " --data-name seg\tSet the name of the DATA segment\n"
102 " --debug\t\tDebug mode\n"
103 " --debug-info\t\tAdd debug info to object file\n"
104 " --help\t\tHelp (this text)\n"
105 " --include-dir dir\tSet an include directory search path\n"
106 " --rodata-name seg\tSet the name of the RODATA segment\n"
107 " --signed-chars\tDefault characters are signed\n"
108 " --static-locals\tMake local variables static\n"
109 " --target sys\t\tSet the target system\n"
110 " --verbose\t\tIncrease verbosity\n"
111 " --version\t\tPrint the compiler version number\n",
117 static void cbmsys (const char* sys)
118 /* Define a CBM system */
120 AddNumericMacro ("__CBM__", 1);
121 AddNumericMacro (sys, 1);
126 static void SetSys (const char* Sys)
127 /* Define a target system */
129 switch (Target = FindTarget (Sys)) {
135 AddNumericMacro ("__ATARI__", 1);
151 cbmsys ("__PLUS4__");
155 cbmsys ("__CBM610__");
163 AddNumericMacro ("__NES__", 1);
167 AddNumericMacro ("__APPLE2__", 1);
171 /* Do not handle as a CBM system */
172 AddNumericMacro ("__GEOS__", 1);
176 AbEnd ("Unknown target system type");
179 /* Initialize the translation tables for the target system */
185 static void DoCreateDep (const char* OutputName)
186 /* Create the dependency file */
188 /* Make the dependency file name from the output file name */
189 char* DepName = MakeFilename (OutputName, ".u");
192 FILE* F = fopen (DepName, "w");
194 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
197 /* Write the dependencies to the file */
198 WriteDependencies (F, OutputName);
200 /* Close the file, check for errors */
201 if (fclose (F) != 0) {
203 Fatal (FAT_CANNOT_WRITE_OUTPUT);
212 static void DefineSym (const char* Def)
213 /* Define a symbol on the command line */
217 /* The symbol must start with a character or underline */
218 if (Def [0] != '_' && !isalpha (Def [0])) {
222 /* Check the symbol name */
223 while (isalnum (*P) || *P == '_') {
227 /* Do we have a value given? */
232 /* No value given. Define the macro with the value 1 */
233 AddNumericMacro (Def, 1);
235 /* We have a value, P points to the '=' character. Since the argument
236 * is const, create a copy and replace the '=' in the copy by a zero
240 unsigned Len = strlen (Def)+1;
241 char* S = xmalloc (Len);
242 memcpy (S, Def, Len);
246 /* Define this as a macro */
249 /* Release the allocated memory */
256 static void CheckSegName (const char* Seg)
257 /* Abort if the given name is not a valid segment name */
259 /* Print an error and abort if the name is not ok */
260 if (!ValidSegName (Seg)) {
261 AbEnd ("Segment name `%s' is invalid", Seg);
267 static void OptAddSource (const char* Opt, const char* Arg)
268 /* Add source lines as comments in generated assembler file */
275 static void OptAnsi (const char* Opt, const char* Arg)
276 /* Compile in strict ANSI mode */
283 static void OptBssName (const char* Opt, const char* Arg)
284 /* Handle the --bss-name option */
286 /* Check for a valid name */
290 NewSegName (SEG_BSS, Arg);
295 static void OptCodeName (const char* Opt, const char* Arg)
296 /* Handle the --code-name option */
298 /* Check for a valid name */
302 NewSegName (SEG_CODE, Arg);
307 static void OptCreateDep (const char* Opt, const char* Arg)
308 /* Handle the --create-dep option */
315 static void OptCPU (const char* Opt, const char* Arg)
316 /* Handle the --cpu option */
318 if (strcmp (Arg, "6502") == 0) {
320 } else if (strcmp (Arg, "65C02") == 0) {
323 AbEnd ("Invalid CPU: `%s'", Arg);
329 static void OptDataName (const char* Opt, const char* Arg)
330 /* Handle the --code-name option */
332 /* Check for a valid name */
336 NewSegName (SEG_DATA, Arg);
341 static void OptDebug (const char* Opt, const char* Arg)
342 /* Compiler debug mode */
349 static void OptDebugInfo (const char* Opt, const char* Arg)
350 /* Add debug info to the object file */
357 static void OptHelp (const char* Opt, const char* Arg)
358 /* Print usage information and exit */
366 static void OptIncludeDir (const char* Opt, const char* Arg)
367 /* Add an include search path */
369 AddIncludePath (Arg, INC_SYS | INC_USER);
374 static void OptRodataName (const char* Opt, const char* Arg)
375 /* Handle the --rodata-name option */
377 /* Check for a valid name */
381 NewSegName (SEG_RODATA, Arg);
386 static void OptSignedChars (const char* Opt, const char* Arg)
387 /* Make default characters signed */
394 static void OptStaticLocals (const char* Opt, const char* Arg)
395 /* Place local variables in static storage */
402 static void OptTarget (const char* Opt, const char* Arg)
403 /* Set the target system */
410 static void OptVerbose (const char* Opt, const char* Arg)
411 /* Increase verbosity */
418 static void OptVersion (const char* Opt, const char* Arg)
419 /* Print the assembler version */
423 VER_MAJOR, VER_MINOR, VER_PATCH);
428 int main (int argc, char* argv[])
430 /* Program long options */
431 static const LongOpt OptTab[] = {
432 { "--add-source", 0, OptAddSource },
433 { "--ansi", 0, OptAnsi },
434 { "--bss-name", 1, OptBssName },
435 { "--code-name", 1, OptCodeName },
436 { "--create-dep", 0, OptCreateDep },
437 { "--cpu", 1, OptCPU },
438 { "--data-name", 1, OptDataName },
439 { "--debug", 0, OptDebug },
440 { "--debug-info", 0, OptDebugInfo },
441 { "--help", 0, OptHelp },
442 { "--include-dir", 1, OptIncludeDir },
443 { "--rodata-name", 1, OptRodataName },
444 { "--signed-chars", 0, OptSignedChars },
445 { "--static-locals", 0, OptStaticLocals },
446 { "--target", 1, OptTarget },
447 { "--verbose", 0, OptVerbose },
448 { "--version", 0, OptVersion },
453 /* Initialize the output file name */
454 const char* OutputFile = 0;
455 const char* InputFile = 0;
457 /* Initialize the cmdline module */
458 InitCmdLine (argc, argv, "cc65");
460 /* Initialize the default segment names */
463 /* Parse the command line */
469 /* Get the argument */
470 const char* Arg = argv [I];
472 /* Check for an option */
473 if (Arg [0] == '-') {
478 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
491 OptDebugInfo (Arg, 0);
495 OptSignedChars (Arg, 0);
499 OutputFile = GetArg (&I, 2);
503 OptTarget (Arg, GetArg (&I, 2));
507 OptCreateDep (Arg, 0);
523 OptStaticLocals (Arg, 0);
533 DefineSym (GetArg (&I, 2));
537 OptIncludeDir (Arg, GetArg (&I, 2));
546 sscanf (P, "%lx", (long*) &OptDisable);
562 OptAddSource (Arg, 0);
579 fprintf (stderr, "additional file specs ignored\n");
589 /* Did we have a file spec on the command line? */
590 if (InputFile == 0) {
591 AbEnd ("No input files");
594 /* Open the input file */
595 OpenMainFile (InputFile);
597 /* Create the output file name if it was not explicitly given */
598 if (OutputFile == 0) {
599 OutputFile = MakeFilename (InputFile, ".s");
605 /* Create the output file if we didn't had any errors */
606 if (ErrorCount == 0 || Debug) {
610 /* Optimize the output if requested */
616 F = fopen (OutputFile, "w");
618 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
621 /* Write the output to the file */
624 /* Close the file, check for errors */
625 if (fclose (F) != 0) {
627 Fatal (FAT_CANNOT_WRITE_OUTPUT);
630 /* Create dependencies if requested */
632 DoCreateDep (OutputFile);
637 /* Return an apropriate exit code */
638 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;