blob: 8fe1e7feddb69a8e73d95627d71dd574e5861442 [file] [log] [blame]
Ben Murdoch086aeea2011-05-13 15:57:08 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/disassembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/code-stubs.h"
8#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/deoptimizer.h"
11#include "src/disasm.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010012#include "src/ic/ic.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/macro-assembler.h"
Ben Murdochda12d292016-06-02 14:46:10 +010014#include "src/snapshot/serializer-common.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/string-stream.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000016
17namespace v8 {
18namespace internal {
19
20#ifdef ENABLE_DISASSEMBLER
21
Steve Blocka7e24c12009-10-30 11:49:00 +000022class V8NameConverter: public disasm::NameConverter {
23 public:
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_; }
28 private:
29 Code* code_;
Steve Block44f0eee2011-05-26 01:26:41 +010030
31 EmbeddedVector<char, 128> v8_buffer_;
Steve Blocka7e24c12009-10-30 11:49:00 +000032};
33
34
35const char* V8NameConverter::NameOfAddress(byte* pc) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 const char* name =
37 code_ == NULL ? NULL : code_->GetIsolate()->builtins()->Lookup(pc);
38
Steve Blocka7e24c12009-10-30 11:49:00 +000039 if (name != NULL) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010040 SNPrintF(v8_buffer_, "%s (%p)", name, static_cast<void*>(pc));
Steve Block44f0eee2011-05-26 01:26:41 +010041 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000042 }
43
44 if (code_ != NULL) {
Steve Blockd0582a62009-12-15 09:54:21 +000045 int offs = static_cast<int>(pc - code_->instruction_start());
Steve Blocka7e24c12009-10-30 11:49:00 +000046 // print as code offset, if it seems reasonable
47 if (0 <= offs && offs < code_->instruction_size()) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010048 SNPrintF(v8_buffer_, "%d (%p)", offs, static_cast<void*>(pc));
Steve Block44f0eee2011-05-26 01:26:41 +010049 return v8_buffer_.start();
Steve Blocka7e24c12009-10-30 11:49:00 +000050 }
51 }
52
53 return disasm::NameConverter::NameOfAddress(pc);
54}
55
56
57const char* V8NameConverter::NameInCode(byte* addr) const {
58 // The V8NameConverter is used for well known code, so we can "safely"
59 // dereference pointers in generated code.
60 return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : "";
61}
62
63
Emily Bernierd0a1eb72015-03-24 16:35:39 -040064static void DumpBuffer(std::ostream* os, StringBuilder* out) {
65 (*os) << out->Finalize() << std::endl;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000066 out->Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +000067}
68
Ben Murdoch69a99ed2011-11-30 16:03:39 +000069
Steve Blocka7e24c12009-10-30 11:49:00 +000070static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
71static const int kRelocInfoPosition = 57;
72
Emily Bernierd0a1eb72015-03-24 16:35:39 -040073static int DecodeIt(Isolate* isolate, std::ostream* os,
74 const V8NameConverter& converter, byte* begin, byte* end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 SealHandleScope shs(isolate);
76 DisallowHeapAllocation no_alloc;
77 ExternalReferenceEncoder ref_encoder(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000078
79 v8::internal::EmbeddedVector<char, 128> decode_buffer;
80 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000081 StringBuilder out(out_buffer.start(), out_buffer.length());
Steve Blocka7e24c12009-10-30 11:49:00 +000082 byte* pc = begin;
83 disasm::Disassembler d(converter);
84 RelocIterator* it = NULL;
85 if (converter.code() != NULL) {
86 it = new RelocIterator(converter.code());
87 } else {
88 // No relocation information when printing code stubs.
89 }
90 int constants = -1; // no constants being decoded at the start
91
92 while (pc < end) {
93 // First decode instruction so that we know its length.
94 byte* prev_pc = pc;
95 if (constants > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 SNPrintF(decode_buffer,
97 "%08x constant",
98 *reinterpret_cast<int32_t*>(pc));
Steve Blocka7e24c12009-10-30 11:49:00 +000099 constants--;
100 pc += 4;
101 } else {
102 int num_const = d.ConstantPoolSizeAt(pc);
103 if (num_const >= 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 SNPrintF(decode_buffer,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105 "%08x constant pool begin (num_const = %d)",
106 *reinterpret_cast<int32_t*>(pc), num_const);
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 constants = num_const;
108 pc += 4;
109 } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
110 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
111 // raw pointer embedded in code stream, e.g., jump table
112 byte* ptr = *reinterpret_cast<byte**>(pc);
Ben Murdochc5610432016-08-08 18:44:38 +0100113 SNPrintF(
114 decode_buffer, "%08" V8PRIxPTR " jump table entry %4" PRIuS,
115 reinterpret_cast<intptr_t>(ptr), static_cast<size_t>(ptr - begin));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 pc += sizeof(ptr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 } else {
118 decode_buffer[0] = '\0';
119 pc += d.InstructionDecode(decode_buffer, pc);
120 }
121 }
122
123 // Collect RelocInfo for this instruction (prev_pc .. pc-1)
124 List<const char*> comments(4);
125 List<byte*> pcs(1);
126 List<RelocInfo::Mode> rmodes(1);
127 List<intptr_t> datas(1);
128 if (it != NULL) {
129 while (!it->done() && it->rinfo()->pc() < pc) {
130 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
131 // For comments just collect the text.
132 comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
133 } else {
134 // For other reloc info collect all data.
135 pcs.Add(it->rinfo()->pc());
136 rmodes.Add(it->rinfo()->rmode());
137 datas.Add(it->rinfo()->data());
138 }
139 it->next();
140 }
141 }
142
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 // Comments.
144 for (int i = 0; i < comments.length(); i++) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000145 out.AddFormatted(" %s", comments[i]);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146 DumpBuffer(os, &out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 }
148
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 // Instruction address and instruction offset.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100150 out.AddFormatted("%p %4" V8PRIdPTRDIFF " ", static_cast<void*>(prev_pc),
151 prev_pc - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 // Instruction.
154 out.AddFormatted("%s", decode_buffer.start());
155
156 // Print all the reloc info for this instruction which are not comments.
157 for (int i = 0; i < pcs.length(); i++) {
158 // Put together the reloc info
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 RelocInfo relocinfo(isolate, pcs[i], rmodes[i], datas[i],
160 converter.code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // Indent the printing of the reloc info.
163 if (i == 0) {
164 // The first reloc info is printed after the disassembled instruction.
165 out.AddPadding(' ', kRelocInfoPosition - out.position());
166 } else {
167 // Additional reloc infos are printed on separate lines.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400168 DumpBuffer(os, &out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 out.AddPadding(' ', kRelocInfoPosition);
170 }
171
172 RelocInfo::Mode rmode = relocinfo.rmode();
173 if (RelocInfo::IsPosition(rmode)) {
174 if (RelocInfo::IsStatementPosition(rmode)) {
Ben Murdochc5610432016-08-08 18:44:38 +0100175 out.AddFormatted(" ;; debug: statement %" V8PRIdPTR,
176 relocinfo.data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 } else {
Ben Murdochc5610432016-08-08 18:44:38 +0100178 out.AddFormatted(" ;; debug: position %" V8PRIdPTR,
179 relocinfo.data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181 } else if (rmode == RelocInfo::DEOPT_REASON) {
182 Deoptimizer::DeoptReason reason =
183 static_cast<Deoptimizer::DeoptReason>(relocinfo.data());
184 out.AddFormatted(" ;; debug: deopt reason '%s'",
185 Deoptimizer::GetDeoptReason(reason));
Ben Murdochc5610432016-08-08 18:44:38 +0100186 } else if (rmode == RelocInfo::DEOPT_ID) {
187 out.AddFormatted(" ;; debug: deopt index %d",
188 static_cast<int>(relocinfo.data()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
190 HeapStringAllocator allocator;
191 StringStream accumulator(&allocator);
192 relocinfo.target_object()->ShortPrint(&accumulator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 base::SmartArrayPointer<const char> obj_name = accumulator.ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 out.AddFormatted(" ;; object: %s", obj_name.get());
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 const char* reference_name = ref_encoder.NameOfAddress(
197 isolate, relocinfo.target_external_reference());
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 out.AddFormatted(" ;; external reference (%s)", reference_name);
199 } else if (RelocInfo::IsCodeTarget(rmode)) {
200 out.AddFormatted(" ;; code:");
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
202 Code::Kind kind = code->kind();
203 if (code->is_inline_cache_stub()) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100204 if (kind == Code::LOAD_GLOBAL_IC &&
205 LoadGlobalICState::GetTypeofMode(code->extra_ic_state()) ==
206 INSIDE_TYPEOF) {
207 out.AddFormatted(" inside typeof,");
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100209 out.AddFormatted(" %s", Code::Kind2String(kind));
210 if (!IC::ICUseVector(kind)) {
211 InlineCacheState ic_state = IC::StateFromCode(code);
212 out.AddFormatted(" %s", Code::ICState2String(ic_state));
213 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 } else if (kind == Code::STUB || kind == Code::HANDLER) {
215 // Get the STUB key and extract major and minor key.
216 uint32_t key = code->stub_key();
217 uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
218 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
219 DCHECK(major_key == CodeStub::MajorKeyFromKey(key));
220 out.AddFormatted(" %s, %s, ", Code::Kind2String(kind),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 CodeStub::MajorName(major_key));
222 out.AddFormatted("minor: %d", minor_key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 } else {
224 out.AddFormatted(" %s", Code::Kind2String(kind));
225 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000226 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
227 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
228 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 } else if (RelocInfo::IsRuntimeEntry(rmode) &&
230 isolate->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231 // A runtime entry reloinfo might be a deoptimization bailout.
232 Address addr = relocinfo.target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 int id = Deoptimizer::GetDeoptimizationId(isolate,
234 addr,
235 Deoptimizer::EAGER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236 if (id == Deoptimizer::kNotDeoptimizationEntry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 id = Deoptimizer::GetDeoptimizationId(isolate,
238 addr,
239 Deoptimizer::LAZY);
240 if (id == Deoptimizer::kNotDeoptimizationEntry) {
241 id = Deoptimizer::GetDeoptimizationId(isolate,
242 addr,
243 Deoptimizer::SOFT);
244 if (id == Deoptimizer::kNotDeoptimizationEntry) {
245 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
246 } else {
247 out.AddFormatted(" ;; soft deoptimization bailout %d", id);
248 }
249 } else {
250 out.AddFormatted(" ;; lazy deoptimization bailout %d", id);
251 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100252 } else {
253 out.AddFormatted(" ;; deoptimization bailout %d", id);
254 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 } else {
256 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
257 }
258 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400259 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000260 }
261
262 // Emit comments following the last instruction (if any).
263 if (it != NULL) {
264 for ( ; !it->done(); it->next()) {
265 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
266 out.AddFormatted(" %s",
267 reinterpret_cast<const char*>(it->rinfo()->data()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400268 DumpBuffer(os, &out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000269 }
270 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 }
272
273 delete it;
Steve Blockd0582a62009-12-15 09:54:21 +0000274 return static_cast<int>(pc - begin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000275}
276
277
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400278int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
279 byte* end, Code* code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 V8NameConverter v8NameConverter(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400281 return DecodeIt(isolate, os, v8NameConverter, begin, end);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284#else // ENABLE_DISASSEMBLER
285
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400286int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
287 byte* end, Code* code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 return 0;
289}
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291#endif // ENABLE_DISASSEMBLER
292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293} // namespace internal
294} // namespace v8