deps: update v8 to 4.3.61.21
[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/snapshot/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   int constants = -1;  // no constants being decoded at the start
89
90   while (pc < end) {
91     // First decode instruction so that we know its length.
92     byte* prev_pc = pc;
93     if (constants > 0) {
94       SNPrintF(decode_buffer,
95                "%08x       constant",
96                *reinterpret_cast<int32_t*>(pc));
97       constants--;
98       pc += 4;
99     } else {
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;
106         pc += 4;
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),
114                  ptr - begin);
115         pc += sizeof(ptr);
116       } else {
117         decode_buffer[0] = '\0';
118         pc += d.InstructionDecode(decode_buffer, pc);
119       }
120     }
121
122     // Collect RelocInfo for this instruction (prev_pc .. pc-1)
123     List<const char*> comments(4);
124     List<byte*> pcs(1);
125     List<RelocInfo::Mode> rmodes(1);
126     List<intptr_t> datas(1);
127     if (it != NULL) {
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()));
132         } else {
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());
137         }
138         it->next();
139       }
140     }
141
142     // Comments.
143     for (int i = 0; i < comments.length(); i++) {
144       out.AddFormatted("                  %s", comments[i]);
145       DumpBuffer(os, &out);
146     }
147
148     // Instruction address and instruction offset.
149     out.AddFormatted("%p  %4d  ", prev_pc, prev_pc - begin);
150
151     // Instruction.
152     out.AddFormatted("%s", decode_buffer.start());
153
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());
158
159       // Indent the printing of the reloc info.
160       if (i == 0) {
161         // The first reloc info is printed after the disassembled instruction.
162         out.AddPadding(' ', kRelocInfoPosition - out.position());
163       } else {
164         // Additional reloc infos are printed on separate lines.
165         DumpBuffer(os, &out);
166         out.AddPadding(' ', kRelocInfoPosition);
167       }
168
169       RelocInfo::Mode rmode = relocinfo.rmode();
170       if (RelocInfo::IsPosition(rmode)) {
171         if (RelocInfo::IsStatementPosition(rmode)) {
172           out.AddFormatted("    ;; debug: statement %d", relocinfo.data());
173         } else {
174           out.AddFormatted("    ;; debug: position %d", relocinfo.data());
175         }
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,");
195         }
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()) ==
201                   CONTEXTUAL) {
202             out.AddFormatted(" contextual,");
203           }
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));
210           }
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));
219           switch (major_key) {
220             case CodeStub::CallFunction: {
221               int argc = CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
222               out.AddFormatted("argc = %d", argc);
223               break;
224             }
225             default:
226               out.AddFormatted("minor: %d", minor_key);
227           }
228         } else {
229           out.AddFormatted(" %s", Code::Kind2String(kind));
230         }
231         if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
232           out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
233         }
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,
239                                                   addr,
240                                                   Deoptimizer::EAGER);
241         if (id == Deoptimizer::kNotDeoptimizationEntry) {
242           id = Deoptimizer::GetDeoptimizationId(isolate,
243                                                 addr,
244                                                 Deoptimizer::LAZY);
245           if (id == Deoptimizer::kNotDeoptimizationEntry) {
246             id = Deoptimizer::GetDeoptimizationId(isolate,
247                                                   addr,
248                                                   Deoptimizer::SOFT);
249             if (id == Deoptimizer::kNotDeoptimizationEntry) {
250               out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
251             } else {
252               out.AddFormatted("    ;; soft deoptimization bailout %d", id);
253             }
254           } else {
255             out.AddFormatted("    ;; lazy deoptimization bailout %d", id);
256           }
257         } else {
258           out.AddFormatted("    ;; deoptimization bailout %d", id);
259         }
260       } else {
261         out.AddFormatted("    ;; %s", RelocInfo::RelocModeName(rmode));
262       }
263     }
264     DumpBuffer(os, &out);
265   }
266
267   // Emit comments following the last instruction (if any).
268   if (it != NULL) {
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);
274       }
275     }
276   }
277
278   delete it;
279   return static_cast<int>(pc - begin);
280 }
281
282
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);
287 }
288
289 #else  // ENABLE_DISASSEMBLER
290
291 int Disassembler::Decode(Isolate* isolate, std::ostream* os, byte* begin,
292                          byte* end, Code* code) {
293   return 0;
294 }
295
296 #endif  // ENABLE_DISASSEMBLER
297
298 } }  // namespace v8::internal