blob: 4e09a276ef38b17e22a189b4a37cb069431489ab [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 Murdoch4a90d5f2016-03-22 12:00:34 +000030 Register InputRegister(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 return ToRegister(instr_->InputAt(index));
32 }
33
Ben Murdoch61f157c2016-09-16 13:49:30 +010034 FloatRegister InputFloatRegister(size_t index) {
35 return ToFloatRegister(instr_->InputAt(index));
36 }
37
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 DoubleRegister InputDoubleRegister(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 return ToDoubleRegister(instr_->InputAt(index));
40 }
41
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 double InputDouble(size_t index) { return ToDouble(instr_->InputAt(index)); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 float InputFloat32(size_t index) { return ToFloat32(instr_->InputAt(index)); }
45
46 int32_t InputInt32(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 return ToConstant(instr_->InputAt(index)).ToInt32();
48 }
49
Ben Murdochc5610432016-08-08 18:44:38 +010050 uint32_t InputUint32(size_t index) {
51 return bit_cast<uint32_t>(InputInt32(index));
52 }
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 int64_t InputInt64(size_t index) {
55 return ToConstant(instr_->InputAt(index)).ToInt64();
56 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 int8_t InputInt8(size_t index) {
59 return static_cast<int8_t>(InputInt32(index));
60 }
61
62 int16_t InputInt16(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 return static_cast<int16_t>(InputInt32(index));
64 }
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 uint8_t InputInt5(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 return static_cast<uint8_t>(InputInt32(index) & 0x1F);
68 }
69
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 uint8_t InputInt6(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 return static_cast<uint8_t>(InputInt32(index) & 0x3F);
72 }
73
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 ExternalReference InputExternalReference(size_t index) {
75 return ToExternalReference(instr_->InputAt(index));
76 }
77
78 Handle<HeapObject> InputHeapObject(size_t index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 return ToHeapObject(instr_->InputAt(index));
80 }
81
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 Label* InputLabel(size_t index) { return ToLabel(instr_->InputAt(index)); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 RpoNumber InputRpo(size_t index) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085 return ToRpoNumber(instr_->InputAt(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 }
87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 Register OutputRegister(size_t index = 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 return ToRegister(instr_->OutputAt(index));
90 }
91
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 Register TempRegister(size_t index) {
93 return ToRegister(instr_->TempAt(index));
94 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040095
Ben Murdoch61f157c2016-09-16 13:49:30 +010096 FloatRegister OutputFloatRegister() {
97 return ToFloatRegister(instr_->Output());
98 }
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 DoubleRegister OutputDoubleRegister() {
101 return ToDoubleRegister(instr_->Output());
102 }
103
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400104 // -- Conversions for operands -----------------------------------------------
105
106 Label* ToLabel(InstructionOperand* op) {
107 return gen_->GetLabel(ToRpoNumber(op));
108 }
109
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 RpoNumber ToRpoNumber(InstructionOperand* op) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400111 return ToConstant(op).ToRpoNumber();
112 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113
114 Register ToRegister(InstructionOperand* op) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 return LocationOperand::cast(op)->GetRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 }
117
118 DoubleRegister ToDoubleRegister(InstructionOperand* op) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 return LocationOperand::cast(op)->GetDoubleRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 }
121
Ben Murdoch61f157c2016-09-16 13:49:30 +0100122 FloatRegister ToFloatRegister(InstructionOperand* op) {
123 return LocationOperand::cast(op)->GetFloatRegister();
124 }
125
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400126 Constant ToConstant(InstructionOperand* op) {
127 if (op->IsImmediate()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 return gen_->code()->GetImmediate(ImmediateOperand::cast(op));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 return gen_->code()->GetConstant(
131 ConstantOperand::cast(op)->virtual_register());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 }
133
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400134 double ToDouble(InstructionOperand* op) { return ToConstant(op).ToFloat64(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 float ToFloat32(InstructionOperand* op) { return ToConstant(op).ToFloat32(); }
137
138 ExternalReference ToExternalReference(InstructionOperand* op) {
139 return ToConstant(op).ToExternalReference();
140 }
141
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400142 Handle<HeapObject> ToHeapObject(InstructionOperand* op) {
143 return ToConstant(op).ToHeapObject();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 }
145
Ben Murdochc5610432016-08-08 18:44:38 +0100146 const Frame* frame() const { return gen_->frame(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 FrameAccessState* frame_access_state() const {
148 return gen_->frame_access_state();
149 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 Isolate* isolate() const { return gen_->isolate(); }
151 Linkage* linkage() const { return gen_->linkage(); }
152
153 protected:
154 CodeGenerator* gen_;
155 Instruction* instr_;
156};
157
Ben Murdochda12d292016-06-02 14:46:10 +0100158// Eager deoptimization exit.
159class DeoptimizationExit : public ZoneObject {
160 public:
161 explicit DeoptimizationExit(int deoptimization_id)
162 : deoptimization_id_(deoptimization_id) {}
163
164 int deoptimization_id() const { return deoptimization_id_; }
165 Label* label() { return &label_; }
166
167 private:
168 int const deoptimization_id_;
169 Label label_;
170};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400172// Generator for out-of-line code that is emitted after the main code is done.
173class OutOfLineCode : public ZoneObject {
174 public:
175 explicit OutOfLineCode(CodeGenerator* gen);
176 virtual ~OutOfLineCode();
177
178 virtual void Generate() = 0;
179
180 Label* entry() { return &entry_; }
181 Label* exit() { return &exit_; }
Ben Murdochc5610432016-08-08 18:44:38 +0100182 const Frame* frame() const { return frame_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 Isolate* isolate() const { return masm()->isolate(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400184 MacroAssembler* masm() const { return masm_; }
185 OutOfLineCode* next() const { return next_; }
186
187 private:
188 Label entry_;
189 Label exit_;
Ben Murdochc5610432016-08-08 18:44:38 +0100190 const Frame* const frame_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400191 MacroAssembler* const masm_;
192 OutOfLineCode* const next_;
193};
194
195
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196// TODO(dcarney): generify this on bleeding_edge and replace this call
197// when merged.
198static inline void FinishCode(MacroAssembler* masm) {
199#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_ARM
200 masm->CheckConstPool(true, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201#elif V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
202 masm->ud2();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203#endif
204}
205
206} // namespace compiler
207} // namespace internal
208} // namespace v8
209
210#endif // V8_COMPILER_CODE_GENERATOR_IMPL_H