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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
#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: {
assert(ejit_int_type(args[i].type));
emit_insn_r(f, PARAM, i, args[i].type, args[i].r);
break;
}
case EJIT_OPERAND_FPR: {
assert(ejit_float_type(args[i].type));
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;
}
/* doesn't really matter what we put here as long as it isn't 0 */
f->size = 1;
}
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_retval_f(struct ejit_func *s, struct ejit_fpr r0)
{
emit_insn_i(s, RETVAL_F, r0.f, 0, 0);
}
void ejit_stxi_8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, STXI8, r0.r, r1.r, o);
}
void ejit_stxi_16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, STXI16, r0.r, r1.r, o);
}
void ejit_stxi_32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, STXI32, r0.r, r1.r, o);
}
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_stxi_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, STXIF, r0.f, r1.r, o);
}
void ejit_stxi_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, STXID, r0.f, r1.r, o);
}
void ejit_ldi_i8(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDI8, r0.r, 0, p);
}
void ejit_ldi_i16(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDI16, r0.r, 0, p);
}
void ejit_ldi_i32(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDI32, r0.r, 0, p);
}
void ejit_ldi_i64(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDI64, r0.r, 0, p);
}
void ejit_ldi_u8(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDIU8, r0.r, 0, p);
}
void ejit_ldi_u16(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDIU16, r0.r, 0, p);
}
void ejit_ldi_u32(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDIU32, r0.r, 0, p);
}
void ejit_ldi_u64(struct ejit_func *s, struct ejit_gpr r0, void *p)
{
emit_insn_p(s, LDIU64, r0.r, 0, p);
}
void ejit_ldi_f(struct ejit_func *s, struct ejit_fpr r0, void *p)
{
emit_insn_p(s, LDIF, r0.f, 0, p);
}
void ejit_ldi_d(struct ejit_func *s, struct ejit_fpr r0, void *p)
{
emit_insn_p(s, LDID, r0.f, 0, p);
}
void ejit_ldxi_i8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXI8, r0.r, r1.r, o);
}
void ejit_ldxi_i16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXI16, r0.r, r1.r, o);
}
void ejit_ldxi_i32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXI32, r0.r, r1.r, o);
}
void ejit_ldxi_i64(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXI64, r0.r, r1.r, o);
}
void ejit_ldxi_u8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXIU8, r0.r, r1.r, o);
}
void ejit_ldxi_u16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXIU16, r0.r, r1.r, o);
}
void ejit_ldxi_u32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXIU32, 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_ldxi_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXIF, r0.f, r1.r, o);
}
void ejit_ldxi_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LDXID, r0.f, r1.r, o);
}
void ejit_ldxr_i8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXR8, r0.r, r1.r, r2.r);
}
void ejit_ldxr_i16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXR16, r0.r, r1.r, r2.r);
}
void ejit_ldxr_i32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXR32, r0.r, r1.r, r2.r);
}
void ejit_ldxr_i64(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXR64, r0.r, r1.r, r2.r);
}
void ejit_ldxr_u8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXRU8, r0.r, r1.r, r2.r);
}
void ejit_ldxr_u16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXRU16, r0.r, r1.r, r2.r);
}
void ejit_ldxr_u32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXRU32, r0.r, r1.r, r2.r);
}
void ejit_ldxr_u64(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXIU64, r0.r, r1.r, r2.r);
}
void ejit_ldxr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXRF, r0.f, r1.r, r2.r);
}
void ejit_ldxr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LDXRD, r0.f, r1.r, r2.r);
}
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_extr_8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTR8, r0.r, r1.r, 0);
}
void ejit_extr_16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTR16, r0.r, r1.r, 0);
}
void ejit_extr_32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTR32, r0.r, r1.r, 0);
}
void ejit_extr_u8(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTRU8, r0.r, r1.r, 0);
}
void ejit_extr_u16(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTRU16, r0.r, r1.r, 0);
}
void ejit_extr_u32(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, EXTRU32, r0.r, r1.r, 0);
}
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_divr_u(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, DIVR_U, r0.r, r1.r, r2.r);
}
void ejit_divr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, DIVR_F, r0.f, r1.f, r2.f);
}
void ejit_lshi(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, LSHI, r0.r, r1.r, o);
}
void ejit_lshr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, LSHR, r0.r, r1.r, r2.r);
}
void ejit_rshi(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
int64_t o)
{
emit_insn_i(s, RSHI, r0.r, r1.r, o);
}
void ejit_rshr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, RSHR, 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_comr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
emit_insn_i(s, COMR, r0.r, r1.r, 0);
}
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_eqr_f(struct ejit_func *s, struct ejit_gpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, EQR_F, r0.r, r1.f, r2.f);
}
void ejit_gtr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GTR, r0.r, r1.r, r2.r);
}
void ejit_gtr_u(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GTR_U, r0.r, r1.r, r2.r);
}
void ejit_gtr_f(struct ejit_func *s, struct ejit_gpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, GTR_F, r0.r, r1.f, r2.f);
}
void ejit_ltr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GER, r0.r, r2.r, r1.r);
}
void ejit_ltr_u(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GER_U, r0.r, r2.r, r1.r);
}
void ejit_ltr_f(struct ejit_func *s, struct ejit_gpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, GER_F, r0.r, r2.f, r1.f);
}
void ejit_ger(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GER, r0.r, r1.r, r2.r);
}
void ejit_ger_u(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GER_U, r0.r, r1.r, r2.r);
}
void ejit_ger_f(struct ejit_func *s, struct ejit_gpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, GER_F, r0.r, r1.f, r2.f);
}
void ejit_ler(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GTR, r0.r, r2.r, r1.r);
}
void ejit_ler_u(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1,
struct ejit_gpr r2)
{
emit_insn_r(s, GTR_U, r0.r, r2.r, r1.r);
}
void ejit_ler_f(struct ejit_func *s, struct ejit_gpr r0, struct ejit_fpr r1,
struct ejit_fpr r2)
{
emit_insn_r(s, GTR_F, r0.r, r2.f, r1.f);
}
struct ejit_reloc ejit_bner(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BNER, 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_bner_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BNER_F, 0, r0.f, r1.f);
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_beqr_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BEQR_F, 0, r0.f, r1.f);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bger(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER, 0, r0.r, r1.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bger_u(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER_U, 0, r0.r, r1.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bger_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER_F, 0, r0.f, r1.f);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bgei(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BGEI, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bgei_u(struct ejit_func *s, struct ejit_gpr r0,
int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BGEI_U, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bler(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER, 0, r1.r, r0.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bler_u(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER_U, 0, r1.r, r0.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bler_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGER_F, 0, r1.f, r0.f);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_blei(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BLEI, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_blei_u(struct ejit_func *s, struct ejit_gpr r0,
int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BLEI_U, 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_bgti_u(struct ejit_func *s, struct ejit_gpr r0,
int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BGTI_U, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bgtr(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGTR, 0, r0.r, r1.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bgtr_u(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGTR_U, 0, r0.r, r1.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bgtr_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGTR_F, 0, r0.f, r1.f);
return (struct ejit_reloc){.insn = addr};
}
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, BGTR, 0, r1.r, r0.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bltr_u(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGTR_U, 0, r1.r, r0.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bltr_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BGTR_F, 0, r1.f, r0.f);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_blti(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BLTI, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_blti_u(struct ejit_func *s, struct ejit_gpr r0,
int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BLTI_U, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bltgtr_f(struct ejit_func *s, struct ejit_fpr r0,
struct ejit_fpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_r(s, BLTGTR_F, 0, r0.f, r1.f);
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};
}
struct ejit_reloc ejit_jmpr(struct ejit_func *s, struct ejit_gpr r0)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, JMPR, 0, r0.r, 0);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bmci(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BMCI, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bmcr(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BMCR, 0, r0.r, r1.r);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bmsi(struct ejit_func *s, struct ejit_gpr r0, int64_t o)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BMSI, 0, r0.r, o);
return (struct ejit_reloc){.insn = addr};
}
struct ejit_reloc ejit_bmsr(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
size_t addr = vec_len(&s->insns);
emit_insn_i(s, BMSR, 0, r0.r, r1.r);
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->size && "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->size && "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;
}
|