]> git.sur5r.net Git - cc65/blob - src/ca65/main.c
Use the new InvArg function.
[cc65] / src / ca65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                 Main program for the ca65 macroassembler                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-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 <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40
41 /* common */
42 #include "addrsize.h"
43 #include "chartype.h"
44 #include "cmdline.h"
45 #include "mmodel.h"
46 #include "print.h"
47 #include "strbuf.h"
48 #include "target.h"
49 #include "tgttrans.h"
50 #include "version.h"
51
52 /* ca65 */
53 #include "abend.h"
54 #include "asserts.h"
55 #include "error.h"
56 #include "expr.h"
57 #include "feature.h"
58 #include "filetab.h"
59 #include "global.h"
60 #include "incpath.h"
61 #include "instr.h"
62 #include "istack.h"
63 #include "lineinfo.h"
64 #include "listing.h"
65 #include "macpack.h"
66 #include "macro.h"
67 #include "nexttok.h"
68 #include "objfile.h"
69 #include "options.h"
70 #include "pseudo.h"
71 #include "scanner.h"
72 #include "segment.h"
73 #include "sizeof.h"
74 #include "spool.h"
75 #include "symtab.h"
76 #include "ulabel.h"
77
78
79
80 /*****************************************************************************/
81 /*                                   Code                                    */
82 /*****************************************************************************/
83
84
85
86 static void Usage (void)
87 /* Print usage information and exit */
88 {
89     printf ("Usage: %s [options] file\n"
90             "Short options:\n"
91             "  -D name[=value]\tDefine a symbol\n"
92             "  -I dir\t\tSet an include directory search path\n"
93             "  -U\t\t\tMark unresolved symbols as import\n"
94             "  -V\t\t\tPrint the assembler version\n"
95             "  -W n\t\t\tSet warning level n\n"
96             "  -g\t\t\tAdd debug info to object file\n"
97             "  -h\t\t\tHelp (this text)\n"
98             "  -i\t\t\tIgnore case of symbols\n"
99             "  -l\t\t\tCreate a listing if assembly was ok\n"
100             "  -mm model\t\tSet the memory model\n"
101             "  -o name\t\tName the output file\n"
102             "  -s\t\t\tEnable smart mode\n"
103             "  -t sys\t\tSet the target system\n"
104             "  -v\t\t\tIncrease verbosity\n"
105             "\n"
106             "Long options:\n"
107             "  --auto-import\t\tMark unresolved symbols as import\n"
108             "  --cpu type\t\tSet cpu type\n"
109             "  --debug-info\t\tAdd debug info to object file\n"
110             "  --feature name\tSet an emulation feature\n"
111             "  --forget-inc-paths\tForget include search paths\n"
112             "  --help\t\tHelp (this text)\n"
113             "  --ignore-case\t\tIgnore case of symbols\n"
114             "  --include-dir dir\tSet an include directory search path\n"
115             "  --listing\t\tCreate a listing if assembly was ok\n"
116             "  --list-bytes n\tMaximum number of bytes per listing line\n"
117             "  --macpack-dir dir\tSet a macro package directory\n"
118             "  --memory-model model\tSet the memory model\n"
119             "  --pagelength n\tSet the page length for the listing\n"
120             "  --smart\t\tEnable smart mode\n"
121             "  --target sys\t\tSet the target system\n"
122             "  --verbose\t\tIncrease verbosity\n"
123             "  --version\t\tPrint the assembler version\n",
124             ProgName);
125 }
126
127
128
129 static void SetOptions (void)
130 /* Set the option for the translator */
131 {
132     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
133
134     /* Set the translator */
135     SB_Printf (&Buf, "ca65 V%s", GetVersionAsString ());
136     OptTranslator (&Buf);
137
138     /* Set date and time */
139     OptDateTime ((unsigned long) time(0));
140
141     /* Release memory for the string */
142     SB_Done (&Buf);
143 }
144
145
146
147 static void NewSymbol (const char* SymName, long Val)
148 /* Define a symbol with a fixed numeric value in the current scope */
149 {
150     ExprNode* Expr;
151     SymEntry* Sym;
152
153     /* Convert the name to a string buffer */
154     StrBuf SymBuf = STATIC_STRBUF_INITIALIZER;
155     SB_CopyStr (&SymBuf, SymName);
156
157     /* Search for the symbol, allocate a new one if it doesn't exist */
158     Sym = SymFind (CurrentScope, &SymBuf, SYM_ALLOC_NEW);
159
160     /* Check if have already a symbol with this name */
161     if (SymIsDef (Sym)) {
162         AbEnd ("`%s' is already defined", SymName);
163     }
164
165     /* Generate an expression for the symbol */
166     Expr = GenLiteralExpr (Val);
167
168     /* Mark the symbol as defined */
169     SymDef (Sym, Expr, ADDR_SIZE_DEFAULT, SF_NONE);
170
171     /* Free string buffer memory */
172     SB_Done (&SymBuf);
173 }
174
175
176
177 static void CBMSystem (const char* Sys)
178 /* Define a CBM system */
179 {
180     NewSymbol ("__CBM__", 1);
181     NewSymbol (Sys, 1);
182 }
183
184
185
186 static void SetSys (const char* Sys)
187 /* Define a target system */
188 {
189     switch (Target = FindTarget (Sys)) {
190
191         case TGT_NONE:
192             break;
193
194         case TGT_MODULE:
195             AbEnd ("Cannot use `module' as a target for the assembler");
196             break;
197
198         case TGT_ATARI:
199             NewSymbol ("__ATARI__", 1);
200             break;
201
202         case TGT_C16:
203             CBMSystem ("__C16__");
204             break;
205
206         case TGT_C64:
207             CBMSystem ("__C64__");
208             break;
209
210         case TGT_VIC20:
211             CBMSystem ("__VIC20__");
212             break;
213
214         case TGT_C128:
215             CBMSystem ("__C128__");
216             break;
217
218         case TGT_PLUS4:
219             CBMSystem ("__PLUS4__");
220             break;
221
222         case TGT_CBM510:
223             CBMSystem ("__CBM510__");
224             break;
225
226         case TGT_CBM610:
227             CBMSystem ("__CBM610__");
228             break;
229
230         case TGT_PET:
231             CBMSystem ("__PET__");
232             break;
233
234         case TGT_BBC:
235             NewSymbol ("__BBC__", 1);
236             break;
237
238         case TGT_APPLE2:
239             NewSymbol ("__APPLE2__", 1);
240             break;
241
242         case TGT_APPLE2ENH:
243             NewSymbol ("__APPLE2ENH__", 1);
244             break;
245
246         case TGT_GEOS:
247             /* Do not handle as a CBM system */
248             NewSymbol ("__GEOS__", 1);
249             break;
250
251         case TGT_LUNIX:
252             NewSymbol ("__LUNIX__", 1);
253             break;
254
255         case TGT_ATMOS:
256             NewSymbol ("__ATMOS__", 1);
257             break;
258
259         case TGT_NES:
260             NewSymbol ("__NES__", 1);
261             break;
262
263         case TGT_SUPERVISION:
264             NewSymbol ("__SUPERVISION__", 1);
265             break;
266
267         case TGT_LYNX:
268             NewSymbol ("__LYNX__", 1);
269             break;
270
271         default:
272             AbEnd ("Invalid target name: `%s'", Sys);
273
274     }
275
276     /* Initialize the translation tables for the target system */
277     TgtTranslateInit ();
278 }
279
280
281
282 static void DefineSymbol (const char* Def)
283 /* Define a symbol from the command line */
284 {
285     const char* P;
286     unsigned I;
287     long Val;
288     StrBuf SymName = AUTO_STRBUF_INITIALIZER;
289
290
291     /* The symbol must start with a character or underline */
292     if (!IsIdStart (Def [0])) {
293         InvDef (Def);
294     }
295     P = Def;
296
297     /* Copy the symbol, checking the rest */
298     I = 0;
299     while (IsIdChar (*P)) {
300         SB_AppendChar (&SymName, *P++);
301     }
302     SB_Terminate (&SymName);
303
304     /* Do we have a value given? */
305     if (*P != '=') {
306         if (*P != '\0') {
307             InvDef (Def);
308         }
309         Val = 0;
310     } else {
311         /* We have a value */
312         ++P;
313         if (*P == '$') {
314             ++P;
315             if (sscanf (P, "%lx", &Val) != 1) {
316                 InvDef (Def);
317             }
318         } else {
319             if (sscanf (P, "%li", &Val) != 1) {
320                 InvDef (Def);
321             }
322         }
323     }
324
325     /* Define the new symbol */
326     NewSymbol (SB_GetConstBuf (&SymName), Val);
327
328     /* Release string memory */
329     SB_Done (&SymName);
330 }
331
332
333
334 static void OptAutoImport (const char* Opt attribute ((unused)),
335                            const char* Arg attribute ((unused)))
336 /* Mark unresolved symbols as imported */
337 {
338     AutoImport = 1;
339 }
340
341
342
343 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
344 /* Handle the --cpu option */
345 {
346     cpu_t CPU = FindCPU (Arg);
347     if (CPU == CPU_UNKNOWN) {
348         AbEnd ("Invalid CPU: `%s'", Arg);
349     } else {
350         SetCPU (CPU);
351     }
352 }
353
354
355
356 static void OptDebugInfo (const char* Opt attribute ((unused)),
357                           const char* Arg attribute ((unused)))
358 /* Add debug info to the object file */
359 {
360     DbgSyms = 1;
361 }
362
363
364
365 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
366 /* Set an emulation feature */
367 {
368     /* Make a string buffer from Arg */
369     StrBuf Feature;
370
371     /* Set the feature, check for errors */
372     if (SetFeature (SB_InitFromString (&Feature, Arg)) == FEAT_UNKNOWN) {
373         AbEnd ("Illegal emulation feature: `%s'", Arg);
374     }
375 }
376
377
378
379 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
380                                const char* Arg attribute ((unused)))
381 /* Forget all currently defined include paths */
382 {
383     ForgetAllIncludePaths ();
384 }
385
386
387
388 static void OptHelp (const char* Opt attribute ((unused)),
389                      const char* Arg attribute ((unused)))
390 /* Print usage information and exit */
391 {
392     Usage ();
393     exit (EXIT_SUCCESS);
394 }
395
396
397
398 static void OptIgnoreCase (const char* Opt attribute ((unused)),
399                            const char* Arg attribute ((unused)))
400 /* Ignore case on symbols */
401 {
402     IgnoreCase = 1;
403 }
404
405
406
407 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
408 /* Add an include search path */
409 {
410     AddIncludePath (Arg);
411 }
412
413
414
415 static void OptListBytes (const char* Opt, const char* Arg)
416 /* Set the maximum number of bytes per listing line */
417 {
418     unsigned Num;
419     char     Check;
420
421     /* Convert the argument to a number */
422     if (sscanf (Arg, "%u%c", &Num, &Check) != 1) {
423         InvArg (Opt, Arg);
424     }
425
426     /* Check the bounds */
427     if (Num != 0 && (Num < MIN_LIST_BYTES || Num > MAX_LIST_BYTES)) {
428         AbEnd ("Argument for option `%s' is out of range", Opt);
429     }
430
431     /* Use the value */
432     SetListBytes (Num);
433 }
434
435
436
437 static void OptListing (const char* Opt attribute ((unused)),
438                         const char* Arg attribute ((unused)))
439 /* Create a listing file */
440 {
441     Listing = 1;
442 }
443
444
445
446 static void OptMacPackDir (const char* Opt attribute ((unused)), const char* Arg)
447 /* Set a macro package directory */
448 {
449     /* Make a string buffer from Arg */
450     StrBuf Dir;
451
452     /* Use the directory */
453     MacPackSetDir (SB_InitFromString (&Dir, Arg));
454 }
455
456
457
458 static void OptMemoryModel (const char* Opt, const char* Arg)
459 /* Set the memory model */
460 {
461     mmodel_t M;
462
463     /* Check the current memory model */
464     if (MemoryModel != MMODEL_UNKNOWN) {
465         AbEnd ("Cannot use option `%s' twice", Opt);
466     }
467
468     /* Translate the memory model name and check it */
469     M = FindMemoryModel (Arg);
470     if (M == MMODEL_UNKNOWN) {
471         AbEnd ("Unknown memory model: %s", Arg);
472     } else if (M == MMODEL_HUGE) {
473         AbEnd ("Unsupported memory model: %s", Arg);
474     }
475
476     /* Set the memory model */
477     SetMemoryModel (M);
478 }
479
480
481
482 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
483 /* Handle the --pagelength option */
484 {
485     int Len = atoi (Arg);
486     if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
487         AbEnd ("Invalid page length: %d", Len);
488     }
489     PageLength = Len;
490 }
491
492
493
494 static void OptSmart (const char* Opt attribute ((unused)),
495                       const char* Arg attribute ((unused)))
496 /* Handle the -s/--smart options */
497 {
498     SmartMode = 1;
499 }
500
501
502
503 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
504 /* Set the target system */
505 {
506     SetSys (Arg);
507 }
508
509
510
511 static void OptVerbose (const char* Opt attribute ((unused)),
512                         const char* Arg attribute ((unused)))
513 /* Increase verbosity */
514 {
515     ++Verbosity;
516 }
517
518
519
520 static void OptVersion (const char* Opt attribute ((unused)),
521                         const char* Arg attribute ((unused)))
522 /* Print the assembler version */
523 {
524     fprintf (stderr, "ca65 V%s - %s\n", GetVersionAsString (), Copyright);
525 }
526
527
528
529 static void DoPCAssign (void)
530 /* Start absolute code */
531 {
532     long PC = ConstExpression ();
533     if (PC < 0 || PC > 0xFFFFFF) {
534         Error ("Range error");
535     } else {
536         EnterAbsoluteMode (PC);
537     }
538 }
539
540
541
542 static void OneLine (void)
543 /* Assemble one line */
544 {
545     Segment*      Seg   = 0;
546     unsigned long PC    = 0;
547     SymEntry*     Sym   = 0;
548     int           Macro = 0;
549     int           Instr = -1;
550
551     /* Initialize the new listing line if we are actually reading from file
552      * and not from internally pushed input.
553      */
554     if (!HavePushedInput ()) {
555         InitListingLine ();
556     }
557
558     if (Tok == TOK_COLON) {
559         /* An unnamed label */
560         ULabDef ();
561         NextTok ();
562     }
563
564     /* If the first token on the line is an identifier, check for a macro or
565      * an instruction.
566      */
567     if (Tok == TOK_IDENT) {
568         if (!UbiquitousIdents) {
569             /* Macros and symbols cannot use instruction names */
570             Instr = FindInstruction (&SVal);
571             if (Instr < 0) {
572                 Macro = IsMacro (&SVal);
573             }
574         } else {
575             /* Macros and symbols may use the names of instructions */
576             Macro = IsMacro (&SVal);
577         }
578     }
579
580     /* Handle an identifier */
581     if (Tok == TOK_LOCAL_IDENT || (Tok == TOK_IDENT && Instr < 0 && !Macro)) {
582
583         /* Did we have whitespace before the ident? */
584         int HadWS = WS;
585
586         /* Generate the symbol table entry, then skip the name */
587         if (Tok == TOK_IDENT) {
588             Sym = SymFind (CurrentScope, &SVal, SYM_ALLOC_NEW);
589         } else {
590             Sym = SymFindLocal (SymLast, &SVal, SYM_ALLOC_NEW);
591         }
592         NextTok ();
593
594         /* If a colon follows, this is a label definition. If there
595          * is no colon, it's an assignment.
596          */
597         if (Tok == TOK_EQ || Tok == TOK_ASSIGN) {
598
599             /* Determine the symbol flags from the assignment token */
600             unsigned Flags = (Tok == TOK_ASSIGN)? SF_LABEL : SF_NONE;
601
602             /* Skip the '=' */
603             NextTok ();
604
605             /* Define the symbol with the expression following the '=' */
606             SymDef (Sym, Expression(), ADDR_SIZE_DEFAULT, Flags);
607
608             /* Don't allow anything after a symbol definition */
609             ConsumeSep ();
610             return;
611
612         } else if (Tok == TOK_SET) {
613
614             ExprNode* Expr;
615
616             /* .SET defines variables (= redefinable symbols) */
617             NextTok ();
618
619             /* Read the assignment expression, which must be constant */
620             Expr = GenLiteralExpr (ConstExpression ());
621
622             /* Define the symbol with the constant expression following
623              * the '='
624              */
625             SymDef (Sym, Expr, ADDR_SIZE_DEFAULT, SF_VAR);
626
627             /* Don't allow anything after a symbol definition */
628             ConsumeSep ();
629             return;
630
631         } else {
632
633             /* A label. Remember the current segment, so we can later
634              * determine the size of the data stored under the label.
635              */
636             Seg = ActiveSeg;
637             PC  = GetPC ();
638
639             /* Define the label */
640             SymDef (Sym, GenCurrentPC (), ADDR_SIZE_DEFAULT, SF_LABEL);
641
642             /* Skip the colon. If NoColonLabels is enabled, allow labels
643              * without a colon if there is no whitespace before the
644              * identifier.
645              */
646             if (Tok != TOK_COLON) {
647                 if (HadWS || !NoColonLabels) {
648                     Error ("`:' expected");
649                     /* Try some smart error recovery */
650                     if (Tok == TOK_NAMESPACE) {
651                         NextTok ();
652                     }
653                 }
654             } else {
655                 /* Skip the colon */
656                 NextTok ();
657             }
658
659             /* If we come here, a new identifier may be waiting, which may
660              * be a macro or instruction.
661              */
662             if (Tok == TOK_IDENT) {
663                 if (!UbiquitousIdents) {
664                     /* Macros and symbols cannot use instruction names */
665                     Instr = FindInstruction (&SVal);
666                     if (Instr < 0) {
667                         Macro = IsMacro (&SVal);
668                     }
669                 } else {
670                     /* Macros and symbols may use the names of instructions */
671                     Macro = IsMacro (&SVal);
672                 }
673             }
674         }
675     }
676
677     /* We've handled a possible label, now handle the remainder of the line */
678     if (Tok >= TOK_FIRSTPSEUDO && Tok <= TOK_LASTPSEUDO) {
679         /* A control command */
680         HandlePseudo ();
681     } else if (Macro) {
682         /* A macro expansion */
683         MacExpandStart ();
684     } else if (Instr >= 0 ||
685                (UbiquitousIdents && ((Instr = FindInstruction (&SVal)) >= 0))) {
686         /* A mnemonic - assemble one instruction */
687         HandleInstruction (Instr);
688     } else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
689         NextTok ();
690         if (Tok != TOK_EQ) {
691             Error ("`=' expected");
692             SkipUntilSep ();
693         } else {
694             /* Skip the equal sign */
695             NextTok ();
696             /* Enter absolute mode */
697             DoPCAssign ();
698         }
699     }
700
701     /* If we have defined a label, remember its size. Sym is also set by
702      * a symbol assignment, but in this case Done is false, so we don't
703      * come here.
704      */
705     if (Sym) {
706         unsigned long Size;
707         if (Seg == ActiveSeg) {
708             /* Same segment */
709             Size = GetPC () - PC;
710         } else {
711             /* The line has switched the segment */
712             Size = 0;
713         }
714         DefSizeOfSymbol (Sym, Size);
715     }
716
717     /* Line separator must come here */
718     ConsumeSep ();
719 }
720
721
722
723 static void Assemble (void)
724 /* Start the ball rolling ... */
725 {
726     /* Prime the pump */
727     NextTok ();
728
729     /* Assemble lines until end of file */
730     while (Tok != TOK_EOF) {
731         OneLine ();
732     }
733 }
734
735
736
737 static void CreateObjFile (void)
738 /* Create the object file */
739 {
740     /* Open the object, write the header */
741     ObjOpen ();
742
743     /* Write the object file options */
744     WriteOptions ();
745
746     /* Write the list of input files */
747     WriteFiles ();
748
749     /* Write the segment data to the file */
750     WriteSegments ();
751
752     /* Write the import list */
753     WriteImports ();
754
755     /* Write the export list */
756     WriteExports ();
757
758     /* Write debug symbols if requested */
759     WriteDbgSyms ();
760
761     /* Write line infos if requested */
762     WriteLineInfo ();
763
764     /* Write the string pool */
765     WriteStrPool ();
766
767     /* Write the assertions */
768     WriteAssertions ();
769
770     /* Write an updated header and close the file */
771     ObjClose ();
772 }
773
774
775
776 int main (int argc, char* argv [])
777 /* Assembler main program */
778 {
779     /* Program long options */
780     static const LongOpt OptTab[] = {
781         { "--auto-import",      0,      OptAutoImport           },
782         { "--cpu",              1,      OptCPU                  },
783         { "--debug-info",       0,      OptDebugInfo            },
784         { "--feature",          1,      OptFeature              },
785         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
786         { "--help",             0,      OptHelp                 },
787         { "--ignore-case",      0,      OptIgnoreCase           },
788         { "--include-dir",      1,      OptIncludeDir           },
789         { "--list-bytes",       1,      OptListBytes            },
790         { "--listing",          0,      OptListing              },
791         { "--macpack-dir",      1,      OptMacPackDir           },
792         { "--memory-model",     1,      OptMemoryModel          },
793         { "--pagelength",       1,      OptPageLength           },
794         { "--smart",            0,      OptSmart                },
795         { "--target",           1,      OptTarget               },
796         { "--verbose",          0,      OptVerbose              },
797         { "--version",          0,      OptVersion              },
798     };
799
800     /* Name of the global name space */
801     static const StrBuf GlobalNameSpace = STATIC_STRBUF_INITIALIZER;
802
803     unsigned I;
804
805     /* Initialize the cmdline module */
806     InitCmdLine (&argc, &argv, "ca65");
807
808     /* Initialize the include search paths */
809     InitIncludePaths ();
810
811     /* Enter the base lexical level. We must do that here, since we may
812      * define symbols using -D.
813      */
814     SymEnterLevel (&GlobalNameSpace, ST_GLOBAL, ADDR_SIZE_DEFAULT);
815
816     /* Check the parameters */
817     I = 1;
818     while (I < ArgCount) {
819
820         /* Get the argument */
821         const char* Arg = ArgVec [I];
822
823         /* Check for an option */
824         if (Arg[0] == '-') {
825             switch (Arg[1]) {
826
827                 case '-':
828                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
829                     break;
830
831                 case 'g':
832                     OptDebugInfo (Arg, 0);
833                     break;
834
835                 case 'h':
836                     OptHelp (Arg, 0);
837                     break;
838
839                 case 'i':
840                     OptIgnoreCase (Arg, 0);
841                     break;
842
843                 case 'l':
844                     OptListing (Arg, 0);
845                     break;
846
847                 case 'm':
848                     if (Arg[2] == 'm') {
849                         OptMemoryModel (Arg, GetArg (&I, 3));
850                     } else {
851                         UnknownOption (Arg);
852                     }
853                     break;
854
855                 case 'o':
856                     OutFile = GetArg (&I, 2);
857                     break;
858
859                 case 's':
860                     OptSmart (Arg, 0);
861                     break;
862
863                 case 't':
864                     OptTarget (Arg, GetArg (&I, 2));
865                     break;
866
867                 case 'v':
868                     OptVerbose (Arg, 0);
869                     break;
870
871                 case 'D':
872                     DefineSymbol (GetArg (&I, 2));
873                     break;
874
875                 case 'I':
876                     OptIncludeDir (Arg, GetArg (&I, 2));
877                     break;
878
879                 case 'U':
880                     OptAutoImport (Arg, 0);
881                     break;
882
883                 case 'V':
884                     OptVersion (Arg, 0);
885                     break;
886
887                 case 'W':
888                     WarnLevel = atoi (GetArg (&I, 2));
889                     break;
890
891                 default:
892                     UnknownOption (Arg);
893                     break;
894
895             }
896         } else {
897             /* Filename. Check if we already had one */
898             if (InFile) {
899                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
900                          ProgName, Arg);
901                 exit (EXIT_FAILURE);
902             } else {
903                 InFile = Arg;
904             }
905         }
906
907         /* Next argument */
908         ++I;
909     }
910
911     /* Do we have an input file? */
912     if (InFile == 0) {
913         fprintf (stderr, "%s: No input files\n", ProgName);
914         exit (EXIT_FAILURE);
915     }
916
917     /* If no CPU given, use the default CPU for the target */
918     if (GetCPU () == CPU_UNKNOWN) {
919         if (Target != TGT_UNKNOWN) {
920             SetCPU (DefaultCPU[Target]);
921         } else {
922             SetCPU (CPU_6502);
923         }
924     }
925
926     /* If no memory model was given, use the default */
927     if (MemoryModel == MMODEL_UNKNOWN) {
928         SetMemoryModel (MMODEL_NEAR);
929     }
930
931     /* Initialize the segments */
932     InitSegments ();
933
934     /* Initialize the scanner, open the input file */
935     InitScanner (InFile);
936
937     /* Define the default options */
938     SetOptions ();
939
940     /* Assemble the input */
941     Assemble ();
942
943     /* If we didn't have any errors, check the segment stack */
944     if (ErrorCount == 0) {
945         SegStackCheck ();
946     }
947
948     /* If we didn't have any errors, check the unnamed labels */
949     if (ErrorCount == 0) {
950         ULabCheck ();
951     }
952
953     /* If we didn't have any errors, check the symbol table */
954     if (ErrorCount == 0) {
955         SymCheck ();
956     }
957
958     /* If we didn't have any errors, check and resolve the segment data */
959     if (ErrorCount == 0) {
960         SegCheck ();
961     }
962
963     /* If we didn't have any errors, check the assertions */
964     if (ErrorCount == 0) {
965         CheckAssertions ();
966     }
967
968     /* If we didn't have an errors, index the line infos */
969     MakeLineInfoIndex ();
970
971     /* Dump the data */
972     if (Verbosity >= 2) {
973         SymDump (stdout);
974         SegDump ();
975     }
976
977     /* If we didn't have any errors, create the object and listing files */
978     if (ErrorCount == 0) {
979         CreateObjFile ();
980         if (Listing) {
981             CreateListing ();
982         }
983     }
984
985     /* Close the input file */
986     DoneScanner ();
987
988     /* Return an apropriate exit code */
989     return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;
990 }
991
992
993