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