summaryrefslogtreecommitdiff
path: root/src/vm.c
blob: 3e98d2c977dd401d51dc7246eb68c5fbbafde0e2 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include <stdio.h>

#include <ejit/ejit.h>
#include <berg/vm.h>

#define VEC_NAME funcs
#define VEC_TYPE struct berg_func *
#include <conts/vec.h>

#define VEC_NAME insns
#define VEC_TYPE struct berg_insn
#include <conts/vec.h>

#define VEC_NAME operands
#define VEC_TYPE struct berg_operand
#include <conts/vec.h>

#define VEC_NAME labels
#define VEC_TYPE struct ejit_label
#include <conts/vec.h>

struct reloc_helper {
	struct ejit_reloc reloc;
	size_t target;
};

#define VEC_NAME relocs
#define VEC_TYPE struct reloc_helper
#include <conts/vec.h>

struct alloc {
	void *base, *top;
};

struct allocs {
	/* must always be pow2 */
	size_t size;
	struct alloc *buf;
};

struct berg_vm {
	struct funcs funcs;
	struct allocs allocs;
};

struct berg_func {
	/* owning vm */
	struct berg_vm *vm;

	/* vm instructions */
	struct insns insns;

	/* labels */
	struct labels labels;

	/* relocs */
	struct relocs relocs;

	/* arguments */
	struct operands operands;

	/* actual function */
	struct ejit_func *func;
};

static struct allocs allocs_create(size_t pow2)
{
	size_t size = 1ULL << pow2;
	struct alloc *buf = calloc(size, sizeof(struct alloc));
	struct allocs allocs = {.size = size, buf = buf};
	return allocs;
}

static size_t allocs_idx(void *ptr, size_t size)
{
	/* presumably all pointers have at least 16 bytes alignment, although
	 * this might only apply to amd64 (check max_alignment_t?) */
	return ((uintptr_t)ptr >> 4) & (size - 1);
}

static long allocs_resize(struct allocs *allocs)
{
	/* very naive allocation strategy, a single collision causes a regrow */
	size_t new_size = allocs->size * 2;

top:
	struct alloc *new_buf = calloc(new_size, sizeof(struct alloc));
	if (!new_buf)
		return -1;

	for (size_t i = 0; i < allocs->size; ++i) {
		struct alloc *alloc = &allocs->buf[i];
		if (!alloc->base)
			continue;

		uintptr_t idx = allocs_idx(alloc->base, new_size);
		struct alloc *new_alloc = &new_buf[idx];
		if (new_alloc->base) {
			/* try again */
			free(new_buf);
			new_size *= 2;
			goto top;
		}

		*new_alloc = *alloc;
	}

	allocs->size = new_size;
	allocs->buf = new_buf;
	return 0;
}

static long allocs_insert(struct allocs *allocs, void *ptr, size_t size)
{
	void *top = ptr + size;

	while (1) {
		/* try to place allocation into map */
		size_t idx = allocs_idx(ptr, allocs->size);
		struct alloc *node = &allocs->buf[idx];

		/* free slot */
		if (node->base == NULL) {
			node->base = ptr;
			node->top = top;
			return 0;
		}

		int ret = allocs_resize(allocs);
		if (ret)
			return ret;
	}

	return 0;
}

static long allocs_remove(struct allocs *allocs, void *ptr)
{
	uintptr_t idx = allocs_idx(ptr, allocs->size);
	struct alloc *alloc = &allocs->buf[idx];
	if (alloc->base != ptr)
		return -1;

	alloc->base = NULL;
	alloc->top = NULL;
	return 0;
}

struct berg_vm *create_berg_vm()
{
	struct berg_vm *vm = calloc(1, sizeof(struct berg_vm));
	vm->funcs = funcs_create(0);

	/* 2^4 elements to start with */
	vm->allocs = allocs_create(4);
	return vm;
}

static enum ejit_type ejit_type_from(enum berg_type type)
{
	switch (type) {
	case BERG_POINTER: return EJIT_POINTER;
	case BERG_VOID: return EJIT_VOID;
	default: abort();
	}
}

const uint8_t NOGPR = 255;
/* two reserved registers for keeping track of allocation stuff. Should be
 * fetched from VM to registers (or stack I guess?) whenever a call happens,
 * since the size of the allocation hashmap might've changed while we weren't
 * looking */
const struct ejit_gpr ALLOC_MASK = EJIT_GPR(0);
const struct ejit_gpr ALLOC_BUF = EJIT_GPR(1);
const struct ejit_gpr TMP0 = EJIT_GPR(2);
const struct ejit_gpr TMP1 = EJIT_GPR(3);

static struct ejit_gpr ejit_gpr_from(uint8_t r)
{
	assert(r != NOGPR);
	/* four reserved registers (seems like a lot?) */
	return EJIT_GPR(r + 4);
}

static long reload_alloc_status(struct berg_func *f)
{
	EJIT_LDI(f->func, size_t, ALLOC_MASK, &f->vm->allocs.size);
	EJIT_LDI(f->func, void *, ALLOC_BUF, &f->vm->allocs.buf);
	ejit_subi(f->func, ALLOC_MASK, ALLOC_MASK, 1);
	return 0;
}

struct berg_func *create_berg_func(struct berg_vm *vm, enum berg_type rtype, size_t argc, const struct berg_operand args[argc])
{
	struct berg_func *f = calloc(1, sizeof(struct berg_func));
	f->vm = vm;
	f->insns = insns_create(0);
	f->labels = labels_create(0);
	f->operands = operands_create(argc);

	struct ejit_operand ejit_operands[argc];
	for (size_t i = 0; i < argc; ++i) {
		operands_append(&f->operands, args[i]);
		ejit_operands[i] = EJIT_OPERAND_GPR(
				ejit_gpr_from(args[i].gpr.r).r,
				ejit_type_from(args[i].type)
				);
	}

	f->func = ejit_create_func(ejit_type_from(rtype), argc, ejit_operands);
	reload_alloc_status(f);

	funcs_append(&vm->funcs, f);
	return f;
}


static struct berg_insn insn_ori(enum berg_op op, struct berg_gpr ra, int64_t imm)
{
	return (struct berg_insn){
		.op = op,
		.ra = ra.r,
		.rb = NOGPR,
		.rc = NOGPR,
		.rd = NOGPR,
		.imm = imm
	};
}

static struct berg_insn insn_orr(enum berg_op op, struct berg_gpr ra, struct berg_gpr rb)
{
	return (struct berg_insn){
		.op = op,
		.ra = ra.r,
		.rb = rb.r,
		.rc = NOGPR,
		.rd = NOGPR,
		.imm = 0
	};
}

static struct berg_insn insn_orrr(enum berg_op op, struct berg_gpr ra, struct berg_gpr rb, struct berg_gpr rc)
{
	return (struct berg_insn){
		.op = op,
		.ra = ra.r,
		.rb = rb.r,
		.rc = rc.r,
		.rd = NOGPR,
		.imm = 0
	};
}

static struct berg_insn insn_orri(enum berg_op op, struct berg_gpr ra, struct berg_gpr rb, int64_t imm)
{
	return (struct berg_insn){
		.op = op,
		.ra = ra.r,
		.rb = rb.r,
		.rc = NOGPR,
		.rd = NOGPR,
		.imm = imm
	};
}

static struct berg_insn insn_oxrri(enum berg_op op, struct berg_gpr rb, struct berg_gpr rc, int64_t imm)
{
	return (struct berg_insn){
		.op = op,
		.ra = NOGPR,
		.rb = rb.r,
		.rc = rc.r,
		.rd = NOGPR,
		.imm = imm
	};
}

static struct berg_insn insn_oxrrr(enum berg_op op, struct berg_gpr rb, struct berg_gpr rc, struct berg_gpr rd)
{
	return (struct berg_insn){
		.op = op,
		.ra = NOGPR,
		.rb = rb.r,
		.rc = rc.r,
		.rd = rd.r,
		.imm = 0
	};
}

static struct berg_insn insn_o(enum berg_op op)
{
	return (struct berg_insn){
		.op = op,
		.ra = NOGPR,
		.rb = NOGPR,
		.rc = NOGPR,
		.rd = NOGPR,
		.imm = 0
	};
}

static struct berg_insn insn_op(enum berg_op op, void *p)
{
	return (struct berg_insn){
		.op = op,
		.ra = NOGPR,
		.rb = NOGPR,
		.rc = NOGPR,
		.rd = NOGPR,
		.p = p
	};
}

static struct berg_insn insn_oi(enum berg_op op, int64_t imm)
{
	return (struct berg_insn){
		.op = op,
		.ra = NOGPR,
		.rb = NOGPR,
		.rc = NOGPR,
		.rd = NOGPR,
		.imm = imm
	};
}

long berg_movi(struct berg_func *f, struct berg_gpr ra, int64_t imm)
{
	insns_append(&f->insns, insn_ori(BERG_MOVI, ra, imm));
	/* for now */
	return 0;
}

long berg_movr(struct berg_func *f, struct berg_gpr ra, struct berg_gpr rb)
{
	insns_append(&f->insns, insn_orr(BERG_MOVR, ra, rb));
	return 0;
}

long berg_ret(struct berg_func *f)
{
	insns_append(&f->insns, insn_o(BERG_RET));
	return 0;
}

long berg_ecall(struct berg_func *f, struct berg_gpr ra, int64_t imm)
{
	insns_append(&f->insns, insn_ori(BERG_ECALL, ra, imm));
	return 0;
}

long berg_call(struct berg_func *f, struct berg_func *c)
{
	assert(f->vm == c->vm);
	insns_append(&f->insns, insn_op(BERG_CALL, c));
	return 0;
}

long berg_addr(struct berg_func *f, struct berg_gpr ra, struct berg_gpr rb, struct berg_gpr rc)
{
	insns_append(&f->insns, insn_orrr(BERG_ADDR, ra, rb, rc));
	return 0;
}

long berg_addi(struct berg_func *f, struct berg_gpr ra, struct berg_gpr rb, int64_t imm)
{
	insns_append(&f->insns, insn_orri(BERG_ADDI, ra, rb, imm));
	return 0;
}

long berg_mulr(struct berg_func *f, struct berg_gpr ra, struct berg_gpr rb, struct berg_gpr rc)
{
	insns_append(&f->insns, insn_orrr(BERG_MULR, ra, rb, rc));
	return 0;
}

long berg_lshi(struct berg_func *f, struct berg_gpr ra, struct berg_gpr rb, int64_t imm)
{
	assert(imm < 64);
	insns_append(&f->insns, insn_orri(BERG_LSHI, ra, rb, imm));
	return 0;
}

long berg_stxr_32(struct berg_func *f, struct berg_gpr rb, struct berg_gpr rc, struct berg_gpr rd)
{
	insns_append(&f->insns, insn_oxrrr(BERG_STXR_32, rb, rc, rd));
	return 0;
}

long berg_ldxr_i32(struct berg_func *f, struct berg_gpr rb, struct berg_gpr rc, struct berg_gpr rd)
{
	insns_append(&f->insns, insn_oxrrr(BERG_LDXR_I32, rb, rc, rd));
	return 0;
}

struct berg_reloc berg_bger(struct berg_func *f,
		struct berg_gpr rb,
		struct berg_gpr rc)
{
	struct berg_reloc r = {.r = insns_len(&f->insns)};
	insns_append(&f->insns, insn_oxrri(BERG_BGER, rb, rc, 0));
	return r;
}

struct berg_reloc berg_jmp(struct berg_func *f)
{
	struct berg_reloc r = {.r = insns_len(&f->insns)};
	insns_append(&f->insns, insn_oi(BERG_JMP, 0));
	return r;
}

struct berg_label berg_label(struct berg_func *f)
{
	return (struct berg_label){.l = insns_len(&f->insns)};
}

void berg_patch(struct berg_func *f, struct berg_reloc r, struct berg_label l)
{
	struct berg_insn *insn = insns_at(&f->insns, r.r);
	insn->imm = l.l;
}

const char *berg_op_str(enum berg_op op)
{
#define CASE(x) case x: return #x;
	switch (op) {
		CASE(BERG_LABEL);
		CASE(BERG_LDXR_I32);
		CASE(BERG_STXR_32);
		CASE(BERG_CALL);
		CASE(BERG_RET);
		CASE(BERG_ECALL);
		CASE(BERG_MOVI);
		CASE(BERG_MOVR);
		CASE(BERG_ADDR);
		CASE(BERG_ADDI);
		CASE(BERG_LSHI);
		CASE(BERG_MULR);
		CASE(BERG_BGER);
		CASE(BERG_JMP);
	}

	return "???";
#undef CASE
}

static long compile_ret(struct berg_func *f)
{
	ejit_ret(f->func);
	return 0;
}

static long compile_movi(struct berg_func *f, struct berg_insn *i)
{
	ejit_movi(f->func, ejit_gpr_from(i->ra), i->imm);
	return 0;
}

/* should really be uintptr_t... */
static long escape_malloc(size_t argc, const struct ejit_arg args[argc])
{
	struct berg_vm *vm = EJIT_PARAM(argc, args, 0, struct berg_vm *);
	size_t size = EJIT_PARAM(argc, args, 1, size_t);
	void *ptr = malloc(size);
	if (!ptr)
		return 0;

	if (allocs_insert(&vm->allocs, ptr, size)) {
		free(ptr);
		return 0;
	}

	return (long)ptr;
}

static long escape_free(size_t argc, const struct ejit_arg args[argc])
{
	struct berg_vm *vm = EJIT_PARAM(argc, args, 0, struct berg_vm *);
	void *ptr = EJIT_PARAM(argc, args, 1, void *);
	if (allocs_remove(&vm->allocs, ptr)) {
		fprintf(stderr, "trying to free nonexistant pointer\n");
		abort();
	}

	free(ptr);
	return 0;
}

static long compile_malloc(struct berg_func *f, struct berg_insn *i)
{
	struct berg_gpr size = berg_gpr_arg(0);

	ejit_movi(f->func, TMP0, (int64_t)f->vm);
	struct ejit_operand args[] = {
		EJIT_OPERAND_GPR(TMP0.r, EJIT_POINTER),
		EJIT_OPERAND_GPR(ejit_gpr_from(size.r).r, EJIT_TYPE(size_t)),
	};
	ejit_escapei_l(f->func, escape_malloc, 2, args);
	ejit_retval(f->func, ejit_gpr_from(i->ra));

	/* reload allocation context if the hashmap has changed */
	reload_alloc_status(f);
	return 0;
}

static long compile_free(struct berg_func *f, struct berg_insn *i)
{
	struct berg_gpr ptr = berg_gpr_arg(0);

	ejit_movi(f->func, TMP0, (int64_t)f->vm);
	struct ejit_operand args[] = {
		EJIT_OPERAND_GPR(TMP0.r, EJIT_POINTER),
		EJIT_OPERAND_GPR(ejit_gpr_from(ptr.r).r, EJIT_POINTER),
	};
	ejit_escapei_l(f->func, escape_free, 2, args);
	ejit_retval(f->func, ejit_gpr_from(i->ra));

	/* no need to reload anything since (at least currently) hashmap can't
	 * shrink */
	return 0;
}

static long escape_puti32(size_t argc, const struct ejit_arg args[argc])
{
	int32_t i32 = EJIT_PARAM(argc, args, 0, int32_t);
	printf("%lli\n", (long long)i32);
	return 0;
}

static long compile_puti32(struct berg_func *f, struct berg_insn *i)
{
	struct berg_gpr i32 = berg_gpr_arg(0);

	struct ejit_operand args[] = {
		EJIT_OPERAND_GPR(ejit_gpr_from(i32.r).r, EJIT_INT32),
	};
	ejit_escapei_l(f->func, escape_puti32, 1, args);
	ejit_retval(f->func, ejit_gpr_from(i->ra));

	return 0;
}

static long compile_ecall(struct berg_func *f, struct berg_insn *i)
{
	switch (i->imm) {
	case BERG_MALLOC: return compile_malloc(f, i);
	case BERG_FREE: return compile_free(f, i);
	case BERG_PUTI32: return compile_puti32(f, i);
	}

	fprintf(stderr, "unknown envcall: %lli\n", (long long)i->imm);
	return -1;
}

/** @todo this doesn't really work for calling functions via registers, unsure
 * how that situation should be handled */
static long compile_call(struct berg_func *f, struct berg_insn *i)
{
	struct berg_func *c = i->p;

	size_t operands = operands_len(&c->operands);
	struct ejit_operand ejit_operands[operands];
	for (size_t i = 0; i < operands; ++i) {
		struct berg_gpr reg = berg_gpr_arg(i);
		struct berg_operand *operand = operands_at(&c->operands, i);
		ejit_operands[i] = EJIT_OPERAND_GPR(
				ejit_gpr_from(reg.r).r,
				ejit_type_from(operand->type)
				);
	}

	ejit_calli(f->func, c->func, operands, ejit_operands);

	/* reload allocation map since we don't know if something was maybe
	 * freed */
	reload_alloc_status(f);
	return 0;
}

static long compile_movr(struct berg_func *f, struct berg_insn *i)
{
	ejit_movr(f->func, ejit_gpr_from(i->ra), ejit_gpr_from(i->rb));
	return 0;
}

static long compile_bger(struct berg_func *f, struct berg_insn *i)
{
	struct ejit_reloc r = ejit_bger(f->func, ejit_gpr_from(i->rb), ejit_gpr_from(i->rc));
	relocs_append(&f->relocs, (struct reloc_helper){.reloc = r, .target = i->imm});
	return 0;
}

static long compile_jmp(struct berg_func *f, struct berg_insn *i)
{
	struct ejit_reloc r = ejit_jmp(f->func);
	relocs_append(&f->relocs, (struct reloc_helper){.reloc = r, .target = i->imm});
	return 0;
}

static long compile_mulr(struct berg_func *f, struct berg_insn *i)
{
	ejit_mulr(f->func, ejit_gpr_from(i->ra), ejit_gpr_from(i->rb), ejit_gpr_from(i->rc));
	return 0;
}

static long compile_addr(struct berg_func *f, struct berg_insn *i)
{
	ejit_addr(f->func, ejit_gpr_from(i->ra), ejit_gpr_from(i->rb), ejit_gpr_from(i->rc));
	return 0;
}

static long compile_addi(struct berg_func *f, struct berg_insn *i)
{
	ejit_addi(f->func, ejit_gpr_from(i->ra), ejit_gpr_from(i->rb), i->imm);
	return 0;
}

static long compile_lshi(struct berg_func *f, struct berg_insn *i)
{
	ejit_lshi(f->func, ejit_gpr_from(i->ra), ejit_gpr_from(i->rb), i->imm);
	return 0;
}

static long compile_reg_bounds_check(struct berg_func *f, ejit_escape_i_t handler, uint8_t base, uint8_t offset, uint8_t data, enum ejit_type type)
{
	/* load base */
	ejit_rshi(f->func, TMP0, ejit_gpr_from(base), 4);
	ejit_andr(f->func, TMP0, TMP0, ALLOC_MASK);

	static_assert(sizeof(struct alloc) == 1ULL << 4);
	ejit_lshi(f->func, TMP0, TMP0, 4);

	/** @todo would probably be faster to have inline boolean checks instead of
	 * double jumps, should add support for something like that to ejit */

	EJIT_LDXR(f->func, size_t, TMP1, ALLOC_BUF, TMP0);
	/* check that base is larger than loaded value (value can be NULL at
	 * this point) */
	struct ejit_reloc base_check = ejit_bger_u(f->func, TMP1, ejit_gpr_from(base));

	/* do slow fallback */
	struct ejit_operand args[] = {
		EJIT_OPERAND_GPR(TMP0.r, EJIT_POINTER),
		EJIT_OPERAND_GPR(ejit_gpr_from(base).r, EJIT_POINTER),
		EJIT_OPERAND_GPR(ejit_gpr_from(offset).r, EJIT_TYPE(intptr_t)),
		EJIT_OPERAND_GPR(ejit_gpr_from(data).r, type)
	};
	ejit_movi(f->func, TMP0, (uintptr_t)f->vm);
	ejit_escapei_l(f->func, handler, 4, args);

	struct ejit_reloc base_continue = ejit_jmp(f->func);
	ejit_patch(f->func, base_check, ejit_label(f->func));

	/* load top */
	ejit_addi(f->func, TMP1, TMP0, offsetof(struct alloc, top));
	EJIT_LDXR(f->func, void *, TMP1, ALLOC_BUF, TMP1);
	ejit_addr(f->func, TMP0, ejit_gpr_from(base), ejit_gpr_from(offset));
	struct ejit_reloc top_check = ejit_bler_u(f->func, TMP0, TMP1);

	/* do slow fallback */
	ejit_movi(f->func, TMP0, (uintptr_t)f->vm);
	ejit_escapei_l(f->func, handler, 4, args);

	struct ejit_label ok = ejit_label(f->func);
	ejit_patch(f->func, top_check, ok);
	ejit_patch(f->func, base_continue, ok);
	return 0;
}

static long escape_stxr_32_bounds_check(size_t argc, const struct ejit_arg args[argc])
{
	struct berg_vm *vm = EJIT_PARAM(argc, args, 0, struct berg_vm *);
	void *base = EJIT_PARAM(argc, args, 1, void *);
	intptr_t offset = EJIT_PARAM(argc, args, 2, intptr_t);
	int32_t data = EJIT_PARAM(argc, args, 3, int32_t);

	fprintf(stderr, "stxr32 fallback bounds check unimplemented\n");
	abort();
	return 0;
}

static long compile_stxr_32(struct berg_func *f, struct berg_insn *i)
{
	/* for now, always check if address is valid */
	uint8_t base = i->rc;
	uint8_t offset = i->rd;
	uint8_t data = i->rb;

	compile_reg_bounds_check(f, escape_stxr_32_bounds_check, base, offset, data, EJIT_INT32);

	/* ok, do actual store */
	ejit_stxr_32(f->func, ejit_gpr_from(data), ejit_gpr_from(base), ejit_gpr_from(offset));
	return 0;
}

static long escape_ldxr_i32_bounds_check(size_t argc, const struct ejit_arg args[argc])
{
	struct berg_vm *vm = EJIT_PARAM(argc, args, 0, struct berg_vm *);
	void *base = EJIT_PARAM(argc, args, 1, void *);
	intptr_t offset = EJIT_PARAM(argc, args, 2, intptr_t);
	int32_t data = EJIT_PARAM(argc, args, 3, int32_t);

	fprintf(stderr, "ldxr32 fallback bounds check unimplemented\n");
	abort();
	return 0;
}

static long compile_ldxr_i32(struct berg_func *f, struct berg_insn *i)
{
	/* for now, always check if address is valid */
	uint8_t base = i->rc;
	uint8_t offset = i->rd;
	uint8_t data = i->rb;

	compile_reg_bounds_check(f, escape_ldxr_i32_bounds_check, base, offset, data, EJIT_INT32);

	/* ok, do actual store */
	ejit_stxr_32(f->func, ejit_gpr_from(data), ejit_gpr_from(base), ejit_gpr_from(offset));
	return 0;
}

long compile_berg_func(struct berg_func *f)
{
	/** @todo eventually should make this be block-specific */
	long ret = 0;
	foreach(insns, i, &f->insns) {
		labels_append(&f->labels, ejit_label(f->func));

		switch (i->op) {
		case BERG_RET:
			if ((ret = compile_ret(f)))
				return ret;
			break;

		case BERG_MOVI:
			if ((ret = compile_movi(f, i)))
				return ret;
			break;

		case BERG_MOVR:
			if ((ret = compile_movr(f, i)))
				return ret;
			break;

		case BERG_CALL:
			if ((ret = compile_call(f, i)))
				return ret;
			break;

		case BERG_ECALL:
			if ((ret = compile_ecall(f, i)))
				return ret;
			break;

		case BERG_MULR:
			if ((ret = compile_mulr(f, i)))
				return ret;
			break;

		case BERG_ADDR:
			if ((ret = compile_addr(f, i)))
				return ret;
			break;

		case BERG_ADDI:
			if ((ret = compile_addi(f, i)))
				return ret;
			break;

		case BERG_LSHI:
			if ((ret = compile_lshi(f, i)))
				return ret;
			break;

		case BERG_STXR_32:
			if ((ret = compile_stxr_32(f, i)))
				return ret;
			break;

		case BERG_LDXR_I32:
			if ((ret = compile_ldxr_i32(f, i)))
				return ret;
			break;

		case BERG_LABEL:
			break;

		case BERG_BGER:
			if ((ret = compile_bger(f, i)))
				return ret;
			break;

		case BERG_JMP:
			if ((ret = compile_jmp(f, i)))
				return ret;
			break;

		default:
			fprintf(stderr, "unhandled op %s\n", berg_op_str(i->op));
			return -1;
		}
	}

	/* patch relocs */
	foreach(relocs, r, &f->relocs) {
		struct ejit_label *label = labels_at(&f->labels, r->target);
		ejit_patch(f->func, r->reloc, *label);
	}

	ejit_compile_func(f->func);
	return 0;
}

long run_berg_func(struct berg_func *f)
{
	struct ejit_arg arg = ejit_run_func(f->func, 0, NULL);
	/* for now */
	assert(arg.type == EJIT_VOID);
	return 0;
}

static void destroy_berg_func(struct berg_func *f)
{
	insns_destroy(&f->insns);
	operands_destroy(&f->operands);
	labels_destroy(&f->labels);
	relocs_destroy(&f->relocs);
	ejit_destroy_func(f->func);
	free(f);
}

void destroy_berg_vm(struct berg_vm *vm)
{
	foreach(funcs, f, &vm->funcs) {
		destroy_berg_func(*f);
	}
	funcs_destroy(&vm->funcs);

	for (size_t i = 0; i < vm->allocs.size; ++i) {
		struct alloc a = vm->allocs.buf[i];
		if (a.base)
			free(a.base);
	}

	free(vm->allocs.buf);
	free(vm);
}