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 | #include "src/interpreter/bytecode-pipeline.h" |
| 6 | |
| 7 | #include <iomanip> |
| 8 | #include "src/interpreter/source-position-table.h" |
| 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | namespace interpreter { |
| 13 | |
| 14 | void BytecodeSourceInfo::Update(const BytecodeSourceInfo& entry) { |
| 15 | DCHECK(entry.is_valid()); |
| 16 | if (!is_valid() || (entry.is_statement() && !is_statement()) || |
| 17 | (entry.is_statement() && is_statement() && |
| 18 | entry.source_position() > source_position())) { |
| 19 | // Position is updated if any of the following conditions are met: |
| 20 | // (1) there is no existing position. |
| 21 | // (2) the incoming position is a statement and the current position |
| 22 | // is an expression. |
| 23 | // (3) the existing position is a statement and the incoming |
| 24 | // statement has a later source position. |
| 25 | // Condition 3 is needed for the first statement in a function which |
| 26 | // may end up with later statement positions being added during bytecode |
| 27 | // generation. |
| 28 | source_position_ = entry.source_position_; |
| 29 | is_statement_ = entry.is_statement_; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | BytecodeNode::BytecodeNode(Bytecode bytecode) { |
| 34 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 0); |
| 35 | bytecode_ = bytecode; |
| 36 | operand_scale_ = OperandScale::kSingle; |
| 37 | } |
| 38 | |
| 39 | BytecodeNode::BytecodeNode(Bytecode bytecode, uint32_t operand0, |
| 40 | OperandScale operand_scale) { |
| 41 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 1); |
| 42 | bytecode_ = bytecode; |
| 43 | operands_[0] = operand0; |
| 44 | operand_scale_ = operand_scale; |
| 45 | } |
| 46 | |
| 47 | BytecodeNode::BytecodeNode(Bytecode bytecode, uint32_t operand0, |
| 48 | uint32_t operand1, OperandScale operand_scale) { |
| 49 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 2); |
| 50 | bytecode_ = bytecode; |
| 51 | operands_[0] = operand0; |
| 52 | operands_[1] = operand1; |
| 53 | operand_scale_ = operand_scale; |
| 54 | } |
| 55 | |
| 56 | BytecodeNode::BytecodeNode(Bytecode bytecode, uint32_t operand0, |
| 57 | uint32_t operand1, uint32_t operand2, |
| 58 | OperandScale operand_scale) { |
| 59 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 3); |
| 60 | bytecode_ = bytecode; |
| 61 | operands_[0] = operand0; |
| 62 | operands_[1] = operand1; |
| 63 | operands_[2] = operand2; |
| 64 | operand_scale_ = operand_scale; |
| 65 | } |
| 66 | |
| 67 | BytecodeNode::BytecodeNode(Bytecode bytecode, uint32_t operand0, |
| 68 | uint32_t operand1, uint32_t operand2, |
| 69 | uint32_t operand3, OperandScale operand_scale) { |
| 70 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 4); |
| 71 | bytecode_ = bytecode; |
| 72 | operands_[0] = operand0; |
| 73 | operands_[1] = operand1; |
| 74 | operands_[2] = operand2; |
| 75 | operands_[3] = operand3; |
| 76 | operand_scale_ = operand_scale; |
| 77 | } |
| 78 | |
| 79 | void BytecodeNode::set_bytecode(Bytecode bytecode) { |
| 80 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 0); |
| 81 | bytecode_ = bytecode; |
| 82 | operand_scale_ = OperandScale::kSingle; |
| 83 | } |
| 84 | |
| 85 | void BytecodeNode::set_bytecode(Bytecode bytecode, uint32_t operand0, |
| 86 | OperandScale operand_scale) { |
| 87 | DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), 1); |
| 88 | bytecode_ = bytecode; |
| 89 | operands_[0] = operand0; |
| 90 | operand_scale_ = operand_scale; |
| 91 | } |
| 92 | |
| 93 | size_t BytecodeNode::Size() const { |
| 94 | size_t size = Bytecodes::Size(bytecode_, operand_scale_); |
| 95 | if (Bytecodes::OperandScaleRequiresPrefixBytecode(operand_scale_)) { |
| 96 | size += 1; |
| 97 | } |
| 98 | return size; |
| 99 | } |
| 100 | |
| 101 | void BytecodeNode::Print(std::ostream& os) const { |
| 102 | #ifdef DEBUG |
| 103 | std::ios saved_state(nullptr); |
| 104 | saved_state.copyfmt(os); |
| 105 | |
| 106 | os << Bytecodes::ToString(bytecode_); |
| 107 | if (Bytecodes::OperandScaleRequiresPrefixBytecode(operand_scale_)) { |
| 108 | Bytecode scale_prefix = |
| 109 | Bytecodes::OperandScaleToPrefixBytecode(operand_scale_); |
| 110 | os << '.' << Bytecodes::ToString(scale_prefix); |
| 111 | } |
| 112 | |
| 113 | for (int i = 0; i < operand_count(); ++i) { |
| 114 | os << ' ' << std::setw(8) << std::setfill('0') << std::hex << operands_[i]; |
| 115 | } |
| 116 | os.copyfmt(saved_state); |
| 117 | |
| 118 | if (source_info_.is_valid()) { |
| 119 | os << source_info_; |
| 120 | } |
| 121 | os << '\n'; |
| 122 | #else |
| 123 | os << static_cast<const void*>(this); |
| 124 | #endif // DEBUG |
| 125 | } |
| 126 | |
| 127 | void BytecodeNode::Clone(const BytecodeNode* const other) { |
| 128 | memcpy(this, other, sizeof(*other)); |
| 129 | } |
| 130 | |
| 131 | bool BytecodeNode::operator==(const BytecodeNode& other) const { |
| 132 | if (this == &other) { |
| 133 | return true; |
| 134 | } else if (this->bytecode() != other.bytecode() || |
| 135 | this->source_info() != other.source_info()) { |
| 136 | return false; |
| 137 | } else { |
| 138 | for (int i = 0; i < this->operand_count(); ++i) { |
| 139 | if (this->operand(i) != other.operand(i)) { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | std::ostream& operator<<(std::ostream& os, const BytecodeNode& node) { |
| 148 | node.Print(os); |
| 149 | return os; |
| 150 | } |
| 151 | |
| 152 | std::ostream& operator<<(std::ostream& os, const BytecodeSourceInfo& info) { |
| 153 | if (info.is_valid()) { |
| 154 | char description = info.is_statement() ? 'S' : 'E'; |
| 155 | os << info.source_position() << ' ' << description << '>'; |
| 156 | } |
| 157 | return os; |
| 158 | } |
| 159 | |
| 160 | } // namespace interpreter |
| 161 | } // namespace internal |
| 162 | } // namespace v8 |