]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
The -A and --ansi switches are gone, together with the __STRICT_ANSI__
[cc65] / src / cc65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                             cc65 main program                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-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 <string.h>
38 #include <stdlib.h>
39 #include <errno.h>
40
41 /* common */
42 #include "abend.h"
43 #include "chartype.h"
44 #include "cmdline.h"
45 #include "cpu.h"
46 #include "debugflag.h"
47 #include "fname.h"
48 #include "mmodel.h"
49 #include "print.h"
50 #include "segnames.h"
51 #include "target.h"
52 #include "tgttrans.h"
53 #include "version.h"
54 #include "xmalloc.h"
55
56 /* cc65 */
57 #include "asmcode.h"
58 #include "compile.h"
59 #include "codeopt.h"
60 #include "error.h"
61 #include "global.h"
62 #include "incpath.h"
63 #include "input.h"
64 #include "macrotab.h"
65 #include "scanner.h"
66 #include "standard.h"
67 #include "segments.h"
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 static void Usage (void)
78 /* Print usage information to stderr */
79 {
80     fprintf (stderr,
81              "Usage: %s [options] file\n"
82              "Short options:\n"
83              "  -Cl\t\t\tMake local variables static\n"
84              "  -Dsym[=defn]\t\tDefine a symbol\n"
85              "  -I dir\t\tSet an include directory search path\n"
86              "  -O\t\t\tOptimize code\n"
87              "  -Oi\t\t\tOptimize code, inline more code\n"
88              "  -Or\t\t\tEnable register variables\n"
89              "  -Os\t\t\tInline some known functions\n"
90              "  -T\t\t\tInclude source as comment\n"
91              "  -V\t\t\tPrint the compiler version number\n"
92              "  -W\t\t\tSuppress warnings\n"
93              "  -d\t\t\tDebug mode\n"
94              "  -g\t\t\tAdd debug info to object file\n"
95              "  -h\t\t\tHelp (this text)\n"
96              "  -j\t\t\tDefault characters are signed\n"
97              "  -mm model\t\tSet the memory model\n"
98              "  -o name\t\tName the output file\n"
99              "  -r\t\t\tEnable register variables\n"
100              "  -t sys\t\tSet the target system\n"
101              "  -v\t\t\tIncrease verbosity\n"
102              "\n"
103              "Long options:\n"
104              "  --add-source\t\tInclude source as comment\n"
105              "  --bss-name seg\tSet the name of the BSS segment\n"
106              "  --check-stack\t\tGenerate stack overflow checks\n"
107              "  --code-name seg\tSet the name of the CODE segment\n"
108              "  --codesize x\t\tAccept larger code by factor x\n"
109              "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
110              "  --create-dep\t\tCreate a make dependency file\n"
111              "  --data-name seg\tSet the name of the DATA segment\n"
112              "  --debug\t\tDebug mode\n"
113              "  --debug-info\t\tAdd debug info to object file\n"
114              "  --debug-opt name\tDebug optimization steps\n"
115              "  --disable-opt name\tDisable an optimization step\n"
116              "  --enable-opt name\tEnable an optimization step\n"
117              "  --forget-inc-paths\tForget include search paths\n"
118              "  --help\t\tHelp (this text)\n"
119              "  --include-dir dir\tSet an include directory search path\n"
120              "  --list-opt-steps\tList all optimizer steps and exit\n"
121              "  --memory-model model\tSet the memory model\n"
122              "  --register-space b\tSet space available for register variables\n"
123              "  --register-vars\tEnable register variables\n"
124              "  --rodata-name seg\tSet the name of the RODATA segment\n"
125              "  --signed-chars\tDefault characters are signed\n"
126              "  --standard std\tLanguage standard (c89, c99, cc65)\n"
127              "  --static-locals\tMake local variables static\n"
128              "  --target sys\t\tSet the target system\n"
129              "  --verbose\t\tIncrease verbosity\n"
130              "  --version\t\tPrint the compiler version number\n"
131              "  --writable-strings\tMake string literals writable\n",
132              ProgName);
133 }
134
135
136
137 static void cbmsys (const char* sys)
138 /* Define a CBM system */
139 {
140     DefineNumericMacro ("__CBM__", 1);
141     DefineNumericMacro (sys, 1);
142 }
143
144
145
146 static void SetSys (const char* Sys)
147 /* Define a target system */
148 {
149     switch (Target = FindTarget (Sys)) {
150
151         case TGT_NONE:
152             break;
153
154         case TGT_MODULE:
155             AbEnd ("Cannot use `module' as a target for the compiler");
156             break;
157
158         case TGT_ATARI:
159             DefineNumericMacro ("__ATARI__", 1);
160             break;
161
162         case TGT_C16:
163             cbmsys ("__C16__");
164             break;
165
166         case TGT_C64:
167             cbmsys ("__C64__");
168             break;
169
170         case TGT_VIC20:
171             cbmsys ("__VIC20__");
172             break;
173
174         case TGT_C128:
175             cbmsys ("__C128__");
176             break;
177
178         case TGT_ACE:
179             cbmsys ("__ACE__");
180             break;
181
182         case TGT_PLUS4:
183             cbmsys ("__PLUS4__");
184             break;
185
186         case TGT_CBM510:
187             cbmsys ("__CBM510__");
188             break;
189
190         case TGT_CBM610:
191             cbmsys ("__CBM610__");
192             break;
193
194         case TGT_PET:
195             cbmsys ("__PET__");
196             break;
197
198         case TGT_BBC:
199             DefineNumericMacro ("__BBC__", 1);
200             break;
201
202         case TGT_APPLE2:
203             DefineNumericMacro ("__APPLE2__", 1);
204             break;
205
206         case TGT_APPLE2ENH:
207             DefineNumericMacro ("__APPLE2ENH__", 1);
208             break;
209
210         case TGT_GEOS:
211             /* Do not handle as a CBM system */
212             DefineNumericMacro ("__GEOS__", 1);
213             break;
214
215         case TGT_LUNIX:
216             DefineNumericMacro ("__LUNIX__", 1);
217             break;
218
219         case TGT_ATMOS:
220             DefineNumericMacro ("__ATMOS__", 1);
221             break;
222
223         case TGT_NES:
224             DefineNumericMacro ("__NES__", 1);
225             break;
226
227         case TGT_SUPERVISION:
228             DefineNumericMacro ("__SUPERVISION__", 1);
229             break;
230
231         default:
232             AbEnd ("Unknown target system type %d", Target);
233     }
234
235     /* Initialize the translation tables for the target system */
236     TgtTranslateInit ();
237 }
238
239
240
241 static void DoCreateDep (const char* OutputName)
242 /* Create the dependency file */
243 {
244     /* Make the dependency file name from the output file name */
245     char* DepName = MakeFilename (OutputName, ".u");
246
247     /* Open the file */
248     FILE* F = fopen (DepName, "w");
249     if (F == 0) {
250         Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
251     }
252
253     /* Write the dependencies to the file */
254     WriteDependencies (F, OutputName);
255
256     /* Close the file, check for errors */
257     if (fclose (F) != 0) {
258         remove (DepName);
259         Fatal ("Cannot write to dependeny file (disk full?)");
260     }
261
262     /* Free the name */
263     xfree (DepName);
264 }
265
266
267
268 static void DefineSym (const char* Def)
269 /* Define a symbol on the command line */
270 {
271     const char* P = Def;
272
273     /* The symbol must start with a character or underline */
274     if (Def [0] != '_' && !IsAlpha (Def [0])) {
275         InvDef (Def);
276     }
277
278     /* Check the symbol name */
279     while (IsAlNum (*P) || *P == '_') {
280         ++P;
281     }
282
283     /* Do we have a value given? */
284     if (*P != '=') {
285         if (*P != '\0') {
286             InvDef (Def);
287         }
288         /* No value given. Define the macro with the value 1 */
289         DefineNumericMacro (Def, 1);
290     } else {
291         /* We have a value, P points to the '=' character. Since the argument
292          * is const, create a copy and replace the '=' in the copy by a zero
293          * terminator.
294          */
295         char* Q;
296         unsigned Len = strlen (Def)+1;
297         char* S = (char*) xmalloc (Len);
298         memcpy (S, Def, Len);
299         Q = S + (P - Def);
300         *Q++ = '\0';
301
302         /* Define this as a macro */
303         DefineTextMacro (S, Q);
304
305         /* Release the allocated memory */
306         xfree (S);
307     }
308 }
309
310
311
312 static void CheckSegName (const char* Seg)
313 /* Abort if the given name is not a valid segment name */
314 {
315     /* Print an error and abort if the name is not ok */
316     if (!ValidSegName (Seg)) {
317         AbEnd ("Segment name `%s' is invalid", Seg);
318     }
319 }
320
321
322
323 static void OptAddSource (const char* Opt attribute ((unused)),
324                           const char* Arg attribute ((unused)))
325 /* Add source lines as comments in generated assembler file */
326 {
327     AddSource = 1;
328 }
329
330
331
332 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
333 /* Handle the --bss-name option */
334 {
335     /* Check for a valid name */
336     CheckSegName (Arg);
337
338     /* Set the name */
339     SetSegName (SEG_BSS, Arg);
340 }
341
342
343
344 static void OptCheckStack (const char* Opt attribute ((unused)),
345                            const char* Arg attribute ((unused)))
346 /* Handle the --check-stack option */
347 {
348     IS_Set (&CheckStack, 1);
349 }
350
351
352
353 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
354 /* Handle the --code-name option */
355 {
356     /* Check for a valid name */
357     CheckSegName (Arg);
358
359     /* Set the name */
360     SetSegName (SEG_CODE, Arg);
361 }
362
363
364
365 static void OptCodeSize (const char* Opt, const char* Arg)
366 /* Handle the --codesize option */
367 {
368     unsigned Factor;
369     char     BoundsCheck;
370
371     /* Numeric argument expected */
372     if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
373         Factor < 10 || Factor > 1000) {
374         AbEnd ("Argument for %s is invalid", Opt);
375     }
376     IS_Set (&CodeSizeFactor, Factor);
377 }
378
379
380
381 static void OptCreateDep (const char* Opt attribute ((unused)),
382                           const char* Arg attribute ((unused)))
383 /* Handle the --create-dep option */
384 {
385     CreateDep = 1;
386 }
387
388
389
390 static void OptCPU (const char* Opt, const char* Arg)
391 /* Handle the --cpu option */
392 {
393     /* Find the CPU from the given name */
394     CPU = FindCPU (Arg);
395     if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
396         CPU != CPU_65C02 && CPU != CPU_65816) {
397         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
398     }
399 }
400
401
402
403 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
404 /* Handle the --data-name option */
405 {
406     /* Check for a valid name */
407     CheckSegName (Arg);
408
409     /* Set the name */
410     SetSegName (SEG_DATA, Arg);
411 }
412
413
414
415 static void OptDebug (const char* Opt attribute ((unused)),
416                       const char* Arg attribute ((unused)))
417 /* Compiler debug mode */
418 {
419     ++Debug;
420 }
421
422
423
424 static void OptDebugInfo (const char* Opt attribute ((unused)),
425                           const char* Arg attribute ((unused)))
426 /* Add debug info to the object file */
427 {
428     DebugInfo = 1;
429 }
430
431
432
433 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
434 /* Debug optimization steps */
435 {
436     char Buf [128];
437     char* Line;
438
439     /* Open the file */
440     FILE* F = fopen (Arg, "r");
441     if (F == 0) {
442         AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
443     }
444
445     /* Read line by line, ignore empty lines and switch optimization
446      * steps on/off.
447      */
448     while (fgets (Buf, sizeof (Buf), F) != 0) {
449
450         /* Remove trailing control chars. This will also remove the
451          * trailing newline.
452          */
453         unsigned Len = strlen (Buf);
454         while (Len > 0 && IsControl (Buf[Len-1])) {
455             --Len;
456         }
457         Buf[Len] = '\0';
458
459         /* Get a pointer to the buffer and remove leading white space */
460         Line = Buf;
461         while (IsBlank (*Line)) {
462             ++Line;
463         }
464
465         /* Check the first character and enable/disable the step or
466          * ignore the line
467          */
468         switch (*Line) {
469
470             case '\0':
471             case '#':
472             case ';':
473                 /* Empty or comment line */
474                 continue;
475
476             case '-':
477                 DisableOpt (Line+1);
478                 break;
479
480             case '+':
481                 ++Line;
482                 /* FALLTHROUGH */
483
484             default:
485                 EnableOpt (Line);
486                 break;
487
488         }
489
490     }
491
492     /* Close the file, no error check here since we were just reading and
493      * this is only a debug function.
494      */
495     (void) fclose (F);
496 }
497
498
499
500 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
501 /* Disable an optimization step */
502 {
503     DisableOpt (Arg);
504 }
505
506
507
508 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
509 /* Enable an optimization step */
510 {
511     EnableOpt (Arg);
512 }
513
514
515
516 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
517                                const char* Arg attribute ((unused)))
518 /* Forget all currently defined include paths */
519 {
520     ForgetAllIncludePaths ();
521 }
522
523
524
525 static void OptHelp (const char* Opt attribute ((unused)),
526                      const char* Arg attribute ((unused)))
527 /* Print usage information and exit */
528 {
529     Usage ();
530     exit (EXIT_SUCCESS);
531 }
532
533
534
535 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
536 /* Add an include search path */
537 {
538     AddIncludePath (Arg, INC_SYS | INC_USER);
539 }
540
541
542
543 static void OptListOptSteps (const char* Opt attribute ((unused)),
544                              const char* Arg attribute ((unused)))
545 /* List all optimizer steps */
546 {
547     /* List the optimizer steps */
548     ListOptSteps (stdout);
549
550     /* Terminate */
551     exit (EXIT_SUCCESS);
552 }
553
554
555
556 static void OptMemoryModel (const char* Opt, const char* Arg)
557 /* Set the memory model */
558 {
559     mmodel_t M;
560
561     /* Check the current memory model */
562     if (MemoryModel != MMODEL_UNKNOWN) {
563         AbEnd ("Cannot use option `%s' twice", Opt);
564     }
565
566     /* Translate the memory model name and check it */
567     M = FindMemoryModel (Arg);
568     if (M == MMODEL_UNKNOWN) {
569         AbEnd ("Unknown memory model: %s", Arg);
570     } else if (M == MMODEL_HUGE) {
571         AbEnd ("Unsupported memory model: %s", Arg);
572     }
573
574     /* Set the memory model */
575     SetMemoryModel (M);
576 }
577
578
579
580 static void OptRegisterSpace (const char* Opt, const char* Arg)
581 /* Handle the --register-space option */
582 {
583     /* Numeric argument expected */
584     if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
585         AbEnd ("Argument for option %s is invalid", Opt);
586     }
587 }
588
589
590
591 static void OptRegisterVars (const char* Opt attribute ((unused)),
592                              const char* Arg attribute ((unused)))
593 /* Handle the --register-vars option */
594 {
595     IS_Set (&EnableRegVars, 1);
596 }
597
598
599
600 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
601 /* Handle the --rodata-name option */
602 {
603     /* Check for a valid name */
604     CheckSegName (Arg);
605
606     /* Set the name */
607     SetSegName (SEG_RODATA, Arg);
608 }
609
610
611
612 static void OptSignedChars (const char* Opt attribute ((unused)),
613                             const char* Arg attribute ((unused)))
614 /* Make default characters signed */
615 {
616     IS_Set (&SignedChars, 1);
617 }
618
619
620
621 static void OptStandard (const char* Opt, const char* Arg)
622 /* Handle the --standard option */
623 {
624     /* Find the standard from the given name */
625     standard_t Std = FindStandard (Arg);
626     if (Std == STD_UNKNOWN) {
627         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
628     } else if (IS_Get (&Standard) != STD_UNKNOWN) {
629         AbEnd ("Option %s given more than once", Opt);
630     } else {
631         IS_Set (&Standard, Std);
632     }
633 }
634
635
636
637 static void OptStaticLocals (const char* Opt attribute ((unused)),
638                              const char* Arg attribute ((unused)))
639 /* Place local variables in static storage */
640 {
641     IS_Set (&StaticLocals, 1);
642 }
643
644
645
646 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
647 /* Set the target system */
648 {
649     SetSys (Arg);
650 }
651
652
653
654 static void OptVerbose (const char* Opt attribute ((unused)),
655                         const char* Arg attribute ((unused)))
656 /* Increase verbosity */
657 {
658     ++Verbosity;
659 }
660
661
662
663 static void OptVersion (const char* Opt attribute ((unused)),
664                         const char* Arg attribute ((unused)))
665 /* Print the assembler version */
666 {
667     fprintf (stderr,
668              "cc65 V%u.%u.%u\n",
669              VER_MAJOR, VER_MINOR, VER_PATCH);
670 }
671
672
673
674 static void OptWritableStrings (const char* Opt attribute ((unused)),
675                                 const char* Arg attribute ((unused)))
676 /* Make string literals writable */
677 {
678     IS_Set (&WritableStrings, 1);
679 }
680
681
682
683 int main (int argc, char* argv[])
684 {
685     /* Program long options */
686     static const LongOpt OptTab[] = {
687         { "--add-source",       0,      OptAddSource            },
688         { "--bss-name",         1,      OptBssName              },
689         { "--check-stack",      0,      OptCheckStack           },
690         { "--code-name",        1,      OptCodeName             },
691         { "--codesize",         1,      OptCodeSize             },
692         { "--cpu",              1,      OptCPU                  },
693         { "--create-dep",       0,      OptCreateDep            },
694         { "--data-name",        1,      OptDataName             },
695         { "--debug",            0,      OptDebug                },
696         { "--debug-info",       0,      OptDebugInfo            },
697         { "--debug-opt",        1,      OptDebugOpt             },
698         { "--disable-opt",      1,      OptDisableOpt           },
699         { "--enable-opt",       1,      OptEnableOpt            },
700         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
701         { "--help",             0,      OptHelp                 },
702         { "--include-dir",      1,      OptIncludeDir           },
703         { "--list-opt-steps",   0,      OptListOptSteps         },
704         { "--memory-model",     1,      OptMemoryModel          },
705         { "--register-space",   1,      OptRegisterSpace        },
706         { "--register-vars",    0,      OptRegisterVars         },
707         { "--rodata-name",      1,      OptRodataName           },
708         { "--signed-chars",     0,      OptSignedChars          },
709         { "--standard",         1,      OptStandard             },
710         { "--static-locals",    0,      OptStaticLocals         },
711         { "--target",           1,      OptTarget               },
712         { "--verbose",          0,      OptVerbose              },
713         { "--version",          0,      OptVersion              },
714         { "--writable-strings", 0,      OptWritableStrings      },
715     };
716
717     unsigned I;
718
719     /* Initialize the output file name */
720     const char* OutputFile = 0;
721     const char* InputFile  = 0;
722
723     /* Initialize the cmdline module */
724     InitCmdLine (&argc, &argv, "cc65");
725
726     /* Initialize the default segment names */
727     InitSegNames ();
728
729     /* Initialize the include search paths */
730     InitIncludePaths ();
731
732     /* Parse the command line */
733     I = 1;
734     while (I < ArgCount) {
735
736         const char* P;
737
738         /* Get the argument */
739         const char* Arg = ArgVec[I];
740
741         /* Check for an option */
742         if (Arg [0] == '-') {
743
744             switch (Arg [1]) {
745
746                 case '-':
747                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
748                     break;
749
750                 case 'd':
751                     OptDebug (Arg, 0);
752                     break;
753
754                 case 'h':
755                 case '?':
756                     OptHelp (Arg, 0);
757                     break;
758
759                 case 'g':
760                     OptDebugInfo (Arg, 0);
761                     break;
762
763                 case 'j':
764                     OptSignedChars (Arg, 0);
765                     break;
766
767                 case 'o':
768                     OutputFile = GetArg (&I, 2);
769                     break;
770
771                 case 'r':
772                     OptRegisterVars (Arg, 0);
773                     break;
774
775                 case 't':
776                     OptTarget (Arg, GetArg (&I, 2));
777                     break;
778
779                 case 'u':
780                     OptCreateDep (Arg, 0);
781                     break;
782
783                 case 'v':
784                     OptVerbose (Arg, 0);
785                     break;
786
787                 case 'C':
788                     P = Arg + 2;
789                     while (*P) {
790                         switch (*P++) {
791                             case 'l':
792                                 OptStaticLocals (Arg, 0);
793                                 break;
794                             default:
795                                 UnknownOption (Arg);
796                                 break;
797                         }
798                     }
799                     break;
800
801                 case 'D':
802                     DefineSym (GetArg (&I, 2));
803                     break;
804
805                 case 'I':
806                     OptIncludeDir (Arg, GetArg (&I, 2));
807                     break;
808
809                 case 'O':
810                     IS_Set (&Optimize, 1);
811                     P = Arg + 2;
812                     while (*P) {
813                         switch (*P++) {
814                             case 'i':
815                                 IS_Set (&CodeSizeFactor, 200);
816                                 break;
817                             case 'r':
818                                 IS_Set (&EnableRegVars, 1);
819                                 break;
820                             case 's':
821                                 IS_Set (&InlineStdFuncs, 1);
822                                 break;
823                         }
824                     }
825                     break;
826
827                 case 'T':
828                     OptAddSource (Arg, 0);
829                     break;
830
831                 case 'V':
832                     OptVersion (Arg, 0);
833                     break;
834
835                 case 'W':
836                     IS_Set (&WarnDisable, 1);
837                     break;
838
839                 default:
840                     UnknownOption (Arg);
841                     break;
842             }
843         } else {
844             if (InputFile) {
845                 fprintf (stderr, "additional file specs ignored\n");
846             } else {
847                 InputFile = Arg;
848             }
849         }
850
851         /* Next argument */
852         ++I;
853     }
854
855     /* Did we have a file spec on the command line? */
856     if (InputFile == 0) {
857         AbEnd ("No input files");
858     }
859
860     /* Create the output file name if it was not explicitly given */
861     if (OutputFile == 0) {
862         OutputFile = MakeFilename (InputFile, ".s");
863     }
864
865     /* If no CPU given, use the default CPU for the target */
866     if (CPU == CPU_UNKNOWN) {
867         if (Target != TGT_UNKNOWN) {
868             CPU = DefaultCPU[Target];
869         } else {
870             CPU = CPU_6502;
871         }
872     }
873
874     /* If no memory model was given, use the default */
875     if (MemoryModel == MMODEL_UNKNOWN) {
876         SetMemoryModel (MMODEL_NEAR);
877     }
878
879     /* If no language standard was given, use the default one */
880     if (IS_Get (&Standard) == STD_UNKNOWN) {
881         IS_Set (&Standard, STD_DEFAULT);
882     }
883
884     /* Go! */
885     Compile (InputFile);
886
887     /* Create the output file if we didn't had any errors */
888     if (ErrorCount == 0 || Debug) {
889
890         /* Open the file */
891         FILE* F = fopen (OutputFile, "w");
892         if (F == 0) {
893             Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
894         }
895         Print (stdout, 1, "Opened output file `%s'\n", OutputFile);
896
897         /* Write the output to the file */
898         WriteOutput (F);
899         Print (stdout, 1, "Wrote output to `%s'\n", OutputFile);
900
901         /* Close the file, check for errors */
902         if (fclose (F) != 0) {
903             remove (OutputFile);
904             Fatal ("Cannot write to output file (disk full?)");
905         }
906         Print (stdout, 1, "Closed output file `%s'\n", OutputFile);
907
908         /* Create dependencies if requested */
909         if (CreateDep) {
910             DoCreateDep (OutputFile);
911             Print (stdout, 1, "Creating dependeny file");
912         }
913
914     }
915
916     /* Return an apropriate exit code */
917     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
918 }
919
920
921