X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..81345200c95645a1b0d2635520f96ad55dfde63f:/bytecode/CodeBlock.cpp?ds=inline diff --git a/bytecode/CodeBlock.cpp b/bytecode/CodeBlock.cpp index be060d0..da60b48 100644 --- a/bytecode/CodeBlock.cpp +++ b/bytecode/CodeBlock.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. + * Copyright (C) 2008, 2009, 2010, 2012, 2013, 2014 Apple Inc. All rights reserved. * Copyright (C) 2008 Cameron Zwarich * * Redistribution and use in source and binary forms, with or without @@ -11,7 +11,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -30,83 +30,185 @@ #include "config.h" #include "CodeBlock.h" -#include "JIT.h" -#include "JSValue.h" -#include "Interpreter.h" -#include "Debugger.h" #include "BytecodeGenerator.h" -#include +#include "BytecodeUseDef.h" +#include "CallLinkStatus.h" +#include "DFGCapabilities.h" +#include "DFGCommon.h" +#include "DFGDriver.h" +#include "DFGJITCode.h" +#include "DFGWorklist.h" +#include "Debugger.h" +#include "Interpreter.h" +#include "JIT.h" +#include "JITStubs.h" +#include "JSActivation.h" +#include "JSCJSValue.h" +#include "JSFunction.h" +#include "JSNameScope.h" +#include "LLIntEntrypoint.h" +#include "LowLevelInterpreter.h" +#include "JSCInlines.h" +#include "PolymorphicGetByIdList.h" +#include "PolymorphicPutByIdList.h" +#include "ProfilerDatabase.h" +#include "ReduceWhitespace.h" +#include "Repatch.h" +#include "RepatchBuffer.h" +#include "SlotVisitorInlines.h" +#include "UnlinkedInstructionStream.h" +#include +#include #include +#include + +#if ENABLE(DFG_JIT) +#include "DFGOperations.h" +#endif -#define DUMP_CODE_BLOCK_STATISTICS 0 +#if ENABLE(FTL_JIT) +#include "FTLJITCode.h" +#endif namespace JSC { -#if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING) +CString CodeBlock::inferredName() const +{ + switch (codeType()) { + case GlobalCode: + return ""; + case EvalCode: + return ""; + case FunctionCode: + return jsCast(ownerExecutable())->inferredName().utf8(); + default: + CRASH(); + return CString("", 0); + } +} + +bool CodeBlock::hasHash() const +{ + return !!m_hash; +} + +bool CodeBlock::isSafeToComputeHash() const +{ + return !isCompilationThread(); +} -static UString escapeQuotes(const UString& str) +CodeBlockHash CodeBlock::hash() const { - UString result = str; - int pos = 0; - while ((pos = result.find('\"', pos)) >= 0) { - result = result.substr(0, pos) + "\"\\\"\"" + result.substr(pos + 1); - pos += 4; + if (!m_hash) { + RELEASE_ASSERT(isSafeToComputeHash()); + m_hash = CodeBlockHash(ownerExecutable()->source(), specializationKind()); } - return result; + return m_hash; } -static UString valueToSourceString(ExecState* exec, JSValuePtr val) +CString CodeBlock::sourceCodeForTools() const { - if (val.isString()) { - UString result("\""); - result += escapeQuotes(val.toString(exec)) + "\""; - return result; - } + if (codeType() != FunctionCode) + return ownerExecutable()->source().toUTF8(); + + SourceProvider* provider = source(); + FunctionExecutable* executable = jsCast(ownerExecutable()); + UnlinkedFunctionExecutable* unlinked = executable->unlinkedExecutable(); + unsigned unlinkedStartOffset = unlinked->startOffset(); + unsigned linkedStartOffset = executable->source().startOffset(); + int delta = linkedStartOffset - unlinkedStartOffset; + unsigned rangeStart = delta + unlinked->unlinkedFunctionNameStart(); + unsigned rangeEnd = delta + unlinked->startOffset() + unlinked->sourceLength(); + return toCString( + "function ", + provider->source().impl()->utf8ForRange(rangeStart, rangeEnd - rangeStart)); +} + +CString CodeBlock::sourceCodeOnOneLine() const +{ + return reduceWhitespace(sourceCodeForTools()); +} - return val.toString(exec); +CString CodeBlock::hashAsStringIfPossible() const +{ + if (hasHash() || isSafeToComputeHash()) + return toCString(hash()); + return ""; } -static CString registerName(int r) +void CodeBlock::dumpAssumingJITType(PrintStream& out, JITCode::JITType jitType) const { - if (r == missingThisObjectMarker()) - return ""; + out.print(inferredName(), "#", hashAsStringIfPossible()); + out.print(":[", RawPointer(this), "->"); + if (!!m_alternative) + out.print(RawPointer(m_alternative.get()), "->"); + out.print(RawPointer(ownerExecutable()), ", ", jitType, codeType()); + + if (codeType() == FunctionCode) + out.print(specializationKind()); + out.print(", ", instructionCount()); + if (this->jitType() == JITCode::BaselineJIT && m_shouldAlwaysBeInlined) + out.print(" (SABI)"); + if (ownerExecutable()->neverInline()) + out.print(" (NeverInline)"); + if (ownerExecutable()->isStrictMode()) + out.print(" (StrictMode)"); + if (this->jitType() == JITCode::BaselineJIT && m_didFailFTLCompilation) + out.print(" (FTLFail)"); + if (this->jitType() == JITCode::BaselineJIT && m_hasBeenCompiledWithFTL) + out.print(" (HadFTLReplacement)"); + out.print("]"); +} - return (UString("r") + UString::from(r)).UTF8String(); +void CodeBlock::dump(PrintStream& out) const +{ + dumpAssumingJITType(out, jitType()); } -static CString constantName(ExecState* exec, int k, JSValuePtr value) +static CString constantName(int k, JSValue value) { - return (valueToSourceString(exec, value) + "(@k" + UString::from(k) + ")").UTF8String(); + return toCString(value, "(@k", k - FirstConstantRegisterIndex, ")"); } static CString idName(int id0, const Identifier& ident) { - return (ident.ustring() + "(@id" + UString::from(id0) +")").UTF8String(); + return toCString(ident.impl(), "(@id", id0, ")"); +} + +CString CodeBlock::registerName(int r) const +{ + if (r == missingThisObjectMarker()) + return ""; + + if (isConstantRegisterIndex(r)) + return constantName(r, getConstant(r)); + + if (operandIsArgument(r)) { + if (!VirtualRegister(r).toArgument()) + return "this"; + return toCString("arg", VirtualRegister(r).toArgument()); + } + + return toCString("loc", VirtualRegister(r).toLocal()); } -static UString regexpToSourceString(RegExp* regExp) +static CString regexpToSourceString(RegExp* regExp) { - UString pattern = UString("/") + regExp->pattern() + "/"; + char postfix[5] = { '/', 0, 0, 0, 0 }; + int index = 1; if (regExp->global()) - pattern += "g"; + postfix[index++] = 'g'; if (regExp->ignoreCase()) - pattern += "i"; + postfix[index++] = 'i'; if (regExp->multiline()) - pattern += "m"; + postfix[index] = 'm'; - return pattern; + return toCString("/", regExp->pattern().impl(), postfix); } static CString regexpName(int re, RegExp* regexp) { - return (regexpToSourceString(regexp) + "(@re" + UString::from(re) + ")").UTF8String(); -} - -static UString pointerToSourceString(void* p) -{ - char buffer[2 + 2 * sizeof(void*) + 1]; // 0x [two characters per byte] \0 - snprintf(buffer, sizeof(buffer), "%p", p); - return buffer; + return toCString(regexpToSourceString(regexp), "(@re", re, ")"); } NEVER_INLINE static const char* debugHookName(int debugHookID) @@ -126,966 +228,1171 @@ NEVER_INLINE static const char* debugHookName(int debugHookID) return "didReachBreakpoint"; } - ASSERT_NOT_REACHED(); + RELEASE_ASSERT_NOT_REACHED(); return ""; } -static int locationForOffset(const Vector::const_iterator& begin, Vector::const_iterator& it, int offset) -{ - return it - begin + offset; -} - -static void printUnaryOp(int location, Vector::const_iterator& it, const char* op) +void CodeBlock::printUnaryOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op) { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; - printf("[%4d] %s\t\t %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str()); + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %s", registerName(r0).data(), registerName(r1).data()); } -static void printBinaryOp(int location, Vector::const_iterator& it, const char* op) +void CodeBlock::printBinaryOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op) { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int r2 = (++it)->u.operand; - printf("[%4d] %s\t\t %s, %s, %s\n", location, op, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str()); + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); } -static void printConditionalJump(const Vector::const_iterator& begin, Vector::const_iterator& it, int location, const char* op) +void CodeBlock::printConditionalJump(PrintStream& out, ExecState* exec, const Instruction*, const Instruction*& it, int location, const char* op) { int r0 = (++it)->u.operand; int offset = (++it)->u.operand; - printf("[%4d] %s\t\t %s, %d(->%d)\n", location, op, registerName(r0).c_str(), offset, locationForOffset(begin, it, offset)); + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %d(->%d)", registerName(r0).data(), offset, location + offset); } -static void printGetByIdOp(int location, Vector::const_iterator& it, const Vector& m_identifiers, const char* op) +void CodeBlock::printGetByIdOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it) { + const char* op; + switch (exec->interpreter()->getOpcodeID(it->u.opcode)) { + case op_get_by_id: + op = "get_by_id"; + break; + case op_get_by_id_out_of_line: + op = "get_by_id_out_of_line"; + break; + case op_get_array_length: + op = "array_length"; + break; + default: + RELEASE_ASSERT_NOT_REACHED(); + op = 0; + } int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int id0 = (++it)->u.operand; - 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()); - it += 4; + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), idName(id0, identifier(id0)).data()); + it += 4; // Increment up to the value profiler. } -static void printPutByIdOp(int location, Vector::const_iterator& it, const Vector& m_identifiers, const char* op) +static void dumpStructure(PrintStream& out, const char* name, ExecState* exec, Structure* structure, const Identifier& ident) { - int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; - int r1 = (++it)->u.operand; - 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()); - it += 4; + if (!structure) + return; + + out.printf("%s = %p", name, structure); + + PropertyOffset offset = structure->getConcurrently(exec->vm(), ident.impl()); + if (offset != invalidOffset) + out.printf(" (offset = %d)", offset); } -#if ENABLE(JIT) -static bool isGlobalResolve(OpcodeID opcodeID) +#if ENABLE(JIT) // unused when not ENABLE(JIT), leading to silly warnings +static void dumpChain(PrintStream& out, ExecState* exec, StructureChain* chain, const Identifier& ident) { - return opcodeID == op_resolve_global; + out.printf("chain = %p: [", chain); + bool first = true; + for (WriteBarrier* currentStructure = chain->head(); + *currentStructure; + ++currentStructure) { + if (first) + first = false; + else + out.printf(", "); + dumpStructure(out, "struct", exec, currentStructure->get(), ident); + } + out.printf("]"); } +#endif -static bool isPropertyAccess(OpcodeID opcodeID) +void CodeBlock::printGetByIdCacheStatus(PrintStream& out, ExecState* exec, int location, const StubInfoMap& map) { - switch (opcodeID) { - case op_get_by_id_self: - case op_get_by_id_proto: - case op_get_by_id_chain: - case op_get_by_id_self_list: - case op_get_by_id_proto_list: - case op_put_by_id_transition: - case op_put_by_id_replace: - case op_get_by_id: - case op_put_by_id: - case op_get_by_id_generic: - case op_put_by_id_generic: - case op_get_array_length: - case op_get_string_length: - return true; - default: - return false; + Instruction* instruction = instructions().begin() + location; + + const Identifier& ident = identifier(instruction[3].u.operand); + + UNUSED_PARAM(ident); // tell the compiler to shut up in certain platform configurations. + + if (exec->interpreter()->getOpcodeID(instruction[0].u.opcode) == op_get_array_length) + out.printf(" llint(array_length)"); + else if (Structure* structure = instruction[4].u.structure.get()) { + out.printf(" llint("); + dumpStructure(out, "struct", exec, structure, ident); + out.printf(")"); } -} -static unsigned instructionOffsetForNth(ExecState* exec, const Vector& instructions, int nth, bool (*predicate)(OpcodeID)) -{ - size_t i = 0; - while (i < instructions.size()) { - OpcodeID currentOpcode = exec->interpreter()->getOpcodeID(instructions[i].u.opcode); - if (predicate(currentOpcode)) { - if (!--nth) - return i; +#if ENABLE(JIT) + if (StructureStubInfo* stubPtr = map.get(CodeOrigin(location))) { + StructureStubInfo& stubInfo = *stubPtr; + if (stubInfo.resetByGC) + out.print(" (Reset By GC)"); + + if (stubInfo.seen) { + out.printf(" jit("); + + Structure* baseStructure = 0; + Structure* prototypeStructure = 0; + StructureChain* chain = 0; + PolymorphicGetByIdList* list = 0; + + switch (stubInfo.accessType) { + case access_get_by_id_self: + out.printf("self"); + baseStructure = stubInfo.u.getByIdSelf.baseObjectStructure.get(); + break; + case access_get_by_id_chain: + out.printf("chain"); + baseStructure = stubInfo.u.getByIdChain.baseObjectStructure.get(); + chain = stubInfo.u.getByIdChain.chain.get(); + break; + case access_get_by_id_list: + out.printf("list"); + list = stubInfo.u.getByIdList.list; + break; + case access_unset: + out.printf("unset"); + break; + default: + RELEASE_ASSERT_NOT_REACHED(); + break; + } + + if (baseStructure) { + out.printf(", "); + dumpStructure(out, "struct", exec, baseStructure, ident); + } + + if (prototypeStructure) { + out.printf(", "); + dumpStructure(out, "prototypeStruct", exec, baseStructure, ident); + } + + if (chain) { + out.printf(", "); + dumpChain(out, exec, chain, ident); + } + + if (list) { + out.printf(", list = %p: [", list); + for (unsigned i = 0; i < list->size(); ++i) { + if (i) + out.printf(", "); + out.printf("("); + dumpStructure(out, "base", exec, list->at(i).structure(), ident); + if (list->at(i).chain()) { + out.printf(", "); + dumpChain(out, exec, list->at(i).chain(), ident); + } + out.printf(")"); + } + out.printf("]"); + } + out.printf(")"); } - i += opcodeLengths[currentOpcode]; } - - ASSERT_NOT_REACHED(); - return 0; -} - -static void printGlobalResolveInfo(const GlobalResolveInfo& resolveInfo, unsigned instructionOffset) -{ - printf(" [%4d] %s: %s\n", instructionOffset, "resolve_global", pointerToSourceString(resolveInfo.structure).UTF8String().c_str()); +#else + UNUSED_PARAM(map); +#endif } -static void printStructureStubInfo(const StructureStubInfo& stubInfo, unsigned instructionOffset) +void CodeBlock::printCallOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op, CacheDumpMode cacheDumpMode, bool& hasPrintedProfiling, const CallLinkInfoMap& map) { - switch (stubInfo.opcodeID) { - case op_get_by_id_self: - printf(" [%4d] %s: %s\n", instructionOffset, "get_by_id_self", pointerToSourceString(stubInfo.u.getByIdSelf.baseObjectStructure).UTF8String().c_str()); - return; - case op_get_by_id_proto: - 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()); - return; - case op_get_by_id_chain: - 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()); - return; - case op_get_by_id_self_list: - 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); - return; - case op_get_by_id_proto_list: - 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); - return; - case op_put_by_id_transition: - 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()); - return; - case op_put_by_id_replace: - printf(" [%4d] %s: %s\n", instructionOffset, "put_by_id_replace", pointerToSourceString(stubInfo.u.putByIdReplace.baseObjectStructure).UTF8String().c_str()); - return; - case op_get_by_id: - printf(" [%4d] %s\n", instructionOffset, "get_by_id"); - return; - case op_put_by_id: - printf(" [%4d] %s\n", instructionOffset, "put_by_id"); - return; - case op_get_by_id_generic: - printf(" [%4d] %s\n", instructionOffset, "op_get_by_id_generic"); - return; - case op_put_by_id_generic: - printf(" [%4d] %s\n", instructionOffset, "op_put_by_id_generic"); - return; - case op_get_array_length: - printf(" [%4d] %s\n", instructionOffset, "op_get_array_length"); - return; - case op_get_string_length: - printf(" [%4d] %s\n", instructionOffset, "op_get_string_length"); - return; - default: - ASSERT_NOT_REACHED(); - } -} + int dst = (++it)->u.operand; + int func = (++it)->u.operand; + int argCount = (++it)->u.operand; + int registerOffset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %s, %d, %d", registerName(dst).data(), registerName(func).data(), argCount, registerOffset); + if (cacheDumpMode == DumpCaches) { + LLIntCallLinkInfo* callLinkInfo = it[1].u.callLinkInfo; + if (callLinkInfo->lastSeenCallee) { + out.printf( + " llint(%p, exec %p)", + callLinkInfo->lastSeenCallee.get(), + callLinkInfo->lastSeenCallee->executable()); + } +#if ENABLE(JIT) + if (CallLinkInfo* info = map.get(CodeOrigin(location))) { + JSFunction* target = info->lastSeenCallee.get(); + if (target) + out.printf(" jit(%p, exec %p)", target, target->executable()); + } + out.print(" status(", CallLinkStatus::computeFor(this, location, map), ")"); +#else + UNUSED_PARAM(map); #endif - -void CodeBlock::printStructure(const char* name, const Instruction* vPC, int operand) const -{ - unsigned instructionOffset = vPC - m_instructions.begin(); - printf(" [%4d] %s: %s\n", instructionOffset, name, pointerToSourceString(vPC[operand].u.structure).UTF8String().c_str()); + } + ++it; + ++it; + dumpArrayProfiling(out, it, hasPrintedProfiling); + dumpValueProfiling(out, it, hasPrintedProfiling); } -void CodeBlock::printStructures(const Instruction* vPC) const +void CodeBlock::printPutByIdOp(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op) { - Interpreter* interpreter = m_globalData->interpreter; - unsigned instructionOffset = vPC - m_instructions.begin(); - - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id)) { - printStructure("get_by_id", vPC, 4); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) { - printStructure("get_by_id_self", vPC, 4); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) { - 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()); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) { - 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()); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) { - 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()); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id)) { - printStructure("put_by_id", vPC, 4); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) { - printStructure("put_by_id_replace", vPC, 4); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) { - printStructure("resolve_global", vPC, 4); - return; - } - - // These m_instructions doesn't ref Structures. - 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)); + int r0 = (++it)->u.operand; + int id0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, op); + out.printf("%s, %s, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), registerName(r1).data()); + it += 5; } -void CodeBlock::dump(ExecState* exec) const +void CodeBlock::dumpBytecode(PrintStream& out) { - if (m_instructions.isEmpty()) { - printf("No instructions available.\n"); - return; - } - + // We only use the ExecState* for things that don't actually lead to JS execution, + // like converting a JSString to a String. Hence the globalExec is appropriate. + ExecState* exec = m_globalObject->globalExec(); + size_t instructionCount = 0; - for (size_t i = 0; i < m_instructions.size(); i += opcodeLengths[exec->interpreter()->getOpcodeID(m_instructions[i].u.opcode)]) + for (size_t i = 0; i < instructions().size(); i += opcodeLengths[exec->interpreter()->getOpcodeID(instructions()[i].u.opcode)]) ++instructionCount; - printf("%lu m_instructions; %lu bytes at %p; %d parameter(s); %d callee register(s)\n\n", - static_cast(instructionCount), - static_cast(m_instructions.size() * sizeof(Instruction)), - this, m_numParameters, m_numCalleeRegisters); - - Vector::const_iterator begin = m_instructions.begin(); - Vector::const_iterator end = m_instructions.end(); - for (Vector::const_iterator it = begin; it != end; ++it) - dump(exec, begin, it); - - if (!m_identifiers.isEmpty()) { - printf("\nIdentifiers:\n"); + out.print(*this); + out.printf( + ": %lu m_instructions; %lu bytes; %d parameter(s); %d callee register(s); %d variable(s)", + static_cast(instructions().size()), + static_cast(instructions().size() * sizeof(Instruction)), + m_numParameters, m_numCalleeRegisters, m_numVars); + if (symbolTable() && symbolTable()->captureCount()) { + out.printf( + "; %d captured var(s) (from r%d to r%d, inclusive)", + symbolTable()->captureCount(), symbolTable()->captureStart(), symbolTable()->captureEnd() + 1); + } + if (usesArguments()) { + out.printf( + "; uses arguments, in r%d, r%d", + argumentsRegister().offset(), + unmodifiedArgumentsRegister(argumentsRegister()).offset()); + } + if (needsActivation() && codeType() == FunctionCode) + out.printf("; activation in r%d", activationRegister().offset()); + out.printf("\n"); + + StubInfoMap stubInfos; + CallLinkInfoMap callLinkInfos; + getStubInfoMap(stubInfos); + getCallLinkInfoMap(callLinkInfos); + + const Instruction* begin = instructions().begin(); + const Instruction* end = instructions().end(); + for (const Instruction* it = begin; it != end; ++it) + dumpBytecode(out, exec, begin, it, stubInfos, callLinkInfos); + + if (numberOfIdentifiers()) { + out.printf("\nIdentifiers:\n"); size_t i = 0; do { - printf(" id%u = %s\n", static_cast(i), m_identifiers[i].ascii()); + out.printf(" id%u = %s\n", static_cast(i), identifier(i).string().utf8().data()); ++i; - } while (i != m_identifiers.size()); + } while (i != numberOfIdentifiers()); } if (!m_constantRegisters.isEmpty()) { - printf("\nConstants:\n"); - unsigned registerIndex = m_numVars; + out.printf("\nConstants:\n"); size_t i = 0; do { - printf(" r%u = %s\n", registerIndex, valueToSourceString(exec, m_constantRegisters[i].jsValue(exec)).ascii()); + out.printf(" k%u = %s\n", static_cast(i), toCString(m_constantRegisters[i].get()).data()); ++i; - ++registerIndex; } while (i < m_constantRegisters.size()); } - if (m_rareData && !m_rareData->m_unexpectedConstants.isEmpty()) { - printf("\nUnexpected Constants:\n"); - size_t i = 0; - do { - printf(" k%u = %s\n", static_cast(i), valueToSourceString(exec, m_rareData->m_unexpectedConstants[i]).ascii()); - ++i; - } while (i < m_rareData->m_unexpectedConstants.size()); - } - - if (m_rareData && !m_rareData->m_regexps.isEmpty()) { - printf("\nm_regexps:\n"); + if (size_t count = m_unlinkedCode->numberOfRegExps()) { + out.printf("\nm_regexps:\n"); size_t i = 0; do { - printf(" re%u = %s\n", static_cast(i), regexpToSourceString(m_rareData->m_regexps[i].get()).ascii()); + out.printf(" re%u = %s\n", static_cast(i), regexpToSourceString(m_unlinkedCode->regexp(i)).data()); ++i; - } while (i < m_rareData->m_regexps.size()); - } - -#if ENABLE(JIT) - if (!m_globalResolveInfos.isEmpty() || !m_structureStubInfos.isEmpty()) - printf("\nStructures:\n"); - - if (!m_globalResolveInfos.isEmpty()) { - size_t i = 0; - do { - printGlobalResolveInfo(m_globalResolveInfos[i], instructionOffsetForNth(exec, m_instructions, i + 1, isGlobalResolve)); - ++i; - } while (i < m_globalResolveInfos.size()); - } - if (!m_structureStubInfos.isEmpty()) { - size_t i = 0; - do { - printStructureStubInfo(m_structureStubInfos[i], instructionOffsetForNth(exec, m_instructions, i + 1, isPropertyAccess)); - ++i; - } while (i < m_structureStubInfos.size()); - } -#else - if (!m_globalResolveInstructions.isEmpty() || !m_propertyAccessInstructions.isEmpty()) - printf("\nStructures:\n"); - - if (!m_globalResolveInstructions.isEmpty()) { - size_t i = 0; - do { - printStructures(&m_instructions[m_globalResolveInstructions[i]]); - ++i; - } while (i < m_globalResolveInstructions.size()); + } while (i < count); } - if (!m_propertyAccessInstructions.isEmpty()) { - size_t i = 0; - do { - printStructures(&m_instructions[m_propertyAccessInstructions[i]]); - ++i; - } while (i < m_propertyAccessInstructions.size()); - } -#endif if (m_rareData && !m_rareData->m_exceptionHandlers.isEmpty()) { - printf("\nException Handlers:\n"); + out.printf("\nException Handlers:\n"); unsigned i = 0; do { - 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); + out.printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] depth: [%4d] }\n", i + 1, m_rareData->m_exceptionHandlers[i].start, m_rareData->m_exceptionHandlers[i].end, m_rareData->m_exceptionHandlers[i].target, m_rareData->m_exceptionHandlers[i].scopeDepth); ++i; } while (i < m_rareData->m_exceptionHandlers.size()); } - if (m_rareData && !m_rareData->m_immediateSwitchJumpTables.isEmpty()) { - printf("Immediate Switch Jump Tables:\n"); + if (m_rareData && !m_rareData->m_switchJumpTables.isEmpty()) { + out.printf("Switch Jump Tables:\n"); unsigned i = 0; do { - printf(" %1d = {\n", i); + out.printf(" %1d = {\n", i); int entry = 0; - Vector::const_iterator end = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.end(); - for (Vector::const_iterator iter = m_rareData->m_immediateSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) { + Vector::const_iterator end = m_rareData->m_switchJumpTables[i].branchOffsets.end(); + for (Vector::const_iterator iter = m_rareData->m_switchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) { if (!*iter) continue; - printf("\t\t%4d => %04d\n", entry + m_rareData->m_immediateSwitchJumpTables[i].min, *iter); + out.printf("\t\t%4d => %04d\n", entry + m_rareData->m_switchJumpTables[i].min, *iter); } - printf(" }\n"); - ++i; - } while (i < m_rareData->m_immediateSwitchJumpTables.size()); - } - - if (m_rareData && !m_rareData->m_characterSwitchJumpTables.isEmpty()) { - printf("\nCharacter Switch Jump Tables:\n"); - unsigned i = 0; - do { - printf(" %1d = {\n", i); - int entry = 0; - Vector::const_iterator end = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.end(); - for (Vector::const_iterator iter = m_rareData->m_characterSwitchJumpTables[i].branchOffsets.begin(); iter != end; ++iter, ++entry) { - if (!*iter) - continue; - ASSERT(!((i + m_rareData->m_characterSwitchJumpTables[i].min) & ~0xFFFF)); - UChar ch = static_cast(entry + m_rareData->m_characterSwitchJumpTables[i].min); - printf("\t\t\"%s\" => %04d\n", UString(&ch, 1).ascii(), *iter); - } - printf(" }\n"); + out.printf(" }\n"); ++i; - } while (i < m_rareData->m_characterSwitchJumpTables.size()); + } while (i < m_rareData->m_switchJumpTables.size()); } if (m_rareData && !m_rareData->m_stringSwitchJumpTables.isEmpty()) { - printf("\nString Switch Jump Tables:\n"); + out.printf("\nString Switch Jump Tables:\n"); unsigned i = 0; do { - printf(" %1d = {\n", i); + out.printf(" %1d = {\n", i); StringJumpTable::StringOffsetTable::const_iterator end = m_rareData->m_stringSwitchJumpTables[i].offsetTable.end(); for (StringJumpTable::StringOffsetTable::const_iterator iter = m_rareData->m_stringSwitchJumpTables[i].offsetTable.begin(); iter != end; ++iter) - printf("\t\t\"%s\" => %04d\n", UString(iter->first).ascii(), iter->second.branchOffset); - printf(" }\n"); + out.printf("\t\t\"%s\" => %04d\n", iter->key->utf8().data(), iter->value.branchOffset); + out.printf(" }\n"); ++i; } while (i < m_rareData->m_stringSwitchJumpTables.size()); } - printf("\n"); + out.printf("\n"); +} + +void CodeBlock::beginDumpProfiling(PrintStream& out, bool& hasPrintedProfiling) +{ + if (hasPrintedProfiling) { + out.print("; "); + return; + } + + out.print(" "); + hasPrintedProfiling = true; +} + +void CodeBlock::dumpValueProfiling(PrintStream& out, const Instruction*& it, bool& hasPrintedProfiling) +{ + ConcurrentJITLocker locker(m_lock); + + ++it; + CString description = it->u.profile->briefDescription(locker); + if (!description.length()) + return; + beginDumpProfiling(out, hasPrintedProfiling); + out.print(description); +} + +void CodeBlock::dumpArrayProfiling(PrintStream& out, const Instruction*& it, bool& hasPrintedProfiling) +{ + ConcurrentJITLocker locker(m_lock); + + ++it; + if (!it->u.arrayProfile) + return; + CString description = it->u.arrayProfile->briefDescription(locker, this); + if (!description.length()) + return; + beginDumpProfiling(out, hasPrintedProfiling); + out.print(description); +} + +void CodeBlock::dumpRareCaseProfile(PrintStream& out, const char* name, RareCaseProfile* profile, bool& hasPrintedProfiling) +{ + if (!profile || !profile->m_counter) + return; + + beginDumpProfiling(out, hasPrintedProfiling); + out.print(name, profile->m_counter); +} + +void CodeBlock::printLocationAndOp(PrintStream& out, ExecState*, int location, const Instruction*&, const char* op) +{ + out.printf("[%4d] %-17s ", location, op); +} + +void CodeBlock::printLocationOpAndRegisterOperand(PrintStream& out, ExecState* exec, int location, const Instruction*& it, const char* op, int operand) +{ + printLocationAndOp(out, exec, location, it, op); + out.printf("%s", registerName(operand).data()); } -void CodeBlock::dump(ExecState* exec, const Vector::const_iterator& begin, Vector::const_iterator& it) const +void CodeBlock::dumpBytecode( + PrintStream& out, ExecState* exec, const Instruction* begin, const Instruction*& it, + const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos) { int location = it - begin; - switch (exec->interpreter()->getOpcodeID(it->u.opcode)) { + bool hasPrintedProfiling = false; + OpcodeID opcode = exec->interpreter()->getOpcodeID(it->u.opcode); + switch (opcode) { case op_enter: { - printf("[%4d] enter\n", location); + printLocationAndOp(out, exec, location, it, "enter"); + break; + } + case op_touch_entry: { + printLocationAndOp(out, exec, location, it, "touch_entry"); break; } - case op_enter_with_activation: { + case op_create_activation: { int r0 = (++it)->u.operand; - printf("[%4d] enter_with_activation %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "create_activation", r0); break; } case op_create_arguments: { - printf("[%4d] create_arguments\n", location); + int r0 = (++it)->u.operand; + printLocationOpAndRegisterOperand(out, exec, location, it, "create_arguments", r0); break; } - case op_convert_this: { + case op_init_lazy_reg: { int r0 = (++it)->u.operand; - printf("[%4d] convert_this %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "init_lazy_reg", r0); break; } - case op_unexpected_load: { + case op_get_callee: { int r0 = (++it)->u.operand; - int k0 = (++it)->u.operand; - printf("[%4d] unexpected_load\t %s, %s\n", location, registerName(r0).c_str(), constantName(exec, k0, unexpectedConstant(k0)).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "get_callee", r0); + ++it; + break; + } + case op_create_this: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + unsigned inferredInlineCapacity = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "create_this"); + out.printf("%s, %s, %u", registerName(r0).data(), registerName(r1).data(), inferredInlineCapacity); + break; + } + case op_to_this: { + int r0 = (++it)->u.operand; + printLocationOpAndRegisterOperand(out, exec, location, it, "to_this", r0); + Structure* structure = (++it)->u.structure.get(); + if (structure) + out.print(" cache(struct = ", RawPointer(structure), ")"); break; } case op_new_object: { int r0 = (++it)->u.operand; - printf("[%4d] new_object\t %s\n", location, registerName(r0).c_str()); + unsigned inferredInlineCapacity = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "new_object"); + out.printf("%s, %u", registerName(r0).data(), inferredInlineCapacity); + ++it; // Skip object allocation profile. break; } case op_new_array: { int dst = (++it)->u.operand; int argv = (++it)->u.operand; int argc = (++it)->u.operand; - printf("[%4d] new_array\t %s, %s, %d\n", location, registerName(dst).c_str(), registerName(argv).c_str(), argc); + printLocationAndOp(out, exec, location, it, "new_array"); + out.printf("%s, %s, %d", registerName(dst).data(), registerName(argv).data(), argc); + ++it; // Skip array allocation profile. + break; + } + case op_new_array_with_size: { + int dst = (++it)->u.operand; + int length = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "new_array_with_size"); + out.printf("%s, %s", registerName(dst).data(), registerName(length).data()); + ++it; // Skip array allocation profile. + break; + } + case op_new_array_buffer: { + int dst = (++it)->u.operand; + int argv = (++it)->u.operand; + int argc = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "new_array_buffer"); + out.printf("%s, %d, %d", registerName(dst).data(), argv, argc); + ++it; // Skip array allocation profile. break; } case op_new_regexp: { int r0 = (++it)->u.operand; int re0 = (++it)->u.operand; - printf("[%4d] new_regexp\t %s, %s\n", location, registerName(r0).c_str(), regexpName(re0, regexp(re0)).c_str()); + printLocationAndOp(out, exec, location, it, "new_regexp"); + out.printf("%s, ", registerName(r0).data()); + if (r0 >=0 && r0 < (int)m_unlinkedCode->numberOfRegExps()) + out.printf("%s", regexpName(re0, regexp(re0)).data()); + else + out.printf("bad_regexp(%d)", re0); break; } case op_mov: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; - printf("[%4d] mov\t\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str()); + printLocationAndOp(out, exec, location, it, "mov"); + out.printf("%s, %s", registerName(r0).data(), registerName(r1).data()); + break; + } + case op_captured_mov: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "captured_mov"); + out.printf("%s, %s", registerName(r0).data(), registerName(r1).data()); + ++it; break; } case op_not: { - printUnaryOp(location, it, "not"); + printUnaryOp(out, exec, location, it, "not"); break; } case op_eq: { - printBinaryOp(location, it, "eq"); + printBinaryOp(out, exec, location, it, "eq"); break; } case op_eq_null: { - printUnaryOp(location, it, "eq_null"); + printUnaryOp(out, exec, location, it, "eq_null"); break; } case op_neq: { - printBinaryOp(location, it, "neq"); + printBinaryOp(out, exec, location, it, "neq"); break; } case op_neq_null: { - printUnaryOp(location, it, "neq_null"); + printUnaryOp(out, exec, location, it, "neq_null"); break; } case op_stricteq: { - printBinaryOp(location, it, "stricteq"); + printBinaryOp(out, exec, location, it, "stricteq"); break; } case op_nstricteq: { - printBinaryOp(location, it, "nstricteq"); + printBinaryOp(out, exec, location, it, "nstricteq"); break; } case op_less: { - printBinaryOp(location, it, "less"); + printBinaryOp(out, exec, location, it, "less"); break; } case op_lesseq: { - printBinaryOp(location, it, "lesseq"); + printBinaryOp(out, exec, location, it, "lesseq"); break; } - case op_pre_inc: { - int r0 = (++it)->u.operand; - printf("[%4d] pre_inc\t\t %s\n", location, registerName(r0).c_str()); + case op_greater: { + printBinaryOp(out, exec, location, it, "greater"); break; } - case op_pre_dec: { - int r0 = (++it)->u.operand; - printf("[%4d] pre_dec\t\t %s\n", location, registerName(r0).c_str()); + case op_greatereq: { + printBinaryOp(out, exec, location, it, "greatereq"); break; } - case op_post_inc: { - printUnaryOp(location, it, "post_inc"); + case op_inc: { + int r0 = (++it)->u.operand; + printLocationOpAndRegisterOperand(out, exec, location, it, "inc", r0); break; } - case op_post_dec: { - printUnaryOp(location, it, "post_dec"); + case op_dec: { + int r0 = (++it)->u.operand; + printLocationOpAndRegisterOperand(out, exec, location, it, "dec", r0); break; } - case op_to_jsnumber: { - printUnaryOp(location, it, "to_jsnumber"); + case op_to_number: { + printUnaryOp(out, exec, location, it, "to_number"); break; } case op_negate: { - printUnaryOp(location, it, "negate"); + printUnaryOp(out, exec, location, it, "negate"); break; } case op_add: { - printBinaryOp(location, it, "add"); + printBinaryOp(out, exec, location, it, "add"); ++it; break; } case op_mul: { - printBinaryOp(location, it, "mul"); + printBinaryOp(out, exec, location, it, "mul"); ++it; break; } case op_div: { - printBinaryOp(location, it, "div"); + printBinaryOp(out, exec, location, it, "div"); + ++it; break; } case op_mod: { - printBinaryOp(location, it, "mod"); + printBinaryOp(out, exec, location, it, "mod"); break; } case op_sub: { - printBinaryOp(location, it, "sub"); + printBinaryOp(out, exec, location, it, "sub"); ++it; break; } case op_lshift: { - printBinaryOp(location, it, "lshift"); + printBinaryOp(out, exec, location, it, "lshift"); break; } case op_rshift: { - printBinaryOp(location, it, "rshift"); + printBinaryOp(out, exec, location, it, "rshift"); break; } case op_urshift: { - printBinaryOp(location, it, "urshift"); + printBinaryOp(out, exec, location, it, "urshift"); break; } case op_bitand: { - printBinaryOp(location, it, "bitand"); + printBinaryOp(out, exec, location, it, "bitand"); ++it; break; } case op_bitxor: { - printBinaryOp(location, it, "bitxor"); + printBinaryOp(out, exec, location, it, "bitxor"); ++it; break; } case op_bitor: { - printBinaryOp(location, it, "bitor"); + printBinaryOp(out, exec, location, it, "bitor"); ++it; break; } - case op_bitnot: { - printUnaryOp(location, it, "bitnot"); + case op_check_has_instance: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int r2 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "check_has_instance"); + out.printf("%s, %s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), registerName(r2).data(), offset, location + offset); break; } case op_instanceof: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int r2 = (++it)->u.operand; - int r3 = (++it)->u.operand; - 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()); + printLocationAndOp(out, exec, location, it, "instanceof"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); + break; + } + case op_unsigned: { + printUnaryOp(out, exec, location, it, "unsigned"); break; } case op_typeof: { - printUnaryOp(location, it, "typeof"); + printUnaryOp(out, exec, location, it, "typeof"); break; } case op_is_undefined: { - printUnaryOp(location, it, "is_undefined"); + printUnaryOp(out, exec, location, it, "is_undefined"); break; } case op_is_boolean: { - printUnaryOp(location, it, "is_boolean"); + printUnaryOp(out, exec, location, it, "is_boolean"); break; } case op_is_number: { - printUnaryOp(location, it, "is_number"); + printUnaryOp(out, exec, location, it, "is_number"); break; } case op_is_string: { - printUnaryOp(location, it, "is_string"); + printUnaryOp(out, exec, location, it, "is_string"); break; } case op_is_object: { - printUnaryOp(location, it, "is_object"); + printUnaryOp(out, exec, location, it, "is_object"); break; } case op_is_function: { - printUnaryOp(location, it, "is_function"); + printUnaryOp(out, exec, location, it, "is_function"); break; } case op_in: { - printBinaryOp(location, it, "in"); + printBinaryOp(out, exec, location, it, "in"); break; } - case op_resolve: { - int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; - printf("[%4d] resolve\t\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str()); + case op_init_global_const_nop: { + printLocationAndOp(out, exec, location, it, "init_global_const_nop"); + it++; + it++; + it++; + it++; break; } - case op_resolve_skip: { + case op_init_global_const: { + WriteBarrier* registerPointer = (++it)->u.registerPointer; int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; - int skipLevels = (++it)->u.operand; - printf("[%4d] resolve_skip\t %s, %s, %d\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str(), skipLevels); + printLocationAndOp(out, exec, location, it, "init_global_const"); + out.printf("g%d(%p), %s", m_globalObject->findRegisterIndex(registerPointer), registerPointer, registerName(r0).data()); + it++; + it++; break; } - case op_resolve_global: { - int r0 = (++it)->u.operand; - JSValuePtr scope = JSValuePtr((++it)->u.jsCell); - int id0 = (++it)->u.operand; - 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()); - it += 2; + case op_get_by_id: + case op_get_by_id_out_of_line: + case op_get_array_length: { + printGetByIdOp(out, exec, location, it); + printGetByIdCacheStatus(out, exec, location, stubInfos); + dumpValueProfiling(out, it, hasPrintedProfiling); break; } - case op_get_scoped_var: { - int r0 = (++it)->u.operand; - int index = (++it)->u.operand; - int skipLevels = (++it)->u.operand; - printf("[%4d] get_scoped_var\t %s, %d, %d\n", location, registerName(r0).c_str(), index, skipLevels); + case op_get_arguments_length: { + printUnaryOp(out, exec, location, it, "get_arguments_length"); + it++; break; } - case op_put_scoped_var: { - int index = (++it)->u.operand; - int skipLevels = (++it)->u.operand; - int r0 = (++it)->u.operand; - printf("[%4d] put_scoped_var\t %d, %d, %s\n", location, index, skipLevels, registerName(r0).c_str()); + case op_put_by_id: { + printPutByIdOp(out, exec, location, it, "put_by_id"); break; } - case op_get_global_var: { - int r0 = (++it)->u.operand; - JSValuePtr scope = JSValuePtr((++it)->u.jsCell); - int index = (++it)->u.operand; - printf("[%4d] get_global_var\t %s, %s, %d\n", location, registerName(r0).c_str(), valueToSourceString(exec, scope).ascii(), index); + case op_put_by_id_out_of_line: { + printPutByIdOp(out, exec, location, it, "put_by_id_out_of_line"); break; } - case op_put_global_var: { - JSValuePtr scope = JSValuePtr((++it)->u.jsCell); - int index = (++it)->u.operand; - int r0 = (++it)->u.operand; - printf("[%4d] put_global_var\t %s, %d, %s\n", location, valueToSourceString(exec, scope).ascii(), index, registerName(r0).c_str()); + case op_put_by_id_transition_direct: { + printPutByIdOp(out, exec, location, it, "put_by_id_transition_direct"); break; } - case op_resolve_base: { - int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; - printf("[%4d] resolve_base\t %s, %s\n", location, registerName(r0).c_str(), idName(id0, m_identifiers[id0]).c_str()); + case op_put_by_id_transition_direct_out_of_line: { + printPutByIdOp(out, exec, location, it, "put_by_id_transition_direct_out_of_line"); + break; + } + case op_put_by_id_transition_normal: { + printPutByIdOp(out, exec, location, it, "put_by_id_transition_normal"); + break; + } + case op_put_by_id_transition_normal_out_of_line: { + printPutByIdOp(out, exec, location, it, "put_by_id_transition_normal_out_of_line"); break; } - case op_resolve_with_base: { + case op_put_getter_setter: { int r0 = (++it)->u.operand; - int r1 = (++it)->u.operand; int id0 = (++it)->u.operand; - 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()); + int r1 = (++it)->u.operand; + int r2 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "put_getter_setter"); + out.printf("%s, %s, %s, %s", registerName(r0).data(), idName(id0, identifier(id0)).data(), registerName(r1).data(), registerName(r2).data()); break; } - case op_resolve_func: { + case op_del_by_id: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int id0 = (++it)->u.operand; - printf("[%4d] resolve_func\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), idName(id0, m_identifiers[id0]).c_str()); - break; - } - case op_get_by_id: { - printGetByIdOp(location, it, m_identifiers, "get_by_id"); - break; - } - case op_get_by_id_self: { - printGetByIdOp(location, it, m_identifiers, "get_by_id_self"); - break; - } - case op_get_by_id_self_list: { - printGetByIdOp(location, it, m_identifiers, "get_by_id_self_list"); - break; - } - case op_get_by_id_proto: { - printGetByIdOp(location, it, m_identifiers, "get_by_id_proto"); - break; - } - case op_get_by_id_proto_list: { - printGetByIdOp(location, it, m_identifiers, "op_get_by_id_proto_list"); - break; - } - case op_get_by_id_chain: { - printGetByIdOp(location, it, m_identifiers, "get_by_id_chain"); - break; - } - case op_get_by_id_generic: { - printGetByIdOp(location, it, m_identifiers, "get_by_id_generic"); - break; - } - case op_get_array_length: { - printGetByIdOp(location, it, m_identifiers, "get_array_length"); - break; - } - case op_get_string_length: { - printGetByIdOp(location, it, m_identifiers, "get_string_length"); - break; - } - case op_put_by_id: { - printPutByIdOp(location, it, m_identifiers, "put_by_id"); + printLocationAndOp(out, exec, location, it, "del_by_id"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), idName(id0, identifier(id0)).data()); break; } - case op_put_by_id_replace: { - printPutByIdOp(location, it, m_identifiers, "put_by_id_replace"); - break; - } - case op_put_by_id_transition: { - printPutByIdOp(location, it, m_identifiers, "put_by_id_transition"); - break; - } - case op_put_by_id_generic: { - printPutByIdOp(location, it, m_identifiers, "put_by_id_generic"); - break; - } - case op_put_getter: { + case op_get_by_val: { int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; int r1 = (++it)->u.operand; - 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()); + int r2 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "get_by_val"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); + dumpArrayProfiling(out, it, hasPrintedProfiling); + dumpValueProfiling(out, it, hasPrintedProfiling); break; } - case op_put_setter: { + case op_get_argument_by_val: { int r0 = (++it)->u.operand; - int id0 = (++it)->u.operand; int r1 = (++it)->u.operand; - 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()); + int r2 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "get_argument_by_val"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); + ++it; + dumpValueProfiling(out, it, hasPrintedProfiling); break; } - case op_del_by_id: { + case op_get_by_pname: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; - int id0 = (++it)->u.operand; - 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()); + int r2 = (++it)->u.operand; + int r3 = (++it)->u.operand; + int r4 = (++it)->u.operand; + int r5 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "get_by_pname"); + out.printf("%s, %s, %s, %s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data(), registerName(r3).data(), registerName(r4).data(), registerName(r5).data()); break; } - case op_get_by_val: { + case op_put_by_val: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int r2 = (++it)->u.operand; - printf("[%4d] get_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str()); + printLocationAndOp(out, exec, location, it, "put_by_val"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); + dumpArrayProfiling(out, it, hasPrintedProfiling); break; } - case op_put_by_val: { + case op_put_by_val_direct: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int r2 = (++it)->u.operand; - printf("[%4d] put_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str()); + printLocationAndOp(out, exec, location, it, "put_by_val_direct"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); + dumpArrayProfiling(out, it, hasPrintedProfiling); break; } case op_del_by_val: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int r2 = (++it)->u.operand; - printf("[%4d] del_by_val\t %s, %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str(), registerName(r2).c_str()); + printLocationAndOp(out, exec, location, it, "del_by_val"); + out.printf("%s, %s, %s", registerName(r0).data(), registerName(r1).data(), registerName(r2).data()); break; } case op_put_by_index: { int r0 = (++it)->u.operand; unsigned n0 = (++it)->u.operand; int r1 = (++it)->u.operand; - printf("[%4d] put_by_index\t %s, %u, %s\n", location, registerName(r0).c_str(), n0, registerName(r1).c_str()); + printLocationAndOp(out, exec, location, it, "put_by_index"); + out.printf("%s, %u, %s", registerName(r0).data(), n0, registerName(r1).data()); break; } case op_jmp: { int offset = (++it)->u.operand; - printf("[%4d] jmp\t\t %d(->%d)\n", location, offset, locationForOffset(begin, it, offset)); - break; - } - case op_loop: { - int offset = (++it)->u.operand; - printf("[%4d] loop\t\t %d(->%d)\n", location, offset, locationForOffset(begin, it, offset)); + printLocationAndOp(out, exec, location, it, "jmp"); + out.printf("%d(->%d)", offset, location + offset); break; } case op_jtrue: { - printConditionalJump(begin, it, location, "jtrue"); - break; - } - case op_loop_if_true: { - printConditionalJump(begin, it, location, "loop_if_true"); + printConditionalJump(out, exec, begin, it, location, "jtrue"); break; } case op_jfalse: { - printConditionalJump(begin, it, location, "jfalse"); + printConditionalJump(out, exec, begin, it, location, "jfalse"); break; } case op_jeq_null: { - printConditionalJump(begin, it, location, "jeq_null"); + printConditionalJump(out, exec, begin, it, location, "jeq_null"); break; } case op_jneq_null: { - printConditionalJump(begin, it, location, "jneq_null"); + printConditionalJump(out, exec, begin, it, location, "jneq_null"); + break; + } + case op_jneq_ptr: { + int r0 = (++it)->u.operand; + Special::Pointer pointer = (++it)->u.specialPointer; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jneq_ptr"); + out.printf("%s, %d (%p), %d(->%d)", registerName(r0).data(), pointer, m_globalObject->actualPointerFor(pointer), offset, location + offset); + break; + } + case op_jless: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jless"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); + break; + } + case op_jlesseq: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jlesseq"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); + break; + } + case op_jgreater: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jgreater"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); + break; + } + case op_jgreatereq: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jgreatereq"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); break; } case op_jnless: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int offset = (++it)->u.operand; - printf("[%4d] jnless\t\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, locationForOffset(begin, it, offset)); + printLocationAndOp(out, exec, location, it, "jnless"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); break; } - case op_loop_if_less: { + case op_jnlesseq: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int offset = (++it)->u.operand; - printf("[%4d] loop_if_less\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, locationForOffset(begin, it, offset)); + printLocationAndOp(out, exec, location, it, "jnlesseq"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); break; } - case op_loop_if_lesseq: { + case op_jngreater: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; int offset = (++it)->u.operand; - printf("[%4d] loop_if_lesseq\t %s, %s, %d(->%d)\n", location, registerName(r0).c_str(), registerName(r1).c_str(), offset, locationForOffset(begin, it, offset)); + printLocationAndOp(out, exec, location, it, "jngreater"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); + break; + } + case op_jngreatereq: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int offset = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "jngreatereq"); + out.printf("%s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), offset, location + offset); + break; + } + case op_loop_hint: { + printLocationAndOp(out, exec, location, it, "loop_hint"); break; } case op_switch_imm: { int tableIndex = (++it)->u.operand; int defaultTarget = (++it)->u.operand; int scrutineeRegister = (++it)->u.operand; - printf("[%4d] switch_imm\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, locationForOffset(begin, it, defaultTarget), registerName(scrutineeRegister).c_str()); + printLocationAndOp(out, exec, location, it, "switch_imm"); + out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data()); break; } case op_switch_char: { int tableIndex = (++it)->u.operand; int defaultTarget = (++it)->u.operand; int scrutineeRegister = (++it)->u.operand; - printf("[%4d] switch_char\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, locationForOffset(begin, it, defaultTarget), registerName(scrutineeRegister).c_str()); + printLocationAndOp(out, exec, location, it, "switch_char"); + out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data()); break; } case op_switch_string: { int tableIndex = (++it)->u.operand; int defaultTarget = (++it)->u.operand; int scrutineeRegister = (++it)->u.operand; - printf("[%4d] switch_string\t %d, %d(->%d), %s\n", location, tableIndex, defaultTarget, locationForOffset(begin, it, defaultTarget), registerName(scrutineeRegister).c_str()); + printLocationAndOp(out, exec, location, it, "switch_string"); + out.printf("%d, %d(->%d), %s", tableIndex, defaultTarget, location + defaultTarget, registerName(scrutineeRegister).data()); break; } case op_new_func: { int r0 = (++it)->u.operand; int f0 = (++it)->u.operand; - printf("[%4d] new_func\t\t %s, f%d\n", location, registerName(r0).c_str(), f0); + int shouldCheck = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "new_func"); + out.printf("%s, f%d, %s", registerName(r0).data(), f0, shouldCheck ? "" : ""); + break; + } + case op_new_captured_func: { + int r0 = (++it)->u.operand; + int f0 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "new_captured_func"); + out.printf("%s, f%d", registerName(r0).data(), f0); + ++it; break; } case op_new_func_exp: { int r0 = (++it)->u.operand; int f0 = (++it)->u.operand; - printf("[%4d] new_func_exp\t %s, f%d\n", location, registerName(r0).c_str(), f0); + printLocationAndOp(out, exec, location, it, "new_func_exp"); + out.printf("%s, f%d", registerName(r0).data(), f0); break; } case op_call: { - int dst = (++it)->u.operand; - int func = (++it)->u.operand; - int argCount = (++it)->u.operand; - int registerOffset = (++it)->u.operand; - printf("[%4d] call\t\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset); + printCallOp(out, exec, location, it, "call", DumpCaches, hasPrintedProfiling, callLinkInfos); break; } case op_call_eval: { - int dst = (++it)->u.operand; - int func = (++it)->u.operand; - int argCount = (++it)->u.operand; - int registerOffset = (++it)->u.operand; - printf("[%4d] call_eval\t %s, %s, %d, %d\n", location, registerName(dst).c_str(), registerName(func).c_str(), argCount, registerOffset); + printCallOp(out, exec, location, it, "call_eval", DontDumpCaches, hasPrintedProfiling, callLinkInfos); + break; + } + + case op_construct_varargs: + case op_call_varargs: { + int result = (++it)->u.operand; + int callee = (++it)->u.operand; + int thisValue = (++it)->u.operand; + int arguments = (++it)->u.operand; + int firstFreeRegister = (++it)->u.operand; + int varArgOffset = (++it)->u.operand; + ++it; + printLocationAndOp(out, exec, location, it, opcode == op_call_varargs ? "call_varargs" : "construct_varargs"); + out.printf("%s, %s, %s, %s, %d, %d", registerName(result).data(), registerName(callee).data(), registerName(thisValue).data(), registerName(arguments).data(), firstFreeRegister, varArgOffset); + dumpValueProfiling(out, it, hasPrintedProfiling); break; } + case op_tear_off_activation: { int r0 = (++it)->u.operand; - printf("[%4d] tear_off_activation\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "tear_off_activation", r0); break; } case op_tear_off_arguments: { - printf("[%4d] tear_off_arguments\n", location); + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "tear_off_arguments"); + out.printf("%s, %s", registerName(r0).data(), registerName(r1).data()); break; } case op_ret: { int r0 = (++it)->u.operand; - printf("[%4d] ret\t\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "ret", r0); + break; + } + case op_ret_object_or_this: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "constructor_ret"); + out.printf("%s %s", registerName(r0).data(), registerName(r1).data()); break; } case op_construct: { - int dst = (++it)->u.operand; - int func = (++it)->u.operand; - int argCount = (++it)->u.operand; - int registerOffset = (++it)->u.operand; - int proto = (++it)->u.operand; - int thisRegister = (++it)->u.operand; - 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()); + printCallOp(out, exec, location, it, "construct", DumpCaches, hasPrintedProfiling, callLinkInfos); break; } - case op_construct_verify: { + case op_strcat: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; - printf("[%4d] construct_verify\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str()); + int count = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "strcat"); + out.printf("%s, %s, %d", registerName(r0).data(), registerName(r1).data(), count); break; } - case op_get_pnames: { + case op_to_primitive: { int r0 = (++it)->u.operand; int r1 = (++it)->u.operand; - printf("[%4d] get_pnames\t %s, %s\n", location, registerName(r0).c_str(), registerName(r1).c_str()); + printLocationAndOp(out, exec, location, it, "to_primitive"); + out.printf("%s, %s", registerName(r0).data(), registerName(r1).data()); break; } - case op_next_pname: { - int dest = (++it)->u.operand; - int iter = (++it)->u.operand; - int offset = (++it)->u.operand; - printf("[%4d] next_pname\t %s, %s, %d(->%d)\n", location, registerName(dest).c_str(), registerName(iter).c_str(), offset, locationForOffset(begin, it, offset)); + case op_get_pnames: { + int r0 = it[1].u.operand; + int r1 = it[2].u.operand; + int r2 = it[3].u.operand; + int r3 = it[4].u.operand; + int offset = it[5].u.operand; + printLocationAndOp(out, exec, location, it, "get_pnames"); + out.printf("%s, %s, %s, %s, %d(->%d)", registerName(r0).data(), registerName(r1).data(), registerName(r2).data(), registerName(r3).data(), offset, location + offset); + it += OPCODE_LENGTH(op_get_pnames) - 1; break; } - case op_push_scope: { + case op_next_pname: { + int dest = it[1].u.operand; + int base = it[2].u.operand; + int i = it[3].u.operand; + int size = it[4].u.operand; + int iter = it[5].u.operand; + int offset = it[6].u.operand; + printLocationAndOp(out, exec, location, it, "next_pname"); + out.printf("%s, %s, %s, %s, %s, %d(->%d)", registerName(dest).data(), registerName(base).data(), registerName(i).data(), registerName(size).data(), registerName(iter).data(), offset, location + offset); + it += OPCODE_LENGTH(op_next_pname) - 1; + break; + } + case op_push_with_scope: { int r0 = (++it)->u.operand; - printf("[%4d] push_scope\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "push_with_scope", r0); break; } case op_pop_scope: { - printf("[%4d] pop_scope\n", location); + printLocationAndOp(out, exec, location, it, "pop_scope"); break; } - case op_push_new_scope: { - int r0 = (++it)->u.operand; + case op_push_name_scope: { int id0 = (++it)->u.operand; int r1 = (++it)->u.operand; - 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()); - break; - } - case op_jmp_scopes: { - int scopeDelta = (++it)->u.operand; - int offset = (++it)->u.operand; - printf("[%4d] jmp_scopes\t^%d, %d(->%d)\n", location, scopeDelta, offset, locationForOffset(begin, it, offset)); + unsigned attributes = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "push_name_scope"); + out.printf("%s, %s, %u", idName(id0, identifier(id0)).data(), registerName(r1).data(), attributes); break; } case op_catch: { int r0 = (++it)->u.operand; - printf("[%4d] catch\t\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "catch", r0); break; } case op_throw: { int r0 = (++it)->u.operand; - printf("[%4d] throw\t\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "throw", r0); break; } - case op_new_error: { - int r0 = (++it)->u.operand; - int errorType = (++it)->u.operand; + case op_throw_static_error: { int k0 = (++it)->u.operand; - printf("[%4d] new_error\t %s, %d, %s\n", location, registerName(r0).c_str(), errorType, constantName(exec, k0, unexpectedConstant(k0)).c_str()); - break; - } - case op_jsr: { - int retAddrDst = (++it)->u.operand; - int offset = (++it)->u.operand; - printf("[%4d] jsr\t\t %s, %d(->%d)\n", location, registerName(retAddrDst).c_str(), offset, locationForOffset(begin, it, offset)); - break; - } - case op_sret: { - int retAddrSrc = (++it)->u.operand; - printf("[%4d] sret\t\t %s\n", location, registerName(retAddrSrc).c_str()); + int k1 = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "throw_static_error"); + out.printf("%s, %s", constantName(k0, getConstant(k0)).data(), k1 ? "true" : "false"); break; } case op_debug: { int debugHookID = (++it)->u.operand; - int firstLine = (++it)->u.operand; - int lastLine = (++it)->u.operand; - printf("[%4d] debug\t\t %s, %d, %d\n", location, debugHookName(debugHookID), firstLine, lastLine); + int hasBreakpointFlag = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "debug"); + out.printf("%s %d", debugHookName(debugHookID), hasBreakpointFlag); break; } case op_profile_will_call: { int function = (++it)->u.operand; - printf("[%4d] profile_will_call %s\n", location, registerName(function).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "profile_will_call", function); break; } case op_profile_did_call: { int function = (++it)->u.operand; - printf("[%4d] profile_did_call\t %s\n", location, registerName(function).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "profile_did_call", function); break; } case op_end: { int r0 = (++it)->u.operand; - printf("[%4d] end\t\t %s\n", location, registerName(r0).c_str()); + printLocationOpAndRegisterOperand(out, exec, location, it, "end", r0); + break; + } + case op_resolve_scope: { + int r0 = (++it)->u.operand; + int id0 = (++it)->u.operand; + ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand); + int depth = (++it)->u.operand; + printLocationAndOp(out, exec, location, it, "resolve_scope"); + out.printf("%s, %s, %u<%s|%s>, %d", registerName(r0).data(), idName(id0, identifier(id0)).data(), + modeAndType.operand(), resolveModeName(modeAndType.mode()), resolveTypeName(modeAndType.type()), + depth); + ++it; + break; + } + case op_get_from_scope: { + int r0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + int id0 = (++it)->u.operand; + ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand); + ++it; // Structure + int operand = (++it)->u.operand; // Operand + ++it; // Skip value profile. + printLocationAndOp(out, exec, location, it, "get_from_scope"); + out.printf("%s, %s, %s, %u<%s|%s>, , %d", + registerName(r0).data(), registerName(r1).data(), idName(id0, identifier(id0)).data(), + modeAndType.operand(), resolveModeName(modeAndType.mode()), resolveTypeName(modeAndType.type()), + operand); + break; + } + case op_put_to_scope: { + int r0 = (++it)->u.operand; + int id0 = (++it)->u.operand; + int r1 = (++it)->u.operand; + ResolveModeAndType modeAndType = ResolveModeAndType((++it)->u.operand); + ++it; // Structure + int operand = (++it)->u.operand; // Operand + printLocationAndOp(out, exec, location, it, "put_to_scope"); + out.printf("%s, %s, %s, %u<%s|%s>, , %d", + registerName(r0).data(), idName(id0, identifier(id0)).data(), registerName(r1).data(), + modeAndType.operand(), resolveModeName(modeAndType.mode()), resolveTypeName(modeAndType.type()), + operand); break; } + default: + RELEASE_ASSERT_NOT_REACHED(); } -} -#endif // !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING) + dumpRareCaseProfile(out, "rare case: ", rareCaseProfileForBytecodeOffset(location), hasPrintedProfiling); + dumpRareCaseProfile(out, "special fast case: ", specialFastCaseProfileForBytecodeOffset(location), hasPrintedProfiling); + +#if ENABLE(DFG_JIT) + Vector exitSites = exitProfile().exitSitesFor(location); + if (!exitSites.isEmpty()) { + out.print(" !! frequent exits: "); + CommaPrinter comma; + for (unsigned i = 0; i < exitSites.size(); ++i) + out.print(comma, exitSites[i].kind(), " ", exitSites[i].jitType()); + } +#else // ENABLE(DFG_JIT) + UNUSED_PARAM(location); +#endif // ENABLE(DFG_JIT) + out.print("\n"); +} -#if DUMP_CODE_BLOCK_STATISTICS -static HashSet liveCodeBlockSet; -#endif +void CodeBlock::dumpBytecode( + PrintStream& out, unsigned bytecodeOffset, + const StubInfoMap& stubInfos, const CallLinkInfoMap& callLinkInfos) +{ + ExecState* exec = m_globalObject->globalExec(); + const Instruction* it = instructions().begin() + bytecodeOffset; + dumpBytecode(out, exec, instructions().begin(), it, stubInfos, callLinkInfos); +} #define FOR_EACH_MEMBER_VECTOR(macro) \ macro(instructions) \ - macro(globalResolveInfos) \ - macro(structureStubInfos) \ macro(callLinkInfos) \ macro(linkedCallerList) \ macro(identifiers) \ @@ -1095,18 +1402,13 @@ static HashSet liveCodeBlockSet; #define FOR_EACH_MEMBER_VECTOR_RARE_DATA(macro) \ macro(regexps) \ macro(functions) \ - macro(unexpectedConstants) \ macro(exceptionHandlers) \ - macro(immediateSwitchJumpTables) \ - macro(characterSwitchJumpTables) \ + macro(switchJumpTables) \ macro(stringSwitchJumpTables) \ - macro(functionRegisterInfos) - -#define FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(macro) \ + macro(evalCodeCache) \ macro(expressionInfo) \ macro(lineInfo) \ - macro(getByIdExceptionInfo) \ - macro(pcVector) + macro(callReturnIndexVector) template static size_t sizeInBytes(const Vector& vector) @@ -1114,352 +1416,1126 @@ static size_t sizeInBytes(const Vector& vector) return vector.capacity() * sizeof(T); } -void CodeBlock::dumpStatistics() +CodeBlock::CodeBlock(CopyParsedBlockTag, CodeBlock& other) + : m_globalObject(other.m_globalObject) + , m_heap(other.m_heap) + , m_numCalleeRegisters(other.m_numCalleeRegisters) + , m_numVars(other.m_numVars) + , m_isConstructor(other.m_isConstructor) + , m_shouldAlwaysBeInlined(true) + , m_didFailFTLCompilation(false) + , m_hasBeenCompiledWithFTL(false) + , m_unlinkedCode(*other.m_vm, other.m_ownerExecutable.get(), other.m_unlinkedCode.get()) + , m_hasDebuggerStatement(false) + , m_steppingMode(SteppingModeDisabled) + , m_numBreakpoints(0) + , m_ownerExecutable(*other.m_vm, other.m_ownerExecutable.get(), other.m_ownerExecutable.get()) + , m_vm(other.m_vm) + , m_instructions(other.m_instructions) + , m_thisRegister(other.m_thisRegister) + , m_argumentsRegister(other.m_argumentsRegister) + , m_activationRegister(other.m_activationRegister) + , m_isStrictMode(other.m_isStrictMode) + , m_needsActivation(other.m_needsActivation) + , m_mayBeExecuting(false) + , m_visitAggregateHasBeenCalled(false) + , m_source(other.m_source) + , m_sourceOffset(other.m_sourceOffset) + , m_firstLineColumnOffset(other.m_firstLineColumnOffset) + , m_codeType(other.m_codeType) + , m_constantRegisters(other.m_constantRegisters) + , m_functionDecls(other.m_functionDecls) + , m_functionExprs(other.m_functionExprs) + , m_osrExitCounter(0) + , m_optimizationDelayCounter(0) + , m_reoptimizationRetryCounter(0) + , m_hash(other.m_hash) +#if ENABLE(JIT) + , m_capabilityLevelState(DFG::CapabilityLevelNotSet) +#endif { -#if DUMP_CODE_BLOCK_STATISTICS - #define DEFINE_VARS(name) size_t name##IsNotEmpty = 0; size_t name##TotalSize = 0; - FOR_EACH_MEMBER_VECTOR(DEFINE_VARS) - FOR_EACH_MEMBER_VECTOR_RARE_DATA(DEFINE_VARS) - FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(DEFINE_VARS) - #undef DEFINE_VARS + ASSERT(m_heap->isDeferred()); + + if (SymbolTable* symbolTable = other.symbolTable()) + m_symbolTable.set(*m_vm, m_ownerExecutable.get(), symbolTable); + + setNumParameters(other.numParameters()); + optimizeAfterWarmUp(); + jitAfterWarmUp(); + + if (other.m_rareData) { + createRareDataIfNecessary(); + + m_rareData->m_exceptionHandlers = other.m_rareData->m_exceptionHandlers; + m_rareData->m_constantBuffers = other.m_rareData->m_constantBuffers; + m_rareData->m_switchJumpTables = other.m_rareData->m_switchJumpTables; + m_rareData->m_stringSwitchJumpTables = other.m_rareData->m_stringSwitchJumpTables; + } + + m_heap->m_codeBlocks.add(this); + m_heap->reportExtraMemoryCost(sizeof(CodeBlock)); +} - // Non-vector data members - size_t evalCodeCacheIsNotEmpty = 0; +CodeBlock::CodeBlock(ScriptExecutable* ownerExecutable, UnlinkedCodeBlock* unlinkedCodeBlock, JSScope* scope, PassRefPtr sourceProvider, unsigned sourceOffset, unsigned firstLineColumnOffset) + : m_globalObject(scope->globalObject()->vm(), ownerExecutable, scope->globalObject()) + , m_heap(&m_globalObject->vm().heap) + , m_numCalleeRegisters(unlinkedCodeBlock->m_numCalleeRegisters) + , m_numVars(unlinkedCodeBlock->m_numVars) + , m_isConstructor(unlinkedCodeBlock->isConstructor()) + , m_shouldAlwaysBeInlined(true) + , m_didFailFTLCompilation(false) + , m_hasBeenCompiledWithFTL(false) + , m_unlinkedCode(m_globalObject->vm(), ownerExecutable, unlinkedCodeBlock) + , m_hasDebuggerStatement(false) + , m_steppingMode(SteppingModeDisabled) + , m_numBreakpoints(0) + , m_ownerExecutable(m_globalObject->vm(), ownerExecutable, ownerExecutable) + , m_vm(unlinkedCodeBlock->vm()) + , m_thisRegister(unlinkedCodeBlock->thisRegister()) + , m_argumentsRegister(unlinkedCodeBlock->argumentsRegister()) + , m_activationRegister(unlinkedCodeBlock->activationRegister()) + , m_isStrictMode(unlinkedCodeBlock->isStrictMode()) + , m_needsActivation(unlinkedCodeBlock->hasActivationRegister() && unlinkedCodeBlock->codeType() == FunctionCode) + , m_mayBeExecuting(false) + , m_visitAggregateHasBeenCalled(false) + , m_source(sourceProvider) + , m_sourceOffset(sourceOffset) + , m_firstLineColumnOffset(firstLineColumnOffset) + , m_codeType(unlinkedCodeBlock->codeType()) + , m_osrExitCounter(0) + , m_optimizationDelayCounter(0) + , m_reoptimizationRetryCounter(0) +#if ENABLE(JIT) + , m_capabilityLevelState(DFG::CapabilityLevelNotSet) +#endif +{ + ASSERT(m_heap->isDeferred()); - size_t symbolTableIsNotEmpty = 0; - size_t symbolTableTotalSize = 0; + bool didCloneSymbolTable = false; + + if (SymbolTable* symbolTable = unlinkedCodeBlock->symbolTable()) { + if (codeType() == FunctionCode && symbolTable->captureCount()) { + m_symbolTable.set(*m_vm, m_ownerExecutable.get(), symbolTable->cloneCapturedNames(*m_vm)); + didCloneSymbolTable = true; + } else + m_symbolTable.set(*m_vm, m_ownerExecutable.get(), symbolTable); + } + + ASSERT(m_source); + setNumParameters(unlinkedCodeBlock->numParameters()); + + setConstantRegisters(unlinkedCodeBlock->constantRegisters()); + if (unlinkedCodeBlock->usesGlobalObject()) + m_constantRegisters[unlinkedCodeBlock->globalObjectRegister().toConstantIndex()].set(*m_vm, ownerExecutable, m_globalObject.get()); + m_functionDecls.resizeToFit(unlinkedCodeBlock->numberOfFunctionDecls()); + for (size_t count = unlinkedCodeBlock->numberOfFunctionDecls(), i = 0; i < count; ++i) { + UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionDecl(i); + unsigned lineCount = unlinkedExecutable->lineCount(); + unsigned firstLine = ownerExecutable->lineNo() + unlinkedExecutable->firstLineOffset(); + bool startColumnIsOnOwnerStartLine = !unlinkedExecutable->firstLineOffset(); + unsigned startColumn = unlinkedExecutable->unlinkedBodyStartColumn() + (startColumnIsOnOwnerStartLine ? ownerExecutable->startColumn() : 1); + bool endColumnIsOnStartLine = !lineCount; + unsigned endColumn = unlinkedExecutable->unlinkedBodyEndColumn() + (endColumnIsOnStartLine ? startColumn : 1); + unsigned startOffset = sourceOffset + unlinkedExecutable->startOffset(); + unsigned sourceLength = unlinkedExecutable->sourceLength(); + SourceCode code(m_source, startOffset, startOffset + sourceLength, firstLine, startColumn); + FunctionExecutable* executable = FunctionExecutable::create(*m_vm, code, unlinkedExecutable, firstLine, firstLine + lineCount, startColumn, endColumn); + m_functionDecls[i].set(*m_vm, ownerExecutable, executable); + } - size_t hasExceptionInfo = 0; - size_t hasRareData = 0; + m_functionExprs.resizeToFit(unlinkedCodeBlock->numberOfFunctionExprs()); + for (size_t count = unlinkedCodeBlock->numberOfFunctionExprs(), i = 0; i < count; ++i) { + UnlinkedFunctionExecutable* unlinkedExecutable = unlinkedCodeBlock->functionExpr(i); + unsigned lineCount = unlinkedExecutable->lineCount(); + unsigned firstLine = ownerExecutable->lineNo() + unlinkedExecutable->firstLineOffset(); + bool startColumnIsOnOwnerStartLine = !unlinkedExecutable->firstLineOffset(); + unsigned startColumn = unlinkedExecutable->unlinkedBodyStartColumn() + (startColumnIsOnOwnerStartLine ? ownerExecutable->startColumn() : 1); + bool endColumnIsOnStartLine = !lineCount; + unsigned endColumn = unlinkedExecutable->unlinkedBodyEndColumn() + (endColumnIsOnStartLine ? startColumn : 1); + unsigned startOffset = sourceOffset + unlinkedExecutable->startOffset(); + unsigned sourceLength = unlinkedExecutable->sourceLength(); + SourceCode code(m_source, startOffset, startOffset + sourceLength, firstLine, startColumn); + FunctionExecutable* executable = FunctionExecutable::create(*m_vm, code, unlinkedExecutable, firstLine, firstLine + lineCount, startColumn, endColumn); + m_functionExprs[i].set(*m_vm, ownerExecutable, executable); + } - size_t isFunctionCode = 0; - size_t isGlobalCode = 0; - size_t isEvalCode = 0; + if (unlinkedCodeBlock->hasRareData()) { + createRareDataIfNecessary(); + if (size_t count = unlinkedCodeBlock->constantBufferCount()) { + m_rareData->m_constantBuffers.grow(count); + for (size_t i = 0; i < count; i++) { + const UnlinkedCodeBlock::ConstantBuffer& buffer = unlinkedCodeBlock->constantBuffer(i); + m_rareData->m_constantBuffers[i] = buffer; + } + } + if (size_t count = unlinkedCodeBlock->numberOfExceptionHandlers()) { + m_rareData->m_exceptionHandlers.resizeToFit(count); + size_t nonLocalScopeDepth = scope->depth(); + for (size_t i = 0; i < count; i++) { + const UnlinkedHandlerInfo& handler = unlinkedCodeBlock->exceptionHandler(i); + m_rareData->m_exceptionHandlers[i].start = handler.start; + m_rareData->m_exceptionHandlers[i].end = handler.end; + m_rareData->m_exceptionHandlers[i].target = handler.target; + m_rareData->m_exceptionHandlers[i].scopeDepth = nonLocalScopeDepth + handler.scopeDepth; +#if ENABLE(JIT) + m_rareData->m_exceptionHandlers[i].nativeCode = CodeLocationLabel(MacroAssemblerCodePtr::createFromExecutableAddress(LLInt::getCodePtr(op_catch))); +#endif + } + } - HashSet::const_iterator end = liveCodeBlockSet.end(); - for (HashSet::const_iterator it = liveCodeBlockSet.begin(); it != end; ++it) { - CodeBlock* codeBlock = *it; + if (size_t count = unlinkedCodeBlock->numberOfStringSwitchJumpTables()) { + m_rareData->m_stringSwitchJumpTables.grow(count); + for (size_t i = 0; i < count; i++) { + UnlinkedStringJumpTable::StringOffsetTable::iterator ptr = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.begin(); + UnlinkedStringJumpTable::StringOffsetTable::iterator end = unlinkedCodeBlock->stringSwitchJumpTable(i).offsetTable.end(); + for (; ptr != end; ++ptr) { + OffsetLocation offset; + offset.branchOffset = ptr->value; + m_rareData->m_stringSwitchJumpTables[i].offsetTable.add(ptr->key, offset); + } + } + } - #define GET_STATS(name) if (!codeBlock->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_##name); } - FOR_EACH_MEMBER_VECTOR(GET_STATS) - #undef GET_STATS + if (size_t count = unlinkedCodeBlock->numberOfSwitchJumpTables()) { + m_rareData->m_switchJumpTables.grow(count); + for (size_t i = 0; i < count; i++) { + UnlinkedSimpleJumpTable& sourceTable = unlinkedCodeBlock->switchJumpTable(i); + SimpleJumpTable& destTable = m_rareData->m_switchJumpTables[i]; + destTable.branchOffsets = sourceTable.branchOffsets; + destTable.min = sourceTable.min; + } + } + } - if (!codeBlock->m_symbolTable.isEmpty()) { - symbolTableIsNotEmpty++; - symbolTableTotalSize += (codeBlock->m_symbolTable.capacity() * (sizeof(SymbolTable::KeyType) + sizeof(SymbolTable::MappedType))); + // Allocate metadata buffers for the bytecode + if (size_t size = unlinkedCodeBlock->numberOfLLintCallLinkInfos()) + m_llintCallLinkInfos.resizeToFit(size); + if (size_t size = unlinkedCodeBlock->numberOfArrayProfiles()) + m_arrayProfiles.grow(size); + if (size_t size = unlinkedCodeBlock->numberOfArrayAllocationProfiles()) + m_arrayAllocationProfiles.resizeToFit(size); + if (size_t size = unlinkedCodeBlock->numberOfValueProfiles()) + m_valueProfiles.resizeToFit(size); + if (size_t size = unlinkedCodeBlock->numberOfObjectAllocationProfiles()) + m_objectAllocationProfiles.resizeToFit(size); + + // Copy and translate the UnlinkedInstructions + unsigned instructionCount = unlinkedCodeBlock->instructions().count(); + UnlinkedInstructionStream::Reader instructionReader(unlinkedCodeBlock->instructions()); + + Vector instructions(instructionCount); + for (unsigned i = 0; !instructionReader.atEnd(); ) { + const UnlinkedInstruction* pc = instructionReader.next(); + + unsigned opLength = opcodeLength(pc[0].u.opcode); + + instructions[i] = vm()->interpreter->getOpcode(pc[0].u.opcode); + for (size_t j = 1; j < opLength; ++j) { + if (sizeof(int32_t) != sizeof(intptr_t)) + instructions[i + j].u.pointer = 0; + instructions[i + j].u.operand = pc[j].u.operand; + } + switch (pc[0].u.opcode) { + case op_call_varargs: + case op_construct_varargs: + case op_get_by_val: + case op_get_argument_by_val: { + int arrayProfileIndex = pc[opLength - 2].u.operand; + m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i); + + instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex]; + FALLTHROUGH; + } + case op_get_by_id: { + ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand]; + ASSERT(profile->m_bytecodeOffset == -1); + profile->m_bytecodeOffset = i; + instructions[i + opLength - 1] = profile; + break; + } + case op_put_by_val: { + int arrayProfileIndex = pc[opLength - 1].u.operand; + m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i); + instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex]; + break; + } + case op_put_by_val_direct: { + int arrayProfileIndex = pc[opLength - 1].u.operand; + m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i); + instructions[i + opLength - 1] = &m_arrayProfiles[arrayProfileIndex]; + break; } - if (codeBlock->m_exceptionInfo) { - hasExceptionInfo++; - #define GET_STATS(name) if (!codeBlock->m_exceptionInfo->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_exceptionInfo->m_##name); } - FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(GET_STATS) - #undef GET_STATS + case op_new_array: + case op_new_array_buffer: + case op_new_array_with_size: { + int arrayAllocationProfileIndex = pc[opLength - 1].u.operand; + instructions[i + opLength - 1] = &m_arrayAllocationProfiles[arrayAllocationProfileIndex]; + break; } + case op_new_object: { + int objectAllocationProfileIndex = pc[opLength - 1].u.operand; + ObjectAllocationProfile* objectAllocationProfile = &m_objectAllocationProfiles[objectAllocationProfileIndex]; + int inferredInlineCapacity = pc[opLength - 2].u.operand; - if (codeBlock->m_rareData) { - hasRareData++; - #define GET_STATS(name) if (!codeBlock->m_rareData->m_##name.isEmpty()) { name##IsNotEmpty++; name##TotalSize += sizeInBytes(codeBlock->m_rareData->m_##name); } - FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_STATS) - #undef GET_STATS + instructions[i + opLength - 1] = objectAllocationProfile; + objectAllocationProfile->initialize(*vm(), + m_ownerExecutable.get(), m_globalObject->objectPrototype(), inferredInlineCapacity); + break; + } - if (!codeBlock->m_rareData->m_evalCodeCache.isEmpty()) - evalCodeCacheIsNotEmpty++; + case op_call: + case op_call_eval: { + ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand]; + ASSERT(profile->m_bytecodeOffset == -1); + profile->m_bytecodeOffset = i; + instructions[i + opLength - 1] = profile; + int arrayProfileIndex = pc[opLength - 2].u.operand; + m_arrayProfiles[arrayProfileIndex] = ArrayProfile(i); + instructions[i + opLength - 2] = &m_arrayProfiles[arrayProfileIndex]; + instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand]; + break; + } + case op_construct: { + instructions[i + 5] = &m_llintCallLinkInfos[pc[5].u.operand]; + ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand]; + ASSERT(profile->m_bytecodeOffset == -1); + profile->m_bytecodeOffset = i; + instructions[i + opLength - 1] = profile; + break; } + case op_get_by_id_out_of_line: + case op_get_array_length: + CRASH(); - switch (codeBlock->codeType()) { - case FunctionCode: - ++isFunctionCode; - break; - case GlobalCode: - ++isGlobalCode; - break; - case EvalCode: - ++isEvalCode; + case op_init_global_const_nop: { + ASSERT(codeType() == GlobalCode); + Identifier ident = identifier(pc[4].u.operand); + SymbolTableEntry entry = m_globalObject->symbolTable()->get(ident.impl()); + if (entry.isNull()) break; + + instructions[i + 0] = vm()->interpreter->getOpcode(op_init_global_const); + instructions[i + 1] = &m_globalObject->registerAt(entry.getIndex()); + break; } - } - size_t totalSize = 0; + case op_resolve_scope: { + const Identifier& ident = identifier(pc[2].u.operand); + ResolveType type = static_cast(pc[3].u.operand); + + ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), scope, ident, Get, type); + instructions[i + 3].u.operand = op.type; + instructions[i + 4].u.operand = op.depth; + if (op.activation) + instructions[i + 5].u.activation.set(*vm(), ownerExecutable, op.activation); + break; + } + + case op_get_from_scope: { + ValueProfile* profile = &m_valueProfiles[pc[opLength - 1].u.operand]; + ASSERT(profile->m_bytecodeOffset == -1); + profile->m_bytecodeOffset = i; + instructions[i + opLength - 1] = profile; + + // get_from_scope dst, scope, id, ResolveModeAndType, Structure, Operand + const Identifier& ident = identifier(pc[3].u.operand); + ResolveModeAndType modeAndType = ResolveModeAndType(pc[4].u.operand); + ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), scope, ident, Get, modeAndType.type()); - #define GET_TOTAL_SIZE(name) totalSize += name##TotalSize; - FOR_EACH_MEMBER_VECTOR(GET_TOTAL_SIZE) - FOR_EACH_MEMBER_VECTOR_RARE_DATA(GET_TOTAL_SIZE) - FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(GET_TOTAL_SIZE) - #undef GET_TOTAL_SIZE + instructions[i + 4].u.operand = ResolveModeAndType(modeAndType.mode(), op.type).operand(); + if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks) + instructions[i + 5].u.watchpointSet = op.watchpointSet; + else if (op.structure) + instructions[i + 5].u.structure.set(*vm(), ownerExecutable, op.structure); + instructions[i + 6].u.pointer = reinterpret_cast(op.operand); + break; + } - totalSize += symbolTableTotalSize; - totalSize += (liveCodeBlockSet.size() * sizeof(CodeBlock)); + case op_put_to_scope: { + // put_to_scope scope, id, value, ResolveModeAndType, Structure, Operand + const Identifier& ident = identifier(pc[2].u.operand); + ResolveModeAndType modeAndType = ResolveModeAndType(pc[4].u.operand); + ResolveOp op = JSScope::abstractResolve(m_globalObject->globalExec(), scope, ident, Put, modeAndType.type()); - printf("Number of live CodeBlocks: %d\n", liveCodeBlockSet.size()); - printf("Size of a single CodeBlock [sizeof(CodeBlock)]: %zu\n", sizeof(CodeBlock)); - printf("Size of all CodeBlocks: %zu\n", totalSize); - printf("Average size of a CodeBlock: %zu\n", totalSize / liveCodeBlockSet.size()); + instructions[i + 4].u.operand = ResolveModeAndType(modeAndType.mode(), op.type).operand(); + if (op.type == GlobalVar || op.type == GlobalVarWithVarInjectionChecks) + instructions[i + 5].u.watchpointSet = op.watchpointSet; + else if (op.type == ClosureVar || op.type == ClosureVarWithVarInjectionChecks) { + if (op.watchpointSet) + op.watchpointSet->invalidate(); + } else if (op.structure) + instructions[i + 5].u.structure.set(*vm(), ownerExecutable, op.structure); + instructions[i + 6].u.pointer = reinterpret_cast(op.operand); + break; + } + + case op_captured_mov: + case op_new_captured_func: { + if (pc[3].u.index == UINT_MAX) { + instructions[i + 3].u.watchpointSet = 0; + break; + } + StringImpl* uid = identifier(pc[3].u.index).impl(); + RELEASE_ASSERT(didCloneSymbolTable); + ConcurrentJITLocker locker(m_symbolTable->m_lock); + SymbolTable::Map::iterator iter = m_symbolTable->find(locker, uid); + ASSERT(iter != m_symbolTable->end(locker)); + iter->value.prepareToWatch(symbolTable()); + instructions[i + 3].u.watchpointSet = iter->value.watchpointSet(); + break; + } - printf("Number of FunctionCode CodeBlocks: %zu (%.3f%%)\n", isFunctionCode, static_cast(isFunctionCode) * 100.0 / liveCodeBlockSet.size()); - printf("Number of GlobalCode CodeBlocks: %zu (%.3f%%)\n", isGlobalCode, static_cast(isGlobalCode) * 100.0 / liveCodeBlockSet.size()); - printf("Number of EvalCode CodeBlocks: %zu (%.3f%%)\n", isEvalCode, static_cast(isEvalCode) * 100.0 / liveCodeBlockSet.size()); + case op_debug: { + if (pc[1].u.index == DidReachBreakpoint) + m_hasDebuggerStatement = true; + break; + } - printf("Number of CodeBlocks with exception info: %zu (%.3f%%)\n", hasExceptionInfo, static_cast(hasExceptionInfo) * 100.0 / liveCodeBlockSet.size()); - printf("Number of CodeBlocks with rare data: %zu (%.3f%%)\n", hasRareData, static_cast(hasRareData) * 100.0 / liveCodeBlockSet.size()); + default: + break; + } + i += opLength; + } + m_instructions = WTF::RefCountedArray(instructions); - #define PRINT_STATS(name) printf("Number of CodeBlocks with " #name ": %zu\n", name##IsNotEmpty); printf("Size of all " #name ": %zu\n", name##TotalSize); - FOR_EACH_MEMBER_VECTOR(PRINT_STATS) - FOR_EACH_MEMBER_VECTOR_RARE_DATA(PRINT_STATS) - FOR_EACH_MEMBER_VECTOR_EXCEPTION_INFO(PRINT_STATS) - #undef PRINT_STATS + // Set optimization thresholds only after m_instructions is initialized, since these + // rely on the instruction count (and are in theory permitted to also inspect the + // instruction stream to more accurate assess the cost of tier-up). + optimizeAfterWarmUp(); + jitAfterWarmUp(); - printf("Number of CodeBlocks with evalCodeCache: %zu\n", evalCodeCacheIsNotEmpty); - printf("Number of CodeBlocks with symbolTable: %zu\n", symbolTableIsNotEmpty); + // If the concurrent thread will want the code block's hash, then compute it here + // synchronously. + if (Options::alwaysComputeHash()) + hash(); - printf("Size of all symbolTables: %zu\n", symbolTableTotalSize); + if (Options::dumpGeneratedBytecodes()) + dumpBytecode(); + + m_heap->m_codeBlocks.add(this); + m_heap->reportExtraMemoryCost(sizeof(CodeBlock) + m_instructions.size() * sizeof(Instruction)); +} -#else - printf("Dumping CodeBlock statistics is not enabled.\n"); +CodeBlock::~CodeBlock() +{ + if (m_vm->m_perBytecodeProfiler) + m_vm->m_perBytecodeProfiler->notifyDestruction(this); + +#if ENABLE(VERBOSE_VALUE_PROFILE) + dumpValueProfiles(); #endif -} + while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end()) + m_incomingLLIntCalls.begin()->remove(); +#if ENABLE(JIT) + // We may be destroyed before any CodeBlocks that refer to us are destroyed. + // Consider that two CodeBlocks become unreachable at the same time. There + // is no guarantee about the order in which the CodeBlocks are destroyed. + // So, if we don't remove incoming calls, and get destroyed before the + // CodeBlock(s) that have calls into us, then the CallLinkInfo vector's + // destructor will try to remove nodes from our (no longer valid) linked list. + while (m_incomingCalls.begin() != m_incomingCalls.end()) + m_incomingCalls.begin()->remove(); + + // Note that our outgoing calls will be removed from other CodeBlocks' + // m_incomingCalls linked lists through the execution of the ~CallLinkInfo + // destructors. + for (Bag::iterator iter = m_stubInfos.begin(); !!iter; ++iter) + (*iter)->deref(); +#endif // ENABLE(JIT) +} -CodeBlock::CodeBlock(ScopeNode* ownerNode, CodeType codeType, PassRefPtr sourceProvider, unsigned sourceOffset) - : m_numCalleeRegisters(0) - , m_numConstants(0) - , m_numVars(0) - , m_numParameters(0) - , m_ownerNode(ownerNode) - , m_globalData(0) -#ifndef NDEBUG - , m_instructionCount(0) -#endif - , m_needsFullScopeChain(ownerNode->needsActivation()) - , m_usesEval(ownerNode->usesEval()) - , m_isNumericCompareFunction(false) - , m_codeType(codeType) - , m_source(sourceProvider) - , m_sourceOffset(sourceOffset) - , m_exceptionInfo(new ExceptionInfo) +void CodeBlock::setNumParameters(int newValue) { - ASSERT(m_source); + m_numParameters = newValue; -#if DUMP_CODE_BLOCK_STATISTICS - liveCodeBlockSet.add(this); -#endif + m_argumentValueProfiles.resizeToFit(newValue); } -CodeBlock::~CodeBlock() +void EvalCodeCache::visitAggregate(SlotVisitor& visitor) { -#if !ENABLE(JIT) - for (size_t size = m_globalResolveInstructions.size(), i = 0; i < size; ++i) - derefStructures(&m_instructions[m_globalResolveInstructions[i]]); + EvalCacheMap::iterator end = m_cacheMap.end(); + for (EvalCacheMap::iterator ptr = m_cacheMap.begin(); ptr != end; ++ptr) + visitor.append(&ptr->value); +} - for (size_t size = m_propertyAccessInstructions.size(), i = 0; i < size; ++i) - derefStructures(&m_instructions[m_propertyAccessInstructions[i]]); -#else - for (size_t size = m_globalResolveInfos.size(), i = 0; i < size; ++i) { - if (m_globalResolveInfos[i].structure) - m_globalResolveInfos[i].structure->deref(); +CodeBlock* CodeBlock::specialOSREntryBlockOrNull() +{ +#if ENABLE(FTL_JIT) + if (jitType() != JITCode::DFGJIT) + return 0; + DFG::JITCode* jitCode = m_jitCode->dfg(); + return jitCode->osrEntryBlock.get(); +#else // ENABLE(FTL_JIT) + return 0; +#endif // ENABLE(FTL_JIT) +} + +void CodeBlock::visitAggregate(SlotVisitor& visitor) +{ +#if ENABLE(PARALLEL_GC) + // I may be asked to scan myself more than once, and it may even happen concurrently. + // To this end, use a CAS loop to check if I've been called already. Only one thread + // may proceed past this point - whichever one wins the CAS race. + unsigned oldValue; + do { + oldValue = m_visitAggregateHasBeenCalled; + if (oldValue) { + // Looks like someone else won! Return immediately to ensure that we don't + // trace the same CodeBlock concurrently. Doing so is hazardous since we will + // be mutating the state of ValueProfiles, which contain JSValues, which can + // have word-tearing on 32-bit, leading to awesome timing-dependent crashes + // that are nearly impossible to track down. + + // Also note that it must be safe to return early as soon as we see the + // value true (well, (unsigned)1), since once a GC thread is in this method + // and has won the CAS race (i.e. was responsible for setting the value true) + // it will definitely complete the rest of this method before declaring + // termination. + return; + } + } while (!WTF::weakCompareAndSwap(&m_visitAggregateHasBeenCalled, 0, 1)); +#endif // ENABLE(PARALLEL_GC) + + if (!!m_alternative) + m_alternative->visitAggregate(visitor); + + if (CodeBlock* otherBlock = specialOSREntryBlockOrNull()) + otherBlock->visitAggregate(visitor); + + visitor.reportExtraMemoryUsage(ownerExecutable(), sizeof(CodeBlock)); + if (m_jitCode) + visitor.reportExtraMemoryUsage(ownerExecutable(), m_jitCode->size()); + if (m_instructions.size()) { + // Divide by refCount() because m_instructions points to something that is shared + // by multiple CodeBlocks, and we only want to count it towards the heap size once. + // Having each CodeBlock report only its proportional share of the size is one way + // of accomplishing this. + visitor.reportExtraMemoryUsage(ownerExecutable(), m_instructions.size() * sizeof(Instruction) / m_instructions.refCount()); } - for (size_t size = m_structureStubInfos.size(), i = 0; i < size; ++i) - m_structureStubInfos[i].deref(); + visitor.append(&m_unlinkedCode); - for (size_t size = m_callLinkInfos.size(), i = 0; i < size; ++i) { - CallLinkInfo* callLinkInfo = &m_callLinkInfos[i]; - if (callLinkInfo->isLinked()) - callLinkInfo->callee->removeCaller(callLinkInfo); + // There are three things that may use unconditional finalizers: lazy bytecode freeing, + // inline cache clearing, and jettisoning. The probability of us wanting to do at + // least one of those things is probably quite close to 1. So we add one no matter what + // and when it runs, it figures out whether it has any work to do. + visitor.addUnconditionalFinalizer(this); + + m_allTransitionsHaveBeenMarked = false; + + if (shouldImmediatelyAssumeLivenessDuringScan()) { + // This code block is live, so scan all references strongly and return. + stronglyVisitStrongReferences(visitor); + stronglyVisitWeakReferences(visitor); + propagateTransitions(visitor); + return; } + + // There are two things that we use weak reference harvesters for: DFG fixpoint for + // jettisoning, and trying to find structures that would be live based on some + // inline cache. So it makes sense to register them regardless. + visitor.addWeakReferenceHarvester(this); + +#if ENABLE(DFG_JIT) + // We get here if we're live in the sense that our owner executable is live, + // but we're not yet live for sure in another sense: we may yet decide that this + // code block should be jettisoned based on its outgoing weak references being + // stale. Set a flag to indicate that we're still assuming that we're dead, and + // perform one round of determining if we're live. The GC may determine, based on + // either us marking additional objects, or by other objects being marked for + // other reasons, that this iteration should run again; it will notify us of this + // decision by calling harvestWeakReferences(). + + m_jitCode->dfgCommon()->livenessHasBeenProved = false; + + propagateTransitions(visitor); + determineLiveness(visitor); +#else // ENABLE(DFG_JIT) + RELEASE_ASSERT_NOT_REACHED(); +#endif // ENABLE(DFG_JIT) +} - unlinkCallers(); -#endif - -#if DUMP_CODE_BLOCK_STATISTICS - liveCodeBlockSet.remove(this); +bool CodeBlock::shouldImmediatelyAssumeLivenessDuringScan() +{ +#if ENABLE(DFG_JIT) + // Interpreter and Baseline JIT CodeBlocks don't need to be jettisoned when + // their weak references go stale. So if a basline JIT CodeBlock gets + // scanned, we can assume that this means that it's live. + if (!JITCode::isOptimizingJIT(jitType())) + return true; + + // For simplicity, we don't attempt to jettison code blocks during GC if + // they are executing. Instead we strongly mark their weak references to + // allow them to continue to execute soundly. + if (m_mayBeExecuting) + return true; + + if (Options::forceDFGCodeBlockLiveness()) + return true; + + return false; +#else + return true; #endif } -#if ENABLE(JIT) -void CodeBlock::unlinkCallers() +bool CodeBlock::isKnownToBeLiveDuringGC() { - size_t size = m_linkedCallerList.size(); - for (size_t i = 0; i < size; ++i) { - CallLinkInfo* currentCaller = m_linkedCallerList[i]; - JIT::unlinkCall(currentCaller); - currentCaller->setUnlinked(); - } - m_linkedCallerList.clear(); -} +#if ENABLE(DFG_JIT) + // This should return true for: + // - Code blocks that behave like normal objects - i.e. if they are referenced then they + // are live. + // - Code blocks that were running on the stack. + // - Code blocks that survived the last GC if the current GC is an Eden GC. This is + // because either livenessHasBeenProved would have survived as true or m_mayBeExecuting + // would survive as true. + // - Code blocks that don't have any dead weak references. + + return shouldImmediatelyAssumeLivenessDuringScan() + || m_jitCode->dfgCommon()->livenessHasBeenProved; +#else + return true; #endif +} -void CodeBlock::derefStructures(Instruction* vPC) const +void CodeBlock::propagateTransitions(SlotVisitor& visitor) { - Interpreter* interpreter = m_globalData->interpreter; + UNUSED_PARAM(visitor); - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) { - vPC[4].u.structure->deref(); - return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) { - vPC[4].u.structure->deref(); - vPC[5].u.structure->deref(); + if (m_allTransitionsHaveBeenMarked) return; + + bool allAreMarkedSoFar = true; + + Interpreter* interpreter = m_vm->interpreter; + if (jitType() == JITCode::InterpreterThunk) { + const Vector& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions(); + for (size_t i = 0; i < propertyAccessInstructions.size(); ++i) { + Instruction* instruction = &instructions()[propertyAccessInstructions[i]]; + switch (interpreter->getOpcodeID(instruction[0].u.opcode)) { + case op_put_by_id_transition_direct: + case op_put_by_id_transition_normal: + case op_put_by_id_transition_direct_out_of_line: + case op_put_by_id_transition_normal_out_of_line: { + if (Heap::isMarked(instruction[4].u.structure.get())) + visitor.append(&instruction[6].u.structure); + else + allAreMarkedSoFar = false; + break; + } + default: + break; + } + } } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) { - vPC[4].u.structure->deref(); - vPC[5].u.structureChain->deref(); - return; + +#if ENABLE(JIT) + if (JITCode::isJIT(jitType())) { + for (Bag::iterator iter = m_stubInfos.begin(); !!iter; ++iter) { + StructureStubInfo& stubInfo = **iter; + switch (stubInfo.accessType) { + case access_put_by_id_transition_normal: + case access_put_by_id_transition_direct: { + JSCell* origin = stubInfo.codeOrigin.codeOriginOwner(); + if ((!origin || Heap::isMarked(origin)) + && Heap::isMarked(stubInfo.u.putByIdTransition.previousStructure.get())) + visitor.append(&stubInfo.u.putByIdTransition.structure); + else + allAreMarkedSoFar = false; + break; + } + + case access_put_by_id_list: { + PolymorphicPutByIdList* list = stubInfo.u.putByIdList.list; + JSCell* origin = stubInfo.codeOrigin.codeOriginOwner(); + if (origin && !Heap::isMarked(origin)) { + allAreMarkedSoFar = false; + break; + } + for (unsigned j = list->size(); j--;) { + PutByIdAccess& access = list->m_list[j]; + if (!access.isTransition()) + continue; + if (Heap::isMarked(access.oldStructure())) + visitor.append(&access.m_newStructure); + else + allAreMarkedSoFar = false; + } + break; + } + + default: + break; + } + } } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) { - vPC[4].u.structure->deref(); - vPC[5].u.structure->deref(); - vPC[6].u.structureChain->deref(); - return; +#endif // ENABLE(JIT) + +#if ENABLE(DFG_JIT) + if (JITCode::isOptimizingJIT(jitType())) { + DFG::CommonData* dfgCommon = m_jitCode->dfgCommon(); + for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) { + if ((!dfgCommon->transitions[i].m_codeOrigin + || Heap::isMarked(dfgCommon->transitions[i].m_codeOrigin.get())) + && Heap::isMarked(dfgCommon->transitions[i].m_from.get())) { + // If the following three things are live, then the target of the + // transition is also live: + // - This code block. We know it's live already because otherwise + // we wouldn't be scanning ourselves. + // - The code origin of the transition. Transitions may arise from + // code that was inlined. They are not relevant if the user's + // object that is required for the inlinee to run is no longer + // live. + // - The source of the transition. The transition checks if some + // heap location holds the source, and if so, stores the target. + // Hence the source must be live for the transition to be live. + visitor.append(&dfgCommon->transitions[i].m_to); + } else + allAreMarkedSoFar = false; + } } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) { - vPC[4].u.structure->deref(); +#endif // ENABLE(DFG_JIT) + + if (allAreMarkedSoFar) + m_allTransitionsHaveBeenMarked = true; +} + +void CodeBlock::determineLiveness(SlotVisitor& visitor) +{ + UNUSED_PARAM(visitor); + + if (shouldImmediatelyAssumeLivenessDuringScan()) return; - } - if (vPC[0].u.opcode == interpreter->getOpcode(op_resolve_global)) { - if(vPC[4].u.structure) - vPC[4].u.structure->deref(); + +#if ENABLE(DFG_JIT) + // Check if we have any remaining work to do. + DFG::CommonData* dfgCommon = m_jitCode->dfgCommon(); + if (dfgCommon->livenessHasBeenProved) return; + + // Now check all of our weak references. If all of them are live, then we + // have proved liveness and so we scan our strong references. If at end of + // GC we still have not proved liveness, then this code block is toast. + bool allAreLiveSoFar = true; + for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) { + if (!Heap::isMarked(dfgCommon->weakReferences[i].get())) { + allAreLiveSoFar = false; + break; + } } - if ((vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto_list)) - || (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self_list))) { - PolymorphicAccessStructureList* polymorphicStructures = vPC[4].u.polymorphicStructures; - polymorphicStructures->derefStructures(vPC[5].u.operand); - delete polymorphicStructures; + + // If some weak references are dead, then this fixpoint iteration was + // unsuccessful. + if (!allAreLiveSoFar) return; - } + + // All weak references are live. Record this information so we don't + // come back here again, and scan the strong references. + dfgCommon->livenessHasBeenProved = true; + stronglyVisitStrongReferences(visitor); +#endif // ENABLE(DFG_JIT) +} - // These instructions don't ref their Structures. - 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)); +void CodeBlock::visitWeakReferences(SlotVisitor& visitor) +{ + propagateTransitions(visitor); + determineLiveness(visitor); } -void CodeBlock::refStructures(Instruction* vPC) const +void CodeBlock::finalizeUnconditionally() { - Interpreter* interpreter = m_globalData->interpreter; + Interpreter* interpreter = m_vm->interpreter; + if (JITCode::couldBeInterpreted(jitType())) { + const Vector& propertyAccessInstructions = m_unlinkedCode->propertyAccessInstructions(); + for (size_t size = propertyAccessInstructions.size(), i = 0; i < size; ++i) { + Instruction* curInstruction = &instructions()[propertyAccessInstructions[i]]; + switch (interpreter->getOpcodeID(curInstruction[0].u.opcode)) { + case op_get_by_id: + case op_get_by_id_out_of_line: + case op_put_by_id: + case op_put_by_id_out_of_line: + if (!curInstruction[4].u.structure || Heap::isMarked(curInstruction[4].u.structure.get())) + break; + if (Options::verboseOSR()) + dataLogF("Clearing LLInt property access with structure %p.\n", curInstruction[4].u.structure.get()); + curInstruction[4].u.structure.clear(); + curInstruction[5].u.operand = 0; + break; + case op_put_by_id_transition_direct: + case op_put_by_id_transition_normal: + case op_put_by_id_transition_direct_out_of_line: + case op_put_by_id_transition_normal_out_of_line: + if (Heap::isMarked(curInstruction[4].u.structure.get()) + && Heap::isMarked(curInstruction[6].u.structure.get()) + && Heap::isMarked(curInstruction[7].u.structureChain.get())) + break; + if (Options::verboseOSR()) { + dataLogF("Clearing LLInt put transition with structures %p -> %p, chain %p.\n", + curInstruction[4].u.structure.get(), + curInstruction[6].u.structure.get(), + curInstruction[7].u.structureChain.get()); + } + curInstruction[4].u.structure.clear(); + curInstruction[6].u.structure.clear(); + curInstruction[7].u.structureChain.clear(); + curInstruction[0].u.opcode = interpreter->getOpcode(op_put_by_id); + break; + case op_get_array_length: + break; + case op_to_this: + if (!curInstruction[2].u.structure || Heap::isMarked(curInstruction[2].u.structure.get())) + break; + if (Options::verboseOSR()) + dataLogF("Clearing LLInt to_this with structure %p.\n", curInstruction[2].u.structure.get()); + curInstruction[2].u.structure.clear(); + break; + case op_get_callee: + if (!curInstruction[2].u.jsCell || Heap::isMarked(curInstruction[2].u.jsCell.get())) + break; + if (Options::verboseOSR()) + dataLogF("Clearing LLInt get callee with function %p.\n", curInstruction[2].u.jsCell.get()); + curInstruction[2].u.jsCell.clear(); + break; + case op_resolve_scope: { + WriteBarrierBase& activation = curInstruction[5].u.activation; + if (!activation || Heap::isMarked(activation.get())) + break; + if (Options::verboseOSR()) + dataLogF("Clearing dead activation %p.\n", activation.get()); + activation.clear(); + break; + } + case op_get_from_scope: + case op_put_to_scope: { + ResolveModeAndType modeAndType = + ResolveModeAndType(curInstruction[4].u.operand); + if (modeAndType.type() == GlobalVar || modeAndType.type() == GlobalVarWithVarInjectionChecks) + continue; + WriteBarrierBase& structure = curInstruction[5].u.structure; + if (!structure || Heap::isMarked(structure.get())) + break; + if (Options::verboseOSR()) + dataLogF("Clearing scope access with structure %p.\n", structure.get()); + structure.clear(); + break; + } + default: + RELEASE_ASSERT_NOT_REACHED(); + } + } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_self)) { - vPC[4].u.structure->ref(); - return; + for (unsigned i = 0; i < m_llintCallLinkInfos.size(); ++i) { + if (m_llintCallLinkInfos[i].isLinked() && !Heap::isMarked(m_llintCallLinkInfos[i].callee.get())) { + if (Options::verboseOSR()) + dataLog("Clearing LLInt call from ", *this, "\n"); + m_llintCallLinkInfos[i].unlink(); + } + if (!!m_llintCallLinkInfos[i].lastSeenCallee && !Heap::isMarked(m_llintCallLinkInfos[i].lastSeenCallee.get())) + m_llintCallLinkInfos[i].lastSeenCallee.clear(); + } } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_proto)) { - vPC[4].u.structure->ref(); - vPC[5].u.structure->ref(); + +#if ENABLE(DFG_JIT) + // Check if we're not live. If we are, then jettison. + if (!isKnownToBeLiveDuringGC()) { + if (Options::verboseOSR()) + dataLog(*this, " has dead weak references, jettisoning during GC.\n"); + + if (DFG::shouldShowDisassembly()) { + dataLog(*this, " will be jettisoned because of the following dead references:\n"); + DFG::CommonData* dfgCommon = m_jitCode->dfgCommon(); + for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) { + DFG::WeakReferenceTransition& transition = dfgCommon->transitions[i]; + JSCell* origin = transition.m_codeOrigin.get(); + JSCell* from = transition.m_from.get(); + JSCell* to = transition.m_to.get(); + if ((!origin || Heap::isMarked(origin)) && Heap::isMarked(from)) + continue; + dataLog(" Transition under ", RawPointer(origin), ", ", RawPointer(from), " -> ", RawPointer(to), ".\n"); + } + for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) { + JSCell* weak = dfgCommon->weakReferences[i].get(); + if (Heap::isMarked(weak)) + continue; + dataLog(" Weak reference ", RawPointer(weak), ".\n"); + } + } + + jettison(Profiler::JettisonDueToWeakReference); return; } - if (vPC[0].u.opcode == interpreter->getOpcode(op_get_by_id_chain)) { - vPC[4].u.structure->ref(); - vPC[5].u.structureChain->ref(); - return; +#endif // ENABLE(DFG_JIT) + +#if ENABLE(JIT) + // Handle inline caches. + if (!!jitCode()) { + RepatchBuffer repatchBuffer(this); + + for (auto iter = callLinkInfosBegin(); !!iter; ++iter) + (*iter)->visitWeak(repatchBuffer); + + for (Bag::iterator iter = m_stubInfos.begin(); !!iter; ++iter) { + StructureStubInfo& stubInfo = **iter; + + if (stubInfo.visitWeakReferences(repatchBuffer)) + continue; + + resetStubDuringGCInternal(repatchBuffer, stubInfo); + } } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_transition)) { - vPC[4].u.structure->ref(); - vPC[5].u.structure->ref(); - vPC[6].u.structureChain->ref(); +#endif +} + +void CodeBlock::getStubInfoMap(const ConcurrentJITLocker&, StubInfoMap& result) +{ +#if ENABLE(JIT) + toHashMap(m_stubInfos, getStructureStubInfoCodeOrigin, result); +#else + UNUSED_PARAM(result); +#endif +} + +void CodeBlock::getStubInfoMap(StubInfoMap& result) +{ + ConcurrentJITLocker locker(m_lock); + getStubInfoMap(locker, result); +} + +void CodeBlock::getCallLinkInfoMap(const ConcurrentJITLocker&, CallLinkInfoMap& result) +{ +#if ENABLE(JIT) + toHashMap(m_callLinkInfos, getCallLinkInfoCodeOrigin, result); +#else + UNUSED_PARAM(result); +#endif +} + +void CodeBlock::getCallLinkInfoMap(CallLinkInfoMap& result) +{ + ConcurrentJITLocker locker(m_lock); + getCallLinkInfoMap(locker, result); +} + +#if ENABLE(JIT) +StructureStubInfo* CodeBlock::addStubInfo() +{ + ConcurrentJITLocker locker(m_lock); + return m_stubInfos.add(); +} + +CallLinkInfo* CodeBlock::addCallLinkInfo() +{ + ConcurrentJITLocker locker(m_lock); + return m_callLinkInfos.add(); +} + +void CodeBlock::resetStub(StructureStubInfo& stubInfo) +{ + if (stubInfo.accessType == access_unset) return; + + ConcurrentJITLocker locker(m_lock); + + RepatchBuffer repatchBuffer(this); + resetStubInternal(repatchBuffer, stubInfo); +} + +void CodeBlock::resetStubInternal(RepatchBuffer& repatchBuffer, StructureStubInfo& stubInfo) +{ + AccessType accessType = static_cast(stubInfo.accessType); + + if (Options::verboseOSR()) { + // This can be called from GC destructor calls, so we don't try to do a full dump + // of the CodeBlock. + dataLog("Clearing structure cache (kind ", static_cast(stubInfo.accessType), ") in ", RawPointer(this), ".\n"); } - if (vPC[0].u.opcode == interpreter->getOpcode(op_put_by_id_replace)) { - vPC[4].u.structure->ref(); - return; + + RELEASE_ASSERT(JITCode::isJIT(jitType())); + + if (isGetByIdAccess(accessType)) + resetGetByID(repatchBuffer, stubInfo); + else if (isPutByIdAccess(accessType)) + resetPutByID(repatchBuffer, stubInfo); + else { + RELEASE_ASSERT(isInAccess(accessType)); + resetIn(repatchBuffer, stubInfo); } - // These instructions don't ref their Structures. - 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)); + stubInfo.reset(); } -void CodeBlock::mark() +void CodeBlock::resetStubDuringGCInternal(RepatchBuffer& repatchBuffer, StructureStubInfo& stubInfo) { - for (size_t i = 0; i < m_constantRegisters.size(); ++i) - if (!m_constantRegisters[i].marked()) - m_constantRegisters[i].mark(); - - for (size_t i = 0; i < m_functionExpressions.size(); ++i) - m_functionExpressions[i]->body()->mark(); + resetStubInternal(repatchBuffer, stubInfo); + stubInfo.resetByGC = true; +} - if (m_rareData) { - for (size_t i = 0; i < m_rareData->m_functions.size(); ++i) - m_rareData->m_functions[i]->body()->mark(); +CallLinkInfo* CodeBlock::getCallLinkInfoForBytecodeIndex(unsigned index) +{ + for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter) { + if ((*iter)->codeOrigin == CodeOrigin(index)) + return *iter; + } + return nullptr; +} +#endif - for (size_t i = 0; i < m_rareData->m_unexpectedConstants.size(); ++i) { - if (!m_rareData->m_unexpectedConstants[i].marked()) - m_rareData->m_unexpectedConstants[i].mark(); - } - m_rareData->m_evalCodeCache.mark(); +void CodeBlock::stronglyVisitStrongReferences(SlotVisitor& visitor) +{ + visitor.append(&m_globalObject); + visitor.append(&m_ownerExecutable); + visitor.append(&m_symbolTable); + visitor.append(&m_unlinkedCode); + if (m_rareData) + m_rareData->m_evalCodeCache.visitAggregate(visitor); + visitor.appendValues(m_constantRegisters.data(), m_constantRegisters.size()); + for (size_t i = 0; i < m_functionExprs.size(); ++i) + visitor.append(&m_functionExprs[i]); + for (size_t i = 0; i < m_functionDecls.size(); ++i) + visitor.append(&m_functionDecls[i]); + for (unsigned i = 0; i < m_objectAllocationProfiles.size(); ++i) + m_objectAllocationProfiles[i].visitAggregate(visitor); + +#if ENABLE(DFG_JIT) + if (JITCode::isOptimizingJIT(jitType())) { + DFG::CommonData* dfgCommon = m_jitCode->dfgCommon(); + if (dfgCommon->inlineCallFrames.get()) + dfgCommon->inlineCallFrames->visitAggregate(visitor); } +#endif + + updateAllPredictions(); } -void CodeBlock::reparseForExceptionInfoIfNecessary(CallFrame* callFrame) +void CodeBlock::stronglyVisitWeakReferences(SlotVisitor& visitor) { - if (m_exceptionInfo) + UNUSED_PARAM(visitor); + +#if ENABLE(DFG_JIT) + if (!JITCode::isOptimizingJIT(jitType())) return; + + DFG::CommonData* dfgCommon = m_jitCode->dfgCommon(); - ScopeChainNode* scopeChain = callFrame->scopeChain(); - if (m_needsFullScopeChain) { - ScopeChain sc(scopeChain); - int scopeDelta = sc.localDepth(); - if (m_codeType == EvalCode) - scopeDelta -= static_cast(this)->baseScopeDepth(); - else if (m_codeType == FunctionCode) - scopeDelta++; // Compilation of function code assumes activation is not on the scope chain yet. - ASSERT(scopeDelta >= 0); - while (scopeDelta--) - scopeChain = scopeChain->next; + for (unsigned i = 0; i < dfgCommon->transitions.size(); ++i) { + if (!!dfgCommon->transitions[i].m_codeOrigin) + visitor.append(&dfgCommon->transitions[i].m_codeOrigin); // Almost certainly not necessary, since the code origin should also be a weak reference. Better to be safe, though. + visitor.append(&dfgCommon->transitions[i].m_from); + visitor.append(&dfgCommon->transitions[i].m_to); } + + for (unsigned i = 0; i < dfgCommon->weakReferences.size(); ++i) + visitor.append(&dfgCommon->weakReferences[i]); +#endif +} - switch (m_codeType) { - case FunctionCode: { - FunctionBodyNode* ownerFunctionBodyNode = static_cast(m_ownerNode); - RefPtr newFunctionBody = m_globalData->parser->reparse(m_globalData, ownerFunctionBodyNode); - ASSERT(newFunctionBody); - newFunctionBody->finishParsing(ownerFunctionBodyNode->copyParameters(), ownerFunctionBodyNode->parameterCount()); - - m_globalData->scopeNodeBeingReparsed = newFunctionBody.get(); - - CodeBlock& newCodeBlock = newFunctionBody->bytecodeForExceptionInfoReparse(scopeChain, this); - ASSERT(newCodeBlock.m_exceptionInfo); - ASSERT(newCodeBlock.m_instructionCount == m_instructionCount); +CodeBlock* CodeBlock::baselineAlternative() +{ +#if ENABLE(JIT) + CodeBlock* result = this; + while (result->alternative()) + result = result->alternative(); + RELEASE_ASSERT(result); + RELEASE_ASSERT(JITCode::isBaselineCode(result->jitType()) || result->jitType() == JITCode::None); + return result; +#else + return this; +#endif +} +CodeBlock* CodeBlock::baselineVersion() +{ #if ENABLE(JIT) - JIT::compile(m_globalData, &newCodeBlock); - ASSERT(newCodeBlock.m_jitCode.codeSize == m_jitCode.codeSize); + if (JITCode::isBaselineCode(jitType())) + return this; + CodeBlock* result = replacement(); + if (!result) { + // This can happen if we're creating the original CodeBlock for an executable. + // Assume that we're the baseline CodeBlock. + RELEASE_ASSERT(jitType() == JITCode::None); + return this; + } + result = result->baselineAlternative(); + return result; +#else + return this; #endif +} - m_exceptionInfo.set(newCodeBlock.m_exceptionInfo.release()); +#if ENABLE(JIT) +bool CodeBlock::hasOptimizedReplacement(JITCode::JITType typeToReplace) +{ + return JITCode::isHigherTier(replacement()->jitType(), typeToReplace); +} - m_globalData->scopeNodeBeingReparsed = 0; +bool CodeBlock::hasOptimizedReplacement() +{ + return hasOptimizedReplacement(jitType()); +} +#endif - break; - } - case EvalCode: { - EvalNode* ownerEvalNode = static_cast(m_ownerNode); - RefPtr newEvalBody = m_globalData->parser->reparse(m_globalData, ownerEvalNode); +bool CodeBlock::isCaptured(VirtualRegister operand, InlineCallFrame* inlineCallFrame) const +{ + if (operand.isArgument()) + return operand.toArgument() && usesArguments(); - m_globalData->scopeNodeBeingReparsed = newEvalBody.get(); + if (inlineCallFrame) + return inlineCallFrame->capturedVars.get(operand.toLocal()); - EvalCodeBlock& newCodeBlock = newEvalBody->bytecodeForExceptionInfoReparse(scopeChain, this); - ASSERT(newCodeBlock.m_exceptionInfo); - ASSERT(newCodeBlock.m_instructionCount == m_instructionCount); + // The activation object isn't in the captured region, but it's "captured" + // in the sense that stores to its location can be observed indirectly. + if (needsActivation() && operand == activationRegister()) + return true; -#if ENABLE(JIT) - JIT::compile(m_globalData, &newCodeBlock); - ASSERT(newCodeBlock.m_jitCode.codeSize == m_jitCode.codeSize); -#endif + // Ditto for the arguments object. + if (usesArguments() && operand == argumentsRegister()) + return true; + if (usesArguments() && operand == unmodifiedArgumentsRegister(argumentsRegister())) + return true; - m_exceptionInfo.set(newCodeBlock.m_exceptionInfo.release()); + // We're in global code so there are no locals to capture + if (!symbolTable()) + return false; - m_globalData->scopeNodeBeingReparsed = 0; + return symbolTable()->isCaptured(operand.offset()); +} - break; - } - default: - // CodeBlocks for Global code blocks are transient and therefore to not gain from - // from throwing out there exception information. - ASSERT_NOT_REACHED(); - } +int CodeBlock::framePointerOffsetToGetActivationRegisters(int machineCaptureStart) +{ + // We'll be adding this to the stack pointer to get a registers pointer that looks + // like it would have looked in the baseline engine. For example, if bytecode would + // have put the first captured variable at offset -5 but we put it at offset -1, then + // we'll have an offset of 4. + int32_t offset = 0; + + // Compute where we put the captured variables. This offset will point the registers + // pointer directly at the first captured var. + offset += machineCaptureStart; + + // Now compute the offset needed to make the runtime see the captured variables at the + // same offset that the bytecode would have used. + offset -= symbolTable()->captureStart(); + + return offset; +} + +int CodeBlock::framePointerOffsetToGetActivationRegisters() +{ + if (!JITCode::isOptimizingJIT(jitType())) + return 0; +#if ENABLE(DFG_JIT) + return framePointerOffsetToGetActivationRegisters(jitCode()->dfgCommon()->machineCaptureStart); +#else + RELEASE_ASSERT_NOT_REACHED(); + return 0; +#endif } HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset) { - ASSERT(bytecodeOffset < m_instructionCount); + RELEASE_ASSERT(bytecodeOffset < instructions().size()); if (!m_rareData) return 0; @@ -1468,219 +2544,1060 @@ HandlerInfo* CodeBlock::handlerForBytecodeOffset(unsigned bytecodeOffset) for (size_t i = 0; i < exceptionHandlers.size(); ++i) { // Handlers are ordered innermost first, so the first handler we encounter // that contains the source address is the correct handler to use. - if (exceptionHandlers[i].start <= bytecodeOffset && exceptionHandlers[i].end >= bytecodeOffset) + if (exceptionHandlers[i].start <= bytecodeOffset && exceptionHandlers[i].end > bytecodeOffset) return &exceptionHandlers[i]; } return 0; } -int CodeBlock::lineNumberForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset) +unsigned CodeBlock::lineNumberForBytecodeOffset(unsigned bytecodeOffset) { - ASSERT(bytecodeOffset < m_instructionCount); + RELEASE_ASSERT(bytecodeOffset < instructions().size()); + return m_ownerExecutable->lineNo() + m_unlinkedCode->lineNumberForBytecodeOffset(bytecodeOffset); +} - reparseForExceptionInfoIfNecessary(callFrame); - ASSERT(m_exceptionInfo); +unsigned CodeBlock::columnNumberForBytecodeOffset(unsigned bytecodeOffset) +{ + int divot; + int startOffset; + int endOffset; + unsigned line; + unsigned column; + expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column); + return column; +} - if (!m_exceptionInfo->m_lineInfo.size()) - return m_ownerNode->source().firstLine(); // Empty function +void CodeBlock::expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column) +{ + m_unlinkedCode->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column); + divot += m_sourceOffset; + column += line ? 1 : firstLineColumnOffset(); + line += m_ownerExecutable->lineNo(); +} - int low = 0; - int high = m_exceptionInfo->m_lineInfo.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_exceptionInfo->m_lineInfo[mid].instructionOffset <= bytecodeOffset) - low = mid + 1; - else - high = mid; +bool CodeBlock::hasOpDebugForLineAndColumn(unsigned line, unsigned column) +{ + Interpreter* interpreter = vm()->interpreter; + const Instruction* begin = instructions().begin(); + const Instruction* end = instructions().end(); + for (const Instruction* it = begin; it != end;) { + OpcodeID opcodeID = interpreter->getOpcodeID(it->u.opcode); + if (opcodeID == op_debug) { + unsigned bytecodeOffset = it - begin; + int unused; + unsigned opDebugLine; + unsigned opDebugColumn; + expressionRangeForBytecodeOffset(bytecodeOffset, unused, unused, unused, opDebugLine, opDebugColumn); + if (line == opDebugLine && (column == Breakpoint::unspecifiedColumn || column == opDebugColumn)) + return true; + } + it += opcodeLengths[opcodeID]; } + return false; +} + +void CodeBlock::shrinkToFit(ShrinkMode shrinkMode) +{ + m_rareCaseProfiles.shrinkToFit(); + m_specialFastCaseProfiles.shrinkToFit(); - if (!low) - return m_ownerNode->source().firstLine(); - return m_exceptionInfo->m_lineInfo[low - 1].lineNumber; + if (shrinkMode == EarlyShrink) { + m_constantRegisters.shrinkToFit(); + + if (m_rareData) { + m_rareData->m_switchJumpTables.shrinkToFit(); + m_rareData->m_stringSwitchJumpTables.shrinkToFit(); + } + } // else don't shrink these, because we would have already pointed pointers into these tables. } -int CodeBlock::expressionRangeForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset) +unsigned CodeBlock::addOrFindConstant(JSValue v) { - ASSERT(bytecodeOffset < m_instructionCount); + unsigned result; + if (findConstant(v, result)) + return result; + return addConstant(v); +} - reparseForExceptionInfoIfNecessary(callFrame); - ASSERT(m_exceptionInfo); +bool CodeBlock::findConstant(JSValue v, unsigned& index) +{ + unsigned numberOfConstants = numberOfConstantRegisters(); + for (unsigned i = 0; i < numberOfConstants; ++i) { + if (getConstant(FirstConstantRegisterIndex + i) == v) { + index = i; + return true; + } + } + index = numberOfConstants; + return false; +} - if (!m_exceptionInfo->m_expressionInfo.size()) { - // We didn't think anything could throw. Apparently we were wrong. - startOffset = 0; - endOffset = 0; - divot = 0; - return lineNumberForBytecodeOffset(callFrame, bytecodeOffset); +#if ENABLE(JIT) +void CodeBlock::unlinkCalls() +{ + if (!!m_alternative) + m_alternative->unlinkCalls(); + for (size_t i = 0; i < m_llintCallLinkInfos.size(); ++i) { + if (m_llintCallLinkInfos[i].isLinked()) + m_llintCallLinkInfos[i].unlink(); } + if (m_callLinkInfos.isEmpty()) + return; + if (!m_vm->canUseJIT()) + return; + RepatchBuffer repatchBuffer(this); + for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter) { + CallLinkInfo& info = **iter; + if (!info.isLinked()) + continue; + info.unlink(repatchBuffer); + } +} - int low = 0; - int high = m_exceptionInfo->m_expressionInfo.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_exceptionInfo->m_expressionInfo[mid].instructionOffset <= bytecodeOffset) - low = mid + 1; - else - high = mid; +void CodeBlock::linkIncomingCall(ExecState* callerFrame, CallLinkInfo* incoming) +{ + noticeIncomingCall(callerFrame); + m_incomingCalls.push(incoming); +} +#endif // ENABLE(JIT) + +void CodeBlock::unlinkIncomingCalls() +{ + while (m_incomingLLIntCalls.begin() != m_incomingLLIntCalls.end()) + m_incomingLLIntCalls.begin()->unlink(); +#if ENABLE(JIT) + if (m_incomingCalls.isEmpty()) + return; + RepatchBuffer repatchBuffer(this); + while (m_incomingCalls.begin() != m_incomingCalls.end()) + m_incomingCalls.begin()->unlink(repatchBuffer); +#endif // ENABLE(JIT) +} + +void CodeBlock::linkIncomingCall(ExecState* callerFrame, LLIntCallLinkInfo* incoming) +{ + noticeIncomingCall(callerFrame); + m_incomingLLIntCalls.push(incoming); +} + +void CodeBlock::clearEvalCache() +{ + if (!!m_alternative) + m_alternative->clearEvalCache(); + if (CodeBlock* otherBlock = specialOSREntryBlockOrNull()) + otherBlock->clearEvalCache(); + if (!m_rareData) + return; + m_rareData->m_evalCodeCache.clear(); +} + +void CodeBlock::install() +{ + ownerExecutable()->installCode(this); +} + +PassRefPtr CodeBlock::newReplacement() +{ + return ownerExecutable()->newReplacementCodeBlockFor(specializationKind()); +} + +const SlowArgument* CodeBlock::machineSlowArguments() +{ + if (!JITCode::isOptimizingJIT(jitType())) + return symbolTable()->slowArguments(); + +#if ENABLE(DFG_JIT) + return jitCode()->dfgCommon()->slowArguments.get(); +#else // ENABLE(DFG_JIT) + return 0; +#endif // ENABLE(DFG_JIT) +} + +#if ENABLE(JIT) +CodeBlock* ProgramCodeBlock::replacement() +{ + return jsCast(ownerExecutable())->codeBlock(); +} + +CodeBlock* EvalCodeBlock::replacement() +{ + return jsCast(ownerExecutable())->codeBlock(); +} + +CodeBlock* FunctionCodeBlock::replacement() +{ + return jsCast(ownerExecutable())->codeBlockFor(m_isConstructor ? CodeForConstruct : CodeForCall); +} + +DFG::CapabilityLevel ProgramCodeBlock::capabilityLevelInternal() +{ + return DFG::programCapabilityLevel(this); +} + +DFG::CapabilityLevel EvalCodeBlock::capabilityLevelInternal() +{ + return DFG::evalCapabilityLevel(this); +} + +DFG::CapabilityLevel FunctionCodeBlock::capabilityLevelInternal() +{ + if (m_isConstructor) + return DFG::functionForConstructCapabilityLevel(this); + return DFG::functionForCallCapabilityLevel(this); +} +#endif + +void CodeBlock::jettison(Profiler::JettisonReason reason, ReoptimizationMode mode) +{ + RELEASE_ASSERT(reason != Profiler::NotJettisoned); + +#if ENABLE(DFG_JIT) + if (DFG::shouldShowDisassembly()) { + dataLog("Jettisoning ", *this); + if (mode == CountReoptimization) + dataLog(" and counting reoptimization"); + dataLog(" due to ", reason, ".\n"); + } + + DeferGCForAWhile deferGC(*m_heap); + RELEASE_ASSERT(JITCode::isOptimizingJIT(jitType())); + + if (Profiler::Compilation* compilation = jitCode()->dfgCommon()->compilation.get()) + compilation->setJettisonReason(reason); + + // We want to accomplish two things here: + // 1) Make sure that if this CodeBlock is on the stack right now, then if we return to it + // we should OSR exit at the top of the next bytecode instruction after the return. + // 2) Make sure that if we call the owner executable, then we shouldn't call this CodeBlock. + + // This accomplishes the OSR-exit-on-return part, and does its own book-keeping about + // whether the invalidation has already happened. + if (!jitCode()->dfgCommon()->invalidate()) { + // Nothing to do since we've already been invalidated. That means that we cannot be + // the optimized replacement. + RELEASE_ASSERT(this != replacement()); + return; } - ASSERT(low); - if (!low) { - startOffset = 0; - endOffset = 0; - divot = 0; - return lineNumberForBytecodeOffset(callFrame, bytecodeOffset); + if (DFG::shouldShowDisassembly()) + dataLog(" Did invalidate ", *this, "\n"); + + // Count the reoptimization if that's what the user wanted. + if (mode == CountReoptimization) { + // FIXME: Maybe this should call alternative(). + // https://bugs.webkit.org/show_bug.cgi?id=123677 + baselineAlternative()->countReoptimization(); + if (DFG::shouldShowDisassembly()) + dataLog(" Did count reoptimization for ", *this, "\n"); } + + // Now take care of the entrypoint. + if (this != replacement()) { + // This means that we were never the entrypoint. This can happen for OSR entry code + // blocks. + return; + } + alternative()->optimizeAfterWarmUp(); + tallyFrequentExitSites(); + alternative()->install(); + if (DFG::shouldShowDisassembly()) + dataLog(" Did install baseline version of ", *this, "\n"); +#else // ENABLE(DFG_JIT) + UNUSED_PARAM(mode); + UNREACHABLE_FOR_PLATFORM(); +#endif // ENABLE(DFG_JIT) +} - startOffset = m_exceptionInfo->m_expressionInfo[low - 1].startOffset; - endOffset = m_exceptionInfo->m_expressionInfo[low - 1].endOffset; - divot = m_exceptionInfo->m_expressionInfo[low - 1].divotPoint + m_sourceOffset; - return lineNumberForBytecodeOffset(callFrame, bytecodeOffset); +JSGlobalObject* CodeBlock::globalObjectFor(CodeOrigin codeOrigin) +{ + if (!codeOrigin.inlineCallFrame) + return globalObject(); + return jsCast(codeOrigin.inlineCallFrame->executable.get())->eitherCodeBlock()->globalObject(); } -bool CodeBlock::getByIdExceptionInfoForBytecodeOffset(CallFrame* callFrame, unsigned bytecodeOffset, OpcodeID& opcodeID) +void CodeBlock::noticeIncomingCall(ExecState* callerFrame) { - ASSERT(bytecodeOffset < m_instructionCount); + CodeBlock* callerCodeBlock = callerFrame->codeBlock(); + + if (Options::verboseCallLink()) + dataLog("Noticing call link from ", *callerCodeBlock, " to ", *this, "\n"); + + if (!m_shouldAlwaysBeInlined) + return; - reparseForExceptionInfoIfNecessary(callFrame); - ASSERT(m_exceptionInfo); +#if ENABLE(DFG_JIT) + if (!hasBaselineJITProfiling()) + return; - if (!m_exceptionInfo->m_getByIdExceptionInfo.size()) - return false; + if (!DFG::mightInlineFunction(this)) + return; - int low = 0; - int high = m_exceptionInfo->m_getByIdExceptionInfo.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_exceptionInfo->m_getByIdExceptionInfo[mid].bytecodeOffset <= bytecodeOffset) - low = mid + 1; - else - high = mid; + if (!canInline(m_capabilityLevelState)) + return; + + if (!DFG::isSmallEnoughToInlineCodeInto(callerCodeBlock)) { + m_shouldAlwaysBeInlined = false; + if (Options::verboseCallLink()) + dataLog(" Clearing SABI because caller is too large.\n"); + return; } - if (!low || m_exceptionInfo->m_getByIdExceptionInfo[low - 1].bytecodeOffset != bytecodeOffset) - return false; + if (callerCodeBlock->jitType() == JITCode::InterpreterThunk) { + // If the caller is still in the interpreter, then we can't expect inlining to + // happen anytime soon. Assume it's profitable to optimize it separately. This + // ensures that a function is SABI only if it is called no more frequently than + // any of its callers. + m_shouldAlwaysBeInlined = false; + if (Options::verboseCallLink()) + dataLog(" Clearing SABI because caller is in LLInt.\n"); + return; + } + + if (callerCodeBlock->codeType() != FunctionCode) { + // If the caller is either eval or global code, assume that that won't be + // optimized anytime soon. For eval code this is particularly true since we + // delay eval optimization by a *lot*. + m_shouldAlwaysBeInlined = false; + if (Options::verboseCallLink()) + dataLog(" Clearing SABI because caller is not a function.\n"); + return; + } + + ExecState* frame = callerFrame; + for (unsigned i = Options::maximumInliningDepth(); i--; frame = frame->callerFrame()) { + if (frame->isVMEntrySentinel()) + break; + if (frame->codeBlock() == this) { + // Recursive calls won't be inlined. + if (Options::verboseCallLink()) + dataLog(" Clearing SABI because recursion was detected.\n"); + m_shouldAlwaysBeInlined = false; + return; + } + } + + RELEASE_ASSERT(callerCodeBlock->m_capabilityLevelState != DFG::CapabilityLevelNotSet); + + if (canCompile(callerCodeBlock->m_capabilityLevelState)) + return; + + if (Options::verboseCallLink()) + dataLog(" Clearing SABI because the caller is not a DFG candidate.\n"); + + m_shouldAlwaysBeInlined = false; +#endif +} - opcodeID = m_exceptionInfo->m_getByIdExceptionInfo[low - 1].isOpConstruct ? op_construct : op_instanceof; - return true; +unsigned CodeBlock::reoptimizationRetryCounter() const +{ +#if ENABLE(JIT) + ASSERT(m_reoptimizationRetryCounter <= Options::reoptimizationRetryCounterMax()); + return m_reoptimizationRetryCounter; +#else + return 0; +#endif // ENABLE(JIT) } #if ENABLE(JIT) -bool CodeBlock::functionRegisterForBytecodeOffset(unsigned bytecodeOffset, int& functionRegisterIndex) +void CodeBlock::countReoptimization() { - ASSERT(bytecodeOffset < m_instructionCount); + m_reoptimizationRetryCounter++; + if (m_reoptimizationRetryCounter > Options::reoptimizationRetryCounterMax()) + m_reoptimizationRetryCounter = Options::reoptimizationRetryCounterMax(); +} - if (!m_rareData || !m_rareData->m_functionRegisterInfos.size()) - return false; +unsigned CodeBlock::numberOfDFGCompiles() +{ + ASSERT(JITCode::isBaselineCode(jitType())); + if (Options::testTheFTL()) { + if (m_didFailFTLCompilation) + return 1000000; + return (m_hasBeenCompiledWithFTL ? 1 : 0) + m_reoptimizationRetryCounter; + } + return (JITCode::isOptimizingJIT(replacement()->jitType()) ? 1 : 0) + m_reoptimizationRetryCounter; +} - int low = 0; - int high = m_rareData->m_functionRegisterInfos.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_rareData->m_functionRegisterInfos[mid].bytecodeOffset <= bytecodeOffset) - low = mid + 1; - else - high = mid; +int32_t CodeBlock::codeTypeThresholdMultiplier() const +{ + if (codeType() == EvalCode) + return Options::evalThresholdMultiplier(); + + return 1; +} + +double CodeBlock::optimizationThresholdScalingFactor() +{ + // This expression arises from doing a least-squares fit of + // + // F[x_] =: a * Sqrt[x + b] + Abs[c * x] + d + // + // against the data points: + // + // x F[x_] + // 10 0.9 (smallest reasonable code block) + // 200 1.0 (typical small-ish code block) + // 320 1.2 (something I saw in 3d-cube that I wanted to optimize) + // 1268 5.0 (something I saw in 3d-cube that I didn't want to optimize) + // 4000 5.5 (random large size, used to cause the function to converge to a shallow curve of some sort) + // 10000 6.0 (similar to above) + // + // I achieve the minimization using the following Mathematica code: + // + // MyFunctionTemplate[x_, a_, b_, c_, d_] := a*Sqrt[x + b] + Abs[c*x] + d + // + // samples = {{10, 0.9}, {200, 1}, {320, 1.2}, {1268, 5}, {4000, 5.5}, {10000, 6}} + // + // solution = + // Minimize[Plus @@ ((MyFunctionTemplate[#[[1]], a, b, c, d] - #[[2]])^2 & /@ samples), + // {a, b, c, d}][[2]] + // + // And the code below (to initialize a, b, c, d) is generated by: + // + // Print["const double " <> ToString[#[[1]]] <> " = " <> + // If[#[[2]] < 0.00001, "0.0", ToString[#[[2]]]] <> ";"] & /@ solution + // + // We've long known the following to be true: + // - Small code blocks are cheap to optimize and so we should do it sooner rather + // than later. + // - Large code blocks are expensive to optimize and so we should postpone doing so, + // and sometimes have a large enough threshold that we never optimize them. + // - The difference in cost is not totally linear because (a) just invoking the + // DFG incurs some base cost and (b) for large code blocks there is enough slop + // in the correlation between instruction count and the actual compilation cost + // that for those large blocks, the instruction count should not have a strong + // influence on our threshold. + // + // I knew the goals but I didn't know how to achieve them; so I picked an interesting + // example where the heuristics were right (code block in 3d-cube with instruction + // count 320, which got compiled early as it should have been) and one where they were + // totally wrong (code block in 3d-cube with instruction count 1268, which was expensive + // to compile and didn't run often enough to warrant compilation in my opinion), and + // then threw in additional data points that represented my own guess of what our + // heuristics should do for some round-numbered examples. + // + // The expression to which I decided to fit the data arose because I started with an + // affine function, and then did two things: put the linear part in an Abs to ensure + // that the fit didn't end up choosing a negative value of c (which would result in + // the function turning over and going negative for large x) and I threw in a Sqrt + // term because Sqrt represents my intution that the function should be more sensitive + // to small changes in small values of x, but less sensitive when x gets large. + + // Note that the current fit essentially eliminates the linear portion of the + // expression (c == 0.0). + const double a = 0.061504; + const double b = 1.02406; + const double c = 0.0; + const double d = 0.825914; + + double instructionCount = this->instructionCount(); + + ASSERT(instructionCount); // Make sure this is called only after we have an instruction stream; otherwise it'll just return the value of d, which makes no sense. + + double result = d + a * sqrt(instructionCount + b) + c * instructionCount; + + result *= codeTypeThresholdMultiplier(); + + if (Options::verboseOSR()) { + dataLog( + *this, ": instruction count is ", instructionCount, + ", scaling execution counter by ", result, " * ", codeTypeThresholdMultiplier(), + "\n"); } + return result; +} - if (!low || m_rareData->m_functionRegisterInfos[low - 1].bytecodeOffset != bytecodeOffset) - return false; +static int32_t clipThreshold(double threshold) +{ + if (threshold < 1.0) + return 1; + + if (threshold > static_cast(std::numeric_limits::max())) + return std::numeric_limits::max(); + + return static_cast(threshold); +} - functionRegisterIndex = m_rareData->m_functionRegisterInfos[low - 1].functionRegisterIndex; - return true; +int32_t CodeBlock::adjustedCounterValue(int32_t desiredThreshold) +{ + return clipThreshold( + static_cast(desiredThreshold) * + optimizationThresholdScalingFactor() * + (1 << reoptimizationRetryCounter())); } + +bool CodeBlock::checkIfOptimizationThresholdReached() +{ +#if ENABLE(DFG_JIT) + if (DFG::Worklist* worklist = DFG::existingGlobalDFGWorklistOrNull()) { + if (worklist->compilationState(DFG::CompilationKey(this, DFG::DFGMode)) + == DFG::Worklist::Compiled) { + optimizeNextInvocation(); + return true; + } + } #endif + + return m_jitExecuteCounter.checkIfThresholdCrossedAndSet(this); +} -#if !ENABLE(JIT) -bool CodeBlock::hasGlobalResolveInstructionAtBytecodeOffset(unsigned bytecodeOffset) +void CodeBlock::optimizeNextInvocation() { - if (m_globalResolveInstructions.isEmpty()) - return false; + if (Options::verboseOSR()) + dataLog(*this, ": Optimizing next invocation.\n"); + m_jitExecuteCounter.setNewThreshold(0, this); +} - int low = 0; - int high = m_globalResolveInstructions.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_globalResolveInstructions[mid] <= bytecodeOffset) - low = mid + 1; - else - high = mid; - } +void CodeBlock::dontOptimizeAnytimeSoon() +{ + if (Options::verboseOSR()) + dataLog(*this, ": Not optimizing anytime soon.\n"); + m_jitExecuteCounter.deferIndefinitely(); +} - if (!low || m_globalResolveInstructions[low - 1] != bytecodeOffset) - return false; - return true; +void CodeBlock::optimizeAfterWarmUp() +{ + if (Options::verboseOSR()) + dataLog(*this, ": Optimizing after warm-up.\n"); +#if ENABLE(DFG_JIT) + m_jitExecuteCounter.setNewThreshold( + adjustedCounterValue(Options::thresholdForOptimizeAfterWarmUp()), this); +#endif } -#else -bool CodeBlock::hasGlobalResolveInfoAtBytecodeOffset(unsigned bytecodeOffset) + +void CodeBlock::optimizeAfterLongWarmUp() { - if (m_globalResolveInfos.isEmpty()) - return false; + if (Options::verboseOSR()) + dataLog(*this, ": Optimizing after long warm-up.\n"); +#if ENABLE(DFG_JIT) + m_jitExecuteCounter.setNewThreshold( + adjustedCounterValue(Options::thresholdForOptimizeAfterLongWarmUp()), this); +#endif +} + +void CodeBlock::optimizeSoon() +{ + if (Options::verboseOSR()) + dataLog(*this, ": Optimizing soon.\n"); +#if ENABLE(DFG_JIT) + m_jitExecuteCounter.setNewThreshold( + adjustedCounterValue(Options::thresholdForOptimizeSoon()), this); +#endif +} - int low = 0; - int high = m_globalResolveInfos.size(); - while (low < high) { - int mid = low + (high - low) / 2; - if (m_globalResolveInfos[mid].bytecodeOffset <= bytecodeOffset) - low = mid + 1; +void CodeBlock::forceOptimizationSlowPathConcurrently() +{ + if (Options::verboseOSR()) + dataLog(*this, ": Forcing slow path concurrently.\n"); + m_jitExecuteCounter.forceSlowPathConcurrently(); +} + +#if ENABLE(DFG_JIT) +void CodeBlock::setOptimizationThresholdBasedOnCompilationResult(CompilationResult result) +{ + JITCode::JITType type = jitType(); + if (type != JITCode::BaselineJIT) { + dataLog(*this, ": expected to have baseline code but have ", type, "\n"); + RELEASE_ASSERT_NOT_REACHED(); + } + + CodeBlock* theReplacement = replacement(); + if ((result == CompilationSuccessful) != (theReplacement != this)) { + dataLog(*this, ": we have result = ", result, " but "); + if (theReplacement == this) + dataLog("we are our own replacement.\n"); else - high = mid; + dataLog("our replacement is ", pointerDump(theReplacement), "\n"); + RELEASE_ASSERT_NOT_REACHED(); } - - if (!low || m_globalResolveInfos[low - 1].bytecodeOffset != bytecodeOffset) - return false; - return true; + + switch (result) { + case CompilationSuccessful: + RELEASE_ASSERT(JITCode::isOptimizingJIT(replacement()->jitType())); + optimizeNextInvocation(); + return; + case CompilationFailed: + dontOptimizeAnytimeSoon(); + return; + case CompilationDeferred: + // We'd like to do dontOptimizeAnytimeSoon() but we cannot because + // forceOptimizationSlowPathConcurrently() is inherently racy. It won't + // necessarily guarantee anything. So, we make sure that even if that + // function ends up being a no-op, we still eventually retry and realize + // that we have optimized code ready. + optimizeAfterWarmUp(); + return; + case CompilationInvalidated: + // Retry with exponential backoff. + countReoptimization(); + optimizeAfterWarmUp(); + return; + } + + dataLog("Unrecognized result: ", static_cast(result), "\n"); + RELEASE_ASSERT_NOT_REACHED(); } + #endif + +uint32_t CodeBlock::adjustedExitCountThreshold(uint32_t desiredThreshold) +{ + ASSERT(JITCode::isOptimizingJIT(jitType())); + // Compute this the lame way so we don't saturate. This is called infrequently + // enough that this loop won't hurt us. + unsigned result = desiredThreshold; + for (unsigned n = baselineVersion()->reoptimizationRetryCounter(); n--;) { + unsigned newResult = result << 1; + if (newResult < result) + return std::numeric_limits::max(); + result = newResult; + } + return result; +} -#if ENABLE(JIT) -void CodeBlock::setJITCode(JITCodeRef& jitCode) +uint32_t CodeBlock::exitCountThresholdForReoptimization() { - m_jitCode = jitCode; -#if !ENABLE(OPCODE_SAMPLING) - if (!BytecodeGenerator::dumpsGeneratedCode()) - m_instructions.clear(); -#endif + return adjustedExitCountThreshold(Options::osrExitCountForReoptimization() * codeTypeThresholdMultiplier()); +} + +uint32_t CodeBlock::exitCountThresholdForReoptimizationFromLoop() +{ + return adjustedExitCountThreshold(Options::osrExitCountForReoptimizationFromLoop() * codeTypeThresholdMultiplier()); +} + +bool CodeBlock::shouldReoptimizeNow() +{ + return osrExitCounter() >= exitCountThresholdForReoptimization(); +} + +bool CodeBlock::shouldReoptimizeFromLoopNow() +{ + return osrExitCounter() >= exitCountThresholdForReoptimizationFromLoop(); } #endif -void CodeBlock::shrinkToFit() +ArrayProfile* CodeBlock::getArrayProfile(unsigned bytecodeOffset) { - m_instructions.shrinkToFit(); + for (unsigned i = 0; i < m_arrayProfiles.size(); ++i) { + if (m_arrayProfiles[i].bytecodeOffset() == bytecodeOffset) + return &m_arrayProfiles[i]; + } + return 0; +} -#if !ENABLE(JIT) - m_propertyAccessInstructions.shrinkToFit(); - m_globalResolveInstructions.shrinkToFit(); -#else - m_structureStubInfos.shrinkToFit(); - m_globalResolveInfos.shrinkToFit(); - m_callLinkInfos.shrinkToFit(); - m_linkedCallerList.shrinkToFit(); +ArrayProfile* CodeBlock::getOrAddArrayProfile(unsigned bytecodeOffset) +{ + ArrayProfile* result = getArrayProfile(bytecodeOffset); + if (result) + return result; + return addArrayProfile(bytecodeOffset); +} + +void CodeBlock::updateAllPredictionsAndCountLiveness(unsigned& numberOfLiveNonArgumentValueProfiles, unsigned& numberOfSamplesInProfiles) +{ + ConcurrentJITLocker locker(m_lock); + + numberOfLiveNonArgumentValueProfiles = 0; + numberOfSamplesInProfiles = 0; // If this divided by ValueProfile::numberOfBuckets equals numberOfValueProfiles() then value profiles are full. + for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) { + ValueProfile* profile = getFromAllValueProfiles(i); + unsigned numSamples = profile->totalNumberOfSamples(); + if (numSamples > ValueProfile::numberOfBuckets) + numSamples = ValueProfile::numberOfBuckets; // We don't want profiles that are extremely hot to be given more weight. + numberOfSamplesInProfiles += numSamples; + if (profile->m_bytecodeOffset < 0) { + profile->computeUpdatedPrediction(locker); + continue; + } + if (profile->numberOfSamples() || profile->m_prediction != SpecNone) + numberOfLiveNonArgumentValueProfiles++; + profile->computeUpdatedPrediction(locker); + } + +#if ENABLE(DFG_JIT) + m_lazyOperandValueProfiles.computeUpdatedPredictions(locker); #endif +} + +void CodeBlock::updateAllValueProfilePredictions() +{ + unsigned ignoredValue1, ignoredValue2; + updateAllPredictionsAndCountLiveness(ignoredValue1, ignoredValue2); +} + +void CodeBlock::updateAllArrayPredictions() +{ + ConcurrentJITLocker locker(m_lock); + + for (unsigned i = m_arrayProfiles.size(); i--;) + m_arrayProfiles[i].computeUpdatedPrediction(locker, this); + + // Don't count these either, for similar reasons. + for (unsigned i = m_arrayAllocationProfiles.size(); i--;) + m_arrayAllocationProfiles[i].updateIndexingType(); +} - m_identifiers.shrinkToFit(); - m_functionExpressions.shrinkToFit(); - m_constantRegisters.shrinkToFit(); +void CodeBlock::updateAllPredictions() +{ + updateAllValueProfilePredictions(); + updateAllArrayPredictions(); +} + +bool CodeBlock::shouldOptimizeNow() +{ + if (Options::verboseOSR()) + dataLog("Considering optimizing ", *this, "...\n"); - if (m_exceptionInfo) { - m_exceptionInfo->m_expressionInfo.shrinkToFit(); - m_exceptionInfo->m_lineInfo.shrinkToFit(); - m_exceptionInfo->m_getByIdExceptionInfo.shrinkToFit(); + if (m_optimizationDelayCounter >= Options::maximumOptimizationDelay()) + return true; + + updateAllArrayPredictions(); + + unsigned numberOfLiveNonArgumentValueProfiles; + unsigned numberOfSamplesInProfiles; + updateAllPredictionsAndCountLiveness(numberOfLiveNonArgumentValueProfiles, numberOfSamplesInProfiles); + + if (Options::verboseOSR()) { + dataLogF( + "Profile hotness: %lf (%u / %u), %lf (%u / %u)\n", + (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles(), + numberOfLiveNonArgumentValueProfiles, numberOfValueProfiles(), + (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / numberOfValueProfiles(), + numberOfSamplesInProfiles, ValueProfile::numberOfBuckets * numberOfValueProfiles()); } - if (m_rareData) { - m_rareData->m_exceptionHandlers.shrinkToFit(); - m_rareData->m_functions.shrinkToFit(); - m_rareData->m_unexpectedConstants.shrinkToFit(); - m_rareData->m_regexps.shrinkToFit(); - m_rareData->m_immediateSwitchJumpTables.shrinkToFit(); - m_rareData->m_characterSwitchJumpTables.shrinkToFit(); - m_rareData->m_stringSwitchJumpTables.shrinkToFit(); -#if ENABLE(JIT) - m_rareData->m_functionRegisterInfos.shrinkToFit(); + if ((!numberOfValueProfiles() || (double)numberOfLiveNonArgumentValueProfiles / numberOfValueProfiles() >= Options::desiredProfileLivenessRate()) + && (!totalNumberOfValueProfiles() || (double)numberOfSamplesInProfiles / ValueProfile::numberOfBuckets / totalNumberOfValueProfiles() >= Options::desiredProfileFullnessRate()) + && static_cast(m_optimizationDelayCounter) + 1 >= Options::minimumOptimizationDelay()) + return true; + + ASSERT(m_optimizationDelayCounter < std::numeric_limits::max()); + m_optimizationDelayCounter++; + optimizeAfterWarmUp(); + return false; +} + +#if ENABLE(DFG_JIT) +void CodeBlock::tallyFrequentExitSites() +{ + ASSERT(JITCode::isOptimizingJIT(jitType())); + ASSERT(alternative()->jitType() == JITCode::BaselineJIT); + + CodeBlock* profiledBlock = alternative(); + + switch (jitType()) { + case JITCode::DFGJIT: { + DFG::JITCode* jitCode = m_jitCode->dfg(); + for (unsigned i = 0; i < jitCode->osrExit.size(); ++i) { + DFG::OSRExit& exit = jitCode->osrExit[i]; + + if (!exit.considerAddingAsFrequentExitSite(profiledBlock)) + continue; + } + break; + } + +#if ENABLE(FTL_JIT) + case JITCode::FTLJIT: { + // There is no easy way to avoid duplicating this code since the FTL::JITCode::osrExit + // vector contains a totally different type, that just so happens to behave like + // DFG::JITCode::osrExit. + FTL::JITCode* jitCode = m_jitCode->ftl(); + for (unsigned i = 0; i < jitCode->osrExit.size(); ++i) { + FTL::OSRExit& exit = jitCode->osrExit[i]; + + if (!exit.considerAddingAsFrequentExitSite(profiledBlock)) + continue; + } + break; + } #endif + + default: + RELEASE_ASSERT_NOT_REACHED(); + break; + } +} +#endif // ENABLE(DFG_JIT) + +#if ENABLE(VERBOSE_VALUE_PROFILE) +void CodeBlock::dumpValueProfiles() +{ + dataLog("ValueProfile for ", *this, ":\n"); + for (unsigned i = 0; i < totalNumberOfValueProfiles(); ++i) { + ValueProfile* profile = getFromAllValueProfiles(i); + if (profile->m_bytecodeOffset < 0) { + ASSERT(profile->m_bytecodeOffset == -1); + dataLogF(" arg = %u: ", i); + } else + dataLogF(" bc = %d: ", profile->m_bytecodeOffset); + if (!profile->numberOfSamples() && profile->m_prediction == SpecNone) { + dataLogF("\n"); + continue; + } + profile->dump(WTF::dataFile()); + dataLogF("\n"); + } + dataLog("RareCaseProfile for ", *this, ":\n"); + for (unsigned i = 0; i < numberOfRareCaseProfiles(); ++i) { + RareCaseProfile* profile = rareCaseProfile(i); + dataLogF(" bc = %d: %u\n", profile->m_bytecodeOffset, profile->m_counter); + } + dataLog("SpecialFastCaseProfile for ", *this, ":\n"); + for (unsigned i = 0; i < numberOfSpecialFastCaseProfiles(); ++i) { + RareCaseProfile* profile = specialFastCaseProfile(i); + dataLogF(" bc = %d: %u\n", profile->m_bytecodeOffset, profile->m_counter); + } +} +#endif // ENABLE(VERBOSE_VALUE_PROFILE) + +unsigned CodeBlock::frameRegisterCount() +{ + switch (jitType()) { + case JITCode::InterpreterThunk: + return LLInt::frameRegisterCountFor(this); + +#if ENABLE(JIT) + case JITCode::BaselineJIT: + return JIT::frameRegisterCountFor(this); +#endif // ENABLE(JIT) + +#if ENABLE(DFG_JIT) + case JITCode::DFGJIT: + case JITCode::FTLJIT: + return jitCode()->dfgCommon()->frameRegisterCount; +#endif // ENABLE(DFG_JIT) + + default: + RELEASE_ASSERT_NOT_REACHED(); + return 0; + } +} + +int CodeBlock::stackPointerOffset() +{ + return virtualRegisterForLocal(frameRegisterCount() - 1).offset(); +} + +size_t CodeBlock::predictedMachineCodeSize() +{ + // This will be called from CodeBlock::CodeBlock before either m_vm or the + // instructions have been initialized. It's OK to return 0 because what will really + // matter is the recomputation of this value when the slow path is triggered. + if (!m_vm) + return 0; + + if (!m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT) + return 0; // It's as good of a prediction as we'll get. + + // Be conservative: return a size that will be an overestimation 84% of the time. + double multiplier = m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT.mean() + + m_vm->machineCodeBytesPerBytecodeWordForBaselineJIT.standardDeviation(); + + // Be paranoid: silently reject bogus multipiers. Silently doing the "wrong" thing + // here is OK, since this whole method is just a heuristic. + if (multiplier < 0 || multiplier > 1000) + return 0; + + double doubleResult = multiplier * m_instructions.size(); + + // Be even more paranoid: silently reject values that won't fit into a size_t. If + // the function is so huge that we can't even fit it into virtual memory then we + // should probably have some other guards in place to prevent us from even getting + // to this point. + if (doubleResult > std::numeric_limits::max()) + return 0; + + return static_cast(doubleResult); +} + +bool CodeBlock::usesOpcode(OpcodeID opcodeID) +{ + Interpreter* interpreter = vm()->interpreter; + Instruction* instructionsBegin = instructions().begin(); + unsigned instructionCount = instructions().size(); + + for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) { + switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset].u.opcode)) { +#define DEFINE_OP(curOpcode, length) \ + case curOpcode: \ + if (curOpcode == opcodeID) \ + return true; \ + bytecodeOffset += length; \ + break; + FOR_EACH_OPCODE_ID(DEFINE_OP) +#undef DEFINE_OP + default: + RELEASE_ASSERT_NOT_REACHED(); + break; + } + } + + return false; +} + +String CodeBlock::nameForRegister(VirtualRegister virtualRegister) +{ + ConcurrentJITLocker locker(symbolTable()->m_lock); + SymbolTable::Map::iterator end = symbolTable()->end(locker); + for (SymbolTable::Map::iterator ptr = symbolTable()->begin(locker); ptr != end; ++ptr) { + if (ptr->value.getIndex() == virtualRegister.offset()) { + // FIXME: This won't work from the compilation thread. + // https://bugs.webkit.org/show_bug.cgi?id=115300 + return String(ptr->key); + } + } + if (needsActivation() && virtualRegister == activationRegister()) + return ASCIILiteral("activation"); + if (virtualRegister == thisRegister()) + return ASCIILiteral("this"); + if (usesArguments()) { + if (virtualRegister == argumentsRegister()) + return ASCIILiteral("arguments"); + if (unmodifiedArgumentsRegister(argumentsRegister()) == virtualRegister) + return ASCIILiteral("real arguments"); + } + if (virtualRegister.isArgument()) + return String::format("arguments[%3d]", virtualRegister.toArgument()).impl(); + + return ""; +} + +namespace { + +struct VerifyCapturedDef { + void operator()(CodeBlock* codeBlock, Instruction* instruction, OpcodeID opcodeID, int operand) + { + unsigned bytecodeOffset = instruction - codeBlock->instructions().begin(); + + if (codeBlock->isConstantRegisterIndex(operand)) { + codeBlock->beginValidationDidFail(); + dataLog(" At bc#", bytecodeOffset, " encountered a definition of a constant.\n"); + codeBlock->endValidationDidFail(); + return; + } + + switch (opcodeID) { + case op_enter: + case op_captured_mov: + case op_init_lazy_reg: + case op_create_arguments: + case op_new_captured_func: + return; + default: + break; + } + + VirtualRegister virtualReg(operand); + if (!virtualReg.isLocal()) + return; + + if (codeBlock->captureCount() && codeBlock->symbolTable()->isCaptured(operand)) { + codeBlock->beginValidationDidFail(); + dataLog(" At bc#", bytecodeOffset, " encountered invalid assignment to captured variable loc", virtualReg.toLocal(), ".\n"); + codeBlock->endValidationDidFail(); + return; + } + + return; + } +}; + +} // anonymous namespace + +void CodeBlock::validate() +{ + BytecodeLivenessAnalysis liveness(this); // Compute directly from scratch so it doesn't effect CodeBlock footprint. + + FastBitVector liveAtHead = liveness.getLivenessInfoAtBytecodeOffset(0); + + if (liveAtHead.numBits() != static_cast(m_numCalleeRegisters)) { + beginValidationDidFail(); + dataLog(" Wrong number of bits in result!\n"); + dataLog(" Result: ", liveAtHead, "\n"); + dataLog(" Bit count: ", liveAtHead.numBits(), "\n"); + endValidationDidFail(); + } + + for (unsigned i = m_numCalleeRegisters; i--;) { + bool isCaptured = false; + VirtualRegister reg = virtualRegisterForLocal(i); + + if (captureCount()) + isCaptured = reg.offset() <= captureStart() && reg.offset() > captureEnd(); + + if (isCaptured) { + if (!liveAtHead.get(i)) { + beginValidationDidFail(); + dataLog(" Variable loc", i, " is expected to be live because it is captured, but it isn't live.\n"); + dataLog(" Result: ", liveAtHead, "\n"); + endValidationDidFail(); + } + } else { + if (liveAtHead.get(i)) { + beginValidationDidFail(); + dataLog(" Variable loc", i, " is expected to be dead.\n"); + dataLog(" Result: ", liveAtHead, "\n"); + endValidationDidFail(); + } + } } + + for (unsigned bytecodeOffset = 0; bytecodeOffset < instructions().size();) { + Instruction* currentInstruction = instructions().begin() + bytecodeOffset; + OpcodeID opcodeID = m_vm->interpreter->getOpcodeID(currentInstruction->u.opcode); + + VerifyCapturedDef verifyCapturedDef; + computeDefsForBytecodeOffset(this, bytecodeOffset, verifyCapturedDef); + + bytecodeOffset += opcodeLength(opcodeID); + } +} + +void CodeBlock::beginValidationDidFail() +{ + dataLog("Validation failure in ", *this, ":\n"); + dataLog("\n"); +} + +void CodeBlock::endValidationDidFail() +{ + dataLog("\n"); + dumpBytecode(); + dataLog("\n"); + dataLog("Validation failure.\n"); + RELEASE_ASSERT_NOT_REACHED(); } +void CodeBlock::addBreakpoint(unsigned numBreakpoints) +{ + m_numBreakpoints += numBreakpoints; + ASSERT(m_numBreakpoints); + if (JITCode::isOptimizingJIT(jitType())) + jettison(Profiler::JettisonDueToDebuggerBreakpoint); +} + +void CodeBlock::setSteppingMode(CodeBlock::SteppingMode mode) +{ + m_steppingMode = mode; + if (mode == SteppingModeEnabled && JITCode::isOptimizingJIT(jitType())) + jettison(Profiler::JettisonDueToDebuggerStepping); +} + +RareCaseProfile* CodeBlock::rareCaseProfileForBytecodeOffset(int bytecodeOffset) +{ + return tryBinarySearch( + m_rareCaseProfiles, m_rareCaseProfiles.size(), bytecodeOffset, + getRareCaseProfileBytecodeOffset); +} + +#if ENABLE(JIT) +DFG::CapabilityLevel CodeBlock::capabilityLevel() +{ + DFG::CapabilityLevel result = capabilityLevelInternal(); + m_capabilityLevelState = result; + return result; +} +#endif + } // namespace JSC