Home | History | Annotate | Download | only in compiler
      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/compiler/machine-type.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 namespace compiler {
     13 
     14 // Forward declarations.
     15 struct MachineOperatorBuilderImpl;
     16 class Operator;
     17 
     18 
     19 // Supported write barrier modes.
     20 enum WriteBarrierKind { kNoWriteBarrier, kFullWriteBarrier };
     21 
     22 OStream& operator<<(OStream& os, const WriteBarrierKind& write_barrier_kind);
     23 
     24 
     25 typedef MachineType LoadRepresentation;
     26 
     27 
     28 // A Store needs a MachineType and a WriteBarrierKind
     29 // in order to emit the correct write barrier.
     30 class StoreRepresentation FINAL {
     31  public:
     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_;
     42 };
     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 }
     49 
     50 inline bool operator!=(const StoreRepresentation& rep1,
     51                        const StoreRepresentation& rep2) {
     52   return !(rep1 == rep2);
     53 }
     54 
     55 OStream& operator<<(OStream& os, const StoreRepresentation& rep);
     56 
     57 
     58 // Interface for building machine-level operators. These operators are
     59 // machine-level but machine-independent and thus define a language suitable
     60 // for generating code to run on architectures such as ia32, x64, arm, etc.
     61 class MachineOperatorBuilder FINAL {
     62  public:
     63   explicit MachineOperatorBuilder(MachineType word = kMachPtr);
     64 
     65   const Operator* Word32And();
     66   const Operator* Word32Or();
     67   const Operator* Word32Xor();
     68   const Operator* Word32Shl();
     69   const Operator* Word32Shr();
     70   const Operator* Word32Sar();
     71   const Operator* Word32Ror();
     72   const Operator* Word32Equal();
     73 
     74   const Operator* Word64And();
     75   const Operator* Word64Or();
     76   const Operator* Word64Xor();
     77   const Operator* Word64Shl();
     78   const Operator* Word64Shr();
     79   const Operator* Word64Sar();
     80   const Operator* Word64Ror();
     81   const Operator* Word64Equal();
     82 
     83   const Operator* Int32Add();
     84   const Operator* Int32AddWithOverflow();
     85   const Operator* Int32Sub();
     86   const Operator* Int32SubWithOverflow();
     87   const Operator* Int32Mul();
     88   const Operator* Int32Div();
     89   const Operator* Int32UDiv();
     90   const Operator* Int32Mod();
     91   const Operator* Int32UMod();
     92   const Operator* Int32LessThan();
     93   const Operator* Int32LessThanOrEqual();
     94   const Operator* Uint32LessThan();
     95   const Operator* Uint32LessThanOrEqual();
     96 
     97   const Operator* Int64Add();
     98   const Operator* Int64Sub();
     99   const Operator* Int64Mul();
    100   const Operator* Int64Div();
    101   const Operator* Int64UDiv();
    102   const Operator* Int64Mod();
    103   const Operator* Int64UMod();
    104   const Operator* Int64LessThan();
    105   const Operator* Int64LessThanOrEqual();
    106 
    107   // These operators change the representation of numbers while preserving the
    108   // value of the number. Narrowing operators assume the input is representable
    109   // in the target type and are *not* defined for other inputs.
    110   // Use narrowing change operators only when there is a static guarantee that
    111   // the input value is representable in the target value.
    112   const Operator* ChangeFloat32ToFloat64();
    113   const Operator* ChangeFloat64ToInt32();   // narrowing
    114   const Operator* ChangeFloat64ToUint32();  // narrowing
    115   const Operator* ChangeInt32ToFloat64();
    116   const Operator* ChangeInt32ToInt64();
    117   const Operator* ChangeUint32ToFloat64();
    118   const Operator* ChangeUint32ToUint64();
    119 
    120   // These operators truncate numbers, both changing the representation of
    121   // the number and mapping multiple input values onto the same output value.
    122   const Operator* TruncateFloat64ToFloat32();
    123   const Operator* TruncateFloat64ToInt32();  // JavaScript semantics.
    124   const Operator* TruncateInt64ToInt32();
    125 
    126   // Floating point operators always operate with IEEE 754 round-to-nearest.
    127   const Operator* Float64Add();
    128   const Operator* Float64Sub();
    129   const Operator* Float64Mul();
    130   const Operator* Float64Div();
    131   const Operator* Float64Mod();
    132   const Operator* Float64Sqrt();
    133 
    134   // Floating point comparisons complying to IEEE 754.
    135   const Operator* Float64Equal();
    136   const Operator* Float64LessThan();
    137   const Operator* Float64LessThanOrEqual();
    138 
    139   // load [base + index]
    140   const Operator* Load(LoadRepresentation rep);
    141 
    142   // store [base + index], value
    143   const Operator* Store(StoreRepresentation rep);
    144 
    145   // Target machine word-size assumed by this builder.
    146   bool Is32() const { return word() == kRepWord32; }
    147   bool Is64() const { return word() == kRepWord64; }
    148   MachineType word() const { return word_; }
    149 
    150 // Pseudo operators that translate to 32/64-bit operators depending on the
    151 // word-size of the target machine assumed by this builder.
    152 #define PSEUDO_OP_LIST(V) \
    153   V(Word, And)            \
    154   V(Word, Or)             \
    155   V(Word, Xor)            \
    156   V(Word, Shl)            \
    157   V(Word, Shr)            \
    158   V(Word, Sar)            \
    159   V(Word, Ror)            \
    160   V(Word, Equal)          \
    161   V(Int, Add)             \
    162   V(Int, Sub)             \
    163   V(Int, Mul)             \
    164   V(Int, Div)             \
    165   V(Int, UDiv)            \
    166   V(Int, Mod)             \
    167   V(Int, UMod)            \
    168   V(Int, LessThan)        \
    169   V(Int, LessThanOrEqual)
    170 #define PSEUDO_OP(Prefix, Suffix)                                \
    171   const Operator* Prefix##Suffix() {                             \
    172     return Is32() ? Prefix##32##Suffix() : Prefix##64##Suffix(); \
    173   }
    174   PSEUDO_OP_LIST(PSEUDO_OP)
    175 #undef PSEUDO_OP
    176 #undef PSEUDO_OP_LIST
    177 
    178  private:
    179   const MachineOperatorBuilderImpl& impl_;
    180   const MachineType word_;
    181 };
    182 
    183 }  // namespace compiler
    184 }  // namespace internal
    185 }  // namespace v8
    186 
    187 #endif  // V8_COMPILER_MACHINE_OPERATOR_H_
    188