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/fname.h"
44 #include "../common/version.h"
45 #include "../common/xmalloc.h"
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* Names of the target systems sorted by target name */
67 static const char* TargetNames [] = {
83 /*****************************************************************************/
85 /*****************************************************************************/
89 static void Usage (void)
92 "Usage: %s [options] file\n"
94 " -d\t\t\tDebug mode\n"
95 " -g\t\t\tAdd debug info to object file\n"
96 " -h\t\t\tHelp (this text)\n"
97 " -j\t\t\tDefault characters are signed\n"
98 " -o name\t\tName the output file\n"
99 " -t sys\t\tSet the target system\n"
100 " -v\t\t\tIncrease verbosity\n"
101 " -A\t\t\tStrict ANSI mode\n"
102 " -Cl\t\t\tMake local variables static\n"
103 " -Dsym[=defn]\t\tDefine a symbol\n"
104 " -I path\t\tSet an include directory search path\n"
105 " -O\t\t\tOptimize code\n"
106 " -Oi\t\t\tOptimize code, inline more code\n"
107 " -Or\t\t\tEnable register variables\n"
108 " -Os\t\t\tInline some known functions\n"
109 " -T\t\t\tInclude source as comment\n"
110 " -V\t\t\tPrint the compiler version number\n"
111 " -W\t\t\tSuppress warnings\n"
114 " --ansi\t\tStrict ANSI mode\n"
115 " --cpu type\t\tSet cpu type\n"
116 " --debug-info\t\tAdd debug info to object file\n"
117 " --help\t\tHelp (this text)\n"
118 " --include-dir dir\tSet an include directory search path\n"
119 " --signed-chars\tDefault characters are signed\n"
120 " --static-locals\tMake local variables static\n"
121 " --target sys\t\tSet the target system\n"
122 " --verbose\t\tIncrease verbosity\n"
123 " --version\t\tPrint the compiler version number\n",
129 static void cbmsys (const char* sys)
130 /* Define a CBM system */
132 AddNumericMacro ("__CBM__", 1);
133 AddNumericMacro (sys, 1);
138 static int MapSys (const char* Name)
139 /* Map a target name to a system code. Return -1 in case of an error */
143 /* Check for a numeric target */
144 if (isdigit (*Name)) {
145 int Target = atoi (Name);
146 if (Target >= 0 && Target < TGT_COUNT) {
151 /* Check for a target string */
152 for (I = 0; I < TGT_COUNT; ++I) {
153 if (strcmp (TargetNames [I], Name) == 0) {
163 static void SetSys (const char* Sys)
164 /* Define a target system */
166 switch (Target = MapSys (Sys)) {
172 AddNumericMacro ("__ATARI__", 1);
188 cbmsys ("__PLUS4__");
192 cbmsys ("__CBM610__");
200 AddNumericMacro ("__NES__", 1);
204 AddNumericMacro ("__APPLE2__", 1);
208 /* Do not handle as a CBM system */
209 AddNumericMacro ("__GEOS__", 1);
213 fputs ("Unknown system type\n", stderr);
220 static void DefineSym (const char* Def)
221 /* Define a symbol on the command line */
225 /* The symbol must start with a character or underline */
226 if (Def [0] != '_' && !isalpha (Def [0])) {
230 /* Check the symbol name */
231 while (isalnum (*P) || *P == '_') {
235 /* Do we have a value given? */
240 /* No value given. Define the macro with the value 1 */
241 AddNumericMacro (Def, 1);
243 /* We have a value, P points to the '=' character. Since the argument
244 * is const, create a copy and replace the '=' in the copy by a zero
248 unsigned Len = strlen (Def)+1;
249 char* S = xmalloc (Len);
250 memcpy (S, Def, Len);
254 /* Define this as a macro */
257 /* Release the allocated memory */
264 static void OptAddSource (const char* Opt, const char* Arg)
265 /* Add source lines as comments in generated assembler file */
272 static void OptAnsi (const char* Opt, const char* Arg)
273 /* Compile in strict ANSI mode */
280 static void OptCPU (const char* Opt, const char* Arg)
281 /* Handle the --cpu option */
286 if (strcmp (Arg, "6502") == 0) {
288 } else if (strcmp (Arg, "65C02") == 0) {
291 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
298 static void OptDebugInfo (const char* Opt, const char* Arg)
299 /* Add debug info to the object file */
306 static void OptHelp (const char* Opt, const char* Arg)
307 /* Print usage information and exit */
315 static void OptIncludeDir (const char* Opt, const char* Arg)
316 /* Add an include search path */
321 AddIncludePath (Arg, INC_SYS | INC_USER);
326 static void OptSignedChars (const char* Opt, const char* Arg)
327 /* Make default characters signed */
334 static void OptStaticLocals (const char* Opt, const char* Arg)
335 /* Place local variables in static storage */
342 static void OptTarget (const char* Opt, const char* Arg)
343 /* Set the target system */
353 static void OptVerbose (const char* Opt, const char* Arg)
354 /* Increase verbosity */
361 static void OptVersion (const char* Opt, const char* Arg)
362 /* Print the assembler version */
366 VER_MAJOR, VER_MINOR, VER_PATCH);
371 int main (int argc, char* argv[])
373 /* Program long options */
374 static const LongOpt OptTab[] = {
375 { "--add-source", 0, OptAddSource },
376 { "--ansi", 0, OptAnsi },
377 { "--cpu", 1, OptCPU },
378 { "--debug-info", 0, OptDebugInfo },
379 { "--help", 0, OptHelp },
380 { "--include-dir", 1, OptIncludeDir },
381 { "--signed-chars", 0, OptSignedChars },
382 { "--static-locals", 0, OptStaticLocals },
383 { "--target", 1, OptTarget },
384 { "--verbose", 0, OptVerbose },
385 { "--version", 0, OptVersion },
390 /* Initialize the output file name */
391 const char* OutputFile = 0;
392 const char* InputFile = 0;
394 /* Initialize the cmdline module */
395 InitCmdLine (argc, argv, "cc65");
397 /* Parse the command line */
403 /* Get the argument */
404 const char* Arg = argv [I];
406 /* Check for an option */
407 if (Arg [0] == '-') {
412 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
415 case 'd': /* debug mode */
425 OptDebugInfo (Arg, 0);
429 OptSignedChars (Arg, 0);
433 OutputFile = GetArg (&I, 2);
437 OptTarget (Arg, GetArg (&I, 2));
453 OptStaticLocals (Arg, 0);
463 DefineSym (GetArg (&I, 2));
467 OptIncludeDir (Arg, GetArg (&I, 2));
476 sscanf (P, "%lx", (long*) &OptDisable);
492 OptAddSource (Arg, 0);
509 fprintf (stderr, "additional file specs ignored\n");
519 /* Did we have a file spec on the command line? */
520 if (InputFile == 0) {
521 fprintf (stderr, "%s: No input files\n", argv [0]);
525 /* Open the input file */
526 OpenMainFile (InputFile);
528 /* Create the output file name if it was not explicitly given */
529 if (OutputFile == 0) {
530 OutputFile = MakeFilename (InputFile, ".s");
536 /* Create the output file if we didn't had any errors */
537 if (ErrorCount == 0 || Debug) {
541 /* Optimize the output if requested */
547 F = fopen (OutputFile, "w");
549 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
552 /* Write the output to the file */
555 /* Close the file, check for errors */
556 if (fclose (F) != 0) {
558 Fatal (FAT_CANNOT_WRITE_OUTPUT);
562 /* Return an apropriate exit code */
563 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;