List.unreachable

This commit is contained in:
Folkert 2022-07-02 19:31:35 +02:00
parent 543615292a
commit 506e374642
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
7 changed files with 36 additions and 0 deletions

View file

@ -790,3 +790,5 @@ iterHelp = \list, state, f, index, length ->
Break b
else
Continue state
unreachable : [] -> a

View file

@ -100,6 +100,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
STR_TO_I16 => str_to_num,
STR_TO_U8 => str_to_num,
STR_TO_I8 => str_to_num,
LIST_UNREACHABLE => roc_unreachable,
LIST_LEN => list_len,
LIST_WITH_CAPACITY => list_with_capacity,
LIST_GET_UNSAFE => list_get_unsafe,
@ -2475,6 +2476,11 @@ fn list_walk_until(symbol: Symbol, var_store: &mut VarStore) -> Def {
lowlevel_3(symbol, LowLevel::ListWalkUntil, var_store)
}
/// List.unreachable : [] -> a
fn roc_unreachable(symbol: Symbol, var_store: &mut VarStore) -> Def {
lowlevel_1(symbol, LowLevel::Unreachable, var_store)
}
/// List.map : List before, (before -> after) -> List after
fn list_map(symbol: Symbol, var_store: &mut VarStore) -> Def {
lowlevel_2(symbol, LowLevel::ListMap, var_store)

View file

@ -6068,6 +6068,20 @@ fn run_low_level<'a, 'ctx, 'env>(
PtrCast | RefCountInc | RefCountDec => {
unreachable!("Not used in LLVM backend: {:?}", op);
}
Unreachable => match RocReturn::from_layout(env, layout) {
RocReturn::Return => {
let basic_type = basic_type_from_layout(env, layout);
basic_type.const_zero()
}
RocReturn::ByPointer => {
let basic_type = basic_type_from_layout(env, layout);
let ptr = env.builder.build_alloca(basic_type, "unreachable_alloca");
env.builder.build_store(ptr, basic_type.const_zero());
ptr.into()
}
},
}
}

View file

@ -1631,6 +1631,17 @@ impl<'a> LowLevelCall<'a> {
BoxExpr | UnboxExpr => {
unreachable!("The {:?} operation is turned into mono Expr", self.lowlevel)
}
Unreachable => match self.ret_storage {
StoredValue::VirtualMachineStack { value_type, .. }
| StoredValue::Local { value_type, .. } => match value_type {
ValueType::I32 => backend.code_builder.i32_const(0),
ValueType::I64 => backend.code_builder.i64_const(0),
ValueType::F32 => backend.code_builder.f32_const(0.0),
ValueType::F64 => backend.code_builder.f64_const(0.0),
},
StoredValue::StackMemory { .. } => { /* do nothing */ }
},
}
}

View file

@ -119,6 +119,7 @@ pub enum LowLevel {
RefCountDec,
BoxExpr,
UnboxExpr,
Unreachable,
}
macro_rules! higher_order {

View file

@ -1255,6 +1255,7 @@ define_builtins! {
61 LIST_REPLACE_UNSAFE: "replaceUnsafe"
62 LIST_WITH_CAPACITY: "withCapacity"
63 LIST_ITERATE: "iterate"
64 LIST_UNREACHABLE: "unreachable"
}
6 RESULT: "Result" => {
0 RESULT_RESULT: "Result" // the Result.Result type alias

View file

@ -890,6 +890,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
// - arguments that we may want to update destructively must be Owned
// - other refcounted arguments are Borrowed
match op {
Unreachable => arena.alloc_slice_copy(&[irrelevant]),
ListLen | StrIsEmpty | StrCountGraphemes => arena.alloc_slice_copy(&[borrowed]),
ListWithCapacity => arena.alloc_slice_copy(&[irrelevant]),
ListReplaceUnsafe => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),