Rizin
unix-like reverse engineering framework and cli tools
asm_or1k.c File Reference
#include <rz_asm.h>
#include <rz_lib.h>
#include "../arch/or1k/or1k_disas.h"

Go to the source code of this file.

Classes

struct  operands
 

Functions

static int insn_to_str (RzAsm *a, char **line, insn_t *descr, insn_extra_t *extra, ut32 insn)
 
static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 

Variables

RzAsmPlugin rz_asm_plugin_or1k
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ disassemble()

static int disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 108 of file asm_or1k.c.

108  {
109  ut32 insn, opcode;
110  ut8 opcode_idx;
111  char *line = NULL;
112  insn_t *insn_descr;
113  insn_extra_t *extra_descr;
114 
115  op->size = -1;
116 
117  if (len < 4) {
118  line = sdb_fmt("invalid");
119  rz_strbuf_set(&op->buf_asm, line);
120  return op->size;
121  }
122 
123  /* read instruction and basic opcode value */
124  insn = rz_read_be32(buf);
125  op->size = 4;
126  opcode = (insn & INSN_OPCODE_MASK);
127  opcode_idx = opcode >> INSN_OPCODE_SHIFT;
128 
129  /* make sure instruction descriptor table is not overflowed */
130  if (opcode_idx >= insns_count) {
131  line = sdb_fmt("invalid");
132  rz_strbuf_set(&op->buf_asm, line);
133  return op->size;
134  }
135 
136  /* if instruction is marked as invalid finish processing now */
137  insn_descr = &or1k_insns[opcode_idx];
138  if (insn_descr->type == INSN_INVAL) {
139  line = sdb_fmt("invalid");
140  rz_strbuf_set(&op->buf_asm, line);
141  return op->size;
142  }
143 
144  /* if name is null, but extra is present, it means 6 most significant bits
145  * are not enough to decode instruction */
146  if (!insn_descr->name && insn_descr->extra) {
147  if ((extra_descr = find_extra_descriptor(insn_descr->extra, insn)) != NULL) {
148  insn_to_str(a, &line, insn_descr, extra_descr, insn);
149  } else {
150  line = "invalid";
151  }
152  rz_strbuf_set(&op->buf_asm, line);
153  } else {
154  /* otherwise basic descriptor is enough */
155  insn_to_str(a, &line, insn_descr, NULL, insn);
156  rz_strbuf_set(&op->buf_asm, line);
157  }
158  return op->size;
159 }
size_t len
Definition: 6502dis.c:15
static int insn_to_str(RzAsm *a, char **line, insn_t *descr, insn_extra_t *extra, ut32 insn)
Definition: asm_or1k.c:20
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
line
Definition: setup.py:34
insn_t or1k_insns[]
Definition: or1k_disas.c:219
size_t insns_count
Definition: or1k_disas.c:286
insn_extra_t * find_extra_descriptor(insn_extra_t *extra_descr, ut32 insn)
Definition: or1k_disas.c:288
@ INSN_INVAL
Definition: or1k_disas.h:54
#define INSN_OPCODE_MASK
Definition: or1k_disas.h:10
#define INSN_OPCODE_SHIFT
Definition: or1k_disas.h:11
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
#define a(i)
Definition: sha256.c:41
insn_extra_t * extra
Definition: or1k_disas.h:118
char * name
Definition: or1k_disas.h:115
int type
Definition: or1k_disas.h:116
Definition: dis.c:32

References a, insn_t::extra, find_extra_descriptor(), INSN_INVAL, INSN_OPCODE_MASK, INSN_OPCODE_SHIFT, insn_to_str(), insns_count, len, setup::line, insn_t::name, NULL, or1k_insns, rz_read_be32(), rz_strbuf_set(), sdb_fmt(), and insn_t::type.

◆ insn_to_str()

static int insn_to_str ( RzAsm a,
char **  line,
insn_t descr,
insn_extra_t extra,
ut32  insn 
)
static

Definition at line 20 of file asm_or1k.c.

20  {
21  struct operands o = { 0 };
22  char *name;
23  insn_type_t type = type_of_opcode(descr, extra);
24  insn_type_descr_t *type_descr = &types[INSN_X];
25 
26  /* only use type descriptor if it has some useful data */
28  type_descr = &types[type];
29  }
30 
31  o.rd = get_operand_value(insn, type_descr, INSN_OPER_D);
32  o.ra = get_operand_value(insn, type_descr, INSN_OPER_A);
33  o.rb = get_operand_value(insn, type_descr, INSN_OPER_B);
34  o.k1 = get_operand_value(insn, type_descr, INSN_OPER_K1);
35  o.k2 = get_operand_value(insn, type_descr, INSN_OPER_K2);
36  o.n = get_operand_value(insn, type_descr, INSN_OPER_N);
37  o.k = get_operand_value(insn, type_descr, INSN_OPER_K);
38  o.i = get_operand_value(insn, type_descr, INSN_OPER_I);
39  o.l = get_operand_value(insn, type_descr, INSN_OPER_L);
40 
41  name = extra ? extra->name : descr->name;
42 
43  if (!name || !type_descr->format) {
44  /* this should not happen, give up */
45  *line = sdb_fmt("invalid");
46  return 4;
47  }
48 
49  switch (type) {
50  case INSN_X:
51  *line = sdb_fmt(type_descr->format, name);
52  break;
53  case INSN_N:
54  *line = sdb_fmt(type_descr->format, name,
55  (sign_extend(o.n, get_operand_mask(type_descr, INSN_OPER_N)) << 2) +
56  a->pc);
57  break;
58  case INSN_K:
59  *line = sdb_fmt(type_descr->format, name, o.k);
60  break;
61  case INSN_DK:
62  *line = sdb_fmt(type_descr->format, name, o.rd, o.k);
63  break;
64  case INSN_DN:
65  *line = sdb_fmt(type_descr->format, name, o.rd, o.n << 13);
66  break;
67  case INSN_B:
68  *line = sdb_fmt(type_descr->format, name, o.rb);
69  break;
70  case INSN_D:
71  *line = sdb_fmt(type_descr->format, name, o.rd);
72  break;
73  case INSN_AI:
74  *line = sdb_fmt(type_descr->format, name, o.ra, o.i);
75  break;
76  case INSN_DAI:
77  *line = sdb_fmt(type_descr->format, name, o.rd, o.ra, o.i);
78  break;
79  case INSN_DAK:
80  *line = sdb_fmt(type_descr->format, name, o.rd, o.ra, o.i);
81  break;
82  case INSN_DAL:
83  *line = sdb_fmt(type_descr->format, name, o.rd, o.ra, o.l);
84  break;
85  case INSN_DA:
86  *line = sdb_fmt(type_descr->format, name, o.rd, o.ra);
87  break;
88  case INSN_DAB:
89  *line = sdb_fmt(type_descr->format, name, o.rd, o.ra, o.rb);
90  break;
91  case INSN_AB:
92  *line = sdb_fmt(type_descr->format, name, o.ra, o.rb);
93  break;
94  case INSN_IABI:
95  *line = sdb_fmt(type_descr->format, name,
96  o.ra, o.rb, (o.k1 << 11) | o.k2);
97  break;
98  case INSN_KABK:
99  *line = sdb_fmt(type_descr->format, name,
100  o.ra, o.rb, (o.k1 << 11) | o.k2);
101  break;
102  default:
103  *line = sdb_fmt("invalid");
104  }
105  return 4;
106 }
static void sign_extend(st32 *value, ut8 bit)
int type
Definition: mipsasm.c:17
const char * name
Definition: op.c:541
insn_type_descr_t types[]
Definition: or1k_disas.c:7
static ut32 get_operand_mask(insn_type_descr_t *type_descr, insn_oper_t operand)
Definition: or1k_disas.h:152
static int is_type_descriptor_defined(insn_type_t type)
Definition: or1k_disas.h:168
static int has_type_descriptor(insn_type_t type)
Definition: or1k_disas.h:164
@ INSN_D
Definition: or1k_disas.h:60
@ INSN_DAB
Definition: or1k_disas.h:73
@ INSN_KABK
Definition: or1k_disas.h:69
@ INSN_DA
Definition: or1k_disas.h:72
@ INSN_X
Definition: or1k_disas.h:55
@ INSN_AI
Definition: or1k_disas.h:62
@ INSN_N
Definition: or1k_disas.h:56
@ INSN_B
Definition: or1k_disas.h:61
@ INSN_DN
Definition: or1k_disas.h:57
@ INSN_DAL
Definition: or1k_disas.h:67
@ INSN_IABI
Definition: or1k_disas.h:75
@ INSN_K
Definition: or1k_disas.h:58
@ INSN_DAI
Definition: or1k_disas.h:63
@ INSN_AB
Definition: or1k_disas.h:71
@ INSN_DK
Definition: or1k_disas.h:59
@ INSN_DAK
Definition: or1k_disas.h:65
static ut32 get_operand_value(ut32 insn, insn_type_descr_t *type_descr, insn_oper_t operand)
Definition: or1k_disas.h:160
@ INSN_OPER_A
Definition: or1k_disas.h:83
@ INSN_OPER_B
Definition: or1k_disas.h:84
@ INSN_OPER_I
Definition: or1k_disas.h:88
@ INSN_OPER_N
Definition: or1k_disas.h:85
@ INSN_OPER_K
Definition: or1k_disas.h:86
@ INSN_OPER_D
Definition: or1k_disas.h:87
@ INSN_OPER_L
Definition: or1k_disas.h:89
@ INSN_OPER_K2
Definition: or1k_disas.h:82
@ INSN_OPER_K1
Definition: or1k_disas.h:81
static insn_type_t type_of_opcode(insn_t *descr, insn_extra_t *extra_descr)
Definition: or1k_disas.h:172
enum insn_type insn_type_t
char * name
Definition: or1k_disas.h:107
Definition: z80asm.h:102

References a, insn_type_descr_t::format, get_operand_mask(), get_operand_value(), has_type_descriptor(), operands::i, INSN_AB, INSN_AI, INSN_B, INSN_D, INSN_DA, INSN_DAB, INSN_DAI, INSN_DAK, INSN_DAL, INSN_DK, INSN_DN, INSN_IABI, INSN_K, INSN_KABK, INSN_N, INSN_OPER_A, INSN_OPER_B, INSN_OPER_D, INSN_OPER_I, INSN_OPER_K, INSN_OPER_K1, INSN_OPER_K2, INSN_OPER_L, INSN_OPER_N, INSN_X, is_type_descriptor_defined(), operands::k, operands::k1, operands::k2, operands::l, setup::line, operands::n, name, insn_extra_t::name, insn_t::name, operands::ra, operands::rb, operands::rd, sdb_fmt(), sign_extend(), type, type_of_opcode(), and types.

Referenced by disassemble().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
}
RzAsmPlugin rz_asm_plugin_or1k
Definition: asm_or1k.c:161
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 172 of file asm_or1k.c.

◆ rz_asm_plugin_or1k

RzAsmPlugin rz_asm_plugin_or1k
Initial value:
= {
.name = "or1k",
.desc = "OpenRISC 1000",
.license = "LGPL3",
.arch = "or1k",
.bits = 32,
.endian = RZ_SYS_ENDIAN_BIG,
.disassemble = &disassemble,
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_or1k.c:108
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527

Definition at line 161 of file asm_or1k.c.