set up GetTagId

This commit is contained in:
Eric Correia 2021-06-20 10:54:53 -04:00
parent f08c764cad
commit 342d1f34c2
6 changed files with 136 additions and 5 deletions

View File

@ -447,6 +447,9 @@ where
Expr::AccessAtIndex { structure, .. } => {
self.set_last_seen(*structure, stmt);
}
Expr::GetTagId { structure, .. } => {
self.set_last_seen(*structure, stmt);
}
Expr::Array { elems, .. } => {
for sym in *elems {
self.set_last_seen(*sym, stmt);

View File

@ -1639,6 +1639,83 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
EmptyArray => empty_polymorphic_list(env),
Array { elem_layout, elems } => list_literal(env, scope, elem_layout, elems),
RuntimeErrorFunction(_) => todo!(),
GetTagId {
structure,
union_layout,
} => {
let builder = env.builder;
// cast the argument bytes into the desired shape for this tag
let (argument, _structure_layout) = load_symbol_and_layout(scope, structure);
match union_layout {
UnionLayout::NonRecursive(_) => {
let pointer = builder.build_alloca(argument.get_type(), "get_type");
builder.build_store(pointer, argument);
let tag_id_pointer = builder.build_bitcast(
pointer,
env.context.i64_type().ptr_type(AddressSpace::Generic),
"tag_id_pointer",
);
builder.build_load(tag_id_pointer.into_pointer_value(), "load_tag_id")
}
UnionLayout::Recursive(_) => {
let pointer = builder.build_alloca(argument.get_type(), "get_type");
builder.build_store(pointer, argument);
let tag_id_pointer = builder.build_bitcast(
pointer,
env.context.i64_type().ptr_type(AddressSpace::Generic),
"tag_id_pointer",
);
builder.build_load(tag_id_pointer.into_pointer_value(), "load_tag_id")
}
UnionLayout::NonNullableUnwrapped(_) => env.context.i64_type().const_zero().into(),
UnionLayout::NullableWrapped { nullable_id, .. } => {
let argument_ptr = argument.into_pointer_value();
let is_null = env.builder.build_is_null(argument_ptr, "is_null");
let ctx = env.context;
let then_block = ctx.append_basic_block(parent, "then");
let else_block = ctx.append_basic_block(parent, "else");
let cont_block = ctx.append_basic_block(parent, "cont");
let result = builder.build_alloca(ctx.i64_type(), "result");
env.builder
.build_conditional_branch(is_null, then_block, else_block);
{
env.builder.position_at_end(then_block);
let tag_id = ctx.i64_type().const_int(*nullable_id as u64, false);
env.builder.build_store(result, tag_id);
env.builder.build_unconditional_branch(cont_block);
}
{
env.builder.position_at_end(else_block);
let tag_id = extract_tag_discriminant_ptr(env, argument_ptr);
env.builder.build_store(result, tag_id);
env.builder.build_unconditional_branch(cont_block);
}
env.builder.position_at_end(cont_block);
env.builder.build_load(result, "load_result")
}
UnionLayout::NullableUnwrapped { nullable_id, .. } => {
let argument_ptr = argument.into_pointer_value();
let is_null = env.builder.build_is_null(argument_ptr, "is_null");
let ctx = env.context;
let then_value = ctx.i64_type().const_int(*nullable_id as u64, false);
let else_value = ctx.i64_type().const_int(!*nullable_id as u64, false);
env.builder
.build_select(is_null, then_value, else_value, "select_tag_id")
}
}
}
}
}

View File

@ -934,6 +934,7 @@ fn expr_spec(
builder.add_terminate(block, type_id)
}
GetTagId { .. } => builder.add_make_tuple(block, &[]),
}
}

View File

@ -580,6 +580,19 @@ impl<'a> BorrowInfState<'a> {
Call(call) => self.collect_call(z, call),
Literal(_) | RuntimeErrorFunction(_) => {}
GetTagId { structure: x, .. } => {
// if the structure (record/tag/array) is owned, the extracted value is
if self.is_owned(*x) {
self.own_var(z);
}
// if the extracted value is owned, the structure must be too
if self.is_owned(z) {
self.own_var(*x);
}
}
}
}

View File

@ -129,6 +129,12 @@ pub fn occurring_variables_expr(expr: &Expr<'_>, result: &mut MutSet<Symbol>) {
}
EmptyArray | RuntimeErrorFunction(_) | Literal(_) => {}
GetTagId {
structure: symbol, ..
} => {
result.insert(*symbol);
}
}
}
@ -753,6 +759,19 @@ impl<'a> Context<'a> {
// function pointers are persistent
self.arena.alloc(Stmt::Let(z, v, l, b))
}
GetTagId { structure: x, .. } => {
let b = self.add_dec_if_needed(x, b, b_live_vars);
let info_x = self.get_var_info(x);
let b = if info_x.consume {
self.add_inc(z, 1, b)
} else {
b
};
self.arena.alloc(Stmt::Let(z, v, l, b))
}
};
(new_b, live_vars)

View File

@ -1192,6 +1192,11 @@ pub enum Expr<'a> {
wrapped: Wrapped,
},
GetTagId {
structure: Symbol,
union_layout: UnionLayout<'a>,
},
Array {
elem_layout: Layout<'a>,
elems: &'a [Symbol],
@ -1344,6 +1349,10 @@ impl<'a> Expr<'a> {
.append(symbol_to_doc(alloc, *structure)),
RuntimeErrorFunction(s) => alloc.text(format!("ErrorFunction {}", s)),
GetTagId { structure, .. } => alloc
.text("GetTagId")
.append(symbol_to_doc(alloc, *structure)),
}
}
}
@ -5514,6 +5523,17 @@ fn substitute_in_expr<'a>(
}),
None => None,
},
GetTagId {
structure,
union_layout,
} => match substitute(subs, *structure) {
Some(structure) => Some(GetTagId {
structure,
union_layout: *union_layout,
}),
None => None,
},
}
}
@ -7577,7 +7597,7 @@ where
ToLowLevelCall: Fn(Symbol, Symbol, Option<Layout<'a>>, CallSpecId) -> Call<'a> + Copy,
{
match lambda_set.runtime_representation() {
Layout::Union(_) => {
Layout::Union(union_layout) => {
let closure_tag_id_symbol = env.unique_symbol();
let result = lowlevel_union_lambda_set_to_switch(
@ -7594,11 +7614,9 @@ where
);
// extract & assign the closure_tag_id_symbol
let expr = Expr::AccessAtIndex {
index: 0,
field_layouts: env.arena.alloc([Layout::Builtin(Builtin::Int64)]),
let expr = Expr::GetTagId {
structure: closure_data_symbol,
wrapped: Wrapped::MultiTagUnion,
union_layout,
};
Stmt::Let(