blob: 0b4eb9ca551daa505e8cc20aeb1f4e8b35e6cb31 [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"
Andreas Gampe5678db52017-06-08 14:11:18 -070028#include "base/stl_util_identity.h"
jeffhao7fbee072012-08-24 17:56:54 -070029#include "constants_mips.h"
30#include "globals.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
jeffhao7fbee072012-08-24 17:56:54 -070032#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070033#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020034#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070035#include "utils/jni_macro_assembler.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020036#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070037
38namespace art {
39namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070040
Lena Djokic0758ae72017-05-23 11:06:23 +020041static constexpr size_t kMipsHalfwordSize = 2;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020042static constexpr size_t kMipsWordSize = 4;
43static constexpr size_t kMipsDoublewordSize = 8;
44
jeffhao7fbee072012-08-24 17:56:54 -070045enum LoadOperandType {
46 kLoadSignedByte,
47 kLoadUnsignedByte,
48 kLoadSignedHalfword,
49 kLoadUnsignedHalfword,
50 kLoadWord,
Lena Djokic2e0a7e52017-07-06 11:55:24 +020051 kLoadDoubleword,
52 kLoadQuadword
jeffhao7fbee072012-08-24 17:56:54 -070053};
54
55enum StoreOperandType {
56 kStoreByte,
57 kStoreHalfword,
58 kStoreWord,
Lena Djokic2e0a7e52017-07-06 11:55:24 +020059 kStoreDoubleword,
60 kStoreQuadword
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020061};
62
Chris Larsenb74353a2015-11-20 09:07:09 -080063// Used to test the values returned by ClassS/ClassD.
64enum FPClassMaskType {
65 kSignalingNaN = 0x001,
66 kQuietNaN = 0x002,
67 kNegativeInfinity = 0x004,
68 kNegativeNormal = 0x008,
69 kNegativeSubnormal = 0x010,
70 kNegativeZero = 0x020,
71 kPositiveInfinity = 0x040,
72 kPositiveNormal = 0x080,
73 kPositiveSubnormal = 0x100,
74 kPositiveZero = 0x200,
75};
76
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020077class MipsLabel : public Label {
78 public:
79 MipsLabel() : prev_branch_id_plus_one_(0) {}
80
81 MipsLabel(MipsLabel&& src)
82 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
83
Alexey Frunzea663d9d2017-07-31 18:43:18 -070084 void AdjustBoundPosition(int delta) {
85 CHECK(IsBound());
86 // Bound label's position is negative, hence decrementing it.
87 position_ -= delta;
88 }
89
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020090 private:
91 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
92
93 friend class MipsAssembler;
94 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
95};
96
Alexey Frunzee3fb2452016-05-10 16:08:05 -070097// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
98class Literal {
99 public:
100 static constexpr size_t kMaxSize = 8;
101
102 Literal(uint32_t size, const uint8_t* data)
103 : label_(), size_(size) {
104 DCHECK_LE(size, Literal::kMaxSize);
105 memcpy(data_, data, size);
106 }
107
108 template <typename T>
109 T GetValue() const {
110 DCHECK_EQ(size_, sizeof(T));
111 T value;
112 memcpy(&value, data_, sizeof(T));
113 return value;
114 }
115
116 uint32_t GetSize() const {
117 return size_;
118 }
119
120 const uint8_t* GetData() const {
121 return data_;
122 }
123
124 MipsLabel* GetLabel() {
125 return &label_;
126 }
127
128 const MipsLabel* GetLabel() const {
129 return &label_;
130 }
131
132 private:
133 MipsLabel label_;
134 const uint32_t size_;
135 uint8_t data_[kMaxSize];
136
137 DISALLOW_COPY_AND_ASSIGN(Literal);
138};
139
Alexey Frunze96b66822016-09-10 02:32:44 -0700140// Jump table: table of labels emitted after the literals. Similar to literals.
141class JumpTable {
142 public:
143 explicit JumpTable(std::vector<MipsLabel*>&& labels)
144 : label_(), labels_(std::move(labels)) {
145 }
146
147 uint32_t GetSize() const {
148 return static_cast<uint32_t>(labels_.size()) * sizeof(uint32_t);
149 }
150
151 const std::vector<MipsLabel*>& GetData() const {
152 return labels_;
153 }
154
155 MipsLabel* GetLabel() {
156 return &label_;
157 }
158
159 const MipsLabel* GetLabel() const {
160 return &label_;
161 }
162
163 private:
164 MipsLabel label_;
165 std::vector<MipsLabel*> labels_;
166
167 DISALLOW_COPY_AND_ASSIGN(JumpTable);
168};
169
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200170// Slowpath entered when Thread::Current()->_exception is non-null.
171class MipsExceptionSlowPath {
172 public:
173 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
174 : scratch_(scratch), stack_adjust_(stack_adjust) {}
175
176 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800177 : scratch_(src.scratch_),
178 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200179 exception_entry_(std::move(src.exception_entry_)) {}
180
181 private:
182 MipsLabel* Entry() { return &exception_entry_; }
183 const MipsManagedRegister scratch_;
184 const size_t stack_adjust_;
185 MipsLabel exception_entry_;
186
187 friend class MipsAssembler;
188 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700189};
190
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700191class MipsAssembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k32> {
jeffhao7fbee072012-08-24 17:56:54 -0700192 public:
Igor Murashkinae7ff922016-10-06 14:59:19 -0700193 using JNIBase = JNIMacroAssembler<PointerSize::k32>;
194
Vladimir Marko93205e32016-04-13 11:59:46 +0100195 explicit MipsAssembler(ArenaAllocator* arena,
196 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
197 : Assembler(arena),
198 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200199 overwrite_location_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700200 reordering_(true),
201 ds_fsm_state_(kExpectingLabel),
202 ds_fsm_target_pc_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700203 literals_(arena->Adapter(kArenaAllocAssembler)),
Alexey Frunze96b66822016-09-10 02:32:44 -0700204 jump_tables_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200205 last_position_adjustment_(0),
206 last_old_position_(0),
207 last_branch_id_(0),
Lena Djokic0758ae72017-05-23 11:06:23 +0200208 has_msa_(instruction_set_features != nullptr ? instruction_set_features->HasMsa() : false),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100209 isa_features_(instruction_set_features) {
210 cfi().DelayEmittingAdvancePCs();
211 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200212
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700213 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700214 size_t CodePosition() OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700215 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
216
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200217 virtual ~MipsAssembler() {
218 for (auto& branch : branches_) {
219 CHECK(branch.IsResolved());
220 }
221 }
jeffhao7fbee072012-08-24 17:56:54 -0700222
223 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700224 void Addu(Register rd, Register rs, Register rt);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700225 void Addiu(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label);
jeffhao7fbee072012-08-24 17:56:54 -0700226 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700227 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200228
229 void MultR2(Register rs, Register rt); // R2
230 void MultuR2(Register rs, Register rt); // R2
231 void DivR2(Register rs, Register rt); // R2
232 void DivuR2(Register rs, Register rt); // R2
233 void MulR2(Register rd, Register rs, Register rt); // R2
234 void DivR2(Register rd, Register rs, Register rt); // R2
235 void ModR2(Register rd, Register rs, Register rt); // R2
236 void DivuR2(Register rd, Register rs, Register rt); // R2
237 void ModuR2(Register rd, Register rs, Register rt); // R2
238 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800239 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200240 void MuhuR6(Register rd, Register rs, Register rt); // R6
241 void DivR6(Register rd, Register rs, Register rt); // R6
242 void ModR6(Register rd, Register rs, Register rt); // R6
243 void DivuR6(Register rd, Register rs, Register rt); // R6
244 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700245
246 void And(Register rd, Register rs, Register rt);
247 void Andi(Register rt, Register rs, uint16_t imm16);
248 void Or(Register rd, Register rs, Register rt);
249 void Ori(Register rt, Register rs, uint16_t imm16);
250 void Xor(Register rd, Register rs, Register rt);
251 void Xori(Register rt, Register rs, uint16_t imm16);
252 void Nor(Register rd, Register rs, Register rt);
253
Chris Larsene3845472015-11-18 12:27:15 -0800254 void Movz(Register rd, Register rs, Register rt); // R2
255 void Movn(Register rd, Register rs, Register rt); // R2
256 void Seleqz(Register rd, Register rs, Register rt); // R6
257 void Selnez(Register rd, Register rs, Register rt); // R6
258 void ClzR6(Register rd, Register rs);
259 void ClzR2(Register rd, Register rs);
260 void CloR6(Register rd, Register rs);
261 void CloR2(Register rd, Register rs);
262
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200263 void Seb(Register rd, Register rt); // R2+
264 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700265 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800266 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200267
268 void Sll(Register rd, Register rt, int shamt);
269 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700270 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200271 void Sra(Register rd, Register rt, int shamt);
272 void Sllv(Register rd, Register rt, Register rs);
273 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800274 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200275 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800276 void Ext(Register rd, Register rt, int pos, int size); // R2+
277 void Ins(Register rd, Register rt, int pos, int size); // R2+
Chris Larsen692235e2016-11-21 16:04:53 -0800278 void Lsa(Register rd, Register rs, Register rt, int saPlusOne); // R6
Chris Larsencd0295d2017-03-31 15:26:54 -0700279 void ShiftAndAdd(Register dst, Register src_idx, Register src_base, int shamt, Register tmp = AT);
jeffhao7fbee072012-08-24 17:56:54 -0700280
281 void Lb(Register rt, Register rs, uint16_t imm16);
282 void Lh(Register rt, Register rs, uint16_t imm16);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700283 void Lw(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label);
jeffhao7fbee072012-08-24 17:56:54 -0700284 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800285 void Lwl(Register rt, Register rs, uint16_t imm16);
286 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700287 void Lbu(Register rt, Register rs, uint16_t imm16);
288 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700289 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700290 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700291 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Alexey Frunze4147fcc2017-06-17 19:57:27 -0700292 void AddUpper(Register rt, Register rs, uint16_t imm16, Register tmp = AT);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200293 void Sync(uint32_t stype);
294 void Mfhi(Register rd); // R2
295 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700296
297 void Sb(Register rt, Register rs, uint16_t imm16);
298 void Sh(Register rt, Register rs, uint16_t imm16);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700299 void Sw(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label);
jeffhao7fbee072012-08-24 17:56:54 -0700300 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800301 void Swl(Register rt, Register rs, uint16_t imm16);
302 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700303
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700304 void LlR2(Register rt, Register base, int16_t imm16 = 0);
305 void ScR2(Register rt, Register base, int16_t imm16 = 0);
306 void LlR6(Register rt, Register base, int16_t imm9 = 0);
307 void ScR6(Register rt, Register base, int16_t imm9 = 0);
308
jeffhao7fbee072012-08-24 17:56:54 -0700309 void Slt(Register rd, Register rs, Register rt);
310 void Sltu(Register rd, Register rs, Register rt);
311 void Slti(Register rt, Register rs, uint16_t imm16);
312 void Sltiu(Register rt, Register rs, uint16_t imm16);
313
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700314 // Branches and jumps to immediate offsets/addresses do not take care of their
315 // delay/forbidden slots and generally should not be used directly. This applies
316 // to the following R2 and R6 branch/jump instructions with imm16, imm21, addr26
317 // offsets/addresses.
318 // Use branches/jumps to labels instead.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200319 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700320 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200321 void Beq(Register rs, Register rt, uint16_t imm16);
322 void Bne(Register rs, Register rt, uint16_t imm16);
323 void Beqz(Register rt, uint16_t imm16);
324 void Bnez(Register rt, uint16_t imm16);
325 void Bltz(Register rt, uint16_t imm16);
326 void Bgez(Register rt, uint16_t imm16);
327 void Blez(Register rt, uint16_t imm16);
328 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800329 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800330 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800331 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800332 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200333 void J(uint32_t addr26);
334 void Jal(uint32_t addr26);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700335 // Jalr() and Jr() fill their delay slots when reordering is enabled.
336 // When reordering is disabled, the delay slots must be filled manually.
337 // You may use NopIfNoReordering() to fill them when reordering is disabled.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200338 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700339 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200340 void Jr(Register rs);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700341 // Nal() does not fill its delay slot. It must be filled manually.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200342 void Nal();
343 void Auipc(Register rs, uint16_t imm16); // R6
344 void Addiupc(Register rs, uint32_t imm19); // R6
345 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700346 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200347 void Jic(Register rt, uint16_t imm16); // R6
348 void Jialc(Register rt, uint16_t imm16); // R6
349 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
350 void Bltzc(Register rt, uint16_t imm16); // R6
351 void Bgtzc(Register rt, uint16_t imm16); // R6
352 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
353 void Bgezc(Register rt, uint16_t imm16); // R6
354 void Blezc(Register rt, uint16_t imm16); // R6
355 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
356 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
357 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
358 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
359 void Beqzc(Register rs, uint32_t imm21); // R6
360 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800361 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
362 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700363
364 void AddS(FRegister fd, FRegister fs, FRegister ft);
365 void SubS(FRegister fd, FRegister fs, FRegister ft);
366 void MulS(FRegister fd, FRegister fs, FRegister ft);
367 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200368 void AddD(FRegister fd, FRegister fs, FRegister ft);
369 void SubD(FRegister fd, FRegister fs, FRegister ft);
370 void MulD(FRegister fd, FRegister fs, FRegister ft);
371 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800372 void SqrtS(FRegister fd, FRegister fs);
373 void SqrtD(FRegister fd, FRegister fs);
374 void AbsS(FRegister fd, FRegister fs);
375 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700376 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200377 void MovD(FRegister fd, FRegister fs);
378 void NegS(FRegister fd, FRegister fs);
379 void NegD(FRegister fd, FRegister fs);
380
Chris Larsenb74353a2015-11-20 09:07:09 -0800381 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800382 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800383 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800384 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800385 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800386 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800387 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800388 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800389 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800390 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800391 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800392 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800393 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800394 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800395 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800396 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800397 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800398 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800399 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800400 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800401 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800402 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800403 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800404 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800405 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800406 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800407 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800408 void CuleD(int cc, FRegister fs, FRegister ft); // R2
409 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
410 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
411 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
412 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
413 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
414 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
415 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
416 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
417 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
418 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
419 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
420 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
421 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
422 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
423 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
424 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
425 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
426 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
427 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
428 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800429 void Movf(Register rd, Register rs, int cc = 0); // R2
430 void Movt(Register rd, Register rs, int cc = 0); // R2
431 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
432 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
433 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
434 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700435 void MovzS(FRegister fd, FRegister fs, Register rt); // R2
436 void MovzD(FRegister fd, FRegister fs, Register rt); // R2
437 void MovnS(FRegister fd, FRegister fs, Register rt); // R2
438 void MovnD(FRegister fd, FRegister fs, Register rt); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800439 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
440 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700441 void SeleqzS(FRegister fd, FRegister fs, FRegister ft); // R6
442 void SeleqzD(FRegister fd, FRegister fs, FRegister ft); // R6
443 void SelnezS(FRegister fd, FRegister fs, FRegister ft); // R6
444 void SelnezD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800445 void ClassS(FRegister fd, FRegister fs); // R6
446 void ClassD(FRegister fd, FRegister fs); // R6
447 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
448 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
449 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
450 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800451
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800452 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
453 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
454 void TruncWS(FRegister fd, FRegister fs);
455 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200456 void Cvtsw(FRegister fd, FRegister fs);
457 void Cvtdw(FRegister fd, FRegister fs);
458 void Cvtsd(FRegister fd, FRegister fs);
459 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800460 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
461 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800462 void FloorWS(FRegister fd, FRegister fs);
463 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700464
465 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200466 void Mtc1(Register rt, FRegister fs);
467 void Mfhc1(Register rt, FRegister fs);
468 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800469 void MoveFromFpuHigh(Register rt, FRegister fs);
470 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700471 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200472 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700473 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200474 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700475
476 void Break();
jeffhao07030602012-09-26 14:33:14 -0700477 void Nop();
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700478 void NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200479 void Move(Register rd, Register rs);
480 void Clear(Register rd);
481 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700482
Lena Djokic0758ae72017-05-23 11:06:23 +0200483 // MSA instructions.
484 void AndV(VectorRegister wd, VectorRegister ws, VectorRegister wt);
485 void OrV(VectorRegister wd, VectorRegister ws, VectorRegister wt);
486 void NorV(VectorRegister wd, VectorRegister ws, VectorRegister wt);
487 void XorV(VectorRegister wd, VectorRegister ws, VectorRegister wt);
488
489 void AddvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
490 void AddvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
491 void AddvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
492 void AddvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
493 void SubvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
494 void SubvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
495 void SubvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
496 void SubvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
497 void MulvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
498 void MulvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
499 void MulvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
500 void MulvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
501 void Div_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
502 void Div_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
503 void Div_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
504 void Div_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
505 void Div_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
506 void Div_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
507 void Div_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
508 void Div_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
509 void Mod_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
510 void Mod_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
511 void Mod_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
512 void Mod_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
513 void Mod_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
514 void Mod_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
515 void Mod_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
516 void Mod_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
517 void Add_aB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
518 void Add_aH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
519 void Add_aW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
520 void Add_aD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
521 void Ave_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
522 void Ave_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
523 void Ave_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
524 void Ave_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
525 void Ave_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
526 void Ave_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
527 void Ave_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
528 void Ave_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
529 void Aver_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
530 void Aver_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
531 void Aver_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
532 void Aver_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
533 void Aver_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
534 void Aver_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
535 void Aver_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
536 void Aver_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
537 void Max_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
538 void Max_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
539 void Max_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
540 void Max_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
541 void Max_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
542 void Max_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
543 void Max_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
544 void Max_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
545 void Min_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
546 void Min_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
547 void Min_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
548 void Min_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
549 void Min_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
550 void Min_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
551 void Min_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
552 void Min_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
553
554 void FaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
555 void FaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
556 void FsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
557 void FsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
558 void FmulW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
559 void FmulD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
560 void FdivW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
561 void FdivD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
562 void FmaxW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
563 void FmaxD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
564 void FminW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
565 void FminD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
566
567 void Ffint_sW(VectorRegister wd, VectorRegister ws);
568 void Ffint_sD(VectorRegister wd, VectorRegister ws);
569 void Ftint_sW(VectorRegister wd, VectorRegister ws);
570 void Ftint_sD(VectorRegister wd, VectorRegister ws);
571
572 void SllB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
573 void SllH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
574 void SllW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
575 void SllD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
576 void SraB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
577 void SraH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
578 void SraW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
579 void SraD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
580 void SrlB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
581 void SrlH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
582 void SrlW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
583 void SrlD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
584
585 // Immediate shift instructions, where shamtN denotes shift amount (must be between 0 and 2^N-1).
586 void SlliB(VectorRegister wd, VectorRegister ws, int shamt3);
587 void SlliH(VectorRegister wd, VectorRegister ws, int shamt4);
588 void SlliW(VectorRegister wd, VectorRegister ws, int shamt5);
589 void SlliD(VectorRegister wd, VectorRegister ws, int shamt6);
590 void SraiB(VectorRegister wd, VectorRegister ws, int shamt3);
591 void SraiH(VectorRegister wd, VectorRegister ws, int shamt4);
592 void SraiW(VectorRegister wd, VectorRegister ws, int shamt5);
593 void SraiD(VectorRegister wd, VectorRegister ws, int shamt6);
594 void SrliB(VectorRegister wd, VectorRegister ws, int shamt3);
595 void SrliH(VectorRegister wd, VectorRegister ws, int shamt4);
596 void SrliW(VectorRegister wd, VectorRegister ws, int shamt5);
597 void SrliD(VectorRegister wd, VectorRegister ws, int shamt6);
598
599 void MoveV(VectorRegister wd, VectorRegister ws);
600 void SplatiB(VectorRegister wd, VectorRegister ws, int n4);
601 void SplatiH(VectorRegister wd, VectorRegister ws, int n3);
602 void SplatiW(VectorRegister wd, VectorRegister ws, int n2);
603 void SplatiD(VectorRegister wd, VectorRegister ws, int n1);
604 void FillB(VectorRegister wd, Register rs);
605 void FillH(VectorRegister wd, Register rs);
606 void FillW(VectorRegister wd, Register rs);
607
608 void LdiB(VectorRegister wd, int imm8);
609 void LdiH(VectorRegister wd, int imm10);
610 void LdiW(VectorRegister wd, int imm10);
611 void LdiD(VectorRegister wd, int imm10);
612 void LdB(VectorRegister wd, Register rs, int offset);
613 void LdH(VectorRegister wd, Register rs, int offset);
614 void LdW(VectorRegister wd, Register rs, int offset);
615 void LdD(VectorRegister wd, Register rs, int offset);
616 void StB(VectorRegister wd, Register rs, int offset);
617 void StH(VectorRegister wd, Register rs, int offset);
618 void StW(VectorRegister wd, Register rs, int offset);
619 void StD(VectorRegister wd, Register rs, int offset);
620
621 void IlvrB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
622 void IlvrH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
623 void IlvrW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
624 void IlvrD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
625
Lena Djokicb3d79e42017-07-25 11:20:52 +0200626 void MaddvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
627 void MaddvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
628 void MaddvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
629 void MaddvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
630 void MsubvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
631 void MsubvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
632 void MsubvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
633 void MsubvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
634 void FmaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
635 void FmaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
636 void FmsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
637 void FmsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
638
Lena Djokic51765b02017-06-22 13:49:59 +0200639 // Helper for replicating floating point value in all destination elements.
640 void ReplicateFPToVectorRegister(VectorRegister dst, FRegister src, bool is_double);
641
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200642 // Higher level composite instructions.
643 void LoadConst32(Register rd, int32_t value);
644 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
645 void LoadDConst64(FRegister rd, int64_t value, Register temp);
646 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200647 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
648
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200649 void Bind(MipsLabel* label);
Alexey Frunze0cab6562017-07-25 15:19:36 -0700650 // When `is_bare` is false, the branches will promote to long (if the range
651 // of the individual branch instruction is insufficient) and the delay/
652 // forbidden slots will be taken care of.
653 // Use `is_bare = false` when the branch target may be out of reach of the
654 // individual branch instruction. IOW, this is for general purpose use.
655 //
656 // When `is_bare` is true, just the branch instructions will be generated
657 // leaving delay/forbidden slot filling up to the caller and the branches
658 // won't promote to long if the range is insufficient (you'll get a
659 // compilation error when the range is exceeded).
660 // Use `is_bare = true` when the branch target is known to be within reach
661 // of the individual branch instruction. This is intended for small local
662 // optimizations around delay/forbidden slots.
663 // Also prefer using `is_bare = true` if the code near the branch is to be
664 // patched or analyzed at run time (e.g. introspection) to
665 // - show the intent and
666 // - fail during compilation rather than during patching/execution if the
667 // bare branch range is insufficent but the code size and layout are
668 // expected to remain unchanged
669 //
670 // R2 branches with delay slots that are also available on R6.
671 // On R6 when `is_bare` is false these convert to equivalent R6 compact
672 // branches (to reduce code size). On R2 or when `is_bare` is true they
673 // remain R2 branches with delay slots.
674 void B(MipsLabel* label, bool is_bare = false);
675 void Bal(MipsLabel* label, bool is_bare = false);
676 void Beq(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
677 void Bne(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
678 void Beqz(Register rt, MipsLabel* label, bool is_bare = false);
679 void Bnez(Register rt, MipsLabel* label, bool is_bare = false);
680 void Bltz(Register rt, MipsLabel* label, bool is_bare = false);
681 void Bgez(Register rt, MipsLabel* label, bool is_bare = false);
682 void Blez(Register rt, MipsLabel* label, bool is_bare = false);
683 void Bgtz(Register rt, MipsLabel* label, bool is_bare = false);
684 void Blt(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
685 void Bge(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
686 void Bltu(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
687 void Bgeu(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
688 // R2-only branches with delay slots.
689 void Bc1f(MipsLabel* label, bool is_bare = false); // R2
690 void Bc1f(int cc, MipsLabel* label, bool is_bare = false); // R2
691 void Bc1t(MipsLabel* label, bool is_bare = false); // R2
692 void Bc1t(int cc, MipsLabel* label, bool is_bare = false); // R2
693 // R6-only compact branches without delay/forbidden slots.
694 void Bc(MipsLabel* label, bool is_bare = false); // R6
695 void Balc(MipsLabel* label, bool is_bare = false); // R6
696 // R6-only compact branches with forbidden slots.
697 void Beqc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
698 void Bnec(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
699 void Beqzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
700 void Bnezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
701 void Bltzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
702 void Bgezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
703 void Blezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
704 void Bgtzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
705 void Bltc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
706 void Bgec(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
707 void Bltuc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
708 void Bgeuc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
709 // R6-only branches with delay slots.
710 void Bc1eqz(FRegister ft, MipsLabel* label, bool is_bare = false); // R6
711 void Bc1nez(FRegister ft, MipsLabel* label, bool is_bare = false); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700712
713 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700714 void AdjustBaseAndOffset(Register& base,
715 int32_t& offset,
716 bool is_doubleword,
717 bool is_float = false);
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200718 void AdjustBaseOffsetAndElementSizeShift(Register& base,
719 int32_t& offset,
720 int& element_size_shift);
Alexey Frunze2923db72016-08-20 01:55:47 -0700721
722 private:
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100723 // This will be used as an argument for loads/stores
724 // when there is no need for implicit null checks.
Alexey Frunze2923db72016-08-20 01:55:47 -0700725 struct NoImplicitNullChecker {
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100726 void operator()() const {}
Alexey Frunze2923db72016-08-20 01:55:47 -0700727 };
728
729 public:
730 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunzef58b2482016-09-02 22:14:06 -0700731 void StoreConstToOffset(StoreOperandType type,
732 int64_t value,
733 Register base,
734 int32_t offset,
735 Register temp,
736 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
737 // We permit `base` and `temp` to coincide (however, we check that neither is AT),
738 // in which case the `base` register may be overwritten in the process.
Alexey Frunze2923db72016-08-20 01:55:47 -0700739 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 -0700740 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
Alexey Frunze2923db72016-08-20 01:55:47 -0700741 uint32_t low = Low32Bits(value);
742 uint32_t high = High32Bits(value);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700743 Register reg;
744 // If the adjustment left `base` unchanged and equal to `temp`, we can't use `temp`
745 // to load and hold the value but we can use AT instead as AT hasn't been used yet.
746 // Otherwise, `temp` can be used for the value. And if `temp` is the same as the
747 // original `base` (that is, `base` prior to the adjustment), the original `base`
748 // register will be overwritten.
749 if (base == temp) {
750 temp = AT;
Alexey Frunze2923db72016-08-20 01:55:47 -0700751 }
Alexey Frunzef58b2482016-09-02 22:14:06 -0700752 if (low == 0) {
753 reg = ZERO;
Alexey Frunze2923db72016-08-20 01:55:47 -0700754 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -0700755 reg = temp;
756 LoadConst32(reg, low);
757 }
758 switch (type) {
759 case kStoreByte:
760 Sb(reg, base, offset);
761 break;
762 case kStoreHalfword:
763 Sh(reg, base, offset);
764 break;
765 case kStoreWord:
766 Sw(reg, base, offset);
767 break;
768 case kStoreDoubleword:
769 Sw(reg, base, offset);
770 null_checker();
771 if (high == 0) {
772 reg = ZERO;
773 } else {
774 reg = temp;
775 if (high != low) {
776 LoadConst32(reg, high);
777 }
778 }
779 Sw(reg, base, offset + kMipsWordSize);
780 break;
781 default:
782 LOG(FATAL) << "UNREACHABLE";
783 }
784 if (type != kStoreDoubleword) {
785 null_checker();
Alexey Frunze2923db72016-08-20 01:55:47 -0700786 }
787 }
788
789 template <typename ImplicitNullChecker = NoImplicitNullChecker>
790 void LoadFromOffset(LoadOperandType type,
791 Register reg,
792 Register base,
793 int32_t offset,
794 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
795 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
796 switch (type) {
797 case kLoadSignedByte:
798 Lb(reg, base, offset);
799 break;
800 case kLoadUnsignedByte:
801 Lbu(reg, base, offset);
802 break;
803 case kLoadSignedHalfword:
804 Lh(reg, base, offset);
805 break;
806 case kLoadUnsignedHalfword:
807 Lhu(reg, base, offset);
808 break;
809 case kLoadWord:
810 Lw(reg, base, offset);
811 break;
812 case kLoadDoubleword:
813 if (reg == base) {
814 // This will clobber the base when loading the lower register. Since we have to load the
815 // higher register as well, this will fail. Solution: reverse the order.
816 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
817 null_checker();
818 Lw(reg, base, offset);
819 } else {
820 Lw(reg, base, offset);
821 null_checker();
822 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
823 }
824 break;
825 default:
826 LOG(FATAL) << "UNREACHABLE";
827 }
828 if (type != kLoadDoubleword) {
829 null_checker();
830 }
831 }
832
833 template <typename ImplicitNullChecker = NoImplicitNullChecker>
834 void LoadSFromOffset(FRegister reg,
835 Register base,
836 int32_t offset,
837 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
838 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
839 Lwc1(reg, base, offset);
840 null_checker();
841 }
842
843 template <typename ImplicitNullChecker = NoImplicitNullChecker>
844 void LoadDFromOffset(FRegister reg,
845 Register base,
846 int32_t offset,
847 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
848 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
849 if (IsAligned<kMipsDoublewordSize>(offset)) {
850 Ldc1(reg, base, offset);
851 null_checker();
852 } else {
853 if (Is32BitFPU()) {
854 Lwc1(reg, base, offset);
855 null_checker();
856 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
857 } else {
858 // 64-bit FPU.
859 Lwc1(reg, base, offset);
860 null_checker();
861 Lw(T8, base, offset + kMipsWordSize);
862 Mthc1(T8, reg);
863 }
864 }
865 }
866
867 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200868 void LoadQFromOffset(FRegister reg,
869 Register base,
870 int32_t offset,
871 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
872 int element_size_shift = -1;
873 AdjustBaseOffsetAndElementSizeShift(base, offset, element_size_shift);
874 switch (element_size_shift) {
875 case TIMES_1: LdB(static_cast<VectorRegister>(reg), base, offset); break;
876 case TIMES_2: LdH(static_cast<VectorRegister>(reg), base, offset); break;
877 case TIMES_4: LdW(static_cast<VectorRegister>(reg), base, offset); break;
878 case TIMES_8: LdD(static_cast<VectorRegister>(reg), base, offset); break;
879 default:
880 LOG(FATAL) << "UNREACHABLE";
881 }
882 null_checker();
883 }
884
885 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunze2923db72016-08-20 01:55:47 -0700886 void StoreToOffset(StoreOperandType type,
887 Register reg,
888 Register base,
889 int32_t offset,
890 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
891 // Must not use AT as `reg`, so as not to overwrite the value being stored
892 // with the adjusted `base`.
893 CHECK_NE(reg, AT);
894 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
895 switch (type) {
896 case kStoreByte:
897 Sb(reg, base, offset);
898 break;
899 case kStoreHalfword:
900 Sh(reg, base, offset);
901 break;
902 case kStoreWord:
903 Sw(reg, base, offset);
904 break;
905 case kStoreDoubleword:
906 CHECK_NE(reg, base);
907 CHECK_NE(static_cast<Register>(reg + 1), base);
908 Sw(reg, base, offset);
909 null_checker();
910 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
911 break;
912 default:
913 LOG(FATAL) << "UNREACHABLE";
914 }
915 if (type != kStoreDoubleword) {
916 null_checker();
917 }
918 }
919
920 template <typename ImplicitNullChecker = NoImplicitNullChecker>
921 void StoreSToOffset(FRegister reg,
922 Register base,
923 int32_t offset,
924 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
925 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
926 Swc1(reg, base, offset);
927 null_checker();
928 }
929
930 template <typename ImplicitNullChecker = NoImplicitNullChecker>
931 void StoreDToOffset(FRegister reg,
932 Register base,
933 int32_t offset,
934 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
935 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
936 if (IsAligned<kMipsDoublewordSize>(offset)) {
937 Sdc1(reg, base, offset);
938 null_checker();
939 } else {
940 if (Is32BitFPU()) {
941 Swc1(reg, base, offset);
942 null_checker();
943 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
944 } else {
945 // 64-bit FPU.
946 Mfhc1(T8, reg);
947 Swc1(reg, base, offset);
948 null_checker();
949 Sw(T8, base, offset + kMipsWordSize);
950 }
951 }
952 }
953
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200954 template <typename ImplicitNullChecker = NoImplicitNullChecker>
955 void StoreQToOffset(FRegister reg,
956 Register base,
957 int32_t offset,
958 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
959 int element_size_shift = -1;
960 AdjustBaseOffsetAndElementSizeShift(base, offset, element_size_shift);
961 switch (element_size_shift) {
962 case TIMES_1: StB(static_cast<VectorRegister>(reg), base, offset); break;
963 case TIMES_2: StH(static_cast<VectorRegister>(reg), base, offset); break;
964 case TIMES_4: StW(static_cast<VectorRegister>(reg), base, offset); break;
965 case TIMES_8: StD(static_cast<VectorRegister>(reg), base, offset); break;
966 default:
967 LOG(FATAL) << "UNREACHABLE";
968 }
969 null_checker();
970 }
971
jeffhao7fbee072012-08-24 17:56:54 -0700972 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
973 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200974 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200975 void LoadQFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700976 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000977 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200978 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200979 void StoreQToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700980
jeffhao7fbee072012-08-24 17:56:54 -0700981 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200982 void Emit(uint32_t value);
983
984 // Push/pop composite routines.
985 void Push(Register rs);
986 void Pop(Register rd);
987 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700988
Alexey Frunzec061de12017-02-14 13:27:23 -0800989 //
990 // Heap poisoning.
991 //
992
993 // Poison a heap reference contained in `src` and store it in `dst`.
994 void PoisonHeapReference(Register dst, Register src) {
995 // dst = -src.
996 Subu(dst, ZERO, src);
997 }
998 // Poison a heap reference contained in `reg`.
999 void PoisonHeapReference(Register reg) {
1000 // reg = -reg.
1001 PoisonHeapReference(reg, reg);
1002 }
1003 // Unpoison a heap reference contained in `reg`.
1004 void UnpoisonHeapReference(Register reg) {
1005 // reg = -reg.
1006 Subu(reg, ZERO, reg);
1007 }
1008 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
1009 void MaybePoisonHeapReference(Register reg) {
1010 if (kPoisonHeapReferences) {
1011 PoisonHeapReference(reg);
1012 }
1013 }
1014 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
1015 void MaybeUnpoisonHeapReference(Register reg) {
1016 if (kPoisonHeapReferences) {
1017 UnpoisonHeapReference(reg);
1018 }
1019 }
1020
Andreas Gampe85b62f22015-09-09 13:15:38 -07001021 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001022 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001023 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001024 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
1025 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -07001026 }
1027
Igor Murashkinae7ff922016-10-06 14:59:19 -07001028 // Don't warn about a different virtual Bind/Jump in the base class.
1029 using JNIBase::Bind;
1030 using JNIBase::Jump;
1031
1032 // Create a new label that can be used with Jump/Bind calls.
1033 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
1034 LOG(FATAL) << "Not implemented on MIPS32";
1035 UNREACHABLE();
1036 }
1037 // Emit an unconditional jump to the label.
1038 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
1039 LOG(FATAL) << "Not implemented on MIPS32";
1040 UNREACHABLE();
1041 }
1042 // Emit a conditional jump to the label by applying a unary condition test to the register.
1043 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
1044 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
1045 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
1046 LOG(FATAL) << "Not implemented on MIPS32";
1047 UNREACHABLE();
1048 }
1049
1050 // Code at this offset will serve as the target for the Jump call.
1051 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
1052 LOG(FATAL) << "Not implemented on MIPS32";
1053 UNREACHABLE();
1054 }
1055
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001056 // Create a new literal with a given value.
1057 // NOTE: Force the template parameter to be explicitly specified.
1058 template <typename T>
1059 Literal* NewLiteral(typename Identity<T>::type value) {
1060 static_assert(std::is_integral<T>::value, "T must be an integral type.");
1061 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
1062 }
1063
Alexey Frunze96b66822016-09-10 02:32:44 -07001064 // Load label address using the base register (for R2 only) or using PC-relative loads
1065 // (for R6 only; base_reg must be ZERO). To be used with data labels in the literal /
1066 // jump table area only and not with regular code labels.
1067 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
1068
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001069 // Create a new literal with the given data.
1070 Literal* NewLiteral(size_t size, const uint8_t* data);
1071
1072 // Load literal using the base register (for R2 only) or using PC-relative loads
1073 // (for R6 only; base_reg must be ZERO).
1074 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
1075
Alexey Frunze96b66822016-09-10 02:32:44 -07001076 // Create a jump table for the given labels that will be emitted when finalizing.
1077 // When the table is emitted, offsets will be relative to the location of the table.
1078 // The table location is determined by the location of its label (the label precedes
1079 // the table data) and should be loaded using LoadLabelAddress().
1080 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
1081
jeffhao7fbee072012-08-24 17:56:54 -07001082 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001083 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -07001084 //
1085
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001086 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001087 void BuildFrame(size_t frame_size,
1088 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +01001089 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001090 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001091
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001092 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +01001093 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -07001094 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001095
Ian Rogersdd7624d2014-03-14 17:43:00 -07001096 void IncreaseFrameSize(size_t adjust) OVERRIDE;
1097 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001098
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001099 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001100 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
1101 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
1102 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001103
Ian Rogersdd7624d2014-03-14 17:43:00 -07001104 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001105
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001106 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
1107 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001108 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001109
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001110 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001111
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001112 void StoreSpanning(FrameOffset dest,
1113 ManagedRegister msrc,
1114 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001115 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001116
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001117 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001118 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001119
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001120 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001121
Mathieu Chartiere401d142015-04-22 13:56:20 -07001122 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001123
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001124 void LoadRef(ManagedRegister mdest,
1125 ManagedRegister base,
1126 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +01001127 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001128
Ian Rogersdd7624d2014-03-14 17:43:00 -07001129 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001130
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001131 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001132
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001133 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001134 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001135
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001136 void CopyRawPtrFromThread(FrameOffset fr_offs,
1137 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001138 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001139
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001140 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
1141 FrameOffset fr_offs,
1142 ManagedRegister mscratch) OVERRIDE;
1143
Ian Rogersdd7624d2014-03-14 17:43:00 -07001144 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001145
Ian Rogersdd7624d2014-03-14 17:43:00 -07001146 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001147
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001148 void Copy(FrameOffset dest,
1149 ManagedRegister src_base,
1150 Offset src_offset,
1151 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001152 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001153
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001154 void Copy(ManagedRegister dest_base,
1155 Offset dest_offset,
1156 FrameOffset src,
1157 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001158 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001159
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001160 void Copy(FrameOffset dest,
1161 FrameOffset src_base,
1162 Offset src_offset,
1163 ManagedRegister mscratch,
1164 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001165
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001166 void Copy(ManagedRegister dest,
1167 Offset dest_offset,
1168 ManagedRegister src,
1169 Offset src_offset,
1170 ManagedRegister mscratch,
1171 size_t size) OVERRIDE;
1172
1173 void Copy(FrameOffset dest,
1174 Offset dest_offset,
1175 FrameOffset src,
1176 Offset src_offset,
1177 ManagedRegister mscratch,
1178 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001179
Ian Rogersdd7624d2014-03-14 17:43:00 -07001180 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001181
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001182 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001183 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001184
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001185 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001186 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001187
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001188 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -07001189 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
1190 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001191
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001192 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -07001193 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001194 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001195 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001196 void CreateHandleScopeEntry(ManagedRegister out_reg,
1197 FrameOffset handlescope_offset,
1198 ManagedRegister in_reg,
1199 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001200
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001201 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -07001202 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001203 void CreateHandleScopeEntry(FrameOffset out_off,
1204 FrameOffset handlescope_offset,
1205 ManagedRegister mscratch,
1206 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001207
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001208 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001209 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001210
1211 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
1212 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001213 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
1214 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001215
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001216 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -07001217 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
1218 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001219 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001220
jeffhao7fbee072012-08-24 17:56:54 -07001221 // Generate code to check if Thread::Current()->exception_ is non-null
1222 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001223 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001224
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001225 // Emit slow paths queued during assembly and promote short branches to long if needed.
1226 void FinalizeCode() OVERRIDE;
1227
1228 // Emit branches and finalize all instructions.
1229 void FinalizeInstructions(const MemoryRegion& region);
1230
1231 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
1232 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001233 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001234
1235 // Get the final position of a label after local fixup based on the old position
1236 // recorded before FinalizeCode().
1237 uint32_t GetAdjustedPosition(uint32_t old_position);
1238
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001239 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
1240 // reading the PC value into a general-purpose register with the NAL instruction and then loading
1241 // literals through this base register. The code generator calls this method (at most once per
1242 // method being compiled) to bind a label to the location for which the PC value is acquired.
1243 // The assembler then computes literal offsets relative to this label.
1244 void BindPcRelBaseLabel();
1245
Alexey Frunze06a46c42016-07-19 15:00:40 -07001246 // Returns the location of the label bound with BindPcRelBaseLabel().
1247 uint32_t GetPcRelBaseLabelLocation() const;
1248
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001249 // Note that PC-relative literal loads are handled as pseudo branches because they need very
1250 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
1251 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001252 enum BranchCondition {
1253 kCondLT,
1254 kCondGE,
1255 kCondLE,
1256 kCondGT,
1257 kCondLTZ,
1258 kCondGEZ,
1259 kCondLEZ,
1260 kCondGTZ,
1261 kCondEQ,
1262 kCondNE,
1263 kCondEQZ,
1264 kCondNEZ,
1265 kCondLTU,
1266 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001267 kCondF, // Floating-point predicate false.
1268 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001269 kUncond,
1270 };
1271 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
1272
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001273 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
1274 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
1275 // Returns the last state, which may be useful for temporary enabling/disabling of
1276 // reordering.
1277 bool SetReorder(bool enable);
1278
jeffhao7fbee072012-08-24 17:56:54 -07001279 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001280 // Description of the last instruction in terms of input and output registers.
1281 // Used to make the decision of moving the instruction into a delay slot.
1282 struct DelaySlot {
1283 DelaySlot();
1284 // Encoded instruction that may be used to fill the delay slot or 0
1285 // (0 conveniently represents NOP).
1286 uint32_t instruction_;
1287 // Mask of output GPRs for the instruction.
1288 uint32_t gpr_outs_mask_;
1289 // Mask of input GPRs for the instruction.
1290 uint32_t gpr_ins_mask_;
1291 // Mask of output FPRs for the instruction.
1292 uint32_t fpr_outs_mask_;
1293 // Mask of input FPRs for the instruction.
1294 uint32_t fpr_ins_mask_;
1295 // Mask of output FPU condition code flags for the instruction.
1296 uint32_t cc_outs_mask_;
1297 // Mask of input FPU condition code flags for the instruction.
1298 uint32_t cc_ins_mask_;
1299 // Branches never operate on the LO and HI registers, hence there's
1300 // no mask for LO and HI.
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001301
1302 // Label for patchable instructions to allow moving them into delay slots.
1303 MipsLabel* patcher_label_;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001304 };
1305
1306 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
1307 // upon every new instruction and label generated. The FSM detects instructions
1308 // suitable for delay slots and immediately preceded with labels. These are target
1309 // instructions for branches. If an unconditional R2 branch does not get its delay
1310 // slot filled with the immediately preceding instruction, it may instead get the
1311 // slot filled with the target instruction (the branch will need its offset
1312 // incremented past the target instruction). We call this "absorption". The FSM
1313 // records PCs of the target instructions suitable for this optimization.
1314 enum DsFsmState {
1315 kExpectingLabel,
1316 kExpectingInstruction,
1317 kExpectingCommit
1318 };
1319 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
1320
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001321 class Branch {
1322 public:
1323 enum Type {
Alexey Frunze0cab6562017-07-25 15:19:36 -07001324 // R2 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001325 kUncondBranch,
1326 kCondBranch,
1327 kCall,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001328 // R2 short branches (can't be promoted to long), delay slots filled manually.
1329 kBareUncondBranch,
1330 kBareCondBranch,
1331 kBareCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001332 // R2 near label.
1333 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001334 // R2 near literal.
1335 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001336 // R2 long branches.
1337 kLongUncondBranch,
1338 kLongCondBranch,
1339 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001340 // R2 far label.
1341 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001342 // R2 far literal.
1343 kFarLiteral,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001344 // R6 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001345 kR6UncondBranch,
1346 kR6CondBranch,
1347 kR6Call,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001348 // R6 short branches (can't be promoted to long), forbidden/delay slots filled manually.
1349 kR6BareUncondBranch,
1350 kR6BareCondBranch,
1351 kR6BareCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001352 // R6 near label.
1353 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001354 // R6 near literal.
1355 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001356 // R6 long branches.
1357 kR6LongUncondBranch,
1358 kR6LongCondBranch,
1359 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001360 // R6 far label.
1361 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001362 // R6 far literal.
1363 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001364 };
1365 // Bit sizes of offsets defined as enums to minimize chance of typos.
1366 enum OffsetBits {
1367 kOffset16 = 16,
1368 kOffset18 = 18,
1369 kOffset21 = 21,
1370 kOffset23 = 23,
1371 kOffset28 = 28,
1372 kOffset32 = 32,
1373 };
1374
1375 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1376 static constexpr int32_t kMaxBranchLength = 32;
1377 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001378 // The following two instruction encodings can never legally occur in branch delay
1379 // slots and are used as markers.
1380 //
1381 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1382 // instruction to fill its delay slot (the latter is only possible with unconditional
1383 // R2 branches and is termed here as "absorption").
1384 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1385 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1386 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1387 // absorption of the target instruction when reordering is disabled.
1388 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001389
1390 struct BranchInfo {
1391 // Branch length as a number of 4-byte-long instructions.
1392 uint32_t length;
1393 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1394 // PC-relative offset (or its most significant 16-bit half, which goes first).
1395 uint32_t instr_offset;
1396 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1397 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1398 // instructions) from the instruction containing the offset.
1399 uint32_t pc_org;
1400 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
Alexey Frunze0cab6562017-07-25 15:19:36 -07001401 // and kR6BareCondBranch are an exception: use kOffset23 for beqzc/bnezc).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001402 OffsetBits offset_size;
1403 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1404 // count.
1405 int offset_shift;
1406 };
1407 static const BranchInfo branch_info_[/* Type */];
1408
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001409 // Unconditional branch or call.
Alexey Frunze0cab6562017-07-25 15:19:36 -07001410 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call, bool is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001411 // Conditional branch.
1412 Branch(bool is_r6,
1413 uint32_t location,
1414 uint32_t target,
1415 BranchCondition condition,
1416 Register lhs_reg,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001417 Register rhs_reg,
1418 bool is_bare);
Alexey Frunze96b66822016-09-10 02:32:44 -07001419 // Label address (in literal area) or literal.
1420 Branch(bool is_r6,
1421 uint32_t location,
1422 Register dest_reg,
1423 Register base_reg,
1424 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001425
1426 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1427 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1428 // So, we need a way to identify such branches in order to emit no instructions for them
1429 // or change them to unconditional.
1430 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1431 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1432
1433 static BranchCondition OppositeCondition(BranchCondition cond);
1434
1435 Type GetType() const;
1436 BranchCondition GetCondition() const;
1437 Register GetLeftRegister() const;
1438 Register GetRightRegister() const;
1439 uint32_t GetTarget() const;
1440 uint32_t GetLocation() const;
1441 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001442 uint32_t GetPrecedingInstructionLength(Type type) const;
1443 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001444 uint32_t GetLength() const;
1445 uint32_t GetOldLength() const;
1446 uint32_t GetSize() const;
1447 uint32_t GetOldSize() const;
1448 uint32_t GetEndLocation() const;
1449 uint32_t GetOldEndLocation() const;
Alexey Frunze0cab6562017-07-25 15:19:36 -07001450 bool IsBare() const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001451 bool IsLong() const;
1452 bool IsResolved() const;
1453
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001454 // Various helpers for branch delay slot management.
1455 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001456 void SetDelayedInstruction(uint32_t instruction, MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001457 uint32_t GetDelayedInstruction() const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001458 MipsLabel* GetPatcherLabel() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001459 void DecrementLocations();
1460
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001461 // Returns the bit size of the signed offset that the branch instruction can handle.
1462 OffsetBits GetOffsetSize() const;
1463
1464 // Calculates the distance between two byte locations in the assembler buffer and
1465 // returns the number of bits needed to represent the distance as a signed integer.
1466 //
1467 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1468 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1469 //
1470 // Composite branches (made of several instructions) with longer reach have 32-bit
1471 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001472 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1473 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1474 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1475 // Consider the following implementation of a long unconditional branch, for
1476 // example:
1477 //
1478 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1479 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1480 //
1481 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1482 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1483 // due to sign extension. This must be compensated for by incrementing offset_31_16
1484 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1485 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1486 // Therefore, the long branch range is something like from PC - 0x80000000 to
1487 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001488 //
1489 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1490 // case with the addiu instruction and a 16 bit offset.
1491 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1492
1493 // Resolve a branch when the target is known.
1494 void Resolve(uint32_t target);
1495
1496 // Relocate a branch by a given delta if needed due to expansion of this or another
1497 // branch at a given location by this delta (just changes location_ and target_).
1498 void Relocate(uint32_t expand_location, uint32_t delta);
1499
1500 // If the branch is short, changes its type to long.
1501 void PromoteToLong();
1502
1503 // If necessary, updates the type by promoting a short branch to a long branch
1504 // based on the branch location and target. Returns the amount (in bytes) by
1505 // which the branch size has increased.
1506 // max_short_distance caps the maximum distance between location_ and target_
1507 // that is allowed for short branches. This is for debugging/testing purposes.
1508 // max_short_distance = 0 forces all short branches to become long.
1509 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001510 uint32_t PromoteIfNeeded(uint32_t location,
1511 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001512
1513 // Returns the location of the instruction(s) containing the offset.
1514 uint32_t GetOffsetLocation() const;
1515
1516 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001517 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001518
1519 private:
1520 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001521 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001522 // Helper for the above.
1523 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1524
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001525 uint32_t old_location_; // Offset into assembler buffer in bytes.
1526 uint32_t location_; // Offset into assembler buffer in bytes.
1527 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001528
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001529 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1530 // FPU condition code. Destination register in literals.
1531 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1532 // Base register in literals (ZERO on R6).
1533 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001534
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001535 Type type_; // Current type of the branch.
1536 Type old_type_; // Initial type of the branch.
1537
1538 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1539 // kUnfilledDelaySlot if none but fillable or
1540 // kUnfillableDelaySlot if none and unfillable
1541 // (the latter is only used for unconditional R2
1542 // branches).
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001543
1544 MipsLabel* patcher_label_; // Patcher label for the instruction in the delay slot.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001545 };
1546 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1547 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1548
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001549 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1550 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1551 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1552 uint32_t EmitI26(int opcode, uint32_t imm26);
1553 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1554 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001555 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1556 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
Lena Djokic0758ae72017-05-23 11:06:23 +02001557 uint32_t EmitMsa3R(int operation,
1558 int df,
1559 VectorRegister wt,
1560 VectorRegister ws,
1561 VectorRegister wd,
1562 int minor_opcode);
1563 uint32_t EmitMsaBIT(int operation,
1564 int df_m,
1565 VectorRegister ws,
1566 VectorRegister wd,
1567 int minor_opcode);
1568 uint32_t EmitMsaELM(int operation,
1569 int df_n,
1570 VectorRegister ws,
1571 VectorRegister wd,
1572 int minor_opcode);
1573 uint32_t EmitMsaMI10(int s10, Register rs, VectorRegister wd, int minor_opcode, int df);
1574 uint32_t EmitMsaI10(int operation, int df, int i10, VectorRegister wd, int minor_opcode);
1575 uint32_t EmitMsa2R(int operation, int df, VectorRegister ws, VectorRegister wd, int minor_opcode);
1576 uint32_t EmitMsa2RF(int operation,
1577 int df,
1578 VectorRegister ws,
1579 VectorRegister wd,
1580 int minor_opcode);
jeffhao7fbee072012-08-24 17:56:54 -07001581
Alexey Frunze0cab6562017-07-25 15:19:36 -07001582 void Buncond(MipsLabel* label, bool is_r6, bool is_bare);
1583 void Bcond(MipsLabel* label,
1584 bool is_r6,
1585 bool is_bare,
1586 BranchCondition condition,
1587 Register lhs,
1588 Register rhs = ZERO);
1589 void Call(MipsLabel* label, bool is_r6, bool is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001590 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001591
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001592 // Various helpers for branch delay slot management.
1593 void DsFsmInstr(uint32_t instruction,
1594 uint32_t gpr_outs_mask,
1595 uint32_t gpr_ins_mask,
1596 uint32_t fpr_outs_mask,
1597 uint32_t fpr_ins_mask,
1598 uint32_t cc_outs_mask,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001599 uint32_t cc_ins_mask,
1600 MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001601 void DsFsmInstrNop(uint32_t instruction);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001602 void DsFsmInstrRrr(uint32_t instruction,
1603 Register out,
1604 Register in1,
1605 Register in2,
1606 MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001607 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1608 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1609 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07001610 void DsFsmInstrFffr(uint32_t instruction, FRegister in1_out, FRegister in2, Register in3);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001611 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1612 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1613 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1614 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1615 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1616 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1617 void DsFsmLabel();
1618 void DsFsmCommitLabel();
1619 void DsFsmDropLabel();
1620 void MoveInstructionToDelaySlot(Branch& branch);
1621 bool CanExchangeWithSlt(Register rs, Register rt) const;
1622 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1623 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1624
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001625 Branch* GetBranch(uint32_t branch_id);
1626 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001627 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1628 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001629 void BindRelativeToPrecedingBranch(MipsLabel* label,
1630 uint32_t prev_branch_id_plus_one,
1631 uint32_t position);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001632
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001633 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001634 void ReserveJumpTableSpace();
1635 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001636 void PromoteBranches();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001637 void EmitBranch(uint32_t branch_id);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001638 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001639 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001640
1641 // Emits exception block.
1642 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1643
Lena Djokic0758ae72017-05-23 11:06:23 +02001644 bool HasMsa() const {
1645 return has_msa_;
1646 }
1647
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001648 bool IsR6() const {
1649 if (isa_features_ != nullptr) {
1650 return isa_features_->IsR6();
1651 } else {
1652 return false;
1653 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001654 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001655
1656 bool Is32BitFPU() const {
1657 if (isa_features_ != nullptr) {
1658 return isa_features_->Is32BitFloatingPoint();
1659 } else {
1660 return true;
1661 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001662 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001663
1664 // List of exception blocks to generate at the end of the code cache.
1665 std::vector<MipsExceptionSlowPath> exception_blocks_;
1666
1667 std::vector<Branch> branches_;
1668
1669 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1670 bool overwriting_;
1671 // The current overwrite location.
1672 uint32_t overwrite_location_;
1673
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001674 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1675 bool reordering_;
1676 // Information about the last instruction that may be used to fill a branch delay slot.
1677 DelaySlot delay_slot_;
1678 // Delay slot FSM state.
1679 DsFsmState ds_fsm_state_;
1680 // PC of the current labeled target instruction.
1681 uint32_t ds_fsm_target_pc_;
1682 // PCs of labeled target instructions.
1683 std::vector<uint32_t> ds_fsm_target_pcs_;
1684
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001685 // Use std::deque<> for literal labels to allow insertions at the end
1686 // without invalidating pointers and references to existing elements.
1687 ArenaDeque<Literal> literals_;
1688
Alexey Frunze96b66822016-09-10 02:32:44 -07001689 // Jump table list.
1690 ArenaDeque<JumpTable> jump_tables_;
1691
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001692 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1693 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1694 // that PC (from NAL) points to.
1695 MipsLabel pc_rel_base_label_;
1696
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001697 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001698 uint32_t last_position_adjustment_;
1699 uint32_t last_old_position_;
1700 uint32_t last_branch_id_;
1701
Lena Djokic0758ae72017-05-23 11:06:23 +02001702 const bool has_msa_;
1703
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001704 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001705
jeffhao7fbee072012-08-24 17:56:54 -07001706 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1707};
1708
jeffhao7fbee072012-08-24 17:56:54 -07001709} // namespace mips
1710} // namespace art
1711
Ian Rogers166db042013-07-26 12:05:57 -07001712#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_