Really "fix" CTI mode on windows 2k3.
[WebKit-https.git] / JavaScriptCore / VM / CodeBlock.cpp
1 /*
2  * Copyright (C) 2008 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 "CTI.h"
34 #include "JSValue.h"
35 #include "Machine.h"
36 #include "debugger.h"
37 #include <stdio.h>
38 #include <wtf/StringExtras.h>
39
40 namespace JSC {
41
42 #if !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
43
44 static UString escapeQuotes(const UString& str)
45 {
46     UString result = str;
47     int pos = 0;
48     while ((pos = result.find('\"', pos)) >= 0) {
49         result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1);
50         pos += 4;
51     }
52     return result;
53 }
54
55 static UString valueToSourceString(ExecState* exec, JSValuePtr val)
56 {
57     if (val->isString()) {
58         UString result("\"");
59         result += escapeQuotes(val->toString(exec)) + "\"";
60         return result;
61     } 
62
63     return val->toString(exec);
64 }
65
66 static CString registerName(int r)
67 {
68     if (r == missingThisObjectMarker())
69         return "<null>";
70
71     return (UString("r") + UString::from(r)).UTF8String();
72 }
73
74 static CString constantName(ExecState* exec, int k, JSValuePtr value)
75 {
76     return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String();
77 }
78
79 static CString idName(int id0, const Identifier& ident)
80 {
81     return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String();
82 }
83
84 static UString regexpToSourceString(RegExp* regExp)
85 {
86     UString pattern = UString("/") + regExp->pattern() + "/";
87     if (regExp->global())
88         pattern += "g";
89     if (regExp->ignoreCase())
90         pattern += "i";
91     if (regExp->multiline())
92         pattern += "m";
93
94     return pattern;
95 }
96
97 static CString regexpName(int re, RegExp* regexp)
98 {
99     return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String();
100 }
101
102 static UString pointerToSourceString(void* p)
103 {
104     char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
105     snprintf(buffer, sizeof(buffer), "%p", p);
106     return buffer;
107 }
108
109 NEVER_INLINE static const char* debugHookName(int debugHookID)
110 {
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";
124     }
125
126     ASSERT_NOT_REACHED();
127     return "";
128 }
129
130 static int jumpTarget(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int offset)
131 {
132     return it - begin + offset;
133 }
134
135 static void printUnaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
136 {
137     int r0 = (++it)->u.operand;
138     int r1 = (++it)->u.operand;
139
140     printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str());
141 }
142
143 static void printBinaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
144 {
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());
149 }
150
151 static void printConditionalJump(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int location, const char* op)
152 {
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));
156 }
157
158 static void printGetByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
159 {
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());
164     it += 4;
165 }
166
167 static void printPutByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
168 {
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());
173     it += 4;
174 }
175
176 void CodeBlock::printStructureID(const char* name, const Instruction* vPC, int operand) const
177 {
178     unsigned instructionOffset = vPC - instructions.begin();
179     printf("  [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structureID).UTF8String().c_str());
180 }
181
182 void CodeBlock::printStructureIDs(const Instruction* vPC) const
183 {
184     Machine* machine = globalData->machine;
185     unsigned instructionOffset = vPC - instructions.begin();
186
187     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id)) {
188         printStructureID("get_by_id", vPC, 4);
189         return;
190     }
191     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
192         printStructureID("get_by_id_self", vPC, 4);
193         return;
194     }
195     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
196         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());
197         return;
198     }
199     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
200         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());
201         return;
202     }
203     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
204         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());
205         return;
206     }
207     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id)) {
208         printStructureID("put_by_id", vPC, 4);
209         return;
210     }
211     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
212         printStructureID("put_by_id_replace", vPC, 4);
213         return;
214     }
215     if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
216         printStructureID("resolve_global", vPC, 4);
217         return;
218     }
219
220     // These instructions doesn't ref StructureIDs.
221     ASSERT(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_call) || vPC[0].u.opcode == machine->getOpcode(op_call_eval) || vPC[0].u.opcode == machine->getOpcode(op_construct));
222 }
223
224 void CodeBlock::dump(ExecState* exec) const
225 {
226     Vector<Instruction>::const_iterator begin = instructions.begin();
227     Vector<Instruction>::const_iterator end = instructions.end();
228
229     size_t instructionCount = 0;
230     for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
231         if (exec->machine()->isOpcode(it->u.opcode))
232             ++instructionCount;
233
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);
238     
239     for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
240         dump(exec, begin, it);
241
242     if (identifiers.size()) {
243         printf("\nIdentifiers:\n");
244         size_t i = 0;
245         do {
246             printf("  id%u = %s\n", static_cast<unsigned>(i), identifiers[i].ascii());
247             ++i;
248         } while (i != identifiers.size());
249     }
250
251     if (constantRegisters.size()) {
252         printf("\nConstants:\n");
253         size_t i = 0;
254         do {
255             printf("   r%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, constantRegisters[i].jsValue(exec)).ascii());
256             ++i;
257         } while (i < constantRegisters.size());
258     }
259
260     if (unexpectedConstants.size()) {
261         printf("\nUnexpected Constants:\n");
262         size_t i = 0;
263         do {
264             printf("  k%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, unexpectedConstants[i]).ascii());
265             ++i;
266         } while (i < unexpectedConstants.size());
267     }
268     
269     if (regexps.size()) {
270         printf("\nRegExps:\n");
271         size_t i = 0;
272         do {
273             printf("  re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(regexps[i].get()).ascii());
274             ++i;
275         } while (i < regexps.size());
276     }
277
278     if (structureIDInstructions.size()) {
279         printf("\nStructureIDs:\n");
280         size_t i = 0;
281         do {
282              printStructureIDs(&instructions[structureIDInstructions[i].opcodeIndex]);
283              ++i;
284         } while (i < structureIDInstructions.size());
285     }
286  
287     if (exceptionHandlers.size()) {
288         printf("\nException Handlers:\n");
289         unsigned i = 0;
290         do {
291             printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, exceptionHandlers[i].start, exceptionHandlers[i].end, exceptionHandlers[i].target);
292             ++i;
293         } while (i < exceptionHandlers.size());
294     }
295     
296     if (immediateSwitchJumpTables.size()) {
297         printf("Immediate Switch Jump Tables:\n");
298         unsigned i = 0;
299         do {
300             printf("  %1d = {\n", i);
301             int entry = 0;
302             Vector<int32_t>::const_iterator end = immediateSwitchJumpTables[i].branchOffsets.end();
303             for (Vector<int32_t>::const_iterator iter = immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
304                 if (!*iter)
305                     continue;
306                 printf("\t\t%4d => %04d\n", entry + immediateSwitchJumpTables[i].min, *iter);
307             }
308             printf("      }\n");
309             ++i;
310         } while (i < immediateSwitchJumpTables.size());
311     }
312     
313     if (characterSwitchJumpTables.size()) {
314         printf("\nCharacter Switch Jump Tables:\n");
315         unsigned i = 0;
316         do {
317             printf("  %1d = {\n", i);
318             int entry = 0;
319             Vector<int32_t>::const_iterator end = characterSwitchJumpTables[i].branchOffsets.end();
320             for (Vector<int32_t>::const_iterator iter = characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
321                 if (!*iter)
322                     continue;
323                 ASSERT(!((i + characterSwitchJumpTables[i].min) & ~0xFFFF));
324                 UChar ch = static_cast<UChar>(entry + characterSwitchJumpTables[i].min);
325                 printf("\t\t\"%s\" => %04d\n", UString(&ch, 1).ascii(), *iter);
326         }
327             printf("      }\n");
328             ++i;
329         } while (i < characterSwitchJumpTables.size());
330     }
331     
332     if (stringSwitchJumpTables.size()) {
333         printf("\nString Switch Jump Tables:\n");
334         unsigned i = 0;
335         do {
336             printf("  %1d = {\n", i);
337             StringJumpTable::StringOffsetTable::const_iterator end = stringSwitchJumpTables[i].offsetTable.end();
338             for (StringJumpTable::StringOffsetTable::const_iterator iter = stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
339                 printf("\t\t\"%s\" => %04d\n", UString(iter->first).ascii(), iter->second.branchOffset);
340             printf("      }\n");
341             ++i;
342         } while (i < stringSwitchJumpTables.size());
343     }
344
345     printf("\n");
346 }
347
348 void CodeBlock::dump(ExecState* exec, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it) const
349 {
350     int location = it - begin;
351     switch (exec->machine()->getOpcodeID(it->u.opcode)) {
352         case op_enter: {
353             printf("[%4d] enter\n", location);
354             break;
355         }
356         case op_enter_with_activation: {
357             int r0 = (++it)->u.operand;
358             printf("[%4d] enter_with_activation %s\n", location, registerName(r0).c_str());
359             break;
360         }
361         case op_create_arguments: {
362             printf("[%4d] create_arguments\n", location);
363             break;
364         }
365         case op_convert_this: {
366             int r0 = (++it)->u.operand;
367             printf("[%4d] convert_this %s\n", location, registerName(r0).c_str());
368             break;
369         }
370         case op_unexpected_load: {
371             int r0 = (++it)->u.operand;
372             int k0 = (++it)->u.operand;
373             printf("[%4d] unexpected_load\t %s, %s\n", location, registerName(r0).c_str(), constantName(exec, k0, unexpectedConstants[k0]).c_str());
374             break;
375         }
376         case op_new_object: {
377             int r0 = (++it)->u.operand;
378             printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str());
379             break;
380         }
381         case op_new_array: {
382             int dst = (++it)->u.operand;
383             int argv = (++it)->u.operand;
384             int argc = (++it)->u.operand;
385             printf("[%4d] new_array\t %s, %s, %d\n", location, registerName(dst).c_str(), registerName(argv).c_str(), argc);
386             break;
387         }
388         case op_new_regexp: {
389             int r0 = (++it)->u.operand;
390             int re0 = (++it)->u.operand;
391             printf("[%4d] new_regexp\t %s, %s\n", location, registerName(r0).c_str(), regexpName(re0, regexps[re0].get()).c_str());
392             break;
393         }
394         case op_mov: {
395             int r0 = (++it)->u.operand;
396             int r1 = (++it)->u.operand;
397             printf("[%4d] mov\t\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
398             break;
399         }
400         case op_not: {
401             printUnaryOp(location, it, "not");
402             break;
403         }
404         case op_eq: {
405             printBinaryOp(location, it, "eq");
406             break;
407         }
408         case op_eq_null: {
409             printUnaryOp(location, it, "eq_null");
410             break;
411         }
412         case op_neq: {
413             printBinaryOp(location, it, "neq");
414             break;
415         }
416         case op_neq_null: {
417             printUnaryOp(location, it, "neq_null");
418             break;
419         }
420         case op_stricteq: {
421             printBinaryOp(location, it, "stricteq");
422             break;
423         }
424         case op_nstricteq: {
425             printBinaryOp(location, it, "nstricteq");
426             break;
427         }
428         case op_less: {
429             printBinaryOp(location, it, "less");
430             break;
431         }
432         case op_lesseq: {
433             printBinaryOp(location, it, "lesseq");
434             break;
435         }
436         case op_pre_inc: {
437             int r0 = (++it)->u.operand;
438             printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str());
439             break;
440         }
441         case op_pre_dec: {
442             int r0 = (++it)->u.operand;
443             printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str());
444             break;
445         }
446         case op_post_inc: {
447             printUnaryOp(location, it, "post_inc");
448             break;
449         }
450         case op_post_dec: {
451             printUnaryOp(location, it, "post_dec");
452             break;
453         }
454         case op_to_jsnumber: {
455             printUnaryOp(location, it, "to_jsnumber");
456             break;
457         }
458         case op_negate: {
459             printUnaryOp(location, it, "negate");
460             break;
461         }
462         case op_add: {
463             printBinaryOp(location, it, "add");
464             ++it;
465             break;
466         }
467         case op_mul: {
468             printBinaryOp(location, it, "mul");
469             ++it;
470             break;
471         }
472         case op_div: {
473             printBinaryOp(location, it, "div");
474             break;
475         }
476         case op_mod: {
477             printBinaryOp(location, it, "mod");
478             break;
479         }
480         case op_sub: {
481             printBinaryOp(location, it, "sub");
482             ++it;
483             break;
484         }
485         case op_lshift: {
486             printBinaryOp(location, it, "lshift");
487             break;            
488         }
489         case op_rshift: {
490             printBinaryOp(location, it, "rshift");
491             break;
492         }
493         case op_urshift: {
494             printBinaryOp(location, it, "urshift");
495             break;
496         }
497         case op_bitand: {
498             printBinaryOp(location, it, "bitand");
499             ++it;
500             break;
501         }
502         case op_bitxor: {
503             printBinaryOp(location, it, "bitxor");
504             ++it;
505             break;
506         }
507         case op_bitor: {
508             printBinaryOp(location, it, "bitor");
509             ++it;
510             break;
511         }
512         case op_bitnot: {
513             printUnaryOp(location, it, "bitnot");
514             break;
515         }
516         case op_instanceof: {
517             int r0 = (++it)->u.operand;
518             int r1 = (++it)->u.operand;
519             int r2 = (++it)->u.operand;
520             int r3 = (++it)->u.operand;
521             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());
522             break;
523         }
524         case op_typeof: {
525             printUnaryOp(location, it, "typeof");
526             break;
527         }
528         case op_is_undefined: {
529             printUnaryOp(location, it, "is_undefined");
530             break;
531         }
532         case op_is_boolean: {
533             printUnaryOp(location, it, "is_boolean");
534             break;
535         }
536         case op_is_number: {
537             printUnaryOp(location, it, "is_number");
538             break;
539         }
540         case op_is_string: {
541             printUnaryOp(location, it, "is_string");
542             break;
543         }
544         case op_is_object: {
545             printUnaryOp(location, it, "is_object");
546             break;
547         }
548         case op_is_function: {
549             printUnaryOp(location, it, "is_function");
550             break;
551         }
552         case op_in: {
553             printBinaryOp(location, it, "in");
554             break;
555         }
556         case op_resolve: {
557             int r0 = (++it)->u.operand;
558             int id0 = (++it)->u.operand;
559             printf("[%4d] resolve\t\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
560             break;
561         }
562         case op_resolve_skip: {
563             int r0 = (++it)->u.operand;
564             int id0 = (++it)->u.operand;
565             int skipLevels = (++it)->u.operand;
566             printf("[%4d] resolve_skip\t %s, %s, %d\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), skipLevels);
567             break;
568         }
569         case op_resolve_global: {
570             int r0 = (++it)->u.operand;
571             JSValuePtr scope = static_cast<JSValuePtr>((++it)->u.jsCell);
572             int id0 = (++it)->u.operand;
573             printf("[%4d] resolve_global\t %s, %s, %s\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), idName(id0, identifiers[id0]).c_str());
574             it += 2;
575             break;
576         }
577         case op_get_scoped_var: {
578             int r0 = (++it)->u.operand;
579             int index = (++it)->u.operand;
580             int skipLevels = (++it)->u.operand;
581             printf("[%4d] get_scoped_var\t %s, %d, %d\n", location, registerName(r0).c_str(), index, skipLevels);
582             break;
583         }
584         case op_put_scoped_var: {
585             int index = (++it)->u.operand;
586             int skipLevels = (++it)->u.operand;
587             int r0 = (++it)->u.operand;
588             printf("[%4d] put_scoped_var\t %d, %d, %s\n", location, index, skipLevels, registerName(r0).c_str());
589             break;
590         }
591         case op_get_global_var: {
592             int r0 = (++it)->u.operand;
593             JSValuePtr scope = static_cast<JSValuePtr>((++it)->u.jsCell);
594             int index = (++it)->u.operand;
595             printf("[%4d] get_global_var\t %s, %s, %d\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), index);
596             break;
597         }
598         case op_put_global_var: {
599             JSValuePtr scope = static_cast<JSValuePtr>((++it)->u.jsCell);
600             int index = (++it)->u.operand;
601             int r0 = (++it)->u.operand;
602             printf("[%4d] put_global_var\t %s, %d, %s\n", location, valueToSourceString(exec, scope).ascii(), index, registerName(r0).c_str());
603             break;
604         }
605         case op_resolve_base: {
606             int r0 = (++it)->u.operand;
607             int id0 = (++it)->u.operand;
608             printf("[%4d] resolve_base\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str());
609             break;
610         }
611         case op_resolve_with_base: {
612             int r0 = (++it)->u.operand;
613             int r1 = (++it)->u.operand;
614             int id0 = (++it)->u.operand;
615             printf("[%4d] resolve_with_base %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
616             break;
617         }
618         case op_resolve_func: {
619             int r0 = (++it)->u.operand;
620             int r1 = (++it)->u.operand;
621             int id0 = (++it)->u.operand;
622             printf("[%4d] resolve_func\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, identifiers[id0]).c_str());
623             break;
624         }
625         case op_get_by_id: {
626             printGetByIdOp(location, it, identifiers, "get_by_id");
627             break;
628         }
629         case op_get_by_id_self: {
630             printGetByIdOp(location, it, identifiers, "get_by_id_self");
631             break;
632         }
633         case op_get_by_id_proto: {
634             printGetByIdOp(location, it, identifiers, "get_by_id_proto");
635             break;
636         }
637         case op_get_by_id_chain: {
638             printGetByIdOp(location, it, identifiers, "get_by_id_chain");
639             break;
640         }
641         case op_get_by_id_generic: {
642             printGetByIdOp(location, it, identifiers, "get_by_id_generic");
643             break;
644         }
645         case op_get_array_length: {
646             printGetByIdOp(location, it, identifiers, "get_array_length");
647             break;
648         }
649         case op_get_string_length: {
650             printGetByIdOp(location, it, identifiers, "get_string_length");
651             break;
652         }
653         case op_put_by_id: {
654             printPutByIdOp(location, it, identifiers, "put_by_id");
655             break;
656         }
657         case op_put_by_id_replace: {
658             printPutByIdOp(location, it, identifiers, "put_by_id_replace");
659             break;
660         }
661         case op_put_by_id_transition: {
662             printPutByIdOp(location, it, identifiers, "put_by_id_transition");
663             break;
664         }
665         case op_put_by_id_generic: {
666             printPutByIdOp(location, it, identifiers, "put_by_id_generic");
667             break;
668         }
669         case op_put_getter: {
670             int r0 = (++it)->u.operand;
671             int id0 = (++it)->u.operand;
672             int r1 = (++it)->u.operand;
673             printf("[%4d] put_getter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
674             break;
675         }
676         case op_put_setter: {
677             int r0 = (++it)->u.operand;
678             int id0 = (++it)->u.operand;
679             int r1 = (++it)->u.operand;
680             printf("[%4d] put_setter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, identifiers[id0]).c_str(), registerName(r1).c_str());
681             break;
682         }
683         case op_del_by_id: {
684             int r0 = (++it)->u.operand;
685             int r1 = (++it)->u.operand;
686             int id0 = (++it)->u.operand;
687             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());
688             break;
689         }
690         case op_get_by_val: {
691             int r0 = (++it)->u.operand;
692             int r1 = (++it)->u.operand;
693             int r2 = (++it)->u.operand;
694             printf("[%4d] get_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
695             break;
696         }
697         case op_put_by_val: {
698             int r0 = (++it)->u.operand;
699             int r1 = (++it)->u.operand;
700             int r2 = (++it)->u.operand;
701             printf("[%4d] put_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
702             break;
703         }
704         case op_del_by_val: {
705             int r0 = (++it)->u.operand;
706             int r1 = (++it)->u.operand;
707             int r2 = (++it)->u.operand;
708             printf("[%4d] del_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
709             break;
710         }
711         case op_put_by_index: {
712             int r0 = (++it)->u.operand;
713             unsigned n0 = (++it)->u.operand;
714             int r1 = (++it)->u.operand;
715             printf("[%4d] put_by_index\t %s, %u, %s\n", location, registerName(r0).c_str(), n0, registerName(r1).c_str());
716             break;
717         }
718         case op_jmp: {
719             int offset = (++it)->u.operand;
720             printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
721             break;
722         }
723         case op_loop: {
724             int offset = (++it)->u.operand;
725             printf("[%4d] loop\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
726             break;
727         }
728         case op_jtrue: {
729             printConditionalJump(begin, it, location, "jtrue");
730             break;
731         }
732         case op_loop_if_true: {
733             printConditionalJump(begin, it, location, "loop_if_true");
734             break;
735         }
736         case op_jfalse: {
737             printConditionalJump(begin, it, location, "jfalse");
738             break;
739         }
740         case op_jeq_null: {
741             printConditionalJump(begin, it, location, "jeq_null");
742             break;
743         }
744         case op_jneq_null: {
745             printConditionalJump(begin, it, location, "jneq_null");
746             break;
747         }
748         case op_jnless: {
749             int r0 = (++it)->u.operand;
750             int r1 = (++it)->u.operand;
751             int offset = (++it)->u.operand;
752             printf("[%4d] jnless\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, jumpTarget(begin, it, offset));
753             break;
754         }
755         case op_loop_if_less: {
756             int r0 = (++it)->u.operand;
757             int r1 = (++it)->u.operand;
758             int offset = (++it)->u.operand;
759             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));
760             break;
761         }
762         case op_loop_if_lesseq: {
763             int r0 = (++it)->u.operand;
764             int r1 = (++it)->u.operand;
765             int offset = (++it)->u.operand;
766             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));
767             break;
768         }
769         case op_switch_imm: {
770             int tableIndex = (++it)->u.operand;
771             int defaultTarget = (++it)->u.operand;
772             int scrutineeRegister = (++it)->u.operand;
773             printf("[%4d] switch_imm\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
774             break;
775         }
776         case op_switch_char: {
777             int tableIndex = (++it)->u.operand;
778             int defaultTarget = (++it)->u.operand;
779             int scrutineeRegister = (++it)->u.operand;
780             printf("[%4d] switch_char\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
781             break;
782         }
783         case op_switch_string: {
784             int tableIndex = (++it)->u.operand;
785             int defaultTarget = (++it)->u.operand;
786             int scrutineeRegister = (++it)->u.operand;
787             printf("[%4d] switch_string\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, jumpTarget(begin, it, defaultTarget), registerName(scrutineeRegister).c_str());
788             break;
789         }
790         case op_new_func: {
791             int r0 = (++it)->u.operand;
792             int f0 = (++it)->u.operand;
793             printf("[%4d] new_func\t\t %s, f%d\n", location, registerName(r0).c_str(), f0);
794             break;
795         }
796         case op_new_func_exp: {
797             int r0 = (++it)->u.operand;
798             int f0 = (++it)->u.operand;
799             printf("[%4d] new_func_exp\t %s, f%d\n", location, registerName(r0).c_str(), f0);
800             break;
801         }
802         case op_call: {
803             int r0 = (++it)->u.operand;
804             int r1 = (++it)->u.operand;
805             int r2 = (++it)->u.operand;
806             int tempCount = (++it)->u.operand;
807             int argCount = (++it)->u.operand;
808             int registerOffset = (++it)->u.operand;
809             printf("[%4d] call\t\t %s, %s, %s, %d, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount, registerOffset);
810             break;
811         }
812         case op_call_eval: {
813             int r0 = (++it)->u.operand;
814             int r1 = (++it)->u.operand;
815             int r2 = (++it)->u.operand;
816             int tempCount = (++it)->u.operand;
817             int argCount = (++it)->u.operand;
818             int registerOffset = (++it)->u.operand;
819             printf("[%4d] call_eval\t\t %s, %s, %s, %d, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount, registerOffset);
820             break;
821         }
822         case op_tear_off_activation: {
823             int r0 = (++it)->u.operand;
824             printf("[%4d] tear_off_activation\t %s\n", location, registerName(r0).c_str());
825             break;
826         }
827         case op_tear_off_arguments: {
828             printf("[%4d] tear_off_arguments\n", location);
829             break;
830         }
831         case op_ret: {
832             int r0 = (++it)->u.operand;
833             printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str());
834             break;
835         }
836         case op_construct: {
837             int r0 = (++it)->u.operand;
838             int r1 = (++it)->u.operand;
839             int r2 = (++it)->u.operand;
840             int tempCount = (++it)->u.operand;
841             int argCount = (++it)->u.operand;
842             int registerOffset = (++it)->u.operand;
843             printf("[%4d] construct\t %s, %s, %s, %d, %d, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str(), tempCount, argCount, registerOffset);
844             break;
845         }
846         case op_construct_verify: {
847             int r0 = (++it)->u.operand;
848             int r1 = (++it)->u.operand;
849             printf("[%4d] construct_verify\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
850             break;
851         }
852         case op_get_pnames: {
853             int r0 = (++it)->u.operand;
854             int r1 = (++it)->u.operand;
855             printf("[%4d] get_pnames\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
856             break;
857         }
858         case op_next_pname: {
859             int dest = (++it)->u.operand;
860             int iter = (++it)->u.operand;
861             int offset = (++it)->u.operand;
862             printf("[%4d] next_pname\t %s, %s, %d(->%d)\n", location, registerName(dest).c_str(), registerName(iter).c_str(), offset, jumpTarget(begin, it, offset));
863             break;
864         }
865         case op_push_scope: {
866             int r0 = (++it)->u.operand;
867             printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str());
868             break;
869         }
870         case op_pop_scope: {
871             printf("[%4d] pop_scope\n", location);
872             break;
873         }
874         case op_push_new_scope: {
875             int r0 = (++it)->u.operand;
876             int id0 = (++it)->u.operand;
877             int r1 = (++it)->u.operand;
878             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());
879             break;
880         }
881         case op_jmp_scopes: {
882             int scopeDelta = (++it)->u.operand;
883             int offset = (++it)->u.operand;
884             printf("[%4d] jmp_scopes\t^%d, %d(->%d)\n", location, scopeDelta, offset, jumpTarget(begin, it, offset));
885             break;
886         }
887         case op_catch: {
888             int r0 = (++it)->u.operand;
889             printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str());
890             break;
891         }
892         case op_throw: {
893             int r0 = (++it)->u.operand;
894             printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str());
895             break;
896         }
897         case op_new_error: {
898             int r0 = (++it)->u.operand;
899             int errorType = (++it)->u.operand;
900             int k0 = (++it)->u.operand;
901             printf("[%4d] new_error\t %s, %d, %s\n", location, registerName(r0).c_str(), errorType, constantName(exec, k0, unexpectedConstants[k0]).c_str());
902             break;
903         }
904         case op_jsr: {
905             int retAddrDst = (++it)->u.operand;
906             int offset = (++it)->u.operand;
907             printf("[%4d] jsr\t\t %s, %d(->%d)\n", location, registerName(retAddrDst).c_str(), offset, jumpTarget(begin, it, offset));
908             break;
909         }
910         case op_sret: {
911             int retAddrSrc = (++it)->u.operand;
912             printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str());
913             break;
914         }
915         case op_debug: {
916             int debugHookID = (++it)->u.operand;
917             int firstLine = (++it)->u.operand;
918             int lastLine = (++it)->u.operand;
919             printf("[%4d] debug\t\t %s, %d, %d\n", location, debugHookName(debugHookID), firstLine, lastLine);
920             break;
921         }
922         case op_profile_will_call: {
923             int function = (++it)->u.operand;
924             printf("[%4d] profile_will_call %s\n", location, registerName(function).c_str());
925             break;
926         }
927         case op_profile_did_call: {
928             int function = (++it)->u.operand;
929             printf("[%4d] profile_did_call\t %s\n", location, registerName(function).c_str());
930             break;
931         }
932         case op_end: {
933             int r0 = (++it)->u.operand;
934             printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str());
935             break;
936         }
937     }
938 }
939
940 #endif // !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
941
942 CodeBlock::~CodeBlock()
943 {
944     size_t size = structureIDInstructions.size();
945     for (size_t i = 0; i < size; ++i) {
946         derefStructureIDs(&instructions[structureIDInstructions[i].opcodeIndex]);
947         if (structureIDInstructions[i].stubRoutine)
948             WTF::fastFreeExecutable(structureIDInstructions[i].stubRoutine);
949         if (CallLinkInfo* callLinkInfo = structureIDInstructions[i].linkInfoPtr) {
950             callLinkInfo->callee->removeCaller(callLinkInfo);
951             delete callLinkInfo;
952         }
953     }
954
955 #if ENABLE(CTI) 
956     unlinkCallers();
957
958     if (ctiCode)
959         WTF::fastFreeExecutable(ctiCode);
960 #endif
961 }
962
963 #if ENABLE(CTI) 
964 void CodeBlock::unlinkCallers()
965 {
966     size_t size = linkedCallerList.size();
967     for (size_t i = 0; i < size; ++i) {
968         CallLinkInfo* currentCaller = linkedCallerList[i];
969         CTI::unlinkCall(currentCaller->callerStructureStubInfo);
970         currentCaller->callerStructureStubInfo->linkInfoPtr = 0;
971         delete currentCaller;
972     }
973     linkedCallerList.clear();
974 }
975 #endif
976
977 void CodeBlock::derefStructureIDs(Instruction* vPC) const
978 {
979     Machine* machine = globalData->machine;
980
981     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
982         vPC[4].u.structureID->deref();
983         return;
984     }
985     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
986         vPC[4].u.structureID->deref();
987         vPC[5].u.structureID->deref();
988         return;
989     }
990     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
991         vPC[4].u.structureID->deref();
992         vPC[5].u.structureIDChain->deref();
993         return;
994     }
995     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
996         vPC[4].u.structureID->deref();
997         vPC[5].u.structureID->deref();
998         vPC[6].u.structureIDChain->deref();
999         return;
1000     }
1001     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
1002         vPC[4].u.structureID->deref();
1003         return;
1004     }
1005     if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
1006         if(vPC[4].u.structureID)
1007             vPC[4].u.structureID->deref();
1008         return;
1009     }
1010     
1011     // These instructions don't ref their StructureIDs.
1012     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)
1013         || vPC[0].u.opcode == machine->getOpcode(op_call_eval) || vPC[0].u.opcode == machine->getOpcode(op_call) || vPC[0].u.opcode == machine->getOpcode(op_construct));
1014 }
1015
1016 void CodeBlock::refStructureIDs(Instruction* vPC) const
1017 {
1018     Machine* machine = globalData->machine;
1019
1020     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
1021         vPC[4].u.structureID->ref();
1022         return;
1023     }
1024     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_proto)) {
1025         vPC[4].u.structureID->ref();
1026         vPC[5].u.structureID->ref();
1027         return;
1028     }
1029     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_chain)) {
1030         vPC[4].u.structureID->ref();
1031         vPC[5].u.structureIDChain->ref();
1032         return;
1033     }
1034     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_transition)) {
1035         vPC[4].u.structureID->ref();
1036         vPC[5].u.structureID->ref();
1037         vPC[6].u.structureIDChain->ref();
1038         return;
1039     }
1040     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
1041         vPC[4].u.structureID->ref();
1042         return;
1043     }
1044     
1045     // These instructions don't ref their StructureIDs.
1046     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));
1047 }
1048
1049 void CodeBlock::mark()
1050 {
1051     for (size_t i = 0; i < constantRegisters.size(); ++i)
1052         if (!constantRegisters[i].marked())
1053             constantRegisters[i].mark();
1054
1055     for (size_t i = 0; i < unexpectedConstants.size(); ++i)
1056         if (!unexpectedConstants[i]->marked())
1057             unexpectedConstants[i]->mark();
1058
1059     for (size_t i = 0; i < functions.size(); ++i)
1060         functions[i]->body()->mark();
1061
1062     for (size_t i = 0; i < functionExpressions.size(); ++i)
1063         functionExpressions[i]->body()->mark();
1064 }
1065
1066 bool CodeBlock::getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth)
1067 {
1068     Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin(); 
1069     Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1070     unsigned addressOffset = vPC - instructions.begin();
1071     ASSERT(addressOffset < instructions.size());
1072     
1073     for (; ptr != end; ++ptr) {
1074         // Handlers are ordered innermost first, so the first handler we encounter
1075         // that contains the source address is the correct handler to use.
1076         if (ptr->start <= addressOffset && ptr->end >= addressOffset) {
1077             scopeDepth = ptr->scopeDepth;
1078             target = instructions.begin() + ptr->target;
1079             return true;
1080         }
1081     }
1082     return false;
1083 }
1084
1085 void* CodeBlock::nativeExceptionCodeForHandlerVPC(const Instruction* handlerVPC)
1086 {
1087     Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin(); 
1088     Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1089     
1090     for (; ptr != end; ++ptr) {
1091         Instruction*target = instructions.begin() + ptr->target;
1092         if (handlerVPC == target)
1093             return ptr->nativeCode;
1094     }
1095
1096     return 0;
1097 }
1098
1099 int CodeBlock::lineNumberForVPC(const Instruction* vPC)
1100 {
1101     ASSERT(lineInfo.size());    
1102     unsigned instructionOffset = vPC - instructions.begin();
1103     ASSERT(instructionOffset < instructions.size());
1104
1105     if (!lineInfo.size())
1106         return 1; // Empty function
1107
1108     int low = 0;
1109     int high = lineInfo.size();
1110     while (low < high) {
1111         int mid = low + (high - low) / 2;
1112         if (lineInfo[mid].instructionOffset <= instructionOffset)
1113             low = mid + 1;
1114         else
1115             high = mid;
1116     }
1117     return lineInfo[low - 1].lineNumber;
1118 }
1119
1120 int CodeBlock::expressionRangeForVPC(const Instruction* vPC, int& divot, int& startOffset, int& endOffset)
1121 {
1122     unsigned instructionOffset = vPC - instructions.begin();
1123     ASSERT(instructionOffset < instructions.size());
1124
1125     if (!expressionInfo.size()) {
1126         // We didn't think anything could throw.  Apparently we were wrong.
1127         startOffset = 0;
1128         endOffset = 0;
1129         divot = 0;
1130         return lineNumberForVPC(vPC);
1131     }
1132
1133     int low = 0;
1134     int high = expressionInfo.size();
1135     while (low < high) {
1136         int mid = low + (high - low) / 2;
1137         if (expressionInfo[mid].instructionOffset <= instructionOffset)
1138             low = mid + 1;
1139         else
1140             high = mid;
1141     }
1142     
1143     ASSERT(low);
1144     if (!low) {
1145         startOffset = 0;
1146         endOffset = 0;
1147         divot = 0;
1148         return lineNumberForVPC(vPC);
1149     }
1150
1151     startOffset = expressionInfo[low - 1].startOffset;
1152     endOffset = expressionInfo[low - 1].endOffset;
1153     divot = expressionInfo[low - 1].divotPoint + sourceOffset;
1154     return lineNumberForVPC(vPC);
1155 }
1156
1157 int32_t SimpleJumpTable::offsetForValue(int32_t value, int32_t defaultOffset)
1158 {
1159     if (value >= min && static_cast<uint32_t>(value - min) < branchOffsets.size()) {
1160         int32_t offset = branchOffsets[value - min];
1161         if (offset)
1162             return offset;
1163     }
1164     return defaultOffset;        
1165 }
1166
1167 } // namespace JSC