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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use crate::bytecompiler::{
Access, BindingAccessOpcode, BindingKind, ByteCompiler, Register, ToJsString,
};
use boa_ast::{
expression::{
access::{PropertyAccess, PropertyAccessField},
operator::{Update, update::UpdateOp},
},
scope::BindingLocatorError,
};
impl ByteCompiler<'_> {
pub(crate) fn compile_update(&mut self, update: &Update, dst: &Register, discard: bool) {
let mut compiler = self.position_guard(update);
let increment = matches!(
update.op(),
UpdateOp::IncrementPost | UpdateOp::IncrementPre
);
let post = matches!(
update.op(),
UpdateOp::IncrementPost | UpdateOp::DecrementPost
);
match Access::from_update_target(update.target()) {
Access::Variable { name } => {
let name = name.to_js_string(compiler.interner());
let binding = compiler
.lexical_scope
.get_identifier_reference(name.clone());
let is_lexical = binding.is_lexical();
let index = compiler.get_binding(&binding);
// Fast path: for mutable local bindings with (post/pre)-increment/decrement,
// use the local register directly to avoid unnecessary Move instructions.
//
// Pre-increment (++i):
// Inc(dst, local); Move(local, dst) → 2 ops
//
// Post-increment (i++):
// Move(dst, local); Inc(local, local) → 2 ops
//
// Inc(local, local) works because Inc writes new to dst AFTER old to src,
// so when dst==src the new value wins.
//
// Skip for const bindings — they must fall through to emit ThrowMutateImmutable.
if is_lexical
&& compiler
.lexical_scope
.set_mutable_binding(name.clone())
.is_ok()
&& let BindingKind::Local(Some(local_reg)) = &index
{
let local_op = (*local_reg).into();
if discard {
// Result unused — just increment in-place.
if increment {
compiler.bytecode.emit_inc(local_op, local_op);
} else {
compiler.bytecode.emit_dec(local_op, local_op);
}
return;
}
if post {
// Save old value to dst (post-increment returns old value).
compiler.bytecode.emit_move(dst.variable(), local_op);
// Increment in-place.
if increment {
compiler.bytecode.emit_inc(local_op, local_op);
} else {
compiler.bytecode.emit_dec(local_op, local_op);
}
} else {
if increment {
compiler.bytecode.emit_inc(dst.variable(), local_op);
} else {
compiler.bytecode.emit_dec(dst.variable(), local_op);
}
// Write the new value back to the local register.
compiler.bytecode.emit_move(local_op, dst.variable());
}
return;
}
if is_lexical {
compiler.emit_binding_access(BindingAccessOpcode::GetName, &index, dst);
} else {
compiler.emit_binding_access(
BindingAccessOpcode::GetNameAndLocator,
&index,
dst,
);
}
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}
if is_lexical {
match compiler.lexical_scope.set_mutable_binding(name.clone()) {
Ok(binding) => {
let index = compiler.insert_binding(binding);
compiler.emit_binding_access(
BindingAccessOpcode::SetName,
&index,
&value,
);
}
Err(BindingLocatorError::MutateImmutable) => {
let index = compiler.get_or_insert_string(name);
compiler.bytecode.emit_throw_mutate_immutable(index.into());
}
Err(BindingLocatorError::Silent) => {}
}
} else {
compiler.emit_binding_access(
BindingAccessOpcode::SetNameByLocator,
&index,
&value,
);
}
if !post && !discard {
compiler
.bytecode
.emit_move(dst.variable(), value.variable());
}
compiler.register_allocator.dealloc(value);
}
Access::Property { access } => match access {
PropertyAccess::Simple(access) => {
let object = compiler.register_allocator.alloc();
compiler.compile_expr(access.target(), &object);
match access.field() {
PropertyAccessField::Const(ident) => {
compiler.emit_get_property_by_name(dst, None, &object, ident.sym());
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}
compiler.emit_set_property_by_name(&value, None, &object, ident.sym());
if !post {
compiler
.bytecode
.emit_move(dst.variable(), value.variable());
}
compiler.register_allocator.dealloc(object);
compiler.register_allocator.dealloc(value);
}
PropertyAccessField::Expr(expr) => {
let key = compiler.register_allocator.alloc();
compiler.compile_expr(expr, &key);
compiler.bytecode.emit_get_property_by_value_push(
dst.variable(),
key.variable(),
object.variable(),
object.variable(),
);
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}
compiler.bytecode.emit_set_property_by_value(
value.variable(),
key.variable(),
object.variable(),
object.variable(),
);
if !post {
compiler
.bytecode
.emit_move(dst.variable(), value.variable());
}
compiler.register_allocator.dealloc(key);
compiler.register_allocator.dealloc(object);
compiler.register_allocator.dealloc(value);
}
}
}
PropertyAccess::Private(access) => {
let index = compiler.get_or_insert_private_name(access.field());
let object = compiler.register_allocator.alloc();
compiler.compile_expr(access.target(), &object);
compiler.bytecode.emit_get_private_field(
dst.variable(),
object.variable(),
index.into(),
);
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}
compiler.bytecode.emit_set_private_field(
value.variable(),
object.variable(),
index.into(),
);
if !post {
compiler
.bytecode
.emit_move(dst.variable(), value.variable());
}
compiler.register_allocator.dealloc(value);
compiler.register_allocator.dealloc(object);
}
PropertyAccess::Super(access) => match access.field() {
PropertyAccessField::Const(ident) => {
let object = compiler.register_allocator.alloc();
let receiver = compiler.register_allocator.alloc();
compiler.super_(&receiver, &object);
compiler.emit_get_property_by_name(
dst,
Some(&receiver),
&object,
ident.sym(),
);
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}
compiler.emit_set_property_by_name(
&value,
Some(&receiver),
&object,
ident.sym(),
);
if !post {
compiler
.bytecode
.emit_move(dst.variable(), value.variable());
}
compiler.register_allocator.dealloc(receiver);
compiler.register_allocator.dealloc(object);
compiler.register_allocator.dealloc(value);
}
PropertyAccessField::Expr(expr) => {
let object = compiler.register_allocator.alloc();
let receiver = compiler.register_allocator.alloc();
compiler.super_(&receiver, &object);
let key = compiler.register_allocator.alloc();
compiler.compile_expr(expr, &key);
compiler.bytecode.emit_get_property_by_value(
dst.variable(),
key.variable(),
receiver.variable(),
object.variable(),
);
if increment {
compiler.bytecode.emit_inc(dst.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(dst.variable(), dst.variable());
}
compiler.bytecode.emit_set_property_by_value(
dst.variable(),
key.variable(),
receiver.variable(),
object.variable(),
);
compiler.register_allocator.dealloc(receiver);
compiler.register_allocator.dealloc(object);
compiler.register_allocator.dealloc(key);
}
},
},
Access::This => unreachable!(),
}
}
}