-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAsmJsJitTemplate.cpp
4667 lines (4016 loc) · 219 KB
/
AsmJsJitTemplate.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 "RuntimeLanguagePch.h"
#include "RuntimeMathPch.h"
#if ENABLE_NATIVE_CODEGEN
#include "../Backend/i386/Reg.h"
static const BYTE RegEncode[] =
{
#define REGDAT(Name, Listing, Encoding, ...) Encoding,
#include "../Backend/i386/RegList.h"
#undef REGDAT
};
#if DBG_DUMP || ENABLE_DEBUG_CONFIG_OPTIONS
extern char16 const * const RegNamesW[];
#endif
#include "AsmJsInstructionTemplate.h"
namespace Js
{
// Mask of Registers that can be saved through function calls
const uint MaskNonVolatileReg = 1 << RegEBX | 1 << RegEDI | 1 << RegESI;
// Reserved RegEDI for ArrayBuffer length
const RegNum ModuleEnvReg = RegEDI;
const RegNum ArrayBufferReg = RegESI;
// Registers that can't be chosen for general purposes
const uint MaskUnavailableReg = 1 << RegESP | 1 << RegEBP | 1 << ModuleEnvReg | 1 << ArrayBufferReg | 1 << RegNOREG;
// Mask for Register in enum RegNum [EAX,ECX,EDX,EBX,ESI,EDI]
const uint Mask32BitsReg = ( ( 1 << ( FIRST_FLOAT_REG ) ) - 1 ) & ~MaskUnavailableReg ;
// Mask for Register in enum RegNum [EAX,ECX,EDX,EBX] aka [al,cl,dl,bl]
const uint Mask8BitsReg = Mask32BitsReg &~(1<<RegEBP|1<<RegESP|1<<RegESI|1<<RegEDI);
// Mask for Register in enum RegNum [XMM0,XMM1,XMM2,XMM3,XMM4,XMM5,XMM6,XMM7]
const uint Mask64BitsReg = ((1 << (FIRST_FLOAT_REG+XMM_REGCOUNT))-1) & ~MaskUnavailableReg & ~Mask32BitsReg;
// Template version to access register mask
template<typename T> uint GetRegMask();
template<> uint GetRegMask<int>() { return Mask32BitsReg; }
template<> uint GetRegMask<double>() { return Mask64BitsReg; }
template<> uint GetRegMask<float>() { return Mask64BitsReg; }
template<> uint GetRegMask<AsmJsSIMDValue>() { return Mask64BitsReg; }
// Template version to access first register available
template<typename T> RegNum GetFirstReg();
template<> RegNum GetFirstReg<int>() { return FIRST_INT_REG; }
template<> RegNum GetFirstReg<double>() { return FIRST_FLOAT_REG; }
template<> RegNum GetFirstReg<float>() { return FIRST_FLOAT_REG; }
template<> RegNum GetFirstReg<AsmJsSIMDValue>() { return FIRST_FLOAT_REG; }
// Returns the last register available + 1, forms an upper bound [GetFirstReg, GetLastReg[
template<typename T> RegNum GetLastReg() { return RegNum(GetFirstReg<T>()+8); }
struct InternalCallInfo
{
// size in bytes of arguments
int argByteSize;
int nextArgIndex;
int currentOffset;
InternalCallInfo* next;
};
struct X86TemplateData
{
private:
InternalCallInfo* mCallInfoList;
// Bit vector : 1 means a useful information is known for this RegNum.
// Never set an unavailable register flag to 1
int mAnyStackSaved;
// Stack offset saved for registers
int mRegisterStackOffsetSaved[RegNumCount];
// Value range [0,8[ add GetFirstReg() for RegNum
RegNum mNext32BitsReg, mNext64BitsReg;
// Template version to access the Next Register
template<typename T> RegNum GetNextRegister();
template<typename T> void SetNextRegister(RegNum reg);
int mBaseOffset;
int mScriptContextOffSet;
int mModuleSlotOffset;
int mModuleEnvOffset;
int mArrayBufferOffSet;
int mArraySizeOffset;
// Applies the register choosing algorithm and returns it
template<typename T> RegNum GetNextReg(RegNum reg);
template<> RegNum GetNextReg<int>(RegNum reg)
{
return RegNum((reg + 1) % GetLastReg<int>());
}
template<> RegNum GetNextReg<double>(RegNum reg)
{
RegNum nextReg = RegNum((reg + 1) % GetLastReg<double>());
if (nextReg < GetFirstReg<double>())
{
return RegNum(GetFirstReg<double>());
}
return nextReg;
}
template<typename T> RegNum NextReg(const int registerRestriction)
{
RegNum reg = GetNextRegister<T>();
const uint unavailable = registerRestriction | MaskUnavailableReg;
Assert( unavailable != GetRegMask<T>() );
if( (1<<reg) & unavailable )
{
while( (1<<reg) & unavailable )
{
reg = GetNextReg<T>(reg);
}
Assert( !(1 << reg & unavailable) );
return reg; // do not change the next register
}
RegNum next = reg;
do
{
next = GetNextReg<T>(next);
} while( ( 1 << next ) & MaskUnavailableReg );
SetNextRegister<T>( next );
Assert( !(1 << reg & unavailable) );
return reg;
}
public:
X86TemplateData()
{
Assert( !( (1<<GetFirstReg<int>()) & MaskUnavailableReg ) );
Assert(!((1 << GetFirstReg<double>()) & MaskUnavailableReg));
Assert(!((1 << GetFirstReg<float>()) & MaskUnavailableReg));
mNext32BitsReg = GetFirstReg<int>();
mNext64BitsReg = GetFirstReg<double>(); // it is the same for float
mAnyStackSaved = 0;
mCallInfoList = nullptr;
for (int i = 0; i < RegNumCount ; i++)
{
mRegisterStackOffsetSaved[i] = 0;
}
}
~X86TemplateData()
{
Assert( !mCallInfoList );
}
InternalCallInfo* GetInternalCallInfo() const
{
return mCallInfoList;
}
void StartInternalCall( int argSizeByte )
{
InternalCallInfo* info = HeapNew( InternalCallInfo );
info->argByteSize = argSizeByte;
info->currentOffset = 0;
info->nextArgIndex = 1;
info->next = mCallInfoList;
mCallInfoList = info;
}
void InternalCallDone()
{
Assert( mCallInfoList );
Assert( mCallInfoList->currentOffset + MachPtr == mCallInfoList->argByteSize );
InternalCallInfo* next = mCallInfoList->next;
HeapDelete( mCallInfoList );
mCallInfoList = next;
}
// Tells this register is holding the content located at the stackOffset
void SetStackInfo( RegNum reg, int stackOffset )
{
Assert( !( 1 << reg & MaskUnavailableReg ) );
mRegisterStackOffsetSaved[reg] = stackOffset;
mAnyStackSaved |= 1 << reg;
}
// Call when register content is data dependent
void InvalidateReg( RegNum reg )
{
mAnyStackSaved &= ~( 1 << reg );
}
void InvalidateAllVolatileReg()
{
mAnyStackSaved &= MaskNonVolatileReg;
}
void InvalidateAllReg()
{
mAnyStackSaved = 0;
}
// Call when stack value has changed
void OverwriteStack( int stackOffset )
{
if( mAnyStackSaved )
{
// check all register with a stack offset saved
int stackSavedReg = mAnyStackSaved;
int reg = 0;
while( stackSavedReg )
{
// skip reg with no stack info
while( !(stackSavedReg & 1) )
{
stackSavedReg >>= 1;
++reg;
}
// invalidate register with this stack location
if( mRegisterStackOffsetSaved[reg] == stackOffset )
{
InvalidateReg( RegNum( reg ) );
}
// next register
stackSavedReg >>= 1;
++reg;
}
}
}
// Gets a register to use
// registerRestriction : bit vector, 1 means the register cannot be chosen
template<typename T> RegNum GetReg(const int registerRestriction = 0)
{
CompileAssert( sizeof(T) == 4 || sizeof(T) == 8 );
const int mask = GetRegMask<T>() & ~registerRestriction;
int stackSavedReg = mAnyStackSaved & mask;
// No more register available
if( stackSavedReg == mask )
{
RegNum reg = NextReg<T>(registerRestriction);
Assert( !(1 << reg & registerRestriction) );
return reg;
}
// making sure we don't choose the unavailable registers
stackSavedReg |= MaskUnavailableReg|registerRestriction;
int reg = GetFirstReg<T>();
stackSavedReg >>= reg;
// will always find a value under these conditions
while( 1 )
{
// if the register hold no useful info, return it
if( !( stackSavedReg & 1 ) )
{
Assert( !(1 << reg & registerRestriction) );
return RegNum( reg );
}
stackSavedReg >>= 1;
++reg;
}
}
// Gets a register to use
// registerRestriction : bit vector, 1 means the register cannot be chosen
template<> RegNum GetReg<float>(const int registerRestriction)
{
const int mask = GetRegMask<double>() & ~registerRestriction;
int stackSavedReg = mAnyStackSaved & mask;
// No more register available
if (stackSavedReg == mask)
{
RegNum reg = NextReg<double>(registerRestriction);
Assert(!(1 << reg & registerRestriction));
return reg;
}
// making sure we don't choose the unavailable registers
stackSavedReg |= MaskUnavailableReg | registerRestriction;
int reg = GetFirstReg<double>();
stackSavedReg >>= reg;
// will always find a value under these conditions
while (1)
{
// if the register hold no useful info, return it
if (!(stackSavedReg & 1))
{
Assert(!(1 << reg & registerRestriction));
return RegNum(reg);
}
stackSavedReg >>= 1;
++reg;
}
}
template<> RegNum GetReg<AsmJsSIMDValue>(const int registerRestriction)
{
return GetReg<float>(registerRestriction);
}
// Search for a register already holding the value at this location
template<typename T> bool FindRegWithStackOffset( RegNum& outReg, int stackOffset, int registerRestriction = 0 )
{
CompileAssert( sizeof(T) == 4 || sizeof(T) == 8 || sizeof(T) == 16);
int stackSavedReg = mAnyStackSaved & GetRegMask<T>() & ~registerRestriction;
if( stackSavedReg )
{
int reg = GetFirstReg<T>();
stackSavedReg >>= reg;
while( stackSavedReg )
{
// skip reg with no stack info
while( !(stackSavedReg & 1) )
{
stackSavedReg >>= 1;
++reg;
}
// invalidate register with this stack location
if( mRegisterStackOffsetSaved[reg] == stackOffset )
{
outReg = RegNum( reg );
return true;
}
// next register
stackSavedReg >>= 1;
++reg;
}
}
return false;
}
void SetBaseOffset(int baseOffSet)
{
// We subtract with the baseoffset as the layout of the stack has changed from the interpreter
// Assume Stack is growing downwards
// Interpreter - Stack is above EBP and offsets are positive
// TJ - Stack is below EBP and offsets are negative
mBaseOffset = baseOffSet;
mModuleSlotOffset = AsmJsJitTemplate::Globals::ModuleSlotOffset - mBaseOffset;
mModuleEnvOffset = AsmJsJitTemplate::Globals::ModuleEnvOffset - mBaseOffset;
mArrayBufferOffSet = AsmJsJitTemplate::Globals::ArrayBufferOffset - mBaseOffset;
mArraySizeOffset = AsmJsJitTemplate::Globals::ArraySizeOffset - mBaseOffset;
mScriptContextOffSet = AsmJsJitTemplate::Globals::ScriptContextOffset - mBaseOffset;
}
int GetBaseOffSet()
{
return mBaseOffset;
}
int GetModuleSlotOffset()
{
return mModuleSlotOffset;
}
int GetModuleEnvOffset()
{
return mModuleEnvOffset;
}
int GetArrayBufferOffset()
{
return mArrayBufferOffSet;
}
int GetArraySizeOffset()
{
return mArraySizeOffset;
}
int GetScriptContextOffset()
{
return mScriptContextOffSet;
}
const int GetCalleSavedRegSizeInByte()
{
//EBX,ESI,EDI
return 3 * sizeof(void*);
}
const int GetEBPOffsetCorrection()
{
//We computed the offset in BCG adjusting for push ebp and ret address
return 2 * sizeof(void*);
}
};
template<> RegNum X86TemplateData::GetNextRegister<int>() { return mNext32BitsReg; }
template<> RegNum X86TemplateData::GetNextRegister<double>() { return mNext64BitsReg; }
template<> void X86TemplateData::SetNextRegister<int>(RegNum reg) { mNext32BitsReg = reg; }
template<> void X86TemplateData::SetNextRegister<double>(RegNum reg) { mNext64BitsReg = reg; }
struct ReturnContent
{
union
{
int intVal;
double doubleVal;
};
template<typename T> T GetReturnVal()const;
#if DBG_DUMP
template<typename T> void Print()const;
#endif
};
template<> int ReturnContent::GetReturnVal<int>()const
{
return intVal;
}
template<> float ReturnContent::GetReturnVal<float>()const
{
return (float)doubleVal;
}
template<> double ReturnContent::GetReturnVal<double>()const
{
return doubleVal;
}
#if DBG_DUMP
template<> void ReturnContent::Print<int>()const
{
Output::Print( _u(" = %d"), intVal );
}
template<> void ReturnContent::Print<double>()const
{
Output::Print( _u(" = %.4f"), doubleVal );
}
template<> void ReturnContent::Print<float>()const
{
Output::Print( _u(" = %.4f"), doubleVal );
}
int AsmJsCallDepth = 0;
#endif
uint CallLoopBody(JavascriptMethod address, ScriptFunction* function, Var frameAddress)
{
void *savedEsp = NULL;
__asm
{
// Save ESP
mov savedEsp, esp
// Add an extra 4-bytes to the stack since we'll be pushing 3 arguments
push eax
}
uint newOffset = (uint)address(function, CallInfo(CallFlags_InternalFrame, 1), frameAddress);
_asm
{
// Restore ESP
mov esp, savedEsp
}
return newOffset;
}
uint DoLoopBodyStart(Js::ScriptFunction* function,Var ebpPtr,uint32 loopNumber)
{
FunctionBody* fn = function->GetFunctionBody();
Assert(loopNumber < fn->GetLoopCount());
Js::LoopHeader *loopHeader = fn->GetLoopHeader(loopNumber);
Js::LoopEntryPointInfo * entryPointInfo = loopHeader->GetCurrentEntryPointInfo();
ScriptContext* scriptContext = fn->GetScriptContext();
// If we have JITted the loop, call the JITted code
if (entryPointInfo != NULL && entryPointInfo->IsCodeGenDone())
{
#if DBG_DUMP
if (PHASE_TRACE1(Js::JITLoopBodyPhase) && CONFIG_FLAG(Verbose))
{
fn->DumpFunctionId(true);
Output::Print(_u(": %-20s LoopBody Execute Loop: %2d\n"), fn->GetDisplayName(), loopNumber);
Output::Flush();
}
loopHeader->nativeCount++;
#endif
#ifdef BGJIT_STATS
entryPointInfo->MarkAsUsed();
#endif
Assert(entryPointInfo->jsMethod);
uint newOffset = CallLoopBody(entryPointInfo->jsMethod, function, ebpPtr);
ptrdiff_t value = NULL;
fn->GetAsmJsFunctionInfo()->mbyteCodeTJMap->TryGetValue(newOffset, &value);
Assert(value != NULL); // value cannot be null
BYTE* newAddress = fn->GetAsmJsFunctionInfo()->mTJBeginAddress + value;
Assert(newAddress);
return (uint)newAddress;
}
// interpreCount for loopHeader is incremented before calling DoLoopBody
const uint loopInterpretCount = fn->GetLoopInterpretCount(loopHeader);
if (loopHeader->interpretCount > loopInterpretCount)
{
if (!fn->DoJITLoopBody())
{
return 0;
}
// If the job is not scheduled then we need to schedule it now.
// It is possible a job was scheduled earlier and we find ourselves looking at the same entry point
// again. For example, if the function with the loop was JITed and bailed out then as we finish
// the call in the interpreter we might encounter a loop for which we had scheduled a JIT job before
// the function was initially scheduled. In such cases, that old JIT job will complete. If it completes
// successfully then we can go ahead and use it. If it fails then it will eventually revert to the
// NotScheduled state. Since transitions from NotScheduled can only occur on the main thread,
// by checking the state we are safe from racing with the JIT thread when looking at the other fields
// of the entry point.
if (entryPointInfo != NULL && entryPointInfo->IsNotScheduled())
{
entryPointInfo->SetIsAsmJSFunction(true);
entryPointInfo->SetIsTJMode(true);
GenerateLoopBody(scriptContext->GetNativeCodeGenerator(), fn, loopHeader, entryPointInfo, fn->GetLocalsCount(), &ebpPtr);
//reset InterpretCount
loopHeader->interpretCount = 0;
}
}
return 0;
}
// Function memory allocation should be done the same way as
// void InterpreterStackFrame::AlignMemoryForAsmJs() (InterpreterStackFrame.cpp)
// update any changes there
void AsmJsCommonEntryPoint(Js::ScriptFunction* func, void* localSlot, void* args)
{
FunctionBody* body = func->GetFunctionBody();
Js::FunctionEntryPointInfo * entryPointInfo = body->GetDefaultFunctionEntryPointInfo();
const uint32 minTemplatizedJitRunCount = (uint32)CONFIG_FLAG(MinTemplatizedJitRunCount);
if ((entryPointInfo->IsNotScheduled() || entryPointInfo->IsCodeGenDone()) && (entryPointInfo->callsCount >= minTemplatizedJitRunCount || body->IsHotAsmJsLoop()))
{
WAsmJs::JitFunctionIfReady(func, 9999);
}
void* constTable = body->GetConstTable();
constTable = (void*)(((Var*)constTable)+AsmJsFunctionMemory::RequiredVarConstants-1);
AsmJsFunctionInfo* asmInfo = body->GetAsmJsFunctionInfo();
const int intConstCount = asmInfo->GetIntConstCount();
const int doubleConstCount = asmInfo->GetDoubleConstCount();
const int floatConstCount = asmInfo->GetFloatConstCount();
// Offset of doubles from (double*)m_localSlot
const int intOffsets = asmInfo->GetIntByteOffset() / sizeof(int);
const int doubleOffsets = asmInfo->GetDoubleByteOffset() / sizeof(double);
const int floatOffset = asmInfo->GetFloatByteOffset() / sizeof(float);
int argoffset = (int)args;
// initialize argument location
int* intArg;
double* doubleArg;
float* floatArg;
AsmJsSIMDValue* simdArg;
// setup stack memory
AsmJsScriptFunction* asmJsFunc = VarTo<AsmJsScriptFunction>(func);
Var moduleEnv = asmJsFunc->GetModuleEnvironment();
JavascriptArrayBuffer* arrayBuffer = asmJsFunc->GetAsmJsArrayBuffer();
int arraySize = 0;
BYTE* arrayPtr = nullptr;
if (VarIsCorrectType<ArrayBuffer>(arrayBuffer))
{
arrayPtr = arrayBuffer->GetBuffer();
arraySize = arrayBuffer->GetByteLength();
}
Var* m_localSlots;
int* m_localIntSlots;
double* m_localDoubleSlots;
float* m_localFloatSlots;
#if DBG_DUMP
const bool tracingFunc = PHASE_TRACE( AsmjsFunctionEntryPhase, body );
if( tracingFunc )
{
if( AsmJsCallDepth )
{
Output::Print( _u("%*c"), AsmJsCallDepth,' ');
}
Output::Print( _u("Executing function %s("), body->GetDisplayName());
++AsmJsCallDepth;
}
#endif
{
m_localSlots = (Var*)localSlot;
const ArgSlot argCount = asmInfo->GetArgCount();
m_localSlots[AsmJsFunctionMemory::ModuleEnvRegister] = moduleEnv;
m_localSlots[AsmJsFunctionMemory::ArrayBufferRegister] = (Var)arrayPtr;
m_localSlots[AsmJsFunctionMemory::ArraySizeRegister] = (Var)arraySize;
m_localSlots[AsmJsFunctionMemory::ScriptContextBufferRegister] = body->GetScriptContext();
m_localIntSlots = ((int*)m_localSlots) + intOffsets;
memcpy_s(m_localIntSlots, intConstCount*sizeof(int), constTable, intConstCount*sizeof(int));
constTable = (void*)(((int*)constTable) + intConstCount);
m_localFloatSlots = ((float*)m_localSlots) + floatOffset;
memcpy_s(m_localFloatSlots, floatConstCount*sizeof(float), constTable, floatConstCount*sizeof(float));
constTable = (void*)(((float*)constTable) + floatConstCount);
m_localDoubleSlots = ((double*)m_localSlots) + doubleOffsets;
memcpy_s(m_localDoubleSlots, doubleConstCount*sizeof(double), constTable, doubleConstCount*sizeof(double));
intArg = m_localIntSlots + intConstCount;
doubleArg = m_localDoubleSlots + doubleConstCount;
floatArg = m_localFloatSlots + floatConstCount;
for(ArgSlot i = 0; i < argCount; i++ )
{
if(asmInfo->GetArgType(i).isInt())
{
__asm
{
mov eax, argoffset
mov eax, [eax]
mov ecx, intArg
mov [ecx], eax
};
#if DBG_DUMP
if( tracingFunc )
{
Output::Print( _u(" %d%c"), *intArg, i+1 < argCount ? ',':' ');
}
#endif
++intArg;
argoffset += sizeof( int );
}
else if (asmInfo->GetArgType(i).isFloat())
{
__asm
{
mov eax, argoffset
movss xmm0, [eax]
mov eax, floatArg
movss[eax], xmm0
};
#if DBG_DUMP
if (tracingFunc)
{
Output::Print(_u(" %.4f%c"), *floatArg, i + 1 < argCount ? ',' : ' ');
}
#endif
++floatArg;
argoffset += sizeof(float);
}
else if (asmInfo->GetArgType(i).isDouble())
{
__asm
{
mov eax, argoffset
movsd xmm0, [eax]
mov eax, doubleArg
movsd [eax], xmm0
};
#if DBG_DUMP
if( tracingFunc )
{
Output::Print( _u(" %.4f%c"), *doubleArg, i+1 < argCount ? ',':' ');
}
#endif
++doubleArg;
argoffset += sizeof( double );
}
else if (asmInfo->GetArgType(i).isSIMD())
{
__asm
{
mov eax, argoffset
movups xmm0, [eax]
mov eax, simdArg
movups[eax], xmm0
};
#if DBG_DUMP
if (tracingFunc)
{
switch (asmInfo->GetArgType(i).which())
{
case AsmJsType::Int32x4:
Output::Print(_u(" I4(%d, %d, %d, %d)"), \
simdArg->i32[SIMD_X], simdArg->i32[SIMD_Y], simdArg->i32[SIMD_Z], simdArg->i32[SIMD_W]);
break;
case AsmJsType::Float32x4:
Output::Print(_u(" F4(%.4f, %.4f, %.4f, %.4f)"), \
simdArg->f32[SIMD_X], simdArg->f32[SIMD_Y], simdArg->f32[SIMD_Z], simdArg->f32[SIMD_W]);
break;
case AsmJsType::Float64x2:
Output::Print(_u(" D2(%.4f, %.4f)%c"), \
simdArg->f64[SIMD_X], simdArg->f64[SIMD_Y]);
break;
}
Output::Print(_u("%c"), i + 1 < argCount ? ',' : ' ');
}
#endif
++simdArg;
argoffset += sizeof(AsmJsSIMDValue);
}
}
}
#if DBG_DUMP
if( tracingFunc )
{
Output::Print( _u("){\n"));
}
#endif
}
#if DBG_DUMP
void AsmJSCommonCallHelper(Js::ScriptFunction* func)
{
FunctionBody* body = func->GetFunctionBody();
AsmJsFunctionInfo* asmInfo = body->GetAsmJsFunctionInfo();
const bool tracingFunc = PHASE_TRACE(AsmjsFunctionEntryPhase, body);
if (tracingFunc)
{
--AsmJsCallDepth;
if (AsmJsCallDepth)
{
Output::Print(_u("%*c}"), AsmJsCallDepth, ' ');
}
else
{
Output::Print(_u("}"));
}
if (asmInfo->GetReturnType() != AsmJsRetType::Void)
{
//returnContent.Print<T>();
}
Output::Print(_u(";\n"));
}
}
#endif
Var ExternalCallHelper( JavascriptFunction* function, int nbArgs, Var* paramsAddr )
{
int flags = CallFlags_Value;
Arguments args(CallInfo((CallFlags)flags, (ushort)nbArgs), paramsAddr);
BEGIN_SAFE_REENTRANT_CALL(function->GetScriptContext()->GetThreadContext())
{
return JavascriptFunction::CallFunction<true>(function, function->GetEntryPoint(), args);
}
END_SAFE_REENTRANT_CALL
}
namespace AsmJsJitTemplate
{
const int Globals::ModuleSlotOffset = (AsmJsFunctionMemory::ModuleSlotRegister + Globals::StackVarCount)*sizeof(Var);
const int Globals::ModuleEnvOffset = (AsmJsFunctionMemory::ModuleEnvRegister + Globals::StackVarCount)*sizeof(Var);
const int Globals::ArrayBufferOffset = (AsmJsFunctionMemory::ArrayBufferRegister + Globals::StackVarCount)*sizeof(Var);
const int Globals::ArraySizeOffset = (AsmJsFunctionMemory::ArraySizeRegister + Globals::StackVarCount)*sizeof(Var);
const int Globals::ScriptContextOffset = (AsmJsFunctionMemory::ScriptContextBufferRegister + Globals::StackVarCount)*sizeof(Var);
#if DBG_DUMP
FunctionBody* Globals::CurrentEncodingFunction = nullptr;
#endif
// Jump relocation : fix the jump offset for a later point in the same template
struct JumpRelocation
{
// buffer : where the instruction will be encoded
// size : address of a variable tracking the instructions size encoded after the jump
JumpRelocation( BYTE* buffer, int* size )
{
#if DBG
mRelocDone = false;
mEncodingImmSize = -1;
#endif
Init( buffer, size );
}
// Default Constructor, must call Init before using
JumpRelocation()
{
#if DBG
mRelocDone = false;
mEncodingImmSize = -1;
#endif
}
#if DBG
~JumpRelocation()
{
// Make sure the relocation is done when destruction the object
Assert( mRelocDone );
}
#endif
void Init( BYTE* buffer, int* size )
{
#if DBG
// this cannot be called twice
Assert( mEncodingImmSize == -1 );
#endif
mBuffer = buffer;
mSize = size;
mInitialSize = *mSize;
}
// to be called right after encoding a jump
void JumpEncoded( const EncodingInfo& info )
{
#if DBG
// this cannot be called twice
Assert( mEncodingImmSize == -1 );
#endif
const int curSize = *mSize;
// move the buffer to the point where we need to fix the value
mBuffer += curSize - mInitialSize - info.immSize;
mInitialSize = curSize;
#if DBG
mEncodingImmSize = info.immSize;
#endif
}
// use when only 1 Byte was allocated
template<typename OffsetType>
void ApplyReloc()
{
#if DBG
Assert( mEncodingImmSize == sizeof(OffsetType) );
mRelocDone = true;
#endif
const int relocSize = *mSize - mInitialSize;
// if we encoded only 1 byte, make sure it fits
Assert( sizeof(OffsetType) != 1 || FitsInByte( relocSize ) );
*(OffsetType*)mBuffer = (OffsetType)relocSize;
}
#if DBG
bool mRelocDone;
int mEncodingImmSize;
#endif
BYTE* mBuffer;
int* mSize;
int mInitialSize;
};
#define GetTemplateData(context) ((X86TemplateData*)context->GetTemplateData())
// Initialize template data
void* InitTemplateData()
{
return HeapNew( X86TemplateData );
}
// Free template data for architecture specific
void FreeTemplateData( void* userData )
{
HeapDelete( (X86TemplateData*)userData );
}
// Typedef to map a type to an instruction
template<typename InstructionSize> struct InstructionBySize;
template<> struct InstructionBySize < int > { typedef MOV MoveInstruction; };
template<> struct InstructionBySize < double > { typedef MOVSD MoveInstruction; };
template<> struct InstructionBySize < float > { typedef MOVSS MoveInstruction; };
template<> struct InstructionBySize < AsmJsSIMDValue > { typedef MOVUPS MoveInstruction; };
namespace EncodingHelpers
{
// put the value on the stack into a register
template<typename RegisterSize>
RegNum GetStackReg( BYTE*& buffer, X86TemplateData* templateData, int varOffset, int &size, const int registerRestriction = 0 )
{
RegNum reg;
if( !templateData->FindRegWithStackOffset<RegisterSize>( reg, varOffset, registerRestriction ) )
{
reg = templateData->GetReg<RegisterSize>( registerRestriction );
size += InstructionBySize<RegisterSize>::MoveInstruction::EncodeInstruction<RegisterSize>( buffer, InstrParamsRegAddr( reg, RegEBP, varOffset ) );
templateData->SetStackInfo( reg, varOffset );
}
return reg;
}
// put the value of a register on the stack
template<typename RegisterSize>
int SetStackReg( BYTE*& buffer, X86TemplateData* templateData, int targetOffset, RegNum reg )
{
CompileAssert(sizeof(RegisterSize) == 4 || sizeof(RegisterSize) == 8);
templateData->OverwriteStack( targetOffset );
templateData->SetStackInfo( reg, targetOffset );
return InstructionBySize<RegisterSize>::MoveInstruction::EncodeInstruction<RegisterSize>( buffer, InstrParamsAddrReg( RegEBP, targetOffset, reg ) );
}
template<typename LaneType=int>
int SIMDSetStackReg(BYTE*& buffer, X86TemplateData* templateData, int targetOffset, RegNum reg)
{
CompileAssert(sizeof(LaneType) == 4 || sizeof(LaneType) == 8);
AssertMsg(((1<<reg) & GetRegMask<AsmJsSIMDValue>()), "Expecting XMM reg.");
// On a stack spill, we need to invalidate any registers holding lane values.
int laneOffset = 0;
while (laneOffset < sizeof(AsmJsSIMDValue))
{
templateData->OverwriteStack(targetOffset + laneOffset);
laneOffset += sizeof(LaneType);
}
templateData->SetStackInfo(reg, targetOffset);
return InstructionBySize<AsmJsSIMDValue>::MoveInstruction::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParamsAddrReg(RegEBP, targetOffset, reg));
}
/*
Simply copy data from memory to memory.
TODO: Optimize to initialize in XMM reg and then store to mem.
*/
template<typename LaneType>
int SIMDInitFromPrimitives(BYTE*& buffer, X86TemplateData* templateData, int targetOffset, int srcOffset1, int srcOffset2, int srcOffset3 = 0, int srcOffset4 = 0)
{
CompileAssert(sizeof(LaneType) == 4 || sizeof(LaneType) == 8);
int size = 0;
int laneOffset = 0;
RegNum reg;
targetOffset -= templateData->GetBaseOffSet();
srcOffset1 -= templateData->GetBaseOffSet();
srcOffset2 -= templateData->GetBaseOffSet();
srcOffset3 -= templateData->GetBaseOffSet();
srcOffset4 -= templateData->GetBaseOffSet();
// Since we overwrite all lanes, any register holding any lane value is invalidated.
reg = EncodingHelpers::GetStackReg<LaneType>(buffer, templateData, srcOffset1, size);
size += EncodingHelpers::SetStackReg<LaneType>(buffer, templateData, targetOffset + laneOffset, reg);
templateData->InvalidateReg(reg);
laneOffset += sizeof(LaneType);
reg = EncodingHelpers::GetStackReg<LaneType>(buffer, templateData, srcOffset2, size);
size += EncodingHelpers::SetStackReg<LaneType>(buffer, templateData, targetOffset + laneOffset, reg);
templateData->InvalidateReg(reg);
laneOffset += sizeof(LaneType);
if (laneOffset < sizeof(AsmJsSIMDValue))
{
reg = EncodingHelpers::GetStackReg<LaneType>(buffer, templateData, srcOffset3, size);
size += EncodingHelpers::SetStackReg<LaneType>(buffer, templateData, targetOffset + laneOffset, reg);
templateData->InvalidateReg(reg);
laneOffset += sizeof(LaneType);
reg = EncodingHelpers::GetStackReg<LaneType>(buffer, templateData, srcOffset4, size);
size += EncodingHelpers::SetStackReg<LaneType>(buffer, templateData, targetOffset + laneOffset, reg);
templateData->InvalidateReg(reg);
}
return size;
}
// Since SIMD data is unaligned, we cannot support "OP reg, [mem]" operations.
template <typename Operation, typename LaneType=int>
int SIMDUnaryOperation(BYTE*& buffer, X86TemplateData* templateData, int targetOffset, int srcOffset, int registerRestriction = 0)
{
int size = 0;
RegNum dstReg, srcReg;
targetOffset -= templateData->GetBaseOffSet();
srcOffset -= templateData->GetBaseOffSet();
// MOVUPS
srcReg = EncodingHelpers::GetStackReg<AsmJsSIMDValue>(buffer, templateData, srcOffset, size);
// Get a new reg for dst, and keep src reg alive
dstReg = templateData->GetReg<AsmJsSIMDValue>(1 << srcReg);
// OP reg1, reg2
size += Operation::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParams2Reg(dstReg, srcReg));
// MOVUPS
size += EncodingHelpers::SIMDSetStackReg<LaneType>(buffer, templateData, targetOffset, dstReg);
return size;
}
template <typename Operation, typename LaneType = int>
int SIMDBinaryOperation(BYTE*& buffer, X86TemplateData* templateData, int targetOffset, int srcOffset1, int srcOffset2)
{
int size = 0;
RegNum srcReg1, srcReg2, dstReg;
targetOffset -= templateData->GetBaseOffSet();
srcOffset1 -= templateData->GetBaseOffSet();
srcOffset2 -= templateData->GetBaseOffSet();
// MOVUPS srcReg1, [srcOffset1]
srcReg1 = EncodingHelpers::GetStackReg<AsmJsSIMDValue>(buffer, templateData, srcOffset1, size);
// MOVUPS srcReg2, [srcOffset2]
srcReg2 = EncodingHelpers::GetStackReg<AsmJsSIMDValue>(buffer, templateData, srcOffset2, size);
// keep src regs alive
// MOVAPS dstReg, srcReg1
dstReg = templateData->GetReg<AsmJsSIMDValue>((1 << srcReg1) | (1 << srcReg2));
size += MOVAPS::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParams2Reg(dstReg, srcReg1));
// OP dstReg, srcReg2
size += Operation::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParams2Reg(dstReg, srcReg2));
// MOVUPS
size += EncodingHelpers::SIMDSetStackReg<LaneType>(buffer, templateData, targetOffset, dstReg);
return size;
}
// for CMP and Shuffle operations
template <typename Operation, typename LaneType = int>
int SIMDBinaryOperation(BYTE*& buffer, X86TemplateData* templateData, int targetOffset, int srcOffset1, int srcOffset2, byte imm8)
{
int size = 0;
RegNum srcReg1, srcReg2, dstReg;
targetOffset -= templateData->GetBaseOffSet();
srcOffset1 -= templateData->GetBaseOffSet();
srcOffset2 -= templateData->GetBaseOffSet();
// MOVUPS srcReg1, [srcOffset1]
srcReg1 = EncodingHelpers::GetStackReg<AsmJsSIMDValue>(buffer, templateData, srcOffset1, size);
// MOVUPS srcReg2, [srcOffset2]
srcReg2 = EncodingHelpers::GetStackReg<AsmJsSIMDValue>(buffer, templateData, srcOffset2, size);
// keep src regs alive
// MOVAPS dstReg, srcReg1
dstReg = templateData->GetReg<AsmJsSIMDValue>((1 << srcReg1) | (1 << srcReg2));
size += MOVAPS::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParams2Reg(dstReg, srcReg1));
// OP dstReg, srcReg2, imm8
size += Operation::EncodeInstruction<AsmJsSIMDValue>(buffer, InstrParams2RegImm<byte>(dstReg, srcReg2, imm8));
// MOVUPS
size += EncodingHelpers::SIMDSetStackReg<LaneType>(buffer, templateData, targetOffset, dstReg);
return size;
}