forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirgen-arith.cpp
1623 lines (1480 loc) · 47.6 KB
/
irgen-arith.cpp
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/irgen-arith.h"
#include "hphp/runtime/base/strings.h"
#include "hphp/runtime/vm/jit/irgen-exit.h"
#include "hphp/runtime/vm/jit/irgen-incdec.h"
#include "hphp/runtime/vm/jit/irgen-interpone.h"
#include "hphp/runtime/vm/jit/irgen-internal.h"
namespace HPHP { namespace jit { namespace irgen {
static const StaticString s_emptyString("");
bool areBinaryArithTypesSupported(Op op, Type t1, Type t2) {
auto checkArith = [](Type ty) {
return ty.subtypeOfAny(TInt, TBool, TDbl);
};
auto checkBitOp = [](Type ty) {
return ty.subtypeOfAny(TInt, TBool);
};
switch (op) {
case Op::Add:
case Op::Sub:
case Op::Mul:
case Op::AddO:
case Op::SubO:
case Op::MulO:
return checkArith(t1) && checkArith(t2);
case Op::BitAnd:
case Op::BitOr:
case Op::BitXor:
return checkBitOp(t1) && checkBitOp(t2);
default:
break;
}
always_assert(0);
}
Opcode intArithOp(Op op) {
switch (op) {
case Op::Add: return AddInt;
case Op::Sub: return SubInt;
case Op::Mul: return MulInt;
case Op::AddO: return AddIntO;
case Op::SubO: return SubIntO;
case Op::MulO: return MulIntO;
default:
break;
}
always_assert(0);
}
Opcode dblArithOp(Op op) {
switch (op) {
case Op::Add: return AddDbl;
case Op::Sub: return SubDbl;
case Op::Mul: return MulDbl;
case Op::AddO: return AddDbl;
case Op::SubO: return SubDbl;
case Op::MulO: return MulDbl;
default:
break;
}
always_assert(0);
}
Opcode bitOp(Op op) {
switch (op) {
case Op::BitAnd: return AndInt;
case Op::BitOr: return OrInt;
case Op::BitXor: return XorInt;
default:
break;
}
always_assert(0);
}
bool isBitOp(Op op) {
switch (op) {
case Op::BitAnd:
case Op::BitOr:
case Op::BitXor:
return true;
default:
return false;
}
}
SSATmp* promoteBool(IRGS& env, SSATmp* src) {
// booleans in arithmetic and bitwise operations get cast to ints
return src->type() <= TBool ? gen(env, ConvBoolToInt, src) : src;
}
Opcode promoteBinaryDoubles(IRGS& env, Op op, SSATmp*& src1, SSATmp*& src2) {
auto const type1 = src1->type();
auto const type2 = src2->type();
auto opc = intArithOp(op);
if (type1 <= TDbl) {
opc = dblArithOp(op);
if (type2 <= TInt) {
src2 = gen(env, ConvIntToDbl, src2);
}
} else if (type2 <= TDbl) {
opc = dblArithOp(op);
src1 = gen(env, ConvIntToDbl, src1);
}
return opc;
}
namespace {
void binaryBitOp(IRGS& env, Op op) {
auto const type2 = topC(env, BCSPRelOffset{0})->type();
auto const type1 = topC(env, BCSPRelOffset{1})->type();
if (!areBinaryArithTypesSupported(op, type1, type2)) {
PUNT(BinaryBitOp-Unsupported);
return;
}
auto const src2 = promoteBool(env, popC(env));
auto const src1 = promoteBool(env, popC(env));
push(env, gen(env, bitOp(op), src1, src2));
}
void binaryArith(IRGS& env, Op op) {
auto const type2 = topC(env, BCSPRelOffset{0})->type();
auto const type1 = topC(env, BCSPRelOffset{1})->type();
if (!areBinaryArithTypesSupported(op, type1, type2)) {
// either an int or a dbl, but can't tell
PUNT(BinaryArith-Unsupported);
}
auto const exitSlow = makeExitSlow(env);
auto src2 = promoteBool(env, popC(env));
auto src1 = promoteBool(env, popC(env));
auto const opc = promoteBinaryDoubles(env, op, src1, src2);
if (opc == AddIntO || opc == SubIntO || opc == MulIntO) {
assertx(src1->isA(TInt) && src2->isA(TInt));
push(env, gen(env, opc, exitSlow, src1, src2));
} else {
push(env, gen(env, opc, src1, src2));
}
}
// Helpers for comparison generation:
Opcode toBoolCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtBool;
case Op::Gte: return GteBool;
case Op::Lt: return LtBool;
case Op::Lte: return LteBool;
case Op::Eq:
case Op::Same: return EqBool;
case Op::Neq:
case Op::NSame: return NeqBool;
case Op::Cmp: return CmpBool;
default: always_assert(false);
}
}
Opcode toIntCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtInt;
case Op::Gte: return GteInt;
case Op::Lt: return LtInt;
case Op::Lte: return LteInt;
case Op::Eq:
case Op::Same: return EqInt;
case Op::Neq:
case Op::NSame: return NeqInt;
case Op::Cmp: return CmpInt;
default: always_assert(false);
}
}
Opcode toDblCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtDbl;
case Op::Gte: return GteDbl;
case Op::Lt: return LtDbl;
case Op::Lte: return LteDbl;
case Op::Eq:
case Op::Same: return EqDbl;
case Op::Neq:
case Op::NSame: return NeqDbl;
case Op::Cmp: return CmpDbl;
default: always_assert(false);
}
}
Opcode toStrCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtStr;
case Op::Gte: return GteStr;
case Op::Lt: return LtStr;
case Op::Lte: return LteStr;
case Op::Eq: return EqStr;
case Op::Same: return SameStr;
case Op::Neq: return NeqStr;
case Op::NSame: return NSameStr;
case Op::Cmp: return CmpStr;
default: always_assert(false);
}
}
Opcode toStrIntCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtStrInt;
case Op::Gte: return GteStrInt;
case Op::Lt: return LtStrInt;
case Op::Lte: return LteStrInt;
case Op::Eq:
case Op::Same: return EqStrInt;
case Op::Neq:
case Op::NSame: return NeqStrInt;
case Op::Cmp: return CmpStrInt;
default: always_assert(false);
}
}
Opcode toObjCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtObj;
case Op::Gte: return GteObj;
case Op::Lt: return LtObj;
case Op::Lte: return LteObj;
case Op::Eq: return EqObj;
case Op::Same: return SameObj;
case Op::Neq: return NeqObj;
case Op::NSame: return NSameObj;
case Op::Cmp: return CmpObj;
default: always_assert(false);
}
}
Opcode toArrCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtArr;
case Op::Gte: return GteArr;
case Op::Lt: return LtArr;
case Op::Lte: return LteArr;
case Op::Eq: return EqArr;
case Op::Same: return SameArr;
case Op::Neq: return NeqArr;
case Op::NSame: return NSameArr;
case Op::Cmp: return CmpArr;
default: always_assert(false);
}
}
Opcode toVecCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtVec;
case Op::Gte: return GteVec;
case Op::Lt: return LtVec;
case Op::Lte: return LteVec;
case Op::Eq: return EqVec;
case Op::Same: return SameVec;
case Op::Neq: return NeqVec;
case Op::NSame: return NSameVec;
case Op::Cmp: return CmpVec;
default: always_assert(false);
}
}
Opcode toDictCmpOpcode(Op op) {
switch (op) {
case Op::Eq: return EqDict;
case Op::Same: return SameDict;
case Op::Neq: return NeqDict;
case Op::NSame: return NSameDict;
default: always_assert(false);
}
}
Opcode toKeysetCmpOpcode(Op op) {
switch (op) {
case Op::Eq: return EqKeyset;
case Op::Same: return SameKeyset;
case Op::Neq: return NeqKeyset;
case Op::NSame: return NSameKeyset;
default: always_assert(false);
}
}
Opcode toResCmpOpcode(Op op) {
switch (op) {
case Op::Gt: return GtRes;
case Op::Gte: return GteRes;
case Op::Lt: return LtRes;
case Op::Lte: return LteRes;
case Op::Eq:
case Op::Same: return EqRes;
case Op::Neq:
case Op::NSame: return NeqRes;
case Op::Cmp: return CmpRes;
default: always_assert(false);
}
}
// Emit a commuted version of the specified operation. The given callable is
// invoked with the adjusted (if any) opcode to use. The callable should
// generate the proper instruction(s) to perform the comparison operation which
// matches the given opcode. The result from those instructions may be used as
// inputs to additional instructions. Regardless, the return value is the result
// of the commuted comparison.
template <typename F>
SSATmp* emitCommutedOp(IRGS& env, Op op, F f) {
switch (op) {
case Op::Gt: return f(Op::Lt);
case Op::Gte: return f(Op::Lte);
case Op::Lt: return f(Op::Gt);
case Op::Lte: return f(Op::Gte);
case Op::Eq: return f(Op::Eq);
case Op::Same: return f(Op::Same);
case Op::Neq: return f(Op::Neq);
case Op::NSame: return f(Op::NSame);
case Op::Cmp:
return gen(env, SubInt, cns(env, 0), f(Op::Cmp));
default: always_assert(false);
}
}
// Emit a check for whether the given object is a collection.
template <typename F>
SSATmp* emitCollectionCheck(IRGS& env, Op op, SSATmp* src, F f) {
assertx(src->type() <= TObj);
return cond(
env,
[&] (Block* taken) {
auto const isCol = gen(env, IsCol, src);
gen(env, JmpNZero, taken, isCol);
},
// Not a collection, just emit the code given by the callable.
[&] { return f(); },
[&] {
// It's a collection, so either ThrowInvalidOperation, or return a
// constant depending on the type of comparison being done.
hint(env, Block::Hint::Unlikely);
switch (op) {
case Op::Gt:
case Op::Gte:
case Op::Lt:
case Op::Lte:
case Op::Cmp:
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithCollection.get())
);
// Dead-code, but needed to satisfy cond().
return cns(env, false);
case Op::Eq: return cns(env, false);
case Op::Neq: return cns(env, true);
default: always_assert(false);
}
}
);
}
// Emit a comparison against an object and string. This needs special handling
// because the behavior varies depending on whether the object has a toString()
// method or not.
SSATmp* emitObjStrCmp(IRGS& env, Op op, SSATmp* obj, SSATmp* str) {
assertx(obj->type() <= TObj);
assertx(str->type() <= TStr);
return cond(
env,
[&] (Block* taken) {
auto const hasToString = gen(env, HasToString, obj);
gen(env, JmpNZero, taken, hasToString);
},
[&] {
// If there's no toString() method, than the object is always greater than
// the string.
switch (op) {
case Op::Neq:
case Op::Gt:
case Op::Gte: return cns(env, true);
case Op::Eq:
case Op::Lt:
case Op::Lte: return cns(env, false);
case Op::Cmp: return cns(env, 1);
default: always_assert(false);
}
},
[&] {
// If there is a toString() method, use it (via ConvObjToStr) to turn the
// object into a string, and then do a string comparison.
auto const converted = gen(env, ConvObjToStr, obj);
auto const result = gen(env, toStrCmpOpcode(op), converted, str);
decRef(env, converted);
return result;
}
);
}
// Emit a boolean comparison against two constants. Will be simplified to a
// constant later on.
SSATmp* emitConstCmp(IRGS& env, Op op, bool left, bool right) {
return gen(env, toBoolCmpOpcode(op), cns(env, left), cns(env, right));
}
// Relational comparisons of vecs with non-vecs isn't allowed and will always
// throw. Equality comparisons always act as if they are not equal.
SSATmp* emitMixedVecCmp(IRGS& env, Op op) {
switch (op) {
case Op::Gt:
case Op::Gte:
case Op::Lt:
case Op::Lte:
case Op::Cmp:
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithVec.get())
);
return cns(env, false);
case Op::Same:
case Op::Eq: return cns(env, false);
case Op::NSame:
case Op::Neq: return cns(env, true);
default: always_assert(false);
}
}
// Relational comparisons of dicts with non-dicts isn't allowed and will always
// throw. Equality comparisons always act as if they are not equal.
SSATmp* emitMixedDictCmp(IRGS& env, Op op) {
switch (op) {
case Op::Gt:
case Op::Gte:
case Op::Lt:
case Op::Lte:
case Op::Cmp:
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithDict.get())
);
return cns(env, false);
case Op::Same:
case Op::Eq: return cns(env, false);
case Op::NSame:
case Op::Neq: return cns(env, true);
default: always_assert(false);
}
}
// Relational comparisons of keysets with non-keysets isn't allowed and will
// always throw. Equality comparisons always act as if they are not equal.
SSATmp* emitMixedKeysetCmp(IRGS& env, Op op) {
switch (op) {
case Op::Gt:
case Op::Gte:
case Op::Lt:
case Op::Lte:
case Op::Cmp:
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithKeyset.get())
);
return cns(env, false);
case Op::Same:
case Op::Eq: return cns(env, false);
case Op::NSame:
case Op::Neq: return cns(env, true);
default: always_assert(false);
}
}
void implNullCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TNull);
auto const rightTy = right->type();
// Left operand is null.
if (rightTy <= TStr) {
// Null converts to the empty string when being compared against a string.
push(env,
gen(env,
toStrCmpOpcode(op),
cns(env, s_emptyString.get()),
right));
} else if (rightTy <= TObj) {
// When compared to an object, null is treated as false, and the object as
// true. We cannot use ConvObjToBool here because that has special behavior
// in certain cases, which we do not want here. Also note that no collection
// check is done.
push(env, emitConstCmp(env, op, false, true));
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// Otherwise, convert both sides to booleans (with null becoming false).
push(env,
gen(env,
toBoolCmpOpcode(op),
cns(env, false),
gen(env, ConvCellToBool, right)));
}
}
void implBoolCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TBool);
auto const rightTy = right->type();
if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// Convert whatever is on the right to a boolean and compare. The conversion
// may be a no-op if the right operand is already a bool.
push(env,
gen(env,
toBoolCmpOpcode(op),
left,
gen(env, ConvCellToBool, right)));
}
}
void implIntCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TInt);
auto const rightTy = right->type();
// Left operand is int.
if (rightTy <= TDbl) {
// If compared against a double, promote to a double.
push(env,
gen(env,
toDblCmpOpcode(op),
gen(env, ConvIntToDbl, left),
right));
} else if (rightTy <= TStr) {
// If compared against a string, commute the expression and do a specialized
// string-int comparison.
push(
env,
emitCommutedOp(
env,
op,
[&](Op op2){ return gen(env, toStrIntCmpOpcode(op2), right, left); }
)
);
} else if (rightTy.subtypeOfAny(TNull, TBool)) {
// If compared against null or bool, convert both sides to bools.
push(env,
gen(env,
toBoolCmpOpcode(op),
gen(env, ConvIntToBool, left),
gen(env, ConvCellToBool, right)));
} else if (rightTy <= TArr) {
// All ints are implicity less than arrays.
push(env, emitConstCmp(env, op, false, true));
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else if (rightTy <= TObj) {
// If compared against an object, emit a collection check before performing
// the comparison.
push(
env,
emitCollectionCheck(
env,
op,
right,
[&]{
return gen(
env,
toIntCmpOpcode(op),
left,
gen(env, ConvObjToInt, right)
);
}
)
);
} else {
// For everything else, convert to an int. The conversion may be a no-op if
// the right operand is already an int.
push(env,
gen(env,
toIntCmpOpcode(op),
left,
gen(env, ConvCellToInt, right)));
}
}
void implDblCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TDbl);
auto const rightTy = right->type();
// Left operand is a double.
if (rightTy.subtypeOfAny(TNull, TBool)) {
// If compared against null or bool, convert both sides to bools.
push(env,
gen(env,
toBoolCmpOpcode(op),
gen(env, ConvDblToBool, left),
gen(env, ConvCellToBool, right)));
} else if (rightTy <= TArr) {
// All doubles are implicitly less than arrays.
push(env, emitConstCmp(env, op, false, true));
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else if (rightTy <= TObj) {
// If compared against an object, emit a collection check before performing
// the comparison.
push(
env,
emitCollectionCheck(
env,
op,
right,
[&]{
return gen(
env,
toDblCmpOpcode(op),
left,
gen(env, ConvObjToDbl, right)
);
}
)
);
} else {
// For everything else, convert to a double. The conversion may be a no-op
// if the right operand is already a double.
push(env,
gen(env,
toDblCmpOpcode(op),
left,
gen(env, ConvCellToDbl, right)));
}
}
void implArrCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TArr);
auto const rightTy = right->type();
// Left operand is an array.
if (rightTy <= TArr) {
// No conversion needed.
push(env, gen(env, toArrCmpOpcode(op), left, right));
} else if (rightTy.subtypeOfAny(TNull, TBool)) {
// If compared against null or bool, convert both sides to bools.
push(env,
gen(env,
toBoolCmpOpcode(op),
gen(env, ConvArrToBool, left),
gen(env, ConvCellToBool, right)));
} else if (rightTy <= TObj) {
// Objects are always greater than arrays. Emit a collection check first.
push(
env,
emitCollectionCheck(
env,
op,
right,
[&]{ return emitConstCmp(env, op, false, true); }
)
);
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// Array is always greater than everything else.
push(env, emitConstCmp(env, op, true, false));
}
}
void implVecCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TVec);
auto const rightTy = right->type();
// Left operand is a vec.
if (rightTy <= TVec) {
push(env, gen(env, toVecCmpOpcode(op), left, right));
} else {
push(env, emitMixedVecCmp(env, op));
}
}
void implDictCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TDict);
auto const rightTy = right->type();
// Left operand is a dict.
if (rightTy <= TDict) {
// Dicts can't use relational comparisons.
if (op == Op::Eq || op == Op::Neq ||
op == Op::Same || op == Op::NSame) {
push(env, gen(env, toDictCmpOpcode(op), left, right));
} else {
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithDict.get())
);
push(env, cns(env, false));
}
} else {
push(env, emitMixedDictCmp(env, op));
}
}
void implKeysetCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TKeyset);
auto const rightTy = right->type();
// Left operand is a keyset.
if (rightTy <= TKeyset) {
// Keysets can't use relational comparisons.
if (op == Op::Eq || op == Op::Neq ||
op == Op::Same || op == Op::NSame) {
push(env, gen(env, toKeysetCmpOpcode(op), left, right));
} else {
gen(
env,
ThrowInvalidOperation,
cns(env, s_cmpWithKeyset.get())
);
push(env, cns(env, false));
}
} else {
push(env, emitMixedKeysetCmp(env, op));
}
}
void implStrCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TStr);
auto const rightTy = right->type();
// Left operand is a string.
if (rightTy <= TStr) {
// No conversion needed.
push(env, gen(env, toStrCmpOpcode(op), left, right));
} else if (rightTy <= TNull) {
// Comparisons against null are turned into comparisons with the empty
// string.
push(env,
gen(env,
toStrCmpOpcode(op),
left,
cns(env, s_emptyString.get())));
} else if (rightTy <= TBool) {
// If compared against a bool, convert the string to a bool.
push(env,
gen(env,
toBoolCmpOpcode(op),
gen(env, ConvStrToBool, left),
right));
} else if (rightTy <= TInt) {
// If compared against an integer, do no conversion and use the specialized
// string-int comparison.
push(env,
gen(env,
toStrIntCmpOpcode(op),
left,
right));
} else if (rightTy <= TDbl) {
// If compared against a double, convert the string to a double.
push(env,
gen(env,
toDblCmpOpcode(op),
gen(env, ConvStrToDbl, left),
right));
} else if (rightTy <= TRes) {
// Bizarrely, comparison against a resource is done by converting both the
// string and the resource to a double and comparing the two.
push(env,
gen(env,
toDblCmpOpcode(op),
gen(env, ConvStrToDbl, left),
gen(env, ConvResToDbl, right)));
} else if (rightTy <= TObj) {
// If compared against an object, first do a collection check on the object,
// and then emit an object-string comparison (swapping the order of the
// operands).
push(
env,
emitCollectionCheck(
env,
op,
right,
[&]{
return emitCommutedOp(
env,
op,
[&](Op op) { return emitObjStrCmp(env, op, right, left); }
);
}
)
);
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// Strings are less than anything else (usually arrays).
push(env, emitConstCmp(env, op, false, true));
}
}
void implObjCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TObj);
auto const rightTy = right->type();
// Left operand is an object.
if (rightTy <= TObj) {
// No conversion needed.
push(env, gen(env, toObjCmpOpcode(op), left, right));
} else if (rightTy <= TBool) {
// If compared against a bool, convert to a bool.
push(env,
gen(env,
toBoolCmpOpcode(op),
gen(env, ConvObjToBool, left),
right));
} else if (rightTy <= TInt) {
// If compared against an integer, emit a collection check before doing the
// comparison.
push(
env,
emitCollectionCheck(
env,
op,
left,
[&]{
return gen(
env,
toIntCmpOpcode(op),
gen(env, ConvObjToInt, left),
right
);
}
)
);
} else if (rightTy <= TDbl) {
// If compared against a double, emit a collection check before performing
// the comparison.
push(
env,
emitCollectionCheck(
env,
op,
left,
[&]{
return gen(
env,
toDblCmpOpcode(op),
gen(env, ConvObjToDbl, left),
right
);
}
)
);
} else if (rightTy <= TStr) {
// If compared against a string, first do a collection check, and then emit
// an object-string comparison.
push(
env,
emitCollectionCheck(
env,
op,
left,
[&]{ return emitObjStrCmp(env, op, left, right); }
)
);
} else if (rightTy <= TArr) {
// Object is always greater than array, but we need a collection check
// first.
push(
env,
emitCollectionCheck(
env,
op,
left,
[&]{ return emitConstCmp(env, op, true, false); }
)
);
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// For anything else, the object is always greater.
push(env, emitConstCmp(env, op, true, false));
}
}
void implResCmp(IRGS& env, Op op, SSATmp* left, SSATmp* right) {
assert(left->type() <= TRes);
auto const rightTy = right->type();
// Left operand is a resource.
if (rightTy <= TRes) {
// No conversion needed.
push(env, gen(env, toResCmpOpcode(op), left, right));
} else if (rightTy <= TNull) {
// Resources are always greater than nulls.
push(env, emitConstCmp(env, op, true, false));
} else if (rightTy <= TBool) {
// If compared against a boolean, convert to a boolean.
push(env,
gen(env,
toBoolCmpOpcode(op),
cns(env, true),
right));
} else if (rightTy <= TInt) {
// If compared against an integer, convert to an integer.
push(env,
gen(env,
toIntCmpOpcode(op),
gen(env, ConvResToInt, left),
right));
} else if (rightTy <= TDbl) {
// If compared against a double, convert to a double.
push(env,
gen(env,
toDblCmpOpcode(op),
gen(env, ConvResToDbl, left),
right));
} else if (rightTy <= TStr) {
// Bizaarly, comparison against a string is done by converting both the
// string and the resource to a double and comparing the two.
push(env,
gen(env,
toDblCmpOpcode(op),
gen(env, ConvResToDbl, left),
gen(env, ConvStrToDbl, right)));
} else if (rightTy <= TVec) {
push(env, emitMixedVecCmp(env, op));
} else if (rightTy <= TDict) {
push(env, emitMixedDictCmp(env, op));
} else if (rightTy <= TKeyset) {
push(env, emitMixedKeysetCmp(env, op));
} else {
// Resources are always less than anything else.
push(env, emitConstCmp(env, op, false, true));
}
}
/*
* Responsible for converting the bytecode comparisons (which are type-agnostic)
* to IR comparisons (which are typed). This generally involves inserting the
* right kind of type conversions to satisfy PHP semantics. For a few special
* cases, (object-string and string-int), we have special IR opcodes because the
* required semantics cannot be easily represented via type conversions.
*/
void implCmp(IRGS& env, Op op) {
auto const right = popC(env);
auto const left = popC(env);