blob: 42f313019f41152e8a88b57f6dbbeeb0ed6ba970 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/base/flags.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/compiler/machine-type.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15// Forward declarations.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016struct MachineOperatorGlobalCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017class Operator;
18
19
20// Supported write barrier modes.
21enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier };
22
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023std::ostream& operator<<(std::ostream& os, WriteBarrierKind);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
25
Emily Bernierd0a1eb72015-03-24 16:35:39 -040026// A Load needs a MachineType.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027typedef MachineType LoadRepresentation;
28
29
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030// A Store needs a MachineType and a WriteBarrierKind in order to emit the
31// correct write barrier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032class 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046bool operator==(StoreRepresentation, StoreRepresentation);
47bool operator!=(StoreRepresentation, StoreRepresentation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049size_t hash_value(StoreRepresentation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051std::ostream& operator<<(std::ostream&, StoreRepresentation);
52
53StoreRepresentation const& StoreRepresentationOf(Operator const*);
54
55
56// A CheckedLoad needs a MachineType.
57typedef MachineType CheckedLoadRepresentation;
58
59CheckedLoadRepresentation CheckedLoadRepresentationOf(Operator const*);
60
61
62// A CheckedStore needs a MachineType.
63typedef MachineType CheckedStoreRepresentation;
64
65CheckedStoreRepresentation CheckedStoreRepresentationOf(Operator const*);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
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.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071class MachineOperatorBuilder FINAL : public ZoneObject {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040073 // 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089
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();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040098 bool Word32ShiftIsSafe() const { return flags_ & kWord32ShiftIsSafe; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099
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();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400114 const Operator* Int32MulHigh();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 const Operator* Int32Div();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 const Operator* Int32Mod();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 const Operator* Int32LessThan();
118 const Operator* Int32LessThanOrEqual();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400119 const Operator* Uint32Div();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 const Operator* Uint32LessThan();
121 const Operator* Uint32LessThanOrEqual();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400122 const Operator* Uint32Mod();
123 const Operator* Uint32MulHigh();
124 bool Int32DivIsSafe() const { return flags_ & kInt32DivIsSafe; }
125 bool Uint32DivIsSafe() const { return flags_ & kUint32DivIsSafe; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126
127 const Operator* Int64Add();
128 const Operator* Int64Sub();
129 const Operator* Int64Mul();
130 const Operator* Int64Div();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 const Operator* Int64Mod();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 const Operator* Int64LessThan();
133 const Operator* Int64LessThanOrEqual();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400134 const Operator* Uint64Div();
135 const Operator* Uint64LessThan();
136 const Operator* Uint64Mod();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137
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
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400170 // 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 // load [base + index]
181 const Operator* Load(LoadRepresentation rep);
182
183 // store [base + index], value
184 const Operator* Store(StoreRepresentation rep);
185
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400186 // 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 // 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) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 V(Int, Mod) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 V(Int, LessThan) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400216 V(Int, LessThanOrEqual) \
217 V(Uint, Div) \
218 V(Uint, LessThan) \
219 V(Uint, Mod)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220#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:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 Zone* zone_;
230 const MachineOperatorGlobalCache& cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 const MachineType word_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400232 const Flags flags_;
233
234 DISALLOW_COPY_AND_ASSIGN(MachineOperatorBuilder);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235};
236
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400237
238DEFINE_OPERATORS_FOR_FLAGS(MachineOperatorBuilder::Flags)
239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240} // namespace compiler
241} // namespace internal
242} // namespace v8
243
244#endif // V8_COMPILER_MACHINE_OPERATOR_H_