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

Commit

Permalink
Add address of <> (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilikhin-d authored May 4, 2024
1 parent 1c059dd commit 69e318d
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ppl/src/memory.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ 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
15 changes: 15 additions & 0 deletions src/runtime/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,18 @@ pub extern "C" fn read_memory(ty: Type, address: MemoryAddress) -> *mut c_void {

address as *mut libc::c_void
}

/// # PPL
/// ```no_run
/// /// Get memory address of a reference
/// @mangle_as("address_of")
/// fn<T> address of <ref: &T> -> MemoryAddress
/// ```
#[no_mangle]
pub extern "C" fn address_of(ptr: *const c_void) -> MemoryAddress {
let address = ptr as usize;

MemoryAddress {
value: integer_from_u64(address as u64),
}
}
7 changes: 7 additions & 0 deletions src/tests/address_of/src/main.ppl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let x = 0
println x

let address = address of x
let x_mut_ref = Integer at address
x_mut_ref = 1
println x
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::e2es;

e2es! {
address_of,
array,
candidate_not_viable,
clone,
Expand Down
11 changes: 11 additions & 0 deletions src/tests/snapshots/ppl__tests__address_of.hir.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/mod.rs
expression: hir
---
let x: Integer = 0
let address: MemoryAddress = address of (&x:Reference<Integer>)
let x_mut_ref: ReferenceMut<Integer> = (Type<Integer>:Type<Integer>) at (address:MemoryAddress)
println clone (x:Integer)
(x_mut_ref:ReferenceMut<Integer>) = 1
println clone (x:Integer)
destroy (x:Integer)
166 changes: 166 additions & 0 deletions src/tests/snapshots/ppl__tests__address_of.ir.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
---
source: src/tests/mod.rs
expression: ir
---
; ModuleID = 'main'
source_filename = "src/main.ppl"

%"Type<String>" = type { %String, %Integer }
%String = type { ptr }
%Integer = type { ptr }
%"Type<Integer>" = type { %String, %Integer }
%MemoryAddress = type { %Integer }

@"Type<String>" = private global %"Type<String>" zeroinitializer
@0 = private unnamed_addr constant [7 x i8] c"String\00", align 1
@"Type<Integer>" = private global %"Type<Integer>" zeroinitializer
@1 = private unnamed_addr constant [8 x i8] c"Integer\00", align 1
@x = global %Integer zeroinitializer
@address = global %MemoryAddress zeroinitializer
@x_mut_ref = global ptr null

define private void @initialize() !dbg !3 {
%1 = alloca %"Type<String>", align 8, !dbg !7
%"Type<String>.name" = getelementptr inbounds %"Type<String>", ptr %1, i32 0, i32 0, !dbg !7
%2 = call %String @string_from_c_string_and_length(ptr @0, i64 6), !dbg !8
store %String %2, ptr %"Type<String>.name", align 8, !dbg !8
%"Type<String>.size" = getelementptr inbounds %"Type<String>", ptr %1, i32 0, i32 1, !dbg !8
%3 = call %Integer @integer_from_i64(i64 8), !dbg !8
store %Integer %3, ptr %"Type<String>.size", align 8, !dbg !8
%4 = load %"Type<String>", ptr %1, align 8, !dbg !8
store %"Type<String>" %4, ptr @"Type<String>", align 8, !dbg !8
br label %return, !dbg !8

return: ; preds = %0
ret void
}

declare %String @string_from_c_string_and_length(ptr, i64)

declare %Integer @integer_from_i64(i64)

define private void @initialize.1() !dbg !9 {
%1 = alloca %"Type<Integer>", align 8, !dbg !10
%"Type<Integer>.name" = getelementptr inbounds %"Type<Integer>", ptr %1, i32 0, i32 0, !dbg !10
%2 = call %String @string_from_c_string_and_length(ptr @1, i64 7), !dbg !11
store %String %2, ptr %"Type<Integer>.name", align 8, !dbg !11
%"Type<Integer>.size" = getelementptr inbounds %"Type<Integer>", ptr %1, i32 0, i32 1, !dbg !11
%3 = call %Integer @integer_from_i64(i64 8), !dbg !11
store %Integer %3, ptr %"Type<Integer>.size", align 8, !dbg !11
%4 = load %"Type<Integer>", ptr %1, align 8, !dbg !11
store %"Type<Integer>" %4, ptr @"Type<Integer>", align 8, !dbg !11
br label %return, !dbg !11

return: ; preds = %0
ret void
}

define void @main.execute() !dbg !12 {
call void @initialize(), !dbg !13
call void @initialize.1(), !dbg !14
call void @initialize.2(), !dbg !15
call void @initialize.3(), !dbg !16
call void @initialize.4(), !dbg !14
%1 = load %Integer, ptr @x, align 8, !dbg !17
%2 = call %Integer @clone_integer(%Integer %1), !dbg !17
call void @"println <:Integer>"(%Integer %2), !dbg !17
%3 = load ptr, ptr @x_mut_ref, align 8, !dbg !18
%4 = call %Integer @integer_from_i64(i64 1), !dbg !19
store %Integer %4, ptr %3, align 8, !dbg !19
%5 = load %Integer, ptr @x, align 8, !dbg !20
%6 = call %Integer @clone_integer(%Integer %5), !dbg !20
call void @"println <:Integer>"(%Integer %6), !dbg !20
call void @destroy_integer(ptr @x), !dbg !21
br label %return, !dbg !21

return: ; preds = %0
ret void
}

define private void @initialize.2() !dbg !22 {
%1 = call %Integer @integer_from_i64(i64 0), !dbg !23
store %Integer %1, ptr @x, align 8, !dbg !23
br label %return, !dbg !23

return: ; preds = %0
ret void
}

define private void @initialize.3() !dbg !24 {
%1 = call %MemoryAddress @address_of(ptr @x), !dbg !25
store %MemoryAddress %1, ptr @address, align 8, !dbg !25
br label %return, !dbg !25

return: ; preds = %0
ret void
}

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
br label %return, !dbg !28

return: ; preds = %0
ret void
}

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

define private void @"println <:Integer>"(%Integer %0) !dbg !29 {
%x = alloca %Integer, align 8
store %Integer %0, ptr %x, align 8
%2 = load %Integer, ptr %x, align 8, !dbg !30
%3 = call %String @integer_as_string(%Integer %2), !dbg !30
call void @"println <:String>"(%String %3), !dbg !30
br label %return, !dbg !30

return: ; preds = %1
ret void
}

declare void @"println <:String>"(%String)

declare %String @integer_as_string(%Integer)

declare %Integer @clone_integer(%Integer)

declare void @destroy_integer(ptr)

!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, producer: "ppl", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, sysroot: "/")
!2 = !DIFile(filename: "src/main.ppl", directory: ".")
!3 = distinct !DISubprogram(name: "initialize", linkageName: "initialize", scope: !2, file: !2, line: 7, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!4 = !DISubroutineType(types: !5)
!5 = !{!6}
!6 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_signed)
!7 = !DILocation(line: 7, scope: !3)
!8 = !DILocation(line: 0, scope: !3)
!9 = distinct !DISubprogram(name: "initialize.1", linkageName: "initialize.1", scope: !2, file: !2, line: 4, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!10 = !DILocation(line: 4, column: 16, scope: !9)
!11 = !DILocation(line: 0, scope: !9)
!12 = distinct !DISubprogram(name: "main.execute", linkageName: "main.execute", scope: !2, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!13 = !DILocation(line: 7, scope: !12)
!14 = !DILocation(line: 4, column: 16, scope: !12)
!15 = !DILocation(line: 0, column: 8, scope: !12)
!16 = !DILocation(line: 3, column: 14, scope: !12)
!17 = !DILocation(line: 1, column: 8, scope: !12)
!18 = !DILocation(line: 5, scope: !12)
!19 = !DILocation(line: 5, column: 12, scope: !12)
!20 = !DILocation(line: 6, column: 8, scope: !12)
!21 = !DILocation(line: 0, scope: !12)
!22 = distinct !DISubprogram(name: "initialize.2", linkageName: "initialize.2", scope: !12, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!23 = !DILocation(line: 0, column: 8, scope: !22)
!24 = distinct !DISubprogram(name: "initialize.3", linkageName: "initialize.3", scope: !12, file: !2, line: 3, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!25 = !DILocation(line: 3, column: 25, scope: !24)
!26 = distinct !DISubprogram(name: "initialize.4", linkageName: "initialize.4", scope: !12, file: !2, line: 4, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!27 = !DILocation(line: 4, column: 16, scope: !26)
!28 = !DILocation(line: 4, column: 27, scope: !26)
!29 = distinct !DISubprogram(name: "println <:Integer>", linkageName: "println <:Integer>", scope: !12, file: !2, line: 7, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!30 = !DILocation(line: 7, scope: !29)
6 changes: 6 additions & 0 deletions src/tests/snapshots/ppl__tests__address_of.run.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/tests/mod.rs
expression: run_log
---
0
1

0 comments on commit 69e318d

Please sign in to comment.