Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Revert "Basic arc classes (breaks Array, MemoryAddress, References) (#…
Browse files Browse the repository at this point in the history
…177)"

This reverts commit 6593940.
  • Loading branch information
gavrilikhin-d committed May 19, 2024
1 parent 6593940 commit 58e96e0
Show file tree
Hide file tree
Showing 61 changed files with 2,797 additions and 2,018 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ indexmap = "2.2.6"
gimli = { version = "0.29.0", default-features = false }
insta = "1.38.0"
cmd_lib = "1.9.3"
derive-visitor = { version = "0.3.0", git = "https://github.com/andylokandy/derive-visitor", branch = "fix" }
derive-visitor = "0.3.0"

[build-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
6 changes: 0 additions & 6 deletions ppl/src/array.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ type Array<T>:
capacity: Integer
data: MemoryAddress

fn<T> default <:Type<Array<T>>> -> Array<T>:
let size = 0
let capacity = 0
let data = default MemoryAddress
return Array<T> { size, capacity, data }

/// Create an empty array
fn<T> <:Type<T>>[] -> Array<T>:
let capacity = 8
Expand Down
33 changes: 12 additions & 21 deletions ppl/src/core.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type None

/// Convert `None` to `String`
fn String from <:None> => "none"

fn default <:Type<None>> => none
//---------------------------------


Expand All @@ -40,8 +38,6 @@ fn default <:Type<None>> => none
@builtin
type Bool

fn default <:Type<Bool>> => false

/// Negate boolean value
fn not <x:Bool> -> Bool:
if x:
Expand Down Expand Up @@ -71,10 +67,11 @@ fn String from <x: Bool> -> String:
//=================================
// Integer
//=================================
@builtin
type Integer
type IntegerImpl

fn default <:Type<Integer>> => 0
@builtin
type Integer:
impl: Reference<IntegerImpl>

@mangle_as("integer_eq_integer")
fn <:Integer> == <:Integer> -> Bool
Expand Down Expand Up @@ -125,10 +122,11 @@ fn destroy <:&mut Integer>
//=================================
// Rational
//=================================
@builtin
type Rational
type RationalImpl

fn default <:Type<Rational>> => 0.0
@builtin
type Rational:
impl: Reference<RationalImpl>

@mangle_as("rational_eq_rational")
fn <:Rational> == <:Rational> -> Bool
Expand Down Expand Up @@ -167,10 +165,11 @@ fn clone <:&Rational> -> Rational
//=================================
// String
//=================================
@builtin
type String
type StringImpl

fn default <:Type<String>> => ""
@builtin
type String:
impl: Reference<StringImpl>

/// Concatenate 2 strings
@mangle_as("string_plus_string")
Expand Down Expand Up @@ -242,11 +241,3 @@ trait Clonnable:
fn clone <:&Self> -> Self
//=================================

//=================================
// Default
//=================================
/// Trait for things that have default value
trait Default:
fn default <:Type<Self>> -> Self
//=================================

2 changes: 0 additions & 2 deletions ppl/src/f64.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use core.*
@builtin
type F64

fn default <:Type<F64>> => F64 from 0.0

fn + <x: F64> => x

@mangle_as("minus_f64")
Expand Down
2 changes: 0 additions & 2 deletions ppl/src/i32.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use core.*
@builtin
type I32

fn default <:Type<I32>> => 0 as I32

fn + <x: I32> => x

@mangle_as("minus_i32")
Expand Down
2 changes: 0 additions & 2 deletions ppl/src/memory.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use math.*
type MemoryAddress:
value: Integer

fn default <:Type<MemoryAddress>> => MemoryAddress { value: 0 }

/// Interpret and integer as memory address
fn <value: Integer> as MemoryAddress => MemoryAddress { value }

Expand Down
7 changes: 1 addition & 6 deletions src/hir/declarations/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
sync::{Arc, RwLock},
};

use derive_visitor::{DriveMut, VisitorMut};
use derive_visitor::DriveMut;
use indexmap::IndexMap;

use crate::{
Expand Down Expand Up @@ -99,17 +99,12 @@ pub struct TraitData {
#[drive(skip)]
pub supertraits: Vec<Trait>,
/// Associated functions
#[drive(with = "drive_functions")]
pub functions: IndexMap<String, Function>,
/// Module this trait is located in
#[drive(skip)]
pub module: Module,
}

fn drive_functions<V: VisitorMut>(funcs: &mut IndexMap<String, Function>, visitor: &mut V) {
funcs.values_mut().for_each(|f| f.drive_mut(visitor));
}

impl TraitData {
/// Iterate over all functions
pub fn all_functions(&self) -> impl Iterator<Item = Function> + '_ {
Expand Down
10 changes: 9 additions & 1 deletion src/hir/declarations/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,15 @@ impl ClassData {
if let Some(builtin) = &self.builtin {
return builtin.size_in_bytes();
}
return POINTER_SIZE;

if self.is_opaque() {
return POINTER_SIZE;
}

self.members
.iter()
.map(|m| m.ty().size_in_bytes())
.sum::<usize>()
}
}

Expand Down
47 changes: 18 additions & 29 deletions src/ir/to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,24 @@ impl<'llvm, C: Context<'llvm>> ToIR<'llvm, C> for ClassData {
return context.types().f64().into();
}

if self.is_any_reference() {
if self.members.is_empty() {
return context.types().opaque(&self.basename).into();
}

let field_types = self
.members
.iter()
.filter_map(|m| m.ty().to_ir(context).try_into_basic_type().ok())
.collect::<Vec<_>>();
context.types().arc(&self.name(), &field_types).into()
if let Some(ty) = context.llvm().get_struct_type(&self.name()) {
return ty.into();
}

let ty = context.llvm().opaque_struct_type(&self.name());
ty.set_body(
self.members
.iter()
.filter_map(|m| m.ty().to_ir(context).try_into_basic_type().ok())
.collect::<Vec<_>>()
.as_slice(),
false,
);
ty.into()
}
}

Expand Down Expand Up @@ -560,25 +568,12 @@ impl<'llvm, 'm> ToIR<'llvm, FunctionContext<'llvm, 'm, '_>> for Constructor {
.expect("non-basic type constructor");
let alloca = context.builder.build_alloca(ty, "").unwrap();

let ptr = context
.builder
.build_struct_gep(
ty,
alloca,
0,
format!("{}.data", self.ty.referenced_type.name()).as_str(),
)
.unwrap();
let data_ty = context
.types()
.arc_data(&self.ty.referenced_type.name())
.unwrap();
for init in self.initializers.iter().filter(|i| !i.value.ty().is_none()) {
let field = context
.builder
.build_struct_gep(
data_ty,
ptr,
ty,
alloca,
init.index as u32,
format!("{}.{}", self.ty.referenced_type.name(), init.member.name()).as_str(),
)
Expand Down Expand Up @@ -614,16 +609,10 @@ impl<'llvm, 'm> HIRExpressionLoweringWithoutLoad<'llvm, 'm> for MemberReference

let base = base.unwrap().into_pointer_value();
let ty = self.base.ty().to_ir(context).try_into_basic_type().unwrap();
let ptr = context
.builder
.build_struct_gep(ty, base, 0, "")
.unwrap()
.into();
let data_ty = context.types().arc_data(&self.base.ty().name()).unwrap();
Some(
context
.builder
.build_struct_gep(data_ty, ptr, self.index as u32, &self.member.name())
.build_struct_gep(ty, base, self.index as u32, &self.member.name())
.unwrap()
.into(),
)
Expand Down
29 changes: 7 additions & 22 deletions src/ir/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use inkwell::{
context::ContextRef,
types::{BasicTypeEnum, FloatType, IntType, PointerType, StructType, VoidType},
types::{FloatType, IntType, PointerType, StructType, VoidType},
AddressSpace,
};

Expand Down Expand Up @@ -71,30 +71,20 @@ impl<'llvm> Types<'llvm> {
}

/// Get wrapper around pointer to opaque impl
pub fn arc(&self, name: &str, field_types: &[BasicTypeEnum<'llvm>]) -> StructType<'llvm> {
fn with_impl(&self, name: &str) -> StructType<'llvm> {
if let Some(ty) = self.llvm.get_struct_type(name) {
return ty;
}

let data_ty = self.llvm.opaque_struct_type(&format!("{name}Impl"));
if !field_types.is_empty() {
data_ty.set_body(field_types, false);
}

let ty = self.llvm.opaque_struct_type(name);
ty.set_body(&[self.pointer().into()], false);
ty.set_body(&[self.opaque(&format!("{name}Impl")).into()], false);
ty
}

/// Get LLVM struct type for data of ARC type
pub fn arc_data(&self, name: &str) -> Option<StructType<'llvm>> {
self.llvm.get_struct_type(&format!("{name}Impl"))
}

/// LLVM IR for [`Class`](Type::Class) type
pub fn opaque(&self, name: &str) -> PointerType<'llvm> {
self.get_or_add_opaque_struct(name);
self.pointer()
self.llvm.ptr_type(AddressSpace::default())
}

/// LLVM IR for [`None`](Type::None) type
Expand All @@ -104,26 +94,21 @@ impl<'llvm> Types<'llvm> {

/// LLVM IR for [`Integer`](Type::Integer) type
pub fn integer(&self) -> StructType<'llvm> {
self.arc("Integer", &[])
self.with_impl("Integer")
}

/// LLVM IR for `Rational` type
pub fn rational(&self) -> StructType<'llvm> {
self.arc("Rational", &[])
self.with_impl("Rational")
}

/// LLVM IR for [`String`](Type::String) type
pub fn string(&self) -> StructType<'llvm> {
self.arc("String", &[])
self.with_impl("String")
}

/// LLVM IR for C string type
pub fn c_string(&self) -> PointerType<'llvm> {
self.pointer()
}

/// LLVM IR for pointer type
pub fn pointer(&self) -> PointerType<'llvm> {
self.llvm.ptr_type(AddressSpace::default())
}
}
2 changes: 1 addition & 1 deletion src/semantics/link_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'ctx, C: Context> TraitFunctionsLinker<'ctx, C> {

fn enter_module_data(&mut self, module: &mut ModuleData) {
module.monomorphized_functions.drive_mut(self);
module.iter_functions_mut().for_each(|f| f.drive_mut(self));
module.functions.drive_mut(self);
}

fn enter_function_data(&mut self, f: &mut FunctionData) {
Expand Down
Loading

0 comments on commit 58e96e0

Please sign in to comment.