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 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static void Usage (void)
74 "Usage: %s [options] file\n"
76 " -d\t\t\tDebug mode\n"
77 " -g\t\t\tAdd debug info to object file\n"
78 " -h\t\t\tHelp (this text)\n"
79 " -j\t\t\tDefault characters are signed\n"
80 " -o name\t\tName the output file\n"
81 " -t sys\t\tSet the target system\n"
82 " -v\t\t\tIncrease verbosity\n"
83 " -A\t\t\tStrict ANSI mode\n"
84 " -Cl\t\t\tMake local variables static\n"
85 " -Dsym[=defn]\t\tDefine a symbol\n"
86 " -I dir\t\tSet an include directory search path\n"
87 " -O\t\t\tOptimize code\n"
88 " -Oi\t\t\tOptimize code, inline more code\n"
89 " -Or\t\t\tEnable register variables\n"
90 " -Os\t\t\tInline some known functions\n"
91 " -T\t\t\tInclude source as comment\n"
92 " -V\t\t\tPrint the compiler version number\n"
93 " -W\t\t\tSuppress warnings\n"
96 " --ansi\t\tStrict ANSI mode\n"
97 " --bss-name seg\tSet the name of the BSS segment\n"
98 " --code-name seg\tSet the name of the CODE segment\n"
99 " --cpu type\t\tSet cpu type\n"
100 " --data-name seg\tSet the name of the DATA segment\n"
101 " --debug\t\tDebug mode\n"
102 " --debug-info\t\tAdd debug info to object file\n"
103 " --help\t\tHelp (this text)\n"
104 " --include-dir dir\tSet an include directory search path\n"
105 " --rodata-name seg\tSet the name of the RODATA segment\n"
106 " --signed-chars\tDefault characters are signed\n"
107 " --static-locals\tMake local variables static\n"
108 " --target sys\t\tSet the target system\n"
109 " --verbose\t\tIncrease verbosity\n"
110 " --version\t\tPrint the compiler version number\n",
116 static void cbmsys (const char* sys)
117 /* Define a CBM system */
119 AddNumericMacro ("__CBM__", 1);
120 AddNumericMacro (sys, 1);
125 static void SetSys (const char* Sys)
126 /* Define a target system */
128 switch (Target = FindTarget (Sys)) {
134 AddNumericMacro ("__ATARI__", 1);
150 cbmsys ("__PLUS4__");
154 cbmsys ("__CBM610__");
162 AddNumericMacro ("__NES__", 1);
166 AddNumericMacro ("__APPLE2__", 1);
170 /* Do not handle as a CBM system */
171 AddNumericMacro ("__GEOS__", 1);
175 AbEnd ("Unknown target system type");
181 static void DoCreateDep (const char* OutputName)
182 /* Create the dependency file */
184 /* Make the dependency file name from the output file name */
185 char* DepName = MakeFilename (OutputName, ".u");
188 FILE* F = fopen (DepName, "w");
190 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
193 /* Write the dependencies to the file */
194 WriteDependencies (F, OutputName);
196 /* Close the file, check for errors */
197 if (fclose (F) != 0) {
199 Fatal (FAT_CANNOT_WRITE_OUTPUT);
208 static void DefineSym (const char* Def)
209 /* Define a symbol on the command line */
213 /* The symbol must start with a character or underline */
214 if (Def [0] != '_' && !isalpha (Def [0])) {
218 /* Check the symbol name */
219 while (isalnum (*P) || *P == '_') {
223 /* Do we have a value given? */
228 /* No value given. Define the macro with the value 1 */
229 AddNumericMacro (Def, 1);
231 /* We have a value, P points to the '=' character. Since the argument
232 * is const, create a copy and replace the '=' in the copy by a zero
236 unsigned Len = strlen (Def)+1;
237 char* S = xmalloc (Len);
238 memcpy (S, Def, Len);
242 /* Define this as a macro */
245 /* Release the allocated memory */
252 static void CheckSegName (const char* Seg)
253 /* Abort if the given name is not a valid segment name */
255 /* Print an error and abort if the name is not ok */
256 if (!ValidSegName (Seg)) {
257 AbEnd ("Segment name `%s' is invalid", Seg);
263 static void OptAddSource (const char* Opt, const char* Arg)
264 /* Add source lines as comments in generated assembler file */
271 static void OptAnsi (const char* Opt, const char* Arg)
272 /* Compile in strict ANSI mode */
279 static void OptBssName (const char* Opt, const char* Arg)
280 /* Handle the --bss-name option */
282 /* Check for a valid name */
286 NewSegName (SEG_BSS, Arg);
291 static void OptCodeName (const char* Opt, const char* Arg)
292 /* Handle the --code-name option */
294 /* Check for a valid name */
298 NewSegName (SEG_CODE, Arg);
303 static void OptCreateDep (const char* Opt, const char* Arg)
304 /* Handle the --create-dep option */
311 static void OptCPU (const char* Opt, const char* Arg)
312 /* Handle the --cpu option */
314 if (strcmp (Arg, "6502") == 0) {
316 } else if (strcmp (Arg, "65C02") == 0) {
319 AbEnd ("Invalid CPU: `%s'", Arg);
325 static void OptDataName (const char* Opt, const char* Arg)
326 /* Handle the --code-name option */
328 /* Check for a valid name */
332 NewSegName (SEG_DATA, Arg);
337 static void OptDebug (const char* Opt, const char* Arg)
338 /* Compiler debug mode */
345 static void OptDebugInfo (const char* Opt, const char* Arg)
346 /* Add debug info to the object file */
353 static void OptHelp (const char* Opt, const char* Arg)
354 /* Print usage information and exit */
362 static void OptIncludeDir (const char* Opt, const char* Arg)
363 /* Add an include search path */
365 AddIncludePath (Arg, INC_SYS | INC_USER);
370 static void OptRodataName (const char* Opt, const char* Arg)
371 /* Handle the --rodata-name option */
373 /* Check for a valid name */
377 NewSegName (SEG_RODATA, Arg);
382 static void OptSignedChars (const char* Opt, const char* Arg)
383 /* Make default characters signed */
390 static void OptStaticLocals (const char* Opt, const char* Arg)
391 /* Place local variables in static storage */
398 static void OptTarget (const char* Opt, const char* Arg)
399 /* Set the target system */
406 static void OptVerbose (const char* Opt, const char* Arg)
407 /* Increase verbosity */
414 static void OptVersion (const char* Opt, const char* Arg)
415 /* Print the assembler version */
419 VER_MAJOR, VER_MINOR, VER_PATCH);
424 int main (int argc, char* argv[])
426 /* Program long options */
427 static const LongOpt OptTab[] = {
428 { "--add-source", 0, OptAddSource },
429 { "--ansi", 0, OptAnsi },
430 { "--bss-name", 1, OptBssName },
431 { "--code-name", 1, OptCodeName },
432 { "--create-dep", 0, OptCreateDep },
433 { "--cpu", 1, OptCPU },
434 { "--data-name", 1, OptDataName },
435 { "--debug", 0, OptDebug },
436 { "--debug-info", 0, OptDebugInfo },
437 { "--help", 0, OptHelp },
438 { "--include-dir", 1, OptIncludeDir },
439 { "--rodata-name", 1, OptRodataName },
440 { "--signed-chars", 0, OptSignedChars },
441 { "--static-locals", 0, OptStaticLocals },
442 { "--target", 1, OptTarget },
443 { "--verbose", 0, OptVerbose },
444 { "--version", 0, OptVersion },
449 /* Initialize the output file name */
450 const char* OutputFile = 0;
451 const char* InputFile = 0;
453 /* Initialize the cmdline module */
454 InitCmdLine (argc, argv, "cc65");
456 /* Initialize the default segment names */
459 /* Parse the command line */
465 /* Get the argument */
466 const char* Arg = argv [I];
468 /* Check for an option */
469 if (Arg [0] == '-') {
474 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
487 OptDebugInfo (Arg, 0);
491 OptSignedChars (Arg, 0);
495 OutputFile = GetArg (&I, 2);
499 OptTarget (Arg, GetArg (&I, 2));
503 OptCreateDep (Arg, 0);
519 OptStaticLocals (Arg, 0);
529 DefineSym (GetArg (&I, 2));
533 OptIncludeDir (Arg, GetArg (&I, 2));
542 sscanf (P, "%lx", (long*) &OptDisable);
558 OptAddSource (Arg, 0);
575 fprintf (stderr, "additional file specs ignored\n");
585 /* Did we have a file spec on the command line? */
586 if (InputFile == 0) {
587 AbEnd ("No input files");
590 /* Open the input file */
591 OpenMainFile (InputFile);
593 /* Create the output file name if it was not explicitly given */
594 if (OutputFile == 0) {
595 OutputFile = MakeFilename (InputFile, ".s");
601 /* Create the output file if we didn't had any errors */
602 if (ErrorCount == 0 || Debug) {
606 /* Optimize the output if requested */
612 F = fopen (OutputFile, "w");
614 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
617 /* Write the output to the file */
620 /* Close the file, check for errors */
621 if (fclose (F) != 0) {
623 Fatal (FAT_CANNOT_WRITE_OUTPUT);
626 /* Create dependencies if requested */
628 DoCreateDep (OutputFile);
633 /* Return an apropriate exit code */
634 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;