]> git.sur5r.net Git - cc65/blob - src/da65/handler.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / da65 / handler.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 handler.c                                 */
4 /*                                                                           */
5 /*               Opcode handler functions for the disassembler               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-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 <stdarg.h>
37
38 /* common */
39 #include "xsprintf.h"
40
41 /* da65 */
42 #include "attrtab.h"
43 #include "code.h"
44 #include "error.h"
45 #include "global.h"
46 #include "handler.h"
47 #include "opctable.h"
48 #include "output.h"
49
50
51
52 /*****************************************************************************/
53 /*                             Helper functions                              */
54 /*****************************************************************************/
55
56
57
58 static void Mnemonic (const char* M)
59 /* Indent and output a mnemonic */
60 {
61     Indent (MIndent);
62     Output ("%s", M);
63 }
64
65
66
67 static void OneLine (const OpcDesc* D, const char* Arg, ...) attribute ((format(printf, 2, 3)));
68 static void OneLine (const OpcDesc* D, const char* Arg, ...)
69 /* Output one line with the given mnemonic and argument */
70 {
71     char Buf [256];
72     va_list ap;
73
74     /* Mnemonic */
75     Mnemonic (D->Mnemo);
76
77     /* Argument */
78     va_start (ap, Arg);
79     xvsprintf (Buf, sizeof (Buf), Arg, ap);
80     va_end (ap);
81     Indent (AIndent);
82     Output (Buf);
83
84     /* Add the code stuff as comment */
85     LineComment (PC, D->Size);
86
87     /* End the line */
88     LineFeed ();
89 }
90
91
92
93 static const char* GetAbsOverride (unsigned Flags, unsigned Addr)
94 /* If the instruction requires an abs override modifier, return the necessary
95  * string, otherwise return the empty string.
96  */
97 {
98     if ((Flags & flAbsOverride) != 0 && Addr < 0x100) {
99         return "a:";
100     } else {
101         return "";
102     }
103 }
104
105
106
107 static const char* GetAddrArg (unsigned Flags, unsigned Addr)
108 /* Return an address argument - a label if we have one, or the address itself */
109 {
110     const char* Label = 0;
111     if (Flags & flUseLabel) {
112         Label = GetLabel (Addr);
113     }
114     if (Label) {
115         return Label;
116     } else {
117         static char Buf [32];
118         if (Addr < 0x100) {
119             xsprintf (Buf, sizeof (Buf), "$%02X", Addr);
120         } else {
121             xsprintf (Buf, sizeof (Buf), "$%04X", Addr);
122         }
123         return Buf;
124     }
125 }
126
127
128
129 static void GenerateLabel (unsigned Flags, unsigned Addr)
130 /* Generate a label in pass one if requested */
131 {
132     if (Pass == 1 && !HaveLabel (Addr)) {
133         if ((Flags & flGenLabel) != 0 ||
134             ((Flags & flUseLabel) != 0 && Addr >= CodeStart && Addr <= CodeEnd)) {
135             AddLabel (Addr, atIntLabel, MakeLabelName (Addr));
136         }
137     }
138 }
139
140
141
142 /*****************************************************************************/
143 /*                                   Code                                    */
144 /*****************************************************************************/
145
146
147
148 void OH_Illegal (const OpcDesc* D attribute ((unused)))
149 {
150     DataByteLine (1);
151 }
152
153
154
155 void OH_Accumulator (const OpcDesc* D)
156 {
157     OneLine (D, "a");
158 }
159
160
161
162 void OH_Implicit (const OpcDesc* D)
163 {
164     Mnemonic (D->Mnemo);
165     LineComment (PC, D->Size);
166     LineFeed ();
167 }
168
169
170
171 void OH_Immidiate (const OpcDesc* D)
172 {
173     OneLine (D, "#$%02X", GetCodeByte (PC+1));
174 }
175
176
177
178 void OH_Direct (const OpcDesc* D)
179 {
180     /* Get the operand */
181     unsigned Addr = GetCodeByte (PC+1);
182
183     /* Generate a label in pass 1 */
184     GenerateLabel (D->Flags, Addr);
185
186     /* Output the line */
187     OneLine (D, "%s", GetAddrArg (D->Flags, Addr));
188 }
189
190
191
192 void OH_DirectX (const OpcDesc* D)
193 {
194     /* Get the operand */
195     unsigned Addr = GetCodeByte (PC+1);
196
197     /* Generate a label in pass 1 */
198     GenerateLabel (D->Flags, Addr);
199
200     /* Output the line */
201     OneLine (D, "%s,x", GetAddrArg (D->Flags, Addr));
202 }
203
204
205
206 void OH_DirectY (const OpcDesc* D)
207 {
208     /* Get the operand */
209     unsigned Addr = GetCodeByte (PC+1);
210
211     /* Generate a label in pass 1 */
212     GenerateLabel (D->Flags, Addr);
213
214     /* Output the line */
215     OneLine (D, "%s,y", GetAddrArg (D->Flags, Addr));
216 }
217
218
219
220 void OH_Absolute (const OpcDesc* D)
221 {
222     /* Get the operand */
223     unsigned Addr = GetCodeWord (PC+1);
224
225     /* Generate a label in pass 1 */
226     GenerateLabel (D->Flags, Addr);
227
228     /* Output the line */
229     OneLine (D, "%s%s", GetAbsOverride (D->Flags, Addr), GetAddrArg (D->Flags, Addr));
230 }
231
232
233
234 void OH_AbsoluteX (const OpcDesc* D)
235 {
236     /* Get the operand */
237     unsigned Addr = GetCodeWord (PC+1);
238
239     /* Generate a label in pass 1 */
240     GenerateLabel (D->Flags, Addr);
241
242     /* Output the line */
243     OneLine (D, "%s%s,x", GetAbsOverride (D->Flags, Addr), GetAddrArg (D->Flags, Addr));
244 }
245
246
247
248 void OH_AbsoluteY (const OpcDesc* D)
249 {
250     /* Get the operand */
251     unsigned Addr = GetCodeWord (PC+1);
252
253     /* Generate a label in pass 1 */
254     GenerateLabel (D->Flags, Addr);
255
256     /* Output the line */
257     OneLine (D, "%s%s,y", GetAbsOverride (D->Flags, Addr), GetAddrArg (D->Flags, Addr));
258 }
259
260
261
262 void OH_AbsoluteLong (const OpcDesc* D attribute ((unused)))
263 {
264     Error ("Not implemented");
265 }
266
267
268
269 void OH_AbsoluteLongX (const OpcDesc* D attribute ((unused)))
270 {
271     Error ("Not implemented");
272 }
273
274
275
276 void OH_Relative (const OpcDesc* D)
277 {
278     /* Get the operand */
279     signed char Offs = GetCodeByte (PC+1);
280
281     /* Calculate the target address */
282     unsigned Addr = (((int) PC+2) + Offs) & 0xFFFF;
283
284     /* Generate a label in pass 1 */
285     GenerateLabel (D->Flags, Addr);
286
287     /* Output the line */
288     OneLine (D, "%s", GetAddrArg (D->Flags, Addr));
289 }
290
291
292
293 void OH_RelativeLong (const OpcDesc* D attribute ((unused)))
294 {
295     Error ("Not implemented");
296 }
297
298
299
300 void OH_DirectIndirect (const OpcDesc* D)
301 {
302     /* Get the operand */
303     unsigned Addr = GetCodeByte (PC+1);
304
305     /* Generate a label in pass 1 */
306     GenerateLabel (D->Flags, Addr);
307
308     /* Output the line */
309     OneLine (D, "(%s)", GetAddrArg (D->Flags, Addr));
310 }
311
312
313
314 void OH_DirectIndirectY (const OpcDesc* D)
315 {
316     /* Get the operand */
317     unsigned Addr = GetCodeByte (PC+1);
318
319     /* Generate a label in pass 1 */
320     GenerateLabel (D->Flags, Addr);
321
322     /* Output the line */
323     OneLine (D, "(%s),y", GetAddrArg (D->Flags, Addr));
324 }
325
326
327
328 void OH_DirectXIndirect (const OpcDesc* D)
329 {
330     /* Get the operand */
331     unsigned Addr = GetCodeByte (PC+1);
332
333     /* Generate a label in pass 1 */
334     GenerateLabel (D->Flags, Addr);
335
336     /* Output the line */
337     OneLine (D, "(%s,x)", GetAddrArg (D->Flags, Addr));
338 }
339
340
341
342 void OH_AbsoluteIndirect (const OpcDesc* D)
343 {
344     /* Get the operand */
345     unsigned Addr = GetCodeWord (PC+1);
346
347     /* Generate a label in pass 1 */
348     GenerateLabel (D->Flags, Addr);
349
350     /* Output the line */
351     OneLine (D, "(%s)", GetAddrArg (D->Flags, Addr));
352 }
353
354
355
356 void OH_BitBranch (const OpcDesc* D)
357 {
358     /* Get the operands */
359     unsigned char TestAddr   = GetCodeByte (PC+1);
360     signed char   BranchOffs = GetCodeByte (PC+2);
361
362     /* Calculate the target address for the branch */
363     unsigned BranchAddr = (((int) PC+3) + BranchOffs) & 0xFFFF;
364
365     /* Generate labels in pass 1. The bit branch codes are special in that
366      * they don't really match the remainder of the 6502 instruction set (they
367      * are a Rockwell addon), so we must pass additional flags as direct
368      * value to the second GenerateLabel call.
369      */
370     GenerateLabel (D->Flags, TestAddr);
371     GenerateLabel (flLabel, BranchAddr);
372
373     /* Output the line */
374     OneLine (D, "%s,%s", GetAddrArg (D->Flags, TestAddr), GetAddrArg (flLabel, BranchAddr));
375 }
376
377
378
379 void OH_StackRelative (const OpcDesc* D attribute ((unused)))
380 {
381     Error ("Not implemented");
382 }
383
384
385
386 void OH_DirectIndirectLongX (const OpcDesc* D attribute ((unused)))
387 {
388     Error ("Not implemented");
389 }
390
391
392
393 void OH_StackRelativeIndirectY (const OpcDesc* D attribute ((unused)))
394 {
395     Error ("Not implemented");
396 }
397
398
399
400 void OH_DirectIndirectLong (const OpcDesc* D attribute ((unused)))
401 {
402     Error ("Not implemented");
403 }
404
405
406
407 void OH_DirectIndirectLongY (const OpcDesc* D attribute ((unused)))
408 {
409     Error ("Not implemented");
410 }
411
412
413
414 void OH_BlockMove (const OpcDesc* D attribute ((unused)))
415 {
416     Error ("Not implemented");
417 }
418
419
420
421 void OH_AbsoluteXIndirect (const OpcDesc* D attribute ((unused)))
422 {
423     Error ("Not implemented");
424 }
425
426
427
428 void OH_Rts (const OpcDesc* D)
429 {
430     OH_Implicit (D);
431     SeparatorLine();
432 }
433
434
435
436 void OH_JmpAbsolute (const OpcDesc* D)
437 {
438     OH_Absolute (D);
439     SeparatorLine ();
440 }
441
442
443
444 void OH_JmpAbsoluteIndirect (const OpcDesc* D)
445 {
446     OH_AbsoluteIndirect (D);
447     SeparatorLine ();
448 }
449
450
451