Skip to content

Commit cfa951e

Browse files
authored
Panic on prompt duplication (#810)
Detect an invalid state dynamically and panic (#244). Todo: print proper error message
1 parent 5f26c54 commit cfa951e

File tree

7 files changed

+52
-18
lines changed

7 files changed

+52
-18
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[error] Process exited with non-zero exit code 1.
2+
[error] Valgrind log:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
effect E() : (() => Unit at {io})
3+
4+
5+
def main() : Unit = {
6+
try {
7+
val f = do E()
8+
f()
9+
} with E { () =>
10+
def f() = { () }
11+
def g() = {
12+
resume(f)
13+
}
14+
resume(g)
15+
}
16+
}

libraries/llvm/forward-declare-c.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare void @c_io_println_Double(%Double)
99
declare void @c_io_println_String(%Pos)
1010

1111
declare void @hole()
12+
declare void @duplicated_prompt()
1213

1314
declare %Pos @c_ref_fresh(%Pos)
1415
declare %Pos @c_ref_get(%Pos)

libraries/llvm/hole.c

Lines changed: 0 additions & 11 deletions
This file was deleted.

libraries/llvm/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "types.c"
1313
#include "bytearray.c"
1414
#include "io.c"
15-
#include "hole.c"
15+
#include "panic.c"
1616
#include "ref.c"
1717
#include "array.c"
1818

libraries/llvm/panic.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef EFFEKT_PANIC_C
2+
#define EFFEKT_PANIC_C
3+
4+
#include <stdio.h>
5+
6+
void hole() {
7+
fprintf(stderr, "PANIC: Reached a hole in the program\n");
8+
exit(1);
9+
}
10+
11+
void duplicated_prompt() {
12+
fprintf(stderr, "PANIC: Continuation invoked itself\n");
13+
exit(1);
14+
}
15+
16+
#endif

libraries/llvm/rts.ll

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ define private %Stack @reset(%Stack %oldStack) {
386386
ret %Stack %stack
387387
}
388388

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

404404
displace:
405-
call void @displace(%Stack %promptStack, %Stack %promptStack)
405+
call void @invalidate(%Stack %promptStack, %Stack %promptStack)
406406
br label %update
407407

408408
update:
409409
store %Stack %stack, ptr %stack_pointer
410410

411411
%next_pointer = getelementptr %StackValue, %Stack %stack, i64 0, i32 4
412412
%next = load %Stack, ptr %next_pointer
413-
tail call void @updatePrompts(%Stack %next)
413+
tail call void @revalidate(%Stack %next)
414414
ret void
415415
}
416416

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

424425
%next = load %Stack, ptr %next_pointer
426+
427+
%isNull = icmp eq %Stack %next, null
428+
br i1 %isNull, label %error, label %check
429+
430+
error:
431+
call void @duplicated_prompt()
432+
ret void
433+
434+
check:
425435
%isEnd = icmp eq %Stack %next, %end
426436
br i1 %isEnd, label %done, label %continue
427437

428438
done:
429439
ret void
430440

431441
continue:
432-
tail call void @displace(%Stack %next, %Stack %end)
442+
tail call void @invalidate(%Stack %next, %Stack %end)
433443
ret void
434444
}
435445

436446
define private %Stack @resume(%Resumption %resumption, %Stack %oldStack) {
437447
%uniqueResumption = call %Resumption @uniqueStack(%Resumption %resumption)
438448
%rest_pointer = getelementptr %StackValue, %Resumption %uniqueResumption, i64 0, i32 4
439449
%start = load %Stack, ptr %rest_pointer
440-
call void @updatePrompts(%Stack %start)
450+
call void @revalidate(%Stack %start)
441451

442452
store %Stack %oldStack, ptr %rest_pointer
443453

0 commit comments

Comments
 (0)