-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyjit_asm.c
1817 lines (1576 loc) · 52.4 KB
/
yjit_asm.c
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a fragment of the yjit.o compilation unit. See yjit.c.
//
// Note that the definition for some of these functions don't specify
// static inline, but their declaration in yjit_asm.h do. The resulting
// linkage is the same as if they both specify. The relevant sections in
// N1256 is 6.2.2p4, 6.2.2p5, and 6.7.4p5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
// For mmapp(), sysconf()
#ifndef _WIN32
#include <unistd.h>
#include <sys/mman.h>
#endif
#include "yjit_asm.h"
// Compute the number of bits needed to encode a signed value
uint32_t sig_imm_size(int64_t imm)
{
// Compute the smallest size this immediate fits in
if (imm >= INT8_MIN && imm <= INT8_MAX)
return 8;
if (imm >= INT16_MIN && imm <= INT16_MAX)
return 16;
if (imm >= INT32_MIN && imm <= INT32_MAX)
return 32;
return 64;
}
// Compute the number of bits needed to encode an unsigned value
uint32_t unsig_imm_size(uint64_t imm)
{
// Compute the smallest size this immediate fits in
if (imm <= UINT8_MAX)
return 8;
else if (imm <= UINT16_MAX)
return 16;
else if (imm <= UINT32_MAX)
return 32;
return 64;
}
x86opnd_t mem_opnd(uint32_t num_bits, x86opnd_t base_reg, int32_t disp)
{
bool is_iprel = base_reg.as.reg.reg_type == REG_IP;
x86opnd_t opnd = {
OPND_MEM,
num_bits,
.as.mem = { base_reg.as.reg.reg_no, 0, 0, false, is_iprel, disp }
};
return opnd;
}
x86opnd_t mem_opnd_sib(uint32_t num_bits, x86opnd_t base_reg, x86opnd_t index_reg, int32_t scale, int32_t disp)
{
uint8_t scale_exp;
switch (scale) {
case 8:
scale_exp = 3;
break;
case 4:
scale_exp = 2;
break;
case 2:
scale_exp = 1;
break;
case 1:
scale_exp = 0;
break;
default:
rb_bug("yjit: scale not one of 1,2,4,8");
break;
}
bool is_iprel = base_reg.as.reg.reg_type == REG_IP;
x86opnd_t opnd = {
OPND_MEM,
num_bits,
.as.mem = {
.base_reg_no = base_reg.as.reg.reg_no,
.idx_reg_no = index_reg.as.reg.reg_no,
.has_idx = 1,
.scale_exp = scale_exp,
.is_iprel = is_iprel,
.disp = disp
}
};
return opnd;
}
static x86opnd_t resize_opnd(x86opnd_t opnd, uint32_t num_bits)
{
assert (num_bits % 8 == 0);
x86opnd_t sub = opnd;
sub.num_bits = num_bits;
return sub;
}
x86opnd_t imm_opnd(int64_t imm)
{
x86opnd_t opnd = {
OPND_IMM,
sig_imm_size(imm),
.as.imm = imm
};
return opnd;
}
x86opnd_t const_ptr_opnd(const void *ptr)
{
x86opnd_t opnd = {
OPND_IMM,
64,
.as.unsig_imm = (uint64_t)ptr
};
return opnd;
}
// Align the current write position to a multiple of bytes
static uint8_t *align_ptr(uint8_t *ptr, uint32_t multiple)
{
// Compute the pointer modulo the given alignment boundary
uint32_t rem = ((uint32_t)(uintptr_t)ptr) % multiple;
// If the pointer is already aligned, stop
if (rem == 0)
return ptr;
// Pad the pointer by the necessary amount to align it
uint32_t pad = multiple - rem;
return ptr + pad;
}
// Allocate a block of executable memory
static uint8_t *alloc_exec_mem(uint32_t mem_size)
{
uint8_t *mem_block;
// On Linux
#if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
// Align the requested address to page size
uint32_t page_size = (uint32_t)sysconf(_SC_PAGESIZE);
uint8_t *req_addr = align_ptr((uint8_t*)&alloc_exec_mem, page_size);
do {
// Try to map a chunk of memory as executable
mem_block = (uint8_t*)mmap(
(void*)req_addr,
mem_size,
PROT_READ | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
-1,
0
);
// If we succeeded, stop
if (mem_block != MAP_FAILED) {
break;
}
// +4MB
req_addr += 4 * 1024 * 1024;
} while (req_addr < (uint8_t*)&alloc_exec_mem + INT32_MAX);
// On MacOS and other platforms
#else
// Try to map a chunk of memory as executable
mem_block = (uint8_t*)mmap(
(void*)alloc_exec_mem,
mem_size,
PROT_READ | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0
);
#endif
// Fallback
if (mem_block == MAP_FAILED) {
// Try again without the address hint (e.g., valgrind)
mem_block = (uint8_t*)mmap(
NULL,
mem_size,
PROT_READ | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0
);
}
// Check that the memory mapping was successful
if (mem_block == MAP_FAILED) {
perror("mmap call failed");
exit(-1);
}
codeblock_t block;
codeblock_t *cb = █
cb_init(cb, mem_block, mem_size);
// Fill the executable memory with PUSH DS (0x1E) so that
// executing uninitialized memory will fault with #UD in
// 64-bit mode.
cb_mark_all_writeable(cb);
memset(mem_block, 0x1E, mem_size);
cb_mark_all_executable(cb);
return mem_block;
}
// Initialize a code block object
void cb_init(codeblock_t *cb, uint8_t *mem_block, uint32_t mem_size)
{
assert (mem_block);
cb->mem_block_ = mem_block;
cb->mem_size = mem_size;
cb->write_pos = 0;
cb->num_labels = 0;
cb->num_refs = 0;
cb->current_aligned_write_pos = ALIGNED_WRITE_POSITION_NONE;
}
// Set the current write position
void cb_set_pos(codeblock_t *cb, uint32_t pos)
{
// Assert here since while assembler functions do bounds checking, there is
// nothing stopping users from taking out an out-of-bounds pointer and
// doing bad accesses with it.
assert (pos < cb->mem_size);
cb->write_pos = pos;
}
// Align the current write position to a multiple of bytes
void cb_align_pos(codeblock_t *cb, uint32_t multiple)
{
// Compute the pointer modulo the given alignment boundary
uint8_t *ptr = cb_get_write_ptr(cb);
uint8_t *aligned_ptr = align_ptr(ptr, multiple);
const uint32_t write_pos = cb->write_pos;
// Pad the pointer by the necessary amount to align it
ptrdiff_t pad = aligned_ptr - ptr;
cb_set_pos(cb, write_pos + (int32_t)pad);
}
// Set the current write position from a pointer
void cb_set_write_ptr(codeblock_t *cb, uint8_t *code_ptr)
{
intptr_t pos = code_ptr - cb->mem_block_;
assert (pos < cb->mem_size);
cb_set_pos(cb, (uint32_t)pos);
}
// Get a direct pointer into the executable memory block
uint8_t *cb_get_ptr(const codeblock_t *cb, uint32_t index)
{
if (index < cb->mem_size) {
return &cb->mem_block_[index];
}
else {
return NULL;
}
}
// Get a direct pointer to the current write position
uint8_t *cb_get_write_ptr(const codeblock_t *cb)
{
return cb_get_ptr(cb, cb->write_pos);
}
// Write a byte at the current position
void cb_write_byte(codeblock_t *cb, uint8_t byte)
{
assert (cb->mem_block_);
if (cb->write_pos < cb->mem_size) {
cb_mark_position_writeable(cb, cb->write_pos);
cb->mem_block_[cb->write_pos] = byte;
cb->write_pos++;
}
else {
cb->dropped_bytes = true;
}
}
// Write multiple bytes starting from the current position
void cb_write_bytes(codeblock_t *cb, uint32_t num_bytes, ...)
{
va_list va;
va_start(va, num_bytes);
for (uint32_t i = 0; i < num_bytes; ++i)
{
uint8_t byte = va_arg(va, int);
cb_write_byte(cb, byte);
}
va_end(va);
}
// Write a signed integer over a given number of bits at the current position
void cb_write_int(codeblock_t *cb, uint64_t val, uint32_t num_bits)
{
assert (num_bits > 0);
assert (num_bits % 8 == 0);
// Switch on the number of bits
switch (num_bits) {
case 8:
cb_write_byte(cb, (uint8_t)val);
break;
case 16:
cb_write_bytes(
cb,
2,
(uint8_t)((val >> 0) & 0xFF),
(uint8_t)((val >> 8) & 0xFF)
);
break;
case 32:
cb_write_bytes(
cb,
4,
(uint8_t)((val >> 0) & 0xFF),
(uint8_t)((val >> 8) & 0xFF),
(uint8_t)((val >> 16) & 0xFF),
(uint8_t)((val >> 24) & 0xFF)
);
break;
default:
{
// Compute the size in bytes
uint32_t num_bytes = num_bits / 8;
// Write out the bytes
for (uint32_t i = 0; i < num_bytes; ++i)
{
uint8_t byte_val = (uint8_t)(val & 0xFF);
cb_write_byte(cb, byte_val);
val >>= 8;
}
}
}
}
// Allocate a new label with a given name
uint32_t cb_new_label(codeblock_t *cb, const char *name)
{
//if (hasASM)
// writeString(to!string(label) ~ ":");
assert (cb->num_labels < MAX_LABELS);
// Allocate the new label
uint32_t label_idx = cb->num_labels++;
// This label doesn't have an address yet
cb->label_addrs[label_idx] = 0;
cb->label_names[label_idx] = name;
return label_idx;
}
// Write a label at the current address
void cb_write_label(codeblock_t *cb, uint32_t label_idx)
{
assert (label_idx < MAX_LABELS);
cb->label_addrs[label_idx] = cb->write_pos;
}
// Add a label reference at the current write position
void cb_label_ref(codeblock_t *cb, uint32_t label_idx)
{
assert (label_idx < MAX_LABELS);
assert (cb->num_refs < MAX_LABEL_REFS);
// Keep track of the reference
cb->label_refs[cb->num_refs] = (labelref_t){ cb->write_pos, label_idx };
cb->num_refs++;
}
// Link internal label references
void cb_link_labels(codeblock_t *cb)
{
uint32_t orig_pos = cb->write_pos;
// For each label reference
for (uint32_t i = 0; i < cb->num_refs; ++i)
{
uint32_t ref_pos = cb->label_refs[i].pos;
uint32_t label_idx = cb->label_refs[i].label_idx;
assert (ref_pos < cb->mem_size);
assert (label_idx < MAX_LABELS);
uint32_t label_addr = cb->label_addrs[label_idx];
assert (label_addr < cb->mem_size);
// Compute the offset from the reference's end to the label
int64_t offset = (int64_t)label_addr - (int64_t)(ref_pos + 4);
cb_set_pos(cb, ref_pos);
cb_write_int(cb, offset, 32);
}
cb->write_pos = orig_pos;
// Clear the label positions and references
cb->num_labels = 0;
cb->num_refs = 0;
}
// Check if an operand needs a REX byte to be encoded
static bool rex_needed(x86opnd_t opnd)
{
if (opnd.type == OPND_NONE || opnd.type == OPND_IMM)
{
return false;
}
if (opnd.type == OPND_REG)
{
return (
opnd.as.reg.reg_no > 7 ||
(opnd.num_bits == 8 && opnd.as.reg.reg_no >= 4 && opnd.as.reg.reg_no <= 7)
);
}
if (opnd.type == OPND_MEM)
{
return (opnd.as.mem.base_reg_no > 7) || (opnd.as.mem.has_idx && opnd.as.mem.idx_reg_no > 7);
}
rb_bug("unreachable");
}
// Check if an SIB byte is needed to encode this operand
static bool sib_needed(x86opnd_t opnd)
{
if (opnd.type != OPND_MEM)
return false;
return (
opnd.as.mem.has_idx ||
opnd.as.mem.base_reg_no == RSP.as.reg.reg_no ||
opnd.as.mem.base_reg_no == R12.as.reg.reg_no
);
}
// Compute the size of the displacement field needed for a memory operand
static uint32_t disp_size(x86opnd_t opnd)
{
assert (opnd.type == OPND_MEM);
// If using RIP as the base, use disp32
if (opnd.as.mem.is_iprel)
{
return 32;
}
// Compute the required displacement size
if (opnd.as.mem.disp != 0)
{
uint32_t num_bits = sig_imm_size(opnd.as.mem.disp);
assert (num_bits <= 32 && "displacement does not fit in 32 bits");
// x86 can only encode 8-bit and 32-bit displacements
if (num_bits == 16)
num_bits = 32;;
return num_bits;
}
// If EBP or RBP or R13 is used as the base, displacement must be encoded
if (opnd.as.mem.base_reg_no == RBP.as.reg.reg_no ||
opnd.as.mem.base_reg_no == R13.as.reg.reg_no)
{
return 8;
}
return 0;
}
// Write the REX byte
static void cb_write_rex(
codeblock_t *cb,
bool w_flag,
uint8_t reg_no,
uint8_t idx_reg_no,
uint8_t rm_reg_no
)
{
// 0 1 0 0 w r x b
// w - 64-bit operand size flag
// r - MODRM.reg extension
// x - SIB.index extension
// b - MODRM.rm or SIB.base extension
uint8_t w = w_flag? 1:0;
uint8_t r = (reg_no & 8)? 1:0;
uint8_t x = (idx_reg_no & 8)? 1:0;
uint8_t b = (rm_reg_no & 8)? 1:0;
// Encode and write the REX byte
uint8_t rexByte = 0x40 + (w << 3) + (r << 2) + (x << 1) + (b);
cb_write_byte(cb, rexByte);
}
// Write an opcode byte with an embedded register operand
static void cb_write_opcode(codeblock_t *cb, uint8_t opcode, x86opnd_t reg)
{
// Write the reg field into the opcode byte
uint8_t op_byte = opcode | (reg.as.reg.reg_no & 7);
cb_write_byte(cb, op_byte);
}
// Encode an RM instruction
static void cb_write_rm(
codeblock_t *cb,
bool szPref,
bool rexW,
x86opnd_t r_opnd,
x86opnd_t rm_opnd,
uint8_t opExt,
uint32_t op_len,
...)
{
assert (op_len > 0 && op_len <= 3);
assert (r_opnd.type == OPND_REG || r_opnd.type == OPND_NONE);
// Flag to indicate the REX prefix is needed
bool need_rex = rexW || rex_needed(r_opnd) || rex_needed(rm_opnd);
// Flag to indicate SIB byte is needed
bool need_sib = sib_needed(r_opnd) || sib_needed(rm_opnd);
// Add the operand-size prefix, if needed
if (szPref == true)
cb_write_byte(cb, 0x66);
// Add the REX prefix, if needed
if (need_rex)
{
// 0 1 0 0 w r x b
// w - 64-bit operand size flag
// r - MODRM.reg extension
// x - SIB.index extension
// b - MODRM.rm or SIB.base extension
uint8_t w = rexW? 1:0;
uint8_t r;
if (r_opnd.type != OPND_NONE)
r = (r_opnd.as.reg.reg_no & 8)? 1:0;
else
r = 0;
uint8_t x;
if (need_sib && rm_opnd.as.mem.has_idx)
x = (rm_opnd.as.mem.idx_reg_no & 8)? 1:0;
else
x = 0;
uint8_t b;
if (rm_opnd.type == OPND_REG)
b = (rm_opnd.as.reg.reg_no & 8)? 1:0;
else if (rm_opnd.type == OPND_MEM)
b = (rm_opnd.as.mem.base_reg_no & 8)? 1:0;
else
b = 0;
// Encode and write the REX byte
uint8_t rex_byte = 0x40 + (w << 3) + (r << 2) + (x << 1) + (b);
cb_write_byte(cb, rex_byte);
}
// Write the opcode bytes to the code block
va_list va;
va_start(va, op_len);
for (uint32_t i = 0; i < op_len; ++i)
{
uint8_t byte = va_arg(va, int);
cb_write_byte(cb, byte);
}
va_end(va);
// MODRM.mod (2 bits)
// MODRM.reg (3 bits)
// MODRM.rm (3 bits)
assert (
!(opExt != 0xFF && r_opnd.type != OPND_NONE) &&
"opcode extension and register operand present"
);
// Encode the mod field
uint8_t mod;
if (rm_opnd.type == OPND_REG)
{
mod = 3;
}
else
{
uint32_t dsize = disp_size(rm_opnd);
if (dsize == 0 || rm_opnd.as.mem.is_iprel)
mod = 0;
else if (dsize == 8)
mod = 1;
else if (dsize == 32)
mod = 2;
else
rb_bug("unreachable");
}
// Encode the reg field
uint8_t reg;
if (opExt != 0xFF)
reg = opExt;
else if (r_opnd.type == OPND_REG)
reg = r_opnd.as.reg.reg_no & 7;
else
reg = 0;
// Encode the rm field
uint8_t rm;
if (rm_opnd.type == OPND_REG)
{
rm = rm_opnd.as.reg.reg_no & 7;
}
else
{
if (need_sib)
rm = 4;
else
rm = rm_opnd.as.mem.base_reg_no & 7;
}
// Encode and write the ModR/M byte
uint8_t rm_byte = (mod << 6) + (reg << 3) + (rm);
cb_write_byte(cb, rm_byte);
// Add the SIB byte, if needed
if (need_sib)
{
// SIB.scale (2 bits)
// SIB.index (3 bits)
// SIB.base (3 bits)
assert (rm_opnd.type == OPND_MEM);
// Encode the scale value
uint8_t scale = rm_opnd.as.mem.scale_exp;
// Encode the index value
uint8_t index;
if (!rm_opnd.as.mem.has_idx)
index = 4;
else
index = rm_opnd.as.mem.idx_reg_no & 7;
// Encode the base register
uint8_t base = rm_opnd.as.mem.base_reg_no & 7;
// Encode and write the SIB byte
uint8_t sib_byte = (scale << 6) + (index << 3) + (base);
cb_write_byte(cb, sib_byte);
}
// Add the displacement
if (rm_opnd.type == OPND_MEM)
{
uint32_t dsize = disp_size(rm_opnd);
if (dsize > 0)
cb_write_int(cb, rm_opnd.as.mem.disp, dsize);
}
}
// Encode a mul-like single-operand RM instruction
static void write_rm_unary(
codeblock_t *cb,
const char *mnem,
uint8_t opMemReg8,
uint8_t opMemRegPref,
uint8_t opExt,
x86opnd_t opnd)
{
// Write a disassembly string
//cb.writeASM(mnem, opnd);
// Check the size of opnd0
uint32_t opndSize;
if (opnd.type == OPND_REG || opnd.type == OPND_MEM)
opndSize = opnd.num_bits;
else
rb_bug("yjit: invalid operand");
assert (opndSize == 8 || opndSize == 16 || opndSize == 32 || opndSize == 64);
bool szPref = opndSize == 16;
bool rexW = opndSize == 64;
if (opndSize == 8)
cb_write_rm(cb, false, false, NO_OPND, opnd, opExt, 1, opMemReg8);
else
cb_write_rm(cb, szPref, rexW, NO_OPND, opnd, opExt, 1, opMemRegPref);
}
// Encode an add-like RM instruction with multiple possible encodings
static void cb_write_rm_multi(
codeblock_t *cb,
const char *mnem,
uint8_t opMemReg8,
uint8_t opMemRegPref,
uint8_t opRegMem8,
uint8_t opRegMemPref,
uint8_t opMemImm8,
uint8_t opMemImmSml,
uint8_t opMemImmLrg,
uint8_t opExtImm,
x86opnd_t opnd0,
x86opnd_t opnd1)
{
assert (opnd0.type == OPND_REG || opnd0.type == OPND_MEM);
/*
// Write disassembly string
if (!opnd1.isNone)
cb.writeASM(mnem, opnd0, opnd1);
else
cb.writeASM(mnem, opnd0);
*/
// Check the size of opnd0
uint32_t opndSize = opnd0.num_bits;
// Check the size of opnd1
if (opnd1.type == OPND_REG || opnd1.type == OPND_MEM)
{
assert (opnd1.num_bits == opndSize && "operand size mismatch");
}
else if (opnd1.type == OPND_IMM)
{
assert (opnd1.num_bits <= opndSize);
}
assert (opndSize == 8 || opndSize == 16 || opndSize == 32 || opndSize == 64);
bool szPref = opndSize == 16;
bool rexW = opndSize == 64;
// R/M + Reg
if ((opnd0.type == OPND_MEM && opnd1.type == OPND_REG) ||
(opnd0.type == OPND_REG && opnd1.type == OPND_REG))
{
// R/M is opnd0
if (opndSize == 8)
cb_write_rm(cb, false, false, opnd1, opnd0, 0xFF, 1, opMemReg8);
else
cb_write_rm(cb, szPref, rexW, opnd1, opnd0, 0xFF, 1, opMemRegPref);
}
// Reg + R/M
else if (opnd0.type == OPND_REG && opnd1.type == OPND_MEM)
{
// R/M is opnd1
if (opndSize == 8)
cb_write_rm(cb, false, false, opnd0, opnd1, 0xFF, 1, opRegMem8);
else
cb_write_rm(cb, szPref, rexW, opnd0, opnd1, 0xFF, 1, opRegMemPref);
}
// R/M + Imm
else if (opnd1.type == OPND_IMM)
{
// 8-bit immediate
if (opnd1.num_bits <= 8)
{
if (opndSize == 8)
cb_write_rm(cb, false, false, NO_OPND, opnd0, opExtImm, 1, opMemImm8);
else
cb_write_rm(cb, szPref, rexW, NO_OPND, opnd0, opExtImm, 1, opMemImmSml);
cb_write_int(cb, opnd1.as.imm, 8);
}
// 32-bit immediate
else if (opnd1.num_bits <= 32)
{
assert (opnd1.num_bits <= opndSize && "immediate too large for dst");
cb_write_rm(cb, szPref, rexW, NO_OPND, opnd0, opExtImm, 1, opMemImmLrg);
cb_write_int(cb, opnd1.as.imm, (opndSize > 32)? 32:opndSize);
}
// Immediate too large
else
{
assert (false && "immediate value too large");
}
}
// Invalid operands
else
{
assert (false && "invalid operand combination");
}
}
// Encode a single-operand shift instruction
static void cb_write_shift(
codeblock_t *cb,
const char *mnem,
uint8_t opMemOnePref,
uint8_t opMemClPref,
uint8_t opMemImmPref,
uint8_t opExt,
x86opnd_t opnd0,
x86opnd_t opnd1)
{
// Write a disassembly string
//cb.writeASM(mnem, opnd0, opnd1);
// Check the size of opnd0
uint32_t opndSize;
if (opnd0.type == OPND_REG || opnd0.type == OPND_MEM)
opndSize = opnd0.num_bits;
else
rb_bug("yjit: shift: invalid first operand");
assert (opndSize == 16 || opndSize == 32 || opndSize == 64);
bool szPref = opndSize == 16;
bool rexW = opndSize == 64;
if (opnd1.type == OPND_IMM)
{
if (opnd1.as.imm == 1)
{
cb_write_rm(cb, szPref, rexW, NO_OPND, opnd0, opExt, 1, opMemOnePref);
}
else
{
assert (opnd1.num_bits <= 8);
cb_write_rm(cb, szPref, rexW, NO_OPND, opnd0, opExt, 1, opMemImmPref);
cb_write_byte(cb, (uint8_t)opnd1.as.imm);
}
}
/*
else if (opnd1.isReg && opnd1.reg == CL)
{
cb.writeRMInstr!('l', opExt, opMemClPref)(szPref, rexW, opnd0, X86Opnd.NONE);
}
*/
else
{
assert (false);
}
}
// Encode a relative jump to a label (direct or conditional)
// Note: this always encodes a 32-bit offset
static void cb_write_jcc(codeblock_t *cb, const char *mnem, uint8_t op0, uint8_t op1, uint32_t label_idx)
{
//cb.writeASM(mnem, label);
// Write the opcode
if (op0 != 0xFF)
cb_write_byte(cb, op0);
cb_write_byte(cb, op1);
// Add a reference to the label
cb_label_ref(cb, label_idx);
// Relative 32-bit offset to be patched
cb_write_int(cb, 0, 32);
}
// Encode a relative jump to a pointer at a 32-bit offset (direct or conditional)
static void cb_write_jcc_ptr(codeblock_t *cb, const char *mnem, uint8_t op0, uint8_t op1, uint8_t *dst_ptr)
{
//cb.writeASM(mnem, label);
// Write the opcode
if (op0 != 0xFF)
cb_write_byte(cb, op0);
cb_write_byte(cb, op1);
// Pointer to the end of this jump instruction
uint8_t *end_ptr = cb_get_ptr(cb, cb->write_pos + 4);
// Compute the jump offset
int64_t rel64 = (int64_t)(dst_ptr - end_ptr);
if (rel64 >= INT32_MIN && rel64 <= INT32_MAX) {
// Write the relative 32-bit jump offset
cb_write_int(cb, (int32_t)rel64, 32);
}
else {
// Offset doesn't fit in 4 bytes. Report error.
cb->dropped_bytes = true;
}
}
// Encode a conditional move instruction
static void cb_write_cmov(codeblock_t *cb, const char *mnem, uint8_t opcode1, x86opnd_t dst, x86opnd_t src)
{
//cb.writeASM(mnem, dst, src);
assert (dst.type == OPND_REG);
assert (src.type == OPND_REG || src.type == OPND_MEM);
assert (dst.num_bits >= 16 && "invalid dst reg size in cmov");
bool szPref = dst.num_bits == 16;
bool rexW = dst.num_bits == 64;
cb_write_rm(cb, szPref, rexW, dst, src, 0xFF, 2, 0x0F, opcode1);
}
// add - Integer addition
void add(codeblock_t *cb, x86opnd_t opnd0, x86opnd_t opnd1)
{
cb_write_rm_multi(
cb,
"add",
0x00, // opMemReg8
0x01, // opMemRegPref
0x02, // opRegMem8
0x03, // opRegMemPref
0x80, // opMemImm8
0x83, // opMemImmSml
0x81, // opMemImmLrg
0x00, // opExtImm
opnd0,
opnd1
);
}
/// and - Bitwise AND
void and(codeblock_t *cb, x86opnd_t opnd0, x86opnd_t opnd1)
{
cb_write_rm_multi(
cb,
"and",
0x20, // opMemReg8
0x21, // opMemRegPref
0x22, // opRegMem8
0x23, // opRegMemPref
0x80, // opMemImm8
0x83, // opMemImmSml
0x81, // opMemImmLrg
0x04, // opExtImm
opnd0,
opnd1
);
}
// call - Call to a pointer with a 32-bit displacement offset
static void call_rel32(codeblock_t *cb, int32_t rel32)
{
//cb.writeASM("call", rel32);
// Write the opcode
cb_write_byte(cb, 0xE8);
// Write the relative 32-bit jump offset
cb_write_int(cb, (int32_t)rel32, 32);
}
// call - Call a pointer, encode with a 32-bit offset if possible
void call_ptr(codeblock_t *cb, x86opnd_t scratch_reg, uint8_t *dst_ptr)
{
assert (scratch_reg.type == OPND_REG);
// Pointer to the end of this call instruction
uint8_t *end_ptr = cb_get_ptr(cb, cb->write_pos + 5);
// Compute the jump offset
int64_t rel64 = (int64_t)(dst_ptr - end_ptr);
// If the offset fits in 32-bit
if (rel64 >= INT32_MIN && rel64 <= INT32_MAX) {
call_rel32(cb, (int32_t)rel64);
return;
}
// Move the pointer into the scratch register and call
mov(cb, scratch_reg, const_ptr_opnd(dst_ptr));
call(cb, scratch_reg);
}