blob: 368c3a89c1ad31c4e444ef8dead572da476b702b [file] [log] [blame]
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "code-stubs.h"
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000031#include "codegen.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033#include "deoptimizer.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "disasm.h"
35#include "disassembler.h"
36#include "macro-assembler.h"
37#include "serialize.h"
38#include "string-stream.h"
39
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef ENABLE_DISASSEMBLER
44
45void Disassembler::Dump(FILE* f, byte* begin, byte* end) {
46 for (byte* pc = begin; pc < end; pc++) {
47 if (f == NULL) {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +000048 PrintF("%" V8PRIxPTR " %4" V8PRIdPTR " %02x\n",
49 reinterpret_cast<intptr_t>(pc),
50 pc - begin,
51 *pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 } else {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000053 fprintf(f, "%" V8PRIxPTR " %4" V8PRIdPTR " %02x\n",
54 reinterpret_cast<uintptr_t>(pc), pc - begin, *pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 }
56 }
57}
58
59
60class V8NameConverter: public disasm::NameConverter {
61 public:
62 explicit V8NameConverter(Code* code) : code_(code) {}
63 virtual const char* NameOfAddress(byte* pc) const;
kasper.lund7276f142008-07-30 08:49:36 +000064 virtual const char* NameInCode(byte* addr) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065 Code* code() const { return code_; }
66 private:
67 Code* code_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068
69 EmbeddedVector<char, 128> v8_buffer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070};
71
72
73const char* V8NameConverter::NameOfAddress(byte* pc) const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 const char* name = Isolate::Current()->builtins()->Lookup(pc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 if (name != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000076 OS::SNPrintF(v8_buffer_, "%s (%p)", name, pc);
77 return v8_buffer_.start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 }
79
80 if (code_ != NULL) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000081 int offs = static_cast<int>(pc - code_->instruction_start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 // print as code offset, if it seems reasonable
83 if (0 <= offs && offs < code_->instruction_size()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 OS::SNPrintF(v8_buffer_, "%d (%p)", offs, pc);
85 return v8_buffer_.start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086 }
87 }
88
89 return disasm::NameConverter::NameOfAddress(pc);
90}
91
92
kasper.lund7276f142008-07-30 08:49:36 +000093const char* V8NameConverter::NameInCode(byte* addr) const {
94 // The V8NameConverter is used for well known code, so we can "safely"
95 // dereference pointers in generated code.
96 return (code_ != NULL) ? reinterpret_cast<const char*>(addr) : "";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
kasper.lund7276f142008-07-30 08:49:36 +000099
100static void DumpBuffer(FILE* f, char* buff) {
101 if (f == NULL) {
102 PrintF("%s", buff);
103 } else {
104 fprintf(f, "%s", buff);
105 }
106}
107
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000108static const int kOutBufferSize = 2048 + String::kMaxShortPrintLength;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109static const int kRelocInfoPosition = 57;
110
111static int DecodeIt(FILE* f,
112 const V8NameConverter& converter,
113 byte* begin,
114 byte* end) {
115 NoHandleAllocation ha;
116 AssertNoAllocation no_alloc;
117 ExternalReferenceEncoder ref_encoder;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 Heap* heap = HEAP;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000120 v8::internal::EmbeddedVector<char, 128> decode_buffer;
121 v8::internal::EmbeddedVector<char, kOutBufferSize> out_buffer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 byte* pc = begin;
123 disasm::Disassembler d(converter);
124 RelocIterator* it = NULL;
125 if (converter.code() != NULL) {
126 it = new RelocIterator(converter.code());
127 } else {
128 // No relocation information when printing code stubs.
129 }
kasper.lund7276f142008-07-30 08:49:36 +0000130 int constants = -1; // no constants being decoded at the start
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
132 while (pc < end) {
133 // First decode instruction so that we know its length.
134 byte* prev_pc = pc;
kasper.lund7276f142008-07-30 08:49:36 +0000135 if (constants > 0) {
136 OS::SNPrintF(decode_buffer,
kasper.lund7276f142008-07-30 08:49:36 +0000137 "%08x constant",
138 *reinterpret_cast<int32_t*>(pc));
139 constants--;
140 pc += 4;
141 } else {
142 int num_const = d.ConstantPoolSizeAt(pc);
143 if (num_const >= 0) {
144 OS::SNPrintF(decode_buffer,
kasper.lund7276f142008-07-30 08:49:36 +0000145 "%08x constant pool begin",
146 *reinterpret_cast<int32_t*>(pc));
147 constants = num_const;
148 pc += 4;
ager@chromium.org236ad962008-09-25 09:45:57 +0000149 } else if (it != NULL && !it->done() && it->rinfo()->pc() == pc &&
150 it->rinfo()->rmode() == RelocInfo::INTERNAL_REFERENCE) {
151 // raw pointer embedded in code stream, e.g., jump table
152 byte* ptr = *reinterpret_cast<byte**>(pc);
153 OS::SNPrintF(decode_buffer,
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000154 "%08" V8PRIxPTR " jump table entry %4" V8PRIdPTR,
155 ptr,
ager@chromium.org236ad962008-09-25 09:45:57 +0000156 ptr - begin);
157 pc += 4;
kasper.lund7276f142008-07-30 08:49:36 +0000158 } else {
159 decode_buffer[0] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000160 pc += d.InstructionDecode(decode_buffer, pc);
kasper.lund7276f142008-07-30 08:49:36 +0000161 }
162 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
164 // Collect RelocInfo for this instruction (prev_pc .. pc-1)
165 List<const char*> comments(4);
166 List<byte*> pcs(1);
ager@chromium.org236ad962008-09-25 09:45:57 +0000167 List<RelocInfo::Mode> rmodes(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 List<intptr_t> datas(1);
169 if (it != NULL) {
170 while (!it->done() && it->rinfo()->pc() < pc) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000171 if (RelocInfo::IsComment(it->rinfo()->rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // For comments just collect the text.
173 comments.Add(reinterpret_cast<const char*>(it->rinfo()->data()));
174 } else {
175 // For other reloc info collect all data.
176 pcs.Add(it->rinfo()->pc());
177 rmodes.Add(it->rinfo()->rmode());
178 datas.Add(it->rinfo()->data());
179 }
180 it->next();
181 }
182 }
183
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000184 StringBuilder out(out_buffer.start(), out_buffer.length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
186 // Comments.
187 for (int i = 0; i < comments.length(); i++) {
kasper.lund7276f142008-07-30 08:49:36 +0000188 out.AddFormatted(" %s\n", comments[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 }
190
191 // Write out comments, resets outp so that we can format the next line.
kasper.lund7276f142008-07-30 08:49:36 +0000192 DumpBuffer(f, out.Finalize());
193 out.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194
195 // Instruction address and instruction offset.
kasper.lund7276f142008-07-30 08:49:36 +0000196 out.AddFormatted("%p %4d ", prev_pc, prev_pc - begin);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197
kasper.lund7276f142008-07-30 08:49:36 +0000198 // Instruction.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000199 out.AddFormatted("%s", decode_buffer.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200
201 // Print all the reloc info for this instruction which are not comments.
202 for (int i = 0; i < pcs.length(); i++) {
203 // Put together the reloc info
204 RelocInfo relocinfo(pcs[i], rmodes[i], datas[i]);
205
206 // Indent the printing of the reloc info.
207 if (i == 0) {
208 // The first reloc info is printed after the disassembled instruction.
kasper.lund7276f142008-07-30 08:49:36 +0000209 out.AddPadding(' ', kRelocInfoPosition - out.position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 } else {
211 // Additional reloc infos are printed on separate lines.
kasper.lund7276f142008-07-30 08:49:36 +0000212 out.AddFormatted("\n");
213 out.AddPadding(' ', kRelocInfoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 }
215
ager@chromium.org236ad962008-09-25 09:45:57 +0000216 RelocInfo::Mode rmode = relocinfo.rmode();
217 if (RelocInfo::IsPosition(rmode)) {
218 if (RelocInfo::IsStatementPosition(rmode)) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000219 out.AddFormatted(" ;; debug: statement %d", relocinfo.data());
220 } else {
221 out.AddFormatted(" ;; debug: position %d", relocinfo.data());
222 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000223 } else if (rmode == RelocInfo::EMBEDDED_OBJECT) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 HeapStringAllocator allocator;
225 StringStream accumulator(&allocator);
226 relocinfo.target_object()->ShortPrint(&accumulator);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000227 SmartPointer<const char> obj_name = accumulator.ToCString();
kasper.lund7276f142008-07-30 08:49:36 +0000228 out.AddFormatted(" ;; object: %s", *obj_name);
ager@chromium.org236ad962008-09-25 09:45:57 +0000229 } else if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 const char* reference_name =
231 ref_encoder.NameOfAddress(*relocinfo.target_reference_address());
kasper.lund7276f142008-07-30 08:49:36 +0000232 out.AddFormatted(" ;; external reference (%s)", reference_name);
ager@chromium.org236ad962008-09-25 09:45:57 +0000233 } else if (RelocInfo::IsCodeTarget(rmode)) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000234 out.AddFormatted(" ;; code:");
ager@chromium.org236ad962008-09-25 09:45:57 +0000235 if (rmode == RelocInfo::CONSTRUCT_CALL) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000236 out.AddFormatted(" constructor,");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000238 Code* code = Code::GetCodeFromTargetAddress(relocinfo.target_address());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000239 Code::Kind kind = code->kind();
240 if (code->is_inline_cache_stub()) {
ager@chromium.org236ad962008-09-25 09:45:57 +0000241 if (rmode == RelocInfo::CODE_TARGET_CONTEXT) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000242 out.AddFormatted(" contextual,");
243 }
244 InlineCacheState ic_state = code->ic_state();
245 out.AddFormatted(" %s, %s", Code::Kind2String(kind),
246 Code::ICState2String(ic_state));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000247 if (ic_state == MONOMORPHIC) {
248 PropertyType type = code->type();
249 out.AddFormatted(", %s", Code::PropertyType2String(type));
250 }
251 if (code->ic_in_loop() == IN_LOOP) {
252 out.AddFormatted(", in_loop");
253 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000254 if (kind == Code::CALL_IC || kind == Code::KEYED_CALL_IC) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000255 out.AddFormatted(", argc = %d", code->arguments_count());
256 }
257 } else if (kind == Code::STUB) {
258 // Reverse lookup required as the minor key cannot be retrieved
259 // from the code object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 Object* obj = heap->code_stubs()->SlowReverseLookup(code);
261 if (obj != heap->undefined_value()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000262 ASSERT(obj->IsSmi());
263 // Get the STUB key and extract major and minor key.
264 uint32_t key = Smi::cast(obj)->value();
265 uint32_t minor_key = CodeStub::MinorKeyFromKey(key);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000266 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
267 ASSERT(major_key == CodeStub::MajorKeyFromKey(key));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000268 out.AddFormatted(" %s, %s, ",
269 Code::Kind2String(kind),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000270 CodeStub::MajorName(major_key, false));
271 switch (major_key) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000272 case CodeStub::CallFunction: {
273 int argc =
274 CallFunctionStub::ExtractArgcFromMinorKey(minor_key);
275 out.AddFormatted("argc = %d", argc);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000276 break;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000277 }
278 default:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000279 out.AddFormatted("minor: %d", minor_key);
280 }
281 }
282 } else {
283 out.AddFormatted(" %s", Code::Kind2String(kind));
284 }
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000285 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
286 out.AddFormatted(" (id = %d)", static_cast<int>(relocinfo.data()));
287 }
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000288 } else if (rmode == RelocInfo::RUNTIME_ENTRY &&
289 Isolate::Current()->deoptimizer_data() != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000290 // A runtime entry reloinfo might be a deoptimization bailout.
291 Address addr = relocinfo.target_address();
292 int id = Deoptimizer::GetDeoptimizationId(addr, Deoptimizer::EAGER);
293 if (id == Deoptimizer::kNotDeoptimizationEntry) {
294 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
295 } else {
296 out.AddFormatted(" ;; deoptimization bailout %d", id);
297 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000298 } else {
299 out.AddFormatted(" ;; %s", RelocInfo::RelocModeName(rmode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 }
301 }
kasper.lund7276f142008-07-30 08:49:36 +0000302 out.AddString("\n");
303 DumpBuffer(f, out.Finalize());
304 out.Reset();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 }
306
307 delete it;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000308 return static_cast<int>(pc - begin);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309}
310
311
312int Disassembler::Decode(FILE* f, byte* begin, byte* end) {
313 V8NameConverter defaultConverter(NULL);
314 return DecodeIt(f, defaultConverter, begin, end);
315}
316
317
kasper.lund7276f142008-07-30 08:49:36 +0000318// Called by Code::CodePrint.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319void Disassembler::Decode(FILE* f, Code* code) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000320 int decode_size = (code->kind() == Code::OPTIMIZED_FUNCTION)
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000321 ? static_cast<int>(code->safepoint_table_offset())
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000322 : code->instruction_size();
323 // If there might be a stack check table, stop before reaching it.
324 if (code->kind() == Code::FUNCTION) {
325 decode_size =
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000326 Min(decode_size, static_cast<int>(code->stack_check_table_offset()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000327 }
328
329 byte* begin = code->instruction_start();
330 byte* end = begin + decode_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 V8NameConverter v8NameConverter(code);
332 DecodeIt(f, v8NameConverter, begin, end);
333}
kasper.lund7276f142008-07-30 08:49:36 +0000334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335#else // ENABLE_DISASSEMBLER
336
337void Disassembler::Dump(FILE* f, byte* begin, byte* end) {}
338int Disassembler::Decode(FILE* f, byte* begin, byte* end) { return 0; }
339void Disassembler::Decode(FILE* f, Code* code) {}
340
341#endif // ENABLE_DISASSEMBLER
342
343} } // namespace v8::internal