1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/code-stubs.h"
8 #include "src/codegen.h"
10 #include "src/deoptimizer.h"
11 #include "src/disasm.h"
12 #include "src/disassembler.h"
13 #include "src/macro-assembler.h"
14 #include "src/snapshot/serialize.h"
15 #include "src/string-stream.h"
20 #ifdef ENABLE_DISASSEMBLER
22 class V8NameConverter: public disasm::NameConverter {
24 explicit V8NameConverter(Code* code) : code_(code) {}
25 virtual const char* NameOfAddress(byte* pc) const;
26 virtual const char* NameInCode(byte* addr) const;
27 Code* code() const { return code_; }
31 EmbeddedVector<char, 128> v8_buffer_;
35 const char* V8NameConverter::NameOfAddress(byte* pc) const {
36 const char* name = code_->GetIsolate()->builtins()->Lookup(pc);
38 SNPrintF(v8_buffer_, "%s (%p)", name, pc);
39 return v8_buffer_.start();
43 int offs = static_cast<int>(pc - code_->instruction_start());
44 // print as code offset, if it seems reasonable
45 if (0 <= offs && offs < code_->instruction_size()) {
46 SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
47 return v8_buffer_.start();
51 return disasm::NameConverter::NameOfAddress(pc);
55 const char* V8NameConverter::NameInCode(byte* addr) const {
56 // The V8NameConverter is used for well known code, so we can "safely"
57 // dereference pointers in generated code.
58 return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : "";
62 static void DumpBuffer(std::ostream* os, StringBuilder* out) {
63 (*os) << out->Finalize() << std::endl;
68 static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
69 static const int kRelocInfoPosition = 57;
71 static int DecodeIt(Isolate* isolate, std::ostream* os,
72 const V8NameConverter& converter, byte* begin, byte* end) {
73 SealHandleScope shs(isolate);
74 DisallowHeapAllocation no_alloc;
75 ExternalReferenceEncoder ref_encoder(isolate);
77 v8::internal::EmbeddedVector<char, 128> decode_buffer;
78 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
79 StringBuilder out(out_buffer.start(), out_buffer.length());
81 disasm::Disassembler d(converter);
82 RelocIterator* it = NULL;
83 if (converter.code() != NULL) {
84 it = new RelocIterator(converter.code());
86 // No relocation information when printing code stubs.
88 int constants = -1; // no constants being decoded at the start
91 // First decode instruction so that we know its length.
94 SNPrintF(decode_buffer,
96 *reinterpret_cast<int32_t*>(pc));
100 int num_const = d.ConstantPoolSizeAt(pc);
101 if (num_const >= 0) {
102 SNPrintF(decode_buffer,
103 "%08x constant pool begin",
104 *reinterpret_cast<int32_t*>(pc));
105 constants = num_const;
107 } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
108 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
109 // raw pointer embedded in code stream, e.g., jump table
110 byte* ptr = *reinterpret_cast<byte**>(pc);
111 SNPrintF(decode_buffer,
112 "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
113 reinterpret_cast<intptr_t>(ptr),
117 decode_buffer[0] = '\0';
118 pc += d.InstructionDecode(decode_buffer, pc);
122 // Collect RelocInfo for this instruction (prev_pc .. pc-1)
123 List<const char*> comments(4);
125 List<RelocInfo::Mode> rmodes(1);
126 List<intptr_t> datas(1);
128 while (!it->done() && it->rinfo()->pc() < pc) {
129 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
130 // For comments just collect the text.
131 comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
133 // For other reloc info collect all data.
134 pcs.Add(it->rinfo()->pc());
135 rmodes.Add(it->rinfo()->rmode());
136 datas.Add(it->rinfo()->data());
143 for (int i = 0; i < comments.length(); i++) {
144 out.AddFormatted(" %s", comments[i]);
145 DumpBuffer(os, &out);
148 // Instruction address and instruction offset.
149 out.AddFormatted("%p %4d ", prev_pc, prev_pc - begin);
152 out.AddFormatted("%s", decode_buffer.start());
154 // Print all the reloc info for this instruction which are not comments.
155 for (int i = 0; i < pcs.length(); i++) {
156 // Put together the reloc info
157 RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], converter.code());
159 // Indent the printing of the reloc info.
161 // The first reloc info is printed after the disassembled instruction.
162 out.AddPadding(' ', kRelocInfoPosition - out.position());
164 // Additional reloc infos are printed on separate lines.
165 DumpBuffer(os, &out);
166 out.AddPadding(' ', kRelocInfoPosition);
169 RelocInfo::Mode rmode = relocinfo.rmode();
170 if (RelocInfo::IsPosition(rmode)) {
171 if (RelocInfo::IsStatementPosition(rmode)) {
172 out.AddFormatted(" ;; debug: statement %d", relocinfo.data());
174 out.AddFormatted(" ;; debug: position %d", relocinfo.data());
176 } else if (rmode == RelocInfo::DEOPT_REASON) {
177 Deoptimizer::DeoptReason reason =
178 static_cast<Deoptimizer::DeoptReason>(relocinfo.data());
179 out.AddFormatted(" ;; debug: deopt reason '%s'",
180 Deoptimizer::GetDeoptReason(reason));
181 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
182 HeapStringAllocator allocator;
183 StringStream accumulator(&allocator);
184 relocinfo.target_object()->ShortPrint(&accumulator);
185 SmartArrayPointer<const char> obj_name = accumulator.ToCString();
186 out.AddFormatted(" ;; object: %s", obj_name.get());
187 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
188 const char* reference_name = ref_encoder.NameOfAddress(
189 isolate, relocinfo.target_external_reference());
190 out.AddFormatted(" ;; external reference (%s)", reference_name);
191 } else if (RelocInfo::IsCodeTarget(rmode)) {
192 out.AddFormatted(" ;; code:");
193 if (rmode == RelocInfo::CONSTRUCT_CALL) {
194 out.AddFormatted(" constructor,");
196 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
197 Code::Kind kind = code->kind();
198 if (code->is_inline_cache_stub()) {
199 if (kind == Code::LOAD_IC &&
200 LoadICState::GetContextualMode(code->extra_ic_state()) ==
202 out.AddFormatted(" contextual,");
204 InlineCacheState ic_state = code->ic_state();
205 out.AddFormatted(" %s, %s", Code::Kind2String(kind),
206 Code::ICState2String(ic_state));
207 if (ic_state == MONOMORPHIC) {
208 Code::StubType type = code->type();
209 out.AddFormatted(", %s", Code::StubType2String(type));
211 } else if (kind == Code::STUB || kind == Code::HANDLER) {
212 // Get the STUB key and extract major and minor key.
213 uint32_t key = code->stub_key();
214 uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
215 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
216 DCHECK(major_key == CodeStub::MajorKeyFromKey(key));
217 out.AddFormatted(" %s, %s, ", Code::Kind2String(kind),
218 CodeStub::MajorName(major_key, false));
220 case CodeStub::CallFunction: {
221 int argc = CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
222 out.AddFormatted("argc = %d", argc);
226 out.AddFormatted("minor: %d", minor_key);
229 out.AddFormatted(" %s", Code::Kind2String(kind));
231 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
232 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
234 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
235 isolate->deoptimizer_data() != NULL) {
236 // A runtime entry reloinfo might be a deoptimization bailout.
237 Address addr = relocinfo.target_address();
238 int id = Deoptimizer::GetDeoptimizationId(isolate,
241 if (id == Deoptimizer::kNotDeoptimizationEntry) {
242 id = Deoptimizer::GetDeoptimizationId(isolate,
245 if (id == Deoptimizer::kNotDeoptimizationEntry) {
246 id = Deoptimizer::GetDeoptimizationId(isolate,
249 if (id == Deoptimizer::kNotDeoptimizationEntry) {
250 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
252 out.AddFormatted(" ;; soft deoptimization bailout %d", id);
255 out.AddFormatted(" ;; lazy deoptimization bailout %d", id);
258 out.AddFormatted(" ;; deoptimization bailout %d", id);
261 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
264 DumpBuffer(os, &out);
267 // Emit comments following the last instruction (if any).
269 for ( ; !it->done(); it->next()) {
270 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
271 out.AddFormatted(" %s",
272 reinterpret_cast<const char*>(it->rinfo()->data()));
273 DumpBuffer(os, &out);
279 return static_cast<int>(pc - begin);
283 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
284 byte* end, Code* code) {
285 V8NameConverter v8NameConverter(code);
286 return DecodeIt(isolate, os, v8NameConverter, begin, end);
289 #else // ENABLE_DISASSEMBLER
291 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
292 byte* end, Code* code) {
296 #endif // ENABLE_DISASSEMBLER
298 } } // namespace v8::internal