source: webkit/trunk/JavaScriptCore/bytecode/CodeBlock.cpp@ 49734

Last change on this file since 49734 was 49734, checked in by [email protected], 16 years ago

Rolled back in r49717 with the build maybe working now?

File size: 61.1 KB
Line 
1/*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <[email protected]>
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 "JIT.h"
34#include "JSValue.h"
35#include "Interpreter.h"
36#include "JSFunction.h"
37#include "JSStaticScopeObject.h"
38#include "Debugger.h"
39#include "BytecodeGenerator.h"
40#include <stdio.h>
41#include <wtf/StringExtras.h>
42
43#define DUMP_CODE_BLOCK_STATISTICS 0
44
45namespace JSC {
46
47#if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
48
49static UString escapeQuotes(const UString& str)
50{
51 UString result = str;
52 int pos = 0;
53 while ((pos = result.find('\"', pos)) >= 0) {
54 result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1);
55 pos += 4;
56 }
57 return result;
58}
59
60static UString valueToSourceString(ExecState* exec, JSValue val)
61{
62 if (!val)
63 return "0";
64
65 if (val.isString()) {
66 UString result("\"");
67 result += escapeQuotes(val.toString(exec)) + "\"";
68 return result;
69 }
70
71 return val.toString(exec);
72}
73
74static CString registerName(int r)
75{
76 if (r == missingThisObjectMarker())
77 return "<null>";
78
79 return (UString("r") + UString::from(r)).UTF8String();
80}
81
82static CString constantName(ExecState* exec, int k, JSValue value)
83{
84 return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String();
85}
86
87static CString idName(int id0, const Identifier& ident)
88{
89 return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String();
90}
91
92static UString regexpToSourceString(RegExp* regExp)
93{
94 UString pattern = UString("/") + regExp->pattern() + "/";
95 if (regExp->global())
96 pattern += "g";
97 if (regExp->ignoreCase())
98 pattern += "i";
99 if (regExp->multiline())
100 pattern += "m";
101
102 return pattern;
103}
104
105static CString regexpName(int re, RegExp* regexp)
106{
107 return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String();
108}
109
110static UString pointerToSourceString(void* p)
111{
112 char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0
113 snprintf(buffer, sizeof(buffer), "%p", p);
114 return buffer;
115}
116
117NEVER_INLINE static const char* debugHookName(int debugHookID)
118{
119 switch (static_cast<DebugHookID>(debugHookID)) {
120 case DidEnterCallFrame:
121 return "didEnterCallFrame";
122 case WillLeaveCallFrame:
123 return "willLeaveCallFrame";
124 case WillExecuteStatement:
125 return "willExecuteStatement";
126 case WillExecuteProgram:
127 return "willExecuteProgram";
128 case DidExecuteProgram:
129 return "didExecuteProgram";
130 case DidReachBreakpoint:
131 return "didReachBreakpoint";
132 }
133
134 ASSERT_NOT_REACHED();
135 return "";
136}
137
138static void printUnaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
139{
140 int r0 = (++it)->u.operand;
141 int r1 = (++it)->u.operand;
142
143 printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str());
144}
145
146static void printBinaryOp(int location, Vector<Instruction>::const_iterator& it, const char* op)
147{
148 int r0 = (++it)->u.operand;
149 int r1 = (++it)->u.operand;
150 int r2 = (++it)->u.operand;
151 printf("[%4d] %s\t\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
152}
153
154static void printConditionalJump(const Vector<Instruction>::const_iterator&, Vector<Instruction>::const_iterator& it, int location, const char* op)
155{
156 int r0 = (++it)->u.operand;
157 int offset = (++it)->u.operand;
158 printf("[%4d] %s\t\t %s, %d(->%d)\n", location, op, registerName(r0).c_str(), offset, location + offset);
159}
160
161static void printGetByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& m_identifiers, const char* op)
162{
163 int r0 = (++it)->u.operand;
164 int r1 = (++it)->u.operand;
165 int id0 = (++it)->u.operand;
166 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, m_identifiers[id0]).c_str());
167 it += 4;
168}
169
170static void printPutByIdOp(int location, Vector<Instruction>::const_iterator& it, const Vector<Identifier>& m_identifiers, const char* op)
171{
172 int r0 = (++it)->u.operand;
173 int id0 = (++it)->u.operand;
174 int r1 = (++it)->u.operand;
175 printf("[%4d] %s\t %s, %s, %s\n", location, op, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), registerName(r1).c_str());
176 it += 4;
177}
178
179#if ENABLE(JIT)
180static bool isGlobalResolve(OpcodeID opcodeID)
181{
182 return opcodeID == op_resolve_global;
183}
184
185static bool isPropertyAccess(OpcodeID opcodeID)
186{
187 switch (opcodeID) {
188 case op_get_by_id_self:
189 case op_get_by_id_proto:
190 case op_get_by_id_chain:
191 case op_get_by_id_self_list:
192 case op_get_by_id_proto_list:
193 case op_put_by_id_transition:
194 case op_put_by_id_replace:
195 case op_get_by_id:
196 case op_put_by_id:
197 case op_get_by_id_generic:
198 case op_put_by_id_generic:
199 case op_get_array_length:
200 case op_get_string_length:
201 return true;
202 default:
203 return false;
204 }
205}
206
207static unsigned instructionOffsetForNth(ExecState* exec, const Vector<Instruction>& instructions, int nth, bool (*predicate)(OpcodeID))
208{
209 size_t i = 0;
210 while (i < instructions.size()) {
211 OpcodeID currentOpcode = exec->interpreter()->getOpcodeID(instructions[i].u.opcode);
212 if (predicate(currentOpcode)) {
213 if (!--nth)
214 return i;
215 }
216 i += opcodeLengths[currentOpcode];
217 }
218
219 ASSERT_NOT_REACHED();
220 return 0;
221}
222
223static void printGlobalResolveInfo(const GlobalResolveInfo& resolveInfo, unsigned instructionOffset)
224{
225 printf(" [%4d] %s: %s\n", instructionOffset, "resolve_global", pointerToSourceString(resolveInfo.structure).UTF8String().c_str());
226}
227
228static void printStructureStubInfo(const StructureStubInfo& stubInfo, unsigned instructionOffset)
229{
230 switch (stubInfo.accessType) {
231 case access_get_by_id_self:
232 printf(" [%4d] %s: %s\n", instructionOffset, "get_by_id_self", pointerToSourceString(stubInfo.u.getByIdSelf.baseObjectStructure).UTF8String().c_str());
233 return;
234 case access_get_by_id_proto:
235 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_proto", pointerToSourceString(stubInfo.u.getByIdProto.baseObjectStructure).UTF8String().c_str(), pointerToSourceString(stubInfo.u.getByIdProto.prototypeStructure).UTF8String().c_str());
236 return;
237 case access_get_by_id_chain:
238 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_chain", pointerToSourceString(stubInfo.u.getByIdChain.baseObjectStructure).UTF8String().c_str(), pointerToSourceString(stubInfo.u.getByIdChain.chain).UTF8String().c_str());
239 return;
240 case access_get_by_id_self_list:
241 printf(" [%4d] %s: %s (%d)\n", instructionOffset, "op_get_by_id_self_list", pointerToSourceString(stubInfo.u.getByIdSelfList.structureList).UTF8String().c_str(), stubInfo.u.getByIdSelfList.listSize);
242 return;
243 case access_get_by_id_proto_list:
244 printf(" [%4d] %s: %s (%d)\n", instructionOffset, "op_get_by_id_proto_list", pointerToSourceString(stubInfo.u.getByIdProtoList.structureList).UTF8String().c_str(), stubInfo.u.getByIdProtoList.listSize);
245 return;
246 case access_put_by_id_transition:
247 printf(" [%4d] %s: %s, %s, %s\n", instructionOffset, "put_by_id_transition", pointerToSourceString(stubInfo.u.putByIdTransition.previousStructure).UTF8String().c_str(), pointerToSourceString(stubInfo.u.putByIdTransition.structure).UTF8String().c_str(), pointerToSourceString(stubInfo.u.putByIdTransition.chain).UTF8String().c_str());
248 return;
249 case access_put_by_id_replace:
250 printf(" [%4d] %s: %s\n", instructionOffset, "put_by_id_replace", pointerToSourceString(stubInfo.u.putByIdReplace.baseObjectStructure).UTF8String().c_str());
251 return;
252 case access_get_by_id:
253 printf(" [%4d] %s\n", instructionOffset, "get_by_id");
254 return;
255 case access_put_by_id:
256 printf(" [%4d] %s\n", instructionOffset, "put_by_id");
257 return;
258 case access_get_by_id_generic:
259 printf(" [%4d] %s\n", instructionOffset, "op_get_by_id_generic");
260 return;
261 case access_put_by_id_generic:
262 printf(" [%4d] %s\n", instructionOffset, "op_put_by_id_generic");
263 return;
264 case access_get_array_length:
265 printf(" [%4d] %s\n", instructionOffset, "op_get_array_length");
266 return;
267 case access_get_string_length:
268 printf(" [%4d] %s\n", instructionOffset, "op_get_string_length");
269 return;
270 default:
271 ASSERT_NOT_REACHED();
272 }
273}
274#endif
275
276void CodeBlock::printStructure(const char* name, const Instruction* vPC, int operand) const
277{
278 unsigned instructionOffset = vPC - m_instructions.begin();
279 printf(" [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structure).UTF8String().c_str());
280}
281
282void CodeBlock::printStructures(const Instruction* vPC) const
283{
284 Interpreter* interpreter = m_globalData->interpreter;
285 unsigned instructionOffset = vPC - m_instructions.begin();
286
287 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id)) {
288 printStructure("get_by_id", vPC, 4);
289 return;
290 }
291 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
292 printStructure("get_by_id_self", vPC, 4);
293 return;
294 }
295 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
296 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_proto", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structure).UTF8String().c_str());
297 return;
298 }
299 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
300 printf(" [%4d] %s: %s, %s, %s\n", instructionOffset, "put_by_id_transition", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[6].u.structureChain).UTF8String().c_str());
301 return;
302 }
303 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
304 printf(" [%4d] %s: %s, %s\n", instructionOffset, "get_by_id_chain", pointerToSourceString(vPC[4].u.structure).UTF8String().c_str(), pointerToSourceString(vPC[5].u.structureChain).UTF8String().c_str());
305 return;
306 }
307 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id)) {
308 printStructure("put_by_id", vPC, 4);
309 return;
310 }
311 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
312 printStructure("put_by_id_replace", vPC, 4);
313 return;
314 }
315 if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) {
316 printStructure("resolve_global", vPC, 4);
317 return;
318 }
319
320 // These m_instructions doesn't ref Structures.
321 ASSERT(vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_call) || vPC[0].u.opcode == interpreter->getOpcode(op_call_eval) || vPC[0].u.opcode == interpreter->getOpcode(op_construct));
322}
323
324void CodeBlock::dump(ExecState* exec) const
325{
326 if (m_instructions.isEmpty()) {
327 printf("No instructions available.\n");
328 return;
329 }
330
331 size_t instructionCount = 0;
332
333 for (size_t i = 0; i < m_instructions.size(); i += opcodeLengths[exec->interpreter()->getOpcodeID(m_instructions[i].u.opcode)])
334 ++instructionCount;
335
336 printf("%lu m_instructions; %lu bytes at %p; %d parameter(s); %d callee register(s)\n\n",
337 static_cast<unsigned long>(instructionCount),
338 static_cast<unsigned long>(m_instructions.size() * sizeof(Instruction)),
339 this, m_numParameters, m_numCalleeRegisters);
340
341 Vector<Instruction>::const_iterator begin = m_instructions.begin();
342 Vector<Instruction>::const_iterator end = m_instructions.end();
343 for (Vector<Instruction>::const_iterator it = begin; it != end; ++it)
344 dump(exec, begin, it);
345
346 if (!m_identifiers.isEmpty()) {
347 printf("\nIdentifiers:\n");
348 size_t i = 0;
349 do {
350 printf(" id%u = %s\n", static_cast<unsigned>(i), m_identifiers[i].ascii());
351 ++i;
352 } while (i != m_identifiers.size());
353 }
354
355 if (!m_constantRegisters.isEmpty()) {
356 printf("\nConstants:\n");
357 unsigned registerIndex = m_numVars;
358 size_t i = 0;
359 do {
360 printf(" r%u = %s\n", registerIndex, valueToSourceString(exec, m_constantRegisters[i].jsValue()).ascii());
361 ++i;
362 ++registerIndex;
363 } while (i < m_constantRegisters.size());
364 }
365
366 if (m_rareData && !m_rareData->m_regexps.isEmpty()) {
367 printf("\nm_regexps:\n");
368 size_t i = 0;
369 do {
370 printf(" re%u = %s\n", static_cast<unsigned>(i), regexpToSourceString(m_rareData->m_regexps[i].get()).ascii());
371 ++i;
372 } while (i < m_rareData->m_regexps.size());
373 }
374
375#if ENABLE(JIT)
376 if (!m_globalResolveInfos.isEmpty() || !m_structureStubInfos.isEmpty())
377 printf("\nStructures:\n");
378
379 if (!m_globalResolveInfos.isEmpty()) {
380 size_t i = 0;
381 do {
382 printGlobalResolveInfo(m_globalResolveInfos[i], instructionOffsetForNth(exec, m_instructions, i + 1, isGlobalResolve));
383 ++i;
384 } while (i < m_globalResolveInfos.size());
385 }
386 if (!m_structureStubInfos.isEmpty()) {
387 size_t i = 0;
388 do {
389 printStructureStubInfo(m_structureStubInfos[i], instructionOffsetForNth(exec, m_instructions, i + 1, isPropertyAccess));
390 ++i;
391 } while (i < m_structureStubInfos.size());
392 }
393#else
394 if (!m_globalResolveInstructions.isEmpty() || !m_propertyAccessInstructions.isEmpty())
395 printf("\nStructures:\n");
396
397 if (!m_globalResolveInstructions.isEmpty()) {
398 size_t i = 0;
399 do {
400 printStructures(&m_instructions[m_globalResolveInstructions[i]]);
401 ++i;
402 } while (i < m_globalResolveInstructions.size());
403 }
404 if (!m_propertyAccessInstructions.isEmpty()) {
405 size_t i = 0;
406 do {
407 printStructures(&m_instructions[m_propertyAccessInstructions[i]]);
408 ++i;
409 } while (i < m_propertyAccessInstructions.size());
410 }
411#endif
412
413 if (m_rareData && !m_rareData->m_exceptionHandlers.isEmpty()) {
414 printf("\nException Handlers:\n");
415 unsigned i = 0;
416 do {
417 printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] }\n", i + 1, m_rareData->m_exceptionHandlers[i].start, m_rareData->m_exceptionHandlers[i].end, m_rareData->m_exceptionHandlers[i].target);
418 ++i;
419 } while (i < m_rareData->m_exceptionHandlers.size());
420 }
421
422 if (m_rareData && !m_rareData->m_immediateSwitchJumpTables.isEmpty()) {
423 printf("Immediate Switch Jump Tables:\n");
424 unsigned i = 0;
425 do {
426 printf(" %1d = {\n", i);
427 int entry = 0;
428 Vector<int32_t>::const_iterator end = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.end();
429 for (Vector<int32_t>::const_iterator iter = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
430 if (!*iter)
431 continue;
432 printf("\t\t%4d => %04d\n", entry + m_rareData->m_immediateSwitchJumpTables[i].min, *iter);
433 }
434 printf(" }\n");
435 ++i;
436 } while (i < m_rareData->m_immediateSwitchJumpTables.size());
437 }
438
439 if (m_rareData && !m_rareData->m_characterSwitchJumpTables.isEmpty()) {
440 printf("\nCharacter Switch Jump Tables:\n");
441 unsigned i = 0;
442 do {
443 printf(" %1d = {\n", i);
444 int entry = 0;
445 Vector<int32_t>::const_iterator end = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.end();
446 for (Vector<int32_t>::const_iterator iter = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) {
447 if (!*iter)
448 continue;
449 ASSERT(!((i + m_rareData->m_characterSwitchJumpTables[i].min) & ~0xFFFF));
450 UChar ch = static_cast<UChar>(entry + m_rareData->m_characterSwitchJumpTables[i].min);
451 printf("\t\t\"%s\" => %04d\n", UString(&ch, 1).ascii(), *iter);
452 }
453 printf(" }\n");
454 ++i;
455 } while (i < m_rareData->m_characterSwitchJumpTables.size());
456 }
457
458 if (m_rareData && !m_rareData->m_stringSwitchJumpTables.isEmpty()) {
459 printf("\nString Switch Jump Tables:\n");
460 unsigned i = 0;
461 do {
462 printf(" %1d = {\n", i);
463 StringJumpTable::StringOffsetTable::const_iterator end = m_rareData->m_stringSwitchJumpTables[i].offsetTable.end();
464 for (StringJumpTable::StringOffsetTable::const_iterator iter = m_rareData->m_stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter)
465 printf("\t\t\"%s\" => %04d\n", UString(iter->first).ascii(), iter->second.branchOffset);
466 printf(" }\n");
467 ++i;
468 } while (i < m_rareData->m_stringSwitchJumpTables.size());
469 }
470
471 printf("\n");
472}
473
474void CodeBlock::dump(ExecState* exec, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator& it) const
475{
476 int location = it - begin;
477 switch (exec->interpreter()->getOpcodeID(it->u.opcode)) {
478 case op_enter: {
479 printf("[%4d] enter\n", location);
480 break;
481 }
482 case op_enter_with_activation: {
483 int r0 = (++it)->u.operand;
484 printf("[%4d] enter_with_activation %s\n", location, registerName(r0).c_str());
485 break;
486 }
487 case op_create_arguments: {
488 printf("[%4d] create_arguments\n", location);
489 break;
490 }
491 case op_init_arguments: {
492 printf("[%4d] init_arguments\n", location);
493 break;
494 }
495 case op_convert_this: {
496 int r0 = (++it)->u.operand;
497 printf("[%4d] convert_this %s\n", location, registerName(r0).c_str());
498 break;
499 }
500 case op_new_object: {
501 int r0 = (++it)->u.operand;
502 printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str());
503 break;
504 }
505 case op_new_array: {
506 int dst = (++it)->u.operand;
507 int argv = (++it)->u.operand;
508 int argc = (++it)->u.operand;
509 printf("[%4d] new_array\t %s, %s, %d\n", location, registerName(dst).c_str(), registerName(argv).c_str(), argc);
510 break;
511 }
512 case op_new_regexp: {
513 int r0 = (++it)->u.operand;
514 int re0 = (++it)->u.operand;
515 printf("[%4d] new_regexp\t %s, %s\n", location, registerName(r0).c_str(), regexpName(re0, regexp(re0)).c_str());
516 break;
517 }
518 case op_mov: {
519 int r0 = (++it)->u.operand;
520 int r1 = (++it)->u.operand;
521 printf("[%4d] mov\t\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
522 break;
523 }
524 case op_not: {
525 printUnaryOp(location, it, "not");
526 break;
527 }
528 case op_eq: {
529 printBinaryOp(location, it, "eq");
530 break;
531 }
532 case op_eq_null: {
533 printUnaryOp(location, it, "eq_null");
534 break;
535 }
536 case op_neq: {
537 printBinaryOp(location, it, "neq");
538 break;
539 }
540 case op_neq_null: {
541 printUnaryOp(location, it, "neq_null");
542 break;
543 }
544 case op_stricteq: {
545 printBinaryOp(location, it, "stricteq");
546 break;
547 }
548 case op_nstricteq: {
549 printBinaryOp(location, it, "nstricteq");
550 break;
551 }
552 case op_less: {
553 printBinaryOp(location, it, "less");
554 break;
555 }
556 case op_lesseq: {
557 printBinaryOp(location, it, "lesseq");
558 break;
559 }
560 case op_pre_inc: {
561 int r0 = (++it)->u.operand;
562 printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str());
563 break;
564 }
565 case op_pre_dec: {
566 int r0 = (++it)->u.operand;
567 printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str());
568 break;
569 }
570 case op_post_inc: {
571 printUnaryOp(location, it, "post_inc");
572 break;
573 }
574 case op_post_dec: {
575 printUnaryOp(location, it, "post_dec");
576 break;
577 }
578 case op_to_jsnumber: {
579 printUnaryOp(location, it, "to_jsnumber");
580 break;
581 }
582 case op_negate: {
583 printUnaryOp(location, it, "negate");
584 break;
585 }
586 case op_add: {
587 printBinaryOp(location, it, "add");
588 ++it;
589 break;
590 }
591 case op_mul: {
592 printBinaryOp(location, it, "mul");
593 ++it;
594 break;
595 }
596 case op_div: {
597 printBinaryOp(location, it, "div");
598 ++it;
599 break;
600 }
601 case op_mod: {
602 printBinaryOp(location, it, "mod");
603 break;
604 }
605 case op_sub: {
606 printBinaryOp(location, it, "sub");
607 ++it;
608 break;
609 }
610 case op_lshift: {
611 printBinaryOp(location, it, "lshift");
612 break;
613 }
614 case op_rshift: {
615 printBinaryOp(location, it, "rshift");
616 break;
617 }
618 case op_urshift: {
619 printBinaryOp(location, it, "urshift");
620 break;
621 }
622 case op_bitand: {
623 printBinaryOp(location, it, "bitand");
624 ++it;
625 break;
626 }
627 case op_bitxor: {
628 printBinaryOp(location, it, "bitxor");
629 ++it;
630 break;
631 }
632 case op_bitor: {
633 printBinaryOp(location, it, "bitor");
634 ++it;
635 break;
636 }
637 case op_bitnot: {
638 printUnaryOp(location, it, "bitnot");
639 break;
640 }
641 case op_instanceof: {
642 int r0 = (++it)->u.operand;
643 int r1 = (++it)->u.operand;
644 int r2 = (++it)->u.operand;
645 int r3 = (++it)->u.operand;
646 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());
647 break;
648 }
649 case op_typeof: {
650 printUnaryOp(location, it, "typeof");
651 break;
652 }
653 case op_is_undefined: {
654 printUnaryOp(location, it, "is_undefined");
655 break;
656 }
657 case op_is_boolean: {
658 printUnaryOp(location, it, "is_boolean");
659 break;
660 }
661 case op_is_number: {
662 printUnaryOp(location, it, "is_number");
663 break;
664 }
665 case op_is_string: {
666 printUnaryOp(location, it, "is_string");
667 break;
668 }
669 case op_is_object: {
670 printUnaryOp(location, it, "is_object");
671 break;
672 }
673 case op_is_function: {
674 printUnaryOp(location, it, "is_function");
675 break;
676 }
677 case op_in: {
678 printBinaryOp(location, it, "in");
679 break;
680 }
681 case op_resolve: {
682 int r0 = (++it)->u.operand;
683 int id0 = (++it)->u.operand;
684 printf("[%4d] resolve\t\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str());
685 break;
686 }
687 case op_resolve_skip: {
688 int r0 = (++it)->u.operand;
689 int id0 = (++it)->u.operand;
690 int skipLevels = (++it)->u.operand;
691 printf("[%4d] resolve_skip\t %s, %s, %d\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), skipLevels);
692 break;
693 }
694 case op_resolve_global: {
695 int r0 = (++it)->u.operand;
696 JSValue scope = JSValue((++it)->u.jsCell);
697 int id0 = (++it)->u.operand;
698 printf("[%4d] resolve_global\t %s, %s, %s\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), idName(id0, m_identifiers[id0]).c_str());
699 it += 2;
700 break;
701 }
702 case op_get_scoped_var: {
703 int r0 = (++it)->u.operand;
704 int index = (++it)->u.operand;
705 int skipLevels = (++it)->u.operand;
706 printf("[%4d] get_scoped_var\t %s, %d, %d\n", location, registerName(r0).c_str(), index, skipLevels);
707 break;
708 }
709 case op_put_scoped_var: {
710 int index = (++it)->u.operand;
711 int skipLevels = (++it)->u.operand;
712 int r0 = (++it)->u.operand;
713 printf("[%4d] put_scoped_var\t %d, %d, %s\n", location, index, skipLevels, registerName(r0).c_str());
714 break;
715 }
716 case op_get_global_var: {
717 int r0 = (++it)->u.operand;
718 JSValue scope = JSValue((++it)->u.jsCell);
719 int index = (++it)->u.operand;
720 printf("[%4d] get_global_var\t %s, %s, %d\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), index);
721 break;
722 }
723 case op_put_global_var: {
724 JSValue scope = JSValue((++it)->u.jsCell);
725 int index = (++it)->u.operand;
726 int r0 = (++it)->u.operand;
727 printf("[%4d] put_global_var\t %s, %d, %s\n", location, valueToSourceString(exec, scope).ascii(), index, registerName(r0).c_str());
728 break;
729 }
730 case op_resolve_base: {
731 int r0 = (++it)->u.operand;
732 int id0 = (++it)->u.operand;
733 printf("[%4d] resolve_base\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str());
734 break;
735 }
736 case op_resolve_with_base: {
737 int r0 = (++it)->u.operand;
738 int r1 = (++it)->u.operand;
739 int id0 = (++it)->u.operand;
740 printf("[%4d] resolve_with_base %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, m_identifiers[id0]).c_str());
741 break;
742 }
743 case op_get_by_id: {
744 printGetByIdOp(location, it, m_identifiers, "get_by_id");
745 break;
746 }
747 case op_get_by_id_self: {
748 printGetByIdOp(location, it, m_identifiers, "get_by_id_self");
749 break;
750 }
751 case op_get_by_id_self_list: {
752 printGetByIdOp(location, it, m_identifiers, "get_by_id_self_list");
753 break;
754 }
755 case op_get_by_id_proto: {
756 printGetByIdOp(location, it, m_identifiers, "get_by_id_proto");
757 break;
758 }
759 case op_get_by_id_proto_list: {
760 printGetByIdOp(location, it, m_identifiers, "op_get_by_id_proto_list");
761 break;
762 }
763 case op_get_by_id_chain: {
764 printGetByIdOp(location, it, m_identifiers, "get_by_id_chain");
765 break;
766 }
767 case op_get_by_id_generic: {
768 printGetByIdOp(location, it, m_identifiers, "get_by_id_generic");
769 break;
770 }
771 case op_get_array_length: {
772 printGetByIdOp(location, it, m_identifiers, "get_array_length");
773 break;
774 }
775 case op_get_string_length: {
776 printGetByIdOp(location, it, m_identifiers, "get_string_length");
777 break;
778 }
779 case op_put_by_id: {
780 printPutByIdOp(location, it, m_identifiers, "put_by_id");
781 break;
782 }
783 case op_put_by_id_replace: {
784 printPutByIdOp(location, it, m_identifiers, "put_by_id_replace");
785 break;
786 }
787 case op_put_by_id_transition: {
788 printPutByIdOp(location, it, m_identifiers, "put_by_id_transition");
789 break;
790 }
791 case op_put_by_id_generic: {
792 printPutByIdOp(location, it, m_identifiers, "put_by_id_generic");
793 break;
794 }
795 case op_put_getter: {
796 int r0 = (++it)->u.operand;
797 int id0 = (++it)->u.operand;
798 int r1 = (++it)->u.operand;
799 printf("[%4d] put_getter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), registerName(r1).c_str());
800 break;
801 }
802 case op_put_setter: {
803 int r0 = (++it)->u.operand;
804 int id0 = (++it)->u.operand;
805 int r1 = (++it)->u.operand;
806 printf("[%4d] put_setter\t %s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), registerName(r1).c_str());
807 break;
808 }
809 case op_method_check: {
810 printf("[%4d] op_method_check\n", location);
811 break;
812 }
813 case op_del_by_id: {
814 int r0 = (++it)->u.operand;
815 int r1 = (++it)->u.operand;
816 int id0 = (++it)->u.operand;
817 printf("[%4d] del_by_id\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, m_identifiers[id0]).c_str());
818 break;
819 }
820 case op_get_by_val: {
821 int r0 = (++it)->u.operand;
822 int r1 = (++it)->u.operand;
823 int r2 = (++it)->u.operand;
824 printf("[%4d] get_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
825 break;
826 }
827 case op_put_by_val: {
828 int r0 = (++it)->u.operand;
829 int r1 = (++it)->u.operand;
830 int r2 = (++it)->u.operand;
831 printf("[%4d] put_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
832 break;
833 }
834 case op_del_by_val: {
835 int r0 = (++it)->u.operand;
836 int r1 = (++it)->u.operand;
837 int r2 = (++it)->u.operand;
838 printf("[%4d] del_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str());
839 break;
840 }
841 case op_put_by_index: {
842 int r0 = (++it)->u.operand;
843 unsigned n0 = (++it)->u.operand;
844 int r1 = (++it)->u.operand;
845 printf("[%4d] put_by_index\t %s, %u, %s\n", location, registerName(r0).c_str(), n0, registerName(r1).c_str());
846 break;
847 }
848 case op_jmp: {
849 int offset = (++it)->u.operand;
850 printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, location + offset);
851 break;
852 }
853 case op_loop: {
854 int offset = (++it)->u.operand;
855 printf("[%4d] loop\t\t %d(->%d)\n", location, offset, location + offset);
856 break;
857 }
858 case op_jtrue: {
859 printConditionalJump(begin, it, location, "jtrue");
860 break;
861 }
862 case op_loop_if_true: {
863 printConditionalJump(begin, it, location, "loop_if_true");
864 break;
865 }
866 case op_jfalse: {
867 printConditionalJump(begin, it, location, "jfalse");
868 break;
869 }
870 case op_jeq_null: {
871 printConditionalJump(begin, it, location, "jeq_null");
872 break;
873 }
874 case op_jneq_null: {
875 printConditionalJump(begin, it, location, "jneq_null");
876 break;
877 }
878 case op_jneq_ptr: {
879 int r0 = (++it)->u.operand;
880 int r1 = (++it)->u.operand;
881 int offset = (++it)->u.operand;
882 printf("[%4d] jneq_ptr\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, location + offset);
883 break;
884 }
885 case op_jnless: {
886 int r0 = (++it)->u.operand;
887 int r1 = (++it)->u.operand;
888 int offset = (++it)->u.operand;
889 printf("[%4d] jnless\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, location + offset);
890 break;
891 }
892 case op_jnlesseq: {
893 int r0 = (++it)->u.operand;
894 int r1 = (++it)->u.operand;
895 int offset = (++it)->u.operand;
896 printf("[%4d] jnlesseq\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, location + offset);
897 break;
898 }
899 case op_loop_if_less: {
900 int r0 = (++it)->u.operand;
901 int r1 = (++it)->u.operand;
902 int offset = (++it)->u.operand;
903 printf("[%4d] loop_if_less\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, location + offset);
904 break;
905 }
906 case op_loop_if_lesseq: {
907 int r0 = (++it)->u.operand;
908 int r1 = (++it)->u.operand;
909 int offset = (++it)->u.operand;
910 printf("[%4d] loop_if_lesseq\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, location + offset);
911 break;
912 }
913 case op_switch_imm: {
914 int tableIndex = (++it)->u.operand;
915 int defaultTarget = (++it)->u.operand;
916 int scrutineeRegister = (++it)->u.operand;
917 printf("[%4d] switch_imm\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).c_str());
918 break;
919 }
920 case op_switch_char: {
921 int tableIndex = (++it)->u.operand;
922 int defaultTarget = (++it)->u.operand;
923 int scrutineeRegister = (++it)->u.operand;
924 printf("[%4d] switch_char\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).c_str());
925 break;
926 }
927 case op_switch_string: {
928 int tableIndex = (++it)->u.operand;
929 int defaultTarget = (++it)->u.operand;
930 int scrutineeRegister = (++it)->u.operand;
931 printf("[%4d] switch_string\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).c_str());
932 break;
933 }
934 case op_new_func: {
935 int r0 = (++it)->u.operand;
936 int f0 = (++it)->u.operand;
937 printf("[%4d] new_func\t\t %s, f%d\n", location, registerName(r0).c_str(), f0);
938 break;
939 }
940 case op_new_func_exp: {
941 int r0 = (++it)->u.operand;
942 int f0 = (++it)->u.operand;
943 printf("[%4d] new_func_exp\t %s, f%d\n", location, registerName(r0).c_str(), f0);
944 break;
945 }
946 case op_call: {
947 int dst = (++it)->u.operand;
948 int func = (++it)->u.operand;
949 int argCount = (++it)->u.operand;
950 int registerOffset = (++it)->u.operand;
951 printf("[%4d] call\t\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset);
952 break;
953 }
954 case op_call_eval: {
955 int dst = (++it)->u.operand;
956 int func = (++it)->u.operand;
957 int argCount = (++it)->u.operand;
958 int registerOffset = (++it)->u.operand;
959 printf("[%4d] call_eval\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset);
960 break;
961 }
962 case op_call_varargs: {
963 int dst = (++it)->u.operand;
964 int func = (++it)->u.operand;
965 int argCount = (++it)->u.operand;
966 int registerOffset = (++it)->u.operand;
967 printf("[%4d] call_varargs\t %s, %s, %s, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), registerName(argCount).c_str(), registerOffset);
968 break;
969 }
970 case op_load_varargs: {
971 printUnaryOp(location, it, "load_varargs");
972 break;
973 }
974 case op_tear_off_activation: {
975 int r0 = (++it)->u.operand;
976 printf("[%4d] tear_off_activation\t %s\n", location, registerName(r0).c_str());
977 break;
978 }
979 case op_tear_off_arguments: {
980 printf("[%4d] tear_off_arguments\n", location);
981 break;
982 }
983 case op_ret: {
984 int r0 = (++it)->u.operand;
985 printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str());
986 break;
987 }
988 case op_construct: {
989 int dst = (++it)->u.operand;
990 int func = (++it)->u.operand;
991 int argCount = (++it)->u.operand;
992 int registerOffset = (++it)->u.operand;
993 int proto = (++it)->u.operand;
994 int thisRegister = (++it)->u.operand;
995 printf("[%4d] construct\t %s, %s, %d, %d, %s, %s\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset, registerName(proto).c_str(), registerName(thisRegister).c_str());
996 break;
997 }
998 case op_construct_verify: {
999 int r0 = (++it)->u.operand;
1000 int r1 = (++it)->u.operand;
1001 printf("[%4d] construct_verify\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
1002 break;
1003 }
1004 case op_strcat: {
1005 int r0 = (++it)->u.operand;
1006 int r1 = (++it)->u.operand;
1007 int count = (++it)->u.operand;
1008 printf("[%4d] op_strcat\t %s, %s, %d\n", location, registerName(r0).c_str(), registerName(r1).c_str(), count);
1009 break;
1010 }
1011 case op_to_primitive: {
1012 int r0 = (++it)->u.operand;
1013 int r1 = (++it)->u.operand;
1014 printf("[%4d] op_to_primitive\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
1015 break;
1016 }
1017 case op_get_pnames: {
1018 int r0 = it[0].u.operand;
1019 int r1 = it[1].u.operand;
1020 printf("[%4d] get_pnames\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str());
1021 it += OPCODE_LENGTH(op_get_pnames) - 1;
1022 break;
1023 }
1024 case op_next_pname: {
1025 int dest = it[0].u.operand;
1026 int iter = it[4].u.operand;
1027 int offset = it[5].u.operand;
1028 printf("[%4d] next_pname\t %s, %s, %d(->%d)\n", location, registerName(dest).c_str(), registerName(iter).c_str(), offset, location + offset);
1029 it += OPCODE_LENGTH(op_next_pname) - 1;
1030 break;
1031 }
1032 case op_push_scope: {
1033 int r0 = (++it)->u.operand;
1034 printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str());
1035 break;
1036 }
1037 case op_pop_scope: {
1038 printf("[%4d] pop_scope\n", location);
1039 break;
1040 }
1041 case op_push_new_scope: {
1042 int r0 = (++it)->u.operand;
1043 int id0 = (++it)->u.operand;
1044 int r1 = (++it)->u.operand;
1045 printf("[%4d] push_new_scope \t%s, %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), registerName(r1).c_str());
1046 break;
1047 }
1048 case op_jmp_scopes: {
1049 int scopeDelta = (++it)->u.operand;
1050 int offset = (++it)->u.operand;
1051 printf("[%4d] jmp_scopes\t^%d, %d(->%d)\n", location, scopeDelta, offset, location + offset);
1052 break;
1053 }
1054 case op_catch: {
1055 int r0 = (++it)->u.operand;
1056 printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str());
1057 break;
1058 }
1059 case op_throw: {
1060 int r0 = (++it)->u.operand;
1061 printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str());
1062 break;
1063 }
1064 case op_new_error: {
1065 int r0 = (++it)->u.operand;
1066 int errorType = (++it)->u.operand;
1067 int k0 = (++it)->u.operand;
1068 printf("[%4d] new_error\t %s, %d, %s\n", location, registerName(r0).c_str(), errorType, constantName(exec, k0, getConstant(k0)).c_str());
1069 break;
1070 }
1071 case op_jsr: {
1072 int retAddrDst = (++it)->u.operand;
1073 int offset = (++it)->u.operand;
1074 printf("[%4d] jsr\t\t %s, %d(->%d)\n", location, registerName(retAddrDst).c_str(), offset, location + offset);
1075 break;
1076 }
1077 case op_sret: {
1078 int retAddrSrc = (++it)->u.operand;
1079 printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str());
1080 break;
1081 }
1082 case op_debug: {
1083 int debugHookID = (++it)->u.operand;
1084 int firstLine = (++it)->u.operand;
1085 int lastLine = (++it)->u.operand;
1086 printf("[%4d] debug\t\t %s, %d, %d\n", location, debugHookName(debugHookID), firstLine, lastLine);
1087 break;
1088 }
1089 case op_profile_will_call: {
1090 int function = (++it)->u.operand;
1091 printf("[%4d] profile_will_call %s\n", location, registerName(function).c_str());
1092 break;
1093 }
1094 case op_profile_did_call: {
1095 int function = (++it)->u.operand;
1096 printf("[%4d] profile_did_call\t %s\n", location, registerName(function).c_str());
1097 break;
1098 }
1099 case op_end: {
1100 int r0 = (++it)->u.operand;
1101 printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str());
1102 break;
1103 }
1104 }
1105}
1106
1107#endif // !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
1108
1109#if DUMP_CODE_BLOCK_STATISTICS
1110static HashSet<CodeBlock*> liveCodeBlockSet;
1111#endif
1112
1113#define FOR_EACH_MEMBER_VECTOR(macro) \
1114 macro(instructions) \
1115 macro(globalResolveInfos) \
1116 macro(structureStubInfos) \
1117 macro(callLinkInfos) \
1118 macro(linkedCallerList) \
1119 macro(identifiers) \
1120 macro(functionExpressions) \
1121 macro(constantRegisters)
1122
1123#define FOR_EACH_MEMBER_VECTOR_RARE_DATA(macro) \
1124 macro(regexps) \
1125 macro(functions) \
1126 macro(exceptionHandlers) \
1127 macro(immediateSwitchJumpTables) \
1128 macro(characterSwitchJumpTables) \
1129 macro(stringSwitchJumpTables) \
1130 macro(functionRegisterInfos)
1131
1132#define FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(macro) \
1133 macro(expressionInfo) \
1134 macro(lineInfo) \
1135 macro(getByIdExceptionInfo) \
1136 macro(pcVector)
1137
1138template<typename T>
1139static size_t sizeInBytes(const Vector<T>& vector)
1140{
1141 return vector.capacity() * sizeof(T);
1142}
1143
1144void CodeBlock::dumpStatistics()
1145{
1146#if DUMP_CODE_BLOCK_STATISTICS
1147 #define DEFINE_VARS(name) size_t name##IsNotEmpty = 0; size_t name##TotalSize = 0;
1148 FOR_EACH_MEMBER_VECTOR(DEFINE_VARS)
1149 FOR_EACH_MEMBER_VECTOR_RARE_DATA(DEFINE_VARS)
1150 FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(DEFINE_VARS)
1151 #undef DEFINE_VARS
1152
1153 // Non-vector data members
1154 size_t evalCodeCacheIsNotEmpty = 0;
1155
1156 size_t symbolTableIsNotEmpty = 0;
1157 size_t symbolTableTotalSize = 0;
1158
1159 size_t hasExceptionInfo = 0;
1160 size_t hasRareData = 0;
1161
1162 size_t isFunctionCode = 0;
1163 size_t isGlobalCode = 0;
1164 size_t isEvalCode = 0;
1165
1166 HashSet<CodeBlock*>::const_iterator end = liveCodeBlockSet.end();
1167 for (HashSet<CodeBlock*>::const_iterator it = liveCodeBlockSet.begin(); it != end; ++it) {
1168 CodeBlock* codeBlock = *it;
1169
1170 #define GET_STATS(name) if (!codeBlock->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_##name); }
1171 FOR_EACH_MEMBER_VECTOR(GET_STATS)
1172 #undef GET_STATS
1173
1174 if (!codeBlock->m_symbolTable.isEmpty()) {
1175 symbolTableIsNotEmpty++;
1176 symbolTableTotalSize += (codeBlock->m_symbolTable.capacity() * (sizeof(SymbolTable::KeyType) + sizeof(SymbolTable::MappedType)));
1177 }
1178
1179 if (codeBlock->m_exceptionInfo) {
1180 hasExceptionInfo++;
1181 #define GET_STATS(name) if (!codeBlock->m_exceptionInfo->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_exceptionInfo->m_##name); }
1182 FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(GET_STATS)
1183 #undef GET_STATS
1184 }
1185
1186 if (codeBlock->m_rareData) {
1187 hasRareData++;
1188 #define GET_STATS(name) if (!codeBlock->m_rareData->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_rareData->m_##name); }
1189 FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_STATS)
1190 #undef GET_STATS
1191
1192 if (!codeBlock->m_rareData->m_evalCodeCache.isEmpty())
1193 evalCodeCacheIsNotEmpty++;
1194 }
1195
1196 switch (codeBlock->codeType()) {
1197 case FunctionCode:
1198 ++isFunctionCode;
1199 break;
1200 case GlobalCode:
1201 ++isGlobalCode;
1202 break;
1203 case EvalCode:
1204 ++isEvalCode;
1205 break;
1206 }
1207 }
1208
1209 size_t totalSize = 0;
1210
1211 #define GET_TOTAL_SIZE(name) totalSize += name##TotalSize;
1212 FOR_EACH_MEMBER_VECTOR(GET_TOTAL_SIZE)
1213 FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_TOTAL_SIZE)
1214 FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(GET_TOTAL_SIZE)
1215 #undef GET_TOTAL_SIZE
1216
1217 totalSize += symbolTableTotalSize;
1218 totalSize += (liveCodeBlockSet.size() * sizeof(CodeBlock));
1219
1220 printf("Number of live CodeBlocks: %d\n", liveCodeBlockSet.size());
1221 printf("Size of a single CodeBlock [sizeof(CodeBlock)]: %zu\n", sizeof(CodeBlock));
1222 printf("Size of all CodeBlocks: %zu\n", totalSize);
1223 printf("Average size of a CodeBlock: %zu\n", totalSize / liveCodeBlockSet.size());
1224
1225 printf("Number of FunctionCode CodeBlocks: %zu (%.3f%%)\n", isFunctionCode, static_cast<double>(isFunctionCode) * 100.0 / liveCodeBlockSet.size());
1226 printf("Number of GlobalCode CodeBlocks: %zu (%.3f%%)\n", isGlobalCode, static_cast<double>(isGlobalCode) * 100.0 / liveCodeBlockSet.size());
1227 printf("Number of EvalCode CodeBlocks: %zu (%.3f%%)\n", isEvalCode, static_cast<double>(isEvalCode) * 100.0 / liveCodeBlockSet.size());
1228
1229 printf("Number of CodeBlocks with exception info: %zu (%.3f%%)\n", hasExceptionInfo, static_cast<double>(hasExceptionInfo) * 100.0 / liveCodeBlockSet.size());
1230 printf("Number of CodeBlocks with rare data: %zu (%.3f%%)\n", hasRareData, static_cast<double>(hasRareData) * 100.0 / liveCodeBlockSet.size());
1231
1232 #define PRINT_STATS(name) printf("Number of CodeBlocks with " #name ": %zu\n", name##IsNotEmpty); printf("Size of all " #name ": %zu\n", name##TotalSize);
1233 FOR_EACH_MEMBER_VECTOR(PRINT_STATS)
1234 FOR_EACH_MEMBER_VECTOR_RARE_DATA(PRINT_STATS)
1235 FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(PRINT_STATS)
1236 #undef PRINT_STATS
1237
1238 printf("Number of CodeBlocks with evalCodeCache: %zu\n", evalCodeCacheIsNotEmpty);
1239 printf("Number of CodeBlocks with symbolTable: %zu\n", symbolTableIsNotEmpty);
1240
1241 printf("Size of all symbolTables: %zu\n", symbolTableTotalSize);
1242
1243#else
1244 printf("Dumping CodeBlock statistics is not enabled.\n");
1245#endif
1246}
1247
1248CodeBlock::CodeBlock(ScriptExecutable* ownerExecutable, CodeType codeType, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, SymbolTable* symTab)
1249 : m_numCalleeRegisters(0)
1250 , m_numVars(0)
1251 , m_numParameters(0)
1252 , m_ownerExecutable(ownerExecutable)
1253 , m_globalData(0)
1254#ifndef NDEBUG
1255 , m_instructionCount(0)
1256#endif
1257 , m_needsFullScopeChain(ownerExecutable->needsActivation())
1258 , m_usesEval(ownerExecutable->usesEval())
1259 , m_isNumericCompareFunction(false)
1260 , m_codeType(codeType)
1261 , m_source(sourceProvider)
1262 , m_sourceOffset(sourceOffset)
1263 , m_symbolTable(symTab)
1264 , m_exceptionInfo(new ExceptionInfo)
1265{
1266 ASSERT(m_source);
1267
1268#if DUMP_CODE_BLOCK_STATISTICS
1269 liveCodeBlockSet.add(this);
1270#endif
1271}
1272
1273CodeBlock::~CodeBlock()
1274{
1275#if !ENABLE(JIT)
1276 for (size_t size = m_globalResolveInstructions.size(), i = 0; i < size; ++i)
1277 derefStructures(&m_instructions[m_globalResolveInstructions[i]]);
1278
1279 for (size_t size = m_propertyAccessInstructions.size(), i = 0; i < size; ++i)
1280 derefStructures(&m_instructions[m_propertyAccessInstructions[i]]);
1281#else
1282 for (size_t size = m_globalResolveInfos.size(), i = 0; i < size; ++i) {
1283 if (m_globalResolveInfos[i].structure)
1284 m_globalResolveInfos[i].structure->deref();
1285 }
1286
1287 for (size_t size = m_structureStubInfos.size(), i = 0; i < size; ++i)
1288 m_structureStubInfos[i].deref();
1289
1290 for (size_t size = m_callLinkInfos.size(), i = 0; i < size; ++i) {
1291 CallLinkInfo* callLinkInfo = &m_callLinkInfos[i];
1292 if (callLinkInfo->isLinked())
1293 callLinkInfo->callee->removeCaller(callLinkInfo);
1294 }
1295
1296 for (size_t size = m_methodCallLinkInfos.size(), i = 0; i < size; ++i) {
1297 if (Structure* structure = m_methodCallLinkInfos[i].cachedStructure) {
1298 structure->deref();
1299 // Both members must be filled at the same time
1300 ASSERT(!!m_methodCallLinkInfos[i].cachedPrototypeStructure);
1301 m_methodCallLinkInfos[i].cachedPrototypeStructure->deref();
1302 }
1303 }
1304
1305#if ENABLE(JIT_OPTIMIZE_CALL)
1306 unlinkCallers();
1307#endif
1308
1309#endif // !ENABLE(JIT)
1310
1311#if DUMP_CODE_BLOCK_STATISTICS
1312 liveCodeBlockSet.remove(this);
1313#endif
1314}
1315
1316#if ENABLE(JIT_OPTIMIZE_CALL)
1317void CodeBlock::unlinkCallers()
1318{
1319 size_t size = m_linkedCallerList.size();
1320 for (size_t i = 0; i < size; ++i) {
1321 CallLinkInfo* currentCaller = m_linkedCallerList[i];
1322 JIT::unlinkCall(currentCaller);
1323 currentCaller->setUnlinked();
1324 }
1325 m_linkedCallerList.clear();
1326}
1327#endif
1328
1329void CodeBlock::derefStructures(Instruction* vPC) const
1330{
1331 Interpreter* interpreter = m_globalData->interpreter;
1332
1333 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
1334 vPC[4].u.structure->deref();
1335 return;
1336 }
1337 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
1338 vPC[4].u.structure->deref();
1339 vPC[5].u.structure->deref();
1340 return;
1341 }
1342 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
1343 vPC[4].u.structure->deref();
1344 vPC[5].u.structureChain->deref();
1345 return;
1346 }
1347 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
1348 vPC[4].u.structure->deref();
1349 vPC[5].u.structure->deref();
1350 vPC[6].u.structureChain->deref();
1351 return;
1352 }
1353 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
1354 vPC[4].u.structure->deref();
1355 return;
1356 }
1357 if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) {
1358 if(vPC[4].u.structure)
1359 vPC[4].u.structure->deref();
1360 return;
1361 }
1362 if ((vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto_list))
1363 || (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self_list))) {
1364 PolymorphicAccessStructureList* polymorphicStructures = vPC[4].u.polymorphicStructures;
1365 polymorphicStructures->derefStructures(vPC[5].u.operand);
1366 delete polymorphicStructures;
1367 return;
1368 }
1369
1370 // These instructions don't ref their Structures.
1371 ASSERT(vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_get_array_length) || vPC[0].u.opcode == interpreter->getOpcode(op_get_string_length));
1372}
1373
1374void CodeBlock::refStructures(Instruction* vPC) const
1375{
1376 Interpreter* interpreter = m_globalData->interpreter;
1377
1378 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) {
1379 vPC[4].u.structure->ref();
1380 return;
1381 }
1382 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) {
1383 vPC[4].u.structure->ref();
1384 vPC[5].u.structure->ref();
1385 return;
1386 }
1387 if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) {
1388 vPC[4].u.structure->ref();
1389 vPC[5].u.structureChain->ref();
1390 return;
1391 }
1392 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) {
1393 vPC[4].u.structure->ref();
1394 vPC[5].u.structure->ref();
1395 vPC[6].u.structureChain->ref();
1396 return;
1397 }
1398 if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) {
1399 vPC[4].u.structure->ref();
1400 return;
1401 }
1402
1403 // These instructions don't ref their Structures.
1404 ASSERT(vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id) || vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_generic) || vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_generic));
1405}
1406
1407void CodeBlock::markAggregate(MarkStack& markStack)
1408{
1409 for (size_t i = 0; i < m_constantRegisters.size(); ++i)
1410 markStack.append(m_constantRegisters[i].jsValue());
1411 for (size_t i = 0; i < m_functionExprs.size(); ++i)
1412 m_functionExprs[i]->markAggregate(markStack);
1413 for (size_t i = 0; i < m_functionDecls.size(); ++i)
1414 m_functionDecls[i]->markAggregate(markStack);
1415}
1416
1417void CodeBlock::reparseForExceptionInfoIfNecessary(CallFrame* callFrame)
1418{
1419 if (m_exceptionInfo)
1420 return;
1421
1422 ScopeChainNode* scopeChain = callFrame->scopeChain();
1423 if (m_needsFullScopeChain) {
1424 ScopeChain sc(scopeChain);
1425 int scopeDelta = sc.localDepth();
1426 if (m_codeType == EvalCode)
1427 scopeDelta -= static_cast<EvalCodeBlock*>(this)->baseScopeDepth();
1428 else if (m_codeType == FunctionCode)
1429 scopeDelta++; // Compilation of function code assumes activation is not on the scope chain yet.
1430 ASSERT(scopeDelta >= 0);
1431 while (scopeDelta--)
1432 scopeChain = scopeChain->next;
1433 }
1434
1435 m_exceptionInfo.set(m_ownerExecutable->reparseExceptionInfo(m_globalData, scopeChain, this));
1436}
1437
1438HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset)
1439{
1440 ASSERT(bytecodeOffset < m_instructionCount);
1441
1442 if (!m_rareData)
1443 return 0;
1444
1445 Vector<HandlerInfo>& exceptionHandlers = m_rareData->m_exceptionHandlers;
1446 for (size_t i = 0; i < exceptionHandlers.size(); ++i) {
1447 // Handlers are ordered innermost first, so the first handler we encounter
1448 // that contains the source address is the correct handler to use.
1449 if (exceptionHandlers[i].start <= bytecodeOffset && exceptionHandlers[i].end >= bytecodeOffset)
1450 return &exceptionHandlers[i];
1451 }
1452
1453 return 0;
1454}
1455
1456int CodeBlock::lineNumberForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset)
1457{
1458 ASSERT(bytecodeOffset < m_instructionCount);
1459
1460 reparseForExceptionInfoIfNecessary(callFrame);
1461 ASSERT(m_exceptionInfo);
1462
1463 if (!m_exceptionInfo->m_lineInfo.size())
1464 return m_ownerExecutable->source().firstLine(); // Empty function
1465
1466 int low = 0;
1467 int high = m_exceptionInfo->m_lineInfo.size();
1468 while (low < high) {
1469 int mid = low + (high - low) / 2;
1470 if (m_exceptionInfo->m_lineInfo[mid].instructionOffset <= bytecodeOffset)
1471 low = mid + 1;
1472 else
1473 high = mid;
1474 }
1475
1476 if (!low)
1477 return m_ownerExecutable->source().firstLine();
1478 return m_exceptionInfo->m_lineInfo[low - 1].lineNumber;
1479}
1480
1481int CodeBlock::expressionRangeForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset)
1482{
1483 ASSERT(bytecodeOffset < m_instructionCount);
1484
1485 reparseForExceptionInfoIfNecessary(callFrame);
1486 ASSERT(m_exceptionInfo);
1487
1488 if (!m_exceptionInfo->m_expressionInfo.size()) {
1489 // We didn't think anything could throw. Apparently we were wrong.
1490 startOffset = 0;
1491 endOffset = 0;
1492 divot = 0;
1493 return lineNumberForBytecodeOffset(callFrame, bytecodeOffset);
1494 }
1495
1496 int low = 0;
1497 int high = m_exceptionInfo->m_expressionInfo.size();
1498 while (low < high) {
1499 int mid = low + (high - low) / 2;
1500 if (m_exceptionInfo->m_expressionInfo[mid].instructionOffset <= bytecodeOffset)
1501 low = mid + 1;
1502 else
1503 high = mid;
1504 }
1505
1506 ASSERT(low);
1507 if (!low) {
1508 startOffset = 0;
1509 endOffset = 0;
1510 divot = 0;
1511 return lineNumberForBytecodeOffset(callFrame, bytecodeOffset);
1512 }
1513
1514 startOffset = m_exceptionInfo->m_expressionInfo[low - 1].startOffset;
1515 endOffset = m_exceptionInfo->m_expressionInfo[low - 1].endOffset;
1516 divot = m_exceptionInfo->m_expressionInfo[low - 1].divotPoint + m_sourceOffset;
1517 return lineNumberForBytecodeOffset(callFrame, bytecodeOffset);
1518}
1519
1520bool CodeBlock::getByIdExceptionInfoForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset, OpcodeID& opcodeID)
1521{
1522 ASSERT(bytecodeOffset < m_instructionCount);
1523
1524 reparseForExceptionInfoIfNecessary(callFrame);
1525 ASSERT(m_exceptionInfo);
1526
1527 if (!m_exceptionInfo->m_getByIdExceptionInfo.size())
1528 return false;
1529
1530 int low = 0;
1531 int high = m_exceptionInfo->m_getByIdExceptionInfo.size();
1532 while (low < high) {
1533 int mid = low + (high - low) / 2;
1534 if (m_exceptionInfo->m_getByIdExceptionInfo[mid].bytecodeOffset <= bytecodeOffset)
1535 low = mid + 1;
1536 else
1537 high = mid;
1538 }
1539
1540 if (!low || m_exceptionInfo->m_getByIdExceptionInfo[low - 1].bytecodeOffset != bytecodeOffset)
1541 return false;
1542
1543 opcodeID = m_exceptionInfo->m_getByIdExceptionInfo[low - 1].isOpConstruct ? op_construct : op_instanceof;
1544 return true;
1545}
1546
1547#if ENABLE(JIT)
1548bool CodeBlock::functionRegisterForBytecodeOffset(unsigned bytecodeOffset, int& functionRegisterIndex)
1549{
1550 ASSERT(bytecodeOffset < m_instructionCount);
1551
1552 if (!m_rareData || !m_rareData->m_functionRegisterInfos.size())
1553 return false;
1554
1555 int low = 0;
1556 int high = m_rareData->m_functionRegisterInfos.size();
1557 while (low < high) {
1558 int mid = low + (high - low) / 2;
1559 if (m_rareData->m_functionRegisterInfos[mid].bytecodeOffset <= bytecodeOffset)
1560 low = mid + 1;
1561 else
1562 high = mid;
1563 }
1564
1565 if (!low || m_rareData->m_functionRegisterInfos[low - 1].bytecodeOffset != bytecodeOffset)
1566 return false;
1567
1568 functionRegisterIndex = m_rareData->m_functionRegisterInfos[low - 1].functionRegisterIndex;
1569 return true;
1570}
1571#endif
1572
1573#if !ENABLE(JIT)
1574bool CodeBlock::hasGlobalResolveInstructionAtBytecodeOffset(unsigned bytecodeOffset)
1575{
1576 if (m_globalResolveInstructions.isEmpty())
1577 return false;
1578
1579 int low = 0;
1580 int high = m_globalResolveInstructions.size();
1581 while (low < high) {
1582 int mid = low + (high - low) / 2;
1583 if (m_globalResolveInstructions[mid] <= bytecodeOffset)
1584 low = mid + 1;
1585 else
1586 high = mid;
1587 }
1588
1589 if (!low || m_globalResolveInstructions[low - 1] != bytecodeOffset)
1590 return false;
1591 return true;
1592}
1593#else
1594bool CodeBlock::hasGlobalResolveInfoAtBytecodeOffset(unsigned bytecodeOffset)
1595{
1596 if (m_globalResolveInfos.isEmpty())
1597 return false;
1598
1599 int low = 0;
1600 int high = m_globalResolveInfos.size();
1601 while (low < high) {
1602 int mid = low + (high - low) / 2;
1603 if (m_globalResolveInfos[mid].bytecodeOffset <= bytecodeOffset)
1604 low = mid + 1;
1605 else
1606 high = mid;
1607 }
1608
1609 if (!low || m_globalResolveInfos[low - 1].bytecodeOffset != bytecodeOffset)
1610 return false;
1611 return true;
1612}
1613#endif
1614
1615void CodeBlock::shrinkToFit()
1616{
1617 m_instructions.shrinkToFit();
1618
1619#if !ENABLE(JIT)
1620 m_propertyAccessInstructions.shrinkToFit();
1621 m_globalResolveInstructions.shrinkToFit();
1622#else
1623 m_structureStubInfos.shrinkToFit();
1624 m_globalResolveInfos.shrinkToFit();
1625 m_callLinkInfos.shrinkToFit();
1626 m_linkedCallerList.shrinkToFit();
1627#endif
1628
1629 m_identifiers.shrinkToFit();
1630 m_functionDecls.shrinkToFit();
1631 m_functionExprs.shrinkToFit();
1632 m_constantRegisters.shrinkToFit();
1633
1634 if (m_exceptionInfo) {
1635 m_exceptionInfo->m_expressionInfo.shrinkToFit();
1636 m_exceptionInfo->m_lineInfo.shrinkToFit();
1637 m_exceptionInfo->m_getByIdExceptionInfo.shrinkToFit();
1638 }
1639
1640 if (m_rareData) {
1641 m_rareData->m_exceptionHandlers.shrinkToFit();
1642 m_rareData->m_regexps.shrinkToFit();
1643 m_rareData->m_immediateSwitchJumpTables.shrinkToFit();
1644 m_rareData->m_characterSwitchJumpTables.shrinkToFit();
1645 m_rareData->m_stringSwitchJumpTables.shrinkToFit();
1646#if ENABLE(JIT)
1647 m_rareData->m_functionRegisterInfos.shrinkToFit();
1648#endif
1649 }
1650}
1651
1652} // namespace JSC
Note: See TracBrowser for help on using the repository browser.