diff --git a/cranelift/filetests/src/test_zkasm.rs b/cranelift/filetests/src/test_zkasm.rs index b468a7d226ff..734bfa636471 100644 --- a/cranelift/filetests/src/test_zkasm.rs +++ b/cranelift/filetests/src/test_zkasm.rs @@ -153,6 +153,11 @@ mod tests { // TODO: Preamble should be generated by a linker and/or clift itself. program.append(&mut generate_preamble(start_func.index())); + for (key, init) in &zkasm_environ.info.global_inits { + // program.push(format!("{}: MSTORE(MEM:E)", func_index)); + dbg!(key, init); + } + let num_func_imports = zkasm_environ.get_num_func_imports(); let mut context = cranelift_codegen::Context::new(); for (def_index, func) in zkasm_environ.info.function_bodies.iter() { @@ -321,6 +326,7 @@ mod tests { add, locals, locals_simple, + global, memory, counter, add_func, diff --git a/cranelift/wasm/src/environ/zkasm.rs b/cranelift/wasm/src/environ/zkasm.rs index 07d433ce9d45..6ec1a103d013 100644 --- a/cranelift/wasm/src/environ/zkasm.rs +++ b/cranelift/wasm/src/environ/zkasm.rs @@ -15,6 +15,7 @@ use crate::{ WasmFuncType, WasmHeapType, WasmResult, }; use core::convert::TryFrom; +use std::collections::HashMap; use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::ir::immediates::{Imm64, Offset32, Uimm64}; use cranelift_codegen::ir::{self, GlobalValue, InstBuilder}; @@ -82,6 +83,9 @@ pub struct ZkasmModuleInfo { /// Globals as provided by `declare_global`. pub globals: PrimaryMap>, + /// Inits for globals when available. + pub global_inits: HashMap, + /// The start function. pub start_func: Option, } @@ -101,6 +105,7 @@ impl ZkasmModuleInfo { tables: PrimaryMap::new(), memories: PrimaryMap::new(), globals: PrimaryMap::new(), + global_inits: HashMap::new(), start_func: None, } } @@ -770,8 +775,9 @@ impl<'data> ModuleEnvironment<'data> for ZkasmEnvironment { Ok(()) } - fn declare_global(&mut self, global: Global, _init: GlobalInit) -> WasmResult<()> { - self.info.globals.push(Exportable::new(global)); + fn declare_global(&mut self, global: Global, init: GlobalInit) -> WasmResult<()> { + let index = self.info.globals.push(Exportable::new(global)); + self.info.global_inits.insert(index, init); Ok(()) } diff --git a/cranelift/zkasm_data/generated/global.zkasm b/cranelift/zkasm_data/generated/global.zkasm new file mode 100644 index 000000000000..e8dbc9c829a3 --- /dev/null +++ b/cranelift/zkasm_data/generated/global.zkasm @@ -0,0 +1,33 @@ +start: + zkPC + 2 => RR + :JMP(function_1) + :JMP(finalizeExecution) +function_1: + SP + 1 => SP + RR :MSTORE(SP - 1) + SP + 2 => SP + E :MSTORE(SP - 1) + B :MSTORE(SP - 2) + 12884901888n => A ;; LoadConst32(3) + 65536 => E ;; LoadExtName(User(userextname0)) + ${ E >> 32 } => E + A :MSTORE(MEM:E + 16) + 65536 => E ;; LoadExtName(User(userextname0)) + ${ E >> 32 } => E + $ => A :MLOAD(MEM:E + 8) + 65536 => E ;; LoadExtName(User(userextname0)) + ${ E >> 32 } => E + $ => B :MLOAD(MEM:E + 16) + $ => A :ADD + 4294967296n => B ;; LoadConst32(1) + B :ASSERT + $ => E :MLOAD(SP - 1) + $ => B :MLOAD(SP - 2) + SP - 2 => SP + $ => RR :MLOAD(SP - 1) + SP - 1 => SP + :JMP(RR) +finalizeExecution: + ${beforeLast()} :JMPN(finalizeExecution) + :JMP(start) +INCLUDE "helpers/2-exp.zkasm" \ No newline at end of file diff --git a/cranelift/zkasm_data/global.wat b/cranelift/zkasm_data/global.wat new file mode 100644 index 000000000000..e4c6a82314b2 --- /dev/null +++ b/cranelift/zkasm_data/global.wat @@ -0,0 +1,12 @@ +(module + (import "env" "assert_eq" (func $assert_eq (param i32) (param i32))) + (global $a i32 (i32.const -2)) + (global $b (mut i32) (i32.const 5)) + (func $main + (global.set $b (i32.const 3)) + (global.get $a) + (global.get $b) + i32.add + i32.const 1 + call $assert_eq) + (start $main))