diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index d12731147f..791dfffc04 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -11,7 +11,8 @@ use roc_mono::layout::{Layout, LayoutIds}; use crate::layout::WasmLayout; use crate::storage::{Storage, StoredValue, StoredValueKind}; use crate::wasm_module::linking::{ - DataSymbol, LinkingSection, RelocationSection, WasmObjectSymbol, WASM_SYM_UNDEFINED, + DataSymbol, LinkingSection, RelocationSection, WasmObjectSymbol, WASM_SYM_BINDING_WEAK, + WASM_SYM_UNDEFINED, }; use crate::wasm_module::sections::{ CodeSection, DataMode, DataSection, DataSegment, ExportSection, FunctionSection, GlobalSection, @@ -21,7 +22,10 @@ use crate::wasm_module::{ code_builder, BlockType, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType, LocalId, Signature, SymInfo, ValueType, }; -use crate::{copy_memory, CopyMemoryConfig, Env, PTR_TYPE}; +use crate::{ + copy_memory, CopyMemoryConfig, Env, BUILTINS_IMPORT_MODULE_NAME, MEMORY_NAME, PTR_TYPE, + STACK_POINTER_NAME, +}; /// The memory address where the constants data will be loaded during module instantiation. /// We avoid address zero and anywhere near it. They're valid addresses but maybe bug-prone. @@ -31,8 +35,6 @@ const CONST_SEGMENT_BASE_ADDR: u32 = 1024; /// Index of the data segment where we store constants const CONST_SEGMENT_INDEX: usize = 0; -const IMPORT_MODULE_BUILTINS: &str = "builtins"; - pub struct WasmBackend<'a> { env: &'a Env<'a>, @@ -66,7 +68,7 @@ impl<'a> WasmBackend<'a> { let num_procs = proc_symbols.len(); exports.push(Export { - name: "__linear_memory".to_string(), + name: MEMORY_NAME.to_string(), ty: ExportType::Mem, index: 0, }); @@ -79,10 +81,16 @@ impl<'a> WasmBackend<'a> { init: ConstExpr::I32(MEMORY_INIT_SIZE as i32), }; - linker_symbols.push(SymInfo::Global(WasmObjectSymbol::Defined { - flags: 0, + exports.push(Export { + name: STACK_POINTER_NAME.to_string(), + ty: ExportType::Global, index: 0, - name: "__stack_pointer".to_string(), + }); + + linker_symbols.push(SymInfo::Global(WasmObjectSymbol::Defined { + flags: WASM_SYM_BINDING_WEAK, + index: 0, + name: STACK_POINTER_NAME.to_string(), })); let const_segment = DataSegment { @@ -760,7 +768,7 @@ impl<'a> WasmBackend<'a> { let import_index = self.module.import.entries.len() as u32; let import = Import { - module: IMPORT_MODULE_BUILTINS, + module: BUILTINS_IMPORT_MODULE_NAME, name: name.to_string(), description: ImportDesc::Func { signature_index }, }; diff --git a/compiler/gen_wasm/src/lib.rs b/compiler/gen_wasm/src/lib.rs index 26e3e63bdb..6fcad7c739 100644 --- a/compiler/gen_wasm/src/lib.rs +++ b/compiler/gen_wasm/src/lib.rs @@ -21,6 +21,9 @@ const PTR_TYPE: ValueType = ValueType::I32; pub const STACK_POINTER_GLOBAL_ID: u32 = 0; pub const FRAME_ALIGNMENT_BYTES: i32 = 16; +pub const MEMORY_NAME: &str = "memory"; +pub const BUILTINS_IMPORT_MODULE_NAME: &str = "builtins"; +pub const STACK_POINTER_NAME: &str = "__stack_pointer"; pub struct Env<'a> { pub arena: &'a Bump, diff --git a/compiler/test_gen/src/helpers/wasm.rs b/compiler/test_gen/src/helpers/wasm.rs index e093510399..0e2cd9c3c3 100644 --- a/compiler/test_gen/src/helpers/wasm.rs +++ b/compiler/test_gen/src/helpers/wasm.rs @@ -7,6 +7,7 @@ use crate::helpers::wasm32_test_result::Wasm32TestResult; use roc_builtins::bitcode; use roc_can::builtins::builtin_defs_map; use roc_collections::all::{MutMap, MutSet}; +use roc_gen_wasm::MEMORY_NAME; use tempfile::tempdir; @@ -218,7 +219,7 @@ where let instance = crate::helpers::wasm::helper_wasm(&arena, src, stdlib, &expected); - let memory = instance.exports.get_memory("__linear_memory").unwrap(); + let memory = instance.exports.get_memory(MEMORY_NAME).unwrap(); let test_wrapper = instance.exports.get_function(TEST_WRAPPER_NAME).unwrap();