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

Commit 2fd4aed

Browse files
Small improvements (#178)
* Revert last 4 arc commits (#179) * Revert "Basic arc classes (breaks Array, MemoryAddress, References) (#177)" This reverts commit 6593940. * Revert "Make string to be arc (#176)" This reverts commit e3a2298. * Revert "Make rational to be arc (#175)" This reverts commit 8c52c17. * Revert "Pass by ref for integer (#174)" This reverts commit 584df80. * Small improvements
1 parent 84e0102 commit 2fd4aed

File tree

55 files changed

+5153
-2092
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+5153
-2092
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
### Current task
3737
* [ ] Printable trait should take references
3838
---
39+
* [ ] migrate to pass-by-ref (branch `arc`)
3940
* [ ] Prefer candidates with mutable references, when possible
4041
* [ ] Fix problems with to_ir and loading references (especially globals). This causes issues in iterator
4142
* [ ] Benchmark for linear algebra

ppl/src/core.ppl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ fn String from <x: Bool> -> String:
7171
//=================================
7272
// Integer
7373
//=================================
74+
type IntegerImpl
75+
7476
@builtin
75-
type Integer
77+
type Integer:
78+
impl: Reference<IntegerImpl>
7679

80+
@builtin
7781
fn default <:Type<Integer>> => 0
7882

7983
@mangle_as("integer_eq_integer")
@@ -115,18 +119,21 @@ fn <x: Integer> % <y: Integer> -> Integer
115119
@mangle_as("integer_as_string")
116120
fn String from <:Integer> -> String
117121

118-
@mangle_as("clone_integer")
119-
fn clone <:&Integer> -> Integer
120-
121122
@mangle_as("destroy_integer")
122123
fn destroy <:&mut Integer>
124+
125+
@mangle_as("clone_integer")
126+
fn clone <:&Integer> -> Integer
123127
//---------------------------------
124128

125129
//=================================
126130
// Rational
127131
//=================================
132+
type RationalImpl
133+
128134
@builtin
129-
type Rational
135+
type Rational:
136+
impl: Reference<RationalImpl>
130137

131138
fn default <:Type<Rational>> => 0.0
132139

@@ -167,8 +174,11 @@ fn clone <:&Rational> -> Rational
167174
//=================================
168175
// String
169176
//=================================
177+
type StringImpl
178+
170179
@builtin
171-
type String
180+
type String:
181+
impl: Reference<StringImpl>
172182

173183
fn default <:Type<String>> => ""
174184

src/hir/declarations/types.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,15 @@ impl ClassData {
402402
if let Some(builtin) = &self.builtin {
403403
return builtin.size_in_bytes();
404404
}
405-
return POINTER_SIZE;
405+
406+
if self.is_opaque() {
407+
return POINTER_SIZE;
408+
}
409+
410+
self.members
411+
.iter()
412+
.map(|m| m.ty().size_in_bytes())
413+
.sum::<usize>()
406414
}
407415
}
408416

src/ir/to_ir.rs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,24 @@ impl<'llvm, C: Context<'llvm>> ToIR<'llvm, C> for ClassData {
247247
return context.types().f64().into();
248248
}
249249

250-
if self.is_any_reference() {
250+
if self.members.is_empty() {
251251
return context.types().opaque(&self.basename).into();
252252
}
253253

254-
let field_types = self
255-
.members
256-
.iter()
257-
.filter_map(|m| m.ty().to_ir(context).try_into_basic_type().ok())
258-
.collect::<Vec<_>>();
259-
context.types().arc(&self.name(), &field_types).into()
254+
if let Some(ty) = context.llvm().get_struct_type(&self.name()) {
255+
return ty.into();
256+
}
257+
258+
let ty = context.llvm().opaque_struct_type(&self.name());
259+
ty.set_body(
260+
self.members
261+
.iter()
262+
.filter_map(|m| m.ty().to_ir(context).try_into_basic_type().ok())
263+
.collect::<Vec<_>>()
264+
.as_slice(),
265+
false,
266+
);
267+
ty.into()
260268
}
261269
}
262270

@@ -559,30 +567,12 @@ impl<'llvm, 'm> ToIR<'llvm, FunctionContext<'llvm, 'm, '_>> for Constructor {
559567
.try_into_basic_type()
560568
.expect("non-basic type constructor");
561569
let alloca = context.builder.build_alloca(ty, "").unwrap();
562-
563-
let ptr = context.builder.build_struct_gep(ty, alloca, 0, "").unwrap();
564-
let data_ty = context
565-
.types()
566-
.arc_data(&self.ty.referenced_type.name())
567-
.unwrap();
568-
let arc = context
569-
.builder
570-
.build_call(
571-
context.functions().create_arc(),
572-
&[data_ty.size_of().unwrap().into()],
573-
"",
574-
)
575-
.unwrap()
576-
.try_as_basic_value()
577-
.left()
578-
.unwrap();
579-
context.builder.build_store(ptr, arc).unwrap();
580570
for init in self.initializers.iter().filter(|i| !i.value.ty().is_none()) {
581571
let field = context
582572
.builder
583573
.build_struct_gep(
584-
data_ty,
585-
ptr,
574+
ty,
575+
alloca,
586576
init.index as u32,
587577
format!("{}.{}", self.ty.referenced_type.name(), init.member.name()).as_str(),
588578
)
@@ -618,16 +608,10 @@ impl<'llvm, 'm> HIRExpressionLoweringWithoutLoad<'llvm, 'm> for MemberReference
618608

619609
let base = base.unwrap().into_pointer_value();
620610
let ty = self.base.ty().to_ir(context).try_into_basic_type().unwrap();
621-
let ptr = context
622-
.builder
623-
.build_struct_gep(ty, base, 0, "")
624-
.unwrap()
625-
.into();
626-
let data_ty = context.types().arc_data(&self.base.ty().name()).unwrap();
627611
Some(
628612
context
629613
.builder
630-
.build_struct_gep(data_ty, ptr, self.index as u32, &self.member.name())
614+
.build_struct_gep(ty, base, self.index as u32, &self.member.name())
631615
.unwrap()
632616
.into(),
633617
)

src/ir/types.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use inkwell::{
22
context::ContextRef,
3-
types::{BasicTypeEnum, FloatType, IntType, PointerType, StructType, VoidType},
3+
types::{FloatType, IntType, PointerType, StructType, VoidType},
44
AddressSpace,
55
};
66

@@ -71,26 +71,16 @@ impl<'llvm> Types<'llvm> {
7171
}
7272

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

79-
let data_ty = self.llvm.opaque_struct_type(&format!("{name}Impl"));
80-
if !field_types.is_empty() {
81-
data_ty.set_body(field_types, false);
82-
}
83-
8479
let ty = self.llvm.opaque_struct_type(name);
85-
ty.set_body(&[self.pointer().into()], false);
80+
ty.set_body(&[self.opaque(&format!("{name}Impl")).into()], false);
8681
ty
8782
}
8883

89-
/// Get LLVM struct type for data of ARC type
90-
pub fn arc_data(&self, name: &str) -> Option<StructType<'llvm>> {
91-
self.llvm.get_struct_type(&format!("{name}Impl"))
92-
}
93-
9484
/// LLVM IR for [`Class`](Type::Class) type
9585
pub fn opaque(&self, name: &str) -> PointerType<'llvm> {
9686
self.get_or_add_opaque_struct(name);
@@ -104,17 +94,17 @@ impl<'llvm> Types<'llvm> {
10494

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

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

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

120110
/// LLVM IR for C string type

src/runtime/src/assert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::String;
77
/// ```
88
#[no_mangle]
99
pub extern "C" fn assert(condition: bool, message: &String) {
10-
let message = message.as_ref();
10+
let message = unsafe { message.data.as_ref().unwrap() };
1111
if !condition {
1212
println!("Assertion failed: {message}");
1313
}

src/runtime/src/integer.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,40 @@ use std::ffi::c_char;
22

33
use rug::ops::Pow;
44

5-
use crate::{decrement_strong_count, increment_strong_count, Rational, String};
6-
7-
use std::sync::Arc;
5+
use crate::{Rational, String};
86

97
/// Big integer number.
108
/// Wrapper around pointer to [`rug::Integer`].
119
///
1210
/// # PPL
1311
/// ```no_run
12+
/// type IntegerImpl
13+
///
1414
/// @builtin
15-
/// type Integer
15+
/// type Integer:
16+
/// impl: Reference<IntegerImpl>
1617
/// ```
1718
#[repr(C)]
18-
pub struct Integer(pub *const rug::Integer);
19+
pub struct Integer {
20+
pub data: *mut rug::Integer,
21+
}
1922

2023
impl Clone for Integer {
2124
fn clone(&self) -> Self {
22-
increment_strong_count(self.0 as *const _);
23-
Self(self.0)
25+
self.as_ref().into()
2426
}
2527
}
2628

2729
impl Drop for Integer {
2830
fn drop(&mut self) {
29-
decrement_strong_count(self.0 as *const _);
31+
let _ = unsafe { Box::from_raw(self.data) };
3032
}
3133
}
3234

3335
impl Integer {
3436
/// Get the inner value
3537
pub fn as_ref(&self) -> &rug::Integer {
36-
unsafe { &*self.0 }
38+
unsafe { &*self.data }
3739
}
3840
}
3941

@@ -42,8 +44,10 @@ where
4244
rug::Integer: From<T>,
4345
{
4446
fn from(x: T) -> Self {
45-
let this = Arc::new(rug::Integer::from(x));
46-
Self(Arc::into_raw(this))
47+
let this = Box::new(rug::Integer::from(x));
48+
Self {
49+
data: Box::into_raw(this),
50+
}
4751
}
4852
}
4953

@@ -215,7 +219,7 @@ pub extern "C" fn integer_mod_integer(x: Integer, y: Integer) -> Integer {
215219
/// ```
216220
#[no_mangle]
217221
pub extern "C" fn destroy_integer(x: &mut Integer) {
218-
decrement_strong_count(x.0 as *const _);
222+
let _ = unsafe { Box::from_raw(x.data) };
219223
}
220224

221225
/// # PPL

src/runtime/src/memory.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::Arc;
2-
31
use libc::{c_void, malloc, memcpy, size_t};
42

53
use crate::{integer_from_i64, integer_from_u64, Integer, String, Type};
@@ -108,19 +106,3 @@ pub extern "C" fn copy_bytes(n: &Integer, src: &MemoryAddress, dst: &MemoryAddre
108106
let n = n.as_ref().to_usize().unwrap() as size_t;
109107
unsafe { memcpy(dest, src, n) };
110108
}
111-
112-
#[no_mangle]
113-
pub extern "C" fn create_arc(bytes: usize) -> *const c_void {
114-
let bytes = unsafe { Arc::<[u8]>::new_zeroed_slice(bytes).assume_init() };
115-
Arc::into_raw(bytes) as *const c_void
116-
}
117-
118-
#[no_mangle]
119-
pub extern "C" fn increment_strong_count(ptr: *const c_void) {
120-
unsafe { Arc::increment_strong_count(ptr) }
121-
}
122-
123-
#[no_mangle]
124-
pub extern "C" fn decrement_strong_count(ptr: *const c_void) {
125-
unsafe { Arc::decrement_strong_count(ptr) }
126-
}

src/runtime/src/rational.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::{ffi::c_char, sync::Arc};
1+
use std::ffi::c_char;
22

33
use rug::{ops::Pow, Integer};
44

5-
use crate::{decrement_strong_count, increment_strong_count, String};
5+
use crate::String;
66

77
/// Rational number.
88
/// Wrapper around pointer to [`rug::Rational`].
@@ -16,25 +16,26 @@ use crate::{decrement_strong_count, increment_strong_count, String};
1616
/// impl: Reference<RationalImpl>
1717
/// ```
1818
#[repr(C)]
19-
pub struct Rational(pub *const rug::Rational);
19+
pub struct Rational {
20+
pub data: *mut rug::Rational,
21+
}
2022

2123
impl Clone for Rational {
2224
fn clone(&self) -> Self {
23-
increment_strong_count(self.0 as *const _);
24-
Self(self.0)
25+
self.as_ref().into()
2526
}
2627
}
2728

2829
impl Drop for Rational {
2930
fn drop(&mut self) {
30-
decrement_strong_count(self.0 as *const _);
31+
let _ = unsafe { Box::from_raw(self.data) };
3132
}
3233
}
3334

3435
impl Rational {
3536
/// Get the inner value
3637
pub fn as_ref(&self) -> &rug::Rational {
37-
unsafe { &*self.0 }
38+
unsafe { &*self.data }
3839
}
3940
}
4041

@@ -43,8 +44,10 @@ where
4344
rug::Rational: From<T>,
4445
{
4546
fn from(x: T) -> Self {
46-
let this = Arc::new(rug::Rational::from(x));
47-
Self(Arc::into_raw(this))
47+
let this = Box::new(rug::Rational::from(x));
48+
Self {
49+
data: Box::into_raw(this),
50+
}
4851
}
4952
}
5053

@@ -156,7 +159,7 @@ pub extern "C" fn rational_less_rational(x: Rational, y: Rational) -> bool {
156159
/// ```
157160
#[no_mangle]
158161
pub extern "C" fn destroy_rational(x: &mut Rational) {
159-
decrement_strong_count(x.0 as *const _);
162+
let _ = unsafe { Box::from_raw(x.data) };
160163
}
161164

162165
/// # PPL

0 commit comments

Comments
 (0)