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