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.
5 #ifndef V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_
6 #define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_
10 #include "src/compiler/instruction-selector.h"
11 #include "src/compiler/raw-machine-assembler.h"
12 #include "test/compiler-unittests/compiler-unittests.h"
18 class InstructionSelectorTest : public CompilerTest {
20 InstructionSelectorTest() {}
21 virtual ~InstructionSelectorTest() {}
26 enum StreamBuilderMode { kAllInstructions, kTargetInstructions };
28 class StreamBuilder V8_FINAL : public RawMachineAssembler {
30 StreamBuilder(InstructionSelectorTest* test, MachineType return_type)
31 : RawMachineAssembler(new (test->zone()) Graph(test->zone()),
32 CallDescriptorBuilder(test->zone(), return_type)),
34 StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
35 MachineType parameter0_type)
36 : RawMachineAssembler(new (test->zone()) Graph(test->zone()),
37 CallDescriptorBuilder(test->zone(), return_type,
40 StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
41 MachineType parameter0_type, MachineType parameter1_type)
42 : RawMachineAssembler(
43 new (test->zone()) Graph(test->zone()),
44 CallDescriptorBuilder(test->zone(), return_type, parameter0_type,
48 Stream Build(CpuFeature feature) {
49 return Build(InstructionSelector::Features(feature));
51 Stream Build(CpuFeature feature1, CpuFeature feature2) {
52 return Build(InstructionSelector::Features(feature1, feature2));
54 Stream Build(StreamBuilderMode mode = kTargetInstructions) {
55 return Build(InstructionSelector::Features(), mode);
57 Stream Build(InstructionSelector::Features features,
58 StreamBuilderMode mode = kTargetInstructions);
61 MachineCallDescriptorBuilder* CallDescriptorBuilder(
62 Zone* zone, MachineType return_type) {
63 return new (zone) MachineCallDescriptorBuilder(return_type, 0, NULL);
66 MachineCallDescriptorBuilder* CallDescriptorBuilder(
67 Zone* zone, MachineType return_type, MachineType parameter0_type) {
68 MachineType* parameter_types = zone->NewArray<MachineType>(1);
69 parameter_types[0] = parameter0_type;
71 MachineCallDescriptorBuilder(return_type, 1, parameter_types);
74 MachineCallDescriptorBuilder* CallDescriptorBuilder(
75 Zone* zone, MachineType return_type, MachineType parameter0_type,
76 MachineType parameter1_type) {
77 MachineType* parameter_types = zone->NewArray<MachineType>(2);
78 parameter_types[0] = parameter0_type;
79 parameter_types[1] = parameter1_type;
81 MachineCallDescriptorBuilder(return_type, 2, parameter_types);
85 InstructionSelectorTest* test_;
88 class Stream V8_FINAL {
90 size_t size() const { return instructions_.size(); }
91 const Instruction* operator[](size_t index) const {
92 EXPECT_LT(index, size());
93 return instructions_[index];
96 int32_t ToInt32(const InstructionOperand* operand) const {
97 return ToConstant(operand).ToInt32();
101 Constant ToConstant(const InstructionOperand* operand) const {
102 ConstantMap::const_iterator i;
103 if (operand->IsConstant()) {
104 i = constants_.find(operand->index());
105 EXPECT_NE(constants_.end(), i);
107 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind());
108 i = immediates_.find(operand->index());
109 EXPECT_NE(immediates_.end(), i);
111 EXPECT_EQ(operand->index(), i->first);
115 friend class StreamBuilder;
117 typedef std::map<int, Constant> ConstantMap;
119 ConstantMap constants_;
120 ConstantMap immediates_;
121 std::deque<Instruction*> instructions_;
125 } // namespace compiler
126 } // namespace internal
129 #endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_