blob: e1255f7f233978710733b2c6a52ee370f33613bb [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:
Vladimir Marko93205e32016-04-13 11:59:46 +0100182 explicit MipsAssembler(ArenaAllocator* arena,
183 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
184 : Assembler(arena),
185 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200186 overwrite_location_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700187 reordering_(true),
188 ds_fsm_state_(kExpectingLabel),
189 ds_fsm_target_pc_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700190 literals_(arena->Adapter(kArenaAllocAssembler)),
Alexey Frunze96b66822016-09-10 02:32:44 -0700191 jump_tables_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200192 last_position_adjustment_(0),
193 last_old_position_(0),
194 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100195 isa_features_(instruction_set_features) {
196 cfi().DelayEmittingAdvancePCs();
197 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200198
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700199 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700200 size_t CodePosition() OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700201 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
202
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200203 virtual ~MipsAssembler() {
204 for (auto& branch : branches_) {
205 CHECK(branch.IsResolved());
206 }
207 }
jeffhao7fbee072012-08-24 17:56:54 -0700208
209 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700210 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700211 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700212 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200213
214 void MultR2(Register rs, Register rt); // R2
215 void MultuR2(Register rs, Register rt); // R2
216 void DivR2(Register rs, Register rt); // R2
217 void DivuR2(Register rs, Register rt); // R2
218 void MulR2(Register rd, Register rs, Register rt); // R2
219 void DivR2(Register rd, Register rs, Register rt); // R2
220 void ModR2(Register rd, Register rs, Register rt); // R2
221 void DivuR2(Register rd, Register rs, Register rt); // R2
222 void ModuR2(Register rd, Register rs, Register rt); // R2
223 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800224 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200225 void MuhuR6(Register rd, Register rs, Register rt); // R6
226 void DivR6(Register rd, Register rs, Register rt); // R6
227 void ModR6(Register rd, Register rs, Register rt); // R6
228 void DivuR6(Register rd, Register rs, Register rt); // R6
229 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700230
231 void And(Register rd, Register rs, Register rt);
232 void Andi(Register rt, Register rs, uint16_t imm16);
233 void Or(Register rd, Register rs, Register rt);
234 void Ori(Register rt, Register rs, uint16_t imm16);
235 void Xor(Register rd, Register rs, Register rt);
236 void Xori(Register rt, Register rs, uint16_t imm16);
237 void Nor(Register rd, Register rs, Register rt);
238
Chris Larsene3845472015-11-18 12:27:15 -0800239 void Movz(Register rd, Register rs, Register rt); // R2
240 void Movn(Register rd, Register rs, Register rt); // R2
241 void Seleqz(Register rd, Register rs, Register rt); // R6
242 void Selnez(Register rd, Register rs, Register rt); // R6
243 void ClzR6(Register rd, Register rs);
244 void ClzR2(Register rd, Register rs);
245 void CloR6(Register rd, Register rs);
246 void CloR2(Register rd, Register rs);
247
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200248 void Seb(Register rd, Register rt); // R2+
249 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700250 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800251 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200252
253 void Sll(Register rd, Register rt, int shamt);
254 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700255 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200256 void Sra(Register rd, Register rt, int shamt);
257 void Sllv(Register rd, Register rt, Register rs);
258 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800259 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200260 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800261 void Ext(Register rd, Register rt, int pos, int size); // R2+
262 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700263
264 void Lb(Register rt, Register rs, uint16_t imm16);
265 void Lh(Register rt, Register rs, uint16_t imm16);
266 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800267 void Lwl(Register rt, Register rs, uint16_t imm16);
268 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700269 void Lbu(Register rt, Register rs, uint16_t imm16);
270 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700271 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700272 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700273 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200274 void Sync(uint32_t stype);
275 void Mfhi(Register rd); // R2
276 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700277
278 void Sb(Register rt, Register rs, uint16_t imm16);
279 void Sh(Register rt, Register rs, uint16_t imm16);
280 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800281 void Swl(Register rt, Register rs, uint16_t imm16);
282 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700283
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700284 void LlR2(Register rt, Register base, int16_t imm16 = 0);
285 void ScR2(Register rt, Register base, int16_t imm16 = 0);
286 void LlR6(Register rt, Register base, int16_t imm9 = 0);
287 void ScR6(Register rt, Register base, int16_t imm9 = 0);
288
jeffhao7fbee072012-08-24 17:56:54 -0700289 void Slt(Register rd, Register rs, Register rt);
290 void Sltu(Register rd, Register rs, Register rt);
291 void Slti(Register rt, Register rs, uint16_t imm16);
292 void Sltiu(Register rt, Register rs, uint16_t imm16);
293
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700294 // Branches and jumps to immediate offsets/addresses do not take care of their
295 // delay/forbidden slots and generally should not be used directly. This applies
296 // to the following R2 and R6 branch/jump instructions with imm16, imm21, addr26
297 // offsets/addresses.
298 // Use branches/jumps to labels instead.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200299 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700300 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200301 void Beq(Register rs, Register rt, uint16_t imm16);
302 void Bne(Register rs, Register rt, uint16_t imm16);
303 void Beqz(Register rt, uint16_t imm16);
304 void Bnez(Register rt, uint16_t imm16);
305 void Bltz(Register rt, uint16_t imm16);
306 void Bgez(Register rt, uint16_t imm16);
307 void Blez(Register rt, uint16_t imm16);
308 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800309 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800310 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800311 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800312 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200313 void J(uint32_t addr26);
314 void Jal(uint32_t addr26);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700315 // Jalr() and Jr() fill their delay slots when reordering is enabled.
316 // When reordering is disabled, the delay slots must be filled manually.
317 // You may use NopIfNoReordering() to fill them when reordering is disabled.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200318 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700319 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200320 void Jr(Register rs);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700321 // Nal() does not fill its delay slot. It must be filled manually.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200322 void Nal();
323 void Auipc(Register rs, uint16_t imm16); // R6
324 void Addiupc(Register rs, uint32_t imm19); // R6
325 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700326 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200327 void Jic(Register rt, uint16_t imm16); // R6
328 void Jialc(Register rt, uint16_t imm16); // R6
329 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
330 void Bltzc(Register rt, uint16_t imm16); // R6
331 void Bgtzc(Register rt, uint16_t imm16); // R6
332 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
333 void Bgezc(Register rt, uint16_t imm16); // R6
334 void Blezc(Register rt, uint16_t imm16); // R6
335 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
336 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
337 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
338 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
339 void Beqzc(Register rs, uint32_t imm21); // R6
340 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800341 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
342 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700343
344 void AddS(FRegister fd, FRegister fs, FRegister ft);
345 void SubS(FRegister fd, FRegister fs, FRegister ft);
346 void MulS(FRegister fd, FRegister fs, FRegister ft);
347 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200348 void AddD(FRegister fd, FRegister fs, FRegister ft);
349 void SubD(FRegister fd, FRegister fs, FRegister ft);
350 void MulD(FRegister fd, FRegister fs, FRegister ft);
351 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800352 void SqrtS(FRegister fd, FRegister fs);
353 void SqrtD(FRegister fd, FRegister fs);
354 void AbsS(FRegister fd, FRegister fs);
355 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700356 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200357 void MovD(FRegister fd, FRegister fs);
358 void NegS(FRegister fd, FRegister fs);
359 void NegD(FRegister fd, FRegister fs);
360
Chris Larsenb74353a2015-11-20 09:07:09 -0800361 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800362 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800363 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800364 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800365 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800366 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800367 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800368 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800369 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800370 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800371 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800372 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800373 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800374 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800375 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800376 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800377 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800378 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800379 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800380 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800381 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800382 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800383 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800384 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800385 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800386 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800387 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800388 void CuleD(int cc, FRegister fs, FRegister ft); // R2
389 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
390 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
391 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
392 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
393 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
394 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
395 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
396 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
397 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
398 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
399 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
400 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
401 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
402 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
403 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
404 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
405 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
406 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
407 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
408 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800409 void Movf(Register rd, Register rs, int cc = 0); // R2
410 void Movt(Register rd, Register rs, int cc = 0); // R2
411 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
412 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
413 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
414 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
415 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
416 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
417 void ClassS(FRegister fd, FRegister fs); // R6
418 void ClassD(FRegister fd, FRegister fs); // R6
419 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
420 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
421 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
422 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800423
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800424 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
425 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
426 void TruncWS(FRegister fd, FRegister fs);
427 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200428 void Cvtsw(FRegister fd, FRegister fs);
429 void Cvtdw(FRegister fd, FRegister fs);
430 void Cvtsd(FRegister fd, FRegister fs);
431 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800432 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
433 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800434 void FloorWS(FRegister fd, FRegister fs);
435 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700436
437 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200438 void Mtc1(Register rt, FRegister fs);
439 void Mfhc1(Register rt, FRegister fs);
440 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800441 void MoveFromFpuHigh(Register rt, FRegister fs);
442 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700443 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200444 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700445 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200446 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700447
448 void Break();
jeffhao07030602012-09-26 14:33:14 -0700449 void Nop();
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700450 void NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200451 void Move(Register rd, Register rs);
452 void Clear(Register rd);
453 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700454
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200455 // Higher level composite instructions.
456 void LoadConst32(Register rd, int32_t value);
457 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
458 void LoadDConst64(FRegister rd, int64_t value, Register temp);
459 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200460 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
461
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700462 // These will generate R2 branches or R6 branches as appropriate and take care of
463 // the delay/forbidden slots.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200464 void Bind(MipsLabel* label);
465 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700466 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200467 void Beq(Register rs, Register rt, MipsLabel* label);
468 void Bne(Register rs, Register rt, MipsLabel* label);
469 void Beqz(Register rt, MipsLabel* label);
470 void Bnez(Register rt, MipsLabel* label);
471 void Bltz(Register rt, MipsLabel* label);
472 void Bgez(Register rt, MipsLabel* label);
473 void Blez(Register rt, MipsLabel* label);
474 void Bgtz(Register rt, MipsLabel* label);
475 void Blt(Register rs, Register rt, MipsLabel* label);
476 void Bge(Register rs, Register rt, MipsLabel* label);
477 void Bltu(Register rs, Register rt, MipsLabel* label);
478 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800479 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800480 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800481 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800482 void Bc1t(int cc, MipsLabel* label); // R2
483 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
484 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700485
486 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700487 void AdjustBaseAndOffset(Register& base,
488 int32_t& offset,
489 bool is_doubleword,
490 bool is_float = false);
Alexey Frunze2923db72016-08-20 01:55:47 -0700491
492 private:
493 struct NoImplicitNullChecker {
494 void operator()() {}
495 };
496
497 public:
498 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunzef58b2482016-09-02 22:14:06 -0700499 void StoreConstToOffset(StoreOperandType type,
500 int64_t value,
501 Register base,
502 int32_t offset,
503 Register temp,
504 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
505 // We permit `base` and `temp` to coincide (however, we check that neither is AT),
506 // in which case the `base` register may be overwritten in the process.
Alexey Frunze2923db72016-08-20 01:55:47 -0700507 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 -0700508 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
Alexey Frunze2923db72016-08-20 01:55:47 -0700509 uint32_t low = Low32Bits(value);
510 uint32_t high = High32Bits(value);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700511 Register reg;
512 // If the adjustment left `base` unchanged and equal to `temp`, we can't use `temp`
513 // to load and hold the value but we can use AT instead as AT hasn't been used yet.
514 // Otherwise, `temp` can be used for the value. And if `temp` is the same as the
515 // original `base` (that is, `base` prior to the adjustment), the original `base`
516 // register will be overwritten.
517 if (base == temp) {
518 temp = AT;
Alexey Frunze2923db72016-08-20 01:55:47 -0700519 }
Alexey Frunzef58b2482016-09-02 22:14:06 -0700520 if (low == 0) {
521 reg = ZERO;
Alexey Frunze2923db72016-08-20 01:55:47 -0700522 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -0700523 reg = temp;
524 LoadConst32(reg, low);
525 }
526 switch (type) {
527 case kStoreByte:
528 Sb(reg, base, offset);
529 break;
530 case kStoreHalfword:
531 Sh(reg, base, offset);
532 break;
533 case kStoreWord:
534 Sw(reg, base, offset);
535 break;
536 case kStoreDoubleword:
537 Sw(reg, base, offset);
538 null_checker();
539 if (high == 0) {
540 reg = ZERO;
541 } else {
542 reg = temp;
543 if (high != low) {
544 LoadConst32(reg, high);
545 }
546 }
547 Sw(reg, base, offset + kMipsWordSize);
548 break;
549 default:
550 LOG(FATAL) << "UNREACHABLE";
551 }
552 if (type != kStoreDoubleword) {
553 null_checker();
Alexey Frunze2923db72016-08-20 01:55:47 -0700554 }
555 }
556
557 template <typename ImplicitNullChecker = NoImplicitNullChecker>
558 void LoadFromOffset(LoadOperandType type,
559 Register reg,
560 Register base,
561 int32_t offset,
562 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
563 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
564 switch (type) {
565 case kLoadSignedByte:
566 Lb(reg, base, offset);
567 break;
568 case kLoadUnsignedByte:
569 Lbu(reg, base, offset);
570 break;
571 case kLoadSignedHalfword:
572 Lh(reg, base, offset);
573 break;
574 case kLoadUnsignedHalfword:
575 Lhu(reg, base, offset);
576 break;
577 case kLoadWord:
578 Lw(reg, base, offset);
579 break;
580 case kLoadDoubleword:
581 if (reg == base) {
582 // This will clobber the base when loading the lower register. Since we have to load the
583 // higher register as well, this will fail. Solution: reverse the order.
584 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
585 null_checker();
586 Lw(reg, base, offset);
587 } else {
588 Lw(reg, base, offset);
589 null_checker();
590 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
591 }
592 break;
593 default:
594 LOG(FATAL) << "UNREACHABLE";
595 }
596 if (type != kLoadDoubleword) {
597 null_checker();
598 }
599 }
600
601 template <typename ImplicitNullChecker = NoImplicitNullChecker>
602 void LoadSFromOffset(FRegister reg,
603 Register base,
604 int32_t offset,
605 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
606 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
607 Lwc1(reg, base, offset);
608 null_checker();
609 }
610
611 template <typename ImplicitNullChecker = NoImplicitNullChecker>
612 void LoadDFromOffset(FRegister reg,
613 Register base,
614 int32_t offset,
615 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
616 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
617 if (IsAligned<kMipsDoublewordSize>(offset)) {
618 Ldc1(reg, base, offset);
619 null_checker();
620 } else {
621 if (Is32BitFPU()) {
622 Lwc1(reg, base, offset);
623 null_checker();
624 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
625 } else {
626 // 64-bit FPU.
627 Lwc1(reg, base, offset);
628 null_checker();
629 Lw(T8, base, offset + kMipsWordSize);
630 Mthc1(T8, reg);
631 }
632 }
633 }
634
635 template <typename ImplicitNullChecker = NoImplicitNullChecker>
636 void StoreToOffset(StoreOperandType type,
637 Register reg,
638 Register base,
639 int32_t offset,
640 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
641 // Must not use AT as `reg`, so as not to overwrite the value being stored
642 // with the adjusted `base`.
643 CHECK_NE(reg, AT);
644 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
645 switch (type) {
646 case kStoreByte:
647 Sb(reg, base, offset);
648 break;
649 case kStoreHalfword:
650 Sh(reg, base, offset);
651 break;
652 case kStoreWord:
653 Sw(reg, base, offset);
654 break;
655 case kStoreDoubleword:
656 CHECK_NE(reg, base);
657 CHECK_NE(static_cast<Register>(reg + 1), base);
658 Sw(reg, base, offset);
659 null_checker();
660 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
661 break;
662 default:
663 LOG(FATAL) << "UNREACHABLE";
664 }
665 if (type != kStoreDoubleword) {
666 null_checker();
667 }
668 }
669
670 template <typename ImplicitNullChecker = NoImplicitNullChecker>
671 void StoreSToOffset(FRegister reg,
672 Register base,
673 int32_t offset,
674 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
675 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
676 Swc1(reg, base, offset);
677 null_checker();
678 }
679
680 template <typename ImplicitNullChecker = NoImplicitNullChecker>
681 void StoreDToOffset(FRegister reg,
682 Register base,
683 int32_t offset,
684 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
685 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
686 if (IsAligned<kMipsDoublewordSize>(offset)) {
687 Sdc1(reg, base, offset);
688 null_checker();
689 } else {
690 if (Is32BitFPU()) {
691 Swc1(reg, base, offset);
692 null_checker();
693 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
694 } else {
695 // 64-bit FPU.
696 Mfhc1(T8, reg);
697 Swc1(reg, base, offset);
698 null_checker();
699 Sw(T8, base, offset + kMipsWordSize);
700 }
701 }
702 }
703
jeffhao7fbee072012-08-24 17:56:54 -0700704 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
705 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200706 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700707 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000708 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200709 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700710
jeffhao7fbee072012-08-24 17:56:54 -0700711 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200712 void Emit(uint32_t value);
713
714 // Push/pop composite routines.
715 void Push(Register rs);
716 void Pop(Register rd);
717 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700718
Andreas Gampe85b62f22015-09-09 13:15:38 -0700719 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200720 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700721 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200722 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
723 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700724 }
725
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700726 // Create a new literal with a given value.
727 // NOTE: Force the template parameter to be explicitly specified.
728 template <typename T>
729 Literal* NewLiteral(typename Identity<T>::type value) {
730 static_assert(std::is_integral<T>::value, "T must be an integral type.");
731 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
732 }
733
Alexey Frunze96b66822016-09-10 02:32:44 -0700734 // Load label address using the base register (for R2 only) or using PC-relative loads
735 // (for R6 only; base_reg must be ZERO). To be used with data labels in the literal /
736 // jump table area only and not with regular code labels.
737 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
738
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700739 // Create a new literal with the given data.
740 Literal* NewLiteral(size_t size, const uint8_t* data);
741
742 // Load literal using the base register (for R2 only) or using PC-relative loads
743 // (for R6 only; base_reg must be ZERO).
744 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
745
Alexey Frunze96b66822016-09-10 02:32:44 -0700746 // Create a jump table for the given labels that will be emitted when finalizing.
747 // When the table is emitted, offsets will be relative to the location of the table.
748 // The table location is determined by the location of its label (the label precedes
749 // the table data) and should be loaded using LoadLabelAddress().
750 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
751
jeffhao7fbee072012-08-24 17:56:54 -0700752 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200753 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700754 //
755
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200756 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800757 void BuildFrame(size_t frame_size,
758 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100759 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700760 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700761
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200762 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100763 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700764 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700765
Ian Rogersdd7624d2014-03-14 17:43:00 -0700766 void IncreaseFrameSize(size_t adjust) OVERRIDE;
767 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700768
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200769 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700770 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
771 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
772 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700773
Ian Rogersdd7624d2014-03-14 17:43:00 -0700774 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700775
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700776 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
777 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800778 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700779
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700780 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700781
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800782 void StoreSpanning(FrameOffset dest,
783 ManagedRegister msrc,
784 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700785 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700786
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200787 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700788 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700789
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700790 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700791
Mathieu Chartiere401d142015-04-22 13:56:20 -0700792 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700793
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800794 void LoadRef(ManagedRegister mdest,
795 ManagedRegister base,
796 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100797 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700798
Ian Rogersdd7624d2014-03-14 17:43:00 -0700799 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700800
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700801 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700802
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200803 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700804 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700805
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700806 void CopyRawPtrFromThread(FrameOffset fr_offs,
807 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700808 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700809
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700810 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
811 FrameOffset fr_offs,
812 ManagedRegister mscratch) OVERRIDE;
813
Ian Rogersdd7624d2014-03-14 17:43:00 -0700814 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700815
Ian Rogersdd7624d2014-03-14 17:43:00 -0700816 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700817
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800818 void Copy(FrameOffset dest,
819 ManagedRegister src_base,
820 Offset src_offset,
821 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700822 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700823
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800824 void Copy(ManagedRegister dest_base,
825 Offset dest_offset,
826 FrameOffset src,
827 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700828 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700829
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800830 void Copy(FrameOffset dest,
831 FrameOffset src_base,
832 Offset src_offset,
833 ManagedRegister mscratch,
834 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700835
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800836 void Copy(ManagedRegister dest,
837 Offset dest_offset,
838 ManagedRegister src,
839 Offset src_offset,
840 ManagedRegister mscratch,
841 size_t size) OVERRIDE;
842
843 void Copy(FrameOffset dest,
844 Offset dest_offset,
845 FrameOffset src,
846 Offset src_offset,
847 ManagedRegister mscratch,
848 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700849
Ian Rogersdd7624d2014-03-14 17:43:00 -0700850 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700851
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200852 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700853 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700854
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200855 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700856 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700857
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200858 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700859 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
860 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700861
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700862 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700863 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700864 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700865 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800866 void CreateHandleScopeEntry(ManagedRegister out_reg,
867 FrameOffset handlescope_offset,
868 ManagedRegister in_reg,
869 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700870
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700871 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700872 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800873 void CreateHandleScopeEntry(FrameOffset out_off,
874 FrameOffset handlescope_offset,
875 ManagedRegister mscratch,
876 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700877
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200878 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700879 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700880
881 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
882 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700883 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
884 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700885
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200886 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700887 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
888 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700889 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700890
jeffhao7fbee072012-08-24 17:56:54 -0700891 // Generate code to check if Thread::Current()->exception_ is non-null
892 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700893 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700894
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200895 // Emit slow paths queued during assembly and promote short branches to long if needed.
896 void FinalizeCode() OVERRIDE;
897
898 // Emit branches and finalize all instructions.
899 void FinalizeInstructions(const MemoryRegion& region);
900
901 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
902 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700903 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200904
905 // Get the final position of a label after local fixup based on the old position
906 // recorded before FinalizeCode().
907 uint32_t GetAdjustedPosition(uint32_t old_position);
908
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700909 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
910 // reading the PC value into a general-purpose register with the NAL instruction and then loading
911 // literals through this base register. The code generator calls this method (at most once per
912 // method being compiled) to bind a label to the location for which the PC value is acquired.
913 // The assembler then computes literal offsets relative to this label.
914 void BindPcRelBaseLabel();
915
Alexey Frunze06a46c42016-07-19 15:00:40 -0700916 // Returns the location of the label bound with BindPcRelBaseLabel().
917 uint32_t GetPcRelBaseLabelLocation() const;
918
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700919 // Note that PC-relative literal loads are handled as pseudo branches because they need very
920 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
921 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200922 enum BranchCondition {
923 kCondLT,
924 kCondGE,
925 kCondLE,
926 kCondGT,
927 kCondLTZ,
928 kCondGEZ,
929 kCondLEZ,
930 kCondGTZ,
931 kCondEQ,
932 kCondNE,
933 kCondEQZ,
934 kCondNEZ,
935 kCondLTU,
936 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800937 kCondF, // Floating-point predicate false.
938 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200939 kUncond,
940 };
941 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
942
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700943 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
944 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
945 // Returns the last state, which may be useful for temporary enabling/disabling of
946 // reordering.
947 bool SetReorder(bool enable);
948
jeffhao7fbee072012-08-24 17:56:54 -0700949 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700950 // Description of the last instruction in terms of input and output registers.
951 // Used to make the decision of moving the instruction into a delay slot.
952 struct DelaySlot {
953 DelaySlot();
954 // Encoded instruction that may be used to fill the delay slot or 0
955 // (0 conveniently represents NOP).
956 uint32_t instruction_;
957 // Mask of output GPRs for the instruction.
958 uint32_t gpr_outs_mask_;
959 // Mask of input GPRs for the instruction.
960 uint32_t gpr_ins_mask_;
961 // Mask of output FPRs for the instruction.
962 uint32_t fpr_outs_mask_;
963 // Mask of input FPRs for the instruction.
964 uint32_t fpr_ins_mask_;
965 // Mask of output FPU condition code flags for the instruction.
966 uint32_t cc_outs_mask_;
967 // Mask of input FPU condition code flags for the instruction.
968 uint32_t cc_ins_mask_;
969 // Branches never operate on the LO and HI registers, hence there's
970 // no mask for LO and HI.
971 };
972
973 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
974 // upon every new instruction and label generated. The FSM detects instructions
975 // suitable for delay slots and immediately preceded with labels. These are target
976 // instructions for branches. If an unconditional R2 branch does not get its delay
977 // slot filled with the immediately preceding instruction, it may instead get the
978 // slot filled with the target instruction (the branch will need its offset
979 // incremented past the target instruction). We call this "absorption". The FSM
980 // records PCs of the target instructions suitable for this optimization.
981 enum DsFsmState {
982 kExpectingLabel,
983 kExpectingInstruction,
984 kExpectingCommit
985 };
986 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
987
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200988 class Branch {
989 public:
990 enum Type {
991 // R2 short branches.
992 kUncondBranch,
993 kCondBranch,
994 kCall,
Alexey Frunze96b66822016-09-10 02:32:44 -0700995 // R2 near label.
996 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700997 // R2 near literal.
998 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200999 // R2 long branches.
1000 kLongUncondBranch,
1001 kLongCondBranch,
1002 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001003 // R2 far label.
1004 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001005 // R2 far literal.
1006 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001007 // R6 short branches.
1008 kR6UncondBranch,
1009 kR6CondBranch,
1010 kR6Call,
Alexey Frunze96b66822016-09-10 02:32:44 -07001011 // R6 near label.
1012 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 // R6 near literal.
1014 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001015 // R6 long branches.
1016 kR6LongUncondBranch,
1017 kR6LongCondBranch,
1018 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001019 // R6 far label.
1020 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001021 // R6 far literal.
1022 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001023 };
1024 // Bit sizes of offsets defined as enums to minimize chance of typos.
1025 enum OffsetBits {
1026 kOffset16 = 16,
1027 kOffset18 = 18,
1028 kOffset21 = 21,
1029 kOffset23 = 23,
1030 kOffset28 = 28,
1031 kOffset32 = 32,
1032 };
1033
1034 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1035 static constexpr int32_t kMaxBranchLength = 32;
1036 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001037 // The following two instruction encodings can never legally occur in branch delay
1038 // slots and are used as markers.
1039 //
1040 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1041 // instruction to fill its delay slot (the latter is only possible with unconditional
1042 // R2 branches and is termed here as "absorption").
1043 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1044 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1045 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1046 // absorption of the target instruction when reordering is disabled.
1047 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001048
1049 struct BranchInfo {
1050 // Branch length as a number of 4-byte-long instructions.
1051 uint32_t length;
1052 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1053 // PC-relative offset (or its most significant 16-bit half, which goes first).
1054 uint32_t instr_offset;
1055 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1056 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1057 // instructions) from the instruction containing the offset.
1058 uint32_t pc_org;
1059 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
1060 // is an exception: use kOffset23 for beqzc/bnezc).
1061 OffsetBits offset_size;
1062 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1063 // count.
1064 int offset_shift;
1065 };
1066 static const BranchInfo branch_info_[/* Type */];
1067
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001068 // Unconditional branch or call.
1069 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001070 // Conditional branch.
1071 Branch(bool is_r6,
1072 uint32_t location,
1073 uint32_t target,
1074 BranchCondition condition,
1075 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001076 Register rhs_reg);
Alexey Frunze96b66822016-09-10 02:32:44 -07001077 // Label address (in literal area) or literal.
1078 Branch(bool is_r6,
1079 uint32_t location,
1080 Register dest_reg,
1081 Register base_reg,
1082 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001083
1084 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1085 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1086 // So, we need a way to identify such branches in order to emit no instructions for them
1087 // or change them to unconditional.
1088 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1089 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1090
1091 static BranchCondition OppositeCondition(BranchCondition cond);
1092
1093 Type GetType() const;
1094 BranchCondition GetCondition() const;
1095 Register GetLeftRegister() const;
1096 Register GetRightRegister() const;
1097 uint32_t GetTarget() const;
1098 uint32_t GetLocation() const;
1099 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001100 uint32_t GetPrecedingInstructionLength(Type type) const;
1101 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001102 uint32_t GetLength() const;
1103 uint32_t GetOldLength() const;
1104 uint32_t GetSize() const;
1105 uint32_t GetOldSize() const;
1106 uint32_t GetEndLocation() const;
1107 uint32_t GetOldEndLocation() const;
1108 bool IsLong() const;
1109 bool IsResolved() const;
1110
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001111 // Various helpers for branch delay slot management.
1112 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
1113 void SetDelayedInstruction(uint32_t instruction);
1114 uint32_t GetDelayedInstruction() const;
1115 void DecrementLocations();
1116
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001117 // Returns the bit size of the signed offset that the branch instruction can handle.
1118 OffsetBits GetOffsetSize() const;
1119
1120 // Calculates the distance between two byte locations in the assembler buffer and
1121 // returns the number of bits needed to represent the distance as a signed integer.
1122 //
1123 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1124 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1125 //
1126 // Composite branches (made of several instructions) with longer reach have 32-bit
1127 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001128 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1129 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1130 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1131 // Consider the following implementation of a long unconditional branch, for
1132 // example:
1133 //
1134 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1135 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1136 //
1137 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1138 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1139 // due to sign extension. This must be compensated for by incrementing offset_31_16
1140 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1141 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1142 // Therefore, the long branch range is something like from PC - 0x80000000 to
1143 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001144 //
1145 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1146 // case with the addiu instruction and a 16 bit offset.
1147 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1148
1149 // Resolve a branch when the target is known.
1150 void Resolve(uint32_t target);
1151
1152 // Relocate a branch by a given delta if needed due to expansion of this or another
1153 // branch at a given location by this delta (just changes location_ and target_).
1154 void Relocate(uint32_t expand_location, uint32_t delta);
1155
1156 // If the branch is short, changes its type to long.
1157 void PromoteToLong();
1158
1159 // If necessary, updates the type by promoting a short branch to a long branch
1160 // based on the branch location and target. Returns the amount (in bytes) by
1161 // which the branch size has increased.
1162 // max_short_distance caps the maximum distance between location_ and target_
1163 // that is allowed for short branches. This is for debugging/testing purposes.
1164 // max_short_distance = 0 forces all short branches to become long.
1165 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001166 uint32_t PromoteIfNeeded(uint32_t location,
1167 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001168
1169 // Returns the location of the instruction(s) containing the offset.
1170 uint32_t GetOffsetLocation() const;
1171
1172 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001173 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001174
1175 private:
1176 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001177 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001178 // Helper for the above.
1179 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1180
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001181 uint32_t old_location_; // Offset into assembler buffer in bytes.
1182 uint32_t location_; // Offset into assembler buffer in bytes.
1183 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001184
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001185 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1186 // FPU condition code. Destination register in literals.
1187 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1188 // Base register in literals (ZERO on R6).
1189 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001190
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001191 Type type_; // Current type of the branch.
1192 Type old_type_; // Initial type of the branch.
1193
1194 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1195 // kUnfilledDelaySlot if none but fillable or
1196 // kUnfillableDelaySlot if none and unfillable
1197 // (the latter is only used for unconditional R2
1198 // branches).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001199 };
1200 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1201 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1202
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001203 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1204 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1205 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1206 uint32_t EmitI26(int opcode, uint32_t imm26);
1207 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1208 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001209 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1210 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001211
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001212 void Buncond(MipsLabel* label);
1213 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001214 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001215 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001216
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001217 // Various helpers for branch delay slot management.
1218 void DsFsmInstr(uint32_t instruction,
1219 uint32_t gpr_outs_mask,
1220 uint32_t gpr_ins_mask,
1221 uint32_t fpr_outs_mask,
1222 uint32_t fpr_ins_mask,
1223 uint32_t cc_outs_mask,
1224 uint32_t cc_ins_mask);
1225 void DsFsmInstrNop(uint32_t instruction);
1226 void DsFsmInstrRrr(uint32_t instruction, Register out, Register in1, Register in2);
1227 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1228 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1229 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
1230 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1231 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1232 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1233 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1234 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1235 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1236 void DsFsmLabel();
1237 void DsFsmCommitLabel();
1238 void DsFsmDropLabel();
1239 void MoveInstructionToDelaySlot(Branch& branch);
1240 bool CanExchangeWithSlt(Register rs, Register rt) const;
1241 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1242 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1243
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001244 Branch* GetBranch(uint32_t branch_id);
1245 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001246 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1247 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001248
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001249 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001250 void ReserveJumpTableSpace();
1251 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001252 void PromoteBranches();
1253 void EmitBranch(Branch* branch);
1254 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001255 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001256
1257 // Emits exception block.
1258 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1259
1260 bool IsR6() const {
1261 if (isa_features_ != nullptr) {
1262 return isa_features_->IsR6();
1263 } else {
1264 return false;
1265 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001266 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001267
1268 bool Is32BitFPU() const {
1269 if (isa_features_ != nullptr) {
1270 return isa_features_->Is32BitFloatingPoint();
1271 } else {
1272 return true;
1273 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001274 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001275
1276 // List of exception blocks to generate at the end of the code cache.
1277 std::vector<MipsExceptionSlowPath> exception_blocks_;
1278
1279 std::vector<Branch> branches_;
1280
1281 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1282 bool overwriting_;
1283 // The current overwrite location.
1284 uint32_t overwrite_location_;
1285
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001286 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1287 bool reordering_;
1288 // Information about the last instruction that may be used to fill a branch delay slot.
1289 DelaySlot delay_slot_;
1290 // Delay slot FSM state.
1291 DsFsmState ds_fsm_state_;
1292 // PC of the current labeled target instruction.
1293 uint32_t ds_fsm_target_pc_;
1294 // PCs of labeled target instructions.
1295 std::vector<uint32_t> ds_fsm_target_pcs_;
1296
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001297 // Use std::deque<> for literal labels to allow insertions at the end
1298 // without invalidating pointers and references to existing elements.
1299 ArenaDeque<Literal> literals_;
1300
Alexey Frunze96b66822016-09-10 02:32:44 -07001301 // Jump table list.
1302 ArenaDeque<JumpTable> jump_tables_;
1303
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001304 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1305 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1306 // that PC (from NAL) points to.
1307 MipsLabel pc_rel_base_label_;
1308
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001309 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001310 uint32_t last_position_adjustment_;
1311 uint32_t last_old_position_;
1312 uint32_t last_branch_id_;
1313
1314 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001315
jeffhao7fbee072012-08-24 17:56:54 -07001316 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1317};
1318
jeffhao7fbee072012-08-24 17:56:54 -07001319} // namespace mips
1320} // namespace art
1321
Ian Rogers166db042013-07-26 12:05:57 -07001322#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_