From f33dbcc827fabb12b7b88dfc0b1d82c6f1aabb4b Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Mon, 5 Jul 2021 16:30:25 +0200 Subject: [PATCH] Redesign bytecode virtual machine (#1361) - Implemented Jump and JumpIfFalse opcodes - Implemented If in vm - Implement assign to identifiers - Implement JumpIfTrue opcode - Implemented short circuit operators - Implement while loop - Implement do-while loop - Pop expressions - Split compilation for epression and statement - simplify boolean compilation - Implement loop break and continue with labels - Implement getting property from object - Implement setting object properties - Implement binary assign - Implement throw - Split variable names from literals - Implement conditional operator - Implement array expressions without spread - Implement some unary operators - Add accessor types - Implement this - Add opcode for pushing rationals - Implement instanceof operator --- boa/src/bytecompiler.rs | 693 +++++++++++++++++ boa/src/context.rs | 16 +- boa/src/exec/mod.rs | 2 - boa/src/lib.rs | 6 +- boa/src/object/internal_methods.rs | 10 +- boa/src/syntax/ast/node/block/mod.rs | 2 - boa/src/syntax/ast/node/identifier/mod.rs | 12 - .../ast/node/iteration/do_while_loop/mod.rs | 2 - .../ast/node/iteration/for_in_loop/mod.rs | 2 - .../syntax/ast/node/iteration/for_loop/mod.rs | 2 - .../ast/node/iteration/for_of_loop/mod.rs | 2 - .../ast/node/iteration/while_loop/mod.rs | 14 +- boa/src/syntax/ast/node/mod.rs | 1 + boa/src/syntax/ast/node/object/mod.rs | 17 - .../syntax/ast/node/operator/bin_op/mod.rs | 56 -- .../syntax/ast/node/operator/unary_op/mod.rs | 27 - boa/src/syntax/ast/node/statement_list/mod.rs | 16 - boa/src/syntax/ast/node/switch/mod.rs | 2 - boa/src/value/mod.rs | 12 + boa/src/vm/code_block.rs | 215 ++++++ boa/src/vm/compilation.rs | 112 --- boa/src/vm/instructions.rs | 127 --- boa/src/vm/mod.rs | 727 +++++++++++------- boa/src/vm/opcode.rs | 614 +++++++++++++++ docs/vm.md | 77 +- 25 files changed, 2046 insertions(+), 720 deletions(-) create mode 100644 boa/src/bytecompiler.rs create mode 100644 boa/src/vm/code_block.rs delete mode 100644 boa/src/vm/compilation.rs delete mode 100644 boa/src/vm/instructions.rs create mode 100644 boa/src/vm/opcode.rs diff --git a/boa/src/bytecompiler.rs b/boa/src/bytecompiler.rs new file mode 100644 index 0000000000..9314215084 --- /dev/null +++ b/boa/src/bytecompiler.rs @@ -0,0 +1,693 @@ +use crate::{ + syntax::ast::{ + node::{GetConstField, GetField, Identifier, StatementList}, + op::{AssignOp, BinOp, BitOp, CompOp, LogOp, NumOp, UnaryOp}, + Const, Node, + }, + value::{RcBigInt, RcString}, + vm::{CodeBlock, Opcode}, + Value, +}; + +use std::collections::HashMap; + +#[inline] +fn u16_to_array(value: u16) -> [u8; 2] { + // Safety: Transmuting a `u16` primitive to + // an array of 2 bytes is safe. + unsafe { std::mem::transmute(value) } +} + +#[inline] +fn u32_to_array(value: u32) -> [u8; 4] { + // Safety: Transmuting a `u32` primitive to + // an array of 4 bytes is safe. + unsafe { std::mem::transmute(value) } +} + +#[inline] +fn u64_to_array(value: u64) -> [u8; 8] { + // Safety: Transmuting a `u64` primitive to + // an array of 8 bytes is safe. + unsafe { std::mem::transmute(value) } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +enum Literal { + String(RcString), + BigInt(RcBigInt), +} + +#[must_use] +#[derive(Debug, Clone, Copy)] +struct Label { + index: u32, +} + +#[derive(Debug, Clone)] +struct LoopControlInfo { + label: Option>, + loop_start: u32, + continues: Vec