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