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"
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\tPrint this help\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 " --target sys\t\tSet the target system\n"
121 " --verbose\t\tIncrease verbosity\n"
122 " --version\t\tPrint the compiler version number\n",
128 static void cbmsys (const char* sys)
129 /* Define a CBM system */
131 AddNumericMacro ("__CBM__", 1);
132 AddNumericMacro (sys, 1);
137 static int MapSys (const char* Name)
138 /* Map a target name to a system code. Return -1 in case of an error */
142 /* Check for a numeric target */
143 if (isdigit (*Name)) {
144 int Target = atoi (Name);
145 if (Target >= 0 && Target < TGT_COUNT) {
150 /* Check for a target string */
151 for (I = 0; I < TGT_COUNT; ++I) {
152 if (strcmp (TargetNames [I], Name) == 0) {
162 static void SetSys (const char* Sys)
163 /* Define a target system */
165 switch (Target = MapSys (Sys)) {
171 AddNumericMacro ("__ATARI__", 1);
187 cbmsys ("__PLUS4__");
191 cbmsys ("__CBM610__");
199 AddNumericMacro ("__NES__", 1);
203 AddNumericMacro ("__APPLE2__", 1);
207 /* Do not handle as a CBM system */
208 AddNumericMacro ("__GEOS__", 1);
212 fputs ("Unknown system type\n", stderr);
219 static void DefineSym (const char* Def)
220 /* Define a symbol on the command line */
224 /* The symbol must start with a character or underline */
225 if (Def [0] != '_' && !isalpha (Def [0])) {
229 /* Check the symbol name */
230 while (isalnum (*P) || *P == '_') {
234 /* Do we have a value given? */
239 /* No value given. Define the macro with the value 1 */
240 AddNumericMacro (Def, 1);
242 /* We have a value, P points to the '=' character. Since the argument
243 * is const, create a copy and replace the '=' in the copy by a zero
247 unsigned Len = strlen (Def)+1;
248 char* S = xmalloc (Len);
249 memcpy (S, Def, Len);
253 /* Define this as a macro */
256 /* Release the allocated memory */
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 OptCPU (const char* Opt, const char* Arg)
280 /* Handle the --cpu option */
285 if (strcmp (Arg, "6502") == 0) {
287 } else if (strcmp (Arg, "65C02") == 0) {
290 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
297 static void OptDebugInfo (const char* Opt, const char* Arg)
298 /* Add debug info to the object file */
305 static void OptHelp (const char* Opt, const char* Arg)
306 /* Print usage information and exit */
314 static void OptIncludeDir (const char* Opt, const char* Arg)
315 /* Add an include search path */
320 AddIncludePath (Arg, INC_SYS | INC_USER);
325 static void OptSignedChars (const char* Opt, const char* Arg)
326 /* Make default characters signed */
333 static void OptTarget (const char* Opt, const char* Arg)
334 /* Set the target system */
344 static void OptVerbose (const char* Opt, const char* Arg)
345 /* Increase verbosity */
352 static void OptVersion (const char* Opt, const char* Arg)
353 /* Print the assembler version */
357 VER_MAJOR, VER_MINOR, VER_PATCH);
362 int main (int argc, char* argv[])
364 /* Program long options */
365 static const LongOpt OptTab[] = {
366 { "--add-source", 0, OptAddSource },
367 { "--ansi", 0, OptAnsi },
368 { "--cpu", 1, OptCPU },
369 { "--debug-info", 0, OptDebugInfo },
370 { "--help", 0, OptHelp },
371 { "--include-dir", 1, OptIncludeDir },
372 { "--signed-chars", 0, OptSignedChars },
373 { "--target", 1, OptTarget },
374 { "--verbose", 0, OptVerbose },
375 { "--version", 0, OptVersion },
380 /* Initialize the output file name */
381 const char* OutputFile = 0;
385 /* Initialize the cmdline module */
386 InitCmdLine (argc, argv, "cc65");
388 /* Parse the command line */
394 /* Get the argument */
395 const char* Arg = argv [I];
397 /* Check for an option */
398 if (Arg [0] == '-') {
403 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
406 case 'd': /* debug mode */
416 OptDebugInfo (Arg, 0);
420 OptSignedChars (Arg, 0);
424 OutputFile = GetArg (&I, 2);
428 OptTarget (Arg, GetArg (&I, 2));
451 DefineSym (GetArg (&I, 2));
455 OptIncludeDir (Arg, GetArg (&I, 2));
464 sscanf (P, "%lx", (long*) &OptDisable);
480 OptAddSource (Arg, 0);
497 fprintf (stderr, "additional file specs ignored\n");
500 inp = fopen (fin, "r");
502 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
511 /* Did we have a file spec on the command line? */
513 fprintf (stderr, "%s: No input files\n", argv [0]);
517 /* Create the output file name if it was not explicitly given */
518 if (OutputFile == 0) {
519 OutputFile = MakeFilename (fin, ".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 (OutputFile, "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;