Implement mono of crash

This commit is contained in:
Ayaz Hafiz 2022-11-22 15:44:06 -06:00
parent 72ff0cc800
commit c7ef1668d4
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
10 changed files with 84 additions and 7 deletions

View file

@ -321,7 +321,7 @@ impl<'a> ParamMap<'a> {
}
Refcounting(_, _) => unreachable!("these have not been introduced yet"),
Ret(_) | Jump(_, _) | RuntimeError(_) => {
Ret(_) | Jump(_, _) | RuntimeError(_) | Crash(..) => {
// these are terminal, do nothing
}
}
@ -827,6 +827,11 @@ impl<'a> BorrowInfState<'a> {
Refcounting(_, _) => unreachable!("these have not been introduced yet"),
Crash(msg, _) => {
// Crash is a foreign call, so we must own the argument.
self.own_var(*msg);
}
Ret(_) | RuntimeError(_) => {
// these are terminal, do nothing
}
@ -1001,7 +1006,7 @@ fn call_info_stmt<'a>(arena: &'a Bump, stmt: &Stmt<'a>, info: &mut CallInfo<'a>)
Refcounting(_, _) => unreachable!("these have not been introduced yet"),
Ret(_) | Jump(_, _) | RuntimeError(_) => {
Ret(_) | Jump(_, _) | RuntimeError(_) | Crash(..) => {
// these are terminal, do nothing
}
}

View file

@ -158,6 +158,10 @@ pub fn occurring_variables(stmt: &Stmt<'_>) -> (MutSet<Symbol>, MutSet<Symbol>)
stack.push(default_branch.1);
}
Crash(sym, _) => {
result.insert(*sym);
}
RuntimeError(_) => {}
}
}
@ -1240,6 +1244,19 @@ impl<'a, 'i> Context<'a, 'i> {
(expect, b_live_vars)
}
Crash(x, _) => {
let info = self.get_var_info(*x);
let mut live_vars = MutSet::default();
live_vars.insert(*x);
if info.reference && !info.consume {
(self.add_inc(*x, 1, stmt), live_vars)
} else {
(stmt, live_vars)
}
}
RuntimeError(_) | Refcounting(_, _) => (stmt, MutSet::default()),
}
}
@ -1411,6 +1428,11 @@ pub fn collect_stmt(
vars
}
Crash(m, _) => {
vars.insert(*m);
vars
}
RuntimeError(_) => vars,
}
}

View file

@ -1611,6 +1611,7 @@ pub fn cond<'a>(
}
pub type Stores<'a> = &'a [(Symbol, Layout<'a>, Expr<'a>)];
#[derive(Clone, Debug, PartialEq)]
pub enum Stmt<'a> {
Let(Symbol, Expr<'a>, Layout<'a>, &'a Stmt<'a>),
@ -1656,6 +1657,12 @@ pub enum Stmt<'a> {
},
Jump(JoinPointId, &'a [Symbol]),
RuntimeError(&'a str),
Crash(Symbol, CrashTag),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CrashTag {
User,
}
/// in the block below, symbol `scrutinee` is assumed be be of shape `tag_id`
@ -2304,6 +2311,7 @@ impl<'a> Stmt<'a> {
}
RuntimeError(s) => alloc.text(format!("Error {}", s)),
Crash(s, _src) => alloc.text(format!("Crash {:?}", s)),
Join {
id,
@ -5562,7 +5570,20 @@ pub fn with_hole<'a>(
}
TypedHole(_) => Stmt::RuntimeError("Hit a blank"),
RuntimeError(e) => Stmt::RuntimeError(env.arena.alloc(e.runtime_message())),
Crash { .. } => todo!(),
Crash { msg, ret_var: _ } => {
let msg_sym = env.unique_symbol();
let stmt = Stmt::Crash(msg_sym, CrashTag::User);
with_hole(
env,
msg.value,
Variable::STR,
procs,
layout_cache,
msg_sym,
env.arena.alloc(stmt),
)
}
}
}
@ -7020,6 +7041,7 @@ fn substitute_in_stmt_help<'a>(
}
RuntimeError(_) => None,
Crash(msg, tag) => substitute(subs, *msg).map(|new| &*arena.alloc(Crash(new, *tag))),
}
}

View file

@ -241,7 +241,7 @@ fn function_s<'a, 'i>(
}
}
Ret(_) | Jump(_, _) | RuntimeError(_) => stmt,
Ret(_) | Jump(_, _) | RuntimeError(_) | Crash(..) => stmt,
}
}
@ -535,7 +535,9 @@ fn function_d_main<'a, 'i>(
(arena.alloc(new_join), found)
}
Ret(_) | Jump(_, _) | RuntimeError(_) => (stmt, has_live_var(&env.jp_live_vars, stmt, x)),
Ret(_) | Jump(_, _) | RuntimeError(_) | Crash(..) => {
(stmt, has_live_var(&env.jp_live_vars, stmt, x))
}
}
}
@ -696,7 +698,7 @@ fn function_r<'a, 'i>(env: &mut Env<'a, 'i>, stmt: &'a Stmt<'a>) -> &'a Stmt<'a>
arena.alloc(expect)
}
Ret(_) | Jump(_, _) | RuntimeError(_) => {
Ret(_) | Jump(_, _) | RuntimeError(_) | Crash(..) => {
// terminals
stmt
}
@ -761,6 +763,7 @@ fn has_live_var<'a>(jp_live_vars: &JPLiveVarMap, stmt: &'a Stmt<'a>, needle: Sym
Jump(id, arguments) => {
arguments.iter().any(|s| *s == needle) || jp_live_vars[id].contains(&needle)
}
Crash(m, _) => *m == needle,
RuntimeError(_) => false,
}
}

View file

@ -300,5 +300,6 @@ fn insert_jumps<'a>(
Ret(_) => None,
Jump(_, _) => None,
RuntimeError(_) => None,
Crash(..) => None,
}
}