]> git.sur5r.net Git - cc65/blob - src/da65/main.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / da65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                  Main program for the da65 disassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 /* common */
43 #include "abend.h"
44 #include "cmdline.h"
45 #include "cpu.h"
46 #include "fname.h"
47 #include "print.h"
48 #include "version.h"
49
50 /* da65 */
51 #include "attrtab.h"
52 #include "code.h"
53 #include "data.h"
54 #include "error.h"
55 #include "global.h"
56 #include "infofile.h"
57 #include "opctable.h"
58 #include "output.h"
59 #include "scanner.h"
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static void Usage (void)
70 /* Print usage information and exit */
71 {
72     fprintf (stderr,
73              "Usage: %s [options] [inputfile]\n"
74              "Short options:\n"
75              "  -g\t\t\tAdd debug info to object file\n"
76              "  -h\t\t\tHelp (this text)\n"
77              "  -i name\t\tSpecify an info file\n"
78              "  -o name\t\tName the output file\n"
79              "  -v\t\t\tIncrease verbosity\n"
80              "  -F\t\t\tAdd formfeeds to the output\n"
81              "  -S addr\t\tSet the start/load address\n"
82              "  -V\t\t\tPrint the disassembler version\n"
83              "\n"
84              "Long options:\n"
85              "  --comments n\t\tSet the comment level for the output\n"
86              "  --cpu type\t\tSet cpu type\n"
87              "  --debug-info\t\tAdd debug info to object file\n"
88              "  --formfeeds\t\tAdd formfeeds to the output\n"
89              "  --help\t\tHelp (this text)\n"
90              "  --hexoffs\t\tUse hexadecimal label offsets\n"
91              "  --info name\t\tSpecify an info file\n"
92              "  --pagelength n\tSet the page length for the listing\n"
93              "  --start-addr addr\tSet the start/load address\n"
94              "  --verbose\t\tIncrease verbosity\n"
95              "  --version\t\tPrint the disassembler version\n",
96              ProgName);
97 }
98
99
100
101 static unsigned long CvtNumber (const char* Arg, const char* Number)
102 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
103  * numbers.
104  */
105 {
106     unsigned long Val;
107     int           Converted;
108     char          BoundsCheck;
109
110     /* Convert */
111     if (*Number == '$') {
112         ++Number;
113         Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
114     } else {
115         Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
116     }
117
118     /* Check if we do really have a number */
119     if (Converted != 1) {
120         Error ("Invalid number given in argument: %s\n", Arg);
121     }
122
123     /* Return the result */
124     return Val;
125 }
126
127
128
129 static void OptComments (const char* Opt, const char* Arg)
130 /* Handle the --comments option */
131 {
132     /* Convert the argument to a number */
133     unsigned long Val = CvtNumber (Opt, Arg);
134
135     /* Check for a valid range */
136     if (Val > MAX_COMMENTS) {
137         Error ("Argument for %s outside valid range (%d-%d)",
138                Opt, MIN_COMMENTS, MAX_COMMENTS);
139     }
140
141     /* Use the value */
142     Comments = (unsigned char) Val;
143 }
144
145
146
147 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
148 /* Handle the --cpu option */
149 {
150     /* Find the CPU from the given name */
151     CPU = FindCPU (Arg);
152     SetOpcTable (CPU);
153 }
154
155
156
157 static void OptDebugInfo (const char* Opt attribute ((unused)),
158                           const char* Arg attribute ((unused)))
159 /* Add debug info to the object file */
160 {
161     DebugInfo = 1;
162 }
163
164
165
166 static void OptFormFeeds (const char* Opt attribute ((unused)),
167                           const char* Arg attribute ((unused)))
168 /* Add form feeds to the output */
169 {
170     FormFeeds = 1;
171 }
172
173
174
175 static void OptHelp (const char* Opt attribute ((unused)),
176                      const char* Arg attribute ((unused)))
177 /* Print usage information and exit */
178 {
179     Usage ();
180     exit (EXIT_SUCCESS);
181 }
182
183
184
185 static void OptHexOffs (const char* Opt attribute ((unused)),
186                         const char* Arg attribute ((unused)))
187 /* Handle the --hexoffs option */
188 {
189     UseHexOffs = 1;
190 }
191
192
193
194 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
195 /* Handle the --info option */
196 {
197     InfoSetName (Arg);
198 }
199
200
201
202 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
203 /* Handle the --pagelength option */
204 {
205     int Len = atoi (Arg);
206     if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
207         AbEnd ("Invalid page length: %d", Len);
208     }
209     PageLength = Len;
210 }
211
212
213
214 static void OptStartAddr (const char* Opt, const char* Arg)
215 /* Set the default start address */
216 {
217     StartAddr = CvtNumber (Opt, Arg);
218 }
219
220
221
222 static void OptVerbose (const char* Opt attribute ((unused)),
223                         const char* Arg attribute ((unused)))
224 /* Increase verbosity */
225 {
226     ++Verbosity;
227 }
228
229
230
231 static void OptVersion (const char* Opt attribute ((unused)),
232                         const char* Arg attribute ((unused)))
233 /* Print the disassembler version */
234 {
235     fprintf (stderr,
236              "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
237              VER_MAJOR, VER_MINOR, VER_PATCH);
238 }
239
240
241
242 static void OneOpcode (unsigned RemainingBytes)
243 /* Disassemble one opcode */
244 {
245     /* Get the opcode from the current address */
246     unsigned char OPC = GetCodeByte (PC);
247
248     /* Get the opcode description for the opcode byte */
249     const OpcDesc* D = &OpcTable[OPC];
250
251     /* Get the output style for the current PC */
252     attr_t Style = GetStyleAttr (PC);
253
254     /* If we have a label at this address, output the label, provided that
255      * we aren't in a skip area.
256      */
257     if (Style != atSkip && MustDefLabel (PC)) {
258         DefLabel (GetLabel (PC));
259     }
260
261     /* Check...
262      *   - ...if we have enough bytes remaining for the code at this address.
263      *   - ...if the current instruction is valid for the given CPU.
264      *   - ...if there is no label somewhere between the instruction bytes.
265      * If any of these conditions is false, switch to data mode.
266      */
267     if (Style == atDefault) {
268         if (D->Size > RemainingBytes) {
269             Style = atIllegal;
270             MarkAddr (PC, Style);
271         } else if (D->Flags & flIllegal) {
272            Style = atIllegal;
273             MarkAddr (PC, Style);
274         } else {
275             unsigned I;
276             for (I = 1; I < D->Size; ++I) {
277                 if (HaveLabel (PC+I)) {
278                     Style = atIllegal;
279                     MarkAddr (PC, Style);
280                     break;
281                 }
282             }
283         }
284     }
285
286     /* Disassemble the line */
287     switch (Style) {
288
289         case atDefault:
290         case atCode:
291             D->Handler (D);
292             PC += D->Size;
293             break;
294
295         case atByteTab:
296             ByteTable ();
297             break;
298
299         case atDByteTab:
300             DByteTable ();
301             break;
302
303         case atWordTab:
304             WordTable ();
305             break;
306
307         case atDWordTab:
308             DWordTable ();
309             break;
310
311         case atAddrTab:
312             AddrTable ();
313             break;
314
315         case atRtsTab:
316             RtsTable ();
317             break;
318
319         case atTextTab:
320             TextTable ();
321             break;
322
323         case atSkip:
324             ++PC;
325             break;
326
327         default:
328             DataByteLine (1);
329             ++PC;
330             break;
331
332     }
333 }
334
335
336
337 static void OnePass (void)
338 /* Make one pass through the code */
339 {
340     unsigned Count;
341
342     /* Disassemble until nothing left */
343     while ((Count = GetRemainingBytes()) > 0) {
344         OneOpcode (Count);
345     }
346 }
347
348
349
350 static void Disassemble (void)
351 /* Disassemble the code */
352 {
353     /* Pass 1 */
354     Pass = 1;
355     OnePass ();
356
357     Output ("---------------------------");
358     LineFeed ();
359
360     /* Pass 2 */
361     Pass = 2;
362     ResetCode ();
363     OutputSettings ();
364     DefOutOfRangeLabels ();
365     OnePass ();
366 }
367
368
369
370 int main (int argc, char* argv [])
371 /* Assembler main program */
372 {
373     /* Program long options */
374     static const LongOpt OptTab[] = {
375         { "--comments",         1,      OptComments             },
376         { "--cpu",              1,      OptCPU                  },
377         { "--debug-info",       0,      OptDebugInfo            },
378         { "--formfeeds",        0,      OptFormFeeds            },
379         { "--help",             0,      OptHelp                 },
380         { "--hexoffs",          0,      OptHexOffs              },
381         { "--info",             1,      OptInfo                 },
382         { "--pagelength",       1,      OptPageLength           },
383         { "--start-addr",       1,      OptStartAddr            },
384         { "--verbose",          0,      OptVerbose              },
385         { "--version",          0,      OptVersion              },
386     };
387
388     unsigned I;
389
390     /* Initialize the cmdline module */
391     InitCmdLine (&argc, &argv, "da65");
392
393     /* Check the parameters */
394     I = 1;
395     while (I < ArgCount) {
396
397         /* Get the argument */
398         const char* Arg = ArgVec[I];
399
400         /* Check for an option */
401         if (Arg [0] == '-') {
402             switch (Arg [1]) {
403
404                 case '-':
405                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
406                     break;
407
408                 case 'g':
409                     OptDebugInfo (Arg, 0);
410                     break;
411
412                 case 'h':
413                     OptHelp (Arg, 0);
414                     break;
415
416                 case 'i':
417                     OptInfo (Arg, GetArg (&I, 2));
418                     break;
419
420                 case 'o':
421                     OutFile = GetArg (&I, 2);
422                     break;
423
424                 case 'v':
425                     OptVerbose (Arg, 0);
426                     break;
427
428                 case 'S':
429                     OptStartAddr (Arg, GetArg (&I, 2));
430                     break;
431
432                 case 'V':
433                     OptVersion (Arg, 0);
434                     break;
435
436                 default:
437                     UnknownOption (Arg);
438                     break;
439
440             }
441         } else {
442             /* Filename. Check if we already had one */
443             if (InFile) {
444                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
445                          ProgName, Arg);
446                 exit (EXIT_FAILURE);
447             } else {
448                 InFile = Arg;
449             }
450         }
451
452         /* Next argument */
453         ++I;
454     }
455
456     /* Try to read the info file */
457     ReadInfoFile ();
458
459     /* Must have an input file */
460     if (InFile == 0) {
461         AbEnd ("No input file");
462     }
463
464     /* If no CPU given, use the default CPU */
465     if (CPU == CPU_UNKNOWN) {
466         CPU = CPU_6502;
467     }
468
469     /* Load the input file */
470     LoadCode ();
471
472     /* Open the output file */
473     OpenOutput (OutFile);
474
475     /* Disassemble the code */
476     Disassemble ();
477
478     /* Close the output file */
479     CloseOutput ();
480
481     /* Done */
482     return EXIT_SUCCESS;
483 }
484
485
486