1 // Copyright 2016 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 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_ACCESSOR_H_
6 #define V8_INTERPRETER_BYTECODE_ARRAY_ACCESSOR_H_
7 
8 #include "src/globals.h"
9 #include "src/handles.h"
10 #include "src/interpreter/bytecode-register.h"
11 #include "src/interpreter/bytecodes.h"
12 #include "src/objects.h"
13 #include "src/runtime/runtime.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class BytecodeArray;
19 
20 namespace interpreter {
21 
22 class BytecodeArrayAccessor;
23 
24 struct V8_EXPORT_PRIVATE JumpTableTargetOffset {
25   int case_value;
26   int target_offset;
27 };
28 
29 class V8_EXPORT_PRIVATE JumpTableTargetOffsets final {
30  public:
31   // Minimal iterator implementation for use in ranged-for.
32   class V8_EXPORT_PRIVATE iterator final {
33    public:
34     iterator(int case_value, int table_offset, int table_end,
35              const BytecodeArrayAccessor* accessor);
36 
37     JumpTableTargetOffset operator*();
38     iterator& operator++();
39     bool operator!=(const iterator& other);
40 
41    private:
42     void UpdateAndAdvanceToValid();
43 
44     const BytecodeArrayAccessor* accessor_;
45     Handle<Object> current_;
46     int index_;
47     int table_offset_;
48     int table_end_;
49   };
50 
51   JumpTableTargetOffsets(const BytecodeArrayAccessor* accessor, int table_start,
52                          int table_size, int case_value_base);
53 
54   iterator begin() const;
55   iterator end() const;
56 
57   int size() const;
58 
59  private:
60   const BytecodeArrayAccessor* accessor_;
61   int table_start_;
62   int table_size_;
63   int case_value_base_;
64 };
65 
66 class V8_EXPORT_PRIVATE BytecodeArrayAccessor {
67  public:
68   BytecodeArrayAccessor(Handle<BytecodeArray> bytecode_array,
69                         int initial_offset);
70 
71   void SetOffset(int offset);
72 
73   Bytecode current_bytecode() const;
74   int current_bytecode_size() const;
current_offset()75   int current_offset() const { return bytecode_offset_; }
current_operand_scale()76   OperandScale current_operand_scale() const { return operand_scale_; }
current_prefix_offset()77   int current_prefix_offset() const { return prefix_offset_; }
bytecode_array()78   const Handle<BytecodeArray>& bytecode_array() const {
79     return bytecode_array_;
80   }
81 
82   uint32_t GetFlagOperand(int operand_index) const;
83   uint32_t GetUnsignedImmediateOperand(int operand_index) const;
84   int32_t GetImmediateOperand(int operand_index) const;
85   uint32_t GetIndexOperand(int operand_index) const;
86   FeedbackSlot GetSlotOperand(int operand_index) const;
87   uint32_t GetRegisterCountOperand(int operand_index) const;
88   Register GetRegisterOperand(int operand_index) const;
89   int GetRegisterOperandRange(int operand_index) const;
90   Runtime::FunctionId GetRuntimeIdOperand(int operand_index) const;
91   Runtime::FunctionId GetIntrinsicIdOperand(int operand_index) const;
92   uint32_t GetNativeContextIndexOperand(int operand_index) const;
93   Handle<Object> GetConstantAtIndex(int offset) const;
94   Handle<Object> GetConstantForIndexOperand(int operand_index) const;
95 
96   // Returns the absolute offset of the branch target at the current bytecode.
97   // It is an error to call this method if the bytecode is not for a jump or
98   // conditional jump.
99   int GetJumpTargetOffset() const;
100   // Returns an iterator over the absolute offsets of the targets of the current
101   // switch bytecode's jump table. It is an error to call this method if the
102   // bytecode is not a switch.
103   JumpTableTargetOffsets GetJumpTableTargetOffsets() const;
104 
105   // Returns the absolute offset of the bytecode at the given relative offset
106   // from the current bytecode.
107   int GetAbsoluteOffset(int relative_offset) const;
108 
109   bool OffsetWithinBytecode(int offset) const;
110 
111   std::ostream& PrintTo(std::ostream& os) const;
112 
113  private:
114   bool OffsetInBounds() const;
115 
116   uint32_t GetUnsignedOperand(int operand_index,
117                               OperandType operand_type) const;
118   int32_t GetSignedOperand(int operand_index, OperandType operand_type) const;
119 
120   void UpdateOperandScale();
121 
122   Handle<BytecodeArray> bytecode_array_;
123   int bytecode_offset_;
124   OperandScale operand_scale_;
125   int prefix_offset_;
126 
127   DISALLOW_COPY_AND_ASSIGN(BytecodeArrayAccessor);
128 };
129 
130 }  // namespace interpreter
131 }  // namespace internal
132 }  // namespace v8
133 
134 #endif  // V8_INTERPRETER_BYTECODE_ARRAY_ACCESSOR_H_
135