From 73f0a7c96ceaa23f5a4e3c57ceb37fafd206a43c Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Sat, 28 May 2022 23:35:31 +0200 Subject: [PATCH] wasm: Create a global for __stack_pointer --- compiler/gen_wasm/src/backend.rs | 8 ++++++++ compiler/gen_wasm/src/wasm_module/mod.rs | 8 ++++++++ compiler/gen_wasm/src/wasm_module/sections.rs | 6 +++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 64452e74ed..6017a221d1 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -86,6 +86,14 @@ impl<'a> WasmBackend<'a> { } } + module.global.append(Global { + ty: GlobalType { + value_type: ValueType::I32, + is_mutable: true, + }, + init: ConstExpr::I32(1234), // TODO: come up with a value! + }); + module.export.exports = app_exports; module.code.code_builders.reserve(proc_lookup.len()); diff --git a/compiler/gen_wasm/src/wasm_module/mod.rs b/compiler/gen_wasm/src/wasm_module/mod.rs index f29ebb8704..a6da3f2fc2 100644 --- a/compiler/gen_wasm/src/wasm_module/mod.rs +++ b/compiler/gen_wasm/src/wasm_module/mod.rs @@ -105,6 +105,7 @@ impl<'a> WasmModule<'a> { let function = FunctionSection::parse(arena, bytes, &mut cursor)?; let table = TableSection::parse((), bytes, &mut cursor)?; let memory = MemorySection::parse(arena, bytes, &mut cursor)?; + let global_start = cursor; let global = GlobalSection::parse(arena, bytes, &mut cursor)?; let export = ExportSection::parse(arena, bytes, &mut cursor)?; let start = OpaqueSection::parse((arena, SectionId::Start), bytes, &mut cursor)?; @@ -127,6 +128,13 @@ impl<'a> WasmModule<'a> { let reloc_data = RelocationSection::parse((arena, "reloc.DATA"), bytes, &mut cursor)?; let names = NameSection::parse(arena, bytes, &mut cursor)?; + if global.count != 0 { + return Err(ParseError { + offset: global_start, + message: format!("All globals in a relocatable Wasm module should be imported, but found {} internally defined", global.count), + }); + } + Ok(WasmModule { types, import, diff --git a/compiler/gen_wasm/src/wasm_module/sections.rs b/compiler/gen_wasm/src/wasm_module/sections.rs index ae1e6ad47f..c81ce7ccd6 100644 --- a/compiler/gen_wasm/src/wasm_module/sections.rs +++ b/compiler/gen_wasm/src/wasm_module/sections.rs @@ -385,14 +385,15 @@ impl<'a> ImportSection<'a> { impl<'a> Parse<&'a Bump> for ImportSection<'a> { fn parse(arena: &'a Bump, module_bytes: &[u8], cursor: &mut usize) -> Result { + let start = *cursor; let (count, range) = parse_section(Self::ID, module_bytes, cursor)?; let mut bytes = Vec::with_capacity_in(range.len() * 2, arena); let mut fn_signatures = Vec::with_capacity_in(range.len() / 8, arena); let end = range.end; - bytes.extend_from_slice(&module_bytes[range]); while *cursor < end { + let import_start = *cursor; String::skip_bytes(module_bytes, cursor)?; // import namespace String::skip_bytes(module_bytes, cursor)?; // import name @@ -403,6 +404,7 @@ impl<'a> Parse<&'a Bump> for ImportSection<'a> { ImportTypeId::Func => { let sig = u32::parse((), module_bytes, cursor)?; fn_signatures.push(sig); + bytes.extend_from_slice(&module_bytes[import_start..*cursor]); } ImportTypeId::Table => { TableType::skip_bytes(module_bytes, cursor)?; @@ -416,6 +418,8 @@ impl<'a> Parse<&'a Bump> for ImportSection<'a> { } } + dbg!(bytes.len(), end - start); + Ok(ImportSection { count, fn_signatures,