Unreviewed, rolling out r189079.
[WebKit-https.git] / Source / JavaScriptCore / bytecode / CodeBlock.cpp
1 /*
2  * Copyright (C) 2008-2010, 2012-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 #include "config.h"
31 #include "CodeBlock.h"
32
33 #include "BasicBlockLocation.h"
34 #include "BytecodeGenerator.h"
35 #include "BytecodeUseDef.h"
36 #include "CallLinkStatus.h"
37 #include "DFGCapabilities.h"
38 #include "DFGCommon.h"
39 #include "DFGDriver.h"
40 #include "DFGJITCode.h"
41 #include "DFGWorklist.h"
42 #include "Debugger.h"
43 #include "FunctionExecutableDump.h"
44 #include "InlineCallFrame.h"
45 #include "Interpreter.h"
46 #include "JIT.h"
47 #include "JITStubs.h"
48 #include "JSCJSValue.h"
49 #include "JSFunction.h"
50 #include "JSLexicalEnvironment.h"
51 #include "LLIntEntrypoint.h"
52 #include "LowLevelInterpreter.h"
53 #include "JSCInlines.h"
54 #include "PolymorphicGetByIdList.h"
55 #include "PolymorphicPutByIdList.h"
56 #include "ProfilerDatabase.h"
57 #include "ReduceWhitespace.h"
58 #include "Repatch.h"
59 #include "RepatchBuffer.h"
60 #include "SlotVisitorInlines.h"
61 #include "StackVisitor.h"
62 #include "TypeLocationCache.h"
63 #include "TypeProfiler.h"
64 #include "UnlinkedInstructionStream.h"
65 #include <wtf/BagToHashMap.h>
66 #include <wtf/CommaPrinter.h>
67 #include <wtf/StringExtras.h>
68 #include <wtf/StringPrintStream.h>
69 #include <wtf/text/UniquedStringImpl.h>
70
71 #if ENABLE(DFG_JIT)
72 #include "DFGOperations.h"
73 #endif
74
75 #if ENABLE(FTL_JIT)
76 #include "FTLJITCode.h"
77 #endif
78
79 namespace JSC {
80
81 CString CodeBlock::inferredName() const
82 {
83     switch (codeType()) {
84     case GlobalCode:
85         return "<global>";
86     case EvalCode:
87         return "<eval>";
88     case FunctionCode:
89         return jsCast<FunctionExecutable*>(ownerExecutable())->inferredName().utf8();
90     default:
91         CRASH();
92         return CString("", 0);
93     }
94 }
95
96 bool CodeBlock::hasHash() const
97 {
98     return !!m_hash;
99 }
100
101 bool CodeBlock::isSafeToComputeHash() const
102 {
103     return !isCompilationThread();
104 }
105
106 CodeBlockHash CodeBlock::hash() const
107 {
108     if (!m_hash) {
109         RELEASE_ASSERT(isSafeToComputeHash());
110         m_hash = CodeBlockHash(ownerExecutable()->source(), specializationKind());
111     }
112     return m_hash;
113 }
114
115 CString CodeBlock::sourceCodeForTools() const
116 {
117     if (codeType() != FunctionCode)
118         return ownerExecutable()->source().toUTF8();
119     
120     SourceProvider* provider = source();
121     FunctionExecutable* executable = jsCast<FunctionExecutable*>(ownerExecutable());
122     UnlinkedFunctionExecutable* unlinked = executable->unlinkedExecutable();
123     unsigned unlinkedStartOffset = unlinked->startOffset();
124     unsigned linkedStartOffset = executable->source().startOffset();
125     int delta = linkedStartOffset - unlinkedStartOffset;
126     unsigned rangeStart = delta + unlinked->unlinkedFunctionNameStart();
127     unsigned rangeEnd = delta + unlinked->startOffset() + unlinked->sourceLength();
128     return toCString(
129         "function ",
130         provider->source().impl()->utf8ForRange(rangeStart, rangeEnd - rangeStart));
131 }
132
133 CString CodeBlock::sourceCodeOnOneLine() const
134 {
135     return reduceWhitespace(sourceCodeForTools());
136 }
137
138 CString CodeBlock::hashAsStringIfPossible() const
139 {
140     if (hasHash() || isSafeToComputeHash())
141         return toCString(hash());
142     return "<no-hash>";
143 }
144
145 void CodeBlock::dumpAssumingJITType(PrintStream& out, JITCode::JITType jitType) const
146 {
147     out.print(inferredName(), "#", hashAsStringIfPossible());
148     out.print(":[", RawPointer(this), "->");
149     if (!!m_alternative)
150         out.print(RawPointer(m_alternative.get()), "->");
151     out.print(RawPointer(ownerExecutable()), ", ", jitType, codeType());
152
153     if (codeType() == FunctionCode)
154         out.print(specializationKind());
155     out.print(", ", instructionCount());
156     if (this->jitType() == JITCode::BaselineJIT && m_shouldAlwaysBeInlined)
157         out.print(" (ShouldAlwaysBeInlined)");
158     if (ownerExecutable()->neverInline())
159         out.print(" (NeverInline)");
160     if (ownerExecutable()->didTryToEnterInLoop())
161         out.print(" (DidTryToEnterInLoop)");
162     if (ownerExecutable()->isStrictMode())
163         out.print(" (StrictMode)");
164     if (this->jitType() == JITCode::BaselineJIT && m_didFailFTLCompilation)
165         out.print(" (FTLFail)");
166     if (this->jitType() == JITCode::BaselineJIT && m_hasBeenCompiledWithFTL)
167         out.print(" (HadFTLReplacement)");
168     out.print("]");
169 }
170
171 void CodeBlock::dump(PrintStream& out) const
172 {
173     dumpAssumingJITType(out, jitType());
174 }
175
176 static CString idName(int id0, const Identifier& ident)
177 {
178     return toCString(ident.impl(), "(@id", id0, ")");
179 }
180
181 CString CodeBlock::registerName(int r) const
182 {
183     if (isConstantRegisterIndex(r))
184         return constantName(r);
185
186     return toCString(VirtualRegister(r));
187 }
188
189 CString CodeBlock::constantName(int index) const
190 {
191     JSValue value = getConstant(index);
192     return toCString(value, "(", VirtualRegister(index), ")");
193 }
194
195 static CString regexpToSourceString(RegExp* regExp)
196 {
197     char postfix[5] = { '/', 0, 0, 0, 0 };
198     int index = 1;
199     if (regExp->global())
200         postfix[index++] = 'g';
201     if (regExp->ignoreCase())
202         postfix[index++] = 'i';
203     if (regExp->multiline())
204         postfix[index] = 'm';
205
206     return toCString("/", regExp->pattern().impl(), postfix);
207 }
208
209 static CString regexpName(int re, RegExp* regexp)
210 {
211     return toCString(regexpToSourceString(regexp), "(@re", re, ")");
212 }
213
214 NEVER_INLINE static const char* debugHookName(int debugHookID)
215 {
216     switch (static_cast<DebugHookID>(debugHookID)) {
217         case DidEnterCallFrame:
218             return "didEnterCallFrame";
219         case WillLeaveCallFrame:
220             return "willLeaveCallFrame";
221         case WillExecuteStatement:
222             return "willExecuteStatement";
223         case WillExecuteProgram:
224             return "willExecuteProgram";
225         case DidExecuteProgram:
226             return "didExecuteProgram";
227         case DidReachBreakpoint:
228             return "didReachBreakpoint";
229     }
230
231     RELEASE_ASSERT_NOT_REACHED();
232     return "";
233 }
234
235 void CodeBlock::printUnaryOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op)
236 {
237     int r0 = (++it)->u.operand;
238     int r1 = (++it)->u.operand;
239
240     printLocationAndOp(out, exec, location, it, op);
241     out.printf("%s, %s", registerName(r0).data(), registerName(r1).data());
242 }
243
244 void CodeBlock::printBinaryOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op)
245 {
246     int r0 = (++it)->u.operand;
247     int r1 = (++it)->u.operand;
248     int r2 = (++it)->u.operand;
249     printLocationAndOp(out, exec, location, it, op);
250     out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
251 }
252
253 void CodeBlock::printConditionalJump(PrintStream& out, ExecState* exec, const Instruction*, const Instruction*& it, int location, const char* op)
254 {
255     int r0 = (++it)->u.operand;
256     int offset = (++it)->u.operand;
257     printLocationAndOp(out, exec, location, it, op);
258     out.printf("%s, %d(->%d)", registerName(r0).data(), offset, location + offset);
259 }
260
261 void CodeBlock::printGetByIdOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it)
262 {
263     const char* op;
264     switch (exec->interpreter()->getOpcodeID(it->u.opcode)) {
265     case op_get_by_id:
266         op = "get_by_id";
267         break;
268     case op_get_by_id_out_of_line:
269         op = "get_by_id_out_of_line";
270         break;
271     case op_get_array_length:
272         op = "array_length";
273         break;
274     default:
275         RELEASE_ASSERT_NOT_REACHED();
276 #if COMPILER_QUIRK(CONSIDERS_UNREACHABLE_CODE)
277         op = 0;
278 #endif
279     }
280     int r0 = (++it)->u.operand;
281     int r1 = (++it)->u.operand;
282     int id0 = (++it)->u.operand;
283     printLocationAndOp(out, exec, location, it, op);
284     out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), idName(id0, identifier(id0)).data());
285     it += 4; // Increment up to the value profiler.
286 }
287
288 static void dumpStructure(PrintStream& out, const char* name, Structure* structure, const Identifier& ident)
289 {
290     if (!structure)
291         return;
292     
293     out.printf("%s = %p", name, structure);
294     
295     PropertyOffset offset = structure->getConcurrently(ident.impl());
296     if (offset != invalidOffset)
297         out.printf(" (offset = %d)", offset);
298 }
299
300 static void dumpChain(PrintStream& out, StructureChain* chain, const Identifier& ident)
301 {
302     out.printf("chain = %p: [", chain);
303     bool first = true;
304     for (WriteBarrier<Structure>* currentStructure = chain->head();
305          *currentStructure;
306          ++currentStructure) {
307         if (first)
308             first = false;
309         else
310             out.printf(", ");
311         dumpStructure(out, "struct", currentStructure->get(), ident);
312     }
313     out.printf("]");
314 }
315
316 void CodeBlock::printGetByIdCacheStatus(PrintStream& out, ExecState* exec, int location, const StubInfoMap& map)
317 {
318     Instruction* instruction = instructions().begin() + location;
319
320     const Identifier& ident = identifier(instruction[3].u.operand);
321     
322     UNUSED_PARAM(ident); // tell the compiler to shut up in certain platform configurations.
323     
324     if (exec->interpreter()->getOpcodeID(instruction[0].u.opcode) == op_get_array_length)
325         out.printf(" llint(array_length)");
326     else if (Structure* structure = instruction[4].u.structure.get()) {
327         out.printf(" llint(");
328         dumpStructure(out, "struct", structure, ident);
329         out.printf(")");
330     }
331
332 #if ENABLE(JIT)
333     if (StructureStubInfo* stubPtr = map.get(CodeOrigin(location))) {
334         StructureStubInfo& stubInfo = *stubPtr;
335         if (stubInfo.resetByGC)
336             out.print(" (Reset By GC)");
337         
338         if (stubInfo.seen) {
339             out.printf(" jit(");
340             
341             Structure* baseStructure = 0;
342             Structure* prototypeStructure = 0;
343             PolymorphicGetByIdList* list = 0;
344             
345             switch (stubInfo.accessType) {
346             case access_get_by_id_self:
347                 out.printf("self");
348                 baseStructure = stubInfo.u.getByIdSelf.baseObjectStructure.get();
349                 break;
350             case access_get_by_id_list:
351                 out.printf("list");
352                 list = stubInfo.u.getByIdList.list;
353                 break;
354             case access_unset:
355                 out.printf("unset");
356                 break;
357             default:
358                 RELEASE_ASSERT_NOT_REACHED();
359                 break;
360             }
361             
362             if (baseStructure) {
363                 out.printf(", ");
364                 dumpStructure(out, "struct", baseStructure, ident);
365             }
366             
367             if (prototypeStructure) {
368                 out.printf(", ");
369                 dumpStructure(out, "prototypeStruct", baseStructure, ident);
370             }
371             
372             if (list) {
373                 out.printf(", list = %p: [", list);
374                 for (unsigned i = 0; i < list->size(); ++i) {
375                     if (i)
376                         out.printf(", ");
377                     out.printf("(");
378                     dumpStructure(out, "base", list->at(i).structure(), ident);
379                     if (!list->at(i).conditionSet().isEmpty()) {
380                         out.printf(", ");
381                         out.print(list->at(i).conditionSet());
382                     }
383                     out.printf(")");
384                 }
385                 out.printf("]");
386             }
387             out.printf(")");
388         }
389     }
390 #else
391     UNUSED_PARAM(map);
392 #endif
393 }
394
395 void CodeBlock::printPutByIdCacheStatus(PrintStream& out, ExecState* exec, int location, const StubInfoMap& map)
396 {
397     Instruction* instruction = instructions().begin() + location;
398
399     const Identifier& ident = identifier(instruction[2].u.operand);
400     
401     UNUSED_PARAM(ident); // tell the compiler to shut up in certain platform configurations.
402     
403     if (Structure* structure = instruction[4].u.structure.get()) {
404         switch (exec->interpreter()->getOpcodeID(instruction[0].u.opcode)) {
405         case op_put_by_id:
406         case op_put_by_id_out_of_line:
407             out.print(" llint(");
408             dumpStructure(out, "struct", structure, ident);
409             out.print(")");
410             break;
411             
412         case op_put_by_id_transition_direct:
413         case op_put_by_id_transition_normal:
414         case op_put_by_id_transition_direct_out_of_line:
415         case op_put_by_id_transition_normal_out_of_line:
416             out.print(" llint(");
417             dumpStructure(out, "prev", structure, ident);
418             out.print(", ");
419             dumpStructure(out, "next", instruction[6].u.structure.get(), ident);
420             if (StructureChain* chain = instruction[7].u.structureChain.get()) {
421                 out.print(", ");
422                 dumpChain(out, chain, ident);
423             }
424             out.print(")");
425             break;
426             
427         default:
428             out.print(" llint(unknown)");
429             break;
430         }
431     }
432
433 #if ENABLE(JIT)
434     if (StructureStubInfo* stubPtr = map.get(CodeOrigin(location))) {
435         StructureStubInfo& stubInfo = *stubPtr;
436         if (stubInfo.resetByGC)
437             out.print(" (Reset By GC)");
438         
439         if (stubInfo.seen) {
440             out.printf(" jit(");
441             
442             switch (stubInfo.accessType) {
443             case access_put_by_id_replace:
444                 out.print("replace, ");
445                 dumpStructure(out, "struct", stubInfo.u.putByIdReplace.baseObjectStructure.get(), ident);
446                 break;
447             case access_put_by_id_transition_normal:
448             case access_put_by_id_transition_direct:
449                 out.print("transition, ");
450                 dumpStructure(out, "prev", stubInfo.u.putByIdTransition.previousStructure.get(), ident);
451                 out.print(", ");
452                 dumpStructure(out, "next", stubInfo.u.putByIdTransition.structure.get(), ident);
453                 if (stubInfo.u.putByIdTransition.rawConditionSet)
454                     out.print(", ", ObjectPropertyConditionSet::fromRawPointer(stubInfo.u.putByIdTransition.rawConditionSet));
455                 break;
456             case access_put_by_id_list: {
457                 out.printf("list = [");
458                 PolymorphicPutByIdList* list = stubInfo.u.putByIdList.list;
459                 CommaPrinter comma;
460                 for (unsigned i = 0; i < list->size(); ++i) {
461                     out.print(comma, "(");
462                     const PutByIdAccess& access = list->at(i);
463                     
464                     if (access.isReplace()) {
465                         out.print("replace, ");
466                         dumpStructure(out, "struct", access.oldStructure(), ident);
467                     } else if (access.isSetter()) {
468                         out.print("setter, ");
469                         dumpStructure(out, "struct", access.oldStructure(), ident);
470                     } else if (access.isCustom()) {
471                         out.print("custom, ");
472                         dumpStructure(out, "struct", access.oldStructure(), ident);
473                     } else if (access.isTransition()) {
474                         out.print("transition, ");
475                         dumpStructure(out, "prev", access.oldStructure(), ident);
476                         out.print(", ");
477                         dumpStructure(out, "next", access.newStructure(), ident);
478                         if (!access.conditionSet().isEmpty())
479                             out.print(", ", access.conditionSet());
480                     } else
481                         out.print("unknown");
482                     
483                     out.print(")");
484                 }
485                 out.print("]");
486                 break;
487             }
488             case access_unset:
489                 out.printf("unset");
490                 break;
491             default:
492                 RELEASE_ASSERT_NOT_REACHED();
493                 break;
494             }
495             out.printf(")");
496         }
497     }
498 #else
499     UNUSED_PARAM(map);
500 #endif
501 }
502
503 void CodeBlock::printCallOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op, CacheDumpMode cacheDumpMode, bool& hasPrintedProfiling, const CallLinkInfoMap& map)
504 {
505     int dst = (++it)->u.operand;
506     int func = (++it)->u.operand;
507     int argCount = (++it)->u.operand;
508     int registerOffset = (++it)->u.operand;
509     printLocationAndOp(out, exec, location, it, op);
510     out.printf("%s, %s, %d, %d", registerName(dst).data(), registerName(func).data(), argCount, registerOffset);
511     if (cacheDumpMode == DumpCaches) {
512         LLIntCallLinkInfo* callLinkInfo = it[1].u.callLinkInfo;
513         if (callLinkInfo->lastSeenCallee) {
514             out.printf(
515                 " llint(%p, exec %p)",
516                 callLinkInfo->lastSeenCallee.get(),
517                 callLinkInfo->lastSeenCallee->executable());
518         }
519 #if ENABLE(JIT)
520         if (CallLinkInfo* info = map.get(CodeOrigin(location))) {
521             JSFunction* target = info->lastSeenCallee();
522             if (target)
523                 out.printf(" jit(%p, exec %p)", target, target->executable());
524         }
525         
526         if (jitType() != JITCode::FTLJIT)
527             out.print(" status(", CallLinkStatus::computeFor(this, location, map), ")");
528 #else
529         UNUSED_PARAM(map);
530 #endif
531     }
532     ++it;
533     ++it;
534     dumpArrayProfiling(out, it, hasPrintedProfiling);
535     dumpValueProfiling(out, it, hasPrintedProfiling);
536 }
537
538 void CodeBlock::printPutByIdOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op)
539 {
540     int r0 = (++it)->u.operand;
541     int id0 = (++it)->u.operand;
542     int r1 = (++it)->u.operand;
543     printLocationAndOp(out, exec, location, it, op);
544     out.printf("%s, %s, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), registerName(r1).data());
545     it += 5;
546 }
547
548 void CodeBlock::dumpSource()
549 {
550     dumpSource(WTF::dataFile());
551 }
552
553 void CodeBlock::dumpSource(PrintStream& out)
554 {
555     ScriptExecutable* executable = ownerExecutable();
556     if (executable->isFunctionExecutable()) {
557         FunctionExecutable* functionExecutable = reinterpret_cast<FunctionExecutable*>(executable);
558         String source = functionExecutable->source().provider()->getRange(
559             functionExecutable->parametersStartOffset(),
560             functionExecutable->typeProfilingEndOffset() + 1); // Type profiling end offset is the character before the '}'.
561         
562         out.print("function ", inferredName(), source);
563         return;
564     }
565     out.print(executable->source().toString());
566 }
567
568 void CodeBlock::dumpBytecode()
569 {
570     dumpBytecode(WTF::dataFile());
571 }
572
573 void CodeBlock::dumpBytecode(PrintStream& out)
574 {
575     // We only use the ExecState* for things that don't actually lead to JS execution,
576     // like converting a JSString to a String. Hence the globalExec is appropriate.
577     ExecState* exec = m_globalObject->globalExec();
578     
579     size_t instructionCount = 0;
580
581     for (size_t i = 0; i < instructions().size(); i += opcodeLengths[exec->interpreter()->getOpcodeID(instructions()[i].u.opcode)])
582         ++instructionCount;
583
584     out.print(*this);
585     out.printf(
586         ": %lu m_instructions; %lu bytes; %d parameter(s); %d callee register(s); %d variable(s)",
587         static_cast<unsigned long>(instructions().size()),
588         static_cast<unsigned long>(instructions().size() * sizeof(Instruction)),
589         m_numParameters, m_numCalleeRegisters, m_numVars);
590     if (needsActivation() && codeType() == FunctionCode)
591         out.printf("; lexical environment in r%d", activationRegister().offset());
592     out.printf("\n");
593     
594     StubInfoMap stubInfos;
595     CallLinkInfoMap callLinkInfos;
596     getStubInfoMap(stubInfos);
597     getCallLinkInfoMap(callLinkInfos);
598     
599     const Instruction* begin = instructions().begin();
600     const Instruction* end = instructions().end();
601     for (const Instruction* it = begin; it != end; ++it)
602         dumpBytecode(out, exec, begin, it, stubInfos, callLinkInfos);
603     
604     if (numberOfIdentifiers()) {
605         out.printf("\nIdentifiers:\n");
606         size_t i = 0;
607         do {
608             out.printf("  id%u = %s\n", static_cast<unsigned>(i), identifier(i).string().utf8().data());
609             ++i;
610         } while (i != numberOfIdentifiers());
611     }
612
613     if (!m_constantRegisters.isEmpty()) {
614         out.printf("\nConstants:\n");
615         size_t i = 0;
616         do {
617             const char* sourceCodeRepresentationDescription = nullptr;
618             switch (m_constantsSourceCodeRepresentation[i]) {
619             case SourceCodeRepresentation::Double:
620                 sourceCodeRepresentationDescription = ": in source as double";
621                 break;
622             case SourceCodeRepresentation::Integer:
623                 sourceCodeRepresentationDescription = ": in source as integer";
624                 break;
625             case SourceCodeRepresentation::Other:
626                 sourceCodeRepresentationDescription = "";
627                 break;
628             }
629             out.printf("   k%u = %s%s\n", static_cast<unsigned>(i), toCString(m_constantRegisters[i].get()).data(), sourceCodeRepresentationDescription);
630             ++i;
631         } while (i < m_constantRegisters.size());
632     }
633
634     if (size_t count = m_unlinkedCode->numberOfRegExps()) {
635         out.printf("\nm_regexps:\n");
636         size_t i = 0;
637         do {
638             out.printf("  re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(m_unlinkedCode->regexp(i)).data());
639             ++i;
640         } while (i < count);
641     }
642
643     if (m_rareData && !m_rareData->m_exceptionHandlers.isEmpty()) {
644         out.printf("\nException Handlers:\n");
645         unsigned i = 0;
646         do {
647             HandlerInfo& handler = m_rareData->m_exceptionHandlers[i];
648             out.printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] } %s\n",
649                 i + 1, handler.start, handler.end, handler.target, handler.typeName());
650             ++i;
651         } while (i < m_rareData->m_exceptionHandlers.size());
652     }
653     
654     if (m_rareData && !m_rareData->m_switchJumpTables.isEmpty()) {
655         out.printf("Switch Jump Tables:\n");
656         unsigned i = 0;
657         do {
658             out.printf("  %1d = {\n", i);
659             int entry = 0;
660             Vector<int32_t>::const_iterator end = m_rareData->m_switchJumpTables[i].branchOffsets.end();
661             for (Vector<int32_t>::const_iterator iter = m_rareData->m_switchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
662                 if (!*iter)
663                     continue;
664                 out.printf("\t\t%4d => %04d\n", entry + m_rareData->m_switchJumpTables[i].min, *iter);
665             }
666             out.printf("      }\n");
667             ++i;
668         } while (i < m_rareData->m_switchJumpTables.size());
669     }
670     
671     if (m_rareData && !m_rareData->m_stringSwitchJumpTables.isEmpty()) {
672         out.printf("\nString Switch Jump Tables:\n");
673         unsigned i = 0;
674         do {
675             out.printf("  %1d = {\n", i);
676             StringJumpTable::StringOffsetTable::const_iterator end = m_rareData->m_stringSwitchJumpTables[i].offsetTable.end();
677             for (StringJumpTable::StringOffsetTable::const_iterator iter = m_rareData->m_stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
678                 out.printf("\t\t\"%s\" => %04d\n", iter->key->utf8().data(), iter->value.branchOffset);
679             out.printf("      }\n");
680             ++i;
681         } while (i < m_rareData->m_stringSwitchJumpTables.size());
682     }
683
684     out.printf("\n");
685 }
686
687 void CodeBlock::beginDumpProfiling(PrintStream& out, bool& hasPrintedProfiling)
688 {
689     if (hasPrintedProfiling) {
690         out.print("; ");
691         return;
692     }
693     
694     out.print("    ");
695     hasPrintedProfiling = true;
696 }
697
698 void CodeBlock::dumpValueProfiling(PrintStream& out, const Instruction*& it, bool& hasPrintedProfiling)
699 {
700     ConcurrentJITLocker locker(m_lock);
701     
702     ++it;
703     CString description = it->u.profile->briefDescription(locker);
704     if (!description.length())
705         return;
706     beginDumpProfiling(out, hasPrintedProfiling);
707     out.print(description);
708 }
709
710 void CodeBlock::dumpArrayProfiling(PrintStream& out, const Instruction*& it, bool& hasPrintedProfiling)
711 {
712     ConcurrentJITLocker locker(m_lock);
713     
714     ++it;
715     if (!it->u.arrayProfile)
716         return;
717     CString description = it->u.arrayProfile->briefDescription(locker, this);
718     if (!description.length())
719         return;
720     beginDumpProfiling(out, hasPrintedProfiling);
721     out.print(description);
722 }
723
724 void CodeBlock::dumpRareCaseProfile(PrintStream& out, const char* name, RareCaseProfile* profile, bool& hasPrintedProfiling)
725 {
726     if (!profile || !profile->m_counter)
727         return;
728
729     beginDumpProfiling(out, hasPrintedProfiling);
730     out.print(name, profile->m_counter);
731 }
732
733 void CodeBlock::printLocationAndOp(PrintStream& out, ExecState*, int location, const Instruction*&, const char* op)
734 {
735     out.printf("[%4d] %-17s ", location, op);
736 }
737
738 void CodeBlock::printLocationOpAndRegisterOperand(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op, int operand)
739 {
740     printLocationAndOp(out, exec, location, it, op);
741     out.printf("%s", registerName(operand).data());
742 }
743
744 void CodeBlock::dumpBytecode(
745     PrintStream& out, ExecState* exec, const Instruction* begin, const Instruction*& it,
746     const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos)
747 {
748     int location = it - begin;
749     bool hasPrintedProfiling = false;
750     OpcodeID opcode = exec->interpreter()->getOpcodeID(it->u.opcode);
751     switch (opcode) {
752         case op_enter: {
753             printLocationAndOp(out, exec, location, it, "enter");
754             break;
755         }
756         case op_get_scope: {
757             int r0 = (++it)->u.operand;
758             printLocationOpAndRegisterOperand(out, exec, location, it, "get_scope", r0);
759             break;
760         }
761         case op_load_arrowfunction_this: {
762             int r0 = (++it)->u.operand;
763             printLocationOpAndRegisterOperand(out, exec, location, it, "load_arrowfunction_this", r0);
764             break;
765         }
766         case op_create_direct_arguments: {
767             int r0 = (++it)->u.operand;
768             printLocationAndOp(out, exec, location, it, "create_direct_arguments");
769             out.printf("%s", registerName(r0).data());
770             break;
771         }
772         case op_create_scoped_arguments: {
773             int r0 = (++it)->u.operand;
774             int r1 = (++it)->u.operand;
775             printLocationAndOp(out, exec, location, it, "create_scoped_arguments");
776             out.printf("%s, %s", registerName(r0).data(), registerName(r1).data());
777             break;
778         }
779         case op_create_out_of_band_arguments: {
780             int r0 = (++it)->u.operand;
781             printLocationAndOp(out, exec, location, it, "create_out_of_band_arguments");
782             out.printf("%s", registerName(r0).data());
783             break;
784         }
785         case op_create_this: {
786             int r0 = (++it)->u.operand;
787             int r1 = (++it)->u.operand;
788             unsigned inferredInlineCapacity = (++it)->u.operand;
789             unsigned cachedFunction = (++it)->u.operand;
790             printLocationAndOp(out, exec, location, it, "create_this");
791             out.printf("%s, %s, %u, %u", registerName(r0).data(), registerName(r1).data(), inferredInlineCapacity, cachedFunction);
792             break;
793         }
794         case op_to_this: {
795             int r0 = (++it)->u.operand;
796             printLocationOpAndRegisterOperand(out, exec, location, it, "to_this", r0);
797             Structure* structure = (++it)->u.structure.get();
798             if (structure)
799                 out.print(", cache(struct = ", RawPointer(structure), ")");
800             out.print(", ", (++it)->u.toThisStatus);
801             break;
802         }
803         case op_check_tdz: {
804             int r0 = (++it)->u.operand;
805             printLocationOpAndRegisterOperand(out, exec, location, it, "op_check_tdz", r0);
806             break;
807         }
808         case op_new_object: {
809             int r0 = (++it)->u.operand;
810             unsigned inferredInlineCapacity = (++it)->u.operand;
811             printLocationAndOp(out, exec, location, it, "new_object");
812             out.printf("%s, %u", registerName(r0).data(), inferredInlineCapacity);
813             ++it; // Skip object allocation profile.
814             break;
815         }
816         case op_new_array: {
817             int dst = (++it)->u.operand;
818             int argv = (++it)->u.operand;
819             int argc = (++it)->u.operand;
820             printLocationAndOp(out, exec, location, it, "new_array");
821             out.printf("%s, %s, %d", registerName(dst).data(), registerName(argv).data(), argc);
822             ++it; // Skip array allocation profile.
823             break;
824         }
825         case op_new_array_with_size: {
826             int dst = (++it)->u.operand;
827             int length = (++it)->u.operand;
828             printLocationAndOp(out, exec, location, it, "new_array_with_size");
829             out.printf("%s, %s", registerName(dst).data(), registerName(length).data());
830             ++it; // Skip array allocation profile.
831             break;
832         }
833         case op_new_array_buffer: {
834             int dst = (++it)->u.operand;
835             int argv = (++it)->u.operand;
836             int argc = (++it)->u.operand;
837             printLocationAndOp(out, exec, location, it, "new_array_buffer");
838             out.printf("%s, %d, %d", registerName(dst).data(), argv, argc);
839             ++it; // Skip array allocation profile.
840             break;
841         }
842         case op_new_regexp: {
843             int r0 = (++it)->u.operand;
844             int re0 = (++it)->u.operand;
845             printLocationAndOp(out, exec, location, it, "new_regexp");
846             out.printf("%s, ", registerName(r0).data());
847             if (r0 >=0 && r0 < (int)m_unlinkedCode->numberOfRegExps())
848                 out.printf("%s", regexpName(re0, regexp(re0)).data());
849             else
850                 out.printf("bad_regexp(%d)", re0);
851             break;
852         }
853         case op_mov: {
854             int r0 = (++it)->u.operand;
855             int r1 = (++it)->u.operand;
856             printLocationAndOp(out, exec, location, it, "mov");
857             out.printf("%s, %s", registerName(r0).data(), registerName(r1).data());
858             break;
859         }
860         case op_profile_type: {
861             int r0 = (++it)->u.operand;
862             ++it;
863             ++it;
864             ++it;
865             ++it;
866             printLocationAndOp(out, exec, location, it, "op_profile_type");
867             out.printf("%s", registerName(r0).data());
868             break;
869         }
870         case op_profile_control_flow: {
871             BasicBlockLocation* basicBlockLocation = (++it)->u.basicBlockLocation;
872             printLocationAndOp(out, exec, location, it, "profile_control_flow");
873             out.printf("[%d, %d]", basicBlockLocation->startOffset(), basicBlockLocation->endOffset());
874             break;
875         }
876         case op_not: {
877             printUnaryOp(out, exec, location, it, "not");
878             break;
879         }
880         case op_eq: {
881             printBinaryOp(out, exec, location, it, "eq");
882             break;
883         }
884         case op_eq_null: {
885             printUnaryOp(out, exec, location, it, "eq_null");
886             break;
887         }
888         case op_neq: {
889             printBinaryOp(out, exec, location, it, "neq");
890             break;
891         }
892         case op_neq_null: {
893             printUnaryOp(out, exec, location, it, "neq_null");
894             break;
895         }
896         case op_stricteq: {
897             printBinaryOp(out, exec, location, it, "stricteq");
898             break;
899         }
900         case op_nstricteq: {
901             printBinaryOp(out, exec, location, it, "nstricteq");
902             break;
903         }
904         case op_less: {
905             printBinaryOp(out, exec, location, it, "less");
906             break;
907         }
908         case op_lesseq: {
909             printBinaryOp(out, exec, location, it, "lesseq");
910             break;
911         }
912         case op_greater: {
913             printBinaryOp(out, exec, location, it, "greater");
914             break;
915         }
916         case op_greatereq: {
917             printBinaryOp(out, exec, location, it, "greatereq");
918             break;
919         }
920         case op_inc: {
921             int r0 = (++it)->u.operand;
922             printLocationOpAndRegisterOperand(out, exec, location, it, "inc", r0);
923             break;
924         }
925         case op_dec: {
926             int r0 = (++it)->u.operand;
927             printLocationOpAndRegisterOperand(out, exec, location, it, "dec", r0);
928             break;
929         }
930         case op_to_number: {
931             printUnaryOp(out, exec, location, it, "to_number");
932             break;
933         }
934         case op_to_string: {
935             printUnaryOp(out, exec, location, it, "to_string");
936             break;
937         }
938         case op_negate: {
939             printUnaryOp(out, exec, location, it, "negate");
940             break;
941         }
942         case op_add: {
943             printBinaryOp(out, exec, location, it, "add");
944             ++it;
945             break;
946         }
947         case op_mul: {
948             printBinaryOp(out, exec, location, it, "mul");
949             ++it;
950             break;
951         }
952         case op_div: {
953             printBinaryOp(out, exec, location, it, "div");
954             ++it;
955             break;
956         }
957         case op_mod: {
958             printBinaryOp(out, exec, location, it, "mod");
959             break;
960         }
961         case op_sub: {
962             printBinaryOp(out, exec, location, it, "sub");
963             ++it;
964             break;
965         }
966         case op_lshift: {
967             printBinaryOp(out, exec, location, it, "lshift");
968             break;            
969         }
970         case op_rshift: {
971             printBinaryOp(out, exec, location, it, "rshift");
972             break;
973         }
974         case op_urshift: {
975             printBinaryOp(out, exec, location, it, "urshift");
976             break;
977         }
978         case op_bitand: {
979             printBinaryOp(out, exec, location, it, "bitand");
980             ++it;
981             break;
982         }
983         case op_bitxor: {
984             printBinaryOp(out, exec, location, it, "bitxor");
985             ++it;
986             break;
987         }
988         case op_bitor: {
989             printBinaryOp(out, exec, location, it, "bitor");
990             ++it;
991             break;
992         }
993         case op_check_has_instance: {
994             int r0 = (++it)->u.operand;
995             int r1 = (++it)->u.operand;
996             int r2 = (++it)->u.operand;
997             int offset = (++it)->u.operand;
998             printLocationAndOp(out, exec, location, it, "check_has_instance");
999             out.printf("%s, %s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), registerName(r2).data(), offset, location + offset);
1000             break;
1001         }
1002         case op_instanceof: {
1003             int r0 = (++it)->u.operand;
1004             int r1 = (++it)->u.operand;
1005             int r2 = (++it)->u.operand;
1006             printLocationAndOp(out, exec, location, it, "instanceof");
1007             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
1008             break;
1009         }
1010         case op_unsigned: {
1011             printUnaryOp(out, exec, location, it, "unsigned");
1012             break;
1013         }
1014         case op_typeof: {
1015             printUnaryOp(out, exec, location, it, "typeof");
1016             break;
1017         }
1018         case op_is_undefined: {
1019             printUnaryOp(out, exec, location, it, "is_undefined");
1020             break;
1021         }
1022         case op_is_boolean: {
1023             printUnaryOp(out, exec, location, it, "is_boolean");
1024             break;
1025         }
1026         case op_is_number: {
1027             printUnaryOp(out, exec, location, it, "is_number");
1028             break;
1029         }
1030         case op_is_string: {
1031             printUnaryOp(out, exec, location, it, "is_string");
1032             break;
1033         }
1034         case op_is_object: {
1035             printUnaryOp(out, exec, location, it, "is_object");
1036             break;
1037         }
1038         case op_is_object_or_null: {
1039             printUnaryOp(out, exec, location, it, "is_object_or_null");
1040             break;
1041         }
1042         case op_is_function: {
1043             printUnaryOp(out, exec, location, it, "is_function");
1044             break;
1045         }
1046         case op_in: {
1047             printBinaryOp(out, exec, location, it, "in");
1048             break;
1049         }
1050         case op_get_by_id:
1051         case op_get_by_id_out_of_line:
1052         case op_get_array_length: {
1053             printGetByIdOp(out, exec, location, it);
1054             printGetByIdCacheStatus(out, exec, location, stubInfos);
1055             dumpValueProfiling(out, it, hasPrintedProfiling);
1056             break;
1057         }
1058         case op_put_by_id: {
1059             printPutByIdOp(out, exec, location, it, "put_by_id");
1060             printPutByIdCacheStatus(out, exec, location, stubInfos);
1061             break;
1062         }
1063         case op_put_by_id_out_of_line: {
1064             printPutByIdOp(out, exec, location, it, "put_by_id_out_of_line");
1065             printPutByIdCacheStatus(out, exec, location, stubInfos);
1066             break;
1067         }
1068         case op_put_by_id_transition_direct: {
1069             printPutByIdOp(out, exec, location, it, "put_by_id_transition_direct");
1070             printPutByIdCacheStatus(out, exec, location, stubInfos);
1071             break;
1072         }
1073         case op_put_by_id_transition_direct_out_of_line: {
1074             printPutByIdOp(out, exec, location, it, "put_by_id_transition_direct_out_of_line");
1075             printPutByIdCacheStatus(out, exec, location, stubInfos);
1076             break;
1077         }
1078         case op_put_by_id_transition_normal: {
1079             printPutByIdOp(out, exec, location, it, "put_by_id_transition_normal");
1080             printPutByIdCacheStatus(out, exec, location, stubInfos);
1081             break;
1082         }
1083         case op_put_by_id_transition_normal_out_of_line: {
1084             printPutByIdOp(out, exec, location, it, "put_by_id_transition_normal_out_of_line");
1085             printPutByIdCacheStatus(out, exec, location, stubInfos);
1086             break;
1087         }
1088         case op_put_getter_by_id: {
1089             int r0 = (++it)->u.operand;
1090             int id0 = (++it)->u.operand;
1091             int n0 = (++it)->u.operand;
1092             int r1 = (++it)->u.operand;
1093             printLocationAndOp(out, exec, location, it, "put_getter_by_id");
1094             out.printf("%s, %s, %d, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), n0, registerName(r1).data());
1095             break;
1096         }
1097         case op_put_setter_by_id: {
1098             int r0 = (++it)->u.operand;
1099             int id0 = (++it)->u.operand;
1100             int n0 = (++it)->u.operand;
1101             int r1 = (++it)->u.operand;
1102             printLocationAndOp(out, exec, location, it, "put_setter_by_id");
1103             out.printf("%s, %s, %d, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), n0, registerName(r1).data());
1104             break;
1105         }
1106         case op_put_getter_setter: {
1107             int r0 = (++it)->u.operand;
1108             int id0 = (++it)->u.operand;
1109             int n0 = (++it)->u.operand;
1110             int r1 = (++it)->u.operand;
1111             int r2 = (++it)->u.operand;
1112             printLocationAndOp(out, exec, location, it, "put_getter_setter");
1113             out.printf("%s, %s, %d, %s, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), n0, registerName(r1).data(), registerName(r2).data());
1114             break;
1115         }
1116         case op_del_by_id: {
1117             int r0 = (++it)->u.operand;
1118             int r1 = (++it)->u.operand;
1119             int id0 = (++it)->u.operand;
1120             printLocationAndOp(out, exec, location, it, "del_by_id");
1121             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), idName(id0, identifier(id0)).data());
1122             break;
1123         }
1124         case op_get_by_val: {
1125             int r0 = (++it)->u.operand;
1126             int r1 = (++it)->u.operand;
1127             int r2 = (++it)->u.operand;
1128             printLocationAndOp(out, exec, location, it, "get_by_val");
1129             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
1130             dumpArrayProfiling(out, it, hasPrintedProfiling);
1131             dumpValueProfiling(out, it, hasPrintedProfiling);
1132             break;
1133         }
1134         case op_put_by_val: {
1135             int r0 = (++it)->u.operand;
1136             int r1 = (++it)->u.operand;
1137             int r2 = (++it)->u.operand;
1138             printLocationAndOp(out, exec, location, it, "put_by_val");
1139             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
1140             dumpArrayProfiling(out, it, hasPrintedProfiling);
1141             break;
1142         }
1143         case op_put_by_val_direct: {
1144             int r0 = (++it)->u.operand;
1145             int r1 = (++it)->u.operand;
1146             int r2 = (++it)->u.operand;
1147             printLocationAndOp(out, exec, location, it, "put_by_val_direct");
1148             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
1149             dumpArrayProfiling(out, it, hasPrintedProfiling);
1150             break;
1151         }
1152         case op_del_by_val: {
1153             int r0 = (++it)->u.operand;
1154             int r1 = (++it)->u.operand;
1155             int r2 = (++it)->u.operand;
1156             printLocationAndOp(out, exec, location, it, "del_by_val");
1157             out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data());
1158             break;
1159         }
1160         case op_put_by_index: {
1161             int r0 = (++it)->u.operand;
1162             unsigned n0 = (++it)->u.operand;
1163             int r1 = (++it)->u.operand;
1164             printLocationAndOp(out, exec, location, it, "put_by_index");
1165             out.printf("%s, %u, %s", registerName(r0).data(), n0, registerName(r1).data());
1166             break;
1167         }
1168         case op_jmp: {
1169             int offset = (++it)->u.operand;
1170             printLocationAndOp(out, exec, location, it, "jmp");
1171             out.printf("%d(->%d)", offset, location + offset);
1172             break;
1173         }
1174         case op_jtrue: {
1175             printConditionalJump(out, exec, begin, it, location, "jtrue");
1176             break;
1177         }
1178         case op_jfalse: {
1179             printConditionalJump(out, exec, begin, it, location, "jfalse");
1180             break;
1181         }
1182         case op_jeq_null: {
1183             printConditionalJump(out, exec, begin, it, location, "jeq_null");
1184             break;
1185         }
1186         case op_jneq_null: {
1187             printConditionalJump(out, exec, begin, it, location, "jneq_null");
1188             break;
1189         }
1190         case op_jneq_ptr: {
1191             int r0 = (++it)->u.operand;
1192             Special::Pointer pointer = (++it)->u.specialPointer;
1193             int offset = (++it)->u.operand;
1194             printLocationAndOp(out, exec, location, it, "jneq_ptr");
1195             out.printf("%s, %d (%p), %d(->%d)", registerName(r0).data(), pointer, m_globalObject->actualPointerFor(pointer), offset, location + offset);
1196             break;
1197         }
1198         case op_jless: {
1199             int r0 = (++it)->u.operand;
1200             int r1 = (++it)->u.operand;
1201             int offset = (++it)->u.operand;
1202             printLocationAndOp(out, exec, location, it, "jless");
1203             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1204             break;
1205         }
1206         case op_jlesseq: {
1207             int r0 = (++it)->u.operand;
1208             int r1 = (++it)->u.operand;
1209             int offset = (++it)->u.operand;
1210             printLocationAndOp(out, exec, location, it, "jlesseq");
1211             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1212             break;
1213         }
1214         case op_jgreater: {
1215             int r0 = (++it)->u.operand;
1216             int r1 = (++it)->u.operand;
1217             int offset = (++it)->u.operand;
1218             printLocationAndOp(out, exec, location, it, "jgreater");
1219             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1220             break;
1221         }
1222         case op_jgreatereq: {
1223             int r0 = (++it)->u.operand;
1224             int r1 = (++it)->u.operand;
1225             int offset = (++it)->u.operand;
1226             printLocationAndOp(out, exec, location, it, "jgreatereq");
1227             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1228             break;
1229         }
1230         case op_jnless: {
1231             int r0 = (++it)->u.operand;
1232             int r1 = (++it)->u.operand;
1233             int offset = (++it)->u.operand;
1234             printLocationAndOp(out, exec, location, it, "jnless");
1235             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1236             break;
1237         }
1238         case op_jnlesseq: {
1239             int r0 = (++it)->u.operand;
1240             int r1 = (++it)->u.operand;
1241             int offset = (++it)->u.operand;
1242             printLocationAndOp(out, exec, location, it, "jnlesseq");
1243             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1244             break;
1245         }
1246         case op_jngreater: {
1247             int r0 = (++it)->u.operand;
1248             int r1 = (++it)->u.operand;
1249             int offset = (++it)->u.operand;
1250             printLocationAndOp(out, exec, location, it, "jngreater");
1251             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1252             break;
1253         }
1254         case op_jngreatereq: {
1255             int r0 = (++it)->u.operand;
1256             int r1 = (++it)->u.operand;
1257             int offset = (++it)->u.operand;
1258             printLocationAndOp(out, exec, location, it, "jngreatereq");
1259             out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset);
1260             break;
1261         }
1262         case op_loop_hint: {
1263             printLocationAndOp(out, exec, location, it, "loop_hint");
1264             break;
1265         }
1266         case op_switch_imm: {
1267             int tableIndex = (++it)->u.operand;
1268             int defaultTarget = (++it)->u.operand;
1269             int scrutineeRegister = (++it)->u.operand;
1270             printLocationAndOp(out, exec, location, it, "switch_imm");
1271             out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data());
1272             break;
1273         }
1274         case op_switch_char: {
1275             int tableIndex = (++it)->u.operand;
1276             int defaultTarget = (++it)->u.operand;
1277             int scrutineeRegister = (++it)->u.operand;
1278             printLocationAndOp(out, exec, location, it, "switch_char");
1279             out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data());
1280             break;
1281         }
1282         case op_switch_string: {
1283             int tableIndex = (++it)->u.operand;
1284             int defaultTarget = (++it)->u.operand;
1285             int scrutineeRegister = (++it)->u.operand;
1286             printLocationAndOp(out, exec, location, it, "switch_string");
1287             out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data());
1288             break;
1289         }
1290         case op_new_func: {
1291             int r0 = (++it)->u.operand;
1292             int r1 = (++it)->u.operand;
1293             int f0 = (++it)->u.operand;
1294             printLocationAndOp(out, exec, location, it, "new_func");
1295             out.printf("%s, %s, f%d", registerName(r0).data(), registerName(r1).data(), f0);
1296             break;
1297         }
1298         case op_new_arrow_func_exp: {
1299             int r0 = (++it)->u.operand;
1300             int r1 = (++it)->u.operand;
1301             int f0 = (++it)->u.operand;
1302             int r2 = (++it)->u.operand;
1303             printLocationAndOp(out, exec, location, it, "op_new_arrow_func_exp");
1304             out.printf("%s, %s, f%d, %s", registerName(r0).data(), registerName(r1).data(), f0, registerName(r2).data());
1305             break;
1306         }
1307         case op_new_func_exp: {
1308             int r0 = (++it)->u.operand;
1309             int r1 = (++it)->u.operand;
1310             int f0 = (++it)->u.operand;
1311             printLocationAndOp(out, exec, location, it, "new_func_exp");
1312             out.printf("%s, %s, f%d", registerName(r0).data(), registerName(r1).data(), f0);
1313             break;
1314         }
1315         case op_call: {
1316             printCallOp(out, exec, location, it, "call", DumpCaches, hasPrintedProfiling, callLinkInfos);
1317             break;
1318         }
1319         case op_call_eval: {
1320             printCallOp(out, exec, location, it, "call_eval", DontDumpCaches, hasPrintedProfiling, callLinkInfos);
1321             break;
1322         }
1323             
1324         case op_construct_varargs:
1325         case op_call_varargs: {
1326             int result = (++it)->u.operand;
1327             int callee = (++it)->u.operand;
1328             int thisValue = (++it)->u.operand;
1329             int arguments = (++it)->u.operand;
1330             int firstFreeRegister = (++it)->u.operand;
1331             int varArgOffset = (++it)->u.operand;
1332             ++it;
1333             printLocationAndOp(out, exec, location, it, opcode == op_call_varargs ? "call_varargs" : "construct_varargs");
1334             out.printf("%s, %s, %s, %s, %d, %d", registerName(result).data(), registerName(callee).data(), registerName(thisValue).data(), registerName(arguments).data(), firstFreeRegister, varArgOffset);
1335             dumpValueProfiling(out, it, hasPrintedProfiling);
1336             break;
1337         }
1338
1339         case op_ret: {
1340             int r0 = (++it)->u.operand;
1341             printLocationOpAndRegisterOperand(out, exec, location, it, "ret", r0);
1342             break;
1343         }
1344         case op_construct: {
1345             printCallOp(out, exec, location, it, "construct", DumpCaches, hasPrintedProfiling, callLinkInfos);
1346             break;
1347         }
1348         case op_strcat: {
1349             int r0 = (++it)->u.operand;
1350             int r1 = (++it)->u.operand;
1351             int count = (++it)->u.operand;
1352             printLocationAndOp(out, exec, location, it, "strcat");
1353             out.printf("%s, %s, %d", registerName(r0).data(), registerName(r1).data(), count);
1354             break;
1355         }
1356         case op_to_primitive: {
1357             int r0 = (++it)->u.operand;
1358             int r1 = (++it)->u.operand;
1359             printLocationAndOp(out, exec, location, it, "to_primitive");
1360             out.printf("%s, %s", registerName(r0).data(), registerName(r1).data());
1361             break;
1362         }
1363         case op_get_enumerable_length: {
1364             int dst = it[1].u.operand;
1365             int base = it[2].u.operand;
1366             printLocationAndOp(out, exec, location, it, "op_get_enumerable_length");
1367             out.printf("%s, %s", registerName(dst).data(), registerName(base).data());
1368             it += OPCODE_LENGTH(op_get_enumerable_length) - 1;
1369             break;
1370         }
1371         case op_has_indexed_property: {
1372             int dst = it[1].u.operand;
1373             int base = it[2].u.operand;
1374             int propertyName = it[3].u.operand;
1375             ArrayProfile* arrayProfile = it[4].u.arrayProfile;
1376             printLocationAndOp(out, exec, location, it, "op_has_indexed_property");
1377             out.printf("%s, %s, %s, %p", registerName(dst).data(), registerName(base).data(), registerName(propertyName).data(), arrayProfile);
1378             it += OPCODE_LENGTH(op_has_indexed_property) - 1;
1379             break;
1380         }
1381         case op_has_structure_property: {
1382             int dst = it[1].u.operand;
1383             int base = it[2].u.operand;
1384             int propertyName = it[3].u.operand;
1385             int enumerator = it[4].u.operand;
1386             printLocationAndOp(out, exec, location, it, "op_has_structure_property");
1387             out.printf("%s, %s, %s, %s", registerName(dst).data(), registerName(base).data(), registerName(propertyName).data(), registerName(enumerator).data());
1388             it += OPCODE_LENGTH(op_has_structure_property) - 1;
1389             break;
1390         }
1391         case op_has_generic_property: {
1392             int dst = it[1].u.operand;
1393             int base = it[2].u.operand;
1394             int propertyName = it[3].u.operand;
1395             printLocationAndOp(out, exec, location, it, "op_has_generic_property");
1396             out.printf("%s, %s, %s", registerName(dst).data(), registerName(base).data(), registerName(propertyName).data());
1397             it += OPCODE_LENGTH(op_has_generic_property) - 1;
1398             break;
1399         }
1400         case op_get_direct_pname: {
1401             int dst = it[1].u.operand;
1402             int base = it[2].u.operand;
1403             int propertyName = it[3].u.operand;
1404             int index = it[4].u.operand;
1405             int enumerator = it[5].u.operand;
1406             ValueProfile* profile = it[6].u.profile;
1407             printLocationAndOp(out, exec, location, it, "op_get_direct_pname");
1408             out.printf("%s, %s, %s, %s, %s, %p", registerName(dst).data(), registerName(base).data(), registerName(propertyName).data(), registerName(index).data(), registerName(enumerator).data(), profile);
1409             it += OPCODE_LENGTH(op_get_direct_pname) - 1;
1410             break;
1411
1412         }
1413         case op_get_property_enumerator: {
1414             int dst = it[1].u.operand;
1415             int base = it[2].u.operand;
1416             printLocationAndOp(out, exec, location, it, "op_get_property_enumerator");
1417             out.printf("%s, %s", registerName(dst).data(), registerName(base).data());
1418             it += OPCODE_LENGTH(op_get_property_enumerator) - 1;
1419             break;
1420         }
1421         case op_enumerator_structure_pname: {
1422             int dst = it[1].u.operand;
1423             int enumerator = it[2].u.operand;
1424             int index = it[3].u.operand;
1425             printLocationAndOp(out, exec, location, it, "op_enumerator_structure_pname");
1426             out.printf("%s, %s, %s", registerName(dst).data(), registerName(enumerator).data(), registerName(index).data());
1427             it += OPCODE_LENGTH(op_enumerator_structure_pname) - 1;
1428             break;
1429         }
1430         case op_enumerator_generic_pname: {
1431             int dst = it[1].u.operand;
1432             int enumerator = it[2].u.operand;
1433             int index = it[3].u.operand;
1434             printLocationAndOp(out, exec, location, it, "op_enumerator_generic_pname");
1435             out.printf("%s, %s, %s", registerName(dst).data(), registerName(enumerator).data(), registerName(index).data());
1436             it += OPCODE_LENGTH(op_enumerator_generic_pname) - 1;
1437             break;
1438         }
1439         case op_to_index_string: {
1440             int dst = it[1].u.operand;
1441             int index = it[2].u.operand;
1442             printLocationAndOp(out, exec, location, it, "op_to_index_string");
1443             out.printf("%s, %s", registerName(dst).data(), registerName(index).data());
1444             it += OPCODE_LENGTH(op_to_index_string) - 1;
1445             break;
1446         }
1447         case op_push_with_scope: {
1448             int dst = (++it)->u.operand;
1449             int newScope = (++it)->u.operand;
1450             int currentScope = (++it)->u.operand;
1451             printLocationAndOp(out, exec, location, it, "push_with_scope");
1452             out.printf("%s, %s, %s", registerName(dst).data(), registerName(newScope).data(), registerName(currentScope).data());
1453             break;
1454         }
1455         case op_get_parent_scope: {
1456             int dst = (++it)->u.operand;
1457             int parentScope = (++it)->u.operand;
1458             printLocationAndOp(out, exec, location, it, "get_parent_scope");
1459             out.printf("%s, %s", registerName(dst).data(), registerName(parentScope).data());
1460             break;
1461         }
1462         case op_create_lexical_environment: {
1463             int dst = (++it)->u.operand;
1464             int scope = (++it)->u.operand;
1465             int symbolTable = (++it)->u.operand;
1466             int initialValue = (++it)->u.operand;
1467             printLocationAndOp(out, exec, location, it, "create_lexical_environment");
1468             out.printf("%s, %s, %s, %s", 
1469                 registerName(dst).data(), registerName(scope).data(), registerName(symbolTable).data(), registerName(initialValue).data());
1470             break;
1471         }
1472         case op_catch: {
1473             int r0 = (++it)->u.operand;
1474             int r1 = (++it)->u.operand;
1475             printLocationAndOp(out, exec, location, it, "catch");
1476             out.printf("%s, %s", registerName(r0).data(), registerName(r1).data());
1477             break;
1478         }
1479         case op_throw: {
1480             int r0 = (++it)->u.operand;
1481             printLocationOpAndRegisterOperand(out, exec, location, it, "throw", r0);
1482             break;
1483         }
1484         case op_throw_static_error: {
1485             int k0 = (++it)->u.operand;
1486             int k1 = (++it)->u.operand;
1487             printLocationAndOp(out, exec, location, it, "throw_static_error");
1488             out.printf("%s, %s", constantName(k0).data(), k1 ? "true" : "false");
1489             break;
1490         }
1491         case op_debug: {
1492             int debugHookID = (++it)->u.operand;
1493             int hasBreakpointFlag = (++it)->u.operand;
1494             printLocationAndOp(out, exec, location, it, "debug");
1495             out.printf("%s %d", debugHookName(debugHookID), hasBreakpointFlag);
1496             break;
1497         }
1498         case op_profile_will_call: {
1499             int function = (++it)->u.operand;
1500             printLocationOpAndRegisterOperand(out, exec, location, it, "profile_will_call", function);
1501             break;
1502         }
1503         case op_profile_did_call: {
1504             int function = (++it)->u.operand;
1505             printLocationOpAndRegisterOperand(out, exec, location, it, "profile_did_call", function);
1506             break;
1507         }
1508         case op_end: {
1509             int r0 = (++it)->u.operand;
1510             printLocationOpAndRegisterOperand(out, exec, location, it, "end", r0);
1511             break;
1512         }
1513         case op_resolve_scope: {
1514             int r0 = (++it)->u.operand;
1515             int scope = (++it)->u.operand;
1516             int id0 = (++it)->u.operand;
1517             ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand);
1518             int depth = (++it)->u.operand;
1519             printLocationAndOp(out, exec, location, it, "resolve_scope");
1520             out.printf("%s, %s, %s, %u<%s|%s>, %d", registerName(r0).data(), registerName(scope).data(), idName(id0, identifier(id0)).data(),
1521                 modeAndType.operand(), resolveModeName(modeAndType.mode()), resolveTypeName(modeAndType.type()),
1522                 depth);
1523             ++it;
1524             break;
1525         }
1526         case op_get_from_scope: {
1527             int r0 = (++it)->u.operand;
1528             int r1 = (++it)->u.operand;
1529             int id0 = (++it)->u.operand;
1530             ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand);
1531             ++it; // Structure
1532             int operand = (++it)->u.operand; // Operand
1533             printLocationAndOp(out, exec, location, it, "get_from_scope");
1534             out.print(registerName(r0), ", ", registerName(r1));
1535             if (static_cast<unsigned>(id0) == UINT_MAX)
1536                 out.print(", anonymous");
1537             else
1538                 out.print(", ", idName(id0, identifier(id0)));
1539             out.print(", ", modeAndType.operand(), "<", resolveModeName(modeAndType.mode()), "|", resolveTypeName(modeAndType.type()), ">, ", operand);
1540             dumpValueProfiling(out, it, hasPrintedProfiling);
1541             break;
1542         }
1543         case op_put_to_scope: {
1544             int r0 = (++it)->u.operand;
1545             int id0 = (++it)->u.operand;
1546             int r1 = (++it)->u.operand;
1547             ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand);
1548             ++it; // Structure
1549             int operand = (++it)->u.operand; // Operand
1550             printLocationAndOp(out, exec, location, it, "put_to_scope");
1551             out.print(registerName(r0));
1552             if (static_cast<unsigned>(id0) == UINT_MAX)
1553                 out.print(", anonymous");
1554             else
1555                 out.print(", ", idName(id0, identifier(id0)));
1556             out.print(", ", registerName(r1), ", ", modeAndType.operand(), "<", resolveModeName(modeAndType.mode()), "|", resolveTypeName(modeAndType.type()), ">, <structure>, ", operand);
1557             break;
1558         }
1559         case op_get_from_arguments: {
1560             int r0 = (++it)->u.operand;
1561             int r1 = (++it)->u.operand;
1562             int offset = (++it)->u.operand;
1563             printLocationAndOp(out, exec, location, it, "get_from_arguments");
1564             out.printf("%s, %s, %d", registerName(r0).data(), registerName(r1).data(), offset);
1565             dumpValueProfiling(out, it, hasPrintedProfiling);
1566             break;
1567         }
1568         case op_put_to_arguments: {
1569             int r0 = (++it)->u.operand;
1570             int offset = (++it)->u.operand;
1571             int r1 = (++it)->u.operand;
1572             printLocationAndOp(out, exec, location, it, "put_to_arguments");
1573             out.printf("%s, %d, %s", registerName(r0).data(), offset, registerName(r1).data());
1574             break;
1575         }
1576         default:
1577             RELEASE_ASSERT_NOT_REACHED();
1578     }
1579
1580     dumpRareCaseProfile(out, "rare case: ", rareCaseProfileForBytecodeOffset(location), hasPrintedProfiling);
1581     dumpRareCaseProfile(out, "special fast case: ", specialFastCaseProfileForBytecodeOffset(location), hasPrintedProfiling);
1582     
1583 #if ENABLE(DFG_JIT)
1584     Vector<DFG::FrequentExitSite> exitSites = exitProfile().exitSitesFor(location);
1585     if (!exitSites.isEmpty()) {
1586         out.print(" !! frequent exits: ");
1587         CommaPrinter comma;
1588         for (unsigned i = 0; i < exitSites.size(); ++i)
1589             out.print(comma, exitSites[i].kind(), " ", exitSites[i].jitType());
1590     }
1591 #else // ENABLE(DFG_JIT)
1592     UNUSED_PARAM(location);
1593 #endif // ENABLE(DFG_JIT)
1594     out.print("\n");
1595 }
1596
1597 void CodeBlock::dumpBytecode(
1598     PrintStream& out, unsigned bytecodeOffset,
1599     const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos)
1600 {
1601     ExecState* exec = m_globalObject->globalExec();
1602     const Instruction* it = instructions().begin() + bytecodeOffset;
1603     dumpBytecode(out, exec, instructions().begin(), it, stubInfos, callLinkInfos);
1604 }
1605
1606 #define FOR_EACH_MEMBER_VECTOR(macro) \
1607     macro(instructions) \
1608     macro(callLinkInfos) \
1609     macro(linkedCallerList) \
1610     macro(identifiers) \
1611     macro(functionExpressions) \
1612     macro(constantRegisters)
1613
1614 #define FOR_EACH_MEMBER_VECTOR_RARE_DATA(macro) \
1615     macro(regexps) \
1616     macro(functions) \
1617     macro(exceptionHandlers) \
1618     macro(switchJumpTables) \
1619     macro(stringSwitchJumpTables) \
1620     macro(evalCodeCache) \
1621     macro(expressionInfo) \
1622     macro(lineInfo) \
1623     macro(callReturnIndexVector)
1624
1625 template<typename T>
1626 static size_t sizeInBytes(const Vector<T>& vector)
1627 {
1628     return vector.capacity() * sizeof(T);
1629 }
1630
1631 namespace {
1632
1633 class PutToScopeFireDetail : public FireDetail {
1634 public:
1635     PutToScopeFireDetail(CodeBlock* codeBlock, const Identifier& ident)
1636         : m_codeBlock(codeBlock)
1637         , m_ident(ident)
1638     {
1639     }
1640     
1641     virtual void dump(PrintStream& out) const override
1642     {
1643         out.print("Linking put_to_scope in ", FunctionExecutableDump(jsCast<FunctionExecutable*>(m_codeBlock->ownerExecutable())), " for ", m_ident);
1644     }
1645     
1646 private:
1647     CodeBlock* m_codeBlock;
1648     const Identifier& m_ident;
1649 };
1650
1651 } // anonymous namespace
1652
1653 CodeBlock::CodeBlock(CopyParsedBlockTag, CodeBlock& other)
1654     : m_globalObject(other.m_globalObject)
1655     , m_heap(other.m_heap)
1656     , m_numCalleeRegisters(other.m_numCalleeRegisters)
1657     , m_numVars(other.m_numVars)
1658     , m_isConstructor(other.m_isConstructor)
1659     , m_shouldAlwaysBeInlined(true)
1660     , m_didFailFTLCompilation(false)
1661     , m_hasBeenCompiledWithFTL(false)
1662     , m_unlinkedCode(*other.m_vm, other.m_ownerExecutable.get(), other.m_unlinkedCode.get())
1663     , m_hasDebuggerStatement(false)
1664     , m_steppingMode(SteppingModeDisabled)
1665     , m_numBreakpoints(0)
1666     , m_ownerExecutable(*other.m_vm, other.m_ownerExecutable.get(), other.m_ownerExecutable.get())
1667     , m_vm(other.m_vm)
1668     , m_instructions(other.m_instructions)
1669     , m_thisRegister(other.m_thisRegister)
1670     , m_scopeRegister(other.m_scopeRegister)
1671     , m_lexicalEnvironmentRegister(other.m_lexicalEnvironmentRegister)
1672     , m_isStrictMode(other.m_isStrictMode)
1673     , m_needsActivation(other.m_needsActivation)
1674     , m_mayBeExecuting(false)
1675     , m_source(other.m_source)
1676     , m_sourceOffset(other.m_sourceOffset)
1677     , m_firstLineColumnOffset(other.m_firstLineColumnOffset)
1678     , m_codeType(other.m_codeType)
1679     , m_constantRegisters(other.m_constantRegisters)
1680     , m_constantsSourceCodeRepresentation(other.m_constantsSourceCodeRepresentation)
1681     , m_functionDecls(other.m_functionDecls)
1682     , m_functionExprs(other.m_functionExprs)
1683     , m_osrExitCounter(0)
1684     , m_optimizationDelayCounter(0)
1685     , m_reoptimizationRetryCounter(0)
1686     , m_hash(other.m_hash)
1687 #if ENABLE(JIT)
1688     , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
1689 #endif
1690 {
1691     m_visitAggregateHasBeenCalled.store(false, std::memory_order_relaxed);
1692
1693     ASSERT(m_heap->isDeferred());
1694     ASSERT(m_scopeRegister.isLocal());
1695
1696     setNumParameters(other.numParameters());
1697     optimizeAfterWarmUp();
1698     jitAfterWarmUp();
1699
1700     if (other.m_rareData) {
1701         createRareDataIfNecessary();
1702         
1703         m_rareData->m_exceptionHandlers = other.m_rareData->m_exceptionHandlers;
1704         m_rareData->m_constantBuffers = other.m_rareData->m_constantBuffers;
1705         m_rareData->m_switchJumpTables = other.m_rareData->m_switchJumpTables;
1706         m_rareData->m_stringSwitchJumpTables = other.m_rareData->m_stringSwitchJumpTables;
1707     }
1708     
1709     m_heap->m_codeBlocks.add(this);
1710     m_heap->reportExtraMemoryAllocated(sizeof(CodeBlock));
1711 }
1712
1713 CodeBlock::CodeBlock(ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock, JSScope* scope, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset)
1714     : m_globalObject(scope->globalObject()->vm(), ownerExecutable, scope->globalObject())
1715     , m_heap(&m_globalObject->vm().heap)
1716     , m_numCalleeRegisters(unlinkedCodeBlock->m_numCalleeRegisters)
1717     , m_numVars(unlinkedCodeBlock->m_numVars)
1718     , m_isConstructor(unlinkedCodeBlock->isConstructor())
1719     , m_shouldAlwaysBeInlined(true)
1720     , m_didFailFTLCompilation(false)
1721     , m_hasBeenCompiledWithFTL(false)
1722     , m_unlinkedCode(m_globalObject->vm(), ownerExecutable, unlinkedCodeBlock)
1723     , m_hasDebuggerStatement(false)
1724     , m_steppingMode(SteppingModeDisabled)
1725     , m_numBreakpoints(0)
1726     , m_ownerExecutable(m_globalObject->vm(), ownerExecutable, ownerExecutable)
1727     , m_vm(unlinkedCodeBlock->vm())
1728     , m_thisRegister(unlinkedCodeBlock->thisRegister())
1729     , m_scopeRegister(unlinkedCodeBlock->scopeRegister())
1730     , m_lexicalEnvironmentRegister(unlinkedCodeBlock->activationRegister())
1731     , m_isStrictMode(unlinkedCodeBlock->isStrictMode())
1732     , m_needsActivation(unlinkedCodeBlock->hasActivationRegister() && unlinkedCodeBlock->codeType() == FunctionCode)
1733     , m_mayBeExecuting(false)
1734     , m_source(sourceProvider)
1735     , m_sourceOffset(sourceOffset)
1736     , m_firstLineColumnOffset(firstLineColumnOffset)
1737     , m_codeType(unlinkedCodeBlock->codeType())
1738     , m_osrExitCounter(0)
1739     , m_optimizationDelayCounter(0)
1740     , m_reoptimizationRetryCounter(0)
1741 #if ENABLE(JIT)
1742     , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
1743 #endif
1744 {
1745     m_visitAggregateHasBeenCalled.store(false, std::memory_order_relaxed);
1746
1747     ASSERT(m_heap->isDeferred());
1748     ASSERT(m_scopeRegister.isLocal());
1749
1750     ASSERT(m_source);
1751     setNumParameters(unlinkedCodeBlock->numParameters());
1752
1753     if (vm()->typeProfiler() || vm()->controlFlowProfiler())
1754         vm()->functionHasExecutedCache()->removeUnexecutedRange(m_ownerExecutable->sourceID(), m_ownerExecutable->typeProfilingStartOffset(), m_ownerExecutable->typeProfilingEndOffset());
1755
1756     setConstantRegisters(unlinkedCodeBlock->constantRegisters(), unlinkedCodeBlock->constantsSourceCodeRepresentation());
1757     if (unlinkedCodeBlock->usesGlobalObject())
1758         m_constantRegisters[unlinkedCodeBlock->globalObjectRegister().toConstantIndex()].set(*m_vm, ownerExecutable, m_globalObject.get());
1759
1760     for (unsigned i = 0; i < LinkTimeConstantCount; i++) {
1761         LinkTimeConstant type = static_cast<LinkTimeConstant>(i);
1762         if (unsigned registerIndex = unlinkedCodeBlock->registerIndexForLinkTimeConstant(type))
1763             m_constantRegisters[registerIndex].set(*m_vm, ownerExecutable, m_globalObject->jsCellForLinkTimeConstant(type));
1764     }
1765
1766     HashSet<int, WTF::IntHash<int>, WTF::UnsignedWithZeroKeyHashTraits<int>> clonedConstantSymbolTables;
1767     {
1768         HashSet<SymbolTable*> clonedSymbolTables;
1769         for (unsigned i = 0; i < m_constantRegisters.size(); i++) {
1770             if (m_constantRegisters[i].get().isEmpty())
1771                 continue;
1772             if (SymbolTable* symbolTable = jsDynamicCast<SymbolTable*>(m_constantRegisters[i].get())) {
1773                 RELEASE_ASSERT(clonedSymbolTables.add(symbolTable).isNewEntry);
1774                 if (m_vm->typeProfiler()) {
1775                     ConcurrentJITLocker locker(symbolTable->m_lock);
1776                     symbolTable->prepareForTypeProfiling(locker);
1777                 }
1778                 m_constantRegisters[i].set(*m_vm, ownerExecutable, symbolTable->cloneScopePart(*m_vm));
1779                 clonedConstantSymbolTables.add(i + FirstConstantRegisterIndex);
1780             }
1781         }
1782     }
1783
1784     m_functionDecls.resizeToFit(unlinkedCodeBlock->numberOfFunctionDecls());
1785     for (size_t count = unlinkedCodeBlock->numberOfFunctionDecls(), i = 0; i < count; ++i) {
1786         UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionDecl(i);
1787         if (vm()->typeProfiler() || vm()->controlFlowProfiler())
1788             vm()->functionHasExecutedCache()->insertUnexecutedRange(m_ownerExecutable->sourceID(), unlinkedExecutable->typeProfilingStartOffset(), unlinkedExecutable->typeProfilingEndOffset());
1789         m_functionDecls[i].set(*m_vm, ownerExecutable, unlinkedExecutable->link(*m_vm, ownerExecutable->source()));
1790     }
1791
1792     m_functionExprs.resizeToFit(unlinkedCodeBlock->numberOfFunctionExprs());
1793     for (size_t count = unlinkedCodeBlock->numberOfFunctionExprs(), i = 0; i < count; ++i) {
1794         UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionExpr(i);
1795         if (vm()->typeProfiler() || vm()->controlFlowProfiler())
1796             vm()->functionHasExecutedCache()->insertUnexecutedRange(m_ownerExecutable->sourceID(), unlinkedExecutable->typeProfilingStartOffset(), unlinkedExecutable->typeProfilingEndOffset());
1797         m_functionExprs[i].set(*m_vm, ownerExecutable, unlinkedExecutable->link(*m_vm, ownerExecutable->source()));
1798     }
1799
1800     if (unlinkedCodeBlock->hasRareData()) {
1801         createRareDataIfNecessary();
1802         if (size_t count = unlinkedCodeBlock->constantBufferCount()) {
1803             m_rareData->m_constantBuffers.grow(count);
1804             for (size_t i = 0; i < count; i++) {
1805                 const UnlinkedCodeBlock::ConstantBuffer& buffer = unlinkedCodeBlock->constantBuffer(i);
1806                 m_rareData->m_constantBuffers[i] = buffer;
1807             }
1808         }
1809         if (size_t count = unlinkedCodeBlock->numberOfExceptionHandlers()) {
1810             m_rareData->m_exceptionHandlers.resizeToFit(count);
1811             for (size_t i = 0; i < count; i++) {
1812                 const UnlinkedHandlerInfo& unlinkedHandler = unlinkedCodeBlock->exceptionHandler(i);
1813                 HandlerInfo& handler = m_rareData->m_exceptionHandlers[i];
1814 #if ENABLE(JIT)
1815                 handler.initialize(unlinkedHandler, CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(LLInt::getCodePtr(op_catch))));
1816 #else
1817                 handler.initialize(unlinkedHandler);
1818 #endif
1819             }
1820         }
1821
1822         if (size_t count = unlinkedCodeBlock->numberOfStringSwitchJumpTables()) {
1823             m_rareData->m_stringSwitchJumpTables.grow(count);
1824             for (size_t i = 0; i < count; i++) {
1825                 UnlinkedStringJumpTable::StringOffsetTable::iterator ptr = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.begin();
1826                 UnlinkedStringJumpTable::StringOffsetTable::iterator end = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.end();
1827                 for (; ptr != end; ++ptr) {
1828                     OffsetLocation offset;
1829                     offset.branchOffset = ptr->value;
1830                     m_rareData->m_stringSwitchJumpTables[i].offsetTable.add(ptr->key, offset);
1831                 }
1832             }
1833         }
1834
1835         if (size_t count = unlinkedCodeBlock->numberOfSwitchJumpTables()) {
1836             m_rareData->m_switchJumpTables.grow(count);
1837             for (size_t i = 0; i < count; i++) {
1838                 UnlinkedSimpleJumpTable& sourceTable = unlinkedCodeBlock->switchJumpTable(i);
1839                 SimpleJumpTable& destTable = m_rareData->m_switchJumpTables[i];
1840                 destTable.branchOffsets = sourceTable.branchOffsets;
1841                 destTable.min = sourceTable.min;
1842             }
1843         }
1844     }
1845
1846     // Allocate metadata buffers for the bytecode
1847     if (size_t size = unlinkedCodeBlock->numberOfLLintCallLinkInfos())
1848         m_llintCallLinkInfos.resizeToFit(size);
1849     if (size_t size = unlinkedCodeBlock->numberOfArrayProfiles())
1850         m_arrayProfiles.grow(size);
1851     if (size_t size = unlinkedCodeBlock->numberOfArrayAllocationProfiles())
1852         m_arrayAllocationProfiles.resizeToFit(size);
1853     if (size_t size = unlinkedCodeBlock->numberOfValueProfiles())
1854         m_valueProfiles.resizeToFit(size);
1855     if (size_t size = unlinkedCodeBlock->numberOfObjectAllocationProfiles())
1856         m_objectAllocationProfiles.resizeToFit(size);
1857
1858     // Copy and translate the UnlinkedInstructions
1859     unsigned instructionCount = unlinkedCodeBlock->instructions().count();
1860     UnlinkedInstructionStream::Reader instructionReader(unlinkedCodeBlock->instructions());
1861
1862     Vector<Instruction, 0, UnsafeVectorOverflow> instructions(instructionCount);
1863
1864     for (unsigned i = 0; !instructionReader.atEnd(); ) {
1865         const UnlinkedInstruction* pc = instructionReader.next();
1866
1867         unsigned opLength = opcodeLength(pc[0].u.opcode);
1868
1869         instructions[i] = vm()->interpreter->getOpcode(pc[0].u.opcode);
1870         for (size_t j = 1; j < opLength; ++j) {
1871             if (sizeof(int32_t) != sizeof(intptr_t))
1872                 instructions[i + j].u.pointer = 0;
1873             instructions[i + j].u.operand = pc[j].u.operand;
1874         }
1875         switch (pc[0].u.opcode) {
1876         case op_has_indexed_property: {
1877             int arrayProfileIndex = pc[opLength - 1].u.operand;
1878             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1879
1880             instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
1881             break;
1882         }
1883         case op_call_varargs:
1884         case op_construct_varargs:
1885         case op_get_by_val: {
1886             int arrayProfileIndex = pc[opLength - 2].u.operand;
1887             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1888
1889             instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex];
1890             FALLTHROUGH;
1891         }
1892         case op_get_direct_pname:
1893         case op_get_by_id:
1894         case op_get_from_arguments: {
1895             ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand];
1896             ASSERT(profile->m_bytecodeOffset == -1);
1897             profile->m_bytecodeOffset = i;
1898             instructions[i + opLength - 1] = profile;
1899             break;
1900         }
1901         case op_put_by_val: {
1902             int arrayProfileIndex = pc[opLength - 1].u.operand;
1903             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1904             instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
1905             break;
1906         }
1907         case op_put_by_val_direct: {
1908             int arrayProfileIndex = pc[opLength - 1].u.operand;
1909             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1910             instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
1911             break;
1912         }
1913
1914         case op_new_array:
1915         case op_new_array_buffer:
1916         case op_new_array_with_size: {
1917             int arrayAllocationProfileIndex = pc[opLength - 1].u.operand;
1918             instructions[i + opLength - 1] = &m_arrayAllocationProfiles[arrayAllocationProfileIndex];
1919             break;
1920         }
1921         case op_new_object: {
1922             int objectAllocationProfileIndex = pc[opLength - 1].u.operand;
1923             ObjectAllocationProfile* objectAllocationProfile = &m_objectAllocationProfiles[objectAllocationProfileIndex];
1924             int inferredInlineCapacity = pc[opLength - 2].u.operand;
1925
1926             instructions[i + opLength - 1] = objectAllocationProfile;
1927             objectAllocationProfile->initialize(*vm(),
1928                 m_ownerExecutable.get(), m_globalObject->objectPrototype(), inferredInlineCapacity);
1929             break;
1930         }
1931
1932         case op_call:
1933         case op_call_eval: {
1934             ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand];
1935             ASSERT(profile->m_bytecodeOffset == -1);
1936             profile->m_bytecodeOffset = i;
1937             instructions[i + opLength - 1] = profile;
1938             int arrayProfileIndex = pc[opLength - 2].u.operand;
1939             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1940             instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex];
1941             instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand];
1942             break;
1943         }
1944         case op_construct: {
1945             instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand];
1946             ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand];
1947             ASSERT(profile->m_bytecodeOffset == -1);
1948             profile->m_bytecodeOffset = i;
1949             instructions[i + opLength - 1] = profile;
1950             break;
1951         }
1952         case op_get_by_id_out_of_line:
1953         case op_get_array_length:
1954             CRASH();
1955
1956         case op_create_lexical_environment: {
1957             int symbolTableIndex = pc[3].u.operand;
1958             RELEASE_ASSERT(clonedConstantSymbolTables.contains(symbolTableIndex));
1959             break;
1960         }
1961
1962         case op_resolve_scope: {
1963             const Identifier& ident = identifier(pc[3].u.operand);
1964             ResolveType type = static_cast<ResolveType>(pc[4].u.operand);
1965             RELEASE_ASSERT(type != LocalClosureVar);
1966             int localScopeDepth = pc[5].u.operand;
1967
1968             ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, type);
1969             instructions[i + 4].u.operand = op.type;
1970             instructions[i + 5].u.operand = op.depth;
1971             if (op.lexicalEnvironment)
1972                 instructions[i + 6].u.symbolTable.set(*vm(), ownerExecutable, op.lexicalEnvironment->symbolTable());
1973             else
1974                 instructions[i + 6].u.pointer = nullptr;
1975             break;
1976         }
1977
1978         case op_get_from_scope: {
1979             ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand];
1980             ASSERT(profile->m_bytecodeOffset == -1);
1981             profile->m_bytecodeOffset = i;
1982             instructions[i + opLength - 1] = profile;
1983
1984             // get_from_scope dst, scope, id, ResolveModeAndType, Structure, Operand
1985
1986             int localScopeDepth = pc[5].u.operand;
1987             instructions[i + 5].u.pointer = nullptr;
1988
1989             ResolveModeAndType modeAndType = ResolveModeAndType(pc[4].u.operand);
1990             if (modeAndType.type() == LocalClosureVar) {
1991                 instructions[i + 4] = ResolveModeAndType(modeAndType.mode(), ClosureVar).operand();
1992                 break;
1993             }
1994
1995             const Identifier& ident = identifier(pc[3].u.operand);
1996             ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, modeAndType.type());
1997
1998             instructions[i + 4].u.operand = ResolveModeAndType(modeAndType.mode(), op.type).operand();
1999             if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks)
2000                 instructions[i + 5].u.watchpointSet = op.watchpointSet;
2001             else if (op.structure)
2002                 instructions[i + 5].u.structure.set(*vm(), ownerExecutable, op.structure);
2003             instructions[i + 6].u.pointer = reinterpret_cast<void*>(op.operand);
2004             break;
2005         }
2006
2007         case op_put_to_scope: {
2008             // put_to_scope scope, id, value, ResolveModeAndType, Structure, Operand
2009             ResolveModeAndType modeAndType = ResolveModeAndType(pc[4].u.operand);
2010             if (modeAndType.type() == LocalClosureVar) {
2011                 // Only do watching if the property we're putting to is not anonymous.
2012                 if (static_cast<unsigned>(pc[2].u.operand) != UINT_MAX) {
2013                     int symbolTableIndex = pc[5].u.operand;
2014                     RELEASE_ASSERT(clonedConstantSymbolTables.contains(symbolTableIndex));
2015                     SymbolTable* symbolTable = jsCast<SymbolTable*>(getConstant(symbolTableIndex));
2016                     const Identifier& ident = identifier(pc[2].u.operand);
2017                     ConcurrentJITLocker locker(symbolTable->m_lock);
2018                     auto iter = symbolTable->find(locker, ident.impl());
2019                     RELEASE_ASSERT(iter != symbolTable->end(locker));
2020                     iter->value.prepareToWatch();
2021                     instructions[i + 5].u.watchpointSet = iter->value.watchpointSet();
2022                 } else
2023                     instructions[i + 5].u.watchpointSet = nullptr;
2024                 break;
2025             }
2026
2027             const Identifier& ident = identifier(pc[2].u.operand);
2028             int localScopeDepth = pc[5].u.operand;
2029             instructions[i + 5].u.pointer = nullptr;
2030             ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Put, modeAndType.type());
2031
2032             instructions[i + 4].u.operand = ResolveModeAndType(modeAndType.mode(), op.type).operand();
2033             if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks)
2034                 instructions[i + 5].u.watchpointSet = op.watchpointSet;
2035             else if (op.type == ClosureVar || op.type == ClosureVarWithVarInjectionChecks) {
2036                 if (op.watchpointSet)
2037                     op.watchpointSet->invalidate(PutToScopeFireDetail(this, ident));
2038             } else if (op.structure)
2039                 instructions[i + 5].u.structure.set(*vm(), ownerExecutable, op.structure);
2040             instructions[i + 6].u.pointer = reinterpret_cast<void*>(op.operand);
2041
2042             break;
2043         }
2044
2045         case op_profile_type: {
2046             RELEASE_ASSERT(vm()->typeProfiler());
2047             // The format of this instruction is: op_profile_type regToProfile, TypeLocation*, flag, identifier?, resolveType?
2048             size_t instructionOffset = i + opLength - 1;
2049             unsigned divotStart, divotEnd;
2050             GlobalVariableID globalVariableID = 0;
2051             RefPtr<TypeSet> globalTypeSet;
2052             bool shouldAnalyze = m_unlinkedCode->typeProfilerExpressionInfoForBytecodeOffset(instructionOffset, divotStart, divotEnd);
2053             VirtualRegister profileRegister(pc[1].u.operand);
2054             ProfileTypeBytecodeFlag flag = static_cast<ProfileTypeBytecodeFlag>(pc[3].u.operand);
2055             SymbolTable* symbolTable = nullptr;
2056
2057             switch (flag) {
2058             case ProfileTypeBytecodeClosureVar: {
2059                 const Identifier& ident = identifier(pc[4].u.operand);
2060                 int localScopeDepth = pc[2].u.operand;
2061                 ResolveType type = static_cast<ResolveType>(pc[5].u.operand);
2062                 // Even though type profiling may be profiling either a Get or a Put, we can always claim a Get because
2063                 // we're abstractly "read"ing from a JSScope.
2064                 ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), localScopeDepth, scope, ident, Get, type);
2065
2066                 if (op.type == ClosureVar)
2067                     symbolTable = op.lexicalEnvironment->symbolTable();
2068                 else if (op.type == GlobalVar)
2069                     symbolTable = m_globalObject.get()->symbolTable();
2070                 
2071                 if (symbolTable) {
2072                     ConcurrentJITLocker locker(symbolTable->m_lock);
2073                     // If our parent scope was created while profiling was disabled, it will not have prepared for profiling yet.
2074                     symbolTable->prepareForTypeProfiling(locker);
2075                     globalVariableID = symbolTable->uniqueIDForVariable(locker, ident.impl(), *vm());
2076                     globalTypeSet = symbolTable->globalTypeSetForVariable(locker, ident.impl(), *vm());
2077                 } else
2078                     globalVariableID = TypeProfilerNoGlobalIDExists;
2079
2080                 break;
2081             }
2082             case ProfileTypeBytecodeLocallyResolved: {
2083                 int symbolTableIndex = pc[2].u.operand;
2084                 RELEASE_ASSERT(clonedConstantSymbolTables.contains(symbolTableIndex));
2085                 SymbolTable* symbolTable = jsCast<SymbolTable*>(getConstant(symbolTableIndex));
2086                 const Identifier& ident = identifier(pc[4].u.operand);
2087                 ConcurrentJITLocker locker(symbolTable->m_lock);
2088                 // If our parent scope was created while profiling was disabled, it will not have prepared for profiling yet.
2089                 globalVariableID = symbolTable->uniqueIDForVariable(locker, ident.impl(), *vm());
2090                 globalTypeSet = symbolTable->globalTypeSetForVariable(locker, ident.impl(), *vm());
2091
2092                 break;
2093             }
2094             case ProfileTypeBytecodeDoesNotHaveGlobalID: 
2095             case ProfileTypeBytecodeFunctionArgument: {
2096                 globalVariableID = TypeProfilerNoGlobalIDExists;
2097                 break;
2098             }
2099             case ProfileTypeBytecodeFunctionReturnStatement: {
2100                 RELEASE_ASSERT(ownerExecutable->isFunctionExecutable());
2101                 globalTypeSet = jsCast<FunctionExecutable*>(ownerExecutable)->returnStatementTypeSet();
2102                 globalVariableID = TypeProfilerReturnStatement;
2103                 if (!shouldAnalyze) {
2104                     // Because a return statement can be added implicitly to return undefined at the end of a function,
2105                     // and these nodes don't emit expression ranges because they aren't in the actual source text of
2106                     // the user's program, give the type profiler some range to identify these return statements.
2107                     // Currently, the text offset that is used as identification is "f" in the function keyword
2108                     // and is stored on TypeLocation's m_divotForFunctionOffsetIfReturnStatement member variable.
2109                     divotStart = divotEnd = m_ownerExecutable->typeProfilingStartOffset();
2110                     shouldAnalyze = true;
2111                 }
2112                 break;
2113             }
2114             }
2115
2116             std::pair<TypeLocation*, bool> locationPair = vm()->typeProfiler()->typeLocationCache()->getTypeLocation(globalVariableID,
2117                 m_ownerExecutable->sourceID(), divotStart, divotEnd, globalTypeSet, vm());
2118             TypeLocation* location = locationPair.first;
2119             bool isNewLocation = locationPair.second;
2120
2121             if (flag == ProfileTypeBytecodeFunctionReturnStatement)
2122                 location->m_divotForFunctionOffsetIfReturnStatement = m_ownerExecutable->typeProfilingStartOffset();
2123
2124             if (shouldAnalyze && isNewLocation)
2125                 vm()->typeProfiler()->insertNewLocation(location);
2126
2127             instructions[i + 2].u.location = location;
2128             break;
2129         }
2130
2131         case op_debug: {
2132             if (pc[1].u.index == DidReachBreakpoint)
2133                 m_hasDebuggerStatement = true;
2134             break;
2135         }
2136
2137         default:
2138             break;
2139         }
2140         i += opLength;
2141     }
2142
2143     if (vm()->controlFlowProfiler())
2144         insertBasicBlockBoundariesForControlFlowProfiler(instructions);
2145
2146     m_instructions = WTF::RefCountedArray<Instruction>(instructions);
2147
2148     // Set optimization thresholds only after m_instructions is initialized, since these
2149     // rely on the instruction count (and are in theory permitted to also inspect the
2150     // instruction stream to more accurate assess the cost of tier-up).
2151     optimizeAfterWarmUp();
2152     jitAfterWarmUp();
2153
2154     // If the concurrent thread will want the code block's hash, then compute it here
2155     // synchronously.
2156     if (Options::alwaysComputeHash())
2157         hash();
2158
2159     if (Options::dumpGeneratedBytecodes())
2160         dumpBytecode();
2161     
2162     m_heap->m_codeBlocks.add(this);
2163     m_heap->reportExtraMemoryAllocated(sizeof(CodeBlock) + m_instructions.size() * sizeof(Instruction));
2164 }
2165
2166 CodeBlock::~CodeBlock()
2167 {
2168     if (m_vm->m_perBytecodeProfiler)
2169         m_vm->m_perBytecodeProfiler->notifyDestruction(this);
2170     
2171 #if ENABLE(VERBOSE_VALUE_PROFILE)
2172     dumpValueProfiles();
2173 #endif
2174     while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end())
2175         m_incomingLLIntCalls.begin()->remove();
2176 #if ENABLE(JIT)
2177     // We may be destroyed before any CodeBlocks that refer to us are destroyed.
2178     // Consider that two CodeBlocks become unreachable at the same time. There
2179     // is no guarantee about the order in which the CodeBlocks are destroyed.
2180     // So, if we don't remove incoming calls, and get destroyed before the
2181     // CodeBlock(s) that have calls into us, then the CallLinkInfo vector's
2182     // destructor will try to remove nodes from our (no longer valid) linked list.
2183     while (m_incomingCalls.begin() != m_incomingCalls.end())
2184         m_incomingCalls.begin()->remove();
2185     while (m_incomingPolymorphicCalls.begin() != m_incomingPolymorphicCalls.end())
2186         m_incomingPolymorphicCalls.begin()->remove();
2187     
2188     // Note that our outgoing calls will be removed from other CodeBlocks'
2189     // m_incomingCalls linked lists through the execution of the ~CallLinkInfo
2190     // destructors.
2191
2192     for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter)
2193         (*iter)->deref();
2194 #endif // ENABLE(JIT)
2195 }
2196
2197 void CodeBlock::setNumParameters(int newValue)
2198 {
2199     m_numParameters = newValue;
2200
2201     m_argumentValueProfiles.resizeToFit(newValue);
2202 }
2203
2204 void EvalCodeCache::visitAggregate(SlotVisitor& visitor)
2205 {
2206     EvalCacheMap::iterator end = m_cacheMap.end();
2207     for (EvalCacheMap::iterator ptr = m_cacheMap.begin(); ptr != end; ++ptr)
2208         visitor.append(&ptr->value);
2209 }
2210
2211 CodeBlock* CodeBlock::specialOSREntryBlockOrNull()
2212 {
2213 #if ENABLE(FTL_JIT)
2214     if (jitType() != JITCode::DFGJIT)
2215         return 0;
2216     DFG::JITCode* jitCode = m_jitCode->dfg();
2217     return jitCode->osrEntryBlock.get();
2218 #else // ENABLE(FTL_JIT)
2219     return 0;
2220 #endif // ENABLE(FTL_JIT)
2221 }
2222
2223 void CodeBlock::visitAggregate(SlotVisitor& visitor)
2224 {
2225 #if ENABLE(PARALLEL_GC)
2226     // I may be asked to scan myself more than once, and it may even happen concurrently.
2227     // To this end, use an atomic operation to check (and set) if I've been called already.
2228     // Only one thread may proceed past this point - whichever one wins the atomic set race.
2229     bool setByMe = m_visitAggregateHasBeenCalled.compareExchangeStrong(false, true);
2230     if (!setByMe)
2231         return;
2232 #endif // ENABLE(PARALLEL_GC)
2233     
2234     if (!!m_alternative)
2235         m_alternative->visitAggregate(visitor);
2236     
2237     if (CodeBlock* otherBlock = specialOSREntryBlockOrNull())
2238         otherBlock->visitAggregate(visitor);
2239
2240     visitor.reportExtraMemoryVisited(ownerExecutable(), sizeof(CodeBlock));
2241     if (m_jitCode)
2242         visitor.reportExtraMemoryVisited(ownerExecutable(), m_jitCode->size());
2243     if (m_instructions.size()) {
2244         // Divide by refCount() because m_instructions points to something that is shared
2245         // by multiple CodeBlocks, and we only want to count it towards the heap size once.
2246         // Having each CodeBlock report only its proportional share of the size is one way
2247         // of accomplishing this.
2248         visitor.reportExtraMemoryVisited(ownerExecutable(), m_instructions.size() * sizeof(Instruction) / m_instructions.refCount());
2249     }
2250
2251     visitor.append(&m_unlinkedCode);
2252
2253     // There are three things that may use unconditional finalizers: lazy bytecode freeing,
2254     // inline cache clearing, and jettisoning. The probability of us wanting to do at
2255     // least one of those things is probably quite close to 1. So we add one no matter what
2256     // and when it runs, it figures out whether it has any work to do.
2257     visitor.addUnconditionalFinalizer(this);
2258     
2259     m_allTransitionsHaveBeenMarked = false;
2260     
2261     if (shouldImmediatelyAssumeLivenessDuringScan()) {
2262         // This code block is live, so scan all references strongly and return.
2263         stronglyVisitStrongReferences(visitor);
2264         stronglyVisitWeakReferences(visitor);
2265         propagateTransitions(visitor);
2266         return;
2267     }
2268     
2269     // There are two things that we use weak reference harvesters for: DFG fixpoint for
2270     // jettisoning, and trying to find structures that would be live based on some
2271     // inline cache. So it makes sense to register them regardless.
2272     visitor.addWeakReferenceHarvester(this);
2273
2274 #if ENABLE(DFG_JIT)
2275     // We get here if we're live in the sense that our owner executable is live,
2276     // but we're not yet live for sure in another sense: we may yet decide that this
2277     // code block should be jettisoned based on its outgoing weak references being
2278     // stale. Set a flag to indicate that we're still assuming that we're dead, and
2279     // perform one round of determining if we're live. The GC may determine, based on
2280     // either us marking additional objects, or by other objects being marked for
2281     // other reasons, that this iteration should run again; it will notify us of this
2282     // decision by calling harvestWeakReferences().
2283     
2284     m_jitCode->dfgCommon()->livenessHasBeenProved = false;
2285     
2286     propagateTransitions(visitor);
2287     determineLiveness(visitor);
2288 #else // ENABLE(DFG_JIT)
2289     RELEASE_ASSERT_NOT_REACHED();
2290 #endif // ENABLE(DFG_JIT)
2291 }
2292
2293 bool CodeBlock::shouldImmediatelyAssumeLivenessDuringScan()
2294 {
2295 #if ENABLE(DFG_JIT)
2296     // Interpreter and Baseline JIT CodeBlocks don't need to be jettisoned when
2297     // their weak references go stale. So if a basline JIT CodeBlock gets
2298     // scanned, we can assume that this means that it's live.
2299     if (!JITCode::isOptimizingJIT(jitType()))
2300         return true;
2301
2302     // For simplicity, we don't attempt to jettison code blocks during GC if
2303     // they are executing. Instead we strongly mark their weak references to
2304     // allow them to continue to execute soundly.
2305     if (m_mayBeExecuting)
2306         return true;
2307
2308     if (Options::forceDFGCodeBlockLiveness())
2309         return true;
2310
2311     return false;
2312 #else
2313     return true;
2314 #endif
2315 }
2316
2317 bool CodeBlock::isKnownToBeLiveDuringGC()
2318 {
2319 #if ENABLE(DFG_JIT)
2320     // This should return true for:
2321     // - Code blocks that behave like normal objects - i.e. if they are referenced then they
2322     //   are live.
2323     // - Code blocks that were running on the stack.
2324     // - Code blocks that survived the last GC if the current GC is an Eden GC. This is
2325     //   because either livenessHasBeenProved would have survived as true or m_mayBeExecuting
2326     //   would survive as true.
2327     // - Code blocks that don't have any dead weak references.
2328     
2329     return shouldImmediatelyAssumeLivenessDuringScan()
2330         || m_jitCode->dfgCommon()->livenessHasBeenProved;
2331 #else
2332     return true;
2333 #endif
2334 }
2335
2336 #if ENABLE(DFG_JIT)
2337 static bool shouldMarkTransition(DFG::WeakReferenceTransition& transition)
2338 {
2339     if (transition.m_codeOrigin && !Heap::isMarked(transition.m_codeOrigin.get()))
2340         return false;
2341     
2342     if (!Heap::isMarked(transition.m_from.get()))
2343         return false;
2344     
2345     return true;
2346 }
2347 #endif // ENABLE(DFG_JIT)
2348
2349 void CodeBlock::propagateTransitions(SlotVisitor& visitor)
2350 {
2351     UNUSED_PARAM(visitor);
2352
2353     if (m_allTransitionsHaveBeenMarked)
2354         return;
2355
2356     bool allAreMarkedSoFar = true;
2357         
2358     Interpreter* interpreter = m_vm->interpreter;
2359     if (jitType() == JITCode::InterpreterThunk) {
2360         const Vector<unsigned>& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions();
2361         for (size_t i = 0; i < propertyAccessInstructions.size(); ++i) {
2362             Instruction* instruction = &instructions()[propertyAccessInstructions[i]];
2363             switch (interpreter->getOpcodeID(instruction[0].u.opcode)) {
2364             case op_put_by_id_transition_direct:
2365             case op_put_by_id_transition_normal:
2366             case op_put_by_id_transition_direct_out_of_line:
2367             case op_put_by_id_transition_normal_out_of_line: {
2368                 if (Heap::isMarked(instruction[4].u.structure.get()))
2369                     visitor.append(&instruction[6].u.structure);
2370                 else
2371                     allAreMarkedSoFar = false;
2372                 break;
2373             }
2374             default:
2375                 break;
2376             }
2377         }
2378     }
2379
2380 #if ENABLE(JIT)
2381     if (JITCode::isJIT(jitType())) {
2382         for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter) {
2383             StructureStubInfo& stubInfo = **iter;
2384             switch (stubInfo.accessType) {
2385             case access_put_by_id_transition_normal:
2386             case access_put_by_id_transition_direct: {
2387                 JSCell* origin = stubInfo.codeOrigin.codeOriginOwner();
2388                 if ((!origin || Heap::isMarked(origin))
2389                     && Heap::isMarked(stubInfo.u.putByIdTransition.previousStructure.get()))
2390                     visitor.append(&stubInfo.u.putByIdTransition.structure);
2391                 else
2392                     allAreMarkedSoFar = false;
2393                 break;
2394             }
2395
2396             case access_put_by_id_list: {
2397                 PolymorphicPutByIdList* list = stubInfo.u.putByIdList.list;
2398                 JSCell* origin = stubInfo.codeOrigin.codeOriginOwner();
2399                 if (origin && !Heap::isMarked(origin)) {
2400                     allAreMarkedSoFar = false;
2401                     break;
2402                 }
2403                 for (unsigned j = list->size(); j--;) {
2404                     PutByIdAccess& access = list->m_list[j];
2405                     if (!access.isTransition())
2406                         continue;
2407                     if (Heap::isMarked(access.oldStructure()))
2408                         visitor.append(&access.m_newStructure);
2409                     else
2410                         allAreMarkedSoFar = false;
2411                 }
2412                 break;
2413             }
2414             
2415             default:
2416                 break;
2417             }
2418         }
2419     }
2420 #endif // ENABLE(JIT)
2421     
2422 #if ENABLE(DFG_JIT)
2423     if (JITCode::isOptimizingJIT(jitType())) {
2424         DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
2425         
2426         for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) {
2427             if (shouldMarkTransition(dfgCommon->transitions[i])) {
2428                 // If the following three things are live, then the target of the
2429                 // transition is also live:
2430                 //
2431                 // - This code block. We know it's live already because otherwise
2432                 //   we wouldn't be scanning ourselves.
2433                 //
2434                 // - The code origin of the transition. Transitions may arise from
2435                 //   code that was inlined. They are not relevant if the user's
2436                 //   object that is required for the inlinee to run is no longer
2437                 //   live.
2438                 //
2439                 // - The source of the transition. The transition checks if some
2440                 //   heap location holds the source, and if so, stores the target.
2441                 //   Hence the source must be live for the transition to be live.
2442                 //
2443                 // We also short-circuit the liveness if the structure is harmless
2444                 // to mark (i.e. its global object and prototype are both already
2445                 // live).
2446                 
2447                 visitor.append(&dfgCommon->transitions[i].m_to);
2448             } else
2449                 allAreMarkedSoFar = false;
2450         }
2451     }
2452 #endif // ENABLE(DFG_JIT)
2453     
2454     if (allAreMarkedSoFar)
2455         m_allTransitionsHaveBeenMarked = true;
2456 }
2457
2458 void CodeBlock::determineLiveness(SlotVisitor& visitor)
2459 {
2460     UNUSED_PARAM(visitor);
2461     
2462     if (shouldImmediatelyAssumeLivenessDuringScan())
2463         return;
2464     
2465 #if ENABLE(DFG_JIT)
2466     // Check if we have any remaining work to do.
2467     DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
2468     if (dfgCommon->livenessHasBeenProved)
2469         return;
2470     
2471     // Now check all of our weak references. If all of them are live, then we
2472     // have proved liveness and so we scan our strong references. If at end of
2473     // GC we still have not proved liveness, then this code block is toast.
2474     bool allAreLiveSoFar = true;
2475     for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) {
2476         if (!Heap::isMarked(dfgCommon->weakReferences[i].get())) {
2477             allAreLiveSoFar = false;
2478             break;
2479         }
2480     }
2481     if (allAreLiveSoFar) {
2482         for (unsigned i = 0; i < dfgCommon->weakStructureReferences.size(); ++i) {
2483             if (!Heap::isMarked(dfgCommon->weakStructureReferences[i].get())) {
2484                 allAreLiveSoFar = false;
2485                 break;
2486             }
2487         }
2488     }
2489     
2490     // If some weak references are dead, then this fixpoint iteration was
2491     // unsuccessful.
2492     if (!allAreLiveSoFar)
2493         return;
2494     
2495     // All weak references are live. Record this information so we don't
2496     // come back here again, and scan the strong references.
2497     dfgCommon->livenessHasBeenProved = true;
2498     stronglyVisitStrongReferences(visitor);
2499 #endif // ENABLE(DFG_JIT)
2500 }
2501
2502 void CodeBlock::visitWeakReferences(SlotVisitor& visitor)
2503 {
2504     propagateTransitions(visitor);
2505     determineLiveness(visitor);
2506 }
2507
2508 void CodeBlock::finalizeUnconditionally()
2509 {
2510     Interpreter* interpreter = m_vm->interpreter;
2511     if (JITCode::couldBeInterpreted(jitType())) {
2512         const Vector<unsigned>& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions();
2513         for (size_t size = propertyAccessInstructions.size(), i = 0; i < size; ++i) {
2514             Instruction* curInstruction = &instructions()[propertyAccessInstructions[i]];
2515             switch (interpreter->getOpcodeID(curInstruction[0].u.opcode)) {
2516             case op_get_by_id:
2517             case op_get_by_id_out_of_line:
2518             case op_put_by_id:
2519             case op_put_by_id_out_of_line:
2520                 if (!curInstruction[4].u.structure || Heap::isMarked(curInstruction[4].u.structure.get()))
2521                     break;
2522                 if (Options::verboseOSR())
2523                     dataLogF("Clearing LLInt property access with structure %p.\n", curInstruction[4].u.structure.get());
2524                 curInstruction[4].u.structure.clear();
2525                 curInstruction[5].u.operand = 0;
2526                 break;
2527             case op_put_by_id_transition_direct:
2528             case op_put_by_id_transition_normal:
2529             case op_put_by_id_transition_direct_out_of_line:
2530             case op_put_by_id_transition_normal_out_of_line:
2531                 if (Heap::isMarked(curInstruction[4].u.structure.get())
2532                     && Heap::isMarked(curInstruction[6].u.structure.get())
2533                     && Heap::isMarked(curInstruction[7].u.structureChain.get()))
2534                     break;
2535                 if (Options::verboseOSR()) {
2536                     dataLogF("Clearing LLInt put transition with structures %p -> %p, chain %p.\n",
2537                             curInstruction[4].u.structure.get(),
2538                             curInstruction[6].u.structure.get(),
2539                             curInstruction[7].u.structureChain.get());
2540                 }
2541                 curInstruction[4].u.structure.clear();
2542                 curInstruction[6].u.structure.clear();
2543                 curInstruction[7].u.structureChain.clear();
2544                 curInstruction[0].u.opcode = interpreter->getOpcode(op_put_by_id);
2545                 break;
2546             case op_get_array_length:
2547                 break;
2548             case op_to_this:
2549                 if (!curInstruction[2].u.structure || Heap::isMarked(curInstruction[2].u.structure.get()))
2550                     break;
2551                 if (Options::verboseOSR())
2552                     dataLogF("Clearing LLInt to_this with structure %p.\n", curInstruction[2].u.structure.get());
2553                 curInstruction[2].u.structure.clear();
2554                 curInstruction[3].u.toThisStatus = merge(
2555                     curInstruction[3].u.toThisStatus, ToThisClearedByGC);
2556                 break;
2557             case op_create_this: {
2558                 auto& cacheWriteBarrier = curInstruction[4].u.jsCell;
2559                 if (!cacheWriteBarrier || cacheWriteBarrier.unvalidatedGet() == JSCell::seenMultipleCalleeObjects())
2560                     break;
2561                 JSCell* cachedFunction = cacheWriteBarrier.get();
2562                 if (Heap::isMarked(cachedFunction))
2563                     break;
2564                 if (Options::verboseOSR())
2565                     dataLogF("Clearing LLInt create_this with cached callee %p.\n", cachedFunction);
2566                 cacheWriteBarrier.clear();
2567                 break;
2568             }
2569             case op_resolve_scope: {
2570                 // Right now this isn't strictly necessary. Any symbol tables that this will refer to
2571                 // are for outer functions, and we refer to those functions strongly, and they refer
2572                 // to the symbol table strongly. But it's nice to be on the safe side.
2573                 WriteBarrierBase<SymbolTable>& symbolTable = curInstruction[6].u.symbolTable;
2574                 if (!symbolTable || Heap::isMarked(symbolTable.get()))
2575                     break;
2576                 if (Options::verboseOSR())
2577                     dataLogF("Clearing dead symbolTable %p.\n", symbolTable.get());
2578                 symbolTable.clear();
2579                 break;
2580             }
2581             case op_get_from_scope:
2582             case op_put_to_scope: {
2583                 ResolveModeAndType modeAndType =
2584                     ResolveModeAndType(curInstruction[4].u.operand);
2585                 if (modeAndType.type() == GlobalVar || modeAndType.type() == GlobalVarWithVarInjectionChecks || modeAndType.type() == LocalClosureVar)
2586                     continue;
2587                 WriteBarrierBase<Structure>& structure = curInstruction[5].u.structure;
2588                 if (!structure || Heap::isMarked(structure.get()))
2589                     break;
2590                 if (Options::verboseOSR())
2591                     dataLogF("Clearing scope access with structure %p.\n", structure.get());
2592                 structure.clear();
2593                 break;
2594             }
2595             default:
2596                 OpcodeID opcodeID = interpreter->getOpcodeID(curInstruction[0].u.opcode);
2597                 ASSERT_WITH_MESSAGE_UNUSED(opcodeID, false, "Unhandled opcode in CodeBlock::finalizeUnconditionally, %s(%d) at bc %u", opcodeNames[opcodeID], opcodeID, propertyAccessInstructions[i]);
2598             }
2599         }
2600
2601         for (unsigned i = 0; i < m_llintCallLinkInfos.size(); ++i) {
2602             if (m_llintCallLinkInfos[i].isLinked() && !Heap::isMarked(m_llintCallLinkInfos[i].callee.get())) {
2603                 if (Options::verboseOSR())
2604                     dataLog("Clearing LLInt call from ", *this, "\n");
2605                 m_llintCallLinkInfos[i].unlink();
2606             }
2607             if (!!m_llintCallLinkInfos[i].lastSeenCallee && !Heap::isMarked(m_llintCallLinkInfos[i].lastSeenCallee.get()))
2608                 m_llintCallLinkInfos[i].lastSeenCallee.clear();
2609         }
2610     }
2611
2612 #if ENABLE(DFG_JIT)
2613     // Check if we're not live. If we are, then jettison.
2614     if (!isKnownToBeLiveDuringGC()) {
2615         if (Options::verboseOSR())
2616             dataLog(*this, " has dead weak references, jettisoning during GC.\n");
2617
2618         if (DFG::shouldShowDisassembly()) {
2619             dataLog(*this, " will be jettisoned because of the following dead references:\n");
2620             DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
2621             for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) {
2622                 DFG::WeakReferenceTransition& transition = dfgCommon->transitions[i];
2623                 JSCell* origin = transition.m_codeOrigin.get();
2624                 JSCell* from = transition.m_from.get();
2625                 JSCell* to = transition.m_to.get();
2626                 if ((!origin || Heap::isMarked(origin)) && Heap::isMarked(from))
2627                     continue;
2628                 dataLog("    Transition under ", RawPointer(origin), ", ", RawPointer(from), " -> ", RawPointer(to), ".\n");
2629             }
2630             for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) {
2631                 JSCell* weak = dfgCommon->weakReferences[i].get();
2632                 if (Heap::isMarked(weak))
2633                     continue;
2634                 dataLog("    Weak reference ", RawPointer(weak), ".\n");
2635             }
2636         }
2637         
2638         jettison(Profiler::JettisonDueToWeakReference);
2639         return;
2640     }
2641 #endif // ENABLE(DFG_JIT)
2642
2643 #if ENABLE(JIT)
2644     // Handle inline caches.
2645     if (!!jitCode()) {
2646         RepatchBuffer repatchBuffer(this);
2647         
2648         for (auto iter = callLinkInfosBegin(); !!iter; ++iter)
2649             (*iter)->visitWeak(repatchBuffer);
2650
2651         for (Bag<StructureStubInfo>::iterator iter = m_stubInfos.begin(); !!iter; ++iter) {
2652             StructureStubInfo& stubInfo = **iter;
2653             
2654             if (stubInfo.visitWeakReferences(repatchBuffer))
2655                 continue;
2656             
2657             resetStubDuringGCInternal(repatchBuffer, stubInfo);
2658         }
2659     }
2660 #endif
2661 }
2662
2663 void CodeBlock::getStubInfoMap(const ConcurrentJITLocker&, StubInfoMap& result)
2664 {
2665 #if ENABLE(JIT)
2666     toHashMap(m_stubInfos, getStructureStubInfoCodeOrigin, result);
2667 #else
2668     UNUSED_PARAM(result);
2669 #endif
2670 }
2671
2672 void CodeBlock::getStubInfoMap(StubInfoMap& result)
2673 {
2674     ConcurrentJITLocker locker(m_lock);
2675     getStubInfoMap(locker, result);
2676 }
2677
2678 void CodeBlock::getCallLinkInfoMap(const ConcurrentJITLocker&, CallLinkInfoMap& result)
2679 {
2680 #if ENABLE(JIT)
2681     toHashMap(m_callLinkInfos, getCallLinkInfoCodeOrigin, result);
2682 #else
2683     UNUSED_PARAM(result);
2684 #endif
2685 }
2686
2687 void CodeBlock::getCallLinkInfoMap(CallLinkInfoMap& result)
2688 {
2689     ConcurrentJITLocker locker(m_lock);
2690     getCallLinkInfoMap(locker, result);
2691 }
2692
2693 void CodeBlock::getByValInfoMap(const ConcurrentJITLocker&, ByValInfoMap& result)
2694 {
2695 #if ENABLE(JIT)
2696     for (auto* byValInfo : m_byValInfos)
2697         result.add(CodeOrigin(byValInfo->bytecodeIndex), byValInfo);
2698 #else
2699     UNUSED_PARAM(result);
2700 #endif
2701 }
2702
2703 void CodeBlock::getByValInfoMap(ByValInfoMap& result)
2704 {
2705     ConcurrentJITLocker locker(m_lock);
2706     getByValInfoMap(locker, result);
2707 }
2708
2709 #if ENABLE(JIT)
2710 StructureStubInfo* CodeBlock::addStubInfo()
2711 {
2712     ConcurrentJITLocker locker(m_lock);
2713     return m_stubInfos.add();
2714 }
2715
2716 StructureStubInfo* CodeBlock::findStubInfo(CodeOrigin codeOrigin)
2717 {
2718     for (StructureStubInfo* stubInfo : m_stubInfos) {
2719         if (stubInfo->codeOrigin == codeOrigin)
2720             return stubInfo;
2721     }
2722     return nullptr;
2723 }
2724
2725 ByValInfo* CodeBlock::addByValInfo()
2726 {
2727     ConcurrentJITLocker locker(m_lock);
2728     return m_byValInfos.add();
2729 }
2730
2731 CallLinkInfo* CodeBlock::addCallLinkInfo()
2732 {
2733     ConcurrentJITLocker locker(m_lock);
2734     return m_callLinkInfos.add();
2735 }
2736
2737 void CodeBlock::resetStub(StructureStubInfo& stubInfo)
2738 {
2739     if (stubInfo.accessType == access_unset)
2740         return;
2741     
2742     ConcurrentJITLocker locker(m_lock);
2743     
2744     RepatchBuffer repatchBuffer(this);
2745     resetStubInternal(repatchBuffer, stubInfo);
2746 }
2747
2748 void CodeBlock::resetStubInternal(RepatchBuffer& repatchBuffer, StructureStubInfo& stubInfo)
2749 {
2750     AccessType accessType = static_cast<AccessType>(stubInfo.accessType);
2751     
2752     if (Options::verboseOSR()) {
2753         // This can be called from GC destructor calls, so we don't try to do a full dump
2754         // of the CodeBlock.
2755         dataLog("Clearing structure cache (kind ", static_cast<int>(stubInfo.accessType), ") in ", RawPointer(this), ".\n");
2756     }
2757     
2758     RELEASE_ASSERT(JITCode::isJIT(jitType()));
2759     
2760     if (isGetByIdAccess(accessType))
2761         resetGetByID(repatchBuffer, stubInfo);
2762     else if (isPutByIdAccess(accessType))
2763         resetPutByID(repatchBuffer, stubInfo);
2764     else {
2765         RELEASE_ASSERT(isInAccess(accessType));
2766         resetIn(repatchBuffer, stubInfo);
2767     }
2768     
2769     stubInfo.reset();
2770 }
2771
2772 void CodeBlock::resetStubDuringGCInternal(RepatchBuffer& repatchBuffer, StructureStubInfo& stubInfo)
2773 {
2774     resetStubInternal(repatchBuffer, stubInfo);
2775     stubInfo.resetByGC = true;
2776 }
2777
2778 CallLinkInfo* CodeBlock::getCallLinkInfoForBytecodeIndex(unsigned index)
2779 {
2780     for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter) {
2781         if ((*iter)->codeOrigin() == CodeOrigin(index))
2782             return *iter;
2783     }
2784     return nullptr;
2785 }
2786 #endif
2787
2788 void CodeBlock::stronglyVisitStrongReferences(SlotVisitor& visitor)
2789 {
2790     visitor.append(&m_globalObject);
2791     visitor.append(&m_ownerExecutable);
2792     visitor.append(&m_unlinkedCode);
2793     if (m_rareData)
2794         m_rareData->m_evalCodeCache.visitAggregate(visitor);
2795     visitor.appendValues(m_constantRegisters.data(), m_constantRegisters.size());
2796     for (size_t i = 0; i < m_functionExprs.size(); ++i)
2797         visitor.append(&m_functionExprs[i]);
2798     for (size_t i = 0; i < m_functionDecls.size(); ++i)
2799         visitor.append(&m_functionDecls[i]);
2800     for (unsigned i = 0; i < m_objectAllocationProfiles.size(); ++i)
2801         m_objectAllocationProfiles[i].visitAggregate(visitor);
2802
2803 #if ENABLE(DFG_JIT)
2804     if (JITCode::isOptimizingJIT(jitType())) {
2805         // FIXME: This is an antipattern for two reasons. References introduced by the DFG
2806         // that aren't in the original CodeBlock being compiled should be weakly referenced.
2807         // Inline call frames aren't in the original CodeBlock, so they qualify as weak. Also,
2808         // those weak references should already be tracked in the DFG as weak FrozenValues. So,
2809         // there is probably no need for this. We already have assertions that this should be
2810         // unnecessary.
2811         // https://bugs.webkit.org/show_bug.cgi?id=146613
2812         DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
2813         if (dfgCommon->inlineCallFrames.get())
2814             dfgCommon->inlineCallFrames->visitAggregate(visitor);
2815     }
2816 #endif
2817
2818     updateAllPredictions();
2819 }
2820
2821 void CodeBlock::stronglyVisitWeakReferences(SlotVisitor& visitor)
2822 {
2823     UNUSED_PARAM(visitor);
2824
2825 #if ENABLE(DFG_JIT)
2826     if (!JITCode::isOptimizingJIT(jitType()))
2827         return;
2828     
2829     DFG::CommonData* dfgCommon = m_jitCode->dfgCommon();
2830
2831     for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) {
2832         if (!!dfgCommon->transitions[i].m_codeOrigin)
2833             visitor.append(&dfgCommon->transitions[i].m_codeOrigin); // Almost certainly not necessary, since the code origin should also be a weak reference. Better to be safe, though.
2834         visitor.append(&dfgCommon->transitions[i].m_from);
2835         visitor.append(&dfgCommon->transitions[i].m_to);
2836     }
2837     
2838     for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i)
2839         visitor.append(&dfgCommon->weakReferences[i]);
2840
2841     for (unsigned i = 0; i < dfgCommon->weakStructureReferences.size(); ++i)
2842         visitor.append(&dfgCommon->weakStructureReferences[i]);
2843 #endif    
2844 }
2845
2846 CodeBlock* CodeBlock::baselineAlternative()
2847 {
2848 #if ENABLE(JIT)
2849     CodeBlock* result = this;
2850     while (result->alternative())
2851         result = result->alternative();
2852     RELEASE_ASSERT(result);
2853     RELEASE_ASSERT(JITCode::isBaselineCode(result->jitType()) || result->jitType() == JITCode::None);
2854     return result;
2855 #else
2856     return this;
2857 #endif
2858 }
2859
2860 CodeBlock* CodeBlock::baselineVersion()
2861 {
2862 #if ENABLE(JIT)
2863     if (JITCode::isBaselineCode(jitType()))
2864         return this;
2865     CodeBlock* result = replacement();
2866     if (!result) {
2867         // This can happen if we're creating the original CodeBlock for an executable.
2868         // Assume that we're the baseline CodeBlock.
2869         RELEASE_ASSERT(jitType() == JITCode::None);
2870         return this;
2871     }
2872     result = result->baselineAlternative();
2873     return result;
2874 #else
2875     return this;
2876 #endif
2877 }
2878
2879 #if ENABLE(JIT)
2880 bool CodeBlock::hasOptimizedReplacement(JITCode::JITType typeToReplace)
2881 {
2882     return JITCode::isHigherTier(replacement()->jitType(), typeToReplace);
2883 }
2884
2885 bool CodeBlock::hasOptimizedReplacement()
2886 {
2887     return hasOptimizedReplacement(jitType());
2888 }
2889 #endif
2890
2891 HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset, RequiredHandler requiredHandler)
2892 {
2893     RELEASE_ASSERT(bytecodeOffset < instructions().size());
2894
2895     if (!m_rareData)
2896         return 0;
2897     
2898     Vector<HandlerInfo>& exceptionHandlers = m_rareData->m_exceptionHandlers;
2899     for (size_t i = 0; i < exceptionHandlers.size(); ++i) {
2900         HandlerInfo& handler = exceptionHandlers[i];
2901         if ((requiredHandler == RequiredHandler::CatchHandler) && !handler.isCatchHandler())
2902             continue;
2903
2904         // Handlers are ordered innermost first, so the first handler we encounter
2905         // that contains the source address is the correct handler to use.
2906         if (handler.start <= bytecodeOffset && handler.end > bytecodeOffset)
2907             return &handler;
2908     }
2909
2910     return 0;
2911 }
2912
2913 unsigned CodeBlock::lineNumberForBytecodeOffset(unsigned bytecodeOffset)
2914 {
2915     RELEASE_ASSERT(bytecodeOffset < instructions().size());
2916     return m_ownerExecutable->firstLine() + m_unlinkedCode->lineNumberForBytecodeOffset(bytecodeOffset);
2917 }
2918
2919 unsigned CodeBlock::columnNumberForBytecodeOffset(unsigned bytecodeOffset)
2920 {
2921     int divot;
2922     int startOffset;
2923     int endOffset;
2924     unsigned line;
2925     unsigned column;
2926     expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
2927     return column;
2928 }
2929
2930 void CodeBlock::expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column)
2931 {
2932     m_unlinkedCode->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
2933     divot += m_sourceOffset;
2934     column += line ? 1 : firstLineColumnOffset();
2935     line += m_ownerExecutable->firstLine();
2936 }
2937
2938 bool CodeBlock::hasOpDebugForLineAndColumn(unsigned line, unsigned column)
2939 {
2940     Interpreter* interpreter = vm()->interpreter;
2941     const Instruction* begin = instructions().begin();
2942     const Instruction* end = instructions().end();
2943     for (const Instruction* it = begin; it != end;) {
2944         OpcodeID opcodeID = interpreter->getOpcodeID(it->u.opcode);
2945         if (opcodeID == op_debug) {
2946             unsigned bytecodeOffset = it - begin;
2947             int unused;
2948             unsigned opDebugLine;
2949             unsigned opDebugColumn;
2950             expressionRangeForBytecodeOffset(bytecodeOffset, unused, unused, unused, opDebugLine, opDebugColumn);
2951             if (line == opDebugLine && (column == Breakpoint::unspecifiedColumn || column == opDebugColumn))
2952                 return true;
2953         }
2954         it += opcodeLengths[opcodeID];
2955     }
2956     return false;
2957 }
2958
2959 void CodeBlock::shrinkToFit(ShrinkMode shrinkMode)
2960 {
2961     m_rareCaseProfiles.shrinkToFit();
2962     m_specialFastCaseProfiles.shrinkToFit();
2963     
2964     if (shrinkMode == EarlyShrink) {
2965         m_constantRegisters.shrinkToFit();
2966         m_constantsSourceCodeRepresentation.shrinkToFit();
2967         
2968         if (m_rareData) {
2969             m_rareData->m_switchJumpTables.shrinkToFit();
2970             m_rareData->m_stringSwitchJumpTables.shrinkToFit();
2971         }
2972     } // else don't shrink these, because we would have already pointed pointers into these tables.
2973 }
2974
2975 #if ENABLE(JIT)
2976 void CodeBlock::linkIncomingCall(ExecState* callerFrame, CallLinkInfo* incoming)
2977 {
2978     noticeIncomingCall(callerFrame);
2979     m_incomingCalls.push(incoming);
2980 }
2981
2982 void CodeBlock::linkIncomingPolymorphicCall(ExecState* callerFrame, PolymorphicCallNode* incoming)
2983 {
2984     noticeIncomingCall(callerFrame);
2985     m_incomingPolymorphicCalls.push(incoming);
2986 }
2987 #endif // ENABLE(JIT)
2988
2989 void CodeBlock::unlinkIncomingCalls()
2990 {
2991     while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end())
2992         m_incomingLLIntCalls.begin()->unlink();
2993 #if ENABLE(JIT)
2994     if (m_incomingCalls.isEmpty() && m_incomingPolymorphicCalls.isEmpty())
2995         return;
2996     RepatchBuffer repatchBuffer(this);
2997     while (m_incomingCalls.begin() != m_incomingCalls.end())
2998         m_incomingCalls.begin()->unlink(repatchBuffer);
2999     while (m_incomingPolymorphicCalls.begin() != m_incomingPolymorphicCalls.end())
3000         m_incomingPolymorphicCalls.begin()->unlink(repatchBuffer);
3001 #endif // ENABLE(JIT)
3002 }
3003
3004 void CodeBlock::linkIncomingCall(ExecState* callerFrame, LLIntCallLinkInfo* incoming)
3005 {
3006     noticeIncomingCall(callerFrame);
3007     m_incomingLLIntCalls.push(incoming);
3008 }
3009
3010 void CodeBlock::install()
3011 {
3012     ownerExecutable()->installCode(this);
3013 }
3014
3015 PassRefPtr<CodeBlock> CodeBlock::newReplacement()
3016 {
3017     return ownerExecutable()->newReplacementCodeBlockFor(specializationKind());
3018 }
3019
3020 #if ENABLE(JIT)
3021 CodeBlock* ProgramCodeBlock::replacement()
3022 {
3023     return jsCast<ProgramExecutable*>(ownerExecutable())->codeBlock();
3024 }
3025
3026 CodeBlock* EvalCodeBlock::replacement()
3027 {
3028     return jsCast<EvalExecutable*>(ownerExecutable())->codeBlock();
3029 }
3030
3031 CodeBlock* FunctionCodeBlock::replacement()
3032 {
3033     return jsCast<FunctionExecutable*>(ownerExecutable())->codeBlockFor(m_isConstructor ? CodeForConstruct : CodeForCall);
3034 }
3035
3036 DFG::CapabilityLevel ProgramCodeBlock::capabilityLevelInternal()
3037 {
3038     return DFG::programCapabilityLevel(this);
3039 }
3040
3041 DFG::CapabilityLevel EvalCodeBlock::capabilityLevelInternal()
3042 {
3043     return DFG::evalCapabilityLevel(this);
3044 }
3045
3046 DFG::CapabilityLevel FunctionCodeBlock::capabilityLevelInternal()
3047 {
3048     if (m_isConstructor)
3049         return DFG::functionForConstructCapabilityLevel(this);
3050     return DFG::functionForCallCapabilityLevel(this);
3051 }
3052 #endif
3053
3054 void CodeBlock::jettison(Profiler::JettisonReason reason, ReoptimizationMode mode, const FireDetail* detail)
3055 {
3056     RELEASE_ASSERT(reason != Profiler::NotJettisoned);
3057     
3058 #if ENABLE(DFG_JIT)
3059     if (DFG::shouldShowDisassembly()) {
3060         dataLog("Jettisoning ", *this);
3061         if (mode == CountReoptimization)
3062             dataLog(" and counting reoptimization");
3063         dataLog(" due to ", reason);
3064         if (detail)
3065             dataLog(", ", *detail);
3066         dataLog(".\n");
3067     }
3068     
3069     DeferGCForAWhile deferGC(*m_heap);
3070     RELEASE_ASSERT(JITCode::isOptimizingJIT(jitType()));
3071     
3072     if (Profiler::Compilation* compilation = jitCode()->dfgCommon()->compilation.get())
3073         compilation->setJettisonReason(reason, detail);
3074     
3075     // We want to accomplish two things here:
3076     // 1) Make sure that if this CodeBlock is on the stack right now, then if we return to it
3077     //    we should OSR exit at the top of the next bytecode instruction after the return.
3078     // 2) Make sure that if we call the owner executable, then we shouldn't call this CodeBlock.
3079     
3080     // This accomplishes the OSR-exit-on-return part, and does its own book-keeping about
3081     // whether the invalidation has already happened.
3082     if (!jitCode()->dfgCommon()->invalidate()) {
3083         // Nothing to do since we've already been invalidated. That means that we cannot be
3084         // the optimized replacement.
3085         RELEASE_ASSERT(this != replacement());
3086         return;
3087     }
3088     
3089     if (DFG::shouldShowDisassembly())
3090         dataLog("    Did invalidate ", *this, "\n");
3091     
3092     // Count the reoptimization if that's what the user wanted.
3093     if (mode == CountReoptimization) {
3094         // FIXME: Maybe this should call alternative().
3095         // https://bugs.webkit.org/show_bug.cgi?id=123677
3096         baselineAlternative()->countReoptimization();
3097         if (DFG::shouldShowDisassembly())
3098             dataLog("    Did count reoptimization for ", *this, "\n");
3099     }
3100     
3101     // Now take care of the entrypoint.
3102     if (this != replacement()) {
3103         // This means that we were never the entrypoint. This can happen for OSR entry code
3104         // blocks.
3105         return;
3106     }
3107     alternative()->optimizeAfterWarmUp();
3108     tallyFrequentExitSites();
3109     alternative()->install();
3110     if (DFG::shouldShowDisassembly())
3111         dataLog("    Did install baseline version of ", *this, "\n");
3112 #else // ENABLE(DFG_JIT)
3113     UNUSED_PARAM(mode);
3114     UNUSED_PARAM(detail);
3115     UNREACHABLE_FOR_PLATFORM();
3116 #endif // ENABLE(DFG_JIT)
3117 }
3118
3119 JSGlobalObject* CodeBlock::globalObjectFor(CodeOrigin codeOrigin)
3120 {
3121     if (!codeOrigin.inlineCallFrame)
3122         return globalObject();
3123     return jsCast<FunctionExecutable*>(codeOrigin.inlineCallFrame->executable.get())->eitherCodeBlock()->globalObject();
3124 }
3125
3126 class RecursionCheckFunctor {
3127 public:
3128     RecursionCheckFunctor(CallFrame* startCallFrame, CodeBlock* codeBlock, unsigned depthToCheck)
3129         : m_startCallFrame(startCallFrame)
3130         , m_codeBlock(codeBlock)
3131         , m_depthToCheck(depthToCheck)
3132         , m_foundStartCallFrame(false)
3133         , m_didRecurse(false)
3134     { }
3135
3136     StackVisitor::Status operator()(StackVisitor& visitor)
3137     {
3138         CallFrame* currentCallFrame = visitor->callFrame();
3139
3140         if (currentCallFrame == m_startCallFrame)
3141             m_foundStartCallFrame = true;
3142
3143         if (m_foundStartCallFrame) {
3144             if (visitor->callFrame()->codeBlock() == m_codeBlock) {
3145                 m_didRecurse = true;
3146                 return StackVisitor::Done;
3147             }
3148
3149             if (!m_depthToCheck--)
3150                 return StackVisitor::Done;
3151         }
3152
3153         return StackVisitor::Continue;
3154     }
3155
3156     bool didRecurse() const { return m_didRecurse; }
3157
3158 private:
3159     CallFrame* m_startCallFrame;
3160     CodeBlock* m_codeBlock;
3161     unsigned m_depthToCheck;
3162     bool m_foundStartCallFrame;
3163     bool m_didRecurse;
3164 };
3165
3166 void CodeBlock::noticeIncomingCall(ExecState* callerFrame)
3167 {
3168     CodeBlock* callerCodeBlock = callerFrame->codeBlock();
3169     
3170     if (Options::verboseCallLink())
3171         dataLog("Noticing call link from ", pointerDump(callerCodeBlock), " to ", *this, "\n");
3172     
3173 #if ENABLE(DFG_JIT)
3174     if (!m_shouldAlwaysBeInlined)
3175         return;
3176     
3177     if (!callerCodeBlock) {
3178         m_shouldAlwaysBeInlined = false;
3179         if (Options::verboseCallLink())
3180             dataLog("    Clearing SABI because caller is native.\n");
3181         return;
3182     }
3183
3184     if (!hasBaselineJITProfiling())
3185         return;
3186
3187     if (!DFG::mightInlineFunction(this))
3188         return;
3189
3190     if (!canInline(m_capabilityLevelState))
3191         return;
3192     
3193     if (!DFG::isSmallEnoughToInlineCodeInto(callerCodeBlock)) {
3194         m_shouldAlwaysBeInlined = false;
3195         if (Options::verboseCallLink())
3196             dataLog("    Clearing SABI because caller is too large.\n");
3197         return;
3198     }