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