]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
134ae7e8f66db24122392fa282e33fa132349a45
[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 OptAnsi (const char* Opt, const char* Arg)
264 /* Compile in strict ANSI mode */
265 {
266     ANSI = 1;
267 }
268
269
270
271 static void OptCPU (const char* Opt, const char* Arg)
272 /* Handle the --cpu option */
273 {
274     if (Arg == 0) {
275         NeedArg (Opt);
276     }
277     if (strcmp (Arg, "6502") == 0) {
278         CPU = CPU_6502;
279     } else if (strcmp (Arg, "65C02") == 0) {
280         CPU = CPU_65C02;
281     } else {
282         fprintf (stderr, "Invalid CPU: `%s'\n", Arg);
283         exit (EXIT_FAILURE);
284     }
285 }
286
287
288
289 static void OptDebugInfo (const char* Opt, const char* Arg)
290 /* Add debug info to the object file */
291 {
292     DebugInfo = 1;
293 }
294
295
296
297 static void OptHelp (const char* Opt, const char* Arg)
298 /* Print usage information and exit */
299 {
300     Usage ();
301     exit (EXIT_SUCCESS);
302 }
303
304
305
306 static void OptIncludeDir (const char* Opt, const char* Arg)
307 /* Add an include search path */
308 {
309     if (Arg == 0) {
310         NeedArg (Opt);
311     }
312     AddIncludePath (Arg, INC_SYS | INC_USER);
313 }
314
315
316
317 static void OptSignedChars (const char* Opt, const char* Arg)
318 /* Make default characters signed */
319 {
320     SignedChars = 1;
321 }
322
323
324
325 static void OptTarget (const char* Opt, const char* Arg)
326 /* Set the target system */
327 {
328     if (Arg == 0) {
329         NeedArg (Opt);
330     }
331     SetSys (Arg);
332 }
333
334
335
336 static void OptVerbose (const char* Opt, const char* Arg)
337 /* Increase verbosity */
338 {
339     ++Verbose;
340 }
341
342
343
344 static void OptVersion (const char* Opt, const char* Arg)
345 /* Print the assembler version */
346 {
347     fprintf (stderr,
348              "cc65 V%u.%u.%u\n",
349              VER_MAJOR, VER_MINOR, VER_PATCH);
350 }
351
352
353
354 int main (int argc, char* argv[])
355 {
356     /* Program long options */
357     static const LongOpt OptTab[] = {
358         { "--ansi",             0,      OptAnsi                 },
359         { "--cpu",              1,      OptCPU                  },
360         { "--debug-info",       0,      OptDebugInfo            },
361         { "--help",             0,      OptHelp                 },
362         { "--include-dir",      1,      OptIncludeDir           },
363         { "--signed-chars",     0,      OptSignedChars          },
364         { "--target",           1,      OptTarget               },
365         { "--verbose",          0,      OptVerbose              },
366         { "--version",          0,      OptVersion              },
367     };
368
369     int I;
370
371     /* Initialize the output file name */
372     const char* OutputFile = 0;
373
374     fin = NULL;
375
376     /* Initialize the cmdline module */
377     InitCmdLine (argc, argv, "cc65");
378
379     /* Parse the command line */
380     I = 1;
381     while (I < argc) {
382
383         const char* P;
384
385         /* Get the argument */
386         const char* Arg = argv [I];
387
388         /* Check for an option */
389         if (Arg [0] == '-') {
390
391             switch (Arg [1]) {
392
393                 case '-':
394                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
395                     break;
396
397                 case 'd':       /* debug mode */
398                     Debug = 1;
399                     break;
400
401                 case 'h':
402                 case '?':
403                     OptHelp (Arg, 0);
404                     break;
405
406                 case 'g':
407                     OptDebugInfo (Arg, 0);
408                     break;
409
410                 case 'j':
411                     OptSignedChars (Arg, 0);
412                     break;
413
414                 case 'o':
415                     OutputFile = GetArg (&I, 2);
416                     break;
417
418                 case 't':
419                     OptTarget (Arg, GetArg (&I, 2));
420                     break;
421
422                 case 'v':
423                     OptVerbose (Arg, 0);
424                     break;
425
426                 case 'A':
427                     OptAnsi (Arg, 0);
428                     break;
429
430                 case 'C':
431                     P = Arg + 2;
432                     while (*P) {
433                         switch (*P++) {
434                             case 'l':
435                                 LocalsAreStatic = 1;
436                                 break;
437                         }
438                     }
439                     break;
440
441                 case 'D':
442                     DefineSym (GetArg (&I, 2));
443                     break;
444
445                 case 'I':
446                     OptIncludeDir (Arg, GetArg (&I, 2));
447                     break;
448
449                 case 'O':
450                     Optimize = 1;
451                     P = Arg + 2;
452                     while (*P) {
453                         switch (*P++) {
454                             case 'f':
455                                 sscanf (P, "%lx", (long*) &OptDisable);
456                                 break;
457                             case 'i':
458                                 FavourSize = 0;
459                                 break;
460                             case 'r':
461                                 EnableRegVars = 1;
462                                 break;
463                             case 's':
464                                 InlineStdFuncs = 1;
465                                 break;
466                         }
467                     }
468                     break;
469
470                 case 'T':
471                     IncSource = 1;
472                     break;
473
474                 case 'V':
475                     OptVersion (Arg, 0);
476                     break;
477
478                 case 'W':
479                     NoWarn = 1;
480                     break;
481
482                 default:
483                     UnknownOption (Arg);
484                     break;
485             }
486         } else {
487             if (fin) {
488                 fprintf (stderr, "additional file specs ignored\n");
489             } else {
490                 fin = xstrdup (Arg);
491                 inp = fopen (fin, "r");
492                 if (inp == 0) {
493                     Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
494                 }
495             }
496         }
497
498         /* Next argument */
499         ++I;
500     }
501
502     /* Did we have a file spec on the command line? */
503     if (!fin) {
504         fprintf (stderr, "%s: No input files\n", argv [0]);
505         exit (EXIT_FAILURE);
506     }
507
508     /* Create the output file name if it was not explicitly given */
509     if (OutputFile == 0) {
510         OutputFile = MakeFilename (fin, ".s");
511     }
512
513     /* Go! */
514     Compile ();
515
516     /* Create the output file if we didn't had any errors */
517     if (ErrorCount == 0 || Debug) {
518
519         FILE* F;
520
521         /* Optimize the output if requested */
522         if (Optimize) {
523             OptDoOpt ();
524         }
525
526         /* Open the file */
527         F = fopen (OutputFile, "w");
528         if (F == 0) {
529             Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
530         }
531
532         /* Write the output to the file */
533         WriteOutput (F);
534
535         /* Close the file, check for errors */
536         if (fclose (F) != 0) {
537             remove (OutputFile);
538             Fatal (FAT_CANNOT_WRITE_OUTPUT);
539         }
540     }
541
542     /* Return an apropriate exit code */
543     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
544 }
545
546
547