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