forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-gen-x64.cpp
More file actions
2203 lines (1950 loc) · 67.2 KB
/
code-gen-x64.cpp
File metadata and controls
2203 lines (1950 loc) · 67.2 KB
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-2016 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/code-gen-x64.h"
#include <cstring>
#include <iostream>
#include <limits>
#include <vector>
#ifndef _MSC_VER
#include <unwind.h>
#endif
#include <folly/ScopeGuard.h>
#include <folly/Format.h>
#include "hphp/util/trace.h"
#include "hphp/util/text-util.h"
#include "hphp/util/abi-cxx.h"
#include "hphp/runtime/base/apc-local-array.h"
#include "hphp/runtime/base/comparisons.h"
#include "hphp/runtime/base/mixed-array.h"
#include "hphp/runtime/base/packed-array.h"
#include "hphp/runtime/base/rds-header.h"
#include "hphp/runtime/base/rds-util.h"
#include "hphp/runtime/base/rds.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/base/shape.h"
#include "hphp/runtime/base/stats.h"
#include "hphp/runtime/base/string-data.h"
//#include "hphp/runtime/base/typed-value.h"
//#include "hphp/runtime/base/struct-array.h"
#include "hphp/runtime/vm/bytecode.h"
#include "hphp/runtime/vm/hhbc-codec.h"
#include "hphp/runtime/vm/runtime.h"
#include "hphp/runtime/vm/jit/abi.h"
#include "hphp/runtime/vm/jit/arg-group.h"
#include "hphp/runtime/vm/jit/cfg.h"
#include "hphp/runtime/vm/jit/code-gen-cf.h"
#include "hphp/runtime/vm/jit/code-gen-helpers.h"
#include "hphp/runtime/vm/jit/code-gen-internal.h"
#include "hphp/runtime/vm/jit/ir-opcode.h"
#include "hphp/runtime/vm/jit/irlower-internal.h"
#include "hphp/runtime/vm/jit/mc-generator.h"
#include "hphp/runtime/vm/jit/native-calls.h"
#include "hphp/runtime/vm/jit/print.h"
#include "hphp/runtime/vm/jit/prof-data.h"
#include "hphp/runtime/vm/jit/reg-algorithms.h"
#include "hphp/runtime/vm/jit/service-requests.h"
#include "hphp/runtime/vm/jit/stack-offsets.h"
#include "hphp/runtime/vm/jit/stack-overflow.h"
#include "hphp/runtime/vm/jit/target-cache.h"
#include "hphp/runtime/vm/jit/target-profile.h"
#include "hphp/runtime/vm/jit/timer.h"
#include "hphp/runtime/vm/jit/translator-inline.h"
#include "hphp/runtime/vm/jit/translator.h"
#include "hphp/runtime/vm/jit/types.h"
#include "hphp/runtime/vm/jit/unwind-itanium.h"
#include "hphp/runtime/vm/jit/vasm-gen.h"
#include "hphp/runtime/vm/jit/vasm-instr.h"
#include "hphp/runtime/vm/jit/vasm-reg.h"
#include "hphp/runtime/vm/jit/vasm.h"
#include "hphp/runtime/ext/asio/asio-blockable.h"
#include "hphp/runtime/ext/asio/ext_async-function-wait-handle.h"
#include "hphp/runtime/ext/asio/ext_wait-handle.h"
#include "hphp/runtime/ext/std/ext_std_closure.h"
#include "hphp/runtime/ext/collections/hash-collection.h"
#include "hphp/runtime/ext/collections/ext_collections-pair.h"
#include "hphp/runtime/ext/collections/ext_collections-vector.h"
#include "hphp/runtime/ext/generator/ext_generator.h"
#define rvmsp() DontUseRVmSpInThisFile
namespace HPHP { namespace jit { namespace irlower {
TRACE_SET_MOD(hhir);
///////////////////////////////////////////////////////////////////////////////
Vloc CodeGenerator::srcLoc(const IRInstruction* inst, unsigned i) const {
return irlower::srcLoc(m_state, inst, i);
}
Vloc CodeGenerator::dstLoc(const IRInstruction* inst, unsigned i) const {
return irlower::dstLoc(m_state, inst, i);
}
ArgGroup CodeGenerator::argGroup(const IRInstruction* inst) const {
return irlower::argGroup(m_state, inst);
}
void CodeGenerator::cgInst(IRInstruction* inst) {
SCOPE_ASSERT_DETAIL("cgInst") { return inst->toString(); };
switch (inst->op()) {
#define O(name, dsts, srcs, flags) \
case name: FTRACE(7, "cg" #name "\n"); \
cg ## name (inst); \
break;
IR_OPCODES
#undef O
default:
always_assert(false);
}
auto& v = vmain();
if (inst->isBlockEnd() && !v.closed()) {
if (auto next = inst->next()) {
v << jmp{m_state.labels[next]};
} else {
v << ud2{}; // or end?
}
}
}
#define NOOP_OPCODE(opcode) \
void CodeGenerator::cg##opcode(IRInstruction*) {}
#define CALL_OPCODE(opcode) \
void CodeGenerator::cg##opcode(IRInstruction* i) { \
cgCallNative(vmain(), i); \
}
#define DELEGATE_OPCODE(opcode) \
void CodeGenerator::cg##opcode(IRInstruction* i) { \
irlower::cg##opcode(m_state, i); \
}
NOOP_OPCODE(DefConst)
NOOP_OPCODE(DefFP)
NOOP_OPCODE(AssertLoc)
NOOP_OPCODE(Nop)
NOOP_OPCODE(EndGuards)
NOOP_OPCODE(ExitPlaceholder);
NOOP_OPCODE(HintLocInner)
NOOP_OPCODE(HintStkInner)
NOOP_OPCODE(AssertStk)
NOOP_OPCODE(FinishMemberOp)
CALL_OPCODE(AddElemStrKey)
CALL_OPCODE(AddElemIntKey)
CALL_OPCODE(AddNewElem)
CALL_OPCODE(ArrayAdd)
CALL_OPCODE(MapAddElemC)
CALL_OPCODE(ColAddNewElemC)
CALL_OPCODE(CoerceCellToBool);
CALL_OPCODE(CoerceCellToInt);
CALL_OPCODE(CoerceCellToDbl);
CALL_OPCODE(CoerceStrToDbl);
CALL_OPCODE(CoerceStrToInt);
CALL_OPCODE(ConvBoolToArr);
CALL_OPCODE(ConvDblToArr);
CALL_OPCODE(ConvIntToArr);
CALL_OPCODE(ConvObjToArr);
CALL_OPCODE(ConvStrToArr);
CALL_OPCODE(ConvVecToArr);
CALL_OPCODE(ConvDictToArr);
CALL_OPCODE(ConvCellToArr);
CALL_OPCODE(ConvCellToBool);
CALL_OPCODE(ConvArrToDbl);
CALL_OPCODE(ConvObjToDbl);
CALL_OPCODE(ConvStrToDbl);
CALL_OPCODE(ConvResToDbl);
CALL_OPCODE(ConvCellToDbl);
CALL_OPCODE(ConvArrToInt);
CALL_OPCODE(ConvObjToInt);
CALL_OPCODE(ConvStrToInt);
CALL_OPCODE(ConvResToInt);
CALL_OPCODE(ConvCellToInt);
CALL_OPCODE(ConvCellToObj);
CALL_OPCODE(ConvDblToStr);
CALL_OPCODE(ConvIntToStr);
CALL_OPCODE(ConvObjToStr);
CALL_OPCODE(ConvResToStr);
CALL_OPCODE(ConvCellToStr);
CALL_OPCODE(ConcatStrStr);
CALL_OPCODE(ConcatStrInt);
CALL_OPCODE(ConcatIntStr);
CALL_OPCODE(ConcatStr3);
CALL_OPCODE(ConcatStr4);
CALL_OPCODE(GtStr);
CALL_OPCODE(GteStr);
CALL_OPCODE(LtStr);
CALL_OPCODE(LteStr);
CALL_OPCODE(EqStr);
CALL_OPCODE(NeqStr);
CALL_OPCODE(SameStr);
CALL_OPCODE(NSameStr);
CALL_OPCODE(CmpStr);
CALL_OPCODE(GtStrInt);
CALL_OPCODE(GteStrInt);
CALL_OPCODE(LtStrInt);
CALL_OPCODE(LteStrInt);
CALL_OPCODE(EqStrInt);
CALL_OPCODE(NeqStrInt);
CALL_OPCODE(CmpStrInt);
CALL_OPCODE(GtObj);
CALL_OPCODE(GteObj);
CALL_OPCODE(LtObj);
CALL_OPCODE(LteObj);
CALL_OPCODE(EqObj);
CALL_OPCODE(NeqObj);
CALL_OPCODE(CmpObj);
CALL_OPCODE(GtArr);
CALL_OPCODE(GteArr);
CALL_OPCODE(LtArr);
CALL_OPCODE(LteArr);
CALL_OPCODE(EqArr);
CALL_OPCODE(NeqArr);
CALL_OPCODE(SameArr);
CALL_OPCODE(NSameArr);
CALL_OPCODE(CmpArr);
CALL_OPCODE(GtRes);
CALL_OPCODE(GteRes);
CALL_OPCODE(LtRes);
CALL_OPCODE(LteRes);
CALL_OPCODE(CmpRes);
CALL_OPCODE(ThrowInvalidOperation);
CALL_OPCODE(HasToString);
CALL_OPCODE(NewArray)
CALL_OPCODE(NewMixedArray)
CALL_OPCODE(NewDictArray)
CALL_OPCODE(NewLikeArray)
CALL_OPCODE(AllocPackedArray)
CALL_OPCODE(AllocVecArray)
CALL_OPCODE(Clone)
CALL_OPCODE(AllocObj)
CALL_OPCODE(InitProps)
CALL_OPCODE(InitSProps)
CALL_OPCODE(DebugBacktrace)
CALL_OPCODE(InitThrowableFileAndLine)
CALL_OPCODE(RegisterLiveObj)
CALL_OPCODE(LdClsCtor)
CALL_OPCODE(LookupClsRDS)
CALL_OPCODE(PrintStr)
CALL_OPCODE(PrintInt)
CALL_OPCODE(PrintBool)
CALL_OPCODE(DbgAssertPtr)
CALL_OPCODE(LdSwitchDblIndex)
CALL_OPCODE(LdSwitchStrIndex)
CALL_OPCODE(LdSwitchObjIndex)
CALL_OPCODE(VerifyParamCallable)
CALL_OPCODE(VerifyParamFail)
CALL_OPCODE(VerifyParamFailHard)
CALL_OPCODE(VerifyRetCallable)
CALL_OPCODE(VerifyRetFail)
CALL_OPCODE(RaiseUninitLoc)
CALL_OPCODE(RaiseUndefProp)
CALL_OPCODE(RaiseMissingArg)
CALL_OPCODE(RaiseError)
CALL_OPCODE(RaiseWarning)
CALL_OPCODE(RaiseNotice)
CALL_OPCODE(RaiseArrayIndexNotice)
CALL_OPCODE(RaiseArrayKeyNotice)
CALL_OPCODE(IncStatGrouped)
CALL_OPCODE(MapIdx)
CALL_OPCODE(LdClsPropAddrOrNull)
CALL_OPCODE(LdClsPropAddrOrRaise)
CALL_OPCODE(LdGblAddrDef)
CALL_OPCODE(MethodExists)
// Vector instruction helpers
CALL_OPCODE(StringGet)
CALL_OPCODE(BindElem)
CALL_OPCODE(SetWithRefElem)
CALL_OPCODE(SetOpElem)
CALL_OPCODE(IncDecElem)
CALL_OPCODE(SetNewElem)
CALL_OPCODE(SetNewElemArray)
CALL_OPCODE(BindNewElem)
CALL_OPCODE(VectorIsset)
CALL_OPCODE(PairIsset)
CALL_OPCODE(ThrowOutOfBounds)
CALL_OPCODE(ThrowArithmeticError)
CALL_OPCODE(ThrowDivisionByZeroError)
CALL_OPCODE(ZeroErrorLevel)
CALL_OPCODE(RestoreErrorLevel)
CALL_OPCODE(Count)
CALL_OPCODE(SuspendHookE)
CALL_OPCODE(SuspendHookR)
CALL_OPCODE(ReturnHook)
CALL_OPCODE(OODeclExists)
CALL_OPCODE(GetMemoKey)
DELEGATE_OPCODE(Box)
DELEGATE_OPCODE(BoxPtr)
DELEGATE_OPCODE(UnboxPtr)
DELEGATE_OPCODE(AddInt)
DELEGATE_OPCODE(SubInt)
DELEGATE_OPCODE(MulInt)
DELEGATE_OPCODE(AddIntO)
DELEGATE_OPCODE(SubIntO)
DELEGATE_OPCODE(MulIntO)
DELEGATE_OPCODE(AddDbl)
DELEGATE_OPCODE(SubDbl)
DELEGATE_OPCODE(MulDbl)
DELEGATE_OPCODE(DivDbl)
DELEGATE_OPCODE(DivInt)
DELEGATE_OPCODE(Mod)
DELEGATE_OPCODE(Floor)
DELEGATE_OPCODE(Ceil)
DELEGATE_OPCODE(AbsDbl)
DELEGATE_OPCODE(Sqrt)
DELEGATE_OPCODE(AndInt)
DELEGATE_OPCODE(OrInt)
DELEGATE_OPCODE(XorInt)
DELEGATE_OPCODE(Shl)
DELEGATE_OPCODE(Shr)
DELEGATE_OPCODE(XorBool)
DELEGATE_OPCODE(GtInt)
DELEGATE_OPCODE(GteInt)
DELEGATE_OPCODE(LtInt)
DELEGATE_OPCODE(LteInt)
DELEGATE_OPCODE(EqInt)
DELEGATE_OPCODE(NeqInt)
DELEGATE_OPCODE(CmpInt)
DELEGATE_OPCODE(GtBool)
DELEGATE_OPCODE(GteBool)
DELEGATE_OPCODE(LtBool)
DELEGATE_OPCODE(LteBool)
DELEGATE_OPCODE(EqBool)
DELEGATE_OPCODE(NeqBool)
DELEGATE_OPCODE(CmpBool)
DELEGATE_OPCODE(GtDbl)
DELEGATE_OPCODE(GteDbl)
DELEGATE_OPCODE(LtDbl)
DELEGATE_OPCODE(LteDbl)
DELEGATE_OPCODE(EqDbl)
DELEGATE_OPCODE(NeqDbl)
DELEGATE_OPCODE(CmpDbl)
DELEGATE_OPCODE(SameObj)
DELEGATE_OPCODE(NSameObj)
DELEGATE_OPCODE(EqRes)
DELEGATE_OPCODE(NeqRes)
DELEGATE_OPCODE(EqCls)
DELEGATE_OPCODE(InstanceOf)
DELEGATE_OPCODE(InstanceOfIface)
DELEGATE_OPCODE(InstanceOfIfaceVtable)
DELEGATE_OPCODE(ExtendsClass)
DELEGATE_OPCODE(InstanceOfBitmask)
DELEGATE_OPCODE(NInstanceOfBitmask)
DELEGATE_OPCODE(InterfaceSupportsArr)
DELEGATE_OPCODE(InterfaceSupportsStr)
DELEGATE_OPCODE(InterfaceSupportsInt)
DELEGATE_OPCODE(InterfaceSupportsDbl)
DELEGATE_OPCODE(CheckType)
DELEGATE_OPCODE(CheckTypeMem)
DELEGATE_OPCODE(CheckLoc)
DELEGATE_OPCODE(CheckStk)
DELEGATE_OPCODE(CheckRefInner)
DELEGATE_OPCODE(IsType)
DELEGATE_OPCODE(IsNType)
DELEGATE_OPCODE(IsTypeMem)
DELEGATE_OPCODE(IsNTypeMem)
DELEGATE_OPCODE(IsScalarType)
DELEGATE_OPCODE(LdLoc)
DELEGATE_OPCODE(LdLocAddr)
DELEGATE_OPCODE(StLoc)
DELEGATE_OPCODE(StLocRange)
DELEGATE_OPCODE(DbgTrashFrame)
DELEGATE_OPCODE(LdLocPseudoMain)
DELEGATE_OPCODE(StLocPseudoMain)
DELEGATE_OPCODE(LdStk)
DELEGATE_OPCODE(LdStkAddr)
DELEGATE_OPCODE(StStk)
DELEGATE_OPCODE(DbgTrashStk)
DELEGATE_OPCODE(LdMem)
DELEGATE_OPCODE(StMem)
DELEGATE_OPCODE(DbgTrashMem)
DELEGATE_OPCODE(LdRef)
DELEGATE_OPCODE(StRef)
DELEGATE_OPCODE(LdElem)
DELEGATE_OPCODE(StElem)
DELEGATE_OPCODE(LdMIStateAddr)
DELEGATE_OPCODE(LdMBase)
DELEGATE_OPCODE(StMBase)
DELEGATE_OPCODE(LdGblAddr)
DELEGATE_OPCODE(LdPropAddr)
DELEGATE_OPCODE(LdRDSAddr)
DELEGATE_OPCODE(LdTVAux)
DELEGATE_OPCODE(IncRef)
DELEGATE_OPCODE(IncRefCtx)
DELEGATE_OPCODE(DecRef)
DELEGATE_OPCODE(DecRefNZ)
DELEGATE_OPCODE(DbgAssertRefCount)
DELEGATE_OPCODE(DefLabel)
DELEGATE_OPCODE(Jmp)
DELEGATE_OPCODE(JmpZero)
DELEGATE_OPCODE(JmpNZero)
DELEGATE_OPCODE(ProfileSwitchDest)
DELEGATE_OPCODE(JmpSwitchDest)
DELEGATE_OPCODE(JmpSSwitchDest)
DELEGATE_OPCODE(LdSSwitchDestFast)
DELEGATE_OPCODE(LdSSwitchDestSlow)
DELEGATE_OPCODE(ReqBindJmp)
DELEGATE_OPCODE(ReqRetranslate)
DELEGATE_OPCODE(ReqRetranslateOpt)
DELEGATE_OPCODE(LdClsCtx)
DELEGATE_OPCODE(LdClsCctx)
DELEGATE_OPCODE(CastCtxThis)
DELEGATE_OPCODE(CheckCtxThis)
DELEGATE_OPCODE(LdClsName)
DELEGATE_OPCODE(LdClsMethod)
DELEGATE_OPCODE(LdIfaceMethod)
DELEGATE_OPCODE(LdObjInvoke)
DELEGATE_OPCODE(LdFuncVecLen)
DELEGATE_OPCODE(LdFuncNumParams)
DELEGATE_OPCODE(BaseG)
DELEGATE_OPCODE(PropX)
DELEGATE_OPCODE(PropDX)
DELEGATE_OPCODE(PropQ)
DELEGATE_OPCODE(CGetProp)
DELEGATE_OPCODE(CGetPropQ)
DELEGATE_OPCODE(VGetProp)
DELEGATE_OPCODE(BindProp)
DELEGATE_OPCODE(SetProp)
DELEGATE_OPCODE(UnsetProp)
DELEGATE_OPCODE(SetOpProp)
DELEGATE_OPCODE(IncDecProp)
DELEGATE_OPCODE(IssetProp)
DELEGATE_OPCODE(EmptyProp)
DELEGATE_OPCODE(ElemX)
DELEGATE_OPCODE(ElemDX)
DELEGATE_OPCODE(ElemUX)
DELEGATE_OPCODE(CGetElem)
DELEGATE_OPCODE(VGetElem)
DELEGATE_OPCODE(SetElem)
DELEGATE_OPCODE(UnsetElem)
DELEGATE_OPCODE(IssetElem)
DELEGATE_OPCODE(EmptyElem)
DELEGATE_OPCODE(ProfileMixedArrayOffset)
DELEGATE_OPCODE(CheckMixedArrayOffset)
DELEGATE_OPCODE(CheckArrayCOW)
DELEGATE_OPCODE(ElemArray)
DELEGATE_OPCODE(ElemArrayW)
DELEGATE_OPCODE(ElemArrayD)
DELEGATE_OPCODE(ElemArrayU)
DELEGATE_OPCODE(ElemMixedArrayK)
DELEGATE_OPCODE(ArrayGet)
DELEGATE_OPCODE(MixedArrayGetK)
DELEGATE_OPCODE(ArraySet)
DELEGATE_OPCODE(ArraySetRef)
DELEGATE_OPCODE(ArrayIsset)
DELEGATE_OPCODE(ArrayIdx)
DELEGATE_OPCODE(MapGet)
DELEGATE_OPCODE(MapSet)
DELEGATE_OPCODE(MapIsset)
DELEGATE_OPCODE(LdCls)
DELEGATE_OPCODE(LdClsCached)
DELEGATE_OPCODE(LdClsCachedSafe)
DELEGATE_OPCODE(LdFunc)
DELEGATE_OPCODE(LdFuncCached)
DELEGATE_OPCODE(LdFuncCachedU)
DELEGATE_OPCODE(LdFuncCachedSafe)
DELEGATE_OPCODE(LdObjMethod)
DELEGATE_OPCODE(LookupClsMethodCache)
DELEGATE_OPCODE(LdClsMethodCacheFunc)
DELEGATE_OPCODE(LdClsMethodCacheCls)
DELEGATE_OPCODE(LookupClsMethodFCache)
DELEGATE_OPCODE(LdClsMethodFCacheFunc)
DELEGATE_OPCODE(GetCtxFwdCall)
DELEGATE_OPCODE(GetCtxFwdCallDyn)
DELEGATE_OPCODE(LdCns)
DELEGATE_OPCODE(LookupCns)
DELEGATE_OPCODE(LookupCnsE)
DELEGATE_OPCODE(LookupCnsU)
DELEGATE_OPCODE(LdClsCns)
DELEGATE_OPCODE(InitClsCns)
DELEGATE_OPCODE(SpillFrame)
DELEGATE_OPCODE(CufIterSpillFrame)
DELEGATE_OPCODE(LdARFuncPtr)
DELEGATE_OPCODE(LdARNumArgsAndFlags)
DELEGATE_OPCODE(StARNumArgsAndFlags)
DELEGATE_OPCODE(LdARNumParams)
DELEGATE_OPCODE(CheckARMagicFlag)
DELEGATE_OPCODE(LdCtx)
DELEGATE_OPCODE(LdCctx)
DELEGATE_OPCODE(InitCtx)
DELEGATE_OPCODE(LdARInvName)
DELEGATE_OPCODE(StARInvName)
DELEGATE_OPCODE(InitExtraArgs)
DELEGATE_OPCODE(PackMagicArgs)
DELEGATE_OPCODE(Call)
DELEGATE_OPCODE(CallArray)
DELEGATE_OPCODE(CallBuiltin)
DELEGATE_OPCODE(NativeImpl)
DELEGATE_OPCODE(DbgTraceCall)
DELEGATE_OPCODE(EnterFrame)
DELEGATE_OPCODE(RetCtrl)
DELEGATE_OPCODE(AsyncRetCtrl)
DELEGATE_OPCODE(AsyncRetFast)
DELEGATE_OPCODE(AsyncSwitchFast)
DELEGATE_OPCODE(LdRetVal)
DELEGATE_OPCODE(DbgTrashRetVal)
DELEGATE_OPCODE(FreeActRec)
DELEGATE_OPCODE(GenericRetDecRefs)
DELEGATE_OPCODE(ReleaseVVAndSkip)
DELEGATE_OPCODE(BeginInlining)
DELEGATE_OPCODE(DefInlineFP)
DELEGATE_OPCODE(InlineReturn)
DELEGATE_OPCODE(InlineReturnNoFrame)
DELEGATE_OPCODE(SyncReturnBC)
DELEGATE_OPCODE(Conjure)
DELEGATE_OPCODE(ConjureUse)
DELEGATE_OPCODE(LdStaticLoc)
DELEGATE_OPCODE(InitStaticLoc)
DELEGATE_OPCODE(CheckClosureStaticLocInit)
DELEGATE_OPCODE(LdClosureStaticLoc)
DELEGATE_OPCODE(InitClosureStaticLoc)
DELEGATE_OPCODE(LdClosure)
DELEGATE_OPCODE(LdClosureCtx)
DELEGATE_OPCODE(StClosureCtx)
DELEGATE_OPCODE(StClosureArg)
DELEGATE_OPCODE(LdResumableArObj)
DELEGATE_OPCODE(CreateCont)
DELEGATE_OPCODE(ContEnter)
DELEGATE_OPCODE(ContPreNext)
DELEGATE_OPCODE(ContStartedCheck)
DELEGATE_OPCODE(ContStarted)
DELEGATE_OPCODE(ContValid)
DELEGATE_OPCODE(StContArState)
DELEGATE_OPCODE(LdContField)
DELEGATE_OPCODE(LdContActRec)
DELEGATE_OPCODE(LdContArValue)
DELEGATE_OPCODE(StContArValue)
DELEGATE_OPCODE(LdContResumeAddr)
DELEGATE_OPCODE(StContArResume)
DELEGATE_OPCODE(LdContArKey)
DELEGATE_OPCODE(StContArKey)
DELEGATE_OPCODE(ContArIncKey)
DELEGATE_OPCODE(ContArIncIdx)
DELEGATE_OPCODE(ContArUpdateIdx)
DELEGATE_OPCODE(CreateAFWH)
DELEGATE_OPCODE(CreateAFWHNoVV)
DELEGATE_OPCODE(CreateSSWH)
DELEGATE_OPCODE(AFWHPrepareChild)
DELEGATE_OPCODE(AFWHBlockOn)
DELEGATE_OPCODE(ABCUnblock)
DELEGATE_OPCODE(IsWaitHandle)
DELEGATE_OPCODE(LdWHState)
DELEGATE_OPCODE(StAsyncArSucceeded)
DELEGATE_OPCODE(LdWHResult)
DELEGATE_OPCODE(StAsyncArResult)
DELEGATE_OPCODE(LdAFWHActRec)
DELEGATE_OPCODE(LdAsyncArParentChain)
DELEGATE_OPCODE(StAsyncArResume)
DELEGATE_OPCODE(IterInit)
DELEGATE_OPCODE(IterInitK)
DELEGATE_OPCODE(WIterInit)
DELEGATE_OPCODE(WIterInitK)
DELEGATE_OPCODE(MIterInit)
DELEGATE_OPCODE(MIterInitK)
DELEGATE_OPCODE(IterNext)
DELEGATE_OPCODE(IterNextK)
DELEGATE_OPCODE(WIterNext)
DELEGATE_OPCODE(WIterNextK)
DELEGATE_OPCODE(MIterNext)
DELEGATE_OPCODE(MIterNextK)
DELEGATE_OPCODE(IterFree)
DELEGATE_OPCODE(MIterFree)
DELEGATE_OPCODE(CIterFree)
DELEGATE_OPCODE(DecodeCufIter)
DELEGATE_OPCODE(CheckSurpriseFlags)
DELEGATE_OPCODE(CheckStackOverflow)
DELEGATE_OPCODE(CheckSurpriseFlagsEnter)
DELEGATE_OPCODE(CheckSurpriseAndStack)
#undef NOOP_OPCODE
#undef DELEGATE_OPCODE
///////////////////////////////////////////////////////////////////////////////
Vlabel CodeGenerator::label(Block* b) {
return irlower::label(m_state, b);
}
void CodeGenerator::emitFwdJcc(Vout& v, ConditionCode cc, Vreg sf,
Block* target) {
irlower::fwdJcc(v, m_state, cc, sf, target);
}
///////////////////////////////////////////////////////////////////////////////
void CodeGenerator::cgDefSP(IRInstruction* inst) {
auto const sp = dstLoc(inst, 0).reg();
auto& v = vmain();
if (inst->marker().resumed()) {
v << defvmsp{sp};
return;
}
auto const fp = srcLoc(inst, 0).reg();
v << lea{fp[-cellsToBytes(inst->extra<DefSP>()->offset.offset)], sp};
}
void CodeGenerator::cgCheckNullptr(IRInstruction* inst) {
if (!inst->taken()) return;
auto reg = srcLoc(inst, 0).reg(0);
auto& v = vmain();
auto const sf = v.makeReg();
v << testq{reg, reg, sf};
v << jcc{CC_NZ, sf, {label(inst->next()), label(inst->taken())}};
}
void CodeGenerator::cgCheckNonNull(IRInstruction* inst) {
auto srcReg = srcLoc(inst, 0).reg();
auto dstReg = dstLoc(inst, 0).reg();
auto taken = inst->taken();
assertx(taken);
auto& v = vmain();
auto const sf = v.makeReg();
v << testq{srcReg, srcReg, sf};
emitFwdJcc(v, CC_Z, sf, taken);
v << copy{srcReg, dstReg};
}
void CodeGenerator::cgAssertNonNull(IRInstruction* inst) {
auto& v = vmain();
auto srcReg = srcLoc(inst, 0).reg();
auto dstReg = dstLoc(inst, 0).reg();
if (RuntimeOption::EvalHHIRGenerateAsserts) {
auto const sf = v.makeReg();
v << testq{srcReg, srcReg, sf};
ifThen(v, CC_Z, sf, [&](Vout& v) {
v << ud2{};
});
}
v << copy{srcReg, dstReg};
}
void CodeGenerator::cgAssertType(IRInstruction* inst) {
copyTV(vmain(), srcLoc(inst, 0), dstLoc(inst, 0), inst->dst()->type());
}
void CodeGenerator::cgLdUnwinderValue(IRInstruction* inst) {
loadTV(vmain(), inst->dst(), dstLoc(inst, 0), rvmtl()[unwinderTVOff()]);
}
void CodeGenerator::cgBeginCatch(IRInstruction* inst) {
auto& v = vmain();
auto const callType = m_state.catch_calls[inst->block()];
always_assert(callType != CatchCall::Uninit &&
"Tried to emit BeginCatch with Uninit call type. "
"Catch blocks must be emitted after their predecessors.");
v << landingpad{callType == CatchCall::PHP};
emitIncStat(v, Stats::TC_CatchTrace);
}
void CodeGenerator::cgEndCatch(IRInstruction* inst) {
auto& v = vmain();
// endCatchHelper only expects rvmtl() and rvmfp() to be live.
v << jmpi{mcg->ustubs().endCatchHelper, rvmtl() | rvmfp()};
}
void CodeGenerator::cgUnwindCheckSideExit(IRInstruction* inst) {
auto& v = vmain();
auto const sf = v.makeReg();
v << cmpbim{0, rvmtl()[unwinderSideExitOff()], sf};
auto done = v.makeBlock();
v << jcc{CC_E, sf, {done, label(inst->taken())}};
v = done;
// doSideExit == true, so fall through to the side exit code
emitIncStat(v, Stats::TC_CatchSideExit);
}
//////////////////////////////////////////////////////////////////////
void CodeGenerator::cgHalt(IRInstruction* inst) {
vmain() << ud2{};
}
//////////////////////////////////////////////////////////////////////
void CodeGenerator::cgCallNative(Vout& v, IRInstruction* inst) {
return irlower::cgCallNative(v, m_state, inst);
}
CallDest CodeGenerator::callDest(Vreg reg0) const {
return irlower::callDest(reg0);
}
CallDest CodeGenerator::callDest(Vreg reg0, Vreg reg1) const {
return irlower::callDest(reg0, reg1);
}
CallDest CodeGenerator::callDest(const IRInstruction* inst) const {
return irlower::callDest(m_state, inst);
}
CallDest CodeGenerator::callDestTV(const IRInstruction* inst) const {
return irlower::callDestTV(m_state, inst);
}
CallDest CodeGenerator::callDestDbl(const IRInstruction* inst) const {
return irlower::callDestDbl(m_state, inst);
}
void CodeGenerator::cgCallHelper(Vout& v, CallSpec call,
const CallDest& dstInfo,
SyncOptions sync,
const ArgGroup& args) {
irlower::cgCallHelper(v, m_state, call, dstInfo, sync, args);
}
void CodeGenerator::cgMov(IRInstruction* inst) {
always_assert(inst->src(0)->numWords() == inst->dst(0)->numWords());
copyTV(vmain(), srcLoc(inst, 0), dstLoc(inst, 0), inst->dst()->type());
}
///////////////////////////////////////////////////////////////////////////////
void CodeGenerator::cgEqFunc(IRInstruction* inst) {
auto const dst = dstLoc(inst, 0).reg();
auto const src1 = srcLoc(inst, 0).reg();
auto const src2 = srcLoc(inst, 1).reg();
auto& v = vmain();
auto const sf = v.makeReg();
emitCmpLowPtr<Func>(v, sf, src2, src1);
v << setcc{CC_E, sf, dst};
}
void CodeGenerator::cgConvDblToInt(IRInstruction* inst) {
auto dstReg = dstLoc(inst, 0).reg();
auto srcReg = srcLoc(inst, 0).reg();
auto& v = vmain();
constexpr uint64_t maxULong = std::numeric_limits<unsigned long>::max();
constexpr uint64_t maxLong = std::numeric_limits<long>::max();
auto rIndef = v.cns(0x8000000000000000L);
auto dst1 = v.makeReg();
v << cvttsd2siq{srcReg, dst1};
auto const sf = v.makeReg();
v << cmpq{rIndef, dst1, sf};
unlikelyCond(v, vcold(), CC_E, sf, dstReg, [&](Vout& v) {
// result > max signed int or unordered
auto const sf = v.makeReg();
v << ucomisd{v.cns(0.0), srcReg, sf};
return cond(v, CC_NB, sf, v.makeReg(), [&](Vout& v) {
return dst1;
}, [&](Vout& v) {
// src0 > 0 (CF = 1 -> less than 0 or unordered)
return cond(v, CC_P, sf, v.makeReg(), [&](Vout& v) {
// PF = 1 -> unordered, i.e., we are doing an int cast of NaN. PHP5
// didn't formally define this, but observationally returns the
// truncated value (i.e., what dst1 currently holds). PHP7 formally
// defines this case to return 0.
if (RuntimeOption::PHP7_IntSemantics) {
return v.cns(0);
} else {
return dst1;
}
}, [&](Vout& v) {
auto const sf = v.makeReg();
v << ucomisd{v.cns(static_cast<double>(maxULong)), srcReg, sf};
return cond(v, CC_B, sf, v.makeReg(), [&](Vout& v) { // src0 > ULONG_MAX
return v.cns(0);
}, [&](Vout& v) {
// 0 < src0 <= ULONG_MAX
// we know that LONG_MAX < src0 <= UINT_MAX, therefore,
// 0 < src0 - ULONG_MAX <= LONG_MAX
auto tmp_sub = v.makeReg();
auto tmp_int = v.makeReg();
auto dst5 = v.makeReg();
v << subsd{v.cns(static_cast<double>(maxLong)), srcReg, tmp_sub};
v << cvttsd2siq{tmp_sub, tmp_int};
// We want to simulate integer overflow so we take the resulting
// integer and flip its sign bit (NB: we don't use orq here
// because it's possible that src0 == LONG_MAX in which case
// cvttsd2siq will yield an indefiniteInteger, which we would
// like to make zero)
v << xorq{rIndef, tmp_int, dst5, v.makeReg()};
return dst5;
});
});
});
}, [&](Vout& v) {
return dst1;
});
}
void CodeGenerator::cgConvDblToBool(IRInstruction* inst) {
auto dst = dstLoc(inst, 0).reg();
auto src = srcLoc(inst, 0).reg();
auto& v = vmain();
auto t1 = v.makeReg();
auto const sf = v.makeReg();
auto const movtdq_res = v.makeReg();
v << movtdq{src, movtdq_res};
v << shlqi{1, movtdq_res, t1, sf}; // 0.0 stays zero and -0.0 is now 0.0
v << setcc{CC_NE, sf, dst}; // lower byte becomes 1 if dstReg != 0
}
void CodeGenerator::cgConvIntToBool(IRInstruction* inst) {
auto dstReg = dstLoc(inst, 0).reg();
auto srcReg = srcLoc(inst, 0).reg();
auto& v = vmain();
auto const sf = v.makeReg();
v << testq{srcReg, srcReg, sf};
v << setcc{CC_NE, sf, dstReg};
}
void CodeGenerator::cgConvArrToBool(IRInstruction* inst) {
auto dstReg = dstLoc(inst, 0).reg();
auto srcReg = srcLoc(inst, 0).reg();
auto& v = vmain();
auto const sf = v.makeReg();
v << cmplim{0, srcReg[ArrayData::offsetofSize()], sf};
unlikelyCond(v, vcold(), CC_S, sf, dstReg,
[&](Vout& v) {
auto vsize = v.makeReg();
auto dst1 = v.makeReg();
cgCallHelper(v, CallSpec::method(&ArrayData::vsize),
callDest(vsize), SyncOptions::None,
argGroup(inst).ssa(0));
auto const sf = v.makeReg();
v << testl{vsize, vsize, sf};
v << setcc{CC_NZ, sf, dst1};
return dst1;
},
[&](Vout& v) {
auto dst2 = v.makeReg();
v << setcc{CC_NZ, sf, dst2};
return dst2;
}
);
}
void CodeGenerator::cgIsCol(IRInstruction* inst) {
assertx(inst->src(0)->type() <= TObj);
auto const rdst = dstLoc(inst, 0).reg();
auto const rsrc = srcLoc(inst, 0).reg();
auto& v = vmain();
auto const sf = v.makeReg();
v << testwim{ObjectData::IsCollection, rsrc[ObjectData::attributeOff()], sf};
v << setcc{CC_NE, sf, rdst};
}
void CodeGenerator::cgColIsEmpty(IRInstruction* inst) {
DEBUG_ONLY auto const ty = inst->src(0)->type();
assertx(ty < TObj &&
ty.clsSpec().cls() &&
ty.clsSpec().cls()->isCollectionClass());
auto& v = vmain();
auto const sf = v.makeReg();
v << cmplim{0, srcLoc(inst, 0).reg()[collections::FAST_SIZE_OFFSET], sf};
v << setcc{CC_E, sf, dstLoc(inst, 0).reg()};
}
void CodeGenerator::cgColIsNEmpty(IRInstruction* inst) {
DEBUG_ONLY auto const ty = inst->src(0)->type();
assertx(ty < TObj &&
ty.clsSpec().cls() &&
ty.clsSpec().cls()->isCollectionClass());
auto& v = vmain();
auto const sf = v.makeReg();
v << cmplim{0, srcLoc(inst, 0).reg()[collections::FAST_SIZE_OFFSET], sf};
v << setcc{CC_NE, sf, dstLoc(inst, 0).reg()};
}
void CodeGenerator::cgConvObjToBool(IRInstruction* inst) {
auto const rdst = dstLoc(inst, 0).reg();
auto const rsrc = srcLoc(inst, 0).reg();
auto& v = vmain();
auto const sf = v.makeReg();
v << testwim{ObjectData::CallToImpl, rsrc[ObjectData::attributeOff()], sf};
unlikelyCond(v, vcold(), CC_NZ, sf, rdst,
[&] (Vout& v) {
auto const sf = v.makeReg();
v << testwim{ObjectData::IsCollection, rsrc[ObjectData::attributeOff()],
sf};
return cond(v, CC_NZ, sf, v.makeReg(),
[&] (Vout& v) { // rsrc points to native collection
auto dst2 = v.makeReg();
auto const sf = v.makeReg();
v << cmplim{0, rsrc[collections::FAST_SIZE_OFFSET], sf};
v << setcc{CC_NE, sf, dst2}; // true iff size not zero
return dst2;
}, [&] (Vout& v) { // rsrc is not a native collection
auto dst3 = v.makeReg();
cgCallHelper(v,
CallSpec::method(&ObjectData::toBoolean),
CallDest{DestType::Byte, dst3},
SyncOptions::Sync,
argGroup(inst).ssa(0));
return dst3;
});
}, [&] (Vout& v) {
return v.cns(true);
}
);
}
void CodeGenerator::cgConvStrToBool(IRInstruction* inst) {
auto const dst = dstLoc(inst, 0).reg();
auto const src = srcLoc(inst, 0).reg();
auto& v = vmain();
auto const sf = v.makeReg();
v << cmplim{1, src[StringData::sizeOff()], sf};
unlikelyCond(v, vcold(), CC_E, sf, dst, [&] (Vout& v) {
// Unlikely case is we end up having to check whether the first byte of the
// string is equal to '0'.
auto const dst = v.makeReg();
auto const sf = v.makeReg();
#ifdef NO_M_DATA
v << cmpbim{'0', src[sizeof(StringData)], sf};
#else
auto const sd = v.makeReg();
v << load{src[StringData::dataOff()], sd};
v << cmpbim{'0', sd[0], sf};
#endif
v << setcc{CC_NE, sf, dst};
return dst;
}, [&] (Vout& v) {
// Common case is we have an empty string or a string with size bigger than
// one.
auto const dst = v.makeReg();
v << setcc{CC_G, sf, dst};
return dst;
});
}
void CodeGenerator::emitConvBoolOrIntToDbl(IRInstruction* inst) {
SSATmp* src = inst->src(0);
assertx(src->isA(TBool) || src->isA(TInt));
auto dstReg = dstLoc(inst, 0).reg();
auto srcReg = srcLoc(inst, 0).reg();
// cvtsi2sd doesn't modify the high bits of its target, which can
// cause false dependencies to prevent register renaming from kicking
// in. Break the dependency chain by zeroing out the XMM reg.
auto& v = vmain();
auto s2 = zeroExtendIfBool(v, src, srcReg);
v << cvtsi2sd{s2, dstReg};