aboutsummaryrefslogtreecommitdiff
path: root/src/interp.c
blob: bbf461b5aadedb8046a6c3492ff83625534dc14b (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ejit/ejit.h>

#include "common.h"


union interp_ret ejit_interp(struct ejit_func *f, size_t argc, struct ejit_arg args[argc], bool run, void ***labels_wb)
{
	static void *labels[OPCODE_COUNT] = {
		[MOVI] = &&movi,
		[MOVR] = &&movr,
		[MOVR_F] = &&movr_f,

		[ADDR] = &&addr,
		[ADDR_F] = &&addr_f,
		[SUBR] = &&subr,
		[SUBR_F] = &&subr_f,

		[BLTR] = &&bltr,

		[RET] = &&ret,
		[RET_I] = &&ret_i,
		[RET_F] = &&ret_f,
		[RET_FI] = &&ret_fi,

		[ARG] = &&arg,
		[ARG_I] = &&arg_i,
		[ARG_F] = &&arg_f,
		[ARG_FI] = &&arg_fi,

		[PARAM] = &&param,
		[PARAM_F] = &&param_f,

		[CALLI] = &&calli,
		[CALLI_F] = &&calli_f,

		[START] = &&start,
		[END] = &&end,
	};

	if (!run) {
		*labels_wb = labels;
		return (union interp_ret){.r = 0};
	}

	/** @todo this can be optimized by for example using a common buffer
	 * 'stack' */
	long *gpr = malloc(f->gpr * sizeof(long));
	long *fpr = malloc(f->fpr * sizeof(long));
	struct vec call_args = vec_create(sizeof(struct ejit_arg));
	struct ejit_insn *insns = f->insns.buf;

	struct ejit_insn i;
	long retval = 0; double retval_f = 0.;
	size_t pc = 0;

#define DO(x) x: { i = insns[pc];
#define JUMP(a) goto *insns[pc = a].addr;
#define DISPATCH() } goto *insns[++pc].addr;

	DO(start);
	DISPATCH();

	DO(end);
	goto out_int;
	DISPATCH();

	DO(movi);
	gpr[i.r0] = i.o;
	DISPATCH();

	DO(movr);
	gpr[i.r0] = gpr[i.r1];
	DISPATCH();

	DO(movr_f);
	fpr[i.r0] = fpr[i.r1];
	DISPATCH();

	DO(addr);
	gpr[i.r0] = gpr[i.r1] + gpr[i.r2];
	DISPATCH();

	DO(addr_f);
	fpr[i.r0] = fpr[i.r1] + fpr[i.r2];
	DISPATCH();

	DO(subr);
	gpr[i.r0] = gpr[i.r1] - gpr[i.r2];
	DISPATCH();

	DO(subr_f);
	fpr[i.r0] = fpr[i.r1] - fpr[i.r2];
	DISPATCH();

	DO(bltr);
	if (gpr[i.r0] < gpr[i.r1])
		JUMP(i.o);

	DISPATCH();

	DO(param);
	gpr[i.r0] = args[i.o].l;
	DISPATCH();

	DO(param_f);
	fpr[i.r0] = args[i.o].d;
	DISPATCH();

	DO(arg);
	struct ejit_arg a = ejit_build_arg(i.r1, gpr[i.r0]);
	vec_append(&call_args, &a);
	DISPATCH();

	DO(arg_i);
	struct ejit_arg a = ejit_build_arg(i.r1, i.o);
	vec_append(&call_args, &a);
	DISPATCH();

	DO(arg_f);
	struct ejit_arg a = ejit_build_arg_f(i.r1, fpr[i.r0]);
	vec_append(&call_args, &a);
	DISPATCH();

	DO(arg_fi);
	struct ejit_arg a = ejit_build_arg_f(i.r1, i.d);
	vec_append(&call_args, &a);
	DISPATCH();

	DO(calli);
	struct ejit_func *f = i.p;
	retval = ejit_run_func(f, vec_len(&call_args), call_args.buf);
	vec_reset(&call_args);
	DISPATCH();

	DO(calli_f);
	struct ejit_func *f = i.p;
	retval_f = ejit_run_func_f(f, vec_len(&call_args), call_args.buf);
	vec_reset(&call_args);
	DISPATCH();

	/* dispatch is technically unnecessary for returns, but keep it for
	 * symmetry */
	DO(ret);
	retval = gpr[i.r0];
	goto out_int;
	DISPATCH();

	DO(ret_i);
	retval = i.o;
	goto out_int;
	DISPATCH();

	DO(ret_f);
	retval_f = fpr[i.r0];
	goto out_float;
	DISPATCH();

	DO(ret_fi);
	retval_f = i.d;
	goto out_float;
	DISPATCH();

#undef DISPATCH
#undef JUMP
#undef DO

out_int:
	vec_destroy(&call_args);
	free(gpr);
	free(fpr);
	return (union interp_ret){.r = retval};

out_float:
	vec_destroy(&call_args);
	free(gpr);
	free(fpr);
	return (union interp_ret){.d = retval_f};
}