]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
a0f570d215a5a01907fee569541747845bd093c8
[cc65] / src / cc65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                             cc65 main program                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <errno.h>
40
41 /* common */
42 #include "abend.h"
43 #include "chartype.h"
44 #include "cmdline.h"
45 #include "cpu.h"
46 #include "debugflag.h"
47 #include "fname.h"
48 #include "mmodel.h"
49 #include "print.h"
50 #include "segnames.h"
51 #include "strbuf.h"
52 #include "target.h"
53 #include "tgttrans.h"
54 #include "version.h"
55 #include "xmalloc.h"
56
57 /* cc65 */
58 #include "asmcode.h"
59 #include "compile.h"
60 #include "codeopt.h"
61 #include "error.h"
62 #include "global.h"
63 #include "incpath.h"
64 #include "input.h"
65 #include "macrotab.h"
66 #include "output.h"
67 #include "scanner.h"
68 #include "segments.h"
69 #include "standard.h"
70 #include "svnversion.h"
71
72
73
74 /*****************************************************************************/
75 /*                                   Code                                    */
76 /*****************************************************************************/
77
78
79
80 static void Usage (void)
81 /* Print usage information to stderr */
82 {
83     printf ("Usage: %s [options] file\n"
84             "Short options:\n"
85             "  -Cl\t\t\tMake local variables static\n"
86             "  -Dsym[=defn]\t\tDefine a symbol\n"
87             "  -E\t\t\tStop after the preprocessing stage\n"
88             "  -I dir\t\tSet an include directory search path\n"
89             "  -O\t\t\tOptimize code\n"
90             "  -Oi\t\t\tOptimize code, inline more code\n"
91             "  -Or\t\t\tEnable register variables\n"
92             "  -Os\t\t\tInline some known functions\n"
93             "  -T\t\t\tInclude source as comment\n"
94             "  -V\t\t\tPrint the compiler version number\n"
95             "  -W\t\t\tSuppress warnings\n"
96             "  -d\t\t\tDebug mode\n"
97             "  -g\t\t\tAdd debug info to object file\n"
98             "  -h\t\t\tHelp (this text)\n"
99             "  -j\t\t\tDefault characters are signed\n"
100             "  -mm model\t\tSet the memory model\n"
101             "  -o name\t\tName the output file\n"
102             "  -r\t\t\tEnable register variables\n"
103             "  -t sys\t\tSet the target system\n"
104             "  -v\t\t\tIncrease verbosity\n"
105             "\n"
106             "Long options:\n"
107             "  --add-source\t\tInclude source as comment\n"
108             "  --bss-name seg\tSet the name of the BSS segment\n"
109             "  --check-stack\t\tGenerate stack overflow checks\n"
110             "  --code-name seg\tSet the name of the CODE segment\n"
111             "  --codesize x\t\tAccept larger code by factor x\n"
112             "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
113             "  --create-dep\t\tCreate a make dependency file\n"
114             "  --data-name seg\tSet the name of the DATA segment\n"
115             "  --debug\t\tDebug mode\n"
116             "  --debug-info\t\tAdd debug info to object file\n"
117             "  --debug-opt name\tDebug optimization steps\n"
118             "  --disable-opt name\tDisable an optimization step\n"
119             "  --enable-opt name\tEnable an optimization step\n"
120             "  --forget-inc-paths\tForget include search paths\n"
121             "  --help\t\tHelp (this text)\n"
122             "  --include-dir dir\tSet an include directory search path\n"
123             "  --list-opt-steps\tList all optimizer steps and exit\n"
124             "  --memory-model model\tSet the memory model\n"
125             "  --register-space b\tSet space available for register variables\n"
126             "  --register-vars\tEnable register variables\n"
127             "  --rodata-name seg\tSet the name of the RODATA segment\n"
128             "  --signed-chars\tDefault characters are signed\n"
129             "  --standard std\tLanguage standard (c89, c99, cc65)\n"
130             "  --static-locals\tMake local variables static\n"
131             "  --target sys\t\tSet the target system\n"
132             "  --verbose\t\tIncrease verbosity\n"
133             "  --version\t\tPrint the compiler version number\n"
134             "  --writable-strings\tMake string literals writable\n",
135             ProgName);
136 }
137
138
139
140 static void cbmsys (const char* sys)
141 /* Define a CBM system */
142 {
143     DefineNumericMacro ("__CBM__", 1);
144     DefineNumericMacro (sys, 1);
145 }
146
147
148
149 static void SetSys (const char* Sys)
150 /* Define a target system */
151 {
152     switch (Target = FindTarget (Sys)) {
153
154         case TGT_NONE:
155             break;
156
157         case TGT_MODULE:
158             AbEnd ("Cannot use `module' as a target for the compiler");
159             break;
160
161         case TGT_ATARI:
162             DefineNumericMacro ("__ATARI__", 1);
163             break;
164
165         case TGT_C16:
166             cbmsys ("__C16__");
167             break;
168
169         case TGT_C64:
170             cbmsys ("__C64__");
171             break;
172
173         case TGT_VIC20:
174             cbmsys ("__VIC20__");
175             break;
176
177         case TGT_C128:
178             cbmsys ("__C128__");
179             break;
180
181         case TGT_PLUS4:
182             cbmsys ("__PLUS4__");
183             break;
184
185         case TGT_CBM510:
186             cbmsys ("__CBM510__");
187             break;
188
189         case TGT_CBM610:
190             cbmsys ("__CBM610__");
191             break;
192
193         case TGT_PET:
194             cbmsys ("__PET__");
195             break;
196
197         case TGT_BBC:
198             DefineNumericMacro ("__BBC__", 1);
199             break;
200
201         case TGT_APPLE2:
202             DefineNumericMacro ("__APPLE2__", 1);
203             break;
204
205         case TGT_APPLE2ENH:
206             DefineNumericMacro ("__APPLE2ENH__", 1);
207             break;
208
209         case TGT_GEOS:
210             /* Do not handle as a CBM system */
211             DefineNumericMacro ("__GEOS__", 1);
212             break;
213
214         case TGT_LUNIX:
215             DefineNumericMacro ("__LUNIX__", 1);
216             break;
217
218         case TGT_ATMOS:
219             DefineNumericMacro ("__ATMOS__", 1);
220             break;
221
222         case TGT_NES:
223             DefineNumericMacro ("__NES__", 1);
224             break;
225
226         case TGT_SUPERVISION:
227             DefineNumericMacro ("__SUPERVISION__", 1);
228             break;
229
230         case TGT_LYNX:
231             DefineNumericMacro ("__LYNX__", 1);
232             break;
233
234         default:
235             AbEnd ("Unknown target system type %d", Target);
236     }
237
238     /* Initialize the translation tables for the target system */
239     TgtTranslateInit ();
240 }
241
242
243
244 static void DoCreateDep (const char* OutputName)
245 /* Create the dependency file */
246 {
247     /* Make the dependency file name from the output file name */
248     char* DepName = MakeFilename (OutputName, ".u");
249
250     /* Open the file */
251     FILE* F = fopen (DepName, "w");
252     if (F == 0) {
253         Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
254     }
255
256     /* Write the dependencies to the file */
257     WriteDependencies (F, OutputName);
258
259     /* Close the file, check for errors */
260     if (fclose (F) != 0) {
261         remove (DepName);
262         Fatal ("Cannot write to dependeny file (disk full?)");
263     }
264
265     /* Free the name */
266     xfree (DepName);
267 }
268
269
270
271 static void DefineSym (const char* Def)
272 /* Define a symbol on the command line */
273 {
274     const char* P = Def;
275
276     /* The symbol must start with a character or underline */
277     if (Def [0] != '_' && !IsAlpha (Def [0])) {
278         InvDef (Def);
279     }
280
281     /* Check the symbol name */
282     while (IsAlNum (*P) || *P == '_') {
283         ++P;
284     }
285
286     /* Do we have a value given? */
287     if (*P != '=') {
288         if (*P != '\0') {
289             InvDef (Def);
290         }
291         /* No value given. Define the macro with the value 1 */
292         DefineNumericMacro (Def, 1);
293     } else {
294         /* We have a value, P points to the '=' character. Since the argument
295          * is const, create a copy and replace the '=' in the copy by a zero
296          * terminator.
297          */
298         char* Q;
299         unsigned Len = strlen (Def)+1;
300         char* S = (char*) xmalloc (Len);
301         memcpy (S, Def, Len);
302         Q = S + (P - Def);
303         *Q++ = '\0';
304
305         /* Define this as a macro */
306         DefineTextMacro (S, Q);
307
308         /* Release the allocated memory */
309         xfree (S);
310     }
311 }
312
313
314
315 static void CheckSegName (const char* Seg)
316 /* Abort if the given name is not a valid segment name */
317 {
318     /* Print an error and abort if the name is not ok */
319     if (!ValidSegName (Seg)) {
320         AbEnd ("Segment name `%s' is invalid", Seg);
321     }
322 }
323
324
325
326 static void OptAddSource (const char* Opt attribute ((unused)),
327                           const char* Arg attribute ((unused)))
328 /* Add source lines as comments in generated assembler file */
329 {
330     AddSource = 1;
331 }
332
333
334
335 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
336 /* Handle the --bss-name option */
337 {
338     /* Check for a valid name */
339     CheckSegName (Arg);
340
341     /* Set the name */
342     SetSegName (SEG_BSS, Arg);
343 }
344
345
346
347 static void OptCheckStack (const char* Opt attribute ((unused)),
348                            const char* Arg attribute ((unused)))
349 /* Handle the --check-stack option */
350 {
351     IS_Set (&CheckStack, 1);
352 }
353
354
355
356 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
357 /* Handle the --code-name option */
358 {
359     /* Check for a valid name */
360     CheckSegName (Arg);
361
362     /* Set the name */
363     SetSegName (SEG_CODE, Arg);
364 }
365
366
367
368 static void OptCodeSize (const char* Opt, const char* Arg)
369 /* Handle the --codesize option */
370 {
371     unsigned Factor;
372     char     BoundsCheck;
373
374     /* Numeric argument expected */
375     if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
376         Factor < 10 || Factor > 1000) {
377         AbEnd ("Argument for %s is invalid", Opt);
378     }
379     IS_Set (&CodeSizeFactor, Factor);
380 }
381
382
383
384 static void OptCreateDep (const char* Opt attribute ((unused)),
385                           const char* Arg attribute ((unused)))
386 /* Handle the --create-dep option */
387 {
388     CreateDep = 1;
389 }
390
391
392
393 static void OptCPU (const char* Opt, const char* Arg)
394 /* Handle the --cpu option */
395 {
396     /* Find the CPU from the given name */
397     CPU = FindCPU (Arg);
398     if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
399         CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
400         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
401     }
402 }
403
404
405
406 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
407 /* Handle the --data-name option */
408 {
409     /* Check for a valid name */
410     CheckSegName (Arg);
411
412     /* Set the name */
413     SetSegName (SEG_DATA, Arg);
414 }
415
416
417
418 static void OptDebug (const char* Opt attribute ((unused)),
419                       const char* Arg attribute ((unused)))
420 /* Compiler debug mode */
421 {
422     ++Debug;
423 }
424
425
426
427 static void OptDebugInfo (const char* Opt attribute ((unused)),
428                           const char* Arg attribute ((unused)))
429 /* Add debug info to the object file */
430 {
431     DebugInfo = 1;
432 }
433
434
435
436 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
437 /* Debug optimization steps */
438 {
439     char Buf [128];
440     char* Line;
441
442     /* Open the file */
443     FILE* F = fopen (Arg, "r");
444     if (F == 0) {
445         AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
446     }
447
448     /* Read line by line, ignore empty lines and switch optimization
449      * steps on/off.
450      */
451     while (fgets (Buf, sizeof (Buf), F) != 0) {
452
453         /* Remove trailing control chars. This will also remove the
454          * trailing newline.
455          */
456         unsigned Len = strlen (Buf);
457         while (Len > 0 && IsControl (Buf[Len-1])) {
458             --Len;
459         }
460         Buf[Len] = '\0';
461
462         /* Get a pointer to the buffer and remove leading white space */
463         Line = Buf;
464         while (IsBlank (*Line)) {
465             ++Line;
466         }
467
468         /* Check the first character and enable/disable the step or
469          * ignore the line
470          */
471         switch (*Line) {
472
473             case '\0':
474             case '#':
475             case ';':
476                 /* Empty or comment line */
477                 continue;
478
479             case '-':
480                 DisableOpt (Line+1);
481                 break;
482
483             case '+':
484                 ++Line;
485                 /* FALLTHROUGH */
486
487             default:
488                 EnableOpt (Line);
489                 break;
490
491         }
492
493     }
494
495     /* Close the file, no error check here since we were just reading and
496      * this is only a debug function.
497      */
498     (void) fclose (F);
499 }
500
501
502
503 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
504 /* Disable an optimization step */
505 {
506     DisableOpt (Arg);
507 }
508
509
510
511 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
512 /* Enable an optimization step */
513 {
514     EnableOpt (Arg);
515 }
516
517
518
519 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
520                                const char* Arg attribute ((unused)))
521 /* Forget all currently defined include paths */
522 {
523     ForgetAllIncludePaths ();
524 }
525
526
527
528 static void OptHelp (const char* Opt attribute ((unused)),
529                      const char* Arg attribute ((unused)))
530 /* Print usage information and exit */
531 {
532     Usage ();
533     exit (EXIT_SUCCESS);
534 }
535
536
537
538 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
539 /* Add an include search path */
540 {
541     AddIncludePath (Arg, INC_SYS | INC_USER);
542 }
543
544
545
546 static void OptListOptSteps (const char* Opt attribute ((unused)),
547                              const char* Arg attribute ((unused)))
548 /* List all optimizer steps */
549 {
550     /* List the optimizer steps */
551     ListOptSteps (stdout);
552
553     /* Terminate */
554     exit (EXIT_SUCCESS);
555 }
556
557
558
559 static void OptMemoryModel (const char* Opt, const char* Arg)
560 /* Set the memory model */
561 {
562     mmodel_t M;
563
564     /* Check the current memory model */
565     if (MemoryModel != MMODEL_UNKNOWN) {
566         AbEnd ("Cannot use option `%s' twice", Opt);
567     }
568
569     /* Translate the memory model name and check it */
570     M = FindMemoryModel (Arg);
571     if (M == MMODEL_UNKNOWN) {
572         AbEnd ("Unknown memory model: %s", Arg);
573     } else if (M == MMODEL_HUGE) {
574         AbEnd ("Unsupported memory model: %s", Arg);
575     }
576
577     /* Set the memory model */
578     SetMemoryModel (M);
579 }
580
581
582
583 static void OptRegisterSpace (const char* Opt, const char* Arg)
584 /* Handle the --register-space option */
585 {
586     /* Numeric argument expected */
587     if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
588         AbEnd ("Argument for option %s is invalid", Opt);
589     }
590 }
591
592
593
594 static void OptRegisterVars (const char* Opt attribute ((unused)),
595                              const char* Arg attribute ((unused)))
596 /* Handle the --register-vars option */
597 {
598     IS_Set (&EnableRegVars, 1);
599 }
600
601
602
603 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
604 /* Handle the --rodata-name option */
605 {
606     /* Check for a valid name */
607     CheckSegName (Arg);
608
609     /* Set the name */
610     SetSegName (SEG_RODATA, Arg);
611 }
612
613
614
615 static void OptSignedChars (const char* Opt attribute ((unused)),
616                             const char* Arg attribute ((unused)))
617 /* Make default characters signed */
618 {
619     IS_Set (&SignedChars, 1);
620 }
621
622
623
624 static void OptStandard (const char* Opt, const char* Arg)
625 /* Handle the --standard option */
626 {
627     /* Find the standard from the given name */
628     standard_t Std = FindStandard (Arg);
629     if (Std == STD_UNKNOWN) {
630         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
631     } else if (IS_Get (&Standard) != STD_UNKNOWN) {
632         AbEnd ("Option %s given more than once", Opt);
633     } else {
634         IS_Set (&Standard, Std);
635     }
636 }
637
638
639
640 static void OptStaticLocals (const char* Opt attribute ((unused)),
641                              const char* Arg attribute ((unused)))
642 /* Place local variables in static storage */
643 {
644     IS_Set (&StaticLocals, 1);
645 }
646
647
648
649 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
650 /* Set the target system */
651 {
652     SetSys (Arg);
653 }
654
655
656
657 static void OptVerbose (const char* Opt attribute ((unused)),
658                         const char* Arg attribute ((unused)))
659 /* Increase verbosity */
660 {
661     ++Verbosity;
662 }
663
664
665
666 static void OptVersion (const char* Opt attribute ((unused)),
667                         const char* Arg attribute ((unused)))
668 /* Print the compiler version */
669 {
670     fprintf (stderr,
671              "cc65 V%s\nSVN version: %s\n",
672              GetVersionAsString (), SVNVersion);
673     exit (EXIT_SUCCESS);
674 }
675
676
677
678 static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
679 /* Handle the -W option */
680 {
681     StrBuf W = AUTO_STRBUF_INITIALIZER;
682
683     /* Arg is a list of suboptions, separated by commas */
684     while (Arg) {
685
686         const char* Pos;
687         int         Enabled = 1;
688         IntStack*   S;
689
690         /* The suboption may be prefixed with '-' or '+' */
691         if (*Arg == '-') {
692             Enabled = 0;
693             ++Arg;
694         } else if (*Arg == '+') {
695             /* This is the default */
696             ++Arg;
697         }
698
699         /* Get the next suboption */
700         Pos = strchr (Arg, ',');
701         if (Pos) {
702             SB_CopyBuf (&W, Arg, Pos - Arg);
703             Arg = Pos + 1;
704         } else {
705             SB_CopyStr (&W, Arg);
706             Arg = 0;
707         }
708         SB_Terminate (&W);
709
710         /* Search for the warning */
711         S = FindWarning (SB_GetConstBuf (&W));
712         if (S == 0) {
713             InvArg (Opt, SB_GetConstBuf (&W));
714         }
715         IS_Set (S, Enabled);
716     }
717
718     /* Free allocated memory */
719     SB_Done (&W);
720 }
721
722
723
724 static void OptWritableStrings (const char* Opt attribute ((unused)),
725                                 const char* Arg attribute ((unused)))
726 /* Make string literals writable */
727 {
728     IS_Set (&WritableStrings, 1);
729 }
730
731
732
733 int main (int argc, char* argv[])
734 {
735     /* Program long options */
736     static const LongOpt OptTab[] = {
737         { "--add-source",       0,      OptAddSource            },
738         { "--bss-name",         1,      OptBssName              },
739         { "--check-stack",      0,      OptCheckStack           },
740         { "--code-name",        1,      OptCodeName             },
741         { "--codesize",         1,      OptCodeSize             },
742         { "--cpu",              1,      OptCPU                  },
743         { "--create-dep",       0,      OptCreateDep            },
744         { "--data-name",        1,      OptDataName             },
745         { "--debug",            0,      OptDebug                },
746         { "--debug-info",       0,      OptDebugInfo            },
747         { "--debug-opt",        1,      OptDebugOpt             },
748         { "--disable-opt",      1,      OptDisableOpt           },
749         { "--enable-opt",       1,      OptEnableOpt            },
750         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
751         { "--help",             0,      OptHelp                 },
752         { "--include-dir",      1,      OptIncludeDir           },
753         { "--list-opt-steps",   0,      OptListOptSteps         },
754         { "--memory-model",     1,      OptMemoryModel          },
755         { "--register-space",   1,      OptRegisterSpace        },
756         { "--register-vars",    0,      OptRegisterVars         },
757         { "--rodata-name",      1,      OptRodataName           },
758         { "--signed-chars",     0,      OptSignedChars          },
759         { "--standard",         1,      OptStandard             },
760         { "--static-locals",    0,      OptStaticLocals         },
761         { "--target",           1,      OptTarget               },
762         { "--verbose",          0,      OptVerbose              },
763         { "--version",          0,      OptVersion              },
764         { "--writable-strings", 0,      OptWritableStrings      },
765     };
766
767     unsigned I;
768
769     /* Initialize the input file name */
770     const char* InputFile  = 0;
771
772     /* Initialize the cmdline module */
773     InitCmdLine (&argc, &argv, "cc65");
774
775     /* Initialize the default segment names */
776     InitSegNames ();
777
778     /* Initialize the include search paths */
779     InitIncludePaths ();
780
781     /* Parse the command line */
782     I = 1;
783     while (I < ArgCount) {
784
785         const char* P;
786
787         /* Get the argument */
788         const char* Arg = ArgVec[I];
789
790         /* Check for an option */
791         if (Arg[0] == '-') {
792
793             switch (Arg[1]) {
794
795                 case '-':
796                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
797                     break;
798
799                 case 'd':
800                     OptDebug (Arg, 0);
801                     break;
802
803                 case 'h':
804                 case '?':
805                     OptHelp (Arg, 0);
806                     break;
807
808                 case 'g':
809                     OptDebugInfo (Arg, 0);
810                     break;
811
812                 case 'j':
813                     OptSignedChars (Arg, 0);
814                     break;
815
816                 case 'o':
817                     SetOutputName (GetArg (&I, 2));
818                     break;
819
820                 case 'r':
821                     OptRegisterVars (Arg, 0);
822                     break;
823
824                 case 't':
825                     OptTarget (Arg, GetArg (&I, 2));
826                     break;
827
828                 case 'u':
829                     OptCreateDep (Arg, 0);
830                     break;
831
832                 case 'v':
833                     OptVerbose (Arg, 0);
834                     break;
835
836                 case 'C':
837                     P = Arg + 2;
838                     while (*P) {
839                         switch (*P++) {
840                             case 'l':
841                                 OptStaticLocals (Arg, 0);
842                                 break;
843                             default:
844                                 UnknownOption (Arg);
845                                 break;
846                         }
847                     }
848                     break;
849
850                 case 'D':
851                     DefineSym (GetArg (&I, 2));
852                     break;
853
854                 case 'E':
855                     PreprocessOnly = 1;
856                     break;
857
858                 case 'I':
859                     OptIncludeDir (Arg, GetArg (&I, 2));
860                     break;
861
862                 case 'O':
863                     IS_Set (&Optimize, 1);
864                     P = Arg + 2;
865                     while (*P) {
866                         switch (*P++) {
867                             case 'i':
868                                 IS_Set (&CodeSizeFactor, 200);
869                                 break;
870                             case 'r':
871                                 IS_Set (&EnableRegVars, 1);
872                                 break;
873                             case 's':
874                                 IS_Set (&InlineStdFuncs, 1);
875                                 break;
876                         }
877                     }
878                     break;
879
880                 case 'T':
881                     OptAddSource (Arg, 0);
882                     break;
883
884                 case 'V':
885                     OptVersion (Arg, 0);
886                     break;
887
888                 case 'W':
889                     OptWarning (Arg, GetArg (&I, 2));
890                     break;
891
892                 default:
893                     UnknownOption (Arg);
894                     break;
895             }
896         } else {
897             if (InputFile) {
898                 fprintf (stderr, "additional file specs ignored\n");
899             } else {
900                 InputFile = Arg;
901             }
902         }
903
904         /* Next argument */
905         ++I;
906     }
907
908     /* Did we have a file spec on the command line? */
909     if (InputFile == 0) {
910         AbEnd ("No input files");
911     }
912
913     /* Create the output file name if it was not explicitly given */
914     MakeDefaultOutputName (InputFile);
915
916     /* If no CPU given, use the default CPU for the target */
917     if (CPU == CPU_UNKNOWN) {
918         if (Target != TGT_UNKNOWN) {
919             CPU = DefaultCPU[Target];
920         } else {
921             CPU = CPU_6502;
922         }
923     }
924
925     /* If no memory model was given, use the default */
926     if (MemoryModel == MMODEL_UNKNOWN) {
927         SetMemoryModel (MMODEL_NEAR);
928     }
929
930     /* If no language standard was given, use the default one */
931     if (IS_Get (&Standard) == STD_UNKNOWN) {
932         IS_Set (&Standard, STD_DEFAULT);
933     }
934
935     /* Go! */
936     Compile (InputFile);
937
938     /* Create the output file if we didn't had any errors */
939     if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
940
941         /* Emit literals, externals, do cleanup and optimizations */
942         FinishCompile ();
943
944         /* Open the file */
945         OpenOutputFile ();
946
947         /* Write the output to the file */
948         WriteAsmOutput ();
949         Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
950
951         /* Close the file, check for errors */
952         CloseOutputFile ();
953
954         /* Create dependencies if requested */
955         if (CreateDep) {
956             DoCreateDep (OutputFilename);
957             Print (stdout, 1, "Creating dependeny file\n");
958         }
959
960     }
961
962     /* Return an apropriate exit code */
963     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
964 }
965
966
967