Skip to content

Commit a7c9077

Browse files
author
Arnaud Riess
committed
refactore: IR: removed global type
1 parent db8d4f9 commit a7c9077

7 files changed

Lines changed: 20 additions & 17 deletions

File tree

crates/herkos/src/codegen/constructor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ pub fn emit_const_globals<B: Backend>(_backend: &B, info: &ModuleInfo) -> String
2323
let mut code = String::new();
2424
for (idx, g) in info.globals.iter().enumerate() {
2525
if !g.mutable {
26-
let (rust_ty, value_str) =
27-
crate::codegen::types::global_init_to_rust(&g.init_value, &g.wasm_type);
26+
let (rust_ty, value_str) = crate::codegen::types::global_init_to_rust(&g.init_value);
2827
code.push_str(&format!("pub const G{idx}: {rust_ty} = {value_str};\n"));
2928
}
3029
}
@@ -88,8 +87,7 @@ pub fn generate_constructor<B: Backend>(
8887
if !first {
8988
fields.push_str(", ");
9089
}
91-
let (_, value_str) =
92-
crate::codegen::types::global_init_to_rust(&g.init_value, &g.wasm_type);
90+
let (_, value_str) = crate::codegen::types::global_init_to_rust(&g.init_value);
9391
fields.push_str(&format!("g{idx}: {value_str}"));
9492
first = false;
9593
}

crates/herkos/src/codegen/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn generate_function_with_info<B: Backend>(
116116
let local_idx = *index - info.imported_globals.len();
117117
info.globals
118118
.get(local_idx)
119-
.map(|g| g.wasm_type)
119+
.map(|g| g.init_value.ty())
120120
.unwrap_or(WasmType::I32)
121121
};
122122
var_types.insert(*dest, ty);

crates/herkos/src/codegen/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ mod tests {
483483
table_max: 0,
484484
element_segments: Vec::new(),
485485
globals: vec![GlobalDef {
486-
wasm_type: WasmType::I32,
487486
mutable: true,
488487
init_value: GlobalInit::I32(0),
489488
}],
@@ -616,7 +615,6 @@ mod tests {
616615
table_max: 0,
617616
element_segments: Vec::new(),
618617
globals: vec![GlobalDef {
619-
wasm_type: WasmType::I32,
620618
mutable: false,
621619
init_value: GlobalInit::I32(42),
622620
}],

crates/herkos/src/codegen/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn generate_wrapper_module<B: Backend>(backend: &B, info: &ModuleInfo) -> Result
3636
rust_code.push_str("pub struct Globals {\n");
3737
for (idx, g) in info.globals.iter().enumerate() {
3838
if g.mutable {
39-
let rust_ty = crate::codegen::types::wasm_type_to_rust(&g.wasm_type);
39+
let rust_ty = crate::codegen::types::wasm_type_to_rust(&g.init_value.ty());
4040
rust_code.push_str(&format!(" pub g{idx}: {rust_ty},\n"));
4141
}
4242
}

crates/herkos/src/codegen/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ pub fn format_return_type(ty: Option<&WasmType>) -> String {
2525
}
2626

2727
/// Convert a GlobalInit to (Rust type string, value literal string).
28-
pub fn global_init_to_rust(init: &GlobalInit, ty: &WasmType) -> (&'static str, String) {
29-
let rust_ty = wasm_type_to_rust(ty);
28+
pub fn global_init_to_rust(init: &GlobalInit) -> (&'static str, String) {
29+
let ty = init.ty();
30+
let rust_ty = wasm_type_to_rust(&ty);
3031
let value = match init {
3132
GlobalInit::I32(v) => format!("{v}i32"),
3233
GlobalInit::I64(v) => format!("{v}i64"),

crates/herkos/src/ir/builder/assembly.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ fn build_globals(parsed: &ParsedModule) -> Vec<GlobalDef> {
5555
.globals
5656
.iter()
5757
.map(|g| {
58-
let wasm_type = WasmType::from_wasmparser(g.val_type);
5958
let init_value = match g.init_value {
6059
crate::parser::InitValue::I32(v) => GlobalInit::I32(v),
6160
crate::parser::InitValue::I64(v) => GlobalInit::I64(v),
6261
crate::parser::InitValue::F32(v) => GlobalInit::F32(v),
6362
crate::parser::InitValue::F64(v) => GlobalInit::F64(v),
6463
};
6564
GlobalDef {
66-
wasm_type,
6765
mutable: g.mutable,
6866
init_value,
6967
}

crates/herkos/src/ir/types.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,9 @@ impl fmt::Display for BinOp {
654654
/// Definition of a Wasm global variable.
655655
#[derive(Debug, Clone)]
656656
pub struct GlobalDef {
657-
/// The Wasm value type of the global.
658-
pub wasm_type: WasmType,
659657
/// Whether the global is mutable.
660658
pub mutable: bool,
661-
/// The constant initializer value.
659+
/// The constant initializer value (also encodes the type).
662660
pub init_value: GlobalInit,
663661
}
664662

@@ -671,6 +669,18 @@ pub enum GlobalInit {
671669
F64(f64),
672670
}
673671

672+
impl GlobalInit {
673+
/// Get the WasmType of this global init value.
674+
pub fn ty(&self) -> WasmType {
675+
match self {
676+
GlobalInit::I32(_) => WasmType::I32,
677+
GlobalInit::I64(_) => WasmType::I64,
678+
GlobalInit::F32(_) => WasmType::F32,
679+
GlobalInit::F64(_) => WasmType::F64,
680+
}
681+
}
682+
}
683+
674684
/// A data segment to initialize memory.
675685
#[derive(Debug, Clone)]
676686
pub struct DataSegmentDef {
@@ -1138,14 +1148,12 @@ mod tests {
11381148
assert!(!info.has_mutable_globals());
11391149

11401150
info.globals.push(GlobalDef {
1141-
wasm_type: WasmType::I32,
11421151
mutable: false,
11431152
init_value: GlobalInit::I32(0),
11441153
});
11451154
assert!(!info.has_mutable_globals());
11461155

11471156
info.globals.push(GlobalDef {
1148-
wasm_type: WasmType::I32,
11491157
mutable: true,
11501158
init_value: GlobalInit::I32(0),
11511159
});

0 commit comments

Comments
 (0)