2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "CodeBlock.h"
35 #include "Interpreter.h"
38 #include <wtf/StringExtras.h>
42 #if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
44 static UString escapeQuotes(const UString& str)
48 while ((pos = result.find('\"', pos)) >= 0) {
49 result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1);
55 static UString valueToSourceString(ExecState* exec, JSValue* val)
57 if (val->isString()) {
59 result += escapeQuotes(val->toString(exec)) + "\"";
63 return val->toString(exec);
66 static CString registerName(int r)
68 if (r == missingThisObjectMarker())
71 return (UString("r") + UString::from(r)).UTF8String();
74 static CString constantName(ExecState* exec, int k, JSValue* value)
76 return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String();
79 static CString idName(int id0, const Identifier& ident)
81 return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String();
84 static UString regexpToSourceString(RegExp* regExp)
86 UString pattern = UString("/") + regExp->pattern() + "/";
89 if (regExp->ignoreCase())
91 if (regExp->multiline())
97 static CString regexpName(int re, RegExp* regexp)
99 return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String();
102 static UString pointerToSourceString(void* p)
104 char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
105 snprintf(buffer, sizeof(buffer), "%p", p);
109 NEVER_INLINE static const char* debugHookName(int debugHookID)
111 switch (static_cast<DebugHookID>(debugHookID)) {
112 case DidEnterCallFrame:
113 return "didEnterCallFrame";
114 case WillLeaveCallFrame:
115 return "willLeaveCallFrame";
116 case WillExecuteStatement:
117 return "willExecuteStatement";
118 case WillExecuteProgram:
119 return "willExecuteProgram";
120 case DidExecuteProgram:
121 return "didExecuteProgram";
122 case DidReachBreakpoint:
123 return "didReachBreakpoint";
126 ASSERT_NOT_REACHED();
130 static int jumpTarget(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int offset)
132 return it - begin + offset;
135 static void printUnaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
137 int r0 = (++it)->u.operand;
138 int r1 = (++it)->u.operand;
140 printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str());
143 static void printBinaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
145 int r0 = (++it)->u.operand;
146 int r1 = (++it)->u.operand;
147 int r2 = (++it)->u.operand;
148 printf("[%4d] %s\t\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
151 static void printConditionalJump(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int location, const char* op)
153 int r0 = (++it)->u.operand;
154 int offset = (++it)->u.operand;
155 printf("[%4d] %s\t\t %s, %d(->%d)\n", location, op, registerName(r0).c_str(), offset, jumpTarget(begin, it, offset));
158 static void printGetByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
160 int r0 = (++it)->u.operand;
161 int r1 = (++it)->u.operand;
162 int id0 = (++it)->u.operand;
163 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
167 static void printPutByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
169 int r0 = (++it)->u.operand;
170 int id0 = (++it)->u.operand;
171 int r1 = (++it)->u.operand;
172 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
176 void CodeBlock::printStructure(const char* name, const Instruction* vPC, int operand) const
178 unsigned instructionOffset = vPC - instructions.begin();
179 printf(" [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structure).UTF8String().c_str());
182 void CodeBlock::printStructures(const Instruction* vPC) const
184 Interpreter* interpreter = globalData->interpreter;
185 unsigned instructionOffset = vPC - instructions.begin();
187 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id)) {
188 printStructure("get_by_id", vPC, 4);
191 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
192 printStructure("get_by_id_self", vPC, 4);
195 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
196 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_proto", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structure).UTF8String().c_str());
199 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
200 printf(" [%4d] %s: %s, %s, %s\n", instructionOffset, "put_by_id_new", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[6].u.structureChain).UTF8String().c_str());
203 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
204 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_chain", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structureChain).UTF8String().c_str());
207 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id)) {
208 printStructure("put_by_id", vPC, 4);
211 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
212 printStructure("put_by_id_replace", vPC, 4);
215 if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) {
216 printStructure("resolve_global", vPC, 4);
220 // These instructions doesn't ref Structures.
221 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));
224 void CodeBlock::dump(ExecState* exec) const
226 Vector<Instruction>::const_iterator begin = instructions.begin();
227 Vector<Instruction>::const_iterator end = instructions.end();
229 size_t instructionCount = 0;
230 for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
231 if (exec->interpreter()->isOpcode(it->u.opcode))
234 printf("%lu instructions; %lu bytes at %p; %d parameter(s); %d callee register(s)\n\n",
235 static_cast<unsigned long>(instructionCount),
236 static_cast<unsigned long>(instructions.size() * sizeof(Instruction)),
237 this, numParameters, numCalleeRegisters);
239 for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
240 dump(exec, begin, it);
242 if (identifiers.size()) {
243 printf("\nIdentifiers:\n");
246 printf(" id%u = %s\n", static_cast<unsigned>(i), identifiers[i].ascii());
248 } while (i != identifiers.size());
251 if (constantRegisters.size()) {
252 printf("\nConstants:\n");
253 unsigned registerIndex = numVars;
256 printf(" r%u = %s\n", registerIndex, valueToSourceString(exec, constantRegisters[i].jsValue(exec)).ascii());
259 } while (i < constantRegisters.size());
262 if (unexpectedConstants.size()) {
263 printf("\nUnexpected Constants:\n");
266 printf(" k%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, unexpectedConstants[i]).ascii());
268 } while (i < unexpectedConstants.size());
271 if (regexps.size()) {
272 printf("\nRegExps:\n");
275 printf(" re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(regexps[i].get()).ascii());
277 } while (i < regexps.size());
280 if (globalResolveInstructions.size() || propertyAccessInstructions.size())
281 printf("\nStructures:\n");
283 if (globalResolveInstructions.size()) {
286 printStructures(&instructions[globalResolveInstructions[i]]);
288 } while (i < globalResolveInstructions.size());
290 if (propertyAccessInstructions.size()) {
293 printStructures(&instructions[propertyAccessInstructions[i].bytecodeIndex]);
295 } while (i < propertyAccessInstructions.size());
298 if (exceptionHandlers.size()) {
299 printf("\nException Handlers:\n");
302 printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, exceptionHandlers[i].start, exceptionHandlers[i].end, exceptionHandlers[i].target);
304 } while (i < exceptionHandlers.size());
307 if (immediateSwitchJumpTables.size()) {
308 printf("Immediate Switch Jump Tables:\n");
311 printf(" %1d = {\n", i);
313 Vector<int32_t>::const_iterator end = immediateSwitchJumpTables[i].branchOffsets.end();
314 for (Vector<int32_t>::const_iterator iter = immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
317 printf("\t\t%4d => %04d\n", entry + immediateSwitchJumpTables[i].min, *iter);
321 } while (i < immediateSwitchJumpTables.size());
324 if (characterSwitchJumpTables.size()) {
325 printf("\nCharacter Switch Jump Tables:\n");
328 printf(" %1d = {\n", i);
330 Vector<int32_t>::const_iterator end = characterSwitchJumpTables[i].branchOffsets.end();
331 for (Vector<int32_t>::const_iterator iter = characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
334 ASSERT(!((i + characterSwitchJumpTables[i].min) & ~0xFFFF));
335 UChar ch = static_cast<UChar>(entry + characterSwitchJumpTables[i].min);
336 printf("\t\t\"%s\" => %04d\n", UString(&ch, 1).ascii(), *iter);
340 } while (i < characterSwitchJumpTables.size());
343 if (stringSwitchJumpTables.size()) {
344 printf("\nString Switch Jump Tables:\n");
347 printf(" %1d = {\n", i);
348 StringJumpTable::StringOffsetTable::const_iterator end = stringSwitchJumpTables[i].offsetTable.end();
349 for (StringJumpTable::StringOffsetTable::const_iterator iter = stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
350 printf("\t\t\"%s\" => %04d\n", UString(iter->first).ascii(), iter->second.branchOffset);
353 } while (i < stringSwitchJumpTables.size());
359 void CodeBlock::dump(ExecState* exec, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it) const
361 int location = it - begin;
362 switch (exec->interpreter()->getOpcodeID(it->u.opcode)) {
364 printf("[%4d] enter\n", location);
367 case op_enter_with_activation: {
368 int r0 = (++it)->u.operand;
369 printf("[%4d] enter_with_activation %s\n", location, registerName(r0).c_str());
372 case op_create_arguments: {
373 printf("[%4d] create_arguments\n", location);
376 case op_convert_this: {
377 int r0 = (++it)->u.operand;
378 printf("[%4d] convert_this %s\n", location, registerName(r0).c_str());
381 case op_unexpected_load: {
382 int r0 = (++it)->u.operand;
383 int k0 = (++it)->u.operand;
384 printf("[%4d] unexpected_load\t %s, %s\n", location, registerName(r0).c_str(), constantName(exec, k0, unexpectedConstants[k0]).c_str());
387 case op_new_object: {
388 int r0 = (++it)->u.operand;
389 printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str());
393 int dst = (++it)->u.operand;
394 int argv = (++it)->u.operand;
395 int argc = (++it)->u.operand;
396 printf("[%4d] new_array\t %s, %s, %d\n", location, registerName(dst).c_str(), registerName(argv).c_str(), argc);
399 case op_new_regexp: {
400 int r0 = (++it)->u.operand;
401 int re0 = (++it)->u.operand;
402 printf("[%4d] new_regexp\t %s, %s\n", location, registerName(r0).c_str(), regexpName(re0, regexps[re0].get()).c_str());
406 int r0 = (++it)->u.operand;
407 int r1 = (++it)->u.operand;
408 printf("[%4d] mov\t\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
412 printUnaryOp(location, it, "not");
416 printBinaryOp(location, it, "eq");
420 printUnaryOp(location, it, "eq_null");
424 printBinaryOp(location, it, "neq");
428 printUnaryOp(location, it, "neq_null");
432 printBinaryOp(location, it, "stricteq");
436 printBinaryOp(location, it, "nstricteq");
440 printBinaryOp(location, it, "less");
444 printBinaryOp(location, it, "lesseq");
448 int r0 = (++it)->u.operand;
449 printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str());
453 int r0 = (++it)->u.operand;
454 printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str());
458 printUnaryOp(location, it, "post_inc");
462 printUnaryOp(location, it, "post_dec");
465 case op_to_jsnumber: {
466 printUnaryOp(location, it, "to_jsnumber");
470 printUnaryOp(location, it, "negate");
474 printBinaryOp(location, it, "add");
479 printBinaryOp(location, it, "mul");
484 printBinaryOp(location, it, "div");
488 printBinaryOp(location, it, "mod");
492 printBinaryOp(location, it, "sub");
497 printBinaryOp(location, it, "lshift");
501 printBinaryOp(location, it, "rshift");
505 printBinaryOp(location, it, "urshift");
509 printBinaryOp(location, it, "bitand");
514 printBinaryOp(location, it, "bitxor");
519 printBinaryOp(location, it, "bitor");
524 printUnaryOp(location, it, "bitnot");
527 case op_instanceof: {
528 int r0 = (++it)->u.operand;
529 int r1 = (++it)->u.operand;
530 int r2 = (++it)->u.operand;
531 int r3 = (++it)->u.operand;
532 printf("[%4d] instanceof\t\t %s, %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), registerName(r3).c_str());
536 printUnaryOp(location, it, "typeof");
539 case op_is_undefined: {
540 printUnaryOp(location, it, "is_undefined");
543 case op_is_boolean: {
544 printUnaryOp(location, it, "is_boolean");
548 printUnaryOp(location, it, "is_number");
552 printUnaryOp(location, it, "is_string");
556 printUnaryOp(location, it, "is_object");
559 case op_is_function: {
560 printUnaryOp(location, it, "is_function");
564 printBinaryOp(location, it, "in");
568 int r0 = (++it)->u.operand;
569 int id0 = (++it)->u.operand;
570 printf("[%4d] resolve\t\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
573 case op_resolve_skip: {
574 int r0 = (++it)->u.operand;
575 int id0 = (++it)->u.operand;
576 int skipLevels = (++it)->u.operand;
577 printf("[%4d] resolve_skip\t %s, %s, %d\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), skipLevels);
580 case op_resolve_global: {
581 int r0 = (++it)->u.operand;
582 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
583 int id0 = (++it)->u.operand;
584 printf("[%4d] resolve_global\t %s, %s, %s\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), idName(id0, identifiers[id0]).c_str());
588 case op_get_scoped_var: {
589 int r0 = (++it)->u.operand;
590 int index = (++it)->u.operand;
591 int skipLevels = (++it)->u.operand;
592 printf("[%4d] get_scoped_var\t %s, %d, %d\n", location, registerName(r0).c_str(), index, skipLevels);
595 case op_put_scoped_var: {
596 int index = (++it)->u.operand;
597 int skipLevels = (++it)->u.operand;
598 int r0 = (++it)->u.operand;
599 printf("[%4d] put_scoped_var\t %d, %d, %s\n", location, index, skipLevels, registerName(r0).c_str());
602 case op_get_global_var: {
603 int r0 = (++it)->u.operand;
604 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
605 int index = (++it)->u.operand;
606 printf("[%4d] get_global_var\t %s, %s, %d\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), index);
609 case op_put_global_var: {
610 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
611 int index = (++it)->u.operand;
612 int r0 = (++it)->u.operand;
613 printf("[%4d] put_global_var\t %s, %d, %s\n", location, valueToSourceString(exec, scope).ascii(), index, registerName(r0).c_str());
616 case op_resolve_base: {
617 int r0 = (++it)->u.operand;
618 int id0 = (++it)->u.operand;
619 printf("[%4d] resolve_base\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
622 case op_resolve_with_base: {
623 int r0 = (++it)->u.operand;
624 int r1 = (++it)->u.operand;
625 int id0 = (++it)->u.operand;
626 printf("[%4d] resolve_with_base %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
629 case op_resolve_func: {
630 int r0 = (++it)->u.operand;
631 int r1 = (++it)->u.operand;
632 int id0 = (++it)->u.operand;
633 printf("[%4d] resolve_func\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
637 printGetByIdOp(location, it, identifiers, "get_by_id");
640 case op_get_by_id_self: {
641 printGetByIdOp(location, it, identifiers, "get_by_id_self");
644 case op_get_by_id_self_list: {
645 printGetByIdOp(location, it, identifiers, "get_by_id_self_list");
648 case op_get_by_id_proto: {
649 printGetByIdOp(location, it, identifiers, "get_by_id_proto");
652 case op_get_by_id_proto_list: {
653 printGetByIdOp(location, it, identifiers, "op_get_by_id_proto_list");
656 case op_get_by_id_chain: {
657 printGetByIdOp(location, it, identifiers, "get_by_id_chain");
660 case op_get_by_id_generic: {
661 printGetByIdOp(location, it, identifiers, "get_by_id_generic");
664 case op_get_array_length: {
665 printGetByIdOp(location, it, identifiers, "get_array_length");
668 case op_get_string_length: {
669 printGetByIdOp(location, it, identifiers, "get_string_length");
673 printPutByIdOp(location, it, identifiers, "put_by_id");
676 case op_put_by_id_replace: {
677 printPutByIdOp(location, it, identifiers, "put_by_id_replace");
680 case op_put_by_id_transition: {
681 printPutByIdOp(location, it, identifiers, "put_by_id_transition");
684 case op_put_by_id_generic: {
685 printPutByIdOp(location, it, identifiers, "put_by_id_generic");
688 case op_put_getter: {
689 int r0 = (++it)->u.operand;
690 int id0 = (++it)->u.operand;
691 int r1 = (++it)->u.operand;
692 printf("[%4d] put_getter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
695 case op_put_setter: {
696 int r0 = (++it)->u.operand;
697 int id0 = (++it)->u.operand;
698 int r1 = (++it)->u.operand;
699 printf("[%4d] put_setter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
703 int r0 = (++it)->u.operand;
704 int r1 = (++it)->u.operand;
705 int id0 = (++it)->u.operand;
706 printf("[%4d] del_by_id\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
709 case op_get_by_val: {
710 int r0 = (++it)->u.operand;
711 int r1 = (++it)->u.operand;
712 int r2 = (++it)->u.operand;
713 printf("[%4d] get_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
716 case op_put_by_val: {
717 int r0 = (++it)->u.operand;
718 int r1 = (++it)->u.operand;
719 int r2 = (++it)->u.operand;
720 printf("[%4d] put_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
723 case op_del_by_val: {
724 int r0 = (++it)->u.operand;
725 int r1 = (++it)->u.operand;
726 int r2 = (++it)->u.operand;
727 printf("[%4d] del_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
730 case op_put_by_index: {
731 int r0 = (++it)->u.operand;
732 unsigned n0 = (++it)->u.operand;
733 int r1 = (++it)->u.operand;
734 printf("[%4d] put_by_index\t %s, %u, %s\n", location, registerName(r0).c_str(), n0, registerName(r1).c_str());
738 int offset = (++it)->u.operand;
739 printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
743 int offset = (++it)->u.operand;
744 printf("[%4d] loop\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
748 printConditionalJump(begin, it, location, "jtrue");
751 case op_loop_if_true: {
752 printConditionalJump(begin, it, location, "loop_if_true");
756 printConditionalJump(begin, it, location, "jfalse");
760 printConditionalJump(begin, it, location, "jeq_null");
764 printConditionalJump(begin, it, location, "jneq_null");
768 int r0 = (++it)->u.operand;
769 int r1 = (++it)->u.operand;
770 int offset = (++it)->u.operand;
771 printf("[%4d] jnless\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, jumpTarget(begin, it, offset));
774 case op_loop_if_less: {
775 int r0 = (++it)->u.operand;
776 int r1 = (++it)->u.operand;
777 int offset = (++it)->u.operand;
778 printf("[%4d] loop_if_less\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, jumpTarget(begin, it, offset));
781 case op_loop_if_lesseq: {
782 int r0 = (++it)->u.operand;
783 int r1 = (++it)->u.operand;
784 int offset = (++it)->u.operand;
785 printf("[%4d] loop_if_lesseq\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, jumpTarget(begin, it, offset));
788 case op_switch_imm: {
789 int tableIndex = (++it)->u.operand;
790 int defaultTarget = (++it)->u.operand;
791 int scrutineeRegister = (++it)->u.operand;
792 printf("[%4d] switch_imm\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
795 case op_switch_char: {
796 int tableIndex = (++it)->u.operand;
797 int defaultTarget = (++it)->u.operand;
798 int scrutineeRegister = (++it)->u.operand;
799 printf("[%4d] switch_char\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
802 case op_switch_string: {
803 int tableIndex = (++it)->u.operand;
804 int defaultTarget = (++it)->u.operand;
805 int scrutineeRegister = (++it)->u.operand;
806 printf("[%4d] switch_string\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
810 int r0 = (++it)->u.operand;
811 int f0 = (++it)->u.operand;
812 printf("[%4d] new_func\t\t %s, f%d\n", location, registerName(r0).c_str(), f0);
815 case op_new_func_exp: {
816 int r0 = (++it)->u.operand;
817 int f0 = (++it)->u.operand;
818 printf("[%4d] new_func_exp\t %s, f%d\n", location, registerName(r0).c_str(), f0);
822 int dst = (++it)->u.operand;
823 int func = (++it)->u.operand;
824 int argCount = (++it)->u.operand;
825 int registerOffset = (++it)->u.operand;
826 printf("[%4d] call\t\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset);
830 int dst = (++it)->u.operand;
831 int func = (++it)->u.operand;
832 int argCount = (++it)->u.operand;
833 int registerOffset = (++it)->u.operand;
834 printf("[%4d] call_eval\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset);
837 case op_tear_off_activation: {
838 int r0 = (++it)->u.operand;
839 printf("[%4d] tear_off_activation\t %s\n", location, registerName(r0).c_str());
842 case op_tear_off_arguments: {
843 printf("[%4d] tear_off_arguments\n", location);
847 int r0 = (++it)->u.operand;
848 printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str());
852 int dst = (++it)->u.operand;
853 int func = (++it)->u.operand;
854 int argCount = (++it)->u.operand;
855 int registerOffset = (++it)->u.operand;
856 int proto = (++it)->u.operand;
857 int thisRegister = (++it)->u.operand;
858 printf("[%4d] construct\t %s, %s, %d, %d, %s, %s\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset, registerName(proto).c_str(), registerName(thisRegister).c_str());
861 case op_construct_verify: {
862 int r0 = (++it)->u.operand;
863 int r1 = (++it)->u.operand;
864 printf("[%4d] construct_verify\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
867 case op_get_pnames: {
868 int r0 = (++it)->u.operand;
869 int r1 = (++it)->u.operand;
870 printf("[%4d] get_pnames\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
873 case op_next_pname: {
874 int dest = (++it)->u.operand;
875 int iter = (++it)->u.operand;
876 int offset = (++it)->u.operand;
877 printf("[%4d] next_pname\t %s, %s, %d(->%d)\n", location, registerName(dest).c_str(), registerName(iter).c_str(), offset, jumpTarget(begin, it, offset));
880 case op_push_scope: {
881 int r0 = (++it)->u.operand;
882 printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str());
886 printf("[%4d] pop_scope\n", location);
889 case op_push_new_scope: {
890 int r0 = (++it)->u.operand;
891 int id0 = (++it)->u.operand;
892 int r1 = (++it)->u.operand;
893 printf("[%4d] push_new_scope \t%s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
896 case op_jmp_scopes: {
897 int scopeDelta = (++it)->u.operand;
898 int offset = (++it)->u.operand;
899 printf("[%4d] jmp_scopes\t^%d, %d(->%d)\n", location, scopeDelta, offset, jumpTarget(begin, it, offset));
903 int r0 = (++it)->u.operand;
904 printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str());
908 int r0 = (++it)->u.operand;
909 printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str());
913 int r0 = (++it)->u.operand;
914 int errorType = (++it)->u.operand;
915 int k0 = (++it)->u.operand;
916 printf("[%4d] new_error\t %s, %d, %s\n", location, registerName(r0).c_str(), errorType, constantName(exec, k0, unexpectedConstants[k0]).c_str());
920 int retAddrDst = (++it)->u.operand;
921 int offset = (++it)->u.operand;
922 printf("[%4d] jsr\t\t %s, %d(->%d)\n", location, registerName(retAddrDst).c_str(), offset, jumpTarget(begin, it, offset));
926 int retAddrSrc = (++it)->u.operand;
927 printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str());
931 int debugHookID = (++it)->u.operand;
932 int firstLine = (++it)->u.operand;
933 int lastLine = (++it)->u.operand;
934 printf("[%4d] debug\t\t %s, %d, %d\n", location, debugHookName(debugHookID), firstLine, lastLine);
937 case op_profile_will_call: {
938 int function = (++it)->u.operand;
939 printf("[%4d] profile_will_call %s\n", location, registerName(function).c_str());
942 case op_profile_did_call: {
943 int function = (++it)->u.operand;
944 printf("[%4d] profile_did_call\t %s\n", location, registerName(function).c_str());
948 int r0 = (++it)->u.operand;
949 printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str());
955 #endif // !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
957 CodeBlock::~CodeBlock()
959 for (size_t size = globalResolveInstructions.size(), i = 0; i < size; ++i) {
960 derefStructures(&instructions[globalResolveInstructions[i]]);
963 for (size_t size = propertyAccessInstructions.size(), i = 0; i < size; ++i) {
964 derefStructures(&instructions[propertyAccessInstructions[i].bytecodeIndex]);
965 if (propertyAccessInstructions[i].stubRoutine)
966 WTF::fastFreeExecutable(propertyAccessInstructions[i].stubRoutine);
969 for (size_t size = callLinkInfos.size(), i = 0; i < size; ++i) {
970 CallLinkInfo* callLinkInfo = &callLinkInfos[i];
971 if (callLinkInfo->isLinked())
972 callLinkInfo->callee->removeCaller(callLinkInfo);
979 WTF::fastFreeExecutable(ctiCode);
984 void CodeBlock::unlinkCallers()
986 size_t size = linkedCallerList.size();
987 for (size_t i = 0; i < size; ++i) {
988 CallLinkInfo* currentCaller = linkedCallerList[i];
989 JIT::unlinkCall(currentCaller);
990 currentCaller->setUnlinked();
992 linkedCallerList.clear();
996 void CodeBlock::derefStructures(Instruction* vPC) const
998 Interpreter* interpreter = globalData->interpreter;
1000 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
1001 vPC[4].u.structure->deref();
1004 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
1005 vPC[4].u.structure->deref();
1006 vPC[5].u.structure->deref();
1009 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
1010 vPC[4].u.structure->deref();
1011 vPC[5].u.structureChain->deref();
1014 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
1015 vPC[4].u.structure->deref();
1016 vPC[5].u.structure->deref();
1017 vPC[6].u.structureChain->deref();
1020 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
1021 vPC[4].u.structure->deref();
1024 if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) {
1025 if(vPC[4].u.structure)
1026 vPC[4].u.structure->deref();
1029 if ((vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto_list))
1030 || (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self_list))) {
1031 PolymorphicAccessStructureList* polymorphicStructures = vPC[4].u.polymorphicStructures;
1032 polymorphicStructures->derefStructures(vPC[5].u.operand);
1033 delete polymorphicStructures;
1037 // These instructions don't ref their Structures.
1038 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));
1041 void CodeBlock::refStructures(Instruction* vPC) const
1043 Interpreter* interpreter = globalData->interpreter;
1045 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
1046 vPC[4].u.structure->ref();
1049 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
1050 vPC[4].u.structure->ref();
1051 vPC[5].u.structure->ref();
1054 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
1055 vPC[4].u.structure->ref();
1056 vPC[5].u.structureChain->ref();
1059 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
1060 vPC[4].u.structure->ref();
1061 vPC[5].u.structure->ref();
1062 vPC[6].u.structureChain->ref();
1065 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
1066 vPC[4].u.structure->ref();
1070 // These instructions don't ref their Structures.
1071 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));
1074 void CodeBlock::mark()
1076 for (size_t i = 0; i < constantRegisters.size(); ++i)
1077 if (!constantRegisters[i].marked())
1078 constantRegisters[i].mark();
1080 for (size_t i = 0; i < unexpectedConstants.size(); ++i)
1081 if (!unexpectedConstants[i]->marked())
1082 unexpectedConstants[i]->mark();
1084 for (size_t i = 0; i < functions.size(); ++i)
1085 functions[i]->body()->mark();
1087 for (size_t i = 0; i < functionExpressions.size(); ++i)
1088 functionExpressions[i]->body()->mark();
1091 bool CodeBlock::getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth)
1093 Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin();
1094 Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1095 unsigned addressOffset = vPC - instructions.begin();
1096 ASSERT(addressOffset < instructions.size());
1098 for (; ptr != end; ++ptr) {
1099 // Handlers are ordered innermost first, so the first handler we encounter
1100 // that contains the source address is the correct handler to use.
1101 if (ptr->start <= addressOffset && ptr->end >= addressOffset) {
1102 scopeDepth = ptr->scopeDepth;
1103 target = instructions.begin() + ptr->target;
1110 void* CodeBlock::nativeExceptionCodeForHandlerVPC(const Instruction* handlerVPC)
1112 Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin();
1113 Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1115 for (; ptr != end; ++ptr) {
1116 Instruction*target = instructions.begin() + ptr->target;
1117 if (handlerVPC == target)
1118 return ptr->nativeCode;
1124 int CodeBlock::lineNumberForVPC(const Instruction* vPC)
1126 unsigned instructionOffset = vPC - instructions.begin();
1127 ASSERT(instructionOffset < instructions.size());
1129 if (!lineInfo.size())
1130 return ownerNode->source().firstLine(); // Empty function
1133 int high = lineInfo.size();
1134 while (low < high) {
1135 int mid = low + (high - low) / 2;
1136 if (lineInfo[mid].instructionOffset <= instructionOffset)
1143 return ownerNode->source().firstLine();
1144 return lineInfo[low - 1].lineNumber;
1147 int CodeBlock::expressionRangeForVPC(const Instruction* vPC, int& divot, int& startOffset, int& endOffset)
1149 unsigned instructionOffset = vPC - instructions.begin();
1150 ASSERT(instructionOffset < instructions.size());
1152 if (!expressionInfo.size()) {
1153 // We didn't think anything could throw. Apparently we were wrong.
1157 return lineNumberForVPC(vPC);
1161 int high = expressionInfo.size();
1162 while (low < high) {
1163 int mid = low + (high - low) / 2;
1164 if (expressionInfo[mid].instructionOffset <= instructionOffset)
1175 return lineNumberForVPC(vPC);
1178 startOffset = expressionInfo[low - 1].startOffset;
1179 endOffset = expressionInfo[low - 1].endOffset;
1180 divot = expressionInfo[low - 1].divotPoint + sourceOffset;
1181 return lineNumberForVPC(vPC);
1184 int32_t SimpleJumpTable::offsetForValue(int32_t value, int32_t defaultOffset)
1186 if (value >= min && static_cast<uint32_t>(value - min) < branchOffsets.size()) {
1187 int32_t offset = branchOffsets[value - min];
1191 return defaultOffset;