forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionBody.h
3942 lines (3331 loc) · 168 KB
/
FunctionBody.h
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) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
#include "AuxPtrs.h"
#include "CompactCounters.h"
// Where should I include this file?
#include "FunctionExecutionStateMachine.h"
struct CodeGenWorkItem;
class SourceContextInfo;
struct DeferredFunctionStub;
#ifdef DYNAMIC_PROFILE_MUTATOR
class DynamicProfileMutator;
class DynamicProfileMutatorImpl;
#endif
#define MAX_FUNCTION_BODY_DEBUG_STRING_SIZE 42 //11*3+8+1
typedef BVSparse<ArenaAllocator> ActiveFunctionSet;
#if ENABLE_NATIVE_CODEGEN
class JitTransferData;
class NativeEntryPointData;
class InProcNativeEntryPointData;
#if ENABLE_OOP_NATIVE_CODEGEN
class OOPNativeEntryPointData;
#endif
#endif
namespace Js
{
#pragma region Class Forward Declarations
class ByteCodeBufferReader;
class ByteCodeBufferBuilder;
class ByteCodeCache;
class ScopeInfo;
class SmallSpanSequence;
struct StatementLocation;
class SmallSpanSequenceIter;
struct StatementData;
struct PropertyIdOnRegSlotsContainer;
struct InlineCache;
class PolymorphicInlineCache;
struct IsInstInlineCache;
class ScopeObjectChain;
class EntryPointInfo;
class FunctionProxy;
class ParseableFunctionInfo;
class FunctionBody;
class DebuggerScopeProperty;
class DebuggerScope;
class FunctionEntryPointInfo;
#ifdef ASMJS_PLAT
class AsmJsFunctionInfo;
class AmsJsModuleInfo;
#endif
class ArrayBuffer;
class SharedArrayBuffer;
class FunctionCodeGenRuntimeData;
class JavascriptNumber;
#pragma endregion
// Definition of scopes such as With, Catch and Block which will be used further in the debugger for additional look-ups.
enum DiagExtraScopesType
{
DiagUnknownScope, // Unknown scope set when deserializing bytecode and the scope is not yet known.
DiagWithScope, // With scope.
DiagCatchScopeDirect, // Catch scope in regslot
DiagCatchScopeInSlot, // Catch scope in slot array
DiagCatchScopeInObject, // Catch scope in scope object
DiagBlockScopeDirect, // Block scope in regslot
DiagBlockScopeInSlot, // Block scope in slot array
DiagBlockScopeInObject, // Block scope in activation object
DiagBlockScopeRangeEnd, // Used to end a block scope range.
DiagParamScope, // The scope represents symbols at formals
DiagParamScopeInObject, // The scope represents symbols at formals and formal scope in activation object
};
struct EnumeratorCache
{
Type * type;
void * data;
};
#pragma region Inline Cache Info class declarations
class PolymorphicCacheUtilizationArray
{
private:
Field(byte *) utilArray;
public:
PolymorphicCacheUtilizationArray()
: utilArray(nullptr)
{
}
void EnsureUtilArray(Recycler * const recycler, Js::FunctionBody * functionBody);
byte* GetByteArray() { return utilArray; }
void SetUtil(Js::FunctionBody* functionBody, uint index, byte util);
byte GetUtil(Js::FunctionBody* functionBody, uint index);
};
class PolymorphicInlineCacheInfo sealed
{
private:
Field(InlineCachePointerArray<PolymorphicInlineCache>) polymorphicInlineCaches;
Field(PolymorphicCacheUtilizationArray) polymorphicCacheUtilizationArray;
Field(FunctionBody *) functionBody;
public:
PolymorphicInlineCacheInfo(FunctionBody * functionBody)
: functionBody(functionBody)
{
}
InlineCachePointerArray<PolymorphicInlineCache> * GetPolymorphicInlineCaches() { return &polymorphicInlineCaches; }
PolymorphicCacheUtilizationArray * GetUtilArray() { return &polymorphicCacheUtilizationArray; }
byte * GetUtilByteArray() { return polymorphicCacheUtilizationArray.GetByteArray(); }
FunctionBody * GetFunctionBody() const { return functionBody; }
};
class EntryPointPolymorphicInlineCacheInfo sealed
{
private:
Field(PolymorphicInlineCacheInfo) selfInfo;
typedef SListCounted<PolymorphicInlineCacheInfo*, Recycler> PolymorphicInlineCacheInfoListType;
Field(PolymorphicInlineCacheInfoListType) inlineeInfo;
static void SetPolymorphicInlineCache(PolymorphicInlineCacheInfo * polymorphicInlineCacheInfo, FunctionBody * functionBody, uint index, PolymorphicInlineCache * polymorphicInlineCache, byte polyCacheUtil);
public:
EntryPointPolymorphicInlineCacheInfo(FunctionBody * functionBody);
PolymorphicInlineCacheInfo * GetSelfInfo() { return &selfInfo; }
PolymorphicInlineCacheInfo * EnsureInlineeInfo(Recycler * recycler, FunctionBody * inlineeFunctionBody);
PolymorphicInlineCacheInfo * GetInlineeInfo(FunctionBody * inlineeFunctionBody);
SListCounted<PolymorphicInlineCacheInfo*, Recycler> * GetInlineeInfo() { return &this->inlineeInfo; }
void SetPolymorphicInlineCache(FunctionBody * functionBody, uint index, PolymorphicInlineCache * polymorphicInlineCache, bool isInlinee, byte polyCacheUtil);
template <class Fn>
void MapInlinees(Fn fn)
{
SListCounted<PolymorphicInlineCacheInfo*, Recycler>::Iterator iter(&inlineeInfo);
while (iter.Next())
{
fn(iter.Data());
}
}
};
#pragma endregion
#ifdef FIELD_ACCESS_STATS
struct FieldAccessStats
{
Field(uint) totalInlineCacheCount;
Field(uint) noInfoInlineCacheCount;
Field(uint) monoInlineCacheCount;
Field(uint) emptyMonoInlineCacheCount;
Field(uint) polyInlineCacheCount;
Field(uint) nullPolyInlineCacheCount;
Field(uint) emptyPolyInlineCacheCount;
Field(uint) ignoredPolyInlineCacheCount;
Field(uint) highUtilPolyInlineCacheCount;
Field(uint) lowUtilPolyInlineCacheCount;
Field(uint) equivPolyInlineCacheCount;
Field(uint) nonEquivPolyInlineCacheCount;
Field(uint) disabledPolyInlineCacheCount;
Field(uint) clonedMonoInlineCacheCount;
Field(uint) clonedPolyInlineCacheCount;
FieldAccessStats() :
totalInlineCacheCount(0), noInfoInlineCacheCount(0), monoInlineCacheCount(0), emptyMonoInlineCacheCount(0),
polyInlineCacheCount(0), nullPolyInlineCacheCount(0), emptyPolyInlineCacheCount(0), ignoredPolyInlineCacheCount(0),
highUtilPolyInlineCacheCount(0), lowUtilPolyInlineCacheCount(0),
equivPolyInlineCacheCount(0), nonEquivPolyInlineCacheCount(0), disabledPolyInlineCacheCount(0),
clonedMonoInlineCacheCount(0), clonedPolyInlineCacheCount(0) {}
void Add(FieldAccessStats* other);
};
typedef FieldAccessStats* FieldAccessStatsPtr;
#else
typedef void* FieldAccessStatsPtr;
#endif
#pragma region Entry point class declarations
class ProxyEntryPointInfo: public ExpirableObject
{
public:
// These are public because we don't manage them nor their consistency;
// the user of this class does.
FieldNoBarrier(Js::JavascriptMethod) jsMethod;
ProxyEntryPointInfo(Js::JavascriptMethod jsMethod, ThreadContext* context = nullptr):
ExpirableObject(context),
jsMethod(jsMethod)
{
}
static DWORD GetAddressOffset() { return offsetof(ProxyEntryPointInfo, jsMethod); }
virtual void Expire()
{
AssertMsg(false, "Expire called on object that doesn't support expiration");
}
virtual void EnterExpirableCollectMode()
{
AssertMsg(false, "EnterExpirableCollectMode called on object that doesn't support expiration");
}
virtual bool IsFunctionEntryPointInfo() const { return false; }
};
// Not thread safe.
// Note that instances of this class are read from and written to from the
// main and JIT threads.
class EntryPointInfo : public ProxyEntryPointInfo
{
private:
enum State : BYTE
{
NotScheduled, // code gen has not been scheduled
CodeGenPending, // code gen job has been scheduled
CodeGenQueued, // code gen has been queued and all the code gen data has been gathered.
CodeGenRecorded, // backend completed, but job still pending
CodeGenDone, // code gen job successfully completed
JITCapReached, // workitem created but JIT cap reached
PendingCleanup, // workitem needs to be cleaned up but couldn't for some reason- it'll be cleaned up at the next opportunity
CleanedUp // the entry point has been cleaned up
};
// The following fields are packed into a 32-bit/64-bit, and the tag to avoid fals positive.
Field(const bool) tag : 1;
Field(bool) isLoopBody : 1;
Field(bool) hasJittedStackClosure : 1;
Field(bool) isAsmJsFunction : 1; // true if entrypoint is for asmjs function
Field(bool) nativeEntryPointProcessed : 1;
Field(bool) mIsTemplatizedJitMode : 1; // true only if in TJ mode, used only for debugging
Field(State) state; // Single state member so users can query state w/o a lock
#if ENABLE_NATIVE_CODEGEN
Field(NativeEntryPointData *) nativeEntryPointData;
#endif // ENABLE_NATIVE_CODEGEN
protected:
Field(CodeGenWorkItem *) workItem;
Field(JavascriptLibrary*) library;
#if ENABLE_ENTRYPOINT_CLEANUP_TRACE
#if ENABLE_DEBUG_STACK_BACK_TRACE
FieldNoBarrier(StackBackTrace*) cleanupStack; // NoCheckHeapAllocator
#endif
public:
enum CleanupReason
{
NotCleanedUp,
CodeGenFailedOOM,
CodeGenFailedStackOverflow,
CodeGenFailedAborted,
CodeGenFailedExceedJITLimit,
CodeGenFailedUnknown,
NativeCodeInstallFailure,
CleanUpForFinalize
};
private:
Field(CleanupReason) cleanupReason;
#endif // ENABLE_ENTRYPOINT_CLEANUP_TRACE
#ifdef FIELD_ACCESS_STATS
private:
Field(FieldAccessStatsPtr) fieldAccessStats;
#endif
#if DEBUG
private:
Field(const unsigned char*) serializedRpcData = nullptr;
Field(size_t) serializedRpcDataSize = 0;
public:
void SetSerializedRpcData(const unsigned char* data, size_t size)
{
if (this->serializedRpcData != nullptr)
{
// We may have multiple codegens happen for same entrypoint
const unsigned char* rpcData = this->serializedRpcData;
HeapDeleteArray(this->serializedRpcDataSize, rpcData);
}
serializedRpcData = data;
serializedRpcDataSize = size;
}
#endif
public:
virtual void Finalize(bool isShutdown) override;
#if ENABLE_NATIVE_CODEGEN
NativeEntryPointData * EnsureNativeEntryPointData();
bool HasNativeEntryPointData() const;
NativeEntryPointData * GetNativeEntryPointData() const;
InProcNativeEntryPointData * GetInProcNativeEntryPointData();
#if ENABLE_OOP_NATIVE_CODEGEN
OOPNativeEntryPointData * GetOOPNativeEntryPointData();
#endif
#endif
protected:
EntryPointInfo(Js::JavascriptMethod method, JavascriptLibrary* library, ThreadContext* context = nullptr, bool isLoopBody = false) :
ProxyEntryPointInfo(method, context), tag(1), nativeEntryPointProcessed(false), mIsTemplatizedJitMode(false),
#if ENABLE_NATIVE_CODEGEN
nativeEntryPointData(nullptr), workItem(nullptr), state(NotScheduled),
isLoopBody(isLoopBody), hasJittedStackClosure(false),
#endif
library(library), isAsmJsFunction(false)
#if ENABLE_ENTRYPOINT_CLEANUP_TRACE
#if ENABLE_DEBUG_STACK_BACK_TRACE
, cleanupStack(nullptr)
#endif
, cleanupReason(NotCleanedUp)
#endif
#ifdef FIELD_ACCESS_STATS
, fieldAccessStats(nullptr)
#endif
{}
virtual void ReleasePendingWorkItem() {};
virtual void OnCleanup(bool isShutdown) = 0;
#ifdef PERF_COUNTERS
virtual void OnRecorded() = 0;
#endif
private:
State GetState() const
{
Assert(this->state >= NotScheduled && this->state <= CleanedUp);
return this->state;
}
public:
ScriptContext* GetScriptContext();
virtual FunctionBody *GetFunctionBody() const = 0;
#if ENABLE_NATIVE_CODEGEN
uint GetFrameHeight();
JitTransferData* GetJitTransferData();
#ifdef FIELD_ACCESS_STATS
FieldAccessStats* GetFieldAccessStats() { return this->fieldAccessStats; }
FieldAccessStats* EnsureFieldAccessStats(Recycler* recycler);
#endif
void PinTypeRefs(ScriptContext* scriptContext);
void InstallGuards(ScriptContext* scriptContext);
#endif
void Cleanup(bool isShutdown, bool captureCleanupStack);
#if ENABLE_ENTRYPOINT_CLEANUP_TRACE
#if ENABLE_DEBUG_STACK_BACK_TRACE
void CaptureCleanupStackTrace();
#endif
#endif
bool IsNotScheduled() const
{
return this->GetState() == NotScheduled;
}
bool IsCodeGenPending() const
{
return this->GetState() == CodeGenPending;
}
bool IsCodeGenRecorded() const
{
return this->GetState() == CodeGenRecorded;
}
bool IsNativeCode() const
{
#if ENABLE_NATIVE_CODEGEN
return this->GetState() == CodeGenRecorded ||
this->GetState() == CodeGenDone;
#else
return false;
#endif
}
bool IsCodeGenDone() const
{
#if ENABLE_NATIVE_CODEGEN
return this->GetState() == CodeGenDone;
#else
return false;
#endif
}
bool IsCodeGenQueued() const
{
#if ENABLE_NATIVE_CODEGEN
return this->GetState() == CodeGenQueued;
#else
return false;
#endif
}
bool IsJITCapReached() const
{
#if ENABLE_NATIVE_CODEGEN
return this->GetState() == JITCapReached;
#else
return false;
#endif
}
bool IsCleanedUp() const
{
return this->GetState() == CleanedUp;
}
bool IsPendingCleanup() const
{
return this->GetState() == PendingCleanup;
}
void SetPendingCleanup()
{
this->state = PendingCleanup;
}
#if ENABLE_ENTRYPOINT_CLEANUP_TRACE
void SetCleanupReason(CleanupReason reason)
{
this->cleanupReason = reason;
}
#endif
bool IsLoopBody() const
{
return this->isLoopBody;
}
#if ENABLE_NATIVE_CODEGEN
bool HasJittedStackClosure() const
{
return this->hasJittedStackClosure;
}
void SetHasJittedStackClosure()
{
this->hasJittedStackClosure = true;
}
#endif
void Reset(bool resetStateToNotScheduled);
#if ENABLE_NATIVE_CODEGEN
void SetCodeGenPending(CodeGenWorkItem * workItem)
{
Assert(this->GetState() == NotScheduled || this->GetState() == CleanedUp);
Assert(workItem != nullptr);
this->workItem = workItem;
this->state = CodeGenPending;
}
void SetCodeGenPending()
{
Assert(this->GetState() == CodeGenQueued);
this->state = CodeGenPending;
}
void SetCodeGenQueued()
{
Assert(this->GetState() == CodeGenPending);
this->state = CodeGenQueued;
}
void RevertToNotScheduled()
{
Assert(this->GetState() == CodeGenPending);
Assert(this->workItem != nullptr);
this->workItem = nullptr;
this->state = NotScheduled;
}
void SetCodeGenPendingWithStackAllocatedWorkItem()
{
Assert(this->GetState() == NotScheduled || this->GetState() == CleanedUp);
this->workItem = nullptr;
this->state = CodeGenPending;
}
void SetCodeGenRecorded(Js::JavascriptMethod thunkAddress, Js::JavascriptMethod nativeAddress, ptrdiff_t codeSize, void * validationCookie);
void SetCodeGenDone();
void SetJITCapReached()
{
Assert(this->GetState() == CodeGenQueued);
this->state = JITCapReached;
this->workItem = nullptr;
}
SmallSpanSequence* GetNativeThrowSpanSequence() const;
void SetNativeThrowSpanSequence(SmallSpanSequence* seq);
bool IsInNativeAddressRange(DWORD_PTR codeAddress) {
return (IsNativeCode() &&
codeAddress >= GetNativeAddress() &&
codeAddress < GetNativeAddress() + GetCodeSize());
}
DWORD_PTR GetNativeAddress() const;
Js::JavascriptMethod GetThunkAddress() const;
Js::JavascriptMethod GetNativeEntrypoint() const;
ptrdiff_t GetCodeSize() const;
#endif
CodeGenWorkItem * GetWorkItem() const
{
State state = this->GetState();
Assert(state != NotScheduled || this->workItem == nullptr);
Assert(state == CleanedUp && this->workItem == nullptr ||
state != CleanedUp);
if (state == PendingCleanup)
{
return nullptr;
}
return this->workItem;
}
#ifdef ASMJS_PLAT
// set code size, used by TJ to set the code size
void SetTJCodeSize(ptrdiff_t size);
void SetTJNativeAddress(Js::JavascriptMethod address, void * validationCookie);
void SetIsAsmJSFunction(bool value)
{
this->isAsmJsFunction = value;
}
void SetTJCodeGenDone()
{
Assert(isAsmJsFunction);
this->state = CodeGenDone;
this->workItem = nullptr;
}
void SetIsTJMode(bool value);
bool GetIsTJMode() const;
#endif
bool GetIsAsmJSFunction()const
{
return this->isAsmJsFunction;
}
#if ENABLE_NATIVE_CODEGEN
bool IsNativeEntryPointProcessed() { return this->nativeEntryPointProcessed; }
void SetNativeEntryPointProcessed() { this->nativeEntryPointProcessed = true; }
void EnsureIsReadyToCall();
void ProcessJitTransferData();
void ResetOnLazyBailoutFailure();
void OnNativeCodeInstallFailure();
virtual void ResetOnNativeCodeInstallFailure() = 0;
void FreeJitTransferData();
bool ClearEquivalentTypeCaches();
virtual void Invalidate(bool prolongEntryPoint) { Assert(false); }
InlineeFrameRecord* FindInlineeFrame(void* returnAddress);
bool HasInlinees();
#if DBG
void DoLazyBailout(BYTE **addressOfInstructionPointer, BYTE *framePointer, Js::FunctionBody *functionBody, const PropertyRecord *propertyRecord);
#else
void DoLazyBailout(BYTE **addressOfInstructionPointer, BYTE *framePointer);
#endif
void CleanupNativeCode(ScriptContext * scriptContext);
#if DBG_DUMP
public:
#elif defined(VTUNE_PROFILING)
private:
#endif
#if DBG_DUMP || defined(VTUNE_PROFILING)
public:
void RecordNativeMap(uint32 offset, uint32 statementIndex);
int GetNativeOffsetMapCount() const;
#endif
#if DBG_DUMP && ENABLE_NATIVE_CODEGEN
void DumpNativeOffsetMaps();
void DumpNativeThrowSpanSequence();
#endif
#ifdef VTUNE_PROFILING
public:
uint PopulateLineInfo(void* pLineInfo, FunctionBody* body);
#endif
#endif // ENABLE_NATIVE_CODEGEN
};
class FunctionEntryPointInfo : public EntryPointInfo
{
public:
Field(FunctionProxy *) functionProxy;
Field(FunctionEntryPointInfo*) nextEntryPoint;
// The offset on the native stack, from which the locals are located (Populated at RegAlloc phase). Used for debug purpose.
Field(int32) localVarSlotsOffset;
// The offset which stores that any of the locals are changed from the debugger.
Field(int32) localVarChangedOffset;
Field(uint) entryPointIndex;
Field(uint32) callsCount;
Field(uint32) lastCallsCount;
private:
Field(ExecutionMode) jitMode;
public:
FunctionEntryPointInfo(FunctionProxy * functionInfo, Js::JavascriptMethod method, ThreadContext* context);
virtual bool IsFunctionEntryPointInfo() const override { return true; }
bool ExecutedSinceCallCountCollection() const;
void CollectCallCounts();
virtual FunctionBody *GetFunctionBody() const override;
#if ENABLE_NATIVE_CODEGEN
ExecutionMode GetJitMode() const;
void SetJitMode(const ExecutionMode jitMode);
virtual void Invalidate(bool prolongEntryPoint) override;
virtual void Expire() override;
virtual void EnterExpirableCollectMode() override;
virtual void ResetOnNativeCodeInstallFailure() override;
static uint8 GetDecrCallCountPerBailout()
{
return (uint8)CONFIG_FLAG(CallsToBailoutsRatioForRejit) + 1;
}
#endif
virtual void OnCleanup(bool isShutdown) override;
virtual void ReleasePendingWorkItem() override;
#ifdef PERF_COUNTERS
virtual void OnRecorded() override;
#endif
};
class LoopEntryPointInfo : public EntryPointInfo
{
public:
Field(LoopHeader*) loopHeader;
Field(uint) jittedLoopIterationsSinceLastBailout; // number of times the loop iterated in the jitted code before bailing out
Field(uint) totalJittedLoopIterations; // total number of times the loop has iterated in the jitted code for this entry point for a particular invocation of the loop
LoopEntryPointInfo(LoopHeader* loopHeader, Js::JavascriptLibrary* library) :
EntryPointInfo(nullptr, library, /*threadContext*/ nullptr, /*isLoopBody*/ true),
loopHeader(loopHeader),
jittedLoopIterationsSinceLastBailout(0),
totalJittedLoopIterations(0)
#ifdef BGJIT_STATS
,used(false)
#endif
{ }
virtual FunctionBody *GetFunctionBody() const override;
virtual void OnCleanup(bool isShutdown) override;
#if ENABLE_NATIVE_CODEGEN
virtual void ResetOnNativeCodeInstallFailure() override;
static uint8 GetDecrLoopCountPerBailout()
{
return (uint8)CONFIG_FLAG(LoopIterationsToBailoutsRatioForRejit) + 1;
}
#endif
#ifdef PERF_COUNTERS
virtual void OnRecorded() override;
#endif
#ifdef BGJIT_STATS
bool IsUsed() const
{
return this->used;
}
void MarkAsUsed()
{
this->used = true;
}
private:
Field(bool) used;
#endif
};
typedef RecyclerWeakReference<FunctionEntryPointInfo> FunctionEntryPointWeakRef;
typedef SynchronizableList<FunctionEntryPointWeakRef*, JsUtil::List<FunctionEntryPointWeakRef*>> FunctionEntryPointList;
typedef SynchronizableList<LoopEntryPointInfo*, JsUtil::List<LoopEntryPointInfo*>> LoopEntryPointList;
#pragma endregion
struct LoopHeader
{
private:
Field(LoopEntryPointList*) entryPoints;
public:
Field(uint) startOffset;
Field(uint) endOffset;
Field(uint) interpretCount;
Field(uint) profiledLoopCounter;
#if ENABLE_NATIVE_CODEGEN
Field(uint) rejitCount;
#endif
Field(bool) hasYield;
Field(bool) isNested;
Field(bool) isInTry;
Field(bool) isInTryFinally;
Field(FunctionBody *) functionBody;
#if DBG_DUMP
Field(uint) nativeCount;
#endif
static const uint NoLoop = (uint)-1;
static uint GetOffsetOfProfiledLoopCounter() { return offsetof(LoopHeader, profiledLoopCounter); }
static uint GetOffsetOfInterpretCount() { return offsetof(LoopHeader, interpretCount); }
bool Contains(Js::LoopHeader * loopHeader) const
{
return (this->startOffset <= loopHeader->startOffset && loopHeader->endOffset <= this->endOffset);
}
bool Contains(uint offset) const
{
return this->startOffset <= offset && offset < this->endOffset;
}
Js::JavascriptMethod GetCurrentEntryPoint() const
{
LoopEntryPointInfo * entryPoint = GetCurrentEntryPointInfo();
if (entryPoint != nullptr)
{
return this->entryPoints->Item(this->GetCurrentEntryPointIndex())->jsMethod;
}
return nullptr;
}
LoopEntryPointInfo * GetCurrentEntryPointInfo() const
{
Assert(this->entryPoints->Count() > 0);
return this->entryPoints->Item(this->GetCurrentEntryPointIndex());
}
uint GetByteCodeCount()
{
return (endOffset - startOffset);
}
int GetCurrentEntryPointIndex() const
{
return this->entryPoints->Count() - 1;
}
LoopEntryPointInfo * GetEntryPointInfo(int index) const
{
return this->entryPoints->Item(index);
}
template <class Fn>
void MapEntryPoints(Fn fn) const
{
if (this->entryPoints) // ETW rundown may call this before entryPoints initialization
{
this->entryPoints->Map([&](int index, LoopEntryPointInfo * entryPoint)
{
if (entryPoint != nullptr)
{
fn(index, entryPoint);
}
});
}
}
template <class Fn>
bool MapEntryPointsUntil(Fn fn) const
{
if (this->entryPoints) // ETW rundown may call this before entryPoints initialization
{
return this->entryPoints->MapUntil([&](int index, LoopEntryPointInfo * entryPoint)
{
if (entryPoint != nullptr)
{
return fn(index, entryPoint);
}
return false;
});
}
return false;
}
template <class DebugSite, class Fn>
HRESULT MapEntryPoints(DebugSite site, Fn fn) const // external debugging version
{
return Map(site, PointerValue(this->entryPoints), [&](int index, LoopEntryPointInfo * entryPoint)
{
if (entryPoint != nullptr)
{
fn(index, entryPoint);
}
});
}
void Init(FunctionBody * functionBody);
#if ENABLE_NATIVE_CODEGEN
int CreateEntryPoint();
void ReleaseEntryPoints();
void IncRejitCount() { ++rejitCount; }
uint GetRejitCount() { return rejitCount; }
#endif
void ResetInterpreterCount()
{
this->interpretCount = 0;
}
void ResetProfiledLoopCounter()
{
this->profiledLoopCounter = 0;
}
};
// ----------------------------------------------------------------------------
// SWB-TODO: Below explicitly uses write barrier. Move top code to new header file.
//
class FunctionProxy;
typedef Field(FunctionInfo*)* FunctionInfoArray;
typedef Field(FunctionInfo*)* FunctionInfoPtrPtr;
struct PrintOffsets
{
uint cbStartPrintOffset;
uint cbEndPrintOffset;
};
//
// FunctionProxy represents a user defined function
// This could be either from a source file or the byte code cache
// The function need not have been compiled yet- it could be parsed or compiled
// at a later time
//
class FunctionProxy : public FinalizableObject
{
public:
typedef RecyclerWeakReference<ScriptFunctionType> FunctionTypeWeakRef;
typedef JsUtil::List<FunctionTypeWeakRef*, Recycler, false, WeakRefFreeListedRemovePolicy> FunctionTypeWeakRefList;
protected:
FunctionProxy(ScriptContext* scriptContext, Utf8SourceInfo* utf8SourceInfo, uint functionNumber);
DEFINE_VTABLE_CTOR_NOBASE(FunctionProxy);
enum class AuxPointerType : uint8 {
DeferredStubs = 0,
CachedSourceString = 1,
AsmJsFunctionInfo = 2,
AsmJsModuleInfo = 3,
StatementMaps = 4,
StackNestedFuncParent = 5,
SimpleJitEntryPointInfo = 6,
FunctionObjectTypeList = 7, // Script function types not including the deferred prototype type
CodeGenGetSetRuntimeData = 8,
PropertyIdOnRegSlotsContainer = 9, // This is used for showing locals for the current frame.
LoopHeaderArray = 10,
CodeGenRuntimeData = 11,
PolymorphicInlineCachesHead = 12, // DList of all polymorphic inline caches that aren't finalized yet
PropertyIdsForScopeSlotArray = 13, // For SourceInfo
#if ENABLE_PROFILE_INFO
PolymorphicCallSiteInfoHead = 14,
#endif
AuxBlock = 15, // Optional auxiliary information
AuxContextBlock = 16, // Optional auxiliary context specific information
ReferencedPropertyIdMap = 17,
LiteralRegexes = 18,
ObjLiteralTypes = 19,
ScopeInfo = 20,
FormalsPropIdArray = 21,
ForInCacheArray = 22,
SlotIdInCachedScopeToNestedIndexArray = 23,
CodeGenCallbackRuntimeData = 24,
#if ENABLE_PROFILE_INFO
CallbackArgOutInfoList = 25,
#endif
#if ENABLE_NATIVE_CODEGEN
CodeGenCallApplyTargetRuntimeData = 26,
CallSiteToCallApplyCallSiteArray = 27,
#endif
PrintOffsets = 28,
Max,
Invalid = 0xff
};
template<AuxPointerType T>
struct AuxPointerTypeTable {
typedef void* type;
};
template<>
struct AuxPointerTypeTable<AuxPointerType::Max> {};
template<>
struct AuxPointerTypeTable<AuxPointerType::Invalid> {};
#define AuxPointerTypeEntry(apt, T) template<> struct AuxPointerTypeTable<apt> { typedef T type; }
AuxPointerTypeEntry(AuxPointerType::DeferredStubs, DeferredFunctionStub*);
AuxPointerTypeEntry(AuxPointerType::CachedSourceString, RecyclerWeakReference<JavascriptString>*);
AuxPointerTypeEntry(AuxPointerType::AsmJsFunctionInfo, AsmJsFunctionInfo*);
AuxPointerTypeEntry(AuxPointerType::AsmJsModuleInfo, AsmJsModuleInfo*);
//AuxPointerTypeEntry(AuxPointerType::StatementMaps, StatementMapList*); // the type for this isn't declared yet
AuxPointerTypeEntry(AuxPointerType::StackNestedFuncParent, RecyclerWeakReference<FunctionInfo>*);
AuxPointerTypeEntry(AuxPointerType::SimpleJitEntryPointInfo, FunctionEntryPointInfo*);
AuxPointerTypeEntry(AuxPointerType::FunctionObjectTypeList, FunctionTypeWeakRefList*);
AuxPointerTypeEntry(AuxPointerType::CodeGenGetSetRuntimeData, Field(Js::FunctionCodeGenRuntimeData*)*);
AuxPointerTypeEntry(AuxPointerType::PropertyIdOnRegSlotsContainer, Js::PropertyIdOnRegSlotsContainer*);
AuxPointerTypeEntry(AuxPointerType::LoopHeaderArray, Js::LoopHeader*);
AuxPointerTypeEntry(AuxPointerType::CodeGenRuntimeData, Field(FunctionCodeGenRuntimeData*)*);
AuxPointerTypeEntry(AuxPointerType::PolymorphicInlineCachesHead, FunctionBodyPolymorphicInlineCache*);
AuxPointerTypeEntry(AuxPointerType::PropertyIdsForScopeSlotArray, Js::PropertyId*);
#if ENABLE_PROFILE_INFO
AuxPointerTypeEntry(AuxPointerType::PolymorphicCallSiteInfoHead , PolymorphicCallSiteInfo*);
#endif
AuxPointerTypeEntry(AuxPointerType::AuxBlock, ByteBlock*);
AuxPointerTypeEntry(AuxPointerType::AuxContextBlock, ByteBlock*);
AuxPointerTypeEntry(AuxPointerType::ReferencedPropertyIdMap, PropertyId*);
AuxPointerTypeEntry(AuxPointerType::LiteralRegexes, Field(UnifiedRegex::RegexPattern*)*);
AuxPointerTypeEntry(AuxPointerType::ObjLiteralTypes, Field(DynamicType*)*);
AuxPointerTypeEntry(AuxPointerType::ScopeInfo, ScopeInfo*);
AuxPointerTypeEntry(AuxPointerType::FormalsPropIdArray, PropertyIdArray*);
AuxPointerTypeEntry(AuxPointerType::ForInCacheArray, EnumeratorCache*);
AuxPointerTypeEntry(AuxPointerType::SlotIdInCachedScopeToNestedIndexArray, Js::AuxArray<uint32>*);
AuxPointerTypeEntry(AuxPointerType::CodeGenCallbackRuntimeData, Field(FunctionCodeGenRuntimeData*)*);
#if ENABLE_PROFILE_INFO
AuxPointerTypeEntry(AuxPointerType::CallbackArgOutInfoList, CallbackInfoList*);
#endif
#if ENABLE_NATIVE_CODEGEN
AuxPointerTypeEntry(AuxPointerType::CodeGenCallApplyTargetRuntimeData, Field(FunctionCodeGenRuntimeData*)*);
AuxPointerTypeEntry(AuxPointerType::CallSiteToCallApplyCallSiteArray, ProfileId*);
#endif
AuxPointerTypeEntry(AuxPointerType::PrintOffsets, PrintOffsets*);
#undef AuxPointerTypeEntry
typedef AuxPtrs<FunctionProxy, AuxPointerType> AuxPtrsT;
friend AuxPtrsT;
FieldWithBarrier(AuxPtrsT*) auxPtrs;
template<AuxPointerType T, typename MappedType = typename AuxPointerTypeTable<T>::type>
inline MappedType GetAuxPtr() const
{
return static_cast<MappedType>(GetAuxPtr(T));
}
template<AuxPointerType T, typename MappedType = typename AuxPointerTypeTable<T>::type>
inline MappedType GetAuxPtrWithLock() const
{
return static_cast<MappedType>(GetAuxPtrWithLock(T));
}
template<AuxPointerType T, typename MappedType = typename AuxPointerTypeTable<T>::type>
void SetAuxPtr(MappedType ptr)
{
SetAuxPtr(T, ptr);
}
void* GetAuxPtr(AuxPointerType e) const;
void* GetAuxPtrWithLock(AuxPointerType e) const;
void SetAuxPtr(AuxPointerType e, void* ptr);
FieldWithBarrier(FunctionInfo *) functionInfo;
public:
enum SetDisplayNameFlags
{
SetDisplayNameFlagsNone = 0,
SetDisplayNameFlagsDontCopy = 1,
SetDisplayNameFlagsRecyclerAllocated = 2
};
virtual void Dispose(bool isShutdown) override