Skip to content

Commit

Permalink
remove last wide instrs
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 31, 2024
1 parent 56fdbeb commit 0defa5b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 99 deletions.
43 changes: 14 additions & 29 deletions crates/dash_compiler/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ impl<'cx, 'interner> InstructionBuilder<'cx, 'interner> {
Ok(())
}

// pub fn build_boolean_constant(&mut self, b: bool) -> Result<

pub fn build_global_load(&mut self, ident: Symbol) -> Result<(), LimitExceededError> {
let SymbolConstant(id) = self.current_function_mut().cp.add_symbol(ident)?;
self.write_instr(Instruction::LdGlobal);
Expand All @@ -171,19 +169,19 @@ impl<'cx, 'interner> InstructionBuilder<'cx, 'interner> {

pub fn build_global_store(&mut self, kind: AssignKind, ident: Symbol) -> Result<(), LimitExceededError> {
let SymbolConstant(id) = self.current_function_mut().cp.add_symbol(ident)?;
self.write_wide_instr(Instruction::StoreGlobal, Instruction::StoreGlobalW, id);
self.write_instr(Instruction::StoreGlobal);
self.writew(id);
self.write(kind as u8);
Ok(())
}

pub fn build_local_store(&mut self, kind: AssignKind, id: u16, is_extern: bool) {
let (thin, wide) = if is_extern {
(Instruction::StoreLocalExt, Instruction::StoreLocalExtW)
if is_extern {
self.write_instr(Instruction::StoreLocalExt);
} else {
(Instruction::StoreLocal, Instruction::StoreLocalW)
};

self.write_wide_instr(thin, wide, id);
self.write_instr(Instruction::StoreLocal);
}
self.writew(id);
self.write(kind as u8);
}

Expand Down Expand Up @@ -274,15 +272,9 @@ impl<'cx, 'interner> InstructionBuilder<'cx, 'interner> {
}

pub fn build_arraylit(&mut self, len: u16, stack_values: u16) {
if let (Ok(len), Ok(stack_values)) = (u8::try_from(len), u8::try_from(stack_values)) {
self.write_instr(Instruction::ArrayLit);
self.write(len);
self.write(stack_values);
} else {
self.write_instr(Instruction::ArrayLitW);
self.writew(len);
self.writew(stack_values);
}
self.write_instr(Instruction::ArrayLit);
self.writew(len);
self.writew(stack_values);
}

pub fn build_object_member_like_instruction(
Expand Down Expand Up @@ -614,19 +606,12 @@ pub enum NamedExportKind {
}

pub fn compile_local_load_into(out: &mut Vec<u8>, index: u16, is_extern: bool) {
let (thin, wide) = if is_extern {
(Instruction::LdLocalExt, Instruction::LdLocalExtW)
} else {
(Instruction::LdLocal, Instruction::LdLocalW)
};

if let Ok(index) = u8::try_from(index) {
out.push(thin as u8);
out.push(index);
if is_extern {
out.push(Instruction::LdLocalExt as u8);
} else {
out.push(wide as u8);
out.extend_from_slice(&index.to_ne_bytes());
out.push(Instruction::LdLocal as u8);
}
out.extend_from_slice(&index.to_ne_bytes());
}

/// Convenience function for creating a vec and calling `compile_local_load_into`.
Expand Down
48 changes: 13 additions & 35 deletions crates/dash_decompiler/src/decompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,13 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
Ok(())
}

/// Handles an opcode with a single argument that is in the following bytecode.
fn handle_inc_op_instr2(&mut self, name: &str) -> Result<(), DecompileError> {
let b = self.read()?;
let b2 = self.read()?;
self.handle_op_instr(name, &[&b, &b2]);
Ok(())
}

/// Handles an opcode with a single wide argument that is in the following bytecode.
fn handle_incw_op_instr(&mut self, name: &str) -> Result<(), DecompileError> {
let b = self.read_u16()?;
self.handle_op_instr(name, &[&b]);
Ok(())
}

/// Handles an opcode with a single wide argument that is in the following bytecode.
fn handle_incw_op_instr2(&mut self, name: &str) -> Result<(), DecompileError> {
let b = self.read_u16()?;
let b2 = self.read()?;
self.handle_op_instr(name, &[&b, &b2]);
Ok(())
}

fn read(&mut self) -> Result<u8, DecompileError> {
self.reader.read().ok_or(DecompileError::AbruptEof)
}
Expand Down Expand Up @@ -176,7 +160,6 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
// TODO: use debug symbols to find the name
self.handle_op_instr("ldlocal", &[&b]);
}
Instruction::LdLocalW => self.handle_incw_op_instr("ldlocalw")?,
Instruction::Jmp
| Instruction::JmpFalseNP
| Instruction::JmpFalseP
Expand Down Expand Up @@ -214,8 +197,11 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
.to_owned();
self.handle_op_instr("ldglobal", &[&ident]);
}
Instruction::StoreLocal => self.handle_inc_op_instr2("storelocal")?,
Instruction::StoreLocalW => self.handle_inc_op_instr2("storelocalw")?,
Instruction::StoreLocal => {
let id = self.read_u16()?;
let kind = self.read()?;
self.handle_op_instr("storelocal", &[&id, &kind]);
}
Instruction::Call => {
let meta = FunctionCallMetadata::from(self.read()?);
self.handle_op_map_instr(
Expand Down Expand Up @@ -254,29 +240,19 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
Instruction::BitNot => self.handle_opless_instr("bitnot"),
Instruction::Not => self.handle_opless_instr("not"),
Instruction::StoreGlobal => {
let b = self.read()?;
let _kind = self.read()?;
let ident = self
.interner
.resolve(self.constants.symbols[SymbolConstant(b as u16)])
.to_owned();
self.handle_op_instr("storeglobal", &[&ident]);
}
Instruction::StoreGlobalW => {
let b = self.read_u16()?;
let _kind = self.read()?;
let ident = self
.interner
.resolve(self.constants.symbols[SymbolConstant(b)])
.to_owned();
self.handle_op_instr("storeglobalw", &[&ident]);
self.handle_op_instr("storeglobal", &[&ident]);
}
Instruction::DynamicPropAccess => {
let b = self.read()?;
self.handle_op_map_instr("dynamicpropaccess", &[("preserve_this", &(b == 1))])
}
Instruction::ArrayLit => self.handle_inc_op_instr("arraylit")?,
Instruction::ArrayLitW => self.handle_incw_op_instr("arraylitw")?,
Instruction::ArrayLit => self.handle_incw_op_instr("arraylit")?,
Instruction::ObjLit => {
let len = self.read()?;
let mut props = Vec::new();
Expand Down Expand Up @@ -320,10 +296,12 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> {
let _k = self.read()?;
self.handle_opless_instr("dynamicpropset")
}
Instruction::LdLocalExt => self.handle_inc_op_instr("ldlocalext")?,
Instruction::LdLocalExtW => self.handle_incw_op_instr("ldlocalextw")?,
Instruction::StoreLocalExt => self.handle_inc_op_instr2("storelocalext")?,
Instruction::StoreLocalExtW => self.handle_incw_op_instr2("storelocalextw")?,
Instruction::LdLocalExt => self.handle_incw_op_instr("ldlocalext")?,
Instruction::StoreLocalExt => {
let id = self.read_u16()?;
let kind = self.read()?;
self.handle_op_instr("storelocalext", &[&id, &kind]);
}
Instruction::StrictEq => self.handle_opless_instr("stricteq"),
Instruction::StrictNe => self.handle_opless_instr("strictne"),
Instruction::Try => self.handle_incw_op_instr("try")?, // TODO: show @offset like in JMP
Expand Down
6 changes: 3 additions & 3 deletions crates/dash_llvm_jit_backend/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ impl<'a, 'q, Q: CodegenQuery> CodegenCtxt<'a, 'q, Q> {
stack.push(val);
}
Instruction::StoreLocal => {
let id = dcx.next_byte();
let id = dcx.next_wide();
let kind = AssignKind::from_repr(dcx.next_byte()).unwrap();
assert_eq!(kind, AssignKind::Assignment);
let value = stack.pop();
self.store_local(id.into(), &value);
let value = self.load_local(id.into());
self.store_local(id, &value);
let value = self.load_local(id);
stack.push(value);
}
Instruction::Boolean | Instruction::Number => {
Expand Down
6 changes: 0 additions & 6 deletions crates/dash_middle/src/compiler/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum Instruction {
Ne,
Pop,
LdLocal,
LdLocalW,
LdGlobal,
String,
Boolean,
Expand All @@ -38,27 +37,22 @@ pub enum Instruction {
BitNot,
Not,
StoreLocal,
StoreLocalW,
StoreGlobal,
StoreGlobalW,
Ret,
Call,
JmpFalseP,
Jmp,
StaticPropAccess,
DynamicPropAccess,
ArrayLit,
ArrayLitW,
ObjLit,
This,
StaticPropAssign,
DynamicPropAssign,
/// Loads an external variable
LdLocalExt,
LdLocalExtW,
/// Stores a value into an external variable
StoreLocalExt,
StoreLocalExtW,
StrictEq,
StrictNe,
Try,
Expand Down
16 changes: 4 additions & 12 deletions crates/dash_typed_cfg/src/passes/type_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ impl<'a, 'q, Q: TypeInferQuery> TypeInferCtxt<'a, 'q, Q> {
ty_stack.push(Type::Boolean);
}
Instruction::Pop => drop(ty_stack.pop()),
Instruction::LdLocal | Instruction::LdLocalW => {
let index = match instr {
Instruction::LdLocal => dcx.next_byte().into(),
Instruction::LdLocalW => dcx.next_wide(),
_ => unreachable!(),
};
Instruction::LdLocal => {
let index = dcx.next_byte().into();

let ty = self.get_or_insert_local_ty(index);
ty_stack.push(ty);
Expand All @@ -134,12 +130,8 @@ impl<'a, 'q, Q: TypeInferQuery> TypeInferCtxt<'a, 'q, Q> {
| Instruction::Function => {
todo!("unimplemented constant type: {instr:?}")
}
Instruction::StoreLocal | Instruction::StoreLocalW => {
let index = match instr {
Instruction::StoreLocal => dcx.next_byte().into(),
Instruction::StoreLocalW => dcx.next_wide(),
_ => unreachable!(),
};
Instruction::StoreLocal => {
let index = dcx.next_wide();
let _kind = dcx.next_byte();

let ty = ty_stack.pop();
Expand Down
5 changes: 0 additions & 5 deletions crates/dash_typed_cfg/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ impl<'a> DecodeCtxt<'a> {
| Instruction::Undefined
| Instruction::Function => drop(self.next_byte()),
Instruction::LdLocal => drop(self.next_byte()),
Instruction::LdLocalW => drop(self.next_wide()),
Instruction::StoreLocal => {
self.next_byte();
self.next_byte();
}
Instruction::StoreLocalW => {
self.next_wide();
self.next_byte();
}
Expand Down
17 changes: 8 additions & 9 deletions crates/dash_vm/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ mod handlers {
}

pub fn storeglobal(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let id = cx.fetch_and_inc_ip();
let name = JsString::from(cx.constants().symbols[SymbolConstant(id.into())]);
let id = cx.fetchw_and_inc_ip();
let name = JsString::from(cx.constants().symbols[SymbolConstant(id)]);
let kind = AssignKind::from_repr(cx.fetch_and_inc_ip()).unwrap();

macro_rules! op {
Expand Down Expand Up @@ -1363,7 +1363,7 @@ mod handlers {
}

pub fn storelocal(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let id = cx.fetch_and_inc_ip() as usize;
let id = cx.fetchw_and_inc_ip() as usize;
let kind = AssignKind::from_repr(cx.fetch_and_inc_ip()).unwrap();

macro_rules! op {
Expand Down Expand Up @@ -1427,7 +1427,7 @@ mod handlers {
}

pub fn ldlocal(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let id = cx.fetch_and_inc_ip();
let id = cx.fetchw_and_inc_ip();
let value = cx.get_local(id.into());

cx.stack.push(value);
Expand Down Expand Up @@ -1479,8 +1479,8 @@ mod handlers {
}

pub fn arraylit(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let len = cx.fetch_and_inc_ip() as usize;
let stack_values = cx.fetch_and_inc_ip() as usize;
let len = cx.fetchw_and_inc_ip() as usize;
let stack_values = cx.fetchw_and_inc_ip() as usize;
// Split up into two functions as a non-holey array literal can be evaluated more efficiently
let array = if len == stack_values {
arraylit_dense(&mut cx, len)?
Expand Down Expand Up @@ -1758,7 +1758,7 @@ mod handlers {
}

pub fn ldlocalext(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let id = cx.fetch_and_inc_ip();
let id = cx.fetchw_and_inc_ip();
let value = Value::external(cx.get_external(id.into()).id());

// Unbox external values such that any use will create a copy
Expand All @@ -1773,7 +1773,7 @@ mod handlers {
}

pub fn storelocalext(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
let id = cx.fetch_and_inc_ip();
let id = cx.fetchw_and_inc_ip();
let kind = AssignKind::from_repr(cx.fetch_and_inc_ip()).unwrap();

macro_rules! op {
Expand Down Expand Up @@ -2405,6 +2405,5 @@ pub fn handle(vm: &mut Vm, instruction: Instruction) -> Result<Option<HandleResu
Instruction::AssignProperties => handlers::assign_properties(cx),
Instruction::DelayedReturn => handlers::delayed_ret(cx),
Instruction::Nop => Ok(None),
_ => unimplemented!("{:?}", instruction),
}
}

0 comments on commit 0defa5b

Please sign in to comment.