blob: 040a745e3cfd553a7796f36846197084c46db7f2 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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_JS_GRAPH_H_
6#define V8_COMPILER_JS_GRAPH_H_
7
8#include "src/compiler/common-node-cache.h"
9#include "src/compiler/common-operator.h"
10#include "src/compiler/graph.h"
11#include "src/compiler/js-operator.h"
12#include "src/compiler/machine-operator.h"
13#include "src/compiler/node-properties.h"
14
15namespace v8 {
16namespace internal {
17namespace compiler {
18
19class Typer;
20
21// Implements a facade on a Graph, enhancing the graph with JS-specific
22// notions, including a builder for for JS* operators, canonicalized global
23// constants, and various helper methods.
24class JSGraph : public ZoneObject {
25 public:
26 JSGraph(Graph* graph, CommonOperatorBuilder* common,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040027 JSOperatorBuilder* javascript, MachineOperatorBuilder* machine)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 : graph_(graph),
29 common_(common),
30 javascript_(javascript),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 machine_(machine),
32 cache_(zone()) {}
33
34 // Canonicalized global constants.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040035 Node* CEntryStubConstant(int result_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 Node* UndefinedConstant();
37 Node* TheHoleConstant();
38 Node* TrueConstant();
39 Node* FalseConstant();
40 Node* NullConstant();
41 Node* ZeroConstant();
42 Node* OneConstant();
43 Node* NaNConstant();
44
45 // Creates a HeapConstant node, possibly canonicalized, without inspecting the
46 // object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 Node* HeapConstant(Unique<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048
49 // Creates a HeapConstant node, possibly canonicalized, and may access the
50 // heap to inspect the object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051 Node* HeapConstant(Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052
53 // Creates a Constant node of the appropriate type for the given object.
54 // Accesses the heap to inspect the object and determine whether one of the
55 // canonicalized globals or a number constant should be returned.
56 Node* Constant(Handle<Object> value);
57
58 // Creates a NumberConstant node, usually canonicalized.
59 Node* Constant(double value);
60
61 // Creates a NumberConstant node, usually canonicalized.
62 Node* Constant(int32_t value);
63
64 // Creates a Int32Constant node, usually canonicalized.
65 Node* Int32Constant(int32_t value);
66 Node* Uint32Constant(uint32_t value) {
67 return Int32Constant(bit_cast<int32_t>(value));
68 }
69
Emily Bernierd0a1eb72015-03-24 16:35:39 -040070 // Creates a HeapConstant node for either true or false.
71 Node* BooleanConstant(bool is_true) {
72 return is_true ? TrueConstant() : FalseConstant();
73 }
74
75 // Creates a Int64Constant node, usually canonicalized.
76 Node* Int64Constant(int64_t value);
77 Node* Uint64Constant(uint64_t value) {
78 return Int64Constant(bit_cast<int64_t>(value));
79 }
80
81 // Creates a Int32Constant/Int64Constant node, depending on the word size of
82 // the target machine.
83 // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
84 // constants is probably not serializable.
85 Node* IntPtrConstant(intptr_t value) {
86 return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
87 : Int64Constant(static_cast<int64_t>(value));
88 }
89 template <typename T>
90 Node* PointerConstant(T* value) {
91 return IntPtrConstant(bit_cast<intptr_t>(value));
92 }
93
94 // Creates a Float32Constant node, usually canonicalized.
95 Node* Float32Constant(float value);
96
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 // Creates a Float64Constant node, usually canonicalized.
98 Node* Float64Constant(double value);
99
100 // Creates an ExternalConstant node, usually canonicalized.
101 Node* ExternalConstant(ExternalReference ref);
102
103 Node* SmiConstant(int32_t immediate) {
104 DCHECK(Smi::IsValid(immediate));
105 return Constant(immediate);
106 }
107
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400108 // Creates a dummy Constant node, used to satisfy calling conventions of
109 // stubs and runtime functions that do not require a context.
110 Node* NoContextConstant() { return ZeroConstant(); }
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 JSOperatorBuilder* javascript() { return javascript_; }
113 CommonOperatorBuilder* common() { return common_; }
114 MachineOperatorBuilder* machine() { return machine_; }
115 Graph* graph() { return graph_; }
116 Zone* zone() { return graph()->zone(); }
117 Isolate* isolate() { return zone()->isolate(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118 Factory* factory() { return isolate()->factory(); }
119
120 void GetCachedNodes(NodeVector* nodes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121
122 private:
123 Graph* graph_;
124 CommonOperatorBuilder* common_;
125 JSOperatorBuilder* javascript_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 MachineOperatorBuilder* machine_;
127
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400128 // TODO(titzer): make this into a simple array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 SetOncePointer<Node> c_entry_stub_constant_;
130 SetOncePointer<Node> undefined_constant_;
131 SetOncePointer<Node> the_hole_constant_;
132 SetOncePointer<Node> true_constant_;
133 SetOncePointer<Node> false_constant_;
134 SetOncePointer<Node> null_constant_;
135 SetOncePointer<Node> zero_constant_;
136 SetOncePointer<Node> one_constant_;
137 SetOncePointer<Node> nan_constant_;
138
139 CommonNodeCache cache_;
140
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400141 Node* ImmovableHeapConstant(Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 Node* NumberConstant(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400144 DISALLOW_COPY_AND_ASSIGN(JSGraph);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145};
146
147} // namespace compiler
148} // namespace internal
149} // namespace v8
150
151#endif