Rizin
unix-like reverse engineering framework and cli tools
v850_disas.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_endian.h>
#include "v850_disas.h"

Go to the source code of this file.

Functions

static int decode_reg_reg (const ut16 instr, struct v850_cmd *cmd)
 
static int decode_imm_reg (const ut16 instr, struct v850_cmd *cmd)
 
static int decode_bcond (const ut16 instr, int len, struct v850_cmd *cmd)
 
static int decode_jarl (const ut8 *instr, int len, struct v850_cmd *cmd)
 
static int decode_3operands (const ut8 *instr, int len, struct v850_cmd *cmd)
 
static int decode_load_store (const ut8 *instr, int len, struct v850_cmd *cmd)
 
static int decode_bit_op (const ut8 *instr, int len, struct v850_cmd *cmd)
 
static int decode_extended (const ut8 *instr, int len, struct v850_cmd *cmd)
 
int v850_decode_command (const ut8 *instr, int len, struct v850_cmd *cmd)
 

Variables

static const char * instrs []
 
static const char * bit_instrs []
 
static const char * ext_instrs1 []
 
static const char * ext_instrs2 []
 
static const char * conds []
 

Function Documentation

◆ decode_3operands()

static int decode_3operands ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 189 of file v850_disas.c.

189  {
190  if (len < 4) {
191  return -1;
192  }
193  ut16 word1 = rz_read_le16(instr);
194  ut16 word2 = rz_read_at_le16(instr, 2);
195  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s", instrs[get_opcode(word1)]);
196  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%x, r%d, r%d",
197  word2, get_reg1(word1), get_reg2(word1));
198  return 4;
199 }
size_t len
Definition: 6502dis.c:15
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
uint16_t ut16
snprintf
Definition: kernel.h:364
#define get_opcode(insn)
Definition: libhppa.h:348
static ut16 rz_read_at_le16(const void *src, size_t offset)
Definition: rz_endian.h:214
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
static const char * instrs[]
Definition: v850_disas.c:10
static ut8 get_reg2(const ut16 instr)
Definition: v850_disas.h:192
#define V850_INSTR_MAXLEN
Definition: v850_disas.h:7
static ut8 get_reg1(const ut16 instr)
Definition: v850_disas.h:188

References cmd, get_opcode, get_reg1(), get_reg2(), instrs, len, rz_read_at_le16(), rz_read_le16(), snprintf, and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_bcond()

static int decode_bcond ( const ut16  instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 154 of file v850_disas.c.

154  {
155 #if 0
156  ut16 disp = ((instr >> 4) & 0x7) | (instr >> 11);
157  disp <<= 1;
158  snprintf (cmd->instr, V850_INSTR_MAXLEN - 1, "b%s", conds[instr & 0xF]);
159  snprintf (cmd->operands, V850_INSTR_MAXLEN - 1, "0x%x", disp);
160 #else
161  ut64 delta = ((((instr >> 4) & 0x7) | ((instr >> 11) << 3)) << 1);
162  if (delta & 0x100) {
163  delta |= 0xFE00;
164  }
165  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "b%s", conds[instr & 0xF]);
166  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%08" PFMT64x, cmd->addr + delta);
167 #endif
168  return 2;
169 }
#define PFMT64x
Definition: rz_types.h:393
static const char * conds[]
Definition: v850_disas.c:85
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References cmd, conds, delta, PFMT64x, snprintf, ut64(), and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_bit_op()

static int decode_bit_op ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 237 of file v850_disas.c.

237  {
238  if (len < 4) {
239  return -1;
240  }
241 
242  ut16 word1 = rz_read_le16(instr);
243  ut16 word2 = rz_read_at_le16(instr, 2);
244  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s", bit_instrs[word1 >> 14]);
245  ut8 reg1 = get_reg1(word1);
246  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "%u, 0x%x[r%d]",
247  (word1 >> 11) & 0x7, word2, reg1);
248  return 4;
249 }
uint8_t ut8
Definition: lh5801.h:11
static const char * bit_instrs[]
Definition: v850_disas.c:60

References bit_instrs, cmd, get_reg1(), len, rz_read_at_le16(), rz_read_le16(), snprintf, and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_extended()

static int decode_extended ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 251 of file v850_disas.c.

251  {
252  if (len < 4) {
253  return -1;
254  }
255 
256  ut16 word1 = rz_read_le16(instr);
257  ut16 word2 = rz_read_at_le16(instr, 2);
258 
259  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s",
260  ext_instrs1[get_subopcode(word1)]);
261 
262  switch (get_subopcode(word1)) {
263  case V850_EXT_SETF:
264  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "%s, r%d",
265  conds[word1 & 0xF], get_reg2(word1));
266  break;
267  case V850_EXT_LDSR:
268  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%d, r%d",
269  get_reg2(word1), get_reg1(word1));
270  break;
271  case V850_EXT_STSR:
272  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%d, r%d",
273  get_reg1(word1), get_reg2(word1));
274  break;
275  case V850_EXT_SHR:
276  case V850_EXT_SAR:
277  case V850_EXT_SHL:
278  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%d, r%d",
279  get_reg1(word1), get_reg2(word2));
280  break;
281  case V850_EXT_TRAP:
282  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%x",
283  get_reg1(word1));
284  break;
285  case V850_EXT_HALT:
286  case V850_EXT_RETI:
287  cmd->operands[0] = '\0';
288  break;
289  case V850_EXT_EXT2:
290  // can be only 0 or 1
291  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s",
292  ext_instrs2[(word2 >> 13) & 1]);
293  break;
294  default:
295  return -1;
296  }
297 
298  return 4;
299 }
static const char * ext_instrs1[]
Definition: v850_disas.c:67
static const char * ext_instrs2[]
Definition: v850_disas.c:80
@ V850_EXT_LDSR
Definition: v850_disas.h:97
@ V850_EXT_SHL
Definition: v850_disas.h:102
@ V850_EXT_SHR
Definition: v850_disas.h:100
@ V850_EXT_SAR
Definition: v850_disas.h:101
@ V850_EXT_EXT2
Definition: v850_disas.h:107
@ V850_EXT_HALT
Definition: v850_disas.h:105
@ V850_EXT_RETI
Definition: v850_disas.h:106
@ V850_EXT_STSR
Definition: v850_disas.h:98
@ V850_EXT_TRAP
Definition: v850_disas.h:104
@ V850_EXT_SETF
Definition: v850_disas.h:96
static ut8 get_subopcode(const ut16 instr)
Definition: v850_disas.h:184

References cmd, conds, ext_instrs1, ext_instrs2, get_reg1(), get_reg2(), get_subopcode(), len, rz_read_at_le16(), rz_read_le16(), snprintf, V850_EXT_EXT2, V850_EXT_HALT, V850_EXT_LDSR, V850_EXT_RETI, V850_EXT_SAR, V850_EXT_SETF, V850_EXT_SHL, V850_EXT_SHR, V850_EXT_STSR, V850_EXT_TRAP, and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_imm_reg()

static int decode_imm_reg ( const ut16  instr,
struct v850_cmd cmd 
)
static

Definition at line 124 of file v850_disas.c.

124  {
125  ut8 opcode = get_opcode(instr);
126  if (opcode >= sizeof(instrs) / sizeof(char *)) {
127  return -1;
128  }
129 
130  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s", instrs[opcode]);
131 
132  st8 immed = get_reg1(instr);
133 
134  if (immed & 0x10) {
135  immed |= 0xE0;
136  }
137 
138  if (immed >= -9 && immed <= 9) {
139  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "%d, r%u",
140  immed, get_reg2(instr));
141  } else {
142  if (immed >= 0) {
143  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1,
144  "0x%x, r%u", immed, get_reg2(instr));
145  } else {
146  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1,
147  "-0x%x, r%u", immed * -1, get_reg2(instr));
148  }
149  }
150 
151  return 2;
152 }
#define st8
Definition: rz_types_base.h:16

References cmd, get_opcode, get_reg1(), get_reg2(), instrs, snprintf, st8, and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_jarl()

static int decode_jarl ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 171 of file v850_disas.c.

171  {
172  if (len < 4) {
173  return -1;
174  }
175 
176  ut16 word1 = rz_read_le16(instr);
177  ut16 word2 = rz_read_at_le16(instr, 2);
178 
179  ut8 reg = get_reg2(word1);
180  ut32 disp = (word2 << 6) | get_reg1(word1);
181 
182  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s", instrs[get_opcode(word1)]);
183  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%08x, r%d",
184  disp << 1, reg);
185 
186  return 4;
187 }
uint32_t ut32
#define reg(n)

References cmd, get_opcode, get_reg1(), get_reg2(), instrs, len, reg, rz_read_at_le16(), rz_read_le16(), snprintf, and V850_INSTR_MAXLEN.

Referenced by v850_decode_command().

◆ decode_load_store()

static int decode_load_store ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)
static

Definition at line 201 of file v850_disas.c.

201  {
202  if (len < 4) {
203  return -1;
204  }
205 
206  ut16 word1 = rz_read_le16(instr);
207  ut16 word2 = rz_read_at_le16(instr, 2);
208 
209  switch (get_opcode(word1)) {
210  case V850_STB:
211  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s.b", instrs[get_opcode(word1)]);
212  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%d, 0x%x[r%d]",
213  get_reg2(word1), word2, get_reg1(word1));
214  break;
215  case V850_LDB:
216  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s.b", instrs[get_opcode(word1)]);
217  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%x[r%d], r%d",
218  get_reg1(word1), word2, get_reg2(word1));
219  break;
220  case V850_LDHW:
221  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s.%c",
222  instrs[get_opcode(word1)], word2 & 1 ? 'w' : 'h');
223  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "0x%x[r%d], r%d",
224  word2 & 0xFFFE, get_reg1(word1), get_reg2(word1));
225  break;
226  case V850_STHW:
227  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s.%c",
228  instrs[get_opcode(word1)], word2 & 1 ? 'w' : 'h');
229  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%d, 0x%x[r%d]",
230  get_reg2(word1), word2 & 0xFFFE, get_reg1(word1));
231  break;
232  }
233 
234  return 4;
235 }
@ V850_STHW
Definition: v850_disas.h:59
@ V850_LDB
Definition: v850_disas.h:56
@ V850_LDHW
Definition: v850_disas.h:57
@ V850_STB
Definition: v850_disas.h:58

References cmd, get_opcode, get_reg1(), get_reg2(), instrs, len, rz_read_at_le16(), rz_read_le16(), snprintf, V850_INSTR_MAXLEN, V850_LDB, V850_LDHW, V850_STB, and V850_STHW.

Referenced by v850_decode_command().

◆ decode_reg_reg()

static int decode_reg_reg ( const ut16  instr,
struct v850_cmd cmd 
)
static

Definition at line 104 of file v850_disas.c.

104  {
105  ut8 opcode = get_opcode(instr);
106 
107  if (opcode >= sizeof(instrs) / sizeof(char *)) {
108  return -1;
109  }
110 
111  snprintf(cmd->instr, V850_INSTR_MAXLEN - 1, "%s", instrs[opcode]);
112 
113  if (opcode == V850_JMP) {
114  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "[r%u]",
115  get_reg1(instr));
116  } else {
117  snprintf(cmd->operands, V850_INSTR_MAXLEN - 1, "r%u, r%u",
118  get_reg1(instr), get_reg2(instr));
119  }
120 
121  return 2;
122 }
@ V850_JMP
Definition: v850_disas.h:17

References cmd, get_opcode, get_reg1(), get_reg2(), instrs, snprintf, V850_INSTR_MAXLEN, and V850_JMP.

Referenced by v850_decode_command().

◆ v850_decode_command()

int v850_decode_command ( const ut8 instr,
int  len,
struct v850_cmd cmd 
)

Definition at line 301 of file v850_disas.c.

301  {
302  int ret;
303 
304  if (len < 2) {
305  return -1;
306  }
307  ut16 in = rz_read_le16(instr);
308 
309  switch (get_opcode(in)) {
310  case V850_MOV:
311  case V850_NOT:
312  case V850_DIVH:
313  case V850_JMP:
314  case V850_SATSUBR:
315  case V850_SATSUB:
316  case V850_SATADD:
317  case V850_MULH:
318  case V850_OR:
319  case V850_XOR:
320  case V850_AND:
321  case V850_TST:
322  case V850_SUBR:
323  case V850_SUB:
324  case V850_ADD:
325  case V850_CMP:
326  ret = decode_reg_reg(in, cmd);
327  break;
328  case V850_MOV_IMM5:
329  case V850_SATADD_IMM5:
330  case V850_ADD_IMM5:
331  case V850_CMP_IMM5:
332  case V850_SHR_IMM5:
333  case V850_SAR_IMM5:
334  case V850_SHL_IMM5:
335  case V850_MULH_IMM5:
336  ret = decode_imm_reg(in, cmd);
337  break;
338  case V850_ADDI:
339  case V850_MOVEA:
340  case V850_MOVHI:
341  case V850_SATSUBI:
342  case V850_ORI:
343  case V850_XORI:
344  case V850_ANDI:
345  case V850_MULHI:
346  ret = decode_3operands(instr, len, cmd);
347  break;
348  case V850_JARL1:
349  case V850_JARL2:
350  ret = decode_jarl(instr, len, cmd);
351  break;
352  case V850_STB:
353  case V850_LDB:
354  case V850_LDHW:
355  case V850_STHW:
356  ret = decode_load_store(instr, len, cmd);
357  break;
358  case V850_BIT_MANIP:
359  ret = decode_bit_op(instr, len, cmd);
360  break;
361  case V850_EXT1:
362  ret = decode_extended(instr, len, cmd);
363  break;
364  default:
365  if ((get_opcode(in) >> 2) == 0xB) {
366  ret = decode_bcond(in, len, cmd);
367  } else {
368  ret = -1;
369  }
370  }
371 
372  return ret;
373 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
static int decode_bit_op(const ut8 *instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:237
static int decode_bcond(const ut16 instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:154
static int decode_reg_reg(const ut16 instr, struct v850_cmd *cmd)
Definition: v850_disas.c:104
static int decode_extended(const ut8 *instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:251
static int decode_load_store(const ut8 *instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:201
static int decode_imm_reg(const ut16 instr, struct v850_cmd *cmd)
Definition: v850_disas.c:124
static int decode_jarl(const ut8 *instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:171
static int decode_3operands(const ut8 *instr, int len, struct v850_cmd *cmd)
Definition: v850_disas.c:189
@ V850_MULH_IMM5
Definition: v850_disas.h:37
@ V850_SAR_IMM5
Definition: v850_disas.h:35
@ V850_BIT_MANIP
Definition: v850_disas.h:62
@ V850_AND
Definition: v850_disas.h:24
@ V850_TST
Definition: v850_disas.h:25
@ V850_ADDI
Definition: v850_disas.h:48
@ V850_SUBR
Definition: v850_disas.h:26
@ V850_ADD
Definition: v850_disas.h:28
@ V850_CMP_IMM5
Definition: v850_disas.h:33
@ V850_EXT1
Definition: v850_disas.h:63
@ V850_XORI
Definition: v850_disas.h:53
@ V850_JARL1
Definition: v850_disas.h:60
@ V850_SHR_IMM5
Definition: v850_disas.h:34
@ V850_OR
Definition: v850_disas.h:22
@ V850_SATSUBI
Definition: v850_disas.h:51
@ V850_SUB
Definition: v850_disas.h:27
@ V850_DIVH
Definition: v850_disas.h:16
@ V850_ORI
Definition: v850_disas.h:52
@ V850_MOV_IMM5
Definition: v850_disas.h:30
@ V850_SATSUBR
Definition: v850_disas.h:18
@ V850_NOT
Definition: v850_disas.h:15
@ V850_MOVEA
Definition: v850_disas.h:49
@ V850_MOV
Definition: v850_disas.h:14
@ V850_MOVHI
Definition: v850_disas.h:50
@ V850_SATADD_IMM5
Definition: v850_disas.h:31
@ V850_SATADD
Definition: v850_disas.h:20
@ V850_MULH
Definition: v850_disas.h:21
@ V850_SHL_IMM5
Definition: v850_disas.h:36
@ V850_ADD_IMM5
Definition: v850_disas.h:32
@ V850_XOR
Definition: v850_disas.h:23
@ V850_MULHI
Definition: v850_disas.h:55
@ V850_CMP
Definition: v850_disas.h:29
@ V850_ANDI
Definition: v850_disas.h:54
@ V850_SATSUB
Definition: v850_disas.h:19
@ V850_JARL2
Definition: v850_disas.h:61

References cmd, decode_3operands(), decode_bcond(), decode_bit_op(), decode_extended(), decode_imm_reg(), decode_jarl(), decode_load_store(), decode_reg_reg(), get_opcode, in, len, rz_read_le16(), V850_ADD, V850_ADD_IMM5, V850_ADDI, V850_AND, V850_ANDI, V850_BIT_MANIP, V850_CMP, V850_CMP_IMM5, V850_DIVH, V850_EXT1, V850_JARL1, V850_JARL2, V850_JMP, V850_LDB, V850_LDHW, V850_MOV, V850_MOV_IMM5, V850_MOVEA, V850_MOVHI, V850_MULH, V850_MULH_IMM5, V850_MULHI, V850_NOT, V850_OR, V850_ORI, V850_SAR_IMM5, V850_SATADD, V850_SATADD_IMM5, V850_SATSUB, V850_SATSUBI, V850_SATSUBR, V850_SHL_IMM5, V850_SHR_IMM5, V850_STB, V850_STHW, V850_SUB, V850_SUBR, V850_TST, V850_XOR, and V850_XORI.

Referenced by disassemble(), and v850_op().

Variable Documentation

◆ bit_instrs

const char* bit_instrs[]
static
Initial value:
= {
[V850_BIT_SET1] = "set1",
[V850_BIT_NOT1] = "not1",
[V850_BIT_CLR1] = "clr1",
[V850_BIT_TST1] = "tst1",
}
@ V850_BIT_SET1
Definition: v850_disas.h:89
@ V850_BIT_NOT1
Definition: v850_disas.h:90
@ V850_BIT_TST1
Definition: v850_disas.h:92
@ V850_BIT_CLR1
Definition: v850_disas.h:91

Definition at line 60 of file v850_disas.c.

Referenced by decode_bit_op().

◆ conds

const char* conds[]
static
Initial value:
= {
[V850_COND_V] = "v",
[V850_COND_CL] = "cl",
[V850_COND_ZE] = "z",
[V850_COND_NH] = "nh",
[V850_COND_N] = "n",
[V850_COND_AL] = "",
[V850_COND_LT] = "lt",
[V850_COND_LE] = "le",
[V850_COND_NV] = "nv",
[V850_COND_NC] = "nc",
[V850_COND_NZ] = "nz",
[V850_COND_H] = "h",
[V850_COND_NS] = "ns",
[V850_COND_SA] = "sa",
[V850_COND_GE] = "ge",
[V850_COND_GT] = "gt",
}
@ V850_COND_H
Definition: v850_disas.h:80
@ V850_COND_NZ
Definition: v850_disas.h:79
@ V850_COND_ZE
Definition: v850_disas.h:69
@ V850_COND_LT
Definition: v850_disas.h:73
@ V850_COND_N
Definition: v850_disas.h:71
@ V850_COND_V
Definition: v850_disas.h:67
@ V850_COND_CL
Definition: v850_disas.h:68
@ V850_COND_GE
Definition: v850_disas.h:84
@ V850_COND_NV
Definition: v850_disas.h:75
@ V850_COND_NS
Definition: v850_disas.h:82
@ V850_COND_GT
Definition: v850_disas.h:85
@ V850_COND_SA
Definition: v850_disas.h:83
@ V850_COND_AL
Definition: v850_disas.h:72
@ V850_COND_LE
Definition: v850_disas.h:74
@ V850_COND_NH
Definition: v850_disas.h:70
@ V850_COND_NC
Definition: v850_disas.h:77

Definition at line 85 of file v850_disas.c.

Referenced by decode_bcond(), and decode_extended().

◆ ext_instrs1

const char* ext_instrs1[]
static
Initial value:
= {
[V850_EXT_SETF] = "setf",
[V850_EXT_LDSR] = "ldsr",
[V850_EXT_STSR] = "stsr",
[V850_EXT_SHR] = "shr",
[V850_EXT_SAR] = "sar",
[V850_EXT_SHL] = "shl",
[V850_EXT_TRAP] = "trap",
[V850_EXT_HALT] = "halt",
[V850_EXT_RETI] = "reti",
[V850_EXT_EXT2] = "ext2",
}

Definition at line 67 of file v850_disas.c.

Referenced by decode_extended().

◆ ext_instrs2

const char* ext_instrs2[]
static
Initial value:
= {
[V850_EXT_DI] = "di",
[V850_EXT_EI] = "ei",
}
@ V850_EXT_EI
Definition: v850_disas.h:112
@ V850_EXT_DI
Definition: v850_disas.h:111

Definition at line 80 of file v850_disas.c.

Referenced by decode_extended().

◆ instrs

const char* instrs[]
static