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