From 96832ed1e391b74f48e579e20fdc4088fbf47f35 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Sat, 30 Oct 2021 18:22:09 +0100 Subject: [PATCH] self review --- compiler/gen_wasm/src/backend.rs | 20 ++++--- compiler/gen_wasm/src/code_builder.rs | 4 +- .../tests/helpers/wasm32_test_result.rs | 54 +++++++++---------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 3878185cdf..3b169540c0 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -395,14 +395,18 @@ impl<'a> WasmBackend<'a> { self.storage.load_symbols(&mut self.code_builder, wasm_args); - // Index of the called function in the Wasm module code section - // TODO: update when we start inlining functions (remember we emit procs out of order) - let func_index = self - .proc_symbols - .iter() - .position(|s| s == func_sym) - .map(|i| i as u32) - .unwrap_or(u32::MAX); // foreign symbol, updated at link time + // Index of the called function in the code section + // TODO: account for inlined functions when we start doing that (remember we emit procs out of order) + let func_index = match self.proc_symbols.iter().position(|s| s == func_sym) { + Some(i) => i as u32, + None => { + // TODO: actually useful linking! Push a relocation for it. + return Err(format!( + "Not yet supporteed: calling foreign function {:?}", + func_sym + )); + } + }; // Index of the function's name in the symbol table let symbol_index = func_index; // TODO: update this when we add other things to the symbol table diff --git a/compiler/gen_wasm/src/code_builder.rs b/compiler/gen_wasm/src/code_builder.rs index bcc5b020f5..03555fa415 100644 --- a/compiler/gen_wasm/src/code_builder.rs +++ b/compiler/gen_wasm/src/code_builder.rs @@ -141,8 +141,8 @@ pub struct CodeBuilder<'a> { /// Keeps track of where Symbol values are in the VM stack vm_stack: Vec<'a, Symbol>, - /// Which byte offsets in the code section correspond to which symbols. - /// e.g. Function indices may change when we link Roc + builtins + platform + /// Linker info to help combine the Roc module with builtin & platform modules, + /// e.g. to modify call instructions when function indices change relocations: Vec<'a, RelocationEntry>, } diff --git a/compiler/gen_wasm/tests/helpers/wasm32_test_result.rs b/compiler/gen_wasm/tests/helpers/wasm32_test_result.rs index 7f2046b458..dfd78b88fb 100644 --- a/compiler/gen_wasm/tests/helpers/wasm32_test_result.rs +++ b/compiler/gen_wasm/tests/helpers/wasm32_test_result.rs @@ -12,7 +12,7 @@ pub trait Wasm32TestResult { module_builder: &mut builder::ModuleBuilder, code_section_bytes: &mut std::vec::Vec, wrapper_name: &str, - main_fn_index: u32, + main_function_index: u32, ) { let signature = builder::signature() .with_result(parity_wasm::elements::ValueType::I32) @@ -28,7 +28,7 @@ pub trait Wasm32TestResult { module_builder.push_export(export); let mut code_builder = CodeBuilder::new(arena); - Self::build_wrapper_body(&mut code_builder, main_fn_index); + Self::build_wrapper_body(&mut code_builder, main_function_index); code_builder.serialize(code_section_bytes); @@ -41,12 +41,12 @@ pub trait Wasm32TestResult { code_section_bytes.overwrite_padded_u32(5, num_procs + 1); } - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32); + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32); } macro_rules! build_wrapper_body_primitive { ($store_instruction: ident, $align: expr) => { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { let frame_pointer_id = LocalId(0); let frame_pointer = Some(frame_pointer_id); let local_types = &[ValueType::I32]; @@ -54,7 +54,7 @@ macro_rules! build_wrapper_body_primitive { code_builder.get_local(frame_pointer_id); // Raw "call" instruction. Don't bother with symbol & relocation since we're not going to link. - code_builder.inst_imm32(roc_gen_wasm::opcodes::CALL, 0, true, main_fn_index); + code_builder.inst_imm32(roc_gen_wasm::opcodes::CALL, 0, true, main_function_index); code_builder.$store_instruction($align, 0); code_builder.get_local(frame_pointer_id); @@ -73,7 +73,7 @@ macro_rules! wasm_test_result_primitive { fn build_wrapper_body_stack_memory( code_builder: &mut CodeBuilder, - main_fn_index: u32, + main_function_index: u32, size: usize, ) { let local_id = LocalId(0); @@ -82,7 +82,7 @@ fn build_wrapper_body_stack_memory( code_builder.get_local(local_id); // Raw "call" instruction. Don't bother with symbol & relocation since we're not going to link. - code_builder.inst_imm32(roc_gen_wasm::opcodes::CALL, 0, true, main_fn_index); + code_builder.inst_imm32(roc_gen_wasm::opcodes::CALL, 0, true, main_function_index); code_builder.get_local(local_id); code_builder.finalize(local_types, size as i32, frame_pointer); } @@ -90,10 +90,10 @@ fn build_wrapper_body_stack_memory( macro_rules! wasm_test_result_stack_memory { ($type_name: ident) => { impl Wasm32TestResult for $type_name { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, $type_name::ACTUAL_WIDTH, ) } @@ -122,8 +122,8 @@ wasm_test_result_stack_memory!(RocDec); wasm_test_result_stack_memory!(RocStr); impl Wasm32TestResult for RocList { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { - build_wrapper_body_stack_memory(code_builder, main_fn_index, 12) + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { + build_wrapper_body_stack_memory(code_builder, main_function_index, 12) } } @@ -135,8 +135,8 @@ impl Wasm32TestResult for [T; N] where T: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { - build_wrapper_body_stack_memory(code_builder, main_fn_index, N * T::ACTUAL_WIDTH) + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { + build_wrapper_body_stack_memory(code_builder, main_function_index, N * T::ACTUAL_WIDTH) } } @@ -145,10 +145,10 @@ where T: Wasm32TestResult + FromWasm32Memory, U: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH, ) } @@ -160,10 +160,10 @@ where U: Wasm32TestResult + FromWasm32Memory, V: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH, ) } @@ -176,10 +176,10 @@ where V: Wasm32TestResult + FromWasm32Memory, W: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH, ) } @@ -193,10 +193,10 @@ where W: Wasm32TestResult + FromWasm32Memory, X: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH + X::ACTUAL_WIDTH, ) } @@ -211,10 +211,10 @@ where X: Wasm32TestResult + FromWasm32Memory, Y: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH @@ -235,10 +235,10 @@ where Y: Wasm32TestResult + FromWasm32Memory, Z: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH @@ -261,10 +261,10 @@ where Z: Wasm32TestResult + FromWasm32Memory, A: Wasm32TestResult + FromWasm32Memory, { - fn build_wrapper_body(code_builder: &mut CodeBuilder, main_fn_index: u32) { + fn build_wrapper_body(code_builder: &mut CodeBuilder, main_function_index: u32) { build_wrapper_body_stack_memory( code_builder, - main_fn_index, + main_function_index, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH