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