e0316441afef180e222ce30b908adbc425787caf
[platform/upstream/nodejs.git] / deps / v8 / src / disassembler.cc
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.
4
5 #include "src/v8.h"
6
7 #include "src/code-stubs.h"
8 #include "src/codegen.h"
9 #include "src/debug.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"
16
17 namespace v8 {
18 namespace internal {
19
20 #ifdef ENABLE_DISASSEMBLER
21
22 class 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_;
30
31   EmbeddedVector<char, 128> v8_buffer_;
32 };
33
34
35 const char* V8NameConverter::NameOfAddress(byte* pc) const {
36   const char* name = code_->GetIsolate()->builtins()->Lookup(pc);
37   if (name != NULL) {
38     SNPrintF(v8_buffer_, "%s  (%p)", name, pc);
39     return v8_buffer_.start();
40   }
41
42   if (code_ != NULL) {
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();
48     }
49   }
50
51   return disasm::NameConverter::NameOfAddress(pc);
52 }
53
54
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) : "";
59 }
60
61
62 static void DumpBuffer(std::ostream* os, StringBuilder* out) {
63   (*os) << out->Finalize() << std::endl;
64   out->Reset();
65 }
66
67
68 static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
69 static const int kRelocInfoPosition = 57;
70
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);
76
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());
80   byte* pc = begin;
81   disasm::Disassembler d(converter);
82   RelocIterator* it = NULL;
83   if (converter.code() != NULL) {
84     it = new RelocIterator(converter.code());
85   } else {
86     // No relocation information when printing code stubs.
87   }
88 #if !V8_TARGET_ARCH_PPC
89   int constants = -1;  // no constants being decoded at the start
90 #endif
91
92   while (pc < end) {
93     // First decode instruction so that we know its length.
94     byte* prev_pc = pc;
95 #if !V8_TARGET_ARCH_PPC
96     if (constants > 0) {
97       SNPrintF(decode_buffer,
98                "%08x       constant",
99                *reinterpret_cast<int32_t*>(pc));
100       constants--;
101       pc += 4;
102     } else {
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;
109         pc += 4;
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),
117                  ptr - begin);
118         pc += sizeof(ptr);
119       } else {
120         decode_buffer[0] = '\0';
121         pc += d.InstructionDecode(decode_buffer, pc);
122       }
123     }
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.
129     int skip;
130     if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
131         it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE &&
132         (skip = Assembler::DecodeInternalReference(decode_buffer, pc))) {
133       pc += skip;
134     } else {
135       decode_buffer[0] = '\0';
136       pc += d.InstructionDecode(decode_buffer, pc);
137     }
138 #else
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
143
144     // Collect RelocInfo for this instruction (prev_pc .. pc-1)
145     List<const char*> comments(4);
146     List<byte*> pcs(1);
147     List<RelocInfo::Mode> rmodes(1);
148     List<intptr_t> datas(1);
149     if (it != NULL) {
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()));
154         } else {
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());
159         }
160         it->next();
161       }
162     }
163
164     // Comments.
165     for (int i = 0; i < comments.length(); i++) {
166       out.AddFormatted("                  %s", comments[i]);
167       DumpBuffer(os, &out);
168     }
169
170     // Instruction address and instruction offset.
171     out.AddFormatted("%p  %4d  ", prev_pc, prev_pc - begin);
172
173     // Instruction.
174     out.AddFormatted("%s", decode_buffer.start());
175
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());
180
181       // Indent the printing of the reloc info.
182       if (i == 0) {
183         // The first reloc info is printed after the disassembled instruction.
184         out.AddPadding(' ', kRelocInfoPosition - out.position());
185       } else {
186         // Additional reloc infos are printed on separate lines.
187         DumpBuffer(os, &out);
188         out.AddPadding(' ', kRelocInfoPosition);
189       }
190
191       RelocInfo::Mode rmode = relocinfo.rmode();
192       if (RelocInfo::IsPosition(rmode)) {
193         if (RelocInfo::IsStatementPosition(rmode)) {
194           out.AddFormatted("    ;; debug: statement %d", relocinfo.data());
195         } else {
196           out.AddFormatted("    ;; debug: position %d", relocinfo.data());
197         }
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,");
217         }
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()) ==
223                   CONTEXTUAL) {
224             out.AddFormatted(" contextual,");
225           }
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));
232           }
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));
241           switch (major_key) {
242             case CodeStub::CallFunction: {
243               int argc = CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
244               out.AddFormatted("argc = %d", argc);
245               break;
246             }
247             default:
248               out.AddFormatted("minor: %d", minor_key);
249           }
250         } else {
251           out.AddFormatted(" %s", Code::Kind2String(kind));
252         }
253         if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
254           out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
255         }
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,
261                                                   addr,
262                                                   Deoptimizer::EAGER);
263         if (id == Deoptimizer::kNotDeoptimizationEntry) {
264           id = Deoptimizer::GetDeoptimizationId(isolate,
265                                                 addr,
266                                                 Deoptimizer::LAZY);
267           if (id == Deoptimizer::kNotDeoptimizationEntry) {
268             id = Deoptimizer::GetDeoptimizationId(isolate,
269                                                   addr,
270                                                   Deoptimizer::SOFT);
271             if (id == Deoptimizer::kNotDeoptimizationEntry) {
272               out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
273             } else {
274               out.AddFormatted("    ;; soft deoptimization bailout %d", id);
275             }
276           } else {
277             out.AddFormatted("    ;; lazy deoptimization bailout %d", id);
278           }
279         } else {
280           out.AddFormatted("    ;; deoptimization bailout %d", id);
281         }
282       } else {
283         out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
284       }
285     }
286     DumpBuffer(os, &out);
287   }
288
289   // Emit comments following the last instruction (if any).
290   if (it != NULL) {
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);
296       }
297     }
298   }
299
300   delete it;
301   return static_cast<int>(pc - begin);
302 }
303
304
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);
309 }
310
311 #else  // ENABLE_DISASSEMBLER
312
313 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
314                          byte* end, Code* code) {
315   return 0;
316 }
317
318 #endif  // ENABLE_DISASSEMBLER
319
320 } }  // namespace v8::internal