]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
bc0d763b13385a95ef8677febb05cc74fdd06264
[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
46 #include "asmcode.h"
47 #include "compile.h"
48 #include "cpu.h"
49 #include "error.h"
50 #include "global.h"
51 #include "incpath.h"
52 #include "io.h"
53 #include "macrotab.h"
54 #include "mem.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              "  --target sys\t\tSet the target system\n"
121              "  --verbose\t\tIncrease verbosity\n"
122              "  --version\t\tPrint the compiler version number\n",
123              ProgName);
124 }
125
126
127
128 static void cbmsys (const char* sys)
129 /* Define a CBM system */
130 {
131     AddNumericMacro ("__CBM__", 1);
132     AddNumericMacro (sys, 1);
133 }
134
135
136
137 static int MapSys (const char* Name)
138 /* Map a target name to a system code. Return -1 in case of an error */
139 {
140     unsigned I;
141
142     /* Check for a numeric target */
143     if (isdigit (*Name)) {
144         int Target = atoi (Name);
145         if (Target >= 0 && Target < TGT_COUNT) {
146             return Target;
147         }
148     }
149
150     /* Check for a target string */
151     for (I = 0; I < TGT_COUNT; ++I) {
152         if (strcmp (TargetNames [I], Name) == 0) {
153             return I;
154         }
155     }
156     /* Not found */
157     return -1;
158 }
159
160
161
162 static void SetSys (const char* Sys)
163 /* Define a target system */
164 {
165     switch (Target = MapSys (Sys)) {
166
167         case TGT_NONE:
168             break;
169
170         case TGT_ATARI:
171             AddNumericMacro ("__ATARI__", 1);
172             break;
173
174         case TGT_C64:
175             cbmsys ("__C64__");
176             break;
177
178         case TGT_C128:
179             cbmsys ("__C128__");
180             break;
181
182         case TGT_ACE:
183             cbmsys ("__ACE__");
184             break;
185
186         case TGT_PLUS4:
187             cbmsys ("__PLUS4__");
188             break;
189
190         case TGT_CBM610:
191             cbmsys ("__CBM610__");
192             break;
193
194         case TGT_PET:
195             cbmsys ("__PET__");
196             break;
197
198         case TGT_NES:
199             AddNumericMacro ("__NES__", 1);
200             break;
201
202         case TGT_APPLE2:
203             AddNumericMacro ("__APPLE2__", 1);
204             break;
205
206         case TGT_GEOS:
207             /* Do not handle as a CBM system */
208             AddNumericMacro ("__GEOS__", 1);
209             break;
210
211         default:
212             fputs ("Unknown system type\n", stderr);
213             exit (EXIT_FAILURE);
214     }
215 }
216
217
218
219 static void DefineSym (const char* Def)
220 /* Define a symbol on the command line */
221 {
222     const char* P = Def;
223
224     /* The symbol must start with a character or underline */
225     if (Def [0] != '_' && !isalpha (Def [0])) {
226         InvDef (Def);
227     }
228
229     /* Check the symbol name */
230     while (isalnum (*P) || *P == '_') {
231         ++P;
232     }
233
234     /* Do we have a value given? */
235     if (*P != '=') {
236         if (*P != '\0') {
237             InvDef (Def);
238         }
239         /* No value given. Define the macro with the value 1 */
240         AddNumericMacro (Def, 1);
241     } else {
242         /* We have a value, P points to the '=' character. Since the argument
243          * is const, create a copy and replace the '=' in the copy by a zero
244          * terminator.
245          */
246         char* Q;
247         unsigned Len = strlen (Def)+1;
248         char* S = xmalloc (Len);
249         memcpy (S, Def, Len);
250         Q = S + (P - Def);
251         *Q++ = '\0';
252
253         /* Define this as a macro */
254         AddTextMacro (S, Q);
255
256         /* Release the allocated memory */
257         xfree (S);
258     }
259 }
260
261
262
263 static void OptAddSource (const char* Opt, const char* Arg)
264 /* Add source lines as comments in generated assembler file */
265 {
266     AddSource = 1;
267 }
268
269
270
271 static void OptAnsi (const char* Opt, const char* Arg)
272 /* Compile in strict ANSI mode */
273 {
274     ANSI = 1;
275 }
276
277
278
279 static void OptCPU (const char* Opt, const char* Arg)
280 /* Handle the --cpu option */
281 {
282     if (Arg == 0) {
283         NeedArg (Opt);
284     }
285     if (strcmp (Arg, "6502") == 0) {
286         CPU = CPU_6502;
287     } else if (strcmp (Arg, "65C02") == 0) {
288         CPU = CPU_65C02;
289     } else {
290         fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
291         exit (EXIT_FAILURE);
292     }
293 }
294
295
296
297 static void OptDebugInfo (const char* Opt, const char* Arg)
298 /* Add debug info to the object file */
299 {
300     DebugInfo = 1;
301 }
302
303
304
305 static void OptHelp (const char* Opt, const char* Arg)
306 /* Print usage information and exit */
307 {
308     Usage ();
309     exit (EXIT_SUCCESS);
310 }
311
312
313
314 static void OptIncludeDir (const char* Opt, const char* Arg)
315 /* Add an include search path */
316 {
317     if (Arg == 0) {
318         NeedArg (Opt);
319     }
320     AddIncludePath (Arg, INC_SYS | INC_USER);
321 }
322
323
324
325 static void OptSignedChars (const char* Opt, const char* Arg)
326 /* Make default characters signed */
327 {
328     SignedChars = 1;
329 }
330
331
332
333 static void OptTarget (const char* Opt, const char* Arg)
334 /* Set the target system */
335 {
336     if (Arg == 0) {
337         NeedArg (Opt);
338     }
339     SetSys (Arg);
340 }
341
342
343
344 static void OptVerbose (const char* Opt, const char* Arg)
345 /* Increase verbosity */
346 {
347     ++Verbose;
348 }
349
350
351
352 static void OptVersion (const char* Opt, const char* Arg)
353 /* Print the assembler version */
354 {
355     fprintf (stderr,
356              "cc65 V%u.%u.%u\n",
357              VER_MAJOR, VER_MINOR, VER_PATCH);
358 }
359
360
361
362 int main (int argc, char* argv[])
363 {
364     /* Program long options */
365     static const LongOpt OptTab[] = {
366         { "--add-source",       0,      OptAddSource            },
367         { "--ansi",             0,      OptAnsi                 },
368         { "--cpu",              1,      OptCPU                  },
369         { "--debug-info",       0,      OptDebugInfo            },
370         { "--help",             0,      OptHelp                 },
371         { "--include-dir",      1,      OptIncludeDir           },
372         { "--signed-chars",     0,      OptSignedChars          },
373         { "--target",           1,      OptTarget               },
374         { "--verbose",          0,      OptVerbose              },
375         { "--version",          0,      OptVersion              },
376     };
377
378     int I;
379
380     /* Initialize the output file name */
381     const char* OutputFile = 0;
382
383     fin = NULL;
384
385     /* Initialize the cmdline module */
386     InitCmdLine (argc, argv, "cc65");
387
388     /* Parse the command line */
389     I = 1;
390     while (I < argc) {
391
392         const char* P;
393
394         /* Get the argument */
395         const char* Arg = argv [I];
396
397         /* Check for an option */
398         if (Arg [0] == '-') {
399
400             switch (Arg [1]) {
401
402                 case '-':
403                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
404                     break;
405
406                 case 'd':       /* debug mode */
407                     Debug = 1;
408                     break;
409
410                 case 'h':
411                 case '?':
412                     OptHelp (Arg, 0);
413                     break;
414
415                 case 'g':
416                     OptDebugInfo (Arg, 0);
417                     break;
418
419                 case 'j':
420                     OptSignedChars (Arg, 0);
421                     break;
422
423                 case 'o':
424                     OutputFile = GetArg (&I, 2);
425                     break;
426
427                 case 't':
428                     OptTarget (Arg, GetArg (&I, 2));
429                     break;
430
431                 case 'v':
432                     OptVerbose (Arg, 0);
433                     break;
434
435                 case 'A':
436                     OptAnsi (Arg, 0);
437                     break;
438
439                 case 'C':
440                     P = Arg + 2;
441                     while (*P) {
442                         switch (*P++) {
443                             case 'l':
444                                 LocalsAreStatic = 1;
445                                 break;
446                         }
447                     }
448                     break;
449
450                 case 'D':
451                     DefineSym (GetArg (&I, 2));
452                     break;
453
454                 case 'I':
455                     OptIncludeDir (Arg, GetArg (&I, 2));
456                     break;
457
458                 case 'O':
459                     Optimize = 1;
460                     P = Arg + 2;
461                     while (*P) {
462                         switch (*P++) {
463                             case 'f':
464                                 sscanf (P, "%lx", (long*) &OptDisable);
465                                 break;
466                             case 'i':
467                                 FavourSize = 0;
468                                 break;
469                             case 'r':
470                                 EnableRegVars = 1;
471                                 break;
472                             case 's':
473                                 InlineStdFuncs = 1;
474                                 break;
475                         }
476                     }
477                     break;
478
479                 case 'T':
480                     OptAddSource (Arg, 0);
481                     break;
482
483                 case 'V':
484                     OptVersion (Arg, 0);
485                     break;
486
487                 case 'W':
488                     NoWarn = 1;
489                     break;
490
491                 default:
492                     UnknownOption (Arg);
493                     break;
494             }
495         } else {
496             if (fin) {
497                 fprintf (stderr, "additional file specs ignored\n");
498             } else {
499                 fin = xstrdup (Arg);
500                 inp = fopen (fin, "r");
501                 if (inp == 0) {
502                     Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
503                 }
504             }
505         }
506
507         /* Next argument */
508         ++I;
509     }
510
511     /* Did we have a file spec on the command line? */
512     if (!fin) {
513         fprintf (stderr, "%s: No input files\n", argv [0]);
514         exit (EXIT_FAILURE);
515     }
516
517     /* Create the output file name if it was not explicitly given */
518     if (OutputFile == 0) {
519         OutputFile = MakeFilename (fin, ".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 (OutputFile, "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 (OutputFile);
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