]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
4bf527f98b970fcdeadd86316f56c97d63d577bd
[cc65] / src / cc65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                             cc65 main program                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 <ctype.h>
40 #include <errno.h>
41
42 /* common */
43 #include "abend.h"
44 #include "cmdline.h"
45 #include "fname.h"
46 #include "version.h"
47 #include "xmalloc.h"
48
49 /* cc65 */
50 #include "asmcode.h"
51 #include "compile.h"
52 #include "cpu.h"
53 #include "error.h"
54 #include "global.h"
55 #include "incpath.h"
56 #include "input.h"
57 #include "macrotab.h"
58 #include "optimize.h"
59 #include "scanner.h"
60 #include "segname.h"
61
62
63
64 /*****************************************************************************/
65 /*                                   data                                    */
66 /*****************************************************************************/
67
68
69
70 /* Names of the target systems sorted by target name */
71 static const char* TargetNames [] = {
72     "none",
73     "atari",
74     "c64",
75     "c128",
76     "ace",
77     "plus4",
78     "cbm610",
79     "pet",
80     "nes",
81     "apple2",
82     "geos",
83 };
84
85
86
87 /*****************************************************************************/
88 /*                                   Code                                    */
89 /*****************************************************************************/
90
91
92
93 static void Usage (void)
94 {
95     fprintf (stderr,
96              "Usage: %s [options] file\n"
97              "Short options:\n"
98              "  -d\t\t\tDebug mode\n"
99              "  -g\t\t\tAdd debug info to object file\n"
100              "  -h\t\t\tHelp (this text)\n"
101              "  -j\t\t\tDefault characters are signed\n"
102              "  -o name\t\tName the output file\n"
103              "  -t sys\t\tSet the target system\n"
104              "  -v\t\t\tIncrease verbosity\n"
105              "  -A\t\t\tStrict ANSI mode\n"
106              "  -Cl\t\t\tMake local variables static\n"
107              "  -Dsym[=defn]\t\tDefine a symbol\n"
108              "  -I dir\t\tSet an include directory search path\n"
109              "  -O\t\t\tOptimize code\n"
110              "  -Oi\t\t\tOptimize code, inline more code\n"
111              "  -Or\t\t\tEnable register variables\n"
112              "  -Os\t\t\tInline some known functions\n"
113              "  -T\t\t\tInclude source as comment\n"
114              "  -V\t\t\tPrint the compiler version number\n"
115              "  -W\t\t\tSuppress warnings\n"
116              "\n"
117              "Long options:\n"
118              "  --ansi\t\tStrict ANSI mode\n"
119              "  --bss-name seg\tSet the name of the BSS segment\n"
120              "  --code-name seg\tSet the name of the CODE segment\n"
121              "  --cpu type\t\tSet cpu type\n"
122              "  --data-name seg\tSet the name of the DATA segment\n"
123              "  --debug\t\tDebug mode\n"
124              "  --debug-info\t\tAdd debug info to object file\n"
125              "  --help\t\tHelp (this text)\n"
126              "  --include-dir dir\tSet an include directory search path\n"
127              "  --rodata-name seg\tSet the name of the RODATA segment\n"
128              "  --signed-chars\tDefault characters are signed\n"
129              "  --static-locals\tMake local variables static\n"
130              "  --target sys\t\tSet the target system\n"
131              "  --verbose\t\tIncrease verbosity\n"
132              "  --version\t\tPrint the compiler version number\n",
133              ProgName);
134 }
135
136
137
138 static void cbmsys (const char* sys)
139 /* Define a CBM system */
140 {
141     AddNumericMacro ("__CBM__", 1);
142     AddNumericMacro (sys, 1);
143 }
144
145
146
147 static int MapSys (const char* Name)
148 /* Map a target name to a system code. Return -1 in case of an error */
149 {
150     unsigned I;
151
152     /* Check for a numeric target */
153     if (isdigit (*Name)) {
154         int Target = atoi (Name);
155         if (Target >= 0 && Target < TGT_COUNT) {
156             return Target;
157         }
158     }
159
160     /* Check for a target string */
161     for (I = 0; I < TGT_COUNT; ++I) {
162         if (strcmp (TargetNames [I], Name) == 0) {
163             return I;
164         }
165     }
166     /* Not found */
167     return -1;
168 }
169
170
171
172 static void SetSys (const char* Sys)
173 /* Define a target system */
174 {
175     switch (Target = MapSys (Sys)) {
176
177         case TGT_NONE:
178             break;
179
180         case TGT_ATARI:
181             AddNumericMacro ("__ATARI__", 1);
182             break;
183
184         case TGT_C64:
185             cbmsys ("__C64__");
186             break;
187
188         case TGT_C128:
189             cbmsys ("__C128__");
190             break;
191
192         case TGT_ACE:
193             cbmsys ("__ACE__");
194             break;
195
196         case TGT_PLUS4:
197             cbmsys ("__PLUS4__");
198             break;
199
200         case TGT_CBM610:
201             cbmsys ("__CBM610__");
202             break;
203
204         case TGT_PET:
205             cbmsys ("__PET__");
206             break;
207
208         case TGT_NES:
209             AddNumericMacro ("__NES__", 1);
210             break;
211
212         case TGT_APPLE2:
213             AddNumericMacro ("__APPLE2__", 1);
214             break;
215
216         case TGT_GEOS:
217             /* Do not handle as a CBM system */
218             AddNumericMacro ("__GEOS__", 1);
219             break;
220
221         default:
222             AbEnd ("Unknown target system type");
223     }
224 }
225
226
227
228 static void DefineSym (const char* Def)
229 /* Define a symbol on the command line */
230 {
231     const char* P = Def;
232
233     /* The symbol must start with a character or underline */
234     if (Def [0] != '_' && !isalpha (Def [0])) {
235         InvDef (Def);
236     }
237
238     /* Check the symbol name */
239     while (isalnum (*P) || *P == '_') {
240         ++P;
241     }
242
243     /* Do we have a value given? */
244     if (*P != '=') {
245         if (*P != '\0') {
246             InvDef (Def);
247         }
248         /* No value given. Define the macro with the value 1 */
249         AddNumericMacro (Def, 1);
250     } else {
251         /* We have a value, P points to the '=' character. Since the argument
252          * is const, create a copy and replace the '=' in the copy by a zero
253          * terminator.
254          */
255         char* Q;
256         unsigned Len = strlen (Def)+1;
257         char* S = xmalloc (Len);
258         memcpy (S, Def, Len);
259         Q = S + (P - Def);
260         *Q++ = '\0';
261
262         /* Define this as a macro */
263         AddTextMacro (S, Q);
264
265         /* Release the allocated memory */
266         xfree (S);
267     }
268 }
269
270
271
272 static void CheckSegName (const char* Seg)
273 /* Abort if the given name is not a valid segment name */
274 {
275     /* Print an error and abort if the name is not ok */
276     if (!ValidSegName (Seg)) {
277         AbEnd ("Segment name `%s' is invalid", Seg);
278     }
279 }
280
281
282
283 static void OptAddSource (const char* Opt, const char* Arg)
284 /* Add source lines as comments in generated assembler file */
285 {
286     AddSource = 1;
287 }
288
289
290
291 static void OptAnsi (const char* Opt, const char* Arg)
292 /* Compile in strict ANSI mode */
293 {
294     ANSI = 1;
295 }
296
297
298
299 static void OptBssName (const char* Opt, const char* Arg)
300 /* Handle the --bss-name option */
301 {
302     /* Must have a segment name */
303     if (Arg == 0) {
304         NeedArg (Opt);
305     }
306
307     /* Check for a valid name */
308     CheckSegName (Arg);
309
310     /* Set the name */
311     NewSegName (SEG_BSS, Arg);
312 }
313
314
315
316 static void OptCodeName (const char* Opt, const char* Arg)
317 /* Handle the --code-name option */
318 {
319     /* Must have a segment name */
320     if (Arg == 0) {
321         NeedArg (Opt);
322     }
323
324     /* Check for a valid name */
325     CheckSegName (Arg);
326
327     /* Set the name */
328     NewSegName (SEG_CODE, Arg);
329 }
330
331
332
333 static void OptCPU (const char* Opt, const char* Arg)
334 /* Handle the --cpu option */
335 {
336     if (Arg == 0) {
337         NeedArg (Opt);
338     }
339     if (strcmp (Arg, "6502") == 0) {
340         CPU = CPU_6502;
341     } else if (strcmp (Arg, "65C02") == 0) {
342         CPU = CPU_65C02;
343     } else {
344         AbEnd ("Invalid CPU: `%s'", Arg);
345     }
346 }
347
348
349
350 static void OptDataName (const char* Opt, const char* Arg)
351 /* Handle the --code-name option */
352 {
353     /* Must have a segment name */
354     if (Arg == 0) {
355         NeedArg (Opt);
356     }
357
358     /* Check for a valid name */
359     CheckSegName (Arg);
360
361     /* Set the name */
362     NewSegName (SEG_DATA, Arg);
363 }
364
365
366
367 static void OptDebug (const char* Opt, const char* Arg)
368 /* Compiler debug mode */
369 {
370     Debug = 1;
371 }
372
373
374
375 static void OptDebugInfo (const char* Opt, const char* Arg)
376 /* Add debug info to the object file */
377 {
378     DebugInfo = 1;
379 }
380
381
382
383 static void OptHelp (const char* Opt, const char* Arg)
384 /* Print usage information and exit */
385 {
386     Usage ();
387     exit (EXIT_SUCCESS);
388 }
389
390
391
392 static void OptIncludeDir (const char* Opt, const char* Arg)
393 /* Add an include search path */
394 {
395     if (Arg == 0) {
396         NeedArg (Opt);
397     }
398     AddIncludePath (Arg, INC_SYS | INC_USER);
399 }
400
401
402
403 static void OptRodataName (const char* Opt, const char* Arg)
404 /* Handle the --rodata-name option */
405 {
406     /* Must have a segment name */
407     if (Arg == 0) {
408         NeedArg (Opt);
409     }
410
411     /* Check for a valid name */
412     CheckSegName (Arg);
413
414     /* Set the name */
415     NewSegName (SEG_RODATA, Arg);
416 }
417
418
419
420 static void OptSignedChars (const char* Opt, const char* Arg)
421 /* Make default characters signed */
422 {
423     SignedChars = 1;
424 }
425
426
427
428 static void OptStaticLocals (const char* Opt, const char* Arg)
429 /* Place local variables in static storage */
430 {
431     StaticLocals = 1;
432 }
433
434
435
436 static void OptTarget (const char* Opt, const char* Arg)
437 /* Set the target system */
438 {
439     if (Arg == 0) {
440         NeedArg (Opt);
441     }
442     SetSys (Arg);
443 }
444
445
446
447 static void OptVerbose (const char* Opt, const char* Arg)
448 /* Increase verbosity */
449 {
450     ++Verbose;
451 }
452
453
454
455 static void OptVersion (const char* Opt, const char* Arg)
456 /* Print the assembler version */
457 {
458     fprintf (stderr,
459              "cc65 V%u.%u.%u\n",
460              VER_MAJOR, VER_MINOR, VER_PATCH);
461 }
462
463
464
465 int main (int argc, char* argv[])
466 {
467     /* Program long options */
468     static const LongOpt OptTab[] = {
469         { "--add-source",       0,      OptAddSource            },
470         { "--ansi",             0,      OptAnsi                 },
471         { "--bss-name",         1,      OptBssName              },
472         { "--code-name",        1,      OptCodeName             },
473         { "--cpu",              1,      OptCPU                  },
474         { "--data-name",        1,      OptDataName             },
475         { "--debug",            0,      OptDebug                },
476         { "--debug-info",       0,      OptDebugInfo            },
477         { "--help",             0,      OptHelp                 },
478         { "--include-dir",      1,      OptIncludeDir           },
479         { "--rodata-name",      1,      OptRodataName           },
480         { "--signed-chars",     0,      OptSignedChars          },
481         { "--static-locals",    0,      OptStaticLocals         },
482         { "--target",           1,      OptTarget               },
483         { "--verbose",          0,      OptVerbose              },
484         { "--version",          0,      OptVersion              },
485     };
486
487     int I;
488
489     /* Initialize the output file name */
490     const char* OutputFile = 0;
491     const char* InputFile  = 0;
492
493     /* Initialize the cmdline module */
494     InitCmdLine (argc, argv, "cc65");
495
496     /* Initialize the default segment names */
497     InitSegNames ();
498
499     /* Parse the command line */
500     I = 1;
501     while (I < argc) {
502
503         const char* P;
504
505         /* Get the argument */
506         const char* Arg = argv [I];
507
508         /* Check for an option */
509         if (Arg [0] == '-') {
510
511             switch (Arg [1]) {
512
513                 case '-':
514                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
515                     break;
516
517                 case 'd':
518                     OptDebug (Arg, 0);
519                     break;
520
521                 case 'h':
522                 case '?':
523                     OptHelp (Arg, 0);
524                     break;
525
526                 case 'g':
527                     OptDebugInfo (Arg, 0);
528                     break;
529
530                 case 'j':
531                     OptSignedChars (Arg, 0);
532                     break;
533
534                 case 'o':
535                     OutputFile = GetArg (&I, 2);
536                     break;
537
538                 case 't':
539                     OptTarget (Arg, GetArg (&I, 2));
540                     break;
541
542                 case 'v':
543                     OptVerbose (Arg, 0);
544                     break;
545
546                 case 'A':
547                     OptAnsi (Arg, 0);
548                     break;
549
550                 case 'C':
551                     P = Arg + 2;
552                     while (*P) {
553                         switch (*P++) {
554                             case 'l':
555                                 OptStaticLocals (Arg, 0);
556                                 break;
557                             default:
558                                 UnknownOption (Arg);
559                                 break;
560                         }
561                     }
562                     break;
563
564                 case 'D':
565                     DefineSym (GetArg (&I, 2));
566                     break;
567
568                 case 'I':
569                     OptIncludeDir (Arg, GetArg (&I, 2));
570                     break;
571
572                 case 'O':
573                     Optimize = 1;
574                     P = Arg + 2;
575                     while (*P) {
576                         switch (*P++) {
577                             case 'f':
578                                 sscanf (P, "%lx", (long*) &OptDisable);
579                                 break;
580                             case 'i':
581                                 FavourSize = 0;
582                                 break;
583                             case 'r':
584                                 EnableRegVars = 1;
585                                 break;
586                             case 's':
587                                 InlineStdFuncs = 1;
588                                 break;
589                         }
590                     }
591                     break;
592
593                 case 'T':
594                     OptAddSource (Arg, 0);
595                     break;
596
597                 case 'V':
598                     OptVersion (Arg, 0);
599                     break;
600
601                 case 'W':
602                     NoWarn = 1;
603                     break;
604
605                 default:
606                     UnknownOption (Arg);
607                     break;
608             }
609         } else {
610             if (InputFile) {
611                 fprintf (stderr, "additional file specs ignored\n");
612             } else {
613                 InputFile = Arg;
614             }
615         }
616
617         /* Next argument */
618         ++I;
619     }
620
621     /* Did we have a file spec on the command line? */
622     if (InputFile == 0) {
623         AbEnd ("No input files");
624     }
625
626     /* Open the input file */
627     OpenMainFile (InputFile);
628
629     /* Create the output file name if it was not explicitly given */
630     if (OutputFile == 0) {
631         OutputFile = MakeFilename (InputFile, ".s");
632     }
633
634     /* Go! */
635     Compile ();
636
637     /* Create the output file if we didn't had any errors */
638     if (ErrorCount == 0 || Debug) {
639
640         FILE* F;
641
642         /* Optimize the output if requested */
643         if (Optimize) {
644             OptDoOpt ();
645         }
646
647         /* Open the file */
648         F = fopen (OutputFile, "w");
649         if (F == 0) {
650             Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
651         }
652
653         /* Write the output to the file */
654         WriteOutput (F);
655
656         /* Close the file, check for errors */
657         if (fclose (F) != 0) {
658             remove (OutputFile);
659             Fatal (FAT_CANNOT_WRITE_OUTPUT);
660         }
661     }
662
663     /* Return an apropriate exit code */
664     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
665 }
666
667
668