diff --git a/crates/dash_compiler/src/instruction.rs b/crates/dash_compiler/src/instruction.rs index 12427849..51257885 100755 --- a/crates/dash_compiler/src/instruction.rs +++ b/crates/dash_compiler/src/instruction.rs @@ -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); @@ -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); } @@ -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( @@ -614,19 +606,12 @@ pub enum NamedExportKind { } pub fn compile_local_load_into(out: &mut Vec, 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`. diff --git a/crates/dash_decompiler/src/decompiler.rs b/crates/dash_decompiler/src/decompiler.rs index d733ece9..d5c14e4b 100644 --- a/crates/dash_decompiler/src/decompiler.rs +++ b/crates/dash_decompiler/src/decompiler.rs @@ -73,14 +73,6 @@ 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()?; @@ -88,14 +80,6 @@ impl<'interner, 'buf> FunctionDecompiler<'interner, 'buf> { 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 { self.reader.read().ok_or(DecompileError::AbruptEof) } @@ -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 @@ -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( @@ -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(); @@ -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 diff --git a/crates/dash_llvm_jit_backend/src/codegen/mod.rs b/crates/dash_llvm_jit_backend/src/codegen/mod.rs index eca4a526..7013a1b5 100644 --- a/crates/dash_llvm_jit_backend/src/codegen/mod.rs +++ b/crates/dash_llvm_jit_backend/src/codegen/mod.rs @@ -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 => { diff --git a/crates/dash_middle/src/compiler/instruction.rs b/crates/dash_middle/src/compiler/instruction.rs index 9f205cef..700f6b2d 100644 --- a/crates/dash_middle/src/compiler/instruction.rs +++ b/crates/dash_middle/src/compiler/instruction.rs @@ -20,7 +20,6 @@ pub enum Instruction { Ne, Pop, LdLocal, - LdLocalW, LdGlobal, String, Boolean, @@ -38,9 +37,7 @@ pub enum Instruction { BitNot, Not, StoreLocal, - StoreLocalW, StoreGlobal, - StoreGlobalW, Ret, Call, JmpFalseP, @@ -48,17 +45,14 @@ pub enum Instruction { 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, diff --git a/crates/dash_typed_cfg/src/passes/type_infer.rs b/crates/dash_typed_cfg/src/passes/type_infer.rs index 543ab641..d7401c54 100644 --- a/crates/dash_typed_cfg/src/passes/type_infer.rs +++ b/crates/dash_typed_cfg/src/passes/type_infer.rs @@ -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); @@ -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(); diff --git a/crates/dash_typed_cfg/src/util.rs b/crates/dash_typed_cfg/src/util.rs index b745387d..16baa785 100644 --- a/crates/dash_typed_cfg/src/util.rs +++ b/crates/dash_typed_cfg/src/util.rs @@ -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(); } diff --git a/crates/dash_vm/src/dispatch.rs b/crates/dash_vm/src/dispatch.rs index 53d5b813..bd1d7431 100755 --- a/crates/dash_vm/src/dispatch.rs +++ b/crates/dash_vm/src/dispatch.rs @@ -902,8 +902,8 @@ mod handlers { } pub fn storeglobal(mut cx: DispatchContext<'_>) -> Result, 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 { @@ -1363,7 +1363,7 @@ mod handlers { } pub fn storelocal(mut cx: DispatchContext<'_>) -> Result, 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 { @@ -1427,7 +1427,7 @@ mod handlers { } pub fn ldlocal(mut cx: DispatchContext<'_>) -> Result, 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); @@ -1479,8 +1479,8 @@ mod handlers { } pub fn arraylit(mut cx: DispatchContext<'_>) -> Result, 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)? @@ -1758,7 +1758,7 @@ mod handlers { } pub fn ldlocalext(mut cx: DispatchContext<'_>) -> Result, 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 @@ -1773,7 +1773,7 @@ mod handlers { } pub fn storelocalext(mut cx: DispatchContext<'_>) -> Result, 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 { @@ -2405,6 +2405,5 @@ pub fn handle(vm: &mut Vm, instruction: Instruction) -> Result handlers::assign_properties(cx), Instruction::DelayedReturn => handlers::delayed_ret(cx), Instruction::Nop => Ok(None), - _ => unimplemented!("{:?}", instruction), } }