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
use crate::enums::kind::Kind;
use crate::records::compile_error::CompileError;
use crate::records::compiler::Compiler;
use crate::records::l_value::LValue;
use luaur_ast::records::ast_expr::AstExpr;
use luaur_ast::records::ast_expr_index_expr::AstExprIndexExpr;
use luaur_ast::records::ast_expr_index_name::AstExprIndexName;
use luaur_ast::records::ast_node::AstNode;
use luaur_bytecode::methods::bytecode_builder_get_string_hash::bytecode_builder_get_string_hash;
use luaur_common::enums::luau_bytecode_type::LBC_TYPE_TABLE;
use luaur_common::enums::luau_opcode::LuauOpcode;
use luaur_common::macros::luau_assert::LUAU_ASSERT;
impl Compiler {
pub fn compile_l_value_use(
&mut self,
lv: &LValue,
reg: u8,
set: bool,
target_expr: *mut AstExpr,
) {
// C++ `compileLValueUse` opens with `setDebugLine(lv.location)` so the store/load
// instruction is attributed to the index's own location (e.g. the `["d"]` line of
// `a["b"]["c"]["d"] = 4`), not whatever line was set while compiling the lvalue base.
self.set_debug_line_location(&lv.location);
unsafe {
match lv.kind {
Kind::Kind_Local => {
if set {
(*self.bytecode).emit_abc(LuauOpcode::LOP_MOVE, lv.reg, reg, 0);
} else {
(*self.bytecode).emit_abc(LuauOpcode::LOP_MOVE, reg, lv.reg, 0);
}
}
Kind::Kind_Upvalue => {
if set {
(*self.bytecode).emit_abc(LuauOpcode::LOP_SETUPVAL, reg, lv.upval, 0);
} else {
(*self.bytecode).emit_abc(LuauOpcode::LOP_GETUPVAL, reg, lv.upval, 0);
}
}
Kind::Kind_Global => {
let cid = (*self.bytecode).add_constant_string(lv.name);
if cid < 0 {
CompileError::raise(
&lv.location,
format_args!("Exceeded constant limit; simplify the code to compile"),
);
}
let hash = bytecode_builder_get_string_hash(lv.name) as u8;
if set {
(*self.bytecode).emit_abc(LuauOpcode::LOP_SETGLOBAL, reg, 0, hash);
} else {
(*self.bytecode).emit_abc(LuauOpcode::LOP_GETGLOBAL, reg, 0, hash);
}
(*self.bytecode).emit_aux(cid as u32);
}
Kind::Kind_IndexName => {
let cid = (*self.bytecode).add_constant_string(lv.name);
if cid < 0 {
CompileError::raise(
&lv.location,
format_args!("Exceeded constant limit; simplify the code to compile"),
);
}
let hash = bytecode_builder_get_string_hash(lv.name) as u8;
if set {
(*self.bytecode).emit_abc(LuauOpcode::LOP_SETTABLEKS, reg, lv.reg, hash);
} else {
(*self.bytecode).emit_abc(LuauOpcode::LOP_GETTABLEKS, reg, lv.reg, hash);
}
(*self.bytecode).emit_aux(cid as u32);
if !target_expr.is_null() {
let target_expr_index_name = luaur_ast::rtti::ast_node_as::<AstExprIndexName>(
target_expr as *mut AstNode,
);
if !target_expr_index_name.is_null() {
self.hint_temporary_expr_reg_type(
(*target_expr_index_name).expr,
lv.reg as i32,
LBC_TYPE_TABLE,
2,
);
}
}
}
Kind::Kind_IndexNumber => {
if set {
(*self.bytecode).emit_abc(
LuauOpcode::LOP_SETTABLEN,
reg,
lv.reg,
lv.number,
);
} else {
(*self.bytecode).emit_abc(
LuauOpcode::LOP_GETTABLEN,
reg,
lv.reg,
lv.number,
);
}
if !target_expr.is_null() {
let target_expr_index_expr = luaur_ast::rtti::ast_node_as::<AstExprIndexExpr>(
target_expr as *mut AstNode,
);
if !target_expr_index_expr.is_null() {
self.hint_temporary_expr_reg_type(
(*target_expr_index_expr).expr,
lv.reg as i32,
LBC_TYPE_TABLE,
1,
);
}
}
}
Kind::Kind_IndexExpr => {
if set {
(*self.bytecode).emit_abc(LuauOpcode::LOP_SETTABLE, reg, lv.reg, lv.index);
} else {
(*self.bytecode).emit_abc(LuauOpcode::LOP_GETTABLE, reg, lv.reg, lv.index);
}
if !target_expr.is_null() {
let target_expr_index_expr = luaur_ast::rtti::ast_node_as::<AstExprIndexExpr>(
target_expr as *mut AstNode,
);
if !target_expr_index_expr.is_null() {
self.hint_temporary_expr_reg_type(
(*target_expr_index_expr).expr,
lv.reg as i32,
LBC_TYPE_TABLE,
1,
);
self.hint_temporary_expr_reg_type(
(*target_expr_index_expr).index,
lv.index as i32,
luaur_common::enums::luau_bytecode_type::LBC_TYPE_NUMBER,
1,
);
}
}
}
_ => LUAU_ASSERT!(false),
}
}
}
}