From 448140d2232a6f9045b278c3392dc517e4e63c12 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Wed, 23 Mar 2022 22:25:52 +0000 Subject: [PATCH] wasm: refactor to prepare for HigherOrder calls --- compiler/gen_wasm/src/backend.rs | 8 ++++- compiler/gen_wasm/src/lib.rs | 2 +- compiler/gen_wasm/src/low_level.rs | 48 +++++++++++++++++++++++++----- compiler/module/src/low_level.rs | 2 +- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 66aeadb673..d7337ab51b 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -799,11 +799,17 @@ impl<'a> WasmBackend<'a> { ret_storage, ) } + CallType::LowLevel { op: lowlevel, .. } => { self.expr_call_low_level(*lowlevel, arguments, ret_sym, ret_layout, ret_storage) } - x => todo!("call type {:?}", x), + CallType::HigherOrder(higher_order_lowlevel) => { + // Note: Zig's calling convention bug will require a generated wrapper function! + todo!("higher order calls"); + } + + CallType::Foreign { .. } => todo!("CallType::Foreign"), } } diff --git a/compiler/gen_wasm/src/lib.rs b/compiler/gen_wasm/src/lib.rs index 57266be640..6d8eb1db4c 100644 --- a/compiler/gen_wasm/src/lib.rs +++ b/compiler/gen_wasm/src/lib.rs @@ -134,7 +134,7 @@ pub fn build_module_without_wrapper<'a>( println!("## procs"); for proc in procs.iter() { println!("{}", proc.to_pretty(200)); - // println!("{:#?}", proc); + // println!("{:?}", proc); } } diff --git a/compiler/gen_wasm/src/low_level.rs b/compiler/gen_wasm/src/low_level.rs index 86eb023cd8..90412ce3b3 100644 --- a/compiler/gen_wasm/src/low_level.rs +++ b/compiler/gen_wasm/src/low_level.rs @@ -1,9 +1,11 @@ use bumpalo::collections::Vec; use roc_builtins::bitcode::{self, FloatWidth, IntWidth}; use roc_error_macros::internal_error; -use roc_module::low_level::{LowLevel, LowLevel::*}; +use roc_module::low_level::LowLevel; use roc_module::symbol::Symbol; +use roc_mono::ir::HigherOrderLowLevel; use roc_mono::layout::{Builtin, Layout, UnionLayout}; +use roc_mono::low_level::HigherOrder; use crate::backend::WasmBackend; use crate::layout::CallConv; @@ -174,6 +176,7 @@ impl<'a> LowLevelCall<'a> { /// Main entrypoint from WasmBackend pub fn generate(&self, backend: &mut WasmBackend<'a>) { + use LowLevel::*; use CodeGenNumType::*; let panic_ret_type = || { @@ -260,14 +263,21 @@ impl<'a> LowLevelCall<'a> { _ => internal_error!("invalid storage for List"), }, + ListMap | ListMap2 | ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf | ListWalk + | ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith + | ListAny | ListAll | ListFindUnsafe | DictWalk => { + internal_error!("HigherOrder lowlevels should not be handled here") + } + ListGetUnsafe | ListReplaceUnsafe | ListSingle | ListRepeat | ListReverse | ListConcat | ListContains | ListAppend | ListPrepend | ListJoin | ListRange - | ListMap | ListMap2 | ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf - | ListWalk | ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs - | ListSortWith | ListSublist | ListDropAt | ListSwap | ListAny | ListAll - | ListFindUnsafe | DictSize | DictEmpty | DictInsert | DictRemove | DictContains - | DictGetUnsafe | DictKeys | DictValues | DictUnion | DictIntersection - | DictDifference | DictWalk | SetFromList => { + | ListSublist | ListDropAt | ListSwap => { + todo!("{:?}", self.lowlevel); + } + + DictSize | DictEmpty | DictInsert | DictRemove | DictContains | DictGetUnsafe + | DictKeys | DictValues | DictUnion | DictIntersection | DictDifference + | SetFromList => { todo!("{:?}", self.lowlevel); } @@ -705,7 +715,7 @@ impl<'a> LowLevelCall<'a> { "Cannot do `==` comparison on different types" ); - let invert_result = matches!(self.lowlevel, NotEq); + let invert_result = matches!(self.lowlevel, LowLevel::NotEq); match arg_layout { Layout::Builtin( @@ -921,3 +931,25 @@ fn num_is_finite(backend: &mut WasmBackend<'_>, argument: Symbol) { } } } + +fn gen_higher_order(higher_order: HigherOrder) { + use HigherOrder::*; + match higher_order { + ListMap { .. } + | ListMap2 { .. } + | ListMap3 { .. } + | ListMap4 { .. } + | ListMapWithIndex { .. } + | ListKeepIf { .. } + | ListWalk { .. } + | ListWalkUntil { .. } + | ListWalkBackwards { .. } + | ListKeepOks { .. } + | ListKeepErrs { .. } + | ListSortWith { .. } + | ListAny { .. } + | ListAll { .. } + | ListFindUnsafe { .. } + | DictWalk { .. } => todo!("{:?}", higher_order), + } +} diff --git a/compiler/module/src/low_level.rs b/compiler/module/src/low_level.rs index 98feb49ea8..1271816043 100644 --- a/compiler/module/src/low_level.rs +++ b/compiler/module/src/low_level.rs @@ -240,7 +240,7 @@ impl LowLevelWrapperType { Symbol::LIST_PREPEND => CanBeReplacedBy(ListPrepend), Symbol::LIST_JOIN => CanBeReplacedBy(ListJoin), Symbol::LIST_RANGE => CanBeReplacedBy(ListRange), - Symbol::LIST_MAP => CanBeReplacedBy(ListMap), + Symbol::LIST_MAP => WrapperIsRequired, Symbol::LIST_MAP2 => CanBeReplacedBy(ListMap2), Symbol::LIST_MAP3 => CanBeReplacedBy(ListMap3), Symbol::LIST_MAP4 => CanBeReplacedBy(ListMap4),