Skip to content

Commit 1da865a

Browse files
authored
refactor: Bytecode is one word and VaryingOperand does not vary any more (#4978)
in #4827, `VaryingOperand` is not actually varying any more and the name should reflect as such. Furthermore, currently there are both `bytecode` and `ByteCode` while bytecode is supposed to be one word (as far as i can tell). There are one or two more meaningful things i want to do with vm but want to raise this issue first so i dont submit a large pr with a lot of refactor changes + a few non-trivial changes.
1 parent ef8dbea commit 1da865a

File tree

31 files changed

+268
-290
lines changed

31 files changed

+268
-290
lines changed

core/engine/src/bytecompiler/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
vm::{
3535
CallFrame, CodeBlock, CodeBlockFlags, Constant, GeneratorResumeKind, GlobalFunctionBinding,
3636
Handler, InlineCache,
37-
opcode::{Address, BindingOpcode, ByteCodeEmitter, RegisterOperand},
37+
opcode::{Address, BindingOpcode, BytecodeEmitter, RegisterOperand},
3838
source_info::{SourceInfo, SourceMap, SourceMapBuilder, SourcePath},
3939
},
4040
};
@@ -487,7 +487,7 @@ pub struct ByteCompiler<'ctx> {
487487
pub(crate) parameter_scope: Scope,
488488

489489
/// Bytecode
490-
pub(crate) bytecode: ByteCodeEmitter,
490+
pub(crate) bytecode: BytecodeEmitter,
491491

492492
pub(crate) source_map_builder: SourceMapBuilder,
493493
pub(crate) source_path: SourcePath,
@@ -625,7 +625,7 @@ impl<'ctx> ByteCompiler<'ctx> {
625625
Self {
626626
function_name: name,
627627
length: 0,
628-
bytecode: ByteCodeEmitter::new(),
628+
bytecode: BytecodeEmitter::new(),
629629
source_map_builder: SourceMapBuilder::default(),
630630
constants: ThinVec::default(),
631631
bindings: Vec::default(),
@@ -1170,15 +1170,15 @@ impl<'ctx> ByteCompiler<'ctx> {
11701170
}
11711171

11721172
fn try_fused_comparison_branch(&mut self, op: RelationalOp, binary: &Binary) -> Option<Label> {
1173-
use crate::vm::opcode::ByteCodeEmitter;
1173+
use crate::vm::opcode::BytecodeEmitter;
11741174

1175-
let emit_fn: fn(&mut ByteCodeEmitter, Address, RegisterOperand, RegisterOperand) = match op
1175+
let emit_fn: fn(&mut BytecodeEmitter, Address, RegisterOperand, RegisterOperand) = match op
11761176
{
1177-
RelationalOp::LessThan => ByteCodeEmitter::emit_jump_if_not_less_than,
1178-
RelationalOp::LessThanOrEqual => ByteCodeEmitter::emit_jump_if_not_less_than_or_equal,
1179-
RelationalOp::GreaterThan => ByteCodeEmitter::emit_jump_if_not_greater_than,
1177+
RelationalOp::LessThan => BytecodeEmitter::emit_jump_if_not_less_than,
1178+
RelationalOp::LessThanOrEqual => BytecodeEmitter::emit_jump_if_not_less_than_or_equal,
1179+
RelationalOp::GreaterThan => BytecodeEmitter::emit_jump_if_not_greater_than,
11801180
RelationalOp::GreaterThanOrEqual => {
1181-
ByteCodeEmitter::emit_jump_if_not_greater_than_or_equal
1181+
BytecodeEmitter::emit_jump_if_not_greater_than_or_equal
11821182
}
11831183
_ => return None,
11841184
};

core/engine/src/vm/code_block.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use thin_vec::ThinVec;
1919

2020
use super::{
2121
InlineCache,
22-
opcode::{Address, ByteCode, Instruction, InstructionIterator},
22+
opcode::{Address, Bytecode, Instruction, InstructionIterator},
2323
source_info::{SourceInfo, SourceMap, SourcePath},
2424
};
2525

@@ -144,7 +144,7 @@ pub struct CodeBlock {
144144

145145
/// Bytecode
146146
#[unsafe_ignore_trace]
147-
pub(crate) bytecode: ByteCode,
147+
pub(crate) bytecode: Bytecode,
148148

149149
pub(crate) constants: ThinVec<Constant>,
150150

@@ -178,7 +178,7 @@ impl CodeBlock {
178178
let mut flags = CodeBlockFlags::empty();
179179
flags.set(CodeBlockFlags::STRICT, strict);
180180
Self {
181-
bytecode: ByteCode::default(),
181+
bytecode: Bytecode::default(),
182182
constants: ThinVec::default(),
183183
bindings: Box::default(),
184184
flags: Cell::new(flags),
@@ -1040,7 +1040,7 @@ impl Display for CodeBlock {
10401040
} else {
10411041
f.write_char('\n')?;
10421042

1043-
let bytecode_len = self.bytecode.bytecode.len() as u32;
1043+
let bytecode_len = self.bytecode.bytes.len() as u32;
10441044
for (i, handler) in self.source_info().map().entries().windows(2).enumerate() {
10451045
let current = handler[0];
10461046
let next = handler.get(1);

core/engine/src/vm/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ impl Context {
867867
.frame()
868868
.code_block
869869
.bytecode
870-
.bytecode
870+
.bytes
871871
.get(self.vm.frame().pc as usize)
872872
{
873873
let opcode = Opcode::decode(*byte);
@@ -905,7 +905,7 @@ impl Context {
905905
.frame()
906906
.code_block
907907
.bytecode
908-
.bytecode
908+
.bytes
909909
.get(self.vm.frame().pc as usize)
910910
{
911911
let opcode = Opcode::decode(*byte);

core/engine/src/vm/opcode/args.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use thin_vec::ThinVec;
22

3-
use super::{Address, RegisterOperand, VaryingOperand};
3+
use super::{Address, IndexOperand, RegisterOperand};
44

55
/// A trait for types that can be read from a byte slice.
66
///
@@ -138,9 +138,9 @@ impl Argument for () {
138138
}
139139
}
140140

141-
impl Argument for VaryingOperand {
141+
impl Argument for IndexOperand {
142142
fn encode(self, bytes: &mut Vec<u8>) {
143-
write_u32(bytes, self.value);
143+
write_u32(bytes, self.0);
144144
}
145145

146146
fn decode(bytes: &[u8], pos: usize) -> (Self, usize) {
@@ -151,7 +151,7 @@ impl Argument for VaryingOperand {
151151

152152
impl Argument for RegisterOperand {
153153
fn encode(self, bytes: &mut Vec<u8>) {
154-
write_u32(bytes, self.value);
154+
write_u32(bytes, self.0);
155155
}
156156

157157
fn decode(bytes: &[u8], pos: usize) -> (Self, usize) {
@@ -163,7 +163,7 @@ impl Argument for RegisterOperand {
163163
impl Argument for Address {
164164
#[inline(always)]
165165
fn encode(self, bytes: &mut Vec<u8>) {
166-
write_u32(bytes, self.value);
166+
write_u32(bytes, self.0);
167167
}
168168

169169
#[inline(always)]
@@ -221,7 +221,7 @@ impl_argument_for_int!(u8 u16 u32 u64 i8 i16 i32 f32 f64);
221221

222222
#[cfg(test)]
223223
mod tests {
224-
use super::{Address, Argument, RegisterOperand, VaryingOperand};
224+
use super::{Address, Argument, IndexOperand, RegisterOperand};
225225
use std::mem::size_of;
226226
use thin_vec::ThinVec;
227227

@@ -266,10 +266,8 @@ mod tests {
266266

267267
#[test]
268268
fn test_varying_operand_round_trip() {
269-
round_trip_eq(&VaryingOperand::new(0), |a, b| {
270-
u32::from(*a) == u32::from(*b)
271-
});
272-
round_trip_eq(&VaryingOperand::new(0xFFFF_FFFF), |a, b| {
269+
round_trip_eq(&IndexOperand::new(0), |a, b| u32::from(*a) == u32::from(*b));
270+
round_trip_eq(&IndexOperand::new(0xFFFF_FFFF), |a, b| {
273271
u32::from(*a) == u32::from(*b)
274272
});
275273
}

core/engine/src/vm/opcode/binary_ops/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{RegisterOperand, VaryingOperand};
1+
use super::{IndexOperand, RegisterOperand};
22
use crate::{Context, JsResult, error::JsNativeError, vm::opcode::Operation};
33

44
pub(crate) mod logical;
@@ -105,7 +105,7 @@ pub(crate) struct InPrivate;
105105
impl InPrivate {
106106
#[inline(always)]
107107
pub(super) fn operation(
108-
(dst, index, rhs): (RegisterOperand, VaryingOperand, RegisterOperand),
108+
(dst, index, rhs): (RegisterOperand, IndexOperand, RegisterOperand),
109109
context: &mut Context,
110110
) -> JsResult<()> {
111111
let name = context

core/engine/src/vm/opcode/call/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cell::RefCell, mem::MaybeUninit};
33
use boa_string::JsString;
44
use dynify::Dynify;
55

6-
use super::{RegisterOperand, VaryingOperand};
6+
use super::{IndexOperand, RegisterOperand};
77
use crate::{
88
Context, JsError, JsObject, JsResult, JsValue, NativeFunction,
99
builtins::{Promise, promise::PromiseCapability},
@@ -24,7 +24,7 @@ pub(crate) struct CallEval;
2424
impl CallEval {
2525
#[inline(always)]
2626
pub(super) fn operation(
27-
(argument_count, scope_index): (VaryingOperand, VaryingOperand),
27+
(argument_count, scope_index): (IndexOperand, IndexOperand),
2828
context: &mut Context,
2929
) -> JsResult<()> {
3030
let func = context
@@ -102,7 +102,7 @@ pub(crate) struct CallEvalSpread;
102102

103103
impl CallEvalSpread {
104104
#[inline(always)]
105-
pub(super) fn operation(index: VaryingOperand, context: &mut Context) -> JsResult<()> {
105+
pub(super) fn operation(index: IndexOperand, context: &mut Context) -> JsResult<()> {
106106
// Get the arguments that are stored as an array object on the stack.
107107
let arguments_array = context.vm.stack.pop();
108108
let arguments_array_object = arguments_array
@@ -183,7 +183,7 @@ pub(crate) struct Call;
183183

184184
impl Call {
185185
#[inline(always)]
186-
pub(super) fn operation(argument_count: VaryingOperand, context: &mut Context) -> JsResult<()> {
186+
pub(super) fn operation(argument_count: IndexOperand, context: &mut Context) -> JsResult<()> {
187187
let func = context
188188
.vm
189189
.stack

core/engine/src/vm/opcode/control_flow/throw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
Context, JsError, JsNativeError, JsResult,
55
vm::{
66
CompletionRecord,
7-
opcode::{Operation, RegisterOperand, VaryingOperand},
7+
opcode::{IndexOperand, Operation, RegisterOperand},
88
},
99
};
1010

@@ -152,7 +152,7 @@ pub(crate) struct ThrowNewTypeError;
152152

153153
impl ThrowNewTypeError {
154154
#[inline(always)]
155-
pub(crate) fn operation(index: VaryingOperand, context: &mut Context) -> JsError {
155+
pub(crate) fn operation(index: IndexOperand, context: &mut Context) -> JsError {
156156
let msg = context
157157
.vm
158158
.frame()
@@ -180,7 +180,7 @@ pub(crate) struct ThrowNewReferenceError;
180180

181181
impl ThrowNewReferenceError {
182182
#[inline(always)]
183-
pub(crate) fn operation(index: VaryingOperand, context: &mut Context) -> JsError {
183+
pub(crate) fn operation(index: IndexOperand, context: &mut Context) -> JsError {
184184
let msg = context
185185
.vm
186186
.frame()

core/engine/src/vm/opcode/define/class/getter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
builtins::function::{OrdinaryFunction, set_function_name},
66
object::internal_methods::InternalMethodPropertyContext,
77
property::PropertyDescriptor,
8-
vm::opcode::{Operation, RegisterOperand, VaryingOperand},
8+
vm::opcode::{IndexOperand, Operation, RegisterOperand},
99
};
1010

1111
/// `DefineClassStaticGetterByName` implements the Opcode Operation for `Opcode::DefineClassStaticGetterByName`
@@ -18,7 +18,7 @@ pub(crate) struct DefineClassStaticGetterByName;
1818
impl DefineClassStaticGetterByName {
1919
#[inline(always)]
2020
pub(crate) fn operation(
21-
(function, class, index): (RegisterOperand, RegisterOperand, VaryingOperand),
21+
(function, class, index): (RegisterOperand, RegisterOperand, IndexOperand),
2222
context: &mut Context,
2323
) -> JsResult<()> {
2424
let function = context.vm.get_register(function.into()).clone();
@@ -75,7 +75,7 @@ pub(crate) struct DefineClassGetterByName;
7575
impl DefineClassGetterByName {
7676
#[inline(always)]
7777
pub(crate) fn operation(
78-
(function, class_proto, index): (RegisterOperand, RegisterOperand, VaryingOperand),
78+
(function, class_proto, index): (RegisterOperand, RegisterOperand, IndexOperand),
7979
context: &mut Context,
8080
) -> JsResult<()> {
8181
let function = context.vm.get_register(function.into()).clone();

core/engine/src/vm/opcode/define/class/method.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
builtins::function::{OrdinaryFunction, set_function_name},
44
object::internal_methods::InternalMethodPropertyContext,
55
property::PropertyDescriptor,
6-
vm::opcode::{Operation, RegisterOperand, VaryingOperand},
6+
vm::opcode::{IndexOperand, Operation, RegisterOperand},
77
};
88

99
/// `DefineClassStaticMethodByName` implements the Opcode Operation for `Opcode::DefineClassStaticMethodByName`
@@ -16,7 +16,7 @@ pub(crate) struct DefineClassStaticMethodByName;
1616
impl DefineClassStaticMethodByName {
1717
#[inline(always)]
1818
pub(crate) fn operation(
19-
(function, class, index): (RegisterOperand, RegisterOperand, VaryingOperand),
19+
(function, class, index): (RegisterOperand, RegisterOperand, IndexOperand),
2020
context: &mut Context,
2121
) -> JsResult<()> {
2222
let function = context.vm.get_register(function.into()).clone();
@@ -69,7 +69,7 @@ pub(crate) struct DefineClassMethodByName;
6969
impl DefineClassMethodByName {
7070
#[inline(always)]
7171
pub(crate) fn operation(
72-
(function, class_proto, index): (RegisterOperand, RegisterOperand, VaryingOperand),
72+
(function, class_proto, index): (RegisterOperand, RegisterOperand, IndexOperand),
7373
context: &mut Context,
7474
) -> JsResult<()> {
7575
let function = context.vm.get_register(function.into()).clone();

core/engine/src/vm/opcode/define/class/setter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
builtins::function::{OrdinaryFunction, set_function_name},
66
object::internal_methods::InternalMethodPropertyContext,
77
property::PropertyDescriptor,
8-
vm::opcode::{Operation, RegisterOperand, VaryingOperand},
8+
vm::opcode::{IndexOperand, Operation, RegisterOperand},
99
};
1010

1111
/// `DefineClassStaticSetterByName` implements the Opcode Operation for `Opcode::DefineClassStaticSetterByName`
@@ -18,7 +18,7 @@ pub(crate) struct DefineClassStaticSetterByName;
1818
impl DefineClassStaticSetterByName {
1919
#[inline(always)]
2020
pub(crate) fn operation(
21-
(function, class, index): (RegisterOperand, RegisterOperand, VaryingOperand),
21+
(function, class, index): (RegisterOperand, RegisterOperand, IndexOperand),
2222
context: &mut Context,
2323
) -> JsResult<()> {
2424
let function = context.vm.get_register(function.into()).clone();
@@ -76,7 +76,7 @@ pub(crate) struct DefineClassSetterByName;
7676
impl DefineClassSetterByName {
7777
#[inline(always)]
7878
pub(crate) fn operation(
79-
(function, class_proto, index): (RegisterOperand, RegisterOperand, VaryingOperand),
79+
(function, class_proto, index): (RegisterOperand, RegisterOperand, IndexOperand),
8080
context: &mut Context,
8181
) -> JsResult<()> {
8282
let function = context.vm.get_register(function.into()).clone();

0 commit comments

Comments
 (0)