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