Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 3, 2024
1 parent 2e34385 commit 772b766
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
23 changes: 14 additions & 9 deletions core/engine/src/bytecompiler/expression/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,21 @@ impl ByteCompiler<'_> {
}

if let Some(early_exit) = early_exit {
let exit = self.emit_opcode_with_operand(Opcode::Jump);

self.patch_jump(early_exit);
self.push_from_register(&lhs);

for _ in 0..pop_count {
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::Pop);
if pop_count == 0 {
self.patch_jump(early_exit);
self.push_from_register(&lhs);
} else {
let exit = self.emit_opcode_with_operand(Opcode::Jump);

self.patch_jump(early_exit);
self.push_from_register(&lhs);

for _ in 0..pop_count {
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::Pop);
}
self.patch_jump(exit);
}
self.patch_jump(exit);
}

self.register_allocator.dealloc(lhs);
Expand Down
84 changes: 65 additions & 19 deletions core/engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub(crate) enum Operand2<'a> {
pub(crate) enum InstructionOperand<'a> {
Register(&'a Reg),
Argument(u32),
Constant(u32),
Constant(i32),
}

/// The [`ByteCompiler`] is used to compile ECMAScript AST from [`boa_ast`] to bytecode.
Expand Down Expand Up @@ -634,19 +634,36 @@ impl<'ctx> ByteCompiler<'ctx> {

has_operand_types = true;

let (value, type_) = match operand {
InstructionOperand::Register(reg) => (reg.index(), 0b0000_0000),
InstructionOperand::Argument(index) => (index, 0b0000_0001),
InstructionOperand::Constant(value) => (value, 0b0000_0010),
let type_ = match operand {
InstructionOperand::Register(reg) => {
if u8::try_from(reg.index()).is_ok() {
} else if u16::try_from(reg.index()).is_ok() {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U16);
} else {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U32);
}
0b0000_0000
}
InstructionOperand::Argument(index) => {
if u8::try_from(index).is_ok() {
} else if u16::try_from(index).is_ok() {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U16);
} else {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U32);
}
0b0000_0001
}
InstructionOperand::Constant(value) => {
if i8::try_from(value).is_ok() {
} else if i16::try_from(value).is_ok() {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U16);
} else {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U32);
}
0b0000_0010
}
};

if u8::try_from(value).is_ok() {
} else if u16::try_from(value).is_ok() {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U16);
} else {
varying_kind = std::cmp::max(varying_kind, VaryingOperandKind::U32);
}

operand_types <<= 2;
operand_types |= type_;
}
Expand Down Expand Up @@ -680,13 +697,17 @@ impl<'ctx> ByteCompiler<'ctx> {
Operand2::U32(v) => self.emit_u32(v),
Operand2::I64(v) => self.emit_i64(v),
Operand2::U64(v) => self.emit_u64(v),
Operand2::Varying(v)
| Operand2::Operand(
InstructionOperand::Argument(v) | InstructionOperand::Constant(v),
) => match varying_kind {
VaryingOperandKind::U8 => self.emit_u8(v as u8),
VaryingOperandKind::U16 => self.emit_u16(v as u16),
VaryingOperandKind::U32 => self.emit_u32(v),
Operand2::Varying(v) | Operand2::Operand(InstructionOperand::Argument(v)) => {
match varying_kind {
VaryingOperandKind::U8 => self.emit_u8(v as u8),
VaryingOperandKind::U16 => self.emit_u16(v as u16),
VaryingOperandKind::U32 => self.emit_u32(v),
}
}
Operand2::Operand(InstructionOperand::Constant(v)) => match varying_kind {
VaryingOperandKind::U8 => self.emit_i8(v as i8),
VaryingOperandKind::U16 => self.emit_i16(v as i16),
VaryingOperandKind::U32 => self.emit_i32(v),
},
Operand2::Register(reg) | Operand2::Operand(InstructionOperand::Register(reg)) => {
let v = reg.index();
Expand Down Expand Up @@ -714,6 +735,16 @@ impl<'ctx> ByteCompiler<'ctx> {
fn push_from_register(&mut self, src: &Reg) {
self.emit2(Opcode::PushFromRegister, &[Operand2::Register(src)]);
}
/// TODO: Temporary function, remove once transition is complete.
fn push_from_operand(&mut self, src: InstructionOperand<'_>) {
match src {
InstructionOperand::Register(reg) => self.push_from_register(reg),
InstructionOperand::Argument(index) => {
self.emit_with_varying_operand(Opcode::GetArgument, index);
}
InstructionOperand::Constant(value) => self.emit_push_integer(value),
}
}

/// Emits an opcode with one varying operand.
///
Expand Down Expand Up @@ -1238,6 +1269,21 @@ impl<'ctx> ByteCompiler<'ctx> {
self.compile_expr_impl(expr, use_expr);
}

// The function should take an optional prefered reg
// Should output

/// Compile an [`Expression`].
#[inline]
pub(crate) fn compile_expr2<'a>(
&mut self,
expr: &Expression,
reg: &'a Reg,
) -> InstructionOperand<'a> {
self.compile_expr_impl(expr, true);
self.pop_into_register(reg);
InstructionOperand::Register(reg)
}

/// Compile a property access expression, prepending `this` to the property value in the stack.
///
/// This compiles the access in a way that the state of the stack after executing the property
Expand Down

0 comments on commit 772b766

Please sign in to comment.