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"
37 #include <wtf/StringExtras.h>
41 #if !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
43 static UString escapeQuotes(const UString& str)
47 while ((pos = result.find('\"', pos)) >= 0) {
48 result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1);
54 static UString valueToSourceString(ExecState* exec, JSValue* val)
56 if (val->isString()) {
58 result += escapeQuotes(val->toString(exec)) + "\"";
62 return val->toString(exec);
65 static CString registerName(int r)
68 return (UString("lr") + UString::from(-r)).UTF8String();
70 if (r == missingThisObjectMarker())
73 return (UString("tr") + UString::from(r)).UTF8String();
76 static CString constantName(ExecState* exec, int k, JSValue* value)
78 return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String();
81 static CString idName(int id0, const Identifier& ident)
83 return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String();
86 static UString regexpToSourceString(RegExp* regExp)
88 UString pattern = UString("/") + regExp->pattern() + "/";
91 if (regExp->ignoreCase())
93 if (regExp->multiline())
99 static CString regexpName(int re, RegExp* regexp)
101 return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String();
104 static UString pointerToSourceString(void* p)
106 char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
107 snprintf(buffer, sizeof(buffer), "%p", p);
111 NEVER_INLINE static const char* debugHookName(int debugHookID)
113 switch (static_cast<DebugHookID>(debugHookID)) {
114 case DidEnterCallFrame:
115 return "didEnterCallFrame";
116 case WillLeaveCallFrame:
117 return "willLeaveCallFrame";
118 case WillExecuteStatement:
119 return "willExecuteStatement";
120 case WillExecuteProgram:
121 return "willExecuteProgram";
122 case DidExecuteProgram:
123 return "didExecuteProgram";
124 case DidReachBreakpoint:
125 return "didReachBreakpoint";
128 ASSERT_NOT_REACHED();
132 static int jumpTarget(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int offset)
134 return it - begin + offset;
137 static void printUnaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
139 int r0 = (++it)->u.operand;
140 int r1 = (++it)->u.operand;
142 printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str());
145 static void printBinaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
147 int r0 = (++it)->u.operand;
148 int r1 = (++it)->u.operand;
149 int r2 = (++it)->u.operand;
150 printf("[%4d] %s\t\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
153 static void printConditionalJump(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int location, const char* op)
155 int r0 = (++it)->u.operand;
156 int offset = (++it)->u.operand;
157 printf("[%4d] %s\t\t %s, %d(->%d)\n", location, op, registerName(r0).c_str(), offset, jumpTarget(begin, it, offset));
160 static void printGetByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
162 int r0 = (++it)->u.operand;
163 int r1 = (++it)->u.operand;
164 int id0 = (++it)->u.operand;
165 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
169 static void printPutByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
171 int r0 = (++it)->u.operand;
172 int id0 = (++it)->u.operand;
173 int r1 = (++it)->u.operand;
174 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
178 void CodeBlock::printStructureID(const char* name, const Instruction* vPC, int operand) const
180 unsigned instructionOffset = vPC - instructions.begin();
181 printf(" [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structureID).UTF8String().c_str());
184 void CodeBlock::printStructureIDs(const Instruction* vPC) const
186 Machine* machine = globalData->machine;
187 unsigned instructionOffset = vPC - instructions.begin();
189 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id)) {
190 printStructureID("get_by_id", vPC, 4);
193 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
194 printStructureID("get_by_id_self", vPC, 4);
197 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
198 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_proto", pointerToSourceString(vPC[4].u.structureID).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structureID).UTF8String().c_str());
201 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
202 printf(" [%4d] %s: %s, %s, %s\n", instructionOffset, "put_by_id_new", pointerToSourceString(vPC[4].u.structureID).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structureID).UTF8String().c_str(), pointerToSourceString(vPC[6].u.structureIDChain).UTF8String().c_str());
205 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
206 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_chain", pointerToSourceString(vPC[4].u.structureID).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structureIDChain).UTF8String().c_str());
209 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id)) {
210 printStructureID("put_by_id", vPC, 4);
213 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
214 printStructureID("put_by_id_replace", vPC, 4);
217 if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
218 printStructureID("resolve_global", vPC, 4);
222 // These instructions doesn't ref StructureIDs.
223 ASSERT(vPC[0].u.opcode == machine->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == machine->getOpcode(op_put_by_id_generic));
226 void CodeBlock::dump(ExecState* exec) const
228 Vector<Instruction>::const_iterator begin = instructions.begin();
229 Vector<Instruction>::const_iterator end = instructions.end();
231 size_t instructionCount = 0;
232 for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
233 if (exec->machine()->isOpcode(it->u.opcode))
236 printf("%lu instructions; %lu bytes at %p; %d locals (%d parameters); %d temporaries\n\n",
237 static_cast<unsigned long>(instructionCount),
238 static_cast<unsigned long>(instructions.size() * sizeof(Instruction)),
239 this, numLocals, numParameters, numTemporaries);
241 for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
242 dump(exec, begin, it);
244 if (identifiers.size()) {
245 printf("\nIdentifiers:\n");
248 printf(" id%u = %s\n", static_cast<unsigned>(i), identifiers[i].ascii());
250 } while (i != identifiers.size());
253 if (constantRegisters.size()) {
254 printf("\nConstants:\n");
257 printf(" tr%u = %s\n", static_cast<unsigned>(i), 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 (structureIDInstructions.size()) {
281 printf("\nStructureIDs:\n");
284 printStructureIDs(&instructions[structureIDInstructions[i].opcodeIndex]);
286 } while (i < structureIDInstructions.size());
289 if (exceptionHandlers.size()) {
290 printf("\nException Handlers:\n");
293 printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, exceptionHandlers[i].start, exceptionHandlers[i].end, exceptionHandlers[i].target);
295 } while (i < exceptionHandlers.size());
298 if (immediateSwitchJumpTables.size()) {
299 printf("Immediate Switch Jump Tables:\n");
302 printf(" %1d = {\n", i);
304 Vector<int32_t>::const_iterator end = immediateSwitchJumpTables[i].branchOffsets.end();
305 for (Vector<int32_t>::const_iterator iter = immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
308 printf("\t\t%4d => %04d\n", entry + immediateSwitchJumpTables[i].min, *iter);
312 } while (i < immediateSwitchJumpTables.size());
315 if (characterSwitchJumpTables.size()) {
316 printf("\nCharacter Switch Jump Tables:\n");
319 printf(" %1d = {\n", i);
321 Vector<int32_t>::const_iterator end = characterSwitchJumpTables[i].branchOffsets.end();
322 for (Vector<int32_t>::const_iterator iter = characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
325 ASSERT(!((i + characterSwitchJumpTables[i].min) & ~0xFFFF));
326 UChar ch = static_cast<UChar>(entry + characterSwitchJumpTables[i].min);
327 printf("\t\t\"%s\" => %04d\n", UString(&ch, 1).ascii(), *iter);
331 } while (i < characterSwitchJumpTables.size());
334 if (stringSwitchJumpTables.size()) {
335 printf("\nString Switch Jump Tables:\n");
338 printf(" %1d = {\n", i);
339 StringJumpTable::StringOffsetTable::const_iterator end = stringSwitchJumpTables[i].offsetTable.end();
340 for (StringJumpTable::StringOffsetTable::const_iterator iter = stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
341 printf("\t\t\"%s\" => %04d\n", UString(iter->first).ascii(), iter->second.branchOffset);
344 } while (i < stringSwitchJumpTables.size());
350 void CodeBlock::dump(ExecState* exec, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it) const
352 int location = it - begin;
353 switch (exec->machine()->getOpcodeID(it->u.opcode)) {
354 case op_unexpected_load: {
355 int r0 = (++it)->u.operand;
356 int k0 = (++it)->u.operand;
357 printf("[%4d] unexpected_load\t %s, %s\n", location, registerName(r0).c_str(), constantName(exec, k0, unexpectedConstants[k0]).c_str());
360 case op_new_object: {
361 int r0 = (++it)->u.operand;
362 printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str());
366 int dst = (++it)->u.operand;
367 int argv = (++it)->u.operand;
368 int argc = (++it)->u.operand;
369 printf("[%4d] new_array\t %s, %s, %d\n", location, registerName(dst).c_str(), registerName(argv).c_str(), argc);
372 case op_new_regexp: {
373 int r0 = (++it)->u.operand;
374 int re0 = (++it)->u.operand;
375 printf("[%4d] new_regexp\t %s, %s\n", location, registerName(r0).c_str(), regexpName(re0, regexps[re0].get()).c_str());
379 int r0 = (++it)->u.operand;
380 int r1 = (++it)->u.operand;
381 printf("[%4d] mov\t\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
385 printUnaryOp(location, it, "not");
389 printBinaryOp(location, it, "eq");
393 printUnaryOp(location, it, "eq_null");
397 printBinaryOp(location, it, "neq");
401 printUnaryOp(location, it, "neq_null");
405 printBinaryOp(location, it, "stricteq");
409 printBinaryOp(location, it, "nstricteq");
413 printBinaryOp(location, it, "less");
417 printBinaryOp(location, it, "lesseq");
421 int r0 = (++it)->u.operand;
422 printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str());
426 int r0 = (++it)->u.operand;
427 printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str());
431 printUnaryOp(location, it, "post_inc");
435 printUnaryOp(location, it, "post_dec");
438 case op_to_jsnumber: {
439 printUnaryOp(location, it, "to_jsnumber");
443 printUnaryOp(location, it, "negate");
447 printBinaryOp(location, it, "add");
451 printBinaryOp(location, it, "mul");
455 printBinaryOp(location, it, "div");
459 printBinaryOp(location, it, "mod");
463 printBinaryOp(location, it, "sub");
467 printBinaryOp(location, it, "lshift");
471 printBinaryOp(location, it, "rshift");
475 printBinaryOp(location, it, "urshift");
479 printBinaryOp(location, it, "bitand");
483 printBinaryOp(location, it, "bitxor");
487 printBinaryOp(location, it, "bitor");
491 printUnaryOp(location, it, "bitnot");
494 case op_instanceof: {
495 int r0 = (++it)->u.operand;
496 int r1 = (++it)->u.operand;
497 int r2 = (++it)->u.operand;
498 int r3 = (++it)->u.operand;
499 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());
503 printUnaryOp(location, it, "typeof");
506 case op_is_undefined: {
507 printUnaryOp(location, it, "is_undefined");
510 case op_is_boolean: {
511 printUnaryOp(location, it, "is_boolean");
515 printUnaryOp(location, it, "is_number");
519 printUnaryOp(location, it, "is_string");
523 printUnaryOp(location, it, "is_object");
526 case op_is_function: {
527 printUnaryOp(location, it, "is_function");
531 printBinaryOp(location, it, "in");
535 int r0 = (++it)->u.operand;
536 int id0 = (++it)->u.operand;
537 printf("[%4d] resolve\t\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
540 case op_resolve_skip: {
541 int r0 = (++it)->u.operand;
542 int id0 = (++it)->u.operand;
543 int skipLevels = (++it)->u.operand;
544 printf("[%4d] resolve_skip\t %s, %s, %d\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), skipLevels);
547 case op_resolve_global: {
548 int r0 = (++it)->u.operand;
549 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
550 int id0 = (++it)->u.operand;
551 printf("[%4d] resolve_global\t %s, %s, %s\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), idName(id0, identifiers[id0]).c_str());
555 case op_get_scoped_var: {
556 int r0 = (++it)->u.operand;
557 int index = (++it)->u.operand;
558 int skipLevels = (++it)->u.operand;
559 printf("[%4d] get_scoped_var\t %s, %d, %d\n", location, registerName(r0).c_str(), index, skipLevels);
562 case op_put_scoped_var: {
563 int index = (++it)->u.operand;
564 int skipLevels = (++it)->u.operand;
565 int r0 = (++it)->u.operand;
566 printf("[%4d] put_scoped_var\t %d, %d, %s\n", location, index, skipLevels, registerName(r0).c_str());
569 case op_get_global_var: {
570 int r0 = (++it)->u.operand;
571 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
572 int index = (++it)->u.operand;
573 printf("[%4d] get_global_var\t %s, %s, %d\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), index);
576 case op_put_global_var: {
577 JSValue* scope = static_cast<JSValue*>((++it)->u.jsCell);
578 int index = (++it)->u.operand;
579 int r0 = (++it)->u.operand;
580 printf("[%4d] put_global_var\t %s, %d, %s\n", location, valueToSourceString(exec, scope).ascii(), index, registerName(r0).c_str());
583 case op_resolve_base: {
584 int r0 = (++it)->u.operand;
585 int id0 = (++it)->u.operand;
586 printf("[%4d] resolve_base\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
589 case op_resolve_with_base: {
590 int r0 = (++it)->u.operand;
591 int r1 = (++it)->u.operand;
592 int id0 = (++it)->u.operand;
593 printf("[%4d] resolve_with_base %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
596 case op_resolve_func: {
597 int r0 = (++it)->u.operand;
598 int r1 = (++it)->u.operand;
599 int id0 = (++it)->u.operand;
600 printf("[%4d] resolve_func\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
604 printGetByIdOp(location, it, identifiers, "get_by_id");
607 case op_get_by_id_self: {
608 printGetByIdOp(location, it, identifiers, "get_by_id_self");
611 case op_get_by_id_proto: {
612 printGetByIdOp(location, it, identifiers, "get_by_id_proto");
615 case op_get_by_id_chain: {
616 printGetByIdOp(location, it, identifiers, "get_by_id_chain");
619 case op_get_by_id_generic: {
620 printGetByIdOp(location, it, identifiers, "get_by_id_generic");
623 case op_get_array_length: {
624 printGetByIdOp(location, it, identifiers, "get_array_length");
627 case op_get_string_length: {
628 printGetByIdOp(location, it, identifiers, "get_string_length");
632 printPutByIdOp(location, it, identifiers, "put_by_id");
635 case op_put_by_id_replace: {
636 printPutByIdOp(location, it, identifiers, "put_by_id_replace");
639 case op_put_by_id_transition: {
640 printPutByIdOp(location, it, identifiers, "put_by_id_transition");
643 case op_put_by_id_generic: {
644 printPutByIdOp(location, it, identifiers, "put_by_id_generic");
647 case op_put_getter: {
648 int r0 = (++it)->u.operand;
649 int id0 = (++it)->u.operand;
650 int r1 = (++it)->u.operand;
651 printf("[%4d] put_getter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
654 case op_put_setter: {
655 int r0 = (++it)->u.operand;
656 int id0 = (++it)->u.operand;
657 int r1 = (++it)->u.operand;
658 printf("[%4d] put_setter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
662 int r0 = (++it)->u.operand;
663 int r1 = (++it)->u.operand;
664 int id0 = (++it)->u.operand;
665 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());
668 case op_get_by_val: {
669 int r0 = (++it)->u.operand;
670 int r1 = (++it)->u.operand;
671 int r2 = (++it)->u.operand;
672 printf("[%4d] get_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
675 case op_put_by_val: {
676 int r0 = (++it)->u.operand;
677 int r1 = (++it)->u.operand;
678 int r2 = (++it)->u.operand;
679 printf("[%4d] put_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
682 case op_del_by_val: {
683 int r0 = (++it)->u.operand;
684 int r1 = (++it)->u.operand;
685 int r2 = (++it)->u.operand;
686 printf("[%4d] del_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
689 case op_put_by_index: {
690 int r0 = (++it)->u.operand;
691 unsigned n0 = (++it)->u.operand;
692 int r1 = (++it)->u.operand;
693 printf("[%4d] put_by_index\t %s, %u, %s\n", location, registerName(r0).c_str(), n0, registerName(r1).c_str());
697 int offset = (++it)->u.operand;
698 printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
702 int offset = (++it)->u.operand;
703 printf("[%4d] loop\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
707 printConditionalJump(begin, it, location, "jtrue");
710 case op_loop_if_true: {
711 printConditionalJump(begin, it, location, "loop_if_true");
715 printConditionalJump(begin, it, location, "jfalse");
719 int r0 = (++it)->u.operand;
720 int r1 = (++it)->u.operand;
721 int offset = (++it)->u.operand;
722 printf("[%4d] jnless\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, jumpTarget(begin, it, offset));
725 case op_loop_if_less: {
726 int r0 = (++it)->u.operand;
727 int r1 = (++it)->u.operand;
728 int offset = (++it)->u.operand;
729 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));
732 case op_loop_if_lesseq: {
733 int r0 = (++it)->u.operand;
734 int r1 = (++it)->u.operand;
735 int offset = (++it)->u.operand;
736 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));
739 case op_switch_imm: {
740 int tableIndex = (++it)->u.operand;
741 int defaultTarget = (++it)->u.operand;
742 int scrutineeRegister = (++it)->u.operand;
743 printf("[%4d] switch_imm\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
746 case op_switch_char: {
747 int tableIndex = (++it)->u.operand;
748 int defaultTarget = (++it)->u.operand;
749 int scrutineeRegister = (++it)->u.operand;
750 printf("[%4d] switch_char\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
753 case op_switch_string: {
754 int tableIndex = (++it)->u.operand;
755 int defaultTarget = (++it)->u.operand;
756 int scrutineeRegister = (++it)->u.operand;
757 printf("[%4d] switch_string\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
761 int r0 = (++it)->u.operand;
762 int f0 = (++it)->u.operand;
763 printf("[%4d] new_func\t\t %s, f%d\n", location, registerName(r0).c_str(), f0);
766 case op_new_func_exp: {
767 int r0 = (++it)->u.operand;
768 int f0 = (++it)->u.operand;
769 printf("[%4d] new_func_exp\t %s, f%d\n", location, registerName(r0).c_str(), f0);
773 int r0 = (++it)->u.operand;
774 int r1 = (++it)->u.operand;
775 int r2 = (++it)->u.operand;
776 int tempCount = (++it)->u.operand;
777 int argCount = (++it)->u.operand;
778 printf("[%4d] call\t\t %s, %s, %s, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount);
782 int r0 = (++it)->u.operand;
783 int r1 = (++it)->u.operand;
784 int r2 = (++it)->u.operand;
785 int tempCount = (++it)->u.operand;
786 int argCount = (++it)->u.operand;
787 printf("[%4d] call_eval\t\t %s, %s, %s, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount);
791 int r0 = (++it)->u.operand;
792 printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str());
796 int r0 = (++it)->u.operand;
797 int r1 = (++it)->u.operand;
798 int r2 = (++it)->u.operand;
799 int tempCount = (++it)->u.operand;
800 int argCount = (++it)->u.operand;
801 printf("[%4d] construct\t %s, %s, %s, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount);
804 case op_construct_verify: {
805 int r0 = (++it)->u.operand;
806 int r1 = (++it)->u.operand;
807 printf("[%4d] construct_verify\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
810 case op_get_pnames: {
811 int r0 = (++it)->u.operand;
812 int r1 = (++it)->u.operand;
813 printf("[%4d] get_pnames\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
816 case op_next_pname: {
817 int dest = (++it)->u.operand;
818 int iter = (++it)->u.operand;
819 int offset = (++it)->u.operand;
820 printf("[%4d] next_pname\t %s, %s, %d(->%d)\n", location, registerName(dest).c_str(), registerName(iter).c_str(), offset, jumpTarget(begin, it, offset));
823 case op_push_scope: {
824 int r0 = (++it)->u.operand;
825 printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str());
829 printf("[%4d] pop_scope\n", location);
832 case op_push_new_scope: {
833 int r0 = (++it)->u.operand;
834 int id0 = (++it)->u.operand;
835 int r1 = (++it)->u.operand;
836 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());
839 case op_jmp_scopes: {
840 int scopeDelta = (++it)->u.operand;
841 int offset = (++it)->u.operand;
842 printf("[%4d] jmp_scopes\t^%d, %d(->%d)\n", location, scopeDelta, offset, jumpTarget(begin, it, offset));
846 int r0 = (++it)->u.operand;
847 printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str());
851 int r0 = (++it)->u.operand;
852 printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str());
856 int r0 = (++it)->u.operand;
857 int errorType = (++it)->u.operand;
858 int k0 = (++it)->u.operand;
859 printf("[%4d] new_error\t %s, %d, %s\n", location, registerName(r0).c_str(), errorType, constantName(exec, k0, unexpectedConstants[k0]).c_str());
863 int retAddrDst = (++it)->u.operand;
864 int offset = (++it)->u.operand;
865 printf("[%4d] jsr\t\t %s, %d(->%d)\n", location, registerName(retAddrDst).c_str(), offset, jumpTarget(begin, it, offset));
869 int retAddrSrc = (++it)->u.operand;
870 printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str());
874 int debugHookID = (++it)->u.operand;
875 int firstLine = (++it)->u.operand;
876 int lastLine = (++it)->u.operand;
877 printf("[%4d] debug\t\t %s, %d, %d\n", location, debugHookName(debugHookID), firstLine, lastLine);
881 int r0 = (++it)->u.operand;
882 printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str());
886 ASSERT_NOT_REACHED();
892 #endif // !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
894 CodeBlock::~CodeBlock()
896 size_t size = structureIDInstructions.size();
897 for (size_t i = 0; i < size; ++i) {
898 derefStructureIDs(&instructions[structureIDInstructions[i].opcodeIndex]);
899 if (structureIDInstructions[i].stubRoutine)
900 fastFree(structureIDInstructions[i].stubRoutine);
908 void CodeBlock::derefStructureIDs(Instruction* vPC) const
910 Machine* machine = globalData->machine;
912 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
913 vPC[4].u.structureID->deref();
916 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
917 vPC[4].u.structureID->deref();
918 vPC[5].u.structureID->deref();
921 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
922 vPC[4].u.structureID->deref();
923 vPC[5].u.structureIDChain->deref();
926 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
927 vPC[4].u.structureID->deref();
928 vPC[5].u.structureID->deref();
929 vPC[6].u.structureIDChain->deref();
932 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
933 vPC[4].u.structureID->deref();
936 if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
937 if(vPC[4].u.structureID)
938 vPC[4].u.structureID->deref();
942 // These instructions don't ref their StructureIDs.
943 ASSERT(vPC[0].u.opcode == machine->getOpcode(op_get_by_id) || vPC[0].u.opcode == machine->getOpcode(op_put_by_id) || vPC[0].u.opcode == machine->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == machine->getOpcode(op_put_by_id_generic) || vPC[0].u.opcode == machine->getOpcode(op_get_array_length) || vPC[0].u.opcode == machine->getOpcode(op_get_string_length));
946 void CodeBlock::refStructureIDs(Instruction* vPC) const
948 Machine* machine = globalData->machine;
950 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
951 vPC[4].u.structureID->ref();
954 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
955 vPC[4].u.structureID->ref();
956 vPC[5].u.structureID->ref();
959 if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
960 vPC[4].u.structureID->ref();
961 vPC[5].u.structureIDChain->ref();
964 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
965 vPC[4].u.structureID->ref();
966 vPC[5].u.structureID->ref();
967 vPC[6].u.structureIDChain->ref();
970 if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
971 vPC[4].u.structureID->ref();
975 // These instructions don't ref their StructureIDs.
976 ASSERT(vPC[0].u.opcode == machine->getOpcode(op_get_by_id) || vPC[0].u.opcode == machine->getOpcode(op_put_by_id) || vPC[0].u.opcode == machine->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == machine->getOpcode(op_put_by_id_generic));
979 void CodeBlock::mark()
981 for (size_t i = 0; i < constantRegisters.size(); ++i)
982 if (!constantRegisters[i].marked())
983 constantRegisters[i].mark();
985 for (size_t i = 0; i < unexpectedConstants.size(); ++i)
986 if (!unexpectedConstants[i]->marked())
987 unexpectedConstants[i]->mark();
989 for (size_t i = 0; i < functions.size(); ++i)
990 functions[i]->body()->mark();
992 for (size_t i = 0; i < functionExpressions.size(); ++i)
993 functionExpressions[i]->body()->mark();
996 bool CodeBlock::getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth)
998 Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin();
999 Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1000 unsigned addressOffset = vPC - instructions.begin();
1001 ASSERT(addressOffset < instructions.size());
1003 for (; ptr != end; ++ptr) {
1004 // Handlers are ordered innermost first, so the first handler we encounter
1005 // that contains the source address is the correct handler to use.
1006 if (ptr->start <= addressOffset && ptr->end >= addressOffset) {
1007 scopeDepth = ptr->scopeDepth;
1008 target = instructions.begin() + ptr->target;
1015 void* CodeBlock::nativeExceptionCodeForHandlerVPC(const Instruction* handlerVPC)
1017 Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin();
1018 Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1020 for (; ptr != end; ++ptr) {
1021 Instruction*target = instructions.begin() + ptr->target;
1022 if (handlerVPC == target)
1023 return ptr->nativeCode;
1029 int CodeBlock::lineNumberForVPC(const Instruction* vPC)
1031 ASSERT(lineInfo.size());
1032 unsigned instructionOffset = vPC - instructions.begin();
1033 ASSERT(instructionOffset < instructions.size());
1035 if (!lineInfo.size())
1036 return 1; // Empty function
1039 int high = lineInfo.size();
1040 while (low < high) {
1041 int mid = low + (high - low) / 2;
1042 if (lineInfo[mid].instructionOffset <= instructionOffset)
1047 return lineInfo[low - 1].lineNumber;
1050 int CodeBlock::expressionRangeForVPC(const Instruction* vPC, int& divot, int& startOffset, int& endOffset)
1052 unsigned instructionOffset = vPC - instructions.begin();
1053 ASSERT(instructionOffset < instructions.size());
1055 if (!expressionInfo.size()) {
1056 // We didn't think anything could throw. Apparently we were wrong.
1060 return lineNumberForVPC(vPC);
1064 int high = expressionInfo.size();
1065 while (low < high) {
1066 int mid = low + (high - low) / 2;
1067 if (expressionInfo[mid].instructionOffset <= instructionOffset)
1078 return lineNumberForVPC(vPC);
1081 startOffset = expressionInfo[low - 1].startOffset;
1082 endOffset = expressionInfo[low - 1].endOffset;
1083 divot = expressionInfo[low - 1].divotPoint + sourceOffset;
1084 return lineNumberForVPC(vPC);
1087 int32_t SimpleJumpTable::offsetForValue(int32_t value, int32_t defaultOffset)
1089 if (value >= min && static_cast<uint32_t>(value - min) < branchOffsets.size()) {
1090 int32_t offset = branchOffsets[value - min];
1094 return defaultOffset;