Migrate to own Global and Memory sections

This commit is contained in:
Brian Carroll 2021-11-02 13:32:54 +00:00
parent edadd4ce13
commit e720873c05
2 changed files with 46 additions and 46 deletions

View file

@ -8,9 +8,9 @@ pub mod serialize;
mod storage; mod storage;
use bumpalo::{self, collections::Vec, Bump}; use bumpalo::{self, collections::Vec, Bump};
use parity_wasm::builder::{self, ModuleBuilder}; use parity_wasm::builder::{ModuleBuilder};
use parity_wasm::elements::{Instruction, Section, Serialize as ParitySerialize}; 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};
@ -19,7 +19,8 @@ use roc_mono::layout::LayoutIds;
use crate::backend::WasmBackend; use crate::backend::WasmBackend;
use crate::code_builder::{Align, CodeBuilder, ValueType}; use crate::code_builder::{Align, CodeBuilder, ValueType};
use crate::module_builder::{ use crate::module_builder::{
Export, ExportType, LinkingSection, LinkingSubSection, SectionId, SymInfo, WasmModule, Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSection, LinkingSubSection,
SectionId, SymInfo, WasmModule,
}; };
use crate::serialize::{SerialBuffer, Serialize}; use crate::serialize::{SerialBuffer, Serialize};
@ -62,8 +63,6 @@ pub fn build_module_help<'a>(
let mut layout_ids = LayoutIds::default(); let mut layout_ids = LayoutIds::default();
let mut symbol_table_entries = Vec::with_capacity_in(procedures.len(), env.arena); let mut symbol_table_entries = Vec::with_capacity_in(procedures.len(), env.arena);
for (i, ((sym, layout), proc)) in procedures.into_iter().enumerate() { for (i, ((sym, layout), proc)) in procedures.into_iter().enumerate() {
let proc_name = layout_ids let proc_name = layout_ids
.get(proc.name, &proc.ret_layout) .get(proc.name, &proc.ret_layout)
@ -120,13 +119,7 @@ pub fn build_module_help<'a>(
payload: code_reloc_section_bytes, payload: code_reloc_section_bytes,
}); });
const MIN_MEMORY_SIZE_KB: u32 = 1024; const MIN_MEMORY_SIZE_KB: i32 = 1024;
const PAGE_SIZE_KB: u32 = 64;
let memory = builder::MemoryBuilder::new()
.with_min(MIN_MEMORY_SIZE_KB / PAGE_SIZE_KB)
.build();
backend.parity_builder.push_memory(memory);
backend.module.export.entries.push(Export { backend.module.export.entries.push(Export {
name: "memory".to_string(), name: "memory".to_string(),
@ -134,12 +127,14 @@ pub fn build_module_help<'a>(
index: 0, index: 0,
}); });
let stack_pointer_global = builder::global() let stack_pointer_global = Global {
.with_type(parity_wasm::elements::ValueType::I32) ty: GlobalType {
.mutable() value_type: ValueType::I32,
.init_expr(Instruction::I32Const((MIN_MEMORY_SIZE_KB * 1024) as i32)) is_mutable: true,
.build(); },
backend.parity_builder.push_global(stack_pointer_global); init_value: GlobalInitValue::I32(MIN_MEMORY_SIZE_KB * 1024),
};
backend.module.global.entries.push(stack_pointer_global);
Ok((backend.parity_builder, backend.module)) Ok((backend.parity_builder, backend.module))
} }
@ -190,12 +185,12 @@ pub fn combine_and_serialize<'a>(
// wasm_module.table.serialize(buffer); // wasm_module.table.serialize(buffer);
// maybe_increment_section(buffer.size(), &mut prev_size, &mut index); // maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
// wasm_module.memory.serialize(buffer); wasm_module.memory.serialize(buffer);
serialize_parity!(buffer, sections, |s| matches!(s, Section::Memory(_))); // serialize_parity!(buffer, sections, |s| matches!(s, Section::Memory(_)));
maybe_increment_section(buffer.size(), &mut prev_size, &mut index); maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
// wasm_module.global.serialize(buffer); wasm_module.global.serialize(buffer);
serialize_parity!(buffer, sections, |s| matches!(s, Section::Global(_))); // serialize_parity!(buffer, sections, |s| matches!(s, Section::Global(_)));
maybe_increment_section(buffer.size(), &mut prev_size, &mut index); maybe_increment_section(buffer.size(), &mut prev_size, &mut index);
wasm_module.export.serialize(buffer); wasm_module.export.serialize(buffer);

View file

@ -127,7 +127,7 @@ impl<'a> Serialize for [ValueType] {
} }
} }
struct Signature<'a> { pub struct Signature<'a> {
param_types: Vec<'a, ValueType>, param_types: Vec<'a, ValueType>,
ret_type: Option<ValueType>, ret_type: Option<ValueType>,
} }
@ -169,14 +169,14 @@ impl<'a> Serialize for TypeSection<'a> {
#[repr(u8)] #[repr(u8)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum RefType { pub enum RefType {
Func = 0x70, Func = 0x70,
Extern = 0x6f, Extern = 0x6f,
} }
struct TableType { pub struct TableType {
ref_type: RefType, pub ref_type: RefType,
limits: Limits, pub limits: Limits,
} }
impl Serialize for TableType { impl Serialize for TableType {
@ -186,17 +186,17 @@ impl Serialize for TableType {
} }
} }
enum ImportDesc { pub enum ImportDesc {
Func { signature_index: u32 }, Func { signature_index: u32 },
Table { ty: TableType }, Table { ty: TableType },
Mem { limits: Limits }, Mem { limits: Limits },
Global { ty: GlobalType }, Global { ty: GlobalType },
} }
struct Import { pub struct Import {
module: String, pub module: String,
name: String, pub name: String,
description: ImportDesc, pub description: ImportDesc,
} }
impl Serialize for Import { impl Serialize for Import {
@ -224,17 +224,17 @@ impl Serialize for Import {
} }
} }
pub struct ImportSection<'a>(Vec<'a, Import>); pub struct ImportSection<'a> { entries: Vec<'a, Import> }
impl<'a> ImportSection<'a> { impl<'a> ImportSection<'a> {
pub fn new(arena: &'a Bump) -> Self { pub fn new(arena: &'a Bump) -> Self {
ImportSection(bumpalo::vec![in arena]) ImportSection { entries: bumpalo::vec![in arena] }
} }
} }
impl<'a> Serialize for ImportSection<'a> { impl<'a> Serialize for ImportSection<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
serialize_vector_section(buffer, SectionId::Import, &self.0); serialize_vector_section(buffer, SectionId::Import, &self.entries);
} }
} }
@ -269,7 +269,7 @@ impl<'a> Serialize for FunctionSection<'a> {
* *
*******************************************************************/ *******************************************************************/
enum Limits { pub enum Limits {
Min(u32), Min(u32),
MinMax(u32, u32), MinMax(u32, u32),
} }
@ -322,9 +322,9 @@ impl Serialize for MemorySection {
* *
*******************************************************************/ *******************************************************************/
struct GlobalType { pub struct GlobalType {
value_type: ValueType, pub value_type: ValueType,
is_mutable: bool, pub is_mutable: bool,
} }
impl Serialize for GlobalType { impl Serialize for GlobalType {
@ -334,16 +334,16 @@ impl Serialize for GlobalType {
} }
} }
enum GlobalInitValue { pub enum GlobalInitValue {
I32(i32), I32(i32),
I64(i64), I64(i64),
F32(f32), F32(f32),
F64(f64), F64(f64),
} }
struct Global { pub struct Global {
ty: GlobalType, pub ty: GlobalType,
init_value: GlobalInitValue, pub init_value: GlobalInitValue,
} }
impl Serialize for Global { impl Serialize for Global {
@ -367,20 +367,25 @@ impl Serialize for Global {
buffer.encode_f64(x); buffer.encode_f64(x);
} }
} }
buffer.append_u8(opcodes::END);
} }
} }
pub struct GlobalSection<'a>(Vec<'a, Global>); pub struct GlobalSection<'a> {
pub entries: Vec<'a, Global>,
}
impl<'a> GlobalSection<'a> { impl<'a> GlobalSection<'a> {
pub fn new(arena: &'a Bump) -> Self { pub fn new(arena: &'a Bump) -> Self {
GlobalSection(Vec::with_capacity_in(1, arena)) GlobalSection {
entries: Vec::with_capacity_in(1, arena),
}
} }
} }
impl<'a> Serialize for GlobalSection<'a> { impl<'a> Serialize for GlobalSection<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
serialize_vector_section(buffer, SectionId::Global, &self.0); serialize_vector_section(buffer, SectionId::Global, &self.entries);
} }
} }