Rizin
unix-like reverse engineering framework and cli tools
arch_53.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 // SPDX-FileCopyrightText: 2017 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
4 
5 #ifndef BUILD_ARCH_53_H
6 #define BUILD_ARCH_53_H
7 
8 #include <rz_types.h>
9 #include <rz_asm.h>
10 #include <stddef.h>
12 
13 /*===========================================================================
14  We assume that instructions are unsigned numbers.
15  All instructions have an opcode in the first 6 bits.
16  Instructions can have the following fields:
17  'A' : 8 bits
18  'B' : 9 bits
19  'C' : 9 bits
20  'Ax' : 26 bits ('A', 'B', and 'C' together)
21  'Bx' : 18 bits ('B' and 'C' together)
22  'sBx' : signed Bx
23  A signed argument is represented in excess K; that is, the number
24  value is the unsigned value minus K. K is exactly the maximum value
25  for that argument (so that -max is represented by 0, and +max is
26  represented by 2*max), which is half the maximum for the corresponding
27  unsigned argument.
28 ===========================================================================*/
29 
30 typedef enum {
34  iAx
36 
37 /* parameter flags */
38 #define PARAM_A 1
39 #define PARAM_B 2
40 #define PARAM_C 4
41 #define PARAM_Ax 8
42 #define PARAM_Bx 16
43 #define PARAM_sBx 32
44 
45 #define has_param_flag(flag, bit) ((flag) & (bit)) ? true : false
46 
47 /* Offset of arguments in opcode */
48 #define SIZE_C 9
49 #define SIZE_B 9
50 #define SIZE_Bx (SIZE_C + SIZE_B)
51 #define SIZE_A 8
52 #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
53 #define SIZE_OP 6
54 
55 #define POS_OP 0
56 #define POS_A (POS_OP + SIZE_OP)
57 #define POS_C (POS_A + SIZE_A)
58 #define POS_B (POS_C + SIZE_C)
59 #define POS_Bx POS_C
60 #define POS_Ax POS_A
61 
62 typedef enum {
63  /*----------------------------------------------------------------------
64 name args description
65 ------------------------------------------------------------------------*/
66  OP_MOVE, /* A B R(A) := R(B) */
67  OP_LOADK, /* A Bx R(A) := Kst(Bx) */
68  OP_LOADKX, /* A R(A) := Kst(extra arg) */
69  OP_LOADBOOL, /* A B C R(A) := (Bool)B; if (C) pc++ */
70  OP_LOADNIL, /* A B R(A), R(A+1), ..., R(A+B) := nil */
71  OP_GETUPVAL, /* A B R(A) := UpValue[B] */
72 
73  OP_GETTABUP, /* A B C R(A) := UpValue[B][RK(C)] */
74  OP_GETTABLE, /* A B C R(A) := R(B)[RK(C)] */
75 
76  OP_SETTABUP, /* A B C UpValue[A][RK(B)] := RK(C) */
77  OP_SETUPVAL, /* A B UpValue[B] := R(A) */
78  OP_SETTABLE, /* A B C R(A)[RK(B)] := RK(C) */
79 
80  OP_NEWTABLE, /* A B C R(A) := {} (size = B,C) */
81 
82  OP_SELF, /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
83 
84  OP_ADD, /* A B C R(A) := RK(B) + RK(C) */
85  OP_SUB, /* A B C R(A) := RK(B) - RK(C) */
86  OP_MUL, /* A B C R(A) := RK(B) * RK(C) */
87  OP_MOD, /* A B C R(A) := RK(B) % RK(C) */
88  OP_POW, /* A B C R(A) := RK(B) ^ RK(C) */
89  OP_DIV, /* A B C R(A) := RK(B) / RK(C) */
90  OP_IDIV, /* A B C R(A) := RK(B) // RK(C) */
91  OP_BAND, /* A B C R(A) := RK(B) & RK(C) */
92  OP_BOR, /* A B C R(A) := RK(B) | RK(C) */
93  OP_BXOR, /* A B C R(A) := RK(B) ~ RK(C) */
94  OP_SHL, /* A B C R(A) := RK(B) << RK(C) */
95  OP_SHR, /* A B C R(A) := RK(B) >> RK(C) */
96  OP_UNM, /* A B R(A) := -R(B) */
97  OP_BNOT, /* A B R(A) := ~R(B) */
98  OP_NOT, /* A B R(A) := not R(B) */
99  OP_LEN, /* A B R(A) := length of R(B) */
100 
101  OP_CONCAT, /* A B C R(A) := R(B).. ... ..R(C) */
102 
103  OP_JMP, /* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
104  OP_EQ, /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
105  OP_LT, /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
106  OP_LE, /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
107 
108  OP_TEST, /* A C if not (R(A) <=> C) then pc++ */
109  OP_TESTSET, /* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
110 
111  OP_CALL, /* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
112  OP_TAILCALL, /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
113  OP_RETURN, /* A B return R(A), ... ,R(A+B-2) (see note) */
114 
115  OP_FORLOOP, /* A sBx R(A)+=R(A+2);
116  if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
117  OP_FORPREP, /* A sBx R(A)-=R(A+2); pc+=sBx */
118 
119  OP_TFORCALL, /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
120  OP_TFORLOOP, /* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
121 
122  OP_SETLIST, /* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
123 
124  OP_CLOSURE, /* A Bx R(A) := closure(KPROTO[Bx]) */
125 
126  OP_VARARG, /* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
127 
128  OP_EXTRAARG /* Ax extra (larger) argument for previous opcode */
130 
131 #define LUA_NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
132 
133 #define MAX_INT INT_MAX /* maximum value of an int */
134 
135 #define LUAI_BITSINT 32
136 
137 /*
138 ** limits for opcode arguments.
139 ** we use (signed) int to manipulate most arguments,
140 ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
141 */
142 #if SIZE_Bx < LUAI_BITSINT - 1
143 #define MAXARG_Bx ((1 << SIZE_Bx) - 1)
144 #define MAXARG_sBx (MAXARG_Bx >> 1) /* 'sBx' is signed */
145 #else
146 #define MAXARG_Bx MAX_INT
147 #define MAXARG_sBx MAX_INT
148 #endif
149 
150 #if SIZE_Ax < LUAI_BITSINT - 1
151 #define MAXARG_Ax ((1 << SIZE_Ax) - 1)
152 #else
153 #define MAXARG_Ax MAX_INT
154 #endif
155 
156 #define MAXARG_A ((1 << SIZE_A) - 1)
157 #define MAXARG_B ((1 << SIZE_B) - 1)
158 #define MAXARG_C ((1 << SIZE_C) - 1)
159 
160 /* creates a mask with 'n' 1 bits at position 'p' */
161 #define MASK1(n, p) ((~((~0u) << (n))) << (p))
162 
163 /* creates a mask with 'n' 0 bits at position 'p' */
164 #define MASK0(n, p) (~MASK1(n, p))
165 
166 #define cast(x, y) ((x)(y))
167 
168 #define GET_OPCODE(i) (cast(LuaOpCode, ((i) >> POS_OP) & MASK1(SIZE_OP, 0)))
169 #define SET_OPCODE(i, o) ((i) = (((i)&MASK0(SIZE_OP, POS_OP)) | \
170  ((cast(ut32, o) << POS_OP) & MASK1(SIZE_OP, POS_OP))))
171 
172 #define getarg(i, pos, size) (cast(int, ((i) >> (pos)) & MASK1(size, 0)))
173 #define setarg(i, v, pos, size) ((i) = (((i)&MASK0(size, pos)) | \
174  ((cast(ut32, v) << (pos)) & MASK1(size, pos))))
175 
176 #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
177 #define SETARG_A(i, v) setarg(i, v, POS_A, SIZE_A)
178 
179 #define GETARG_B(i) getarg(i, POS_B, SIZE_B)
180 #define SETARG_B(i, v) setarg(i, v, POS_B, SIZE_B)
181 
182 #define GETARG_C(i) getarg(i, POS_C, SIZE_C)
183 #define SETARG_C(i, v) setarg(i, v, POS_C, SIZE_C)
184 
185 #define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)
186 #define SETARG_Bx(i, v) setarg(i, v, POS_Bx, SIZE_Bx)
187 
188 #define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)
189 #define SETARG_Ax(i, v) setarg(i, v, POS_Ax, SIZE_Ax)
190 
191 #define GETARG_sBx(i) (GETARG_Bx(i) - MAXARG_sBx)
192 #define SETARG_sBx(i, b) SETARG_Bx((i), cast(unsigned int, (b) + MAXARG_sBx))
193 
194 #define CREATE_ABC(o, a, b, c) ((cast(ut32, o) << POS_OP) | (cast(ut32, a) << POS_A) | (cast(ut32, b) << POS_B) | (cast(ut32, c) << POS_C))
195 
196 #define CREATE_ABx(o, a, bc) ((cast(ut32, o) << POS_OP) | (cast(ut32, a) << POS_A) | (cast(ut32, bc) << POS_Bx))
197 
198 #define CREATE_Ax(o, a) ((cast(ut32) << POS_OP) | (cast(ut32, a) << POS_Ax))
199 
200 #endif // BUILD_ARCH_53_H
LuaOpCode
Definition: arch_53.h:62
@ OP_SETLIST
Definition: arch_53.h:122
@ OP_CALL
Definition: arch_53.h:111
@ 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_MOD
Definition: arch_53.h:87
@ 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_DIV
Definition: arch_53.h:89
@ 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_ADD
Definition: arch_53.h:84
@ OP_FORLOOP
Definition: arch_53.h:115
@ OP_MUL
Definition: arch_53.h:86
@ 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_LOADBOOL
Definition: arch_53.h:69
@ OP_BXOR
Definition: arch_53.h:93
@ OP_JMP
Definition: arch_53.h:103
@ OP_TAILCALL
Definition: arch_53.h:112
LuaOpMode
Definition: arch_53.h:30
@ iAx
Definition: arch_53.h:34
@ iABC
Definition: arch_53.h:31
@ iAsBx
Definition: arch_53.h:33
@ iABx
Definition: arch_53.h:32