-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
LowerMD.cpp
7736 lines (6574 loc) · 283 KB
/
LowerMD.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#include "Language/JavascriptFunctionArgIndex.h"
const Js::OpCode LowererMD::MDUncondBranchOpcode = Js::OpCode::B;
const Js::OpCode LowererMD::MDMultiBranchOpcode = Js::OpCode::BX;
const Js::OpCode LowererMD::MDTestOpcode = Js::OpCode::TST;
const Js::OpCode LowererMD::MDOrOpcode = Js::OpCode::ORR;
const Js::OpCode LowererMD::MDXorOpcode = Js::OpCode::EOR;
const Js::OpCode LowererMD::MDOverflowBranchOpcode = Js::OpCode::BVS;
const Js::OpCode LowererMD::MDNotOverflowBranchOpcode = Js::OpCode::BVC;
const Js::OpCode LowererMD::MDConvertFloat32ToFloat64Opcode = Js::OpCode::VCVTF64F32;
const Js::OpCode LowererMD::MDConvertFloat64ToFloat32Opcode = Js::OpCode::VCVTF32F64;
const Js::OpCode LowererMD::MDCallOpcode = Js::OpCode::Call;
const Js::OpCode LowererMD::MDImulOpcode = Js::OpCode::MUL;
const Js::OpCode LowererMD::MDLea = Js::OpCode::LEA;
template<typename T>
inline void Swap(T& x, T& y)
{
T temp = x;
x = y;
y = temp;
}
// Static utility fn()
//
bool
LowererMD::IsAssign(const IR::Instr *instr)
{
return (instr->m_opcode == Js::OpCode::MOV ||
instr->m_opcode == Js::OpCode::VMOV ||
instr->m_opcode == Js::OpCode::LDIMM ||
instr->m_opcode == Js::OpCode::LDR ||
instr->m_opcode == Js::OpCode::VLDR ||
instr->m_opcode == Js::OpCode::VLDR32 ||
instr->m_opcode == Js::OpCode::STR ||
instr->m_opcode == Js::OpCode::VSTR ||
instr->m_opcode == Js::OpCode::VSTR32);
}
///----------------------------------------------------------------------------
///
/// LowererMD::IsCall
///
///----------------------------------------------------------------------------
bool
LowererMD::IsCall(const IR::Instr *instr)
{
return (instr->m_opcode == Js::OpCode::BL ||
instr->m_opcode == Js::OpCode::BLX);
}
///----------------------------------------------------------------------------
///
/// LowererMD::IsIndirectBranch
///
///----------------------------------------------------------------------------
bool
LowererMD::IsIndirectBranch(const IR::Instr *instr)
{
return (instr->m_opcode == Js::OpCode::BX);
}
///----------------------------------------------------------------------------
///
/// LowererMD::IsUnconditionalBranch
///
///----------------------------------------------------------------------------
bool
LowererMD::IsUnconditionalBranch(const IR::Instr *instr)
{
return instr->m_opcode == Js::OpCode::B;
}
bool
LowererMD::IsReturnInstr(const IR::Instr *instr)
{
return instr->m_opcode == Js::OpCode::LDRRET || instr->m_opcode == Js::OpCode::RET;
}
///----------------------------------------------------------------------------
///
/// LowererMD::InvertBranch
///
///----------------------------------------------------------------------------
void
LowererMD::InvertBranch(IR::BranchInstr *branchInstr)
{
switch (branchInstr->m_opcode)
{
case Js::OpCode::BEQ:
branchInstr->m_opcode = Js::OpCode::BNE;
break;
case Js::OpCode::BNE:
branchInstr->m_opcode = Js::OpCode::BEQ;
break;
case Js::OpCode::BGE:
branchInstr->m_opcode = Js::OpCode::BLT;
break;
case Js::OpCode::BGT:
branchInstr->m_opcode = Js::OpCode::BLE;
break;
case Js::OpCode::BLT:
branchInstr->m_opcode = Js::OpCode::BGE;
break;
case Js::OpCode::BLE:
branchInstr->m_opcode = Js::OpCode::BGT;
break;
case Js::OpCode::BCS:
branchInstr->m_opcode = Js::OpCode::BCC;
break;
case Js::OpCode::BCC:
branchInstr->m_opcode = Js::OpCode::BCS;
break;
case Js::OpCode::BMI:
branchInstr->m_opcode = Js::OpCode::BPL;
break;
case Js::OpCode::BPL:
branchInstr->m_opcode = Js::OpCode::BMI;
break;
case Js::OpCode::BVS:
branchInstr->m_opcode = Js::OpCode::BVC;
break;
case Js::OpCode::BVC:
branchInstr->m_opcode = Js::OpCode::BVS;
break;
case Js::OpCode::BLS:
branchInstr->m_opcode = Js::OpCode::BHI;
break;
case Js::OpCode::BHI:
branchInstr->m_opcode = Js::OpCode::BLS;
break;
default:
AssertMsg(UNREACHED, "B missing in InvertBranch()");
}
}
Js::OpCode
LowererMD::MDConvertFloat64ToInt32Opcode(const RoundMode roundMode)
{
switch (roundMode)
{
case RoundModeTowardZero:
return Js::OpCode::VCVTS32F64;
case RoundModeTowardInteger:
return Js::OpCode::Nop;
case RoundModeHalfToEven:
return Js::OpCode::VCVTRS32F64;
default:
AssertMsg(0, "RoundMode has no MD mapping.");
return Js::OpCode::Nop;
}
}
// GenerateMemRef: Return an opnd that can be used to access the given address.
// ARM can't encode direct accesses to physical addresses, so put the address in a register
// and return an indir. (This facilitates re-use of the loaded address without having to re-load it.)
IR::Opnd *
LowererMD::GenerateMemRef(intptr_t addr, IRType type, IR::Instr *instr, bool dontEncode)
{
IR::RegOpnd *baseOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
IR::AddrOpnd *addrOpnd = IR::AddrOpnd::New(addr, IR::AddrOpndKindDynamicMisc, this->m_func, dontEncode);
Lowerer::InsertMove(baseOpnd, addrOpnd, instr);
return IR::IndirOpnd::New(baseOpnd, 0, type, this->m_func);
}
void
LowererMD::FlipHelperCallArgsOrder()
{
int left = 0;
int right = helperCallArgsCount - 1;
while (left < right)
{
IR::Opnd *tempOpnd = helperCallArgs[left];
helperCallArgs[left] = helperCallArgs[right];
helperCallArgs[right] = tempOpnd;
left++;
right--;
}
}
IR::Instr *
LowererMD::LowerCallHelper(IR::Instr *instrCall)
{
IR::Opnd *argOpnd = instrCall->UnlinkSrc2();
IR::Instr *prevInstr = instrCall;
IR::JnHelperMethod helperMethod = instrCall->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
instrCall->FreeSrc1();
while (argOpnd)
{
Assert(argOpnd->IsRegOpnd());
IR::RegOpnd *regArg = argOpnd->AsRegOpnd();
Assert(regArg->m_sym->m_isSingleDef);
IR::Instr *instrArg = regArg->m_sym->m_instrDef;
Assert(instrArg->m_opcode == Js::OpCode::ArgOut_A ||
(helperMethod == IR::JnHelperMethod::HelperOP_InitCachedScope && instrArg->m_opcode == Js::OpCode::ExtendArg_A) ||
(helperMethod == IR::JnHelperMethod::HelperScrFunc_OP_NewScFuncHomeObj && instrArg->m_opcode == Js::OpCode::ExtendArg_A) ||
(helperMethod == IR::JnHelperMethod::HelperScrFunc_OP_NewScGenFuncHomeObj && instrArg->m_opcode == Js::OpCode::ExtendArg_A));
prevInstr = this->LoadHelperArgument(prevInstr, instrArg->GetSrc1());
argOpnd = instrArg->GetSrc2();
if (instrArg->m_opcode == Js::OpCode::ArgOut_A)
{
instrArg->UnlinkSrc1();
if (argOpnd)
{
instrArg->UnlinkSrc2();
}
regArg->Free(this->m_func);
instrArg->Remove();
}
else if (instrArg->m_opcode == Js::OpCode::ExtendArg_A)
{
if (instrArg->GetSrc1()->IsRegOpnd())
{
m_lowerer->addToLiveOnBackEdgeSyms->Set(instrArg->GetSrc1()->AsRegOpnd()->GetStackSym()->m_id);
}
}
}
switch (helperMethod)
{
case IR::JnHelperMethod::HelperScrFunc_OP_NewScFuncHomeObj:
case IR::JnHelperMethod::HelperScrFunc_OP_NewScGenFuncHomeObj:
break;
default:
prevInstr = m_lowerer->LoadScriptContext(prevInstr);
break;
}
this->FlipHelperCallArgsOrder();
return this->ChangeToHelperCall(instrCall, helperMethod);
}
// Lower a call: May be either helper or native JS call. Just set the opcode, and
// put the result into the return register. (No stack adjustment required.)
IR::Instr *
LowererMD::LowerCall(IR::Instr * callInstr, Js::ArgSlot argCount)
{
IR::Instr *retInstr = callInstr;
IR::Opnd *targetOpnd = callInstr->GetSrc1();
AssertMsg(targetOpnd, "Call without a target?");
// This is required here due to calls created during lowering
callInstr->m_func->SetHasCallsOnSelfAndParents();
if (targetOpnd->IsRegOpnd())
{
// Indirect call
callInstr->m_opcode = Js::OpCode::BLX;
}
else
{
AssertMsg(targetOpnd->IsHelperCallOpnd(), "Why haven't we loaded the call target?");
// Direct call
//
// load the address into a register because we cannot directly access more than 24 bit constants
// in BL instruction. Non helper call methods will already be accessed indirectly.
//
// Skip this for bailout calls. The register allocator will lower that as appropriate, without affecting spill choices.
if (!callInstr->HasBailOutInfo())
{
IR::RegOpnd *regOpnd = IR::RegOpnd::New(nullptr, RegLR, TyMachPtr, this->m_func);
IR::Instr *movInstr = IR::Instr::New(Js::OpCode::LDIMM, regOpnd, callInstr->GetSrc1(), this->m_func);
regOpnd->m_isCallArg = true;
callInstr->UnlinkSrc1();
callInstr->SetSrc1(regOpnd);
callInstr->InsertBefore(movInstr);
}
callInstr->m_opcode = Js::OpCode::BLX;
}
// For the sake of the prolog/epilog, note that we're not in a leaf. (Deliberately not
// overloading Func::m_isLeaf here, as that's used for other purposes.)
this->m_func->m_unwindInfo.SetHasCalls(true);
IR::Opnd *dstOpnd = callInstr->GetDst();
if (dstOpnd)
{
IR::Instr * movInstr;
if(dstOpnd->IsFloat64())
{
movInstr = callInstr->SinkDst(Js::OpCode::VMOV);
callInstr->GetDst()->AsRegOpnd()->SetReg(RETURN_DBL_REG);
movInstr->GetSrc1()->AsRegOpnd()->SetReg(RETURN_DBL_REG);
retInstr = movInstr;
}
else
{
movInstr = callInstr->SinkDst(Js::OpCode::MOV);
callInstr->GetDst()->AsRegOpnd()->SetReg(RETURN_REG);
movInstr->GetSrc1()->AsRegOpnd()->SetReg(RETURN_REG);
retInstr = movInstr;
}
}
//
// assign the arguments to appropriate positions
//
AssertMsg(this->helperCallArgsCount >= 0, "Fatal. helper call arguments ought to be positive");
AssertMsg(this->helperCallArgsCount <= MaxArgumentsToHelper, "Too many helper call arguments");
uint16 argsLeft = this->helperCallArgsCount;
uint16 doubleArgsLeft = this->helperCallDoubleArgsCount;
uint16 intArgsLeft = argsLeft - doubleArgsLeft;
while(argsLeft > 0)
{
IR::Opnd *helperArgOpnd = this->helperCallArgs[this->helperCallArgsCount - argsLeft];
IR::Opnd * opndParam = nullptr;
if (helperArgOpnd->IsFloat())
{
opndParam = this->GetOpndForArgSlot(doubleArgsLeft - 1, helperArgOpnd);
AssertMsg(opndParam->IsRegOpnd(), "NYI for other kind of operands");
--doubleArgsLeft;
}
else
{
opndParam = this->GetOpndForArgSlot(intArgsLeft - 1, helperArgOpnd);
--intArgsLeft;
}
Lowerer::InsertMove(opndParam, helperArgOpnd, callInstr);
--argsLeft;
}
Assert(doubleArgsLeft == 0 && intArgsLeft == 0 && argsLeft == 0);
// We're done with the args (if any) now, so clear the param location state.
this->FinishArgLowering();
return retInstr;
}
IR::Instr *
LowererMD::LoadDynamicArgument(IR::Instr *instr, uint argNumber)
{
Assert(instr->m_opcode == Js::OpCode::ArgOut_A_Dynamic);
Assert(instr->GetSrc2() == nullptr);
IR::Opnd* dst = GetOpndForArgSlot((Js::ArgSlot) (argNumber - 1));
instr->SetDst(dst);
instr->m_opcode = Js::OpCode::MOV;
LegalizeMD::LegalizeInstr(instr);
return instr;
}
IR::Instr *
LowererMD::LoadDynamicArgumentUsingLength(IR::Instr *instr)
{
Assert(instr->m_opcode == Js::OpCode::ArgOut_A_Dynamic);
IR::RegOpnd* src2 = instr->UnlinkSrc2()->AsRegOpnd();
IR::Instr *add = IR::Instr::New(Js::OpCode::SUB, IR::RegOpnd::New(TyInt32, this->m_func), src2, IR::IntConstOpnd::New(1, TyInt8, this->m_func), this->m_func);
instr->InsertBefore(add);
//We need store nth actuals, so stack location is after function object, callinfo & this pointer
IR::RegOpnd *stackPointer = IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func);
IR::IndirOpnd *actualsLocation = IR::IndirOpnd::New(stackPointer, add->GetDst()->AsRegOpnd(), GetDefaultIndirScale(), TyMachReg, this->m_func);
instr->SetDst(actualsLocation);
instr->m_opcode = Js::OpCode::LDR;
LegalizeMD::LegalizeInstr(instr);
return instr;
}
void
LowererMD::SetMaxArgSlots(Js::ArgSlot actualCount /*including this*/)
{
Js::ArgSlot offset = 3;//For function object & callInfo & this
if (this->m_func->m_argSlotsForFunctionsCalled < (uint32) (actualCount + offset))
{
this->m_func->m_argSlotsForFunctionsCalled = (uint32)(actualCount + offset);
}
return;
}
void
LowererMD::GenerateMemInit(IR::RegOpnd * opnd, int32 offset, size_t value, IR::Instr * insertBeforeInstr, bool isZeroed)
{
m_lowerer->GenerateMemInit(opnd, offset, (uint32)value, insertBeforeInstr, isZeroed);
}
IR::Instr *
LowererMD::LowerCallIDynamic(IR::Instr *callInstr, IR::Instr*saveThisArgOutInstr, IR::Opnd *argsLength, ushort callFlags, IR::Instr * insertBeforeInstrForCFG)
{
callInstr->InsertBefore(saveThisArgOutInstr); //Move this Argout next to call;
this->LoadDynamicArgument(saveThisArgOutInstr, 3); //this pointer is the 3rd argument
//callInfo
if (callInstr->m_func->IsInlinee())
{
Assert(argsLength->AsIntConstOpnd()->GetValue() == callInstr->m_func->actualCount);
this->SetMaxArgSlots((Js::ArgSlot)callInstr->m_func->actualCount);
}
else
{
callInstr->InsertBefore(IR::Instr::New(Js::OpCode::ADD, argsLength, argsLength, IR::IntConstOpnd::New(1, TyInt8, this->m_func), this->m_func));
this->SetMaxArgSlots(Js::InlineeCallInfo::MaxInlineeArgoutCount);
}
Lowerer::InsertMove( this->GetOpndForArgSlot(1), argsLength, callInstr);
IR::RegOpnd *funcObjOpnd = callInstr->UnlinkSrc1()->AsRegOpnd();
GeneratePreCall(callInstr, funcObjOpnd);
// functionOpnd is the first argument.
IR::Opnd * opndParam = this->GetOpndForArgSlot(0);
Lowerer::InsertMove(opndParam, funcObjOpnd, callInstr);
return this->LowerCall(callInstr, 0);
}
void
LowererMD::GenerateFunctionObjectTest(IR::Instr * callInstr, IR::RegOpnd *functionObjOpnd, bool isHelper, IR::LabelInstr* continueAfterExLabel /* = nullptr */)
{
AssertMsg(!m_func->IsJitInDebugMode() || continueAfterExLabel, "When jit is in debug mode, continueAfterExLabel must be provided otherwise continue after exception may cause AV.");
if (!functionObjOpnd->IsNotTaggedValue())
{
IR::Instr * insertBeforeInstr = callInstr;
// Need check and error if we are calling a tagged int.
if (!functionObjOpnd->IsTaggedInt())
{
// TST functionObjOpnd, 1
IR::Instr * instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
instr->SetSrc1(functionObjOpnd);
instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func));
callInstr->InsertBefore(instr);
// BNE $helper
// B $callLabel
IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
instr = IR::BranchInstr::New(Js::OpCode::BNE, helperLabel, this->m_func);
callInstr->InsertBefore(instr);
IR::LabelInstr * callLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
instr = IR::BranchInstr::New(Js::OpCode::B, callLabel, this->m_func);
callInstr->InsertBefore(instr);
callInstr->InsertBefore(helperLabel);
callInstr->InsertBefore(callLabel);
insertBeforeInstr = callLabel;
}
this->m_lowerer->GenerateRuntimeError(insertBeforeInstr, JSERR_NeedFunction);
if (continueAfterExLabel)
{
// Under debugger the RuntimeError (exception) can be ignored, generate branch right after RunTimeError instr
// to jmp to a safe place (which would normally be debugger bailout check).
IR::BranchInstr* continueAfterEx = IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, continueAfterExLabel, this->m_func);
insertBeforeInstr->InsertBefore(continueAfterEx);
}
}
}
IR::Instr*
LowererMD::GeneratePreCall(IR::Instr * callInstr, IR::Opnd *functionObjOpnd)
{
IR::RegOpnd * functionTypeRegOpnd = nullptr;
// For calls to fixed functions we load the function's type directly from the known (hard-coded) function object address.
// For other calls, we need to load it from the function object stored in a register operand.
if (functionObjOpnd->IsAddrOpnd() && functionObjOpnd->AsAddrOpnd()->m_isFunction)
{
functionTypeRegOpnd = this->m_lowerer->GenerateFunctionTypeFromFixedFunctionObject(callInstr, functionObjOpnd);
}
else if (functionObjOpnd->IsRegOpnd())
{
AssertMsg(functionObjOpnd->AsRegOpnd()->m_sym->IsStackSym(), "Expected call target to be stackSym");
functionTypeRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
IR::IndirOpnd* functionTypeIndirOpnd = IR::IndirOpnd::New(functionObjOpnd->AsRegOpnd(),
Js::RecyclableObject::GetOffsetOfType(), TyMachReg, this->m_func);
Lowerer::InsertMove(functionTypeRegOpnd, functionTypeIndirOpnd, callInstr);
}
else
{
AssertMsg(false, "Unexpected call target operand type.");
}
int entryPointOffset = Js::Type::GetOffsetOfEntryPoint();
IR::IndirOpnd* entryPointOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, entryPointOffset, TyMachPtr, this->m_func);
IR::RegOpnd * targetAddrOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
IR::Instr * stackParamInsert = Lowerer::InsertMove(targetAddrOpnd, entryPointOpnd, callInstr);
// targetAddrOpnd is the address we'll call.
callInstr->SetSrc1(targetAddrOpnd);
return stackParamInsert;
}
IR::Instr *
LowererMD::LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper, IR::Instr * insertBeforeInstrForCFG)
{
// Indirect call using JS calling convention:
// R0 = callee func object
// R1 = callinfo
// R2 = arg0 ("this")
// R3 = arg1
// [sp] = arg2
// etc.
// First load the target address. Note that we want to wind up with this:
// ...
// [sp+4] = arg3
// [sp] = arg2
// load target addr from func obj
// R3 = arg1
// ...
// R0 = func obj
// BLX target addr
// This way the register containing the target addr interferes with the param regs
// only, not the regs we use to store params to the stack.
// We're sinking the stores of stack params so that the call sequence is contiguous.
// This is required by nested calls, since each call will re-use the same stack slots.
// But if there is no nesting, stack params can be stored as soon as they're computed.
IR::Opnd * functionObjOpnd = callInstr->UnlinkSrc1();
// If this is a call for new, we already pass the function operand through NewScObject,
// which checks if the function operand is a real function or not, don't need to add a check again.
// If this is a call to a fixed function, we've already verified that the target is, indeed, a function.
if (callInstr->m_opcode != Js::OpCode::CallIFixed && !(callFlags & Js::CallFlags_New))
{
IR::LabelInstr* continueAfterExLabel = Lowerer::InsertContinueAfterExceptionLabelForDebugger(m_func, callInstr, isHelper);
GenerateFunctionObjectTest(callInstr, functionObjOpnd->AsRegOpnd(), isHelper, continueAfterExLabel);
// TODO: Remove unreachable code if we have proved that it is a tagged in.
}
// Can't assert until we remove unreachable code if we have proved that it is a tagged int.
// Assert((callFlags & Js::CallFlags_New) || !functionWrapOpnd->IsTaggedInt());
IR::Instr * stackParamInsert = GeneratePreCall(callInstr, functionObjOpnd);
// We need to get the calculated CallInfo in SimpleJit because that doesn't include any changes for stack alignment
IR::IntConstOpnd *callInfo;
int32 argCount = this->LowerCallArgs(callInstr, stackParamInsert, callFlags, 1, &callInfo);
// functionObjOpnd is the first argument.
IR::Opnd * opndParam = this->GetOpndForArgSlot(0);
Lowerer::InsertMove(opndParam, functionObjOpnd, callInstr);
IR::Opnd *const finalDst = callInstr->GetDst();
// Finally, lower the call instruction itself.
IR::Instr* ret = this->LowerCall(callInstr, (Js::ArgSlot)argCount);
IR::AutoReuseOpnd autoReuseSavedFunctionObjOpnd;
if (callInstr->IsJitProfilingInstr())
{
Assert(callInstr->m_func->IsSimpleJit());
Assert(!CONFIG_FLAG(NewSimpleJit));
if(finalDst &&
finalDst->IsRegOpnd() &&
functionObjOpnd->IsRegOpnd() &&
finalDst->AsRegOpnd()->m_sym == functionObjOpnd->AsRegOpnd()->m_sym)
{
// The function object sym is going to be overwritten, so save it in a temp for profiling
IR::RegOpnd *const savedFunctionObjOpnd = IR::RegOpnd::New(functionObjOpnd->GetType(), callInstr->m_func);
autoReuseSavedFunctionObjOpnd.Initialize(savedFunctionObjOpnd, callInstr->m_func);
Lowerer::InsertMove(savedFunctionObjOpnd, functionObjOpnd, callInstr->m_next);
functionObjOpnd = savedFunctionObjOpnd;
}
auto instr = callInstr->AsJitProfilingInstr();
ret = this->m_lowerer->GenerateCallProfiling(
instr->profileId,
instr->inlineCacheIndex,
instr->GetDst(),
functionObjOpnd,
callInfo,
instr->isProfiledReturnCall,
callInstr,
ret);
}
return ret;
}
int32
LowererMD::LowerCallArgs(IR::Instr *callInstr, IR::Instr *stackParamInsert, ushort callFlags, Js::ArgSlot extraParams, IR::IntConstOpnd **callInfoOpndRef)
{
AssertMsg(this->helperCallArgsCount == 0, "We don't support nested helper calls yet");
uint32 argCount = 0;
IR::Opnd* opndParam;
// Now walk the user arguments and remember the arg count.
IR::Instr * argInstr = callInstr;
IR::Opnd *src2Opnd = callInstr->UnlinkSrc2();
while (src2Opnd->IsSymOpnd())
{
// Get the arg instr
IR::SymOpnd * argLinkOpnd = src2Opnd->AsSymOpnd();
StackSym * argLinkSym = argLinkOpnd->m_sym->AsStackSym();
AssertMsg(argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
argLinkOpnd->Free(this->m_func);
argInstr = argLinkSym->m_instrDef;
// The arg sym isn't assigned a constant directly anymore
argLinkSym->m_isConst = false;
argLinkSym->m_isIntConst = false;
argLinkSym->m_isTaggableIntConst = false;
// The arg slot nums are 1-based, so subtract 1. Then add 1 for the non-user args (callinfo).
auto argSlotNum = argLinkSym->GetArgSlotNum();
if(argSlotNum + extraParams < argSlotNum)
{
Js::Throw::OutOfMemory();
}
opndParam = this->GetOpndForArgSlot(argSlotNum + extraParams);
src2Opnd = argInstr->UnlinkSrc2();
argInstr->ReplaceDst(opndParam);
argInstr->Unlink();
if (opndParam->IsRegOpnd())
{
callInstr->InsertBefore(argInstr);
}
else
{
stackParamInsert->InsertBefore(argInstr);
}
this->ChangeToAssign(argInstr);
argCount++;
}
IR::RegOpnd * argLinkOpnd = src2Opnd->AsRegOpnd();
StackSym *argLinkSym = argLinkOpnd->m_sym->AsStackSym();
AssertMsg(!argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
IR::Instr *startCallInstr = argLinkSym->m_instrDef;
AssertMsg(startCallInstr->m_opcode == Js::OpCode::StartCall || startCallInstr->m_opcode == Js::OpCode::LoweredStartCall, "Problem with arg chain.");
AssertMsg(startCallInstr->GetArgOutCount(/*getInterpreterArgOutCount*/ false) == argCount,
"ArgCount doesn't match StartCall count");
// Deal with the SC.
this->LowerStartCall(startCallInstr);
// Second argument is the callinfo.
IR::IntConstOpnd *opndCallInfo = Lowerer::MakeCallInfoConst(callFlags, argCount, m_func);
if(callInfoOpndRef)
{
opndCallInfo->Use(m_func);
*callInfoOpndRef = opndCallInfo;
}
opndParam = this->GetOpndForArgSlot(extraParams);
Lowerer::InsertMove(opndParam, opndCallInfo, callInstr);
return argCount + 1 + extraParams; // + 1 for call flags
}
IR::Instr *
LowererMD::LowerStartCall(IR::Instr * instr)
{
// StartCall doesn't need to generate a stack adjustment. Just delete it.
instr->m_opcode = Js::OpCode::LoweredStartCall;
return instr;
}
IR::Instr *
LowererMD::LoadHelperArgument(IR::Instr * instr, IR::Opnd * opndArgValue)
{
// Load the given parameter into the appropriate location.
// We update the current param state so we can do this work without making the caller
// do the work.
Assert(this->helperCallArgsCount < LowererMD::MaxArgumentsToHelper);
__analysis_assume(this->helperCallArgsCount < MaxArgumentsToHelper);
helperCallArgs[helperCallArgsCount++] = opndArgValue;
if (opndArgValue->GetType() == TyMachDouble)
{
this->helperCallDoubleArgsCount++;
}
return instr;
}
void
LowererMD::FinishArgLowering()
{
this->helperCallArgsCount = 0;
this->helperCallDoubleArgsCount = 0;
}
IR::Opnd *
LowererMD::GetOpndForArgSlot(Js::ArgSlot argSlot, IR::Opnd * argOpnd)
{
IR::Opnd * opndParam = nullptr;
IRType type = argOpnd ? argOpnd->GetType() : TyMachReg;
if (argOpnd == nullptr || !argOpnd->IsFloat())
{
if (argSlot < NUM_INT_ARG_REGS)
{
// Return an instance of the next arg register.
IR::RegOpnd *regOpnd;
regOpnd = IR::RegOpnd::New(nullptr, (RegNum)(argSlot + FIRST_INT_ARG_REG), type, this->m_func);
regOpnd->m_isCallArg = true;
opndParam = regOpnd;
}
else
{
// Create a stack slot reference and bump up the size of this function's outgoing param area,
// if necessary.
argSlot = argSlot - NUM_INT_ARG_REGS;
IntConstType offset = argSlot * MachRegInt;
IR::RegOpnd * spBase = IR::RegOpnd::New(nullptr, this->GetRegStackPointer(), TyMachReg, this->m_func);
opndParam = IR::IndirOpnd::New(spBase, offset, type, this->m_func);
if (this->m_func->m_argSlotsForFunctionsCalled < (uint32)(argSlot + 1))
{
this->m_func->m_argSlotsForFunctionsCalled = argSlot + 1;
}
}
}
else
{
if (argSlot < MaxDoubleArgumentsToHelper)
{
// Return an instance of the next arg register.
IR::RegOpnd *regOpnd;
regOpnd = IR::RegOpnd::New(nullptr, (RegNum)(argSlot + FIRST_DOUBLE_ARG_REG), type, this->m_func);
regOpnd->m_isCallArg = true;
opndParam = regOpnd;
}
else
{
AssertMsg(false,"More than 8 double parameter passing disallowed");
}
}
return opndParam;
}
IR::Instr *
LowererMD::LoadDoubleHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
{
// Load the given parameter into the appropriate location.
// We update the current param state so we can do this work without making the caller
// do the work.
Assert(opndArg->GetType() == TyMachDouble);
return this->LoadHelperArgument(instr, opndArg);
}
void
LowererMD::GenerateStackProbe(IR::Instr *insertInstr, bool afterProlog)
{
//
// Generate a stack overflow check. This can be as simple as a cmp esp, const
// because this function is guaranteed to be called on its base thread only.
// If the check fails call ThreadContext::ProbeCurrentStack which will check again and must throw.
//
// LDIMM r12, ThreadContext::scriptStackLimit + frameSize //Load to register first, as this can be more than 12 bit supported in CMP
// CMP sp, r12
// BGT done
// begin:
// LDIMM r0, frameSize
// LDIMM r1, scriptContext
// LDIMM r2, ThreadContext::ProbeCurrentStack //MUST THROW
// BLX r2 //BX r2 if the stackprobe is before prolog
// done:
//
// For thread context with script interrupt enabled:
// LDIMM r12, &ThreadContext::scriptStackLimitForCurrentThread
// LDR r12, [r12]
// ADD r12, frameSize
// BVS $helper
// CMP sp, r12
// BGT done
// $helper:
// LDIMM r0, frameSize
// LDIMM r1, scriptContext
// LDIMM r2, ThreadContext::ProbeCurrentStack //MUST THROW
// BLX r2 //BX r2 if the stackprobe is before prolog
// done:
//
//m_localStackHeight for ARM contains (m_argSlotsForFunctionsCalled * MachPtr)
uint32 frameSize = this->m_func->m_localStackHeight + Js::Constants::MinStackJIT;
IR::RegOpnd *scratchOpnd = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
IR::LabelInstr *helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, afterProlog);
IR::Instr *instr;
bool doInterruptProbe = m_func->GetJITFunctionBody()->DoInterruptProbe();
if (doInterruptProbe || !m_func->GetThreadContextInfo()->IsThreadBound())
{
// Load the current stack limit and add the current frame allocation.
{
intptr_t pLimit = m_func->GetThreadContextInfo()->GetThreadStackLimitAddr();
Lowerer::InsertMove(scratchOpnd, IR::AddrOpnd::New(pLimit, IR::AddrOpndKindDynamicMisc, this->m_func), insertInstr);
Lowerer::InsertMove(scratchOpnd, IR::IndirOpnd::New(scratchOpnd, 0, TyMachReg, this->m_func), insertInstr);
}
if (EncoderMD::CanEncodeModConst12(frameSize))
{
// If the frame size is small enough, just add the constant.
// Does this ever happen with the size of the MinStackJIT constant?
instr = IR::Instr::New(Js::OpCode::ADDS, scratchOpnd, scratchOpnd,
IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func), this->m_func);
insertInstr->InsertBefore(instr);
}
else
{
// We need a second scratch reg.
// If we're probing after the prolog, the reg has already been saved and will be restored.
// If not, push and pop it here, knowing that we'll never throw while the stack is whacked.
Assert(!afterProlog || this->m_func->m_unwindInfo.GetSavedScratchReg());
BVUnit scratchBit;
IR::Opnd *opnd;
if (!afterProlog)
{
opnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func);
instr = IR::Instr::New(Js::OpCode::PUSH, opnd, this->m_func);
scratchBit.Set(RegEncode[SP_ALLOC_SCRATCH_REG]);
opnd = IR::RegBVOpnd::New(scratchBit, TyMachReg, this->m_func);
instr->SetSrc1(opnd);
insertInstr->InsertBefore(instr);
}
IR::Opnd *scratchOpnd2 = IR::RegOpnd::New(nullptr, SP_ALLOC_SCRATCH_REG, TyMachReg, this->m_func);
Lowerer::InsertMove(scratchOpnd2, IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func), insertInstr);
instr = IR::Instr::New(Js::OpCode::ADDS, scratchOpnd, scratchOpnd, scratchOpnd2, this->m_func);
insertInstr->InsertBefore(instr);
if (!afterProlog)
{
Assert(scratchBit.Test(RegEncode[SP_ALLOC_SCRATCH_REG]));
opnd = IR::RegBVOpnd::New(scratchBit, TyMachReg, this->m_func);
instr = IR::Instr::New(Js::OpCode::POP, opnd, this->m_func);
opnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func);
instr->SetSrc1(opnd);
insertInstr->InsertBefore(instr);
}
}
// If this add overflows, we have to call the helper.
instr = IR::BranchInstr::New(Js::OpCode::BVS, helperLabel, this->m_func);
insertInstr->InsertBefore(instr);
}
else
{
uint32 scriptStackLimit = (uint32)m_func->GetThreadContextInfo()->GetScriptStackLimit();
IR::Opnd *stackLimitOpnd = IR::IntConstOpnd::New(frameSize + scriptStackLimit, TyMachReg, this->m_func);
Lowerer::InsertMove(scratchOpnd, stackLimitOpnd, insertInstr);
}
IR::LabelInstr *doneLabelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, false);
if (!IS_FAULTINJECT_STACK_PROBE_ON) // Do stack check fastpath only if not doing StackProbe fault injection
{
instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
instr->SetSrc1(IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func));
instr->SetSrc2(scratchOpnd);
insertInstr->InsertBefore(instr);
instr = IR::BranchInstr::New(Js::OpCode::BGT, doneLabelInstr, this->m_func);
insertInstr->InsertBefore(instr);
}
insertInstr->InsertBefore(helperLabel);
// Zero out the pointer to the list of stack nested funcs, since the functions won't be initialized on this path.
scratchOpnd = IR::RegOpnd::New(nullptr, RegR0, TyMachReg, m_func);
IR::RegOpnd *frameReg = IR::RegOpnd::New(nullptr, GetRegFramePointer(), TyMachReg, m_func);
Lowerer::InsertMove(scratchOpnd, IR::IntConstOpnd::New(0, TyMachReg, m_func), insertInstr);
IR::Opnd *indirOpnd = IR::IndirOpnd::New(
frameReg, -(int32)(Js::Constants::StackNestedFuncList * sizeof(Js::Var)), TyMachReg, m_func);
Lowerer::InsertMove(indirOpnd, scratchOpnd, insertInstr);
IR::RegOpnd *r0Opnd = IR::RegOpnd::New(nullptr, RegR0, TyMachReg, this->m_func);
Lowerer::InsertMove(r0Opnd, IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func, true), insertInstr);
IR::RegOpnd *r1Opnd = IR::RegOpnd::New(nullptr, RegR1, TyMachReg, this->m_func);
Lowerer::InsertMove(r1Opnd, this->m_lowerer->LoadScriptContextOpnd(insertInstr), insertInstr);
IR::RegOpnd *r2Opnd = IR::RegOpnd::New(nullptr, RegR2, TyMachReg, m_func);
Lowerer::InsertMove(r2Opnd, IR::HelperCallOpnd::New(IR::HelperProbeCurrentStack, this->m_func), insertInstr);
instr = IR::Instr::New(afterProlog? Js::OpCode::BLX : Js::OpCode::BX, this->m_func);
instr->SetSrc1(r2Opnd);
insertInstr->InsertBefore(instr);
insertInstr->InsertBefore(doneLabelInstr);
Security::InsertRandomFunctionPad(doneLabelInstr);
}
//
// Emits the code to allocate 'size' amount of space on stack. for values smaller than PAGE_SIZE
// this will just emit sub rsp,size otherwise calls _chkstk.
//
bool
LowererMD::GenerateStackAllocation(IR::Instr *instr, uint32 allocSize, uint32 probeSize)
{
IR::RegOpnd * spOpnd = IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func);
if (IsSmallStack(probeSize))
{
AssertMsg(!(allocSize & 0xFFFFF000), "Must fit in 12 bits");
// Generate SUB SP, SP, stackSize
IR::IntConstOpnd * stackSizeOpnd = IR::IntConstOpnd::New(allocSize, TyMachReg, this->m_func, true);
IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, spOpnd, spOpnd, stackSizeOpnd, this->m_func);
instr->InsertBefore(subInstr);
return false;
}
//__chkStk is a leaf function and hence alignment is not required.
// Generate _chkstk call
// LDIMM RegR4, stackSize/4 //input: r4 = the number of WORDS (word = 4 bytes) to allocate,
// LDIMM RegR12, HelperCRT_chkstk
// BLX RegR12
// SUB SP, SP, RegR4 //output: r4 = total number of BYTES probed/allocated.
//chkstk expects the stacksize argument in R4 register
IR::RegOpnd *r4Opnd = IR::RegOpnd::New(nullptr, SP_ALLOC_SCRATCH_REG, TyMachReg, this->m_func);
IR::RegOpnd *targetOpnd = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
IR::IntConstOpnd * stackSizeOpnd = IR::IntConstOpnd::New((allocSize/MachPtr), TyMachReg, this->m_func, true);
IR::Instr *movInstr = IR::Instr::New(Js::OpCode::LDIMM, r4Opnd, stackSizeOpnd, this->m_func);
instr->InsertBefore(movInstr);
IR::Instr *movHelperAddrInstr = IR::Instr::New(Js::OpCode::LDIMM, targetOpnd, IR::HelperCallOpnd::New(IR::HelperCRT_chkstk, this->m_func), this->m_func);
instr->InsertBefore(movHelperAddrInstr);
IR::Instr * callInstr = IR::Instr::New(Js::OpCode::BLX, r4Opnd, targetOpnd, this->m_func);
instr->InsertBefore(callInstr);
// Generate SUB SP, SP, R4
IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, spOpnd, spOpnd, r4Opnd, this->m_func);
instr->InsertBefore(subInstr);
// return true to imply scratch register is trashed
return true;
}
void
LowererMD::GenerateStackDeallocation(IR::Instr *instr, uint32 allocSize)
{
IR::RegOpnd * spOpnd = IR::RegOpnd::New(nullptr, this->GetRegStackPointer(), TyMachReg, this->m_func);
IR::Instr * spAdjustInstr = IR::Instr::New(Js::OpCode::ADD,
spOpnd,
spOpnd,
IR::IntConstOpnd::New(allocSize, TyMachReg, this->m_func, true), this->m_func);
instr->InsertBefore(spAdjustInstr);
LegalizeMD::LegalizeInstr(spAdjustInstr);
}
//------------------------------------------------------------------------------------------
//
// Prologs and epilogs on ARM:
//
// 1. Normal non-leaf function:
//
// MOV r12,0 -- prepare to clear the arg obj slot (not in prolog or pdata)
// $PrologStart:
// PUSH {r0-r3} -- home parameters (homes only r0-r1 for global function, r2 as well for eval with "this"
// PUSH {r11,lr} -- save frame pointer and return address
// MOV r11,sp -- set up frame chain (r11 points to saved r11)
// PUSH {r4-r10,r12} -- save non-volatile regs (only used), clear arg obj slot
// VPUSH {d8-d15} -- save non-volatile double regs (only used)
// SUB sp, stack -- allocate locals and arg out area
// <probe stack> -- not in prolog