+#include "libiberty.h"
+
+static const int v850_cacheop_codes[] =
+{
+ 0x00, 0x20, 0x40, 0x60, 0x61, 0x04, 0x06,
+ 0x07, 0x24, 0x26, 0x27, 0x44, 0x64, 0x65, -1
+};
+
+static const int v850_prefop_codes[] =
+{ 0x00, 0x04, -1};
+
+static void
+print_value (int flags,
+ bfd_vma memaddr,
+ struct disassemble_info *info,
+ long value)
+{
+ if (flags & V850_PCREL)
+ {
+ bfd_vma addr = value + memaddr;
+
+ if (flags & V850_INVERSE_PCREL)
+ addr = memaddr - value;
+ info->print_address_func (addr, info);
+ }
+ else if (flags & V850_OPERAND_DISP)
+ {
+ if (flags & V850_OPERAND_SIGNED)
+ {
+ info->fprintf_func (info->stream, "%ld", value);
+ }
+ else
+ {
+ info->fprintf_func (info->stream, "%lu", value);
+ }
+ }
+ else if ((flags & V850E_IMMEDIATE32)
+ || (flags & V850E_IMMEDIATE16HI))
+ {
+ info->fprintf_func (info->stream, "0x%lx", value);
+ }
+ else
+ {
+ if (flags & V850_OPERAND_SIGNED)
+ {
+ info->fprintf_func (info->stream, "%ld", value);
+ }
+ else
+ {
+ info->fprintf_func (info->stream, "%lu", value);
+ }
+ }
+}
+
+static long
+get_operand_value (const struct v850_operand *operand,
+ unsigned long insn,
+ int bytes_read,
+ bfd_vma memaddr,
+ struct disassemble_info * info,
+ bfd_boolean noerror,
+ int *invalid)
+{
+ unsigned long value;
+ bfd_byte buffer[4];
+
+ if ((operand->flags & V850E_IMMEDIATE16)
+ || (operand->flags & V850E_IMMEDIATE16HI))
+ {
+ int status = info->read_memory_func (memaddr + bytes_read, buffer, 2, info);
+
+ if (status == 0)
+ {
+ value = bfd_getl16 (buffer);
+
+ if (operand->flags & V850E_IMMEDIATE16HI)
+ value <<= 16;
+ else if (value & 0x8000)
+ value |= (-1UL << 16);
+
+ return value;
+ }
+
+ if (!noerror)
+ info->memory_error_func (status, memaddr + bytes_read, info);
+
+ return 0;
+ }
+
+ if (operand->flags & V850E_IMMEDIATE23)
+ {
+ int status = info->read_memory_func (memaddr + 2, buffer, 4, info);
+
+ if (status == 0)
+ {
+ value = bfd_getl32 (buffer);
+
+ value = (operand->extract) (value, invalid);
+
+ return value;
+ }
+
+ if (!noerror)
+ info->memory_error_func (status, memaddr + bytes_read, info);
+
+ return 0;
+ }
+
+ if (operand->flags & V850E_IMMEDIATE32)
+ {
+ int status = info->read_memory_func (memaddr + bytes_read, buffer, 4, info);
+
+ if (status == 0)
+ {
+ bytes_read += 4;
+ value = bfd_getl32 (buffer);
+
+ return value;
+ }
+
+ if (!noerror)
+ info->memory_error_func (status, memaddr + bytes_read, info);
+
+ return 0;
+ }
+
+ if (operand->extract)
+ value = (operand->extract) (insn, invalid);
+ else
+ {
+ if (operand->bits == -1)
+ value = (insn & operand->shift);
+ else
+ value = (insn >> operand->shift) & ((1ul << operand->bits) - 1);
+
+ if (operand->flags & V850_OPERAND_SIGNED)
+ {
+ unsigned long sign = 1ul << (operand->bits - 1);
+ value = (value ^ sign) - sign;
+ }
+ }
+
+ return value;
+}
+
+static const char *
+get_v850_sreg_name (unsigned int reg)
+{
+ static const char *const v850_sreg_names[] =
+ {
+ "eipc/vip/mpm", "eipsw/mpc", "fepc/tid", "fepsw/ppa", "ecr/vmecr", "psw/vmtid",
+ "sr6/fpsr/vmadr/dcc", "sr7/fpepc/dc0",
+ "sr8/fpst/vpecr/dcv1", "sr9/fpcc/vptid", "sr10/fpcfg/vpadr/spal", "sr11/spau",
+ "sr12/vdecr/ipa0l", "eiic/vdtid/ipa0u", "feic/ipa1l", "dbic/ipa1u",
+ "ctpc/ipa2l", "ctpsw/ipa2u", "dbpc/ipa3l", "dbpsw/ipa3u", "ctbp/dpa0l",
+ "dir/dpa0u", "bpc/dpa0u", "asid/dpa1l",
+ "bpav/dpa1u", "bpam/dpa2l", "bpdv/dpa2u", "bpdm/dpa3l", "eiwr/dpa3u",
+ "fewr", "dbwr", "bsel"
+ };
+
+ if (reg < ARRAY_SIZE (v850_sreg_names))
+ return v850_sreg_names[reg];
+ return _("<invalid s-reg number>");
+}
+
+static const char *
+get_v850_reg_name (unsigned int reg)
+{
+ static const char *const v850_reg_names[] =
+ {
+ "r0", "r1", "r2", "sp", "gp", "r5", "r6", "r7",
+ "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+ "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+ "r24", "r25", "r26", "r27", "r28", "r29", "ep", "lp"
+ };
+
+ if (reg < ARRAY_SIZE (v850_reg_names))
+ return v850_reg_names[reg];
+ return _("<invalid reg number>");
+}
+
+static const char *
+get_v850_vreg_name (unsigned int reg)
+{
+ static const char *const v850_vreg_names[] =
+ {
+ "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7", "vr8", "vr9",
+ "vr10", "vr11", "vr12", "vr13", "vr14", "vr15", "vr16", "vr17", "vr18",
+ "vr19", "vr20", "vr21", "vr22", "vr23", "vr24", "vr25", "vr26", "vr27",
+ "vr28", "vr29", "vr30", "vr31"
+ };
+
+ if (reg < ARRAY_SIZE (v850_vreg_names))
+ return v850_vreg_names[reg];
+ return _("<invalid v-reg number>");
+}
+
+static const char *
+get_v850_cc_name (unsigned int reg)
+{
+ static const char *const v850_cc_names[] =
+ {
+ "v", "c/l", "z", "nh", "s/n", "t", "lt", "le",
+ "nv", "nc/nl", "nz", "h", "ns/p", "sa", "ge", "gt"
+ };