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