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