Bug 20874: op_resolve does not do any form of caching
[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 "JSValue.h"
34 #include "Machine.h"
35 #include "debugger.h"
36 #include <stdio.h>
37 #include <wtf/StringExtras.h>
38
39 namespace JSC {
40
41 #if !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
42
43 static UString escapeQuotes(const UString& str)
44 {
45     UString result = str;
46     int pos = 0;
47     while ((pos = result.find('\"', pos)) >= 0) {
48         result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1);
49         pos += 4;
50     }
51     return result;
52 }
53
54 static UString valueToSourceString(ExecState* exec, JSValue* val)
55 {
56     if (val->isString()) {
57         UString result("\"");
58         result += escapeQuotes(val->toString(exec)) + "\"";
59         return result;
60     } 
61
62     return val->toString(exec);
63 }
64
65 static CString registerName(int r)
66 {
67     if (r < 0)
68         return (UString("lr") + UString::from(-r)).UTF8String(); 
69         
70     if (r == missingThisObjectMarker())
71         return "<null>";
72
73     return (UString("tr") + UString::from(r)).UTF8String();
74 }
75
76 static CString constantName(ExecState* exec, int k, JSValue* value)
77 {
78     return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String();
79 }
80
81 static CString idName(int id0, const Identifier& ident)
82 {
83     return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String();
84 }
85
86 static UString regexpToSourceString(RegExp* regExp)
87 {
88     UString pattern = UString("/") + regExp->pattern() + "/";
89     if (regExp->global())
90         pattern += "g";
91     if (regExp->ignoreCase())
92         pattern += "i";
93     if (regExp->multiline())
94         pattern += "m";
95
96     return pattern;
97 }
98
99 static CString regexpName(int re, RegExp* regexp)
100 {
101     return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String();
102 }
103
104 static UString pointerToSourceString(void* p)
105 {
106     char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
107     snprintf(buffer, sizeof(buffer), "%p", p);
108     return buffer;
109 }
110
111 NEVER_INLINE static const char* debugHookName(int debugHookID)
112 {
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";
126     }
127
128     ASSERT_NOT_REACHED();
129     return "";
130 }
131
132 static int jumpTarget(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int offset)
133 {
134     return it - begin + offset;
135 }
136
137 static void printUnaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
138 {
139     int r0 = (++it)->u.operand;
140     int r1 = (++it)->u.operand;
141
142     printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str());
143 }
144
145 static void printBinaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
146 {
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());
151 }
152
153 static void printConditionalJump(const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it, int location, const char* op)
154 {
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));
158 }
159
160 static void printGetByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
161 {
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());
166     it += 4;
167 }
168
169 static void printPutByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& identifiers, const char* op)
170 {
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());
175     it += 4;
176 }
177
178 void CodeBlock::printStructureID(const char* name, const Instruction* vPC, int operand) const
179 {
180     unsigned instructionOffset = vPC - instructions.begin();
181     printf("  [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structureID).UTF8String().c_str());
182 }
183
184 void CodeBlock::printStructureIDs(const Instruction* vPC) const
185 {
186     Machine* machine = globalData->machine;
187     unsigned instructionOffset = vPC - instructions.begin();
188
189     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id)) {
190         printStructureID("get_by_id", vPC, 4);
191         return;
192     }
193     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
194         printStructureID("get_by_id_self", vPC, 4);
195         return;
196     }
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());
199         return;
200     }
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());
203         return;
204     }
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());
207         return;
208     }
209     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id)) {
210         printStructureID("put_by_id", vPC, 4);
211         return;
212     }
213     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
214         printStructureID("put_by_id_replace", vPC, 4);
215         return;
216     }
217     if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
218         printStructureID("resolve_global", vPC, 4);
219         return;
220     }
221
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));
224 }
225
226 void CodeBlock::dump(ExecState* exec) const
227 {
228     Vector<Instruction>::const_iterator begin = instructions.begin();
229     Vector<Instruction>::const_iterator end = instructions.end();
230
231     size_t instructionCount = 0;
232     for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
233         if (exec->machine()->isOpcode(it->u.opcode))
234             ++instructionCount;
235
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);
240     
241     for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
242         dump(exec, begin, it);
243
244     if (identifiers.size()) {
245         printf("\nIdentifiers:\n");
246         size_t i = 0;
247         do {
248             printf("  id%u = %s\n", static_cast<unsigned>(i), identifiers[i].ascii());
249             ++i;
250         } while (i != identifiers.size());
251     }
252
253     if (constantRegisters.size()) {
254         printf("\nConstants:\n");
255         size_t i = 0;
256         do {
257             printf("  tr%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, constantRegisters[i].jsValue(exec)).ascii());
258             ++i;
259         } while (i < constantRegisters.size());
260     }
261
262     if (unexpectedConstants.size()) {
263         printf("\nUnexpected Constants:\n");
264         size_t i = 0;
265         do {
266             printf("  k%u = %s\n", static_cast<unsigned>(i), valueToSourceString(exec, unexpectedConstants[i]).ascii());
267             ++i;
268         } while (i < unexpectedConstants.size());
269     }
270     
271     if (regexps.size()) {
272         printf("\nRegExps:\n");
273         size_t i = 0;
274         do {
275             printf("  re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(regexps[i].get()).ascii());
276             ++i;
277         } while (i < regexps.size());
278     }
279
280     if (structureIDInstructions.size()) {
281         printf("\nStructureIDs:\n");
282         size_t i = 0;
283         do {
284              printStructureIDs(&instructions[structureIDInstructions[i].opcodeIndex]);
285              ++i;
286         } while (i < structureIDInstructions.size());
287     }
288  
289     if (exceptionHandlers.size()) {
290         printf("\nException Handlers:\n");
291         unsigned i = 0;
292         do {
293             printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, exceptionHandlers[i].start, exceptionHandlers[i].end, exceptionHandlers[i].target);
294             ++i;
295         } while (i < exceptionHandlers.size());
296     }
297     
298     if (immediateSwitchJumpTables.size()) {
299         printf("Immediate Switch Jump Tables:\n");
300         unsigned i = 0;
301         do {
302             printf("  %1d = {\n", i);
303             int entry = 0;
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) {
306                 if (!*iter)
307                     continue;
308                 printf("\t\t%4d => %04d\n", entry + immediateSwitchJumpTables[i].min, *iter);
309             }
310             printf("      }\n");
311             ++i;
312         } while (i < immediateSwitchJumpTables.size());
313     }
314     
315     if (characterSwitchJumpTables.size()) {
316         printf("\nCharacter Switch Jump Tables:\n");
317         unsigned i = 0;
318         do {
319             printf("  %1d = {\n", i);
320             int entry = 0;
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) {
323                 if (!*iter)
324                     continue;
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);
328         }
329             printf("      }\n");
330             ++i;
331         } while (i < characterSwitchJumpTables.size());
332     }
333     
334     if (stringSwitchJumpTables.size()) {
335         printf("\nString Switch Jump Tables:\n");
336         unsigned i = 0;
337         do {
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);
342             printf("      }\n");
343             ++i;
344         } while (i < stringSwitchJumpTables.size());
345     }
346
347     printf("\n");
348 }
349
350 void CodeBlock::dump(ExecState* exec, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it) const
351 {
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());
358             break;
359         }
360         case op_new_object: {
361             int r0 = (++it)->u.operand;
362             printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str());
363             break;
364         }
365         case op_new_array: {
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);
370             break;
371         }
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());
376             break;
377         }
378         case op_mov: {
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());
382             break;
383         }
384         case op_not: {
385             printUnaryOp(location, it, "not");
386             break;
387         }
388         case op_eq: {
389             printBinaryOp(location, it, "eq");
390             break;
391         }
392         case op_eq_null: {
393             printUnaryOp(location, it, "eq_null");
394             break;
395         }
396         case op_neq: {
397             printBinaryOp(location, it, "neq");
398             break;
399         }
400         case op_neq_null: {
401             printUnaryOp(location, it, "neq_null");
402             break;
403         }
404         case op_stricteq: {
405             printBinaryOp(location, it, "stricteq");
406             break;
407         }
408         case op_nstricteq: {
409             printBinaryOp(location, it, "nstricteq");
410             break;
411         }
412         case op_less: {
413             printBinaryOp(location, it, "less");
414             break;
415         }
416         case op_lesseq: {
417             printBinaryOp(location, it, "lesseq");
418             break;
419         }
420         case op_pre_inc: {
421             int r0 = (++it)->u.operand;
422             printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str());
423             break;
424         }
425         case op_pre_dec: {
426             int r0 = (++it)->u.operand;
427             printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str());
428             break;
429         }
430         case op_post_inc: {
431             printUnaryOp(location, it, "post_inc");
432             break;
433         }
434         case op_post_dec: {
435             printUnaryOp(location, it, "post_dec");
436             break;
437         }
438         case op_to_jsnumber: {
439             printUnaryOp(location, it, "to_jsnumber");
440             break;
441         }
442         case op_negate: {
443             printUnaryOp(location, it, "negate");
444             break;
445         }
446         case op_add: {
447             printBinaryOp(location, it, "add");
448             break;
449         }
450         case op_mul: {
451             printBinaryOp(location, it, "mul");
452             break;
453         }
454         case op_div: {
455             printBinaryOp(location, it, "div");
456             break;
457         }
458         case op_mod: {
459             printBinaryOp(location, it, "mod");
460             break;
461         }
462         case op_sub: {
463             printBinaryOp(location, it, "sub");
464             break;
465         }
466         case op_lshift: {
467             printBinaryOp(location, it, "lshift");
468             break;            
469         }
470         case op_rshift: {
471             printBinaryOp(location, it, "rshift");
472             break;
473         }
474         case op_urshift: {
475             printBinaryOp(location, it, "urshift");
476             break;
477         }
478         case op_bitand: {
479             printBinaryOp(location, it, "bitand");
480             break;
481         }
482         case op_bitxor: {
483             printBinaryOp(location, it, "bitxor");
484             break;
485         }
486         case op_bitor: {
487             printBinaryOp(location, it, "bitor");
488             break;
489         }
490         case op_bitnot: {
491             printUnaryOp(location, it, "bitnot");
492             break;
493         }
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());
500             break;
501         }
502         case op_typeof: {
503             printUnaryOp(location, it, "typeof");
504             break;
505         }
506         case op_is_undefined: {
507             printUnaryOp(location, it, "is_undefined");
508             break;
509         }
510         case op_is_boolean: {
511             printUnaryOp(location, it, "is_boolean");
512             break;
513         }
514         case op_is_number: {
515             printUnaryOp(location, it, "is_number");
516             break;
517         }
518         case op_is_string: {
519             printUnaryOp(location, it, "is_string");
520             break;
521         }
522         case op_is_object: {
523             printUnaryOp(location, it, "is_object");
524             break;
525         }
526         case op_is_function: {
527             printUnaryOp(location, it, "is_function");
528             break;
529         }
530         case op_in: {
531             printBinaryOp(location, it, "in");
532             break;
533         }
534         case op_resolve: {
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());
538             break;
539         }
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);
545             break;
546         }
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());
552             it += 2;
553             break;
554         }
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);
560             break;
561         }
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());
567             break;
568         }
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);
574             break;
575         }
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());
581             break;
582         }
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());
587             break;
588         }
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());
594             break;
595         }
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());
601             break;
602         }
603         case op_get_by_id: {
604             printGetByIdOp(location, it, identifiers, "get_by_id");
605             break;
606         }
607         case op_get_by_id_self: {
608             printGetByIdOp(location, it, identifiers, "get_by_id_self");
609             break;
610         }
611         case op_get_by_id_proto: {
612             printGetByIdOp(location, it, identifiers, "get_by_id_proto");
613             break;
614         }
615         case op_get_by_id_chain: {
616             printGetByIdOp(location, it, identifiers, "get_by_id_chain");
617             break;
618         }
619         case op_get_by_id_generic: {
620             printGetByIdOp(location, it, identifiers, "get_by_id_generic");
621             break;
622         }
623         case op_get_array_length: {
624             printGetByIdOp(location, it, identifiers, "get_array_length");
625             break;
626         }
627         case op_get_string_length: {
628             printGetByIdOp(location, it, identifiers, "get_string_length");
629             break;
630         }
631         case op_put_by_id: {
632             printPutByIdOp(location, it, identifiers, "put_by_id");
633             break;
634         }
635         case op_put_by_id_replace: {
636             printPutByIdOp(location, it, identifiers, "put_by_id_replace");
637             break;
638         }
639         case op_put_by_id_transition: {
640             printPutByIdOp(location, it, identifiers, "put_by_id_transition");
641             break;
642         }
643         case op_put_by_id_generic: {
644             printPutByIdOp(location, it, identifiers, "put_by_id_generic");
645             break;
646         }
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());
652             break;
653         }
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());
659             break;
660         }
661         case op_del_by_id: {
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());
666             break;
667         }
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());
673             break;
674         }
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());
680             break;
681         }
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());
687             break;
688         }
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());
694             break;
695         }
696         case op_jmp: {
697             int offset = (++it)->u.operand;
698             printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
699             break;
700         }
701         case op_loop: {
702             int offset = (++it)->u.operand;
703             printf("[%4d] loop\t\t %d(->%d)\n", location, offset, jumpTarget(begin, it, offset));
704             break;
705         }
706         case op_jtrue: {
707             printConditionalJump(begin, it, location, "jtrue");
708             break;
709         }
710         case op_loop_if_true: {
711             printConditionalJump(begin, it, location, "loop_if_true");
712             break;
713         }
714         case op_jfalse: {
715             printConditionalJump(begin, it, location, "jfalse");
716             break;
717         }
718         case op_jnless: {
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));
723             break;
724         }
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));
730             break;
731         }
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));
737             break;
738         }
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());
744             break;
745         }
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());
751             break;
752         }
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());
758             break;
759         }
760         case op_new_func: {
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);
764             break;
765         }
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);
770             break;
771         }
772         case op_call: {
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);
779             break;
780         }
781         case op_call_eval: {
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);
788             break;
789         }
790         case op_ret: {
791             int r0 = (++it)->u.operand;
792             printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str());
793             break;
794         }
795         case op_construct: {
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);
802             break;
803         }
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());
808             break;
809         }
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());
814             break;
815         }
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));
821             break;
822         }
823         case op_push_scope: {
824             int r0 = (++it)->u.operand;
825             printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str());
826             break;
827         }
828         case op_pop_scope: {
829             printf("[%4d] pop_scope\n", location);
830             break;
831         }
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());
837             break;
838         }
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));
843             break;
844         }
845         case op_catch: {
846             int r0 = (++it)->u.operand;
847             printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str());
848             break;
849         }
850         case op_throw: {
851             int r0 = (++it)->u.operand;
852             printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str());
853             break;
854         }
855         case op_new_error: {
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());
860             break;
861         }
862         case op_jsr: {
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));
866             break;
867         }
868         case op_sret: {
869             int retAddrSrc = (++it)->u.operand;
870             printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str());
871             break;
872         }
873         case op_debug: {
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);
878             break;
879         }
880         case op_end: {
881             int r0 = (++it)->u.operand;
882             printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str());
883             break;
884         }
885         default: {
886             ASSERT_NOT_REACHED();
887             break;
888         }
889     }
890 }
891
892 #endif // !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
893
894 CodeBlock::~CodeBlock()
895 {
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);
901     }
902 #if ENABLE(CTI)
903     if (ctiCode)
904         fastFree(ctiCode);
905 #endif
906 }
907
908 void CodeBlock::derefStructureIDs(Instruction* vPC) const
909 {
910     Machine* machine = globalData->machine;
911
912     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
913         vPC[4].u.structureID->deref();
914         return;
915     }
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();
919         return;
920     }
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();
924         return;
925     }
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();
930         return;
931     }
932     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
933         vPC[4].u.structureID->deref();
934         return;
935     }
936     if (vPC[0].u.opcode == machine->getOpcode(op_resolve_global)) {
937         if(vPC[4].u.structureID)
938             vPC[4].u.structureID->deref();
939         return;
940     }
941     
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));
944 }
945
946 void CodeBlock::refStructureIDs(Instruction* vPC) const
947 {
948     Machine* machine = globalData->machine;
949
950     if (vPC[0].u.opcode == machine->getOpcode(op_get_by_id_self)) {
951         vPC[4].u.structureID->ref();
952         return;
953     }
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();
957         return;
958     }
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();
962         return;
963     }
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();
968         return;
969     }
970     if (vPC[0].u.opcode == machine->getOpcode(op_put_by_id_replace)) {
971         vPC[4].u.structureID->ref();
972         return;
973     }
974     
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));
977 }
978
979 void CodeBlock::mark()
980 {
981     for (size_t i = 0; i < constantRegisters.size(); ++i)
982         if (!constantRegisters[i].marked())
983             constantRegisters[i].mark();
984
985     for (size_t i = 0; i < unexpectedConstants.size(); ++i)
986         if (!unexpectedConstants[i]->marked())
987             unexpectedConstants[i]->mark();
988
989     for (size_t i = 0; i < functions.size(); ++i)
990         functions[i]->body()->mark();
991
992     for (size_t i = 0; i < functionExpressions.size(); ++i)
993         functionExpressions[i]->body()->mark();
994 }
995
996 bool CodeBlock::getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth)
997 {
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());
1002     
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;
1009             return true;
1010         }
1011     }
1012     return false;
1013 }
1014
1015 void* CodeBlock::nativeExceptionCodeForHandlerVPC(const Instruction* handlerVPC)
1016 {
1017     Vector<HandlerInfo>::iterator ptr = exceptionHandlers.begin(); 
1018     Vector<HandlerInfo>::iterator end = exceptionHandlers.end();
1019     
1020     for (; ptr != end; ++ptr) {
1021         Instruction*target = instructions.begin() + ptr->target;
1022         if (handlerVPC == target)
1023             return ptr->nativeCode;
1024     }
1025
1026     return 0;
1027 }
1028
1029 int CodeBlock::lineNumberForVPC(const Instruction* vPC)
1030 {
1031     ASSERT(lineInfo.size());    
1032     unsigned instructionOffset = vPC - instructions.begin();
1033     ASSERT(instructionOffset < instructions.size());
1034
1035     if (!lineInfo.size())
1036         return 1; // Empty function
1037
1038     int low = 0;
1039     int high = lineInfo.size();
1040     while (low < high) {
1041         int mid = low + (high - low) / 2;
1042         if (lineInfo[mid].instructionOffset <= instructionOffset)
1043             low = mid + 1;
1044         else
1045             high = mid;
1046     }
1047     return lineInfo[low - 1].lineNumber;
1048 }
1049
1050 int CodeBlock::expressionRangeForVPC(const Instruction* vPC, int& divot, int& startOffset, int& endOffset)
1051 {
1052     unsigned instructionOffset = vPC - instructions.begin();
1053     ASSERT(instructionOffset < instructions.size());
1054
1055     if (!expressionInfo.size()) {
1056         // We didn't think anything could throw.  Apparently we were wrong.
1057         startOffset = 0;
1058         endOffset = 0;
1059         divot = 0;
1060         return lineNumberForVPC(vPC);
1061     }
1062
1063     int low = 0;
1064     int high = expressionInfo.size();
1065     while (low < high) {
1066         int mid = low + (high - low) / 2;
1067         if (expressionInfo[mid].instructionOffset <= instructionOffset)
1068             low = mid + 1;
1069         else
1070             high = mid;
1071     }
1072     
1073     ASSERT(low);
1074     if (!low) {
1075         startOffset = 0;
1076         endOffset = 0;
1077         divot = 0;
1078         return lineNumberForVPC(vPC);
1079     }
1080
1081     startOffset = expressionInfo[low - 1].startOffset;
1082     endOffset = expressionInfo[low - 1].endOffset;
1083     divot = expressionInfo[low - 1].divotPoint + sourceOffset;
1084     return lineNumberForVPC(vPC);
1085 }
1086
1087 int32_t SimpleJumpTable::offsetForValue(int32_t value, int32_t defaultOffset)
1088 {
1089     if (value >= min && static_cast<uint32_t>(value - min) < branchOffsets.size()) {
1090         int32_t offset = branchOffsets[value - min];
1091         if (offset)
1092             return offset;
1093     }
1094     return defaultOffset;        
1095 }
1096
1097 } // namespace JSC