cbb8cf5701cce795a6252f4a7e7a04a5741b0b2f
[WebKit-https.git] / Source / JavaScriptCore / bytecode / CodeBlock.h
1 /*
2  * Copyright (C) 2008-2016 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef CodeBlock_h
31 #define CodeBlock_h
32
33 #include "ArrayProfile.h"
34 #include "ByValInfo.h"
35 #include "BytecodeConventions.h"
36 #include "BytecodeLivenessAnalysis.h"
37 #include "CallLinkInfo.h"
38 #include "CallReturnOffsetToBytecodeOffset.h"
39 #include "CodeBlockHash.h"
40 #include "CodeBlockSet.h"
41 #include "CodeOrigin.h"
42 #include "CodeType.h"
43 #include "CompactJITCodeMap.h"
44 #include "ConcurrentJITLock.h"
45 #include "DFGCommon.h"
46 #include "DFGExitProfile.h"
47 #include "DeferredCompilationCallback.h"
48 #include "EvalCodeCache.h"
49 #include "ExecutionCounter.h"
50 #include "ExpressionRangeInfo.h"
51 #include "HandlerInfo.h"
52 #include "Instruction.h"
53 #include "JITCode.h"
54 #include "JITWriteBarrier.h"
55 #include "JSCell.h"
56 #include "JSGlobalObject.h"
57 #include "JumpTable.h"
58 #include "LLIntCallLinkInfo.h"
59 #include "LazyOperandValueProfile.h"
60 #include "ObjectAllocationProfile.h"
61 #include "Options.h"
62 #include "ProfilerJettisonReason.h"
63 #include "PutPropertySlot.h"
64 #include "RegExpObject.h"
65 #include "UnconditionalFinalizer.h"
66 #include "ValueProfile.h"
67 #include "VirtualRegister.h"
68 #include "Watchpoint.h"
69 #include <wtf/Bag.h>
70 #include <wtf/FastBitVector.h>
71 #include <wtf/FastMalloc.h>
72 #include <wtf/RefCountedArray.h>
73 #include <wtf/RefPtr.h>
74 #include <wtf/SegmentedVector.h>
75 #include <wtf/Vector.h>
76 #include <wtf/text/WTFString.h>
77
78 namespace JSC {
79
80 class ExecState;
81 class JSModuleEnvironment;
82 class LLIntOffsetsExtractor;
83 class PCToCodeOriginMap;
84 class RegisterAtOffsetList;
85 class StructureStubInfo;
86 class TypeLocation;
87
88 enum class AccessType : int8_t;
89
90 typedef HashMap<CodeOrigin, StructureStubInfo*, CodeOriginApproximateHash> StubInfoMap;
91
92 enum ReoptimizationMode { DontCountReoptimization, CountReoptimization };
93
94 class CodeBlock : public JSCell {
95     typedef JSCell Base;
96     friend class BytecodeLivenessAnalysis;
97     friend class JIT;
98     friend class LLIntOffsetsExtractor;
99
100     class UnconditionalFinalizer : public JSC::UnconditionalFinalizer { 
101         void finalizeUnconditionally() override;
102     };
103
104     class WeakReferenceHarvester : public JSC::WeakReferenceHarvester {
105         void visitWeakReferences(SlotVisitor&) override;
106     };
107
108 public:
109     enum CopyParsedBlockTag { CopyParsedBlock };
110
111     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
112
113     DECLARE_INFO;
114
115 protected:
116     CodeBlock(VM*, Structure*, CopyParsedBlockTag, CodeBlock& other);
117     CodeBlock(VM*, Structure*, ScriptExecutable* ownerExecutable, UnlinkedCodeBlock*, JSScope*, PassRefPtr<SourceProvider>, unsigned sourceOffset, unsigned firstLineColumnOffset);
118 #if ENABLE(WEBASSEMBLY)
119     CodeBlock(VM*, Structure*, WebAssemblyExecutable* ownerExecutable, JSGlobalObject*);
120 #endif
121
122     void finishCreation(VM&, CopyParsedBlockTag, CodeBlock& other);
123     void finishCreation(VM&, ScriptExecutable* ownerExecutable, UnlinkedCodeBlock*, JSScope*);
124 #if ENABLE(WEBASSEMBLY)
125     void finishCreation(VM&, WebAssemblyExecutable* ownerExecutable, JSGlobalObject*);
126 #endif
127
128     WriteBarrier<JSGlobalObject> m_globalObject;
129
130 public:
131     JS_EXPORT_PRIVATE ~CodeBlock();
132
133     UnlinkedCodeBlock* unlinkedCodeBlock() const { return m_unlinkedCode.get(); }
134
135     CString inferredName() const;
136     CodeBlockHash hash() const;
137     bool hasHash() const;
138     bool isSafeToComputeHash() const;
139     CString hashAsStringIfPossible() const;
140     CString sourceCodeForTools() const; // Not quite the actual source we parsed; this will do things like prefix the source for a function with a reified signature.
141     CString sourceCodeOnOneLine() const; // As sourceCodeForTools(), but replaces all whitespace runs with a single space.
142     void dumpAssumingJITType(PrintStream&, JITCode::JITType) const;
143     void dump(PrintStream&) const;
144
145     int numParameters() const { return m_numParameters; }
146     void setNumParameters(int newValue);
147
148     int numCalleeLocals() const { return m_numCalleeLocals; }
149
150     int* addressOfNumParameters() { return &m_numParameters; }
151     static ptrdiff_t offsetOfNumParameters() { return OBJECT_OFFSETOF(CodeBlock, m_numParameters); }
152
153     CodeBlock* alternative() const { return static_cast<CodeBlock*>(m_alternative.get()); }
154     void setAlternative(VM&, CodeBlock*);
155
156     template <typename Functor> void forEachRelatedCodeBlock(Functor&& functor)
157     {
158         Functor f(std::forward<Functor>(functor));
159         Vector<CodeBlock*, 4> codeBlocks;
160         codeBlocks.append(this);
161
162         while (!codeBlocks.isEmpty()) {
163             CodeBlock* currentCodeBlock = codeBlocks.takeLast();
164             f(currentCodeBlock);
165
166             if (CodeBlock* alternative = currentCodeBlock->alternative())
167                 codeBlocks.append(alternative);
168             if (CodeBlock* osrEntryBlock = currentCodeBlock->specialOSREntryBlockOrNull())
169                 codeBlocks.append(osrEntryBlock);
170         }
171     }
172     
173     CodeSpecializationKind specializationKind() const
174     {
175         return specializationFromIsConstruct(m_isConstructor);
176     }
177
178     CodeBlock* alternativeForJettison();    
179     JS_EXPORT_PRIVATE CodeBlock* baselineAlternative();
180     
181     // FIXME: Get rid of this.
182     // https://bugs.webkit.org/show_bug.cgi?id=123677
183     CodeBlock* baselineVersion();
184
185     static size_t estimatedSize(JSCell*);
186     static void visitChildren(JSCell*, SlotVisitor&);
187     void visitChildren(SlotVisitor&);
188     void visitWeakly(SlotVisitor&);
189     void clearVisitWeaklyHasBeenCalled();
190
191     void dumpSource();
192     void dumpSource(PrintStream&);
193
194     void dumpBytecode();
195     void dumpBytecode(PrintStream&);
196     void dumpBytecode(
197         PrintStream&, unsigned bytecodeOffset,
198         const StubInfoMap& = StubInfoMap(), const CallLinkInfoMap& = CallLinkInfoMap());
199     void dumpExceptionHandlers(PrintStream&);
200     void printStructures(PrintStream&, const Instruction*);
201     void printStructure(PrintStream&, const char* name, const Instruction*, int operand);
202
203     bool isStrictMode() const { return m_isStrictMode; }
204     ECMAMode ecmaMode() const { return isStrictMode() ? StrictMode : NotStrictMode; }
205
206     inline bool isKnownNotImmediate(int index)
207     {
208         if (index == m_thisRegister.offset() && !m_isStrictMode)
209             return true;
210
211         if (isConstantRegisterIndex(index))
212             return getConstant(index).isCell();
213
214         return false;
215     }
216
217     ALWAYS_INLINE bool isTemporaryRegisterIndex(int index)
218     {
219         return index >= m_numVars;
220     }
221
222     enum class RequiredHandler {
223         CatchHandler,
224         AnyHandler
225     };
226     HandlerInfo* handlerForBytecodeOffset(unsigned bytecodeOffset, RequiredHandler = RequiredHandler::AnyHandler);
227     HandlerInfo* handlerForIndex(unsigned, RequiredHandler = RequiredHandler::AnyHandler);
228     void removeExceptionHandlerForCallSite(CallSiteIndex);
229     unsigned lineNumberForBytecodeOffset(unsigned bytecodeOffset);
230     unsigned columnNumberForBytecodeOffset(unsigned bytecodeOffset);
231     void expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot,
232                                           int& startOffset, int& endOffset, unsigned& line, unsigned& column);
233
234     Optional<unsigned> bytecodeOffsetFromCallSiteIndex(CallSiteIndex);
235
236     void getStubInfoMap(const ConcurrentJITLocker&, StubInfoMap& result);
237     void getStubInfoMap(StubInfoMap& result);
238     
239     void getCallLinkInfoMap(const ConcurrentJITLocker&, CallLinkInfoMap& result);
240     void getCallLinkInfoMap(CallLinkInfoMap& result);
241
242     void getByValInfoMap(const ConcurrentJITLocker&, ByValInfoMap& result);
243     void getByValInfoMap(ByValInfoMap& result);
244     
245 #if ENABLE(JIT)
246     StructureStubInfo* addStubInfo(AccessType);
247     Bag<StructureStubInfo>::iterator stubInfoBegin() { return m_stubInfos.begin(); }
248     Bag<StructureStubInfo>::iterator stubInfoEnd() { return m_stubInfos.end(); }
249     
250     // O(n) operation. Use getStubInfoMap() unless you really only intend to get one
251     // stub info.
252     StructureStubInfo* findStubInfo(CodeOrigin);
253
254     ByValInfo* addByValInfo();
255
256     CallLinkInfo* addCallLinkInfo();
257     Bag<CallLinkInfo>::iterator callLinkInfosBegin() { return m_callLinkInfos.begin(); }
258     Bag<CallLinkInfo>::iterator callLinkInfosEnd() { return m_callLinkInfos.end(); }
259
260     // This is a slow function call used primarily for compiling OSR exits in the case
261     // that there had been inlining. Chances are if you want to use this, you're really
262     // looking for a CallLinkInfoMap to amortize the cost of calling this.
263     CallLinkInfo* getCallLinkInfoForBytecodeIndex(unsigned bytecodeIndex);
264 #endif // ENABLE(JIT)
265
266     void unlinkIncomingCalls();
267
268 #if ENABLE(JIT)
269     void linkIncomingCall(ExecState* callerFrame, CallLinkInfo*);
270     void linkIncomingPolymorphicCall(ExecState* callerFrame, PolymorphicCallNode*);
271 #endif // ENABLE(JIT)
272
273     void linkIncomingCall(ExecState* callerFrame, LLIntCallLinkInfo*);
274
275     void setJITCodeMap(std::unique_ptr<CompactJITCodeMap> jitCodeMap)
276     {
277         m_jitCodeMap = WTFMove(jitCodeMap);
278     }
279     CompactJITCodeMap* jitCodeMap()
280     {
281         return m_jitCodeMap.get();
282     }
283
284     unsigned bytecodeOffset(Instruction* returnAddress)
285     {
286         RELEASE_ASSERT(returnAddress >= instructions().begin() && returnAddress < instructions().end());
287         return static_cast<Instruction*>(returnAddress) - instructions().begin();
288     }
289
290     unsigned numberOfInstructions() const { return m_instructions.size(); }
291     RefCountedArray<Instruction>& instructions() { return m_instructions; }
292     const RefCountedArray<Instruction>& instructions() const { return m_instructions; }
293
294     size_t predictedMachineCodeSize();
295
296     bool usesOpcode(OpcodeID);
297
298     unsigned instructionCount() const { return m_instructions.size(); }
299
300     // Exactly equivalent to codeBlock->ownerExecutable()->newReplacementCodeBlockFor(codeBlock->specializationKind())
301     CodeBlock* newReplacement();
302     
303     void setJITCode(PassRefPtr<JITCode> code)
304     {
305         ASSERT(heap()->isDeferred());
306         heap()->reportExtraMemoryAllocated(code->size());
307         ConcurrentJITLocker locker(m_lock);
308         WTF::storeStoreFence(); // This is probably not needed because the lock will also do something similar, but it's good to be paranoid.
309         m_jitCode = code;
310     }
311     PassRefPtr<JITCode> jitCode() { return m_jitCode; }
312     static ptrdiff_t jitCodeOffset() { return OBJECT_OFFSETOF(CodeBlock, m_jitCode); }
313     JITCode::JITType jitType() const
314     {
315         JITCode* jitCode = m_jitCode.get();
316         WTF::loadLoadFence();
317         JITCode::JITType result = JITCode::jitTypeFor(jitCode);
318         WTF::loadLoadFence(); // This probably isn't needed. Oh well, paranoia is good.
319         return result;
320     }
321
322     bool hasBaselineJITProfiling() const
323     {
324         return jitType() == JITCode::BaselineJIT;
325     }
326     
327 #if ENABLE(JIT)
328     CodeBlock* replacement();
329
330     DFG::CapabilityLevel computeCapabilityLevel();
331     DFG::CapabilityLevel capabilityLevel();
332     DFG::CapabilityLevel capabilityLevelState() { return static_cast<DFG::CapabilityLevel>(m_capabilityLevelState); }
333
334     bool hasOptimizedReplacement(JITCode::JITType typeToReplace);
335     bool hasOptimizedReplacement(); // the typeToReplace is my JITType
336 #endif
337
338     void jettison(Profiler::JettisonReason, ReoptimizationMode = DontCountReoptimization, const FireDetail* = nullptr);
339     
340     ExecutableBase* ownerExecutable() const { return m_ownerExecutable.get(); }
341     ScriptExecutable* ownerScriptExecutable() const { return jsCast<ScriptExecutable*>(m_ownerExecutable.get()); }
342
343     VM* vm() const { return m_vm; }
344
345     void setThisRegister(VirtualRegister thisRegister) { m_thisRegister = thisRegister; }
346     VirtualRegister thisRegister() const { return m_thisRegister; }
347
348     bool usesEval() const { return m_unlinkedCode->usesEval(); }
349
350     void setScopeRegister(VirtualRegister scopeRegister)
351     {
352         ASSERT(scopeRegister.isLocal() || !scopeRegister.isValid());
353         m_scopeRegister = scopeRegister;
354     }
355
356     VirtualRegister scopeRegister() const
357     {
358         return m_scopeRegister;
359     }
360     
361     CodeType codeType() const
362     {
363         return static_cast<CodeType>(m_codeType);
364     }
365
366     PutPropertySlot::Context putByIdContext() const
367     {
368         if (codeType() == EvalCode)
369             return PutPropertySlot::PutByIdEval;
370         return PutPropertySlot::PutById;
371     }
372
373     SourceProvider* source() const { return m_source.get(); }
374     unsigned sourceOffset() const { return m_sourceOffset; }
375     unsigned firstLineColumnOffset() const { return m_firstLineColumnOffset; }
376
377     size_t numberOfJumpTargets() const { return m_unlinkedCode->numberOfJumpTargets(); }
378     unsigned jumpTarget(int index) const { return m_unlinkedCode->jumpTarget(index); }
379
380     String nameForRegister(VirtualRegister);
381
382     unsigned numberOfArgumentValueProfiles()
383     {
384         ASSERT(m_numParameters >= 0);
385         ASSERT(m_argumentValueProfiles.size() == static_cast<unsigned>(m_numParameters));
386         return m_argumentValueProfiles.size();
387     }
388     ValueProfile* valueProfileForArgument(unsigned argumentIndex)
389     {
390         ValueProfile* result = &m_argumentValueProfiles[argumentIndex];
391         ASSERT(result->m_bytecodeOffset == -1);
392         return result;
393     }
394
395     unsigned numberOfValueProfiles() { return m_valueProfiles.size(); }
396     ValueProfile* valueProfile(int index) { return &m_valueProfiles[index]; }
397     ValueProfile* valueProfileForBytecodeOffset(int bytecodeOffset);
398     SpeculatedType valueProfilePredictionForBytecodeOffset(const ConcurrentJITLocker& locker, int bytecodeOffset)
399     {
400         return valueProfileForBytecodeOffset(bytecodeOffset)->computeUpdatedPrediction(locker);
401     }
402
403     unsigned totalNumberOfValueProfiles()
404     {
405         return numberOfArgumentValueProfiles() + numberOfValueProfiles();
406     }
407     ValueProfile* getFromAllValueProfiles(unsigned index)
408     {
409         if (index < numberOfArgumentValueProfiles())
410             return valueProfileForArgument(index);
411         return valueProfile(index - numberOfArgumentValueProfiles());
412     }
413
414     RareCaseProfile* addRareCaseProfile(int bytecodeOffset)
415     {
416         m_rareCaseProfiles.append(RareCaseProfile(bytecodeOffset));
417         return &m_rareCaseProfiles.last();
418     }
419     unsigned numberOfRareCaseProfiles() { return m_rareCaseProfiles.size(); }
420     RareCaseProfile* rareCaseProfileForBytecodeOffset(int bytecodeOffset);
421     unsigned rareCaseProfileCountForBytecodeOffset(int bytecodeOffset);
422
423     bool likelyToTakeSlowCase(int bytecodeOffset)
424     {
425         if (!hasBaselineJITProfiling())
426             return false;
427         unsigned value = rareCaseProfileCountForBytecodeOffset(bytecodeOffset);
428         return value >= Options::likelyToTakeSlowCaseMinimumCount();
429     }
430
431     bool couldTakeSlowCase(int bytecodeOffset)
432     {
433         if (!hasBaselineJITProfiling())
434             return false;
435         unsigned value = rareCaseProfileCountForBytecodeOffset(bytecodeOffset);
436         return value >= Options::couldTakeSlowCaseMinimumCount();
437     }
438
439     ResultProfile* ensureResultProfile(int bytecodeOffset);
440     ResultProfile* ensureResultProfile(const ConcurrentJITLocker&, int bytecodeOffset);
441     unsigned numberOfResultProfiles() { return m_resultProfiles.size(); }
442     ResultProfile* resultProfileForBytecodeOffset(int bytecodeOffset);
443     ResultProfile* resultProfileForBytecodeOffset(const ConcurrentJITLocker&, int bytecodeOffset);
444
445     unsigned specialFastCaseProfileCountForBytecodeOffset(int bytecodeOffset)
446     {
447         ResultProfile* profile = resultProfileForBytecodeOffset(bytecodeOffset);
448         if (!profile)
449             return 0;
450         return profile->specialFastPathCount();
451     }
452
453     bool couldTakeSpecialFastCase(int bytecodeOffset)
454     {
455         if (!hasBaselineJITProfiling())
456             return false;
457         unsigned specialFastCaseCount = specialFastCaseProfileCountForBytecodeOffset(bytecodeOffset);
458         return specialFastCaseCount >= Options::couldTakeSlowCaseMinimumCount();
459     }
460
461     unsigned numberOfArrayProfiles() const { return m_arrayProfiles.size(); }
462     const ArrayProfileVector& arrayProfiles() { return m_arrayProfiles; }
463     ArrayProfile* addArrayProfile(unsigned bytecodeOffset)
464     {
465         m_arrayProfiles.append(ArrayProfile(bytecodeOffset));
466         return &m_arrayProfiles.last();
467     }
468     ArrayProfile* getArrayProfile(unsigned bytecodeOffset);
469     ArrayProfile* getOrAddArrayProfile(unsigned bytecodeOffset);
470
471     // Exception handling support
472
473     size_t numberOfExceptionHandlers() const { return m_rareData ? m_rareData->m_exceptionHandlers.size() : 0; }
474     HandlerInfo& exceptionHandler(int index) { RELEASE_ASSERT(m_rareData); return m_rareData->m_exceptionHandlers[index]; }
475
476     bool hasExpressionInfo() { return m_unlinkedCode->hasExpressionInfo(); }
477
478 #if ENABLE(DFG_JIT)
479     Vector<CodeOrigin, 0, UnsafeVectorOverflow>& codeOrigins();
480     
481     // Having code origins implies that there has been some inlining.
482     bool hasCodeOrigins()
483     {
484         return JITCode::isOptimizingJIT(jitType());
485     }
486         
487     bool canGetCodeOrigin(CallSiteIndex index)
488     {
489         if (!hasCodeOrigins())
490             return false;
491         return index.bits() < codeOrigins().size();
492     }
493
494     CodeOrigin codeOrigin(CallSiteIndex index)
495     {
496         return codeOrigins()[index.bits()];
497     }
498
499     bool addFrequentExitSite(const DFG::FrequentExitSite& site)
500     {
501         ASSERT(JITCode::isBaselineCode(jitType()));
502         ConcurrentJITLocker locker(m_lock);
503         return m_exitProfile.add(locker, this, site);
504     }
505
506     bool hasExitSite(const ConcurrentJITLocker& locker, const DFG::FrequentExitSite& site) const
507     {
508         return m_exitProfile.hasExitSite(locker, site);
509     }
510     bool hasExitSite(const DFG::FrequentExitSite& site) const
511     {
512         ConcurrentJITLocker locker(m_lock);
513         return hasExitSite(locker, site);
514     }
515
516     DFG::ExitProfile& exitProfile() { return m_exitProfile; }
517
518     CompressedLazyOperandValueProfileHolder& lazyOperandValueProfiles()
519     {
520         return m_lazyOperandValueProfiles;
521     }
522 #endif // ENABLE(DFG_JIT)
523
524     // Constant Pool
525 #if ENABLE(DFG_JIT)
526     size_t numberOfIdentifiers() const { return m_unlinkedCode->numberOfIdentifiers() + numberOfDFGIdentifiers(); }
527     size_t numberOfDFGIdentifiers() const;
528     const Identifier& identifier(int index) const;
529 #else
530     size_t numberOfIdentifiers() const { return m_unlinkedCode->numberOfIdentifiers(); }
531     const Identifier& identifier(int index) const { return m_unlinkedCode->identifier(index); }
532 #endif
533
534     Vector<WriteBarrier<Unknown>>& constants() { return m_constantRegisters; }
535     Vector<SourceCodeRepresentation>& constantsSourceCodeRepresentation() { return m_constantsSourceCodeRepresentation; }
536     unsigned addConstant(JSValue v)
537     {
538         unsigned result = m_constantRegisters.size();
539         m_constantRegisters.append(WriteBarrier<Unknown>());
540         m_constantRegisters.last().set(m_globalObject->vm(), this, v);
541         m_constantsSourceCodeRepresentation.append(SourceCodeRepresentation::Other);
542         return result;
543     }
544
545     unsigned addConstantLazily()
546     {
547         unsigned result = m_constantRegisters.size();
548         m_constantRegisters.append(WriteBarrier<Unknown>());
549         m_constantsSourceCodeRepresentation.append(SourceCodeRepresentation::Other);
550         return result;
551     }
552
553     WriteBarrier<Unknown>& constantRegister(int index) { return m_constantRegisters[index - FirstConstantRegisterIndex]; }
554     ALWAYS_INLINE bool isConstantRegisterIndex(int index) const { return index >= FirstConstantRegisterIndex; }
555     ALWAYS_INLINE JSValue getConstant(int index) const { return m_constantRegisters[index - FirstConstantRegisterIndex].get(); }
556     ALWAYS_INLINE SourceCodeRepresentation constantSourceCodeRepresentation(int index) const { return m_constantsSourceCodeRepresentation[index - FirstConstantRegisterIndex]; }
557
558     FunctionExecutable* functionDecl(int index) { return m_functionDecls[index].get(); }
559     int numberOfFunctionDecls() { return m_functionDecls.size(); }
560     FunctionExecutable* functionExpr(int index) { return m_functionExprs[index].get(); }
561     
562     RegExp* regexp(int index) const { return m_unlinkedCode->regexp(index); }
563
564     unsigned numberOfConstantBuffers() const
565     {
566         if (!m_rareData)
567             return 0;
568         return m_rareData->m_constantBuffers.size();
569     }
570     unsigned addConstantBuffer(const Vector<JSValue>& buffer)
571     {
572         createRareDataIfNecessary();
573         unsigned size = m_rareData->m_constantBuffers.size();
574         m_rareData->m_constantBuffers.append(buffer);
575         return size;
576     }
577
578     Vector<JSValue>& constantBufferAsVector(unsigned index)
579     {
580         ASSERT(m_rareData);
581         return m_rareData->m_constantBuffers[index];
582     }
583     JSValue* constantBuffer(unsigned index)
584     {
585         return constantBufferAsVector(index).data();
586     }
587
588     Heap* heap() const { return &m_vm->heap; }
589     JSGlobalObject* globalObject() { return m_globalObject.get(); }
590
591     JSGlobalObject* globalObjectFor(CodeOrigin);
592
593     BytecodeLivenessAnalysis& livenessAnalysis()
594     {
595         {
596             ConcurrentJITLocker locker(m_lock);
597             if (!!m_livenessAnalysis)
598                 return *m_livenessAnalysis;
599         }
600         std::unique_ptr<BytecodeLivenessAnalysis> analysis =
601             std::make_unique<BytecodeLivenessAnalysis>(this);
602         {
603             ConcurrentJITLocker locker(m_lock);
604             if (!m_livenessAnalysis)
605                 m_livenessAnalysis = WTFMove(analysis);
606             return *m_livenessAnalysis;
607         }
608     }
609     
610     void validate();
611
612     // Jump Tables
613
614     size_t numberOfSwitchJumpTables() const { return m_rareData ? m_rareData->m_switchJumpTables.size() : 0; }
615     SimpleJumpTable& addSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_switchJumpTables.append(SimpleJumpTable()); return m_rareData->m_switchJumpTables.last(); }
616     SimpleJumpTable& switchJumpTable(int tableIndex) { RELEASE_ASSERT(m_rareData); return m_rareData->m_switchJumpTables[tableIndex]; }
617     void clearSwitchJumpTables()
618     {
619         if (!m_rareData)
620             return;
621         m_rareData->m_switchJumpTables.clear();
622     }
623
624     size_t numberOfStringSwitchJumpTables() const { return m_rareData ? m_rareData->m_stringSwitchJumpTables.size() : 0; }
625     StringJumpTable& addStringSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_stringSwitchJumpTables.append(StringJumpTable()); return m_rareData->m_stringSwitchJumpTables.last(); }
626     StringJumpTable& stringSwitchJumpTable(int tableIndex) { RELEASE_ASSERT(m_rareData); return m_rareData->m_stringSwitchJumpTables[tableIndex]; }
627
628     // Live callee registers at yield points.
629     const FastBitVector& liveCalleeLocalsAtYield(unsigned index) const
630     {
631         RELEASE_ASSERT(m_rareData);
632         return m_rareData->m_liveCalleeLocalsAtYield[index];
633     }
634     FastBitVector& liveCalleeLocalsAtYield(unsigned index)
635     {
636         RELEASE_ASSERT(m_rareData);
637         return m_rareData->m_liveCalleeLocalsAtYield[index];
638     }
639
640     EvalCodeCache& evalCodeCache() { createRareDataIfNecessary(); return m_rareData->m_evalCodeCache; }
641
642     enum ShrinkMode {
643         // Shrink prior to generating machine code that may point directly into vectors.
644         EarlyShrink,
645
646         // Shrink after generating machine code, and after possibly creating new vectors
647         // and appending to others. At this time it is not safe to shrink certain vectors
648         // because we would have generated machine code that references them directly.
649         LateShrink
650     };
651     void shrinkToFit(ShrinkMode);
652
653     // Functions for controlling when JITting kicks in, in a mixed mode
654     // execution world.
655
656     bool checkIfJITThresholdReached()
657     {
658         return m_llintExecuteCounter.checkIfThresholdCrossedAndSet(this);
659     }
660
661     void dontJITAnytimeSoon()
662     {
663         m_llintExecuteCounter.deferIndefinitely();
664     }
665
666     void jitAfterWarmUp()
667     {
668         m_llintExecuteCounter.setNewThreshold(Options::thresholdForJITAfterWarmUp(), this);
669     }
670
671     void jitSoon()
672     {
673         m_llintExecuteCounter.setNewThreshold(Options::thresholdForJITSoon(), this);
674     }
675
676     const BaselineExecutionCounter& llintExecuteCounter() const
677     {
678         return m_llintExecuteCounter;
679     }
680
681     // Functions for controlling when tiered compilation kicks in. This
682     // controls both when the optimizing compiler is invoked and when OSR
683     // entry happens. Two triggers exist: the loop trigger and the return
684     // trigger. In either case, when an addition to m_jitExecuteCounter
685     // causes it to become non-negative, the optimizing compiler is
686     // invoked. This includes a fast check to see if this CodeBlock has
687     // already been optimized (i.e. replacement() returns a CodeBlock
688     // that was optimized with a higher tier JIT than this one). In the
689     // case of the loop trigger, if the optimized compilation succeeds
690     // (or has already succeeded in the past) then OSR is attempted to
691     // redirect program flow into the optimized code.
692
693     // These functions are called from within the optimization triggers,
694     // and are used as a single point at which we define the heuristics
695     // for how much warm-up is mandated before the next optimization
696     // trigger files. All CodeBlocks start out with optimizeAfterWarmUp(),
697     // as this is called from the CodeBlock constructor.
698
699     // When we observe a lot of speculation failures, we trigger a
700     // reoptimization. But each time, we increase the optimization trigger
701     // to avoid thrashing.
702     JS_EXPORT_PRIVATE unsigned reoptimizationRetryCounter() const;
703     void countReoptimization();
704 #if ENABLE(JIT)
705     static unsigned numberOfLLIntBaselineCalleeSaveRegisters() { return RegisterSet::llintBaselineCalleeSaveRegisters().numberOfSetRegisters(); }
706     static size_t llintBaselineCalleeSaveSpaceAsVirtualRegisters();
707     size_t calleeSaveSpaceAsVirtualRegisters();
708
709     unsigned numberOfDFGCompiles();
710
711     int32_t codeTypeThresholdMultiplier() const;
712
713     int32_t adjustedCounterValue(int32_t desiredThreshold);
714
715     int32_t* addressOfJITExecuteCounter()
716     {
717         return &m_jitExecuteCounter.m_counter;
718     }
719
720     static ptrdiff_t offsetOfJITExecuteCounter() { return OBJECT_OFFSETOF(CodeBlock, m_jitExecuteCounter) + OBJECT_OFFSETOF(BaselineExecutionCounter, m_counter); }
721     static ptrdiff_t offsetOfJITExecutionActiveThreshold() { return OBJECT_OFFSETOF(CodeBlock, m_jitExecuteCounter) + OBJECT_OFFSETOF(BaselineExecutionCounter, m_activeThreshold); }
722     static ptrdiff_t offsetOfJITExecutionTotalCount() { return OBJECT_OFFSETOF(CodeBlock, m_jitExecuteCounter) + OBJECT_OFFSETOF(BaselineExecutionCounter, m_totalCount); }
723
724     const BaselineExecutionCounter& jitExecuteCounter() const { return m_jitExecuteCounter; }
725
726     unsigned optimizationDelayCounter() const { return m_optimizationDelayCounter; }
727
728     // Check if the optimization threshold has been reached, and if not,
729     // adjust the heuristics accordingly. Returns true if the threshold has
730     // been reached.
731     bool checkIfOptimizationThresholdReached();
732
733     // Call this to force the next optimization trigger to fire. This is
734     // rarely wise, since optimization triggers are typically more
735     // expensive than executing baseline code.
736     void optimizeNextInvocation();
737
738     // Call this to prevent optimization from happening again. Note that
739     // optimization will still happen after roughly 2^29 invocations,
740     // so this is really meant to delay that as much as possible. This
741     // is called if optimization failed, and we expect it to fail in
742     // the future as well.
743     void dontOptimizeAnytimeSoon();
744
745     // Call this to reinitialize the counter to its starting state,
746     // forcing a warm-up to happen before the next optimization trigger
747     // fires. This is called in the CodeBlock constructor. It also
748     // makes sense to call this if an OSR exit occurred. Note that
749     // OSR exit code is code generated, so the value of the execute
750     // counter that this corresponds to is also available directly.
751     void optimizeAfterWarmUp();
752
753     // Call this to force an optimization trigger to fire only after
754     // a lot of warm-up.
755     void optimizeAfterLongWarmUp();
756
757     // Call this to cause an optimization trigger to fire soon, but
758     // not necessarily the next one. This makes sense if optimization
759     // succeeds. Successfuly optimization means that all calls are
760     // relinked to the optimized code, so this only affects call
761     // frames that are still executing this CodeBlock. The value here
762     // is tuned to strike a balance between the cost of OSR entry
763     // (which is too high to warrant making every loop back edge to
764     // trigger OSR immediately) and the cost of executing baseline
765     // code (which is high enough that we don't necessarily want to
766     // have a full warm-up). The intuition for calling this instead of
767     // optimizeNextInvocation() is for the case of recursive functions
768     // with loops. Consider that there may be N call frames of some
769     // recursive function, for a reasonably large value of N. The top
770     // one triggers optimization, and then returns, and then all of
771     // the others return. We don't want optimization to be triggered on
772     // each return, as that would be superfluous. It only makes sense
773     // to trigger optimization if one of those functions becomes hot
774     // in the baseline code.
775     void optimizeSoon();
776
777     void forceOptimizationSlowPathConcurrently();
778
779     void setOptimizationThresholdBasedOnCompilationResult(CompilationResult);
780     
781     uint32_t osrExitCounter() const { return m_osrExitCounter; }
782
783     void countOSRExit() { m_osrExitCounter++; }
784
785     uint32_t* addressOfOSRExitCounter() { return &m_osrExitCounter; }
786
787     static ptrdiff_t offsetOfOSRExitCounter() { return OBJECT_OFFSETOF(CodeBlock, m_osrExitCounter); }
788
789     uint32_t adjustedExitCountThreshold(uint32_t desiredThreshold);
790     uint32_t exitCountThresholdForReoptimization();
791     uint32_t exitCountThresholdForReoptimizationFromLoop();
792     bool shouldReoptimizeNow();
793     bool shouldReoptimizeFromLoopNow();
794
795     void setCalleeSaveRegisters(RegisterSet);
796     void setCalleeSaveRegisters(std::unique_ptr<RegisterAtOffsetList>);
797     
798     RegisterAtOffsetList* calleeSaveRegisters() const { return m_calleeSaveRegisters.get(); }
799 #else // No JIT
800     static unsigned numberOfLLIntBaselineCalleeSaveRegisters() { return 0; }
801     static size_t llintBaselineCalleeSaveSpaceAsVirtualRegisters() { return 0; };
802     void optimizeAfterWarmUp() { }
803     unsigned numberOfDFGCompiles() { return 0; }
804 #endif
805
806     bool shouldOptimizeNow();
807     void updateAllValueProfilePredictions();
808     void updateAllArrayPredictions();
809     void updateAllPredictions();
810
811     unsigned frameRegisterCount();
812     int stackPointerOffset();
813
814     bool hasOpDebugForLineAndColumn(unsigned line, unsigned column);
815
816     bool hasDebuggerRequests() const { return m_debuggerRequests; }
817     void* debuggerRequestsAddress() { return &m_debuggerRequests; }
818
819     void addBreakpoint(unsigned numBreakpoints);
820     void removeBreakpoint(unsigned numBreakpoints)
821     {
822         ASSERT(m_numBreakpoints >= numBreakpoints);
823         m_numBreakpoints -= numBreakpoints;
824     }
825
826     enum SteppingMode {
827         SteppingModeDisabled,
828         SteppingModeEnabled
829     };
830     void setSteppingMode(SteppingMode);
831
832     void clearDebuggerRequests()
833     {
834         m_steppingMode = SteppingModeDisabled;
835         m_numBreakpoints = 0;
836     }
837
838     bool wasCompiledWithDebuggingOpcodes() const { return m_unlinkedCode->wasCompiledWithDebuggingOpcodes(); }
839     
840     // FIXME: Make these remaining members private.
841
842     int m_numCalleeLocals;
843     int m_numVars;
844     
845     // This is intentionally public; it's the responsibility of anyone doing any
846     // of the following to hold the lock:
847     //
848     // - Modifying any inline cache in this code block.
849     //
850     // - Quering any inline cache in this code block, from a thread other than
851     //   the main thread.
852     //
853     // Additionally, it's only legal to modify the inline cache on the main
854     // thread. This means that the main thread can query the inline cache without
855     // locking. This is crucial since executing the inline cache is effectively
856     // "querying" it.
857     //
858     // Another exception to the rules is that the GC can do whatever it wants
859     // without holding any locks, because the GC is guaranteed to wait until any
860     // concurrent compilation threads finish what they're doing.
861     mutable ConcurrentJITLock m_lock;
862
863     Atomic<bool> m_visitWeaklyHasBeenCalled;
864
865     bool m_shouldAlwaysBeInlined; // Not a bitfield because the JIT wants to store to it.
866
867 #if ENABLE(JIT)
868     unsigned m_capabilityLevelState : 2; // DFG::CapabilityLevel
869 #endif
870
871     bool m_allTransitionsHaveBeenMarked : 1; // Initialized and used on every GC.
872
873     bool m_didFailFTLCompilation : 1;
874     bool m_hasBeenCompiledWithFTL : 1;
875     bool m_isConstructor : 1;
876     bool m_isStrictMode : 1;
877     unsigned m_codeType : 2; // CodeType
878
879     // Internal methods for use by validation code. It would be private if it wasn't
880     // for the fact that we use it from anonymous namespaces.
881     void beginValidationDidFail();
882     NO_RETURN_DUE_TO_CRASH void endValidationDidFail();
883
884     struct RareData {
885         WTF_MAKE_FAST_ALLOCATED;
886     public:
887         Vector<HandlerInfo> m_exceptionHandlers;
888
889         // Buffers used for large array literals
890         Vector<Vector<JSValue>> m_constantBuffers;
891
892         // Jump Tables
893         Vector<SimpleJumpTable> m_switchJumpTables;
894         Vector<StringJumpTable> m_stringSwitchJumpTables;
895
896         Vector<FastBitVector> m_liveCalleeLocalsAtYield;
897
898         EvalCodeCache m_evalCodeCache;
899     };
900
901     void clearExceptionHandlers()
902     {
903         if (m_rareData)
904             m_rareData->m_exceptionHandlers.clear();
905     }
906
907     void appendExceptionHandler(const HandlerInfo& handler)
908     {
909         createRareDataIfNecessary(); // We may be handling the exception of an inlined call frame.
910         m_rareData->m_exceptionHandlers.append(handler);
911     }
912
913     CallSiteIndex newExceptionHandlingCallSiteIndex(CallSiteIndex originalCallSite);
914
915 #if ENABLE(JIT)
916     void setPCToCodeOriginMap(std::unique_ptr<PCToCodeOriginMap>&&);
917     Optional<CodeOrigin> findPC(void* pc);
918 #endif
919
920 protected:
921     void finalizeLLIntInlineCaches();
922     void finalizeBaselineJITInlineCaches();
923
924 #if ENABLE(DFG_JIT)
925     void tallyFrequentExitSites();
926 #else
927     void tallyFrequentExitSites() { }
928 #endif
929
930 private:
931     friend class CodeBlockSet;
932     
933     CodeBlock* specialOSREntryBlockOrNull();
934     
935     void noticeIncomingCall(ExecState* callerFrame);
936     
937     double optimizationThresholdScalingFactor();
938
939     void updateAllPredictionsAndCountLiveness(unsigned& numberOfLiveNonArgumentValueProfiles, unsigned& numberOfSamplesInProfiles);
940
941     void setConstantRegisters(const Vector<WriteBarrier<Unknown>>& constants, const Vector<SourceCodeRepresentation>& constantsSourceCodeRepresentation);
942
943     void replaceConstant(int index, JSValue value)
944     {
945         ASSERT(isConstantRegisterIndex(index) && static_cast<size_t>(index - FirstConstantRegisterIndex) < m_constantRegisters.size());
946         m_constantRegisters[index - FirstConstantRegisterIndex].set(m_globalObject->vm(), this, value);
947     }
948
949     void dumpBytecode(
950         PrintStream&, ExecState*, const Instruction* begin, const Instruction*&,
951         const StubInfoMap& = StubInfoMap(), const CallLinkInfoMap& = CallLinkInfoMap());
952
953     CString registerName(int r) const;
954     CString constantName(int index) const;
955     void printUnaryOp(PrintStream&, ExecState*, int location, const Instruction*&, const char* op);
956     void printBinaryOp(PrintStream&, ExecState*, int location, const Instruction*&, const char* op);
957     void printConditionalJump(PrintStream&, ExecState*, const Instruction*, const Instruction*&, int location, const char* op);
958     void printGetByIdOp(PrintStream&, ExecState*, int location, const Instruction*&);
959     void printGetByIdCacheStatus(PrintStream&, ExecState*, int location, const StubInfoMap&);
960     enum CacheDumpMode { DumpCaches, DontDumpCaches };
961     void printCallOp(PrintStream&, ExecState*, int location, const Instruction*&, const char* op, CacheDumpMode, bool& hasPrintedProfiling, const CallLinkInfoMap&);
962     void printPutByIdOp(PrintStream&, ExecState*, int location, const Instruction*&, const char* op);
963     void printPutByIdCacheStatus(PrintStream&, int location, const StubInfoMap&);
964     void printLocationAndOp(PrintStream&, ExecState*, int location, const Instruction*&, const char* op);
965     void printLocationOpAndRegisterOperand(PrintStream&, ExecState*, int location, const Instruction*& it, const char* op, int operand);
966
967     void beginDumpProfiling(PrintStream&, bool& hasPrintedProfiling);
968     void dumpValueProfiling(PrintStream&, const Instruction*&, bool& hasPrintedProfiling);
969     void dumpArrayProfiling(PrintStream&, const Instruction*&, bool& hasPrintedProfiling);
970     void dumpRareCaseProfile(PrintStream&, const char* name, RareCaseProfile*, bool& hasPrintedProfiling);
971     void dumpResultProfile(PrintStream&, ResultProfile*, bool& hasPrintedProfiling);
972
973     bool shouldVisitStrongly();
974     bool shouldJettisonDueToWeakReference();
975     bool shouldJettisonDueToOldAge();
976     
977     void propagateTransitions(SlotVisitor&);
978     void determineLiveness(SlotVisitor&);
979         
980     void stronglyVisitStrongReferences(SlotVisitor&);
981     void stronglyVisitWeakReferences(SlotVisitor&);
982     void visitOSRExitTargets(SlotVisitor&);
983
984     std::chrono::milliseconds timeSinceCreation()
985     {
986         return std::chrono::duration_cast<std::chrono::milliseconds>(
987             std::chrono::steady_clock::now() - m_creationTime);
988     }
989
990     void createRareDataIfNecessary()
991     {
992         if (!m_rareData)
993             m_rareData = std::make_unique<RareData>();
994     }
995
996     void insertBasicBlockBoundariesForControlFlowProfiler(RefCountedArray<Instruction>&);
997
998     WriteBarrier<UnlinkedCodeBlock> m_unlinkedCode;
999     int m_numParameters;
1000     union {
1001         unsigned m_debuggerRequests;
1002         struct {
1003             unsigned m_hasDebuggerStatement : 1;
1004             unsigned m_steppingMode : 1;
1005             unsigned m_numBreakpoints : 30;
1006         };
1007     };
1008     WriteBarrier<ExecutableBase> m_ownerExecutable;
1009     VM* m_vm;
1010
1011     RefCountedArray<Instruction> m_instructions;
1012     VirtualRegister m_thisRegister;
1013     VirtualRegister m_scopeRegister;
1014     mutable CodeBlockHash m_hash;
1015
1016     RefPtr<SourceProvider> m_source;
1017     unsigned m_sourceOffset;
1018     unsigned m_firstLineColumnOffset;
1019
1020     RefCountedArray<LLIntCallLinkInfo> m_llintCallLinkInfos;
1021     SentinelLinkedList<LLIntCallLinkInfo, BasicRawSentinelNode<LLIntCallLinkInfo>> m_incomingLLIntCalls;
1022     RefPtr<JITCode> m_jitCode;
1023 #if ENABLE(JIT)
1024     std::unique_ptr<RegisterAtOffsetList> m_calleeSaveRegisters;
1025     Bag<StructureStubInfo> m_stubInfos;
1026     Bag<ByValInfo> m_byValInfos;
1027     Bag<CallLinkInfo> m_callLinkInfos;
1028     SentinelLinkedList<CallLinkInfo, BasicRawSentinelNode<CallLinkInfo>> m_incomingCalls;
1029     SentinelLinkedList<PolymorphicCallNode, BasicRawSentinelNode<PolymorphicCallNode>> m_incomingPolymorphicCalls;
1030     std::unique_ptr<PCToCodeOriginMap> m_pcToCodeOriginMap;
1031 #endif
1032     std::unique_ptr<CompactJITCodeMap> m_jitCodeMap;
1033 #if ENABLE(DFG_JIT)
1034     // This is relevant to non-DFG code blocks that serve as the profiled code block
1035     // for DFG code blocks.
1036     DFG::ExitProfile m_exitProfile;
1037     CompressedLazyOperandValueProfileHolder m_lazyOperandValueProfiles;
1038 #endif
1039     RefCountedArray<ValueProfile> m_argumentValueProfiles;
1040     RefCountedArray<ValueProfile> m_valueProfiles;
1041     SegmentedVector<RareCaseProfile, 8> m_rareCaseProfiles;
1042     SegmentedVector<ResultProfile, 8> m_resultProfiles;
1043     typedef HashMap<unsigned, unsigned, IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> BytecodeOffsetToResultProfileIndexMap;
1044     std::unique_ptr<BytecodeOffsetToResultProfileIndexMap> m_bytecodeOffsetToResultProfileIndexMap;
1045     RefCountedArray<ArrayAllocationProfile> m_arrayAllocationProfiles;
1046     ArrayProfileVector m_arrayProfiles;
1047     RefCountedArray<ObjectAllocationProfile> m_objectAllocationProfiles;
1048
1049     // Constant Pool
1050     COMPILE_ASSERT(sizeof(Register) == sizeof(WriteBarrier<Unknown>), Register_must_be_same_size_as_WriteBarrier_Unknown);
1051     // TODO: This could just be a pointer to m_unlinkedCodeBlock's data, but the DFG mutates
1052     // it, so we're stuck with it for now.
1053     Vector<WriteBarrier<Unknown>> m_constantRegisters;
1054     Vector<SourceCodeRepresentation> m_constantsSourceCodeRepresentation;
1055     RefCountedArray<WriteBarrier<FunctionExecutable>> m_functionDecls;
1056     RefCountedArray<WriteBarrier<FunctionExecutable>> m_functionExprs;
1057
1058     WriteBarrier<CodeBlock> m_alternative;
1059     
1060     BaselineExecutionCounter m_llintExecuteCounter;
1061
1062     BaselineExecutionCounter m_jitExecuteCounter;
1063     uint32_t m_osrExitCounter;
1064     uint16_t m_optimizationDelayCounter;
1065     uint16_t m_reoptimizationRetryCounter;
1066
1067     std::chrono::steady_clock::time_point m_creationTime;
1068
1069     std::unique_ptr<BytecodeLivenessAnalysis> m_livenessAnalysis;
1070
1071     std::unique_ptr<RareData> m_rareData;
1072
1073     UnconditionalFinalizer m_unconditionalFinalizer;
1074     WeakReferenceHarvester m_weakReferenceHarvester;
1075 };
1076
1077 // Program code is not marked by any function, so we make the global object
1078 // responsible for marking it.
1079
1080 class GlobalCodeBlock : public CodeBlock {
1081     typedef CodeBlock Base;
1082     DECLARE_INFO;
1083
1084 protected:
1085     GlobalCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, GlobalCodeBlock& other)
1086         : CodeBlock(vm, structure, CopyParsedBlock, other)
1087     {
1088     }
1089
1090     GlobalCodeBlock(VM* vm, Structure* structure, ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock, JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset)
1091         : CodeBlock(vm, structure, ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, sourceOffset, firstLineColumnOffset)
1092     {
1093     }
1094 };
1095
1096 class ProgramCodeBlock : public GlobalCodeBlock {
1097 public:
1098     typedef GlobalCodeBlock Base;
1099     DECLARE_INFO;
1100
1101     static ProgramCodeBlock* create(VM* vm, CopyParsedBlockTag, ProgramCodeBlock& other)
1102     {
1103         ProgramCodeBlock* instance = new (NotNull, allocateCell<ProgramCodeBlock>(vm->heap))
1104             ProgramCodeBlock(vm, vm->programCodeBlockStructure.get(), CopyParsedBlock, other);
1105         instance->finishCreation(*vm, CopyParsedBlock, other);
1106         return instance;
1107     }
1108
1109     static ProgramCodeBlock* create(VM* vm, ProgramExecutable* ownerExecutable, UnlinkedProgramCodeBlock* unlinkedCodeBlock,
1110         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned firstLineColumnOffset)
1111     {
1112         ProgramCodeBlock* instance = new (NotNull, allocateCell<ProgramCodeBlock>(vm->heap))
1113             ProgramCodeBlock(vm, vm->programCodeBlockStructure.get(), ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, firstLineColumnOffset);
1114         instance->finishCreation(*vm, ownerExecutable, unlinkedCodeBlock, scope);
1115         return instance;
1116     }
1117
1118     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
1119     {
1120         return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
1121     }
1122
1123 private:
1124     ProgramCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, ProgramCodeBlock& other)
1125         : GlobalCodeBlock(vm, structure, CopyParsedBlock, other)
1126     {
1127     }
1128
1129     ProgramCodeBlock(VM* vm, Structure* structure, ProgramExecutable* ownerExecutable, UnlinkedProgramCodeBlock* unlinkedCodeBlock,
1130         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned firstLineColumnOffset)
1131         : GlobalCodeBlock(vm, structure, ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, 0, firstLineColumnOffset)
1132     {
1133     }
1134
1135     static void destroy(JSCell*);
1136 };
1137
1138 class ModuleProgramCodeBlock : public GlobalCodeBlock {
1139 public:
1140     typedef GlobalCodeBlock Base;
1141     DECLARE_INFO;
1142
1143     static ModuleProgramCodeBlock* create(VM* vm, CopyParsedBlockTag, ModuleProgramCodeBlock& other)
1144     {
1145         ModuleProgramCodeBlock* instance = new (NotNull, allocateCell<ModuleProgramCodeBlock>(vm->heap))
1146             ModuleProgramCodeBlock(vm, vm->moduleProgramCodeBlockStructure.get(), CopyParsedBlock, other);
1147         instance->finishCreation(*vm, CopyParsedBlock, other);
1148         return instance;
1149     }
1150
1151     static ModuleProgramCodeBlock* create(VM* vm, ModuleProgramExecutable* ownerExecutable, UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock,
1152         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned firstLineColumnOffset)
1153     {
1154         ModuleProgramCodeBlock* instance = new (NotNull, allocateCell<ModuleProgramCodeBlock>(vm->heap))
1155             ModuleProgramCodeBlock(vm, vm->moduleProgramCodeBlockStructure.get(), ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, firstLineColumnOffset);
1156         instance->finishCreation(*vm, ownerExecutable, unlinkedCodeBlock, scope);
1157         return instance;
1158     }
1159
1160     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
1161     {
1162         return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
1163     }
1164
1165 private:
1166     ModuleProgramCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, ModuleProgramCodeBlock& other)
1167         : GlobalCodeBlock(vm, structure, CopyParsedBlock, other)
1168     {
1169     }
1170
1171     ModuleProgramCodeBlock(VM* vm, Structure* structure, ModuleProgramExecutable* ownerExecutable, UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock,
1172         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned firstLineColumnOffset)
1173         : GlobalCodeBlock(vm, structure, ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, 0, firstLineColumnOffset)
1174     {
1175     }
1176
1177     static void destroy(JSCell*);
1178 };
1179
1180 class EvalCodeBlock : public GlobalCodeBlock {
1181 public:
1182     typedef GlobalCodeBlock Base;
1183     DECLARE_INFO;
1184
1185     static EvalCodeBlock* create(VM* vm, CopyParsedBlockTag, EvalCodeBlock& other)
1186     {
1187         EvalCodeBlock* instance = new (NotNull, allocateCell<EvalCodeBlock>(vm->heap))
1188             EvalCodeBlock(vm, vm->evalCodeBlockStructure.get(), CopyParsedBlock, other);
1189         instance->finishCreation(*vm, CopyParsedBlock, other);
1190         return instance;
1191     }
1192
1193     static EvalCodeBlock* create(VM* vm, EvalExecutable* ownerExecutable, UnlinkedEvalCodeBlock* unlinkedCodeBlock,
1194         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider)
1195     {
1196         EvalCodeBlock* instance = new (NotNull, allocateCell<EvalCodeBlock>(vm->heap))
1197             EvalCodeBlock(vm, vm->evalCodeBlockStructure.get(), ownerExecutable, unlinkedCodeBlock, scope, sourceProvider);
1198         instance->finishCreation(*vm, ownerExecutable, unlinkedCodeBlock, scope);
1199         return instance;
1200     }
1201
1202     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
1203     {
1204         return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
1205     }
1206
1207     const Identifier& variable(unsigned index) { return unlinkedEvalCodeBlock()->variable(index); }
1208     unsigned numVariables() { return unlinkedEvalCodeBlock()->numVariables(); }
1209     
1210 private:
1211     EvalCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, EvalCodeBlock& other)
1212         : GlobalCodeBlock(vm, structure, CopyParsedBlock, other)
1213     {
1214     }
1215         
1216     EvalCodeBlock(VM* vm, Structure* structure, EvalExecutable* ownerExecutable, UnlinkedEvalCodeBlock* unlinkedCodeBlock,
1217         JSScope* scope, PassRefPtr<SourceProvider> sourceProvider)
1218         : GlobalCodeBlock(vm, structure, ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, 0, 1)
1219     {
1220     }
1221     
1222     static void destroy(JSCell*);
1223
1224 private:
1225     UnlinkedEvalCodeBlock* unlinkedEvalCodeBlock() const { return jsCast<UnlinkedEvalCodeBlock*>(unlinkedCodeBlock()); }
1226 };
1227
1228 class FunctionCodeBlock : public CodeBlock {
1229 public:
1230     typedef CodeBlock Base;
1231     DECLARE_INFO;
1232
1233     static FunctionCodeBlock* create(VM* vm, CopyParsedBlockTag, FunctionCodeBlock& other)
1234     {
1235         FunctionCodeBlock* instance = new (NotNull, allocateCell<FunctionCodeBlock>(vm->heap))
1236             FunctionCodeBlock(vm, vm->functionCodeBlockStructure.get(), CopyParsedBlock, other);
1237         instance->finishCreation(*vm, CopyParsedBlock, other);
1238         return instance;
1239     }
1240
1241     static FunctionCodeBlock* create(VM* vm, FunctionExecutable* ownerExecutable, UnlinkedFunctionCodeBlock* unlinkedCodeBlock, JSScope* scope,
1242         PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset)
1243     {
1244         FunctionCodeBlock* instance = new (NotNull, allocateCell<FunctionCodeBlock>(vm->heap))
1245             FunctionCodeBlock(vm, vm->functionCodeBlockStructure.get(), ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, sourceOffset, firstLineColumnOffset);
1246         instance->finishCreation(*vm, ownerExecutable, unlinkedCodeBlock, scope);
1247         return instance;
1248     }
1249
1250     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
1251     {
1252         return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
1253     }
1254
1255 private:
1256     FunctionCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, FunctionCodeBlock& other)
1257         : CodeBlock(vm, structure, CopyParsedBlock, other)
1258     {
1259     }
1260
1261     FunctionCodeBlock(VM* vm, Structure* structure, FunctionExecutable* ownerExecutable, UnlinkedFunctionCodeBlock* unlinkedCodeBlock, JSScope* scope,
1262         PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset)
1263         : CodeBlock(vm, structure, ownerExecutable, unlinkedCodeBlock, scope, sourceProvider, sourceOffset, firstLineColumnOffset)
1264     {
1265     }
1266     
1267     static void destroy(JSCell*);
1268 };
1269
1270 #if ENABLE(WEBASSEMBLY)
1271 class WebAssemblyCodeBlock : public CodeBlock {
1272 public:
1273     typedef CodeBlock Base;
1274     DECLARE_INFO;
1275
1276     static WebAssemblyCodeBlock* create(VM* vm, CopyParsedBlockTag, WebAssemblyCodeBlock& other)
1277     {
1278         WebAssemblyCodeBlock* instance = new (NotNull, allocateCell<WebAssemblyCodeBlock>(vm->heap))
1279             WebAssemblyCodeBlock(vm, vm->webAssemblyCodeBlockStructure.get(), CopyParsedBlock, other);
1280         instance->finishCreation(*vm, CopyParsedBlock, other);
1281         return instance;
1282     }
1283
1284     static WebAssemblyCodeBlock* create(VM* vm, WebAssemblyExecutable* ownerExecutable, JSGlobalObject* globalObject)
1285     {
1286         WebAssemblyCodeBlock* instance = new (NotNull, allocateCell<WebAssemblyCodeBlock>(vm->heap))
1287             WebAssemblyCodeBlock(vm, vm->webAssemblyCodeBlockStructure.get(), ownerExecutable, globalObject);
1288         instance->finishCreation(*vm, ownerExecutable, globalObject);
1289         return instance;
1290     }
1291
1292     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
1293     {
1294         return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
1295     }
1296
1297 private:
1298     WebAssemblyCodeBlock(VM* vm, Structure* structure, CopyParsedBlockTag, WebAssemblyCodeBlock& other)
1299         : CodeBlock(vm, structure, CopyParsedBlock, other)
1300     {
1301     }
1302
1303     WebAssemblyCodeBlock(VM* vm, Structure* structure, WebAssemblyExecutable* ownerExecutable, JSGlobalObject* globalObject)
1304         : CodeBlock(vm, structure, ownerExecutable, globalObject)
1305     {
1306     }
1307
1308     static void destroy(JSCell*);
1309 };
1310 #endif
1311
1312 inline Register& ExecState::r(int index)
1313 {
1314     CodeBlock* codeBlock = this->codeBlock();
1315     if (codeBlock->isConstantRegisterIndex(index))
1316         return *reinterpret_cast<Register*>(&codeBlock->constantRegister(index));
1317     return this[index];
1318 }
1319
1320 inline Register& ExecState::r(VirtualRegister reg)
1321 {
1322     return r(reg.offset());
1323 }
1324
1325 inline Register& ExecState::uncheckedR(int index)
1326 {
1327     RELEASE_ASSERT(index < FirstConstantRegisterIndex);
1328     return this[index];
1329 }
1330
1331 inline Register& ExecState::uncheckedR(VirtualRegister reg)
1332 {
1333     return uncheckedR(reg.offset());
1334 }
1335
1336 inline void CodeBlock::clearVisitWeaklyHasBeenCalled()
1337 {
1338     m_visitWeaklyHasBeenCalled.store(false, std::memory_order_relaxed);
1339 }
1340
1341 inline void CodeBlockSet::mark(const LockHolder& locker, void* candidateCodeBlock)
1342 {
1343     ASSERT(m_lock.isLocked());
1344     // We have to check for 0 and -1 because those are used by the HashMap as markers.
1345     uintptr_t value = reinterpret_cast<uintptr_t>(candidateCodeBlock);
1346     
1347     // This checks for both of those nasty cases in one go.
1348     // 0 + 1 = 1
1349     // -1 + 1 = 0
1350     if (value + 1 <= 1)
1351         return;
1352
1353     CodeBlock* codeBlock = static_cast<CodeBlock*>(candidateCodeBlock); 
1354     if (!m_oldCodeBlocks.contains(codeBlock) && !m_newCodeBlocks.contains(codeBlock))
1355         return;
1356
1357     mark(locker, codeBlock);
1358 }
1359
1360 inline void CodeBlockSet::mark(const LockHolder&, CodeBlock* codeBlock)
1361 {
1362     if (!codeBlock)
1363         return;
1364
1365     // Try to recover gracefully if we forget to execute a barrier for a
1366     // CodeBlock that does value profiling. This is probably overkill, but we
1367     // have always done it.
1368     Heap::heap(codeBlock)->writeBarrier(codeBlock);
1369
1370     m_currentlyExecuting.add(codeBlock);
1371 }
1372
1373 template <typename Functor> inline void ScriptExecutable::forEachCodeBlock(Functor&& functor)
1374 {
1375     switch (type()) {
1376     case ProgramExecutableType: {
1377         if (CodeBlock* codeBlock = static_cast<CodeBlock*>(jsCast<ProgramExecutable*>(this)->m_programCodeBlock.get()))
1378             codeBlock->forEachRelatedCodeBlock(std::forward<Functor>(functor));
1379         break;
1380     }
1381
1382     case EvalExecutableType: {
1383         if (CodeBlock* codeBlock = static_cast<CodeBlock*>(jsCast<EvalExecutable*>(this)->m_evalCodeBlock.get()))
1384             codeBlock->forEachRelatedCodeBlock(std::forward<Functor>(functor));
1385         break;
1386     }
1387
1388     case FunctionExecutableType: {
1389         Functor f(std::forward<Functor>(functor));
1390         FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
1391         if (CodeBlock* codeBlock = static_cast<CodeBlock*>(executable->m_codeBlockForCall.get()))
1392             codeBlock->forEachRelatedCodeBlock(f);
1393         if (CodeBlock* codeBlock = static_cast<CodeBlock*>(executable->m_codeBlockForConstruct.get()))
1394             codeBlock->forEachRelatedCodeBlock(f);
1395         break;
1396     }
1397
1398     case ModuleProgramExecutableType: {
1399         if (CodeBlock* codeBlock = static_cast<CodeBlock*>(jsCast<ModuleProgramExecutable*>(this)->m_moduleProgramCodeBlock.get()))
1400             codeBlock->forEachRelatedCodeBlock(std::forward<Functor>(functor));
1401         break;
1402     }
1403
1404     default:
1405         RELEASE_ASSERT_NOT_REACHED();
1406     }
1407 }
1408
1409 #define CODEBLOCK_LOG_EVENT(codeBlock, summary, details) \
1410     (codeBlock->vm()->logEvent(codeBlock, summary, [&] () { return toCString details; }))
1411
1412 } // namespace JSC
1413
1414 #endif // CodeBlock_h