bytecode-array-iterator.cc 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// 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.

#include "src/interpreter/bytecode-array-iterator.h"

#include "src/objects-inl.h"

namespace v8 {
namespace internal {
namespace interpreter {

BytecodeArrayIterator::BytecodeArrayIterator(
    Handle<BytecodeArray> bytecode_array)
    : bytecode_array_(bytecode_array), bytecode_offset_(0) {}


void BytecodeArrayIterator::Advance() {
  bytecode_offset_ += Bytecodes::Size(current_bytecode());
}


bool BytecodeArrayIterator::done() const {
  return bytecode_offset_ >= bytecode_array()->length();
}


Bytecode BytecodeArrayIterator::current_bytecode() const {
  DCHECK(!done());
  uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
  return interpreter::Bytecodes::FromByte(current_byte);
}


35 36
uint32_t BytecodeArrayIterator::GetRawOperand(int operand_index,
                                              OperandType operand_type) const {
37 38 39 40
  DCHECK_GE(operand_index, 0);
  DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
  DCHECK_EQ(operand_type,
            Bytecodes::GetOperandType(current_bytecode(), operand_index));
41 42 43 44 45 46 47 48 49 50 51 52
  uint8_t* operand_start =
      bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
      Bytecodes::GetOperandOffset(current_bytecode(), operand_index);
  switch (Bytecodes::SizeOfOperand(operand_type)) {
    default:
    case OperandSize::kNone:
      UNREACHABLE();
    case OperandSize::kByte:
      return static_cast<uint32_t>(*operand_start);
    case OperandSize::kShort:
      return Bytecodes::ShortOperandFromBytes(operand_start);
  }
53 54 55
}


56 57
int8_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const {
  uint32_t operand = GetRawOperand(operand_index, OperandType::kImm8);
58 59 60 61
  return static_cast<int8_t>(operand);
}


62 63 64 65 66 67
int BytecodeArrayIterator::GetCountOperand(int operand_index) const {
  uint32_t operand = GetRawOperand(operand_index, OperandType::kCount8);
  return static_cast<int>(operand);
}


68
int BytecodeArrayIterator::GetIndexOperand(int operand_index) const {
69 70 71 72 73
  OperandSize size =
      Bytecodes::GetOperandSize(current_bytecode(), operand_index);
  OperandType type =
      (size == OperandSize::kByte) ? OperandType::kIdx8 : OperandType::kIdx16;
  uint32_t operand = GetRawOperand(operand_index, type);
74 75 76 77 78
  return static_cast<int>(operand);
}


Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const {
79
  uint32_t operand = GetRawOperand(operand_index, OperandType::kReg8);
80 81 82 83 84 85 86 87 88 89 90 91 92
  return Register::FromOperand(operand);
}


Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand(
    int operand_index) const {
  Handle<FixedArray> constants = handle(bytecode_array()->constant_pool());
  return FixedArray::get(constants, GetIndexOperand(operand_index));
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8