shithub: mc

Download patch

ref: fdbbf1909462cef844b6b9cbbc5d90a9ec2ebed2
parent: 778aadfc5a73c80a9046250b00a085e074a1870f
author: Ori Bernstein <ori@eigenstate.org>
date: Wed May 25 07:18:53 EDT 2016

Add setjmp/longjmp on posixy systems.

--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -46,6 +46,8 @@
 	readall.myr
 	result.myr
 	search.myr
+	sjlj.myr
+	sjlj-impl+posixy-x64.s
 	slcp.myr
 	sldup.myr
 	sleq.myr
--- /dev/null
+++ b/lib/std/sjlj-impl+posixy-x64.s
@@ -1,0 +1,44 @@
+.globl std$setjmp
+.globl _std$setjmp
+std$setjmp:
+_std$setjmp:
+	/* save registers */
+	movq	%rbx,   (%rdi)
+	movq	%rbp,  8(%rdi)
+	movq	%r12, 16(%rdi)
+	movq	%r13, 24(%rdi)
+	movq	%r14, 32(%rdi)
+	movq	%r15, 40(%rdi)
+
+	/* save %rsp */
+	leaq	8(%rsp),%rdx
+	movq	%rdx,48(%rdi)
+
+	/* save %rip */
+	movq	(%rsp),%rdx
+	movq	%rdx,56(%rdi)
+	xorq	%rax,%rax
+	ret	
+
+.globl std$longjmp
+.globl _std$longjmp
+std$longjmp:
+_std$longjmp:
+	/* return true */
+	movq	$1, %rax
+
+	/* restore registers */
+	movq	  (%rdi), %rbx
+	movq	 8(%rdi), %rbp
+	movq	16(%rdi), %r12
+	movq	24(%rdi), %r13
+	movq	32(%rdi), %r14
+	movq	40(%rdi), %r15
+	movq	48(%rdi), %rdx
+
+	/* load stack */
+	movq	%rdx, %rsp
+
+	/* jmp to return addr */
+	movq	56(%rdi), %rdx
+	jmp	*%rdx
--- /dev/null
+++ b/lib/std/sjlj.myr
@@ -1,0 +1,8 @@
+pkg std =
+	type jmpbuf = struct
+		regs	: uint64[8]
+	;;
+
+	extern const setjmp	: (jb : jmpbuf# -> bool)
+	extern const longjmp	: (jb : jmpbuf# -> void)
+;;
--- /dev/null
+++ b/lib/std/test/sjlj.myr
@@ -1,0 +1,17 @@
+use std
+
+const main = {
+	var jb
+
+	if std.setjmp(&jb)
+		std.put("nonlocal return\n")
+		std.exit(0)
+	;;
+	std.put("doing jmp\n")
+	dolongjmp(&jb)
+	std.fatal("unreachable\n")
+}
+
+const dolongjmp = {jb
+	std.longjmp(jb)
+}