aboutsummaryrefslogtreecommitdiff
path: root/tests/taili.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/taili.c')
-rw-r--r--tests/taili.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/taili.c b/tests/taili.c
new file mode 100644
index 0000000..cc09f59
--- /dev/null
+++ b/tests/taili.c
@@ -0,0 +1,39 @@
+#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_taili(f, f, 2, args);
+
+ ejit_select_compile_func(f, 2, 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);
+}