Skip to content

Commit

Permalink
Implement push f32 value (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored Aug 4, 2023
1 parent 15e5dc4 commit 8bcb4f8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
10 changes: 8 additions & 2 deletions boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,14 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
if f64::from(value as i32).to_bits() == value.to_bits() {
self.emit_push_integer(value as i32);
} else {
self.emit_opcode(Opcode::PushRational);
self.emit_u64(value.to_bits());
let f32_value = value as f32;

#[allow(clippy::float_cmp)]
if f64::from(f32_value) == value {
self.emit(Opcode::PushFloat, &[Operand::U32(f32_value.to_bits())]);
} else {
self.emit(Opcode::PushDouble, &[Operand::U64(value.to_bits())]);
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ impl CodeBlock {
*pc += size_of::<i32>();
result
}
Opcode::PushRational => {
Opcode::PushFloat => {
let operand = self.read::<f32>(*pc);
*pc += size_of::<f32>();
ryu_js::Buffer::new().format(operand).to_string()
}
Opcode::PushDouble => {
let operand = self.read::<f64>(*pc);
*pc += size_of::<f64>();
ryu_js::Buffer::new().format(operand).to_string()
Expand Down Expand Up @@ -676,8 +681,7 @@ impl CodeBlock {
| Opcode::Reserved55
| Opcode::Reserved56
| Opcode::Reserved57
| Opcode::Reserved58
| Opcode::Reserved59 => unreachable!("Reserved opcodes are unrechable"),
| Opcode::Reserved58 => unreachable!("Reserved opcodes are unrechable"),
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::PushRational => {
Opcode::PushFloat => {
pc += size_of::<f32>();

graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::PushDouble => {
pc += size_of::<f64>();

graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
Expand Down Expand Up @@ -611,8 +617,7 @@ impl CodeBlock {
| Opcode::Reserved55
| Opcode::Reserved56
| Opcode::Reserved57
| Opcode::Reserved58
| Opcode::Reserved59 => unreachable!("Reserved opcodes are unrechable"),
| Opcode::Reserved58 => unreachable!("Reserved opcodes are unrechable"),
}
}

Expand Down
11 changes: 8 additions & 3 deletions boa_engine/src/vm/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ generate_impl! {
/// Stack: **=>** value
PushInt32,

/// Push `f32` value on the stack.
///
/// Operands: value: `f32`
///
/// Stack: **=>** value
PushFloat,

/// Push `f64` value on the stack.
///
/// Operands: value: `f64`
///
/// Stack: **=>** value
PushRational,
PushDouble,

/// Push `NaN` integer on the stack.
///
Expand Down Expand Up @@ -1788,8 +1795,6 @@ generate_impl! {
Reserved57 => Reserved,
/// Reserved [`Opcode`].
Reserved58 => Reserved,
/// Reserved [`Opcode`].
Reserved59 => Reserved,
}
}

Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/vm/opcode/push/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ implement_push_numbers_with_conversion!(PushInt8, i8, "Push `i8` value on the st
implement_push_numbers_with_conversion!(PushInt16, i16, "Push `i16` value on the stack");

implement_push_numbers_no_conversion!(PushInt32, i32, "Push `i32` value on the stack");
implement_push_numbers_no_conversion!(PushRational, f64, "Push `f64` value on the stack");
implement_push_numbers_no_conversion!(PushFloat, f32, "Push `f64` value on the stack");
implement_push_numbers_no_conversion!(PushDouble, f64, "Push `f64` value on the stack");

0 comments on commit 8bcb4f8

Please sign in to comment.