Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_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 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
| 19 | class 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. |
| 24 | class JSGraph : public ZoneObject { |
| 25 | public: |
| 26 | JSGraph(Graph* graph, CommonOperatorBuilder* common, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 27 | JSOperatorBuilder* javascript, MachineOperatorBuilder* machine) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | : graph_(graph), |
| 29 | common_(common), |
| 30 | javascript_(javascript), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | machine_(machine), |
| 32 | cache_(zone()) {} |
| 33 | |
| 34 | // Canonicalized global constants. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 35 | Node* CEntryStubConstant(int result_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 47 | Node* HeapConstant(Unique<HeapObject> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | |
| 49 | // Creates a HeapConstant node, possibly canonicalized, and may access the |
| 50 | // heap to inspect the object. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 51 | Node* HeapConstant(Handle<HeapObject> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 70 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | // 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 108 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 118 | Factory* factory() { return isolate()->factory(); } |
| 119 | |
| 120 | void GetCachedNodes(NodeVector* nodes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | Graph* graph_; |
| 124 | CommonOperatorBuilder* common_; |
| 125 | JSOperatorBuilder* javascript_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | MachineOperatorBuilder* machine_; |
| 127 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 128 | // TODO(titzer): make this into a simple array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 141 | Node* ImmovableHeapConstant(Handle<HeapObject> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | Node* NumberConstant(double value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 144 | DISALLOW_COPY_AND_ASSIGN(JSGraph); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace compiler |
| 148 | } // namespace internal |
| 149 | } // namespace v8 |
| 150 | |
| 151 | #endif |