• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Smi* 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   void ApplyDebugBreak();
74 
75   Bytecode current_bytecode() const;
76   int current_bytecode_size() const;
current_offset()77   int current_offset() const { return bytecode_offset_; }
current_operand_scale()78   OperandScale current_operand_scale() const { return operand_scale_; }
current_prefix_offset()79   int current_prefix_offset() const { return prefix_offset_; }
bytecode_array()80   const Handle<BytecodeArray>& bytecode_array() const {
81     return bytecode_array_;
82   }
83 
84   uint32_t GetFlagOperand(int operand_index) const;
85   uint32_t GetUnsignedImmediateOperand(int operand_index) const;
86   int32_t GetImmediateOperand(int operand_index) const;
87   uint32_t GetIndexOperand(int operand_index) const;
88   FeedbackSlot GetSlotOperand(int operand_index) const;
89   uint32_t GetRegisterCountOperand(int operand_index) const;
90   Register GetRegisterOperand(int operand_index) const;
91   int GetRegisterOperandRange(int operand_index) const;
92   Runtime::FunctionId GetRuntimeIdOperand(int operand_index) const;
93   Runtime::FunctionId GetIntrinsicIdOperand(int operand_index) const;
94   uint32_t GetNativeContextIndexOperand(int operand_index) const;
95   Object* GetConstantAtIndex(int offset) const;
96   Object* GetConstantForIndexOperand(int operand_index) const;
97 
98   // Returns the absolute offset of the branch target at the current bytecode.
99   // It is an error to call this method if the bytecode is not for a jump or
100   // conditional jump.
101   int GetJumpTargetOffset() const;
102   // Returns an iterator over the absolute offsets of the targets of the current
103   // switch bytecode's jump table. It is an error to call this method if the
104   // bytecode is not a switch.
105   JumpTableTargetOffsets GetJumpTableTargetOffsets() const;
106 
107   // Returns the absolute offset of the bytecode at the given relative offset
108   // from the current bytecode.
109   int GetAbsoluteOffset(int relative_offset) const;
110 
111   bool OffsetWithinBytecode(int offset) const;
112 
113   std::ostream& PrintTo(std::ostream& os) const;
114 
115  private:
116   bool OffsetInBounds() const;
117 
118   uint32_t GetUnsignedOperand(int operand_index,
119                               OperandType operand_type) const;
120   int32_t GetSignedOperand(int operand_index, OperandType operand_type) const;
121 
122   void UpdateOperandScale();
123 
124   Handle<BytecodeArray> bytecode_array_;
125   int bytecode_offset_;
126   OperandScale operand_scale_;
127   int prefix_offset_;
128 
129   DISALLOW_COPY_AND_ASSIGN(BytecodeArrayAccessor);
130 };
131 
132 }  // namespace interpreter
133 }  // namespace internal
134 }  // namespace v8
135 
136 #endif  // V8_INTERPRETER_BYTECODE_ARRAY_ACCESSOR_H_
137