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