OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_MACHINE_OPERATOR_H_ | 5 #ifndef V8_COMPILER_MACHINE_OPERATOR_H_ |
6 #define V8_COMPILER_MACHINE_OPERATOR_H_ | 6 #define V8_COMPILER_MACHINE_OPERATOR_H_ |
7 | 7 |
8 #include "src/compiler/machine-type.h" | 8 #include "src/compiler/machine-type.h" |
9 #include "src/compiler/opcodes.h" | |
10 #include "src/compiler/operator.h" | |
11 #include "src/zone.h" | |
12 | 9 |
13 namespace v8 { | 10 namespace v8 { |
14 namespace internal { | 11 namespace internal { |
15 namespace compiler { | 12 namespace compiler { |
16 | 13 |
17 // TODO(turbofan): other write barriers are possible based on type | 14 // Forward declarations. |
| 15 struct MachineOperatorBuilderImpl; |
| 16 class Operator; |
| 17 |
| 18 |
| 19 // Supported write barrier modes. |
18 enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier }; | 20 enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier }; |
19 | 21 |
| 22 OStream& operator<<(OStream& os, const WriteBarrierKind& write_barrier_kind); |
| 23 |
| 24 |
| 25 typedef MachineType LoadRepresentation; |
| 26 |
20 | 27 |
21 // A Store needs a MachineType and a WriteBarrierKind | 28 // A Store needs a MachineType and a WriteBarrierKind |
22 // in order to emit the correct write barrier. | 29 // in order to emit the correct write barrier. |
23 struct StoreRepresentation { | 30 class StoreRepresentation FINAL { |
24 MachineType machine_type; | 31 public: |
25 WriteBarrierKind write_barrier_kind; | 32 StoreRepresentation(MachineType machine_type, |
| 33 WriteBarrierKind write_barrier_kind) |
| 34 : machine_type_(machine_type), write_barrier_kind_(write_barrier_kind) {} |
| 35 |
| 36 MachineType machine_type() const { return machine_type_; } |
| 37 WriteBarrierKind write_barrier_kind() const { return write_barrier_kind_; } |
| 38 |
| 39 private: |
| 40 MachineType machine_type_; |
| 41 WriteBarrierKind write_barrier_kind_; |
26 }; | 42 }; |
27 | 43 |
| 44 inline bool operator==(const StoreRepresentation& rep1, |
| 45 const StoreRepresentation& rep2) { |
| 46 return rep1.machine_type() == rep2.machine_type() && |
| 47 rep1.write_barrier_kind() == rep2.write_barrier_kind(); |
| 48 } |
28 | 49 |
29 // TODO(bmeurer): Phi will probably also need this in the future. | 50 inline bool operator!=(const StoreRepresentation& rep1, |
30 template <> | 51 const StoreRepresentation& rep2) { |
31 struct StaticParameterTraits<MachineType> { | 52 return !(rep1 == rep2); |
32 static OStream& PrintTo(OStream& os, MachineType type) { // NOLINT | 53 } |
33 return os << type; | 54 |
34 } | 55 OStream& operator<<(OStream& os, const StoreRepresentation& rep); |
35 static int HashCode(MachineType type) { return type; } | |
36 static bool Equals(MachineType lhs, MachineType rhs) { return lhs == rhs; } | |
37 }; | |
38 | 56 |
39 | 57 |
40 // Interface for building machine-level operators. These operators are | 58 // Interface for building machine-level operators. These operators are |
41 // machine-level but machine-independent and thus define a language suitable | 59 // machine-level but machine-independent and thus define a language suitable |
42 // for generating code to run on architectures such as ia32, x64, arm, etc. | 60 // for generating code to run on architectures such as ia32, x64, arm, etc. |
43 class MachineOperatorBuilder { | 61 class MachineOperatorBuilder FINAL { |
44 public: | 62 public: |
45 explicit MachineOperatorBuilder(Zone* zone, MachineType word = kMachPtr) | 63 explicit MachineOperatorBuilder(MachineType word = kMachPtr); |
46 : zone_(zone), word_(word) { | |
47 CHECK(word == kRepWord32 || word == kRepWord64); | |
48 } | |
49 | 64 |
50 #define SIMPLE(name, properties, inputs, outputs) \ | 65 const Operator* Word32And() WARN_UNUSED_RESULT; |
51 return new (zone_) \ | 66 const Operator* Word32Or() WARN_UNUSED_RESULT; |
52 SimpleOperator(IrOpcode::k##name, properties, inputs, outputs, #name); | 67 const Operator* Word32Xor() WARN_UNUSED_RESULT; |
| 68 const Operator* Word32Shl() WARN_UNUSED_RESULT; |
| 69 const Operator* Word32Shr() WARN_UNUSED_RESULT; |
| 70 const Operator* Word32Sar() WARN_UNUSED_RESULT; |
| 71 const Operator* Word32Ror() WARN_UNUSED_RESULT; |
| 72 const Operator* Word32Equal() WARN_UNUSED_RESULT; |
53 | 73 |
54 #define OP1(name, ptype, pname, properties, inputs, outputs) \ | 74 const Operator* Word64And() WARN_UNUSED_RESULT; |
55 return new (zone_) \ | 75 const Operator* Word64Or() WARN_UNUSED_RESULT; |
56 Operator1<ptype>(IrOpcode::k##name, properties | Operator::kNoThrow, \ | 76 const Operator* Word64Xor() WARN_UNUSED_RESULT; |
57 inputs, outputs, #name, pname) | 77 const Operator* Word64Shl() WARN_UNUSED_RESULT; |
| 78 const Operator* Word64Shr() WARN_UNUSED_RESULT; |
| 79 const Operator* Word64Sar() WARN_UNUSED_RESULT; |
| 80 const Operator* Word64Ror() WARN_UNUSED_RESULT; |
| 81 const Operator* Word64Equal() WARN_UNUSED_RESULT; |
58 | 82 |
59 #define BINOP(name) SIMPLE(name, Operator::kPure, 2, 1) | 83 const Operator* Int32Add() WARN_UNUSED_RESULT; |
60 #define BINOP_O(name) SIMPLE(name, Operator::kPure, 2, 2) | 84 const Operator* Int32AddWithOverflow() WARN_UNUSED_RESULT; |
61 #define BINOP_C(name) \ | 85 const Operator* Int32Sub() WARN_UNUSED_RESULT; |
62 SIMPLE(name, Operator::kCommutative | Operator::kPure, 2, 1) | 86 const Operator* Int32SubWithOverflow() WARN_UNUSED_RESULT; |
63 #define BINOP_AC(name) \ | 87 const Operator* Int32Mul() WARN_UNUSED_RESULT; |
64 SIMPLE(name, \ | 88 const Operator* Int32Div() WARN_UNUSED_RESULT; |
65 Operator::kAssociative | Operator::kCommutative | Operator::kPure, 2, \ | 89 const Operator* Int32UDiv() WARN_UNUSED_RESULT; |
66 1) | 90 const Operator* Int32Mod() WARN_UNUSED_RESULT; |
67 #define BINOP_ACO(name) \ | 91 const Operator* Int32UMod() WARN_UNUSED_RESULT; |
68 SIMPLE(name, \ | 92 const Operator* Int32LessThan() WARN_UNUSED_RESULT; |
69 Operator::kAssociative | Operator::kCommutative | Operator::kPure, 2, \ | 93 const Operator* Int32LessThanOrEqual() WARN_UNUSED_RESULT; |
70 2) | 94 const Operator* Uint32LessThan() WARN_UNUSED_RESULT; |
71 #define UNOP(name) SIMPLE(name, Operator::kPure, 1, 1) | 95 const Operator* Uint32LessThanOrEqual() WARN_UNUSED_RESULT; |
72 | 96 |
73 #define WORD_SIZE(x) return is64() ? Word64##x() : Word32##x() | 97 const Operator* Int64Add() WARN_UNUSED_RESULT; |
74 #define INT_SIZE(x) return is64() ? Int64##x() : Int32##x() | 98 const Operator* Int64Sub() WARN_UNUSED_RESULT; |
75 | 99 const Operator* Int64Mul() WARN_UNUSED_RESULT; |
76 const Operator* Load(MachineType rep) { // load [base + index] | 100 const Operator* Int64Div() WARN_UNUSED_RESULT; |
77 OP1(Load, MachineType, rep, Operator::kNoWrite, 2, 1); | 101 const Operator* Int64UDiv() WARN_UNUSED_RESULT; |
78 } | 102 const Operator* Int64Mod() WARN_UNUSED_RESULT; |
79 // store [base + index], value | 103 const Operator* Int64UMod() WARN_UNUSED_RESULT; |
80 const Operator* Store(MachineType rep, WriteBarrierKind kind) { | 104 const Operator* Int64LessThan() WARN_UNUSED_RESULT; |
81 StoreRepresentation store_rep = {rep, kind}; | 105 const Operator* Int64LessThanOrEqual() WARN_UNUSED_RESULT; |
82 OP1(Store, StoreRepresentation, store_rep, Operator::kNoRead, 3, 0); | |
83 } | |
84 | |
85 const Operator* WordAnd() { WORD_SIZE(And); } | |
86 const Operator* WordOr() { WORD_SIZE(Or); } | |
87 const Operator* WordXor() { WORD_SIZE(Xor); } | |
88 const Operator* WordShl() { WORD_SIZE(Shl); } | |
89 const Operator* WordShr() { WORD_SIZE(Shr); } | |
90 const Operator* WordSar() { WORD_SIZE(Sar); } | |
91 const Operator* WordRor() { WORD_SIZE(Ror); } | |
92 const Operator* WordEqual() { WORD_SIZE(Equal); } | |
93 | |
94 const Operator* Word32And() { BINOP_AC(Word32And); } | |
95 const Operator* Word32Or() { BINOP_AC(Word32Or); } | |
96 const Operator* Word32Xor() { BINOP_AC(Word32Xor); } | |
97 const Operator* Word32Shl() { BINOP(Word32Shl); } | |
98 const Operator* Word32Shr() { BINOP(Word32Shr); } | |
99 const Operator* Word32Sar() { BINOP(Word32Sar); } | |
100 const Operator* Word32Ror() { BINOP(Word32Ror); } | |
101 const Operator* Word32Equal() { BINOP_C(Word32Equal); } | |
102 | |
103 const Operator* Word64And() { BINOP_AC(Word64And); } | |
104 const Operator* Word64Or() { BINOP_AC(Word64Or); } | |
105 const Operator* Word64Xor() { BINOP_AC(Word64Xor); } | |
106 const Operator* Word64Shl() { BINOP(Word64Shl); } | |
107 const Operator* Word64Shr() { BINOP(Word64Shr); } | |
108 const Operator* Word64Sar() { BINOP(Word64Sar); } | |
109 const Operator* Word64Ror() { BINOP(Word64Ror); } | |
110 const Operator* Word64Equal() { BINOP_C(Word64Equal); } | |
111 | |
112 const Operator* Int32Add() { BINOP_AC(Int32Add); } | |
113 const Operator* Int32AddWithOverflow() { BINOP_ACO(Int32AddWithOverflow); } | |
114 const Operator* Int32Sub() { BINOP(Int32Sub); } | |
115 const Operator* Int32SubWithOverflow() { BINOP_O(Int32SubWithOverflow); } | |
116 const Operator* Int32Mul() { BINOP_AC(Int32Mul); } | |
117 const Operator* Int32Div() { BINOP(Int32Div); } | |
118 const Operator* Int32UDiv() { BINOP(Int32UDiv); } | |
119 const Operator* Int32Mod() { BINOP(Int32Mod); } | |
120 const Operator* Int32UMod() { BINOP(Int32UMod); } | |
121 const Operator* Int32LessThan() { BINOP(Int32LessThan); } | |
122 const Operator* Int32LessThanOrEqual() { BINOP(Int32LessThanOrEqual); } | |
123 const Operator* Uint32LessThan() { BINOP(Uint32LessThan); } | |
124 const Operator* Uint32LessThanOrEqual() { BINOP(Uint32LessThanOrEqual); } | |
125 | |
126 const Operator* Int64Add() { BINOP_AC(Int64Add); } | |
127 const Operator* Int64Sub() { BINOP(Int64Sub); } | |
128 const Operator* Int64Mul() { BINOP_AC(Int64Mul); } | |
129 const Operator* Int64Div() { BINOP(Int64Div); } | |
130 const Operator* Int64UDiv() { BINOP(Int64UDiv); } | |
131 const Operator* Int64Mod() { BINOP(Int64Mod); } | |
132 const Operator* Int64UMod() { BINOP(Int64UMod); } | |
133 const Operator* Int64LessThan() { BINOP(Int64LessThan); } | |
134 const Operator* Int64LessThanOrEqual() { BINOP(Int64LessThanOrEqual); } | |
135 | |
136 // Signed comparison of word-sized integer values, translates to int32/int64 | |
137 // comparisons depending on the word-size of the machine. | |
138 const Operator* IntLessThan() { INT_SIZE(LessThan); } | |
139 const Operator* IntLessThanOrEqual() { INT_SIZE(LessThanOrEqual); } | |
140 | 106 |
141 // Convert representation of integers between float64 and int32/uint32. | 107 // Convert representation of integers between float64 and int32/uint32. |
142 // The precise rounding mode and handling of out of range inputs are *not* | 108 // The precise rounding mode and handling of out of range inputs are *not* |
143 // defined for these operators, since they are intended only for use with | 109 // defined for these operators, since they are intended only for use with |
144 // integers. | 110 // integers. |
145 const Operator* ChangeInt32ToFloat64() { UNOP(ChangeInt32ToFloat64); } | 111 const Operator* ChangeInt32ToFloat64() WARN_UNUSED_RESULT; |
146 const Operator* ChangeUint32ToFloat64() { UNOP(ChangeUint32ToFloat64); } | 112 const Operator* ChangeUint32ToFloat64() WARN_UNUSED_RESULT; |
147 const Operator* ChangeFloat64ToInt32() { UNOP(ChangeFloat64ToInt32); } | 113 const Operator* ChangeFloat64ToInt32() WARN_UNUSED_RESULT; |
148 const Operator* ChangeFloat64ToUint32() { UNOP(ChangeFloat64ToUint32); } | 114 const Operator* ChangeFloat64ToUint32() WARN_UNUSED_RESULT; |
149 | 115 |
150 // Sign/zero extend int32/uint32 to int64/uint64. | 116 // Sign/zero extend int32/uint32 to int64/uint64. |
151 const Operator* ChangeInt32ToInt64() { UNOP(ChangeInt32ToInt64); } | 117 const Operator* ChangeInt32ToInt64() WARN_UNUSED_RESULT; |
152 const Operator* ChangeUint32ToUint64() { UNOP(ChangeUint32ToUint64); } | 118 const Operator* ChangeUint32ToUint64() WARN_UNUSED_RESULT; |
153 | 119 |
154 // Truncate double to int32 using JavaScript semantics. | 120 // Truncate double to int32 using JavaScript semantics. |
155 const Operator* TruncateFloat64ToInt32() { UNOP(TruncateFloat64ToInt32); } | 121 const Operator* TruncateFloat64ToInt32() WARN_UNUSED_RESULT; |
156 | 122 |
157 // Truncate the high order bits and convert the remaining bits to int32. | 123 // Truncate the high order bits and convert the remaining bits to int32. |
158 const Operator* TruncateInt64ToInt32() { UNOP(TruncateInt64ToInt32); } | 124 const Operator* TruncateInt64ToInt32() WARN_UNUSED_RESULT; |
159 | 125 |
160 // Floating point operators always operate with IEEE 754 round-to-nearest. | 126 // Floating point operators always operate with IEEE 754 round-to-nearest. |
161 const Operator* Float64Add() { BINOP_C(Float64Add); } | 127 const Operator* Float64Add() WARN_UNUSED_RESULT; |
162 const Operator* Float64Sub() { BINOP(Float64Sub); } | 128 const Operator* Float64Sub() WARN_UNUSED_RESULT; |
163 const Operator* Float64Mul() { BINOP_C(Float64Mul); } | 129 const Operator* Float64Mul() WARN_UNUSED_RESULT; |
164 const Operator* Float64Div() { BINOP(Float64Div); } | 130 const Operator* Float64Div() WARN_UNUSED_RESULT; |
165 const Operator* Float64Mod() { BINOP(Float64Mod); } | 131 const Operator* Float64Mod() WARN_UNUSED_RESULT; |
166 | 132 |
167 // Floating point comparisons complying to IEEE 754. | 133 // Floating point comparisons complying to IEEE 754. |
168 const Operator* Float64Equal() { BINOP_C(Float64Equal); } | 134 const Operator* Float64Equal() WARN_UNUSED_RESULT; |
169 const Operator* Float64LessThan() { BINOP(Float64LessThan); } | 135 const Operator* Float64LessThan() WARN_UNUSED_RESULT; |
170 const Operator* Float64LessThanOrEqual() { BINOP(Float64LessThanOrEqual); } | 136 const Operator* Float64LessThanOrEqual() WARN_UNUSED_RESULT; |
171 | 137 |
172 inline bool is32() const { return word_ == kRepWord32; } | 138 // load [base + index] |
173 inline bool is64() const { return word_ == kRepWord64; } | 139 const Operator* Load(LoadRepresentation rep) WARN_UNUSED_RESULT; |
174 inline MachineType word() const { return word_; } | |
175 | 140 |
176 #undef WORD_SIZE | 141 // store [base + index], value |
177 #undef INT_SIZE | 142 const Operator* Store(StoreRepresentation rep) WARN_UNUSED_RESULT; |
178 #undef UNOP | 143 |
179 #undef BINOP | 144 // Target machine word-size assumed by this builder. |
180 #undef OP1 | 145 bool Is32() const { return word() == kRepWord32; } |
181 #undef SIMPLE | 146 bool Is64() const { return word() == kRepWord64; } |
| 147 MachineType word() const { return word_; } |
| 148 |
| 149 // Pseudo operators that translate to 32/64-bit operators depending on the |
| 150 // word-size of the target machine assumed by this builder. |
| 151 #define PSEUDO_OP_LIST(V) \ |
| 152 V(Word, And) \ |
| 153 V(Word, Or) \ |
| 154 V(Word, Xor) \ |
| 155 V(Word, Shl) \ |
| 156 V(Word, Shr) \ |
| 157 V(Word, Sar) \ |
| 158 V(Word, Ror) \ |
| 159 V(Word, Equal) \ |
| 160 V(Int, Add) \ |
| 161 V(Int, Sub) \ |
| 162 V(Int, Mul) \ |
| 163 V(Int, Div) \ |
| 164 V(Int, UDiv) \ |
| 165 V(Int, Mod) \ |
| 166 V(Int, UMod) \ |
| 167 V(Int, LessThan) \ |
| 168 V(Int, LessThanOrEqual) |
| 169 #define PSEUDO_OP(Prefix, Suffix) \ |
| 170 const Operator* Prefix##Suffix() { \ |
| 171 return Is32() ? Prefix##32##Suffix() : Prefix##64##Suffix(); \ |
| 172 } |
| 173 PSEUDO_OP_LIST(PSEUDO_OP) |
| 174 #undef PSEUDO_OP |
| 175 #undef PSEUDO_OP_LIST |
182 | 176 |
183 private: | 177 private: |
184 Zone* zone_; | 178 const MachineOperatorBuilderImpl& impl_; |
185 MachineType word_; | 179 const MachineType word_; |
186 }; | 180 }; |
187 | 181 |
188 } // namespace compiler | 182 } // namespace compiler |
189 } // namespace internal | 183 } // namespace internal |
190 } // namespace v8 | 184 } // namespace v8 |
191 | 185 |
192 #endif // V8_COMPILER_MACHINE_OPERATOR_H_ | 186 #endif // V8_COMPILER_MACHINE_OPERATOR_H_ |
OLD | NEW |