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