blob: 03939e35305569a72590b914532a37b18be976c6 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
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_MIPS_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_
19
20#include "code_generator.h"
Andreas Gampe8a0128a2016-11-28 07:38:35 -080021#include "dex_file_types.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020022#include "driver/compiler_options.h"
23#include "nodes.h"
24#include "parallel_move_resolver.h"
Alexey Frunze06a46c42016-07-19 15:00:40 -070025#include "string_reference.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "utils/mips/assembler_mips.h"
Alexey Frunze06a46c42016-07-19 15:00:40 -070027#include "utils/type_reference.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028
29namespace art {
30namespace mips {
31
32// InvokeDexCallingConvention registers
33
34static constexpr Register kParameterCoreRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080035 { A1, A2, A3, T0, T1 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020036static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
37
38static constexpr FRegister kParameterFpuRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080039 { F8, F10, F12, F14, F16, F18 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020040static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
41
42
43// InvokeRuntimeCallingConvention registers
44
45static constexpr Register kRuntimeParameterCoreRegisters[] =
46 { A0, A1, A2, A3 };
47static constexpr size_t kRuntimeParameterCoreRegistersLength =
48 arraysize(kRuntimeParameterCoreRegisters);
49
50static constexpr FRegister kRuntimeParameterFpuRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080051 { F12, F14 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020052static constexpr size_t kRuntimeParameterFpuRegistersLength =
53 arraysize(kRuntimeParameterFpuRegisters);
54
55
56static constexpr Register kCoreCalleeSaves[] =
57 { S0, S1, S2, S3, S4, S5, S6, S7, FP, RA };
58static constexpr FRegister kFpuCalleeSaves[] =
59 { F20, F22, F24, F26, F28, F30 };
60
61
62class CodeGeneratorMIPS;
63
64class InvokeDexCallingConvention : public CallingConvention<Register, FRegister> {
65 public:
66 InvokeDexCallingConvention()
67 : CallingConvention(kParameterCoreRegisters,
68 kParameterCoreRegistersLength,
69 kParameterFpuRegisters,
70 kParameterFpuRegistersLength,
71 kMipsPointerSize) {}
72
73 private:
74 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
75};
76
77class InvokeDexCallingConventionVisitorMIPS : public InvokeDexCallingConventionVisitor {
78 public:
79 InvokeDexCallingConventionVisitorMIPS() {}
80 virtual ~InvokeDexCallingConventionVisitorMIPS() {}
81
82 Location GetNextLocation(Primitive::Type type) OVERRIDE;
83 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
84 Location GetMethodLocation() const OVERRIDE;
85
86 private:
87 InvokeDexCallingConvention calling_convention;
88
89 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS);
90};
91
92class InvokeRuntimeCallingConvention : public CallingConvention<Register, FRegister> {
93 public:
94 InvokeRuntimeCallingConvention()
95 : CallingConvention(kRuntimeParameterCoreRegisters,
96 kRuntimeParameterCoreRegistersLength,
97 kRuntimeParameterFpuRegisters,
98 kRuntimeParameterFpuRegistersLength,
99 kMipsPointerSize) {}
100
101 Location GetReturnLocation(Primitive::Type return_type);
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
105};
106
107class FieldAccessCallingConventionMIPS : public FieldAccessCallingConvention {
108 public:
109 FieldAccessCallingConventionMIPS() {}
110
111 Location GetObjectLocation() const OVERRIDE {
112 return Location::RegisterLocation(A1);
113 }
114 Location GetFieldIndexLocation() const OVERRIDE {
115 return Location::RegisterLocation(A0);
116 }
117 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
118 return Primitive::Is64BitType(type)
119 ? Location::RegisterPairLocation(V0, V1)
120 : Location::RegisterLocation(V0);
121 }
122 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
123 return Primitive::Is64BitType(type)
124 ? Location::RegisterPairLocation(A2, A3)
125 : (is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1));
126 }
127 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
128 return Location::FpuRegisterLocation(F0);
129 }
130
131 private:
132 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS);
133};
134
135class ParallelMoveResolverMIPS : public ParallelMoveResolverWithSwap {
136 public:
137 ParallelMoveResolverMIPS(ArenaAllocator* allocator, CodeGeneratorMIPS* codegen)
138 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
139
140 void EmitMove(size_t index) OVERRIDE;
141 void EmitSwap(size_t index) OVERRIDE;
142 void SpillScratch(int reg) OVERRIDE;
143 void RestoreScratch(int reg) OVERRIDE;
144
145 void Exchange(int index1, int index2, bool double_slot);
146
147 MipsAssembler* GetAssembler() const;
148
149 private:
150 CodeGeneratorMIPS* const codegen_;
151
152 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS);
153};
154
155class SlowPathCodeMIPS : public SlowPathCode {
156 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000157 explicit SlowPathCodeMIPS(HInstruction* instruction)
158 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159
160 MipsLabel* GetEntryLabel() { return &entry_label_; }
161 MipsLabel* GetExitLabel() { return &exit_label_; }
162
163 private:
164 MipsLabel entry_label_;
165 MipsLabel exit_label_;
166
167 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS);
168};
169
170class LocationsBuilderMIPS : public HGraphVisitor {
171 public:
172 LocationsBuilderMIPS(HGraph* graph, CodeGeneratorMIPS* codegen)
173 : HGraphVisitor(graph), codegen_(codegen) {}
174
175#define DECLARE_VISIT_INSTRUCTION(name, super) \
176 void Visit##name(H##name* instr) OVERRIDE;
177
178 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
179 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
180
181#undef DECLARE_VISIT_INSTRUCTION
182
183 void VisitInstruction(HInstruction* instruction) OVERRIDE {
184 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
185 << " (id " << instruction->GetId() << ")";
186 }
187
188 private:
189 void HandleInvoke(HInvoke* invoke);
190 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000191 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 void HandleShift(HBinaryOperation* operation);
193 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
194 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700195 Location RegisterOrZeroConstant(HInstruction* instruction);
196 Location FpuRegisterOrConstantForStore(HInstruction* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200197
198 InvokeDexCallingConventionVisitorMIPS parameter_visitor_;
199
200 CodeGeneratorMIPS* const codegen_;
201
202 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS);
203};
204
Aart Bik42249c32016-01-07 15:33:50 -0800205class InstructionCodeGeneratorMIPS : public InstructionCodeGenerator {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 public:
207 InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen);
208
209#define DECLARE_VISIT_INSTRUCTION(name, super) \
210 void Visit##name(H##name* instr) OVERRIDE;
211
212 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
213 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
214
215#undef DECLARE_VISIT_INSTRUCTION
216
217 void VisitInstruction(HInstruction* instruction) OVERRIDE {
218 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
219 << " (id " << instruction->GetId() << ")";
220 }
221
222 MipsAssembler* GetAssembler() const { return assembler_; }
223
Alexey Frunze96b66822016-09-10 02:32:44 -0700224 // Compare-and-jump packed switch generates approx. 3 + 2.5 * N 32-bit
225 // instructions for N cases.
226 // Table-based packed switch generates approx. 11 32-bit instructions
227 // and N 32-bit data words for N cases.
228 // At N = 6 they come out as 18 and 17 32-bit words respectively.
229 // We switch to the table-based method starting with 7 cases.
230 static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
231
Chris Larsen5633ce72017-04-10 15:47:40 -0700232 void GenerateMemoryBarrier(MemBarrierKind kind);
233
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 private:
235 void GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
237 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000238 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200239 void HandleShift(HBinaryOperation* operation);
Goran Jakovljevice114da22016-12-26 14:21:43 +0100240 void HandleFieldSet(HInstruction* instruction,
241 const FieldInfo& field_info,
242 uint32_t dex_pc,
243 bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200244 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc);
Alexey Frunze15958152017-02-09 19:08:30 -0800245
246 // Generate a heap reference load using one register `out`:
247 //
248 // out <- *(out + offset)
249 //
250 // while honoring heap poisoning and/or read barriers (if any).
251 //
252 // Location `maybe_temp` is used when generating a read barrier and
253 // shall be a register in that case; it may be an invalid location
254 // otherwise.
255 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
256 Location out,
257 uint32_t offset,
258 Location maybe_temp,
259 ReadBarrierOption read_barrier_option);
260 // Generate a heap reference load using two different registers
261 // `out` and `obj`:
262 //
263 // out <- *(obj + offset)
264 //
265 // while honoring heap poisoning and/or read barriers (if any).
266 //
267 // Location `maybe_temp` is used when generating a Baker's (fast
268 // path) read barrier and shall be a register in that case; it may
269 // be an invalid location otherwise.
270 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
271 Location out,
272 Location obj,
273 uint32_t offset,
274 Location maybe_temp,
275 ReadBarrierOption read_barrier_option);
276
Alexey Frunze06a46c42016-07-19 15:00:40 -0700277 // Generate a GC root reference load:
278 //
279 // root <- *(obj + offset)
280 //
281 // while honoring read barriers (if any).
282 void GenerateGcRootFieldLoad(HInstruction* instruction,
283 Location root,
284 Register obj,
Alexey Frunze15958152017-02-09 19:08:30 -0800285 uint32_t offset,
286 ReadBarrierOption read_barrier_option);
287
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800288 void GenerateIntCompare(IfCondition cond, LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700289 // When the function returns `false` it means that the condition holds if `dst` is non-zero
290 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
291 // `dst` are exchanged.
292 bool MaterializeIntCompare(IfCondition cond,
293 LocationSummary* input_locations,
294 Register dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800295 void GenerateIntCompareAndBranch(IfCondition cond,
296 LocationSummary* locations,
297 MipsLabel* label);
298 void GenerateLongCompareAndBranch(IfCondition cond,
299 LocationSummary* locations,
300 MipsLabel* label);
Alexey Frunze2ddb7172016-09-06 17:04:55 -0700301 void GenerateFpCompare(IfCondition cond,
302 bool gt_bias,
303 Primitive::Type type,
304 LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700305 // When the function returns `false` it means that the condition holds if the condition
306 // code flag `cc` is non-zero and doesn't hold if `cc` is zero. If it returns `true`,
307 // the roles of zero and non-zero values of the `cc` flag are exchanged.
308 bool MaterializeFpCompareR2(IfCondition cond,
309 bool gt_bias,
310 Primitive::Type type,
311 LocationSummary* input_locations,
312 int cc);
313 // When the function returns `false` it means that the condition holds if `dst` is non-zero
314 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
315 // `dst` are exchanged.
316 bool MaterializeFpCompareR6(IfCondition cond,
317 bool gt_bias,
318 Primitive::Type type,
319 LocationSummary* input_locations,
320 FRegister dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800321 void GenerateFpCompareAndBranch(IfCondition cond,
322 bool gt_bias,
323 Primitive::Type type,
324 LocationSummary* locations,
325 MipsLabel* label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200326 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000327 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200328 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000329 MipsLabel* false_target);
Alexey Frunze7e99e052015-11-24 19:28:01 -0800330 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
331 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
332 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
333 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze96b66822016-09-10 02:32:44 -0700335 void GenPackedSwitchWithCompares(Register value_reg,
336 int32_t lower_bound,
337 uint32_t num_entries,
338 HBasicBlock* switch_block,
339 HBasicBlock* default_block);
340 void GenTableBasedPackedSwitch(Register value_reg,
341 Register constant_area,
342 int32_t lower_bound,
343 uint32_t num_entries,
344 HBasicBlock* switch_block,
345 HBasicBlock* default_block);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700346 void GenConditionalMoveR2(HSelect* select);
347 void GenConditionalMoveR6(HSelect* select);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
349 MipsAssembler* const assembler_;
350 CodeGeneratorMIPS* const codegen_;
351
352 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS);
353};
354
355class CodeGeneratorMIPS : public CodeGenerator {
356 public:
357 CodeGeneratorMIPS(HGraph* graph,
358 const MipsInstructionSetFeatures& isa_features,
359 const CompilerOptions& compiler_options,
360 OptimizingCompilerStats* stats = nullptr);
361 virtual ~CodeGeneratorMIPS() {}
362
Alexey Frunze73296a72016-06-03 22:51:46 -0700363 void ComputeSpillMask() OVERRIDE;
Alexey Frunze58320ce2016-08-30 21:40:46 -0700364 bool HasAllocatedCalleeSaveRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200365 void GenerateFrameEntry() OVERRIDE;
366 void GenerateFrameExit() OVERRIDE;
367
368 void Bind(HBasicBlock* block) OVERRIDE;
369
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200370 void Move32(Location destination, Location source);
371 void Move64(Location destination, Location source);
372 void MoveConstant(Location location, HConstant* c);
373
374 size_t GetWordSize() const OVERRIDE { return kMipsWordSize; }
375
376 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMipsDoublewordSize; }
377
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100378 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 return assembler_.GetLabelLocation(GetLabelOf(block));
380 }
381
382 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
383 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
384 MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; }
385 const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; }
386
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700387 // Emit linker patches.
388 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800389 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700390
Alexey Frunze15958152017-02-09 19:08:30 -0800391 // Fast path implementation of ReadBarrier::Barrier for a heap
392 // reference field load when Baker's read barriers are used.
393 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
394 Location ref,
395 Register obj,
396 uint32_t offset,
397 Location temp,
398 bool needs_null_check);
399 // Fast path implementation of ReadBarrier::Barrier for a heap
400 // reference array load when Baker's read barriers are used.
401 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
402 Location ref,
403 Register obj,
404 uint32_t data_offset,
405 Location index,
406 Location temp,
407 bool needs_null_check);
408
409 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
410 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
411 //
412 // Load the object reference located at the address
413 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
414 // `ref`, and mark it if needed.
415 //
416 // If `always_update_field` is true, the value of the reference is
417 // atomically updated in the holder (`obj`).
418 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
419 Location ref,
420 Register obj,
421 uint32_t offset,
422 Location index,
423 ScaleFactor scale_factor,
424 Location temp,
425 bool needs_null_check,
426 bool always_update_field = false);
427
428 // Generate a read barrier for a heap reference within `instruction`
429 // using a slow path.
430 //
431 // A read barrier for an object reference read from the heap is
432 // implemented as a call to the artReadBarrierSlow runtime entry
433 // point, which is passed the values in locations `ref`, `obj`, and
434 // `offset`:
435 //
436 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
437 // mirror::Object* obj,
438 // uint32_t offset);
439 //
440 // The `out` location contains the value returned by
441 // artReadBarrierSlow.
442 //
443 // When `index` is provided (i.e. for array accesses), the offset
444 // value passed to artReadBarrierSlow is adjusted to take `index`
445 // into account.
446 void GenerateReadBarrierSlow(HInstruction* instruction,
447 Location out,
448 Location ref,
449 Location obj,
450 uint32_t offset,
451 Location index = Location::NoLocation());
452
453 // If read barriers are enabled, generate a read barrier for a heap
454 // reference using a slow path. If heap poisoning is enabled, also
455 // unpoison the reference in `out`.
456 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
457 Location out,
458 Location ref,
459 Location obj,
460 uint32_t offset,
461 Location index = Location::NoLocation());
462
463 // Generate a read barrier for a GC root within `instruction` using
464 // a slow path.
465 //
466 // A read barrier for an object reference GC root is implemented as
467 // a call to the artReadBarrierForRootSlow runtime entry point,
468 // which is passed the value in location `root`:
469 //
470 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
471 //
472 // The `out` location contains the value returned by
473 // artReadBarrierForRootSlow.
474 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
475
Goran Jakovljevice114da22016-12-26 14:21:43 +0100476 void MarkGCCard(Register object, Register value, bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477
478 // Register allocation.
479
David Brazdil58282f42016-01-14 12:45:10 +0000480 void SetupBlockedRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200481
Roland Levillainf41f9562016-09-14 19:26:48 +0100482 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
483 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
484 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
485 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700486 void ClobberRA() {
487 clobbered_ra_ = true;
488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200489
490 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
491 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
492
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200493 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; }
494
495 const MipsInstructionSetFeatures& GetInstructionSetFeatures() const {
496 return isa_features_;
497 }
498
499 MipsLabel* GetLabelOf(HBasicBlock* block) const {
500 return CommonGetLabelOf<MipsLabel>(block_labels_, block);
501 }
502
503 void Initialize() OVERRIDE {
504 block_labels_ = CommonInitializeLabels<MipsLabel>();
505 }
506
507 void Finalize(CodeAllocator* allocator) OVERRIDE;
508
509 // Code generation helpers.
510
511 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
512
Roland Levillainf41f9562016-09-14 19:26:48 +0100513 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200514
515 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
516
517 // Generate code to invoke a runtime entry point.
518 void InvokeRuntime(QuickEntrypointEnum entrypoint,
519 HInstruction* instruction,
520 uint32_t dex_pc,
Serban Constantinescufca16662016-07-14 09:21:59 +0100521 SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200522
Alexey Frunze15958152017-02-09 19:08:30 -0800523 // Generate code to invoke a runtime entry point, but do not record
524 // PC-related information in a stack map.
525 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
526 HInstruction* instruction,
527 SlowPathCode* slow_path,
528 bool direct);
529
530 void GenerateInvokeRuntime(int32_t entry_point_offset, bool direct);
531
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200532 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
533
Roland Levillainf41f9562016-09-14 19:26:48 +0100534 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200535 return type == Primitive::kPrimLong;
536 }
537
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000538 // Check if the desired_string_load_kind is supported. If it is, return it,
539 // otherwise return a fall-back kind that should be used instead.
540 HLoadString::LoadKind GetSupportedLoadStringKind(
541 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
542
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100543 // Check if the desired_class_load_kind is supported. If it is, return it,
544 // otherwise return a fall-back kind that should be used instead.
545 HLoadClass::LoadKind GetSupportedLoadClassKind(
546 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
547
Vladimir Markodc151b22015-10-15 18:02:30 +0100548 // Check if the desired_dispatch_info is supported. If it is, return it,
549 // otherwise return a fall-back info that should be used instead.
550 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
551 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100552 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200554 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
Chris Larsen3acee732015-11-18 13:31:08 -0800555 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200556
557 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
558 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
559 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
560 }
561
Roland Levillainf41f9562016-09-14 19:26:48 +0100562 void GenerateNop() OVERRIDE;
563 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
564 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000565
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700566 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
567 // and boot image strings. The only difference is the interpretation of the offset_or_index.
568 struct PcRelativePatchInfo {
569 PcRelativePatchInfo(const DexFile& dex_file, uint32_t off_or_idx)
570 : target_dex_file(dex_file), offset_or_index(off_or_idx) { }
571 PcRelativePatchInfo(PcRelativePatchInfo&& other) = default;
572
573 const DexFile& target_dex_file;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700574 // Either the dex cache array element offset or the string/type index.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700575 uint32_t offset_or_index;
576 // Label for the instruction loading the most significant half of the offset that's added to PC
577 // to form the base address (the least significant half is loaded with the instruction that
578 // follows).
579 MipsLabel high_label;
580 // Label for the instruction corresponding to PC+0.
581 MipsLabel pc_rel_label;
582 };
583
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000584 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
585 dex::StringIndex string_index);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800586 PcRelativePatchInfo* NewPcRelativeTypePatch(const DexFile& dex_file, dex::TypeIndex type_index);
Vladimir Marko1998cd02017-01-13 13:02:58 +0000587 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file, dex::TypeIndex type_index);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700588 PcRelativePatchInfo* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
589 uint32_t element_offset);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800590 Literal* DeduplicateBootImageStringLiteral(const DexFile& dex_file,
591 dex::StringIndex string_index);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800592 Literal* DeduplicateBootImageTypeLiteral(const DexFile& dex_file, dex::TypeIndex type_index);
Alexey Frunze06a46c42016-07-19 15:00:40 -0700593 Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700594
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800595 void EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info, Register out, Register base);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000596
Alexey Frunze627c1a02017-01-30 19:28:14 -0800597 // The JitPatchInfo is used for JIT string and class loads.
598 struct JitPatchInfo {
599 JitPatchInfo(const DexFile& dex_file, uint64_t idx)
600 : target_dex_file(dex_file), index(idx) { }
601 JitPatchInfo(JitPatchInfo&& other) = default;
602
603 const DexFile& target_dex_file;
604 // String/type index.
605 uint64_t index;
606 // Label for the instruction loading the most significant half of the address.
607 // The least significant half is loaded with the instruction that follows immediately.
608 MipsLabel high_label;
609 };
610
611 void PatchJitRootUse(uint8_t* code,
612 const uint8_t* roots_data,
613 const JitPatchInfo& info,
614 uint64_t index_in_table) const;
615 JitPatchInfo* NewJitRootStringPatch(const DexFile& dex_file,
616 dex::StringIndex dex_index,
617 Handle<mirror::String> handle);
618 JitPatchInfo* NewJitRootClassPatch(const DexFile& dex_file,
619 dex::TypeIndex dex_index,
620 Handle<mirror::Class> handle);
621
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200622 private:
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700623 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
624
Alexey Frunze06a46c42016-07-19 15:00:40 -0700625 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, Literal*>;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700626 using MethodToLiteralMap = ArenaSafeMap<MethodReference, Literal*, MethodReferenceComparator>;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700627 using BootStringToLiteralMap = ArenaSafeMap<StringReference,
628 Literal*,
629 StringReferenceValueComparator>;
630 using BootTypeToLiteralMap = ArenaSafeMap<TypeReference,
631 Literal*,
632 TypeReferenceValueComparator>;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700633
Alexey Frunze06a46c42016-07-19 15:00:40 -0700634 Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700635 Literal* DeduplicateMethodLiteral(MethodReference target_method, MethodToLiteralMap* map);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700636 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
637 uint32_t offset_or_index,
638 ArenaDeque<PcRelativePatchInfo>* patches);
639
Vladimir Markoaad75c62016-10-03 08:46:48 +0000640 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
641 void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
642 ArenaVector<LinkerPatch>* linker_patches);
643
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200644 // Labels for each block that will be compiled.
645 MipsLabel* block_labels_;
646 MipsLabel frame_entry_label_;
647 LocationsBuilderMIPS location_builder_;
648 InstructionCodeGeneratorMIPS instruction_visitor_;
649 ParallelMoveResolverMIPS move_resolver_;
650 MipsAssembler assembler_;
651 const MipsInstructionSetFeatures& isa_features_;
652
Alexey Frunze06a46c42016-07-19 15:00:40 -0700653 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
654 Uint32ToLiteralMap uint32_literals_;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700655 // PC-relative patch info for each HMipsDexCacheArraysBase.
656 ArenaDeque<PcRelativePatchInfo> pc_relative_dex_cache_patches_;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700657 // Deduplication map for boot string literals for kBootImageLinkTimeAddress.
658 BootStringToLiteralMap boot_image_string_patches_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000659 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
Alexey Frunze06a46c42016-07-19 15:00:40 -0700660 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
661 // Deduplication map for boot type literals for kBootImageLinkTimeAddress.
662 BootTypeToLiteralMap boot_image_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000663 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700664 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000665 // PC-relative type patch info for kBssEntry.
666 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800667 // Patches for string root accesses in JIT compiled code.
668 ArenaDeque<JitPatchInfo> jit_string_patches_;
669 // Patches for class root accesses in JIT compiled code.
670 ArenaDeque<JitPatchInfo> jit_class_patches_;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700671
672 // PC-relative loads on R2 clobber RA, which may need to be preserved explicitly in leaf methods.
673 // This is a flag set by pc_relative_fixups_mips and dex_cache_array_fixups_mips optimizations.
674 bool clobbered_ra_;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700675
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200676 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS);
677};
678
679} // namespace mips
680} // namespace art
681
682#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_