encode tag id in pointer and use it to read tag id

This commit is contained in:
Folkert 2021-07-15 23:44:00 +02:00
parent 0eb0d2457d
commit 23867296a4
2 changed files with 20 additions and 9 deletions

View file

@ -1154,13 +1154,15 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let tag_id_type =
basic_type_from_layout(env, &union_layout.tag_id_layout()).into_int_type();
let ptr = tag_pointer_clear_tag_id(env, argument.into_pointer_value());
lookup_at_index_ptr2(
env,
tag_id_type,
union_layout,
field_layouts,
*index as usize,
argument.into_pointer_value(),
ptr,
)
}
UnionLayout::NonNullableUnwrapped(field_layouts) => {
@ -1413,7 +1415,7 @@ pub fn build_tag<'a, 'ctx, 'env>(
builder.build_store(field_ptr, field_val);
}
raw_data_ptr.into()
tag_pointer_set_tag_id(env, tag_id, raw_data_ptr).into()
}
UnionLayout::NonNullableUnwrapped(fields) => {
debug_assert_eq!(union_size, 1);
@ -1683,7 +1685,7 @@ fn tag_pointer_set_tag_id<'a, 'ctx, 'env>(
.build_int_to_ptr(combined, pointer.get_type(), "to_ptr")
}
fn tag_pointer_read_tag_id<'a, 'ctx, 'env>(
pub fn tag_pointer_read_tag_id<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
pointer: PointerValue<'ctx>,
) -> IntValue<'ctx> {
@ -1700,7 +1702,7 @@ fn tag_pointer_read_tag_id<'a, 'ctx, 'env>(
.build_int_cast(masked, env.context.i8_type(), "to_u8")
}
fn tag_pointer_clear_tag_id<'a, 'ctx, 'env>(
pub fn tag_pointer_clear_tag_id<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
pointer: PointerValue<'ctx>,
) -> PointerValue<'ctx> {
@ -1785,7 +1787,11 @@ pub fn get_tag_id<'a, 'ctx, 'env>(
get_tag_id_non_recursive(env, tag)
}
UnionLayout::Recursive(_) => get_tag_id_wrapped(env, argument.into_pointer_value()),
UnionLayout::Recursive(_) => {
// TODO check that all tag ids fit into 2/3 bits
// get_tag_id_wrapped( env, tag_pointer_clear_tag_id(env, argument.into_pointer_value()),)
tag_pointer_read_tag_id(env, argument.into_pointer_value())
}
UnionLayout::NonNullableUnwrapped(_) => tag_id_int_type.const_zero(),
UnionLayout::NullableWrapped { nullable_id, .. } => {
let argument_ptr = argument.into_pointer_value();

View file

@ -1,7 +1,7 @@
use crate::debug_info_init;
use crate::llvm::build::{
add_func, cast_basic_basic, cast_block_of_memory_to_tag, get_tag_id, get_tag_id_non_recursive,
Env, FAST_CALL_CONV, LLVM_SADD_WITH_OVERFLOW_I64, TAG_DATA_INDEX,
tag_pointer_clear_tag_id, Env, FAST_CALL_CONV, LLVM_SADD_WITH_OVERFLOW_I64, TAG_DATA_INDEX,
};
use crate::llvm::build_list::{incrementing_elem_loop, list_len, load_list};
use crate::llvm::convert::{
@ -1233,7 +1233,8 @@ fn build_rec_union_help<'a, 'ctx, 'env>(
let parent = fn_val;
debug_assert!(arg_val.is_pointer_value());
let value_ptr = arg_val.into_pointer_value();
let current_tag_id = get_tag_id(env, fn_val, &union_layout, arg_val);
let value_ptr = tag_pointer_clear_tag_id(env, arg_val.into_pointer_value());
// to increment/decrement the cons-cell itself
let refcount_ptr = PointerToRefcount::from_ptr_to_data(env, value_ptr);
@ -1298,6 +1299,7 @@ fn build_rec_union_help<'a, 'ctx, 'env>(
union_layout,
tags,
value_ptr,
current_tag_id,
refcount_ptr,
do_recurse_block,
DecOrReuse::Dec,
@ -1322,6 +1324,7 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
union_layout: UnionLayout<'a>,
tags: &[&[Layout<'a>]],
value_ptr: PointerValue<'ctx>,
current_tag_id: IntValue<'ctx>,
refcount_ptr: PointerToRefcount<'ctx>,
match_block: BasicBlock<'ctx>,
decrement_or_reuse: DecOrReuse,
@ -1485,7 +1488,7 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
env.builder.build_unconditional_branch(only_branch);
} else {
// read the tag_id
let current_tag_id = get_tag_id(env, parent, &union_layout, value_ptr.into());
// let current_tag_id = get_tag_id(env, parent, &union_layout, value_ptr.into());
let default_block = env.context.append_basic_block(parent, "switch_default");
@ -1601,7 +1604,8 @@ fn build_reuse_rec_union_help<'a, 'ctx, 'env>(
let parent = reset_function;
debug_assert!(arg_val.is_pointer_value());
let value_ptr = arg_val.into_pointer_value();
let current_tag_id = get_tag_id(env, reset_function, &union_layout, arg_val);
let value_ptr = tag_pointer_clear_tag_id(env, arg_val.into_pointer_value());
// to increment/decrement the cons-cell itself
let refcount_ptr = PointerToRefcount::from_ptr_to_data(env, value_ptr);
@ -1654,6 +1658,7 @@ fn build_reuse_rec_union_help<'a, 'ctx, 'env>(
union_layout,
tags,
value_ptr,
current_tag_id,
refcount_ptr,
do_recurse_block,
DecOrReuse::Reuse,