aboutsummaryrefslogtreecommitdiff
path: root/tests/tailr.c
blob: 69ad44b5e51c35119961b2b88eca5649f2506568 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
}