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