Home | History | Annotate | Download | only in interpreter
      1 // 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/interpreter/bytecode-array-accessor.h"
      6 
      7 #include "src/interpreter/bytecode-decoder.h"
      8 #include "src/interpreter/interpreter-intrinsics.h"
      9 #include "src/objects-inl.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 namespace interpreter {
     14 
     15 BytecodeArrayAccessor::BytecodeArrayAccessor(
     16     Handle<BytecodeArray> bytecode_array, int initial_offset)
     17     : bytecode_array_(bytecode_array),
     18       bytecode_offset_(initial_offset),
     19       operand_scale_(OperandScale::kSingle),
     20       prefix_offset_(0) {
     21   UpdateOperandScale();
     22 }
     23 
     24 void BytecodeArrayAccessor::SetOffset(int offset) {
     25   bytecode_offset_ = offset;
     26   UpdateOperandScale();
     27 }
     28 
     29 void BytecodeArrayAccessor::UpdateOperandScale() {
     30   if (OffsetInBounds()) {
     31     uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
     32     Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
     33     if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
     34       operand_scale_ =
     35           Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
     36       prefix_offset_ = 1;
     37     } else {
     38       operand_scale_ = OperandScale::kSingle;
     39       prefix_offset_ = 0;
     40     }
     41   }
     42 }
     43 
     44 bool BytecodeArrayAccessor::OffsetInBounds() const {
     45   return bytecode_offset_ >= 0 && bytecode_offset_ < bytecode_array()->length();
     46 }
     47 
     48 Bytecode BytecodeArrayAccessor::current_bytecode() const {
     49   DCHECK(OffsetInBounds());
     50   uint8_t current_byte =
     51       bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
     52   Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
     53   DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
     54   return current_bytecode;
     55 }
     56 
     57 int BytecodeArrayAccessor::current_bytecode_size() const {
     58   return current_prefix_offset() +
     59          Bytecodes::Size(current_bytecode(), current_operand_scale());
     60 }
     61 
     62 uint32_t BytecodeArrayAccessor::GetUnsignedOperand(
     63     int operand_index, OperandType operand_type) const {
     64   DCHECK_GE(operand_index, 0);
     65   DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
     66   DCHECK_EQ(operand_type,
     67             Bytecodes::GetOperandType(current_bytecode(), operand_index));
     68   DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
     69   const uint8_t* operand_start =
     70       bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
     71       current_prefix_offset() +
     72       Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
     73                                   current_operand_scale());
     74   return BytecodeDecoder::DecodeUnsignedOperand(operand_start, operand_type,
     75                                                 current_operand_scale());
     76 }
     77 
     78 int32_t BytecodeArrayAccessor::GetSignedOperand(
     79     int operand_index, OperandType operand_type) const {
     80   DCHECK_GE(operand_index, 0);
     81   DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
     82   DCHECK_EQ(operand_type,
     83             Bytecodes::GetOperandType(current_bytecode(), operand_index));
     84   DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type));
     85   const uint8_t* operand_start =
     86       bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
     87       current_prefix_offset() +
     88       Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
     89                                   current_operand_scale());
     90   return BytecodeDecoder::DecodeSignedOperand(operand_start, operand_type,
     91                                               current_operand_scale());
     92 }
     93 
     94 uint32_t BytecodeArrayAccessor::GetFlagOperand(int operand_index) const {
     95   DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
     96             OperandType::kFlag8);
     97   return GetUnsignedOperand(operand_index, OperandType::kFlag8);
     98 }
     99 
    100 uint32_t BytecodeArrayAccessor::GetUnsignedImmediateOperand(
    101     int operand_index) const {
    102   DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
    103             OperandType::kUImm);
    104   return GetUnsignedOperand(operand_index, OperandType::kUImm);
    105 }
    106 
    107 int32_t BytecodeArrayAccessor::GetImmediateOperand(int operand_index) const {
    108   DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
    109             OperandType::kImm);
    110   return GetSignedOperand(operand_index, OperandType::kImm);
    111 }
    112 
    113 uint32_t BytecodeArrayAccessor::GetRegisterCountOperand(
    114     int operand_index) const {
    115   DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
    116             OperandType::kRegCount);
    117   return GetUnsignedOperand(operand_index, OperandType::kRegCount);
    118 }
    119 
    120 uint32_t BytecodeArrayAccessor::GetIndexOperand(int operand_index) const {
    121   OperandType operand_type =
    122       Bytecodes::GetOperandType(current_bytecode(), operand_index);
    123   DCHECK_EQ(operand_type, OperandType::kIdx);
    124   return GetUnsignedOperand(operand_index, operand_type);
    125 }
    126 
    127 Register BytecodeArrayAccessor::GetRegisterOperand(int operand_index) const {
    128   OperandType operand_type =
    129       Bytecodes::GetOperandType(current_bytecode(), operand_index);
    130   const uint8_t* operand_start =
    131       bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
    132       current_prefix_offset() +
    133       Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
    134                                   current_operand_scale());
    135   return BytecodeDecoder::DecodeRegisterOperand(operand_start, operand_type,
    136                                                 current_operand_scale());
    137 }
    138 
    139 int BytecodeArrayAccessor::GetRegisterOperandRange(int operand_index) const {
    140   DCHECK_LE(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
    141   const OperandType* operand_types =
    142       Bytecodes::GetOperandTypes(current_bytecode());
    143   OperandType operand_type = operand_types[operand_index];
    144   DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
    145   if (operand_type == OperandType::kRegList) {
    146     return GetRegisterCountOperand(operand_index + 1);
    147   } else {
    148     return Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type);
    149   }
    150 }
    151 
    152 Runtime::FunctionId BytecodeArrayAccessor::GetRuntimeIdOperand(
    153     int operand_index) const {
    154   OperandType operand_type =
    155       Bytecodes::GetOperandType(current_bytecode(), operand_index);
    156   DCHECK(operand_type == OperandType::kRuntimeId);
    157   uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
    158   return static_cast<Runtime::FunctionId>(raw_id);
    159 }
    160 
    161 Runtime::FunctionId BytecodeArrayAccessor::GetIntrinsicIdOperand(
    162     int operand_index) const {
    163   OperandType operand_type =
    164       Bytecodes::GetOperandType(current_bytecode(), operand_index);
    165   DCHECK(operand_type == OperandType::kIntrinsicId);
    166   uint32_t raw_id = GetUnsignedOperand(operand_index, operand_type);
    167   return IntrinsicsHelper::ToRuntimeId(
    168       static_cast<IntrinsicsHelper::IntrinsicId>(raw_id));
    169 }
    170 
    171 Handle<Object> BytecodeArrayAccessor::GetConstantForIndexOperand(
    172     int operand_index) const {
    173   return FixedArray::get(bytecode_array()->constant_pool(),
    174                          GetIndexOperand(operand_index),
    175                          bytecode_array()->GetIsolate());
    176 }
    177 
    178 int BytecodeArrayAccessor::GetJumpTargetOffset() const {
    179   Bytecode bytecode = current_bytecode();
    180   if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
    181     int relative_offset = GetUnsignedImmediateOperand(0);
    182     if (bytecode == Bytecode::kJumpLoop) {
    183       relative_offset = -relative_offset;
    184     }
    185     return current_offset() + relative_offset + current_prefix_offset();
    186   } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
    187     Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
    188     return current_offset() + smi->value() + current_prefix_offset();
    189   } else {
    190     UNREACHABLE();
    191     return kMinInt;
    192   }
    193 }
    194 
    195 bool BytecodeArrayAccessor::OffsetWithinBytecode(int offset) const {
    196   return current_offset() <= offset &&
    197          offset < current_offset() + current_bytecode_size();
    198 }
    199 
    200 std::ostream& BytecodeArrayAccessor::PrintTo(std::ostream& os) const {
    201   return BytecodeDecoder::Decode(
    202       os, bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_,
    203       bytecode_array()->parameter_count());
    204 }
    205 
    206 }  // namespace interpreter
    207 }  // namespace internal
    208 }  // namespace v8
    209