aboutsummaryrefslogtreecommitdiff
path: root/src/ejit.c
blob: e7087400bf474de3c29dbaf37a0bc440fc7598a2 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <assert.h>
#include <sys/mman.h>

#include <ejit/ejit.h>

#include "common.h"

static void emit_insn_i(struct ejit_func *f, enum ejit_opcode op, size_t r0,
                        size_t r1, int64_t o)
{
	struct ejit_insn i = {.op = op, .r0 = r0, .r1 = r1, .o = o};
	vec_append(&f->insns, &i);
}

static void emit_insn_r(struct ejit_func *f, enum ejit_opcode op, size_t r0,
                        size_t r1, size_t r2)
{
	struct ejit_insn i = {.op = op, .r0 = r0, .r1 = r1, .r2 = r2};
	vec_append(&f->insns, &i);
}

static void emit_insn_p(struct ejit_func *f, enum ejit_opcode op, size_t r0,
                        size_t r1, void *p)
{
	struct ejit_insn i = {.op = op, .r0 = r0, .r1 = r1, .p = p};
	vec_append(&f->insns, &i);
}

static void emit_insn_f(struct ejit_func *f, enum ejit_opcode op, size_t r0,
                        size_t r1, double d)
{
	struct ejit_insn i = {.op = op, .r0 = r0, .r1 = r1, .d = d};
	vec_append(&f->insns, &i);
}

struct ejit_func *ejit_create_func(enum ejit_type rtype, size_t argc,
                                   const struct ejit_operand args[argc])
{
	struct ejit_func *f = malloc(sizeof(struct ejit_func));
	assert(f);

	f->rtype = rtype;

	f->insns = vec_create(sizeof(struct ejit_insn));
	f->labels = vec_create(sizeof(size_t));
	f->arena = NULL;
	f->size = 0;

	for (size_t i = 0; i < argc; ++i) {
		switch (args[i].kind) {
		case EJIT_OPERAND_GPR: emit_insn_r(f, PARAM, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FPR: emit_insn_r(f, PARAM_F, i, args[i].type,
			                           args[i].r); break;
		default: abort();
		}
	}

	emit_insn_i(f, START, 0, 0, 0);
	return f;
}

void ejit_compile_func(struct ejit_func *f, size_t gpr, size_t fpr, bool use_64)
{
	ejit_select_compile_func(f, gpr, fpr, use_64, true);
}

void ejit_select_compile_func(struct ejit_func *f, size_t gpr, size_t fpr,
                              bool use_64, bool try_jit)
{
	/* emit a final end instruction in case user didn't do a return */
	emit_insn_i(f, END, 0, 0, 0);

	f->gpr = gpr;
	f->fpr = fpr;

	/* try to jit compile if possible */
	if (try_jit && ejit_compile(f, use_64))
		return;

	/* otherwise, convert opcodes to address labels */

	void **labels;
	/* just get labels, don't actually run anything yet */
	ejit_interp(f, 0, NULL, NULL, false, &labels);
	foreach_vec(ii, f->insns) {
		struct ejit_insn i = vect_at(struct ejit_insn, f->insns, ii);
		void *addr = labels[i.op];
		assert(addr);
		i.addr = addr;
		vect_at(struct ejit_insn, f->insns, ii) = i;
	}
}

void ejit_destroy_func(struct ejit_func *f)
{
	if (f->arena)
		munmap(f->arena, f->size);

	vec_destroy(&f->insns);
	vec_destroy(&f->labels);
	free(f);
}

struct ejit_label ejit_label(struct ejit_func *f)
{
	size_t addr = vec_len(&f->insns);
	vec_append(&f->labels, &addr);
	return (struct ejit_label){.addr = addr};
}

void ejit_patch(struct ejit_func *f, struct ejit_reloc r, struct ejit_label l)
{
	struct ejit_insn i = vect_at(struct ejit_insn, f->insns, r.insn);
	/** @todo some assert that checks the opcode? */
	i.r0 = l.addr;
	vect_at(struct ejit_insn, f->insns, r.insn) = i;
}

void ejit_calli(struct ejit_func *s, struct ejit_func *f, size_t argc,
                const struct ejit_operand args[argc])
{
	for (size_t i = 0; i < argc; ++i) {
		switch (args[i].kind) {
		case EJIT_OPERAND_GPR: emit_insn_r(s, ARG, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FPR: emit_insn_r(s, ARG_F, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_IMM: emit_insn_i(s, ARG_I, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FLT: emit_insn_f(s, ARG_FI, i, args[i].type,
			                           args[i].d); break;
		default: abort();
		}
	}

	emit_insn_p(s, CALLI, 0, 0, f);
}

void ejit_escapei(struct ejit_func *s, ejit_escape_t f, size_t argc,
                  const struct ejit_operand args[argc])
{
	for (size_t i = 0; i < argc; ++i) {
		switch (args[i].kind) {
		case EJIT_OPERAND_GPR: emit_insn_r(s, ARG, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FPR: emit_insn_r(s, ARG_F, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_IMM: emit_insn_r(s, ARG_I, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FLT: emit_insn_f(s, ARG_FI, i, args[i].type,
			                           args[i].d); break;
		default: abort();
		}
	}

	emit_insn_p(s, ESCAPEI, 0, 0, f);
}

void ejit_escapei_f(struct ejit_func *s, ejit_escape_f_t f, size_t argc,
                    const struct ejit_operand args[argc])
{
	for (size_t i = 0; i < argc; ++i) {
		switch (args[i].kind) {
		case EJIT_OPERAND_GPR: emit_insn_r(s, ARG, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FPR: emit_insn_r(s, ARG_F, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_IMM: emit_insn_r(s, ARG_I, i, args[i].type,
			                           args[i].r); break;
		case EJIT_OPERAND_FLT: emit_insn_f(s, ARG_FI, i, args[i].type,
			                           args[i].d); break;
		default: abort();
		}
	}

	emit_insn_p(s, ESCAPEI_F, 0, 0, f);
}

void ejit_retval(struct ejit_func *s, struct ejit_gpr r0)
{
	emit_insn_i(s, RETVAL, r0.r, 0, 0);
}

void ejit_stxi_64(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
                  int64_t o)
{
	emit_insn_i(s, STXI64, r0.r, r1.r, o);
}

void ejit_ldxi_u64(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
                   int64_t o)
{
	emit_insn_i(s, LDXIU64, r0.r, r1.r, o);
}

void ejit_retr(struct ejit_func *s, struct ejit_gpr r0)
{
	emit_insn_r(s, RETR, r0.r, 0, 0);
}

void ejit_retr_f(struct ejit_func *s, struct ejit_fpr r0)
{
	emit_insn_r(s, RETR_F, r0.f, 0, 0);
}

void ejit_reti(struct ejit_func *s, int64_t i)
{
	emit_insn_i(s, RETI, 0, 0, i);
}

void ejit_reti_f(struct ejit_func *s, double f)
{
	emit_insn_f(s, RETI_F, 0, 0, f);
}

void ejit_addr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               struct ejit_gpr r2)
{
	emit_insn_r(s, ADDR, r0.r, r1.r, r2.r);
}

void ejit_addr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1,
                 struct ejit_fpr r2)
{
	emit_insn_r(s, ADDR_F, r0.f, r1.f, r2.f);
}

void ejit_addi(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               int64_t o)
{
	emit_insn_i(s, ADDI, r0.r, r1.r, o);
}

void ejit_absr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1)
{
	emit_insn_i(s, ABSR_F, r0.f, r1.f, 0);
}

void ejit_subr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               struct ejit_gpr r2)
{
	emit_insn_r(s, SUBR, r0.r, r1.r, r2.r);
}

void ejit_subr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1,
                 struct ejit_fpr r2)
{
	emit_insn_r(s, SUBR_F, r0.f, r1.f, r2.f);
}

void ejit_subi(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               long o)
{
	emit_insn_i(s, SUBI, r0.r, r1.r, o);
}

void ejit_mulr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               struct ejit_gpr r2)
{
	emit_insn_r(s, MULR, r0.r, r1.r, r2.r);
}

void ejit_divr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               struct ejit_gpr r2)
{
	emit_insn_r(s, DIVR, r0.r, r1.r, r2.r);
}

void ejit_andr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               struct ejit_gpr r2)
{
	emit_insn_r(s, ANDR, r0.r, r1.r, r2.r);
}

void ejit_andi(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
               int64_t o)
{
	emit_insn_i(s, ANDI, r0.r, r1.r, o);
}

void ejit_negr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
	emit_insn_i(s, NEGR, r0.r, r1.r, 0);
}

void ejit_movi(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
	emit_insn_i(s, MOVI, r0.r, 0, o);
}

void ejit_movr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
	if (r0.r == r1.r)
		return;

	emit_insn_i(s, MOVR, r0.r, r1.r, 0);
}

void ejit_eqr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
              struct ejit_gpr r2)
{
	emit_insn_r(s, EQR, r0.r, r1.r, r2.r);
}

void ejit_ltr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
              struct ejit_gpr r2)
{
	emit_insn_r(s, LTR, r0.r, r1.r, r2.r);
}

struct ejit_reloc ejit_bltr(struct ejit_func *s, struct ejit_gpr r0,
                            struct ejit_gpr r1)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_r(s, BLTR, 0, r0.r, r1.r);
	return (struct ejit_reloc){.insn = addr};
}

struct ejit_reloc ejit_bnei(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_i(s, BNEI, 0, r0.r, o);
	return (struct ejit_reloc){.insn = addr};
}

struct ejit_reloc ejit_beqr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_r(s, BEQR, 0, r0.r, r1.r);
	return (struct ejit_reloc){.insn = addr};
}

struct ejit_reloc ejit_beqi(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_i(s, BEQI, 0, r0.r, o);
	return (struct ejit_reloc){.insn = addr};
}

struct ejit_reloc ejit_bgti(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_i(s, BGTI, 0, r0.r, o);
	return (struct ejit_reloc){.insn = addr};
}

struct ejit_reloc ejit_jmp(struct ejit_func *s)
{
	size_t addr = vec_len(&s->insns);
	emit_insn_i(s, JMP, 0, 0, 0);
	return (struct ejit_reloc){.insn = addr};
}

static struct interp_state create_interp_state()
{
	struct interp_state state;
	state.gprs = vec_create(sizeof(int64_t));
	state.fprs = vec_create(sizeof(double));
	state.args = vec_create(sizeof(struct ejit_arg));
	return state;
}

static void destroy_interp_state(struct interp_state state)
{
	vec_destroy(&state.gprs);
	vec_destroy(&state.fprs);
	vec_destroy(&state.args);
}

long ejit_run_interp(struct ejit_func *f, size_t argc,
                     struct ejit_arg args[argc], struct interp_state *state)
{
	assert(f->gpr && "trying to run a function that hasn't been compiled");
	assert(f->rtype == EJIT_VOID || ejit_int_type(f->rtype));
	if (f->arena)
		return ((ejit_escape_t)f->arena)(argc, args);

	return ejit_interp(f, argc, args, state, true, NULL).r;
}

long ejit_run_func(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
{
	assert(f->gpr && "trying to run a function that hasn't been compiled");
	assert(f->rtype == EJIT_VOID || ejit_int_type(f->rtype));
	if (f->arena)
		return ((ejit_escape_t)f->arena)(argc, args);

	struct interp_state state = create_interp_state();
	long r = ejit_interp(f, argc, args, &state, true, NULL).r;
	destroy_interp_state(state);
	return r;
}

double ejit_run_func_f(struct ejit_func *f, size_t argc,
                       struct ejit_arg args[argc])
{
	assert(f->fpr && "trying to run a function that hasn't been compiled");
	assert(ejit_float_type(f->rtype));
	if (f->arena)
		return ((ejit_escape_f_t)f->arena)(argc, args);

	struct interp_state state = create_interp_state();
	double r = ejit_interp(f, argc, args, &state, true, NULL).d;
	destroy_interp_state(state);
	return r;
}