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