From c1265574b394239dbbfa0278d5516664ca282e3a Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 17 Jan 2021 02:26:04 +0100 Subject: [PATCH] drop the tag! --- compiler/gen/src/llvm/build.rs | 51 +++++++++++++++++----------- compiler/gen/src/llvm/convert.rs | 2 +- compiler/gen/src/llvm/refcounting.rs | 8 +++-- compiler/gen/tests/gen_primitives.rs | 18 +++++----- compiler/mono/src/decision_tree.rs | 1 - compiler/mono/src/ir.rs | 2 -- 6 files changed, 48 insertions(+), 34 deletions(-) diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 654ffab860..969a1796d1 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -28,7 +28,7 @@ use inkwell::debug_info::{ use inkwell::memory_buffer::MemoryBuffer; use inkwell::module::{Linkage, Module}; 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::{ BasicValue, CallSiteValue, FloatValue, FunctionValue, InstructionOpcode, InstructionValue, @@ -1149,6 +1149,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>( tag_name, .. } => { + let other_fields = &other_fields[1..]; + let tag_struct_type = 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(_))); - let tag_field_layouts = &other_fields[0..]; - let arguments = &arguments[0..]; + let tag_field_layouts = other_fields; + let arguments = &arguments[1..]; 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); } - // 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 let (argument, structure_layout) = load_symbol_and_layout(env, scope, structure); - let struct_layout = Layout::Struct(field_layouts); match argument { 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 result = builder @@ -1403,24 +1404,36 @@ pub fn build_exp_expr<'a, 'ctx, 'env>( "select_tag_id", ) } 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( env, - field_layouts, - *index as usize, + &field_layouts[1..], + *index as usize - 1, value, &struct_layout, struct_type, ) } } - _ => lookup_at_index_ptr( - env, - field_layouts, - *index as usize, - value, - &struct_layout, - struct_type, - ), + _ => { + let struct_layout = Layout::Struct(field_layouts); + let struct_type = env + .context + .struct_type(field_types.into_bump_slice(), false); + + lookup_at_index_ptr( + env, + field_layouts, + *index as usize, + value, + &struct_layout, + struct_type, + ) + } }, _ => panic!("cannot look up index in {:?}", argument), } diff --git a/compiler/gen/src/llvm/convert.rs b/compiler/gen/src/llvm/convert.rs index 75a6ce09c0..2ba888eeaa 100644 --- a/compiler/gen/src/llvm/convert.rs +++ b/compiler/gen/src/llvm/convert.rs @@ -143,7 +143,7 @@ pub fn basic_type_from_layout<'ctx>( block.ptr_type(AddressSpace::Generic).into() } 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() } NonRecursive(_) => block_of_memory(context, layout, ptr_bytes), diff --git a/compiler/gen/src/llvm/refcounting.rs b/compiler/gen/src/llvm/refcounting.rs index 19af562935..af15954af6 100644 --- a/compiler/gen/src/llvm/refcounting.rs +++ b/compiler/gen/src/llvm/refcounting.rs @@ -359,10 +359,12 @@ pub fn decrement_refcount_layout<'a, 'ctx, 'env>( NullableUnwrapped { other_fields, .. } => { debug_assert!(value.is_pointer_value()); + let other_fields = &other_fields[1..]; + build_dec_rec_union( env, layout_ids, - &*env.arena.alloc([*other_fields]), + &*env.arena.alloc([other_fields]), value.into_pointer_value(), true, ); @@ -468,10 +470,12 @@ pub fn increment_refcount_layout<'a, 'ctx, 'env>( NullableUnwrapped { other_fields, .. } => { debug_assert!(value.is_pointer_value()); + let other_fields = &other_fields[1..]; + build_inc_rec_union( env, layout_ids, - &*env.arena.alloc([*other_fields]), + &*env.arena.alloc([other_fields]), value.into_pointer_value(), true, ); diff --git a/compiler/gen/tests/gen_primitives.rs b/compiler/gen/tests/gen_primitives.rs index 0708eaa0ea..22517e01e6 100644 --- a/compiler/gen/tests/gen_primitives.rs +++ b/compiler/gen/tests/gen_primitives.rs @@ -564,7 +564,7 @@ mod gen_primitives { Cons _ rest -> 1 + len rest main = - nil : LinkedList {} + nil : LinkedList F64 nil = Nil len nil @@ -584,7 +584,7 @@ mod gen_primitives { LinkedList a : [ Nil, Cons a (LinkedList a) ] - nil : LinkedList (Int *) + nil : LinkedList I64 nil = Nil length : LinkedList a -> Int * @@ -1407,9 +1407,9 @@ mod gen_primitives { balance 0 Empty "# ), - 1, - &i64, - |x: &i64| *x + false, + *const i64, + |x: *const i64| x.is_null() ); } @@ -1500,14 +1500,14 @@ mod gen_primitives { _ -> Node color key value left right - main : RedBlackTree (Int *) (Int *) + main : RedBlackTree F64 F64 main = balance Red 0 0 Empty Empty "# ), - 1, - &i64, - |x: &i64| *x + false, + *const i64, + |x: *const i64| x.is_null() ); } diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index 1ac30ee9d7..4cad47affe 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -1020,7 +1020,6 @@ fn path_to_expr_help<'a>( .. } => { use std::cmp::Ordering; - dbg!(nullable_id, tag_id); match (*tag_id as usize).cmp(&(*nullable_id as usize)) { Ordering::Equal => { // the nullable tag is going to pretend it stores a tag id diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index e90a8a7479..df3c6e2113 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -6225,8 +6225,6 @@ fn from_can_pattern_help<'a>( } => { debug_assert!(!other_fields.is_empty()); - dbg!(nullable_id, &nullable_name); - ctors.push(Ctor { tag_id: TagId(nullable_id as u8), name: nullable_name.clone(),