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_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
| 6 #define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
| 7 |
| 8 #include <deque> |
| 9 |
| 10 #include "src/compiler/instruction-selector.h" |
| 11 #include "src/compiler/raw-machine-assembler.h" |
| 12 #include "test/compiler-unittests/compiler-unittests.h" |
| 13 |
| 14 namespace v8 { |
| 15 namespace internal { |
| 16 namespace compiler { |
| 17 |
| 18 class InstructionSelectorTest : public CompilerTest { |
| 19 public: |
| 20 InstructionSelectorTest() {} |
| 21 virtual ~InstructionSelectorTest() {} |
| 22 |
| 23 protected: |
| 24 class Stream; |
| 25 |
| 26 enum StreamBuilderMode { kAllInstructions, kTargetInstructions }; |
| 27 |
| 28 class StreamBuilder V8_FINAL : public RawMachineAssembler { |
| 29 public: |
| 30 StreamBuilder(InstructionSelectorTest* test, |
| 31 MachineRepresentation return_type) |
| 32 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), |
| 33 CallDescriptorBuilder(test->zone(), return_type)), |
| 34 test_(test) {} |
| 35 StreamBuilder(InstructionSelectorTest* test, |
| 36 MachineRepresentation return_type, |
| 37 MachineRepresentation parameter0_type) |
| 38 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), |
| 39 CallDescriptorBuilder(test->zone(), return_type, |
| 40 parameter0_type)), |
| 41 test_(test) {} |
| 42 StreamBuilder(InstructionSelectorTest* test, |
| 43 MachineRepresentation return_type, |
| 44 MachineRepresentation parameter0_type, |
| 45 MachineRepresentation parameter1_type) |
| 46 : RawMachineAssembler( |
| 47 new (test->zone()) Graph(test->zone()), |
| 48 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, |
| 49 parameter1_type)), |
| 50 test_(test) {} |
| 51 |
| 52 Stream Build(CpuFeature feature) { |
| 53 return Build(InstructionSelector::Features(feature)); |
| 54 } |
| 55 Stream Build(CpuFeature feature1, CpuFeature feature2) { |
| 56 return Build(InstructionSelector::Features(feature1, feature2)); |
| 57 } |
| 58 Stream Build(StreamBuilderMode mode = kTargetInstructions) { |
| 59 return Build(InstructionSelector::Features(), mode); |
| 60 } |
| 61 Stream Build(InstructionSelector::Features features, |
| 62 StreamBuilderMode mode = kTargetInstructions); |
| 63 |
| 64 private: |
| 65 MachineCallDescriptorBuilder* CallDescriptorBuilder( |
| 66 Zone* zone, MachineRepresentation return_type) { |
| 67 return new (zone) MachineCallDescriptorBuilder(return_type, 0, NULL); |
| 68 } |
| 69 |
| 70 MachineCallDescriptorBuilder* CallDescriptorBuilder( |
| 71 Zone* zone, MachineRepresentation return_type, |
| 72 MachineRepresentation parameter0_type) { |
| 73 MachineRepresentation* parameter_types = |
| 74 zone->NewArray<MachineRepresentation>(1); |
| 75 parameter_types[0] = parameter0_type; |
| 76 return new (zone) |
| 77 MachineCallDescriptorBuilder(return_type, 1, parameter_types); |
| 78 } |
| 79 |
| 80 MachineCallDescriptorBuilder* CallDescriptorBuilder( |
| 81 Zone* zone, MachineRepresentation return_type, |
| 82 MachineRepresentation parameter0_type, |
| 83 MachineRepresentation parameter1_type) { |
| 84 MachineRepresentation* parameter_types = |
| 85 zone->NewArray<MachineRepresentation>(2); |
| 86 parameter_types[0] = parameter0_type; |
| 87 parameter_types[1] = parameter1_type; |
| 88 return new (zone) |
| 89 MachineCallDescriptorBuilder(return_type, 2, parameter_types); |
| 90 } |
| 91 |
| 92 private: |
| 93 InstructionSelectorTest* test_; |
| 94 }; |
| 95 |
| 96 class Stream V8_FINAL { |
| 97 public: |
| 98 size_t size() const { return instructions_.size(); } |
| 99 const Instruction* operator[](size_t index) const { |
| 100 EXPECT_LT(index, size()); |
| 101 return instructions_[index]; |
| 102 } |
| 103 |
| 104 int32_t ToInt32(const InstructionOperand* operand) const { |
| 105 return ToConstant(operand).ToInt32(); |
| 106 } |
| 107 |
| 108 private: |
| 109 Constant ToConstant(const InstructionOperand* operand) const { |
| 110 ConstantMap::const_iterator i; |
| 111 if (operand->IsConstant()) { |
| 112 i = constants_.find(operand->index()); |
| 113 EXPECT_NE(constants_.end(), i); |
| 114 } else { |
| 115 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind()); |
| 116 i = immediates_.find(operand->index()); |
| 117 EXPECT_NE(immediates_.end(), i); |
| 118 } |
| 119 EXPECT_EQ(operand->index(), i->first); |
| 120 return i->second; |
| 121 } |
| 122 |
| 123 friend class StreamBuilder; |
| 124 |
| 125 typedef std::map<int, Constant> ConstantMap; |
| 126 |
| 127 ConstantMap constants_; |
| 128 ConstantMap immediates_; |
| 129 std::deque<Instruction*> instructions_; |
| 130 }; |
| 131 }; |
| 132 |
| 133 } // namespace compiler |
| 134 } // namespace internal |
| 135 } // namespace v8 |
| 136 |
| 137 #endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ |
OLD | NEW |