Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 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 | #ifndef V8_INTERPRETER_BYTECODE_PIPELINE_H_ |
| 6 | #define V8_INTERPRETER_BYTECODE_PIPELINE_H_ |
| 7 | |
| 8 | #include "src/interpreter/bytecode-register-allocator.h" |
| 9 | #include "src/interpreter/bytecodes.h" |
| 10 | #include "src/zone-containers.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | namespace interpreter { |
| 15 | |
| 16 | class BytecodeNode; |
| 17 | class BytecodeSourceInfo; |
| 18 | |
| 19 | // Interface for bytecode pipeline stages. |
| 20 | class BytecodePipelineStage { |
| 21 | public: |
| 22 | virtual ~BytecodePipelineStage() {} |
| 23 | |
| 24 | // Write bytecode node |node| into pipeline. The node is only valid |
| 25 | // for the duration of the call. Callee's should clone it if |
| 26 | // deferring Write() to the next stage. |
| 27 | virtual void Write(BytecodeNode* node) = 0; |
| 28 | |
| 29 | // Flush state for bytecode array offset calculation. Returns the |
| 30 | // current size of bytecode array. |
| 31 | virtual size_t FlushForOffset() = 0; |
| 32 | |
| 33 | // Flush state to terminate basic block. |
| 34 | virtual void FlushBasicBlock() = 0; |
| 35 | }; |
| 36 | |
| 37 | // Source code position information. |
| 38 | class BytecodeSourceInfo final { |
| 39 | public: |
| 40 | static const int kUninitializedPosition = -1; |
| 41 | |
| 42 | BytecodeSourceInfo(int position = kUninitializedPosition, |
| 43 | bool is_statement = false) |
| 44 | : source_position_(position), is_statement_(is_statement) {} |
| 45 | |
| 46 | // Combine later source info with current. |
| 47 | void Update(const BytecodeSourceInfo& entry); |
| 48 | |
| 49 | int source_position() const { |
| 50 | DCHECK(is_valid()); |
| 51 | return source_position_; |
| 52 | } |
| 53 | |
| 54 | bool is_statement() const { return is_valid() && is_statement_; } |
| 55 | |
| 56 | bool is_valid() const { return source_position_ != kUninitializedPosition; } |
| 57 | void set_invalid() { source_position_ = kUninitializedPosition; } |
| 58 | |
| 59 | bool operator==(const BytecodeSourceInfo& other) const { |
| 60 | return source_position_ == other.source_position_ && |
| 61 | is_statement_ == other.is_statement_; |
| 62 | } |
| 63 | bool operator!=(const BytecodeSourceInfo& other) const { |
| 64 | return source_position_ != other.source_position_ || |
| 65 | is_statement_ != other.is_statement_; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | int source_position_; |
| 70 | bool is_statement_; |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(BytecodeSourceInfo); |
| 73 | }; |
| 74 | |
| 75 | // A container for a generated bytecode, it's operands, and source information. |
| 76 | // These must be allocated by a BytecodeNodeAllocator instance. |
| 77 | class BytecodeNode final : ZoneObject { |
| 78 | public: |
| 79 | explicit BytecodeNode(Bytecode bytecode = Bytecode::kIllegal); |
| 80 | BytecodeNode(Bytecode bytecode, uint32_t operand0, |
| 81 | OperandScale operand_scale); |
| 82 | BytecodeNode(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
| 83 | OperandScale operand_scale); |
| 84 | BytecodeNode(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
| 85 | uint32_t operand2, OperandScale operand_scale); |
| 86 | BytecodeNode(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
| 87 | uint32_t operand2, uint32_t operand3, |
| 88 | OperandScale operand_scale); |
| 89 | |
| 90 | void set_bytecode(Bytecode bytecode); |
| 91 | void set_bytecode(Bytecode bytecode, uint32_t operand0, |
| 92 | OperandScale operand_scale); |
| 93 | |
| 94 | // Clone |other|. |
| 95 | void Clone(const BytecodeNode* const other); |
| 96 | |
| 97 | // Print to stream |os|. |
| 98 | void Print(std::ostream& os) const; |
| 99 | |
| 100 | // Return the size when this node is serialized to a bytecode array. |
| 101 | size_t Size() const; |
| 102 | |
| 103 | Bytecode bytecode() const { return bytecode_; } |
| 104 | |
| 105 | uint32_t operand(int i) const { |
| 106 | DCHECK_LT(i, operand_count()); |
| 107 | return operands_[i]; |
| 108 | } |
| 109 | uint32_t* operands() { return operands_; } |
| 110 | const uint32_t* operands() const { return operands_; } |
| 111 | |
| 112 | int operand_count() const { return Bytecodes::NumberOfOperands(bytecode_); } |
| 113 | OperandScale operand_scale() const { return operand_scale_; } |
| 114 | |
| 115 | const BytecodeSourceInfo& source_info() const { return source_info_; } |
| 116 | BytecodeSourceInfo& source_info() { return source_info_; } |
| 117 | |
| 118 | bool operator==(const BytecodeNode& other) const; |
| 119 | bool operator!=(const BytecodeNode& other) const { return !(*this == other); } |
| 120 | |
| 121 | private: |
| 122 | static const int kInvalidPosition = kMinInt; |
| 123 | static const size_t kMaxOperands = 4; |
| 124 | |
| 125 | Bytecode bytecode_; |
| 126 | uint32_t operands_[kMaxOperands]; |
| 127 | OperandScale operand_scale_; |
| 128 | BytecodeSourceInfo source_info_; |
| 129 | }; |
| 130 | |
| 131 | std::ostream& operator<<(std::ostream& os, const BytecodeSourceInfo& info); |
| 132 | std::ostream& operator<<(std::ostream& os, const BytecodeNode& node); |
| 133 | |
| 134 | } // namespace interpreter |
| 135 | } // namespace internal |
| 136 | } // namespace v8 |
| 137 | |
| 138 | #endif // V8_INTERPRETER_BYTECODE_PIPELINE_H_ |