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