blob: b932fb82bc2d0bed471293c088a9213b4d0074c8 [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
2 * Copyright (C) 2011 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
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
18#define ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
jeffhao7fbee072012-08-24 17:56:54 -070019
Alexey Frunzee3fb2452016-05-10 16:08:05 -070020#include <deque>
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020021#include <utility>
jeffhao7fbee072012-08-24 17:56:54 -070022#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080023
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020024#include "arch/mips/instruction_set_features_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070025#include "base/arena_containers.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070026#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070028#include "constants_mips.h"
29#include "globals.h"
30#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070031#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020032#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070033#include "utils/jni_macro_assembler.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020034#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070035
36namespace art {
37namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070038
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020039static constexpr size_t kMipsWordSize = 4;
40static constexpr size_t kMipsDoublewordSize = 8;
41
jeffhao7fbee072012-08-24 17:56:54 -070042enum LoadOperandType {
43 kLoadSignedByte,
44 kLoadUnsignedByte,
45 kLoadSignedHalfword,
46 kLoadUnsignedHalfword,
47 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020048 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070049};
50
51enum StoreOperandType {
52 kStoreByte,
53 kStoreHalfword,
54 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020055 kStoreDoubleword
56};
57
Chris Larsenb74353a2015-11-20 09:07:09 -080058// Used to test the values returned by ClassS/ClassD.
59enum FPClassMaskType {
60 kSignalingNaN = 0x001,
61 kQuietNaN = 0x002,
62 kNegativeInfinity = 0x004,
63 kNegativeNormal = 0x008,
64 kNegativeSubnormal = 0x010,
65 kNegativeZero = 0x020,
66 kPositiveInfinity = 0x040,
67 kPositiveNormal = 0x080,
68 kPositiveSubnormal = 0x100,
69 kPositiveZero = 0x200,
70};
71
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020072class MipsLabel : public Label {
73 public:
74 MipsLabel() : prev_branch_id_plus_one_(0) {}
75
76 MipsLabel(MipsLabel&& src)
77 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
78
79 private:
80 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
81
82 friend class MipsAssembler;
83 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
84};
85
Alexey Frunzee3fb2452016-05-10 16:08:05 -070086// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
87class Literal {
88 public:
89 static constexpr size_t kMaxSize = 8;
90
91 Literal(uint32_t size, const uint8_t* data)
92 : label_(), size_(size) {
93 DCHECK_LE(size, Literal::kMaxSize);
94 memcpy(data_, data, size);
95 }
96
97 template <typename T>
98 T GetValue() const {
99 DCHECK_EQ(size_, sizeof(T));
100 T value;
101 memcpy(&value, data_, sizeof(T));
102 return value;
103 }
104
105 uint32_t GetSize() const {
106 return size_;
107 }
108
109 const uint8_t* GetData() const {
110 return data_;
111 }
112
113 MipsLabel* GetLabel() {
114 return &label_;
115 }
116
117 const MipsLabel* GetLabel() const {
118 return &label_;
119 }
120
121 private:
122 MipsLabel label_;
123 const uint32_t size_;
124 uint8_t data_[kMaxSize];
125
126 DISALLOW_COPY_AND_ASSIGN(Literal);
127};
128
Alexey Frunze96b66822016-09-10 02:32:44 -0700129// Jump table: table of labels emitted after the literals. Similar to literals.
130class JumpTable {
131 public:
132 explicit JumpTable(std::vector<MipsLabel*>&& labels)
133 : label_(), labels_(std::move(labels)) {
134 }
135
136 uint32_t GetSize() const {
137 return static_cast<uint32_t>(labels_.size()) * sizeof(uint32_t);
138 }
139
140 const std::vector<MipsLabel*>& GetData() const {
141 return labels_;
142 }
143
144 MipsLabel* GetLabel() {
145 return &label_;
146 }
147
148 const MipsLabel* GetLabel() const {
149 return &label_;
150 }
151
152 private:
153 MipsLabel label_;
154 std::vector<MipsLabel*> labels_;
155
156 DISALLOW_COPY_AND_ASSIGN(JumpTable);
157};
158
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200159// Slowpath entered when Thread::Current()->_exception is non-null.
160class MipsExceptionSlowPath {
161 public:
162 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
163 : scratch_(scratch), stack_adjust_(stack_adjust) {}
164
165 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800166 : scratch_(src.scratch_),
167 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200168 exception_entry_(std::move(src.exception_entry_)) {}
169
170 private:
171 MipsLabel* Entry() { return &exception_entry_; }
172 const MipsManagedRegister scratch_;
173 const size_t stack_adjust_;
174 MipsLabel exception_entry_;
175
176 friend class MipsAssembler;
177 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700178};
179
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700180class MipsAssembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k32> {
jeffhao7fbee072012-08-24 17:56:54 -0700181 public:
Igor Murashkinae7ff922016-10-06 14:59:19 -0700182 using JNIBase = JNIMacroAssembler<PointerSize::k32>;
183
Vladimir Marko93205e32016-04-13 11:59:46 +0100184 explicit MipsAssembler(ArenaAllocator* arena,
185 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
186 : Assembler(arena),
187 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200188 overwrite_location_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700189 reordering_(true),
190 ds_fsm_state_(kExpectingLabel),
191 ds_fsm_target_pc_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700192 literals_(arena->Adapter(kArenaAllocAssembler)),
Alexey Frunze96b66822016-09-10 02:32:44 -0700193 jump_tables_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200194 last_position_adjustment_(0),
195 last_old_position_(0),
196 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100197 isa_features_(instruction_set_features) {
198 cfi().DelayEmittingAdvancePCs();
199 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200200
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700201 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700202 size_t CodePosition() OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700203 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
204
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200205 virtual ~MipsAssembler() {
206 for (auto& branch : branches_) {
207 CHECK(branch.IsResolved());
208 }
209 }
jeffhao7fbee072012-08-24 17:56:54 -0700210
211 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700212 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700213 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700214 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200215
216 void MultR2(Register rs, Register rt); // R2
217 void MultuR2(Register rs, Register rt); // R2
218 void DivR2(Register rs, Register rt); // R2
219 void DivuR2(Register rs, Register rt); // R2
220 void MulR2(Register rd, Register rs, Register rt); // R2
221 void DivR2(Register rd, Register rs, Register rt); // R2
222 void ModR2(Register rd, Register rs, Register rt); // R2
223 void DivuR2(Register rd, Register rs, Register rt); // R2
224 void ModuR2(Register rd, Register rs, Register rt); // R2
225 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800226 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200227 void MuhuR6(Register rd, Register rs, Register rt); // R6
228 void DivR6(Register rd, Register rs, Register rt); // R6
229 void ModR6(Register rd, Register rs, Register rt); // R6
230 void DivuR6(Register rd, Register rs, Register rt); // R6
231 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700232
233 void And(Register rd, Register rs, Register rt);
234 void Andi(Register rt, Register rs, uint16_t imm16);
235 void Or(Register rd, Register rs, Register rt);
236 void Ori(Register rt, Register rs, uint16_t imm16);
237 void Xor(Register rd, Register rs, Register rt);
238 void Xori(Register rt, Register rs, uint16_t imm16);
239 void Nor(Register rd, Register rs, Register rt);
240
Chris Larsene3845472015-11-18 12:27:15 -0800241 void Movz(Register rd, Register rs, Register rt); // R2
242 void Movn(Register rd, Register rs, Register rt); // R2
243 void Seleqz(Register rd, Register rs, Register rt); // R6
244 void Selnez(Register rd, Register rs, Register rt); // R6
245 void ClzR6(Register rd, Register rs);
246 void ClzR2(Register rd, Register rs);
247 void CloR6(Register rd, Register rs);
248 void CloR2(Register rd, Register rs);
249
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200250 void Seb(Register rd, Register rt); // R2+
251 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700252 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800253 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200254
255 void Sll(Register rd, Register rt, int shamt);
256 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700257 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200258 void Sra(Register rd, Register rt, int shamt);
259 void Sllv(Register rd, Register rt, Register rs);
260 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800261 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200262 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800263 void Ext(Register rd, Register rt, int pos, int size); // R2+
264 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700265
266 void Lb(Register rt, Register rs, uint16_t imm16);
267 void Lh(Register rt, Register rs, uint16_t imm16);
268 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800269 void Lwl(Register rt, Register rs, uint16_t imm16);
270 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700271 void Lbu(Register rt, Register rs, uint16_t imm16);
272 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700273 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700274 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700275 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200276 void Sync(uint32_t stype);
277 void Mfhi(Register rd); // R2
278 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700279
280 void Sb(Register rt, Register rs, uint16_t imm16);
281 void Sh(Register rt, Register rs, uint16_t imm16);
282 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800283 void Swl(Register rt, Register rs, uint16_t imm16);
284 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700285
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700286 void LlR2(Register rt, Register base, int16_t imm16 = 0);
287 void ScR2(Register rt, Register base, int16_t imm16 = 0);
288 void LlR6(Register rt, Register base, int16_t imm9 = 0);
289 void ScR6(Register rt, Register base, int16_t imm9 = 0);
290
jeffhao7fbee072012-08-24 17:56:54 -0700291 void Slt(Register rd, Register rs, Register rt);
292 void Sltu(Register rd, Register rs, Register rt);
293 void Slti(Register rt, Register rs, uint16_t imm16);
294 void Sltiu(Register rt, Register rs, uint16_t imm16);
295
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700296 // Branches and jumps to immediate offsets/addresses do not take care of their
297 // delay/forbidden slots and generally should not be used directly. This applies
298 // to the following R2 and R6 branch/jump instructions with imm16, imm21, addr26
299 // offsets/addresses.
300 // Use branches/jumps to labels instead.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200301 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700302 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200303 void Beq(Register rs, Register rt, uint16_t imm16);
304 void Bne(Register rs, Register rt, uint16_t imm16);
305 void Beqz(Register rt, uint16_t imm16);
306 void Bnez(Register rt, uint16_t imm16);
307 void Bltz(Register rt, uint16_t imm16);
308 void Bgez(Register rt, uint16_t imm16);
309 void Blez(Register rt, uint16_t imm16);
310 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800311 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800312 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800313 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800314 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200315 void J(uint32_t addr26);
316 void Jal(uint32_t addr26);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700317 // Jalr() and Jr() fill their delay slots when reordering is enabled.
318 // When reordering is disabled, the delay slots must be filled manually.
319 // You may use NopIfNoReordering() to fill them when reordering is disabled.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200320 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700321 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200322 void Jr(Register rs);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700323 // Nal() does not fill its delay slot. It must be filled manually.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200324 void Nal();
325 void Auipc(Register rs, uint16_t imm16); // R6
326 void Addiupc(Register rs, uint32_t imm19); // R6
327 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700328 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200329 void Jic(Register rt, uint16_t imm16); // R6
330 void Jialc(Register rt, uint16_t imm16); // R6
331 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
332 void Bltzc(Register rt, uint16_t imm16); // R6
333 void Bgtzc(Register rt, uint16_t imm16); // R6
334 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
335 void Bgezc(Register rt, uint16_t imm16); // R6
336 void Blezc(Register rt, uint16_t imm16); // R6
337 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
338 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
339 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
340 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
341 void Beqzc(Register rs, uint32_t imm21); // R6
342 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800343 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
344 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700345
346 void AddS(FRegister fd, FRegister fs, FRegister ft);
347 void SubS(FRegister fd, FRegister fs, FRegister ft);
348 void MulS(FRegister fd, FRegister fs, FRegister ft);
349 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200350 void AddD(FRegister fd, FRegister fs, FRegister ft);
351 void SubD(FRegister fd, FRegister fs, FRegister ft);
352 void MulD(FRegister fd, FRegister fs, FRegister ft);
353 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800354 void SqrtS(FRegister fd, FRegister fs);
355 void SqrtD(FRegister fd, FRegister fs);
356 void AbsS(FRegister fd, FRegister fs);
357 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700358 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200359 void MovD(FRegister fd, FRegister fs);
360 void NegS(FRegister fd, FRegister fs);
361 void NegD(FRegister fd, FRegister fs);
362
Chris Larsenb74353a2015-11-20 09:07:09 -0800363 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800364 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800365 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800366 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800367 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800368 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800369 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800370 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800371 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800372 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800373 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800374 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800375 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800376 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800377 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800378 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800379 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800380 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800381 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800382 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800383 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800384 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800385 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800386 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800387 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800388 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800389 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800390 void CuleD(int cc, FRegister fs, FRegister ft); // R2
391 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
392 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
393 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
394 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
395 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
396 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
397 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
398 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
399 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
400 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
401 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
402 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
403 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
404 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
405 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
406 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
407 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
408 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
409 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
410 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800411 void Movf(Register rd, Register rs, int cc = 0); // R2
412 void Movt(Register rd, Register rs, int cc = 0); // R2
413 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
414 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
415 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
416 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
417 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
418 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
419 void ClassS(FRegister fd, FRegister fs); // R6
420 void ClassD(FRegister fd, FRegister fs); // R6
421 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
422 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
423 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
424 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800425
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800426 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
427 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
428 void TruncWS(FRegister fd, FRegister fs);
429 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200430 void Cvtsw(FRegister fd, FRegister fs);
431 void Cvtdw(FRegister fd, FRegister fs);
432 void Cvtsd(FRegister fd, FRegister fs);
433 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800434 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
435 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800436 void FloorWS(FRegister fd, FRegister fs);
437 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700438
439 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200440 void Mtc1(Register rt, FRegister fs);
441 void Mfhc1(Register rt, FRegister fs);
442 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800443 void MoveFromFpuHigh(Register rt, FRegister fs);
444 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700445 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200446 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700447 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200448 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700449
450 void Break();
jeffhao07030602012-09-26 14:33:14 -0700451 void Nop();
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700452 void NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200453 void Move(Register rd, Register rs);
454 void Clear(Register rd);
455 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700456
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200457 // Higher level composite instructions.
458 void LoadConst32(Register rd, int32_t value);
459 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
460 void LoadDConst64(FRegister rd, int64_t value, Register temp);
461 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200462 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
463
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700464 // These will generate R2 branches or R6 branches as appropriate and take care of
465 // the delay/forbidden slots.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200466 void Bind(MipsLabel* label);
467 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700468 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200469 void Beq(Register rs, Register rt, MipsLabel* label);
470 void Bne(Register rs, Register rt, MipsLabel* label);
471 void Beqz(Register rt, MipsLabel* label);
472 void Bnez(Register rt, MipsLabel* label);
473 void Bltz(Register rt, MipsLabel* label);
474 void Bgez(Register rt, MipsLabel* label);
475 void Blez(Register rt, MipsLabel* label);
476 void Bgtz(Register rt, MipsLabel* label);
477 void Blt(Register rs, Register rt, MipsLabel* label);
478 void Bge(Register rs, Register rt, MipsLabel* label);
479 void Bltu(Register rs, Register rt, MipsLabel* label);
480 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800481 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800482 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800483 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800484 void Bc1t(int cc, MipsLabel* label); // R2
485 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
486 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700487
488 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700489 void AdjustBaseAndOffset(Register& base,
490 int32_t& offset,
491 bool is_doubleword,
492 bool is_float = false);
Alexey Frunze2923db72016-08-20 01:55:47 -0700493
494 private:
495 struct NoImplicitNullChecker {
496 void operator()() {}
497 };
498
499 public:
500 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunzef58b2482016-09-02 22:14:06 -0700501 void StoreConstToOffset(StoreOperandType type,
502 int64_t value,
503 Register base,
504 int32_t offset,
505 Register temp,
506 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
507 // We permit `base` and `temp` to coincide (however, we check that neither is AT),
508 // in which case the `base` register may be overwritten in the process.
Alexey Frunze2923db72016-08-20 01:55:47 -0700509 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
Alexey Frunzef58b2482016-09-02 22:14:06 -0700510 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
Alexey Frunze2923db72016-08-20 01:55:47 -0700511 uint32_t low = Low32Bits(value);
512 uint32_t high = High32Bits(value);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700513 Register reg;
514 // If the adjustment left `base` unchanged and equal to `temp`, we can't use `temp`
515 // to load and hold the value but we can use AT instead as AT hasn't been used yet.
516 // Otherwise, `temp` can be used for the value. And if `temp` is the same as the
517 // original `base` (that is, `base` prior to the adjustment), the original `base`
518 // register will be overwritten.
519 if (base == temp) {
520 temp = AT;
Alexey Frunze2923db72016-08-20 01:55:47 -0700521 }
Alexey Frunzef58b2482016-09-02 22:14:06 -0700522 if (low == 0) {
523 reg = ZERO;
Alexey Frunze2923db72016-08-20 01:55:47 -0700524 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -0700525 reg = temp;
526 LoadConst32(reg, low);
527 }
528 switch (type) {
529 case kStoreByte:
530 Sb(reg, base, offset);
531 break;
532 case kStoreHalfword:
533 Sh(reg, base, offset);
534 break;
535 case kStoreWord:
536 Sw(reg, base, offset);
537 break;
538 case kStoreDoubleword:
539 Sw(reg, base, offset);
540 null_checker();
541 if (high == 0) {
542 reg = ZERO;
543 } else {
544 reg = temp;
545 if (high != low) {
546 LoadConst32(reg, high);
547 }
548 }
549 Sw(reg, base, offset + kMipsWordSize);
550 break;
551 default:
552 LOG(FATAL) << "UNREACHABLE";
553 }
554 if (type != kStoreDoubleword) {
555 null_checker();
Alexey Frunze2923db72016-08-20 01:55:47 -0700556 }
557 }
558
559 template <typename ImplicitNullChecker = NoImplicitNullChecker>
560 void LoadFromOffset(LoadOperandType type,
561 Register reg,
562 Register base,
563 int32_t offset,
564 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
565 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
566 switch (type) {
567 case kLoadSignedByte:
568 Lb(reg, base, offset);
569 break;
570 case kLoadUnsignedByte:
571 Lbu(reg, base, offset);
572 break;
573 case kLoadSignedHalfword:
574 Lh(reg, base, offset);
575 break;
576 case kLoadUnsignedHalfword:
577 Lhu(reg, base, offset);
578 break;
579 case kLoadWord:
580 Lw(reg, base, offset);
581 break;
582 case kLoadDoubleword:
583 if (reg == base) {
584 // This will clobber the base when loading the lower register. Since we have to load the
585 // higher register as well, this will fail. Solution: reverse the order.
586 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
587 null_checker();
588 Lw(reg, base, offset);
589 } else {
590 Lw(reg, base, offset);
591 null_checker();
592 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
593 }
594 break;
595 default:
596 LOG(FATAL) << "UNREACHABLE";
597 }
598 if (type != kLoadDoubleword) {
599 null_checker();
600 }
601 }
602
603 template <typename ImplicitNullChecker = NoImplicitNullChecker>
604 void LoadSFromOffset(FRegister reg,
605 Register base,
606 int32_t offset,
607 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
608 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
609 Lwc1(reg, base, offset);
610 null_checker();
611 }
612
613 template <typename ImplicitNullChecker = NoImplicitNullChecker>
614 void LoadDFromOffset(FRegister reg,
615 Register base,
616 int32_t offset,
617 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
618 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
619 if (IsAligned<kMipsDoublewordSize>(offset)) {
620 Ldc1(reg, base, offset);
621 null_checker();
622 } else {
623 if (Is32BitFPU()) {
624 Lwc1(reg, base, offset);
625 null_checker();
626 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
627 } else {
628 // 64-bit FPU.
629 Lwc1(reg, base, offset);
630 null_checker();
631 Lw(T8, base, offset + kMipsWordSize);
632 Mthc1(T8, reg);
633 }
634 }
635 }
636
637 template <typename ImplicitNullChecker = NoImplicitNullChecker>
638 void StoreToOffset(StoreOperandType type,
639 Register reg,
640 Register base,
641 int32_t offset,
642 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
643 // Must not use AT as `reg`, so as not to overwrite the value being stored
644 // with the adjusted `base`.
645 CHECK_NE(reg, AT);
646 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
647 switch (type) {
648 case kStoreByte:
649 Sb(reg, base, offset);
650 break;
651 case kStoreHalfword:
652 Sh(reg, base, offset);
653 break;
654 case kStoreWord:
655 Sw(reg, base, offset);
656 break;
657 case kStoreDoubleword:
658 CHECK_NE(reg, base);
659 CHECK_NE(static_cast<Register>(reg + 1), base);
660 Sw(reg, base, offset);
661 null_checker();
662 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
663 break;
664 default:
665 LOG(FATAL) << "UNREACHABLE";
666 }
667 if (type != kStoreDoubleword) {
668 null_checker();
669 }
670 }
671
672 template <typename ImplicitNullChecker = NoImplicitNullChecker>
673 void StoreSToOffset(FRegister reg,
674 Register base,
675 int32_t offset,
676 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
677 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
678 Swc1(reg, base, offset);
679 null_checker();
680 }
681
682 template <typename ImplicitNullChecker = NoImplicitNullChecker>
683 void StoreDToOffset(FRegister reg,
684 Register base,
685 int32_t offset,
686 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
687 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
688 if (IsAligned<kMipsDoublewordSize>(offset)) {
689 Sdc1(reg, base, offset);
690 null_checker();
691 } else {
692 if (Is32BitFPU()) {
693 Swc1(reg, base, offset);
694 null_checker();
695 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
696 } else {
697 // 64-bit FPU.
698 Mfhc1(T8, reg);
699 Swc1(reg, base, offset);
700 null_checker();
701 Sw(T8, base, offset + kMipsWordSize);
702 }
703 }
704 }
705
jeffhao7fbee072012-08-24 17:56:54 -0700706 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
707 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200708 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700709 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000710 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200711 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700712
jeffhao7fbee072012-08-24 17:56:54 -0700713 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200714 void Emit(uint32_t value);
715
716 // Push/pop composite routines.
717 void Push(Register rs);
718 void Pop(Register rd);
719 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700720
Andreas Gampe85b62f22015-09-09 13:15:38 -0700721 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200722 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700723 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200724 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
725 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700726 }
727
Igor Murashkinae7ff922016-10-06 14:59:19 -0700728 // Don't warn about a different virtual Bind/Jump in the base class.
729 using JNIBase::Bind;
730 using JNIBase::Jump;
731
732 // Create a new label that can be used with Jump/Bind calls.
733 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
734 LOG(FATAL) << "Not implemented on MIPS32";
735 UNREACHABLE();
736 }
737 // Emit an unconditional jump to the label.
738 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
739 LOG(FATAL) << "Not implemented on MIPS32";
740 UNREACHABLE();
741 }
742 // Emit a conditional jump to the label by applying a unary condition test to the register.
743 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
744 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
745 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
746 LOG(FATAL) << "Not implemented on MIPS32";
747 UNREACHABLE();
748 }
749
750 // Code at this offset will serve as the target for the Jump call.
751 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
752 LOG(FATAL) << "Not implemented on MIPS32";
753 UNREACHABLE();
754 }
755
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700756 // Create a new literal with a given value.
757 // NOTE: Force the template parameter to be explicitly specified.
758 template <typename T>
759 Literal* NewLiteral(typename Identity<T>::type value) {
760 static_assert(std::is_integral<T>::value, "T must be an integral type.");
761 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
762 }
763
Alexey Frunze96b66822016-09-10 02:32:44 -0700764 // Load label address using the base register (for R2 only) or using PC-relative loads
765 // (for R6 only; base_reg must be ZERO). To be used with data labels in the literal /
766 // jump table area only and not with regular code labels.
767 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
768
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700769 // Create a new literal with the given data.
770 Literal* NewLiteral(size_t size, const uint8_t* data);
771
772 // Load literal using the base register (for R2 only) or using PC-relative loads
773 // (for R6 only; base_reg must be ZERO).
774 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
775
Alexey Frunze96b66822016-09-10 02:32:44 -0700776 // Create a jump table for the given labels that will be emitted when finalizing.
777 // When the table is emitted, offsets will be relative to the location of the table.
778 // The table location is determined by the location of its label (the label precedes
779 // the table data) and should be loaded using LoadLabelAddress().
780 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
781
jeffhao7fbee072012-08-24 17:56:54 -0700782 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200783 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700784 //
785
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200786 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800787 void BuildFrame(size_t frame_size,
788 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100789 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700790 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700791
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200792 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100793 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700794 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700795
Ian Rogersdd7624d2014-03-14 17:43:00 -0700796 void IncreaseFrameSize(size_t adjust) OVERRIDE;
797 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700798
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200799 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700800 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
801 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
802 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700803
Ian Rogersdd7624d2014-03-14 17:43:00 -0700804 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700805
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700806 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
807 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800808 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700809
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700810 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700811
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800812 void StoreSpanning(FrameOffset dest,
813 ManagedRegister msrc,
814 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700815 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700816
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200817 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700818 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700819
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700820 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700821
Mathieu Chartiere401d142015-04-22 13:56:20 -0700822 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700823
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800824 void LoadRef(ManagedRegister mdest,
825 ManagedRegister base,
826 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100827 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700828
Ian Rogersdd7624d2014-03-14 17:43:00 -0700829 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700830
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700831 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700832
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200833 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700834 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700835
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700836 void CopyRawPtrFromThread(FrameOffset fr_offs,
837 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700838 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700839
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700840 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
841 FrameOffset fr_offs,
842 ManagedRegister mscratch) OVERRIDE;
843
Ian Rogersdd7624d2014-03-14 17:43:00 -0700844 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700845
Ian Rogersdd7624d2014-03-14 17:43:00 -0700846 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700847
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800848 void Copy(FrameOffset dest,
849 ManagedRegister src_base,
850 Offset src_offset,
851 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700852 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700853
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800854 void Copy(ManagedRegister dest_base,
855 Offset dest_offset,
856 FrameOffset src,
857 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700858 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700859
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800860 void Copy(FrameOffset dest,
861 FrameOffset src_base,
862 Offset src_offset,
863 ManagedRegister mscratch,
864 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700865
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800866 void Copy(ManagedRegister dest,
867 Offset dest_offset,
868 ManagedRegister src,
869 Offset src_offset,
870 ManagedRegister mscratch,
871 size_t size) OVERRIDE;
872
873 void Copy(FrameOffset dest,
874 Offset dest_offset,
875 FrameOffset src,
876 Offset src_offset,
877 ManagedRegister mscratch,
878 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700879
Ian Rogersdd7624d2014-03-14 17:43:00 -0700880 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700881
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200882 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700883 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700884
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200885 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700886 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700887
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200888 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700889 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
890 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700891
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700892 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700893 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700894 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700895 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800896 void CreateHandleScopeEntry(ManagedRegister out_reg,
897 FrameOffset handlescope_offset,
898 ManagedRegister in_reg,
899 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700900
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700901 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700902 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800903 void CreateHandleScopeEntry(FrameOffset out_off,
904 FrameOffset handlescope_offset,
905 ManagedRegister mscratch,
906 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700907
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200908 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700909 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700910
911 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
912 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700913 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
914 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700915
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200916 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700917 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
918 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700919 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700920
jeffhao7fbee072012-08-24 17:56:54 -0700921 // Generate code to check if Thread::Current()->exception_ is non-null
922 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700923 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700924
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200925 // Emit slow paths queued during assembly and promote short branches to long if needed.
926 void FinalizeCode() OVERRIDE;
927
928 // Emit branches and finalize all instructions.
929 void FinalizeInstructions(const MemoryRegion& region);
930
931 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
932 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700933 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200934
935 // Get the final position of a label after local fixup based on the old position
936 // recorded before FinalizeCode().
937 uint32_t GetAdjustedPosition(uint32_t old_position);
938
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700939 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
940 // reading the PC value into a general-purpose register with the NAL instruction and then loading
941 // literals through this base register. The code generator calls this method (at most once per
942 // method being compiled) to bind a label to the location for which the PC value is acquired.
943 // The assembler then computes literal offsets relative to this label.
944 void BindPcRelBaseLabel();
945
Alexey Frunze06a46c42016-07-19 15:00:40 -0700946 // Returns the location of the label bound with BindPcRelBaseLabel().
947 uint32_t GetPcRelBaseLabelLocation() const;
948
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700949 // Note that PC-relative literal loads are handled as pseudo branches because they need very
950 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
951 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200952 enum BranchCondition {
953 kCondLT,
954 kCondGE,
955 kCondLE,
956 kCondGT,
957 kCondLTZ,
958 kCondGEZ,
959 kCondLEZ,
960 kCondGTZ,
961 kCondEQ,
962 kCondNE,
963 kCondEQZ,
964 kCondNEZ,
965 kCondLTU,
966 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800967 kCondF, // Floating-point predicate false.
968 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200969 kUncond,
970 };
971 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
972
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700973 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
974 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
975 // Returns the last state, which may be useful for temporary enabling/disabling of
976 // reordering.
977 bool SetReorder(bool enable);
978
jeffhao7fbee072012-08-24 17:56:54 -0700979 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700980 // Description of the last instruction in terms of input and output registers.
981 // Used to make the decision of moving the instruction into a delay slot.
982 struct DelaySlot {
983 DelaySlot();
984 // Encoded instruction that may be used to fill the delay slot or 0
985 // (0 conveniently represents NOP).
986 uint32_t instruction_;
987 // Mask of output GPRs for the instruction.
988 uint32_t gpr_outs_mask_;
989 // Mask of input GPRs for the instruction.
990 uint32_t gpr_ins_mask_;
991 // Mask of output FPRs for the instruction.
992 uint32_t fpr_outs_mask_;
993 // Mask of input FPRs for the instruction.
994 uint32_t fpr_ins_mask_;
995 // Mask of output FPU condition code flags for the instruction.
996 uint32_t cc_outs_mask_;
997 // Mask of input FPU condition code flags for the instruction.
998 uint32_t cc_ins_mask_;
999 // Branches never operate on the LO and HI registers, hence there's
1000 // no mask for LO and HI.
1001 };
1002
1003 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
1004 // upon every new instruction and label generated. The FSM detects instructions
1005 // suitable for delay slots and immediately preceded with labels. These are target
1006 // instructions for branches. If an unconditional R2 branch does not get its delay
1007 // slot filled with the immediately preceding instruction, it may instead get the
1008 // slot filled with the target instruction (the branch will need its offset
1009 // incremented past the target instruction). We call this "absorption". The FSM
1010 // records PCs of the target instructions suitable for this optimization.
1011 enum DsFsmState {
1012 kExpectingLabel,
1013 kExpectingInstruction,
1014 kExpectingCommit
1015 };
1016 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
1017
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001018 class Branch {
1019 public:
1020 enum Type {
1021 // R2 short branches.
1022 kUncondBranch,
1023 kCondBranch,
1024 kCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001025 // R2 near label.
1026 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001027 // R2 near literal.
1028 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001029 // R2 long branches.
1030 kLongUncondBranch,
1031 kLongCondBranch,
1032 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001033 // R2 far label.
1034 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001035 // R2 far literal.
1036 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001037 // R6 short branches.
1038 kR6UncondBranch,
1039 kR6CondBranch,
1040 kR6Call,
Alexey Frunze96b66822016-09-10 02:32:44 -07001041 // R6 near label.
1042 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001043 // R6 near literal.
1044 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001045 // R6 long branches.
1046 kR6LongUncondBranch,
1047 kR6LongCondBranch,
1048 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001049 // R6 far label.
1050 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001051 // R6 far literal.
1052 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001053 };
1054 // Bit sizes of offsets defined as enums to minimize chance of typos.
1055 enum OffsetBits {
1056 kOffset16 = 16,
1057 kOffset18 = 18,
1058 kOffset21 = 21,
1059 kOffset23 = 23,
1060 kOffset28 = 28,
1061 kOffset32 = 32,
1062 };
1063
1064 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1065 static constexpr int32_t kMaxBranchLength = 32;
1066 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001067 // The following two instruction encodings can never legally occur in branch delay
1068 // slots and are used as markers.
1069 //
1070 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1071 // instruction to fill its delay slot (the latter is only possible with unconditional
1072 // R2 branches and is termed here as "absorption").
1073 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1074 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1075 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1076 // absorption of the target instruction when reordering is disabled.
1077 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001078
1079 struct BranchInfo {
1080 // Branch length as a number of 4-byte-long instructions.
1081 uint32_t length;
1082 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1083 // PC-relative offset (or its most significant 16-bit half, which goes first).
1084 uint32_t instr_offset;
1085 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1086 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1087 // instructions) from the instruction containing the offset.
1088 uint32_t pc_org;
1089 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
1090 // is an exception: use kOffset23 for beqzc/bnezc).
1091 OffsetBits offset_size;
1092 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1093 // count.
1094 int offset_shift;
1095 };
1096 static const BranchInfo branch_info_[/* Type */];
1097
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001098 // Unconditional branch or call.
1099 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001100 // Conditional branch.
1101 Branch(bool is_r6,
1102 uint32_t location,
1103 uint32_t target,
1104 BranchCondition condition,
1105 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001106 Register rhs_reg);
Alexey Frunze96b66822016-09-10 02:32:44 -07001107 // Label address (in literal area) or literal.
1108 Branch(bool is_r6,
1109 uint32_t location,
1110 Register dest_reg,
1111 Register base_reg,
1112 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001113
1114 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1115 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1116 // So, we need a way to identify such branches in order to emit no instructions for them
1117 // or change them to unconditional.
1118 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1119 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1120
1121 static BranchCondition OppositeCondition(BranchCondition cond);
1122
1123 Type GetType() const;
1124 BranchCondition GetCondition() const;
1125 Register GetLeftRegister() const;
1126 Register GetRightRegister() const;
1127 uint32_t GetTarget() const;
1128 uint32_t GetLocation() const;
1129 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001130 uint32_t GetPrecedingInstructionLength(Type type) const;
1131 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001132 uint32_t GetLength() const;
1133 uint32_t GetOldLength() const;
1134 uint32_t GetSize() const;
1135 uint32_t GetOldSize() const;
1136 uint32_t GetEndLocation() const;
1137 uint32_t GetOldEndLocation() const;
1138 bool IsLong() const;
1139 bool IsResolved() const;
1140
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001141 // Various helpers for branch delay slot management.
1142 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
1143 void SetDelayedInstruction(uint32_t instruction);
1144 uint32_t GetDelayedInstruction() const;
1145 void DecrementLocations();
1146
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001147 // Returns the bit size of the signed offset that the branch instruction can handle.
1148 OffsetBits GetOffsetSize() const;
1149
1150 // Calculates the distance between two byte locations in the assembler buffer and
1151 // returns the number of bits needed to represent the distance as a signed integer.
1152 //
1153 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1154 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1155 //
1156 // Composite branches (made of several instructions) with longer reach have 32-bit
1157 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001158 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1159 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1160 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1161 // Consider the following implementation of a long unconditional branch, for
1162 // example:
1163 //
1164 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1165 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1166 //
1167 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1168 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1169 // due to sign extension. This must be compensated for by incrementing offset_31_16
1170 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1171 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1172 // Therefore, the long branch range is something like from PC - 0x80000000 to
1173 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001174 //
1175 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1176 // case with the addiu instruction and a 16 bit offset.
1177 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1178
1179 // Resolve a branch when the target is known.
1180 void Resolve(uint32_t target);
1181
1182 // Relocate a branch by a given delta if needed due to expansion of this or another
1183 // branch at a given location by this delta (just changes location_ and target_).
1184 void Relocate(uint32_t expand_location, uint32_t delta);
1185
1186 // If the branch is short, changes its type to long.
1187 void PromoteToLong();
1188
1189 // If necessary, updates the type by promoting a short branch to a long branch
1190 // based on the branch location and target. Returns the amount (in bytes) by
1191 // which the branch size has increased.
1192 // max_short_distance caps the maximum distance between location_ and target_
1193 // that is allowed for short branches. This is for debugging/testing purposes.
1194 // max_short_distance = 0 forces all short branches to become long.
1195 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001196 uint32_t PromoteIfNeeded(uint32_t location,
1197 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001198
1199 // Returns the location of the instruction(s) containing the offset.
1200 uint32_t GetOffsetLocation() const;
1201
1202 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001203 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001204
1205 private:
1206 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001207 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001208 // Helper for the above.
1209 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1210
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001211 uint32_t old_location_; // Offset into assembler buffer in bytes.
1212 uint32_t location_; // Offset into assembler buffer in bytes.
1213 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001214
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001215 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1216 // FPU condition code. Destination register in literals.
1217 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1218 // Base register in literals (ZERO on R6).
1219 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001220
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001221 Type type_; // Current type of the branch.
1222 Type old_type_; // Initial type of the branch.
1223
1224 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1225 // kUnfilledDelaySlot if none but fillable or
1226 // kUnfillableDelaySlot if none and unfillable
1227 // (the latter is only used for unconditional R2
1228 // branches).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001229 };
1230 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1231 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1232
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001233 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1234 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1235 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1236 uint32_t EmitI26(int opcode, uint32_t imm26);
1237 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1238 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001239 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1240 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001241
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001242 void Buncond(MipsLabel* label);
1243 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001244 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001245 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001246
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001247 // Various helpers for branch delay slot management.
1248 void DsFsmInstr(uint32_t instruction,
1249 uint32_t gpr_outs_mask,
1250 uint32_t gpr_ins_mask,
1251 uint32_t fpr_outs_mask,
1252 uint32_t fpr_ins_mask,
1253 uint32_t cc_outs_mask,
1254 uint32_t cc_ins_mask);
1255 void DsFsmInstrNop(uint32_t instruction);
1256 void DsFsmInstrRrr(uint32_t instruction, Register out, Register in1, Register in2);
1257 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1258 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1259 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
1260 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1261 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1262 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1263 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1264 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1265 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1266 void DsFsmLabel();
1267 void DsFsmCommitLabel();
1268 void DsFsmDropLabel();
1269 void MoveInstructionToDelaySlot(Branch& branch);
1270 bool CanExchangeWithSlt(Register rs, Register rt) const;
1271 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1272 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1273
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001274 Branch* GetBranch(uint32_t branch_id);
1275 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001276 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1277 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001278
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001279 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001280 void ReserveJumpTableSpace();
1281 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001282 void PromoteBranches();
1283 void EmitBranch(Branch* branch);
1284 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001285 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001286
1287 // Emits exception block.
1288 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1289
1290 bool IsR6() const {
1291 if (isa_features_ != nullptr) {
1292 return isa_features_->IsR6();
1293 } else {
1294 return false;
1295 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001296 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001297
1298 bool Is32BitFPU() const {
1299 if (isa_features_ != nullptr) {
1300 return isa_features_->Is32BitFloatingPoint();
1301 } else {
1302 return true;
1303 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001304 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001305
1306 // List of exception blocks to generate at the end of the code cache.
1307 std::vector<MipsExceptionSlowPath> exception_blocks_;
1308
1309 std::vector<Branch> branches_;
1310
1311 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1312 bool overwriting_;
1313 // The current overwrite location.
1314 uint32_t overwrite_location_;
1315
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001316 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1317 bool reordering_;
1318 // Information about the last instruction that may be used to fill a branch delay slot.
1319 DelaySlot delay_slot_;
1320 // Delay slot FSM state.
1321 DsFsmState ds_fsm_state_;
1322 // PC of the current labeled target instruction.
1323 uint32_t ds_fsm_target_pc_;
1324 // PCs of labeled target instructions.
1325 std::vector<uint32_t> ds_fsm_target_pcs_;
1326
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001327 // Use std::deque<> for literal labels to allow insertions at the end
1328 // without invalidating pointers and references to existing elements.
1329 ArenaDeque<Literal> literals_;
1330
Alexey Frunze96b66822016-09-10 02:32:44 -07001331 // Jump table list.
1332 ArenaDeque<JumpTable> jump_tables_;
1333
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001334 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1335 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1336 // that PC (from NAL) points to.
1337 MipsLabel pc_rel_base_label_;
1338
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001339 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001340 uint32_t last_position_adjustment_;
1341 uint32_t last_old_position_;
1342 uint32_t last_branch_id_;
1343
1344 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001345
jeffhao7fbee072012-08-24 17:56:54 -07001346 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1347};
1348
jeffhao7fbee072012-08-24 17:56:54 -07001349} // namespace mips
1350} // namespace art
1351
Ian Rogers166db042013-07-26 12:05:57 -07001352#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_