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