drop the tag!

This commit is contained in:
Folkert 2021-01-17 02:26:04 +01:00
parent 897bc41b6b
commit c1265574b3
6 changed files with 48 additions and 34 deletions

View file

@ -28,7 +28,7 @@ use inkwell::debug_info::{
use inkwell::memory_buffer::MemoryBuffer; use inkwell::memory_buffer::MemoryBuffer;
use inkwell::module::{Linkage, Module}; use inkwell::module::{Linkage, Module};
use inkwell::passes::{PassManager, PassManagerBuilder}; use inkwell::passes::{PassManager, PassManagerBuilder};
use inkwell::types::{BasicTypeEnum, FunctionType, IntType, PointerType, StructType}; use inkwell::types::{BasicTypeEnum, FunctionType, IntType, StructType};
use inkwell::values::BasicValueEnum::{self, *}; use inkwell::values::BasicValueEnum::{self, *};
use inkwell::values::{ use inkwell::values::{
BasicValue, CallSiteValue, FloatValue, FunctionValue, InstructionOpcode, InstructionValue, BasicValue, CallSiteValue, FloatValue, FunctionValue, InstructionOpcode, InstructionValue,
@ -1149,6 +1149,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
tag_name, tag_name,
.. ..
} => { } => {
let other_fields = &other_fields[1..];
let tag_struct_type = let tag_struct_type =
block_of_memory_slices(env.context, &[other_fields], env.ptr_bytes); block_of_memory_slices(env.context, &[other_fields], env.ptr_bytes);
@ -1175,8 +1177,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
debug_assert!(!matches!(tag_name, TagName::Closure(_))); debug_assert!(!matches!(tag_name, TagName::Closure(_)));
let tag_field_layouts = &other_fields[0..]; let tag_field_layouts = other_fields;
let arguments = &arguments[0..]; let arguments = &arguments[1..];
debug_assert_eq!(arguments.len(), tag_field_layouts.len()); debug_assert_eq!(arguments.len(), tag_field_layouts.len());
@ -1317,17 +1319,16 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
field_types.push(field_type); field_types.push(field_type);
} }
// Create the struct_type
let struct_type = env
.context
.struct_type(field_types.into_bump_slice(), false);
// cast the argument bytes into the desired shape for this tag // cast the argument bytes into the desired shape for this tag
let (argument, structure_layout) = load_symbol_and_layout(env, scope, structure); let (argument, structure_layout) = load_symbol_and_layout(env, scope, structure);
let struct_layout = Layout::Struct(field_layouts);
match argument { match argument {
StructValue(value) => { StructValue(value) => {
let struct_layout = Layout::Struct(field_layouts);
let struct_type = env
.context
.struct_type(field_types.into_bump_slice(), false);
let struct_value = cast_struct_struct(builder, value, struct_type); let struct_value = cast_struct_struct(builder, value, struct_type);
let result = builder let result = builder
@ -1403,24 +1404,36 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
"select_tag_id", "select_tag_id",
) )
} else { } else {
let struct_layout = Layout::Struct(&field_layouts[1..]);
let struct_type = env
.context
.struct_type(&field_types.into_bump_slice()[1..], false);
lookup_at_index_ptr( lookup_at_index_ptr(
env, env,
field_layouts, &field_layouts[1..],
*index as usize, *index as usize - 1,
value, value,
&struct_layout, &struct_layout,
struct_type, struct_type,
) )
} }
} }
_ => lookup_at_index_ptr( _ => {
env, let struct_layout = Layout::Struct(field_layouts);
field_layouts, let struct_type = env
*index as usize, .context
value, .struct_type(field_types.into_bump_slice(), false);
&struct_layout,
struct_type, lookup_at_index_ptr(
), env,
field_layouts,
*index as usize,
value,
&struct_layout,
struct_type,
)
}
}, },
_ => panic!("cannot look up index in {:?}", argument), _ => panic!("cannot look up index in {:?}", argument),
} }

View file

@ -143,7 +143,7 @@ pub fn basic_type_from_layout<'ctx>(
block.ptr_type(AddressSpace::Generic).into() block.ptr_type(AddressSpace::Generic).into()
} }
NullableUnwrapped { other_fields, .. } => { NullableUnwrapped { other_fields, .. } => {
let block = block_of_memory_slices(context, &[other_fields], ptr_bytes); let block = block_of_memory_slices(context, &[&other_fields[1..]], ptr_bytes);
block.ptr_type(AddressSpace::Generic).into() block.ptr_type(AddressSpace::Generic).into()
} }
NonRecursive(_) => block_of_memory(context, layout, ptr_bytes), NonRecursive(_) => block_of_memory(context, layout, ptr_bytes),

View file

@ -359,10 +359,12 @@ pub fn decrement_refcount_layout<'a, 'ctx, 'env>(
NullableUnwrapped { other_fields, .. } => { NullableUnwrapped { other_fields, .. } => {
debug_assert!(value.is_pointer_value()); debug_assert!(value.is_pointer_value());
let other_fields = &other_fields[1..];
build_dec_rec_union( build_dec_rec_union(
env, env,
layout_ids, layout_ids,
&*env.arena.alloc([*other_fields]), &*env.arena.alloc([other_fields]),
value.into_pointer_value(), value.into_pointer_value(),
true, true,
); );
@ -468,10 +470,12 @@ pub fn increment_refcount_layout<'a, 'ctx, 'env>(
NullableUnwrapped { other_fields, .. } => { NullableUnwrapped { other_fields, .. } => {
debug_assert!(value.is_pointer_value()); debug_assert!(value.is_pointer_value());
let other_fields = &other_fields[1..];
build_inc_rec_union( build_inc_rec_union(
env, env,
layout_ids, layout_ids,
&*env.arena.alloc([*other_fields]), &*env.arena.alloc([other_fields]),
value.into_pointer_value(), value.into_pointer_value(),
true, true,
); );

View file

@ -564,7 +564,7 @@ mod gen_primitives {
Cons _ rest -> 1 + len rest Cons _ rest -> 1 + len rest
main = main =
nil : LinkedList {} nil : LinkedList F64
nil = Nil nil = Nil
len nil len nil
@ -584,7 +584,7 @@ mod gen_primitives {
LinkedList a : [ Nil, Cons a (LinkedList a) ] LinkedList a : [ Nil, Cons a (LinkedList a) ]
nil : LinkedList (Int *) nil : LinkedList I64
nil = Nil nil = Nil
length : LinkedList a -> Int * length : LinkedList a -> Int *
@ -1407,9 +1407,9 @@ mod gen_primitives {
balance 0 Empty balance 0 Empty
"# "#
), ),
1, false,
&i64, *const i64,
|x: &i64| *x |x: *const i64| x.is_null()
); );
} }
@ -1500,14 +1500,14 @@ mod gen_primitives {
_ -> _ ->
Node color key value left right Node color key value left right
main : RedBlackTree (Int *) (Int *) main : RedBlackTree F64 F64
main = main =
balance Red 0 0 Empty Empty balance Red 0 0 Empty Empty
"# "#
), ),
1, false,
&i64, *const i64,
|x: &i64| *x |x: *const i64| x.is_null()
); );
} }

View file

@ -1020,7 +1020,6 @@ fn path_to_expr_help<'a>(
.. ..
} => { } => {
use std::cmp::Ordering; use std::cmp::Ordering;
dbg!(nullable_id, tag_id);
match (*tag_id as usize).cmp(&(*nullable_id as usize)) { match (*tag_id as usize).cmp(&(*nullable_id as usize)) {
Ordering::Equal => { Ordering::Equal => {
// the nullable tag is going to pretend it stores a tag id // the nullable tag is going to pretend it stores a tag id

View file

@ -6225,8 +6225,6 @@ fn from_can_pattern_help<'a>(
} => { } => {
debug_assert!(!other_fields.is_empty()); debug_assert!(!other_fields.is_empty());
dbg!(nullable_id, &nullable_name);
ctors.push(Ctor { ctors.push(Ctor {
tag_id: TagId(nullable_id as u8), tag_id: TagId(nullable_id as u8),
name: nullable_name.clone(), name: nullable_name.clone(),