mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Inline lowlevel wrapper functions
This commit is contained in:
parent
29d355c4d6
commit
b9e97400d7
2 changed files with 167 additions and 26 deletions
|
@ -2,12 +2,13 @@ use bumpalo::{self, collections::Vec};
|
||||||
|
|
||||||
use code_builder::Align;
|
use code_builder::Align;
|
||||||
use roc_collections::all::MutMap;
|
use roc_collections::all::MutMap;
|
||||||
|
use roc_module::low_level::LowLevel;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
|
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
|
||||||
use roc_mono::layout::{Builtin, Layout, LayoutIds};
|
use roc_mono::layout::{Builtin, Layout, LayoutIds};
|
||||||
|
|
||||||
use crate::layout::WasmLayout;
|
use crate::layout::WasmLayout;
|
||||||
use crate::low_level::{build_call_low_level, LowlevelBuildResult};
|
use crate::low_level::{decode_low_level, symbol_to_lowlevel, LowlevelBuildResult};
|
||||||
use crate::storage::{Storage, StoredValue, StoredValueKind};
|
use crate::storage::{Storage, StoredValue, StoredValueKind};
|
||||||
use crate::wasm_module::linking::{
|
use crate::wasm_module::linking::{
|
||||||
DataSymbol, LinkingSection, RelocationSection, WasmObjectSymbol, WASM_SYM_BINDING_WEAK,
|
DataSymbol, LinkingSection, RelocationSection, WasmObjectSymbol, WASM_SYM_BINDING_WEAK,
|
||||||
|
@ -469,6 +470,11 @@ impl<'a> WasmBackend<'a> {
|
||||||
arguments,
|
arguments,
|
||||||
}) => match call_type {
|
}) => match call_type {
|
||||||
CallType::ByName { name: func_sym, .. } => {
|
CallType::ByName { name: func_sym, .. } => {
|
||||||
|
// If this function is just a lowlevel wrapper, then inline it
|
||||||
|
if let Some(lowlevel) = symbol_to_lowlevel(*func_sym) {
|
||||||
|
return self.build_low_level(lowlevel, arguments, wasm_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 {
|
||||||
WasmLayout::StackMemory { .. } => {
|
WasmLayout::StackMemory { .. } => {
|
||||||
|
@ -511,30 +517,9 @@ impl<'a> WasmBackend<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
CallType::LowLevel { op: lowlevel, .. } => {
|
CallType::LowLevel { op: lowlevel, .. } => {
|
||||||
let return_layout = WasmLayout::new(layout);
|
self.build_low_level(*lowlevel, arguments, wasm_layout)
|
||||||
self.storage.load_symbols(&mut self.code_builder, arguments);
|
}
|
||||||
|
|
||||||
let build_result = build_call_low_level(
|
|
||||||
&mut self.code_builder,
|
|
||||||
&mut self.storage,
|
|
||||||
lowlevel,
|
|
||||||
arguments,
|
|
||||||
&return_layout,
|
|
||||||
);
|
|
||||||
use LowlevelBuildResult::*;
|
|
||||||
|
|
||||||
match build_result {
|
|
||||||
Done => Ok(()),
|
|
||||||
BuiltinCall(name) => {
|
|
||||||
self.call_imported_builtin(name, arguments, &return_layout);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
NotImplemented => Err(format!(
|
|
||||||
"Low level operation {:?} is not yet implemented",
|
|
||||||
lowlevel
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
x => Err(format!("the call type, {:?}, is not yet implemented", x)),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -567,6 +552,36 @@ impl<'a> WasmBackend<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_low_level(
|
||||||
|
&mut self,
|
||||||
|
lowlevel: LowLevel,
|
||||||
|
arguments: &'a [Symbol],
|
||||||
|
return_layout: WasmLayout,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
self.storage.load_symbols(&mut self.code_builder, arguments);
|
||||||
|
|
||||||
|
let build_result = decode_low_level(
|
||||||
|
&mut self.code_builder,
|
||||||
|
&mut self.storage,
|
||||||
|
lowlevel,
|
||||||
|
arguments,
|
||||||
|
&return_layout,
|
||||||
|
);
|
||||||
|
use LowlevelBuildResult::*;
|
||||||
|
|
||||||
|
match build_result {
|
||||||
|
Done => Ok(()),
|
||||||
|
BuiltinCall(name) => {
|
||||||
|
self.call_imported_builtin(name, arguments, &return_layout);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
NotImplemented => Err(format!(
|
||||||
|
"Low level operation {:?} is not yet implemented",
|
||||||
|
lowlevel
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn load_literal(
|
fn load_literal(
|
||||||
&mut self,
|
&mut self,
|
||||||
lit: &Literal<'a>,
|
lit: &Literal<'a>,
|
||||||
|
|
|
@ -15,10 +15,10 @@ pub enum LowlevelBuildResult {
|
||||||
NotImplemented,
|
NotImplemented,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_call_low_level<'a>(
|
pub fn decode_low_level<'a>(
|
||||||
code_builder: &mut CodeBuilder<'a>,
|
code_builder: &mut CodeBuilder<'a>,
|
||||||
storage: &mut Storage<'a>,
|
storage: &mut Storage<'a>,
|
||||||
lowlevel: &LowLevel,
|
lowlevel: LowLevel,
|
||||||
args: &'a [Symbol],
|
args: &'a [Symbol],
|
||||||
ret_layout: &WasmLayout,
|
ret_layout: &WasmLayout,
|
||||||
) -> LowlevelBuildResult {
|
) -> LowlevelBuildResult {
|
||||||
|
@ -129,6 +129,9 @@ pub fn build_call_low_level<'a>(
|
||||||
NumIsMultipleOf => return NotImplemented,
|
NumIsMultipleOf => return NotImplemented,
|
||||||
NumAbs => match ret_layout.value_type() {
|
NumAbs => match ret_layout.value_type() {
|
||||||
I32 => {
|
I32 => {
|
||||||
|
let arg_storage = storage.get(&args[0]).to_owned();
|
||||||
|
storage.ensure_value_has_local(code_builder, args[0], arg_storage);
|
||||||
|
storage.load_symbols(code_builder, args);
|
||||||
code_builder.i32_const(0);
|
code_builder.i32_const(0);
|
||||||
storage.load_symbols(code_builder, args);
|
storage.load_symbols(code_builder, args);
|
||||||
code_builder.i32_sub();
|
code_builder.i32_sub();
|
||||||
|
@ -138,6 +141,9 @@ pub fn build_call_low_level<'a>(
|
||||||
code_builder.select();
|
code_builder.select();
|
||||||
}
|
}
|
||||||
I64 => {
|
I64 => {
|
||||||
|
let arg_storage = storage.get(&args[0]).to_owned();
|
||||||
|
storage.ensure_value_has_local(code_builder, args[0], arg_storage);
|
||||||
|
storage.load_symbols(code_builder, args);
|
||||||
code_builder.i64_const(0);
|
code_builder.i64_const(0);
|
||||||
storage.load_symbols(code_builder, args);
|
storage.load_symbols(code_builder, args);
|
||||||
code_builder.i64_sub();
|
code_builder.i64_sub();
|
||||||
|
@ -358,3 +364,123 @@ fn float_width_from_layout(wasm_layout: &WasmLayout) -> FloatWidth {
|
||||||
FloatWidth::F64
|
FloatWidth::F64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn symbol_to_lowlevel(symbol: Symbol) -> Option<LowLevel> {
|
||||||
|
use LowLevel::*;
|
||||||
|
|
||||||
|
match symbol {
|
||||||
|
Symbol::STR_CONCAT => Some(StrConcat),
|
||||||
|
Symbol::STR_JOIN_WITH => Some(StrJoinWith),
|
||||||
|
Symbol::STR_IS_EMPTY => Some(StrIsEmpty),
|
||||||
|
Symbol::STR_STARTS_WITH => Some(StrStartsWith),
|
||||||
|
Symbol::STR_STARTS_WITH_CODE_PT => Some(StrStartsWithCodePt),
|
||||||
|
Symbol::STR_ENDS_WITH => Some(StrEndsWith),
|
||||||
|
Symbol::STR_SPLIT => Some(StrSplit),
|
||||||
|
Symbol::STR_COUNT_GRAPHEMES => Some(StrCountGraphemes),
|
||||||
|
Symbol::STR_FROM_INT => Some(StrFromInt),
|
||||||
|
Symbol::STR_FROM_UTF8 => Some(StrFromUtf8),
|
||||||
|
Symbol::STR_FROM_UTF8_RANGE => Some(StrFromUtf8Range),
|
||||||
|
Symbol::STR_TO_UTF8 => Some(StrToUtf8),
|
||||||
|
Symbol::STR_REPEAT => Some(StrRepeat),
|
||||||
|
Symbol::STR_FROM_FLOAT => Some(StrFromFloat),
|
||||||
|
Symbol::STR_TRIM => Some(StrTrim),
|
||||||
|
Symbol::STR_TRIM_LEFT => Some(StrTrimLeft),
|
||||||
|
Symbol::STR_TRIM_RIGHT => Some(StrTrimRight),
|
||||||
|
Symbol::LIST_LEN => Some(ListLen),
|
||||||
|
Symbol::LIST_GET => Some(ListGetUnsafe), // ??
|
||||||
|
Symbol::LIST_SET => Some(ListSet),
|
||||||
|
Symbol::LIST_SINGLE => Some(ListSingle),
|
||||||
|
Symbol::LIST_REPEAT => Some(ListRepeat),
|
||||||
|
Symbol::LIST_REVERSE => Some(ListReverse),
|
||||||
|
Symbol::LIST_CONCAT => Some(ListConcat),
|
||||||
|
Symbol::LIST_CONTAINS => Some(ListContains),
|
||||||
|
Symbol::LIST_APPEND => Some(ListAppend),
|
||||||
|
Symbol::LIST_PREPEND => Some(ListPrepend),
|
||||||
|
Symbol::LIST_JOIN => Some(ListJoin),
|
||||||
|
Symbol::LIST_RANGE => Some(ListRange),
|
||||||
|
Symbol::LIST_MAP => Some(ListMap),
|
||||||
|
Symbol::LIST_MAP2 => Some(ListMap2),
|
||||||
|
Symbol::LIST_MAP3 => Some(ListMap3),
|
||||||
|
Symbol::LIST_MAP4 => Some(ListMap4),
|
||||||
|
Symbol::LIST_MAP_WITH_INDEX => Some(ListMapWithIndex),
|
||||||
|
Symbol::LIST_KEEP_IF => Some(ListKeepIf),
|
||||||
|
Symbol::LIST_WALK => Some(ListWalk),
|
||||||
|
Symbol::LIST_WALK_UNTIL => Some(ListWalkUntil),
|
||||||
|
Symbol::LIST_WALK_BACKWARDS => Some(ListWalkBackwards),
|
||||||
|
Symbol::LIST_KEEP_OKS => Some(ListKeepOks),
|
||||||
|
Symbol::LIST_KEEP_ERRS => Some(ListKeepErrs),
|
||||||
|
Symbol::LIST_SORT_WITH => Some(ListSortWith),
|
||||||
|
Symbol::LIST_SUBLIST => Some(ListSublist),
|
||||||
|
|
||||||
|
Symbol::LIST_DROP => Some(ListDrop),
|
||||||
|
Symbol::LIST_DROP_AT => Some(ListDropAt),
|
||||||
|
Symbol::LIST_SWAP => Some(ListSwap),
|
||||||
|
Symbol::LIST_ANY => Some(ListAny),
|
||||||
|
Symbol::LIST_FIND => Some(ListFindUnsafe), // ??
|
||||||
|
Symbol::DICT_LEN => Some(DictSize),
|
||||||
|
Symbol::DICT_EMPTY => Some(DictEmpty),
|
||||||
|
Symbol::DICT_INSERT => Some(DictInsert),
|
||||||
|
Symbol::DICT_REMOVE => Some(DictRemove),
|
||||||
|
Symbol::DICT_CONTAINS => Some(DictContains),
|
||||||
|
Symbol::DICT_GET => Some(DictGetUnsafe), // ??
|
||||||
|
Symbol::DICT_KEYS => Some(DictKeys),
|
||||||
|
Symbol::DICT_VALUES => Some(DictValues),
|
||||||
|
Symbol::DICT_UNION => Some(DictUnion),
|
||||||
|
Symbol::DICT_INTERSECTION => Some(DictIntersection),
|
||||||
|
Symbol::DICT_DIFFERENCE => Some(DictDifference),
|
||||||
|
Symbol::DICT_WALK => Some(DictWalk),
|
||||||
|
Symbol::SET_FROM_LIST => Some(SetFromList),
|
||||||
|
|
||||||
|
Symbol::NUM_ADD => Some(NumAdd),
|
||||||
|
Symbol::NUM_ADD_WRAP => Some(NumAddWrap),
|
||||||
|
Symbol::NUM_ADD_CHECKED => Some(NumAddChecked),
|
||||||
|
Symbol::NUM_SUB => Some(NumSub),
|
||||||
|
Symbol::NUM_SUB_WRAP => Some(NumSubWrap),
|
||||||
|
Symbol::NUM_SUB_CHECKED => Some(NumSubChecked),
|
||||||
|
Symbol::NUM_MUL => Some(NumMul),
|
||||||
|
Symbol::NUM_MUL_WRAP => Some(NumMulWrap),
|
||||||
|
Symbol::NUM_MUL_CHECKED => Some(NumMulChecked),
|
||||||
|
Symbol::NUM_GT => Some(NumGt),
|
||||||
|
Symbol::NUM_GTE => Some(NumGte),
|
||||||
|
Symbol::NUM_LT => Some(NumLt),
|
||||||
|
Symbol::NUM_LTE => Some(NumLte),
|
||||||
|
Symbol::NUM_COMPARE => Some(NumCompare),
|
||||||
|
Symbol::NUM_DIV_FLOAT => Some(NumDivUnchecked), // ??
|
||||||
|
Symbol::NUM_DIV_CEIL => Some(NumDivCeilUnchecked),
|
||||||
|
Symbol::NUM_REM => Some(NumRemUnchecked),
|
||||||
|
Symbol::NUM_IS_MULTIPLE_OF => Some(NumIsMultipleOf),
|
||||||
|
Symbol::NUM_ABS => Some(NumAbs),
|
||||||
|
Symbol::NUM_NEG => Some(NumNeg),
|
||||||
|
Symbol::NUM_SIN => Some(NumSin),
|
||||||
|
Symbol::NUM_COS => Some(NumCos),
|
||||||
|
Symbol::NUM_SQRT => Some(NumSqrtUnchecked),
|
||||||
|
Symbol::NUM_LOG => Some(NumLogUnchecked),
|
||||||
|
Symbol::NUM_ROUND => Some(NumRound),
|
||||||
|
Symbol::NUM_TO_FLOAT => Some(NumToFloat),
|
||||||
|
Symbol::NUM_POW => Some(NumPow),
|
||||||
|
Symbol::NUM_CEILING => Some(NumCeiling),
|
||||||
|
Symbol::NUM_POW_INT => Some(NumPowInt),
|
||||||
|
Symbol::NUM_FLOOR => Some(NumFloor),
|
||||||
|
// => Some(NumIsFinite),
|
||||||
|
Symbol::NUM_ATAN => Some(NumAtan),
|
||||||
|
Symbol::NUM_ACOS => Some(NumAcos),
|
||||||
|
Symbol::NUM_ASIN => Some(NumAsin),
|
||||||
|
Symbol::NUM_BYTES_TO_U16 => Some(NumBytesToU16),
|
||||||
|
Symbol::NUM_BYTES_TO_U32 => Some(NumBytesToU32),
|
||||||
|
Symbol::NUM_BITWISE_AND => Some(NumBitwiseAnd),
|
||||||
|
Symbol::NUM_BITWISE_XOR => Some(NumBitwiseXor),
|
||||||
|
Symbol::NUM_BITWISE_OR => Some(NumBitwiseOr),
|
||||||
|
Symbol::NUM_SHIFT_LEFT => Some(NumShiftLeftBy),
|
||||||
|
Symbol::NUM_SHIFT_RIGHT => Some(NumShiftRightBy),
|
||||||
|
Symbol::NUM_SHIFT_RIGHT_ZERO_FILL => Some(NumShiftRightZfBy),
|
||||||
|
Symbol::NUM_INT_CAST => Some(NumIntCast),
|
||||||
|
Symbol::BOOL_EQ => Some(Eq),
|
||||||
|
Symbol::BOOL_NEQ => Some(NotEq),
|
||||||
|
Symbol::BOOL_AND => Some(And),
|
||||||
|
Symbol::BOOL_OR => Some(Or),
|
||||||
|
Symbol::BOOL_NOT => Some(Not),
|
||||||
|
// => Some(Hash),
|
||||||
|
// => Some(ExpectTrue),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue