Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1 | // Copyright 2014 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_UNITTESTS_COMPILER_INSTRUCTION_SEQUENCE_UNITTEST_H_ |
| 6 | #define V8_UNITTESTS_COMPILER_INSTRUCTION_SEQUENCE_UNITTEST_H_ |
| 7 | |
| 8 | #include "src/compiler/instruction.h" |
| 9 | #include "test/unittests/test-utils.h" |
| 10 | #include "testing/gmock/include/gmock/gmock.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | namespace compiler { |
| 15 | |
| 16 | class InstructionSequenceTest : public TestWithZone { |
| 17 | public: |
| 18 | static const int kDefaultNRegs = 4; |
| 19 | static const int kNoValue = kMinInt; |
| 20 | |
| 21 | typedef BasicBlock::RpoNumber Rpo; |
| 22 | |
| 23 | struct VReg { |
| 24 | VReg() : value_(kNoValue) {} |
| 25 | VReg(PhiInstruction* phi) : value_(phi->virtual_register()) {} // NOLINT |
| 26 | explicit VReg(int value) : value_(value) {} |
| 27 | int value_; |
| 28 | }; |
| 29 | |
| 30 | enum TestOperandType { |
| 31 | kInvalid, |
| 32 | kSameAsFirst, |
| 33 | kRegister, |
| 34 | kFixedRegister, |
| 35 | kSlot, |
| 36 | kFixedSlot, |
| 37 | kImmediate, |
| 38 | kNone, |
| 39 | kConstant, |
| 40 | kUnique, |
| 41 | kUniqueRegister |
| 42 | }; |
| 43 | |
| 44 | struct TestOperand { |
| 45 | TestOperand() : type_(kInvalid), vreg_(), value_(kNoValue) {} |
| 46 | TestOperand(TestOperandType type, int imm) |
| 47 | : type_(type), vreg_(), value_(imm) {} |
| 48 | TestOperand(TestOperandType type, VReg vreg, int value = kNoValue) |
| 49 | : type_(type), vreg_(vreg), value_(value) {} |
| 50 | |
| 51 | TestOperandType type_; |
| 52 | VReg vreg_; |
| 53 | int value_; |
| 54 | }; |
| 55 | |
| 56 | static TestOperand Same() { return TestOperand(kSameAsFirst, VReg()); } |
| 57 | |
| 58 | static TestOperand Reg(VReg vreg, int index = kNoValue) { |
| 59 | TestOperandType type = kRegister; |
| 60 | if (index != kNoValue) type = kFixedRegister; |
| 61 | return TestOperand(type, vreg, index); |
| 62 | } |
| 63 | |
| 64 | static TestOperand Reg(int index = kNoValue) { return Reg(VReg(), index); } |
| 65 | |
| 66 | static TestOperand Slot(VReg vreg, int index = kNoValue) { |
| 67 | TestOperandType type = kSlot; |
| 68 | if (index != kNoValue) type = kFixedSlot; |
| 69 | return TestOperand(type, vreg, index); |
| 70 | } |
| 71 | |
| 72 | static TestOperand Slot(int index = kNoValue) { return Slot(VReg(), index); } |
| 73 | |
| 74 | static TestOperand Const(int index) { |
| 75 | CHECK_NE(kNoValue, index); |
| 76 | return TestOperand(kConstant, VReg(), index); |
| 77 | } |
| 78 | |
| 79 | static TestOperand Use(VReg vreg) { return TestOperand(kNone, vreg); } |
| 80 | |
| 81 | static TestOperand Use() { return Use(VReg()); } |
| 82 | |
| 83 | static TestOperand Unique(VReg vreg) { return TestOperand(kUnique, vreg); } |
| 84 | |
| 85 | static TestOperand UniqueReg(VReg vreg) { |
| 86 | return TestOperand(kUniqueRegister, vreg); |
| 87 | } |
| 88 | |
| 89 | enum BlockCompletionType { kBlockEnd, kFallThrough, kBranch, kJump }; |
| 90 | |
| 91 | struct BlockCompletion { |
| 92 | BlockCompletionType type_; |
| 93 | TestOperand op_; |
| 94 | int offset_0_; |
| 95 | int offset_1_; |
| 96 | }; |
| 97 | |
| 98 | static BlockCompletion FallThrough() { |
| 99 | BlockCompletion completion = {kFallThrough, TestOperand(), 1, kNoValue}; |
| 100 | return completion; |
| 101 | } |
| 102 | |
| 103 | static BlockCompletion Jump(int offset) { |
| 104 | BlockCompletion completion = {kJump, TestOperand(), offset, kNoValue}; |
| 105 | return completion; |
| 106 | } |
| 107 | |
| 108 | static BlockCompletion Branch(TestOperand op, int left_offset, |
| 109 | int right_offset) { |
| 110 | BlockCompletion completion = {kBranch, op, left_offset, right_offset}; |
| 111 | return completion; |
| 112 | } |
| 113 | |
| 114 | static BlockCompletion Last() { |
| 115 | BlockCompletion completion = {kBlockEnd, TestOperand(), kNoValue, kNoValue}; |
| 116 | return completion; |
| 117 | } |
| 118 | |
| 119 | InstructionSequenceTest(); |
| 120 | |
| 121 | void SetNumRegs(int num_general_registers, int num_double_registers); |
| 122 | RegisterConfiguration* config(); |
| 123 | InstructionSequence* sequence(); |
| 124 | |
| 125 | void StartLoop(int loop_blocks); |
| 126 | void EndLoop(); |
| 127 | void StartBlock(); |
| 128 | int EndBlock(BlockCompletion completion = FallThrough()); |
| 129 | |
| 130 | TestOperand Imm(int32_t imm = 0); |
| 131 | VReg Define(TestOperand output_op); |
| 132 | VReg Parameter(TestOperand output_op = Reg()) { return Define(output_op); } |
| 133 | |
| 134 | int Return(TestOperand input_op_0); |
| 135 | int Return(VReg vreg) { return Return(Reg(vreg, 0)); } |
| 136 | |
| 137 | PhiInstruction* Phi(VReg incoming_vreg_0 = VReg(), |
| 138 | VReg incoming_vreg_1 = VReg(), |
| 139 | VReg incoming_vreg_2 = VReg(), |
| 140 | VReg incoming_vreg_3 = VReg()); |
| 141 | void Extend(PhiInstruction* phi, VReg vreg); |
| 142 | |
| 143 | VReg DefineConstant(int32_t imm = 0); |
| 144 | int EmitNop(); |
| 145 | int EmitI(size_t input_size, TestOperand* inputs); |
| 146 | int EmitI(TestOperand input_op_0 = TestOperand(), |
| 147 | TestOperand input_op_1 = TestOperand(), |
| 148 | TestOperand input_op_2 = TestOperand(), |
| 149 | TestOperand input_op_3 = TestOperand()); |
| 150 | VReg EmitOI(TestOperand output_op, size_t input_size, TestOperand* inputs); |
| 151 | VReg EmitOI(TestOperand output_op, TestOperand input_op_0 = TestOperand(), |
| 152 | TestOperand input_op_1 = TestOperand(), |
| 153 | TestOperand input_op_2 = TestOperand(), |
| 154 | TestOperand input_op_3 = TestOperand()); |
| 155 | VReg EmitCall(TestOperand output_op, size_t input_size, TestOperand* inputs); |
| 156 | VReg EmitCall(TestOperand output_op, TestOperand input_op_0 = TestOperand(), |
| 157 | TestOperand input_op_1 = TestOperand(), |
| 158 | TestOperand input_op_2 = TestOperand(), |
| 159 | TestOperand input_op_3 = TestOperand()); |
| 160 | |
| 161 | // Get defining instruction vreg or value returned at instruction creation |
| 162 | // time when there is no return value. |
| 163 | const Instruction* GetInstruction(int instruction_index); |
| 164 | |
| 165 | InstructionBlock* current_block() const { return current_block_; } |
| 166 | int num_general_registers() const { return num_general_registers_; } |
| 167 | int num_double_registers() const { return num_double_registers_; } |
| 168 | |
| 169 | // Called after all instructions have been inserted. |
| 170 | void WireBlocks(); |
| 171 | |
| 172 | private: |
| 173 | VReg NewReg() { return VReg(sequence()->NextVirtualRegister()); } |
| 174 | int NewIndex() { return current_instruction_index_--; } |
| 175 | |
| 176 | static TestOperand Invalid() { return TestOperand(kInvalid, VReg()); } |
| 177 | |
| 178 | int EmitBranch(TestOperand input_op); |
| 179 | int EmitFallThrough(); |
| 180 | int EmitJump(); |
| 181 | Instruction* NewInstruction(InstructionCode code, size_t outputs_size, |
| 182 | InstructionOperand** outputs, |
| 183 | size_t inputs_size = 0, |
| 184 | InstructionOperand* *inputs = nullptr, |
| 185 | size_t temps_size = 0, |
| 186 | InstructionOperand* *temps = nullptr); |
| 187 | InstructionOperand* Unallocated(TestOperand op, |
| 188 | UnallocatedOperand::ExtendedPolicy policy); |
| 189 | InstructionOperand* Unallocated(TestOperand op, |
| 190 | UnallocatedOperand::ExtendedPolicy policy, |
| 191 | UnallocatedOperand::Lifetime lifetime); |
| 192 | InstructionOperand* Unallocated(TestOperand op, |
| 193 | UnallocatedOperand::ExtendedPolicy policy, |
| 194 | int index); |
| 195 | InstructionOperand* Unallocated(TestOperand op, |
| 196 | UnallocatedOperand::BasicPolicy policy, |
| 197 | int index); |
| 198 | InstructionOperand** ConvertInputs(size_t input_size, TestOperand* inputs); |
| 199 | InstructionOperand* ConvertInputOp(TestOperand op); |
| 200 | InstructionOperand* ConvertOutputOp(VReg vreg, TestOperand op); |
| 201 | InstructionBlock* NewBlock(); |
| 202 | void WireBlock(size_t block_offset, int jump_offset); |
| 203 | |
| 204 | int Emit(int instruction_index, InstructionCode code, size_t outputs_size = 0, |
| 205 | InstructionOperand* *outputs = nullptr, size_t inputs_size = 0, |
| 206 | InstructionOperand* *inputs = nullptr, size_t temps_size = 0, |
| 207 | InstructionOperand* *temps = nullptr, bool is_call = false); |
| 208 | |
| 209 | int AddInstruction(int instruction_index, Instruction* instruction); |
| 210 | |
| 211 | struct LoopData { |
| 212 | Rpo loop_header_; |
| 213 | int expected_blocks_; |
| 214 | }; |
| 215 | |
| 216 | typedef std::vector<LoopData> LoopBlocks; |
| 217 | typedef std::map<int, const Instruction*> Instructions; |
| 218 | typedef std::vector<BlockCompletion> Completions; |
| 219 | |
| 220 | SmartPointer<RegisterConfiguration> config_; |
| 221 | InstructionSequence* sequence_; |
| 222 | int num_general_registers_; |
| 223 | int num_double_registers_; |
| 224 | |
| 225 | // Block building state. |
| 226 | InstructionBlocks instruction_blocks_; |
| 227 | Instructions instructions_; |
| 228 | int current_instruction_index_; |
| 229 | Completions completions_; |
| 230 | LoopBlocks loop_blocks_; |
| 231 | InstructionBlock* current_block_; |
| 232 | bool block_returns_; |
| 233 | }; |
| 234 | |
| 235 | } // namespace compiler |
| 236 | } // namespace internal |
| 237 | } // namespace v8 |
| 238 | |
| 239 | #endif // V8_UNITTESTS_COMPILER_INSTRUCTION_SEQUENCE_UNITTEST_H_ |