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