Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / test / compiler-unittests / instruction-selector-unittest.h
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, MachineType return_type)
31         : RawMachineAssembler(new (test->zone()) Graph(test->zone()),
32                               CallDescriptorBuilder(test->zone(), return_type)),
33           test_(test) {}
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,
38                                                     parameter0_type)),
39           test_(test) {}
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,
45                                     parameter1_type)),
46           test_(test) {}
47
48     Stream Build(CpuFeature feature) {
49       return Build(InstructionSelector::Features(feature));
50     }
51     Stream Build(CpuFeature feature1, CpuFeature feature2) {
52       return Build(InstructionSelector::Features(feature1, feature2));
53     }
54     Stream Build(StreamBuilderMode mode = kTargetInstructions) {
55       return Build(InstructionSelector::Features(), mode);
56     }
57     Stream Build(InstructionSelector::Features features,
58                  StreamBuilderMode mode = kTargetInstructions);
59
60    private:
61     MachineCallDescriptorBuilder* CallDescriptorBuilder(
62         Zone* zone, MachineType return_type) {
63       return new (zone) MachineCallDescriptorBuilder(return_type, 0, NULL);
64     }
65
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;
70       return new (zone)
71           MachineCallDescriptorBuilder(return_type, 1, parameter_types);
72     }
73
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;
80       return new (zone)
81           MachineCallDescriptorBuilder(return_type, 2, parameter_types);
82     }
83
84    private:
85     InstructionSelectorTest* test_;
86   };
87
88   class Stream V8_FINAL {
89    public:
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];
94     }
95
96     int32_t ToInt32(const InstructionOperand* operand) const {
97       return ToConstant(operand).ToInt32();
98     }
99
100    private:
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);
106       } else {
107         EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind());
108         i = immediates_.find(operand->index());
109         EXPECT_NE(immediates_.end(), i);
110       }
111       EXPECT_EQ(operand->index(), i->first);
112       return i->second;
113     }
114
115     friend class StreamBuilder;
116
117     typedef std::map<int, Constant> ConstantMap;
118
119     ConstantMap constants_;
120     ConstantMap immediates_;
121     std::deque<Instruction*> instructions_;
122   };
123 };
124
125 }  // namespace compiler
126 }  // namespace internal
127 }  // namespace v8
128
129 #endif  // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_