blob: 099620ccb82c88acf2939a3b28f41eddb1595800 [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>
499 void StoreConst32ToOffset(int32_t value,
500 Register base,
501 int32_t offset,
502 Register temp,
503 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
504 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
505 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false);
506 if (value == 0) {
507 temp = ZERO;
508 } else {
509 LoadConst32(temp, value);
510 }
511 Sw(temp, base, offset);
512 null_checker();
513 }
514
515 template <typename ImplicitNullChecker = NoImplicitNullChecker>
516 void StoreConst64ToOffset(int64_t value,
517 Register base,
518 int32_t offset,
519 Register temp,
520 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
521 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
522 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true);
523 uint32_t low = Low32Bits(value);
524 uint32_t high = High32Bits(value);
525 if (low == 0) {
526 Sw(ZERO, base, offset);
527 } else {
528 LoadConst32(temp, low);
529 Sw(temp, base, offset);
530 }
531 null_checker();
532 if (high == 0) {
533 Sw(ZERO, base, offset + kMipsWordSize);
534 } else {
535 if (high != low) {
536 LoadConst32(temp, high);
537 }
538 Sw(temp, base, offset + kMipsWordSize);
539 }
540 }
541
542 template <typename ImplicitNullChecker = NoImplicitNullChecker>
543 void LoadFromOffset(LoadOperandType type,
544 Register reg,
545 Register base,
546 int32_t offset,
547 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
548 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
549 switch (type) {
550 case kLoadSignedByte:
551 Lb(reg, base, offset);
552 break;
553 case kLoadUnsignedByte:
554 Lbu(reg, base, offset);
555 break;
556 case kLoadSignedHalfword:
557 Lh(reg, base, offset);
558 break;
559 case kLoadUnsignedHalfword:
560 Lhu(reg, base, offset);
561 break;
562 case kLoadWord:
563 Lw(reg, base, offset);
564 break;
565 case kLoadDoubleword:
566 if (reg == base) {
567 // This will clobber the base when loading the lower register. Since we have to load the
568 // higher register as well, this will fail. Solution: reverse the order.
569 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
570 null_checker();
571 Lw(reg, base, offset);
572 } else {
573 Lw(reg, base, offset);
574 null_checker();
575 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
576 }
577 break;
578 default:
579 LOG(FATAL) << "UNREACHABLE";
580 }
581 if (type != kLoadDoubleword) {
582 null_checker();
583 }
584 }
585
586 template <typename ImplicitNullChecker = NoImplicitNullChecker>
587 void LoadSFromOffset(FRegister reg,
588 Register base,
589 int32_t offset,
590 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
591 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
592 Lwc1(reg, base, offset);
593 null_checker();
594 }
595
596 template <typename ImplicitNullChecker = NoImplicitNullChecker>
597 void LoadDFromOffset(FRegister reg,
598 Register base,
599 int32_t offset,
600 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
601 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
602 if (IsAligned<kMipsDoublewordSize>(offset)) {
603 Ldc1(reg, base, offset);
604 null_checker();
605 } else {
606 if (Is32BitFPU()) {
607 Lwc1(reg, base, offset);
608 null_checker();
609 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
610 } else {
611 // 64-bit FPU.
612 Lwc1(reg, base, offset);
613 null_checker();
614 Lw(T8, base, offset + kMipsWordSize);
615 Mthc1(T8, reg);
616 }
617 }
618 }
619
620 template <typename ImplicitNullChecker = NoImplicitNullChecker>
621 void StoreToOffset(StoreOperandType type,
622 Register reg,
623 Register base,
624 int32_t offset,
625 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
626 // Must not use AT as `reg`, so as not to overwrite the value being stored
627 // with the adjusted `base`.
628 CHECK_NE(reg, AT);
629 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
630 switch (type) {
631 case kStoreByte:
632 Sb(reg, base, offset);
633 break;
634 case kStoreHalfword:
635 Sh(reg, base, offset);
636 break;
637 case kStoreWord:
638 Sw(reg, base, offset);
639 break;
640 case kStoreDoubleword:
641 CHECK_NE(reg, base);
642 CHECK_NE(static_cast<Register>(reg + 1), base);
643 Sw(reg, base, offset);
644 null_checker();
645 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
646 break;
647 default:
648 LOG(FATAL) << "UNREACHABLE";
649 }
650 if (type != kStoreDoubleword) {
651 null_checker();
652 }
653 }
654
655 template <typename ImplicitNullChecker = NoImplicitNullChecker>
656 void StoreSToOffset(FRegister reg,
657 Register base,
658 int32_t offset,
659 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
660 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
661 Swc1(reg, base, offset);
662 null_checker();
663 }
664
665 template <typename ImplicitNullChecker = NoImplicitNullChecker>
666 void StoreDToOffset(FRegister reg,
667 Register base,
668 int32_t offset,
669 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
670 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
671 if (IsAligned<kMipsDoublewordSize>(offset)) {
672 Sdc1(reg, base, offset);
673 null_checker();
674 } else {
675 if (Is32BitFPU()) {
676 Swc1(reg, base, offset);
677 null_checker();
678 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
679 } else {
680 // 64-bit FPU.
681 Mfhc1(T8, reg);
682 Swc1(reg, base, offset);
683 null_checker();
684 Sw(T8, base, offset + kMipsWordSize);
685 }
686 }
687 }
688
jeffhao7fbee072012-08-24 17:56:54 -0700689 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
690 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200691 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700692 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000693 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200694 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700695
jeffhao7fbee072012-08-24 17:56:54 -0700696 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200697 void Emit(uint32_t value);
698
699 // Push/pop composite routines.
700 void Push(Register rs);
701 void Pop(Register rd);
702 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700703
Andreas Gampe85b62f22015-09-09 13:15:38 -0700704 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200705 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700706 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200707 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
708 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700709 }
710
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700711 // Create a new literal with a given value.
712 // NOTE: Force the template parameter to be explicitly specified.
713 template <typename T>
714 Literal* NewLiteral(typename Identity<T>::type value) {
715 static_assert(std::is_integral<T>::value, "T must be an integral type.");
716 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
717 }
718
Alexey Frunze96b66822016-09-10 02:32:44 -0700719 // Load label address using the base register (for R2 only) or using PC-relative loads
720 // (for R6 only; base_reg must be ZERO). To be used with data labels in the literal /
721 // jump table area only and not with regular code labels.
722 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
723
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700724 // Create a new literal with the given data.
725 Literal* NewLiteral(size_t size, const uint8_t* data);
726
727 // Load literal using the base register (for R2 only) or using PC-relative loads
728 // (for R6 only; base_reg must be ZERO).
729 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
730
Alexey Frunze96b66822016-09-10 02:32:44 -0700731 // Create a jump table for the given labels that will be emitted when finalizing.
732 // When the table is emitted, offsets will be relative to the location of the table.
733 // The table location is determined by the location of its label (the label precedes
734 // the table data) and should be loaded using LoadLabelAddress().
735 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
736
jeffhao7fbee072012-08-24 17:56:54 -0700737 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200738 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700739 //
740
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200741 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800742 void BuildFrame(size_t frame_size,
743 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100744 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700745 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700746
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200747 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100748 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700749 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700750
Ian Rogersdd7624d2014-03-14 17:43:00 -0700751 void IncreaseFrameSize(size_t adjust) OVERRIDE;
752 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700753
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200754 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700755 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
756 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
757 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700758
Ian Rogersdd7624d2014-03-14 17:43:00 -0700759 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700760
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700761 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
762 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800763 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700764
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700765 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700766
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800767 void StoreSpanning(FrameOffset dest,
768 ManagedRegister msrc,
769 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700770 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700771
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200772 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700773 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700774
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700775 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700776
Mathieu Chartiere401d142015-04-22 13:56:20 -0700777 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700778
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800779 void LoadRef(ManagedRegister mdest,
780 ManagedRegister base,
781 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100782 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700783
Ian Rogersdd7624d2014-03-14 17:43:00 -0700784 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700785
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700786 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700787
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200788 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700789 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700790
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700791 void CopyRawPtrFromThread(FrameOffset fr_offs,
792 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700793 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700794
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700795 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
796 FrameOffset fr_offs,
797 ManagedRegister mscratch) OVERRIDE;
798
Ian Rogersdd7624d2014-03-14 17:43:00 -0700799 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700800
Ian Rogersdd7624d2014-03-14 17:43:00 -0700801 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700802
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800803 void Copy(FrameOffset dest,
804 ManagedRegister src_base,
805 Offset src_offset,
806 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700807 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700808
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800809 void Copy(ManagedRegister dest_base,
810 Offset dest_offset,
811 FrameOffset src,
812 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700813 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700814
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800815 void Copy(FrameOffset dest,
816 FrameOffset src_base,
817 Offset src_offset,
818 ManagedRegister mscratch,
819 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700820
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800821 void Copy(ManagedRegister dest,
822 Offset dest_offset,
823 ManagedRegister src,
824 Offset src_offset,
825 ManagedRegister mscratch,
826 size_t size) OVERRIDE;
827
828 void Copy(FrameOffset dest,
829 Offset dest_offset,
830 FrameOffset src,
831 Offset src_offset,
832 ManagedRegister mscratch,
833 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700834
Ian Rogersdd7624d2014-03-14 17:43:00 -0700835 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700836
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200837 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700838 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700839
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200840 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700841 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700842
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200843 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700844 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
845 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700846
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700847 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700848 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700849 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700850 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800851 void CreateHandleScopeEntry(ManagedRegister out_reg,
852 FrameOffset handlescope_offset,
853 ManagedRegister in_reg,
854 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700855
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700856 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700857 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800858 void CreateHandleScopeEntry(FrameOffset out_off,
859 FrameOffset handlescope_offset,
860 ManagedRegister mscratch,
861 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700862
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200863 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700864 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700865
866 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
867 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700868 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
869 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700870
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200871 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700872 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
873 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700874 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700875
jeffhao7fbee072012-08-24 17:56:54 -0700876 // Generate code to check if Thread::Current()->exception_ is non-null
877 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700878 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700879
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200880 // Emit slow paths queued during assembly and promote short branches to long if needed.
881 void FinalizeCode() OVERRIDE;
882
883 // Emit branches and finalize all instructions.
884 void FinalizeInstructions(const MemoryRegion& region);
885
886 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
887 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700888 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200889
890 // Get the final position of a label after local fixup based on the old position
891 // recorded before FinalizeCode().
892 uint32_t GetAdjustedPosition(uint32_t old_position);
893
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700894 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
895 // reading the PC value into a general-purpose register with the NAL instruction and then loading
896 // literals through this base register. The code generator calls this method (at most once per
897 // method being compiled) to bind a label to the location for which the PC value is acquired.
898 // The assembler then computes literal offsets relative to this label.
899 void BindPcRelBaseLabel();
900
Alexey Frunze06a46c42016-07-19 15:00:40 -0700901 // Returns the location of the label bound with BindPcRelBaseLabel().
902 uint32_t GetPcRelBaseLabelLocation() const;
903
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700904 // Note that PC-relative literal loads are handled as pseudo branches because they need very
905 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
906 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200907 enum BranchCondition {
908 kCondLT,
909 kCondGE,
910 kCondLE,
911 kCondGT,
912 kCondLTZ,
913 kCondGEZ,
914 kCondLEZ,
915 kCondGTZ,
916 kCondEQ,
917 kCondNE,
918 kCondEQZ,
919 kCondNEZ,
920 kCondLTU,
921 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800922 kCondF, // Floating-point predicate false.
923 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200924 kUncond,
925 };
926 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
927
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700928 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
929 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
930 // Returns the last state, which may be useful for temporary enabling/disabling of
931 // reordering.
932 bool SetReorder(bool enable);
933
jeffhao7fbee072012-08-24 17:56:54 -0700934 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700935 // Description of the last instruction in terms of input and output registers.
936 // Used to make the decision of moving the instruction into a delay slot.
937 struct DelaySlot {
938 DelaySlot();
939 // Encoded instruction that may be used to fill the delay slot or 0
940 // (0 conveniently represents NOP).
941 uint32_t instruction_;
942 // Mask of output GPRs for the instruction.
943 uint32_t gpr_outs_mask_;
944 // Mask of input GPRs for the instruction.
945 uint32_t gpr_ins_mask_;
946 // Mask of output FPRs for the instruction.
947 uint32_t fpr_outs_mask_;
948 // Mask of input FPRs for the instruction.
949 uint32_t fpr_ins_mask_;
950 // Mask of output FPU condition code flags for the instruction.
951 uint32_t cc_outs_mask_;
952 // Mask of input FPU condition code flags for the instruction.
953 uint32_t cc_ins_mask_;
954 // Branches never operate on the LO and HI registers, hence there's
955 // no mask for LO and HI.
956 };
957
958 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
959 // upon every new instruction and label generated. The FSM detects instructions
960 // suitable for delay slots and immediately preceded with labels. These are target
961 // instructions for branches. If an unconditional R2 branch does not get its delay
962 // slot filled with the immediately preceding instruction, it may instead get the
963 // slot filled with the target instruction (the branch will need its offset
964 // incremented past the target instruction). We call this "absorption". The FSM
965 // records PCs of the target instructions suitable for this optimization.
966 enum DsFsmState {
967 kExpectingLabel,
968 kExpectingInstruction,
969 kExpectingCommit
970 };
971 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
972
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200973 class Branch {
974 public:
975 enum Type {
976 // R2 short branches.
977 kUncondBranch,
978 kCondBranch,
979 kCall,
Alexey Frunze96b66822016-09-10 02:32:44 -0700980 // R2 near label.
981 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700982 // R2 near literal.
983 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200984 // R2 long branches.
985 kLongUncondBranch,
986 kLongCondBranch,
987 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -0700988 // R2 far label.
989 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700990 // R2 far literal.
991 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200992 // R6 short branches.
993 kR6UncondBranch,
994 kR6CondBranch,
995 kR6Call,
Alexey Frunze96b66822016-09-10 02:32:44 -0700996 // R6 near label.
997 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700998 // R6 near literal.
999 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001000 // R6 long branches.
1001 kR6LongUncondBranch,
1002 kR6LongCondBranch,
1003 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001004 // R6 far label.
1005 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001006 // R6 far literal.
1007 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001008 };
1009 // Bit sizes of offsets defined as enums to minimize chance of typos.
1010 enum OffsetBits {
1011 kOffset16 = 16,
1012 kOffset18 = 18,
1013 kOffset21 = 21,
1014 kOffset23 = 23,
1015 kOffset28 = 28,
1016 kOffset32 = 32,
1017 };
1018
1019 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1020 static constexpr int32_t kMaxBranchLength = 32;
1021 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001022 // The following two instruction encodings can never legally occur in branch delay
1023 // slots and are used as markers.
1024 //
1025 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1026 // instruction to fill its delay slot (the latter is only possible with unconditional
1027 // R2 branches and is termed here as "absorption").
1028 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1029 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1030 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1031 // absorption of the target instruction when reordering is disabled.
1032 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001033
1034 struct BranchInfo {
1035 // Branch length as a number of 4-byte-long instructions.
1036 uint32_t length;
1037 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1038 // PC-relative offset (or its most significant 16-bit half, which goes first).
1039 uint32_t instr_offset;
1040 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1041 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1042 // instructions) from the instruction containing the offset.
1043 uint32_t pc_org;
1044 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
1045 // is an exception: use kOffset23 for beqzc/bnezc).
1046 OffsetBits offset_size;
1047 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1048 // count.
1049 int offset_shift;
1050 };
1051 static const BranchInfo branch_info_[/* Type */];
1052
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001053 // Unconditional branch or call.
1054 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001055 // Conditional branch.
1056 Branch(bool is_r6,
1057 uint32_t location,
1058 uint32_t target,
1059 BranchCondition condition,
1060 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001061 Register rhs_reg);
Alexey Frunze96b66822016-09-10 02:32:44 -07001062 // Label address (in literal area) or literal.
1063 Branch(bool is_r6,
1064 uint32_t location,
1065 Register dest_reg,
1066 Register base_reg,
1067 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001068
1069 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1070 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1071 // So, we need a way to identify such branches in order to emit no instructions for them
1072 // or change them to unconditional.
1073 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1074 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1075
1076 static BranchCondition OppositeCondition(BranchCondition cond);
1077
1078 Type GetType() const;
1079 BranchCondition GetCondition() const;
1080 Register GetLeftRegister() const;
1081 Register GetRightRegister() const;
1082 uint32_t GetTarget() const;
1083 uint32_t GetLocation() const;
1084 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001085 uint32_t GetPrecedingInstructionLength(Type type) const;
1086 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001087 uint32_t GetLength() const;
1088 uint32_t GetOldLength() const;
1089 uint32_t GetSize() const;
1090 uint32_t GetOldSize() const;
1091 uint32_t GetEndLocation() const;
1092 uint32_t GetOldEndLocation() const;
1093 bool IsLong() const;
1094 bool IsResolved() const;
1095
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001096 // Various helpers for branch delay slot management.
1097 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
1098 void SetDelayedInstruction(uint32_t instruction);
1099 uint32_t GetDelayedInstruction() const;
1100 void DecrementLocations();
1101
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001102 // Returns the bit size of the signed offset that the branch instruction can handle.
1103 OffsetBits GetOffsetSize() const;
1104
1105 // Calculates the distance between two byte locations in the assembler buffer and
1106 // returns the number of bits needed to represent the distance as a signed integer.
1107 //
1108 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1109 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1110 //
1111 // Composite branches (made of several instructions) with longer reach have 32-bit
1112 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001113 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1114 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1115 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1116 // Consider the following implementation of a long unconditional branch, for
1117 // example:
1118 //
1119 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1120 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1121 //
1122 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1123 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1124 // due to sign extension. This must be compensated for by incrementing offset_31_16
1125 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1126 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1127 // Therefore, the long branch range is something like from PC - 0x80000000 to
1128 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001129 //
1130 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1131 // case with the addiu instruction and a 16 bit offset.
1132 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1133
1134 // Resolve a branch when the target is known.
1135 void Resolve(uint32_t target);
1136
1137 // Relocate a branch by a given delta if needed due to expansion of this or another
1138 // branch at a given location by this delta (just changes location_ and target_).
1139 void Relocate(uint32_t expand_location, uint32_t delta);
1140
1141 // If the branch is short, changes its type to long.
1142 void PromoteToLong();
1143
1144 // If necessary, updates the type by promoting a short branch to a long branch
1145 // based on the branch location and target. Returns the amount (in bytes) by
1146 // which the branch size has increased.
1147 // max_short_distance caps the maximum distance between location_ and target_
1148 // that is allowed for short branches. This is for debugging/testing purposes.
1149 // max_short_distance = 0 forces all short branches to become long.
1150 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001151 uint32_t PromoteIfNeeded(uint32_t location,
1152 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001153
1154 // Returns the location of the instruction(s) containing the offset.
1155 uint32_t GetOffsetLocation() const;
1156
1157 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001158 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001159
1160 private:
1161 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001162 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001163 // Helper for the above.
1164 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1165
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001166 uint32_t old_location_; // Offset into assembler buffer in bytes.
1167 uint32_t location_; // Offset into assembler buffer in bytes.
1168 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001169
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001170 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1171 // FPU condition code. Destination register in literals.
1172 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1173 // Base register in literals (ZERO on R6).
1174 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001175
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001176 Type type_; // Current type of the branch.
1177 Type old_type_; // Initial type of the branch.
1178
1179 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1180 // kUnfilledDelaySlot if none but fillable or
1181 // kUnfillableDelaySlot if none and unfillable
1182 // (the latter is only used for unconditional R2
1183 // branches).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001184 };
1185 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1186 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1187
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001188 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1189 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1190 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1191 uint32_t EmitI26(int opcode, uint32_t imm26);
1192 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1193 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001194 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1195 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001196
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001197 void Buncond(MipsLabel* label);
1198 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001199 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001200 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001201
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001202 // Various helpers for branch delay slot management.
1203 void DsFsmInstr(uint32_t instruction,
1204 uint32_t gpr_outs_mask,
1205 uint32_t gpr_ins_mask,
1206 uint32_t fpr_outs_mask,
1207 uint32_t fpr_ins_mask,
1208 uint32_t cc_outs_mask,
1209 uint32_t cc_ins_mask);
1210 void DsFsmInstrNop(uint32_t instruction);
1211 void DsFsmInstrRrr(uint32_t instruction, Register out, Register in1, Register in2);
1212 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1213 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1214 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
1215 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1216 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1217 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1218 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1219 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1220 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1221 void DsFsmLabel();
1222 void DsFsmCommitLabel();
1223 void DsFsmDropLabel();
1224 void MoveInstructionToDelaySlot(Branch& branch);
1225 bool CanExchangeWithSlt(Register rs, Register rt) const;
1226 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1227 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1228
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001229 Branch* GetBranch(uint32_t branch_id);
1230 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001231 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1232 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001233
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001234 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001235 void ReserveJumpTableSpace();
1236 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001237 void PromoteBranches();
1238 void EmitBranch(Branch* branch);
1239 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001240 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001241
1242 // Emits exception block.
1243 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1244
1245 bool IsR6() const {
1246 if (isa_features_ != nullptr) {
1247 return isa_features_->IsR6();
1248 } else {
1249 return false;
1250 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001251 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001252
1253 bool Is32BitFPU() const {
1254 if (isa_features_ != nullptr) {
1255 return isa_features_->Is32BitFloatingPoint();
1256 } else {
1257 return true;
1258 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001259 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001260
1261 // List of exception blocks to generate at the end of the code cache.
1262 std::vector<MipsExceptionSlowPath> exception_blocks_;
1263
1264 std::vector<Branch> branches_;
1265
1266 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1267 bool overwriting_;
1268 // The current overwrite location.
1269 uint32_t overwrite_location_;
1270
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001271 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1272 bool reordering_;
1273 // Information about the last instruction that may be used to fill a branch delay slot.
1274 DelaySlot delay_slot_;
1275 // Delay slot FSM state.
1276 DsFsmState ds_fsm_state_;
1277 // PC of the current labeled target instruction.
1278 uint32_t ds_fsm_target_pc_;
1279 // PCs of labeled target instructions.
1280 std::vector<uint32_t> ds_fsm_target_pcs_;
1281
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001282 // Use std::deque<> for literal labels to allow insertions at the end
1283 // without invalidating pointers and references to existing elements.
1284 ArenaDeque<Literal> literals_;
1285
Alexey Frunze96b66822016-09-10 02:32:44 -07001286 // Jump table list.
1287 ArenaDeque<JumpTable> jump_tables_;
1288
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001289 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1290 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1291 // that PC (from NAL) points to.
1292 MipsLabel pc_rel_base_label_;
1293
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001294 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001295 uint32_t last_position_adjustment_;
1296 uint32_t last_old_position_;
1297 uint32_t last_branch_id_;
1298
1299 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001300
jeffhao7fbee072012-08-24 17:56:54 -07001301 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1302};
1303
jeffhao7fbee072012-08-24 17:56:54 -07001304} // namespace mips
1305} // namespace art
1306
Ian Rogers166db042013-07-26 12:05:57 -07001307#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_