129f9409e3882275d80fd0f13689abc9d5198e10
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / code-generator-impl.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_CODE_GENERATOR_IMPL_H_
6 #define V8_COMPILER_CODE_GENERATOR_IMPL_H_
7
8 #include "src/code-stubs.h"
9 #include "src/compiler/code-generator.h"
10 #include "src/compiler/instruction.h"
11 #include "src/compiler/linkage.h"
12 #include "src/compiler/opcodes.h"
13 #include "src/macro-assembler.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
19 // Converts InstructionOperands from a given instruction to
20 // architecture-specific
21 // registers and operands after they have been assigned by the register
22 // allocator.
23 class InstructionOperandConverter {
24  public:
25   InstructionOperandConverter(CodeGenerator* gen, Instruction* instr)
26       : gen_(gen), instr_(instr) {}
27
28   // -- Instruction operand accesses with conversions --------------------------
29
30   Register InputRegister(int index) {
31     return ToRegister(instr_->InputAt(index));
32   }
33
34   DoubleRegister InputDoubleRegister(int index) {
35     return ToDoubleRegister(instr_->InputAt(index));
36   }
37
38   double InputDouble(int index) { return ToDouble(instr_->InputAt(index)); }
39
40   int32_t InputInt32(int index) {
41     return ToConstant(instr_->InputAt(index)).ToInt32();
42   }
43
44   int8_t InputInt8(int index) { return static_cast<int8_t>(InputInt32(index)); }
45
46   int16_t InputInt16(int index) {
47     return static_cast<int16_t>(InputInt32(index));
48   }
49
50   uint8_t InputInt5(int index) {
51     return static_cast<uint8_t>(InputInt32(index) & 0x1F);
52   }
53
54   uint8_t InputInt6(int index) {
55     return static_cast<uint8_t>(InputInt32(index) & 0x3F);
56   }
57
58   Handle<HeapObject> InputHeapObject(int index) {
59     return ToHeapObject(instr_->InputAt(index));
60   }
61
62   Label* InputLabel(int index) { return ToLabel(instr_->InputAt(index)); }
63
64   BasicBlock::RpoNumber InputRpo(int index) {
65     return ToRpoNumber(instr_->InputAt(index));
66   }
67
68   Register OutputRegister(int index = 0) {
69     return ToRegister(instr_->OutputAt(index));
70   }
71
72   Register TempRegister(int index) { return ToRegister(instr_->TempAt(index)); }
73
74   DoubleRegister OutputDoubleRegister() {
75     return ToDoubleRegister(instr_->Output());
76   }
77
78   // -- Conversions for operands -----------------------------------------------
79
80   Label* ToLabel(InstructionOperand* op) {
81     return gen_->GetLabel(ToRpoNumber(op));
82   }
83
84   BasicBlock::RpoNumber ToRpoNumber(InstructionOperand* op) {
85     return ToConstant(op).ToRpoNumber();
86   }
87
88   Register ToRegister(InstructionOperand* op) {
89     DCHECK(op->IsRegister());
90     return Register::FromAllocationIndex(op->index());
91   }
92
93   DoubleRegister ToDoubleRegister(InstructionOperand* op) {
94     DCHECK(op->IsDoubleRegister());
95     return DoubleRegister::FromAllocationIndex(op->index());
96   }
97
98   Constant ToConstant(InstructionOperand* op) {
99     if (op->IsImmediate()) {
100       return gen_->code()->GetImmediate(op->index());
101     }
102     return gen_->code()->GetConstant(op->index());
103   }
104
105   double ToDouble(InstructionOperand* op) { return ToConstant(op).ToFloat64(); }
106
107   Handle<HeapObject> ToHeapObject(InstructionOperand* op) {
108     return ToConstant(op).ToHeapObject();
109   }
110
111   Frame* frame() const { return gen_->frame(); }
112   Isolate* isolate() const { return gen_->isolate(); }
113   Linkage* linkage() const { return gen_->linkage(); }
114
115  protected:
116   CodeGenerator* gen_;
117   Instruction* instr_;
118 };
119
120
121 // Generator for out-of-line code that is emitted after the main code is done.
122 class OutOfLineCode : public ZoneObject {
123  public:
124   explicit OutOfLineCode(CodeGenerator* gen);
125   virtual ~OutOfLineCode();
126
127   virtual void Generate() = 0;
128
129   Label* entry() { return &entry_; }
130   Label* exit() { return &exit_; }
131   MacroAssembler* masm() const { return masm_; }
132   OutOfLineCode* next() const { return next_; }
133
134  private:
135   Label entry_;
136   Label exit_;
137   MacroAssembler* const masm_;
138   OutOfLineCode* const next_;
139 };
140
141
142 // TODO(dcarney): generify this on bleeding_edge and replace this call
143 // when merged.
144 static inline void FinishCode(MacroAssembler* masm) {
145 #if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_ARM
146   masm->CheckConstPool(true, false);
147 #elif V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
148   masm->ud2();
149 #endif
150 }
151
152 }  // namespace compiler
153 }  // namespace internal
154 }  // namespace v8
155
156 #endif  // V8_COMPILER_CODE_GENERATOR_IMPL_H