Skip to content

Commit

Permalink
Introduce a simple test for global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aborg-dev committed Dec 11, 2023
1 parent 6c9a9b2 commit 22dbb76
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cranelift/filetests/src/test_zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -321,6 +326,7 @@ mod tests {
add,
locals,
locals_simple,
global,
memory,
counter,
add_func,
Expand Down
10 changes: 8 additions & 2 deletions cranelift/wasm/src/environ/zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -82,6 +83,9 @@ pub struct ZkasmModuleInfo {
/// Globals as provided by `declare_global`.
pub globals: PrimaryMap<GlobalIndex, Exportable<Global>>,

/// Inits for globals when available.
pub global_inits: HashMap<GlobalIndex, GlobalInit>,

/// The start function.
pub start_func: Option<FuncIndex>,
}
Expand All @@ -101,6 +105,7 @@ impl ZkasmModuleInfo {
tables: PrimaryMap::new(),
memories: PrimaryMap::new(),
globals: PrimaryMap::new(),
global_inits: HashMap::new(),
start_func: None,
}
}
Expand Down Expand Up @@ -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(())
}

Expand Down
33 changes: 33 additions & 0 deletions cranelift/zkasm_data/generated/global.zkasm
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions cranelift/zkasm_data/global.wat
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 22dbb76

Please sign in to comment.