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