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