blob: df3fc0d1e9a8ef32dfc36611375b3f97c5cf9a3a [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 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_MIPS64_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS64_H_
19
20#include "code_generator.h"
21#include "dex/compiler_enums.h"
22#include "driver/compiler_options.h"
23#include "nodes.h"
24#include "parallel_move_resolver.h"
25#include "utils/mips64/assembler_mips64.h"
26
27namespace art {
28namespace mips64 {
29
30// Use a local definition to prevent copying mistakes.
31static constexpr size_t kMips64WordSize = kMips64PointerSize;
32
33
34// InvokeDexCallingConvention registers
35
36static constexpr GpuRegister kParameterCoreRegisters[] =
37 { A1, A2, A3, A4, A5, A6, A7 };
38static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
39
40static constexpr FpuRegister kParameterFpuRegisters[] =
41 { F13, F14, F15, F16, F17, F18, F19 };
42static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
43
44
45// InvokeRuntimeCallingConvention registers
46
47static constexpr GpuRegister kRuntimeParameterCoreRegisters[] =
48 { A0, A1, A2, A3, A4, A5, A6, A7 };
49static constexpr size_t kRuntimeParameterCoreRegistersLength =
50 arraysize(kRuntimeParameterCoreRegisters);
51
52static constexpr FpuRegister kRuntimeParameterFpuRegisters[] =
53 { F12, F13, F14, F15, F16, F17, F18, F19 };
54static constexpr size_t kRuntimeParameterFpuRegistersLength =
55 arraysize(kRuntimeParameterFpuRegisters);
56
57
58static constexpr GpuRegister kCoreCalleeSaves[] =
59 { S0, S1, S2, S3, S4, S5, S6, S7, GP, S8, RA }; // TODO: review
60static constexpr FpuRegister kFpuCalleeSaves[] =
61 { F24, F25, F26, F27, F28, F29, F30, F31 };
62
63
64class CodeGeneratorMIPS64;
65
66class InvokeDexCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
67 public:
68 InvokeDexCallingConvention()
69 : CallingConvention(kParameterCoreRegisters,
70 kParameterCoreRegistersLength,
71 kParameterFpuRegisters,
72 kParameterFpuRegistersLength,
73 kMips64PointerSize) {}
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
77};
78
79class InvokeDexCallingConventionVisitorMIPS64 : public InvokeDexCallingConventionVisitor {
80 public:
81 InvokeDexCallingConventionVisitorMIPS64() {}
82 virtual ~InvokeDexCallingConventionVisitorMIPS64() {}
83
84 Location GetNextLocation(Primitive::Type type) OVERRIDE;
85 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
86 Location GetMethodLocation() const OVERRIDE;
87
88 private:
89 InvokeDexCallingConvention calling_convention;
90
91 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS64);
92};
93
94class InvokeRuntimeCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
95 public:
96 InvokeRuntimeCallingConvention()
97 : CallingConvention(kRuntimeParameterCoreRegisters,
98 kRuntimeParameterCoreRegistersLength,
99 kRuntimeParameterFpuRegisters,
100 kRuntimeParameterFpuRegistersLength,
101 kMips64PointerSize) {}
102
103 Location GetReturnLocation(Primitive::Type return_type);
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
107};
108
Calin Juravlee460d1d2015-09-29 04:52:17 +0100109class FieldAccessCallingConventionMIPS64 : public FieldAccessCallingConvention {
110 public:
111 FieldAccessCallingConventionMIPS64() {}
112
113 Location GetObjectLocation() const OVERRIDE {
114 return Location::RegisterLocation(A1);
115 }
116 Location GetFieldIndexLocation() const OVERRIDE {
117 return Location::RegisterLocation(A0);
118 }
119 Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Goran Jakovljevic8c34ec12015-10-14 11:23:48 +0200120 return Location::RegisterLocation(V0);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100121 }
122 Location GetSetValueLocation(
123 Primitive::Type type ATTRIBUTE_UNUSED, bool is_instance) const OVERRIDE {
124 return is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1);
125 }
126 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
127 return Location::FpuRegisterLocation(F0);
128 }
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS64);
132};
133
Alexey Frunze4dda3372015-06-01 18:31:49 -0700134class ParallelMoveResolverMIPS64 : public ParallelMoveResolverWithSwap {
135 public:
136 ParallelMoveResolverMIPS64(ArenaAllocator* allocator, CodeGeneratorMIPS64* codegen)
137 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
138
139 void EmitMove(size_t index) OVERRIDE;
140 void EmitSwap(size_t index) OVERRIDE;
141 void SpillScratch(int reg) OVERRIDE;
142 void RestoreScratch(int reg) OVERRIDE;
143
144 void Exchange(int index1, int index2, bool double_slot);
145
146 Mips64Assembler* GetAssembler() const;
147
148 private:
149 CodeGeneratorMIPS64* const codegen_;
150
151 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS64);
152};
153
154class SlowPathCodeMIPS64 : public SlowPathCode {
155 public:
156 SlowPathCodeMIPS64() : entry_label_(), exit_label_() {}
157
158 Label* GetEntryLabel() { return &entry_label_; }
159 Label* GetExitLabel() { return &exit_label_; }
160
161 private:
162 Label entry_label_;
163 Label exit_label_;
164
165 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS64);
166};
167
168class LocationsBuilderMIPS64 : public HGraphVisitor {
169 public:
170 LocationsBuilderMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen)
171 : HGraphVisitor(graph), codegen_(codegen) {}
172
173#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100174 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700175
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100176 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
177 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700178
179#undef DECLARE_VISIT_INSTRUCTION
180
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100181 void VisitInstruction(HInstruction* instruction) OVERRIDE {
182 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
183 << " (id " << instruction->GetId() << ")";
184 }
185
Alexey Frunze4dda3372015-06-01 18:31:49 -0700186 private:
187 void HandleInvoke(HInvoke* invoke);
188 void HandleBinaryOp(HBinaryOperation* operation);
189 void HandleShift(HBinaryOperation* operation);
190 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
191 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
192
193 InvokeDexCallingConventionVisitorMIPS64 parameter_visitor_;
194
195 CodeGeneratorMIPS64* const codegen_;
196
197 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS64);
198};
199
200class InstructionCodeGeneratorMIPS64 : public HGraphVisitor {
201 public:
202 InstructionCodeGeneratorMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen);
203
204#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100205 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700206
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100207 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
208 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700209
210#undef DECLARE_VISIT_INSTRUCTION
211
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100212 void VisitInstruction(HInstruction* instruction) OVERRIDE {
213 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
214 << " (id " << instruction->GetId() << ")";
215 }
216
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 Mips64Assembler* GetAssembler() const { return assembler_; }
218
219 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 void GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path, GpuRegister class_reg);
221 void GenerateMemoryBarrier(MemBarrierKind kind);
222 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
223 void HandleBinaryOp(HBinaryOperation* operation);
224 void HandleShift(HBinaryOperation* operation);
225 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
226 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
227 void GenerateImplicitNullCheck(HNullCheck* instruction);
228 void GenerateExplicitNullCheck(HNullCheck* instruction);
229 void GenerateTestAndBranch(HInstruction* instruction,
230 Label* true_target,
231 Label* false_target,
232 Label* always_true_target);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000233 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700234
235 Mips64Assembler* const assembler_;
236 CodeGeneratorMIPS64* const codegen_;
237
238 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS64);
239};
240
241class CodeGeneratorMIPS64 : public CodeGenerator {
242 public:
243 CodeGeneratorMIPS64(HGraph* graph,
244 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100245 const CompilerOptions& compiler_options,
246 OptimizingCompilerStats* stats = nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 virtual ~CodeGeneratorMIPS64() {}
248
249 void GenerateFrameEntry() OVERRIDE;
250 void GenerateFrameExit() OVERRIDE;
251
252 void Bind(HBasicBlock* block) OVERRIDE;
253
254 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
255
256 size_t GetWordSize() const OVERRIDE { return kMips64WordSize; }
257
258 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMips64WordSize; }
259
260 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
261 return GetLabelOf(block)->Position();
262 }
263
264 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
265 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
266 Mips64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100267 const Mips64Assembler& GetAssembler() const OVERRIDE { return assembler_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268
269 void MarkGCCard(GpuRegister object, GpuRegister value);
270
271 // Register allocation.
272
273 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
274 // AllocateFreeRegister() is only used when allocating registers locally
275 // during CompileBaseline().
276 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
277
278 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
279
280 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
281 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
282 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
283 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
284
285 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
286 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
287
288 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips64; }
289
290 const Mips64InstructionSetFeatures& GetInstructionSetFeatures() const {
291 return isa_features_;
292 }
293
294 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100295 return CommonGetLabelOf<Label>(block_labels_, block);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 }
297
298 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100299 block_labels_ = CommonInitializeLabels<Label>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300 }
301
302 void Finalize(CodeAllocator* allocator) OVERRIDE;
303
304 // Code generation helpers.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100305 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700306
Calin Juravle175dc732015-08-25 15:42:32 +0100307 void MoveConstant(Location destination, int32_t value) OVERRIDE;
308
Calin Juravlee460d1d2015-09-29 04:52:17 +0100309 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
310
311
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 void SwapLocations(Location loc1, Location loc2, Primitive::Type type);
313
314 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100315 void InvokeRuntime(QuickEntrypointEnum entrypoint,
316 HInstruction* instruction,
317 uint32_t dex_pc,
318 SlowPathCode* slow_path) OVERRIDE;
319
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 void InvokeRuntime(int32_t offset,
321 HInstruction* instruction,
322 uint32_t dex_pc,
323 SlowPathCode* slow_path);
324
325 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
326
327 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const { return false; }
328
Andreas Gampe85b62f22015-09-09 13:15:38 -0700329 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
330 void GenerateVirtualCall(HInvokeVirtual* invoke ATTRIBUTE_UNUSED,
331 Location temp ATTRIBUTE_UNUSED) OVERRIDE {
332 UNIMPLEMENTED(FATAL);
333 }
334
335 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
336 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
337 UNIMPLEMENTED(FATAL);
338 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700339
340 private:
341 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100342 Label* block_labels_; // Indexed by block id.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343 Label frame_entry_label_;
344 LocationsBuilderMIPS64 location_builder_;
345 InstructionCodeGeneratorMIPS64 instruction_visitor_;
346 ParallelMoveResolverMIPS64 move_resolver_;
347 Mips64Assembler assembler_;
348 const Mips64InstructionSetFeatures& isa_features_;
349
350 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS64);
351};
352
353} // namespace mips64
354} // namespace art
355
356#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS64_H_