From 7f0158ef00d3a92c921efdc3393fe27394ae0ebc Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sun, 28 Nov 2021 06:46:15 -0500 Subject: [PATCH] Improve constant expression generation --- wasm/src/backend/translator/data.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/wasm/src/backend/translator/data.rs b/wasm/src/backend/translator/data.rs index 7522675..4f376f2 100644 --- a/wasm/src/backend/translator/data.rs +++ b/wasm/src/backend/translator/data.rs @@ -51,18 +51,23 @@ fn gen_nil_array(name: &str, len: usize, w: &mut dyn Write) -> Result<()> { } pub fn gen_expression(code: &[Instruction], w: &mut dyn Write) -> Result<()> { - assert!(code.len() == 2); + // FIXME: Badly generated WASM will produce the wrong constant. + for inst in code { + let result = match *inst { + Instruction::I32Const(v) => write!(w, "{} ", v), + Instruction::I64Const(v) => write!(w, "{} ", v), + Instruction::F32Const(v) => write!(w, "{} ", f32::from_bits(v)), + Instruction::F64Const(v) => write!(w, "{} ", f64::from_bits(v)), + Instruction::GetGlobal(i) => write!(w, "GLOBAL_LIST[{}].value ", i), + _ => { + continue; + } + }; - let inst = code.first().unwrap(); - - match *inst { - Instruction::I32Const(v) => write!(w, "{} ", v), - Instruction::I64Const(v) => write!(w, "{} ", v), - Instruction::F32Const(v) => write!(w, "{} ", f32::from_bits(v)), - Instruction::F64Const(v) => write!(w, "{} ", f64::from_bits(v)), - Instruction::GetGlobal(i) => write!(w, "GLOBAL_LIST[{}].value ", i), - _ => unreachable!(), + return result; } + + unreachable!(); } pub struct Module<'a> {