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