self review

This commit is contained in:
Brian Carroll 2021-10-30 18:22:09 +01:00
parent b0e0c14dd8
commit 96832ed1e3
3 changed files with 41 additions and 37 deletions

View file

@ -395,14 +395,18 @@ impl<'a> WasmBackend<'a> {
self.storage.load_symbols(&mut self.code_builder, wasm_args); self.storage.load_symbols(&mut self.code_builder, wasm_args);
// Index of the called function in the Wasm module code section // Index of the called function in the code section
// TODO: update when we start inlining functions (remember we emit procs out of order) // TODO: account for inlined functions when we start doing that (remember we emit procs out of order)
let func_index = self let func_index = match self.proc_symbols.iter().position(|s| s == func_sym) {
.proc_symbols Some(i) => i as u32,
.iter() None => {
.position(|s| s == func_sym) // TODO: actually useful linking! Push a relocation for it.
.map(|i| i as u32) return Err(format!(
.unwrap_or(u32::MAX); // foreign symbol, updated at link time "Not yet supporteed: calling foreign function {:?}",
func_sym
));
}
};
// Index of the function's name in the symbol table // 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 let symbol_index = func_index; // TODO: update this when we add other things to the symbol table

View file

@ -141,8 +141,8 @@ pub struct CodeBuilder<'a> {
/// Keeps track of where Symbol values are in the VM stack /// Keeps track of where Symbol values are in the VM stack
vm_stack: Vec<'a, Symbol>, vm_stack: Vec<'a, Symbol>,
/// Which byte offsets in the code section correspond to which symbols. /// Linker info to help combine the Roc module with builtin & platform modules,
/// e.g. Function indices may change when we link Roc + builtins + platform /// e.g. to modify call instructions when function indices change
relocations: Vec<'a, RelocationEntry>, relocations: Vec<'a, RelocationEntry>,
} }

View file

@ -12,7 +12,7 @@ pub trait Wasm32TestResult {
module_builder: &mut builder::ModuleBuilder, module_builder: &mut builder::ModuleBuilder,
code_section_bytes: &mut std::vec::Vec<u8>, code_section_bytes: &mut std::vec::Vec<u8>,
wrapper_name: &str, wrapper_name: &str,
main_fn_index: u32, main_function_index: u32,
) { ) {
let signature = builder::signature() let signature = builder::signature()
.with_result(parity_wasm::elements::ValueType::I32) .with_result(parity_wasm::elements::ValueType::I32)
@ -28,7 +28,7 @@ pub trait Wasm32TestResult {
module_builder.push_export(export); module_builder.push_export(export);
let mut code_builder = CodeBuilder::new(arena); 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); code_builder.serialize(code_section_bytes);
@ -41,12 +41,12 @@ pub trait Wasm32TestResult {
code_section_bytes.overwrite_padded_u32(5, num_procs + 1); 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 { macro_rules! build_wrapper_body_primitive {
($store_instruction: ident, $align: expr) => { ($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_id = LocalId(0);
let frame_pointer = Some(frame_pointer_id); let frame_pointer = Some(frame_pointer_id);
let local_types = &[ValueType::I32]; let local_types = &[ValueType::I32];
@ -54,7 +54,7 @@ macro_rules! build_wrapper_body_primitive {
code_builder.get_local(frame_pointer_id); code_builder.get_local(frame_pointer_id);
// Raw "call" instruction. Don't bother with symbol & relocation since we're not going to link. // 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.$store_instruction($align, 0);
code_builder.get_local(frame_pointer_id); code_builder.get_local(frame_pointer_id);
@ -73,7 +73,7 @@ macro_rules! wasm_test_result_primitive {
fn build_wrapper_body_stack_memory( fn build_wrapper_body_stack_memory(
code_builder: &mut CodeBuilder, code_builder: &mut CodeBuilder,
main_fn_index: u32, main_function_index: u32,
size: usize, size: usize,
) { ) {
let local_id = LocalId(0); let local_id = LocalId(0);
@ -82,7 +82,7 @@ fn build_wrapper_body_stack_memory(
code_builder.get_local(local_id); code_builder.get_local(local_id);
// Raw "call" instruction. Don't bother with symbol & relocation since we're not going to link. // 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.get_local(local_id);
code_builder.finalize(local_types, size as i32, frame_pointer); 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 { macro_rules! wasm_test_result_stack_memory {
($type_name: ident) => { ($type_name: ident) => {
impl Wasm32TestResult for $type_name { 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
$type_name::ACTUAL_WIDTH, $type_name::ACTUAL_WIDTH,
) )
} }
@ -122,8 +122,8 @@ wasm_test_result_stack_memory!(RocDec);
wasm_test_result_stack_memory!(RocStr); wasm_test_result_stack_memory!(RocStr);
impl<T: Wasm32TestResult> Wasm32TestResult for RocList<T> { impl<T: Wasm32TestResult> Wasm32TestResult for RocList<T> {
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, 12) build_wrapper_body_stack_memory(code_builder, main_function_index, 12)
} }
} }
@ -135,8 +135,8 @@ impl<T, const N: usize> Wasm32TestResult for [T; N]
where where
T: Wasm32TestResult + FromWasm32Memory, T: 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, N * T::ACTUAL_WIDTH) build_wrapper_body_stack_memory(code_builder, main_function_index, N * T::ACTUAL_WIDTH)
} }
} }
@ -145,10 +145,10 @@ where
T: Wasm32TestResult + FromWasm32Memory, T: Wasm32TestResult + FromWasm32Memory,
U: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH,
) )
} }
@ -160,10 +160,10 @@ where
U: Wasm32TestResult + FromWasm32Memory, U: Wasm32TestResult + FromWasm32Memory,
V: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH,
) )
} }
@ -176,10 +176,10 @@ where
V: Wasm32TestResult + FromWasm32Memory, V: Wasm32TestResult + FromWasm32Memory,
W: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH,
) )
} }
@ -193,10 +193,10 @@ where
W: Wasm32TestResult + FromWasm32Memory, W: Wasm32TestResult + FromWasm32Memory,
X: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH + X::ACTUAL_WIDTH, T::ACTUAL_WIDTH + U::ACTUAL_WIDTH + V::ACTUAL_WIDTH + W::ACTUAL_WIDTH + X::ACTUAL_WIDTH,
) )
} }
@ -211,10 +211,10 @@ where
X: Wasm32TestResult + FromWasm32Memory, X: Wasm32TestResult + FromWasm32Memory,
Y: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH T::ACTUAL_WIDTH
+ U::ACTUAL_WIDTH + U::ACTUAL_WIDTH
+ V::ACTUAL_WIDTH + V::ACTUAL_WIDTH
@ -235,10 +235,10 @@ where
Y: Wasm32TestResult + FromWasm32Memory, Y: Wasm32TestResult + FromWasm32Memory,
Z: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH T::ACTUAL_WIDTH
+ U::ACTUAL_WIDTH + U::ACTUAL_WIDTH
+ V::ACTUAL_WIDTH + V::ACTUAL_WIDTH
@ -261,10 +261,10 @@ where
Z: Wasm32TestResult + FromWasm32Memory, Z: Wasm32TestResult + FromWasm32Memory,
A: 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( build_wrapper_body_stack_memory(
code_builder, code_builder,
main_fn_index, main_function_index,
T::ACTUAL_WIDTH T::ACTUAL_WIDTH
+ U::ACTUAL_WIDTH + U::ACTUAL_WIDTH
+ V::ACTUAL_WIDTH + V::ACTUAL_WIDTH