42f313019f41152e8a88b57f6dbbeeb0ed6ba970
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / machine-operator.h
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
3 // found in the LICENSE file.
4
5 #ifndef V8_COMPILER_MACHINE_OPERATOR_H_
6 #define V8_COMPILER_MACHINE_OPERATOR_H_
7
8 #include "src/base/flags.h"
9 #include "src/compiler/machine-type.h"
10
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14
15 // Forward declarations.
16 struct MachineOperatorGlobalCache;
17 class Operator;
18
19
20 // Supported write barrier modes.
21 enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier };
22
23 std::ostream& operator<<(std::ostream& os, WriteBarrierKind);
24
25
26 // A Load needs a MachineType.
27 typedef MachineType LoadRepresentation;
28
29
30 // A Store needs a MachineType and a WriteBarrierKind in order to emit the
31 // correct write barrier.
32 class StoreRepresentation FINAL {
33  public:
34   StoreRepresentation(MachineType machine_type,
35                       WriteBarrierKind write_barrier_kind)
36       : machine_type_(machine_type), write_barrier_kind_(write_barrier_kind) {}
37
38   MachineType machine_type() const { return machine_type_; }
39   WriteBarrierKind write_barrier_kind() const { return write_barrier_kind_; }
40
41  private:
42   MachineType machine_type_;
43   WriteBarrierKind write_barrier_kind_;
44 };
45
46 bool operator==(StoreRepresentation, StoreRepresentation);
47 bool operator!=(StoreRepresentation, StoreRepresentation);
48
49 size_t hash_value(StoreRepresentation);
50
51 std::ostream& operator<<(std::ostream&, StoreRepresentation);
52
53 StoreRepresentation const& StoreRepresentationOf(Operator const*);
54
55
56 // A CheckedLoad needs a MachineType.
57 typedef MachineType CheckedLoadRepresentation;
58
59 CheckedLoadRepresentation CheckedLoadRepresentationOf(Operator const*);
60
61
62 // A CheckedStore needs a MachineType.
63 typedef MachineType CheckedStoreRepresentation;
64
65 CheckedStoreRepresentation CheckedStoreRepresentationOf(Operator const*);
66
67
68 // Interface for building machine-level operators. These operators are
69 // machine-level but machine-independent and thus define a language suitable
70 // for generating code to run on architectures such as ia32, x64, arm, etc.
71 class MachineOperatorBuilder FINAL : public ZoneObject {
72  public:
73   // Flags that specify which operations are available. This is useful
74   // for operations that are unsupported by some back-ends.
75   enum Flag {
76     kNoFlags = 0u,
77     kFloat64Floor = 1u << 0,
78     kFloat64Ceil = 1u << 1,
79     kFloat64RoundTruncate = 1u << 2,
80     kFloat64RoundTiesAway = 1u << 3,
81     kInt32DivIsSafe = 1u << 4,
82     kUint32DivIsSafe = 1u << 5,
83     kWord32ShiftIsSafe = 1u << 6
84   };
85   typedef base::Flags<Flag, unsigned> Flags;
86
87   explicit MachineOperatorBuilder(Zone* zone, MachineType word = kMachPtr,
88                                   Flags supportedOperators = kNoFlags);
89
90   const Operator* Word32And();
91   const Operator* Word32Or();
92   const Operator* Word32Xor();
93   const Operator* Word32Shl();
94   const Operator* Word32Shr();
95   const Operator* Word32Sar();
96   const Operator* Word32Ror();
97   const Operator* Word32Equal();
98   bool Word32ShiftIsSafe() const { return flags_ & kWord32ShiftIsSafe; }
99
100   const Operator* Word64And();
101   const Operator* Word64Or();
102   const Operator* Word64Xor();
103   const Operator* Word64Shl();
104   const Operator* Word64Shr();
105   const Operator* Word64Sar();
106   const Operator* Word64Ror();
107   const Operator* Word64Equal();
108
109   const Operator* Int32Add();
110   const Operator* Int32AddWithOverflow();
111   const Operator* Int32Sub();
112   const Operator* Int32SubWithOverflow();
113   const Operator* Int32Mul();
114   const Operator* Int32MulHigh();
115   const Operator* Int32Div();
116   const Operator* Int32Mod();
117   const Operator* Int32LessThan();
118   const Operator* Int32LessThanOrEqual();
119   const Operator* Uint32Div();
120   const Operator* Uint32LessThan();
121   const Operator* Uint32LessThanOrEqual();
122   const Operator* Uint32Mod();
123   const Operator* Uint32MulHigh();
124   bool Int32DivIsSafe() const { return flags_ & kInt32DivIsSafe; }
125   bool Uint32DivIsSafe() const { return flags_ & kUint32DivIsSafe; }
126
127   const Operator* Int64Add();
128   const Operator* Int64Sub();
129   const Operator* Int64Mul();
130   const Operator* Int64Div();
131   const Operator* Int64Mod();
132   const Operator* Int64LessThan();
133   const Operator* Int64LessThanOrEqual();
134   const Operator* Uint64Div();
135   const Operator* Uint64LessThan();
136   const Operator* Uint64Mod();
137
138   // These operators change the representation of numbers while preserving the
139   // value of the number. Narrowing operators assume the input is representable
140   // in the target type and are *not* defined for other inputs.
141   // Use narrowing change operators only when there is a static guarantee that
142   // the input value is representable in the target value.
143   const Operator* ChangeFloat32ToFloat64();
144   const Operator* ChangeFloat64ToInt32();   // narrowing
145   const Operator* ChangeFloat64ToUint32();  // narrowing
146   const Operator* ChangeInt32ToFloat64();
147   const Operator* ChangeInt32ToInt64();
148   const Operator* ChangeUint32ToFloat64();
149   const Operator* ChangeUint32ToUint64();
150
151   // These operators truncate numbers, both changing the representation of
152   // the number and mapping multiple input values onto the same output value.
153   const Operator* TruncateFloat64ToFloat32();
154   const Operator* TruncateFloat64ToInt32();  // JavaScript semantics.
155   const Operator* TruncateInt64ToInt32();
156
157   // Floating point operators always operate with IEEE 754 round-to-nearest.
158   const Operator* Float64Add();
159   const Operator* Float64Sub();
160   const Operator* Float64Mul();
161   const Operator* Float64Div();
162   const Operator* Float64Mod();
163   const Operator* Float64Sqrt();
164
165   // Floating point comparisons complying to IEEE 754.
166   const Operator* Float64Equal();
167   const Operator* Float64LessThan();
168   const Operator* Float64LessThanOrEqual();
169
170   // Floating point rounding.
171   const Operator* Float64Floor();
172   const Operator* Float64Ceil();
173   const Operator* Float64RoundTruncate();
174   const Operator* Float64RoundTiesAway();
175   bool HasFloat64Floor() { return flags_ & kFloat64Floor; }
176   bool HasFloat64Ceil() { return flags_ & kFloat64Ceil; }
177   bool HasFloat64RoundTruncate() { return flags_ & kFloat64RoundTruncate; }
178   bool HasFloat64RoundTiesAway() { return flags_ & kFloat64RoundTiesAway; }
179
180   // load [base + index]
181   const Operator* Load(LoadRepresentation rep);
182
183   // store [base + index], value
184   const Operator* Store(StoreRepresentation rep);
185
186   // Access to the machine stack.
187   const Operator* LoadStackPointer();
188
189   // checked-load heap, index, length
190   const Operator* CheckedLoad(CheckedLoadRepresentation);
191   // checked-store heap, index, length, value
192   const Operator* CheckedStore(CheckedStoreRepresentation);
193
194   // Target machine word-size assumed by this builder.
195   bool Is32() const { return word() == kRepWord32; }
196   bool Is64() const { return word() == kRepWord64; }
197   MachineType word() const { return word_; }
198
199 // Pseudo operators that translate to 32/64-bit operators depending on the
200 // word-size of the target machine assumed by this builder.
201 #define PSEUDO_OP_LIST(V) \
202   V(Word, And)            \
203   V(Word, Or)             \
204   V(Word, Xor)            \
205   V(Word, Shl)            \
206   V(Word, Shr)            \
207   V(Word, Sar)            \
208   V(Word, Ror)            \
209   V(Word, Equal)          \
210   V(Int, Add)             \
211   V(Int, Sub)             \
212   V(Int, Mul)             \
213   V(Int, Div)             \
214   V(Int, Mod)             \
215   V(Int, LessThan)        \
216   V(Int, LessThanOrEqual) \
217   V(Uint, Div)            \
218   V(Uint, LessThan)       \
219   V(Uint, Mod)
220 #define PSEUDO_OP(Prefix, Suffix)                                \
221   const Operator* Prefix##Suffix() {                             \
222     return Is32() ? Prefix##32##Suffix() : Prefix##64##Suffix(); \
223   }
224   PSEUDO_OP_LIST(PSEUDO_OP)
225 #undef PSEUDO_OP
226 #undef PSEUDO_OP_LIST
227
228  private:
229   Zone* zone_;
230   const MachineOperatorGlobalCache& cache_;
231   const MachineType word_;
232   const Flags flags_;
233
234   DISALLOW_COPY_AND_ASSIGN(MachineOperatorBuilder);
235 };
236
237
238 DEFINE_OPERATORS_FOR_FLAGS(MachineOperatorBuilder::Flags)
239
240 }  // namespace compiler
241 }  // namespace internal
242 }  // namespace v8
243
244 #endif  // V8_COMPILER_MACHINE_OPERATOR_H_