add ExpectTrue lowlevel

This commit is contained in:
Folkert 2021-04-23 10:16:30 +02:00
parent 01c5b0dd66
commit 6633f8ca9f
4 changed files with 38 additions and 3 deletions

View File

@ -4430,6 +4430,34 @@ fn run_low_level<'a, 'ctx, 'env>(
_ => unreachable!("invalid dict layout"),
}
}
ExpectTrue => {
debug_assert_eq!(args.len(), 1);
let context = env.context;
let bd = env.builder;
let (cond, _cond_layout) = load_symbol_and_layout(scope, &args[0]);
let condition = bd.build_int_compare(
IntPredicate::EQ,
cond.into_int_value(),
context.bool_type().const_int(1, false),
"has_not_overflowed",
);
let then_block = context.append_basic_block(parent, "then_block");
let throw_block = context.append_basic_block(parent, "throw_block");
bd.build_conditional_branch(condition, then_block, throw_block);
bd.position_at_end(throw_block);
throw_exception(env, "assert failed!");
bd.position_at_end(then_block);
cond
}
}
}

View File

@ -97,4 +97,5 @@ pub enum LowLevel {
Or,
Not,
Hash,
ExpectTrue,
}

View File

@ -694,5 +694,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
DictWalk => arena.alloc_slice_copy(&[owned, borrowed, owned]),
SetFromList => arena.alloc_slice_copy(&[owned]),
ExpectTrue => arena.alloc_slice_copy(&[irrelevant]),
}
}

View File

@ -4294,15 +4294,19 @@ pub fn from_can<'a>(
}
Expect(condition, rest) => {
let cond_symbol = env.unique_symbol();
let rest = from_can(env, variable, rest.value, procs, layout_cache);
let call_type = CallType::LowLevel { op: LowLevel::Not };
let bool_layout = Layout::Builtin(Builtin::Int1);
let cond_symbol = env.unique_symbol();
let call_type = CallType::LowLevel {
op: LowLevel::ExpectTrue,
};
let arguments = env.arena.alloc([cond_symbol]);
let call = self::Call {
call_type,
arguments,
};
let bool_layout = Layout::Builtin(Builtin::Int1);
let test = Expr::Call(call);
let rest = Stmt::Let(