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;
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_module::symbol::{Interns, Symbol};
use roc_mono::ir::{Proc, ProcLayout};
@ -19,7 +19,8 @@ use roc_mono::layout::LayoutIds;
use crate::backend::WasmBackend;
use crate::code_builder::{Align, CodeBuilder, ValueType};
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};
@ -62,8 +63,6 @@ pub fn build_module_help<'a>(
let mut layout_ids = LayoutIds::default();
let mut symbol_table_entries = Vec::with_capacity_in(procedures.len(), env.arena);
for (i, ((sym, layout), proc)) in procedures.into_iter().enumerate() {
let proc_name = layout_ids
.get(proc.name, &proc.ret_layout)
@ -120,13 +119,7 @@ pub fn build_module_help<'a>(
payload: code_reloc_section_bytes,
});
const MIN_MEMORY_SIZE_KB: u32 = 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);
const MIN_MEMORY_SIZE_KB: i32 = 1024;
backend.module.export.entries.push(Export {
name: "memory".to_string(),
@ -134,12 +127,14 @@ pub fn build_module_help<'a>(
index: 0,
});
let stack_pointer_global = builder::global()
.with_type(parity_wasm::elements::ValueType::I32)
.mutable()
.init_expr(Instruction::I32Const((MIN_MEMORY_SIZE_KB * 1024) as i32))
.build();
backend.parity_builder.push_global(stack_pointer_global);
let stack_pointer_global = Global {
ty: GlobalType {
value_type: ValueType::I32,
is_mutable: true,
},
init_value: GlobalInitValue::I32(MIN_MEMORY_SIZE_KB * 1024),
};
backend.module.global.entries.push(stack_pointer_global);
Ok((backend.parity_builder, backend.module))
}
@ -190,12 +185,12 @@ pub fn combine_and_serialize<'a>(
// 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(_)));
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(_)));
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);

View file

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