code-generator-impl.h 6.08 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_CODE_GENERATOR_IMPL_H_
#define V8_COMPILER_CODE_GENERATOR_IMPL_H_

8
#include "src/code-stubs.h"
9 10 11 12
#include "src/compiler/code-generator.h"
#include "src/compiler/instruction.h"
#include "src/compiler/linkage.h"
#include "src/compiler/opcodes.h"
13
#include "src/macro-assembler.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27

namespace v8 {
namespace internal {
namespace compiler {

// Converts InstructionOperands from a given instruction to
// architecture-specific
// registers and operands after they have been assigned by the register
// allocator.
class InstructionOperandConverter {
 public:
  InstructionOperandConverter(CodeGenerator* gen, Instruction* instr)
      : gen_(gen), instr_(instr) {}

28 29
  // -- Instruction operand accesses with conversions --------------------------

30
  Register InputRegister(size_t index) {
31 32 33
    return ToRegister(instr_->InputAt(index));
  }

34 35 36 37
  FloatRegister InputFloatRegister(size_t index) {
    return ToFloatRegister(instr_->InputAt(index));
  }

38
  DoubleRegister InputDoubleRegister(size_t index) {
39 40 41
    return ToDoubleRegister(instr_->InputAt(index));
  }

42 43 44 45
  Simd128Register InputSimd128Register(size_t index) {
    return ToSimd128Register(instr_->InputAt(index));
  }

46
  double InputDouble(size_t index) { return ToDouble(instr_->InputAt(index)); }
47

48 49
  float InputFloat32(size_t index) { return ToFloat32(instr_->InputAt(index)); }

50
  int32_t InputInt32(size_t index) {
51 52 53
    return ToConstant(instr_->InputAt(index)).ToInt32();
  }

54 55 56 57
  uint32_t InputUint32(size_t index) {
    return bit_cast<uint32_t>(InputInt32(index));
  }

58 59 60 61
  int64_t InputInt64(size_t index) {
    return ToConstant(instr_->InputAt(index)).ToInt64();
  }

62 63 64
  int8_t InputInt8(size_t index) {
    return static_cast<int8_t>(InputInt32(index));
  }
65

66
  int16_t InputInt16(size_t index) {
67 68 69
    return static_cast<int16_t>(InputInt32(index));
  }

70
  uint8_t InputInt5(size_t index) {
71 72 73
    return static_cast<uint8_t>(InputInt32(index) & 0x1F);
  }

74
  uint8_t InputInt6(size_t index) {
75 76 77
    return static_cast<uint8_t>(InputInt32(index) & 0x3F);
  }

78 79 80 81
  ExternalReference InputExternalReference(size_t index) {
    return ToExternalReference(instr_->InputAt(index));
  }

82
  Handle<HeapObject> InputHeapObject(size_t index) {
83 84 85
    return ToHeapObject(instr_->InputAt(index));
  }

86
  Label* InputLabel(size_t index) { return ToLabel(instr_->InputAt(index)); }
87

88
  RpoNumber InputRpo(size_t index) {
89
    return ToRpoNumber(instr_->InputAt(index));
90 91
  }

92
  Register OutputRegister(size_t index = 0) {
93 94
    return ToRegister(instr_->OutputAt(index));
  }
95

96 97 98
  Register TempRegister(size_t index) {
    return ToRegister(instr_->TempAt(index));
  }
99

100 101 102 103
  FloatRegister OutputFloatRegister() {
    return ToFloatRegister(instr_->Output());
  }

104 105 106 107
  DoubleRegister OutputDoubleRegister() {
    return ToDoubleRegister(instr_->Output());
  }

108 109 110 111
  Simd128Register OutputSimd128Register() {
    return ToSimd128Register(instr_->Output());
  }

112 113 114 115 116 117
  // -- Conversions for operands -----------------------------------------------

  Label* ToLabel(InstructionOperand* op) {
    return gen_->GetLabel(ToRpoNumber(op));
  }

118
  RpoNumber ToRpoNumber(InstructionOperand* op) {
119 120
    return ToConstant(op).ToRpoNumber();
  }
121 122

  Register ToRegister(InstructionOperand* op) {
123
    return LocationOperand::cast(op)->GetRegister();
124 125
  }

126 127 128 129
  FloatRegister ToFloatRegister(InstructionOperand* op) {
    return LocationOperand::cast(op)->GetFloatRegister();
  }

130
  DoubleRegister ToDoubleRegister(InstructionOperand* op) {
131
    return LocationOperand::cast(op)->GetDoubleRegister();
132 133
  }

134 135
  Simd128Register ToSimd128Register(InstructionOperand* op) {
    return LocationOperand::cast(op)->GetSimd128Register();
136 137
  }

138 139
  Constant ToConstant(InstructionOperand* op) {
    if (op->IsImmediate()) {
140
      return gen_->code()->GetImmediate(ImmediateOperand::cast(op));
141
    }
142 143
    return gen_->code()->GetConstant(
        ConstantOperand::cast(op)->virtual_register());
144 145
  }

146
  double ToDouble(InstructionOperand* op) { return ToConstant(op).ToFloat64(); }
147

148 149
  float ToFloat32(InstructionOperand* op) { return ToConstant(op).ToFloat32(); }

150 151 152 153
  ExternalReference ToExternalReference(InstructionOperand* op) {
    return ToConstant(op).ToExternalReference();
  }

154 155
  Handle<HeapObject> ToHeapObject(InstructionOperand* op) {
    return ToConstant(op).ToHeapObject();
156 157
  }

158
  const Frame* frame() const { return gen_->frame(); }
159 160 161
  FrameAccessState* frame_access_state() const {
    return gen_->frame_access_state();
  }
162 163 164 165 166 167 168 169
  Isolate* isolate() const { return gen_->isolate(); }
  Linkage* linkage() const { return gen_->linkage(); }

 protected:
  CodeGenerator* gen_;
  Instruction* instr_;
};

170 171 172
// Eager deoptimization exit.
class DeoptimizationExit : public ZoneObject {
 public:
173 174
  explicit DeoptimizationExit(int deoptimization_id, SourcePosition pos)
      : deoptimization_id_(deoptimization_id), pos_(pos) {}
175 176 177

  int deoptimization_id() const { return deoptimization_id_; }
  Label* label() { return &label_; }
178
  SourcePosition pos() const { return pos_; }
179 180 181 182

 private:
  int const deoptimization_id_;
  Label label_;
183
  SourcePosition const pos_;
184
};
185

186 187 188 189 190 191 192 193 194 195
// Generator for out-of-line code that is emitted after the main code is done.
class OutOfLineCode : public ZoneObject {
 public:
  explicit OutOfLineCode(CodeGenerator* gen);
  virtual ~OutOfLineCode();

  virtual void Generate() = 0;

  Label* entry() { return &entry_; }
  Label* exit() { return &exit_; }
196
  const Frame* frame() const { return frame_; }
197
  Isolate* isolate() const { return masm()->isolate(); }
198 199 200 201 202 203
  MacroAssembler* masm() const { return masm_; }
  OutOfLineCode* next() const { return next_; }

 private:
  Label entry_;
  Label exit_;
204
  const Frame* const frame_;
205 206 207 208 209
  MacroAssembler* const masm_;
  OutOfLineCode* const next_;
};


210 211 212 213 214 215 216 217 218 219 220 221 222
// TODO(dcarney): generify this on bleeding_edge and replace this call
// when merged.
static inline void FinishCode(MacroAssembler* masm) {
#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_ARM
  masm->CheckConstPool(true, false);
#endif
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_CODE_GENERATOR_IMPL_H