-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathLowererMDArch.cpp
More file actions
3501 lines (2992 loc) · 125 KB
/
LowererMDArch.cpp
File metadata and controls
3501 lines (2992 loc) · 125 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project 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 "LowererMDArch.h"
#include "Library/Generators/JavascriptGeneratorFunction.h"
const Js::OpCode LowererMD::MDExtend32Opcode = Js::OpCode::MOVSXD;
extern const IRType RegTypes[RegNumCount];
BYTE
LowererMDArch::GetDefaultIndirScale()
{
return IndirScale8;
}
RegNum
LowererMDArch::GetRegShiftCount()
{
return RegRCX;
}
RegNum
LowererMDArch::GetRegReturn(IRType type)
{
return ( IRType_IsFloat(type) || IRType_IsSimd128(type) ) ? RegXMM0 : RegRAX;
}
RegNum
LowererMDArch::GetRegReturnAsmJs(IRType type)
{
if (IRType_IsFloat(type))
{
return RegXMM0;
}
else if (IRType_IsSimd128(type))
{
return RegXMM0;
}
else
{
return RegRAX;
}
}
RegNum
LowererMDArch::GetRegStackPointer()
{
return RegRSP;
}
RegNum
LowererMDArch::GetRegBlockPointer()
{
return RegRBP;
}
RegNum
LowererMDArch::GetRegFramePointer()
{
return RegRBP;
}
RegNum
LowererMDArch::GetRegChkStkParam()
{
return RegRAX;
}
RegNum
LowererMDArch::GetRegIMulDestLower()
{
return RegRAX;
}
RegNum
LowererMDArch::GetRegIMulHighDestLower()
{
return RegRDX;
}
RegNum
LowererMDArch::GetRegArgI4(int32 argNum)
{
// TODO: decide on registers to use for int
return RegNOREG;
}
RegNum
LowererMDArch::GetRegArgR8(int32 argNum)
{
// TODO: decide on registers to use for double
return RegNOREG;
}
Js::OpCode
LowererMDArch::GetAssignOp(IRType type)
{
switch (type)
{
case TyFloat64:
return Js::OpCode::MOVSD;
case TyFloat32:
return Js::OpCode::MOVSS;
case TySimd128F4:
case TySimd128I4:
case TySimd128I8:
case TySimd128I16:
case TySimd128U4:
case TySimd128U8:
case TySimd128U16:
case TySimd128B4:
case TySimd128B8:
case TySimd128B16:
case TySimd128D2:
case TySimd128I2:
return Js::OpCode::MOVUPS;
default:
return Js::OpCode::MOV;
}
}
void
LowererMDArch::Init(LowererMD *lowererMD)
{
this->lowererMD = lowererMD;
this->helperCallArgsCount = 0;
}
///----------------------------------------------------------------------------
///
/// LowererMD::LoadInputParamPtr
///
/// Load the address of the start of the passed-in parameters not including
/// the this parameter.
///
///----------------------------------------------------------------------------
IR::Instr *
LowererMDArch::LoadInputParamPtr(IR::Instr *instrInsert, IR::RegOpnd *optionalDstOpnd /* = nullptr */)
{
if (this->m_func->GetJITFunctionBody()->IsCoroutine())
{
IR::RegOpnd * argPtrRegOpnd = Lowerer::LoadGeneratorArgsPtr(instrInsert);
IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(argPtrRegOpnd, 1 * MachPtr, TyMachPtr, this->m_func);
IR::RegOpnd * dstOpnd = optionalDstOpnd != nullptr ? optionalDstOpnd : IR::RegOpnd::New(TyMachPtr, this->m_func);
return Lowerer::InsertLea(dstOpnd, indirOpnd, instrInsert);
}
else
{
// Stack looks like (EBP chain)+0, (return addr)+4, (function object)+8, (arg count)+12, (this)+16, actual args
StackSym *paramSym = StackSym::New(TyMachReg, this->m_func);
this->m_func->SetArgOffset(paramSym, 5 * MachPtr);
return this->lowererMD->m_lowerer->InsertLoadStackAddress(paramSym, instrInsert, optionalDstOpnd);
}
}
IR::Instr *
LowererMDArch::LoadStackArgPtr(IR::Instr * instrArgPtr)
{
// Get the args pointer relative to the frame pointer.
// NOTE: This code is sufficient for the apply-args optimization, but not for StackArguments,
// if and when that is enabled.
// dst = LEA &[rbp + "this" offset + sizeof(var)]
IR::Instr * instr = LoadInputParamPtr(instrArgPtr, instrArgPtr->UnlinkDst()->AsRegOpnd());
instrArgPtr->Remove();
return instr->m_prev;
}
IR::Instr *
LowererMDArch::LoadHeapArgsCached(IR::Instr *instrArgs)
{
ASSERT_INLINEE_FUNC(instrArgs);
Func *func = instrArgs->m_func;
IR::Instr *instrPrev = instrArgs->m_prev;
if (instrArgs->m_func->IsStackArgsEnabled())
{
instrArgs->m_opcode = Js::OpCode::MOV;
instrArgs->ReplaceSrc1(IR::AddrOpnd::NewNull(func));
if (PHASE_TRACE1(Js::StackArgFormalsOptPhase) && func->GetJITFunctionBody()->GetInParamsCount() > 1)
{
Output::Print(_u("StackArgFormals : %s (%d) :Removing Heap Arguments object creation in Lowerer. \n"), instrArgs->m_func->GetJITFunctionBody()->GetDisplayName(), instrArgs->m_func->GetFunctionNumber());
Output::Flush();
}
}
else
{
// s7 = formals are let decls
// s6 = memory context
// s5 = local frame instance
// s4 = address of first actual argument (after "this")
// s3 = formal argument count
// s2 = actual argument count
// s1 = current function
// dst = JavascriptOperators::LoadArguments(s1, s2, s3, s4, s5, s6, s7)
// s7 = formals are let decls
IR::Opnd * formalsAreLetDecls = IR::IntConstOpnd::New((IntConstType)(instrArgs->m_opcode == Js::OpCode::LdLetHeapArgsCached), TyUint8, func);
this->LoadHelperArgument(instrArgs, formalsAreLetDecls);
// s6 = memory context
this->lowererMD->m_lowerer->LoadScriptContext(instrArgs);
// s5 = local frame instance
IR::Opnd *frameObj = instrArgs->UnlinkSrc1();
this->LoadHelperArgument(instrArgs, frameObj);
if (func->IsInlinee())
{
// s4 = address of first actual argument (after "this").
StackSym *firstRealArgSlotSym = func->GetInlineeArgvSlotOpnd()->m_sym->AsStackSym();
this->m_func->SetArgOffset(firstRealArgSlotSym, firstRealArgSlotSym->m_offset + MachPtr);
IR::Instr *instr = this->lowererMD->m_lowerer->InsertLoadStackAddress(firstRealArgSlotSym, instrArgs);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s3 = formal argument count (without counting "this").
uint32 formalsCount = func->GetJITFunctionBody()->GetInParamsCount() - 1;
this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(formalsCount, TyUint32, func));
// s2 = actual argument count (without counting "this").
instr = IR::Instr::New(Js::OpCode::MOV,
IR::RegOpnd::New(TyMachReg, func),
IR::IntConstOpnd::New(func->actualCount - 1, TyMachReg, func),
func);
instrArgs->InsertBefore(instr);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s1 = current function.
this->LoadHelperArgument(instrArgs, func->GetInlineeFunctionObjectSlotOpnd());
// Save the newly-created args object to its dedicated stack slot.
IR::SymOpnd *argObjSlotOpnd = func->GetInlineeArgumentsObjectSlotOpnd();
instr = IR::Instr::New(Js::OpCode::MOV,
argObjSlotOpnd,
instrArgs->GetDst(),
func);
instrArgs->InsertAfter(instr);
}
else
{
// s4 = address of first actual argument (after "this")
// Stack looks like (EBP chain)+0, (return addr)+4, (function object)+8, (arg count)+12, (this)+16, actual args
IR::Instr *instr = this->LoadInputParamPtr(instrArgs);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s3 = formal argument count (without counting "this")
uint32 formalsCount = func->GetInParamsCount() - 1;
this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(formalsCount, TyInt32, func));
// s2 = actual argument count (without counting "this")
instr = this->lowererMD->LoadInputParamCount(instrArgs);
instr = IR::Instr::New(Js::OpCode::DEC, instr->GetDst(), instr->GetDst(), func);
instrArgs->InsertBefore(instr);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s1 = current function
StackSym *paramSym = StackSym::New(TyMachReg, func);
this->m_func->SetArgOffset(paramSym, 2 * MachPtr);
IR::Opnd * srcOpnd = IR::SymOpnd::New(paramSym, TyMachReg, func);
this->LoadHelperArgument(instrArgs, srcOpnd);
// Save the newly-created args object to its dedicated stack slot.
IR::Opnd *opnd = LowererMD::CreateStackArgumentsSlotOpnd(func);
instr = IR::Instr::New(Js::OpCode::MOV, opnd, instrArgs->GetDst(), func);
instrArgs->InsertAfter(instr);
}
this->lowererMD->ChangeToHelperCall(instrArgs, IR::HelperOp_LoadHeapArgsCached);
}
return instrPrev;
}
///----------------------------------------------------------------------------
///
/// LowererMDArch::LoadHeapArguments
///
/// Load the arguments object
/// NOTE: The same caveat regarding arguments passed on the stack applies here
/// as in LoadInputParamCount above.
///----------------------------------------------------------------------------
IR::Instr *
LowererMDArch::LoadHeapArguments(IR::Instr *instrArgs)
{
ASSERT_INLINEE_FUNC(instrArgs);
Func *func = instrArgs->m_func;
IR::Instr *instrPrev = instrArgs->m_prev;
if (func->IsStackArgsEnabled())
{
instrArgs->m_opcode = Js::OpCode::MOV;
instrArgs->ReplaceSrc1(IR::AddrOpnd::NewNull(func));
if (PHASE_TRACE1(Js::StackArgFormalsOptPhase) && func->GetJITFunctionBody()->GetInParamsCount() > 1)
{
Output::Print(_u("StackArgFormals : %s (%d) :Removing Heap Arguments object creation in Lowerer. \n"), instrArgs->m_func->GetJITFunctionBody()->GetDisplayName(), instrArgs->m_func->GetFunctionNumber());
Output::Flush();
}
}
else
{
// s7 = formals are let decls
// s6 = memory context
// s5 = array of property ID's
// s4 = local frame instance
// s3 = address of first actual argument (after "this")
// s2 = actual argument count
// s1 = current function
// dst = JavascriptOperators::LoadHeapArguments(s1, s2, s3, s4, s5, s6, s7)
// s7 = formals are let decls
this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(instrArgs->m_opcode == Js::OpCode::LdLetHeapArguments ? TRUE : FALSE, TyUint8, func));
// s6 = memory context
instrPrev = this->lowererMD->m_lowerer->LoadScriptContext(instrArgs);
// s5 = array of property ID's
intptr_t formalsPropIdArray = instrArgs->m_func->GetJITFunctionBody()->GetFormalsPropIdArrayAddr();
if (!formalsPropIdArray)
{
formalsPropIdArray = instrArgs->m_func->GetScriptContextInfo()->GetNullAddr();
}
IR::Opnd * argArray = IR::AddrOpnd::New(formalsPropIdArray, IR::AddrOpndKindDynamicMisc, m_func);
this->LoadHelperArgument(instrArgs, argArray);
// s4 = local frame instance
IR::Opnd *frameObj = instrArgs->UnlinkSrc1();
this->LoadHelperArgument(instrArgs, frameObj);
if (func->IsInlinee())
{
// s3 = address of first actual argument (after "this").
StackSym *firstRealArgSlotSym = func->GetInlineeArgvSlotOpnd()->m_sym->AsStackSym();
this->m_func->SetArgOffset(firstRealArgSlotSym, firstRealArgSlotSym->m_offset + MachPtr);
IR::Instr *instr = this->lowererMD->m_lowerer->InsertLoadStackAddress(firstRealArgSlotSym, instrArgs);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s2 = actual argument count (without counting "this").
instr = IR::Instr::New(Js::OpCode::MOV,
IR::RegOpnd::New(TyUint32, func),
IR::IntConstOpnd::New(func->actualCount - 1, TyUint32, func),
func);
instrArgs->InsertBefore(instr);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s1 = current function.
this->LoadHelperArgument(instrArgs, func->GetInlineeFunctionObjectSlotOpnd());
// Save the newly-created args object to its dedicated stack slot.
IR::SymOpnd *argObjSlotOpnd = func->GetInlineeArgumentsObjectSlotOpnd();
instr = IR::Instr::New(Js::OpCode::MOV,
argObjSlotOpnd,
instrArgs->GetDst(),
func);
instrArgs->InsertAfter(instr);
}
else
{
// s3 = address of first actual argument (after "this")
// Stack looks like (EBP chain)+0, (return addr)+4, (function object)+8, (arg count)+12, (this)+16, actual args
IR::Instr *instr = this->LoadInputParamPtr(instrArgs);
this->LoadHelperArgument(instrArgs, instr->GetDst());
// s2 = actual argument count (without counting "this")
instr = this->lowererMD->LoadInputParamCount(instrArgs, -1);
IR::Opnd * opndInputParamCount = instr->GetDst();
this->LoadHelperArgument(instrArgs, opndInputParamCount);
// s1 = current function
StackSym * paramSym = StackSym::New(TyMachReg, func);
this->m_func->SetArgOffset(paramSym, 2 * MachPtr);
IR::Opnd * srcOpnd = IR::SymOpnd::New(paramSym, TyMachReg, func);
if (this->m_func->GetJITFunctionBody()->IsCoroutine())
{
// the function object for generator calls is a GeneratorVirtualScriptFunction object
// and we need to pass the real JavascriptGeneratorFunction object so grab it instead
IR::RegOpnd *tmpOpnd = IR::RegOpnd::New(TyMachReg, func);
Lowerer::InsertMove(tmpOpnd, srcOpnd, instrArgs);
srcOpnd = IR::IndirOpnd::New(tmpOpnd, Js::GeneratorVirtualScriptFunction::GetRealFunctionOffset(), TyMachPtr, func);
}
this->LoadHelperArgument(instrArgs, srcOpnd);
// Save the newly-created args object to its dedicated stack slot.
IR::Opnd *opnd = LowererMD::CreateStackArgumentsSlotOpnd(func);
instr = IR::Instr::New(Js::OpCode::MOV, opnd, instrArgs->GetDst(), func);
instrArgs->InsertAfter(instr);
}
this->lowererMD->ChangeToHelperCall(instrArgs, IR::HelperOp_LoadHeapArguments);
}
return instrPrev;
}
//
// Load the parameter in the first argument slot
//
IR::Instr *
LowererMDArch::LoadNewScObjFirstArg(IR::Instr * instr, IR::Opnd * dst, ushort extraArgs)
{
// Spread moves down the argument slot by one.
IR::Opnd * argOpnd = this->GetArgSlotOpnd(3 + extraArgs);
IR::Instr * argInstr = Lowerer::InsertMove(argOpnd, dst, instr);
return argInstr;
}
inline static RegNum GetRegFromArgPosition(const bool isFloatArg, const uint16 argPosition)
{
RegNum reg = RegNOREG;
if (!isFloatArg && argPosition <= IntArgRegsCount)
{
switch (argPosition)
{
#define REG_INT_ARG(Index, Name) \
case ((Index) + 1): \
reg = Reg ## Name; \
break;
#include "RegList.h"
default:
Assume(UNREACHED);
}
}
else if (isFloatArg && argPosition <= XmmArgRegsCount)
{
switch (argPosition)
{
#define REG_XMM_ARG(Index, Name) \
case ((Index) + 1): \
reg = Reg ## Name; \
break;
#include "RegList.h"
default:
Assume(UNREACHED);
}
}
return reg;
}
int32
LowererMDArch::LowerCallArgs(IR::Instr *callInstr, ushort callFlags, Js::ArgSlot extraParams, IR::IntConstOpnd **callInfoOpndRef /* = nullptr */)
{
AssertMsg(this->helperCallArgsCount == 0, "We don't support nested helper calls yet");
const Js::ArgSlot argOffset = 1;
uint32 argCount = 0;
// Lower args and look for StartCall
IR::Instr * argInstr = callInstr;
IR::Instr * cfgInsertLoc = callInstr->GetPrevRealInstr();
IR::Opnd *src2 = argInstr->UnlinkSrc2();
while (src2->IsSymOpnd())
{
IR::SymOpnd * argLinkOpnd = src2->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;
src2 = argInstr->UnlinkSrc2();
this->lowererMD->ChangeToAssign(argInstr);
// Mov each arg to its argSlot
Js::ArgSlot argPosition = argInstr->GetDst()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum();
Js::ArgSlot index = argOffset + argPosition;
if(index < argPosition)
{
Js::Throw::OutOfMemory();
}
index += extraParams;
if(index < extraParams)
{
Js::Throw::OutOfMemory();
}
IR::Opnd * dstOpnd = this->GetArgSlotOpnd(index, argLinkSym);
argInstr->ReplaceDst(dstOpnd);
cfgInsertLoc = argInstr->GetPrevRealInstr();
// The arg sym isn't assigned a constant directly anymore
// TODO: We can just move the instruction down next to the call if it is just a constant assignment
// but AMD64 doesn't have the MOV mem,imm64 encoding, and we have no code to detect if the value can fit
// into imm32 and hoist the src if it is not.
argLinkSym->m_isConst = false;
argLinkSym->m_isIntConst = false;
argLinkSym->m_isTaggableIntConst = false;
argInstr->Unlink();
callInstr->InsertBefore(argInstr);
argCount++;
}
IR::RegOpnd * argLinkOpnd = src2->AsRegOpnd();
StackSym * argLinkSym = argLinkOpnd->m_sym->AsStackSym();
AssertMsg(!argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
IR::Instr *startCallInstr = argLinkSym->m_instrDef;
if (callInstr->m_opcode == Js::OpCode::NewScObject ||
callInstr->m_opcode == Js::OpCode::NewScObjectSpread ||
callInstr->m_opcode == Js::OpCode::NewScObjectLiteral ||
callInstr->m_opcode == Js::OpCode::NewScObjArray ||
callInstr->m_opcode == Js::OpCode::NewScObjArraySpread)
{
// These push an extra arg.
argCount++;
}
AssertMsg(startCallInstr->m_opcode == Js::OpCode::StartCall ||
startCallInstr->m_opcode == Js::OpCode::LoweredStartCall,
"Problem with arg chain.");
AssertMsg(startCallInstr->GetArgOutCount(/*getInterpreterArgOutCount*/ false) == argCount ||
m_func->GetJITFunctionBody()->IsAsmJsMode(),
"ArgCount doesn't match StartCall count");
//
// Machine dependent lowering
//
if (callInstr->m_opcode != Js::OpCode::AsmJsCallI)
{
// Push argCount
IR::IntConstOpnd *argCountOpnd = Lowerer::MakeCallInfoConst(callFlags, argCount, m_func);
if (callInfoOpndRef)
{
argCountOpnd->Use(m_func);
*callInfoOpndRef = argCountOpnd;
}
Lowerer::InsertMove(this->GetArgSlotOpnd(1 + extraParams), argCountOpnd, callInstr);
}
startCallInstr = this->LowerStartCall(startCallInstr);
const uint32 argSlots = argCount + 1 + extraParams; // + 1 for call flags
this->m_func->m_argSlotsForFunctionsCalled = max(this->m_func->m_argSlotsForFunctionsCalled, argSlots);
if (m_func->GetJITFunctionBody()->IsAsmJsMode())
{
IR::Opnd * functionObjOpnd = callInstr->UnlinkSrc1();
GeneratePreCall(callInstr, functionObjOpnd, cfgInsertLoc->GetNextRealInstr());
}
return argSlots;
}
void
LowererMDArch::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
LowererMDArch::GenerateMemInit(IR::RegOpnd * opnd, int32 offset, size_t value, IR::Instr * insertBeforeInstr, bool isZeroed)
{
IRType type = TyVar;
if (isZeroed)
{
if (value == 0)
{
// Recycler memory are zero initialized
return;
}
type = value <= UINT_MAX ?
(value <= USHORT_MAX ?
(value <= UCHAR_MAX ? TyUint8 : TyUint16) :
TyUint32) :
type;
}
Func * func = this->m_func;
lowererMD->GetLowerer()->InsertMove(IR::IndirOpnd::New(opnd, offset, type, func), IR::IntConstOpnd::New(value, type, func), insertBeforeInstr);
}
IR::Instr *
LowererMDArch::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, TyMachReg, this->m_func), this->m_func));
this->SetMaxArgSlots(Js::InlineeCallInfo::MaxInlineeArgoutCount);
}
callInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, this->GetArgSlotOpnd(2), argsLength, this->m_func));
IR::Opnd *funcObjOpnd = callInstr->UnlinkSrc1();
GeneratePreCall(callInstr, funcObjOpnd, insertBeforeInstrForCFG);
// Normally for dynamic calls we move 4 args to registers and push remaining
// args onto stack (Windows convention, and unchanged on xplat). We need to
// manully home 4 args. inlinees lower differently and follow platform ABI.
// So we need to manually home actualArgsCount + 2 args (function, callInfo).
const uint32 homeArgs = callInstr->m_func->IsInlinee() ?
callInstr->m_func->actualCount + 2 : 4;
LowerCall(callInstr, homeArgs);
return callInstr;
}
void
LowererMDArch::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.");
IR::RegOpnd *functionObjRegOpnd = functionObjOpnd->AsRegOpnd();
IR::Instr * insertBeforeInstr = callInstr;
// Need check and error if we are calling a tagged int.
if (!functionObjRegOpnd->IsNotTaggedValue())
{
IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
if (this->lowererMD->GenerateObjectTest(functionObjRegOpnd, callInstr, helperLabel))
{
IR::LabelInstr * callLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
IR::Instr* instr = IR::BranchInstr::New(Js::OpCode::JMP, callLabel, this->m_func);
callInstr->InsertBefore(instr);
callInstr->InsertBefore(helperLabel);
callInstr->InsertBefore(callLabel);
insertBeforeInstr = callLabel;
lowererMD->m_lowerer->GenerateRuntimeError(insertBeforeInstr, JSERR_NeedFunction);
if (continueAfterExLabel)
{
// Under debugger the RuntimeError (exception) can be ignored, generate branch to jmp to safe place
// (which would normally be debugger bailout check).
IR::BranchInstr* continueAfterEx = IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, continueAfterExLabel, this->m_func);
insertBeforeInstr->InsertBefore(continueAfterEx);
}
}
}
}
void
LowererMDArch::GeneratePreCall(IR::Instr * callInstr, IR::Opnd *functionObjOpnd, IR::Instr * insertBeforeInstrForCFGCheck)
{
if (insertBeforeInstrForCFGCheck == nullptr)
{
insertBeforeInstrForCFGCheck = callInstr;
}
IR::RegOpnd * functionTypeRegOpnd = nullptr;
IR::IndirOpnd * entryPointIndirOpnd = nullptr;
if (callInstr->m_opcode == Js::OpCode::AsmJsCallI)
{
functionTypeRegOpnd = IR::RegOpnd::New(TyMachReg, m_func);
IR::IndirOpnd* functionInfoIndirOpnd = IR::IndirOpnd::New(functionObjOpnd->AsRegOpnd(), Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func);
IR::Instr* instr = IR::Instr::New(Js::OpCode::MOV, functionTypeRegOpnd, functionInfoIndirOpnd, m_func);
insertBeforeInstrForCFGCheck->InsertBefore(instr);
functionInfoIndirOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, Js::ScriptFunctionType::GetEntryPointInfoOffset(), TyMachReg, m_func);
instr = IR::Instr::New(Js::OpCode::MOV, functionTypeRegOpnd, functionInfoIndirOpnd, m_func);
insertBeforeInstrForCFGCheck->InsertBefore(instr);
uint32 entryPointOffset = Js::ProxyEntryPointInfo::GetAddressOffset();
entryPointIndirOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, entryPointOffset, TyMachReg, m_func);
}
else
{
// 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->lowererMD->m_lowerer->GenerateFunctionTypeFromFixedFunctionObject(insertBeforeInstrForCFGCheck, functionObjOpnd);
}
else if (functionObjOpnd->IsRegOpnd())
{
AssertMsg(functionObjOpnd->AsRegOpnd()->m_sym->IsStackSym(), "Expected call target to be a stack symbol.");
functionTypeRegOpnd = IR::RegOpnd::New(TyMachReg, m_func);
// functionTypeRegOpnd(RAX) = MOV function->type
{
IR::IndirOpnd * functionTypeIndirOpnd = IR::IndirOpnd::New(functionObjOpnd->AsRegOpnd(),
Js::DynamicObject::GetOffsetOfType(), TyMachReg, m_func);
IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, functionTypeRegOpnd, functionTypeIndirOpnd, m_func);
insertBeforeInstrForCFGCheck->InsertBefore(mov);
}
}
else
{
AnalysisAssertMsg(false, "Unexpected call target operand type.");
}
// entryPointRegOpnd(RAX) = MOV type->entryPoint
entryPointIndirOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, Js::Type::GetOffsetOfEntryPoint(), TyMachPtr, m_func);
}
IR::RegOpnd *entryPointRegOpnd = functionTypeRegOpnd;
entryPointRegOpnd->m_isCallArg = true;
IR::Instr *mov = IR::Instr::New(Js::OpCode::MOV, entryPointRegOpnd, entryPointIndirOpnd, m_func);
insertBeforeInstrForCFGCheck->InsertBefore(mov);
// entryPointRegOpnd(RAX) = CALL entryPointRegOpnd(RAX)
callInstr->SetSrc1(entryPointRegOpnd);
#if defined(_CONTROL_FLOW_GUARD)
// verify that the call target is valid (CFG Check)
if (!PHASE_OFF(Js::CFGInJitPhase, this->m_func))
{
this->lowererMD->GenerateCFGCheck(entryPointRegOpnd, insertBeforeInstrForCFGCheck);
}
#endif
// Setup the first call argument - pointer to the function being called.
IR::Instr * instrMovArg1 = IR::Instr::New(Js::OpCode::MOV, GetArgSlotOpnd(1), functionObjOpnd, m_func);
callInstr->InsertBefore(instrMovArg1);
}
IR::Instr *
LowererMDArch::LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper, IR::Instr * insertBeforeInstrForCFG)
{
AssertMsg(this->helperCallArgsCount == 0, "We don't support nested helper calls yet");
IR::Opnd * functionObjOpnd = callInstr->UnlinkSrc1();
IR::Instr * insertBeforeInstrForCFGCheck = callInstr;
// 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))
{
Assert(functionObjOpnd->IsRegOpnd());
IR::LabelInstr* continueAfterExLabel = Lowerer::InsertContinueAfterExceptionLabelForDebugger(m_func, callInstr, isHelper);
GenerateFunctionObjectTest(callInstr, functionObjOpnd->AsRegOpnd(), isHelper, continueAfterExLabel);
}
else if (insertBeforeInstrForCFG != nullptr)
{
RegNum dstReg = insertBeforeInstrForCFG->GetDst()->AsRegOpnd()->GetReg();
AssertMsg(dstReg == RegArg2 || dstReg == RegArg3, "NewScObject should insert the first Argument in RegArg2/RegArg3 only based on Spread call or not.");
insertBeforeInstrForCFGCheck = insertBeforeInstrForCFG;
}
GeneratePreCall(callInstr, functionObjOpnd, insertBeforeInstrForCFGCheck);
// We need to get the calculated CallInfo in SimpleJit because that doesn't include any changes for stack alignment
IR::IntConstOpnd *callInfo = nullptr;
int32 argCount = LowerCallArgs(callInstr, callFlags, 1, &callInfo);
IR::Opnd *const finalDst = callInstr->GetDst();
// x64 keeps track of argCount for us, so pass just an arbitrary value there
IR::Instr* ret = this->LowerCall(callInstr, 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->lowererMD->m_lowerer->GenerateCallProfiling(
instr->profileId,
instr->inlineCacheIndex,
instr->GetDst(),
functionObjOpnd,
callInfo,
instr->isProfiledReturnCall,
callInstr,
ret);
}
return ret;
}
static inline IRType ExtendHelperArg(IRType type)
{
#ifdef __clang__
// clang expects caller to extend arg size to int
switch (type)
{
case TyInt8:
case TyInt16:
return TyInt32;
case TyUint8:
case TyUint16:
return TyUint32;
}
#endif
return type;
}
IR::Instr *
LowererMDArch::LowerCall(IR::Instr * callInstr, uint32 argCount)
{
UNREFERENCED_PARAMETER(argCount);
IR::Instr *retInstr = callInstr;
callInstr->m_opcode = Js::OpCode::CALL;
// This is required here due to calls create during lowering
callInstr->m_func->SetHasCallsOnSelfAndParents();
if (callInstr->GetDst())
{
IR::Opnd * dstOpnd;
this->lowererMD->ForceDstToReg(callInstr);
dstOpnd = callInstr->GetDst();
IRType dstType = dstOpnd->GetType();
Js::OpCode assignOp = GetAssignOp(dstType);
if (callInstr->GetSrc1()->IsHelperCallOpnd())
{
// Truncate the result of a conversion to 32-bit int, because the C++ code doesn't.
IR::HelperCallOpnd *helperOpnd = callInstr->GetSrc1()->AsHelperCallOpnd();
if (helperOpnd->m_fnHelper == IR::HelperConv_ToInt32 ||
helperOpnd->m_fnHelper == IR::HelperConv_ToInt32_Full ||
helperOpnd->m_fnHelper == IR::HelperConv_ToInt32Core ||
helperOpnd->m_fnHelper == IR::HelperConv_ToUInt32 ||
helperOpnd->m_fnHelper == IR::HelperConv_ToUInt32_Full ||
helperOpnd->m_fnHelper == IR::HelperConv_ToUInt32Core)
{
assignOp = Js::OpCode::MOV_TRUNC;
}
}
IR::Instr * movInstr = callInstr->SinkDst(assignOp);
RegNum reg = GetRegReturn(dstType);
callInstr->GetDst()->AsRegOpnd()->SetReg(reg);
movInstr->GetSrc1()->AsRegOpnd()->SetReg(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 && MaxArgumentsToHelper < 255, "Too many helper call arguments");
uint16 argsLeft = static_cast<uint16>(this->helperCallArgsCount);
// Sys V x64 ABI assigns int and xmm arg registers separately.
// e.g. args: int, double, int, double, int, double
// Windows: int0, xmm1, int2, xmm3, stack, stack
// Sys V: int0, xmm0, int1, xmm1, int2, xmm2
#ifdef _WIN32
#define _V_ARG_INDEX(index) index
#else
uint16 _vindex[MaxArgumentsToHelper];
{
uint16 intIndex = 1, doubleIndex = 1, stackIndex = IntArgRegsCount + 1;
for (int i = 0; i < this->helperCallArgsCount; i++)
{
IR::Opnd * helperSrc = this->helperCallArgs[this->helperCallArgsCount - 1 - i];
IRType type = helperSrc->GetType();
if (IRType_IsFloat(type) || IRType_IsSimd128(type))
{
if (doubleIndex <= XmmArgRegsCount)
{
_vindex[i] = doubleIndex++;
}
else
{
_vindex[i] = stackIndex++;
}
}
else
{
if (intIndex <= IntArgRegsCount)
{
_vindex[i] = intIndex++;
}
else
{
_vindex[i] = stackIndex++;
}
}
}
}
#define _V_ARG_INDEX(index) _vindex[(index) - 1]
#endif
// xplat NOTE: Lower often loads "known args" with LoadHelperArgument() and
// variadic JS runtime args with LowerCallArgs(). So the full args length is
// this->helperCallArgsCount + argCount
// "argCount > 0" indicates we have variadic JS runtime args and needs to
// manually home registers on xplat.
const bool shouldHomeParams = argCount > 0;
while (argsLeft > 0)
{
IR::Opnd * helperSrc = this->helperCallArgs[this->helperCallArgsCount - argsLeft];
uint16 index = _V_ARG_INDEX(argsLeft);
StackSym * helperSym = m_func->m_symTable->GetArgSlotSym(index);
helperSym->m_type = ExtendHelperArg(helperSrc->GetType());
Lowerer::InsertMove(
this->GetArgSlotOpnd(index, helperSym, /*isHelper*/!shouldHomeParams),
helperSrc,
callInstr, false);
--argsLeft;
}
#ifndef _WIN32
// Manually home args
if (shouldHomeParams)
{
const int callArgCount = this->helperCallArgsCount + static_cast<int>(argCount);
int argRegs = min(callArgCount, static_cast<int>(XmmArgRegsCount));
for (int i = argRegs; i > 0; i--)
{
IRType type = this->xplatCallArgs.args[i];
bool isFloatArg = this->xplatCallArgs.IsFloat(i);
if ( i > IntArgRegsCount && !isFloatArg ) continue;
StackSym * sym = this->m_func->m_symTable->GetArgSlotSym(static_cast<uint16>(i));
RegNum reg = GetRegFromArgPosition(isFloatArg, i);
IR::RegOpnd *regOpnd = IR::RegOpnd::New(nullptr, reg, type, this->m_func);
regOpnd->m_isCallArg = true;
Lowerer::InsertMove(
IR::SymOpnd::New(sym, type, this->m_func),
regOpnd,
callInstr, false);
}
}
this->xplatCallArgs.Reset();
#endif // !_WIN32
//
// load the address into a register because we cannot directly access 64 bit constants
// in CALL 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.
//
// Also skip this for relocatable helper calls. These will be turned into indirect
// calls in lower.
if (callInstr->GetSrc1()->IsHelperCallOpnd())
{
// Helper calls previously do not have bailouts except for bailout call. However with LazyBailOut, they can now have
// bailouts as well. Handle them the same way as before.
if (!callInstr->HasBailOutInfo() || callInstr->OnlyHasLazyBailOut())
{
IR::RegOpnd *targetOpnd = IR::RegOpnd::New(StackSym::New(TyMachPtr, m_func), RegRAX, TyMachPtr, this->m_func);
IR::Instr *movInstr = IR::Instr::New(Js::OpCode::MOV, targetOpnd, callInstr->GetSrc1(), this->m_func);
targetOpnd->m_isCallArg = true;
callInstr->UnlinkSrc1();
callInstr->SetSrc1(targetOpnd);
callInstr->InsertBefore(movInstr);
}
}
if (callInstr->HasLazyBailOut())