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