Refactor literals

This commit is contained in:
Brian Carroll 2021-11-04 13:40:19 +00:00
parent 9111f3cf6c
commit 2f430fce86

View file

@ -348,8 +348,9 @@ impl<'a> WasmBackend<'a> {
expr: &Expr<'a>, expr: &Expr<'a>,
layout: &Layout<'a>, layout: &Layout<'a>,
) -> Result<(), String> { ) -> Result<(), String> {
let wasm_layout = WasmLayout::new(layout);
match expr { match expr {
Expr::Literal(lit) => self.load_literal(lit, layout), Expr::Literal(lit) => self.load_literal(lit, wasm_layout),
Expr::Call(roc_mono::ir::Call { Expr::Call(roc_mono::ir::Call {
call_type, call_type,
@ -359,7 +360,6 @@ impl<'a> WasmBackend<'a> {
// TODO: See if we can make this more efficient // TODO: See if we can make this more efficient
// Recreating the same WasmLayout again, rather than passing it down, // Recreating the same WasmLayout again, rather than passing it down,
// to match signature of Backend::build_expr // to match signature of Backend::build_expr
let wasm_layout = WasmLayout::new(layout);
let mut wasm_args_tmp: Vec<Symbol>; let mut wasm_args_tmp: Vec<Symbol>;
let (wasm_args, has_return_val) = match wasm_layout { let (wasm_args, has_return_val) = match wasm_layout {
@ -413,32 +413,25 @@ impl<'a> WasmBackend<'a> {
} }
} }
fn load_literal(&mut self, lit: &Literal<'a>, layout: &Layout<'a>) -> Result<(), String> { fn load_literal(&mut self, lit: &Literal<'a>, wasm_layout: WasmLayout) -> Result<(), String> {
match lit { let not_supported_error = || Err(format!("Literal value {:?} is not yet implemented", lit));
Literal::Bool(x) => self.code_builder.i32_const(*x as i32),
Literal::Byte(x) => self.code_builder.i32_const(*x as i32), match wasm_layout {
Literal::Int(x) => match layout { WasmLayout::Primitive(value_type, size) => {
Layout::Builtin(Builtin::Int64) => self.code_builder.i64_const(*x as i64), match (lit, value_type) {
Layout::Builtin( (Literal::Float(x), ValueType::F64) => self.code_builder.f64_const(*x as f64),
Builtin::Int32 (Literal::Float(x), ValueType::F32) => self.code_builder.f32_const(*x as f32),
| Builtin::Int16 (Literal::Int(x), ValueType::I64) => self.code_builder.i64_const(*x as i64),
| Builtin::Int8 (Literal::Int(x), ValueType::I32) => self.code_builder.i32_const(*x as i32),
| Builtin::Int1 (Literal::Bool(x), ValueType::I32) => self.code_builder.i32_const(*x as i32),
| Builtin::Usize, (Literal::Byte(x), ValueType::I32) => self.code_builder.i32_const(*x as i32),
) => self.code_builder.i32_const(*x as i32), _ => {
x => { return not_supported_error();
return Err(format!("loading literal, {:?}, is not yet implemented", x));
} }
}, };
Literal::Float(x) => match layout {
Layout::Builtin(Builtin::Float64) => self.code_builder.f64_const(*x as f64),
Layout::Builtin(Builtin::Float32) => self.code_builder.f32_const(*x as f32),
x => {
return Err(format!("loading literal, {:?}, is not yet implemented", x));
} }
}, _ => {
x => { return not_supported_error();
return Err(format!("loading literal, {:?}, is not yet implemented", x));
} }
}; };
Ok(()) Ok(())