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