Rename dataLog() and dataLogV() to dataLogF() and dataLogFV()
[WebKit-https.git] / Source / JavaScriptCore / bytecode / CodeBlock.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2010 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 "DFGCapabilities.h"
35 #include "DFGCommon.h"
36 #include "DFGNode.h"
37 #include "DFGRepatch.h"
38 #include "Debugger.h"
39 #include "Interpreter.h"
40 #include "JIT.h"
41 #include "JITStubs.h"
42 #include "JSActivation.h"
43 #include "JSFunction.h"
44 #include "JSNameScope.h"
45 #include "JSValue.h"
46 #include "LowLevelInterpreter.h"
47 #include "RepatchBuffer.h"
48 #include "SlotVisitorInlines.h"
49 #include <stdio.h>
50 #include <wtf/StringExtras.h>
51 #include <wtf/UnusedParam.h>
52
53 #if ENABLE(DFG_JIT)
54 #include "DFGOperations.h"
55 #endif
56
57 #define DUMP_CODE_BLOCK_STATISTICS 0
58
59 namespace JSC {
60
61 #if ENABLE(DFG_JIT)
62 using namespace DFG;
63 #endif
64
65 static String escapeQuotes(const String& str)
66 {
67     String result = str;
68     size_t pos = 0;
69     while ((pos = result.find('\"', pos)) != notFound) {
70         result = makeString(result.substringSharingImpl(0, pos), "\"\\\"\"", result.substringSharingImpl(pos + 1));
71         pos += 4;
72     }
73     return result;
74 }
75
76 static String valueToSourceString(ExecState* exec, JSValue val)
77 {
78     if (!val)
79         return ASCIILiteral("0");
80
81     if (val.isString())
82         return makeString("\"", escapeQuotes(val.toString(exec)->value(exec)), "\"");
83
84     return val.description();
85 }
86
87 static CString constantName(ExecState* exec, int k, JSValue value)
88 {
89     return makeString(valueToSourceString(exec, value), "(@k", String::number(k - FirstConstantRegisterIndex), ")").utf8();
90 }
91
92 static CString idName(int id0, const Identifier& ident)
93 {
94     return makeString(ident.string(), "(@id", String::number(id0), ")").utf8();
95 }
96
97 void CodeBlock::dumpBytecodeCommentAndNewLine(int location)
98 {
99 #if ENABLE(BYTECODE_COMMENTS)
100     const char* comment = commentForBytecodeOffset(location);
101     if (comment)
102         dataLogF("\t\t ; %s", comment);
103 #else
104     UNUSED_PARAM(location);
105 #endif
106     dataLogF("\n");
107 }
108
109 CString CodeBlock::registerName(ExecState* exec, int r) const
110 {
111     if (r == missingThisObjectMarker())
112         return "<null>";
113
114     if (isConstantRegisterIndex(r))
115         return constantName(exec, r, getConstant(r));
116
117     return makeString("r", String::number(r)).utf8();
118 }
119
120 static String regexpToSourceString(RegExp* regExp)
121 {
122     char postfix[5] = { '/', 0, 0, 0, 0 };
123     int index = 1;
124     if (regExp->global())
125         postfix[index++] = 'g';
126     if (regExp->ignoreCase())
127         postfix[index++] = 'i';
128     if (regExp->multiline())
129         postfix[index] = 'm';
130
131     return makeString("/", regExp->pattern(), postfix);
132 }
133
134 static CString regexpName(int re, RegExp* regexp)
135 {
136     return makeString(regexpToSourceString(regexp), "(@re", String::number(re), ")").utf8();
137 }
138
139 static String pointerToSourceString(void* p)
140 {
141     char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
142     snprintf(buffer, sizeof(buffer), "%p", p);
143     return buffer;
144 }
145
146 NEVER_INLINE static const char* debugHookName(int debugHookID)
147 {
148     switch (static_cast<DebugHookID>(debugHookID)) {
149         case DidEnterCallFrame:
150             return "didEnterCallFrame";
151         case WillLeaveCallFrame:
152             return "willLeaveCallFrame";
153         case WillExecuteStatement:
154             return "willExecuteStatement";
155         case WillExecuteProgram:
156             return "willExecuteProgram";
157         case DidExecuteProgram:
158             return "didExecuteProgram";
159         case DidReachBreakpoint:
160             return "didReachBreakpoint";
161     }
162
163     ASSERT_NOT_REACHED();
164     return "";
165 }
166
167 void CodeBlock::printUnaryOp(ExecState* exec, int location, const Instruction*& it, const char* op)
168 {
169     int r0 = (++it)->u.operand;
170     int r1 = (++it)->u.operand;
171
172     dataLogF("[%4d] %s\t\t %s, %s", location, op, registerName(exec, r0).data(), registerName(exec, r1).data());
173     dumpBytecodeCommentAndNewLine(location);
174 }
175
176 void CodeBlock::printBinaryOp(ExecState* exec, int location, const Instruction*& it, const char* op)
177 {
178     int r0 = (++it)->u.operand;
179     int r1 = (++it)->u.operand;
180     int r2 = (++it)->u.operand;
181     dataLogF("[%4d] %s\t\t %s, %s, %s", location, op, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
182     dumpBytecodeCommentAndNewLine(location);
183 }
184
185 void CodeBlock::printConditionalJump(ExecState* exec, const Instruction*, const Instruction*& it, int location, const char* op)
186 {
187     int r0 = (++it)->u.operand;
188     int offset = (++it)->u.operand;
189     dataLogF("[%4d] %s\t\t %s, %d(->%d)", location, op, registerName(exec, r0).data(), offset, location + offset);
190     dumpBytecodeCommentAndNewLine(location);
191 }
192
193 void CodeBlock::printGetByIdOp(ExecState* exec, int location, const Instruction*& it)
194 {
195     const char* op;
196     switch (exec->interpreter()->getOpcodeID(it->u.opcode)) {
197     case op_get_by_id:
198         op = "get_by_id";
199         break;
200     case op_get_by_id_out_of_line:
201         op = "get_by_id_out_of_line";
202         break;
203     case op_get_by_id_self:
204         op = "get_by_id_self";
205         break;
206     case op_get_by_id_proto:
207         op = "get_by_id_proto";
208         break;
209     case op_get_by_id_chain:
210         op = "get_by_id_chain";
211         break;
212     case op_get_by_id_getter_self:
213         op = "get_by_id_getter_self";
214         break;
215     case op_get_by_id_getter_proto:
216         op = "get_by_id_getter_proto";
217         break;
218     case op_get_by_id_getter_chain:
219         op = "get_by_id_getter_chain";
220         break;
221     case op_get_by_id_custom_self:
222         op = "get_by_id_custom_self";
223         break;
224     case op_get_by_id_custom_proto:
225         op = "get_by_id_custom_proto";
226         break;
227     case op_get_by_id_custom_chain:
228         op = "get_by_id_custom_chain";
229         break;
230     case op_get_by_id_generic:
231         op = "get_by_id_generic";
232         break;
233     case op_get_array_length:
234         op = "array_length";
235         break;
236     case op_get_string_length:
237         op = "string_length";
238         break;
239     default:
240         ASSERT_NOT_REACHED();
241         op = 0;
242     }
243     int r0 = (++it)->u.operand;
244     int r1 = (++it)->u.operand;
245     int id0 = (++it)->u.operand;
246     dataLogF("[%4d] %s\t %s, %s, %s", location, op, registerName(exec, r0).data(), registerName(exec, r1).data(), idName(id0, m_identifiers[id0]).data());
247     it += 5;
248 }
249
250 #if ENABLE(JIT) || ENABLE(LLINT) // unused in some configurations
251 static void dumpStructure(const char* name, ExecState* exec, Structure* structure, Identifier& ident)
252 {
253     if (!structure)
254         return;
255     
256     dataLogF("%s = %p", name, structure);
257     
258     PropertyOffset offset = structure->get(exec->globalData(), ident);
259     if (offset != invalidOffset)
260         dataLogF(" (offset = %d)", offset);
261 }
262 #endif
263
264 #if ENABLE(JIT) // unused when not ENABLE(JIT), leading to silly warnings
265 static void dumpChain(ExecState* exec, StructureChain* chain, Identifier& ident)
266 {
267     dataLogF("chain = %p: [", chain);
268     bool first = true;
269     for (WriteBarrier<Structure>* currentStructure = chain->head();
270          *currentStructure;
271          ++currentStructure) {
272         if (first)
273             first = false;
274         else
275             dataLogF(", ");
276         dumpStructure("struct", exec, currentStructure->get(), ident);
277     }
278     dataLogF("]");
279 }
280 #endif
281
282 void CodeBlock::printGetByIdCacheStatus(ExecState* exec, int location)
283 {
284     Instruction* instruction = instructions().begin() + location;
285
286     Identifier& ident = identifier(instruction[3].u.operand);
287     
288     UNUSED_PARAM(ident); // tell the compiler to shut up in certain platform configurations.
289     
290 #if ENABLE(LLINT)
291     if (exec->interpreter()->getOpcodeID(instruction[0].u.opcode) == op_get_array_length)
292         dataLogF(" llint(array_length)");
293     else {
294         Structure* structure = instruction[4].u.structure.get();
295         dataLogF(" llint(");
296         dumpStructure("struct", exec, structure, ident);
297         dataLogF(")");
298     }
299 #endif
300
301 #if ENABLE(JIT)
302     if (numberOfStructureStubInfos()) {
303         dataLogF(" jit(");
304         StructureStubInfo& stubInfo = getStubInfo(location);
305         if (!stubInfo.seen)
306             dataLogF("not seen");
307         else {
308             Structure* baseStructure = 0;
309             Structure* prototypeStructure = 0;
310             StructureChain* chain = 0;
311             PolymorphicAccessStructureList* structureList = 0;
312             int listSize = 0;
313             
314             switch (stubInfo.accessType) {
315             case access_get_by_id_self:
316                 dataLogF("self");
317                 baseStructure = stubInfo.u.getByIdSelf.baseObjectStructure.get();
318                 break;
319             case access_get_by_id_proto:
320                 dataLogF("proto");
321                 baseStructure = stubInfo.u.getByIdProto.baseObjectStructure.get();
322                 prototypeStructure = stubInfo.u.getByIdProto.prototypeStructure.get();
323                 break;
324             case access_get_by_id_chain:
325                 dataLogF("chain");
326                 baseStructure = stubInfo.u.getByIdChain.baseObjectStructure.get();
327                 chain = stubInfo.u.getByIdChain.chain.get();
328                 break;
329             case access_get_by_id_self_list:
330                 dataLogF("self_list");
331                 structureList = stubInfo.u.getByIdSelfList.structureList;
332                 listSize = stubInfo.u.getByIdSelfList.listSize;
333                 break;
334             case access_get_by_id_proto_list:
335                 dataLogF("proto_list");
336                 structureList = stubInfo.u.getByIdProtoList.structureList;
337                 listSize = stubInfo.u.getByIdProtoList.listSize;
338                 break;
339             case access_unset:
340                 dataLogF("unset");
341                 break;
342             case access_get_by_id_generic:
343                 dataLogF("generic");
344                 break;
345             case access_get_array_length:
346                 dataLogF("array_length");
347                 break;
348             case access_get_string_length:
349                 dataLogF("string_length");
350                 break;
351             default:
352                 ASSERT_NOT_REACHED();
353                 break;
354             }
355             
356             if (baseStructure) {
357                 dataLogF(", ");
358                 dumpStructure("struct", exec, baseStructure, ident);
359             }
360             
361             if (prototypeStructure) {
362                 dataLogF(", ");
363                 dumpStructure("prototypeStruct", exec, baseStructure, ident);
364             }
365             
366             if (chain) {
367                 dataLogF(", ");
368                 dumpChain(exec, chain, ident);
369             }
370             
371             if (structureList) {
372                 dataLogF(", list = %p: [", structureList);
373                 for (int i = 0; i < listSize; ++i) {
374                     if (i)
375                         dataLogF(", ");
376                     dataLogF("(");
377                     dumpStructure("base", exec, structureList->list[i].base.get(), ident);
378                     if (structureList->list[i].isChain) {
379                         if (structureList->list[i].u.chain.get()) {
380                             dataLogF(", ");
381                             dumpChain(exec, structureList->list[i].u.chain.get(), ident);
382                         }
383                     } else {
384                         if (structureList->list[i].u.proto.get()) {
385                             dataLogF(", ");
386                             dumpStructure("proto", exec, structureList->list[i].u.proto.get(), ident);
387                         }
388                     }
389                     dataLogF(")");
390                 }
391                 dataLogF("]");
392             }
393         }
394         dataLogF(")");
395     }
396 #endif
397 }
398
399 void CodeBlock::printCallOp(ExecState* exec, int location, const Instruction*& it, const char* op, CacheDumpMode cacheDumpMode)
400 {
401     int func = (++it)->u.operand;
402     int argCount = (++it)->u.operand;
403     int registerOffset = (++it)->u.operand;
404     dataLogF("[%4d] %s\t %s, %d, %d", location, op, registerName(exec, func).data(), argCount, registerOffset);
405     if (cacheDumpMode == DumpCaches) {
406 #if ENABLE(LLINT)
407         LLIntCallLinkInfo* callLinkInfo = it[1].u.callLinkInfo;
408         if (callLinkInfo->lastSeenCallee) {
409             dataLogF(" llint(%p, exec %p)",
410                     callLinkInfo->lastSeenCallee.get(),
411                     callLinkInfo->lastSeenCallee->executable());
412         } else
413             dataLogF(" llint(not set)");
414 #endif
415 #if ENABLE(JIT)
416         if (numberOfCallLinkInfos()) {
417             JSFunction* target = getCallLinkInfo(location).lastSeenCallee.get();
418             if (target)
419                 dataLogF(" jit(%p, exec %p)", target, target->executable());
420             else
421                 dataLogF(" jit(not set)");
422         }
423 #endif
424     }
425     dumpBytecodeCommentAndNewLine(location);
426     it += 2;
427 }
428
429 void CodeBlock::printPutByIdOp(ExecState* exec, int location, const Instruction*& it, const char* op)
430 {
431     int r0 = (++it)->u.operand;
432     int id0 = (++it)->u.operand;
433     int r1 = (++it)->u.operand;
434     dataLogF("[%4d] %s\t %s, %s, %s", location, op, registerName(exec, r0).data(), idName(id0, m_identifiers[id0]).data(), registerName(exec, r1).data());
435     dumpBytecodeCommentAndNewLine(location);
436     it += 5;
437 }
438
439 void CodeBlock::printStructure(const char* name, const Instruction* vPC, int operand)
440 {
441     unsigned instructionOffset = vPC - instructions().begin();
442     dataLogF("  [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structure).utf8().data());
443 }
444
445 void CodeBlock::printStructures(const Instruction* vPC)
446 {
447     Interpreter* interpreter = m_globalData->interpreter;
448     unsigned instructionOffset = vPC - instructions().begin();
449
450     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id)) {
451         printStructure("get_by_id", vPC, 4);
452         return;
453     }
454     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
455         printStructure("get_by_id_self", vPC, 4);
456         return;
457     }
458     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
459         dataLogF("  [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_proto", pointerToSourceString(vPC[4].u.structure).utf8().data(), pointerToSourceString(vPC[5].u.structure).utf8().data());
460         return;
461     }
462     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
463         dataLogF("  [%4d] %s: %s, %s, %s\n", instructionOffset, "put_by_id_transition", pointerToSourceString(vPC[4].u.structure).utf8().data(), pointerToSourceString(vPC[5].u.structure).utf8().data(), pointerToSourceString(vPC[6].u.structureChain).utf8().data());
464         return;
465     }
466     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
467         dataLogF("  [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_chain", pointerToSourceString(vPC[4].u.structure).utf8().data(), pointerToSourceString(vPC[5].u.structureChain).utf8().data());
468         return;
469     }
470     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id)) {
471         printStructure("put_by_id", vPC, 4);
472         return;
473     }
474     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
475         printStructure("put_by_id_replace", vPC, 4);
476         return;
477     }
478
479     // These m_instructions doesn't ref Structures.
480     ASSERT(vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_call) || vPC[0].u.opcode == interpreter->getOpcode(op_call_eval) || vPC[0].u.opcode == interpreter->getOpcode(op_construct));
481 }
482
483 void CodeBlock::dump()
484 {
485     // We only use the ExecState* for things that don't actually lead to JS execution,
486     // like converting a JSString to a String. Hence the globalExec is appropriate.
487     ExecState* exec = m_globalObject->globalExec();
488     
489     size_t instructionCount = 0;
490
491     for (size_t i = 0; i < instructions().size(); i += opcodeLengths[exec->interpreter()->getOpcodeID(instructions()[i].u.opcode)])
492         ++instructionCount;
493
494     dataLogF(
495         "%lu m_instructions; %lu bytes at %p (%s); %d parameter(s); %d callee register(s); %d variable(s)",
496         static_cast<unsigned long>(instructions().size()),
497         static_cast<unsigned long>(instructions().size() * sizeof(Instruction)),
498         this, codeTypeToString(codeType()), m_numParameters, m_numCalleeRegisters,
499         m_numVars);
500     if (symbolTable()->captureCount())
501         dataLogF("; %d captured var(s)", symbolTable()->captureCount());
502     if (usesArguments()) {
503         dataLogF(
504             "; uses arguments, in r%d, r%d",
505             argumentsRegister(),
506             unmodifiedArgumentsRegister(argumentsRegister()));
507     }
508     if (needsFullScopeChain() && codeType() == FunctionCode)
509         dataLogF("; activation in r%d", activationRegister());
510     dataLogF("\n\n");
511
512     const Instruction* begin = instructions().begin();
513     const Instruction* end = instructions().end();
514     for (const Instruction* it = begin; it != end; ++it)
515         dump(exec, begin, it);
516
517     if (!m_identifiers.isEmpty()) {
518         dataLogF("\nIdentifiers:\n");
519         size_t i = 0;
520         do {
521             dataLogF("  id%u = %s\n", static_cast<unsigned>(i), m_identifiers[i].string().utf8().data());
522             ++i;
523         } while (i != m_identifiers.size());
524     }
525
526     if (!m_constantRegisters.isEmpty()) {
527         dataLogF("\nConstants:\n");
528         size_t i = 0;
529         do {
530             dataLogF("   k%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, m_constantRegisters[i].get()).utf8().data());
531             ++i;
532         } while (i < m_constantRegisters.size());
533     }
534
535     if (size_t count = m_unlinkedCode->numberOfRegExps()) {
536         dataLogF("\nm_regexps:\n");
537         size_t i = 0;
538         do {
539             dataLogF("  re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(m_unlinkedCode->regexp(i)).utf8().data());
540             ++i;
541         } while (i < count);
542     }
543
544 #if ENABLE(JIT)
545     if (!m_structureStubInfos.isEmpty())
546         dataLogF("\nStructures:\n");
547 #endif
548
549     if (m_rareData && !m_rareData->m_exceptionHandlers.isEmpty()) {
550         dataLogF("\nException Handlers:\n");
551         unsigned i = 0;
552         do {
553             dataLogF("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, m_rareData->m_exceptionHandlers[i].start, m_rareData->m_exceptionHandlers[i].end, m_rareData->m_exceptionHandlers[i].target);
554             ++i;
555         } while (i < m_rareData->m_exceptionHandlers.size());
556     }
557     
558     if (m_rareData && !m_rareData->m_immediateSwitchJumpTables.isEmpty()) {
559         dataLogF("Immediate Switch Jump Tables:\n");
560         unsigned i = 0;
561         do {
562             dataLogF("  %1d = {\n", i);
563             int entry = 0;
564             Vector<int32_t>::const_iterator end = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.end();
565             for (Vector<int32_t>::const_iterator iter = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
566                 if (!*iter)
567                     continue;
568                 dataLogF("\t\t%4d => %04d\n", entry + m_rareData->m_immediateSwitchJumpTables[i].min, *iter);
569             }
570             dataLogF("      }\n");
571             ++i;
572         } while (i < m_rareData->m_immediateSwitchJumpTables.size());
573     }
574     
575     if (m_rareData && !m_rareData->m_characterSwitchJumpTables.isEmpty()) {
576         dataLogF("\nCharacter Switch Jump Tables:\n");
577         unsigned i = 0;
578         do {
579             dataLogF("  %1d = {\n", i);
580             int entry = 0;
581             Vector<int32_t>::const_iterator end = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.end();
582             for (Vector<int32_t>::const_iterator iter = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
583                 if (!*iter)
584                     continue;
585                 ASSERT(!((i + m_rareData->m_characterSwitchJumpTables[i].min) & ~0xFFFF));
586                 UChar ch = static_cast<UChar>(entry + m_rareData->m_characterSwitchJumpTables[i].min);
587                 dataLogF("\t\t\"%s\" => %04d\n", String(&ch, 1).utf8().data(), *iter);
588         }
589             dataLogF("      }\n");
590             ++i;
591         } while (i < m_rareData->m_characterSwitchJumpTables.size());
592     }
593     
594     if (m_rareData && !m_rareData->m_stringSwitchJumpTables.isEmpty()) {
595         dataLogF("\nString Switch Jump Tables:\n");
596         unsigned i = 0;
597         do {
598             dataLogF("  %1d = {\n", i);
599             StringJumpTable::StringOffsetTable::const_iterator end = m_rareData->m_stringSwitchJumpTables[i].offsetTable.end();
600             for (StringJumpTable::StringOffsetTable::const_iterator iter = m_rareData->m_stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
601                 dataLogF("\t\t\"%s\" => %04d\n", String(iter->key).utf8().data(), iter->value.branchOffset);
602             dataLogF("      }\n");
603             ++i;
604         } while (i < m_rareData->m_stringSwitchJumpTables.size());
605     }
606
607     dataLogF("\n");
608 }
609
610 void CodeBlock::dump(ExecState* exec, const Instruction* begin, const Instruction*& it)
611 {
612     int location = it - begin;
613     switch (exec->interpreter()->getOpcodeID(it->u.opcode)) {
614         case op_enter: {
615             dataLogF("[%4d] enter", location);
616             dumpBytecodeCommentAndNewLine(location);
617             break;
618         }
619         case op_create_activation: {
620             int r0 = (++it)->u.operand;
621             dataLogF("[%4d] create_activation %s", location, registerName(exec, r0).data());
622             dumpBytecodeCommentAndNewLine(location);
623             break;
624         }
625         case op_create_arguments: {
626             int r0 = (++it)->u.operand;
627             dataLogF("[%4d] create_arguments\t %s", location, registerName(exec, r0).data());
628             dumpBytecodeCommentAndNewLine(location);
629             break;
630         }
631         case op_init_lazy_reg: {
632             int r0 = (++it)->u.operand;
633             dataLogF("[%4d] init_lazy_reg\t %s", location, registerName(exec, r0).data());
634             dumpBytecodeCommentAndNewLine(location);
635             break;
636         }
637         case op_get_callee: {
638             int r0 = (++it)->u.operand;
639             dataLogF("[%4d] op_get_callee %s\n", location, registerName(exec, r0).data());
640             ++it;
641             break;
642         }
643         case op_create_this: {
644             int r0 = (++it)->u.operand;
645             int r1 = (++it)->u.operand;
646             dataLogF("[%4d] create_this %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data());
647             dumpBytecodeCommentAndNewLine(location);
648             break;
649         }
650         case op_convert_this: {
651             int r0 = (++it)->u.operand;
652             dataLogF("[%4d] convert_this\t %s", location, registerName(exec, r0).data());
653             dumpBytecodeCommentAndNewLine(location);
654             ++it; // Skip value profile.
655             break;
656         }
657         case op_new_object: {
658             int r0 = (++it)->u.operand;
659             dataLogF("[%4d] new_object\t %s", location, registerName(exec, r0).data());
660             dumpBytecodeCommentAndNewLine(location);
661             break;
662         }
663         case op_new_array: {
664             int dst = (++it)->u.operand;
665             int argv = (++it)->u.operand;
666             int argc = (++it)->u.operand;
667             dataLogF("[%4d] new_array\t %s, %s, %d", location, registerName(exec, dst).data(), registerName(exec, argv).data(), argc);
668             dumpBytecodeCommentAndNewLine(location);
669             ++it; // Skip array allocation profile.
670             break;
671         }
672         case op_new_array_with_size: {
673             int dst = (++it)->u.operand;
674             int length = (++it)->u.operand;
675             dataLogF("[%4d] new_array_with_size\t %s, %s", location, registerName(exec, dst).data(), registerName(exec, length).data());
676             dumpBytecodeCommentAndNewLine(location);
677             ++it; // Skip array allocation profile.
678             break;
679         }
680         case op_new_array_buffer: {
681             int dst = (++it)->u.operand;
682             int argv = (++it)->u.operand;
683             int argc = (++it)->u.operand;
684             dataLogF("[%4d] new_array_buffer\t %s, %d, %d", location, registerName(exec, dst).data(), argv, argc);
685             dumpBytecodeCommentAndNewLine(location);
686             ++it; // Skip array allocation profile.
687             break;
688         }
689         case op_new_regexp: {
690             int r0 = (++it)->u.operand;
691             int re0 = (++it)->u.operand;
692             dataLogF("[%4d] new_regexp\t %s, ", location, registerName(exec, r0).data());
693             if (r0 >=0 && r0 < (int)m_unlinkedCode->numberOfRegExps())
694                 dataLogF("%s", regexpName(re0, regexp(re0)).data());
695             else
696                 dataLogF("bad_regexp(%d)", re0);
697             dumpBytecodeCommentAndNewLine(location);
698             break;
699         }
700         case op_mov: {
701             int r0 = (++it)->u.operand;
702             int r1 = (++it)->u.operand;
703             dataLogF("[%4d] mov\t\t %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data());
704             dumpBytecodeCommentAndNewLine(location);
705             break;
706         }
707         case op_not: {
708             printUnaryOp(exec, location, it, "not");
709             break;
710         }
711         case op_eq: {
712             printBinaryOp(exec, location, it, "eq");
713             break;
714         }
715         case op_eq_null: {
716             printUnaryOp(exec, location, it, "eq_null");
717             break;
718         }
719         case op_neq: {
720             printBinaryOp(exec, location, it, "neq");
721             break;
722         }
723         case op_neq_null: {
724             printUnaryOp(exec, location, it, "neq_null");
725             break;
726         }
727         case op_stricteq: {
728             printBinaryOp(exec, location, it, "stricteq");
729             break;
730         }
731         case op_nstricteq: {
732             printBinaryOp(exec, location, it, "nstricteq");
733             break;
734         }
735         case op_less: {
736             printBinaryOp(exec, location, it, "less");
737             break;
738         }
739         case op_lesseq: {
740             printBinaryOp(exec, location, it, "lesseq");
741             break;
742         }
743         case op_greater: {
744             printBinaryOp(exec, location, it, "greater");
745             break;
746         }
747         case op_greatereq: {
748             printBinaryOp(exec, location, it, "greatereq");
749             break;
750         }
751         case op_pre_inc: {
752             int r0 = (++it)->u.operand;
753             dataLogF("[%4d] pre_inc\t\t %s", location, registerName(exec, r0).data());
754             dumpBytecodeCommentAndNewLine(location);
755             break;
756         }
757         case op_pre_dec: {
758             int r0 = (++it)->u.operand;
759             dataLogF("[%4d] pre_dec\t\t %s", location, registerName(exec, r0).data());
760             dumpBytecodeCommentAndNewLine(location);
761             break;
762         }
763         case op_post_inc: {
764             printUnaryOp(exec, location, it, "post_inc");
765             break;
766         }
767         case op_post_dec: {
768             printUnaryOp(exec, location, it, "post_dec");
769             break;
770         }
771         case op_to_jsnumber: {
772             printUnaryOp(exec, location, it, "to_jsnumber");
773             break;
774         }
775         case op_negate: {
776             printUnaryOp(exec, location, it, "negate");
777             break;
778         }
779         case op_add: {
780             printBinaryOp(exec, location, it, "add");
781             ++it;
782             break;
783         }
784         case op_mul: {
785             printBinaryOp(exec, location, it, "mul");
786             ++it;
787             break;
788         }
789         case op_div: {
790             printBinaryOp(exec, location, it, "div");
791             ++it;
792             break;
793         }
794         case op_mod: {
795             printBinaryOp(exec, location, it, "mod");
796             break;
797         }
798         case op_sub: {
799             printBinaryOp(exec, location, it, "sub");
800             ++it;
801             break;
802         }
803         case op_lshift: {
804             printBinaryOp(exec, location, it, "lshift");
805             break;            
806         }
807         case op_rshift: {
808             printBinaryOp(exec, location, it, "rshift");
809             break;
810         }
811         case op_urshift: {
812             printBinaryOp(exec, location, it, "urshift");
813             break;
814         }
815         case op_bitand: {
816             printBinaryOp(exec, location, it, "bitand");
817             ++it;
818             break;
819         }
820         case op_bitxor: {
821             printBinaryOp(exec, location, it, "bitxor");
822             ++it;
823             break;
824         }
825         case op_bitor: {
826             printBinaryOp(exec, location, it, "bitor");
827             ++it;
828             break;
829         }
830         case op_check_has_instance: {
831             int r0 = (++it)->u.operand;
832             int r1 = (++it)->u.operand;
833             int r2 = (++it)->u.operand;
834             int offset = (++it)->u.operand;
835             dataLogF("[%4d] check_has_instance\t\t %s, %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data(), offset, location + offset);
836             dumpBytecodeCommentAndNewLine(location);
837             break;
838         }
839         case op_instanceof: {
840             int r0 = (++it)->u.operand;
841             int r1 = (++it)->u.operand;
842             int r2 = (++it)->u.operand;
843             dataLogF("[%4d] instanceof\t\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
844             dumpBytecodeCommentAndNewLine(location);
845             break;
846         }
847         case op_typeof: {
848             printUnaryOp(exec, location, it, "typeof");
849             break;
850         }
851         case op_is_undefined: {
852             printUnaryOp(exec, location, it, "is_undefined");
853             break;
854         }
855         case op_is_boolean: {
856             printUnaryOp(exec, location, it, "is_boolean");
857             break;
858         }
859         case op_is_number: {
860             printUnaryOp(exec, location, it, "is_number");
861             break;
862         }
863         case op_is_string: {
864             printUnaryOp(exec, location, it, "is_string");
865             break;
866         }
867         case op_is_object: {
868             printUnaryOp(exec, location, it, "is_object");
869             break;
870         }
871         case op_is_function: {
872             printUnaryOp(exec, location, it, "is_function");
873             break;
874         }
875         case op_in: {
876             printBinaryOp(exec, location, it, "in");
877             break;
878         }
879         case op_put_to_base_variable:
880         case op_put_to_base: {
881             int base = (++it)->u.operand;
882             int id0 = (++it)->u.operand;
883             int value = (++it)->u.operand;
884             int resolveInfo = (++it)->u.operand;
885             dataLogF("[%4d] put_to_base\t %s, %s, %s, %d", location, registerName(exec, base).data(), idName(id0, m_identifiers[id0]).data(), registerName(exec, value).data(), resolveInfo);
886             dumpBytecodeCommentAndNewLine(location);
887             break;
888         }
889         case op_resolve:
890         case op_resolve_global_property:
891         case op_resolve_global_var:
892         case op_resolve_scoped_var:
893         case op_resolve_scoped_var_on_top_scope:
894         case op_resolve_scoped_var_with_top_scope_check: {
895             int r0 = (++it)->u.operand;
896             int id0 = (++it)->u.operand;
897             int resolveInfo = (++it)->u.operand;
898             dataLogF("[%4d] resolve\t\t %s, %s, %d", location, registerName(exec, r0).data(), idName(id0, m_identifiers[id0]).data(), resolveInfo);
899             dumpBytecodeCommentAndNewLine(location);
900             it++;
901             break;
902         }
903         case op_init_global_const_nop: {
904             dataLogF("[%4d] init_global_const_nop\t", location);
905             dumpBytecodeCommentAndNewLine(location);
906             it++;
907             it++;
908             it++;
909             it++;
910             break;
911         }
912         case op_init_global_const: {
913             WriteBarrier<Unknown>* registerPointer = (++it)->u.registerPointer;
914             int r0 = (++it)->u.operand;
915             dataLogF("[%4d] init_global_const\t g%d(%p), %s", location, m_globalObject->findRegisterIndex(registerPointer), registerPointer, registerName(exec, r0).data());
916             dumpBytecodeCommentAndNewLine(location);
917             it++;
918             it++;
919             break;
920         }
921         case op_init_global_const_check: {
922             WriteBarrier<Unknown>* registerPointer = (++it)->u.registerPointer;
923             int r0 = (++it)->u.operand;
924             dataLogF("[%4d] init_global_const_check\t g%d(%p), %s", location, m_globalObject->findRegisterIndex(registerPointer), registerPointer, registerName(exec, r0).data());
925             dumpBytecodeCommentAndNewLine(location);
926             it++;
927             it++;
928             break;
929         }
930         case op_resolve_base_to_global:
931         case op_resolve_base_to_global_dynamic:
932         case op_resolve_base_to_scope:
933         case op_resolve_base_to_scope_with_top_scope_check:
934         case op_resolve_base: {
935             int r0 = (++it)->u.operand;
936             int id0 = (++it)->u.operand;
937             int isStrict = (++it)->u.operand;
938             int resolveInfo = (++it)->u.operand;
939             int putToBaseInfo = (++it)->u.operand;
940             dataLogF("[%4d] resolve_base%s\t %s, %s, %d, %d", location, isStrict ? "_strict" : "", registerName(exec, r0).data(), idName(id0, m_identifiers[id0]).data(), resolveInfo, putToBaseInfo);
941             dumpBytecodeCommentAndNewLine(location);
942             it++;
943             break;
944         }
945         case op_ensure_property_exists: {
946             int r0 = (++it)->u.operand;
947             int id0 = (++it)->u.operand;
948             dataLogF("[%4d] ensure_property_exists\t %s, %s", location, registerName(exec, r0).data(), idName(id0, m_identifiers[id0]).data());
949             dumpBytecodeCommentAndNewLine(location);
950             break;
951         }
952         case op_resolve_with_base: {
953             int r0 = (++it)->u.operand;
954             int r1 = (++it)->u.operand;
955             int id0 = (++it)->u.operand;
956             int resolveInfo = (++it)->u.operand;
957             int putToBaseInfo = (++it)->u.operand;
958             dataLogF("[%4d] resolve_with_base %s, %s, %s, %d, %d", location, registerName(exec, r0).data(), registerName(exec, r1).data(), idName(id0, m_identifiers[id0]).data(), resolveInfo, putToBaseInfo);
959             dumpBytecodeCommentAndNewLine(location);
960             it++;
961             break;
962         }
963         case op_resolve_with_this: {
964             int r0 = (++it)->u.operand;
965             int r1 = (++it)->u.operand;
966             int id0 = (++it)->u.operand;
967             int resolveInfo = (++it)->u.operand;
968             dataLogF("[%4d] resolve_with_this %s, %s, %s, %d", location, registerName(exec, r0).data(), registerName(exec, r1).data(), idName(id0, m_identifiers[id0]).data(), resolveInfo);
969             dumpBytecodeCommentAndNewLine(location);
970             it++;
971             break;
972         }
973         case op_get_by_id:
974         case op_get_by_id_out_of_line:
975         case op_get_by_id_self:
976         case op_get_by_id_proto:
977         case op_get_by_id_chain:
978         case op_get_by_id_getter_self:
979         case op_get_by_id_getter_proto:
980         case op_get_by_id_getter_chain:
981         case op_get_by_id_custom_self:
982         case op_get_by_id_custom_proto:
983         case op_get_by_id_custom_chain:
984         case op_get_by_id_generic:
985         case op_get_array_length:
986         case op_get_string_length: {
987             printGetByIdOp(exec, location, it);
988             printGetByIdCacheStatus(exec, location);
989             dumpBytecodeCommentAndNewLine(location);
990             break;
991         }
992         case op_get_arguments_length: {
993             printUnaryOp(exec, location, it, "get_arguments_length");
994             it++;
995             break;
996         }
997         case op_put_by_id: {
998             printPutByIdOp(exec, location, it, "put_by_id");
999             break;
1000         }
1001         case op_put_by_id_out_of_line: {
1002             printPutByIdOp(exec, location, it, "put_by_id_out_of_line");
1003             break;
1004         }
1005         case op_put_by_id_replace: {
1006             printPutByIdOp(exec, location, it, "put_by_id_replace");
1007             break;
1008         }
1009         case op_put_by_id_transition: {
1010             printPutByIdOp(exec, location, it, "put_by_id_transition");
1011             break;
1012         }
1013         case op_put_by_id_transition_direct: {
1014             printPutByIdOp(exec, location, it, "put_by_id_transition_direct");
1015             break;
1016         }
1017         case op_put_by_id_transition_direct_out_of_line: {
1018             printPutByIdOp(exec, location, it, "put_by_id_transition_direct_out_of_line");
1019             break;
1020         }
1021         case op_put_by_id_transition_normal: {
1022             printPutByIdOp(exec, location, it, "put_by_id_transition_normal");
1023             break;
1024         }
1025         case op_put_by_id_transition_normal_out_of_line: {
1026             printPutByIdOp(exec, location, it, "put_by_id_transition_normal_out_of_line");
1027             break;
1028         }
1029         case op_put_by_id_generic: {
1030             printPutByIdOp(exec, location, it, "put_by_id_generic");
1031             break;
1032         }
1033         case op_put_getter_setter: {
1034             int r0 = (++it)->u.operand;
1035             int id0 = (++it)->u.operand;
1036             int r1 = (++it)->u.operand;
1037             int r2 = (++it)->u.operand;
1038             dataLogF("[%4d] put_getter_setter\t %s, %s, %s, %s", location, registerName(exec, r0).data(), idName(id0, m_identifiers[id0]).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
1039             dumpBytecodeCommentAndNewLine(location);
1040             break;
1041         }
1042         case op_del_by_id: {
1043             int r0 = (++it)->u.operand;
1044             int r1 = (++it)->u.operand;
1045             int id0 = (++it)->u.operand;
1046             dataLogF("[%4d] del_by_id\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), idName(id0, m_identifiers[id0]).data());
1047             dumpBytecodeCommentAndNewLine(location);
1048             break;
1049         }
1050         case op_get_by_val: {
1051             int r0 = (++it)->u.operand;
1052             int r1 = (++it)->u.operand;
1053             int r2 = (++it)->u.operand;
1054             dataLogF("[%4d] get_by_val\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
1055             dumpBytecodeCommentAndNewLine(location);
1056             it++;
1057             it++;
1058             break;
1059         }
1060         case op_get_argument_by_val: {
1061             int r0 = (++it)->u.operand;
1062             int r1 = (++it)->u.operand;
1063             int r2 = (++it)->u.operand;
1064             dataLogF("[%4d] get_argument_by_val\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
1065             dumpBytecodeCommentAndNewLine(location);
1066             ++it;
1067             ++it;
1068             break;
1069         }
1070         case op_get_by_pname: {
1071             int r0 = (++it)->u.operand;
1072             int r1 = (++it)->u.operand;
1073             int r2 = (++it)->u.operand;
1074             int r3 = (++it)->u.operand;
1075             int r4 = (++it)->u.operand;
1076             int r5 = (++it)->u.operand;
1077             dataLogF("[%4d] get_by_pname\t %s, %s, %s, %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data(), registerName(exec, r3).data(), registerName(exec, r4).data(), registerName(exec, r5).data());
1078             dumpBytecodeCommentAndNewLine(location);
1079             break;
1080         }
1081         case op_put_by_val: {
1082             int r0 = (++it)->u.operand;
1083             int r1 = (++it)->u.operand;
1084             int r2 = (++it)->u.operand;
1085             dataLogF("[%4d] put_by_val\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
1086             dumpBytecodeCommentAndNewLine(location);
1087             ++it;
1088             break;
1089         }
1090         case op_del_by_val: {
1091             int r0 = (++it)->u.operand;
1092             int r1 = (++it)->u.operand;
1093             int r2 = (++it)->u.operand;
1094             dataLogF("[%4d] del_by_val\t %s, %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data());
1095             dumpBytecodeCommentAndNewLine(location);
1096             break;
1097         }
1098         case op_put_by_index: {
1099             int r0 = (++it)->u.operand;
1100             unsigned n0 = (++it)->u.operand;
1101             int r1 = (++it)->u.operand;
1102             dataLogF("[%4d] put_by_index\t %s, %u, %s", location, registerName(exec, r0).data(), n0, registerName(exec, r1).data());
1103             dumpBytecodeCommentAndNewLine(location);
1104             break;
1105         }
1106         case op_jmp: {
1107             int offset = (++it)->u.operand;
1108             dataLogF("[%4d] jmp\t\t %d(->%d)", location, offset, location + offset);
1109             dumpBytecodeCommentAndNewLine(location);
1110             break;
1111         }
1112         case op_loop: {
1113             int offset = (++it)->u.operand;
1114             dataLogF("[%4d] loop\t\t %d(->%d)", location, offset, location + offset);
1115             dumpBytecodeCommentAndNewLine(location);
1116             break;
1117         }
1118         case op_jtrue: {
1119             printConditionalJump(exec, begin, it, location, "jtrue");
1120             break;
1121         }
1122         case op_loop_if_true: {
1123             printConditionalJump(exec, begin, it, location, "loop_if_true");
1124             break;
1125         }
1126         case op_loop_if_false: {
1127             printConditionalJump(exec, begin, it, location, "loop_if_false");
1128             break;
1129         }
1130         case op_jfalse: {
1131             printConditionalJump(exec, begin, it, location, "jfalse");
1132             break;
1133         }
1134         case op_jeq_null: {
1135             printConditionalJump(exec, begin, it, location, "jeq_null");
1136             break;
1137         }
1138         case op_jneq_null: {
1139             printConditionalJump(exec, begin, it, location, "jneq_null");
1140             break;
1141         }
1142         case op_jneq_ptr: {
1143             int r0 = (++it)->u.operand;
1144             Special::Pointer pointer = (++it)->u.specialPointer;
1145             int offset = (++it)->u.operand;
1146             dataLogF("[%4d] jneq_ptr\t\t %s, %d (%p), %d(->%d)", location, registerName(exec, r0).data(), pointer, m_globalObject->actualPointerFor(pointer), offset, location + offset);
1147             dumpBytecodeCommentAndNewLine(location);
1148             break;
1149         }
1150         case op_jless: {
1151             int r0 = (++it)->u.operand;
1152             int r1 = (++it)->u.operand;
1153             int offset = (++it)->u.operand;
1154             dataLogF("[%4d] jless\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1155             dumpBytecodeCommentAndNewLine(location);
1156             break;
1157         }
1158         case op_jlesseq: {
1159             int r0 = (++it)->u.operand;
1160             int r1 = (++it)->u.operand;
1161             int offset = (++it)->u.operand;
1162             dataLogF("[%4d] jlesseq\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1163             dumpBytecodeCommentAndNewLine(location);
1164             break;
1165         }
1166         case op_jgreater: {
1167             int r0 = (++it)->u.operand;
1168             int r1 = (++it)->u.operand;
1169             int offset = (++it)->u.operand;
1170             dataLogF("[%4d] jgreater\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1171             dumpBytecodeCommentAndNewLine(location);
1172             break;
1173         }
1174         case op_jgreatereq: {
1175             int r0 = (++it)->u.operand;
1176             int r1 = (++it)->u.operand;
1177             int offset = (++it)->u.operand;
1178             dataLogF("[%4d] jgreatereq\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1179             dumpBytecodeCommentAndNewLine(location);
1180             break;
1181         }
1182         case op_jnless: {
1183             int r0 = (++it)->u.operand;
1184             int r1 = (++it)->u.operand;
1185             int offset = (++it)->u.operand;
1186             dataLogF("[%4d] jnless\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1187             dumpBytecodeCommentAndNewLine(location);
1188             break;
1189         }
1190         case op_jnlesseq: {
1191             int r0 = (++it)->u.operand;
1192             int r1 = (++it)->u.operand;
1193             int offset = (++it)->u.operand;
1194             dataLogF("[%4d] jnlesseq\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1195             dumpBytecodeCommentAndNewLine(location);
1196             break;
1197         }
1198         case op_jngreater: {
1199             int r0 = (++it)->u.operand;
1200             int r1 = (++it)->u.operand;
1201             int offset = (++it)->u.operand;
1202             dataLogF("[%4d] jngreater\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1203             dumpBytecodeCommentAndNewLine(location);
1204             break;
1205         }
1206         case op_jngreatereq: {
1207             int r0 = (++it)->u.operand;
1208             int r1 = (++it)->u.operand;
1209             int offset = (++it)->u.operand;
1210             dataLogF("[%4d] jngreatereq\t\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1211             dumpBytecodeCommentAndNewLine(location);
1212             break;
1213         }
1214         case op_loop_if_less: {
1215             int r0 = (++it)->u.operand;
1216             int r1 = (++it)->u.operand;
1217             int offset = (++it)->u.operand;
1218             dataLogF("[%4d] loop_if_less\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1219             dumpBytecodeCommentAndNewLine(location);
1220             break;
1221         }
1222         case op_loop_if_lesseq: {
1223             int r0 = (++it)->u.operand;
1224             int r1 = (++it)->u.operand;
1225             int offset = (++it)->u.operand;
1226             dataLogF("[%4d] loop_if_lesseq\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1227             dumpBytecodeCommentAndNewLine(location);
1228             break;
1229         }
1230         case op_loop_if_greater: {
1231             int r0 = (++it)->u.operand;
1232             int r1 = (++it)->u.operand;
1233             int offset = (++it)->u.operand;
1234             dataLogF("[%4d] loop_if_greater\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1235             dumpBytecodeCommentAndNewLine(location);
1236             break;
1237         }
1238         case op_loop_if_greatereq: {
1239             int r0 = (++it)->u.operand;
1240             int r1 = (++it)->u.operand;
1241             int offset = (++it)->u.operand;
1242             dataLogF("[%4d] loop_if_greatereq\t %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), offset, location + offset);
1243             dumpBytecodeCommentAndNewLine(location);
1244             break;
1245         }
1246         case op_loop_hint: {
1247             dataLogF("[%4d] loop_hint", location);
1248             dumpBytecodeCommentAndNewLine(location);
1249             break;
1250         }
1251         case op_switch_imm: {
1252             int tableIndex = (++it)->u.operand;
1253             int defaultTarget = (++it)->u.operand;
1254             int scrutineeRegister = (++it)->u.operand;
1255             dataLogF("[%4d] switch_imm\t %d, %d(->%d), %s", location, tableIndex, defaultTarget, location + defaultTarget, registerName(exec, scrutineeRegister).data());
1256             dumpBytecodeCommentAndNewLine(location);
1257             break;
1258         }
1259         case op_switch_char: {
1260             int tableIndex = (++it)->u.operand;
1261             int defaultTarget = (++it)->u.operand;
1262             int scrutineeRegister = (++it)->u.operand;
1263             dataLogF("[%4d] switch_char\t %d, %d(->%d), %s", location, tableIndex, defaultTarget, location + defaultTarget, registerName(exec, scrutineeRegister).data());
1264             dumpBytecodeCommentAndNewLine(location);
1265             break;
1266         }
1267         case op_switch_string: {
1268             int tableIndex = (++it)->u.operand;
1269             int defaultTarget = (++it)->u.operand;
1270             int scrutineeRegister = (++it)->u.operand;
1271             dataLogF("[%4d] switch_string\t %d, %d(->%d), %s", location, tableIndex, defaultTarget, location + defaultTarget, registerName(exec, scrutineeRegister).data());
1272             dumpBytecodeCommentAndNewLine(location);
1273             break;
1274         }
1275         case op_new_func: {
1276             int r0 = (++it)->u.operand;
1277             int f0 = (++it)->u.operand;
1278             int shouldCheck = (++it)->u.operand;
1279             dataLogF("[%4d] new_func\t\t %s, f%d, %s", location, registerName(exec, r0).data(), f0, shouldCheck ? "<Checked>" : "<Unchecked>");
1280             dumpBytecodeCommentAndNewLine(location);
1281             break;
1282         }
1283         case op_new_func_exp: {
1284             int r0 = (++it)->u.operand;
1285             int f0 = (++it)->u.operand;
1286             dataLogF("[%4d] new_func_exp\t %s, f%d", location, registerName(exec, r0).data(), f0);
1287             dumpBytecodeCommentAndNewLine(location);
1288             break;
1289         }
1290         case op_call: {
1291             printCallOp(exec, location, it, "call", DumpCaches);
1292             break;
1293         }
1294         case op_call_eval: {
1295             printCallOp(exec, location, it, "call_eval", DontDumpCaches);
1296             break;
1297         }
1298         case op_call_varargs: {
1299             int callee = (++it)->u.operand;
1300             int thisValue = (++it)->u.operand;
1301             int arguments = (++it)->u.operand;
1302             int firstFreeRegister = (++it)->u.operand;
1303             dataLogF("[%4d] call_varargs\t %s, %s, %s, %d", location, registerName(exec, callee).data(), registerName(exec, thisValue).data(), registerName(exec, arguments).data(), firstFreeRegister);
1304             dumpBytecodeCommentAndNewLine(location);
1305             break;
1306         }
1307         case op_tear_off_activation: {
1308             int r0 = (++it)->u.operand;
1309             dataLogF("[%4d] tear_off_activation\t %s", location, registerName(exec, r0).data());
1310             dumpBytecodeCommentAndNewLine(location);
1311             break;
1312         }
1313         case op_tear_off_arguments: {
1314             int r0 = (++it)->u.operand;
1315             int r1 = (++it)->u.operand;
1316             dataLogF("[%4d] tear_off_arguments %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data());
1317             dumpBytecodeCommentAndNewLine(location);
1318             break;
1319         }
1320         case op_ret: {
1321             int r0 = (++it)->u.operand;
1322             dataLogF("[%4d] ret\t\t %s", location, registerName(exec, r0).data());
1323             dumpBytecodeCommentAndNewLine(location);
1324             break;
1325         }
1326         case op_call_put_result: {
1327             int r0 = (++it)->u.operand;
1328             dataLogF("[%4d] call_put_result\t\t %s", location, registerName(exec, r0).data());
1329             dumpBytecodeCommentAndNewLine(location);
1330             it++;
1331             break;
1332         }
1333         case op_ret_object_or_this: {
1334             int r0 = (++it)->u.operand;
1335             int r1 = (++it)->u.operand;
1336             dataLogF("[%4d] constructor_ret\t\t %s %s", location, registerName(exec, r0).data(), registerName(exec, r1).data());
1337             dumpBytecodeCommentAndNewLine(location);
1338             break;
1339         }
1340         case op_construct: {
1341             printCallOp(exec, location, it, "construct", DumpCaches);
1342             break;
1343         }
1344         case op_strcat: {
1345             int r0 = (++it)->u.operand;
1346             int r1 = (++it)->u.operand;
1347             int count = (++it)->u.operand;
1348             dataLogF("[%4d] strcat\t\t %s, %s, %d", location, registerName(exec, r0).data(), registerName(exec, r1).data(), count);
1349             dumpBytecodeCommentAndNewLine(location);
1350             break;
1351         }
1352         case op_to_primitive: {
1353             int r0 = (++it)->u.operand;
1354             int r1 = (++it)->u.operand;
1355             dataLogF("[%4d] to_primitive\t %s, %s", location, registerName(exec, r0).data(), registerName(exec, r1).data());
1356             dumpBytecodeCommentAndNewLine(location);
1357             break;
1358         }
1359         case op_get_pnames: {
1360             int r0 = it[1].u.operand;
1361             int r1 = it[2].u.operand;
1362             int r2 = it[3].u.operand;
1363             int r3 = it[4].u.operand;
1364             int offset = it[5].u.operand;
1365             dataLogF("[%4d] get_pnames\t %s, %s, %s, %s, %d(->%d)", location, registerName(exec, r0).data(), registerName(exec, r1).data(), registerName(exec, r2).data(), registerName(exec, r3).data(), offset, location + offset);
1366             dumpBytecodeCommentAndNewLine(location);
1367             it += OPCODE_LENGTH(op_get_pnames) - 1;
1368             break;
1369         }
1370         case op_next_pname: {
1371             int dest = it[1].u.operand;
1372             int base = it[2].u.operand;
1373             int i = it[3].u.operand;
1374             int size = it[4].u.operand;
1375             int iter = it[5].u.operand;
1376             int offset = it[6].u.operand;
1377             dataLogF("[%4d] next_pname\t %s, %s, %s, %s, %s, %d(->%d)", location, registerName(exec, dest).data(), registerName(exec, base).data(), registerName(exec, i).data(), registerName(exec, size).data(), registerName(exec, iter).data(), offset, location + offset);
1378             dumpBytecodeCommentAndNewLine(location);
1379             it += OPCODE_LENGTH(op_next_pname) - 1;
1380             break;
1381         }
1382         case op_push_with_scope: {
1383             int r0 = (++it)->u.operand;
1384             dataLogF("[%4d] push_with_scope\t %s", location, registerName(exec, r0).data());
1385             dumpBytecodeCommentAndNewLine(location);
1386             break;
1387         }
1388         case op_pop_scope: {
1389             dataLogF("[%4d] pop_scope", location);
1390             dumpBytecodeCommentAndNewLine(location);
1391             break;
1392         }
1393         case op_push_name_scope: {
1394             int id0 = (++it)->u.operand;
1395             int r1 = (++it)->u.operand;
1396             unsigned attributes = (++it)->u.operand;
1397             dataLogF("[%4d] push_name_scope \t%s, %s, %u", location, idName(id0, m_identifiers[id0]).data(), registerName(exec, r1).data(), attributes);
1398             dumpBytecodeCommentAndNewLine(location);
1399             break;
1400         }
1401         case op_jmp_scopes: {
1402             int scopeDelta = (++it)->u.operand;
1403             int offset = (++it)->u.operand;
1404             dataLogF("[%4d] jmp_scopes\t^%d, %d(->%d)", location, scopeDelta, offset, location + offset);
1405             dumpBytecodeCommentAndNewLine(location);
1406             break;
1407         }
1408         case op_catch: {
1409             int r0 = (++it)->u.operand;
1410             dataLogF("[%4d] catch\t\t %s", location, registerName(exec, r0).data());
1411             dumpBytecodeCommentAndNewLine(location);
1412             break;
1413         }
1414         case op_throw: {
1415             int r0 = (++it)->u.operand;
1416             dataLogF("[%4d] throw\t\t %s", location, registerName(exec, r0).data());
1417             dumpBytecodeCommentAndNewLine(location);
1418             break;
1419         }
1420         case op_throw_static_error: {
1421             int k0 = (++it)->u.operand;
1422             int k1 = (++it)->u.operand;
1423             dataLogF("[%4d] throw_static_error\t %s, %s", location, constantName(exec, k0, getConstant(k0)).data(), k1 ? "true" : "false");
1424             dumpBytecodeCommentAndNewLine(location);
1425             break;
1426         }
1427         case op_debug: {
1428             int debugHookID = (++it)->u.operand;
1429             int firstLine = (++it)->u.operand;
1430             int lastLine = (++it)->u.operand;
1431             int column = (++it)->u.operand;
1432             dataLogF("[%4d] debug\t\t %s, %d, %d, %d", location, debugHookName(debugHookID), firstLine, lastLine, column);
1433             dumpBytecodeCommentAndNewLine(location);
1434             break;
1435         }
1436         case op_profile_will_call: {
1437             int function = (++it)->u.operand;
1438             dataLogF("[%4d] profile_will_call %s", location, registerName(exec, function).data());
1439             dumpBytecodeCommentAndNewLine(location);
1440             break;
1441         }
1442         case op_profile_did_call: {
1443             int function = (++it)->u.operand;
1444             dataLogF("[%4d] profile_did_call\t %s", location, registerName(exec, function).data());
1445             dumpBytecodeCommentAndNewLine(location);
1446             break;
1447         }
1448         case op_end: {
1449             int r0 = (++it)->u.operand;
1450             dataLogF("[%4d] end\t\t %s", location, registerName(exec, r0).data());
1451             dumpBytecodeCommentAndNewLine(location);
1452             break;
1453         }
1454 #if ENABLE(LLINT_C_LOOP)
1455         default:
1456             ASSERT(false); // We should never get here.
1457 #endif
1458     }
1459 }
1460
1461 void CodeBlock::dump(unsigned bytecodeOffset)
1462 {
1463     ExecState* exec = m_globalObject->globalExec();
1464     const Instruction* it = instructions().begin() + bytecodeOffset;
1465     dump(exec, instructions().begin(), it);
1466 }
1467
1468 #if DUMP_CODE_BLOCK_STATISTICS
1469 static HashSet<CodeBlock*> liveCodeBlockSet;
1470 #endif
1471
1472 #define FOR_EACH_MEMBER_VECTOR(macro) \
1473     macro(instructions) \
1474     macro(globalResolveInfos) \
1475     macro(structureStubInfos) \
1476     macro(callLinkInfos) \
1477     macro(linkedCallerList) \
1478     macro(identifiers) \
1479     macro(functionExpressions) \
1480     macro(constantRegisters)
1481
1482 #define FOR_EACH_MEMBER_VECTOR_RARE_DATA(macro) \
1483     macro(regexps) \
1484     macro(functions) \
1485     macro(exceptionHandlers) \
1486     macro(immediateSwitchJumpTables) \
1487     macro(characterSwitchJumpTables) \
1488     macro(stringSwitchJumpTables) \
1489     macro(evalCodeCache) \
1490     macro(expressionInfo) \
1491     macro(lineInfo) \
1492     macro(callReturnIndexVector)
1493
1494 template<typename T>
1495 static size_t sizeInBytes(const Vector<T>& vector)
1496 {
1497     return vector.capacity() * sizeof(T);
1498 }
1499
1500 void CodeBlock::dumpStatistics()
1501 {
1502 #if DUMP_CODE_BLOCK_STATISTICS
1503     #define DEFINE_VARS(name) size_t name##IsNotEmpty = 0; size_t name##TotalSize = 0;
1504         FOR_EACH_MEMBER_VECTOR(DEFINE_VARS)
1505         FOR_EACH_MEMBER_VECTOR_RARE_DATA(DEFINE_VARS)
1506     #undef DEFINE_VARS
1507
1508     // Non-vector data members
1509     size_t evalCodeCacheIsNotEmpty = 0;
1510
1511     size_t symbolTableIsNotEmpty = 0;
1512     size_t symbolTableTotalSize = 0;
1513
1514     size_t hasRareData = 0;
1515
1516     size_t isFunctionCode = 0;
1517     size_t isGlobalCode = 0;
1518     size_t isEvalCode = 0;
1519
1520     HashSet<CodeBlock*>::const_iterator end = liveCodeBlockSet.end();
1521     for (HashSet<CodeBlock*>::const_iterator it = liveCodeBlockSet.begin(); it != end; ++it) {
1522         CodeBlock* codeBlock = *it;
1523
1524         #define GET_STATS(name) if (!codeBlock->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_##name); }
1525             FOR_EACH_MEMBER_VECTOR(GET_STATS)
1526         #undef GET_STATS
1527
1528         if (codeBlock->symbolTable() && !codeBlock->symbolTable()->isEmpty()) {
1529             symbolTableIsNotEmpty++;
1530             symbolTableTotalSize += (codeBlock->symbolTable()->capacity() * (sizeof(SymbolTable::KeyType) + sizeof(SymbolTable::MappedType)));
1531         }
1532
1533         if (codeBlock->m_rareData) {
1534             hasRareData++;
1535             #define GET_STATS(name) if (!codeBlock->m_rareData->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_rareData->m_##name); }
1536                 FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_STATS)
1537             #undef GET_STATS
1538
1539             if (!codeBlock->m_rareData->m_evalCodeCache.isEmpty())
1540                 evalCodeCacheIsNotEmpty++;
1541         }
1542
1543         switch (codeBlock->codeType()) {
1544             case FunctionCode:
1545                 ++isFunctionCode;
1546                 break;
1547             case GlobalCode:
1548                 ++isGlobalCode;
1549                 break;
1550             case EvalCode:
1551                 ++isEvalCode;
1552                 break;
1553         }
1554     }
1555
1556     size_t totalSize = 0;
1557
1558     #define GET_TOTAL_SIZE(name) totalSize += name##TotalSize;
1559         FOR_EACH_MEMBER_VECTOR(GET_TOTAL_SIZE)
1560         FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_TOTAL_SIZE)
1561     #undef GET_TOTAL_SIZE
1562
1563     totalSize += symbolTableTotalSize;
1564     totalSize += (liveCodeBlockSet.size() * sizeof(CodeBlock));
1565
1566     dataLogF("Number of live CodeBlocks: %d\n", liveCodeBlockSet.size());
1567     dataLogF("Size of a single CodeBlock [sizeof(CodeBlock)]: %zu\n", sizeof(CodeBlock));
1568     dataLogF("Size of all CodeBlocks: %zu\n", totalSize);
1569     dataLogF("Average size of a CodeBlock: %zu\n", totalSize / liveCodeBlockSet.size());
1570
1571     dataLogF("Number of FunctionCode CodeBlocks: %zu (%.3f%%)\n", isFunctionCode, static_cast<double>(isFunctionCode) * 100.0 / liveCodeBlockSet.size());
1572     dataLogF("Number of GlobalCode CodeBlocks: %zu (%.3f%%)\n", isGlobalCode, static_cast<double>(isGlobalCode) * 100.0 / liveCodeBlockSet.size());
1573     dataLogF("Number of EvalCode CodeBlocks: %zu (%.3f%%)\n", isEvalCode, static_cast<double>(isEvalCode) * 100.0 / liveCodeBlockSet.size());
1574
1575     dataLogF("Number of CodeBlocks with rare data: %zu (%.3f%%)\n", hasRareData, static_cast<double>(hasRareData) * 100.0 / liveCodeBlockSet.size());
1576
1577     #define PRINT_STATS(name) dataLogF("Number of CodeBlocks with " #name ": %zu\n", name##IsNotEmpty); dataLogF("Size of all " #name ": %zu\n", name##TotalSize); 
1578         FOR_EACH_MEMBER_VECTOR(PRINT_STATS)
1579         FOR_EACH_MEMBER_VECTOR_RARE_DATA(PRINT_STATS)
1580     #undef PRINT_STATS
1581
1582     dataLogF("Number of CodeBlocks with evalCodeCache: %zu\n", evalCodeCacheIsNotEmpty);
1583     dataLogF("Number of CodeBlocks with symbolTable: %zu\n", symbolTableIsNotEmpty);
1584
1585     dataLogF("Size of all symbolTables: %zu\n", symbolTableTotalSize);
1586
1587 #else
1588     dataLogF("Dumping CodeBlock statistics is not enabled.\n");
1589 #endif
1590 }
1591
1592 CodeBlock::CodeBlock(CopyParsedBlockTag, CodeBlock& other)
1593     : m_globalObject(other.m_globalObject)
1594     , m_heap(other.m_heap)
1595     , m_numCalleeRegisters(other.m_numCalleeRegisters)
1596     , m_numVars(other.m_numVars)
1597     , m_isConstructor(other.m_isConstructor)
1598     , m_unlinkedCode(*other.m_globalData, other.m_ownerExecutable.get(), other.m_unlinkedCode.get())
1599     , m_ownerExecutable(*other.m_globalData, other.m_ownerExecutable.get(), other.m_ownerExecutable.get())
1600     , m_globalData(other.m_globalData)
1601     , m_instructions(other.m_instructions)
1602     , m_thisRegister(other.m_thisRegister)
1603     , m_argumentsRegister(other.m_argumentsRegister)
1604     , m_activationRegister(other.m_activationRegister)
1605     , m_isStrictMode(other.m_isStrictMode)
1606     , m_source(other.m_source)
1607     , m_sourceOffset(other.m_sourceOffset)
1608 #if ENABLE(VALUE_PROFILER)
1609     , m_executionEntryCount(0)
1610 #endif
1611     , m_identifiers(other.m_identifiers)
1612     , m_constantRegisters(other.m_constantRegisters)
1613     , m_functionDecls(other.m_functionDecls)
1614     , m_functionExprs(other.m_functionExprs)
1615     , m_osrExitCounter(0)
1616     , m_optimizationDelayCounter(0)
1617     , m_reoptimizationRetryCounter(0)
1618     , m_resolveOperations(other.m_resolveOperations)
1619     , m_putToBaseOperations(other.m_putToBaseOperations)
1620 #if ENABLE(BYTECODE_COMMENTS)
1621     , m_bytecodeCommentIterator(0)
1622 #endif
1623 #if ENABLE(JIT)
1624     , m_canCompileWithDFGState(DFG::CapabilityLevelNotSet)
1625 #endif
1626 {
1627     setNumParameters(other.numParameters());
1628     optimizeAfterWarmUp();
1629     jitAfterWarmUp();
1630
1631     if (other.m_rareData) {
1632         createRareDataIfNecessary();
1633         
1634         m_rareData->m_exceptionHandlers = other.m_rareData->m_exceptionHandlers;
1635         m_rareData->m_constantBuffers = other.m_rareData->m_constantBuffers;
1636         m_rareData->m_immediateSwitchJumpTables = other.m_rareData->m_immediateSwitchJumpTables;
1637         m_rareData->m_characterSwitchJumpTables = other.m_rareData->m_characterSwitchJumpTables;
1638         m_rareData->m_stringSwitchJumpTables = other.m_rareData->m_stringSwitchJumpTables;
1639     }
1640 }
1641
1642 CodeBlock::CodeBlock(ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock, JSGlobalObject* globalObject, unsigned baseScopeDepth, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, PassOwnPtr<CodeBlock> alternative)
1643     : m_globalObject(globalObject->globalData(), ownerExecutable, globalObject)
1644     , m_heap(&m_globalObject->globalData().heap)
1645     , m_numCalleeRegisters(unlinkedCodeBlock->m_numCalleeRegisters)
1646     , m_numVars(unlinkedCodeBlock->m_numVars)
1647     , m_isConstructor(unlinkedCodeBlock->isConstructor())
1648     , m_unlinkedCode(globalObject->globalData(), ownerExecutable, unlinkedCodeBlock)
1649     , m_ownerExecutable(globalObject->globalData(), ownerExecutable, ownerExecutable)
1650     , m_globalData(unlinkedCodeBlock->globalData())
1651     , m_thisRegister(unlinkedCodeBlock->thisRegister())
1652     , m_argumentsRegister(unlinkedCodeBlock->argumentsRegister())
1653     , m_activationRegister(unlinkedCodeBlock->activationRegister())
1654     , m_isStrictMode(unlinkedCodeBlock->isStrictMode())
1655     , m_source(sourceProvider)
1656     , m_sourceOffset(sourceOffset)
1657 #if ENABLE(VALUE_PROFILER)
1658     , m_executionEntryCount(0)
1659 #endif
1660     , m_alternative(alternative)
1661     , m_osrExitCounter(0)
1662     , m_optimizationDelayCounter(0)
1663     , m_reoptimizationRetryCounter(0)
1664 #if ENABLE(BYTECODE_COMMENTS)
1665     , m_bytecodeCommentIterator(0)
1666 #endif
1667 {
1668     m_globalData->startedCompiling(this);
1669
1670     ASSERT(m_source);
1671     setNumParameters(unlinkedCodeBlock->numParameters());
1672
1673     optimizeAfterWarmUp();
1674     jitAfterWarmUp();
1675
1676 #if DUMP_CODE_BLOCK_STATISTICS
1677     liveCodeBlockSet.add(this);
1678 #endif
1679     setIdentifiers(unlinkedCodeBlock->identifiers());
1680     setConstantRegisters(unlinkedCodeBlock->constantRegisters());
1681
1682     m_functionDecls.grow(unlinkedCodeBlock->numberOfFunctionDecls());
1683     for (size_t count = unlinkedCodeBlock->numberOfFunctionDecls(), i = 0; i < count; ++i) {
1684         UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionDecl(i);
1685         unsigned lineCount = unlinkedExecutable->lineCount();
1686         unsigned firstLine = ownerExecutable->lineNo() + unlinkedExecutable->firstLineOffset();
1687         unsigned startOffset = sourceOffset + unlinkedExecutable->startOffset();
1688         unsigned sourceLength = unlinkedExecutable->sourceLength();
1689         SourceCode code(m_source, startOffset, startOffset + sourceLength, firstLine);
1690         FunctionExecutable* executable = FunctionExecutable::create(*m_globalData, code, unlinkedExecutable, firstLine, firstLine + lineCount);
1691         m_functionDecls[i].set(*m_globalData, ownerExecutable, executable);
1692     }
1693
1694     m_functionExprs.grow(unlinkedCodeBlock->numberOfFunctionExprs());
1695     for (size_t count = unlinkedCodeBlock->numberOfFunctionExprs(), i = 0; i < count; ++i) {
1696         UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionExpr(i);
1697         unsigned lineCount = unlinkedExecutable->lineCount();
1698         unsigned firstLine = ownerExecutable->lineNo() + unlinkedExecutable->firstLineOffset();
1699         unsigned startOffset = sourceOffset + unlinkedExecutable->startOffset();
1700         unsigned sourceLength = unlinkedExecutable->sourceLength();
1701         SourceCode code(m_source, startOffset, startOffset + sourceLength, firstLine);
1702         FunctionExecutable* executable = FunctionExecutable::create(*m_globalData, code, unlinkedExecutable, firstLine, firstLine + lineCount);
1703         m_functionExprs[i].set(*m_globalData, ownerExecutable, executable);
1704     }
1705
1706     if (unlinkedCodeBlock->hasRareData()) {
1707         createRareDataIfNecessary();
1708         if (size_t count = unlinkedCodeBlock->constantBufferCount()) {
1709             m_rareData->m_constantBuffers.grow(count);
1710             for (size_t i = 0; i < count; i++) {
1711                 const UnlinkedCodeBlock::ConstantBuffer& buffer = unlinkedCodeBlock->constantBuffer(i);
1712                 m_rareData->m_constantBuffers[i] = buffer;
1713             }
1714         }
1715         if (size_t count = unlinkedCodeBlock->numberOfExceptionHandlers()) {
1716             m_rareData->m_exceptionHandlers.grow(count);
1717             for (size_t i = 0; i < count; i++) {
1718                 const UnlinkedHandlerInfo& handler = unlinkedCodeBlock->exceptionHandler(i);
1719                 m_rareData->m_exceptionHandlers[i].start = handler.start;
1720                 m_rareData->m_exceptionHandlers[i].end = handler.end;
1721                 m_rareData->m_exceptionHandlers[i].target = handler.target;
1722                 m_rareData->m_exceptionHandlers[i].scopeDepth = handler.scopeDepth + baseScopeDepth;
1723 #if ENABLE(JIT) && ENABLE(LLINT)
1724                 m_rareData->m_exceptionHandlers[i].nativeCode = CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(LLInt::getCodePtr(llint_op_catch)));
1725 #endif
1726             }
1727         }
1728
1729         if (size_t count = unlinkedCodeBlock->numberOfStringSwitchJumpTables()) {
1730             m_rareData->m_stringSwitchJumpTables.grow(count);
1731             for (size_t i = 0; i < count; i++) {
1732                 UnlinkedStringJumpTable::StringOffsetTable::iterator ptr = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.begin();
1733                 UnlinkedStringJumpTable::StringOffsetTable::iterator end = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.end();
1734                 for (; ptr != end; ++ptr) {
1735                     OffsetLocation offset;
1736                     offset.branchOffset = ptr->value;
1737                     m_rareData->m_stringSwitchJumpTables[i].offsetTable.add(ptr->key, offset);
1738                 }
1739             }
1740         }
1741
1742         if (size_t count = unlinkedCodeBlock->numberOfImmediateSwitchJumpTables()) {
1743             m_rareData->m_immediateSwitchJumpTables.grow(count);
1744             for (size_t i = 0; i < count; i++) {
1745                 UnlinkedSimpleJumpTable& sourceTable = unlinkedCodeBlock->immediateSwitchJumpTable(i);
1746                 SimpleJumpTable& destTable = m_rareData->m_immediateSwitchJumpTables[i];
1747                 destTable.branchOffsets = sourceTable.branchOffsets;
1748                 destTable.min = sourceTable.min;
1749             }
1750         }
1751
1752         if (size_t count = unlinkedCodeBlock->numberOfCharacterSwitchJumpTables()) {
1753             m_rareData->m_characterSwitchJumpTables.grow(count);
1754             for (size_t i = 0; i < count; i++) {
1755                 UnlinkedSimpleJumpTable& sourceTable = unlinkedCodeBlock->characterSwitchJumpTable(i);
1756                 SimpleJumpTable& destTable = m_rareData->m_characterSwitchJumpTables[i];
1757                 destTable.branchOffsets = sourceTable.branchOffsets;
1758                 destTable.min = sourceTable.min;
1759             }
1760         }
1761     }
1762
1763     // Allocate metadata buffers for the bytecode
1764 #if ENABLE(LLINT)
1765     if (size_t size = unlinkedCodeBlock->numberOfLLintCallLinkInfos())
1766         m_llintCallLinkInfos.grow(size);
1767 #endif
1768 #if ENABLE(DFG_JIT)
1769     if (size_t size = unlinkedCodeBlock->numberOfArrayProfiles())
1770         m_arrayProfiles.grow(size);
1771     if (size_t size = unlinkedCodeBlock->numberOfArrayAllocationProfiles())
1772         m_arrayAllocationProfiles.grow(size);
1773     if (size_t size = unlinkedCodeBlock->numberOfValueProfiles())
1774         m_valueProfiles.grow(size);
1775 #endif
1776     if (size_t size = unlinkedCodeBlock->numberOfResolveOperations())
1777         m_resolveOperations.grow(size);
1778     size_t putToBaseCount = unlinkedCodeBlock->numberOfPutToBaseOperations();
1779     m_putToBaseOperations.reserveCapacity(putToBaseCount);
1780     for (size_t i = 0; i < putToBaseCount; ++i)
1781         m_putToBaseOperations.append(PutToBaseOperation(isStrictMode()));
1782
1783     ASSERT(m_putToBaseOperations.capacity() == putToBaseCount);
1784
1785     // Copy and translate the UnlinkedInstructions
1786     size_t instructionCount = unlinkedCodeBlock->instructions().size();
1787     UnlinkedInstruction* pc = unlinkedCodeBlock->instructions().data();
1788     Vector<Instruction> instructions(instructionCount);
1789     for (size_t i = 0; i < unlinkedCodeBlock->instructions().size(); ) {
1790         unsigned opLength = opcodeLength(pc[i].u.opcode);
1791         instructions[i] = globalData()->interpreter->getOpcode(pc[i].u.opcode);
1792         for (size_t j = 1; j < opLength; ++j) {
1793             if (sizeof(int32_t) != sizeof(intptr_t))
1794                 instructions[i + j].u.pointer = 0;
1795             instructions[i + j].u.operand = pc[i + j].u.operand;
1796         }
1797         switch (pc[i].u.opcode) {
1798 #if ENABLE(DFG_JIT)
1799         case op_get_by_val:
1800         case op_get_argument_by_val: {
1801             int arrayProfileIndex = pc[i + opLength - 2].u.operand;
1802             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1803
1804             instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex];
1805             // fallthrough
1806         }
1807         case op_convert_this:
1808         case op_resolve:
1809         case op_resolve_base:
1810         case op_resolve_with_base:
1811         case op_resolve_with_this:
1812         case op_get_by_id:
1813         case op_call_put_result:
1814         case op_get_callee: {
1815             ValueProfile* profile = &m_valueProfiles[pc[i + opLength - 1].u.operand];
1816             ASSERT(profile->m_bytecodeOffset == -1);
1817             profile->m_bytecodeOffset = i;
1818             instructions[i + opLength - 1] = profile;
1819             break;
1820         }
1821         case op_put_by_val: {
1822             int arrayProfileIndex = pc[i + opLength - 1].u.operand;
1823             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1824             instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
1825             break;
1826         }
1827
1828         case op_new_array:
1829         case op_new_array_buffer:
1830         case op_new_array_with_size: {
1831             int arrayAllocationProfileIndex = pc[i + opLength - 1].u.operand;
1832             instructions[i + opLength - 1] = &m_arrayAllocationProfiles[arrayAllocationProfileIndex];
1833             break;
1834         }
1835 #endif
1836
1837         case op_call:
1838         case op_call_eval: {
1839 #if ENABLE(DFG_JIT)
1840             int arrayProfileIndex = pc[i + opLength - 1].u.operand;
1841             m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i);
1842             instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex];
1843 #endif
1844 #if ENABLE(LLINT)
1845             instructions[i + 4] = &m_llintCallLinkInfos[pc[i + 4].u.operand];
1846 #endif
1847             break;
1848         }
1849         case op_construct:
1850 #if ENABLE(LLINT)
1851             instructions[i + 4] = &m_llintCallLinkInfos[pc[i + 4].u.operand];
1852 #endif
1853             break;
1854         case op_get_by_id_out_of_line:
1855         case op_get_by_id_self:
1856         case op_get_by_id_proto:
1857         case op_get_by_id_chain:
1858         case op_get_by_id_getter_self:
1859         case op_get_by_id_getter_proto:
1860         case op_get_by_id_getter_chain:
1861         case op_get_by_id_custom_self:
1862         case op_get_by_id_custom_proto:
1863         case op_get_by_id_custom_chain:
1864         case op_get_by_id_generic:
1865         case op_get_array_length:
1866         case op_get_string_length:
1867             CRASH();
1868
1869         case op_init_global_const_nop: {
1870             ASSERT(codeType() == GlobalCode);
1871             Identifier ident = identifier(pc[i + 4].u.operand);
1872             SymbolTableEntry entry = globalObject->symbolTable()->get(ident.impl());
1873             if (entry.isNull())
1874                 break;
1875
1876             if (entry.couldBeWatched()) {
1877                 instructions[i + 0] = globalData()->interpreter->getOpcode(op_init_global_const_check);
1878                 instructions[i + 1] = &globalObject->registerAt(entry.getIndex());
1879                 instructions[i + 3] = entry.addressOfIsWatched();
1880                 break;
1881             }
1882
1883             instructions[i + 0] = globalData()->interpreter->getOpcode(op_init_global_const);
1884             instructions[i + 1] = &globalObject->registerAt(entry.getIndex());
1885             break;
1886         }
1887         default:
1888             break;
1889         }
1890         i += opLength;
1891     }
1892     m_instructions = WTF::RefCountedArray<Instruction>(instructions);
1893
1894     if (BytecodeGenerator::dumpsGeneratedCode())
1895         dump();
1896     m_globalData->finishedCompiling(this);
1897 }
1898
1899 CodeBlock::~CodeBlock()
1900 {
1901 #if ENABLE(DFG_JIT)
1902     // Remove myself from the set of DFG code blocks. Note that I may not be in this set
1903     // (because I'm not a DFG code block), in which case this is a no-op anyway.
1904     m_globalData->heap.m_dfgCodeBlocks.m_set.remove(this);
1905 #endif
1906     
1907 #if ENABLE(VERBOSE_VALUE_PROFILE)
1908     dumpValueProfiles();
1909 #endif
1910
1911 #if ENABLE(LLINT)    
1912     while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end())
1913         m_incomingLLIntCalls.begin()->remove();
1914 #endif // ENABLE(LLINT)
1915 #if ENABLE(JIT)
1916     // We may be destroyed before any CodeBlocks that refer to us are destroyed.
1917     // Consider that two CodeBlocks become unreachable at the same time. There
1918     // is no guarantee about the order in which the CodeBlocks are destroyed.
1919     // So, if we don't remove incoming calls, and get destroyed before the
1920     // CodeBlock(s) that have calls into us, then the CallLinkInfo vector's
1921     // destructor will try to remove nodes from our (no longer valid) linked list.
1922     while (m_incomingCalls.begin() != m_incomingCalls.end())
1923         m_incomingCalls.begin()->remove();
1924     
1925     // Note that our outgoing calls will be removed from other CodeBlocks'
1926     // m_incomingCalls linked lists through the execution of the ~CallLinkInfo
1927     // destructors.
1928
1929     for (size_t size = m_structureStubInfos.size(), i = 0; i < size; ++i)
1930         m_structureStubInfos[i].deref();
1931 #endif // ENABLE(JIT)
1932
1933 #if DUMP_CODE_BLOCK_STATISTICS
1934     liveCodeBlockSet.remove(this);
1935 #endif
1936 }
1937
1938 void CodeBlock::setNumParameters(int newValue)
1939 {
1940     m_numParameters = newValue;
1941
1942 #if ENABLE(VALUE_PROFILER)
1943     m_argumentValueProfiles.resize(newValue);
1944 #endif
1945 }
1946
1947 void CodeBlock::visitStructures(SlotVisitor& visitor, Instruction* vPC)
1948 {
1949     Interpreter* interpreter = m_globalData->interpreter;
1950
1951     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id) && vPC[4].u.structure) {
1952         visitor.append(&vPC[4].u.structure);
1953         return;
1954     }
1955
1956     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_getter_self) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_custom_self)) {
1957         visitor.append(&vPC[4].u.structure);
1958         return;
1959     }
1960     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_getter_proto) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_custom_proto)) {
1961         visitor.append(&vPC[4].u.structure);
1962         visitor.append(&vPC[5].u.structure);
1963         return;
1964     }
1965     if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_getter_chain) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_custom_chain)) {
1966         visitor.append(&vPC[4].u.structure);
1967         if (vPC[5].u.structureChain)
1968             visitor.append(&vPC[5].u.structureChain);
1969         return;
1970     }
1971     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
1972         visitor.append(&vPC[4].u.structure);
1973         visitor.append(&vPC[5].u.structure);
1974         if (vPC[6].u.structureChain)
1975             visitor.append(&vPC[6].u.structureChain);
1976         return;
1977     }
1978     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id) && vPC[4].u.structure) {
1979         visitor.append(&vPC[4].u.structure);
1980         return;
1981     }
1982     if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
1983         visitor.append(&vPC[4].u.structure);
1984         return;
1985     }
1986
1987     // These instructions don't ref their Structures.
1988     ASSERT(vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_get_array_length) || vPC[0].u.opcode == interpreter->getOpcode(op_get_string_length));
1989 }
1990
1991 void EvalCodeCache::visitAggregate(SlotVisitor& visitor)
1992 {
1993     EvalCacheMap::iterator end = m_cacheMap.end();
1994     for (EvalCacheMap::iterator ptr = m_cacheMap.begin(); ptr != end; ++ptr)
1995         visitor.append(&ptr->value);
1996 }
1997
1998 void CodeBlock::visitAggregate(SlotVisitor& visitor)
1999 {
2000 #if ENABLE(PARALLEL_GC) && ENABLE(DFG_JIT)
2001     if (!!m_dfgData) {
2002         // I may be asked to scan myself more than once, and it may even happen concurrently.
2003         // To this end, use a CAS loop to check if I've been called already. Only one thread
2004         // may proceed past this point - whichever one wins the CAS race.
2005         unsigned oldValue;
2006         do {
2007             oldValue = m_dfgData->visitAggregateHasBeenCalled;
2008             if (oldValue) {
2009                 // Looks like someone else won! Return immediately to ensure that we don't
2010                 // trace the same CodeBlock concurrently. Doing so is hazardous since we will
2011                 // be mutating the state of ValueProfiles, which contain JSValues, which can
2012                 // have word-tearing on 32-bit, leading to awesome timing-dependent crashes
2013                 // that are nearly impossible to track down.
2014                 
2015                 // Also note that it must be safe to return early as soon as we see the
2016                 // value true (well, (unsigned)1), since once a GC thread is in this method
2017                 // and has won the CAS race (i.e. was responsible for setting the value true)
2018                 // it will definitely complete the rest of this method before declaring
2019                 // termination.
2020                 return;
2021             }
2022         } while (!WTF::weakCompareAndSwap(&m_dfgData->visitAggregateHasBeenCalled, 0, 1));
2023     }
2024 #endif // ENABLE(PARALLEL_GC) && ENABLE(DFG_JIT)
2025     
2026     if (!!m_alternative)
2027         m_alternative->visitAggregate(visitor);
2028
2029     visitor.append(&m_unlinkedCode);
2030
2031     // There are three things that may use unconditional finalizers: lazy bytecode freeing,
2032     // inline cache clearing, and jettisoning. The probability of us wanting to do at
2033     // least one of those things is probably quite close to 1. So we add one no matter what
2034     // and when it runs, it figures out whether it has any work to do.
2035     visitor.addUnconditionalFinalizer(this);
2036     
2037     if (shouldImmediatelyAssumeLivenessDuringScan()) {
2038         // This code block is live, so scan all references strongly and return.
2039         stronglyVisitStrongReferences(visitor);
2040         stronglyVisitWeakReferences(visitor);
2041         return;
2042     }
2043     
2044 #if ENABLE(DFG_JIT)
2045     // We get here if we're live in the sense that our owner executable is live,
2046     // but we're not yet live for sure in another sense: we may yet decide that this
2047     // code block should be jettisoned based on its outgoing weak references being
2048     // stale. Set a flag to indicate that we're still assuming that we're dead, and
2049     // perform one round of determining if we're live. The GC may determine, based on
2050     // either us marking additional objects, or by other objects being marked for
2051     // other reasons, that this iteration should run again; it will notify us of this
2052     // decision by calling harvestWeakReferences().
2053     
2054     m_dfgData->livenessHasBeenProved = false;
2055     m_dfgData->allTransitionsHaveBeenMarked = false;
2056     
2057     performTracingFixpointIteration(visitor);
2058
2059     // GC doesn't have enough information yet for us to decide whether to keep our DFG
2060     // data, so we need to register a handler to run again at the end of GC, when more
2061     // information is available.
2062     if (!(m_dfgData->livenessHasBeenProved && m_dfgData->allTransitionsHaveBeenMarked))
2063         visitor.addWeakReferenceHarvester(this);
2064     
2065 #else // ENABLE(DFG_JIT)
2066     ASSERT_NOT_REACHED();
2067 #endif // ENABLE(DFG_JIT)
2068 }
2069
2070 void CodeBlock::performTracingFixpointIteration(SlotVisitor& visitor)
2071 {
2072     UNUSED_PARAM(visitor);
2073     
2074 #if ENABLE(DFG_JIT)
2075     // Evaluate our weak reference transitions, if there are still some to evaluate.
2076     if (!m_dfgData->allTransitionsHaveBeenMarked) {
2077         bool allAreMarkedSoFar = true;
2078         for (unsigned i = 0; i < m_dfgData->transitions.size(); ++i) {
2079             if ((!m_dfgData->transitions[i].m_codeOrigin
2080                  || Heap::isMarked(m_dfgData->transitions[i].m_codeOrigin.get()))
2081                 && Heap::isMarked(m_dfgData->transitions[i].m_from.get())) {
2082                 // If the following three things are live, then the target of the
2083                 // transition is also live:
2084                 // - This code block. We know it's live already because otherwise
2085                 //   we wouldn't be scanning ourselves.
2086                 // - The code origin of the transition. Transitions may arise from
2087                 //   code that was inlined. They are not relevant if the user's
2088                 //   object that is required for the inlinee to run is no longer
2089                 //   live.
2090                 // - The source of the transition. The transition checks if some
2091                 //   heap location holds the source, and if so, stores the target.
2092                 //   Hence the source must be live for the transition to be live.
2093                 visitor.append(&m_dfgData->transitions[i].m_to);
2094             } else
2095                 allAreMarkedSoFar = false;
2096         }
2097         
2098         if (allAreMarkedSoFar)
2099             m_dfgData->allTransitionsHaveBeenMarked = true;
2100     }
2101     
2102     // Check if we have any remaining work to do.
2103     if (m_dfgData->livenessHasBeenProved)
2104         return;
2105     
2106     // Now check all of our weak references. If all of them are live, then we
2107     // have proved liveness and so we scan our strong references. If at end of
2108     // GC we still have not proved liveness, then this code block is toast.
2109     bool allAreLiveSoFar = true;
2110     for (unsigned i = 0; i < m_dfgData->weakReferences.size(); ++i) {
2111         if (!Heap::isMarked(m_dfgData->weakReferences[i].get())) {
2112             allAreLiveSoFar = false;
2113             break;
2114         }
2115     }
2116     
2117     // If some weak references are dead, then this fixpoint iteration was
2118     // unsuccessful.
2119     if (!allAreLiveSoFar)
2120         return;
2121     
2122     // All weak references are live. Record this information so we don't
2123     // come back here again, and scan the strong references.
2124     m_dfgData->livenessHasBeenProved = true;
2125     stronglyVisitStrongReferences(visitor);
2126 #endif // ENABLE(DFG_JIT)
2127 }
2128
2129 void CodeBlock::visitWeakReferences(SlotVisitor& visitor)
2130 {
2131     performTracingFixpointIteration(visitor);
2132 }
2133
2134 #if ENABLE(JIT_VERBOSE_OSR)
2135 static const bool verboseUnlinking = true;
2136 #else
2137 static const bool verboseUnlinking = false;
2138 #endif
2139
2140 void CodeBlock::finalizeUnconditionally()
2141 {
2142 #if ENABLE(LLINT)
2143     Interpreter* interpreter = m_globalData->interpreter;
2144     if (!!numberOfInstructions()) {
2145         const Vector<unsigned>& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions();
2146         for (size_t size = propertyAccessInstructions.size(), i = 0; i < size; ++i) {
2147             Instruction* curInstruction = &instructions()[propertyAccessInstructions[i]];
2148             switch (interpreter->getOpcodeID(curInstruction[0].u.opcode)) {
2149             case op_get_by_id:
2150             case op_get_by_id_out_of_line:
2151             case op_put_by_id:
2152             case op_put_by_id_out_of_line:
2153                 if (!curInstruction[4].u.structure || Heap::isMarked(curInstruction[4].u.structure.get()))
2154                     break;
2155                 if (verboseUnlinking)
2156                     dataLogF("Clearing LLInt property access with structure %p.\n", curInstruction[4].u.structure.get());
2157                 curInstruction[4].u.structure.clear();
2158                 curInstruction[5].u.operand = 0;
2159                 break;
2160             case op_put_by_id_transition_direct:
2161             case op_put_by_id_transition_normal:
2162             case op_put_by_id_transition_direct_out_of_line:
2163             case op_put_by_id_transition_normal_out_of_line:
2164                 if (Heap::isMarked(curInstruction[4].u.structure.get())
2165                     && Heap::isMarked(curInstruction[6].u.structure.get())
2166                     && Heap::isMarked(curInstruction[7].u.structureChain.get()))
2167                     break;
2168                 if (verboseUnlinking) {
2169                     dataLogF("Clearing LLInt put transition with structures %p -> %p, chain %p.\n",
2170                             curInstruction[4].u.structure.get(),
2171                             curInstruction[6].u.structure.get(),
2172                             curInstruction[7].u.structureChain.get());
2173                 }
2174                 curInstruction[4].u.structure.clear();
2175                 curInstruction[6].u.structure.clear();
2176                 curInstruction[7].u.structureChain.clear();
2177                 curInstruction[0].u.opcode = interpreter->getOpcode(op_put_by_id);
2178                 break;
2179             case op_get_array_length:
2180                 break;
2181             default:
2182                 ASSERT_NOT_REACHED();
2183             }
2184         }
2185
2186         for (unsigned i = 0; i < m_llintCallLinkInfos.size(); ++i) {
2187             if (m_llintCallLinkInfos[i].isLinked() && !Heap::isMarked(m_llintCallLinkInfos[i].callee.get())) {
2188                 if (verboseUnlinking)
2189                     dataLogF("Clearing LLInt call from %p.\n", this);
2190                 m_llintCallLinkInfos[i].unlink();
2191             }
2192             if (!!m_llintCallLinkInfos[i].lastSeenCallee && !Heap::isMarked(m_llintCallLinkInfos[i].lastSeenCallee.get()))
2193                 m_llintCallLinkInfos[i].lastSeenCallee.clear();
2194         }
2195     }
2196 #endif // ENABLE(LLINT)
2197
2198 #if ENABLE(DFG_JIT)
2199     // Check if we're not live. If we are, then jettison.
2200     if (!(shouldImmediatelyAssumeLivenessDuringScan() || m_dfgData->livenessHasBeenProved)) {
2201         if (verboseUnlinking)
2202             dataLogF("Code block %p (executable %p) has dead weak references, jettisoning during GC.\n", this, ownerExecutable());
2203
2204         // Make sure that the baseline JIT knows that it should re-warm-up before
2205         // optimizing.
2206         alternative()->optimizeAfterWarmUp();
2207         
2208         if (DFG::shouldShowDisassembly()) {
2209             dataLogF("DFG CodeBlock %p will be jettisoned because of the following dead references:\n", this);
2210             for (unsigned i = 0; i < m_dfgData->transitions.size(); ++i) {
2211                 WeakReferenceTransition& transition = m_dfgData->transitions[i];
2212                 JSCell* origin = transition.m_codeOrigin.get();
2213                 JSCell* from = transition.m_from.get();
2214                 JSCell* to = transition.m_to.get();
2215                 if ((!origin || Heap::isMarked(origin)) && Heap::isMarked(from))
2216                     continue;
2217                 dataLogF("    Transition under %s, ", JSValue(origin).description());
2218                 dataLogF("%s -> ", JSValue(from).description());
2219                 dataLogF("%s.\n", JSValue(to).description());
2220             }
2221             for (unsigned i = 0; i < m_dfgData->weakReferences.size(); ++i) {
2222                 JSCell* weak = m_dfgData->weakReferences[i].get();
2223                 if (Heap::isMarked(weak))
2224                     continue;
2225                 dataLogF("    Weak reference %s.\n", JSValue(weak).description());
2226             }
2227         }
2228         
2229         jettison();
2230         return;
2231     }
2232 #endif // ENABLE(DFG_JIT)
2233
2234     for (size_t size = m_putToBaseOperations.size(), i = 0; i < size; ++i) {
2235         if (m_putToBaseOperations[i].m_structure && !Heap::isMarked(m_putToBaseOperations[i].m_structure.get())) {
2236             if (verboseUnlinking)
2237                 dataLogF("Clearing putToBase info in %p.\n", this);
2238             m_putToBaseOperations[i].m_structure.clear();
2239         }
2240     }
2241     for (size_t size = m_resolveOperations.size(), i = 0; i < size; ++i) {
2242         if (m_resolveOperations[i].isEmpty())
2243             continue;
2244 #ifndef NDEBUG
2245         for (size_t insnSize = m_resolveOperations[i].size() - 1, k = 0; k < insnSize; ++k)
2246             ASSERT(!m_resolveOperations[i][k].m_structure);
2247 #endif
2248         m_resolveOperations[i].last().m_structure.clear();
2249         if (m_resolveOperations[i].last().m_structure && !Heap::isMarked(m_resolveOperations[i].last().m_structure.get())) {
2250             if (verboseUnlinking)
2251                 dataLogF("Clearing resolve info in %p.\n", this);
2252             m_resolveOperations[i].last().m_structure.clear();
2253         }
2254     }
2255
2256 #if ENABLE(JIT)
2257     // Handle inline caches.
2258     if (!!getJITCode()) {
2259         RepatchBuffer repatchBuffer(this);
2260         for (unsigned i = 0; i < numberOfCallLinkInfos(); ++i) {
2261             if (callLinkInfo(i).isLinked()) {
2262                 if (ClosureCallStubRoutine* stub = callLinkInfo(i).stub.get()) {
2263                     if (!Heap::isMarked(stub->structure())
2264                         || !Heap::isMarked(stub->executable())) {
2265                         if (verboseUnlinking)
2266                             dataLogF("Clearing closure call from %p to %p, stub routine %p.\n", this, stub->executable(), stub);
2267                         callLinkInfo(i).unlink(*m_globalData, repatchBuffer);
2268                     }
2269                 } else if (!Heap::isMarked(callLinkInfo(i).callee.get())) {
2270                     if (verboseUnlinking)
2271                         dataLogF("Clearing call from %p to %p.\n", this, callLinkInfo(i).callee.get());
2272                     callLinkInfo(i).unlink(*m_globalData, repatchBuffer);
2273                 }
2274             }
2275             if (!!callLinkInfo(i).lastSeenCallee
2276                 && !Heap::isMarked(callLinkInfo(i).lastSeenCallee.get()))
2277                 callLinkInfo(i).lastSeenCallee.clear();
2278         }
2279         for (size_t size = m_structureStubInfos.size(), i = 0; i < size; ++i) {
2280             StructureStubInfo& stubInfo = m_structureStubInfos[i];
2281             
2282             if (stubInfo.visitWeakReferences())
2283                 continue;
2284             
2285             resetStubInternal(repatchBuffer, stubInfo);
2286         }
2287     }
2288 #endif
2289 }
2290
2291 #if ENABLE(JIT)
2292 void CodeBlock::resetStub(StructureStubInfo& stubInfo)
2293 {
2294     if (stubInfo.accessType == access_unset)
2295         return;
2296     
2297     RepatchBuffer repatchBuffer(this);
2298     resetStubInternal(repatchBuffer, stubInfo);
2299 }
2300
2301 void CodeBlock::resetStubInternal(RepatchBuffer& repatchBuffer, StructureStubInfo& stubInfo)
2302 {
2303     AccessType accessType = static_cast<AccessType>(stubInfo.accessType);
2304     
2305     if (verboseUnlinking)
2306         dataLogF("Clearing structure cache (kind %d) in %p.\n", stubInfo.accessType, this);
2307     
2308     if (isGetByIdAccess(accessType)) {
2309         if (getJITCode().jitType() == JITCode::DFGJIT)
2310             DFG::dfgResetGetByID(repatchBuffer, stubInfo);
2311         else
2312             JIT::resetPatchGetById(repatchBuffer, &stubInfo);
2313     } else {
2314         ASSERT(isPutByIdAccess(accessType));
2315         if (getJITCode().jitType() == JITCode::DFGJIT)
2316             DFG::dfgResetPutByID(repatchBuffer, stubInfo);
2317         else 
2318             JIT::resetPatchPutById(repatchBuffer, &stubInfo);
2319     }
2320     
2321     stubInfo.reset();
2322 }
2323 #endif
2324
2325 void CodeBlock::stronglyVisitStrongReferences(SlotVisitor& visitor)
2326 {
2327     visitor.append(&m_globalObject);
2328     visitor.append(&m_ownerExecutable);
2329     visitor.append(&m_unlinkedCode);
2330     if (m_rareData)
2331         m_rareData->m_evalCodeCache.visitAggregate(visitor);
2332     visitor.appendValues(m_constantRegisters.data(), m_constantRegisters.size());
2333     for (size_t i = 0; i < m_functionExprs.size(); ++i)
2334         visitor.append(&m_functionExprs[i]);
2335     for (size_t i = 0; i < m_functionDecls.size(); ++i)
2336         visitor.append(&m_functionDecls[i]);
2337
2338     updateAllPredictions(Collection);
2339 }
2340
2341 void CodeBlock::stronglyVisitWeakReferences(SlotVisitor& visitor)
2342 {
2343     UNUSED_PARAM(visitor);
2344
2345 #if ENABLE(DFG_JIT)
2346     if (!m_dfgData)
2347         return;
2348
2349     for (unsigned i = 0; i < m_dfgData->transitions.size(); ++i) {
2350         if (!!m_dfgData->transitions[i].m_codeOrigin)
2351             visitor.append(&m_dfgData->transitions[i].m_codeOrigin); // Almost certainly not necessary, since the code origin should also be a weak reference. Better to be safe, though.
2352         visitor.append(&m_dfgData->transitions[i].m_from);
2353         visitor.append(&m_dfgData->transitions[i].m_to);
2354     }
2355     
2356     for (unsigned i = 0; i < m_dfgData->weakReferences.size(); ++i)
2357         visitor.append(&m_dfgData->weakReferences[i]);
2358 #endif    
2359 }
2360
2361 #if ENABLE(BYTECODE_COMMENTS)
2362 // Finds the comment string for the specified bytecode offset/PC is available. 
2363 const char* CodeBlock::commentForBytecodeOffset(unsigned bytecodeOffset)
2364 {
2365     ASSERT(bytecodeOffset < instructions().size());
2366
2367     Vector<Comment>& comments = m_bytecodeComments;
2368     size_t numberOfComments = comments.size();
2369     const char* result = 0;
2370
2371     if (!numberOfComments)
2372         return 0; // No comments to match with.
2373
2374     // The next match is most likely the next comment in the list.
2375     // Do a quick check to see if that is a match first.
2376     // m_bytecodeCommentIterator should already be pointing to the
2377     // next comment we should check.
2378
2379     ASSERT(m_bytecodeCommentIterator < comments.size());
2380
2381     size_t i = m_bytecodeCommentIterator;
2382     size_t commentPC = comments[i].pc;
2383     if (commentPC == bytecodeOffset) {
2384         // We've got a match. All done!
2385         m_bytecodeCommentIterator = i;
2386         result = comments[i].string;
2387     } else if (commentPC > bytecodeOffset) {
2388         // The current comment is already greater than the requested PC.
2389         // Start searching from the first comment.
2390         i = 0;
2391     } else {
2392         // Otherwise, the current comment's PC is less than the requested PC.
2393         // Hence, we can just start searching from the next comment in the
2394         // list.
2395         i++;
2396     }
2397
2398     // If the result is still not found, do a linear search in the range
2399     // that we've determined above.
2400     if (!result) {
2401         for (; i < comments.size(); ++i) {
2402             commentPC = comments[i].pc;
2403             if (commentPC == bytecodeOffset) {
2404                 result = comments[i].string;
2405                 break;
2406             }
2407             if (comments[i].pc > bytecodeOffset) {
2408                 // The current comment PC is already past the requested
2409                 // bytecodeOffset. Hence, there are no more possible
2410                 // matches. Just fail.
2411                 break;
2412             }
2413         }
2414     }
2415
2416     // Update the iterator to point to the next comment.
2417     if (++i >= numberOfComments) {
2418         // At most point to the last comment entry. This ensures that the
2419         // next time we call this function, the quick checks will at least
2420         // have one entry to check and can fail fast if appropriate.
2421         i = numberOfComments - 1;
2422     }
2423     m_bytecodeCommentIterator = i;
2424     return result;
2425 }
2426
2427 void CodeBlock::dumpBytecodeComments()
2428 {
2429     Vector<Comment>& comments = m_bytecodeComments;
2430     printf("Comments for codeblock %p: size %lu\n", this, comments.size());
2431     for (size_t i = 0; i < comments.size(); ++i)
2432         printf("     pc %lu : '%s'\n", comments[i].pc, comments[i].string);
2433     printf("End of comments for codeblock %p\n", this);
2434 }
2435 #endif // ENABLE_BYTECODE_COMMENTS
2436
2437 HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset)
2438 {
2439     ASSERT(bytecodeOffset < instructions().size());
2440
2441     if (!m_rareData)
2442         return 0;
2443     
2444     Vector<HandlerInfo>& exceptionHandlers = m_rareData->m_exceptionHandlers;
2445     for (size_t i = 0; i < exceptionHandlers.size(); ++i) {
2446         // Handlers are ordered innermost first, so the first handler we encounter
2447         // that contains the source address is the correct handler to use.
2448         if (exceptionHandlers[i].start <= bytecodeOffset && exceptionHandlers[i].end >= bytecodeOffset)
2449             return &exceptionHandlers[i];
2450     }
2451
2452     return 0;
2453 }
2454
2455 int CodeBlock::lineNumberForBytecodeOffset(unsigned bytecodeOffset)
2456 {
2457     ASSERT(bytecodeOffset < instructions().size());
2458     return m_ownerExecutable->lineNo() + m_unlinkedCode->lineNumberForBytecodeOffset(bytecodeOffset);
2459 }
2460
2461 void CodeBlock::expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset)
2462 {
2463     m_unlinkedCode->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset);
2464     divot += m_sourceOffset;
2465 }
2466
2467 void CodeBlock::shrinkToFit(ShrinkMode shrinkMode)
2468 {
2469 #if ENABLE(LLINT)
2470     m_llintCallLinkInfos.shrinkToFit();
2471 #endif
2472 #if ENABLE(JIT)
2473     m_structureStubInfos.shrinkToFit();
2474     m_callLinkInfos.shrinkToFit();
2475 #endif
2476 #if ENABLE(VALUE_PROFILER)
2477     if (shrinkMode == EarlyShrink)
2478         m_argumentValueProfiles.shrinkToFit();
2479     m_rareCaseProfiles.shrinkToFit();
2480     m_specialFastCaseProfiles.shrinkToFit();
2481 #endif
2482     
2483     if (shrinkMode == EarlyShrink) {
2484         m_identifiers.shrinkToFit();
2485         m_functionDecls.shrinkToFit();
2486         m_functionExprs.shrinkToFit();
2487         m_constantRegisters.shrinkToFit();
2488     } // else don't shrink these, because we would have already pointed pointers into these tables.
2489
2490     if (m_rareData) {
2491         m_rareData->m_exceptionHandlers.shrinkToFit();
2492         m_rareData->m_immediateSwitchJumpTables.shrinkToFit();
2493         m_rareData->m_characterSwitchJumpTables.shrinkToFit();
2494         m_rareData->m_stringSwitchJumpTables.shrinkToFit();
2495 #if ENABLE(JIT)
2496         m_rareData->m_callReturnIndexVector.shrinkToFit();
2497 #endif
2498 #if ENABLE(DFG_JIT)
2499         m_rareData->m_inlineCallFrames.shrinkToFit();
2500         m_rareData->m_codeOrigins.shrinkToFit();
2501 #endif
2502     }
2503     
2504 #if ENABLE(DFG_JIT)
2505     if (m_dfgData) {
2506         m_dfgData->osrEntry.shrinkToFit();
2507         m_dfgData->osrExit.shrinkToFit();
2508         m_dfgData->speculationRecovery.shrinkToFit();
2509         m_dfgData->weakReferences.shrinkToFit();
2510         m_dfgData->transitions.shrinkToFit();
2511         m_dfgData->minifiedDFG.prepareAndShrink();
2512         m_dfgData->variableEventStream.shrinkToFit();
2513     }
2514 #endif
2515 }
2516
2517 void CodeBlock::createActivation(CallFrame* callFrame)
2518 {
2519     ASSERT(codeType() == FunctionCode);
2520     ASSERT(needsFullScopeChain());
2521     ASSERT(!callFrame->uncheckedR(activationRegister()).jsValue());
2522     JSActivation* activation = JSActivation::create(callFrame->globalData(), callFrame, this);
2523     callFrame->uncheckedR(activationRegister()) = JSValue(activation);
2524     callFrame->setScope(activation);
2525 }
2526
2527 unsigned CodeBlock::addOrFindConstant(JSValue v)
2528 {
2529     unsigned numberOfConstants = numberOfConstantRegisters();
2530     for (unsigned i = 0; i < numberOfConstants; ++i) {
2531         if (getConstant(FirstConstantRegisterIndex + i) == v)
2532             return i;
2533     }
2534     return addConstant(v);
2535 }
2536
2537 #if ENABLE(JIT)
2538 void CodeBlock::unlinkCalls()
2539 {
2540     if (!!m_alternative)
2541         m_alternative->unlinkCalls();
2542 #if ENABLE(LLINT)
2543     for (size_t i = 0; i < m_llintCallLinkInfos.size(); ++i) {
2544         if (m_llintCallLinkInfos[i].isLinked())
2545             m_llintCallLinkInfos[i].unlink();
2546     }
2547 #endif
2548     if (!m_callLinkInfos.size())
2549         return;
2550     if (!m_globalData->canUseJIT())
2551         return;
2552     RepatchBuffer repatchBuffer(this);
2553     for (size_t i = 0; i < m_callLinkInfos.size(); i++) {
2554         if (!m_callLinkInfos[i].isLinked())
2555             continue;
2556         m_callLinkInfos[i].unlink(*m_globalData, repatchBuffer);
2557     }
2558 }
2559
2560 void CodeBlock::unlinkIncomingCalls()
2561 {
2562 #if ENABLE(LLINT)
2563     while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end())
2564         m_incomingLLIntCalls.begin()->unlink();
2565 #endif
2566     if (m_incomingCalls.isEmpty())
2567         return;
2568     RepatchBuffer repatchBuffer(this);
2569     while (m_incomingCalls.begin() != m_incomingCalls.end())
2570         m_incomingCalls.begin()->unlink(*m_globalData, repatchBuffer);
2571 }
2572 #endif // ENABLE(JIT)
2573
2574 #if ENABLE(LLINT)
2575 Instruction* CodeBlock::adjustPCIfAtCallSite(Instruction* potentialReturnPC)
2576 {
2577     ASSERT(potentialReturnPC);
2578
2579     unsigned returnPCOffset = potentialReturnPC - instructions().begin();
2580     Instruction* adjustedPC;
2581     unsigned opcodeLength;
2582
2583     // If we are at a callsite, the LLInt stores the PC after the call
2584     // instruction rather than the PC of the call instruction. This requires
2585     // some correcting. If so, we can rely on the fact that the preceding
2586     // instruction must be one of the call instructions, so either it's a
2587     // call_varargs or it's a call, construct, or eval.
2588     //
2589     // If we are not at a call site, then we need to guard against the
2590     // possibility of peeking past the start of the bytecode range for this
2591     // codeBlock. Hence, we do a bounds check before we peek at the
2592     // potential "preceding" instruction.
2593     //     The bounds check is done by comparing the offset of the potential
2594     // returnPC with the length of the opcode. If there is room for a call
2595     // instruction before the returnPC, then the offset of the returnPC must
2596     // be greater than the size of the call opcode we're looking for.
2597
2598     // The determination of the call instruction present (if we are at a
2599     // callsite) depends on the following assumptions. So, assert that
2600     // they are still true:
2601     ASSERT(OPCODE_LENGTH(op_call_varargs) <= OPCODE_LENGTH(op_call));
2602     ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct));
2603     ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_call_eval));
2604
2605     // Check for the case of a preceeding op_call_varargs:
2606     opcodeLength = OPCODE_LENGTH(op_call_varargs);
2607     adjustedPC = potentialReturnPC - opcodeLength;
2608     if ((returnPCOffset >= opcodeLength)
2609         && (adjustedPC->u.pointer == LLInt::getCodePtr(llint_op_call_varargs))) {
2610         return adjustedPC;
2611     }
2612
2613     // Check for the case of the other 3 call instructions:
2614     opcodeLength = OPCODE_LENGTH(op_call);
2615     adjustedPC = potentialReturnPC - opcodeLength;
2616     if ((returnPCOffset >= opcodeLength)
2617         && (adjustedPC->u.pointer == LLInt::getCodePtr(llint_op_call)
2618             || adjustedPC->u.pointer == LLInt::getCodePtr(llint_op_construct)
2619             || adjustedPC->u.pointer == LLInt::getCodePtr(llint_op_call_eval))) {
2620         return adjustedPC;
2621     }
2622
2623     // Not a call site. No need to adjust PC. Just return the original.
2624     return potentialReturnPC;
2625 }
2626 #endif // ENABLE(LLINT)
2627
2628 #if ENABLE(JIT)
2629 ClosureCallStubRoutine* CodeBlock::findClosureCallForReturnPC(ReturnAddressPtr returnAddress)
2630 {
2631     for (unsigned i = m_callLinkInfos.size(); i--;) {
2632         CallLinkInfo& info = m_callLinkInfos[i];
2633         if (!info.stub)
2634             continue;
2635         if (!info.stub->code().executableMemory()->contains(returnAddress.value()))
2636             continue;
2637         
2638         return info.stub.get();
2639     }
2640     
2641     // The stub routine may have been jettisoned. This is rare, but we have to handle it.
2642     const JITStubRoutineSet& set = m_globalData->heap.jitStubRoutines();
2643     for (unsigned i = set.size(); i--;) {
2644         GCAwareJITStubRoutine* genericStub = set.at(i);
2645         if (!genericStub->isClosureCall())
2646             continue;
2647         ClosureCallStubRoutine* stub = static_cast<ClosureCallStubRoutine*>(genericStub);
2648         if (!stub->code().executableMemory()->contains(returnAddress.value()))
2649             continue;
2650         return stub;
2651     }
2652     
2653     return 0;
2654 }
2655 #endif
2656
2657 unsigned CodeBlock::bytecodeOffset(ExecState* exec, ReturnAddressPtr returnAddress)
2658 {
2659     UNUSED_PARAM(exec);
2660     UNUSED_PARAM(returnAddress);
2661 #if ENABLE(LLINT)
2662 #if !ENABLE(LLINT_C_LOOP)
2663     // When using the JIT, we could have addresses that are not bytecode
2664     // addresses. We check if the return address is in the LLint glue and
2665     // opcode handlers range here to ensure that we are looking at bytecode
2666     // before attempting to convert the return address into a bytecode offset.
2667     //
2668     // In the case of the C Loop LLInt, the JIT is disabled, and the only
2669     // valid return addresses should be bytecode PCs. So, we can and need to
2670     // forego this check because when we do not ENABLE(COMPUTED_GOTO_OPCODES),
2671     // then the bytecode "PC"s are actually the opcodeIDs and are not bounded
2672     // by llint_begin and llint_end.
2673     if (returnAddress.value() >= LLInt::getCodePtr(llint_begin)
2674         && returnAddress.value() <= LLInt::getCodePtr(llint_end))
2675 #endif
2676     {
2677         ASSERT(exec->codeBlock());
2678         ASSERT(exec->codeBlock() == this);
2679         ASSERT(JITCode::isBaselineCode(getJITType()));
2680         Instruction* instruction = exec->currentVPC();
2681         ASSERT(instruction);
2682
2683         instruction = adjustPCIfAtCallSite(instruction);
2684         return bytecodeOffset(instruction);
2685     }
2686 #endif // !ENABLE(LLINT)
2687
2688 #if ENABLE(JIT)
2689     if (!m_rareData)
2690         return 1;
2691     Vector<CallReturnOffsetToBytecodeOffset>& callIndices = m_rareData->m_callReturnIndexVector;
2692     if (!callIndices.size())
2693         return 1;
2694     
2695     if (getJITCode().getExecutableMemory()->contains(returnAddress.value())) {
2696         unsigned callReturnOffset = getJITCode().offsetOf(returnAddress.value());
2697         CallReturnOffsetToBytecodeOffset* result =
2698             binarySearch<CallReturnOffsetToBytecodeOffset, unsigned, getCallReturnOffset>(callIndices.begin(), callIndices.size(), callReturnOffset);
2699         ASSERT(result->callReturnOffset == callReturnOffset);
2700         return result->bytecodeOffset;
2701     }
2702
2703     return findClosureCallForReturnPC(returnAddress)->codeOrigin().bytecodeIndex;
2704 #endif // ENABLE(JIT)
2705
2706 #if !ENABLE(LLINT) && !ENABLE(JIT)
2707     return 1;
2708 #endif
2709 }
2710
2711 #if ENABLE(DFG_JIT)
2712 bool CodeBlock::codeOriginForReturn(ReturnAddressPtr returnAddress, CodeOrigin& codeOrigin)
2713 {
2714     if (!hasCodeOrigins())
2715         return false;
2716
2717     if (!getJITCode().getExecutableMemory()->contains(returnAddress.value())) {
2718         codeOrigin = findClosureCallForReturnPC(returnAddress)->codeOrigin();
2719         return true;
2720     }
2721     
2722     unsigned offset = getJITCode().offsetOf(returnAddress.value());
2723     CodeOriginAtCallReturnOffset* entry = binarySearch<CodeOriginAtCallReturnOffset, unsigned, getCallReturnOffsetForCodeOrigin>(codeOrigins().begin(), codeOrigins().size(), offset, WTF::KeyMustNotBePresentInArray);
2724     if (entry->callReturnOffset != offset)
2725         return false;
2726     codeOrigin = entry->codeOrigin;
2727     return true;
2728 }
2729 #endif // ENABLE(DFG_JIT)
2730
2731 void CodeBlock::clearEvalCache()
2732 {
2733     if (!!m_alternative)
2734         m_alternative->clearEvalCache();
2735     if (!m_rareData)
2736         return;
2737     m_rareData->m_evalCodeCache.clear();
2738 }
2739
2740 template<typename T>
2741 inline void replaceExistingEntries(Vector<T>& target, Vector<T>& source)
2742 {
2743     ASSERT(target.size() <= source.size());
2744     for (size_t i = 0; i < target.size(); ++i)
2745         target[i] = source[i];
2746 }
2747
2748 void CodeBlock::copyPostParseDataFrom(CodeBlock* alternative)
2749 {
2750     if (!alternative)
2751         return;
2752     
2753     replaceExistingEntries(m_constantRegisters, alternative->m_constantRegisters);
2754     replaceExistingEntries(m_functionDecls, alternative->m_functionDecls);
2755     replaceExistingEntries(m_functionExprs, alternative->m_functionExprs);
2756     if (!!m_rareData && !!alternative->m_rareData)
2757         replaceExistingEntries(m_rareData->m_constantBuffers, alternative->m_rareData->m_constantBuffers);
2758 }
2759
2760 void CodeBlock::copyPostParseDataFromAlternative()
2761 {
2762     copyPostParseDataFrom(m_alternative.get());
2763 }
2764
2765 #if ENABLE(JIT)
2766 void CodeBlock::reoptimize()
2767 {
2768     ASSERT(replacement() != this);
2769     ASSERT(replacement()->alternative() == this);
2770     replacement()->tallyFrequentExitSites();
2771     if (DFG::shouldShowDisassembly())
2772         dataLogF("DFG CodeBlock %p will be jettisoned due to reoptimization of %p.\n", replacement(), this);
2773     replacement()->jettison();
2774     countReoptimization();
2775     optimizeAfterWarmUp();
2776 }
2777
2778 CodeBlock* ProgramCodeBlock::replacement()
2779 {
2780     return &static_cast<ProgramExecutable*>(ownerExecutable())->generatedBytecode();
2781 }
2782
2783 CodeBlock* EvalCodeBlock::replacement()
2784 {
2785     return &static_cast<EvalExecutable*>(ownerExecutable())->generatedBytecode();
2786 }
2787
2788 CodeBlock* FunctionCodeBlock::replacement()
2789 {
2790     return &static_cast<FunctionExecutable*>(ownerExecutable())->generatedBytecodeFor(m_isConstructor ? CodeForConstruct : CodeForCall);
2791 }
2792
2793 JSObject* ProgramCodeBlock::compileOptimized(ExecState* exec, JSScope* scope, unsigned bytecodeIndex)
2794 {
2795     if (replacement()->getJITType() == JITCode::nextTierJIT(getJITType()))
2796         return 0;
2797     JSObject* error = static_cast<ProgramExecutable*>(ownerExecutable())->compileOptimized(exec, scope, bytecodeIndex);
2798     return error;
2799 }
2800
2801 JSObject* EvalCodeBlock::compileOptimized(ExecState* exec, JSScope* scope, unsigned bytecodeIndex)
2802 {
2803     if (replacement()->getJITType() == JITCode::nextTierJIT(getJITType()))
2804         return 0;
2805     JSObject* error = static_cast<EvalExecutable*>(ownerExecutable())->compileOptimized(exec, scope, bytecodeIndex);
2806     return error;
2807 }
2808
2809 JSObject* FunctionCodeBlock::compileOptimized(ExecState* exec, JSScope* scope, unsigned bytecodeIndex)
2810 {
2811     if (replacement()->getJITType() == JITCode::nextTierJIT(getJITType()))
2812         return 0;
2813     JSObject* error = static_cast<FunctionExecutable*>(ownerExecutable())->compileOptimizedFor(exec, scope, bytecodeIndex, m_isConstructor ? CodeForConstruct : CodeForCall);
2814     return error;
2815 }
2816
2817 DFG::CapabilityLevel ProgramCodeBlock::canCompileWithDFGInternal()
2818 {
2819     return DFG::canCompileProgram(this);
2820 }
2821
2822 DFG::CapabilityLevel EvalCodeBlock::canCompileWithDFGInternal()
2823 {
2824     return DFG::canCompileEval(this);
2825 }
2826
2827 DFG::CapabilityLevel FunctionCodeBlock::canCompileWithDFGInternal()
2828 {
2829     if (m_isConstructor)
2830         return DFG::canCompileFunctionForConstruct(this);
2831     return DFG::canCompileFunctionForCall(this);
2832 }
2833
2834 void ProgramCodeBlock::jettison()
2835 {
2836     ASSERT(JITCode::isOptimizingJIT(getJITType()));
2837     ASSERT(this == replacement());
2838     if (DFG::shouldShowDisassembly())
2839         dataLogF("Jettisoning DFG CodeBlock %p.\n", this);
2840     static_cast<ProgramExecutable*>(ownerExecutable())->jettisonOptimizedCode(*globalData());
2841 }
2842
2843 void EvalCodeBlock::jettison()
2844 {
2845     ASSERT(JITCode::isOptimizingJIT(getJITType()));
2846     ASSERT(this == replacement());
2847     if (DFG::shouldShowDisassembly())
2848         dataLogF("Jettisoning DFG CodeBlock %p.\n", this);
2849     static_cast<EvalExecutable*>(ownerExecutable())->jettisonOptimizedCode(*globalData());
2850 }
2851
2852 void FunctionCodeBlock::jettison()
2853 {
2854     ASSERT(JITCode::isOptimizingJIT(getJITType()));
2855     ASSERT(this == replacement());
2856     if (DFG::shouldShowDisassembly())
2857         dataLogF("Jettisoning DFG CodeBlock %p.\n", this);
2858     static_cast<FunctionExecutable*>(ownerExecutable())->jettisonOptimizedCodeFor(*globalData(), m_isConstructor ? CodeForConstruct : CodeForCall);
2859 }
2860
2861 bool ProgramCodeBlock::jitCompileImpl(ExecState* exec)
2862 {
2863     ASSERT(getJITType() == JITCode::InterpreterThunk);
2864     ASSERT(this == replacement());
2865     return static_cast<ProgramExecutable*>(ownerExecutable())->jitCompile(exec);
2866 }
2867
2868 bool EvalCodeBlock::jitCompileImpl(ExecState* exec)
2869 {
2870     ASSERT(getJITType() == JITCode::InterpreterThunk);
2871     ASSERT(this == replacement());
2872     return static_cast<EvalExecutable*>(ownerExecutable())->jitCompile(exec);
2873 }
2874
2875 bool FunctionCodeBlock::jitCompileImpl(ExecState* exec)
2876 {
2877     ASSERT(getJITType() == JITCode::InterpreterThunk);
2878     ASSERT(this == replacement());
2879     return static_cast<FunctionExecutable*>(ownerExecutable())->jitCompileFor(exec, m_isConstructor ? CodeForConstruct : CodeForCall);
2880 }
2881 #endif
2882
2883 #if ENABLE(VALUE_PROFILER)
2884 ArrayProfile* CodeBlock::getArrayProfile(unsigned bytecodeOffset)
2885 {
2886     for (unsigned i = 0; i < m_arrayProfiles.size(); ++i) {
2887         if (m_arrayProfiles[i].bytecodeOffset() == bytecodeOffset)
2888             return &m_arrayProfiles[i];
2889     }
2890     return 0;
2891 }
2892
2893 ArrayProfile* CodeBlock::getOrAddArrayProfile(unsigned bytecodeOffset)
2894 {
2895     ArrayProfile* result = getArrayProfile(bytecodeOffset);
2896     if (result)
2897         return result;
2898     return addArrayProfile(bytecodeOffset);
2899 }
2900
2901 void CodeBlock::updateAllPredictionsAndCountLiveness(
2902     OperationInProgress operation, unsigned& numberOfLiveNonArgumentValueProfiles, unsigned& numberOfSamplesInProfiles)
2903 {
2904     numberOfLiveNonArgumentValueProfiles = 0;
2905     numberOfSamplesInProfiles = 0; // If this divided by ValueProfile::numberOfBuckets equals numberOfValueProfiles() then value profiles are full.
2906     for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) {
2907         ValueProfile* profile = getFromAllValueProfiles(i);
2908         unsigned numSamples = profile->totalNumberOfSamples();
2909         if (numSamples > ValueProfile::numberOfBuckets)
2910             numSamples = ValueProfile::numberOfBuckets; // We don't want profiles that are extremely hot to be given more weight.
2911         numberOfSamplesInProfiles += numSamples;
2912         if (profile->m_bytecodeOffset < 0) {
2913             profile->computeUpdatedPrediction(operation);
2914             continue;
2915         }
2916         if (profile->numberOfSamples() || profile->m_prediction != SpecNone)
2917             numberOfLiveNonArgumentValueProfiles++;
2918         profile->computeUpdatedPrediction(operation);
2919     }
2920     
2921 #if ENABLE(DFG_JIT)
2922     m_lazyOperandValueProfiles.computeUpdatedPredictions(operation);
2923 #endif
2924 }
2925
2926 void CodeBlock::updateAllValueProfilePredictions(OperationInProgress operation)
2927 {
2928     unsigned ignoredValue1, ignoredValue2;
2929     updateAllPredictionsAndCountLiveness(operation, ignoredValue1, ignoredValue2);
2930 }
2931
2932 void CodeBlock::updateAllArrayPredictions(OperationInProgress operation)
2933 {
2934     for (unsigned i = m_arrayProfiles.size(); i--;)
2935         m_arrayProfiles[i].computeUpdatedPrediction(this, operation);
2936     
2937     // Don't count these either, for similar reasons.
2938     for (unsigned i = m_arrayAllocationProfiles.size(); i--;)
2939         m_arrayAllocationProfiles[i].updateIndexingType();
2940 }
2941
2942 void CodeBlock::updateAllPredictions(OperationInProgress operation)
2943 {
2944     updateAllValueProfilePredictions(operation);
2945     updateAllArrayPredictions(operation);
2946 }
2947
2948 bool CodeBlock::shouldOptimizeNow()
2949 {
2950 #if ENABLE(JIT_VERBOSE_OSR)
2951     dataLogF("Considering optimizing %p...\n", this);
2952 #endif
2953
2954 #if ENABLE(VERBOSE_VALUE_PROFILE)
2955     dumpValueProfiles();
2956 #endif
2957
2958     if (m_optimizationDelayCounter >= Options::maximumOptimizationDelay())
2959         return true;
2960     
2961     updateAllArrayPredictions();
2962     
2963     unsigned numberOfLiveNonArgumentValueProfiles;
2964     unsigned numberOfSamplesInProfiles;
2965     updateAllPredictionsAndCountLiveness(NoOperation, numberOfLiveNonArgumentValueProfiles, numberOfSamplesInProfiles);
2966
2967 #if ENABLE(JIT_VERBOSE_OSR)
2968     dataLogF("Profile hotness: %lf (%u / %u), %lf (%u / %u)\n", (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles(), numberOfLiveNonArgumentValueProfiles, numberOfValueProfiles(), (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / numberOfValueProfiles(), numberOfSamplesInProfiles, ValueProfile::numberOfBuckets * numberOfValueProfiles());
2969 #endif
2970
2971     if ((!numberOfValueProfiles() || (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles() >= Options::desiredProfileLivenessRate())
2972         && (!totalNumberOfValueProfiles() || (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / totalNumberOfValueProfiles() >= Options::desiredProfileFullnessRate())
2973         && static_cast<unsigned>(m_optimizationDelayCounter) + 1 >= Options::minimumOptimizationDelay())
2974         return true;
2975     
2976     ASSERT(m_optimizationDelayCounter < std::numeric_limits<uint8_t>::max());
2977     m_optimizationDelayCounter++;
2978     optimizeAfterWarmUp();
2979     return false;
2980 }
2981 #endif
2982
2983 #if ENABLE(DFG_JIT)
2984 void CodeBlock::tallyFrequentExitSites()
2985 {
2986     ASSERT(getJITType() == JITCode::DFGJIT);
2987     ASSERT(alternative()->getJITType() == JITCode::BaselineJIT);
2988     ASSERT(!!m_dfgData);
2989     
2990     CodeBlock* profiledBlock = alternative();
2991     
2992     for (unsigned i = 0; i < m_dfgData->osrExit.size(); ++i) {
2993         DFG::OSRExit& exit = m_dfgData->osrExit[i];
2994         
2995         if (!exit.considerAddingAsFrequentExitSite(this, profiledBlock))
2996             continue;
2997         
2998 #if DFG_ENABLE(DEBUG_VERBOSE)
2999         dataLogF("OSR exit #%u (bc#%u, @%u, %s) for code block %p occurred frequently; counting as frequent exit site.\n", i, exit.m_codeOrigin.bytecodeIndex, exit.m_nodeIndex, DFG::exitKindToString(exit.m_kind), this);
3000 #endif
3001     }
3002 }
3003 #endif // ENABLE(DFG_JIT)
3004
3005 #if ENABLE(VERBOSE_VALUE_PROFILE)
3006 void CodeBlock::dumpValueProfiles()
3007 {
3008     dataLogF("ValueProfile for %p:\n", this);
3009     for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) {
3010         ValueProfile* profile = getFromAllValueProfiles(i);
3011         if (profile->m_bytecodeOffset < 0) {
3012             ASSERT(profile->m_bytecodeOffset == -1);
3013             dataLogF("   arg = %u: ", i);
3014         } else
3015             dataLogF("   bc = %d: ", profile->m_bytecodeOffset);
3016         if (!profile->numberOfSamples() && profile->m_prediction == SpecNone) {
3017             dataLogF("<empty>\n");
3018             continue;
3019         }
3020         profile->dump(WTF::dataFile());
3021         dataLogF("\n");
3022     }
3023     dataLogF("RareCaseProfile for %p:\n", this);
3024     for (unsigned i = 0; i < numberOfRareCaseProfiles(); ++i) {
3025         RareCaseProfile* profile = rareCaseProfile(i);
3026         dataLogF("   bc = %d: %u\n", profile->m_bytecodeOffset, profile->m_counter);
3027     }
3028     dataLogF("SpecialFastCaseProfile for %p:\n", this);
3029     for (unsigned i = 0; i < numberOfSpecialFastCaseProfiles(); ++i) {
3030         RareCaseProfile* profile = specialFastCaseProfile(i);
3031         dataLogF("   bc = %d: %u\n", profile->m_bytecodeOffset, profile->m_counter);
3032     }
3033 }
3034 #endif // ENABLE(VERBOSE_VALUE_PROFILE)
3035
3036 size_t CodeBlock::predictedMachineCodeSize()
3037 {
3038     // This will be called from CodeBlock::CodeBlock before either m_globalData or the
3039     // instructions have been initialized. It's OK to return 0 because what will really
3040     // matter is the recomputation of this value when the slow path is triggered.
3041     if (!m_globalData)
3042         return 0;
3043     
3044     if (!m_globalData->machineCodeBytesPerBytecodeWordForBaselineJIT)
3045         return 0; // It's as good of a prediction as we'll get.
3046     
3047     // Be conservative: return a size that will be an overestimation 84% of the time.
3048     double multiplier = m_globalData->machineCodeBytesPerBytecodeWordForBaselineJIT.mean() +
3049         m_globalData->machineCodeBytesPerBytecodeWordForBaselineJIT.standardDeviation();
3050     
3051     // Be paranoid: silently reject bogus multipiers. Silently doing the "wrong" thing
3052     // here is OK, since this whole method is just a heuristic.
3053     if (multiplier < 0 || multiplier > 1000)
3054         return 0;
3055     
3056     double doubleResult = multiplier * m_instructions.size();
3057     
3058     // Be even more paranoid: silently reject values that won't fit into a size_t. If
3059     // the function is so huge that we can't even fit it into virtual memory then we
3060     // should probably have some other guards in place to prevent us from even getting
3061     // to this point.
3062     if (doubleResult > std::numeric_limits<size_t>::max())
3063         return 0;
3064     
3065     return static_cast<size_t>(doubleResult);
3066 }
3067
3068 bool CodeBlock::usesOpcode(OpcodeID opcodeID)
3069 {
3070     Interpreter* interpreter = globalData()->interpreter;
3071     Instruction* instructionsBegin = instructions().begin();
3072     unsigned instructionCount = instructions().size();
3073     
3074     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) {
3075         switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset].u.opcode)) {
3076 #define DEFINE_OP(curOpcode, length)        \
3077         case curOpcode:                     \
3078             if (curOpcode == opcodeID)      \
3079                 return true;                \
3080             bytecodeOffset += length;       \
3081             break;
3082             FOR_EACH_OPCODE_ID(DEFINE_OP)
3083 #undef DEFINE_OP
3084         default:
3085             ASSERT_NOT_REACHED();
3086             break;
3087         }
3088     }
3089     
3090     return false;
3091 }
3092
3093 String CodeBlock::nameForRegister(int registerNumber)
3094 {
3095     SymbolTable::iterator end = symbolTable()->end();
3096     for (SymbolTable::iterator ptr = symbolTable()->begin(); ptr != end; ++ptr) {
3097         if (ptr->value.getIndex() == registerNumber)
3098             return String(ptr->key);
3099     }
3100     if (needsActivation() && registerNumber == activationRegister())
3101         return ASCIILiteral("activation");
3102     if (registerNumber == thisRegister())
3103         return ASCIILiteral("this");
3104     if (usesArguments()) {
3105         if (registerNumber == argumentsRegister())
3106             return ASCIILiteral("arguments");
3107         if (unmodifiedArgumentsRegister(argumentsRegister()) == registerNumber)
3108             return ASCIILiteral("real arguments");
3109     }
3110     if (registerNumber < 0) {
3111         int argumentPosition = -registerNumber;
3112         argumentPosition -= JSStack::CallFrameHeaderSize + 1;
3113         return String::format("arguments[%3d]", argumentPosition - 1).impl();
3114     }
3115     return "";
3116 }
3117
3118 } // namespace JSC