2018-05-18 16:26:18 +02:00
|
|
|
/* s12z-dis.c -- Freescale S12Z disassembly
|
2019-01-01 11:31:27 +01:00
|
|
|
Copyright (C) 2018-2019 Free Software Foundation, Inc.
|
2018-05-18 16:26:18 +02:00
|
|
|
|
|
|
|
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>
|
2018-12-18 09:33:51 +01:00
|
|
|
#include "bfd_stdint.h"
|
2018-05-18 16:26:18 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2018-07-11 10:42:01 +02:00
|
|
|
#include "opcode/s12z.h"
|
2018-05-18 16:26:18 +02:00
|
|
|
#include "bfd.h"
|
|
|
|
#include "dis-asm.h"
|
|
|
|
#include "disassemble.h"
|
2019-01-03 18:30:40 +01:00
|
|
|
#include "s12z-opc.h"
|
2019-10-29 10:17:39 +01:00
|
|
|
#include "opintl.h"
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
struct mem_read_abstraction
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
struct mem_read_abstraction_base base;
|
|
|
|
bfd_vma memaddr;
|
|
|
|
struct disassemble_info* info;
|
2018-05-18 16:26:18 +02:00
|
|
|
};
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
static void
|
|
|
|
advance (struct mem_read_abstraction_base *b)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
|
|
|
|
mra->memaddr ++;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
static bfd_vma
|
|
|
|
posn (struct mem_read_abstraction_base *b)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
|
|
|
|
return mra->memaddr;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-01-03 18:30:40 +01:00
|
|
|
abstract_read_memory (struct mem_read_abstraction_base *b,
|
|
|
|
int offset,
|
|
|
|
size_t n, bfd_byte *bytes)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
int status =
|
|
|
|
(*mra->info->read_memory_func) (mra->memaddr + offset,
|
|
|
|
bytes, n, mra->info);
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
if (status != 0)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
(*mra->info->memory_error_func) (status, mra->memaddr, mra->info);
|
|
|
|
return -1;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
2019-01-03 18:30:40 +01:00
|
|
|
return 0;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
/* Start of disassembly file. */
|
2018-05-18 16:26:18 +02:00
|
|
|
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}
|
|
|
|
};
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
static void
|
|
|
|
operand_separator (struct disassemble_info *info)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
if ((info->flags & 0x2))
|
|
|
|
(*info->fprintf_func) (info->stream, ",");
|
|
|
|
|
|
|
|
(*info->fprintf_func) (info->stream, " ");
|
|
|
|
|
|
|
|
info->flags |= 0x2;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
/* 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. */
|
2018-05-18 16:26:18 +02:00
|
|
|
static void
|
2019-01-03 18:30:40 +01:00
|
|
|
decode_possible_symbol (bfd_vma addr, bfd_vma base,
|
|
|
|
struct disassemble_info *info, bool relative)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
const char *fmt = relative ? "*%+" BFD_VMA_FMT "d" : "%" BFD_VMA_FMT "d";
|
|
|
|
if (!info->symbol_at_address_func (addr + base, info))
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
(*info->fprintf_func) (info->stream, fmt, addr);
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asymbol *sym = NULL;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < info->symtab_size; ++j)
|
|
|
|
{
|
|
|
|
sym = info->symtab[j];
|
2019-01-03 18:30:40 +01:00
|
|
|
if (bfd_asymbol_value (sym) == addr + base)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < info->symtab_size)
|
|
|
|
(*info->fprintf_func) (info->stream, "%s", bfd_asymbol_name (sym));
|
2018-10-22 16:59:47 +02:00
|
|
|
else
|
2019-01-03 18:30:40 +01:00
|
|
|
(*info->fprintf_func) (info->stream, fmt, addr);
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
/* Emit the disassembled text for OPR */
|
2018-05-18 16:26:18 +02:00
|
|
|
static void
|
2019-01-03 18:30:40 +01:00
|
|
|
opr_emit_disassembly (const struct operand *opr,
|
|
|
|
struct disassemble_info *info)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
|
|
|
operand_separator (info);
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
switch (opr->cl)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
case OPND_CL_IMMEDIATE:
|
|
|
|
(*info->fprintf_func) (info->stream, "#%d",
|
|
|
|
((struct immediate_operand *) opr)->value);
|
|
|
|
break;
|
|
|
|
case OPND_CL_REGISTER:
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
int r = ((struct register_operand*) opr)->reg;
|
2019-10-29 10:17:39 +01:00
|
|
|
|
|
|
|
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);
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
2019-01-03 18:30:40 +01:00
|
|
|
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:
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
struct simple_memory_operand *mo =
|
|
|
|
(struct simple_memory_operand *) opr;
|
|
|
|
decode_possible_symbol (mo->addr, mo->base, info, mo->relative);
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
2019-01-03 18:30:40 +01:00
|
|
|
break;
|
|
|
|
case OPND_CL_MEMORY:
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
int used_reg = 0;
|
|
|
|
struct memory_operand *mo = (struct memory_operand *) opr;
|
|
|
|
(*info->fprintf_func) (info->stream, "%c", mo->indirect ? '[' : '(');
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2018-12-31 08:48:10 +01:00
|
|
|
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)
|
2019-10-29 10:17:39 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2018-12-31 08:48:10 +01:00
|
|
|
used_reg = 1;
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
if (mo->n_regs > used_reg)
|
|
|
|
{
|
2019-10-29 10:17:39 +01:00
|
|
|
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);
|
2019-01-03 18:30:40 +01:00
|
|
|
}
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
(*info->fprintf_func) (info->stream, "%c",
|
|
|
|
mo->indirect ? ']' : ')');
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
2019-01-03 18:30:40 +01:00
|
|
|
break;
|
|
|
|
};
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 10:17:39 +01:00
|
|
|
#define S12Z_N_SIZES 4
|
|
|
|
static const char shift_size_table[S12Z_N_SIZES] =
|
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
'b', 'w', 'p', 'l'
|
|
|
|
};
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
int
|
|
|
|
print_insn_s12z (bfd_vma memaddr, struct disassemble_info* info)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
int o;
|
2019-04-12 18:39:01 +02:00
|
|
|
enum optr operator = OP_INVALID;
|
2019-01-03 18:30:40 +01:00
|
|
|
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]);
|
2019-01-04 09:44:58 +01:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
/* Ship out size sufficies for those instructions which
|
|
|
|
need them. */
|
|
|
|
if (osize == -1)
|
|
|
|
{
|
|
|
|
bool suffix = false;
|
2019-10-29 10:17:39 +01:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
for (o = 0; o < n_operands; ++o)
|
|
|
|
{
|
2019-01-04 09:44:58 +01:00
|
|
|
if (operands[o] && operands[o]->osize != -1)
|
2019-01-03 18:30:40 +01:00
|
|
|
{
|
|
|
|
if (!suffix)
|
|
|
|
{
|
|
|
|
(*mra.info->fprintf_func) (mra.info->stream, "%c", '.');
|
|
|
|
suffix = true;
|
|
|
|
}
|
2019-10-29 10:17:39 +01:00
|
|
|
|
|
|
|
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]);
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-29 10:17:39 +01:00
|
|
|
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]);
|
2019-01-03 18:30:40 +01:00
|
|
|
}
|
2018-05-18 16:26:18 +02:00
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
/* Ship out the operands. */
|
|
|
|
for (o = 0; o < n_operands; ++o)
|
2018-05-18 16:26:18 +02:00
|
|
|
{
|
2019-01-03 18:30:40 +01:00
|
|
|
if (operands[o])
|
|
|
|
opr_emit_disassembly (operands[o], mra.info);
|
|
|
|
free (operands[o]);
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 18:30:40 +01:00
|
|
|
return n_bytes;
|
2018-05-18 16:26:18 +02:00
|
|
|
}
|