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