~ chicken-core (chicken-5) 1127692f8ab51b07df99ea8b2024c6062ac2bdbb
commit 1127692f8ab51b07df99ea8b2024c6062ac2bdbb
Author: Jim Ursetto <zbigniewsz@gmail.com>
AuthorDate: Thu Jan 12 18:20:53 2012 -0600
Commit: felix <felix@call-with-current-continuation.org>
CommitDate: Sat Feb 18 13:18:54 2012 +0100
Make C_stack_pointer work with LLVM backend
When using LLVM, allocating zero bytes on the stack is legal but
is documented to return an undefined value. In practice, this is
NULL if optimization is enabled, or alloca(1) if not. Work around
this by using alloca(1) to get the stack pointer or, in supported
environments, using inline asm. alloca(1) may waste a machine word,
but does not always; also, llvm uses the stack more efficiently,
so the effects tend to even out.
diff --git a/chicken.h b/chicken.h
index 2ced110b..fdf4b72c 100644
--- a/chicken.h
+++ b/chicken.h
@@ -940,7 +940,17 @@ extern double trunc(double);
#define C_pick(n) (C_temporary_stack[ n ])
#define C_drop(n) (C_temporary_stack += (n))
#define C_alloc(n) ((C_word *)C_alloca((n) * sizeof(C_word)))
-#define C_stack_pointer ((C_word *)C_alloca(0))
+#if defined (__llvm__) && defined (__GNUC__)
+# if defined (__i386__)
+# define C_stack_pointer ({C_word *sp; __asm__ __volatile__("movl %%esp,%0":"=r"(sp):);sp;})
+# elif defined (__x86_64__)
+# define C_stack_pointer ({C_word *sp; __asm__ __volatile__("movq %%rsp,%0":"=r"(sp):);sp;})
+# else
+# define C_stack_pointer ((C_word *)C_alloca(1))
+# endif
+#else
+# define C_stack_pointer ((C_word *)C_alloca(0))
+#endif
#define C_stack_pointer_test ((C_word *)C_alloca(1))
#define C_demand_2(n) (((C_word *)C_fromspace_top + (n)) < (C_word *)C_fromspace_limit)
#define C_fix(n) (((C_word)(n) << C_FIXNUM_SHIFT) | C_FIXNUM_BIT)
Trap