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 OptAnsi (const char* Opt, const char* Arg)
264 /* Compile in strict ANSI mode */
271 static void OptCPU (const char* Opt, const char* Arg)
272 /* Handle the --cpu option */
277 if (strcmp (Arg, "6502") == 0) {
279 } else if (strcmp (Arg, "65C02") == 0) {
282 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
289 static void OptDebugInfo (const char* Opt, const char* Arg)
290 /* Add debug info to the object file */
297 static void OptHelp (const char* Opt, const char* Arg)
298 /* Print usage information and exit */
306 static void OptIncludeDir (const char* Opt, const char* Arg)
307 /* Add an include search path */
312 AddIncludePath (Arg, INC_SYS | INC_USER);
317 static void OptSignedChars (const char* Opt, const char* Arg)
318 /* Make default characters signed */
325 static void OptTarget (const char* Opt, const char* Arg)
326 /* Set the target system */
336 static void OptVerbose (const char* Opt, const char* Arg)
337 /* Increase verbosity */
344 static void OptVersion (const char* Opt, const char* Arg)
345 /* Print the assembler version */
349 VER_MAJOR, VER_MINOR, VER_PATCH);
354 int main (int argc, char* argv[])
356 /* Program long options */
357 static const LongOpt OptTab[] = {
358 { "--ansi", 0, OptAnsi },
359 { "--cpu", 1, OptCPU },
360 { "--debug-info", 0, OptDebugInfo },
361 { "--help", 0, OptHelp },
362 { "--include-dir", 1, OptIncludeDir },
363 { "--signed-chars", 0, OptSignedChars },
364 { "--target", 1, OptTarget },
365 { "--verbose", 0, OptVerbose },
366 { "--version", 0, OptVersion },
371 /* Initialize the output file name */
372 const char* OutputFile = 0;
376 /* Initialize the cmdline module */
377 InitCmdLine (argc, argv, "cc65");
379 /* Parse the command line */
385 /* Get the argument */
386 const char* Arg = argv [I];
388 /* Check for an option */
389 if (Arg [0] == '-') {
394 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
397 case 'd': /* debug mode */
407 OptDebugInfo (Arg, 0);
411 OptSignedChars (Arg, 0);
415 OutputFile = GetArg (&I, 2);
419 OptTarget (Arg, GetArg (&I, 2));
442 DefineSym (GetArg (&I, 2));
446 OptIncludeDir (Arg, GetArg (&I, 2));
455 sscanf (P, "%lx", (long*) &OptDisable);
488 fprintf (stderr, "additional file specs ignored\n");
491 inp = fopen (fin, "r");
493 Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
502 /* Did we have a file spec on the command line? */
504 fprintf (stderr, "%s: No input files\n", argv [0]);
508 /* Create the output file name if it was not explicitly given */
509 if (OutputFile == 0) {
510 OutputFile = MakeFilename (fin, ".s");
516 /* Create the output file if we didn't had any errors */
517 if (ErrorCount == 0 || Debug) {
521 /* Optimize the output if requested */
527 F = fopen (OutputFile, "w");
529 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
532 /* Write the output to the file */
535 /* Close the file, check for errors */
536 if (fclose (F) != 0) {
538 Fatal (FAT_CANNOT_WRITE_OUTPUT);
542 /* Return an apropriate exit code */
543 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;