Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / compiler / graph-builder-tester.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_CCTEST_COMPILER_GRAPH_BUILDER_TESTER_H_
6 #define V8_CCTEST_COMPILER_GRAPH_BUILDER_TESTER_H_
7
8 #include "src/v8.h"
9 #include "test/cctest/cctest.h"
10
11 #include "src/compiler/common-operator.h"
12 #include "src/compiler/graph-builder.h"
13 #include "src/compiler/machine-node-factory.h"
14 #include "src/compiler/machine-operator.h"
15 #include "src/compiler/simplified-node-factory.h"
16 #include "src/compiler/simplified-operator.h"
17 #include "test/cctest/compiler/call-tester.h"
18 #include "test/cctest/compiler/simplified-graph-builder.h"
19
20 namespace v8 {
21 namespace internal {
22 namespace compiler {
23
24 // A class that just passes node creation on to the Graph.
25 class DirectGraphBuilder : public GraphBuilder {
26  public:
27   explicit DirectGraphBuilder(Graph* graph) : GraphBuilder(graph) {}
28   virtual ~DirectGraphBuilder() {}
29
30  protected:
31   virtual Node* MakeNode(Operator* op, int value_input_count,
32                          Node** value_inputs) {
33     return graph()->NewNode(op, value_input_count, value_inputs);
34   }
35 };
36
37
38 class MachineCallHelper : public CallHelper {
39  public:
40   MachineCallHelper(Zone* zone, MachineCallDescriptorBuilder* builder);
41
42   Node* Parameter(int offset);
43
44   void GenerateCode() { Generate(); }
45
46  protected:
47   virtual byte* Generate();
48   virtual void VerifyParameters(int parameter_count, MachineType* parameters);
49   void InitParameters(GraphBuilder* builder, CommonOperatorBuilder* common);
50
51  protected:
52   int parameter_count() const {
53     return call_descriptor_builder_->parameter_count();
54   }
55
56  private:
57   MachineCallDescriptorBuilder* call_descriptor_builder_;
58   Node** parameters_;
59   // TODO(dcarney): shouldn't need graph stored.
60   Graph* graph_;
61   MaybeHandle<Code> code_;
62 };
63
64
65 class GraphAndBuilders {
66  public:
67   explicit GraphAndBuilders(Zone* zone)
68       : main_graph_(new (zone) Graph(zone)),
69         main_common_(zone),
70         main_machine_(zone),
71         main_simplified_(zone) {}
72
73  protected:
74   // Prefixed with main_ to avoid naiming conflicts.
75   Graph* main_graph_;
76   CommonOperatorBuilder main_common_;
77   MachineOperatorBuilder main_machine_;
78   SimplifiedOperatorBuilder main_simplified_;
79 };
80
81
82 template <typename ReturnType>
83 class GraphBuilderTester
84     : public HandleAndZoneScope,
85       private GraphAndBuilders,
86       public MachineCallHelper,
87       public SimplifiedGraphBuilder,
88       public CallHelper2<ReturnType, GraphBuilderTester<ReturnType> > {
89  public:
90   explicit GraphBuilderTester(MachineType p0 = kMachineLast,
91                               MachineType p1 = kMachineLast,
92                               MachineType p2 = kMachineLast,
93                               MachineType p3 = kMachineLast,
94                               MachineType p4 = kMachineLast)
95       : GraphAndBuilders(main_zone()),
96         MachineCallHelper(
97             main_zone(),
98             ToCallDescriptorBuilder(
99                 main_zone(), ReturnValueTraits<ReturnType>::Representation(),
100                 p0, p1, p2, p3, p4)),
101         SimplifiedGraphBuilder(main_graph_, &main_common_, &main_machine_,
102                                &main_simplified_) {
103     Begin(parameter_count());
104     InitParameters(this, &main_common_);
105   }
106   virtual ~GraphBuilderTester() {}
107
108   Factory* factory() const { return isolate()->factory(); }
109 };
110 }  // namespace compiler
111 }  // namespace internal
112 }  // namespace v8
113
114 #endif  // V8_CCTEST_COMPILER_GRAPH_BUILDER_TESTER_H_