2011-01-06 14:13:40 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * 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.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "code-stubs.h"
|
2011-04-07 14:42:37 +00:00
|
|
|
#include "codegen.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "debug.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "deoptimizer.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "disasm.h"
|
|
|
|
#include "disassembler.h"
|
|
|
|
#include "macro-assembler.h"
|
|
|
|
#include "serialize.h"
|
|
|
|
#include "string-stream.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
|
|
|
|
|
|
|
void Disassembler::Dump(FILE* f, byte* begin, byte* end) {
|
|
|
|
for (byte* pc = begin; pc < end; pc++) {
|
|
|
|
if (f == NULL) {
|
2010-09-30 07:22:53 +00:00
|
|
|
PrintF("%" V8PRIxPTR " %4" V8PRIdPTR " %02x\n",
|
|
|
|
reinterpret_cast<intptr_t>(pc),
|
|
|
|
pc - begin,
|
|
|
|
*pc);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2009-05-12 10:33:40 +00:00
|
|
|
fprintf(f, "%" V8PRIxPTR " %4" V8PRIdPTR " %02x\n",
|
|
|
|
reinterpret_cast<uintptr_t>(pc), pc - begin, *pc);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class V8NameConverter: public disasm::NameConverter {
|
|
|
|
public:
|
|
|
|
explicit V8NameConverter(Code* code) : code_(code) {}
|
|
|
|
virtual const char* NameOfAddress(byte* pc) const;
|
2008-07-30 08:49:36 +00:00
|
|
|
virtual const char* NameInCode(byte* addr) const;
|
2008-07-03 15:10:15 +00:00
|
|
|
Code* code() const { return code_; }
|
|
|
|
private:
|
|
|
|
Code* code_;
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
EmbeddedVector<char, 128> v8_buffer_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const char* V8NameConverter::NameOfAddress(byte* pc) const {
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* name = Isolate::Current()->builtins()->Lookup(pc);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (name != NULL) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OS::SNPrintF(v8_buffer_, "%s (%p)", name, pc);
|
|
|
|
return v8_buffer_.start();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (code_ != NULL) {
|
2009-11-11 09:50:06 +00:00
|
|
|
int offs = static_cast<int>(pc - code_->instruction_start());
|
2008-07-03 15:10:15 +00:00
|
|
|
// print as code offset, if it seems reasonable
|
|
|
|
if (0 <= offs && offs < code_->instruction_size()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OS::SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
|
|
|
|
return v8_buffer_.start();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return disasm::NameConverter::NameOfAddress(pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
const char* V8NameConverter::NameInCode(byte* addr) const {
|
|
|
|
// The V8NameConverter is used for well known code, so we can "safely"
|
|
|
|
// dereference pointers in generated code.
|
|
|
|
return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void DumpBuffer(FILE* f, char* buff) {
|
2008-07-30 08:49:36 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
PrintF("%s", buff);
|
|
|
|
} else {
|
|
|
|
fprintf(f, "%s", buff);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-19 11:57:18 +00:00
|
|
|
static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
|
2008-07-03 15:10:15 +00:00
|
|
|
static const int kRelocInfoPosition = 57;
|
|
|
|
|
|
|
|
static int DecodeIt(FILE* f,
|
|
|
|
const V8NameConverter& converter,
|
|
|
|
byte* begin,
|
|
|
|
byte* end) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
ExternalReferenceEncoder ref_encoder;
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = HEAP;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-09-11 14:34:48 +00:00
|
|
|
v8::internal::EmbeddedVector<char, 128> decode_buffer;
|
|
|
|
v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
|
2008-07-03 15:10:15 +00:00
|
|
|
byte* pc = begin;
|
|
|
|
disasm::Disassembler d(converter);
|
|
|
|
RelocIterator* it = NULL;
|
|
|
|
if (converter.code() != NULL) {
|
|
|
|
it = new RelocIterator(converter.code());
|
|
|
|
} else {
|
|
|
|
// No relocation information when printing code stubs.
|
|
|
|
}
|
2008-07-30 08:49:36 +00:00
|
|
|
int constants = -1; // no constants being decoded at the start
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
while (pc < end) {
|
|
|
|
// First decode instruction so that we know its length.
|
|
|
|
byte* prev_pc = pc;
|
2008-07-30 08:49:36 +00:00
|
|
|
if (constants > 0) {
|
|
|
|
OS::SNPrintF(decode_buffer,
|
|
|
|
"%08x constant",
|
|
|
|
*reinterpret_cast<int32_t*>(pc));
|
|
|
|
constants--;
|
|
|
|
pc += 4;
|
|
|
|
} else {
|
|
|
|
int num_const = d.ConstantPoolSizeAt(pc);
|
|
|
|
if (num_const >= 0) {
|
|
|
|
OS::SNPrintF(decode_buffer,
|
|
|
|
"%08x constant pool begin",
|
|
|
|
*reinterpret_cast<int32_t*>(pc));
|
|
|
|
constants = num_const;
|
|
|
|
pc += 4;
|
2008-09-18 11:59:55 +00:00
|
|
|
} else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
|
2008-09-22 13:57:03 +00:00
|
|
|
it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
|
2008-09-18 11:59:55 +00:00
|
|
|
// raw pointer embedded in code stream, e.g., jump table
|
|
|
|
byte* ptr = *reinterpret_cast<byte**>(pc);
|
|
|
|
OS::SNPrintF(decode_buffer,
|
2009-05-12 10:33:40 +00:00
|
|
|
"%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
|
|
|
|
ptr,
|
2008-09-18 11:59:55 +00:00
|
|
|
ptr - begin);
|
|
|
|
pc += 4;
|
2008-07-30 08:49:36 +00:00
|
|
|
} else {
|
|
|
|
decode_buffer[0] = '\0';
|
2008-09-11 14:34:48 +00:00
|
|
|
pc += d.InstructionDecode(decode_buffer, pc);
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Collect RelocInfo for this instruction (prev_pc .. pc-1)
|
|
|
|
List<const char*> comments(4);
|
|
|
|
List<byte*> pcs(1);
|
2008-09-22 13:57:03 +00:00
|
|
|
List<RelocInfo::Mode> rmodes(1);
|
2008-07-03 15:10:15 +00:00
|
|
|
List<intptr_t> datas(1);
|
|
|
|
if (it != NULL) {
|
|
|
|
while (!it->done() && it->rinfo()->pc() < pc) {
|
2008-09-22 13:57:03 +00:00
|
|
|
if (RelocInfo::IsComment(it->rinfo()->rmode())) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// For comments just collect the text.
|
|
|
|
comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
|
|
|
|
} else {
|
|
|
|
// For other reloc info collect all data.
|
|
|
|
pcs.Add(it->rinfo()->pc());
|
|
|
|
rmodes.Add(it->rinfo()->rmode());
|
|
|
|
datas.Add(it->rinfo()->data());
|
|
|
|
}
|
|
|
|
it->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-11 14:34:48 +00:00
|
|
|
StringBuilder out(out_buffer.start(), out_buffer.length());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Comments.
|
|
|
|
for (int i = 0; i < comments.length(); i++) {
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddFormatted(" %s\n", comments[i]);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out comments, resets outp so that we can format the next line.
|
2008-07-30 08:49:36 +00:00
|
|
|
DumpBuffer(f, out.Finalize());
|
|
|
|
out.Reset();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Instruction address and instruction offset.
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddFormatted("%p %4d ", prev_pc, prev_pc - begin);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Instruction.
|
2008-09-11 14:34:48 +00:00
|
|
|
out.AddFormatted("%s", decode_buffer.start());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Print all the reloc info for this instruction which are not comments.
|
|
|
|
for (int i = 0; i < pcs.length(); i++) {
|
|
|
|
// Put together the reloc info
|
|
|
|
RelocInfo relocinfo(pcs[i], rmodes[i], datas[i]);
|
|
|
|
|
|
|
|
// Indent the printing of the reloc info.
|
|
|
|
if (i == 0) {
|
|
|
|
// The first reloc info is printed after the disassembled instruction.
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddPadding(' ', kRelocInfoPosition - out.position());
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// Additional reloc infos are printed on separate lines.
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddFormatted("\n");
|
|
|
|
out.AddPadding(' ', kRelocInfoPosition);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 13:57:03 +00:00
|
|
|
RelocInfo::Mode rmode = relocinfo.rmode();
|
|
|
|
if (RelocInfo::IsPosition(rmode)) {
|
|
|
|
if (RelocInfo::IsStatementPosition(rmode)) {
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(" ;; debug: statement %d", relocinfo.data());
|
|
|
|
} else {
|
|
|
|
out.AddFormatted(" ;; debug: position %d", relocinfo.data());
|
|
|
|
}
|
2008-09-22 13:57:03 +00:00
|
|
|
} else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
|
2008-07-03 15:10:15 +00:00
|
|
|
HeapStringAllocator allocator;
|
|
|
|
StringStream accumulator(&allocator);
|
|
|
|
relocinfo.target_object()->ShortPrint(&accumulator);
|
2008-11-11 14:16:24 +00:00
|
|
|
SmartPointer<const char> obj_name = accumulator.ToCString();
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddFormatted(" ;; object: %s", *obj_name);
|
2008-09-22 13:57:03 +00:00
|
|
|
} else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
|
2008-07-03 15:10:15 +00:00
|
|
|
const char* reference_name =
|
|
|
|
ref_encoder.NameOfAddress(*relocinfo.target_reference_address());
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddFormatted(" ;; external reference (%s)", reference_name);
|
2008-09-22 13:57:03 +00:00
|
|
|
} else if (RelocInfo::IsCodeTarget(rmode)) {
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(" ;; code:");
|
2008-09-22 13:57:03 +00:00
|
|
|
if (rmode == RelocInfo::CONSTRUCT_CALL) {
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(" constructor,");
|
|
|
|
}
|
2008-12-09 12:53:59 +00:00
|
|
|
Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
|
2008-09-17 12:38:50 +00:00
|
|
|
Code::Kind kind = code->kind();
|
|
|
|
if (code->is_inline_cache_stub()) {
|
2008-09-22 13:57:03 +00:00
|
|
|
if (rmode == RelocInfo::CODE_TARGET_CONTEXT) {
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(" contextual,");
|
|
|
|
}
|
|
|
|
InlineCacheState ic_state = code->ic_state();
|
|
|
|
out.AddFormatted(" %s, %s", Code::Kind2String(kind),
|
|
|
|
Code::ICState2String(ic_state));
|
2009-06-30 10:05:36 +00:00
|
|
|
if (ic_state == MONOMORPHIC) {
|
|
|
|
PropertyType type = code->type();
|
|
|
|
out.AddFormatted(", %s", Code::PropertyType2String(type));
|
|
|
|
}
|
2009-06-30 14:07:29 +00:00
|
|
|
if (code->ic_in_loop() == IN_LOOP) {
|
|
|
|
out.AddFormatted(", in_loop");
|
|
|
|
}
|
2010-06-07 08:27:32 +00:00
|
|
|
if (kind == Code::CALL_IC || kind == Code::KEYED_CALL_IC) {
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(", argc = %d", code->arguments_count());
|
|
|
|
}
|
|
|
|
} else if (kind == Code::STUB) {
|
|
|
|
// Reverse lookup required as the minor key cannot be retrieved
|
|
|
|
// from the code object.
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* obj = heap->code_stubs()->SlowReverseLookup(code);
|
|
|
|
if (obj != heap->undefined_value()) {
|
2008-09-17 12:38:50 +00:00
|
|
|
ASSERT(obj->IsSmi());
|
|
|
|
// Get the STUB key and extract major and minor key.
|
|
|
|
uint32_t key = Smi::cast(obj)->value();
|
|
|
|
uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
|
2010-08-30 11:48:07 +00:00
|
|
|
CodeStub::Major major_key = CodeStub::GetMajorKey(code);
|
|
|
|
ASSERT(major_key == CodeStub::MajorKeyFromKey(key));
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted(" %s, %s, ",
|
|
|
|
Code::Kind2String(kind),
|
2010-08-30 11:48:07 +00:00
|
|
|
CodeStub::MajorName(major_key, false));
|
|
|
|
switch (major_key) {
|
2011-01-06 14:13:40 +00:00
|
|
|
case CodeStub::CallFunction: {
|
|
|
|
int argc =
|
|
|
|
CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
|
|
|
|
out.AddFormatted("argc = %d", argc);
|
2008-09-17 12:38:50 +00:00
|
|
|
break;
|
2011-01-06 14:13:40 +00:00
|
|
|
}
|
|
|
|
default:
|
2008-09-17 12:38:50 +00:00
|
|
|
out.AddFormatted("minor: %d", minor_key);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-17 12:38:50 +00:00
|
|
|
} else {
|
|
|
|
out.AddFormatted(" %s", Code::Kind2String(kind));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-04-27 15:02:59 +00:00
|
|
|
if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
|
|
|
|
out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
|
|
|
|
}
|
2011-04-07 11:29:40 +00:00
|
|
|
} else if (rmode == RelocInfo::RUNTIME_ENTRY &&
|
|
|
|
Isolate::Current()->deoptimizer_data() != NULL) {
|
2010-12-07 11:31:57 +00:00
|
|
|
// A runtime entry reloinfo might be a deoptimization bailout.
|
|
|
|
Address addr = relocinfo.target_address();
|
|
|
|
int id = Deoptimizer::GetDeoptimizationId(addr, Deoptimizer::EAGER);
|
|
|
|
if (id == Deoptimizer::kNotDeoptimizationEntry) {
|
|
|
|
out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
|
|
|
|
} else {
|
|
|
|
out.AddFormatted(" ;; deoptimization bailout %d", id);
|
|
|
|
}
|
2008-09-17 12:38:50 +00:00
|
|
|
} else {
|
|
|
|
out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-30 08:49:36 +00:00
|
|
|
out.AddString("\n");
|
|
|
|
DumpBuffer(f, out.Finalize());
|
|
|
|
out.Reset();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete it;
|
2009-11-11 09:50:06 +00:00
|
|
|
return static_cast<int>(pc - begin);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Disassembler::Decode(FILE* f, byte* begin, byte* end) {
|
|
|
|
V8NameConverter defaultConverter(NULL);
|
|
|
|
return DecodeIt(f, defaultConverter, begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
// Called by Code::CodePrint.
|
2008-07-03 15:10:15 +00:00
|
|
|
void Disassembler::Decode(FILE* f, Code* code) {
|
2010-12-07 11:31:57 +00:00
|
|
|
int decode_size = (code->kind() == Code::OPTIMIZED_FUNCTION)
|
2011-02-03 10:07:22 +00:00
|
|
|
? static_cast<int>(code->safepoint_table_offset())
|
2010-12-07 11:31:57 +00:00
|
|
|
: code->instruction_size();
|
|
|
|
// If there might be a stack check table, stop before reaching it.
|
|
|
|
if (code->kind() == Code::FUNCTION) {
|
|
|
|
decode_size =
|
2011-02-03 10:07:22 +00:00
|
|
|
Min(decode_size, static_cast<int>(code->stack_check_table_offset()));
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte* begin = code->instruction_start();
|
|
|
|
byte* end = begin + decode_size;
|
2008-07-03 15:10:15 +00:00
|
|
|
V8NameConverter v8NameConverter(code);
|
|
|
|
DecodeIt(f, v8NameConverter, begin, end);
|
|
|
|
}
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#else // ENABLE_DISASSEMBLER
|
|
|
|
|
|
|
|
void Disassembler::Dump(FILE* f, byte* begin, byte* end) {}
|
|
|
|
int Disassembler::Decode(FILE* f, byte* begin, byte* end) { return 0; }
|
|
|
|
void Disassembler::Decode(FILE* f, Code* code) {}
|
|
|
|
|
|
|
|
#endif // ENABLE_DISASSEMBLER
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|