aboutsummaryrefslogtreecommitdiff
path: root/tests/tailr.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tailr.c')
-rw-r--r--tests/tailr.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/tailr.c b/tests/tailr.c
new file mode 100644
index 0000000..69ad44b
--- /dev/null
+++ b/tests/tailr.c
@@ -0,0 +1,41 @@
+#include <ejit/ejit.h>
+#include <assert.h>
+#include "do_jit.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argv;
+ bool do_jit = argc > 1;
+ struct ejit_operand operands[2] = {
+ EJIT_OPERAND_GPR(0, EJIT_INT32), /* s */
+ EJIT_OPERAND_GPR(1, EJIT_INT32) /* n */
+ };
+
+ struct ejit_func *f = ejit_create_func(EJIT_INT32, 2, operands);
+
+ /* n == 0, return s */
+ struct ejit_reloc r = ejit_bnei(f, EJIT_GPR(1), 0);
+ ejit_retr(f, EJIT_GPR(0));
+ ejit_patch(f, r, ejit_label(f));
+
+ /* s += n */
+ ejit_addr(f, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1));
+
+ /* n -= 1 */
+ ejit_subi(f, EJIT_GPR(1), EJIT_GPR(1), 1);
+
+ struct ejit_operand args[2] = {
+ EJIT_OPERAND_GPR(0, EJIT_INT32), /* s */
+ EJIT_OPERAND_GPR(1, EJIT_INT32) /* n */
+ };
+
+ ejit_movi(f, EJIT_GPR(2), (uintptr_t)f);
+ ejit_tailr(f, EJIT_GPR(2), 2, args);
+
+ ejit_select_compile_func(f, 3, 0, EJIT_USE64(uintptr_t), do_jit, true);
+
+ /* arbitrary number but large enough to most likely cause a stack fault
+ * if the tail call leaks memory or something */
+ assert((int32_t)erfi2(f, EJIT_ARG(0, int32_t), EJIT_ARG(1000000, int32_t)) == 1784293664);
+ ejit_destroy_func(f);
+}