]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
Make the -O and --codesize options stackable.
[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 "segments.h"
67
68
69
70 /*****************************************************************************/
71 /*                                   Code                                    */
72 /*****************************************************************************/
73
74
75
76 static void Usage (void)
77 /* Print usage information to stderr */
78 {
79     fprintf (stderr,
80              "Usage: %s [options] file\n"
81              "Short options:\n"
82              "  -A\t\t\tStrict ANSI mode\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              "  --ansi\t\tStrict ANSI mode\n"
106              "  --bss-name seg\tSet the name of the BSS segment\n"
107              "  --check-stack\t\tGenerate stack overflow checks\n"
108              "  --code-name seg\tSet the name of the CODE segment\n"
109              "  --codesize x\t\tAccept larger code by factor x\n"
110              "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
111              "  --create-dep\t\tCreate a make dependency file\n"
112              "  --data-name seg\tSet the name of the DATA segment\n"
113              "  --debug\t\tDebug mode\n"
114              "  --debug-info\t\tAdd debug info to object file\n"
115              "  --debug-opt name\tDebug optimization steps\n"
116              "  --disable-opt name\tDisable an optimization step\n"
117              "  --enable-opt name\tEnable an optimization step\n"
118              "  --forget-inc-paths\tForget include search paths\n"
119              "  --help\t\tHelp (this text)\n"
120              "  --include-dir dir\tSet an include directory search path\n"
121              "  --list-opt-steps\tList all optimizer steps and exit\n"
122              "  --memory-model model\tSet the memory model\n"
123              "  --register-space b\tSet space available for register variables\n"
124              "  --register-vars\tEnable register variables\n"
125              "  --rodata-name seg\tSet the name of the RODATA segment\n"
126              "  --signed-chars\tDefault characters are signed\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 OptAnsi (const char* Opt attribute ((unused)),
333                      const char* Arg attribute ((unused)))
334 /* Compile in strict ANSI mode */
335 {
336     ANSI = 1;
337 }
338
339
340
341 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
342 /* Handle the --bss-name option */
343 {
344     /* Check for a valid name */
345     CheckSegName (Arg);
346
347     /* Set the name */
348     SetSegName (SEG_BSS, Arg);
349 }
350
351
352
353 static void OptCheckStack (const char* Opt attribute ((unused)),
354                            const char* Arg attribute ((unused)))
355 /* Handle the --check-stack option */
356 {
357     IS_Set (&CheckStack, 1);
358 }
359
360
361
362 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
363 /* Handle the --code-name option */
364 {
365     /* Check for a valid name */
366     CheckSegName (Arg);
367
368     /* Set the name */
369     SetSegName (SEG_CODE, Arg);
370 }
371
372
373
374 static void OptCodeSize (const char* Opt, const char* Arg)
375 /* Handle the --codesize option */
376 {
377     unsigned Factor;
378     char     BoundsCheck;
379
380     /* Numeric argument expected */
381     if (sscanf (Arg, "%u%c", &Factor, &BoundsCheck) != 1 ||
382         Factor < 10 || Factor > 1000) {
383         AbEnd ("Argument for %s is invalid", Opt);
384     }
385     IS_Set (&CodeSizeFactor, Factor);
386 }
387
388
389
390 static void OptCreateDep (const char* Opt attribute ((unused)),
391                           const char* Arg attribute ((unused)))
392 /* Handle the --create-dep option */
393 {
394     CreateDep = 1;
395 }
396
397
398
399 static void OptCPU (const char* Opt, const char* Arg)
400 /* Handle the --cpu option */
401 {
402     /* Find the CPU from the given name */
403     CPU = FindCPU (Arg);
404     if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
405         CPU != CPU_65C02 && CPU != CPU_65816) {
406         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
407     }
408 }
409
410
411
412 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
413 /* Handle the --data-name option */
414 {
415     /* Check for a valid name */
416     CheckSegName (Arg);
417
418     /* Set the name */
419     SetSegName (SEG_DATA, Arg);
420 }
421
422
423
424 static void OptDebug (const char* Opt attribute ((unused)),
425                       const char* Arg attribute ((unused)))
426 /* Compiler debug mode */
427 {
428     ++Debug;
429 }
430
431
432
433 static void OptDebugInfo (const char* Opt attribute ((unused)),
434                           const char* Arg attribute ((unused)))
435 /* Add debug info to the object file */
436 {
437     DebugInfo = 1;
438 }
439
440
441
442 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
443 /* Debug optimization steps */
444 {
445     char Buf [128];
446     char* Line;
447
448     /* Open the file */
449     FILE* F = fopen (Arg, "r");
450     if (F == 0) {
451         AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
452     }
453
454     /* Read line by line, ignore empty lines and switch optimization
455      * steps on/off.
456      */
457     while (fgets (Buf, sizeof (Buf), F) != 0) {
458
459         /* Remove trailing control chars. This will also remove the
460          * trailing newline.
461          */
462         unsigned Len = strlen (Buf);
463         while (Len > 0 && IsControl (Buf[Len-1])) {
464             --Len;
465         }
466         Buf[Len] = '\0';
467
468         /* Get a pointer to the buffer and remove leading white space */
469         Line = Buf;
470         while (IsBlank (*Line)) {
471             ++Line;
472         }
473
474         /* Check the first character and enable/disable the step or
475          * ignore the line
476          */
477         switch (*Line) {
478
479             case '\0':
480             case '#':
481             case ';':
482                 /* Empty or comment line */
483                 continue;
484
485             case '-':
486                 DisableOpt (Line+1);
487                 break;
488
489             case '+':
490                 ++Line;
491                 /* FALLTHROUGH */
492
493             default:
494                 EnableOpt (Line);
495                 break;
496
497         }
498
499     }
500
501     /* Close the file, no error check here since we were just reading and
502      * this is only a debug function.
503      */
504     (void) fclose (F);
505 }
506
507
508
509 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
510 /* Disable an optimization step */
511 {
512     DisableOpt (Arg);
513 }
514
515
516
517 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
518 /* Enable an optimization step */
519 {
520     EnableOpt (Arg);
521 }
522
523
524
525 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
526                                const char* Arg attribute ((unused)))
527 /* Forget all currently defined include paths */
528 {
529     ForgetAllIncludePaths ();
530 }
531
532
533
534 static void OptHelp (const char* Opt attribute ((unused)),
535                      const char* Arg attribute ((unused)))
536 /* Print usage information and exit */
537 {
538     Usage ();
539     exit (EXIT_SUCCESS);
540 }
541
542
543
544 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
545 /* Add an include search path */
546 {
547     AddIncludePath (Arg, INC_SYS | INC_USER);
548 }
549
550
551
552 static void OptListOptSteps (const char* Opt attribute ((unused)),
553                              const char* Arg attribute ((unused)))
554 /* List all optimizer steps */
555 {
556     /* List the optimizer steps */
557     ListOptSteps (stdout);
558
559     /* Terminate */
560     exit (EXIT_SUCCESS);
561 }
562
563
564
565 static void OptMemoryModel (const char* Opt, const char* Arg)
566 /* Set the memory model */
567 {
568     mmodel_t M;
569
570     /* Check the current memory model */
571     if (MemoryModel != MMODEL_UNKNOWN) {
572         AbEnd ("Cannot use option `%s' twice", Opt);
573     }
574
575     /* Translate the memory model name and check it */
576     M = FindMemoryModel (Arg);
577     if (M == MMODEL_UNKNOWN) {
578         AbEnd ("Unknown memory model: %s", Arg);
579     } else if (M == MMODEL_HUGE) {
580         AbEnd ("Unsupported memory model: %s", Arg);
581     }
582
583     /* Set the memory model */
584     SetMemoryModel (M);
585 }
586
587
588
589 static void OptRegisterSpace (const char* Opt, const char* Arg)
590 /* Handle the --register-space option */
591 {
592     /* Numeric argument expected */
593     if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
594         AbEnd ("Argument for option %s is invalid", Opt);
595     }
596 }
597
598
599
600 static void OptRegisterVars (const char* Opt attribute ((unused)),
601                              const char* Arg attribute ((unused)))
602 /* Handle the --register-vars option */
603 {
604     IS_Set (&EnableRegVars, 1);
605 }
606
607
608
609 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
610 /* Handle the --rodata-name option */
611 {
612     /* Check for a valid name */
613     CheckSegName (Arg);
614
615     /* Set the name */
616     SetSegName (SEG_RODATA, Arg);
617 }
618
619
620
621 static void OptSignedChars (const char* Opt attribute ((unused)),
622                             const char* Arg attribute ((unused)))
623 /* Make default characters signed */
624 {
625     IS_Set (&SignedChars, 1);
626 }
627
628
629
630 static void OptStaticLocals (const char* Opt attribute ((unused)),
631                              const char* Arg attribute ((unused)))
632 /* Place local variables in static storage */
633 {
634     IS_Set (&StaticLocals, 1);
635 }
636
637
638
639 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
640 /* Set the target system */
641 {
642     SetSys (Arg);
643 }
644
645
646
647 static void OptVerbose (const char* Opt attribute ((unused)),
648                         const char* Arg attribute ((unused)))
649 /* Increase verbosity */
650 {
651     ++Verbosity;
652 }
653
654
655
656 static void OptVersion (const char* Opt attribute ((unused)),
657                         const char* Arg attribute ((unused)))
658 /* Print the assembler version */
659 {
660     fprintf (stderr,
661              "cc65 V%u.%u.%u\n",
662              VER_MAJOR, VER_MINOR, VER_PATCH);
663 }
664
665
666
667 static void OptWritableStrings (const char* Opt attribute ((unused)),
668                                 const char* Arg attribute ((unused)))
669 /* Make string literals writable */
670 {
671     IS_Set (&WritableStrings, 1);
672 }
673
674
675
676 int main (int argc, char* argv[])
677 {
678     /* Program long options */
679     static const LongOpt OptTab[] = {
680         { "--add-source",       0,      OptAddSource            },
681         { "--ansi",             0,      OptAnsi                 },
682         { "--bss-name",         1,      OptBssName              },
683         { "--check-stack",      0,      OptCheckStack           },
684         { "--code-name",        1,      OptCodeName             },
685         { "--codesize",         1,      OptCodeSize             },
686         { "--cpu",              1,      OptCPU                  },
687         { "--create-dep",       0,      OptCreateDep            },
688         { "--data-name",        1,      OptDataName             },
689         { "--debug",            0,      OptDebug                },
690         { "--debug-info",       0,      OptDebugInfo            },
691         { "--debug-opt",        1,      OptDebugOpt             },
692         { "--disable-opt",      1,      OptDisableOpt           },
693         { "--enable-opt",       1,      OptEnableOpt            },
694         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
695         { "--help",             0,      OptHelp                 },
696         { "--include-dir",      1,      OptIncludeDir           },
697         { "--list-opt-steps",   0,      OptListOptSteps         },
698         { "--memory-model",     1,      OptMemoryModel          },
699         { "--register-space",   1,      OptRegisterSpace        },
700         { "--register-vars",    0,      OptRegisterVars         },
701         { "--rodata-name",      1,      OptRodataName           },
702         { "--signed-chars",     0,      OptSignedChars          },
703         { "--static-locals",    0,      OptStaticLocals         },
704         { "--target",           1,      OptTarget               },
705         { "--verbose",          0,      OptVerbose              },
706         { "--version",          0,      OptVersion              },
707         { "--writable-strings", 0,      OptWritableStrings      },
708     };
709
710     unsigned I;
711
712     /* Initialize the output file name */
713     const char* OutputFile = 0;
714     const char* InputFile  = 0;
715
716     /* Initialize the cmdline module */
717     InitCmdLine (&argc, &argv, "cc65");
718
719     /* Initialize the default segment names */
720     InitSegNames ();
721
722     /* Initialize the include search paths */
723     InitIncludePaths ();
724
725     /* Parse the command line */
726     I = 1;
727     while (I < ArgCount) {
728
729         const char* P;
730
731         /* Get the argument */
732         const char* Arg = ArgVec[I];
733
734         /* Check for an option */
735         if (Arg [0] == '-') {
736
737             switch (Arg [1]) {
738
739                 case '-':
740                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
741                     break;
742
743                 case 'd':
744                     OptDebug (Arg, 0);
745                     break;
746
747                 case 'h':
748                 case '?':
749                     OptHelp (Arg, 0);
750                     break;
751
752                 case 'g':
753                     OptDebugInfo (Arg, 0);
754                     break;
755
756                 case 'j':
757                     OptSignedChars (Arg, 0);
758                     break;
759
760                 case 'o':
761                     OutputFile = GetArg (&I, 2);
762                     break;
763
764                 case 'r':
765                     OptRegisterVars (Arg, 0);
766                     break;
767
768                 case 't':
769                     OptTarget (Arg, GetArg (&I, 2));
770                     break;
771
772                 case 'u':
773                     OptCreateDep (Arg, 0);
774                     break;
775
776                 case 'v':
777                     OptVerbose (Arg, 0);
778                     break;
779
780                 case 'A':
781                     OptAnsi (Arg, 0);
782                     break;
783
784                 case 'C':
785                     P = Arg + 2;
786                     while (*P) {
787                         switch (*P++) {
788                             case 'l':
789                                 OptStaticLocals (Arg, 0);
790                                 break;
791                             default:
792                                 UnknownOption (Arg);
793                                 break;
794                         }
795                     }
796                     break;
797
798                 case 'D':
799                     DefineSym (GetArg (&I, 2));
800                     break;
801
802                 case 'I':
803                     OptIncludeDir (Arg, GetArg (&I, 2));
804                     break;
805
806                 case 'O':
807                     IS_Set (&Optimize, 1);
808                     P = Arg + 2;
809                     while (*P) {
810                         switch (*P++) {
811                             case 'f':
812                                 sscanf (P, "%lx", (long*) &OptDisable);
813                                 break;
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                     NoWarn = 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     /* Go! */
880     Compile (InputFile);
881
882     /* Create the output file if we didn't had any errors */
883     if (ErrorCount == 0 || Debug) {
884
885         /* Open the file */
886         FILE* F = fopen (OutputFile, "w");
887         if (F == 0) {
888             Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
889         }
890         Print (stdout, 1, "Opened output file `%s'\n", OutputFile);
891
892         /* Write the output to the file */
893         WriteOutput (F);
894         Print (stdout, 1, "Wrote output to `%s'\n", OutputFile);
895
896         /* Close the file, check for errors */
897         if (fclose (F) != 0) {
898             remove (OutputFile);
899             Fatal ("Cannot write to output file (disk full?)");
900         }
901         Print (stdout, 1, "Closed output file `%s'\n", OutputFile);
902
903         /* Create dependencies if requested */
904         if (CreateDep) {
905             DoCreateDep (OutputFile);
906             Print (stdout, 1, "Creating dependeny file");
907         }
908
909     }
910
911     /* Return an apropriate exit code */
912     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
913 }
914
915
916