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 /*****************************************************************************/
42 #include "../common/cmdline.h"
43 #include "../common/version.h"
59 /*****************************************************************************/
61 /*****************************************************************************/
65 /* Names of the target systems sorted by target name */
66 static const char* TargetNames [] = {
82 /*****************************************************************************/
84 /*****************************************************************************/
88 static void Usage (void)
91 "Usage: cc65 [options] file\n"
93 " -d\t\t\tDebug mode\n"
94 " -g\t\t\tAdd debug info to object file\n"
95 " -h\t\t\tPrint this help\n"
96 " -j\t\t\tDefault characters are signed\n"
97 " -o name\t\tName the output file\n"
98 " -t sys\t\tSet the target system\n"
99 " -v\t\t\tIncrease verbosity\n"
100 " -A\t\t\tStrict ANSI mode\n"
101 " -Cl\t\t\tMake local variables static\n"
102 " -Dsym[=defn]\t\tDefine a symbol\n"
103 " -I path\t\tSet an include directory search path\n"
104 " -O\t\t\tOptimize code\n"
105 " -Oi\t\t\tOptimize code, inline more code\n"
106 " -Or\t\t\tEnable register variables\n"
107 " -Os\t\t\tInline some known functions\n"
108 " -T\t\t\tInclude source as comment\n"
109 " -V\t\t\tPrint the compiler version number\n"
110 " -W\t\t\tSuppress warnings\n"
113 " --ansi\t\tStrict ANSI mode\n"
114 " --cpu type\t\tSet cpu type\n"
115 " --debug-info\t\tAdd debug info to object file\n"
116 " --help\t\tHelp (this text)\n"
117 " --include-dir dir\tSet an include directory search path\n"
118 " --signed-chars\tDefault characters are signed\n"
119 " --target sys\t\tSet the target system\n"
120 " --verbose\t\tIncrease verbosity\n"
121 " --version\t\tPrint the compiler version number\n");
126 static void cbmsys (const char* sys)
127 /* Define a CBM system */
129 AddNumericMacro ("__CBM__", 1);
130 AddNumericMacro (sys, 1);
135 static int MapSys (const char* Name)
136 /* Map a target name to a system code. Return -1 in case of an error */
140 /* Check for a numeric target */
141 if (isdigit (*Name)) {
142 int Target = atoi (Name);
143 if (Target >= 0 && Target < TGT_COUNT) {
148 /* Check for a target string */
149 for (I = 0; I < TGT_COUNT; ++I) {
150 if (strcmp (TargetNames [I], Name) == 0) {
160 static void SetSys (const char* Sys)
161 /* Define a target system */
163 switch (Target = MapSys (Sys)) {
169 AddNumericMacro ("__ATARI__", 1);
185 cbmsys ("__PLUS4__");
189 cbmsys ("__CBM610__");
197 AddNumericMacro ("__NES__", 1);
201 AddNumericMacro ("__APPLE2__", 1);
205 /* Do not handle as a CBM system */
206 AddNumericMacro ("__GEOS__", 1);
210 fputs ("Unknown system type\n", stderr);
217 static void DefineSym (const char* Def)
218 /* Define a symbol on the command line */
222 /* The symbol must start with a character or underline */
223 if (Def [0] != '_' && !isalpha (Def [0])) {
227 /* Check the symbol name */
228 while (isalnum (*P) || *P == '_') {
232 /* Do we have a value given? */
237 /* No value given. Define the macro with the value 1 */
238 AddNumericMacro (Def, 1);
240 /* We have a value, P points to the '=' character. Since the argument
241 * is const, create a copy and replace the '=' in the copy by a zero
245 unsigned Len = strlen (Def)+1;
246 char* S = xmalloc (Len);
247 memcpy (S, Def, Len);
251 /* Define this as a macro */
254 /* Release the allocated memory */
261 static void OptAnsi (const char* Opt, const char* Arg)
262 /* Compile in strict ANSI mode */
269 static void OptCPU (const char* Opt, const char* Arg)
270 /* Handle the --cpu option */
275 if (strcmp (Arg, "6502") == 0) {
277 } else if (strcmp (Arg, "65C02") == 0) {
280 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
287 static void OptDebugInfo (const char* Opt, const char* Arg)
288 /* Add debug info to the object file */
295 static void OptHelp (const char* Opt, const char* Arg)
296 /* Print usage information and exit */
304 static void OptIncludeDir (const char* Opt, const char* Arg)
305 /* Add an include search path */
310 AddIncludePath (Arg, INC_SYS | INC_USER);
315 static void OptSignedChars (const char* Opt, const char* Arg)
316 /* Make default characters signed */
323 static void OptTarget (const char* Opt, const char* Arg)
324 /* Set the target system */
334 static void OptVerbose (const char* Opt, const char* Arg)
335 /* Increase verbosity */
342 static void OptVersion (const char* Opt, const char* Arg)
343 /* Print the assembler version */
347 VER_MAJOR, VER_MINOR, VER_PATCH);
352 int main (int argc, char* argv[])
354 /* Program long options */
355 static const LongOpt OptTab[] = {
356 { "--ansi", 0, OptAnsi },
357 { "--cpu", 1, OptCPU },
358 { "--debug-info", 0, OptDebugInfo },
359 { "--help", 0, OptHelp },
360 { "--include-dir", 1, OptIncludeDir },
361 { "--signed-chars", 0, OptSignedChars },
362 { "--target", 1, OptTarget },
363 { "--verbose", 0, OptVerbose },
364 { "--version", 0, OptVersion },
370 /* Initialize the output file name */
375 /* Initialize the cmdline module */
376 InitCmdLine (argc, argv);
378 /* Parse the command line */
384 /* Get the argument */
385 const char* Arg = argv [I];
387 /* Check for an option */
388 if (Arg [0] == '-') {
393 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
396 case 'd': /* debug mode */
406 OptDebugInfo (Arg, 0);
410 OptSignedChars (Arg, 0);
414 strcpy (out_name, GetArg (&I, 2));
418 OptTarget (Arg, GetArg (&I, 2));
441 DefineSym (GetArg (&I, 2));
445 OptIncludeDir (Arg, GetArg (&I, 2));
454 sscanf (P, "%lx", (long*) &OptDisable);
487 fprintf (stderr, "additional file specs ignored\n");
490 inp = fopen (fin, "r");
492 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
501 /* Did we have a file spec on the command line? */
503 fprintf (stderr, "%s: No input files\n", argv [0]);
507 /* Create the output file name. We should really have
508 * some checks for string overflow, but I'll drop this because of the
509 * additional code size it would need (as in other places). Sigh.
512 if (out_name [0] == '\0') {
514 /* No output name given, create default */
515 strcpy (out_name, fin);
516 if ((p = strrchr (out_name, '.'))) {
519 strcat (out_name, ".s");
525 /* Create the output file if we didn't had any errors */
526 if (ErrorCount == 0 || Debug) {
530 /* Optimize the output if requested */
536 F = fopen (out_name, "w");
538 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
541 /* Write the output to the file */
544 /* Close the file, check for errors */
545 if (fclose (F) != 0) {
547 Fatal (FAT_CANNOT_WRITE_OUTPUT);
551 /* Return an apropriate exit code */
552 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;