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

Array-print #169

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ppl/src/array.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ fn<T> push <x: T> to <array: &mut Array<T>>:
let new_data = allocate new_capacity T
let mut i = 0
while i < array.size:
let value = T at (new_data + i * bytes)
value = array[i]
let offset = i * bytes
copy bytes bytes from (array.data + offset) to (new_data + offset)
i += 1
free array.data
array.data = new_data
array.capacity = new_capacity

let value = T at (array.data + array.size * bytes)
value = x
copy bytes bytes from (address of x) to (array.data + array.size * bytes)
array.size += 1

fn<T> <array: &Array<T>> is empty => array.size == 0
fn<T> <array: &Array<T>> is not empty => array.size > 0

fn<U: Printable> String from <array: &Array<U>> -> String:
fn<U: Printable> String from <array: Array<U>> -> String:
let mut str = "["
if array is not empty:
str += (String from array[0])
Expand Down
17 changes: 14 additions & 3 deletions ppl/src/memory.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ fn<T> allocate <n: Integer> <:Type<T>> => allocate n * (size of T) bytes

/// Free previously allocated memory
@mangle_as("free_memory")
fn free <address: MemoryAddress>
fn free <address: &MemoryAddress>

// TODO: use `@` instead of `at`
/// Value of specific type at memory address
@mangle_as("read_memory")
fn<T> <ty: Type<T>> at <address: MemoryAddress> -> ReferenceMut<T>
fn<T> <ty: Type<T>> at <address: &MemoryAddress> -> ReferenceMut<T>

/// Get memory address of a reference
@mangle_as("address_of")
fn<T> address of <ref: &T> -> MemoryAddress
fn<T> address of <ref: &T> -> MemoryAddress

/// Copy `n` bytes from `src` to `dst`
@mangle_as("copy_bytes")
fn copy <n: &Integer> bytes from <src: &MemoryAddress> to <dst: &MemoryAddress>

/// Copy `T` from `src` to `dst`
fn<T> copy <:Type<T>> from <src: &MemoryAddress> to <dst: &MemoryAddress>:
copy (size of T) bytes from src to dst

fn<T> copy <src: &T> to <dst: &mut T>:
copy T from (address of src) to (address of dst)
28 changes: 22 additions & 6 deletions src/runtime/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libc::{c_void, malloc};
use libc::{c_void, malloc, memcpy, size_t};

use crate::{integer_from_i64, integer_from_u64, Integer, String, Type};

Expand Down Expand Up @@ -47,10 +47,10 @@ pub extern "C" fn allocate_n_bytes(n: Integer) -> MemoryAddress {

/// # PPL
/// ```no_run
/// fn free <address: MemoryAddress>
/// fn free <address: &MemoryAddress>
/// ```
#[no_mangle]
pub extern "C" fn free_memory(address: MemoryAddress) {
pub extern "C" fn free_memory(address: &MemoryAddress) {
let address = unsafe { address.value.data.as_ref().unwrap() };

let address = address.to_u64();
Expand All @@ -66,12 +66,14 @@ pub extern "C" fn free_memory(address: MemoryAddress) {

/// # PPL
/// ```no_run
/// fn<T> <ty: Type<T>> at <address: MemoryAddress> -> Reference<T>
/// fn<T> <ty: Type<T>> at <address: &MemoryAddress> -> Reference<T>
/// ```
#[no_mangle]
pub extern "C" fn read_memory(ty: Type, address: MemoryAddress) -> *mut c_void {
let _ = ty;
pub extern "C" fn read_memory(_ty: Type, address: &MemoryAddress) -> *mut c_void {
read_memory_impl(address)
}

fn read_memory_impl(address: &MemoryAddress) -> *mut c_void {
let address = unsafe { address.value.data.as_ref().unwrap() };

let address = address.to_u64().unwrap();
Expand All @@ -93,3 +95,17 @@ pub extern "C" fn address_of(ptr: *const c_void) -> MemoryAddress {
value: integer_from_u64(address as u64),
}
}

/// # PPL
/// ```no_run
/// /// Copy `n` bytes from `src` to `dst`
/// @mangle_as("copy_bytes")
/// fn copy <n: &Integer> bytes from <src: &MemoryAddress> to <dst: &MemoryAddress>
/// ```
#[no_mangle]
pub extern "C" fn copy_bytes(n: &Integer, src: &MemoryAddress, dst: &MemoryAddress) {
let dest = read_memory_impl(dst);
let src = read_memory_impl(src);
let n = unsafe { n.data.as_ref().unwrap() }.to_usize().unwrap() as size_t;
unsafe { memcpy(dest, src, n) };
}
9 changes: 5 additions & 4 deletions src/tests/array/src/main.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ if arr is not empty:
println "Not empty"
println arr[0]

let mut it = iterator for arr
while it exists:
println (value from it)
advance it
let mut i = 2
while i <= 10:
push i to arr
i += 1
println arr
4 changes: 2 additions & 2 deletions src/tests/snapshots/ppl__tests__address_of.hir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: hir
---
let x: Integer = 0
let address: MemoryAddress = `address of <:Reference<Integer>>`((&x:Reference<Integer>))
let x_mut_ref: ReferenceMut<Integer> = `<:Type<Integer>> at <:MemoryAddress>`((Type<Integer>:Type<Integer>), (address:MemoryAddress))
let x_mut_ref: ReferenceMut<Integer> = `<:Type<Integer>> at <:Reference<MemoryAddress>>`((Type<Integer>:Type<Integer>), (&address:Reference<MemoryAddress>))
`println <:Integer>`(`clone <:Reference<Integer>>`((x:Integer)))
(x_mut_ref:ReferenceMut<Integer>) = 1
`println <:Integer>`(`clone <:Reference<Integer>>`((x:Integer)))
Expand All @@ -17,7 +17,7 @@ let x_mut_ref: ReferenceMut<Integer> = `<:Type<Integer>> at <:MemoryAddress>`((T
fn<Integer> address of <ref: Reference<Integer>> -> MemoryAddress

@mangle_as("read_memory")
fn<Integer> <ty: Type<Integer>> at <address: MemoryAddress> -> ReferenceMut<Integer>
fn<Integer> <ty: Type<Integer>> at <address: Reference<MemoryAddress>> -> ReferenceMut<Integer>

@mangle_as("integer_as_string")
fn String from <$arg0: Integer> -> String
Expand Down
7 changes: 3 additions & 4 deletions src/tests/snapshots/ppl__tests__address_of.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,15 @@ declare %MemoryAddress @address_of(ptr)

define private void @initialize.4() !dbg !26 {
%1 = load %"Type<Integer>", ptr @"Type<Integer>", align 8, !dbg !27
%2 = load %MemoryAddress, ptr @address, align 8, !dbg !28
%3 = call ptr @read_memory(%"Type<Integer>" %1, %MemoryAddress %2), !dbg !28
store ptr %3, ptr @x_mut_ref, align 8, !dbg !28
%2 = call ptr @read_memory(%"Type<Integer>" %1, ptr @address), !dbg !28
store ptr %2, ptr @x_mut_ref, align 8, !dbg !28
br label %return, !dbg !28

return: ; preds = %0
ret void
}

declare ptr @read_memory(%"Type<Integer>", %MemoryAddress)
declare ptr @read_memory(%"Type<Integer>", ptr)

define private void @"println <:Integer>"(%Integer %0) !dbg !29 {
%x = alloca %Integer, align 8
Expand Down
Loading
Loading