blob: f870d23c2648f5679cb235e85db5d34f618b85d9 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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/runtime/runtime-utils.h"
6
Ben Murdoch097c5b22016-05-18 11:27:45 +01007#include <iomanip>
8
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/arguments.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010010#include "src/frames-inl.h"
11#include "src/interpreter/bytecode-array-iterator.h"
12#include "src/interpreter/bytecodes.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/isolate-inl.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010014#include "src/ostreams.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015
16namespace v8 {
17namespace internal {
18
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019RUNTIME_FUNCTION(Runtime_InterpreterNewClosure) {
20 HandleScope scope(isolate);
21 DCHECK_EQ(2, args.length());
22 CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared, 0);
23 CONVERT_SMI_ARG_CHECKED(pretenured_flag, 1);
24 Handle<Context> context(isolate->context(), isolate);
25 return *isolate->factory()->NewFunctionFromSharedFunctionInfo(
26 shared, context, static_cast<PretenureFlag>(pretenured_flag));
27}
28
Ben Murdoch097c5b22016-05-18 11:27:45 +010029namespace {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030
Ben Murdochda12d292016-06-02 14:46:10 +010031void AdvanceToOffsetForTracing(
32 interpreter::BytecodeArrayIterator& bytecode_iterator, int offset) {
33 while (bytecode_iterator.current_offset() +
34 bytecode_iterator.current_bytecode_size() <=
35 offset) {
36 bytecode_iterator.Advance();
37 }
38 DCHECK(bytecode_iterator.current_offset() == offset ||
39 ((bytecode_iterator.current_offset() + 1) == offset &&
40 bytecode_iterator.current_operand_scale() >
41 interpreter::OperandScale::kSingle));
42}
43
Ben Murdoch097c5b22016-05-18 11:27:45 +010044void PrintRegisters(std::ostream& os, bool is_input,
Ben Murdochda12d292016-06-02 14:46:10 +010045 interpreter::BytecodeArrayIterator& bytecode_iterator,
Ben Murdoch097c5b22016-05-18 11:27:45 +010046 Handle<Object> accumulator) {
Ben Murdochda12d292016-06-02 14:46:10 +010047 static const char kAccumulator[] = "accumulator";
48 static const int kRegFieldWidth = static_cast<int>(sizeof(kAccumulator) - 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +010049 static const char* kInputColourCode = "\033[0;36m";
50 static const char* kOutputColourCode = "\033[0;35m";
51 static const char* kNormalColourCode = "\033[0;m";
52 const char* kArrowDirection = is_input ? " -> " : " <- ";
53 if (FLAG_log_colour) {
54 os << (is_input ? kInputColourCode : kOutputColourCode);
55 }
56
Ben Murdochda12d292016-06-02 14:46:10 +010057 interpreter::Bytecode bytecode = bytecode_iterator.current_bytecode();
58
Ben Murdoch097c5b22016-05-18 11:27:45 +010059 // Print accumulator.
Ben Murdochda12d292016-06-02 14:46:10 +010060 if ((is_input && interpreter::Bytecodes::ReadsAccumulator(bytecode)) ||
61 (!is_input && interpreter::Bytecodes::WritesAccumulator(bytecode))) {
62 os << " [ " << kAccumulator << kArrowDirection;
63 accumulator->ShortPrint();
64 os << " ]" << std::endl;
65 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010066
Ben Murdochc5610432016-08-08 18:44:38 +010067 // Print the registers.
Ben Murdochda12d292016-06-02 14:46:10 +010068 JavaScriptFrameIterator frame_iterator(
69 bytecode_iterator.bytecode_array()->GetIsolate());
Ben Murdochc5610432016-08-08 18:44:38 +010070 InterpretedFrame* frame =
71 reinterpret_cast<InterpretedFrame*>(frame_iterator.frame());
Ben Murdoch097c5b22016-05-18 11:27:45 +010072 int operand_count = interpreter::Bytecodes::NumberOfOperands(bytecode);
73 for (int operand_index = 0; operand_index < operand_count; operand_index++) {
74 interpreter::OperandType operand_type =
75 interpreter::Bytecodes::GetOperandType(bytecode, operand_index);
76 bool should_print =
77 is_input
78 ? interpreter::Bytecodes::IsRegisterInputOperandType(operand_type)
79 : interpreter::Bytecodes::IsRegisterOutputOperandType(operand_type);
80 if (should_print) {
81 interpreter::Register first_reg =
82 bytecode_iterator.GetRegisterOperand(operand_index);
83 int range = bytecode_iterator.GetRegisterOperandRange(operand_index);
84 for (int reg_index = first_reg.index();
85 reg_index < first_reg.index() + range; reg_index++) {
Ben Murdochc5610432016-08-08 18:44:38 +010086 Object* reg_object = frame->ReadInterpreterRegister(reg_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +010087 os << " [ " << std::setw(kRegFieldWidth)
88 << interpreter::Register(reg_index).ToString(
Ben Murdochda12d292016-06-02 14:46:10 +010089 bytecode_iterator.bytecode_array()->parameter_count())
Ben Murdoch097c5b22016-05-18 11:27:45 +010090 << kArrowDirection;
91 reg_object->ShortPrint(os);
92 os << " ]" << std::endl;
93 }
94 }
95 }
96 if (FLAG_log_colour) {
97 os << kNormalColourCode;
98 }
99}
100
101} // namespace
102
103RUNTIME_FUNCTION(Runtime_InterpreterTraceBytecodeEntry) {
104 SealHandleScope shs(isolate);
105 DCHECK_EQ(3, args.length());
106 CONVERT_ARG_HANDLE_CHECKED(BytecodeArray, bytecode_array, 0);
107 CONVERT_SMI_ARG_CHECKED(bytecode_offset, 1);
108 CONVERT_ARG_HANDLE_CHECKED(Object, accumulator, 2);
109 OFStream os(stdout);
110
Ben Murdochda12d292016-06-02 14:46:10 +0100111 int offset = bytecode_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
112 interpreter::BytecodeArrayIterator bytecode_iterator(bytecode_array);
113 AdvanceToOffsetForTracing(bytecode_iterator, offset);
114 if (offset == bytecode_iterator.current_offset()) {
115 // Print bytecode.
Ben Murdochc5610432016-08-08 18:44:38 +0100116 const uint8_t* base_address = bytecode_array->GetFirstBytecodeAddress();
117 const uint8_t* bytecode_address = base_address + offset;
118 os << " -> " << static_cast<const void*>(bytecode_address) << " @ "
119 << std::setw(4) << offset << " : ";
Ben Murdochda12d292016-06-02 14:46:10 +0100120 interpreter::Bytecodes::Decode(os, bytecode_address,
121 bytecode_array->parameter_count());
122 os << std::endl;
123 // Print all input registers and accumulator.
124 PrintRegisters(os, true, bytecode_iterator, accumulator);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100125
Ben Murdochda12d292016-06-02 14:46:10 +0100126 os << std::flush;
127 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100128 return isolate->heap()->undefined_value();
129}
130
131RUNTIME_FUNCTION(Runtime_InterpreterTraceBytecodeExit) {
132 SealHandleScope shs(isolate);
133 DCHECK_EQ(3, args.length());
134 CONVERT_ARG_HANDLE_CHECKED(BytecodeArray, bytecode_array, 0);
135 CONVERT_SMI_ARG_CHECKED(bytecode_offset, 1);
136 CONVERT_ARG_HANDLE_CHECKED(Object, accumulator, 2);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100137
Ben Murdochda12d292016-06-02 14:46:10 +0100138 int offset = bytecode_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
139 interpreter::BytecodeArrayIterator bytecode_iterator(bytecode_array);
140 AdvanceToOffsetForTracing(bytecode_iterator, offset);
141 // The offset comparison here ensures registers only printed when the
142 // (potentially) widened bytecode has completed. The iterator reports
143 // the offset as the offset of the prefix bytecode.
144 if (bytecode_iterator.current_operand_scale() ==
145 interpreter::OperandScale::kSingle ||
146 offset > bytecode_iterator.current_offset()) {
147 OFStream os(stdout);
148 // Print all output registers and accumulator.
149 PrintRegisters(os, false, bytecode_iterator, accumulator);
150 os << std::flush;
151 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100152 return isolate->heap()->undefined_value();
153}
154
155RUNTIME_FUNCTION(Runtime_InterpreterClearPendingMessage) {
156 SealHandleScope shs(isolate);
157 DCHECK_EQ(0, args.length());
158 Object* message = isolate->thread_local_top()->pending_message_obj_;
159 isolate->clear_pending_message();
160 return message;
161}
162
163RUNTIME_FUNCTION(Runtime_InterpreterSetPendingMessage) {
164 SealHandleScope shs(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 DCHECK_EQ(1, args.length());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100166 CONVERT_ARG_HANDLE_CHECKED(Object, message, 0);
167 isolate->thread_local_top()->pending_message_obj_ = *message;
168 return isolate->heap()->undefined_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169}
170
171} // namespace internal
172} // namespace v8