forked from titzer/wizard-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX86_64MacroAssembler.v3
1461 lines (1431 loc) · 51.9 KB
/
X86_64MacroAssembler.v3
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
// Copyright 2022 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
def G = X86_64MasmRegs.toGpr, X = X86_64MasmRegs.toXmmr;
def R: X86_64Regs;
def C: X86_64Conds;
def A(ma: MasmAddr) -> X86_64Addr {
return X86_64Addr.new(G(ma.base), null, 1, ma.offset);
}
def RT: X86_64Runtime;
class X86_64MasmLabel extends MasmLabel {
def label: X86_64Label;
new(create_pos: int, label) super(create_pos) { }
}
class X86_64MacroAssembler extends MacroAssembler {
def w: DataWriter;
def asm = X86_64Assemblers.create64(w);
var scratch: X86_64Gpr;
var jump_tables: Vector<(int, Array<X86_64Label>)>;
var offsets: V3Offsets;
var trap_stubs: X86_64SpcTrapsStub;
new(w, regConfig: RegConfig) super(Target.tagging, regConfig) {
scratch = G(regConfig.scratch);
}
def curCodeBytes() -> u64 {
return u64.!(w.end());
}
def setTargetAddress(addr: u64) {
if (jump_tables != null) {
for (i < jump_tables.length) {
var t = jump_tables[i], offset = t.0, labels = t.1;
for (j < labels.length) {
var l = labels[j], target = addr + u64.!(l.pos);
asm.w.at(offset + j * 8).put_b64(long.view(target));
}
}
jump_tables = null;
}
}
// Label operations
def newLabel(create_pos: int) -> X86_64MasmLabel {
return X86_64MasmLabel.new(create_pos, asm.newLabel());
}
def bindLabel(l: MasmLabel) {
if (Trace.compiler) Trace.OUT.put2(" bind label (+%d) -> @%d", l.create_pos, w.end()).ln();
var label = X86_64MasmLabel.!(l);
asm.bind(label.label);
label.offset = label.label.pos;
}
def bindLabelTo(l: MasmLabel, offset: int) {
if (Trace.compiler) Trace.OUT.put2(" bind label (+%d) -> @%d", l.create_pos, offset).ln();
var label = X86_64MasmLabel.!(l);
label.offset = offset;
label.label.pos = offset;
}
def recordCurSourceLoc() {
recordSourceLoc(asm.pos());
}
def recordRetSourceLoc() {
// To distinguish an entry for the return address of a call from an entry for a Wasm instruction
// following the call, return addresses have a {-1} adjustment.
recordSourceLoc(asm.pos() - 1);
}
def getScratchReg(kind: ValueKind) -> Reg {
match (kind) {
I32, I64, REF => return regConfig.scratch;
F32, F64, V128 => return X86_64MasmRegs.XMM15; // TODO: make configurable
}
}
def getV3ParamReg(kind: ValueKind, index: int) -> Reg {
match (kind) {
I32, I64, REF => return X86_64MasmRegs.PARAM_GPRS[index];
_ => unimplemented();
}
return Reg(0);
}
def getV3ReturnReg(kind: ValueKind, index: int) -> Reg {
match (kind) {
I32, I64, REF => return X86_64MasmRegs.RET_GPRS[index];
_ => unimplemented();
}
return Reg(0);
}
def emit_intentional_crash() {
recordCurSourceLoc();
asm.invalid();
}
def emit_read_v3_array_r_r(kind: ValueKind, dst: Reg, array: Reg, index: Reg) {
var a = G(array), i = G(index);
match (kind) {
I32 => asm.movd_r_m(G(dst), X86_64Addr.new(a, i, 4, getOffsets().Array_contents));
F32 => asm.movss_s_m(X(dst), X86_64Addr.new(a, i, 4, getOffsets().Array_contents));
I64, REF => asm.movq_r_m(G(dst), X86_64Addr.new(a, i, 8, getOffsets().Array_contents));
F64 => asm.movsd_s_m(X(dst), X86_64Addr.new(a, i, 8, getOffsets().Array_contents));
V128 => asm.movdqu_s_m(X(dst), X86_64Addr.new(a, i, 16, getOffsets().Array_contents)); // TODO: can't scale by 16
}
}
def emit_bounds_check_v3_array(array: Reg, index: Reg, oob_label: MasmLabel) {
asm.d.cmp_r_m(G(index), X86_64Addr.new(G(array), null, 1, getOffsets().Array_length));
asm.jc_rel_far(X86_64Conds.GE, X86_64MasmLabel.!(oob_label).label);
}
def emit_read_v3_mem_base(dst: Reg, memobj: Reg) {
asm.movq_r_m(G(dst), X86_64Addr.new(G(memobj), null, 1, getOffsets().X86_64Memory_start));
}
def emit_loadbsx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
var x = if (kind == ValueKind.I64, asm.q, asm.d).movbsx_r_m(G(dst), X86_64Addr.new(G(base), t.0, 1, t.1));
}
def emit_loadbzx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
var x = if (kind == ValueKind.I64, asm.q, asm.d).movbzx_r_m(G(dst), X86_64Addr.new(G(base), t.0, 1, t.1));
}
def emit_loadwsx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
var x = if (kind == ValueKind.I64, asm.q, asm.d).movwsx_r_m(G(dst), X86_64Addr.new(G(base), t.0, 1, t.1));
}
def emit_loadwzx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
var x = if (kind == ValueKind.I64, asm.q, asm.d).movwzx_r_m(G(dst), X86_64Addr.new(G(base), t.0, 1, t.1));
}
def emit_loaddsx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var d = G(dst);
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
asm.q.movd_r_m(d, X86_64Addr.new(G(base), t.0, 1, t.1));
asm.q.shl_r_i(d, 32);
asm.q.sar_r_i(d, 32);
}
def emit_loaddzx_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
asm.q.movd_r_m(G(dst), X86_64Addr.new(G(base), t.0, 1, t.1));
}
def emit_load_r_r_r_i(kind: ValueKind, dst: Reg, base: Reg, index: Reg, offset: u32) {
var b = G(base), t = handle_large_offset(index, offset);
recordCurSourceLoc();
match (kind) {
I32 => asm.movd_r_m(G(dst), X86_64Addr.new(b, t.0, 1, t.1));
REF, I64 => asm.movq_r_m(G(dst), X86_64Addr.new(b, t.0, 1, t.1));
F32 => asm.movss_s_m(X(dst), X86_64Addr.new(b, t.0, 1, t.1));
F64 => asm.movsd_s_m(X(dst), X86_64Addr.new(b, t.0, 1, t.1));
V128 => asm.movdqu_s_m(X(dst), X86_64Addr.new(b, t.0, 1, t.1));
}
}
def emit_v128_load_lane_r_m<T>(dst: Reg, src: X86_64Addr, asm_mov_r_m: (X86_64Gpr, X86_64Addr) -> T) {
recordCurSourceLoc();
asm_mov_r_m(G(dst), src);
}
def emit_v128_store_lane_m_r<T>(dst: X86_64Addr, src: Reg, asm_mov_m_r: (X86_64Addr, X86_64Gpr) -> T) {
recordCurSourceLoc();
asm_mov_m_r(dst, G(src));
}
def decode_memarg_addr(base: Reg, index: Reg, offset: u32) -> X86_64Addr {
var t = handle_large_offset(index, offset);
return X86_64Addr.new(G(base), t.0, 1, t.1);
}
def emit_storeb_r_r_r_i(kind: ValueKind, val: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
asm.q.movb_m_r(X86_64Addr.new(G(base), t.0, 1, t.1), G(val));
}
def emit_storew_r_r_r_i(kind: ValueKind, val: Reg, base: Reg, index: Reg, offset: u32) {
var t = handle_large_offset(index, offset);
recordCurSourceLoc();
asm.q.movw_m_r(X86_64Addr.new(G(base), t.0, 1, t.1), G(val));
}
def emit_store_r_r_r_i(kind: ValueKind, val: Reg, base: Reg, index: Reg, offset: u32) {
var b = G(base), t = handle_large_offset(index, offset);
recordCurSourceLoc();
match (kind) {
I32 => asm.movd_m_r(X86_64Addr.new(b, t.0, 1, t.1), G(val));
REF, I64 => asm.movq_m_r(X86_64Addr.new(b, t.0, 1, t.1), G(val));
F32 => asm.movss_m_s(X86_64Addr.new(b, t.0, 1, t.1), X(val));
F64 => asm.movsd_m_s(X86_64Addr.new(b, t.0, 1, t.1), X(val));
V128 => asm.movdqu_m_s(X86_64Addr.new(b, t.0, 1, t.1), X(val));
}
}
private def handle_large_offset(index: Reg, offset: u32) -> (X86_64Gpr, int) {
var ioffset = int.view(offset), r = G(index);
if (ioffset != offset) { // too large for signed encoding, add explicitly
asm.movd_r_i(scratch, ioffset);
if (r != null) asm.q.add_r_r(scratch, r);
return (scratch, 0);
}
return (r, int.view(offset));
}
def emit_mov_r_r(kind: ValueKind, reg: Reg, reg2: Reg) {
var rd = G(reg);
if (rd != null) {
var rs = G(reg2);
if (rs != null) asm.movq_r_r(rd, G(reg2));
else asm.movq_r_s(rd, X(reg2));
} else {
var xd = X(reg), xs = X(reg2);
if (xs != null) {
if (kind == ValueKind.V128) asm.movaps_s_s(xd, xs);
else asm.movsd_s_s(xd, xs);
} else {
asm.movq_s_r(xd, G(reg2));
}
}
}
def emit_mov_r_m(kind: ValueKind, reg: Reg, ma: MasmAddr) {
var addr = A(ma);
match (kind) {
I32 => asm.movd_r_m(G(reg), addr);
I64, REF => asm.movq_r_m(G(reg), addr);
F32 => asm.movss_s_m(X(reg), addr);
F64 => asm.movsd_s_m(X(reg), addr);
V128 => asm.movdqu_s_m(X(reg), addr);
}
}
def emit_mov_r_i(reg: Reg, val: int) {
asm.movd_r_i(G(reg), val);
}
def emit_mov_r_l32(reg: Reg, val: int) {
asm.movq_r_i(G(reg), val);
}
def emit_mov_r_f32(reg: Reg, val: u32) {
asm.movd_r_i(scratch, int.view(val));
asm.movd_s_r(X(reg), scratch);
}
def emit_mov_r_l(reg: Reg, val: long) {
asm.movq_r_l(G(reg), long.view(val));
}
def emit_mov_r_d64(reg: Reg, val: u64) {
var x = X(reg);
if (val == 0) return void(asm.xorpd_s_s(x, x));
asm.movq_r_l(scratch, long.view(val));
asm.movq_s_r(X(reg), scratch);
}
def emit_mov_r_q(reg: Reg, low: u64, high: u64) {
var x = X(reg);
asm.movq_r_l(scratch, long.view(low));
asm.pinsrq_s_r_i(x, scratch, 0);
asm.movq_r_l(scratch, long.view(high));
asm.pinsrq_s_r_i(x, scratch, 1);
}
def emit_mov_r_trap(reg: Reg, reason: TrapReason) {
var ptr = Pointer.atObject(Execute.trapObjects[reason.tag]);
asm.movq_r_l(G(reg), ptr - Pointer.NULL);
}
def emit_mov_r_abs32(reg: Reg, abs: Pointer) {
asm.movd_r_i(G(reg), int.view(u32.!(abs - Pointer.NULL)));
}
def emit_mov_m_r(kind: ValueKind, ma: MasmAddr, reg: Reg) {
var addr = A(ma);
match (kind) {
I32 => asm.movd_m_r(addr, G(reg));
I64, REF => asm.movq_m_r(addr, G(reg));
F32 => asm.movss_m_s(addr, X(reg));
F64 => asm.movsd_m_s(addr, X(reg));
V128 => asm.movdqu_m_s(addr, X(reg));
}
}
def emit_mov_m_i(ma: MasmAddr, val: int) {
asm.movd_m_i(A(ma), val);
}
def emit_mov_m_l(ma: MasmAddr, val: long) {
var addr = A(ma);
if (val == int.view(val)) asm.movq_m_i(addr, int.view(val));
else { // XXX: use constant pool?
asm.movd_m_i(addr, int.view(val));
var p4 = X86_64Addr.new(addr.base, addr.index, addr.scale, addr.disp + 4);
asm.movd_m_i(p4, int.view(val >> 32));
}
}
def emit_mov_m_f(ma: MasmAddr, bits: u32) {
asm.movd_m_i(A(ma), int.view(bits));
}
def emit_mov_m_d(ma: MasmAddr, bits: u64) {
emit_mov_m_l(ma, long.view(bits));
}
def emit_mov_m_q(ma: MasmAddr, low: u64, high: u64) { // XXX: use constant pool?
emit_mov_m_l(ma, long.view(low));
emit_mov_m_l(MasmAddr(ma.base, ma.offset + 8), long.view(high));
}
def emit_mov_m_m(kind: ValueKind, dst: MasmAddr, src: MasmAddr) {
match (kind) {
I32, F32 => {
asm.movd_r_m(scratch, A(src));
asm.movd_m_r(A(dst), scratch);
}
I64, F64, REF => {
asm.movq_r_m(scratch, A(src));
asm.movq_m_r(A(dst), scratch);
}
V128 => {
var scratch = R.XMM15; // TODO
asm.movdqu_s_m(scratch, A(src));
asm.movdqu_m_s(A(dst), scratch);
}
}
}
def emit_addi_r_r(reg: Reg, reg2: Reg) {
asm.add_r_r(G(reg), G(reg2));
}
def emit_addi_r_i(reg: Reg, val: int) {
asm.add_r_i(G(reg), val);
}
def emit_subw_r_i(reg: Reg, val: int) {
asm.sub_r_i(G(reg), val);
}
def emit_subw_r_r(reg: Reg, reg2: Reg) {
asm.sub_r_r(G(reg), G(reg2));
}
def emit_addw_r_i(r1: Reg, val: int) {
asm.add_r_i(G(r1), val);
}
def emit_addw_r_r(r1: Reg, r2: Reg) {
asm.add_r_r(G(r1), G(r2));
}
def emit_shlw_r_i(reg: Reg, imm: u6) {
asm.shl_r_i(G(reg), imm);
}
def emit_shrw_r_i(reg: Reg, imm: u6) {
asm.shr_r_i(G(reg), imm);
}
def emit_movq_32s_r_m(dst: X86_64Gpr, addr: X86_64Addr) -> X86_64Assembler {
asm.movd_r_m(dst, addr);
asm.q.shl_r_i(dst, 32);
asm.q.sar_r_i(dst, 32);
return asm;
}
def emit_i32_div_s(r2: X86_64Gpr) { // note: (r1=EAX, r2) -> EAX, kills EDX
var r1 = R.RAX;
var div = X86_64Label.new(), done = X86_64Label.new();
asm.d.cmp_r_i(r2, -1);
asm.jc_rel_near(C.NZ, div);
asm.d.cmp_r_i(r1, 0x80000000);
asm.jc_rel_far(C.Z, X86_64MasmLabel.!(newTrapLabel(TrapReason.DIV_UNREPRESENTABLE)).label);
asm.d.neg_r(r1);
asm.jmp_rel_near(done);
asm.bind(div);
asm.d.cdq();
recordCurSourceLoc();
asm.d.idiv_r(r2);
asm.bind(done);
}
def emit_i32_div_u(r2: X86_64Gpr) { // note: (r1=EAX, r2) -> EAX, kills EDX
asm.d.movd_r_i(R.RDX, 0);
recordCurSourceLoc();
asm.d.div_r(r2);
}
def emit_i32_rem_s(r2: X86_64Gpr) { // note: (r1=EAX, r2) -> EDX, kills RAX
var r1 = R.RDX;
var div = X86_64Label.new(), done = X86_64Label.new();
asm.d.cmp_r_i(r2, -1);
asm.jc_rel_near(C.NZ, div);
asm.movd_r_i(r1, 0);
asm.jmp_rel_near(done);
asm.bind(div);
asm.d.cdq();
recordCurSourceLoc();
asm.d.idiv_r(r2);
asm.bind(done);
}
def emit_i32_rem_u(r2: X86_64Gpr) { // note: (r1=EAX, r2) -> EDX, kills RAX
asm.movd_r_i(R.RDX, 0);
recordCurSourceLoc();
asm.d.div_r(r2);
}
def emit_i64_div_s(r2: X86_64Gpr) { // note: (r1=RAX, r2) -> RAX, kills RDX
var div = X86_64Label.new(), done = X86_64Label.new();
var r1 = R.RAX;
asm.q.cmp_r_i(r2, -1);
asm.jc_rel_near(C.NZ, div);
asm.movq_r_i(scratch, 0x80); // XXX: use BTS
asm.q.shl_r_i(scratch, 56);
asm.q.cmp_r_r(r1, scratch);
asm.jc_rel_far(C.Z, X86_64MasmLabel.!(newTrapLabel(TrapReason.DIV_UNREPRESENTABLE)).label);
asm.q.neg_r(r1);
asm.jmp_rel_near(done);
asm.bind(div);
asm.q.cqo();
recordCurSourceLoc();
asm.q.idiv_r(r2);
asm.bind(done);
}
def emit_i64_div_u(r2: X86_64Gpr) { // note: (r1=RAX, r2) -> RAX, kills RDX
asm.movd_r_i(R.RDX, 0);
recordCurSourceLoc();
asm.div_r(r2);
}
def emit_i64_rem_s(r2: X86_64Gpr) { // note: (r1=RAX, r2) -> RDX, kills RAX
var r1 = R.RAX;
var dst = R.RDX;
var div = X86_64Label.new(), done = X86_64Label.new();
asm.cmp_r_i(r2, -1);
asm.jc_rel_near(C.NZ, div);
asm.movq_r_i(dst, 0);
asm.jmp_rel_near(done);
asm.bind(div);
asm.cqo();
recordCurSourceLoc();
asm.idiv_r(r2);
asm.bind(done);
}
def emit_i64_rem_u(r2: X86_64Gpr) { // note: (r1=RAX, r2) -> RDX, kills RAX
asm.movd_r_i(R.RDX, 0);
recordCurSourceLoc();
asm.div_r(r2);
}
def emit_binop_r_r(op: Opcode, reg: Reg, reg2: Reg) {
unimplemented();
}
def emit_binop_r_m(op: Opcode, reg: Reg, ma: MasmAddr) {
unimplemented();
}
def emit_binop_r_i(op: Opcode, reg: Reg, val: int) {
unimplemented();
}
def emit_cmpq_r_i(cond: X86_64Cond, r1: X86_64Gpr, val: int) {
asm.q.cmp_r_i(r1, val);
asm.set_r(cond, r1);
asm.q.movbzx_r_r(r1, r1);
}
def emit_cmpq_r_r_i(cond: X86_64Cond, r1: X86_64Gpr, r2: X86_64Gpr, val: int) {
asm.q.cmp_r_i(r2, val);
asm.set_r(cond, r1);
asm.d.movbzx_r_r(r1, r1);
}
def emit_cmpq_r_r_r(cond: X86_64Cond, r1: X86_64Gpr, r2: X86_64Gpr, r3: X86_64Gpr) {
asm.q.cmp_r_r(r2, r3);
asm.set_r(cond, r1);
asm.d.movbzx_r_r(r1, r1);
}
def emit_cmpq_r_r(cond: X86_64Cond, r1: X86_64Gpr, r2: X86_64Gpr) {
asm.q.cmp_r_r(r1, r2);
asm.set_r(cond, r1);
asm.q.movbzx_r_r(r1, r1);
}
def emit_cmpq_r_m(cond: X86_64Cond, r1: X86_64Gpr, addr: X86_64Addr) {
asm.q.cmp_r_m(r1, addr);
asm.set_r(cond, r1);
asm.q.movbzx_r_r(r1, r1);
}
def emit_pop_r(kind: ValueKind, reg: Reg) {
match (kind) {
I32 => asm.d.popq_r(G(reg));
F32 => { asm.d.popq_r(scratch); asm.movd_s_r(X(reg), scratch); }
I64, REF => asm.q.popq_r(G(reg));
F64 => { asm.q.popq_r(scratch); asm.movq_s_r(X(reg), scratch); }
V128 => System.error("X86_64MacroAssembler", "cannot pop v128 type"); // TODO
}
}
def emit_ret() {
asm.ret();
}
def emit_nop() {
asm.q.or_r_r(R.RAX, R.RAX);
}
def emit_i_trunc_f(op: Opcode, dst: X86_64Gpr, x1: X86_64Xmmr, xscratch: X86_64Xmmr) {
match (op) {
I32_TRUNC_F32_S => emit_i_trunc_f0(TRUNC_i32_f32_s, false, dst, x1, xscratch);
I32_TRUNC_F32_U => emit_i_trunc_f0(TRUNC_i32_f32_u, false, dst, x1, xscratch);
I32_TRUNC_F64_S => emit_i_trunc_f0(TRUNC_i32_f64_s, false, dst, x1, xscratch);
I32_TRUNC_F64_U => emit_i_trunc_f0(TRUNC_i32_f64_u, false, dst, x1, xscratch);
I64_TRUNC_F32_S => emit_i_trunc_f0(TRUNC_i64_f32_s, false, dst, x1, xscratch);
I64_TRUNC_F32_U => emit_i_trunc_f0(TRUNC_i64_f32_u, false, dst, x1, xscratch);
I64_TRUNC_F64_S => emit_i_trunc_f0(TRUNC_i64_f64_s, false, dst, x1, xscratch);
I64_TRUNC_F64_U => emit_i_trunc_f0(TRUNC_i64_f64_u, false, dst, x1, xscratch);
I32_TRUNC_SAT_F32_S => emit_i_trunc_f0(TRUNC_i32_f32_s, true, dst, x1, xscratch);
I32_TRUNC_SAT_F32_U => emit_i_trunc_f0(TRUNC_i32_f32_u, true, dst, x1, xscratch);
I32_TRUNC_SAT_F64_S => emit_i_trunc_f0(TRUNC_i32_f64_s, true, dst, x1, xscratch);
I32_TRUNC_SAT_F64_U => emit_i_trunc_f0(TRUNC_i32_f64_u, true, dst, x1, xscratch);
I64_TRUNC_SAT_F32_S => emit_i64_trunc_sat_f32_s(dst, x1, xscratch); // custom
I64_TRUNC_SAT_F32_U => emit_i_trunc_f0(TRUNC_i64_f32_u, true, dst, x1, xscratch);
I64_TRUNC_SAT_F64_S => emit_i64_trunc_sat_f64_s(dst, x1, xscratch); // custom
I64_TRUNC_SAT_F64_U => emit_i_trunc_f0(TRUNC_i64_f64_u, true, dst, x1, xscratch);
_ => unimplemented();
}
}
private def emit_i64_trunc_sat_f32_s(dst: X86_64Gpr, x1: X86_64Xmmr, xscratch: X86_64Xmmr) {
asm.movd_r_i(dst, int.view(Floats.f_1p63));
asm.movd_s_r(xscratch, dst);
asm.ucomiss_s_s(x1, xscratch);
var is_nan = X86_64Label.new(), ovf_pos = X86_64Label.new(), done = X86_64Label.new();
asm.jc_rel_near(C.P, is_nan);
asm.jc_rel_near(C.NC, ovf_pos);
asm.roundss_s_s(x1, x1, X86_64Rounding.TO_ZERO);
asm.q.cvtss2si_r_s(dst, x1);
asm.jmp_rel_near(done);
asm.bind(is_nan);
asm.movd_r_i(dst, 0);
asm.jmp_rel_near(done);
asm.bind(ovf_pos);
asm.movq_r_i(dst, 0xFFFFFFFE); // TODO: tricky constant
asm.q.ror_r_i(dst, 1); // result = 0x7FFFFFFF_FFFFFFFF
asm.bind(done);
}
private def emit_i64_trunc_sat_f64_s(dst: X86_64Gpr, x1: X86_64Xmmr, xscratch: X86_64Xmmr) {
asm.movd_r_i(dst, int.view(Floats.d_1p63 >> 32));
asm.q.shl_r_i(dst, 32);
asm.movq_s_r(xscratch, dst);
asm.ucomisd_s_s(x1, xscratch);
var is_nan = X86_64Label.new(), ovf_pos = X86_64Label.new(), done = X86_64Label.new();
asm.jc_rel_near(C.P, is_nan);
asm.jc_rel_near(C.NC, ovf_pos);
asm.roundsd_s_s(x1, x1, X86_64Rounding.TO_ZERO);
asm.q.cvtsd2si_r_s(dst, x1);
asm.jmp_rel_near(done);
asm.bind(is_nan);
asm.movd_r_i(dst, 0);
asm.jmp_rel_near(done);
asm.bind(ovf_pos);
asm.movq_r_i(dst, 0xFFFFFFFE); // TODO: tricky constant
asm.q.ror_r_i(dst, 1); // result = 0x7FFFFFFF_FFFFFFFF
asm.bind(done);
}
private def emit_i_trunc_f0(config: FloatTrunc, saturate: bool, dst: X86_64Gpr, x1: X86_64Xmmr, xscratch: X86_64Xmmr) {
config.mov_s_i(asm, xscratch, config.maxv, scratch);
config.ucomi_s_s(asm, x1, xscratch);
var trap = if(!saturate, X86_64MasmLabel.!(newTrapLabel(TrapReason.FLOAT_UNREPRESENTABLE)).label);
var above = X86_64Label.new(), is_nan = X86_64Label.new(), below = X86_64Label.new();
var done = X86_64Label.new();
if (saturate) asm.jc_rel_near(C.P, is_nan);
else asm.jc_rel_far(C.P, trap);
if (saturate) asm.jc_rel_near(C.NC, above);
else asm.jc_rel_far(C.NC, trap);
var not_big = X86_64Label.new();
if (config.isI64 && !config.isSigned) {
// handle u64 convert of 1p63 < v <= 1p64
config.mov_s_i(asm, xscratch, if(config.isF64, Floats.d_1p63, Floats.f_1p63), scratch);
config.ucomi_s_s(asm, x1, xscratch);
asm.jc_rel_near(C.C, not_big);
config.sub_s_s(asm, x1, xscratch);
config.round_s_s(asm, x1, x1, X86_64Rounding.TO_ZERO);
config.cvt2si_r_s(asm.q, dst, x1);
asm.movd_r_i(scratch, 1);
asm.ror_r_i(scratch, 1);
asm.q.add_r_r(dst, scratch);
asm.jmp_rel_near(done);
}
asm.bind(not_big);
if (!saturate || config.isI64 || !config.isSigned) {
config.mov_s_i(asm, xscratch, config.minv, scratch);
config.ucomi_s_s(asm, x1, xscratch);
if (saturate) asm.jc_rel_near(C.NA, below); // v <= min
else asm.jc_rel_far(C.NA, trap); // v <= min
}
config.round_s_s(asm, x1, x1, X86_64Rounding.TO_ZERO);
if (!config.isI64 && config.isSigned) {
config.cvt2si_r_s(asm.d, dst, x1);
} else {
config.cvt2si_r_s(asm.q, dst, x1);
}
if (saturate) {
asm.jmp_rel_near(done);
asm.bind(above);
config.mov_r_i(asm, dst, config.ceilv);
asm.jmp_rel_near(done);
asm.bind(is_nan);
asm.bind(below);
asm.movd_r_i(dst, 0);
}
asm.bind(done);
}
def emit_f32_convert_i64_u(x1: X86_64Xmmr, r1: X86_64Gpr, xscratch: X86_64Xmmr, scratch: X86_64Gpr) {
asm.q.cvtsi2ss_s_r(x1, r1);
asm.q.cmp_r_i(r1, 0);
var done = X86_64Label.new();
asm.jc_rel_near(C.NS, done);
// input < 0, compute 2.0d * cvt((x >> 1) | (x&1))
asm.movq_r_r(scratch, r1);
asm.q.and_r_i(scratch, 1);
asm.q.shr_r_i(r1, 1);
asm.q.or_r_r(r1, scratch);
asm.q.cvtsi2ss_s_r(x1, r1);
asm.movd_r_i(scratch, int.view(Floats.f_1p1)); // XXX: const could be in memory
asm.movd_s_r(xscratch, scratch);
asm.mulss_s_s(x1, xscratch);
// done
asm.bind(done);
}
def emit_f64_convert_i64_u(x1: X86_64Xmmr, r1: X86_64Gpr, xscratch: X86_64Xmmr, scratch: X86_64Gpr) {
asm.q.cvtsi2sd_s_r(x1, r1);
asm.q.cmp_r_i(r1, 0);
var done = X86_64Label.new();
asm.jc_rel_near(C.NS, done);
// input < 0, compute 2.0d * cvt((x >> 1) | (x&1))
asm.movq_r_r(scratch, r1);
asm.q.and_r_i(scratch, 1);
asm.q.shr_r_i(r1, 1);
asm.q.or_r_r(r1, scratch);
asm.q.cvtsi2sd_s_r(x1, r1);
asm.movd_r_i(scratch, int.view(Floats.d_1p1 >> 32));
asm.q.shl_r_i(scratch, 32);
asm.movq_s_r(xscratch, scratch);
asm.mulsd_s_s(x1, xscratch);
// done
asm.bind(done);
}
def emit_f32_copysign(x1: X86_64Xmmr, x2: X86_64Xmmr, scratch1: X86_64Gpr, scratch2: X86_64Gpr) {
asm.movd_r_s(scratch2, x2);
asm.d.and_r_i(scratch2, 0x80000000);
asm.movd_r_s(scratch1, x1);
asm.d.btr_r_i(scratch1, 31);
asm.d.or_r_r(scratch1, scratch2);
asm.movd_s_r(x1, scratch1);
}
def emit_f64_copysign(x1: X86_64Xmmr, x2: X86_64Xmmr, scratch1: X86_64Gpr, scratch2: X86_64Gpr) {
asm.movq_r_s(scratch2, x2);
asm.q.shr_r_i(scratch2, 63); // XXX: use shl, rcl, rcr?
asm.q.shl_r_i(scratch2, 63);
asm.movq_r_s(scratch1, x1);
asm.q.btr_r_i(scratch1, 63);
asm.q.or_r_r(scratch1, scratch2);
asm.movq_s_r(x1, scratch1);
}
def emit_br(label: MasmLabel) {
asm.jmp_rel_far(X86_64MasmLabel.!(label).label);
}
def emit_br_r(reg: Reg, cond: MasmBrCond, label: MasmLabel) {
match (cond) {
IS_WASM_FUNC, IS_NOT_WASM_FUNC => {
asm.d.cmp_m_i(G(reg).plus(0), offsets.WasmFunction_typeId);
}
_ => {
(if(cond.i32, asm.d, asm.q)).cmp_r_i(G(reg), 0);
}
}
var cc = if(cond.zero, X86_64Conds.Z, X86_64Conds.NZ);
asm.jc_rel_far(cc, X86_64MasmLabel.!(label).label);
}
def emit_br_m(addr: MasmAddr, cond: MasmBrCond, label: MasmLabel) {
(if(cond.i32, asm.d, asm.q)).cmp_m_i(A(addr), 0);
var cond = if(cond.zero, X86_64Conds.Z, X86_64Conds.NZ);
asm.jc_rel_far(cond, X86_64MasmLabel.!(label).label);
}
def emit_breq_r_i(r: Reg, val: int, label: MasmLabel) {
asm.d.cmp_r_i(G(r), val);
asm.jc_rel_far(X86_64Conds.Z, X86_64MasmLabel.!(label).label);
}
def emit_breq_r_l(r: Reg, val: int, label: MasmLabel) {
asm.q.cmp_r_i(G(r), val);
asm.jc_rel_far(X86_64Conds.Z, X86_64MasmLabel.!(label).label);
}
def emit_brne_r_i(r: Reg, val: int, label: MasmLabel) {
asm.d.cmp_r_i(G(r), val);
asm.jc_rel_far(X86_64Conds.NZ, X86_64MasmLabel.!(label).label);
}
def emit_br_table_r(reg: Reg, labels: Array<MasmLabel>) {
// XXX: simplify the label patching logic by improving X86_64Assembler
var r1 = G(reg);
asm.d.cmp_r_i(r1, labels.length);
asm.jc_rel_far(C.NC, X86_64MasmLabel.!(labels[labels.length - 1]).label);
var patcher = X86_64MasmJumpTablePatcher.new();
asm.q.patcher = patcher;
asm.q.lea(scratch, X86_64Addr.new(null, null, 1, REL_MARKER));
asm.q.patcher = null;
asm.ijmp_m(X86_64Addr.new(scratch, r1, 8, 0));
w.align(8);
var jtpos = w.atEnd().pos;
if (jump_tables == null) jump_tables = Vector.new();
jump_tables.put(jtpos, Arrays.map(labels, getLabel));
w.skipN(labels.length * 8);
w.at(patcher.pos).put_b32(jtpos - (patcher.pos + patcher.delta));
w.atEnd();
}
def emit_call_abs(abs: Pointer) {
// XXX: direct (relative) call if start is known and displacement is 32-bit.
asm.movq_r_l(scratch, abs - Pointer.NULL);
asm.icall_r(scratch);
}
def emit_call_r(reg: Reg) {
asm.icall_r(G(reg));
recordRetSourceLoc();
}
def emit_jump_r(reg: Reg) {
asm.ijmp_r(G(reg));
}
def emit_increment_CountProbe(tmp: Reg, probe: CountProbe, increment: u64) {
var r1 = G(tmp);
var refOffset = asm.movq_r_p(r1, Pointer.atObject(probe) - Pointer.NULL);
addEmbeddedRefOffset(refOffset);
var addr = r1.plus(getOffsets().CountProbe_count);
if (increment == 1) {
asm.inc_m(addr);
} else if (u31.?(increment)) {
asm.add_m_i(addr, u31.!(increment));
} else {
var g = G(tmp);
asm.movq_r_l(g, long.view(increment));
asm.add_m_r(addr, g);
}
}
def emit_call_OperandProbe_i_v_fire(probe: OperandProbe_i_v, value_reg: Reg) {
var codePtr = CiRuntime.unpackClosure<OperandProbe_i_v, u32, void>(probe.fire_i).0;
var refOffset = asm.movq_r_p(Target.V3_PARAM_GPRS[0], Pointer.atObject(probe) - Pointer.NULL);
addEmbeddedRefOffset(refOffset);
asm.movq_r_l(scratch, codePtr - Pointer.NULL); // XXX: make direct call to runtime if within 2GB
asm.icall_r(scratch);
recordRetSourceLoc();
}
def emit_call_HostCallStub() {
var ic = X86_64PreGenStubs.getInterpreterCode();
asm.movq_r_l(scratch, (ic.start + ic.header.hostCallStubOffset) - Pointer.NULL);
asm.icall_r(scratch);
recordRetSourceLoc();
}
def emit_jump_HostCallStub() {
var ic = X86_64PreGenStubs.getInterpreterCode();
asm.movq_r_l(scratch, (ic.start + ic.header.hostCallStubOffset) - Pointer.NULL);
asm.ijmp_r(scratch);
}
def emit_call_runtime_callHost(func_arg: Reg) {
emit_call_runtime(RT.runtime_callHost);
}
def emit_call_runtime_TRAP() {
emit_call_runtime(RT.runtime_TRAP);
}
def emit_jump_to_trap_at(reason: TrapReason) {
var ip = trap_stubs.getIpForReason(reason);
asm.movq_r_l(scratch, ip - Pointer.NULL);
asm.ijmp_r(scratch);
}
def emit_call_runtime_op(op: Opcode) {
match (op) {
THROW => emit_call_runtime(RT.runtime_THROW);
GLOBAL_GET => emit_call_runtime(RT.runtime_GLOBAL_GET);
GLOBAL_SET => emit_call_runtime(RT.runtime_GLOBAL_SET);
TABLE_GET => emit_call_runtime(RT.runtime_TABLE_GET);
TABLE_SET => emit_call_runtime(RT.runtime_TABLE_SET);
MEMORY_GROW => emit_call_runtime(RT.runtime_MEMORY_GROW);
MEMORY_INIT => emit_call_runtime(RT.runtime_MEMORY_INIT);
MEMORY_COPY => emit_call_runtime(RT.runtime_MEMORY_COPY);
MEMORY_FILL => emit_call_runtime(RT.runtime_MEMORY_FILL);
TABLE_INIT => emit_call_runtime(RT.runtime_TABLE_INIT);
TABLE_COPY => emit_call_runtime(RT.runtime_TABLE_COPY);
TABLE_GROW => emit_call_runtime(RT.runtime_TABLE_GROW);
TABLE_FILL => emit_call_runtime(RT.runtime_TABLE_FILL);
_ => unimplemented();
}
}
def emit_call_runtime_Probe_instr() {
emit_call_runtime(RT.runtime_PROBE_instr);
}
private def emit_call_runtime<P, R>(closure: P -> R) {
var ptr = CiRuntime.unpackClosure<X86_64Interpreter, P, R>(closure).0;
// Do an absolute call into the runtime
asm.movd_r_i(scratch, int.view(u32.!(ptr - Pointer.NULL))); // XXX: make direct call to runtime if within 2GB
asm.icall_r(scratch);
recordRetSourceLoc();
}
private def emit_jump_runtime<P, R>(closure: P -> R) {
var ptr = CiRuntime.unpackClosure<X86_64Interpreter, P, R>(closure).0;
// Do an absolute call into the runtime
asm.movd_r_i(scratch, int.view(u32.!(ptr - Pointer.NULL))); // XXX: make direct jump to runtime if within 2GB
asm.ijmp_r(scratch);
}
def emit_value_copy(r_dst: X86_64Gpr, r_src: X86_64Gpr, r_count: X86_64Gpr, r_xmm0: X86_64Xmmr) {
var copy = X86_64Label.new();
asm.d.shl_r_i(r_count, valuerep.slot_size_log);
asm.bind(copy);
if (valuerep.value_size == 16) { // 16-byte (SIMD) values
asm.movdqu_s_m(r_xmm0, r_src.plusR(r_count, 1, - valuerep.value_size));
asm.movdqu_m_s(r_dst.plusR(r_count, 1, - valuerep.value_size), r_xmm0);
} else { // 8-byte values
asm.movq_r_m(scratch, r_src.plusR(r_count, 1, - Pointer.SIZE));
asm.movq_m_r(r_dst.plusR(r_count, 1, - Pointer.SIZE), scratch);
}
if (valuerep.tagged) {
asm.movb_r_m(scratch, r_src.plusR(r_count, 1, - valuerep.slot_size)); // copy tags
asm.movb_m_r(r_dst.plusR(r_count, 1, - valuerep.slot_size), scratch);
}
asm.d.sub_r_i(r_count, valuerep.slot_size);
asm.jc_rel_near(C.NZ, copy);
}
def emit_store_curstack_vsp(vsp: Reg) {
var offsets = getOffsets();
asm.movq_r_m(scratch, absPointer(offsets.X86_64Runtime_curStack));
asm.movq_m_r(scratch.plus(offsets.X86_64Stack_vsp), G(vsp));
}
def emit_load_curstack_vsp(vsp: Reg) {
var offsets = getOffsets();
asm.movq_r_m(scratch, absPointer(offsets.X86_64Runtime_curStack));
asm.movq_r_m(G(vsp), scratch.plus(offsets.X86_64Stack_vsp));
}
def emit_load_dispatch_table_reg(reg: Reg) {
var offsets = getOffsets();
asm.movq_r_m(G(reg), absPointer(offsets.Interpreter_dispatchTable));
}
def emit_i32_clz_r_r(r: X86_64Gpr, s: X86_64Gpr) {
asm.movd_r_i(scratch, -1);
asm.d.bsr_r_r(r, s);
asm.d.cmov_r(C.Z, r, scratch);
asm.movd_r_i(scratch, 31);
asm.d.sub_r_r(scratch, r);
asm.movd_r_r(r, scratch); // XXX: can save an instruction here?
}
def emit_i32_ctz_r_r(r: X86_64Gpr, s: X86_64Gpr) {
asm.d.bsf_r_r(r, s);
asm.movd_r_i(scratch, 32);
asm.d.cmov_r(C.Z, r, scratch);
}
def emit_i64_clz_r_r(r: X86_64Gpr, s: X86_64Gpr) {
asm.movq_r_i(scratch, -1);
asm.q.bsr_r_r(r, s);
asm.q.cmov_r(C.Z, r, scratch);
asm.movq_r_i(scratch, 63);
asm.q.sub_r_r(scratch, r);
asm.movq_r_r(r, scratch); // XXX: can save an instruction with second output reg
}
def emit_i64_ctz_r_r(r: X86_64Gpr, s: X86_64Gpr) {
asm.q.bsf_r_r(r, s);
asm.movq_r_i(scratch, 64);
asm.q.cmov_r(C.Z, r, scratch);
}
def emit_i64_extend_i32_s(r: X86_64Gpr) {
asm.q.shl_r_i(r, 32);
asm.q.sar_r_i(r, 32);
}
def emit_i64_extend_i32_u(r: X86_64Gpr) {
asm.movd_r_r(r, r);
}
// SSE assemblers and helpers
// Masks for simd instructions
def mask_i8x16_splat_0x0f: (u64, u64) = (0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F);
def mask_i8x16_popcnt: (u64, u64) = (0x0302020102010100, 0x0403030203020201);
def mask_v128_float_neg_constant: (u64, u64) = (0x8000000080000000, 0x8000000080000000);
def mask_v128_double_neg_constant: (u64, u64) = (0x8000000000000000, 0x8000000000000000);
def mask_v128_float_absolute_constant: (u64, u64) = (0x7FFFFFFF7FFFFFFF, 0x7FFFFFFF7FFFFFFF);
def mask_v128_double_absolute_constant: (u64, u64) = (0x7FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF);
def mask_f64x2_convert_low_i32x4_u_int: (u64, u64) = (0x4330000043300000, 0x4330000043300000);
def mask_double_2_power_52: (u64, u64) = (0x4330000000000000, 0x4330000000000000);
def mask_i8x16_splat_0x01: (u64, u64) = (0x0101010101010101, 0x0101010101010101);
def mask_i16x8_splat_0x0001: (u64, u64) = (0x0001000100010001, 0x0001000100010001);
def mask_i8x16_swizzle: (u64, u64) = (0x7070707070707070, 0x7070707070707070);
def mask_int32_max_as_double: (u64, u64) = (0x41dfffffffc00000, 0x41dfffffffc00000);
def mask_uint32_max_as_double: (u64, u64) = (0x41efffffffe00000, 0x41efffffffe00000);
// Load a mask into an Xmm register s
def load_v128_mask(dst: X86_64Xmmr, mask: (u64, u64), tmp: X86_64Gpr) {
// move 64 bit wide general purpose register into an xmm's lower half
asm.movq_r_l(tmp, long.view(mask.0));
asm.pinsrq_s_r_i(dst, tmp, 0);
asm.movq_r_l(tmp, long.view(mask.1));
asm.pinsrq_s_r_i(dst, tmp, 1);
}
def emit_v128_orps(dst: X86_64Xmmr, src: X86_64Xmmr) {
asm.orps_s_s(dst, src);
}
def emit_v128_xorps(dst: X86_64Xmmr, src: X86_64Xmmr) {
asm.xorps_s_s(dst, src);
}
def emit_v128_andps(dst: X86_64Xmmr, src: X86_64Xmmr) {
asm.andps_s_s(dst, src);
}
def emit_v128_neg<T>(dst: X86_64Xmmr, scratch: X86_64Xmmr, f: (X86_64Xmmr, X86_64Xmmr) -> T) {
asm.pxor_s_s(scratch, scratch);
f(scratch, dst);
// todo: optimize out this move
asm.movaps_s_s(dst, scratch);
}
def emit_i8x16_neg(dst: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_neg(dst, scratch, asm.psubb_s_s);
}
def emit_i16x8_neg(dst: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_neg(dst, scratch, asm.psubw_s_s);
}
def emit_i32x4_neg(dst: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_neg(dst, scratch, asm.psubd_s_s);
}
def emit_i64x2_neg(dst: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_neg(dst, scratch, asm.psubq_s_s);
}
def emit_v128_not(dst: X86_64Xmmr, scratch: X86_64Xmmr) {
asm.pcmpeqd_s_s(scratch, scratch);
asm.xorps_s_s(dst, scratch);
}
def emit_v128_bitselect(dst: X86_64Xmmr, src: X86_64Xmmr, mask: X86_64Xmmr, scratch: X86_64Xmmr) {
// AND(v1, c)
asm.andpd_s_s(dst, mask);
// NOT(c)
emit_v128_not(mask, scratch);
// AND(v2, NOT(c))
asm.andpd_s_s(src, mask);
// OR
asm.orps_s_s(dst, src);
}
def emit_v128_negps(dst: X86_64Xmmr, tmp: X86_64Gpr, mask: X86_64Xmmr) {
load_v128_mask(mask, mask_v128_float_neg_constant, tmp);
emit_v128_xorps(dst, mask);
}
def emit_v128_negpd(dst: X86_64Xmmr, tmp: X86_64Gpr, mask: X86_64Xmmr) {
load_v128_mask(mask, mask_v128_double_neg_constant, tmp);
emit_v128_xorps(dst, mask);
}
def emit_v128_absps(dst: X86_64Xmmr, tmp: X86_64Gpr, mask: X86_64Xmmr) {
load_v128_mask(mask, mask_v128_float_absolute_constant, tmp);
emit_v128_andps(dst, mask);
}
def emit_v128_abspd(dst: X86_64Xmmr, tmp: X86_64Gpr, mask: X86_64Xmmr) {
load_v128_mask(mask, mask_v128_double_absolute_constant, tmp);
emit_v128_andps(dst, mask);
}
def emit_v128_zero(s: X86_64Xmmr) {
emit_v128_xorps(s, s);
}
def emit_i64x2_mul(lhs: X86_64Xmmr, rhs: X86_64Xmmr, tmp1: X86_64Xmmr, tmp2: X86_64Xmmr) {
asm.movaps_s_s(tmp1, lhs);
asm.movaps_s_s(tmp2, rhs);
// 1. Multiply high dword of each qword of left with right.
asm.psrlq_i(tmp1, 32);
asm.pmuludq_s_s(tmp1, rhs);
// 2. Multiply high dword of each qword of right with left.
asm.psrlq_i(tmp2, 32);
asm.pmuludq_s_s(tmp2, lhs);
// 3. Add 1 and 2, then shift left by 32 (this is the high dword of result).
asm.paddq_s_s(tmp2, tmp1);
asm.psllq_i(tmp2, 32);
// 4. Multiply low dwords (this is the low dword of result).
asm.pmuludq_s_s(lhs, rhs);
// 5. Add 3 and 4.
asm.paddq_s_s(lhs, tmp2);
}
def emit_v128_ne<T>(dst: X86_64Xmmr, src: X86_64Xmmr, f: (X86_64Xmmr, X86_64Xmmr) -> T) {
f(dst, src);
f(src, src);
asm.pxor_s_s(dst, src);
}
def emit_i8x16_ne(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ne(dst, src, asm.pcmpeqb_s_s);
}
def emit_i16x8_ne(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ne(dst, src, asm.pcmpeqw_s_s);
}
def emit_i32x4_ne(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ne(dst, src, asm.pcmpeqd_s_s);
}
def emit_i64x2_ne(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ne(dst, src, asm.pcmpeqq_s_s);
}
def emit_v128_gt_u<T>(dst: X86_64Xmmr, src: X86_64Xmmr, scratch: X86_64Xmmr,
pmax: (X86_64Xmmr, X86_64Xmmr) -> T, pcmp: (X86_64Xmmr, X86_64Xmmr) -> T) {
pmax(dst, src);
pcmp(dst, src);
pcmp(scratch, scratch);
asm.xorps_s_s(dst, scratch);
}
def emit_i8x16_gt_u(dst: X86_64Xmmr, src: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_gt_u(dst, src, scratch, asm.pmaxub_s_s, asm.pcmpeqb_s_s);
}
def emit_i16x8_gt_u(dst: X86_64Xmmr, src: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_gt_u(dst, src, scratch, asm.pmaxuw_s_s, asm.pcmpeqw_s_s);
}
def emit_i32x4_gt_u(dst: X86_64Xmmr, src: X86_64Xmmr, scratch: X86_64Xmmr) {
emit_v128_gt_u(dst, src, scratch, asm.pmaxud_s_s, asm.pcmpeqd_s_s);
}
def emit_v128_ge<T>(dst: X86_64Xmmr, src: X86_64Xmmr,
pmin: (X86_64Xmmr, X86_64Xmmr) -> T, pcmp: (X86_64Xmmr, X86_64Xmmr) -> T) {
pmin(dst, src);
pcmp(dst, src);
}
def emit_i8x16_ge_s(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminsb_s_s, asm.pcmpeqb_s_s);
}
def emit_i16x8_ge_s(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminsw_s_s, asm.pcmpeqw_s_s);
}
def emit_i32x4_ge_s(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminsd_s_s, asm.pcmpeqd_s_s);
}
def emit_i8x16_ge_u(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminub_s_s, asm.pcmpeqb_s_s);
}
def emit_i16x8_ge_u(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminuw_s_s, asm.pcmpeqw_s_s);
}
def emit_i32x4_ge_u(dst: X86_64Xmmr, src: X86_64Xmmr) {
emit_v128_ge(dst, src, asm.pminud_s_s, asm.pcmpeqd_s_s);
}
def emit_i64x2_ge_s(dst: X86_64Xmmr, src: X86_64Xmmr, scratch: X86_64Xmmr) {
asm.pcmpgtq_s_s(src, dst);