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