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/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 #if !V8_TARGET_ARCH_PPC
89 int constants = -1; // no constants being decoded at the start
93 // First decode instruction so that we know its length.
95 #if !V8_TARGET_ARCH_PPC
97 SNPrintF(decode_buffer,
99 *reinterpret_cast<int32_t*>(pc));
103 int num_const = d.ConstantPoolSizeAt(pc);
104 if (num_const >= 0) {
105 SNPrintF(decode_buffer,
106 "%08x constant pool begin",
107 *reinterpret_cast<int32_t*>(pc));
108 constants = num_const;
110 } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
111 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
112 // raw pointer embedded in code stream, e.g., jump table
113 byte* ptr = *reinterpret_cast<byte**>(pc);
114 SNPrintF(decode_buffer,
115 "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
116 reinterpret_cast<intptr_t>(ptr),
120 decode_buffer[0] = '\0';
121 pc += d.InstructionDecode(decode_buffer, pc);
124 #else // !V8_TARGET_ARCH_PPC
125 #if ABI_USES_FUNCTION_DESCRIPTORS || V8_OOL_CONSTANT_POOL
126 // Function descriptors are specially decoded and skipped.
127 // Other internal references (load of ool constant pool pointer)
128 // are not since they are a encoded as a regular mov sequence.
130 if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
131 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE &&
132 (skip = Assembler::DecodeInternalReference(decode_buffer, pc))) {
135 decode_buffer[0] = '\0';
136 pc += d.InstructionDecode(decode_buffer, pc);
139 decode_buffer[0] = '\0';
140 pc += d.InstructionDecode(decode_buffer, pc);
141 #endif // ABI_USES_FUNCTION_DESCRIPTORS || V8_OOL_CONSTANT_POOL
142 #endif // !V8_TARGET_ARCH_PPC
144 // Collect RelocInfo for this instruction (prev_pc .. pc-1)
145 List<const char*> comments(4);
147 List<RelocInfo::Mode> rmodes(1);
148 List<intptr_t> datas(1);
150 while (!it->done() && it->rinfo()->pc() < pc) {
151 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
152 // For comments just collect the text.
153 comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
155 // For other reloc info collect all data.
156 pcs.Add(it->rinfo()->pc());
157 rmodes.Add(it->rinfo()->rmode());
158 datas.Add(it->rinfo()->data());
165 for (int i = 0; i < comments.length(); i++) {
166 out.AddFormatted(" %s", comments[i]);
167 DumpBuffer(os, &out);
170 // Instruction address and instruction offset.
171 out.AddFormatted("%p %4d ", prev_pc, prev_pc - begin);
174 out.AddFormatted("%s", decode_buffer.start());
176 // Print all the reloc info for this instruction which are not comments.
177 for (int i = 0; i < pcs.length(); i++) {
178 // Put together the reloc info
179 RelocInfo relocinfo(pcs[i], rmodes[i], datas[i], converter.code());
181 // Indent the printing of the reloc info.
183 // The first reloc info is printed after the disassembled instruction.
184 out.AddPadding(' ', kRelocInfoPosition - out.position());
186 // Additional reloc infos are printed on separate lines.
187 DumpBuffer(os, &out);
188 out.AddPadding(' ', kRelocInfoPosition);
191 RelocInfo::Mode rmode = relocinfo.rmode();
192 if (RelocInfo::IsPosition(rmode)) {
193 if (RelocInfo::IsStatementPosition(rmode)) {
194 out.AddFormatted(" ;; debug: statement %d", relocinfo.data());
196 out.AddFormatted(" ;; debug: position %d", relocinfo.data());
198 } else if (rmode == RelocInfo::DEOPT_REASON) {
199 Deoptimizer::DeoptReason reason =
200 static_cast<Deoptimizer::DeoptReason>(relocinfo.data());
201 out.AddFormatted(" ;; debug: deopt reason '%s'",
202 Deoptimizer::GetDeoptReason(reason));
203 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
204 HeapStringAllocator allocator;
205 StringStream accumulator(&allocator);
206 relocinfo.target_object()->ShortPrint(&accumulator);
207 SmartArrayPointer<const char> obj_name = accumulator.ToCString();
208 out.AddFormatted(" ;; object: %s", obj_name.get());
209 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
210 const char* reference_name =
211 ref_encoder.NameOfAddress(relocinfo.target_reference());
212 out.AddFormatted(" ;; external reference (%s)", reference_name);
213 } else if (RelocInfo::IsCodeTarget(rmode)) {
214 out.AddFormatted(" ;; code:");
215 if (rmode == RelocInfo::CONSTRUCT_CALL) {
216 out.AddFormatted(" constructor,");
218 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
219 Code::Kind kind = code->kind();
220 if (code->is_inline_cache_stub()) {
221 if (kind == Code::LOAD_IC &&
222 LoadICState::GetContextualMode(code->extra_ic_state()) ==
224 out.AddFormatted(" contextual,");
226 InlineCacheState ic_state = code->ic_state();
227 out.AddFormatted(" %s, %s", Code::Kind2String(kind),
228 Code::ICState2String(ic_state));
229 if (ic_state == MONOMORPHIC) {
230 Code::StubType type = code->type();
231 out.AddFormatted(", %s", Code::StubType2String(type));
233 } else if (kind == Code::STUB || kind == Code::HANDLER) {
234 // Get the STUB key and extract major and minor key.
235 uint32_t key = code->stub_key();
236 uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
237 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
238 DCHECK(major_key == CodeStub::MajorKeyFromKey(key));
239 out.AddFormatted(" %s, %s, ", Code::Kind2String(kind),
240 CodeStub::MajorName(major_key, false));
242 case CodeStub::CallFunction: {
243 int argc = CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
244 out.AddFormatted("argc = %d", argc);
248 out.AddFormatted("minor: %d", minor_key);
251 out.AddFormatted(" %s", Code::Kind2String(kind));
253 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
254 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
256 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
257 isolate->deoptimizer_data() != NULL) {
258 // A runtime entry reloinfo might be a deoptimization bailout.
259 Address addr = relocinfo.target_address();
260 int id = Deoptimizer::GetDeoptimizationId(isolate,
263 if (id == Deoptimizer::kNotDeoptimizationEntry) {
264 id = Deoptimizer::GetDeoptimizationId(isolate,
267 if (id == Deoptimizer::kNotDeoptimizationEntry) {
268 id = Deoptimizer::GetDeoptimizationId(isolate,
271 if (id == Deoptimizer::kNotDeoptimizationEntry) {
272 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
274 out.AddFormatted(" ;; soft deoptimization bailout %d", id);
277 out.AddFormatted(" ;; lazy deoptimization bailout %d", id);
280 out.AddFormatted(" ;; deoptimization bailout %d", id);
283 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
286 DumpBuffer(os, &out);
289 // Emit comments following the last instruction (if any).
291 for ( ; !it->done(); it->next()) {
292 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
293 out.AddFormatted(" %s",
294 reinterpret_cast<const char*>(it->rinfo()->data()));
295 DumpBuffer(os, &out);
301 return static_cast<int>(pc - begin);
305 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
306 byte* end, Code* code) {
307 V8NameConverter v8NameConverter(code);
308 return DecodeIt(isolate, os, v8NameConverter, begin, end);
311 #else // ENABLE_DISASSEMBLER
313 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
314 byte* end, Code* code) {
318 #endif // ENABLE_DISASSEMBLER
320 } } // namespace v8::internal