From 057131cb20fb1c46e90adecfb4a16eb62f100580 Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Wed, 9 Apr 2025 20:20:48 +0300
Subject: add taili

---
 tests/tailr.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 tests/tailr.c

(limited to 'tests/tailr.c')

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);
+}
-- 
cgit v1.2.3