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