aboutsummaryrefslogtreecommitdiff
path: root/tests/jmp_table.c
blob: 10f9226e2b6988aeab3a2cb0a8362585ec3f402e (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <ejit/ejit.h>
#include <assert.h>
#include "do_jit.h"

#define NTARGETS 4
struct ejit_label targets[NTARGETS];

int main()
{
	struct ejit_operand operands[1] = {
		EJIT_OPERAND_GPR(0, EJIT_POINTER)
	};

	struct ejit_func *f = ejit_create_func(EJIT_TYPE(long), 1, operands);
	struct ejit_reloc default_target = ejit_bgei_u(f, EJIT_GPR(0),
	                                               NTARGETS);

	ejit_lshi(f, EJIT_GPR(0), EJIT_GPR(0),
	          sizeof(struct ejit_label) == 4 ? 2 : 3);

#warning \
	"Label rewriting is not currently working so this will fail the jit test"
	ejit_ldxi_label(f, EJIT_GPR(1), EJIT_GPR(0), (int64_t)targets);
	ejit_jmpr(f, EJIT_GPR(1));

	struct ejit_reloc tails[NTARGETS];
	for (size_t i = 0; i < NTARGETS; i++) {
		targets[i] = ejit_label(f);
		ejit_movi(f, EJIT_GPR(0), i * i);
		tails[i] = ejit_jmp(f);
	}

	struct ejit_label default_label = ejit_label(f);
	ejit_patch(f, default_target, default_label);
	ejit_movi(f, EJIT_GPR(0), 42);

	struct ejit_label dst = ejit_label(f);
	for (int i = 0; i < NTARGETS; i++) {
		ejit_patch(f, tails[i], dst);
	}

	ejit_retr(f, EJIT_GPR(0));

	ejit_select_compile_func(f, 2, 0, EJIT_USE64(long), do_jit);

	for (int i = -2; i < ((int) NTARGETS) + 2; i++) {
		if (i < 0) {
			assert(erf1(f, EJIT_ARG(i, long)) == 42);
		} else if (i < NTARGETS) {
			assert(erf1(f, EJIT_ARG(i, long)) == i * i);
		} else {
			assert(erf1(f, EJIT_ARG(i, long)) == 42);
		}
	}

	ejit_destroy_func(f);
}