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\t\tDebug mode\n"
117 " --debug-info\t\tAdd debug info to object file\n"
118 " --help\t\tHelp (this text)\n"
119 " --include-dir dir\tSet an include directory search path\n"
120 " --signed-chars\tDefault characters are signed\n"
121 " --static-locals\tMake local variables static\n"
122 " --target sys\t\tSet the target system\n"
123 " --verbose\t\tIncrease verbosity\n"
124 " --version\t\tPrint the compiler version number\n",
130 static void cbmsys (const char* sys)
131 /* Define a CBM system */
133 AddNumericMacro ("__CBM__", 1);
134 AddNumericMacro (sys, 1);
139 static int MapSys (const char* Name)
140 /* Map a target name to a system code. Return -1 in case of an error */
144 /* Check for a numeric target */
145 if (isdigit (*Name)) {
146 int Target = atoi (Name);
147 if (Target >= 0 && Target < TGT_COUNT) {
152 /* Check for a target string */
153 for (I = 0; I < TGT_COUNT; ++I) {
154 if (strcmp (TargetNames [I], Name) == 0) {
164 static void SetSys (const char* Sys)
165 /* Define a target system */
167 switch (Target = MapSys (Sys)) {
173 AddNumericMacro ("__ATARI__", 1);
189 cbmsys ("__PLUS4__");
193 cbmsys ("__CBM610__");
201 AddNumericMacro ("__NES__", 1);
205 AddNumericMacro ("__APPLE2__", 1);
209 /* Do not handle as a CBM system */
210 AddNumericMacro ("__GEOS__", 1);
214 fputs ("Unknown system type\n", stderr);
221 static void DefineSym (const char* Def)
222 /* Define a symbol on the command line */
226 /* The symbol must start with a character or underline */
227 if (Def [0] != '_' && !isalpha (Def [0])) {
231 /* Check the symbol name */
232 while (isalnum (*P) || *P == '_') {
236 /* Do we have a value given? */
241 /* No value given. Define the macro with the value 1 */
242 AddNumericMacro (Def, 1);
244 /* We have a value, P points to the '=' character. Since the argument
245 * is const, create a copy and replace the '=' in the copy by a zero
249 unsigned Len = strlen (Def)+1;
250 char* S = xmalloc (Len);
251 memcpy (S, Def, Len);
255 /* Define this as a macro */
258 /* Release the allocated memory */
265 static void OptAddSource (const char* Opt, const char* Arg)
266 /* Add source lines as comments in generated assembler file */
273 static void OptAnsi (const char* Opt, const char* Arg)
274 /* Compile in strict ANSI mode */
281 static void OptCPU (const char* Opt, const char* Arg)
282 /* Handle the --cpu option */
287 if (strcmp (Arg, "6502") == 0) {
289 } else if (strcmp (Arg, "65C02") == 0) {
292 fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
299 static void OptDebug (const char* Opt, const char* Arg)
300 /* Compiler debug mode */
307 static void OptDebugInfo (const char* Opt, const char* Arg)
308 /* Add debug info to the object file */
315 static void OptHelp (const char* Opt, const char* Arg)
316 /* Print usage information and exit */
324 static void OptIncludeDir (const char* Opt, const char* Arg)
325 /* Add an include search path */
330 AddIncludePath (Arg, INC_SYS | INC_USER);
335 static void OptSignedChars (const char* Opt, const char* Arg)
336 /* Make default characters signed */
343 static void OptStaticLocals (const char* Opt, const char* Arg)
344 /* Place local variables in static storage */
351 static void OptTarget (const char* Opt, const char* Arg)
352 /* Set the target system */
362 static void OptVerbose (const char* Opt, const char* Arg)
363 /* Increase verbosity */
370 static void OptVersion (const char* Opt, const char* Arg)
371 /* Print the assembler version */
375 VER_MAJOR, VER_MINOR, VER_PATCH);
380 int main (int argc, char* argv[])
382 /* Program long options */
383 static const LongOpt OptTab[] = {
384 { "--add-source", 0, OptAddSource },
385 { "--ansi", 0, OptAnsi },
386 { "--cpu", 1, OptCPU },
387 { "--debug", 0, OptDebug },
388 { "--debug-info", 0, OptDebugInfo },
389 { "--help", 0, OptHelp },
390 { "--include-dir", 1, OptIncludeDir },
391 { "--signed-chars", 0, OptSignedChars },
392 { "--static-locals", 0, OptStaticLocals },
393 { "--target", 1, OptTarget },
394 { "--verbose", 0, OptVerbose },
395 { "--version", 0, OptVersion },
400 /* Initialize the output file name */
401 const char* OutputFile = 0;
402 const char* InputFile = 0;
404 /* Initialize the cmdline module */
405 InitCmdLine (argc, argv, "cc65");
407 /* Parse the command line */
413 /* Get the argument */
414 const char* Arg = argv [I];
416 /* Check for an option */
417 if (Arg [0] == '-') {
422 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
435 OptDebugInfo (Arg, 0);
439 OptSignedChars (Arg, 0);
443 OutputFile = GetArg (&I, 2);
447 OptTarget (Arg, GetArg (&I, 2));
463 OptStaticLocals (Arg, 0);
473 DefineSym (GetArg (&I, 2));
477 OptIncludeDir (Arg, GetArg (&I, 2));
486 sscanf (P, "%lx", (long*) &OptDisable);
502 OptAddSource (Arg, 0);
519 fprintf (stderr, "additional file specs ignored\n");
529 /* Did we have a file spec on the command line? */
530 if (InputFile == 0) {
531 fprintf (stderr, "%s: No input files\n", argv [0]);
535 /* Open the input file */
536 OpenMainFile (InputFile);
538 /* Create the output file name if it was not explicitly given */
539 if (OutputFile == 0) {
540 OutputFile = MakeFilename (InputFile, ".s");
546 /* Create the output file if we didn't had any errors */
547 if (ErrorCount == 0 || Debug) {
551 /* Optimize the output if requested */
557 F = fopen (OutputFile, "w");
559 Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
562 /* Write the output to the file */
565 /* Close the file, check for errors */
566 if (fclose (F) != 0) {
568 Fatal (FAT_CANNOT_WRITE_OUTPUT);
572 /* Return an apropriate exit code */
573 return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;