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