Move wasm file format code into a submodule

This commit is contained in:
Brian Carroll 2021-11-03 11:20:16 +00:00
parent 7fa6436ee6
commit ad9b761fce
10 changed files with 45 additions and 50 deletions

View file

@ -6,11 +6,10 @@ use roc_module::symbol::Symbol;
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt}; use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
use roc_mono::layout::{Builtin, Layout}; use roc_mono::layout::{Builtin, Layout};
use crate::code_builder::{BlockType, CodeBuilder, ValueType};
use crate::layout::WasmLayout; use crate::layout::WasmLayout;
use crate::module_builder::{Signature, WasmModule};
use crate::storage::{Storage, StoredValue, StoredValueKind}; use crate::storage::{Storage, StoredValue, StoredValueKind};
use crate::{copy_memory, CopyMemoryConfig, Env, LocalId, PTR_TYPE}; use crate::wasm_module::{BlockType, CodeBuilder, LocalId, Signature, ValueType, WasmModule};
use crate::{copy_memory, CopyMemoryConfig, Env, PTR_TYPE};
// Don't allocate any constant data at address zero or near it. Would be valid, but bug-prone. // Don't allocate any constant data at address zero or near it. Would be valid, but bug-prone.
// Follow Emscripten's example by using 1kB (4 bytes would probably do) // Follow Emscripten's example by using 1kB (4 bytes would probably do)

View file

@ -1,6 +1,6 @@
use roc_mono::layout::{Layout, UnionLayout}; use roc_mono::layout::{Layout, UnionLayout};
use crate::{code_builder::ValueType, PTR_SIZE, PTR_TYPE}; use crate::{wasm_module::ValueType, PTR_SIZE, PTR_TYPE};
// See README for background information on Wasm locals, memory and function calls // See README for background information on Wasm locals, memory and function calls
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -1,11 +1,8 @@
mod backend; mod backend;
pub mod code_builder;
pub mod from_wasm32_memory; pub mod from_wasm32_memory;
mod layout; mod layout;
pub mod module_builder;
pub mod opcodes;
pub mod serialize;
mod storage; mod storage;
pub mod wasm_module;
use bumpalo::{self, collections::Vec, Bump}; use bumpalo::{self, collections::Vec, Bump};
@ -15,9 +12,9 @@ use roc_mono::ir::{Proc, ProcLayout};
use roc_mono::layout::LayoutIds; use roc_mono::layout::LayoutIds;
use crate::backend::WasmBackend; use crate::backend::WasmBackend;
use crate::code_builder::{Align, CodeBuilder, ValueType}; use crate::wasm_module::{
use crate::module_builder::{ Align, CodeBuilder, Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSubSection,
Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSubSection, SymInfo, WasmModule, LocalId, SymInfo, ValueType, WasmModule,
}; };
const PTR_SIZE: u32 = 4; const PTR_SIZE: u32 = 4;
@ -26,13 +23,6 @@ const PTR_TYPE: ValueType = ValueType::I32;
pub const STACK_POINTER_GLOBAL_ID: u32 = 0; pub const STACK_POINTER_GLOBAL_ID: u32 = 0;
pub const FRAME_ALIGNMENT_BYTES: i32 = 16; pub const FRAME_ALIGNMENT_BYTES: i32 = 16;
/// Code section ID from spec
/// https://webassembly.github.io/spec/core/binary/modules.html#sections
pub const CODE_SECTION_ID: u8 = 10;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalId(pub u32);
pub struct Env<'a> { pub struct Env<'a> {
pub arena: &'a Bump, pub arena: &'a Bump,
pub interns: Interns, pub interns: Interns,

View file

@ -4,9 +4,9 @@ use bumpalo::Bump;
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use crate::code_builder::{CodeBuilder, ValueType, VirtualMachineSymbolState};
use crate::layout::WasmLayout; use crate::layout::WasmLayout;
use crate::{copy_memory, round_up_to_alignment, CopyMemoryConfig, LocalId, PTR_SIZE, PTR_TYPE}; use crate::wasm_module::{CodeBuilder, LocalId, ValueType, VirtualMachineSymbolState};
use crate::{copy_memory, round_up_to_alignment, CopyMemoryConfig, PTR_SIZE, PTR_TYPE};
pub enum StoredValueKind { pub enum StoredValueKind {
Parameter, Parameter,
@ -291,7 +291,7 @@ impl<'a> Storage<'a> {
| StoredValue::Local { | StoredValue::Local {
value_type, size, .. value_type, size, ..
} => { } => {
use crate::code_builder::Align::*; use crate::wasm_module::Align::*;
code_builder.get_local(to_ptr); code_builder.get_local(to_ptr);
self.load_symbols(code_builder, &[from_symbol]); self.load_symbols(code_builder, &[from_symbol]);
match (value_type, size) { match (value_type, size) {

View file

@ -5,10 +5,13 @@ use std::fmt::Debug;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use crate::module_builder::{IndexRelocType, RelocationEntry}; use super::opcodes::*;
use crate::opcodes::*; use super::sections::{IndexRelocType, RelocationEntry};
use crate::serialize::{SerialBuffer, Serialize}; use super::serialize::{SerialBuffer, Serialize};
use crate::{round_up_to_alignment, LocalId, FRAME_ALIGNMENT_BYTES, STACK_POINTER_GLOBAL_ID}; use crate::{round_up_to_alignment, FRAME_ALIGNMENT_BYTES, STACK_POINTER_GLOBAL_ID};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalId(pub u32);
/// Wasm value type. (Rust representation matches Wasm encoding) /// Wasm value type. (Rust representation matches Wasm encoding)
#[repr(u8)] #[repr(u8)]

View file

@ -0,0 +1,12 @@
pub mod code_builder;
pub mod opcodes;
pub mod sections;
pub mod serialize;
pub use code_builder::{
Align, BlockType, CodeBuilder, LocalId, ValueType, VirtualMachineSymbolState,
};
pub use sections::{
Export, ExportType, Global, GlobalInitValue, GlobalType, LinkingSubSection, Signature, SymInfo,
WasmModule,
};

View file

@ -1,9 +1,9 @@
use bumpalo::collections::vec::Vec; use bumpalo::collections::vec::Vec;
use bumpalo::Bump; use bumpalo::Bump;
use crate::code_builder::{Align, CodeBuilder, ValueType}; use super::opcodes;
use crate::opcodes; use super::serialize::{SerialBuffer, Serialize};
use crate::serialize::{SerialBuffer, Serialize}; use super::{Align, CodeBuilder, ValueType};
/******************************************************************* /*******************************************************************
* *
@ -920,16 +920,6 @@ impl<'a> Serialize for LinkingSection<'a> {
* https://webassembly.github.io/spec/core/binary/modules.html * https://webassembly.github.io/spec/core/binary/modules.html
* *
*******************************************************************/ *******************************************************************/
pub struct RocUnusedSection {}
impl Serialize for RocUnusedSection {
fn serialize<T: SerialBuffer>(&self, _buffer: &mut T) {}
}
impl Default for RocUnusedSection {
fn default() -> Self {
RocUnusedSection {}
}
}
pub struct WasmModule<'a> { pub struct WasmModule<'a> {
pub types: TypeSection<'a>, pub types: TypeSection<'a>,
pub import: ImportSection<'a>, pub import: ImportSection<'a>,
@ -953,13 +943,6 @@ pub struct WasmModule<'a> {
pub reloc_data: RelocationSection<'a>, pub reloc_data: RelocationSection<'a>,
} }
fn maybe_increment_section(size: usize, prev_size: &mut usize, index: &mut u32) {
if size > *prev_size {
*index += 1;
*prev_size = size;
}
}
impl<'a> WasmModule<'a> { impl<'a> WasmModule<'a> {
pub const WASM_VERSION: u32 = 1; pub const WASM_VERSION: u32 = 1;
@ -1040,3 +1023,10 @@ impl<'a> WasmModule<'a> {
self.reloc_data.serialize(buffer); self.reloc_data.serialize(buffer);
} }
} }
fn maybe_increment_section(size: usize, prev_size: &mut usize, index: &mut u32) {
if size > *prev_size {
*index += 1;
*prev_size = size;
}
}

View file

@ -1,9 +1,10 @@
use bumpalo::collections::Vec; use bumpalo::collections::Vec;
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;
use roc_gen_wasm::module_builder::{Export, ExportType, Signature, WasmModule}; use roc_gen_wasm::wasm_module::opcodes;
use roc_gen_wasm::LocalId; use roc_gen_wasm::wasm_module::{
Align, CodeBuilder, Export, ExportType, LocalId, Signature, ValueType, WasmModule,
};
use roc_std::{RocDec, RocList, RocOrder, RocStr}; use roc_std::{RocDec, RocList, RocOrder, RocStr};
pub trait Wasm32TestResult { pub trait Wasm32TestResult {
@ -42,7 +43,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_function_index); code_builder.inst_imm32(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);
@ -70,7 +71,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_function_index); code_builder.inst_imm32(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);
} }