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