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