Get rid of procedure sorting

This commit is contained in:
Brian Carroll 2021-10-26 22:44:28 +01:00
parent a821485f0c
commit fd3d8f5227
2 changed files with 24 additions and 34 deletions

View file

@ -1,6 +1,6 @@
use bumpalo::collections::Vec;
use parity_wasm::builder;
use parity_wasm::builder::{CodeLocation, FunctionDefinition, ModuleBuilder};
use parity_wasm::builder::{FunctionDefinition, ModuleBuilder};
use roc_collections::all::MutMap;
use roc_module::low_level::LowLevel;
@ -28,7 +28,7 @@ pub struct WasmBackend<'a> {
pub code_section_bytes: std::vec::Vec<u8>,
_data_offset_map: MutMap<Literal<'a>, u32>,
_data_offset_next: u32,
proc_symbol_map: MutMap<Symbol, CodeLocation>,
proc_symbols: Vec<'a, Symbol>,
// Function-level data
code_builder: CodeBuilder<'a>,
@ -40,7 +40,7 @@ pub struct WasmBackend<'a> {
}
impl<'a> WasmBackend<'a> {
pub fn new(env: &'a Env<'a>, num_procs: usize) -> Self {
pub fn new(env: &'a Env<'a>, proc_symbols: Vec<'a, Symbol>) -> Self {
// Code section is prefixed with the number of Wasm functions
// For now, this is the same as the number of IR procedures (until we start inlining!)
let mut code_section_bytes = std::vec::Vec::with_capacity(4096);
@ -48,7 +48,7 @@ impl<'a> WasmBackend<'a> {
// Reserve space for code section header: inner byte length and number of functions
// Padded to the maximum 5 bytes each, so we can update later without moving everything
code_section_bytes.resize(10, 0);
overwrite_padded_u32(&mut code_section_bytes[5..10], num_procs as u32); // gets modified in unit tests
overwrite_padded_u32(&mut code_section_bytes[5..10], proc_symbols.len() as u32); // gets modified in unit tests
WasmBackend {
env,
@ -58,7 +58,7 @@ impl<'a> WasmBackend<'a> {
code_section_bytes,
_data_offset_map: MutMap::default(),
_data_offset_next: UNUSED_DATA_SECTION_BYTES,
proc_symbol_map: MutMap::default(),
proc_symbols,
// Function-level data
block_depth: 0,
@ -81,7 +81,7 @@ impl<'a> WasmBackend<'a> {
***********************************************************/
pub fn build_proc(&mut self, proc: Proc<'a>, sym: Symbol) -> Result<u32, String> {
pub fn build_proc(&mut self, proc: Proc<'a>, _sym: Symbol) -> Result<u32, String> {
// println!("\ngenerating procedure {:?}\n", sym);
// Use parity-wasm to add the signature in "types" and "functions" sections
@ -89,7 +89,6 @@ impl<'a> WasmBackend<'a> {
let empty_function_def = self.start_proc(&proc);
let location = self.module_builder.push_function(empty_function_def);
let function_index = location.body;
self.proc_symbol_map.insert(sym, location);
self.build_stmt(&proc.body, &proc.ret_layout)?;
@ -396,13 +395,17 @@ impl<'a> WasmBackend<'a> {
self.storage.load_symbols(&mut self.code_builder, wasm_args);
let function_location = self.proc_symbol_map.get(func_sym).ok_or(format!(
"Cannot find function {:?} called from {:?}",
func_sym, sym
))?;
let function_index = self
.proc_symbols
.iter()
.position(|s| s == func_sym)
.ok_or(format!(
"Cannot find function {:?} called from {:?}",
func_sym, sym
))?;
self.code_builder
.call(function_location.body, wasm_args.len(), has_return_val);
.call(function_index as u32, wasm_args.len(), has_return_val);
Ok(())
}