Rizin
unix-like reverse engineering framework and cli tools
assembly_54.c File Reference
#include "arch_54.h"

Go to the source code of this file.

Functions

static LuaInstruction encode_instruction (ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num)
 
bool lua54_assembly (const char *input, st32 input_size, LuaInstruction *instruction_p)
 

Function Documentation

◆ encode_instruction()

static LuaInstruction encode_instruction ( ut8  opcode,
const char *  arg_start,
ut16  flag,
ut8  arg_num 
)
static

Definition at line 6 of file assembly_54.c.

6  {
8  int args[4];
9  char buffer[64]; // buffer for digits
10  int cur_cnt = 0;
11  int delta_offset;
12 
13  if (arg_num > sizeof(args)) {
14  return -1;
15  }
16 
17  for (int i = 0; i < arg_num; ++i) {
18  delta_offset = lua_load_next_arg_start(arg_start, buffer);
19  if (delta_offset == 0) {
20  return -1;
21  }
24  arg_start += delta_offset;
25  } else {
26  return -1;
27  }
28  }
29 
30  LUA_SET_OPCODE(instruction, opcode);
31  if (has_param_flag(flag, PARAM_A)) {
32  SETARG_A(instruction, args[cur_cnt++]);
33  if (cur_cnt >= arg_num) {
34  return instruction;
35  }
36  }
37  if (has_param_flag(flag, PARAM_B)) {
38  SETARG_B(instruction, args[cur_cnt++]);
39  if (cur_cnt >= arg_num) {
40  return instruction;
41  }
42  }
43  if (has_param_flag(flag, PARAM_sB)) {
44  SETARG_sB(instruction, args[cur_cnt++]);
45  if (cur_cnt >= arg_num) {
46  return instruction;
47  }
48  }
49  if (has_param_flag(flag, PARAM_C)) {
50  SETARG_C(instruction, args[cur_cnt++]);
51  if (cur_cnt >= arg_num) {
52  return instruction;
53  }
54  }
55  if (has_param_flag(flag, PARAM_sC)) {
56  SETARG_sC(instruction, args[cur_cnt++]);
57  if (cur_cnt >= arg_num) {
58  return instruction;
59  }
60  }
61  if (has_param_flag(flag, PARAM_Ax)) {
62  SETARG_Ax(instruction, args[cur_cnt++]);
63  if (cur_cnt >= arg_num) {
64  return instruction;
65  }
66  }
67  if (has_param_flag(flag, PARAM_sBx)) {
68  SETARG_sBx(instruction, args[cur_cnt++]);
69  if (cur_cnt >= arg_num) {
70  return instruction;
71  }
72  }
73  if (has_param_flag(flag, PARAM_Bx)) {
74  SETARG_Bx(instruction, args[cur_cnt++]);
75  if (cur_cnt >= arg_num) {
76  return instruction;
77  }
78  }
79  if (has_param_flag(flag, PARAM_sJ)) {
80  SETARG_sJ(instruction, args[cur_cnt++]);
81  if (cur_cnt >= arg_num) {
82  return instruction;
83  }
84  }
85  if (has_param_flag(flag, PARAM_k)) {
86  SETARG_k(instruction, args[cur_cnt++]);
87  if (cur_cnt >= arg_num) {
88  return instruction;
89  }
90  }
91  assert(cur_cnt == arg_num);
92 
93  return instruction;
94 }
lzma_index ** i
Definition: index.h:629
#define SETARG_A(i, v)
Definition: arch_53.h:177
#define PARAM_Ax
Definition: arch_53.h:41
#define SETARG_C(i, v)
Definition: arch_53.h:183
#define SETARG_Bx(i, v)
Definition: arch_53.h:186
#define PARAM_Bx
Definition: arch_53.h:42
#define PARAM_C
Definition: arch_53.h:40
#define SETARG_B(i, v)
Definition: arch_53.h:180
#define PARAM_sBx
Definition: arch_53.h:43
#define PARAM_B
Definition: arch_53.h:39
#define PARAM_A
Definition: arch_53.h:38
#define SETARG_sBx(i, b)
Definition: arch_53.h:192
#define has_param_flag(flag, bit)
Definition: arch_53.h:45
#define SETARG_Ax(i, v)
Definition: arch_53.h:189
#define PARAM_k
Definition: arch_54.h:242
#define SETARG_sB(i, v)
Definition: arch_54.h:228
#define PARAM_sB
Definition: arch_54.h:241
#define PARAM_sC
Definition: arch_54.h:240
#define LUA_SET_OPCODE(i, o)
Definition: arch_54.h:199
#define PARAM_sJ
Definition: arch_54.h:239
#define SETARG_sC(i, v)
Definition: arch_54.h:227
#define SETARG_sJ(i, j)
Definition: arch_54.h:225
#define SETARG_k(i, v)
Definition: arch_54.h:230
bool lua_is_valid_num_value_string(const char *str)
Definition: lua_arch.c:129
int lua_load_next_arg_start(const char *raw_string, char *recv_buf)
Definition: lua_arch.c:98
int lua_convert_str_to_num(const char *str)
Definition: lua_arch.c:137
ut32 LuaInstruction
Definition: lua_arch.h:27
assert(limit<=UINT32_MAX/2)
int args
Definition: mipsasm.c:18
Definition: buffer.h:15

References args, assert(), has_param_flag, i, lua_convert_str_to_num(), lua_is_valid_num_value_string(), lua_load_next_arg_start(), LUA_SET_OPCODE, PARAM_A, PARAM_Ax, PARAM_B, PARAM_Bx, PARAM_C, PARAM_k, PARAM_sB, PARAM_sBx, PARAM_sC, PARAM_sJ, SETARG_A, SETARG_Ax, SETARG_B, SETARG_Bx, SETARG_C, SETARG_k, SETARG_sB, SETARG_sBx, SETARG_sC, and SETARG_sJ.

Referenced by lua54_assembly().

◆ lua54_assembly()

bool lua54_assembly ( const char *  input,
st32  input_size,
LuaInstruction instruction_p 
)

Definition at line 96 of file assembly_54.c.

96  {
97  const char *opcode_start; // point to the header
98  const char *opcode_end; // point to the first white space
99  int opcode_len;
100 
101  const char *arg_start;
102 
103  ut8 opcode;
105 
106  /* Find the opcode */
107  opcode_start = input;
108  opcode_end = strchr(input, ' ');
109  if (opcode_end == NULL) {
110  opcode_end = input + input_size;
111  }
112 
113  opcode_len = opcode_end - opcode_start;
114  opcode = get_lua54_opcode_by_name(opcode_start, opcode_len);
115 
116  /* Find the arguments */
117  arg_start = rz_str_trim_head_ro(opcode_end);
118 
119  /* Encode opcode and args */
120  switch (opcode) {
121  case OP_GETI:
122  case OP_MMBIN:
123  case OP_GETTABUP:
124  case OP_CALL:
125  case OP_GETTABLE:
126  case OP_ADD:
127  case OP_SUB:
128  case OP_MUL:
129  case OP_POW:
130  case OP_DIV:
131  case OP_IDIV:
132  case OP_BAND:
133  case OP_BOR:
134  case OP_SHL:
135  case OP_SHR:
136  case OP_ADDK:
137  case OP_SUBK:
138  case OP_MULK:
139  case OP_MODK:
140  case OP_POWK:
141  case OP_DIVK:
142  case OP_IDIVK:
143  case OP_BANDK:
144  case OP_BORK:
145  case OP_BXORK:
146  case OP_GETFIELD:
147  instruction = encode_instruction(opcode, arg_start,
148  PARAM_A | PARAM_B | PARAM_C,
149  3);
150  break;
151  // iABC k instruction
152  case OP_TAILCALL:
153  case OP_RETURN:
154  case OP_NEWTABLE:
155  case OP_SETLIST:
156  case OP_MMBINK:
157  case OP_SETTABUP:
158  case OP_SETTABLE:
159  case OP_SETI:
160  case OP_SETFIELD:
161  case OP_SELF:
162  instruction = encode_instruction(opcode, arg_start,
164  4);
165  break;
166  // AsBC k instruction
167  case OP_MMBINI:
168  instruction = encode_instruction(opcode, arg_start,
170  4);
171  break;
172  // ABsC
173  case OP_ADDI:
174  case OP_SHRI:
175  case OP_SHLI:
176  instruction = encode_instruction(opcode, arg_start,
178  3);
179  break;
180  // AB
181  case OP_MOVE:
182  case OP_UNM:
183  case OP_BNOT:
184  case OP_NOT:
185  case OP_LEN:
186  case OP_CONCAT:
187  case OP_LOADNIL:
188  case OP_GETUPVAL:
189  case OP_SETUPVAL:
190  instruction = encode_instruction(opcode, arg_start,
191  PARAM_A | PARAM_B,
192  2);
193  break;
194  // AB with k
195  case OP_EQ:
196  case OP_LT:
197  case OP_LE:
198  case OP_TESTSET:
199  case OP_EQK:
200  instruction = encode_instruction(opcode, arg_start,
201  PARAM_A | PARAM_B | PARAM_k,
202  3);
203  break;
204  // AsB with k
205  case OP_EQI:
206  case OP_LTI:
207  case OP_LEI:
208  case OP_GTI:
209  case OP_GEI:
210  instruction = encode_instruction(opcode, arg_start,
212  3);
213  break;
214  // AC
215  case OP_TFORCALL:
216  case OP_VARARG:
217  instruction = encode_instruction(opcode, arg_start,
218  PARAM_A | PARAM_C,
219  2);
220  break;
221  // A
222  case OP_LOADKX:
223  case OP_LOADFALSE:
224  case OP_LFALSESKIP:
225  case OP_LOADTRUE:
226  case OP_CLOSE:
227  case OP_TBC:
228  case OP_RETURN1:
229  case OP_VARARGPREP:
230  instruction = encode_instruction(opcode, arg_start,
231  PARAM_A,
232  1);
233  break;
234  // A with k
235  case OP_TEST:
236  instruction = encode_instruction(opcode, arg_start,
237  PARAM_A | PARAM_k,
238  2);
239  break;
240  // no arg
241  case OP_RETURN0:
243  break;
244  // A Bx
245  case OP_LOADK:
246  case OP_FORLOOP:
247  case OP_FORPREP:
248  case OP_TFORLOOP:
249  case OP_TFORPREP:
250  case OP_CLOSURE:
251  instruction = encode_instruction(opcode, arg_start,
252  PARAM_A | PARAM_Bx,
253  2);
254  break;
255  // A sBx
256  case OP_LOADI:
257  case OP_LOADF:
258  instruction = encode_instruction(opcode, arg_start,
259  PARAM_A | PARAM_sBx,
260  2);
261  break;
262  // Ax
263  case OP_EXTRAARG:
264  instruction = encode_instruction(opcode, arg_start,
265  PARAM_Ax,
266  1);
267  break;
268  // isJ
269  case OP_JMP:
270  instruction = encode_instruction(opcode, arg_start,
271  PARAM_sJ,
272  1);
273  break;
274  default:
275  return false;
276  }
277 
278  if (instruction == -1) {
279  return false;
280  }
281 
282  *instruction_p = instruction;
283  return true;
284 }
@ OP_DIV
Definition: 8051_ops.h:51
@ OP_ADD
Definition: 8051_ops.h:42
@ OP_MUL
Definition: 8051_ops.h:63
@ OP_JMP
Definition: 8051_ops.h:57
@ OP_SETLIST
Definition: arch_53.h:122
@ OP_EQ
Definition: arch_53.h:104
@ OP_VARARG
Definition: arch_53.h:126
@ OP_CONCAT
Definition: arch_53.h:101
@ OP_BOR
Definition: arch_53.h:92
@ OP_SETTABLE
Definition: arch_53.h:78
@ OP_POW
Definition: arch_53.h:88
@ OP_NOT
Definition: arch_53.h:98
@ OP_TESTSET
Definition: arch_53.h:109
@ OP_CLOSURE
Definition: arch_53.h:124
@ OP_SETUPVAL
Definition: arch_53.h:77
@ OP_FORPREP
Definition: arch_53.h:117
@ OP_LEN
Definition: arch_53.h:99
@ OP_LOADNIL
Definition: arch_53.h:70
@ OP_BAND
Definition: arch_53.h:91
@ OP_SELF
Definition: arch_53.h:82
@ OP_SUB
Definition: arch_53.h:85
@ OP_SHR
Definition: arch_53.h:95
@ OP_LT
Definition: arch_53.h:105
@ OP_TFORLOOP
Definition: arch_53.h:120
@ OP_SHL
Definition: arch_53.h:94
@ OP_TEST
Definition: arch_53.h:108
@ OP_TFORCALL
Definition: arch_53.h:119
@ OP_FORLOOP
Definition: arch_53.h:115
@ OP_GETTABLE
Definition: arch_53.h:74
@ OP_LOADK
Definition: arch_53.h:67
@ OP_GETUPVAL
Definition: arch_53.h:71
@ OP_SETTABUP
Definition: arch_53.h:76
@ OP_IDIV
Definition: arch_53.h:90
@ OP_GETTABUP
Definition: arch_53.h:73
@ OP_LE
Definition: arch_53.h:106
@ OP_RETURN
Definition: arch_53.h:113
@ OP_BNOT
Definition: arch_53.h:97
@ OP_MOVE
Definition: arch_53.h:66
@ OP_UNM
Definition: arch_53.h:96
@ OP_EXTRAARG
Definition: arch_53.h:128
@ OP_LOADKX
Definition: arch_53.h:68
@ OP_NEWTABLE
Definition: arch_53.h:80
@ OP_TAILCALL
Definition: arch_53.h:112
@ OP_MMBINK
Definition: arch_54.h:131
@ OP_EQI
Definition: arch_54.h:148
@ OP_VARARGPREP
Definition: arch_54.h:178
@ OP_LFALSESKIP
Definition: arch_54.h:78
@ OP_MMBINI
Definition: arch_54.h:130
@ OP_POWK
Definition: arch_54.h:104
@ OP_BANDK
Definition: arch_54.h:108
@ OP_MMBIN
Definition: arch_54.h:129
@ OP_MULK
Definition: arch_54.h:102
@ OP_SETFIELD
Definition: arch_54.h:92
@ OP_TBC
Definition: arch_54.h:141
@ OP_GTI
Definition: arch_54.h:151
@ OP_SETI
Definition: arch_54.h:91
@ OP_BORK
Definition: arch_54.h:109
@ OP_LOADI
Definition: arch_54.h:73
@ OP_LTI
Definition: arch_54.h:149
@ OP_LOADF
Definition: arch_54.h:74
@ OP_RETURN1
Definition: arch_54.h:162
@ OP_LOADFALSE
Definition: arch_54.h:77
@ OP_DIVK
Definition: arch_54.h:105
@ OP_SHRI
Definition: arch_54.h:112
@ OP_GETI
Definition: arch_54.h:86
@ OP_IDIVK
Definition: arch_54.h:106
@ OP_CLOSE
Definition: arch_54.h:140
@ OP_ADDK
Definition: arch_54.h:100
@ OP_TFORPREP
Definition: arch_54.h:168
@ OP_SHLI
Definition: arch_54.h:113
@ OP_GETFIELD
Definition: arch_54.h:87
@ OP_LOADTRUE
Definition: arch_54.h:79
@ OP_EQK
Definition: arch_54.h:147
@ OP_MODK
Definition: arch_54.h:103
@ OP_GEI
Definition: arch_54.h:152
@ OP_RETURN0
Definition: arch_54.h:161
@ OP_LEI
Definition: arch_54.h:150
@ OP_BXORK
Definition: arch_54.h:110
@ OP_SUBK
Definition: arch_54.h:101
static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num)
Definition: assembly_54.c:6
#define NULL
Definition: cris-opc.c:27
uint8_t ut8
Definition: lh5801.h:11
ut8 get_lua54_opcode_by_name(const char *name, int len)
Definition: opcode_54.c:105
#define OP_CALL
Definition: nios2.h:263
#define OP_ADDI
Definition: nios2.h:253
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)

References encode_instruction(), get_lua54_opcode_by_name(), input(), LUA_SET_OPCODE, NULL, OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, PARAM_A, PARAM_Ax, PARAM_B, PARAM_Bx, PARAM_C, PARAM_k, PARAM_sB, PARAM_sBx, PARAM_sC, PARAM_sJ, and rz_str_trim_head_ro().

Referenced by rz_luac_asm().