Skip to content

Commit

Permalink
Panic on prompt duplication (#810)
Browse files Browse the repository at this point in the history
Detect an invalid state dynamically and panic (#244).

Todo: print proper error message
  • Loading branch information
serkm authored Feb 4, 2025
1 parent 5f26c54 commit cfa951e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
2 changes: 2 additions & 0 deletions examples/llvm/prompt-duplication.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[error] Process exited with non-zero exit code 1.
[error] Valgrind log:
16 changes: 16 additions & 0 deletions examples/llvm/prompt-duplication.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

effect E() : (() => Unit at {io})


def main() : Unit = {
try {
val f = do E()
f()
} with E { () =>
def f() = { () }
def g() = {
resume(f)
}
resume(g)
}
}
1 change: 1 addition & 0 deletions libraries/llvm/forward-declare-c.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare void @c_io_println_Double(%Double)
declare void @c_io_println_String(%Pos)

declare void @hole()
declare void @duplicated_prompt()

declare %Pos @c_ref_fresh(%Pos)
declare %Pos @c_ref_get(%Pos)
Expand Down
11 changes: 0 additions & 11 deletions libraries/llvm/hole.c

This file was deleted.

2 changes: 1 addition & 1 deletion libraries/llvm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "types.c"
#include "bytearray.c"
#include "io.c"
#include "hole.c"
#include "panic.c"
#include "ref.c"
#include "array.c"

Expand Down
16 changes: 16 additions & 0 deletions libraries/llvm/panic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef EFFEKT_PANIC_C
#define EFFEKT_PANIC_C

#include <stdio.h>

void hole() {
fprintf(stderr, "PANIC: Reached a hole in the program\n");
exit(1);
}

void duplicated_prompt() {
fprintf(stderr, "PANIC: Continuation invoked itself\n");
exit(1);
}

#endif
22 changes: 16 additions & 6 deletions libraries/llvm/rts.ll
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ define private %Stack @reset(%Stack %oldStack) {
ret %Stack %stack
}

define private void @updatePrompts(%Stack %stack) {
define private void @revalidate(%Stack %stack) {
%prompt_pointer = getelementptr %StackValue, %Stack %stack, i64 0, i32 3
%prompt = load %Prompt, ptr %prompt_pointer
%stack_pointer = getelementptr %PromptValue, %Prompt %prompt, i64 0, i32 1
Expand All @@ -402,42 +402,52 @@ continue:
br i1 %isOccupied, label %displace, label %update

displace:
call void @displace(%Stack %promptStack, %Stack %promptStack)
call void @invalidate(%Stack %promptStack, %Stack %promptStack)
br label %update

update:
store %Stack %stack, ptr %stack_pointer

%next_pointer = getelementptr %StackValue, %Stack %stack, i64 0, i32 4
%next = load %Stack, ptr %next_pointer
tail call void @updatePrompts(%Stack %next)
tail call void @revalidate(%Stack %next)
ret void
}

define private void @displace(%Stack %stack, %Stack %end) {
; This panics if we invalidate the meta stack
define private void @invalidate(%Stack %stack, %Stack %end) {
%prompt_pointer = getelementptr %StackValue, %Stack %stack, i64 0, i32 3
%next_pointer = getelementptr %StackValue, %Stack %stack, i64 0, i32 4
%prompt = load %Prompt, ptr %prompt_pointer
%stack_pointer = getelementptr %PromptValue, %Prompt %prompt, i64 0, i32 1
store %Stack null, ptr %stack_pointer

%next = load %Stack, ptr %next_pointer

%isNull = icmp eq %Stack %next, null
br i1 %isNull, label %error, label %check

error:
call void @duplicated_prompt()
ret void

check:
%isEnd = icmp eq %Stack %next, %end
br i1 %isEnd, label %done, label %continue

done:
ret void

continue:
tail call void @displace(%Stack %next, %Stack %end)
tail call void @invalidate(%Stack %next, %Stack %end)
ret void
}

define private %Stack @resume(%Resumption %resumption, %Stack %oldStack) {
%uniqueResumption = call %Resumption @uniqueStack(%Resumption %resumption)
%rest_pointer = getelementptr %StackValue, %Resumption %uniqueResumption, i64 0, i32 4
%start = load %Stack, ptr %rest_pointer
call void @updatePrompts(%Stack %start)
call void @revalidate(%Stack %start)

store %Stack %oldStack, ptr %rest_pointer

Expand Down

0 comments on commit cfa951e

Please sign in to comment.