runtime-interpreter.cc 7.01 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include <iomanip>

7 8 9
#include "src/execution/arguments-inl.h"
#include "src/execution/frames-inl.h"
#include "src/execution/isolate-inl.h"
10
#include "src/interpreter/bytecode-array-iterator.h"
11 12 13
#include "src/interpreter/bytecode-decoder.h"
#include "src/interpreter/bytecode-flags.h"
#include "src/interpreter/bytecode-register.h"
14
#include "src/interpreter/bytecodes.h"
15
#include "src/interpreter/interpreter.h"
16
#include "src/logging/counters.h"
17
#include "src/runtime/runtime-utils.h"
18
#include "src/snapshot/snapshot.h"
19
#include "src/utils/ostreams.h"
20 21 22 23

namespace v8 {
namespace internal {

24 25
#ifdef V8_TRACE_IGNITION

26 27
namespace {

28 29 30 31 32 33 34
void AdvanceToOffsetForTracing(
    interpreter::BytecodeArrayIterator& bytecode_iterator, int offset) {
  while (bytecode_iterator.current_offset() +
             bytecode_iterator.current_bytecode_size() <=
         offset) {
    bytecode_iterator.Advance();
  }
35 36 37 38
  DCHECK(bytecode_iterator.current_offset() == offset ||
         ((bytecode_iterator.current_offset() + 1) == offset &&
          bytecode_iterator.current_operand_scale() >
              interpreter::OperandScale::kSingle));
39 40
}

41
void PrintRegisters(Isolate* isolate, std::ostream& os, bool is_input,
42
                    interpreter::BytecodeArrayIterator& bytecode_iterator,
43
                    Handle<Object> accumulator) {
44 45
  static const char kAccumulator[] = "accumulator";
  static const int kRegFieldWidth = static_cast<int>(sizeof(kAccumulator) - 1);
46 47 48 49 50 51 52 53
  static const char* kInputColourCode = "\033[0;36m";
  static const char* kOutputColourCode = "\033[0;35m";
  static const char* kNormalColourCode = "\033[0;m";
  const char* kArrowDirection = is_input ? " -> " : " <- ";
  if (FLAG_log_colour) {
    os << (is_input ? kInputColourCode : kOutputColourCode);
  }

54 55
  interpreter::Bytecode bytecode = bytecode_iterator.current_bytecode();

56
  // Print accumulator.
57 58 59 60 61 62
  if ((is_input && interpreter::Bytecodes::ReadsAccumulator(bytecode)) ||
      (!is_input && interpreter::Bytecodes::WritesAccumulator(bytecode))) {
    os << "      [ " << kAccumulator << kArrowDirection;
    accumulator->ShortPrint();
    os << " ]" << std::endl;
  }
63

64
  // Print the registers.
65
  JavaScriptFrameIterator frame_iterator(isolate);
66 67
  InterpretedFrame* frame =
      reinterpret_cast<InterpretedFrame*>(frame_iterator.frame());
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  int operand_count = interpreter::Bytecodes::NumberOfOperands(bytecode);
  for (int operand_index = 0; operand_index < operand_count; operand_index++) {
    interpreter::OperandType operand_type =
        interpreter::Bytecodes::GetOperandType(bytecode, operand_index);
    bool should_print =
        is_input
            ? interpreter::Bytecodes::IsRegisterInputOperandType(operand_type)
            : interpreter::Bytecodes::IsRegisterOutputOperandType(operand_type);
    if (should_print) {
      interpreter::Register first_reg =
          bytecode_iterator.GetRegisterOperand(operand_index);
      int range = bytecode_iterator.GetRegisterOperandRange(operand_index);
      for (int reg_index = first_reg.index();
           reg_index < first_reg.index() + range; reg_index++) {
82
        Object reg_object = frame->ReadInterpreterRegister(reg_index);
83 84
        os << "      [ " << std::setw(kRegFieldWidth)
           << interpreter::Register(reg_index).ToString(
85
                  bytecode_iterator.bytecode_array()->parameter_count())
86
           << kArrowDirection;
87
        reg_object.ShortPrint(os);
88 89 90 91 92 93 94 95 96 97 98 99
        os << " ]" << std::endl;
      }
    }
  }
  if (FLAG_log_colour) {
    os << kNormalColourCode;
  }
}

}  // namespace

RUNTIME_FUNCTION(Runtime_InterpreterTraceBytecodeEntry) {
100
  if (!FLAG_trace_ignition) {
101
    return ReadOnlyRoots(isolate).undefined_value();
102 103
  }

104 105 106 107 108 109
  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BytecodeArray, bytecode_array, 0);
  CONVERT_SMI_ARG_CHECKED(bytecode_offset, 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, accumulator, 2);

110 111 112 113
  int offset = bytecode_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
  interpreter::BytecodeArrayIterator bytecode_iterator(bytecode_array);
  AdvanceToOffsetForTracing(bytecode_iterator, offset);
  if (offset == bytecode_iterator.current_offset()) {
114
    StdoutStream os;
115

116
    // Print bytecode.
117 118
    const uint8_t* base_address = reinterpret_cast<const uint8_t*>(
        bytecode_array->GetFirstBytecodeAddress());
119 120 121
    const uint8_t* bytecode_address = base_address + offset;
    os << " -> " << static_cast<const void*>(bytecode_address) << " @ "
       << std::setw(4) << offset << " : ";
122 123
    interpreter::BytecodeDecoder::Decode(os, bytecode_address,
                                         bytecode_array->parameter_count());
124 125
    os << std::endl;
    // Print all input registers and accumulator.
126
    PrintRegisters(isolate, os, true, bytecode_iterator, accumulator);
127 128 129

    os << std::flush;
  }
130
  return ReadOnlyRoots(isolate).undefined_value();
131 132 133
}

RUNTIME_FUNCTION(Runtime_InterpreterTraceBytecodeExit) {
134
  if (!FLAG_trace_ignition) {
135
    return ReadOnlyRoots(isolate).undefined_value();
136 137
  }

138 139 140 141 142 143
  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(BytecodeArray, bytecode_array, 0);
  CONVERT_SMI_ARG_CHECKED(bytecode_offset, 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, accumulator, 2);

144 145 146 147 148 149 150 151 152
  int offset = bytecode_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
  interpreter::BytecodeArrayIterator bytecode_iterator(bytecode_array);
  AdvanceToOffsetForTracing(bytecode_iterator, offset);
  // The offset comparison here ensures registers only printed when the
  // (potentially) widened bytecode has completed. The iterator reports
  // the offset as the offset of the prefix bytecode.
  if (bytecode_iterator.current_operand_scale() ==
          interpreter::OperandScale::kSingle ||
      offset > bytecode_iterator.current_offset()) {
153
    StdoutStream os;
154
    // Print all output registers and accumulator.
155
    PrintRegisters(isolate, os, false, bytecode_iterator, accumulator);
156 157
    os << std::flush;
  }
158
  return ReadOnlyRoots(isolate).undefined_value();
159 160
}

161 162
#endif

163 164 165 166
#ifdef V8_TRACE_FEEDBACK_UPDATES

RUNTIME_FUNCTION(Runtime_InterpreterTraceUpdateFeedback) {
  if (!FLAG_trace_feedback_updates) {
167
    return ReadOnlyRoots(isolate).undefined_value();
168 169 170 171 172 173 174 175
  }

  SealHandleScope shs(isolate);
  DCHECK_EQ(3, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
  CONVERT_SMI_ARG_CHECKED(slot, 1);
  CONVERT_ARG_CHECKED(String, reason, 2);

176
  int slot_count = function->feedback_vector().metadata().slot_count();
177

178
  StdoutStream os;
179
  os << "[Feedback slot " << slot << "/" << slot_count << " in ";
180
  function->shared().ShortPrint(os);
181
  os << " updated to ";
182
  function->feedback_vector().FeedbackSlotPrint(os, FeedbackSlot(slot));
183 184 185 186 187 188 189 190 191 192
  os << " - ";

  StringCharacterStream stream(reason);
  while (stream.HasMore()) {
    uint16_t character = stream.GetNext();
    PrintF("%c", character);
  }

  os << "]" << std::endl;

193
  return ReadOnlyRoots(isolate).undefined_value();
194 195 196 197
}

#endif

198 199
}  // namespace internal
}  // namespace v8