mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
Remove parity-wasm!
This commit is contained in:
parent
59757d638a
commit
87f6246851
8 changed files with 7 additions and 117 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -2781,11 +2781,6 @@ dependencies = [
|
||||||
"syn 1.0.76",
|
"syn 1.0.76",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "parity-wasm"
|
|
||||||
version = "0.44.0"
|
|
||||||
source = "git+https://github.com/brian-carroll/parity-wasm?branch=master#373f655f64d2260a2e9665811f7b6ed17f9db705"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.11.2"
|
version = "0.11.2"
|
||||||
|
@ -3916,7 +3911,6 @@ dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"indoc 0.3.6",
|
"indoc 0.3.6",
|
||||||
"libc",
|
"libc",
|
||||||
"parity-wasm",
|
|
||||||
"pretty_assertions 0.5.1",
|
"pretty_assertions 0.5.1",
|
||||||
"roc_builtins",
|
"roc_builtins",
|
||||||
"roc_can",
|
"roc_can",
|
||||||
|
@ -4755,7 +4749,6 @@ dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"indoc 0.3.6",
|
"indoc 0.3.6",
|
||||||
"libc",
|
"libc",
|
||||||
"parity-wasm",
|
|
||||||
"pretty_assertions 0.5.1",
|
"pretty_assertions 0.5.1",
|
||||||
"roc_builtins",
|
"roc_builtins",
|
||||||
"roc_can",
|
"roc_can",
|
||||||
|
|
|
@ -10,8 +10,6 @@ roc_collections = { path = "../collections" }
|
||||||
roc_module = { path = "../module" }
|
roc_module = { path = "../module" }
|
||||||
roc_mono = { path = "../mono" }
|
roc_mono = { path = "../mono" }
|
||||||
bumpalo = { version = "3.6.1", features = ["collections"] }
|
bumpalo = { version = "3.6.1", features = ["collections"] }
|
||||||
# TODO: switch to parity-wasm 0.44 once it's out (allows bumpalo vectors in some places)
|
|
||||||
parity-wasm = { git = "https://github.com/brian-carroll/parity-wasm", branch = "master" }
|
|
||||||
|
|
||||||
roc_std = { path = "../../roc_std" }
|
roc_std = { path = "../../roc_std" }
|
||||||
wasmer = "2.0.0"
|
wasmer = "2.0.0"
|
||||||
|
|
|
@ -225,6 +225,4 @@ The Module is a _specification_ for how to create an Instance of the program. Th
|
||||||
|
|
||||||
A WebAssembly module is equivalent to an executable file. It doesn't normally need relocations since at the WebAssembly layer, there is no Address Space Layout Randomisation. If it has relocations then it's an object file.
|
A WebAssembly module is equivalent to an executable file. It doesn't normally need relocations since at the WebAssembly layer, there is no Address Space Layout Randomisation. If it has relocations then it's an object file.
|
||||||
|
|
||||||
The [official spec](https://webassembly.github.io/spec/core/binary/modules.html#sections) lists the sections that are part of the final module. It doesn't mention any sections for relocations or symbol names, but it has room for "custom sections" that in practice seem to be used for that.
|
The [official spec](https://webassembly.github.io/spec/core/binary/modules.html#sections) lists the sections that are part of the final module. It doesn't mention any sections for relocations or symbol names, but it does support "custom" sections. Conventions to use those for linking are documented in the WebAssembly `tool-conventions` repo [here](https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md) and it mentions that LLVM is using those conventions.
|
||||||
|
|
||||||
The WebAssembly `tool-conventions` repo has a document on [linking](https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md), and the `parity_wasm` crate supports "name" and "relocation" [sections](https://docs.rs/parity-wasm/0.42.2/parity_wasm/elements/enum.Section.html).
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use parity_wasm::builder;
|
|
||||||
use parity_wasm::builder::{FunctionDefinition, ModuleBuilder};
|
|
||||||
|
|
||||||
use roc_collections::all::MutMap;
|
use roc_collections::all::MutMap;
|
||||||
use roc_module::low_level::LowLevel;
|
use roc_module::low_level::LowLevel;
|
||||||
|
@ -26,7 +24,6 @@ pub struct WasmBackend<'a> {
|
||||||
|
|
||||||
// Module-level data
|
// Module-level data
|
||||||
pub module: WasmModule<'a>,
|
pub module: WasmModule<'a>,
|
||||||
pub parity_builder: ModuleBuilder,
|
|
||||||
_data_offset_map: MutMap<Literal<'a>, u32>,
|
_data_offset_map: MutMap<Literal<'a>, u32>,
|
||||||
_data_offset_next: u32,
|
_data_offset_next: u32,
|
||||||
proc_symbols: Vec<'a, Symbol>,
|
proc_symbols: Vec<'a, Symbol>,
|
||||||
|
@ -47,7 +44,6 @@ impl<'a> WasmBackend<'a> {
|
||||||
|
|
||||||
// Module-level data
|
// Module-level data
|
||||||
module: WasmModule::new(env.arena),
|
module: WasmModule::new(env.arena),
|
||||||
parity_builder: builder::module(),
|
|
||||||
_data_offset_map: MutMap::default(),
|
_data_offset_map: MutMap::default(),
|
||||||
_data_offset_next: UNUSED_DATA_SECTION_BYTES,
|
_data_offset_next: UNUSED_DATA_SECTION_BYTES,
|
||||||
proc_symbols,
|
proc_symbols,
|
||||||
|
|
|
@ -8,9 +8,7 @@ pub mod serialize;
|
||||||
mod storage;
|
mod storage;
|
||||||
|
|
||||||
use bumpalo::{self, collections::Vec, Bump};
|
use bumpalo::{self, collections::Vec, Bump};
|
||||||
use parity_wasm::builder::ModuleBuilder;
|
|
||||||
|
|
||||||
use parity_wasm::elements::{Section, Serialize as ParitySerialize};
|
|
||||||
use roc_collections::all::{MutMap, MutSet};
|
use roc_collections::all::{MutMap, MutSet};
|
||||||
use roc_module::symbol::{Interns, Symbol};
|
use roc_module::symbol::{Interns, Symbol};
|
||||||
use roc_mono::ir::{Proc, ProcLayout};
|
use roc_mono::ir::{Proc, ProcLayout};
|
||||||
|
@ -21,7 +19,6 @@ use crate::code_builder::{Align, CodeBuilder, ValueType};
|
||||||
use crate::module_builder::{
|
use crate::module_builder::{
|
||||||
Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSubSection, SymInfo, WasmModule,
|
Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSubSection, SymInfo, WasmModule,
|
||||||
};
|
};
|
||||||
use crate::serialize::{SerialBuffer, Serialize};
|
|
||||||
|
|
||||||
const PTR_SIZE: u32 = 4;
|
const PTR_SIZE: u32 = 4;
|
||||||
const PTR_TYPE: ValueType = ValueType::I32;
|
const PTR_TYPE: ValueType = ValueType::I32;
|
||||||
|
@ -46,16 +43,16 @@ pub fn build_module<'a>(
|
||||||
env: &'a Env,
|
env: &'a Env,
|
||||||
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
||||||
) -> Result<std::vec::Vec<u8>, String> {
|
) -> Result<std::vec::Vec<u8>, String> {
|
||||||
let (parity_builder, mut wasm_module) = build_module_help(env, procedures)?;
|
let mut wasm_module = build_module_help(env, procedures)?;
|
||||||
let mut buffer = std::vec::Vec::with_capacity(4096);
|
let mut buffer = std::vec::Vec::with_capacity(4096);
|
||||||
combine_and_serialize(&mut buffer, parity_builder, &mut wasm_module);
|
wasm_module.serialize(&mut buffer);
|
||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_module_help<'a>(
|
pub fn build_module_help<'a>(
|
||||||
env: &'a Env,
|
env: &'a Env,
|
||||||
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
procedures: MutMap<(Symbol, ProcLayout<'a>), Proc<'a>>,
|
||||||
) -> Result<(ModuleBuilder, WasmModule<'a>), String> {
|
) -> Result<WasmModule<'a>, String> {
|
||||||
let proc_symbols = Vec::from_iter_in(procedures.keys().map(|(sym, _)| *sym), env.arena);
|
let proc_symbols = Vec::from_iter_in(procedures.keys().map(|(sym, _)| *sym), env.arena);
|
||||||
let mut backend = WasmBackend::new(env, proc_symbols);
|
let mut backend = WasmBackend::new(env, proc_symbols);
|
||||||
|
|
||||||
|
@ -101,91 +98,7 @@ pub fn build_module_help<'a>(
|
||||||
init_value: GlobalInitValue::I32(stack_pointer_init),
|
init_value: GlobalInitValue::I32(stack_pointer_init),
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok((backend.parity_builder, backend.module))
|
Ok(backend.module)
|
||||||
}
|
|
||||||
|
|
||||||
fn maybe_increment_section(size: usize, prev_size: &mut usize, index: &mut u32) {
|
|
||||||
if size > *prev_size {
|
|
||||||
*index += 1;
|
|
||||||
*prev_size = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! serialize_parity {
|
|
||||||
($buffer: expr, $sections: expr, $lambda: expr) => {
|
|
||||||
$sections
|
|
||||||
.remove($sections.iter().position($lambda).unwrap())
|
|
||||||
.serialize($buffer)
|
|
||||||
.unwrap();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replace parity-wasm's code section with our own handmade one
|
|
||||||
pub fn combine_and_serialize<'a>(
|
|
||||||
buffer: &mut std::vec::Vec<u8>,
|
|
||||||
parity_builder: ModuleBuilder,
|
|
||||||
wasm_module: &mut WasmModule<'a>, // backend: &mut WasmBackend<'a>
|
|
||||||
) {
|
|
||||||
buffer.append_u8(0);
|
|
||||||
buffer.append_slice("asm".as_bytes());
|
|
||||||
buffer.write_unencoded_u32(WasmModule::WASM_VERSION);
|
|
||||||
|
|
||||||
let mut index: u32 = 0;
|
|
||||||
let mut prev_size = buffer.size();
|
|
||||||
|
|
||||||
let mut parity_module = parity_builder.build();
|
|
||||||
let sections = parity_module.sections_mut();
|
|
||||||
|
|
||||||
wasm_module.types.serialize(buffer);
|
|
||||||
// serialize_parity!(buffer, sections, |s| matches!(s, Section::Type(_)));
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
// wasm_module.import.serialize(buffer);
|
|
||||||
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.function.serialize(buffer);
|
|
||||||
// serialize_parity!(buffer, sections, |s| matches!(s, Section::Function(_)));
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
// wasm_module.table.serialize(buffer);
|
|
||||||
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.memory.serialize(buffer);
|
|
||||||
// serialize_parity!(buffer, sections, |s| matches!(s, Section::Memory(_)));
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.global.serialize(buffer);
|
|
||||||
// serialize_parity!(buffer, sections, |s| matches!(s, Section::Global(_)));
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.export.serialize(buffer);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
// wasm_module.start.serialize(buffer);
|
|
||||||
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
// wasm_module.element.serialize(buffer);
|
|
||||||
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
// wasm_module.data_count.serialize(buffer);
|
|
||||||
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.reloc_code.target_section_index = Some(index);
|
|
||||||
wasm_module.code.serialize_mut(buffer, &mut wasm_module.reloc_code.entries);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.data.serialize(buffer);
|
|
||||||
wasm_module.reloc_data.target_section_index = Some(index);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.linking.serialize(buffer);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.reloc_code.serialize(buffer);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
|
|
||||||
wasm_module.reloc_data.serialize(buffer);
|
|
||||||
maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CopyMemoryConfig {
|
pub struct CopyMemoryConfig {
|
||||||
|
|
|
@ -9,9 +9,6 @@ edition = "2018"
|
||||||
# roc_module = { path = "../module" }
|
# roc_module = { path = "../module" }
|
||||||
# roc_mono = { path = "../mono" }
|
# roc_mono = { path = "../mono" }
|
||||||
|
|
||||||
# # TODO: switch to parity-wasm 0.44 once it's out (allows bumpalo vectors in some places)
|
|
||||||
parity-wasm = { git = "https://github.com/brian-carroll/parity-wasm", branch = "master" }
|
|
||||||
|
|
||||||
wasmer = "2.0.0"
|
wasmer = "2.0.0"
|
||||||
wasmer-wasi = "2.0.0"
|
wasmer-wasi = "2.0.0"
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@ use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
use roc_can::builtins::builtin_defs_map;
|
use roc_can::builtins::builtin_defs_map;
|
||||||
use roc_collections::all::{MutMap, MutSet};
|
use roc_collections::all::{MutMap, MutSet};
|
||||||
use roc_gen_wasm::combine_and_serialize;
|
|
||||||
// use roc_std::{RocDec, RocList, RocOrder, RocStr};
|
|
||||||
use crate::helpers::wasm32_test_result::Wasm32TestResult;
|
use crate::helpers::wasm32_test_result::Wasm32TestResult;
|
||||||
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
||||||
|
|
||||||
|
@ -104,19 +102,18 @@ pub fn helper_wasm<'a, T: Wasm32TestResult>(
|
||||||
exposed_to_host,
|
exposed_to_host,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut parity_builder, mut wasm_module) =
|
let mut wasm_module =
|
||||||
roc_gen_wasm::build_module_help(&env, procedures).unwrap();
|
roc_gen_wasm::build_module_help(&env, procedures).unwrap();
|
||||||
|
|
||||||
T::insert_test_wrapper(
|
T::insert_test_wrapper(
|
||||||
arena,
|
arena,
|
||||||
&mut parity_builder,
|
|
||||||
&mut wasm_module,
|
&mut wasm_module,
|
||||||
TEST_WRAPPER_NAME,
|
TEST_WRAPPER_NAME,
|
||||||
main_fn_index as u32,
|
main_fn_index as u32,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut module_bytes = std::vec::Vec::with_capacity(4096);
|
let mut module_bytes = std::vec::Vec::with_capacity(4096);
|
||||||
combine_and_serialize(&mut module_bytes, parity_builder, &mut wasm_module);
|
wasm_module.serialize(&mut module_bytes);
|
||||||
|
|
||||||
// for debugging (e.g. with wasm2wat or wasm-objdump)
|
// for debugging (e.g. with wasm2wat or wasm-objdump)
|
||||||
if false {
|
if false {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
use parity_wasm::builder;
|
|
||||||
|
|
||||||
use roc_gen_wasm::code_builder::{Align, CodeBuilder, ValueType};
|
use roc_gen_wasm::code_builder::{Align, CodeBuilder, ValueType};
|
||||||
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
use roc_gen_wasm::from_wasm32_memory::FromWasm32Memory;
|
||||||
|
@ -10,7 +9,6 @@ use roc_std::{RocDec, RocList, RocOrder, RocStr};
|
||||||
pub trait Wasm32TestResult {
|
pub trait Wasm32TestResult {
|
||||||
fn insert_test_wrapper<'a>(
|
fn insert_test_wrapper<'a>(
|
||||||
arena: &'a bumpalo::Bump,
|
arena: &'a bumpalo::Bump,
|
||||||
module_builder: &mut builder::ModuleBuilder,
|
|
||||||
wasm_module: &mut WasmModule<'a>,
|
wasm_module: &mut WasmModule<'a>,
|
||||||
wrapper_name: &str,
|
wrapper_name: &str,
|
||||||
main_function_index: u32,
|
main_function_index: u32,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue