rhue_bytecode/op.rs
1use crate::ArithOp;
2
3macro_rules! opcodes {
4 ($( $(#[$doc:meta])* $name:ident = $mnemonic:literal ),* $(,)?) => {
5 /// Every opcode. The comment on each one gives its operands, `R(x)` is
6 /// register x and `K(x)` is constant x.
7 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8 #[repr(u8)]
9 pub enum Op { $( $(#[$doc])* $name ),* }
10
11 impl Op {
12 pub const ALL: &'static [Op] = &[ $( Op::$name ),* ];
13
14 /// Decodes an opcode byte. It's a match on each opcode's own value
15 /// so the optimiser sees the identity mapping (a lookup table would
16 /// cost a load per instruction in the VM).
17 #[inline]
18 pub fn from_u8(byte: u8) -> Option<Op> {
19 #[allow(non_upper_case_globals)]
20 mod byte_of {
21 use super::Op;
22 $( pub const $name: u8 = Op::$name as u8; )*
23 }
24 match byte {
25 $( byte_of::$name => Some(Op::$name), )*
26 _ => None,
27 }
28 }
29
30 pub fn mnemonic(self) -> &'static str {
31 match self { $( Op::$name => $mnemonic ),* }
32 }
33 }
34 };
35}
36
37opcodes! {
38 /// `R(A) := nil`
39 LoadNil = "LOADNIL",
40 /// `R(A) := (B != 0); if C != 0 then pc++`
41 LoadB = "LOADB",
42 /// `R(A) := sBx` (small integral numbers)
43 LoadN = "LOADN",
44 /// `R(A) := K(Bx)`
45 LoadK = "LOADK",
46 /// `R(A) := R(B)`
47 Move = "MOVE",
48 /// `R(A) := globals[K(Bx)]`
49 GetGlobal = "GETGLOBAL",
50 /// `globals[K(Bx)] := R(A)`
51 SetGlobal = "SETGLOBAL",
52
53 /// `R(A) := R(B) + R(C)`
54 Add = "ADD",
55 Sub = "SUB",
56 Mul = "MUL",
57 Div = "DIV",
58 IDiv = "IDIV",
59 Mod = "MOD",
60 Pow = "POW",
61
62 /// `R(A) := R(B) + K(C)`
63 AddK = "ADDK",
64 SubK = "SUBK",
65 MulK = "MULK",
66 DivK = "DIVK",
67 IDivK = "IDIVK",
68 ModK = "MODK",
69 PowK = "POWK",
70
71 /// `R(A) := -R(B)`
72 Unm = "UNM",
73 /// `R(A) := not R(B)`
74 Not = "NOT",
75 /// `R(A) := #R(B)`
76 Len = "LEN",
77 /// `R(A) := R(B) .. R(C)`
78 Concat = "CONCAT",
79
80 /// `if (R(B) == R(C)) != (A != 0) then pc++` (the next instruction is a `JMP`)
81 Eq = "EQ",
82 /// `if (R(B) < R(C)) != (A != 0) then pc++`
83 Lt = "LT",
84 /// `if (R(B) <= R(C)) != (A != 0) then pc++`
85 Le = "LE",
86 /// `if (R(B) == K(C)) != (A != 0) then pc++` (same for the ones below,
87 /// constant on the right. `a > b` means `b < a` for errors)
88 EqK = "EQK",
89 LtK = "LTK",
90 LeK = "LEK",
91 GtK = "GTK",
92 GeK = "GEK",
93
94 /// `pc += sAx` (24-bit signed offset, relative to the next instruction)
95 Jmp = "JMP",
96 /// `if R(A) is truthy then pc += sBx`
97 JmpIf = "JMPIF",
98 /// `if R(A) is falsy then pc += sBx`
99 JmpIfNot = "JMPIFNOT",
100
101 /// Numeric `for` setup. `R(A)` index, `R(A+1)` limit, `R(A+2)` step (all
102 /// checked to be numbers), `R(A+3)` the visible loop variable. Runs zero
103 /// times: `pc += sBx`. Otherwise `R(A+3) := R(A)`.
104 ForPrep = "FORPREP",
105 /// `R(A) += R(A+2)`, and if it's still in range, `R(A+3) := R(A)` and `pc
106 /// += sBx`.
107 ForLoop = "FORLOOP",
108
109 /// `R(A) := upvalues[B]`
110 GetUpval = "GETUPVAL",
111 /// `upvalues[B] := R(A)`
112 SetUpval = "SETUPVAL",
113 /// `R(A) := closure(protos[Bx])`, capturing upvalues per the child's
114 /// `upvalues` descriptors.
115 Closure = "CLOSURE",
116 /// Closes every open upvalue pointing at `R(A)` or above.
117 Close = "CLOSE",
118 /// `R(A), ..., R(A+C-2) := R(A)(R(A+1), ..., R(A+B-1))`. `B = 0`: arguments
119 /// go up to `top`. `C = 0`: keep every result and set `top` past the last
120 /// one.
121 Call = "CALL",
122 /// `R(A) := tostring(R(B)) .. ... .. tostring(R(B+C-1))`, for interpolated
123 /// strings (`__tostring` applies).
124 Interp = "INTERP",
125 /// `return R(A)(R(A+1), ..., R(A+B-1))`. A Lua callee replaces the current
126 /// frame (so tail recursion doesn't use stack), anything else gets called
127 /// like `CALL A B 0` and the `RETURN A 0 1` that always follows returns its
128 /// results.
129 TailCall = "TAILCALL",
130 /// `R(A), ..., R(A+B-2) := ...`. `B = 0`: copy all the varargs and set
131 /// `top`.
132 Vararg = "VARARG",
133
134 /// `R(A) := {}` with room for `B` array and `C` hash entries (just hints).
135 NewTable = "NEWTABLE",
136 /// `R(A) := R(B)[R(C)]`
137 GetTable = "GETTABLE",
138 /// `R(A) := R(B)[K(C)]` (constant string key)
139 GetField = "GETFIELD",
140 /// `R(A)[R(B)] := R(C)`
141 SetTable = "SETTABLE",
142 /// `R(A)[K(B)] := R(C)` (constant string key)
143 SetField = "SETFIELD",
144 /// `R(A+1) := R(B); R(A) := R(B)[K(C)]` (method lookup for `obj:m()`)
145 SelfOp = "SELF",
146 /// `R(A)[n + i] := R(A + i)` for `i` in `1..=B` (`B = 0`: up to `top`),
147 /// where `n` is the start offset in the `DATA` word after it.
148 SetList = "SETLIST",
149 /// Never executed, it's an operand word for the instruction before it.
150 Data = "DATA",
151 /// Generic-for step: `R(A+3), ..., R(A+2+C) := R(A)(R(A+1), R(A+2))`. If
152 /// `R(A)` is a table it gets iterated directly (Luau's generalized
153 /// iteration, like `next`).
154 TForCall = "TFORCALL",
155 /// `if R(A+3) ~= nil then R(A+2) := R(A+3); pc += sBx`
156 TForLoop = "TFORLOOP",
157
158 /// `return R(A), ..., R(A+B-1)`, or if `C != 0` return `R(A)` up to `top`.
159 Return = "RETURN",
160 /// `R(A) := {}` shaped by `templates[Bx]`. Room for its array and hash
161 /// sizes with its keys already in there (as nil), so a constructor's
162 /// `SETFIELD`s fill existing slots through their inline caches.
163 DupTable = "DUPTABLE",
164}
165
166impl Op {
167 /// The register-register opcode for an arithmetic operator.
168 pub fn arith_rr(op: ArithOp) -> Op {
169 match op {
170 ArithOp::Add => Op::Add,
171 ArithOp::Sub => Op::Sub,
172 ArithOp::Mul => Op::Mul,
173 ArithOp::Div => Op::Div,
174 ArithOp::IDiv => Op::IDiv,
175 ArithOp::Mod => Op::Mod,
176 ArithOp::Pow => Op::Pow,
177 }
178 }
179
180 /// The register-constant opcode for an arithmetic operator.
181 pub fn arith_rk(op: ArithOp) -> Op {
182 match op {
183 ArithOp::Add => Op::AddK,
184 ArithOp::Sub => Op::SubK,
185 ArithOp::Mul => Op::MulK,
186 ArithOp::Div => Op::DivK,
187 ArithOp::IDiv => Op::IDivK,
188 ArithOp::Mod => Op::ModK,
189 ArithOp::Pow => Op::PowK,
190 }
191 }
192
193 /// Inverse of [`Op::arith_rr`] / [`Op::arith_rk`].
194 pub fn as_arith(self) -> Option<ArithOp> {
195 Some(match self {
196 Op::Add | Op::AddK => ArithOp::Add,
197 Op::Sub | Op::SubK => ArithOp::Sub,
198 Op::Mul | Op::MulK => ArithOp::Mul,
199 Op::Div | Op::DivK => ArithOp::Div,
200 Op::IDiv | Op::IDivK => ArithOp::IDiv,
201 Op::Mod | Op::ModK => ArithOp::Mod,
202 Op::Pow | Op::PowK => ArithOp::Pow,
203 _ => return None,
204 })
205 }
206}