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