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