From dada0cb779fa0f342b3789a6c2c24522388d68db Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Sat, 1 Jul 2023 17:12:41 +1000 Subject: [PATCH] Extract constants later so that simplify has more information --- .../src/optimization/optimize.rs | 6 +++++- valuescript_vm/src/bytecode_stack_frame.rs | 20 +++++++++++++------ valuescript_vm/src/cat_stack_frame.rs | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/valuescript_compiler/src/optimization/optimize.rs b/valuescript_compiler/src/optimization/optimize.rs index db604e8..c44d8b1 100644 --- a/valuescript_compiler/src/optimization/optimize.rs +++ b/valuescript_compiler/src/optimization/optimize.rs @@ -10,9 +10,13 @@ use super::simplify::simplify; pub fn optimize(module: &mut Module, pointer_allocator: &mut NameAllocator) { collapse_pointers_of_pointers(module); - extract_constants(module, pointer_allocator); shake_tree(module); simplify(module); remove_noops(module); remove_meta_lines(module); + extract_constants(module, pointer_allocator); + + // After possibly repeated optimization, this ensures that the pointers are ordered correctly. + // TODO: Consider a dedicated step that's only about pointer ordering and not tree shaking. + shake_tree(module); } diff --git a/valuescript_vm/src/bytecode_stack_frame.rs b/valuescript_vm/src/bytecode_stack_frame.rs index 325ce8e..c555444 100644 --- a/valuescript_vm/src/bytecode_stack_frame.rs +++ b/valuescript_vm/src/bytecode_stack_frame.rs @@ -561,12 +561,20 @@ impl StackFrameTrait for BytecodeStackFrame { } Cat => { - assert!( - self.decoder.decode_type() == BytecodeType::Array, - "TODO: cat non-inline arrays" - ); - - let cat_frame = CatStackFrame::from_args(self.decoder.decode_vec_val(&mut self.registers)); + let cat_frame = match self.decoder.peek_type() { + BytecodeType::Array => { + self.decoder.decode_type(); + CatStackFrame::from_vec_val(self.decoder.decode_vec_val(&mut self.registers)) + } + _ => match self.decoder.decode_val(&mut self.registers) { + Val::Array(array) => CatStackFrame::from_vec_val(array.elements.clone()), + _ => { + return Err( + "TODO: cat instruction on non-array (usually type error)".to_internal_error(), + ) + } + }, + }; self.this_target = None; self.return_target = self.decoder.decode_register_index(); diff --git a/valuescript_vm/src/cat_stack_frame.rs b/valuescript_vm/src/cat_stack_frame.rs index 32a7b7f..299b897 100644 --- a/valuescript_vm/src/cat_stack_frame.rs +++ b/valuescript_vm/src/cat_stack_frame.rs @@ -27,7 +27,7 @@ pub enum CatFrameState { } impl CatStackFrame { - pub fn from_args(args: Vec) -> Self { + pub fn from_vec_val(args: Vec) -> Self { Self { state: CatFrameState::ReadNext, iter_result: None,