1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/* s12z-dis.c -- Freescale S12Z disassembly
Copyright (C) 2018-2021 Free Software Foundation, Inc.
This file is part of the GNU opcodes library.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include <stdio.h>
#include "bfd_stdint.h"
#include <stdbool.h>
#include <assert.h>
#include "opcode/s12z.h"
#include "bfd.h"
#include "dis-asm.h"
#include "disassemble.h"
#include "s12z-opc.h"
#include "opintl.h"
struct mem_read_abstraction
{
struct mem_read_abstraction_base base;
bfd_vma memaddr;
struct disassemble_info* info;
};
static void
advance (struct mem_read_abstraction_base *b)
{
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
mra->memaddr ++;
}
static bfd_vma
posn (struct mem_read_abstraction_base *b)
{
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
return mra->memaddr;
}
static int
abstract_read_memory (struct mem_read_abstraction_base *b,
int offset,
size_t n, bfd_byte *bytes)
{
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
int status = (*mra->info->read_memory_func) (mra->memaddr + offset,
bytes, n, mra->info);
return status != 0 ? -1 : 0;
}
/* Start of disassembly file. */
const struct reg registers[S12Z_N_REGISTERS] =
{
{"d2", 2},
{"d3", 2},
{"d4", 2},
{"d5", 2},
{"d0", 1},
{"d1", 1},
{"d6", 4},
{"d7", 4},
{"x", 3},
{"y", 3},
{"s", 3},
{"p", 3},
{"cch", 1},
{"ccl", 1},
{"ccw", 2}
};
static const char *mnemonics[] =
{
"!!invalid!!",
"psh",
"pul",
"tbne", "tbeq", "tbpl", "tbmi", "tbgt", "tble",
"dbne", "dbeq", "dbpl", "dbmi", "dbgt", "dble",
"sex",
"exg",
"lsl", "lsr",
"asl", "asr",
"rol", "ror",
"bfins", "bfext",
"trap",
"ld",
"st",
"cmp",
"stop",
"wai",
"sys",
"minu",
"mins",
"maxu",
"maxs",
"abs",
"adc",
"bit",
"sbc",
"rti",
"clb",
"eor",
"sat",
"nop",
"bgnd",
"brclr",
"brset",
"rts",
"lea",
"mov",
"bra",
"bsr",
"bhi",
"bls",
"bcc",
"bcs",
"bne",
"beq",
"bvc",
"bvs",
"bpl",
"bmi",
"bge",
"blt",
"bgt",
"ble",
"inc",
"clr",
"dec",
"add",
"sub",
"and",
"or",
"tfr",
"jmp",
"jsr",
"com",
"andcc",
"neg",
"orcc",
"bclr",
"bset",
"btgl",
"swi",
"mulu",
"divu",
"modu",
"macu",
"qmulu",
"muls",
"divs",
"mods",
"macs",
"qmuls",
NULL
};
static void
operand_separator (struct disassemble_info *info)
{
if ((info->flags & 0x2))
(*info->fprintf_func) (info->stream, ",");
(*info->fprintf_func) (info->stream, " ");
info->flags |= 0x2;
}
/* Render the symbol name whose value is ADDR + BASE or the adddress itself if
there is no symbol. If BASE is non zero, then the a PC relative adddress is
assumend (ie BASE is the value in the PC. */
static void
decode_possible_symbol (bfd_vma addr, bfd_vma base,
struct disassemble_info *info, bool relative)
{
const char *fmt = relative ? "*%+" BFD_VMA_FMT "d" : "%" BFD_VMA_FMT "d";
if (!info->symbol_at_address_func (addr + base, info))
{
(*info->fprintf_func) (info->stream, fmt, addr);
}
else
{
asymbol *sym = NULL;
int j;
for (j = 0; j < info->symtab_size; ++j)
{
sym = info->symtab[j];
if (bfd_asymbol_value (sym) == addr + base)
{
break;
}
}
if (j < info->symtab_size)
(*info->fprintf_func) (info->stream, "%s", bfd_asymbol_name (sym));
else
(*info->fprintf_func) (info->stream, fmt, addr);
}
}
/* Emit the disassembled text for OPR */
static void
opr_emit_disassembly (const struct operand *opr,
struct disassemble_info *info)
{
operand_separator (info);
switch (opr->cl)
{
case OPND_CL_IMMEDIATE:
(*info->fprintf_func) (info->stream, "#%d",
((struct immediate_operand *) opr)->value);
break;
case OPND_CL_REGISTER:
{
int r = ((struct register_operand*) opr)->reg;
if (r < 0 || r >= S12Z_N_REGISTERS)
(*info->fprintf_func) (info->stream, _("<illegal reg num>"));
else
(*info->fprintf_func) (info->stream, "%s", registers[r].name);
}
break;
case OPND_CL_REGISTER_ALL16:
(*info->fprintf_func) (info->stream, "%s", "ALL16b");
break;
case OPND_CL_REGISTER_ALL:
(*info->fprintf_func) (info->stream, "%s", "ALL");
break;
case OPND_CL_BIT_FIELD:
(*info->fprintf_func) (info->stream, "#%d:%d",
((struct bitfield_operand*)opr)->width,
((struct bitfield_operand*)opr)->offset);
break;
case OPND_CL_SIMPLE_MEMORY:
{
struct simple_memory_operand *mo =
(struct simple_memory_operand *) opr;
decode_possible_symbol (mo->addr, mo->base, info, mo->relative);
}
break;
case OPND_CL_MEMORY:
{
int used_reg = 0;
struct memory_operand *mo = (struct memory_operand *) opr;
(*info->fprintf_func) (info->stream, "%c", mo->indirect ? '[' : '(');
const char *fmt;
assert (mo->mutation == OPND_RM_NONE || mo->n_regs == 1);
switch (mo->mutation)
{
case OPND_RM_PRE_DEC:
fmt = "-%s";
break;
case OPND_RM_PRE_INC:
fmt = "+%s";
break;
case OPND_RM_POST_DEC:
fmt = "%s-";
break;
case OPND_RM_POST_INC:
fmt = "%s+";
break;
case OPND_RM_NONE:
default:
if (mo->n_regs < 2)
(*info->fprintf_func) (info->stream, (mo->n_regs == 0) ? "%d" : "%d,", mo->base_offset);
fmt = "%s";
break;
}
if (mo->n_regs > 0)
{
int r = mo->regs[0];
if (r < 0 || r >= S12Z_N_REGISTERS)
(*info->fprintf_func) (info->stream, fmt, _("<illegal reg num>"));
else
(*info->fprintf_func) (info->stream, fmt, registers[r].name);
}
used_reg = 1;
if (mo->n_regs > used_reg)
{
int r = mo->regs[used_reg];
if (r < 0 || r >= S12Z_N_REGISTERS)
(*info->fprintf_func) (info->stream, _("<illegal reg num>"));
else
(*info->fprintf_func) (info->stream, ",%s",
registers[r].name);
}
(*info->fprintf_func) (info->stream, "%c",
mo->indirect ? ']' : ')');
}
break;
};
}
#define S12Z_N_SIZES 4
static const char shift_size_table[S12Z_N_SIZES] =
{
'b', 'w', 'p', 'l'
};
int
print_insn_s12z (bfd_vma memaddr, struct disassemble_info* info)
{
int o;
enum optr operator = OP_INVALID;
int n_operands = 0;
/* The longest instruction in S12Z can have 6 operands.
(Most have 3 or less. Only PSH and PUL have so many. */
struct operand *operands[6];
struct mem_read_abstraction mra;
mra.base.read = (void *) abstract_read_memory ;
mra.base.advance = advance ;
mra.base.posn = posn;
mra.memaddr = memaddr;
mra.info = info;
short osize = -1;
int n_bytes =
decode_s12z (&operator, &osize, &n_operands, operands,
(struct mem_read_abstraction_base *) &mra);
(info->fprintf_func) (info->stream, "%s", mnemonics[(long)operator]);
/* Ship out size sufficies for those instructions which
need them. */
if (osize == -1)
{
bool suffix = false;
for (o = 0; o < n_operands; ++o)
{
if (operands[o] && operands[o]->osize != -1)
{
if (!suffix)
{
(*mra.info->fprintf_func) (mra.info->stream, "%c", '.');
suffix = true;
}
osize = operands[o]->osize;
if (osize < 0 || osize >= S12Z_N_SIZES)
(*mra.info->fprintf_func) (mra.info->stream, _("<bad>"));
else
(*mra.info->fprintf_func) (mra.info->stream, "%c",
shift_size_table[osize]);
}
}
}
else
{
if (osize < 0 || osize >= S12Z_N_SIZES)
(*mra.info->fprintf_func) (mra.info->stream, _(".<bad>"));
else
(*mra.info->fprintf_func) (mra.info->stream, ".%c",
shift_size_table[osize]);
}
/* Ship out the operands. */
for (o = 0; o < n_operands; ++o)
{
if (operands[o])
opr_emit_disassembly (operands[o], mra.info);
free (operands[o]);
}
return n_bytes;
}
|