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.
5 #ifndef V8_COMPILER_CODE_GENERATOR_IMPL_H_
6 #define V8_COMPILER_CODE_GENERATOR_IMPL_H_
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"
19 // Converts InstructionOperands from a given instruction to
20 // architecture-specific
21 // registers and operands after they have been assigned by the register
23 class InstructionOperandConverter {
25 InstructionOperandConverter(CodeGenerator* gen, Instruction* instr)
26 : gen_(gen), instr_(instr) {}
28 // -- Instruction operand accesses with conversions --------------------------
30 Register InputRegister(size_t index) {
31 return ToRegister(instr_->InputAt(index));
34 DoubleRegister InputDoubleRegister(size_t index) {
35 return ToDoubleRegister(instr_->InputAt(index));
38 double InputDouble(size_t index) { return ToDouble(instr_->InputAt(index)); }
40 int32_t InputInt32(size_t index) {
41 return ToConstant(instr_->InputAt(index)).ToInt32();
44 int8_t InputInt8(size_t index) {
45 return static_cast<int8_t>(InputInt32(index));
48 int16_t InputInt16(size_t index) {
49 return static_cast<int16_t>(InputInt32(index));
52 uint8_t InputInt5(size_t index) {
53 return static_cast<uint8_t>(InputInt32(index) & 0x1F);
56 uint8_t InputInt6(size_t index) {
57 return static_cast<uint8_t>(InputInt32(index) & 0x3F);
60 Handle<HeapObject> InputHeapObject(size_t index) {
61 return ToHeapObject(instr_->InputAt(index));
64 Label* InputLabel(size_t index) { return ToLabel(instr_->InputAt(index)); }
66 RpoNumber InputRpo(size_t index) {
67 return ToRpoNumber(instr_->InputAt(index));
70 Register OutputRegister(size_t index = 0) {
71 return ToRegister(instr_->OutputAt(index));
74 Register TempRegister(size_t index) {
75 return ToRegister(instr_->TempAt(index));
78 DoubleRegister OutputDoubleRegister() {
79 return ToDoubleRegister(instr_->Output());
82 // -- Conversions for operands -----------------------------------------------
84 Label* ToLabel(InstructionOperand* op) {
85 return gen_->GetLabel(ToRpoNumber(op));
88 RpoNumber ToRpoNumber(InstructionOperand* op) {
89 return ToConstant(op).ToRpoNumber();
92 Register ToRegister(InstructionOperand* op) {
93 DCHECK(op->IsRegister());
94 return Register::FromAllocationIndex(op->index());
97 DoubleRegister ToDoubleRegister(InstructionOperand* op) {
98 DCHECK(op->IsDoubleRegister());
99 return DoubleRegister::FromAllocationIndex(op->index());
102 Constant ToConstant(InstructionOperand* op) {
103 if (op->IsImmediate()) {
104 return gen_->code()->GetImmediate(op->index());
106 return gen_->code()->GetConstant(op->index());
109 double ToDouble(InstructionOperand* op) { return ToConstant(op).ToFloat64(); }
111 Handle<HeapObject> ToHeapObject(InstructionOperand* op) {
112 return ToConstant(op).ToHeapObject();
115 Frame* frame() const { return gen_->frame(); }
116 Isolate* isolate() const { return gen_->isolate(); }
117 Linkage* linkage() const { return gen_->linkage(); }
125 // Generator for out-of-line code that is emitted after the main code is done.
126 class OutOfLineCode : public ZoneObject {
128 explicit OutOfLineCode(CodeGenerator* gen);
129 virtual ~OutOfLineCode();
131 virtual void Generate() = 0;
133 Label* entry() { return &entry_; }
134 Label* exit() { return &exit_; }
135 MacroAssembler* masm() const { return masm_; }
136 OutOfLineCode* next() const { return next_; }
141 MacroAssembler* const masm_;
142 OutOfLineCode* const next_;
146 // TODO(dcarney): generify this on bleeding_edge and replace this call
148 static inline void FinishCode(MacroAssembler* masm) {
149 #if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_ARM
150 masm->CheckConstPool(true, false);
151 #elif V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
156 } // namespace compiler
157 } // namespace internal
160 #endif // V8_COMPILER_CODE_GENERATOR_IMPL_H