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 DefineSym (const char* Def)
182 /* Define a symbol on the command line */
186 /* The symbol must start with a character or underline */
187 if (Def [0] != '_' && !isalpha (Def [0])) {
191 /* Check the symbol name */
192 while (isalnum (*P) || *P == '_') {
196 /* Do we have a value given? */
201 /* No value given. Define the macro with the value 1 */
202 AddNumericMacro (Def, 1);
204 /* We have a value, P points to the '=' character. Since the argument
205 * is const, create a copy and replace the '=' in the copy by a zero
209 unsigned Len = strlen (Def)+1;
210 char* S = xmalloc (Len);
211 memcpy (S, Def, Len);
215 /* Define this as a macro */
218 /* Release the allocated memory */
225 static void CheckSegName (const char* Seg)
226 /* Abort if the given name is not a valid segment name */
228 /* Print an error and abort if the name is not ok */
229 if (!ValidSegName (Seg)) {
230 AbEnd ("Segment name `%s' is invalid", Seg);
236 static void OptAddSource (const char* Opt, const char* Arg)
237 /* Add source lines as comments in generated assembler file */
244 static void OptAnsi (const char* Opt, const char* Arg)
245 /* Compile in strict ANSI mode */
252 static void OptBssName (const char* Opt, const char* Arg)
253 /* Handle the --bss-name option */
255 /* Check for a valid name */
259 NewSegName (SEG_BSS, Arg);
264 static void OptCodeName (const char* Opt, const char* Arg)
265 /* Handle the --code-name option */
267 /* Check for a valid name */
271 NewSegName (SEG_CODE, Arg);
276 static void OptCPU (const char* Opt, const char* Arg)
277 /* Handle the --cpu option */
279 if (strcmp (Arg, "6502") == 0) {
281 } else if (strcmp (Arg, "65C02") == 0) {
284 AbEnd ("Invalid CPU: `%s'", Arg);
290 static void OptDataName (const char* Opt, const char* Arg)
291 /* Handle the --code-name option */
293 /* Check for a valid name */
297 NewSegName (SEG_DATA, Arg);
302 static void OptDebug (const char* Opt, const char* Arg)
303 /* Compiler debug mode */
310 static void OptDebugInfo (const char* Opt, const char* Arg)
311 /* Add debug info to the object file */
318 static void OptHelp (const char* Opt, const char* Arg)
319 /* Print usage information and exit */
327 static void OptIncludeDir (const char* Opt, const char* Arg)
328 /* Add an include search path */
330 AddIncludePath (Arg, INC_SYS | INC_USER);
335 static void OptRodataName (const char* Opt, const char* Arg)
336 /* Handle the --rodata-name option */
338 /* Check for a valid name */
342 NewSegName (SEG_RODATA, Arg);
347 static void OptSignedChars (const char* Opt, const char* Arg)
348 /* Make default characters signed */
355 static void OptStaticLocals (const char* Opt, const char* Arg)
356 /* Place local variables in static storage */
363 static void OptTarget (const char* Opt, const char* Arg)
364 /* Set the target system */
371 static void OptVerbose (const char* Opt, const char* Arg)
372 /* Increase verbosity */
379 static void OptVersion (const char* Opt, const char* Arg)
380 /* Print the assembler version */
384 VER_MAJOR, VER_MINOR, VER_PATCH);
389 int main (int argc, char* argv[])
391 /* Program long options */
392 static const LongOpt OptTab[] = {
393 { "--add-source", 0, OptAddSource },
394 { "--ansi", 0, OptAnsi },
395 { "--bss-name", 1, OptBssName },
396 { "--code-name", 1, OptCodeName },
397 { "--cpu", 1, OptCPU },
398 { "--data-name", 1, OptDataName },
399 { "--debug", 0, OptDebug },
400 { "--debug-info", 0, OptDebugInfo },
401 { "--help", 0, OptHelp },
402 { "--include-dir", 1, OptIncludeDir },
403 { "--rodata-name", 1, OptRodataName },
404 { "--signed-chars", 0, OptSignedChars },
405 { "--static-locals", 0, OptStaticLocals },
406 { "--target", 1, OptTarget },
407 { "--verbose", 0, OptVerbose },
408 { "--version", 0, OptVersion },
413 /* Initialize the output file name */
414 const char* OutputFile = 0;
415 const char* InputFile = 0;
417 /* Initialize the cmdline module */
418 InitCmdLine (argc, argv, "cc65");
420 /* Initialize the default segment names */
423 /* Parse the command line */
429 /* Get the argument */
430 const char* Arg = argv [I];
432 /* Check for an option */
433 if (Arg [0] == '-') {
438 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
451 OptDebugInfo (Arg, 0);
455 OptSignedChars (Arg, 0);
459 OutputFile = GetArg (&I, 2);
463 OptTarget (Arg, GetArg (&I, 2));
479 OptStaticLocals (Arg, 0);
489 DefineSym (GetArg (&I, 2));
493 OptIncludeDir (Arg, GetArg (&I, 2));
502 sscanf (P, "%lx", (long*) &OptDisable);
518 OptAddSource (Arg, 0);
535 fprintf (stderr, "additional file specs ignored\n");
545 /* Did we have a file spec on the command line? */
546 if (InputFile == 0) {
547 AbEnd ("No input files");
550 /* Open the input file */
551 OpenMainFile (InputFile);
553 /* Create the output file name if it was not explicitly given */
554 if (OutputFile == 0) {
555 OutputFile = MakeFilename (InputFile, ".s");
561 /* Create the output file if we didn't had any errors */
562 if (ErrorCount == 0 || Debug) {
566 /* Optimize the output if requested */
572 F = fopen (OutputFile, "w");
574 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
577 /* Write the output to the file */
580 /* Close the file, check for errors */
581 if (fclose (F) != 0) {
583 Fatal (FAT_CANNOT_WRITE_OUTPUT);
587 /* Return an apropriate exit code */
588 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;