blob: ad1f22195e17a49932296b000ff20a7f2d3adc98 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_
19
20#include "code_generator.h"
21#include "nodes.h"
22#include "parallel_move_resolver.h"
23#include "utils/arm64/assembler_arm64.h"
24#include "a64/disasm-a64.h"
25#include "a64/macro-assembler-a64.h"
26#include "arch/arm64/quick_method_frame_info_arm64.h"
27
28namespace art {
29namespace arm64 {
30
31class CodeGeneratorARM64;
32
33static constexpr size_t kArm64WordSize = 8;
34static const vixl::Register kParameterCoreRegisters[] = {
35 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
36};
37static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
38static const vixl::FPRegister kParameterFPRegisters[] = {
39 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
40};
41static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
42
43const vixl::Register tr = vixl::x18; // Thread Register
44const vixl::Register wSuspend = vixl::w19; // Suspend Register
45const vixl::Register xSuspend = vixl::x19;
46
47const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000048const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Alexandre Rames5319def2014-10-23 10:03:10 +010049const vixl::CPURegList runtime_reserved_core_registers(tr, xSuspend, vixl::lr);
50const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
51 vixl::kXRegSize,
52 kArm64CalleeSaveRefSpills);
53
Alexandre Ramesa89086e2014-11-07 17:13:25 +000054Location ARM64ReturnLocation(Primitive::Type return_type);
55
Alexandre Rames5319def2014-10-23 10:03:10 +010056class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
57 public:
58 InvokeDexCallingConvention()
59 : CallingConvention(kParameterCoreRegisters,
60 kParameterCoreRegistersLength,
61 kParameterFPRegisters,
62 kParameterFPRegistersLength) {}
63
64 Location GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000065 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +010066 }
67
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
71};
72
73class InvokeDexCallingConventionVisitor {
74 public:
Alexandre Ramesa89086e2014-11-07 17:13:25 +000075 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Alexandre Rames5319def2014-10-23 10:03:10 +010076
77 Location GetNextLocation(Primitive::Type type);
78 Location GetReturnLocation(Primitive::Type return_type) {
79 return calling_convention.GetReturnLocation(return_type);
80 }
81
82 private:
83 InvokeDexCallingConvention calling_convention;
84 // The current index for core registers.
85 uint32_t gp_index_;
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // The current index for floating-point registers.
87 uint32_t fp_index_;
Alexandre Rames5319def2014-10-23 10:03:10 +010088 // The current stack index.
89 uint32_t stack_index_;
90
91 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
92};
93
94class InstructionCodeGeneratorARM64 : public HGraphVisitor {
95 public:
96 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
97
98#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000099 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100100 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
101#undef DECLARE_VISIT_INSTRUCTION
102
103 void LoadCurrentMethod(XRegister reg);
104
105 Arm64Assembler* GetAssembler() const { return assembler_; }
106
107 private:
108 void HandleAddSub(HBinaryOperation* instr);
109
110 Arm64Assembler* const assembler_;
111 CodeGeneratorARM64* const codegen_;
112
113 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
114};
115
116class LocationsBuilderARM64 : public HGraphVisitor {
117 public:
118 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
119 : HGraphVisitor(graph), codegen_(codegen) {}
120
121#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000122 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100123 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
124#undef DECLARE_VISIT_INSTRUCTION
125
126 private:
127 void HandleAddSub(HBinaryOperation* instr);
128 void HandleInvoke(HInvoke* instr);
129
130 CodeGeneratorARM64* const codegen_;
131 InvokeDexCallingConventionVisitor parameter_visitor_;
132
133 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
134};
135
136class CodeGeneratorARM64 : public CodeGenerator {
137 public:
138 explicit CodeGeneratorARM64(HGraph* graph);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000139 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100140
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000141 void GenerateFrameEntry() OVERRIDE;
142 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100143
144 static const vixl::CPURegList& GetFramePreservedRegisters() {
145 static const vixl::CPURegList frame_preserved_regs =
146 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
147 return frame_preserved_regs;
148 }
149 static int GetFramePreservedRegistersSize() {
150 return GetFramePreservedRegisters().TotalSizeInBytes();
151 }
152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000153 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100154
155 vixl::Label* GetLabelOf(HBasicBlock* block) const {
156 return block_labels_ + block->GetBlockId();
157 }
158
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000159 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100160
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000161 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100162 return kArm64WordSize;
163 }
164
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000165 uintptr_t GetAddressOf(HBasicBlock* block ATTRIBUTE_UNUSED) const OVERRIDE {
166 UNIMPLEMENTED(INFO) << "TODO: GetAddressOf";
167 return 0u;
168 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100169
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000170 size_t FrameEntrySpillSize() const OVERRIDE;
171
172 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
173 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
174 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100175
176 // Emit a write barrier.
177 void MarkGCCard(vixl::Register object, vixl::Register value);
178
179 // Register allocation.
180
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000181 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100182 // AllocateFreeRegister() is only used when allocating registers locally
183 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000184 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000186 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100187
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000188 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700189 UNUSED(stack_index);
190 UNUSED(reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100191 UNIMPLEMENTED(INFO) << "TODO: SaveCoreRegister";
192 return 0;
193 }
194
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000195 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700196 UNUSED(stack_index);
197 UNUSED(reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100198 UNIMPLEMENTED(INFO) << "TODO: RestoreCoreRegister";
199 return 0;
200 }
201
202 // The number of registers that can be allocated. The register allocator may
203 // decide to reserve and not use a few of them.
204 // We do not consider registers sp, xzr, wzr. They are either not allocatable
205 // (xzr, wzr), or make for poor allocatable registers (sp alignment
206 // requirements, etc.). This also facilitates our task as all other registers
207 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000208 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
209 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
211
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000212 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
213 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100214
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000215 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100216 return InstructionSet::kArm64;
217 }
218
219 void MoveHelper(Location destination, Location source, Primitive::Type type);
220
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000221 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 HGraph* graph = GetGraph();
223 int length = graph->GetBlocks().Size();
224 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
225 for (int i = 0; i < length; ++i) {
226 new(block_labels_ + i) vixl::Label();
227 }
228 }
229
230 private:
231 // Labels for each block that will be compiled.
232 vixl::Label* block_labels_;
233
234 LocationsBuilderARM64 location_builder_;
235 InstructionCodeGeneratorARM64 instruction_visitor_;
236 Arm64Assembler assembler_;
237
238 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
239};
240
241} // namespace arm64
242} // namespace art
243
244#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_