blob: 79423441c581d914f1bce763859bf61921b3b3ac [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/code-stubs.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/compiler/code-generator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/compiler/instruction.h"
11#include "src/compiler/linkage.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler/opcodes.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/macro-assembler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014
15namespace v8 {
16namespace internal {
17namespace 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.
23class InstructionOperandConverter {
24 public:
25 InstructionOperandConverter(CodeGenerator* gen, Instruction* instr)
26 : gen_(gen), instr_(instr) {}
27
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028 // -- Instruction operand accesses with conversions --------------------------
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062 Label* InputLabel(int index) { return ToLabel(instr_->InputAt(index)); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063
Emily Bernierd0a1eb72015-03-24 16:35:39 -040064 BasicBlock::RpoNumber InputRpo(int index) {
65 return ToRpoNumber(instr_->InputAt(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 }
67
68 Register OutputRegister(int index = 0) {
69 return ToRegister(instr_->OutputAt(index));
70 }
71
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072 Register TempRegister(int index) { return ToRegister(instr_->TempAt(index)); }
73
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 DoubleRegister OutputDoubleRegister() {
75 return ToDoubleRegister(instr_->Output());
76 }
77
Emily Bernierd0a1eb72015-03-24 16:35:39 -040078 // -- 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087
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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040098 Constant ToConstant(InstructionOperand* op) {
99 if (op->IsImmediate()) {
100 return gen_->code()->GetImmediate(op->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400102 return gen_->code()->GetConstant(op->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 }
104
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400105 double ToDouble(InstructionOperand* op) { return ToConstant(op).ToFloat64(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 Handle<HeapObject> ToHeapObject(InstructionOperand* op) {
108 return ToConstant(op).ToHeapObject();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 }
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
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400121// Generator for out-of-line code that is emitted after the main code is done.
122class 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142// TODO(dcarney): generify this on bleeding_edge and replace this call
143// when merged.
144static inline void FinishCode(MacroAssembler* masm) {
145#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_ARM
146 masm->CheckConstPool(true, false);
147#endif
148}
149
150} // namespace compiler
151} // namespace internal
152} // namespace v8
153
154#endif // V8_COMPILER_CODE_GENERATOR_IMPL_H