-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathIRBuilderAsmJs.cpp
7008 lines (5969 loc) · 258 KB
/
IRBuilderAsmJs.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"
#ifdef ASMJS_PLAT
#include "ByteCode/OpCodeUtilAsmJs.h"
#include "../../WasmReader/WasmParseTree.h"
void
IRBuilderAsmJs::Build()
{
m_funcAlloc = m_func->m_alloc;
NoRecoverMemoryJitArenaAllocator localAlloc(_u("BE-IRBuilder"), m_funcAlloc->GetPageAllocator(), Js::Throw::OutOfMemory);
m_tempAlloc = &localAlloc;
uint32 offset;
uint32 statementIndex = m_statementReader ? m_statementReader->GetStatementIndex() : Js::Constants::NoStatementIndex;
m_argStack = JitAnew(m_tempAlloc, SListCounted<IR::Instr *>, m_tempAlloc);
m_tempList = JitAnew(m_tempAlloc, SList<IR::Instr *>, m_tempAlloc);
m_argOffsetStack = JitAnew(m_tempAlloc, SList<int32>, m_tempAlloc);
m_branchRelocList = JitAnew(m_tempAlloc, SList<BranchReloc *>, m_tempAlloc);
m_switchBuilder.Init(m_func, m_tempAlloc, true);
m_firstVarConst = 0;
m_tempCount = 0;
m_firstsType[0] = m_firstVarConst + AsmJsRegSlots::RegCount;
for (int i = 0, j = 1; i < WAsmJs::LIMIT; ++i, ++j)
{
WAsmJs::Types type = (WAsmJs::Types)i;
const auto typedInfo = m_asmFuncInfo->GetTypedSlotInfo(type);
m_firstsType[j] = typedInfo.constCount;
m_firstsType[j + WAsmJs::LIMIT] = typedInfo.varCount;
m_firstsType[j + 2 * WAsmJs::LIMIT] = typedInfo.tmpCount;
m_tempCount += typedInfo.tmpCount;
}
// Fixup the firsts by looking at the previous value
for (int i = 1; i < m_firstsTypeCount; ++i)
{
m_firstsType[i] += m_firstsType[i - 1];
}
m_func->firstIRTemp = m_firstsType[m_firstsTypeCount - 1];
m_simdOpcodesMap = JitAnewArrayZ(m_tempAlloc, Js::OpCode, Js::Simd128AsmJsOpcodeCount());
{
#define MACRO_SIMD(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) m_simdOpcodesMap[(uint32)(Js::OpCodeAsmJs::opcode - Js::OpCodeAsmJs::Simd128_Start)] = Js::OpCode::opcode;
#define MACRO_SIMD_WMS(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) MACRO_SIMD(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr)
// append extended opcodes
#define MACRO_SIMD_EXTEND(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) \
m_simdOpcodesMap[(uint32)(Js::OpCodeAsmJs::opcode - Js::OpCodeAsmJs::Simd128_Start_Extend) + (Js::OpCodeAsmJs::Simd128_End - Js::OpCodeAsmJs::Simd128_Start + 1)] = Js::OpCode::opcode;
#define MACRO_SIMD_EXTEND_WMS(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr, ...) MACRO_SIMD_EXTEND(opcode, asmjsLayout, opCodeAttrAsmJs, OpCodeAttr)
#include "ByteCode/OpCodesSimd.h"
}
// we will be using lower space for type specialized syms, so bump up where new temp syms can be created
m_func->m_symTable->IncreaseStartingID(m_func->firstIRTemp);
if (m_tempCount > 0)
{
m_tempMap = AnewArrayZ(m_tempAlloc, SymID, m_tempCount);
m_fbvTempUsed = BVFixed::New<JitArenaAllocator>(m_tempCount, m_tempAlloc);
}
else
{
m_tempMap = nullptr;
m_fbvTempUsed = nullptr;
}
m_func->m_headInstr = IR::EntryInstr::New(Js::OpCode::FunctionEntry, m_func);
m_func->m_exitInstr = IR::ExitInstr::New(Js::OpCode::FunctionExit, m_func);
m_func->m_tailInstr = m_func->m_exitInstr;
m_func->m_headInstr->InsertAfter(m_func->m_tailInstr);
m_functionStartOffset = m_jnReader.GetCurrentOffset();
m_lastInstr = m_func->m_headInstr;
CompileAssert(sizeof(SymID) == sizeof(Js::RegSlot));
offset = m_functionStartOffset;
// Skip the last EndOfBlock opcode
// EndOfBlock opcode has same value in Asm
Assert(!OpCodeAttr::HasMultiSizeLayout(Js::OpCode::EndOfBlock));
uint32 lastOffset = m_func->GetJITFunctionBody()->GetByteCodeLength() - Js::OpCodeUtil::EncodedSize(Js::OpCode::EndOfBlock, Js::SmallLayout);
uint32 offsetToInstructionCount = lastOffset;
if (this->IsLoopBody())
{
// LdSlot & StSlot needs to cover all the register, including the temps, because we might treat
// those as if they are local for yielding
m_jitLoopBodyData = JitAnew(m_tempAlloc, JitLoopBodyData,
BVFixed::New<JitArenaAllocator>(GetLastTmp(WAsmJs::LastType), m_tempAlloc),
BVFixed::New<JitArenaAllocator>(GetLastTmp(WAsmJs::LastType), m_tempAlloc),
StackSym::New(TyInt32, this->m_func)
);
#if DBG
GetJitLoopBodyData().m_usedAsTemp = BVFixed::New<JitArenaAllocator>(GetLastTmp(WAsmJs::LastType), m_tempAlloc);
#endif
lastOffset = m_func->GetWorkItem()->GetLoopHeader()->endOffset;
// Ret is created at lastOffset + 1, so we need lastOffset + 2 entries
offsetToInstructionCount = lastOffset + 2;
}
m_offsetToInstructionCount = offsetToInstructionCount;
m_offsetToInstruction = JitAnewArrayZ(m_tempAlloc, IR::Instr *, offsetToInstructionCount);
LoadNativeCodeData();
BuildConstantLoads();
if (!this->IsLoopBody() && m_func->GetJITFunctionBody()->HasImplicitArgIns())
{
BuildImplicitArgIns();
}
if (m_statementReader && m_statementReader->AtStatementBoundary(&m_jnReader))
{
statementIndex = AddStatementBoundary(statementIndex, offset);
}
Js::LayoutSize layoutSize;
for (Js::OpCodeAsmJs newOpcode = m_jnReader.ReadAsmJsOp(layoutSize); (uint)m_jnReader.GetCurrentOffset() <= lastOffset; newOpcode = m_jnReader.ReadAsmJsOp(layoutSize))
{
Assert(newOpcode != Js::OpCodeAsmJs::EndOfBlock);
AssertOrFailFastMsg(Js::OpCodeUtilAsmJs::IsValidByteCodeOpcode(newOpcode), "Error getting opcode from m_jnReader.Op()");
uint layoutAndSize = layoutSize * Js::OpLayoutTypeAsmJs::Count + Js::OpCodeUtilAsmJs::GetOpCodeLayout(newOpcode);
switch (layoutAndSize)
{
#define LAYOUT_TYPE(layout) \
case Js::OpLayoutTypeAsmJs::layout: \
Assert(layoutSize == Js::SmallLayout); \
Build##layout(newOpcode, offset); \
break;
#define LAYOUT_TYPE_WMS(layout) \
case Js::SmallLayout * Js::OpLayoutTypeAsmJs::Count + Js::OpLayoutTypeAsmJs::layout: \
Build##layout<Js::SmallLayoutSizePolicy>(newOpcode, offset); \
break; \
case Js::MediumLayout * Js::OpLayoutTypeAsmJs::Count + Js::OpLayoutTypeAsmJs::layout: \
Build##layout<Js::MediumLayoutSizePolicy>(newOpcode, offset); \
break; \
case Js::LargeLayout * Js::OpLayoutTypeAsmJs::Count + Js::OpLayoutTypeAsmJs::layout: \
Build##layout<Js::LargeLayoutSizePolicy>(newOpcode, offset); \
break;
#define EXCLUDE_FRONTEND_LAYOUT
#include "ByteCode/LayoutTypesAsmJs.h"
default:
AssertMsg(UNREACHED, "Unimplemented layout");
Js::Throw::InternalError();
break;
}
offset = m_jnReader.GetCurrentOffset();
if (m_statementReader && m_statementReader->AtStatementBoundary(&m_jnReader))
{
statementIndex = AddStatementBoundary(statementIndex, offset);
}
}
if (m_statementReader && Js::Constants::NoStatementIndex != statementIndex)
{
statementIndex = AddStatementBoundary(statementIndex, Js::Constants::NoByteCodeOffset);
}
if (IsLoopBody())
{
// Insert the LdSlot/StSlot and Ret
IR::Opnd * retOpnd = this->InsertLoopBodyReturnIPInstr(offset, offset);
// Restore and Ret are at the last offset + 1
GenerateLoopBodySlotAccesses(lastOffset + 1);
IR::Instr * retInstr = IR::Instr::New(Js::OpCode::Ret, m_func);
retInstr->SetSrc1(retOpnd);
this->AddInstr(retInstr, lastOffset + 1);
}
// Now fix up the targets for all the branches we've introduced.
InsertLabels();
// Now that we know whether the func is a leaf or not, decide whether we'll emit fast paths.
// Do this once and for all, per-func, since the source size on the ThreadContext will be
// changing while we JIT.
if (m_func->IsTopFunc())
{
m_func->SetDoFastPaths();
}
}
void
IRBuilderAsmJs::LoadNativeCodeData()
{
if (m_func->IsOOPJIT() && m_func->IsTopFunc())
{
IR::RegOpnd * nativeDataOpnd = IR::RegOpnd::New(TyVar, m_func);
IR::Instr * instr = IR::Instr::New(Js::OpCode::LdNativeCodeData, nativeDataOpnd, m_func);
this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
m_func->SetNativeCodeDataSym(nativeDataOpnd->GetStackSym());
}
}
void
IRBuilderAsmJs::AddInstr(IR::Instr * instr, uint32 offset)
{
m_lastInstr->InsertAfter(instr);
if (offset != Js::Constants::NoByteCodeOffset)
{
AssertOrFailFast(offset < m_offsetToInstructionCount);
if (m_offsetToInstruction[offset] == nullptr)
{
m_offsetToInstruction[offset] = instr;
}
else
{
Assert(m_lastInstr->GetByteCodeOffset() == offset);
}
instr->SetByteCodeOffset(offset);
}
else
{
instr->SetByteCodeOffset(m_lastInstr->GetByteCodeOffset());
}
m_lastInstr = instr;
Func *topFunc = m_func->GetTopFunc();
if (!topFunc->GetHasTempObjectProducingInstr())
{
if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
{
topFunc->SetHasTempObjectProducingInstr(true);
}
}
#if DBG_DUMP
if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::IRBuilderPhase, m_func->GetTopFunc()->GetSourceContextId(), m_func->GetTopFunc()->GetLocalFunctionId()))
{
instr->Dump();
}
#endif
}
IR::RegOpnd *
IRBuilderAsmJs::BuildDstOpnd(Js::RegSlot dstRegSlot, IRType type)
{
SymID symID = static_cast<SymID>(dstRegSlot);
if (IsLoopBody() && (RegIsVar(dstRegSlot) || RegIsJitLoopYield(dstRegSlot)))
{
// Use default symID for yield and var registers
EnsureLoopBodyAsmJsStoreSlot(dstRegSlot, type);
}
else if (RegIsTemp(dstRegSlot))
{
#if DBG
if (this->IsLoopBody())
{
// If we are doing loop body, and a temp reg slot is loaded via LdSlot
// That means that we have detected that the slot is live coming in to the loop.
// This would only happen for the value of a "with" statement, so there shouldn't
// be any def for those
Assert(!GetJitLoopBodyData().GetLdSlots()->Test(dstRegSlot));
GetJitLoopBodyData().m_usedAsTemp->Set(dstRegSlot);
}
#endif
// This is a def of a temp. Create a new sym ID for it if it's been used since its last def.
// !!!NOTE: always process an instruction's temp uses before its temp defs!!!
if (GetTempUsed(dstRegSlot))
{
symID = m_func->m_symTable->NewID();
SetTempUsed(dstRegSlot, FALSE);
SetMappedTemp(dstRegSlot, symID);
}
else
{
symID = GetMappedTemp(dstRegSlot);
// The temp hasn't been used since its last def. There are 2 possibilities:
if (symID == 0)
{
// First time we've seen the temp. Just use the number that the front end gave it.
symID = static_cast<SymID>(dstRegSlot);
SetMappedTemp(dstRegSlot, symID);
}
else if (IRType_IsSimd128(type))
{
//In Asm.js, SIMD register space is untyped, so we could have SIMD temp registers.
//Make sure that the StackSym types matches before reusing simd temps.
StackSym * stackSym = m_func->m_symTable->FindStackSym(symID);
if (!stackSym || stackSym->GetType() != type)
{
symID = m_func->m_symTable->NewID();
SetMappedTemp(dstRegSlot, symID);
}
}
}
}
else if (RegIsConstant(dstRegSlot))
{
// Don't need to track constant registers for bailout. Don't set the byte code register for constant.
dstRegSlot = Js::Constants::NoRegister;
}
else
{
// if loop body then one of the above conditions should hold true
Assert(!IsLoopBody() || dstRegSlot == 0);
}
//Simd return values of different IR types share the same reg slot.
//To avoid symbol type mismatch, use the stack symbol with a dummy simd type.
if (RegIsSimd128ReturnVar(symID))
{
type = TySimd128F4;
}
StackSym * symDst = StackSym::FindOrCreate(symID, dstRegSlot, m_func, type);
// Always reset isSafeThis to false. We'll set it to true for singleDef cases,
// but want to reset it to false if it is multi-def.
// NOTE: We could handle the multiDef if they are all safe, but it probably isn't very common.
symDst->m_isSafeThis = false;
IR::RegOpnd *regOpnd = IR::RegOpnd::New(symDst, type, m_func);
return regOpnd;
}
IR::RegOpnd *
IRBuilderAsmJs::BuildSrcOpnd(Js::RegSlot srcRegSlot, IRType type)
{
StackSym * symSrc = m_func->m_symTable->FindStackSym(BuildSrcStackSymID(srcRegSlot, type));
AssertMsg(symSrc, "Tried to use an undefined stack slot?");
IR::RegOpnd * regOpnd = IR::RegOpnd::New(symSrc, type, m_func);
return regOpnd;
}
IR::RegOpnd *
IRBuilderAsmJs::BuildIntConstOpnd(Js::RegSlot regSlot)
{
Js::Var * constTable = (Js::Var*)m_func->GetJITFunctionBody()->GetConstTable();
const WAsmJs::TypedSlotInfo& info = m_func->GetJITFunctionBody()->GetAsmJsInfo()->GetTypedSlotInfo(WAsmJs::INT32);
Assert(info.constSrcByteOffset != Js::Constants::InvalidOffset);
AssertOrFailFast(info.constSrcByteOffset < UInt32Math::Mul<sizeof(Js::Var)>(m_func->GetJITFunctionBody()->GetConstCount()));
int* intConstTable = reinterpret_cast<int*>(((byte*)constTable) + info.constSrcByteOffset);
uint32 srcReg = GetTypedRegFromRegSlot(regSlot, WAsmJs::INT32);
AssertOrFailFast(srcReg >= Js::FunctionBody::FirstRegSlot && srcReg < info.constCount);
const int32 value = intConstTable[srcReg];
IR::IntConstOpnd *opnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
return (IR::RegOpnd*)opnd;
}
SymID
IRBuilderAsmJs::BuildSrcStackSymID(Js::RegSlot regSlot, IRType type /*= IRType::TyVar*/)
{
SymID symID = static_cast<SymID>(regSlot);
if (IsLoopBody() && (RegIsVar(regSlot) || RegIsJitLoopYield(regSlot)))
{
this->EnsureLoopBodyAsmJsLoadSlot(regSlot, type);
}
else if (this->RegIsTemp(regSlot))
{
// This is a use of a temp. Map the reg slot to its sym ID.
// !!!NOTE: always process an instruction's temp uses before its temp defs!!!
symID = this->GetMappedTemp(regSlot);
if (symID == 0)
{
// We might have temps that are live through the loop body via "with" statement
// We need to treat those as if they are locals and don't remap them
Assert(this->IsLoopBody());
Assert(!GetJitLoopBodyData().m_usedAsTemp->Test(regSlot));
symID = static_cast<SymID>(regSlot);
this->SetMappedTemp(regSlot, symID);
this->EnsureLoopBodyAsmJsLoadSlot(regSlot, type);
}
this->SetTempUsed(regSlot, TRUE);
}
else
{
Assert(!IsLoopBody() || this->RegIsConstant(regSlot) || regSlot == 0);
}
return symID;
}
IR::SymOpnd *
IRBuilderAsmJs::BuildFieldOpnd(Js::RegSlot reg, Js::PropertyId propertyId, PropertyKind propertyKind, IRType type, bool scale)
{
Js::PropertyId scaledPropertyId = propertyId;
if (scale)
{
scaledPropertyId *= TySize[type];
}
PropertySym * propertySym = BuildFieldSym(reg, scaledPropertyId, propertyKind);
IR::SymOpnd * symOpnd = IR::SymOpnd::New(propertySym, type, m_func);
return symOpnd;
}
PropertySym *
IRBuilderAsmJs::BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, PropertyKind propertyKind)
{
SymID symId = BuildSrcStackSymID(reg);
AssertMsg(m_func->m_symTable->FindStackSym(symId), "Tried to use an undefined stacksym?");
PropertySym * propertySym = PropertySym::FindOrCreate(symId, propertyId, (Js::PropertyIdIndexType)-1, (uint)-1, propertyKind, m_func);
return propertySym;
}
uint
IRBuilderAsmJs::AddStatementBoundary(uint statementIndex, uint offset)
{
AssertOrFailFast(m_statementReader);
IR::PragmaInstr* pragmaInstr = IR::PragmaInstr::New(Js::OpCode::StatementBoundary, statementIndex, m_func);
this->AddInstr(pragmaInstr, offset);
return m_statementReader->MoveNextStatementBoundary();
}
uint32 IRBuilderAsmJs::GetTypedRegFromRegSlot(Js::RegSlot reg, WAsmJs::Types type)
{
const auto typedInfo = m_asmFuncInfo->GetTypedSlotInfo(type);
Js::RegSlot srcReg = reg;
if (RegIsTypedVar(reg, type))
{
srcReg = reg - GetFirstVar(type);
Assert(srcReg < typedInfo.varCount);
srcReg += typedInfo.constCount;
}
else if (RegIsTemp(reg))
{
srcReg = reg - GetFirstTmp(type);
Assert(srcReg < typedInfo.tmpCount);
srcReg += typedInfo.varCount + typedInfo.constCount;
}
else if (RegIsConstant(reg))
{
srcReg = reg - GetFirstConst(type);
Assert(srcReg < typedInfo.constCount);
}
return srcReg;
}
Js::RegSlot
IRBuilderAsmJs::GetRegSlotFromTypedReg(Js::RegSlot srcReg, WAsmJs::Types type)
{
const auto typedInfo = m_asmFuncInfo->GetTypedSlotInfo(type);
Js::RegSlot reg;
if (srcReg < typedInfo.constCount)
{
reg = srcReg + GetFirstConst(type);
Assert(reg >= GetFirstConst(type) && reg < GetLastConst(type));
return reg;
}
srcReg -= typedInfo.constCount;
if (srcReg < typedInfo.varCount)
{
reg = srcReg + GetFirstVar(type);
Assert(reg >= GetFirstVar(type) && reg < GetLastVar(type));
return reg;
}
srcReg -= typedInfo.varCount;
Assert(srcReg < typedInfo.tmpCount);
reg = srcReg + GetFirstTmp(type);
Assert(reg >= GetFirstTmp(type) && reg < GetLastTmp(type));
return reg;
}
IR::Instr *
IRBuilderAsmJs::AddExtendedArg(IR::RegOpnd *src1, IR::RegOpnd *src2, uint32 offset)
{
Assert(src1);
IR::RegOpnd * dst = IR::RegOpnd::New(src1->GetType(), m_func);
dst->SetValueType(src1->GetValueType());
IR::Instr * instr = IR::Instr::New(Js::OpCode::ExtendArg_A, dst, src1, m_func);
if (src2)
{
instr->SetSrc2(src2);
}
AddInstr(instr, offset);
return instr;
}
Js::RegSlot
IRBuilderAsmJs::GetRegSlotFromVarReg(Js::RegSlot srcVarReg)
{
Js::RegSlot reg;
if (srcVarReg < (Js::RegSlot)(AsmJsRegSlots::RegCount - 1))
{
reg = srcVarReg + m_firstVarConst;
Assert(reg >= m_firstVarConst && reg < GetFirstConst(WAsmJs::FirstType));
}
else
{
reg = srcVarReg - AsmJsRegSlots::RegCount + GetFirstTmp(WAsmJs::FirstType) - 1;
}
return reg;
}
SymID
IRBuilderAsmJs::GetMappedTemp(Js::RegSlot reg)
{
AssertMsg(RegIsTemp(reg), "Processing non-temp reg as a temp?");
AssertMsg(m_tempMap, "Processing non-temp reg without a temp map?");
Js::RegSlot tempIndex = reg - GetFirstTmp(WAsmJs::FirstType);
AssertOrFailFast(tempIndex < m_tempCount);
return m_tempMap[tempIndex];
}
void
IRBuilderAsmJs::SetMappedTemp(Js::RegSlot reg, SymID tempId)
{
AssertMsg(RegIsTemp(reg), "Processing non-temp reg as a temp?");
AssertMsg(m_tempMap, "Processing non-temp reg without a temp map?");
Js::RegSlot tempIndex = reg - GetFirstTmp(WAsmJs::FirstType);
AssertOrFailFast(tempIndex < m_tempCount);
m_tempMap[tempIndex] = tempId;
}
bool
IRBuilderAsmJs::GetTempUsed(Js::RegSlot reg)
{
AssertMsg(RegIsTemp(reg), "Processing non-temp reg as a temp?");
AssertMsg(m_fbvTempUsed, "Processing non-temp reg without a used BV?");
Js::RegSlot tempIndex = reg - GetFirstTmp(WAsmJs::FirstType);
AssertOrFailFast(tempIndex < m_tempCount);
return !!m_fbvTempUsed->Test(tempIndex);
}
void
IRBuilderAsmJs::SetTempUsed(Js::RegSlot reg, bool used)
{
AssertMsg(RegIsTemp(reg), "Processing non-temp reg as a temp?");
AssertMsg(m_fbvTempUsed, "Processing non-temp reg without a used BV?");
Js::RegSlot tempIndex = reg - GetFirstTmp(WAsmJs::FirstType);
AssertOrFailFast(tempIndex < m_tempCount);
if (used)
{
m_fbvTempUsed->Set(tempIndex);
}
else
{
m_fbvTempUsed->Clear(tempIndex);
}
}
bool
IRBuilderAsmJs::RegIsTemp(Js::RegSlot reg)
{
return reg >= GetFirstTmp(WAsmJs::FirstType);
}
bool
IRBuilderAsmJs::RegIsVar(Js::RegSlot reg)
{
for (int i = 0; i < WAsmJs::LIMIT; ++i)
{
if (RegIsTypedVar(reg, (WAsmJs::Types)i))
{
return true;
}
}
return false;
}
bool
IRBuilderAsmJs::RegIsTypedVar(Js::RegSlot reg, WAsmJs::Types type)
{
return reg >= GetFirstVar(type) && reg < GetLastVar(type);
}
bool
IRBuilderAsmJs::RegIsTypedConst(Js::RegSlot reg, WAsmJs::Types type)
{
return reg >= GetFirstConst(type) && reg < GetLastConst(type);
}
bool
IRBuilderAsmJs::RegIsTypedTmp(Js::RegSlot reg, WAsmJs::Types type)
{
return reg >= GetFirstTmp(type) && reg < GetLastTmp(type);
}
bool
IRBuilderAsmJs::RegIs(Js::RegSlot reg, WAsmJs::Types type)
{
return (
RegIsTypedVar(reg, type) ||
RegIsTypedConst(reg, type) ||
RegIsTypedTmp(reg, type)
);
}
bool
IRBuilderAsmJs::RegIsJitLoopYield(Js::RegSlot reg)
{
return IsLoopBody() && GetJitLoopBodyData().IsYieldReg(reg);
}
void
IRBuilderAsmJs::CheckJitLoopReturn(Js::RegSlot reg, IRType type)
{
if (IsLoopBody())
{
#ifdef ENABLE_WASM
if (GetJitLoopBodyData().IsLoopCurRegsInitialized())
{
Assert(m_func->GetJITFunctionBody()->IsWasmFunction());
// In Wasm we use the Return opcode for Yields
// Check if that yield is outside of the loop (this also covers return)
if (!RegIsJitLoopYield(reg))
{
WAsmJs::Types wasmType = WAsmJs::FromIRType(type);
Assert(wasmType < WAsmJs::LIMIT);
uint32 typedReg = GetTypedRegFromRegSlot(reg, wasmType);
if (GetJitLoopBodyData().IsRegOutsideOfLoop(typedReg, wasmType))
{
// The reg should be either a constant (return register)
// Or a temp, in which case, make sure we haven't mapped that temp to something else
Assert(RegIsConstant(reg) || (RegIsTemp(reg) && (GetMappedTemp(reg) == (SymID)reg || GetMappedTemp(reg) == 0)));
GetJitLoopBodyData().SetRegIsYield(reg);
EnsureLoopBodyAsmJsStoreSlot(reg, type);
}
}
}
else
#endif
{
// In Asm.js there is no Yields outside of the loop, if we're here
// It means we are returning a value from the loop
Assert(RegIsConstant(reg));
EnsureLoopBodyAsmJsStoreSlot(reg, type);
}
}
}
bool
IRBuilderAsmJs::RegIsSimd128ReturnVar(Js::RegSlot reg)
{
return (reg == GetFirstConst(WAsmJs::SIMD) &&
Js::AsmJsRetType(m_asmFuncInfo->GetRetType()).toVarType().isSIMD());
}
bool
IRBuilderAsmJs::RegIsConstant(Js::RegSlot reg)
{
return (reg > 0 && reg < GetLastConst(WAsmJs::LastType));
}
BranchReloc *
IRBuilderAsmJs::AddBranchInstr(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
{
AssertOrFailFast(targetOffset <= m_func->GetJITFunctionBody()->GetByteCodeLength());
//
// Loop jitting would be done only till the LoopEnd
// Any branches beyond that offset are for the return statement
//
if (IsLoopBodyOuterOffset(targetOffset))
{
// if we have loaded the loop IP sym from the ProfiledLoopEnd then don't add it here
if (!IsLoopBodyReturnIPInstr(m_lastInstr))
{
this->InsertLoopBodyReturnIPInstr(targetOffset, offset);
}
// Jump the restore StSlot and Ret instruction
targetOffset = GetLoopBodyExitInstrOffset();
}
BranchReloc * reloc = nullptr;
reloc = CreateRelocRecord(branchInstr, offset, targetOffset);
AddInstr(branchInstr, offset);
return reloc;
}
BranchReloc *
IRBuilderAsmJs::CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
{
BranchReloc * reloc = JitAnew(m_tempAlloc, BranchReloc, branchInstr, offset, targetOffset);
m_branchRelocList->Prepend(reloc);
return reloc;
}
void
IRBuilderAsmJs::BuildHeapBufferReload(uint32 offset, bool isFirstLoad)
{
enum ShouldReload
{
DoReload,
DontReload
};
const auto AddLoadField = [&](AsmJsRegSlots::ConstSlots dst, AsmJsRegSlots::ConstSlots src, int32 fieldOffset, IRType type, ShouldReload shouldReload)
{
if (isFirstLoad || shouldReload == DoReload)
{
IR::RegOpnd * dstOpnd = BuildDstOpnd(dst, type);
IR::Opnd * srcOpnd = IR::IndirOpnd::New(BuildSrcOpnd(src, type), fieldOffset, type, m_func);
IR::Instr * instr = IR::Instr::New(Js::OpCode::Ld_A, dstOpnd, srcOpnd, m_func);
AddInstr(instr, offset);
}
};
#ifdef ENABLE_WASM
const bool isWasm = m_func->GetJITFunctionBody()->IsWasmFunction();
const bool isSharedMem = m_func->GetJITFunctionBody()->GetAsmJsInfo()->IsSharedMemory();
if(isWasm)
{
// WebAssembly.Memory only needs to be loaded once as it can't change over the course of the function
AddLoadField(AsmJsRegSlots::WasmMemoryReg, AsmJsRegSlots::ModuleMemReg, (int32)Js::WebAssemblyModule::GetMemoryOffset(), TyVar, DontReload);
if (!isSharedMem)
{
// ArrayBuffer
// GrowMemory can change the ArrayBuffer, we have to reload it
AddLoadField(AsmJsRegSlots::ArrayReg, AsmJsRegSlots::WasmMemoryReg, Js::WebAssemblyMemory::GetOffsetOfArrayBuffer(), TyVar, DoReload);
// The buffer doesn't change when using Fast Virtual buffer even if we grow the memory
ShouldReload shouldReloadBufferPointer = m_func->GetJITFunctionBody()->UsesWAsmJsFastVirtualBuffer() ? DontReload : DoReload;
// ArrayBuffer.bufferContent
AddLoadField(AsmJsRegSlots::RefCountedBuffer, AsmJsRegSlots::ArrayReg, Js::ArrayBuffer::GetBufferContentsOffset(), TyVar, DoReload);
// RefCountedBuffer.buffer
AddLoadField(AsmJsRegSlots::BufferReg, AsmJsRegSlots::RefCountedBuffer, Js::RefCountedBuffer::GetBufferOffset(), TyVar, shouldReloadBufferPointer);
// ArrayBuffer.length
AddLoadField(AsmJsRegSlots::LengthReg, AsmJsRegSlots::ArrayReg, Js::ArrayBuffer::GetByteLengthOffset(), TyUint32, DoReload);
}
else
{
// SharedArrayBuffer
// SharedArrayBuffer cannot be detached and the buffer cannot change, no need to reload
AddLoadField(AsmJsRegSlots::ArrayReg, AsmJsRegSlots::WasmMemoryReg, Js::WebAssemblyMemory::GetOffsetOfArrayBuffer(), TyVar, DontReload);
// SharedArrayBuffer.SharedContents
AddLoadField(AsmJsRegSlots::SharedContents, AsmJsRegSlots::ArrayReg, Js::SharedArrayBuffer::GetSharedContentsOffset(), TyVar, DontReload);
// SharedContents.buffer
AddLoadField(AsmJsRegSlots::BufferReg, AsmJsRegSlots::SharedContents, Js::SharedContents::GetBufferOffset(), TyVar, DontReload);
// SharedContents.length
AddLoadField(AsmJsRegSlots::LengthReg, AsmJsRegSlots::SharedContents, Js::SharedContents::GetBufferLengthOffset(), TyUint32, DoReload);
}
}
else
#endif
{
// ArrayBuffer
// The ArrayBuffer can be changed on the environment, if it is detached, we'll throw
AddLoadField(AsmJsRegSlots::ArrayReg, AsmJsRegSlots::ModuleMemReg, (int32)Js::AsmJsModuleMemory::MemoryTableBeginOffset, TyVar, DontReload);
// ArrayBuffer.bufferContent
AddLoadField(AsmJsRegSlots::RefCountedBuffer, AsmJsRegSlots::ArrayReg, Js::ArrayBuffer::GetBufferContentsOffset(), TyVar, DontReload);
// RefCountedBuffer.buffer
AddLoadField(AsmJsRegSlots::BufferReg, AsmJsRegSlots::RefCountedBuffer, Js::RefCountedBuffer::GetBufferOffset(), TyVar, DontReload);
// ArrayBuffer.length
AddLoadField(AsmJsRegSlots::LengthReg, AsmJsRegSlots::ArrayReg, Js::ArrayBuffer::GetByteLengthOffset(), TyUint32, DontReload);
}
}
template<typename T, typename ConstOpnd, typename F>
void IRBuilderAsmJs::CreateLoadConstInstrForType(
byte* table,
Js::RegSlot& regAllocated,
uint32 constCount,
uint32 byteOffset,
IRType irType,
ValueType valueType,
Js::OpCode opcode,
F extraProcess
)
{
T* typedTable = (T*)(table + byteOffset);
AssertOrFailFast(byteOffset < UInt32Math::Mul<sizeof(Js::Var)>(m_func->GetJITFunctionBody()->GetConstCount()));
AssertOrFailFast(AllocSizeMath::Add((size_t)typedTable, UInt32Math::Mul<sizeof(T)>(constCount)) <= (size_t)((Js::Var*)m_func->GetJITFunctionBody()->GetConstTable() + m_func->GetJITFunctionBody()->GetConstCount()));
// 1 for return register
++regAllocated;
++typedTable;
for (uint32 i = 1; i < constCount; ++i)
{
uint32 reg = regAllocated++;
T constVal = *typedTable++;
IR::RegOpnd * dstOpnd = BuildDstOpnd(reg, irType);
Assert(RegIsConstant(reg));
dstOpnd->m_sym->SetIsFromByteCodeConstantTable();
dstOpnd->SetValueType(valueType);
IR::Instr *instr = IR::Instr::New(opcode, dstOpnd, ConstOpnd::New(constVal, irType, m_func), m_func);
extraProcess(instr, constVal);
AddInstr(instr, Js::Constants::NoByteCodeOffset);
}
}
void
IRBuilderAsmJs::BuildConstantLoads()
{
Js::Var * constTable = (Js::Var *)m_func->GetJITFunctionBody()->GetConstTable();
// Load FrameDisplay
IR::RegOpnd * asmJsEnvDstOpnd = BuildDstOpnd(AsmJsRegSlots::ModuleMemReg, TyVar);
IR::Instr * ldAsmJsEnvInstr = IR::Instr::New(Js::OpCode::LdAsmJsEnv, asmJsEnvDstOpnd, m_func);
AddInstr(ldAsmJsEnvInstr, Js::Constants::NoByteCodeOffset);
// Load heap buffer
if (m_asmFuncInfo->UsesHeapBuffer())
{
BuildHeapBufferReload(Js::Constants::NoByteCodeOffset, true);
}
if (!constTable)
{
return;
}
uint32 regAllocated = AsmJsRegSlots::RegCount;
byte* table = (byte*)constTable;
const bool isOOPJIT = m_func->IsOOPJIT();
for (int i = 0; i < WAsmJs::LIMIT; ++i)
{
WAsmJs::Types type = (WAsmJs::Types)i;
WAsmJs::TypedSlotInfo info = m_asmFuncInfo->GetTypedSlotInfo(type);
if (info.constCount == 0)
{
continue;
}
switch(type)
{
case WAsmJs::INT32:
CreateLoadConstInstrForType<int32, IR::IntConstOpnd>(
table,
regAllocated,
info.constCount,
info.constSrcByteOffset,
TyInt32,
ValueType::GetInt(false),
Js::OpCode::Ld_I4,
[isOOPJIT](IR::Instr* instr, int32 val)
{
IR::RegOpnd* dstOpnd = instr->GetDst()->AsRegOpnd();
if (!isOOPJIT && dstOpnd->m_sym->IsSingleDef())
{
dstOpnd->m_sym->SetIsIntConst(val);
}
}
);
break;
#if TARGET_64
case WAsmJs::INT64:
CreateLoadConstInstrForType<int64, IR::Int64ConstOpnd>(
table,
regAllocated,
info.constCount,
info.constSrcByteOffset,
TyInt64,
ValueType::GetInt(false),
Js::OpCode::Ld_I4,
[&](IR::Instr* instr, int64 val) {}
);
break;
#endif
case WAsmJs::FLOAT32:
CreateLoadConstInstrForType<float, IR::Float32ConstOpnd>(
table,
regAllocated,
info.constCount,
info.constSrcByteOffset,
TyFloat32,
ValueType::Float,
Js::OpCode::LdC_F8_R8,
[isOOPJIT](IR::Instr* instr, float val)
{
#if _M_IX86
IR::RegOpnd* dstOpnd = instr->GetDst()->AsRegOpnd();
if (!isOOPJIT && dstOpnd->m_sym->IsSingleDef())
{
dstOpnd->m_sym->SetIsFloatConst();
}
#endif
}
);
break;
case WAsmJs::FLOAT64:
CreateLoadConstInstrForType<double, IR::FloatConstOpnd>(
table,
regAllocated,
info.constCount,
info.constSrcByteOffset,
TyFloat64,
ValueType::Float,
Js::OpCode::LdC_F8_R8,
[isOOPJIT](IR::Instr* instr, double val)
{
#if _M_IX86
IR::RegOpnd* dstOpnd = instr->GetDst()->AsRegOpnd();
if (!isOOPJIT && dstOpnd->m_sym->IsSingleDef())
{
dstOpnd->m_sym->SetIsFloatConst();
}
#endif
}
);
break;
case WAsmJs::SIMD:
CreateLoadConstInstrForType<AsmJsSIMDValue, IR::Simd128ConstOpnd>(
table,
regAllocated,
info.constCount,
info.constSrcByteOffset,
TySimd128F4,
ValueType::UninitializedObject,
Js::OpCode::Simd128_LdC,
[isOOPJIT](IR::Instr* instr, AsmJsSIMDValue val)
{
#if _M_IX86
IR::RegOpnd* dstOpnd = instr->GetDst()->AsRegOpnd();
if (!isOOPJIT && dstOpnd->m_sym->IsSingleDef())
{
dstOpnd->m_sym->SetIsSimd128Const();
}
#endif
}
);
break;
default:
Assert(false);
break;
}
}
}
void
IRBuilderAsmJs::BuildImplicitArgIns()
{
int32 intArgInCount = 0;
int32 int64ArgInCount = 0;
int32 floatArgInCount = 0;
int32 doubleArgInCount = 0;
int32 simd128ArgInCount = 0;
// formal params are offset from EBP by the EBP chain, return address, and function object
int32 offset = 3 * MachPtr;
for (Js::ArgSlot i = 1; i < m_func->GetJITFunctionBody()->GetInParamsCount(); ++i)
{
StackSym * symSrc = nullptr;
IR::Opnd * srcOpnd = nullptr;
IR::RegOpnd * dstOpnd = nullptr;
IR::Instr * instr = nullptr;
// TODO: double args are not aligned on stack
Js::AsmJsVarType varType = m_func->GetJITFunctionBody()->GetAsmJsInfo()->GetArgType(i - 1);
switch (varType.which())
{
case Js::AsmJsVarType::Which::Int:
symSrc = StackSym::NewParamSlotSym(i, m_func, TyInt32);
m_func->SetArgOffset(symSrc, offset);
srcOpnd = IR::SymOpnd::New(symSrc, TyInt32, m_func);
dstOpnd = BuildDstOpnd(GetFirstVar(WAsmJs::INT32) + intArgInCount, TyInt32);
dstOpnd->SetValueType(ValueType::GetInt(false));
instr = IR::Instr::New(Js::OpCode::ArgIn_A, dstOpnd, srcOpnd, m_func);
offset += MachPtr;
++intArgInCount;
break;
case Js::AsmJsVarType::Which::Float:
symSrc = StackSym::NewParamSlotSym(i, m_func, TyFloat32);
m_func->SetArgOffset(symSrc, offset);
srcOpnd = IR::SymOpnd::New(symSrc, TyFloat32, m_func);
dstOpnd = BuildDstOpnd(GetFirstVar(WAsmJs::FLOAT32) + floatArgInCount, TyFloat32);
dstOpnd->SetValueType(ValueType::Float);
instr = IR::Instr::New(Js::OpCode::ArgIn_A, dstOpnd, srcOpnd, m_func);
offset += MachPtr;
++floatArgInCount;
break;
case Js::AsmJsVarType::Which::Double:
symSrc = StackSym::NewParamSlotSym(i, m_func, TyFloat64);
m_func->SetArgOffset(symSrc, offset);
srcOpnd = IR::SymOpnd::New(symSrc, TyFloat64, m_func);
dstOpnd = BuildDstOpnd(GetFirstVar(WAsmJs::FLOAT64) + doubleArgInCount, TyFloat64);
dstOpnd->SetValueType(ValueType::Float);
instr = IR::Instr::New(Js::OpCode::ArgIn_A, dstOpnd, srcOpnd, m_func);
offset += MachDouble;
++doubleArgInCount;
break;
case Js::AsmJsVarType::Which::Int64:
symSrc = StackSym::NewParamSlotSym(i, m_func, TyInt64);
m_func->SetArgOffset(symSrc, offset);
srcOpnd = IR::SymOpnd::New(symSrc, TyInt64, m_func);
dstOpnd = BuildDstOpnd(GetFirstVar(WAsmJs::INT64) + int64ArgInCount, TyInt64);