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