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