-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
asm_intrinsifier_ia32.cc
2077 lines (1810 loc) · 72.6 KB
/
asm_intrinsifier_ia32.cc
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 (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// The intrinsic code below is executed before a method has built its frame.
// The return address is on the stack and the arguments below it.
// Registers EDX (arguments descriptor) and ECX (function) must be preserved.
// Each intrinsification method returns true if the corresponding
// Dart method was intrinsified.
#include "vm/globals.h" // Needed here to get TARGET_ARCH_IA32.
#if defined(TARGET_ARCH_IA32)
#define SHOULD_NOT_INCLUDE_RUNTIME
#include "vm/class_id.h"
#include "vm/compiler/asm_intrinsifier.h"
#include "vm/compiler/assembler/assembler.h"
namespace dart {
namespace compiler {
// When entering intrinsics code:
// ECX: IC Data
// EDX: Arguments descriptor
// TOS: Return address
// The ECX, EDX registers can be destroyed only if there is no slow-path, i.e.
// if the intrinsified method always executes a return.
// The EBP register should not be modified, because it is used by the profiler.
// The THR register (see constants_ia32.h) must be preserved.
#define __ assembler->
intptr_t AsmIntrinsifier::ParameterSlotFromSp() {
return 0;
}
void AsmIntrinsifier::IntrinsicCallPrologue(Assembler* assembler) {
COMPILE_ASSERT(CALLEE_SAVED_TEMP != ARGS_DESC_REG);
assembler->Comment("IntrinsicCallPrologue");
assembler->movl(CALLEE_SAVED_TEMP, ARGS_DESC_REG);
}
void AsmIntrinsifier::IntrinsicCallEpilogue(Assembler* assembler) {
assembler->Comment("IntrinsicCallEpilogue");
assembler->movl(ARGS_DESC_REG, CALLEE_SAVED_TEMP);
}
// Allocate a GrowableObjectArray:: using the backing array specified.
// On stack: type argument (+2), data (+1), return-address (+0).
void AsmIntrinsifier::GrowableArray_Allocate(Assembler* assembler,
Label* normal_ir_body) {
// This snippet of inlined code uses the following registers:
// EAX, EBX
// and the newly allocated object is returned in EAX.
const intptr_t kTypeArgumentsOffset = 2 * target::kWordSize;
const intptr_t kArrayOffset = 1 * target::kWordSize;
// Try allocating in new space.
const Class& cls = GrowableObjectArrayClass();
__ TryAllocate(cls, normal_ir_body, Assembler::kNearJump, EAX, EBX);
// Store backing array object in growable array object.
__ movl(EBX, Address(ESP, kArrayOffset)); // data argument.
// EAX is new, no barrier needed.
__ StoreIntoObjectNoBarrier(
EAX, FieldAddress(EAX, target::GrowableObjectArray::data_offset()), EBX);
// EAX: new growable array object start as a tagged pointer.
// Store the type argument field in the growable array object.
__ movl(EBX, Address(ESP, kTypeArgumentsOffset)); // type argument.
__ StoreIntoObjectNoBarrier(
EAX,
FieldAddress(EAX, target::GrowableObjectArray::type_arguments_offset()),
EBX);
__ ZeroInitSmiField(
FieldAddress(EAX, target::GrowableObjectArray::length_offset()));
__ ret(); // returns the newly allocated object in EAX.
__ Bind(normal_ir_body);
}
// Tests if two top most arguments are smis, jumps to label not_smi if not.
// Topmost argument is in EAX.
static void TestBothArgumentsSmis(Assembler* assembler, Label* not_smi) {
__ movl(EAX, Address(ESP, +1 * target::kWordSize));
__ movl(EBX, Address(ESP, +2 * target::kWordSize));
__ orl(EBX, EAX);
__ testl(EBX, Immediate(kSmiTagMask));
__ j(NOT_ZERO, not_smi, Assembler::kNearJump);
}
void AsmIntrinsifier::Integer_shl(Assembler* assembler, Label* normal_ir_body) {
ASSERT(kSmiTagShift == 1);
ASSERT(kSmiTag == 0);
Label overflow;
TestBothArgumentsSmis(assembler, normal_ir_body);
// Shift value is in EAX. Compare with tagged Smi.
__ cmpl(EAX, Immediate(target::ToRawSmi(target::kSmiBits)));
__ j(ABOVE_EQUAL, normal_ir_body, Assembler::kNearJump);
__ SmiUntag(EAX);
__ movl(ECX, EAX); // Shift amount must be in ECX.
__ movl(EAX, Address(ESP, +2 * target::kWordSize)); // Value.
// Overflow test - all the shifted-out bits must be same as the sign bit.
__ movl(EBX, EAX);
__ shll(EAX, ECX);
__ sarl(EAX, ECX);
__ cmpl(EAX, EBX);
__ j(NOT_EQUAL, &overflow, Assembler::kNearJump);
__ shll(EAX, ECX); // Shift for result now we know there is no overflow.
// EAX is a correctly tagged Smi.
__ ret();
__ Bind(&overflow);
// Arguments are Smi but the shift produced an overflow to Mint.
__ cmpl(EBX, Immediate(0));
// TODO(srdjan): Implement negative values, for now fall through.
__ j(LESS, normal_ir_body, Assembler::kNearJump);
__ SmiUntag(EBX);
__ movl(EAX, EBX);
__ shll(EBX, ECX);
__ xorl(EDI, EDI);
__ shldl(EDI, EAX, ECX);
// Result in EDI (high) and EBX (low).
const Class& mint_class = MintClass();
__ TryAllocate(mint_class, normal_ir_body, Assembler::kNearJump,
EAX, // Result register.
ECX); // temp
// EBX and EDI are not objects but integer values.
__ movl(FieldAddress(EAX, target::Mint::value_offset()), EBX);
__ movl(FieldAddress(EAX, target::Mint::value_offset() + target::kWordSize),
EDI);
__ ret();
__ Bind(normal_ir_body);
}
static void Push64SmiOrMint(Assembler* assembler,
Register reg,
Register tmp,
Label* not_smi_or_mint) {
Label not_smi, done;
__ testl(reg, Immediate(kSmiTagMask));
__ j(NOT_ZERO, ¬_smi, Assembler::kNearJump);
__ SmiUntag(reg);
// Sign extend to 64 bit
__ movl(tmp, reg);
__ sarl(tmp, Immediate(31));
__ pushl(tmp);
__ pushl(reg);
__ jmp(&done);
__ Bind(¬_smi);
__ CompareClassId(reg, kMintCid, tmp);
__ j(NOT_EQUAL, not_smi_or_mint);
// Mint.
__ pushl(FieldAddress(reg, target::Mint::value_offset() + target::kWordSize));
__ pushl(FieldAddress(reg, target::Mint::value_offset()));
__ Bind(&done);
}
static void CompareIntegers(Assembler* assembler,
Label* normal_ir_body,
Condition true_condition) {
Label try_mint_smi, is_true, is_false, drop_two_fall_through, fall_through;
TestBothArgumentsSmis(assembler, &try_mint_smi);
// EAX contains the right argument.
__ cmpl(Address(ESP, +2 * target::kWordSize), EAX);
__ j(true_condition, &is_true, Assembler::kNearJump);
__ Bind(&is_false);
__ LoadObject(EAX, CastHandle<Object>(FalseObject()));
__ ret();
__ Bind(&is_true);
__ LoadObject(EAX, CastHandle<Object>(TrueObject()));
__ ret();
// 64-bit comparison
Condition hi_true_cond, hi_false_cond, lo_false_cond;
switch (true_condition) {
case LESS:
case LESS_EQUAL:
hi_true_cond = LESS;
hi_false_cond = GREATER;
lo_false_cond = (true_condition == LESS) ? ABOVE_EQUAL : ABOVE;
break;
case GREATER:
case GREATER_EQUAL:
hi_true_cond = GREATER;
hi_false_cond = LESS;
lo_false_cond = (true_condition == GREATER) ? BELOW_EQUAL : BELOW;
break;
default:
UNREACHABLE();
hi_true_cond = hi_false_cond = lo_false_cond = OVERFLOW;
}
__ Bind(&try_mint_smi);
// Note that EDX and ECX must be preserved in case we fall through to main
// method.
// EAX contains the right argument.
__ movl(EBX, Address(ESP, +2 * target::kWordSize)); // Left argument.
// Push left as 64 bit integer.
Push64SmiOrMint(assembler, EBX, EDI, normal_ir_body);
// Push right as 64 bit integer.
Push64SmiOrMint(assembler, EAX, EDI, &drop_two_fall_through);
__ popl(EBX); // Right.LO.
__ popl(ECX); // Right.HI.
__ popl(EAX); // Left.LO.
__ popl(EDX); // Left.HI.
__ cmpl(EDX, ECX); // cmpl left.HI, right.HI.
__ j(hi_false_cond, &is_false, Assembler::kNearJump);
__ j(hi_true_cond, &is_true, Assembler::kNearJump);
__ cmpl(EAX, EBX); // cmpl left.LO, right.LO.
__ j(lo_false_cond, &is_false, Assembler::kNearJump);
// Else is true.
__ jmp(&is_true);
__ Bind(&drop_two_fall_through);
__ Drop(2);
__ Bind(normal_ir_body);
}
void AsmIntrinsifier::Integer_lessThan(Assembler* assembler,
Label* normal_ir_body) {
CompareIntegers(assembler, normal_ir_body, LESS);
}
void AsmIntrinsifier::Integer_greaterThan(Assembler* assembler,
Label* normal_ir_body) {
CompareIntegers(assembler, normal_ir_body, GREATER);
}
void AsmIntrinsifier::Integer_lessEqualThan(Assembler* assembler,
Label* normal_ir_body) {
CompareIntegers(assembler, normal_ir_body, LESS_EQUAL);
}
void AsmIntrinsifier::Integer_greaterEqualThan(Assembler* assembler,
Label* normal_ir_body) {
CompareIntegers(assembler, normal_ir_body, GREATER_EQUAL);
}
// This is called for Smi and Mint receivers. The right argument
// can be Smi, Mint or double.
void AsmIntrinsifier::Integer_equalToInteger(Assembler* assembler,
Label* normal_ir_body) {
Label true_label, check_for_mint;
// For integer receiver '===' check first.
__ movl(EAX, Address(ESP, +1 * target::kWordSize));
__ cmpl(EAX, Address(ESP, +2 * target::kWordSize));
__ j(EQUAL, &true_label, Assembler::kNearJump);
__ movl(EBX, Address(ESP, +2 * target::kWordSize));
__ orl(EAX, EBX);
__ testl(EAX, Immediate(kSmiTagMask));
__ j(NOT_ZERO, &check_for_mint, Assembler::kNearJump);
// Both arguments are smi, '===' is good enough.
__ LoadObject(EAX, CastHandle<Object>(FalseObject()));
__ ret();
__ Bind(&true_label);
__ LoadObject(EAX, CastHandle<Object>(TrueObject()));
__ ret();
// At least one of the arguments was not Smi.
Label receiver_not_smi;
__ Bind(&check_for_mint);
__ movl(EAX, Address(ESP, +2 * target::kWordSize)); // Receiver.
__ testl(EAX, Immediate(kSmiTagMask));
__ j(NOT_ZERO, &receiver_not_smi);
// Left (receiver) is Smi, return false if right is not Double.
// Note that an instance of Mint never contains a value that can be
// represented by Smi.
__ movl(EAX, Address(ESP, +1 * target::kWordSize)); // Right argument.
__ CompareClassId(EAX, kDoubleCid, EDI);
__ j(EQUAL, normal_ir_body);
__ LoadObject(EAX,
CastHandle<Object>(FalseObject())); // Smi == Mint -> false.
__ ret();
__ Bind(&receiver_not_smi);
// EAX:: receiver.
__ CompareClassId(EAX, kMintCid, EDI);
__ j(NOT_EQUAL, normal_ir_body);
// Receiver is Mint, return false if right is Smi.
__ movl(EAX, Address(ESP, +1 * target::kWordSize)); // Right argument.
__ testl(EAX, Immediate(kSmiTagMask));
__ j(NOT_ZERO, normal_ir_body);
__ LoadObject(EAX, CastHandle<Object>(FalseObject()));
__ ret();
// TODO(srdjan): Implement Mint == Mint comparison.
__ Bind(normal_ir_body);
}
void AsmIntrinsifier::Integer_equal(Assembler* assembler,
Label* normal_ir_body) {
Integer_equalToInteger(assembler, normal_ir_body);
}
// Argument is Smi (receiver).
void AsmIntrinsifier::Smi_bitLength(Assembler* assembler,
Label* normal_ir_body) {
ASSERT(kSmiTagShift == 1);
__ movl(EAX, Address(ESP, +1 * target::kWordSize)); // Receiver.
// XOR with sign bit to complement bits if value is negative.
__ movl(ECX, EAX);
__ sarl(ECX, Immediate(31)); // All 0 or all 1.
__ xorl(EAX, ECX);
// BSR does not write the destination register if source is zero. Put a 1 in
// the Smi tag bit to ensure BSR writes to destination register.
__ orl(EAX, Immediate(kSmiTagMask));
__ bsrl(EAX, EAX);
__ SmiTag(EAX);
__ ret();
}
void AsmIntrinsifier::Bigint_lsh(Assembler* assembler, Label* normal_ir_body) {
// static void _lsh(Uint32List x_digits, int x_used, int n,
// Uint32List r_digits)
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
__ movl(EDI, Address(ESP, 5 * target::kWordSize)); // x_digits
__ movl(ECX, Address(ESP, 3 * target::kWordSize)); // n is Smi
__ SmiUntag(ECX);
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // r_digits
__ movl(ESI, ECX);
__ sarl(ESI, Immediate(5)); // ESI = n ~/ _DIGIT_BITS.
__ leal(EBX,
FieldAddress(EBX, ESI, TIMES_4, target::TypedData::data_offset()));
__ movl(ESI, Address(ESP, 4 * target::kWordSize)); // x_used > 0, Smi.
__ SmiUntag(ESI);
__ decl(ESI);
__ xorl(EAX, EAX); // EAX = 0.
__ movl(EDX,
FieldAddress(EDI, ESI, TIMES_4, target::TypedData::data_offset()));
__ shldl(EAX, EDX, ECX);
__ movl(Address(EBX, ESI, TIMES_4, kBytesPerBigIntDigit), EAX);
Label last;
__ cmpl(ESI, Immediate(0));
__ j(EQUAL, &last, Assembler::kNearJump);
Label loop;
__ Bind(&loop);
__ movl(EAX, EDX);
__ movl(EDX, FieldAddress(
EDI, ESI, TIMES_4,
target::TypedData::data_offset() - kBytesPerBigIntDigit));
__ shldl(EAX, EDX, ECX);
__ movl(Address(EBX, ESI, TIMES_4, 0), EAX);
__ decl(ESI);
__ j(NOT_ZERO, &loop, Assembler::kNearJump);
__ Bind(&last);
__ shldl(EDX, ESI, ECX); // ESI == 0.
__ movl(Address(EBX, 0), EDX);
// Restore THR and return.
__ popl(THR);
__ LoadObject(EAX, NullObject());
__ ret();
}
void AsmIntrinsifier::Bigint_rsh(Assembler* assembler, Label* normal_ir_body) {
// static void _rsh(Uint32List x_digits, int x_used, int n,
// Uint32List r_digits)
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
__ movl(EDI, Address(ESP, 5 * target::kWordSize)); // x_digits
__ movl(ECX, Address(ESP, 3 * target::kWordSize)); // n is Smi
__ SmiUntag(ECX);
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // r_digits
__ movl(EDX, ECX);
__ sarl(EDX, Immediate(5)); // EDX = n ~/ _DIGIT_BITS.
__ movl(ESI, Address(ESP, 4 * target::kWordSize)); // x_used > 0, Smi.
__ SmiUntag(ESI);
__ decl(ESI);
// EDI = &x_digits[x_used - 1].
__ leal(EDI,
FieldAddress(EDI, ESI, TIMES_4, target::TypedData::data_offset()));
__ subl(ESI, EDX);
// EBX = &r_digits[x_used - 1 - (n ~/ 32)].
__ leal(EBX,
FieldAddress(EBX, ESI, TIMES_4, target::TypedData::data_offset()));
__ negl(ESI);
__ movl(EDX, Address(EDI, ESI, TIMES_4, 0));
Label last;
__ cmpl(ESI, Immediate(0));
__ j(EQUAL, &last, Assembler::kNearJump);
Label loop;
__ Bind(&loop);
__ movl(EAX, EDX);
__ movl(EDX, Address(EDI, ESI, TIMES_4, kBytesPerBigIntDigit));
__ shrdl(EAX, EDX, ECX);
__ movl(Address(EBX, ESI, TIMES_4, 0), EAX);
__ incl(ESI);
__ j(NOT_ZERO, &loop, Assembler::kNearJump);
__ Bind(&last);
__ shrdl(EDX, ESI, ECX); // ESI == 0.
__ movl(Address(EBX, 0), EDX);
// Restore THR and return.
__ popl(THR);
__ LoadObject(EAX, NullObject());
__ ret();
}
void AsmIntrinsifier::Bigint_absAdd(Assembler* assembler,
Label* normal_ir_body) {
// static void _absAdd(Uint32List digits, int used,
// Uint32List a_digits, int a_used,
// Uint32List r_digits)
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
__ movl(EDI, Address(ESP, 6 * target::kWordSize)); // digits
__ movl(EAX, Address(ESP, 5 * target::kWordSize)); // used is Smi
__ SmiUntag(EAX); // used > 0.
__ movl(ESI, Address(ESP, 4 * target::kWordSize)); // a_digits
__ movl(ECX, Address(ESP, 3 * target::kWordSize)); // a_used is Smi
__ SmiUntag(ECX); // a_used > 0.
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // r_digits
// Precompute 'used - a_used' now so that carry flag is not lost later.
__ subl(EAX, ECX);
__ incl(EAX); // To account for the extra test between loops.
__ pushl(EAX);
__ xorl(EDX, EDX); // EDX = 0, carry flag = 0.
Label add_loop;
__ Bind(&add_loop);
// Loop a_used times, ECX = a_used, ECX > 0.
__ movl(EAX,
FieldAddress(EDI, EDX, TIMES_4, target::TypedData::data_offset()));
__ adcl(EAX,
FieldAddress(ESI, EDX, TIMES_4, target::TypedData::data_offset()));
__ movl(FieldAddress(EBX, EDX, TIMES_4, target::TypedData::data_offset()),
EAX);
__ incl(EDX); // Does not affect carry flag.
__ decl(ECX); // Does not affect carry flag.
__ j(NOT_ZERO, &add_loop, Assembler::kNearJump);
Label last_carry;
__ popl(ECX);
__ decl(ECX); // Does not affect carry flag.
__ j(ZERO, &last_carry, Assembler::kNearJump); // If used - a_used == 0.
Label carry_loop;
__ Bind(&carry_loop);
// Loop used - a_used times, ECX = used - a_used, ECX > 0.
__ movl(EAX,
FieldAddress(EDI, EDX, TIMES_4, target::TypedData::data_offset()));
__ adcl(EAX, Immediate(0));
__ movl(FieldAddress(EBX, EDX, TIMES_4, target::TypedData::data_offset()),
EAX);
__ incl(EDX); // Does not affect carry flag.
__ decl(ECX); // Does not affect carry flag.
__ j(NOT_ZERO, &carry_loop, Assembler::kNearJump);
__ Bind(&last_carry);
__ movl(EAX, Immediate(0));
__ adcl(EAX, Immediate(0));
__ movl(FieldAddress(EBX, EDX, TIMES_4, target::TypedData::data_offset()),
EAX);
// Restore THR and return.
__ popl(THR);
__ LoadObject(EAX, NullObject());
__ ret();
}
void AsmIntrinsifier::Bigint_absSub(Assembler* assembler,
Label* normal_ir_body) {
// static void _absSub(Uint32List digits, int used,
// Uint32List a_digits, int a_used,
// Uint32List r_digits)
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
__ movl(EDI, Address(ESP, 6 * target::kWordSize)); // digits
__ movl(EAX, Address(ESP, 5 * target::kWordSize)); // used is Smi
__ SmiUntag(EAX); // used > 0.
__ movl(ESI, Address(ESP, 4 * target::kWordSize)); // a_digits
__ movl(ECX, Address(ESP, 3 * target::kWordSize)); // a_used is Smi
__ SmiUntag(ECX); // a_used > 0.
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // r_digits
// Precompute 'used - a_used' now so that carry flag is not lost later.
__ subl(EAX, ECX);
__ incl(EAX); // To account for the extra test between loops.
__ pushl(EAX);
__ xorl(EDX, EDX); // EDX = 0, carry flag = 0.
Label sub_loop;
__ Bind(&sub_loop);
// Loop a_used times, ECX = a_used, ECX > 0.
__ movl(EAX,
FieldAddress(EDI, EDX, TIMES_4, target::TypedData::data_offset()));
__ sbbl(EAX,
FieldAddress(ESI, EDX, TIMES_4, target::TypedData::data_offset()));
__ movl(FieldAddress(EBX, EDX, TIMES_4, target::TypedData::data_offset()),
EAX);
__ incl(EDX); // Does not affect carry flag.
__ decl(ECX); // Does not affect carry flag.
__ j(NOT_ZERO, &sub_loop, Assembler::kNearJump);
Label done;
__ popl(ECX);
__ decl(ECX); // Does not affect carry flag.
__ j(ZERO, &done, Assembler::kNearJump); // If used - a_used == 0.
Label carry_loop;
__ Bind(&carry_loop);
// Loop used - a_used times, ECX = used - a_used, ECX > 0.
__ movl(EAX,
FieldAddress(EDI, EDX, TIMES_4, target::TypedData::data_offset()));
__ sbbl(EAX, Immediate(0));
__ movl(FieldAddress(EBX, EDX, TIMES_4, target::TypedData::data_offset()),
EAX);
__ incl(EDX); // Does not affect carry flag.
__ decl(ECX); // Does not affect carry flag.
__ j(NOT_ZERO, &carry_loop, Assembler::kNearJump);
__ Bind(&done);
// Restore THR and return.
__ popl(THR);
__ LoadObject(EAX, NullObject());
__ ret();
}
void AsmIntrinsifier::Bigint_mulAdd(Assembler* assembler,
Label* normal_ir_body) {
// Pseudo code:
// static int _mulAdd(Uint32List x_digits, int xi,
// Uint32List m_digits, int i,
// Uint32List a_digits, int j, int n) {
// uint32_t x = x_digits[xi >> 1]; // xi is Smi.
// if (x == 0 || n == 0) {
// return 1;
// }
// uint32_t* mip = &m_digits[i >> 1]; // i is Smi.
// uint32_t* ajp = &a_digits[j >> 1]; // j is Smi.
// uint32_t c = 0;
// SmiUntag(n);
// do {
// uint32_t mi = *mip++;
// uint32_t aj = *ajp;
// uint64_t t = x*mi + aj + c; // 32-bit * 32-bit -> 64-bit.
// *ajp++ = low32(t);
// c = high32(t);
// } while (--n > 0);
// while (c != 0) {
// uint64_t t = *ajp + c;
// *ajp++ = low32(t);
// c = high32(t); // c == 0 or 1.
// }
// return 1;
// }
Label no_op;
// EBX = x, no_op if x == 0
__ movl(ECX, Address(ESP, 7 * target::kWordSize)); // x_digits
__ movl(EAX, Address(ESP, 6 * target::kWordSize)); // xi is Smi
__ movl(EBX,
FieldAddress(ECX, EAX, TIMES_2, target::TypedData::data_offset()));
__ testl(EBX, EBX);
__ j(ZERO, &no_op, Assembler::kNearJump);
// EDX = SmiUntag(n), no_op if n == 0
__ movl(EDX, Address(ESP, 1 * target::kWordSize));
__ SmiUntag(EDX);
__ j(ZERO, &no_op, Assembler::kNearJump);
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
// EDI = mip = &m_digits[i >> 1]
__ movl(EDI, Address(ESP, 6 * target::kWordSize)); // m_digits
__ movl(EAX, Address(ESP, 5 * target::kWordSize)); // i is Smi
__ leal(EDI,
FieldAddress(EDI, EAX, TIMES_2, target::TypedData::data_offset()));
// ESI = ajp = &a_digits[j >> 1]
__ movl(ESI, Address(ESP, 4 * target::kWordSize)); // a_digits
__ movl(EAX, Address(ESP, 3 * target::kWordSize)); // j is Smi
__ leal(ESI,
FieldAddress(ESI, EAX, TIMES_2, target::TypedData::data_offset()));
// Save n
__ pushl(EDX);
Address n_addr = Address(ESP, 0 * target::kWordSize);
// ECX = c = 0
__ xorl(ECX, ECX);
Label muladd_loop;
__ Bind(&muladd_loop);
// x: EBX
// mip: EDI
// ajp: ESI
// c: ECX
// t: EDX:EAX (not live at loop entry)
// n: ESP[0]
// uint32_t mi = *mip++
__ movl(EAX, Address(EDI, 0));
__ addl(EDI, Immediate(kBytesPerBigIntDigit));
// uint64_t t = x*mi
__ mull(EBX); // t = EDX:EAX = EAX * EBX
__ addl(EAX, ECX); // t += c
__ adcl(EDX, Immediate(0));
// uint32_t aj = *ajp; t += aj
__ addl(EAX, Address(ESI, 0));
__ adcl(EDX, Immediate(0));
// *ajp++ = low32(t)
__ movl(Address(ESI, 0), EAX);
__ addl(ESI, Immediate(kBytesPerBigIntDigit));
// c = high32(t)
__ movl(ECX, EDX);
// while (--n > 0)
__ decl(n_addr); // --n
__ j(NOT_ZERO, &muladd_loop, Assembler::kNearJump);
Label done;
__ testl(ECX, ECX);
__ j(ZERO, &done, Assembler::kNearJump);
// *ajp += c
__ addl(Address(ESI, 0), ECX);
__ j(NOT_CARRY, &done, Assembler::kNearJump);
Label propagate_carry_loop;
__ Bind(&propagate_carry_loop);
__ addl(ESI, Immediate(kBytesPerBigIntDigit));
__ incl(Address(ESI, 0)); // c == 0 or 1
__ j(CARRY, &propagate_carry_loop, Assembler::kNearJump);
__ Bind(&done);
__ Drop(1); // n
// Restore THR and return.
__ popl(THR);
__ Bind(&no_op);
__ movl(EAX, Immediate(target::ToRawSmi(1))); // One digit processed.
__ ret();
}
void AsmIntrinsifier::Bigint_sqrAdd(Assembler* assembler,
Label* normal_ir_body) {
// Pseudo code:
// static int _sqrAdd(Uint32List x_digits, int i,
// Uint32List a_digits, int used) {
// uint32_t* xip = &x_digits[i >> 1]; // i is Smi.
// uint32_t x = *xip++;
// if (x == 0) return 1;
// uint32_t* ajp = &a_digits[i]; // j == 2*i, i is Smi.
// uint32_t aj = *ajp;
// uint64_t t = x*x + aj;
// *ajp++ = low32(t);
// uint64_t c = high32(t);
// int n = ((used - i) >> 1) - 1; // used and i are Smi.
// while (--n >= 0) {
// uint32_t xi = *xip++;
// uint32_t aj = *ajp;
// uint96_t t = 2*x*xi + aj + c; // 2-bit * 32-bit * 32-bit -> 65-bit.
// *ajp++ = low32(t);
// c = high64(t); // 33-bit.
// }
// uint32_t aj = *ajp;
// uint64_t t = aj + c; // 32-bit + 33-bit -> 34-bit.
// *ajp++ = low32(t);
// *ajp = high32(t);
// return 1;
// }
// EDI = xip = &x_digits[i >> 1]
__ movl(EDI, Address(ESP, 4 * target::kWordSize)); // x_digits
__ movl(EAX, Address(ESP, 3 * target::kWordSize)); // i is Smi
__ leal(EDI,
FieldAddress(EDI, EAX, TIMES_2, target::TypedData::data_offset()));
// EBX = x = *xip++, return if x == 0
Label x_zero;
__ movl(EBX, Address(EDI, 0));
__ cmpl(EBX, Immediate(0));
__ j(EQUAL, &x_zero, Assembler::kNearJump);
__ addl(EDI, Immediate(kBytesPerBigIntDigit));
// Preserve THR to free ESI.
__ pushl(THR);
ASSERT(THR == ESI);
// ESI = ajp = &a_digits[i]
__ movl(ESI, Address(ESP, 3 * target::kWordSize)); // a_digits
__ leal(ESI,
FieldAddress(ESI, EAX, TIMES_4, target::TypedData::data_offset()));
// EDX:EAX = t = x*x + *ajp
__ movl(EAX, EBX);
__ mull(EBX);
__ addl(EAX, Address(ESI, 0));
__ adcl(EDX, Immediate(0));
// *ajp++ = low32(t)
__ movl(Address(ESI, 0), EAX);
__ addl(ESI, Immediate(kBytesPerBigIntDigit));
// int n = used - i - 1
__ movl(EAX, Address(ESP, 2 * target::kWordSize)); // used is Smi
__ subl(EAX, Address(ESP, 4 * target::kWordSize)); // i is Smi
__ SmiUntag(EAX);
__ decl(EAX);
__ pushl(EAX); // Save n on stack.
// uint64_t c = high32(t)
__ pushl(Immediate(0)); // push high32(c) == 0
__ pushl(EDX); // push low32(c) == high32(t)
Address n_addr = Address(ESP, 2 * target::kWordSize);
Address ch_addr = Address(ESP, 1 * target::kWordSize);
Address cl_addr = Address(ESP, 0 * target::kWordSize);
Label loop, done;
__ Bind(&loop);
// x: EBX
// xip: EDI
// ajp: ESI
// c: ESP[1]:ESP[0]
// t: ECX:EDX:EAX (not live at loop entry)
// n: ESP[2]
// while (--n >= 0)
__ decl(Address(ESP, 2 * target::kWordSize)); // --n
__ j(NEGATIVE, &done, Assembler::kNearJump);
// uint32_t xi = *xip++
__ movl(EAX, Address(EDI, 0));
__ addl(EDI, Immediate(kBytesPerBigIntDigit));
// uint96_t t = ECX:EDX:EAX = 2*x*xi + aj + c
__ mull(EBX); // EDX:EAX = EAX * EBX
__ xorl(ECX, ECX); // ECX = 0
__ shldl(ECX, EDX, Immediate(1));
__ shldl(EDX, EAX, Immediate(1));
__ shll(EAX, Immediate(1)); // ECX:EDX:EAX <<= 1
__ addl(EAX, Address(ESI, 0)); // t += aj
__ adcl(EDX, Immediate(0));
__ adcl(ECX, Immediate(0));
__ addl(EAX, cl_addr); // t += low32(c)
__ adcl(EDX, ch_addr); // t += high32(c) << 32
__ adcl(ECX, Immediate(0));
// *ajp++ = low32(t)
__ movl(Address(ESI, 0), EAX);
__ addl(ESI, Immediate(kBytesPerBigIntDigit));
// c = high64(t)
__ movl(cl_addr, EDX);
__ movl(ch_addr, ECX);
__ jmp(&loop, Assembler::kNearJump);
__ Bind(&done);
// uint64_t t = aj + c
__ movl(EAX, cl_addr); // t = c
__ movl(EDX, ch_addr);
__ addl(EAX, Address(ESI, 0)); // t += *ajp
__ adcl(EDX, Immediate(0));
// *ajp++ = low32(t)
// *ajp = high32(t)
__ movl(Address(ESI, 0), EAX);
__ movl(Address(ESI, kBytesPerBigIntDigit), EDX);
// Restore THR and return.
__ Drop(3);
__ popl(THR);
__ Bind(&x_zero);
__ movl(EAX, Immediate(target::ToRawSmi(1))); // One digit processed.
__ ret();
}
void AsmIntrinsifier::Bigint_estimateQuotientDigit(Assembler* assembler,
Label* normal_ir_body) {
// Pseudo code:
// static int _estQuotientDigit(Uint32List args, Uint32List digits, int i) {
// uint32_t yt = args[_YT]; // _YT == 1.
// uint32_t* dp = &digits[i >> 1]; // i is Smi.
// uint32_t dh = dp[0]; // dh == digits[i >> 1].
// uint32_t qd;
// if (dh == yt) {
// qd = DIGIT_MASK;
// } else {
// dl = dp[-1]; // dl == digits[(i - 1) >> 1].
// qd = dh:dl / yt; // No overflow possible, because dh < yt.
// }
// args[_QD] = qd; // _QD == 2.
// return 1;
// }
// EDI = args
__ movl(EDI, Address(ESP, 3 * target::kWordSize)); // args
// ECX = yt = args[1]
__ movl(ECX, FieldAddress(EDI, target::TypedData::data_offset() +
kBytesPerBigIntDigit));
// EBX = dp = &digits[i >> 1]
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // digits
__ movl(EAX, Address(ESP, 1 * target::kWordSize)); // i is Smi
__ leal(EBX,
FieldAddress(EBX, EAX, TIMES_2, target::TypedData::data_offset()));
// EDX = dh = dp[0]
__ movl(EDX, Address(EBX, 0));
// EAX = qd = DIGIT_MASK = -1
__ movl(EAX, Immediate(-1));
// Return qd if dh == yt
Label return_qd;
__ cmpl(EDX, ECX);
__ j(EQUAL, &return_qd, Assembler::kNearJump);
// EAX = dl = dp[-1]
__ movl(EAX, Address(EBX, -kBytesPerBigIntDigit));
// EAX = qd = dh:dl / yt = EDX:EAX / ECX
__ divl(ECX);
__ Bind(&return_qd);
// args[2] = qd
__ movl(FieldAddress(
EDI, target::TypedData::data_offset() + 2 * kBytesPerBigIntDigit),
EAX);
__ movl(EAX, Immediate(target::ToRawSmi(1))); // One digit processed.
__ ret();
}
void AsmIntrinsifier::Montgomery_mulMod(Assembler* assembler,
Label* normal_ir_body) {
// Pseudo code:
// static int _mulMod(Uint32List args, Uint32List digits, int i) {
// uint32_t rho = args[_RHO]; // _RHO == 2.
// uint32_t d = digits[i >> 1]; // i is Smi.
// uint64_t t = rho*d;
// args[_MU] = t mod DIGIT_BASE; // _MU == 4.
// return 1;
// }
// EDI = args
__ movl(EDI, Address(ESP, 3 * target::kWordSize)); // args
// ECX = rho = args[2]
__ movl(ECX, FieldAddress(EDI, target::TypedData::data_offset() +
2 * kBytesPerBigIntDigit));
// EAX = digits[i >> 1]
__ movl(EBX, Address(ESP, 2 * target::kWordSize)); // digits
__ movl(EAX, Address(ESP, 1 * target::kWordSize)); // i is Smi
__ movl(EAX,
FieldAddress(EBX, EAX, TIMES_2, target::TypedData::data_offset()));
// EDX:EAX = t = rho*d
__ mull(ECX);
// args[4] = t mod DIGIT_BASE = low32(t)
__ movl(FieldAddress(
EDI, target::TypedData::data_offset() + 4 * kBytesPerBigIntDigit),
EAX);
__ movl(EAX, Immediate(target::ToRawSmi(1))); // One digit processed.
__ ret();
}
// Check if the last argument is a double, jump to label 'is_smi' if smi
// (easy to convert to double), otherwise jump to label 'not_double_smi',
// Returns the last argument in EAX.
static void TestLastArgumentIsDouble(Assembler* assembler,
Label* is_smi,
Label* not_double_smi) {
__ movl(EAX, Address(ESP, +1 * target::kWordSize));
__ testl(EAX, Immediate(kSmiTagMask));
__ j(ZERO, is_smi, Assembler::kNearJump); // Jump if Smi.
__ CompareClassId(EAX, kDoubleCid, EBX);
__ j(NOT_EQUAL, not_double_smi, Assembler::kNearJump);
// Fall through if double.
}
// Both arguments on stack, arg0 (left) is a double, arg1 (right) is of unknown
// type. Return true or false object in the register EAX. Any NaN argument
// returns false. Any non-double arg1 causes control flow to fall through to the
// slow case (compiled method body).
static void CompareDoubles(Assembler* assembler,
Label* normal_ir_body,
Condition true_condition) {
Label is_false, is_true, is_smi, double_op;
TestLastArgumentIsDouble(assembler, &is_smi, normal_ir_body);
// Both arguments are double, right operand is in EAX.
__ movsd(XMM1, FieldAddress(EAX, target::Double::value_offset()));
__ Bind(&double_op);
__ movl(EAX, Address(ESP, +2 * target::kWordSize)); // Left argument.
__ movsd(XMM0, FieldAddress(EAX, target::Double::value_offset()));
__ comisd(XMM0, XMM1);
__ j(PARITY_EVEN, &is_false, Assembler::kNearJump); // NaN -> false;
__ j(true_condition, &is_true, Assembler::kNearJump);
// Fall through false.
__ Bind(&is_false);
__ LoadObject(EAX, CastHandle<Object>(FalseObject()));
__ ret();
__ Bind(&is_true);
__ LoadObject(EAX, CastHandle<Object>(TrueObject()));
__ ret();
__ Bind(&is_smi);
__ SmiUntag(EAX);
__ cvtsi2sd(XMM1, EAX);
__ jmp(&double_op);
__ Bind(normal_ir_body);
}
// arg0 is Double, arg1 is unknown.
void AsmIntrinsifier::Double_greaterThan(Assembler* assembler,
Label* normal_ir_body) {
CompareDoubles(assembler, normal_ir_body, ABOVE);
}
// arg0 is Double, arg1 is unknown.
void AsmIntrinsifier::Double_greaterEqualThan(Assembler* assembler,
Label* normal_ir_body) {
CompareDoubles(assembler, normal_ir_body, ABOVE_EQUAL);
}
// arg0 is Double, arg1 is unknown.
void AsmIntrinsifier::Double_lessThan(Assembler* assembler,
Label* normal_ir_body) {
CompareDoubles(assembler, normal_ir_body, BELOW);
}
// arg0 is Double, arg1 is unknown.
void AsmIntrinsifier::Double_equal(Assembler* assembler,
Label* normal_ir_body) {
CompareDoubles(assembler, normal_ir_body, EQUAL);
}
// arg0 is Double, arg1 is unknown.
void AsmIntrinsifier::Double_lessEqualThan(Assembler* assembler,
Label* normal_ir_body) {
CompareDoubles(assembler, normal_ir_body, BELOW_EQUAL);
}
// Expects left argument to be double (receiver). Right argument is unknown.
// Both arguments are on stack.
static void DoubleArithmeticOperations(Assembler* assembler,
Label* normal_ir_body,
Token::Kind kind) {
Label is_smi, double_op;
TestLastArgumentIsDouble(assembler, &is_smi, normal_ir_body);
// Both arguments are double, right operand is in EAX.
__ movsd(XMM1, FieldAddress(EAX, target::Double::value_offset()));
__ Bind(&double_op);
__ movl(EAX, Address(ESP, +2 * target::kWordSize)); // Left argument.
__ movsd(XMM0, FieldAddress(EAX, target::Double::value_offset()));
switch (kind) {
case Token::kADD:
__ addsd(XMM0, XMM1);
break;
case Token::kSUB:
__ subsd(XMM0, XMM1);
break;
case Token::kMUL:
__ mulsd(XMM0, XMM1);
break;
case Token::kDIV:
__ divsd(XMM0, XMM1);
break;
default:
UNREACHABLE();
}
const Class& double_class = DoubleClass();
__ TryAllocate(double_class, normal_ir_body, Assembler::kNearJump,
EAX, // Result register.
EBX);