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