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