machine-operator.h 10.3 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_MACHINE_OPERATOR_H_
#define V8_COMPILER_MACHINE_OPERATOR_H_

8
#include "src/base/flags.h"
9
#include "src/compiler/machine-type.h"
10 11 12 13 14

namespace v8 {
namespace internal {
namespace compiler {

15
// Forward declarations.
16
struct MachineOperatorGlobalCache;
17 18
class Operator;

19

20
// For operators that are not supported on all platforms.
21
class OptionalOperator final {
22 23 24 25 26 27 28 29 30 31
 public:
  explicit OptionalOperator(const Operator* op) : op_(op) {}

  bool IsSupported() const { return op_ != nullptr; }
  const Operator* op() const {
    DCHECK_NOT_NULL(op_);
    return op_;
  }

 private:
32 33 34 35 36 37 38 39
  const Operator* const op_;
};


// Supported float64 to int32 truncation modes.
enum class TruncationMode : uint8_t {
  kJavaScript,  // ES6 section 7.1.5
  kRoundToZero  // Round towards zero. Implementation defined for NaN and ovf.
40
};
41

42 43 44 45 46 47 48 49 50
V8_INLINE size_t hash_value(TruncationMode mode) {
  return static_cast<uint8_t>(mode);
}

std::ostream& operator<<(std::ostream&, TruncationMode);

TruncationMode TruncationModeOf(Operator const*);


51
// Supported write barrier modes.
52 53
enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier };

54
std::ostream& operator<<(std::ostream& os, WriteBarrierKind);
55 56


57
// A Load needs a MachineType.
58 59
typedef MachineType LoadRepresentation;

60

61 62
// A Store needs a MachineType and a WriteBarrierKind in order to emit the
// correct write barrier.
63
class StoreRepresentation final {
64 65 66 67
 public:
  StoreRepresentation(MachineType machine_type,
                      WriteBarrierKind write_barrier_kind)
      : machine_type_(machine_type), write_barrier_kind_(write_barrier_kind) {}
68

69 70
  MachineType machine_type() const { return machine_type_; }
  WriteBarrierKind write_barrier_kind() const { return write_barrier_kind_; }
71

72 73 74
 private:
  MachineType machine_type_;
  WriteBarrierKind write_barrier_kind_;
75 76
};

77 78
bool operator==(StoreRepresentation, StoreRepresentation);
bool operator!=(StoreRepresentation, StoreRepresentation);
79

80
size_t hash_value(StoreRepresentation);
81

82
std::ostream& operator<<(std::ostream&, StoreRepresentation);
83

84 85
StoreRepresentation const& StoreRepresentationOf(Operator const*);

86

87 88 89 90 91 92 93 94 95 96 97 98
// A CheckedLoad needs a MachineType.
typedef MachineType CheckedLoadRepresentation;

CheckedLoadRepresentation CheckedLoadRepresentationOf(Operator const*);


// A CheckedStore needs a MachineType.
typedef MachineType CheckedStoreRepresentation;

CheckedStoreRepresentation CheckedStoreRepresentationOf(Operator const*);


99 100 101
// Interface for building machine-level operators. These operators are
// machine-level but machine-independent and thus define a language suitable
// for generating code to run on architectures such as ia32, x64, arm, etc.
102
class MachineOperatorBuilder final : public ZoneObject {
103
 public:
104 105
  // Flags that specify which operations are available. This is useful
  // for operations that are unsupported by some back-ends.
106 107
  enum Flag {
    kNoFlags = 0u,
108 109
    // Note that Float*Max behaves like `(a < b) ? b : a`, not like Math.max().
    // Note that Float*Min behaves like `(a < b) ? a : b`, not like Math.min().
110 111 112 113 114 115 116 117 118
    kFloat32Max = 1u << 0,
    kFloat32Min = 1u << 1,
    kFloat64Max = 1u << 2,
    kFloat64Min = 1u << 3,
    kFloat64RoundDown = 1u << 4,
    kFloat64RoundTruncate = 1u << 5,
    kFloat64RoundTiesAway = 1u << 6,
    kInt32DivIsSafe = 1u << 7,
    kUint32DivIsSafe = 1u << 8,
119 120 121 122
    kWord32ShiftIsSafe = 1u << 9,
    kAllOptionalOps = kFloat32Max | kFloat32Min | kFloat64Max | kFloat64Min |
                      kFloat64RoundDown | kFloat64RoundTruncate |
                      kFloat64RoundTiesAway
123 124 125
  };
  typedef base::Flags<Flag, unsigned> Flags;

126
  explicit MachineOperatorBuilder(Zone* zone, MachineType word = kMachPtr,
127
                                  Flags supportedOperators = kNoFlags);
128

129 130 131 132 133 134 135 136
  const Operator* Word32And();
  const Operator* Word32Or();
  const Operator* Word32Xor();
  const Operator* Word32Shl();
  const Operator* Word32Shr();
  const Operator* Word32Sar();
  const Operator* Word32Ror();
  const Operator* Word32Equal();
137
  const Operator* Word32Clz();
138
  bool Word32ShiftIsSafe() const { return flags_ & kWord32ShiftIsSafe; }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  const Operator* Word64And();
  const Operator* Word64Or();
  const Operator* Word64Xor();
  const Operator* Word64Shl();
  const Operator* Word64Shr();
  const Operator* Word64Sar();
  const Operator* Word64Ror();
  const Operator* Word64Equal();

  const Operator* Int32Add();
  const Operator* Int32AddWithOverflow();
  const Operator* Int32Sub();
  const Operator* Int32SubWithOverflow();
  const Operator* Int32Mul();
154
  const Operator* Int32MulHigh();
155 156 157 158
  const Operator* Int32Div();
  const Operator* Int32Mod();
  const Operator* Int32LessThan();
  const Operator* Int32LessThanOrEqual();
159
  const Operator* Uint32Div();
160 161
  const Operator* Uint32LessThan();
  const Operator* Uint32LessThanOrEqual();
162
  const Operator* Uint32Mod();
163
  const Operator* Uint32MulHigh();
164 165
  bool Int32DivIsSafe() const { return flags_ & kInt32DivIsSafe; }
  bool Uint32DivIsSafe() const { return flags_ & kUint32DivIsSafe; }
166 167 168 169 170 171 172 173

  const Operator* Int64Add();
  const Operator* Int64Sub();
  const Operator* Int64Mul();
  const Operator* Int64Div();
  const Operator* Int64Mod();
  const Operator* Int64LessThan();
  const Operator* Int64LessThanOrEqual();
174
  const Operator* Uint64Div();
175
  const Operator* Uint64LessThan();
176
  const Operator* Uint64LessThanOrEqual();
177
  const Operator* Uint64Mod();
178

179 180 181 182 183 184 185 186
  // These operators change the representation of numbers while preserving the
  // value of the number. Narrowing operators assume the input is representable
  // in the target type and are *not* defined for other inputs.
  // Use narrowing change operators only when there is a static guarantee that
  // the input value is representable in the target value.
  const Operator* ChangeFloat32ToFloat64();
  const Operator* ChangeFloat64ToInt32();   // narrowing
  const Operator* ChangeFloat64ToUint32();  // narrowing
187 188
  const Operator* ChangeInt32ToFloat64();
  const Operator* ChangeInt32ToInt64();
189
  const Operator* ChangeUint32ToFloat64();
190
  const Operator* ChangeUint32ToUint64();
191

192 193 194
  // These operators truncate numbers, both changing the representation of
  // the number and mapping multiple input values onto the same output value.
  const Operator* TruncateFloat64ToFloat32();
195
  const Operator* TruncateFloat64ToInt32(TruncationMode);
196
  const Operator* TruncateInt64ToInt32();
197

198 199 200 201 202 203 204
  // These operators reinterpret the bits of a floating point number as an
  // integer and vice versa.
  const Operator* BitcastFloat32ToInt32();
  const Operator* BitcastFloat64ToInt64();
  const Operator* BitcastInt32ToFloat32();
  const Operator* BitcastInt64ToFloat64();

205 206 207 208 209 210 211 212 213 214
  // Floating point operators always operate with IEEE 754 round-to-nearest
  // (single-precision).
  const Operator* Float32Add();
  const Operator* Float32Sub();
  const Operator* Float32Mul();
  const Operator* Float32Div();
  const Operator* Float32Sqrt();

  // Floating point operators always operate with IEEE 754 round-to-nearest
  // (double-precision).
215 216 217 218 219
  const Operator* Float64Add();
  const Operator* Float64Sub();
  const Operator* Float64Mul();
  const Operator* Float64Div();
  const Operator* Float64Mod();
220
  const Operator* Float64Sqrt();
221

222 223 224 225 226 227
  // Floating point comparisons complying to IEEE 754 (single-precision).
  const Operator* Float32Equal();
  const Operator* Float32LessThan();
  const Operator* Float32LessThanOrEqual();

  // Floating point comparisons complying to IEEE 754 (double-precision).
228 229 230
  const Operator* Float64Equal();
  const Operator* Float64LessThan();
  const Operator* Float64LessThanOrEqual();
231

232
  // Floating point min/max complying to IEEE 754 (single-precision).
233 234
  const OptionalOperator Float32Max();
  const OptionalOperator Float32Min();
235 236

  // Floating point min/max complying to IEEE 754 (double-precision).
237 238
  const OptionalOperator Float64Max();
  const OptionalOperator Float64Min();
239

240 241 242 243 244 245
  // Floating point abs complying to IEEE 754 (single-precision).
  const Operator* Float32Abs();

  // Floating point abs complying to IEEE 754 (double-precision).
  const Operator* Float64Abs();

246
  // Floating point rounding.
247 248 249
  const OptionalOperator Float64RoundDown();
  const OptionalOperator Float64RoundTruncate();
  const OptionalOperator Float64RoundTiesAway();
250

251 252 253 254 255 256
  // Floating point bit representation.
  const Operator* Float64ExtractLowWord32();
  const Operator* Float64ExtractHighWord32();
  const Operator* Float64InsertLowWord32();
  const Operator* Float64InsertHighWord32();

257
  // load [base + index]
258
  const Operator* Load(LoadRepresentation rep);
259

260
  // store [base + index], value
261
  const Operator* Store(StoreRepresentation rep);
262

263 264
  // Access to the machine stack.
  const Operator* LoadStackPointer();
265
  const Operator* LoadFramePointer();
266

267 268 269 270 271
  // checked-load heap, index, length
  const Operator* CheckedLoad(CheckedLoadRepresentation);
  // checked-store heap, index, length, value
  const Operator* CheckedStore(CheckedStoreRepresentation);

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  // Target machine word-size assumed by this builder.
  bool Is32() const { return word() == kRepWord32; }
  bool Is64() const { return word() == kRepWord64; }
  MachineType word() const { return word_; }

// Pseudo operators that translate to 32/64-bit operators depending on the
// word-size of the target machine assumed by this builder.
#define PSEUDO_OP_LIST(V) \
  V(Word, And)            \
  V(Word, Or)             \
  V(Word, Xor)            \
  V(Word, Shl)            \
  V(Word, Shr)            \
  V(Word, Sar)            \
  V(Word, Ror)            \
  V(Word, Equal)          \
  V(Int, Add)             \
  V(Int, Sub)             \
  V(Int, Mul)             \
  V(Int, Div)             \
  V(Int, Mod)             \
  V(Int, LessThan)        \
294
  V(Int, LessThanOrEqual) \
295 296 297
  V(Uint, Div)            \
  V(Uint, LessThan)       \
  V(Uint, Mod)
298
#define PSEUDO_OP(Prefix, Suffix)                                \
299
  const Operator* Prefix##Suffix() {                             \
300 301 302 303 304
    return Is32() ? Prefix##32##Suffix() : Prefix##64##Suffix(); \
  }
  PSEUDO_OP_LIST(PSEUDO_OP)
#undef PSEUDO_OP
#undef PSEUDO_OP_LIST
305 306

 private:
307 308 309
  MachineOperatorGlobalCache const& cache_;
  MachineType const word_;
  Flags const flags_;
310

311
  DISALLOW_COPY_AND_ASSIGN(MachineOperatorBuilder);
312
};
313

314 315

DEFINE_OPERATORS_FOR_FLAGS(MachineOperatorBuilder::Flags)
316

317 318 319
}  // namespace compiler
}  // namespace internal
}  // namespace v8
320 321

#endif  // V8_COMPILER_MACHINE_OPERATOR_H_