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