blob: 1c3097ac584ce37746824050dc8dc3cadaba18b1 [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 Markoe764d2e2017-10-05 14:35:55 +0100195 explicit MipsAssembler(ArenaAllocator* allocator,
Vladimir Marko93205e32016-04-13 11:59:46 +0100196 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100197 : Assembler(allocator),
Vladimir Marko93205e32016-04-13 11:59:46 +0100198 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),
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100203 literals_(allocator->Adapter(kArenaAllocAssembler)),
204 jump_tables_(allocator->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);
Lena Djokic72aba712017-10-30 15:47:20 +0100497 void Asub_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
498 void Asub_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
499 void Asub_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
500 void Asub_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
501 void Asub_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
502 void Asub_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
503 void Asub_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
504 void Asub_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
Lena Djokic0758ae72017-05-23 11:06:23 +0200505 void MulvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
506 void MulvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
507 void MulvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
508 void MulvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
509 void Div_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
510 void Div_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
511 void Div_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
512 void Div_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
513 void Div_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
514 void Div_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
515 void Div_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
516 void Div_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
517 void Mod_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
518 void Mod_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
519 void Mod_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
520 void Mod_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
521 void Mod_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
522 void Mod_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
523 void Mod_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
524 void Mod_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
525 void Add_aB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
526 void Add_aH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
527 void Add_aW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
528 void Add_aD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
529 void Ave_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
530 void Ave_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
531 void Ave_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
532 void Ave_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
533 void Ave_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
534 void Ave_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
535 void Ave_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
536 void Ave_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
537 void Aver_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
538 void Aver_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
539 void Aver_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
540 void Aver_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
541 void Aver_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
542 void Aver_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
543 void Aver_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
544 void Aver_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
545 void Max_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
546 void Max_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
547 void Max_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
548 void Max_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
549 void Max_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
550 void Max_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
551 void Max_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
552 void Max_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
553 void Min_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
554 void Min_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
555 void Min_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
556 void Min_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
557 void Min_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
558 void Min_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
559 void Min_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
560 void Min_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
561
562 void FaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
563 void FaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
564 void FsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
565 void FsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
566 void FmulW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
567 void FmulD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
568 void FdivW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
569 void FdivD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
570 void FmaxW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
571 void FmaxD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
572 void FminW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
573 void FminD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
574
575 void Ffint_sW(VectorRegister wd, VectorRegister ws);
576 void Ffint_sD(VectorRegister wd, VectorRegister ws);
577 void Ftint_sW(VectorRegister wd, VectorRegister ws);
578 void Ftint_sD(VectorRegister wd, VectorRegister ws);
579
580 void SllB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
581 void SllH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
582 void SllW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
583 void SllD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
584 void SraB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
585 void SraH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
586 void SraW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
587 void SraD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
588 void SrlB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
589 void SrlH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
590 void SrlW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
591 void SrlD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
592
593 // Immediate shift instructions, where shamtN denotes shift amount (must be between 0 and 2^N-1).
594 void SlliB(VectorRegister wd, VectorRegister ws, int shamt3);
595 void SlliH(VectorRegister wd, VectorRegister ws, int shamt4);
596 void SlliW(VectorRegister wd, VectorRegister ws, int shamt5);
597 void SlliD(VectorRegister wd, VectorRegister ws, int shamt6);
598 void SraiB(VectorRegister wd, VectorRegister ws, int shamt3);
599 void SraiH(VectorRegister wd, VectorRegister ws, int shamt4);
600 void SraiW(VectorRegister wd, VectorRegister ws, int shamt5);
601 void SraiD(VectorRegister wd, VectorRegister ws, int shamt6);
602 void SrliB(VectorRegister wd, VectorRegister ws, int shamt3);
603 void SrliH(VectorRegister wd, VectorRegister ws, int shamt4);
604 void SrliW(VectorRegister wd, VectorRegister ws, int shamt5);
605 void SrliD(VectorRegister wd, VectorRegister ws, int shamt6);
606
607 void MoveV(VectorRegister wd, VectorRegister ws);
608 void SplatiB(VectorRegister wd, VectorRegister ws, int n4);
609 void SplatiH(VectorRegister wd, VectorRegister ws, int n3);
610 void SplatiW(VectorRegister wd, VectorRegister ws, int n2);
611 void SplatiD(VectorRegister wd, VectorRegister ws, int n1);
Lena Djokic3309c012017-10-13 14:34:32 +0200612 void Copy_sB(Register rd, VectorRegister ws, int n4);
613 void Copy_sH(Register rd, VectorRegister ws, int n3);
614 void Copy_sW(Register rd, VectorRegister ws, int n2);
615 void Copy_uB(Register rd, VectorRegister ws, int n4);
616 void Copy_uH(Register rd, VectorRegister ws, int n3);
617 void InsertB(VectorRegister wd, Register rs, int n4);
618 void InsertH(VectorRegister wd, Register rs, int n3);
619 void InsertW(VectorRegister wd, Register rs, int n2);
Lena Djokic0758ae72017-05-23 11:06:23 +0200620 void FillB(VectorRegister wd, Register rs);
621 void FillH(VectorRegister wd, Register rs);
622 void FillW(VectorRegister wd, Register rs);
623
624 void LdiB(VectorRegister wd, int imm8);
625 void LdiH(VectorRegister wd, int imm10);
626 void LdiW(VectorRegister wd, int imm10);
627 void LdiD(VectorRegister wd, int imm10);
628 void LdB(VectorRegister wd, Register rs, int offset);
629 void LdH(VectorRegister wd, Register rs, int offset);
630 void LdW(VectorRegister wd, Register rs, int offset);
631 void LdD(VectorRegister wd, Register rs, int offset);
632 void StB(VectorRegister wd, Register rs, int offset);
633 void StH(VectorRegister wd, Register rs, int offset);
634 void StW(VectorRegister wd, Register rs, int offset);
635 void StD(VectorRegister wd, Register rs, int offset);
636
Lena Djokic3309c012017-10-13 14:34:32 +0200637 void IlvlB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
638 void IlvlH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
639 void IlvlW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
640 void IlvlD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
Lena Djokic0758ae72017-05-23 11:06:23 +0200641 void IlvrB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
642 void IlvrH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
643 void IlvrW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
644 void IlvrD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
Lena Djokic3309c012017-10-13 14:34:32 +0200645 void IlvevB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
646 void IlvevH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
647 void IlvevW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
648 void IlvevD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
649 void IlvodB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
650 void IlvodH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
651 void IlvodW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
652 void IlvodD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
Lena Djokic0758ae72017-05-23 11:06:23 +0200653
Lena Djokicb3d79e42017-07-25 11:20:52 +0200654 void MaddvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
655 void MaddvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
656 void MaddvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
657 void MaddvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
658 void MsubvB(VectorRegister wd, VectorRegister ws, VectorRegister wt);
659 void MsubvH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
660 void MsubvW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
661 void MsubvD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
662 void FmaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
663 void FmaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
664 void FmsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
665 void FmsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
666
Lena Djokic3309c012017-10-13 14:34:32 +0200667 void Hadd_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
668 void Hadd_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
669 void Hadd_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
670 void Hadd_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt);
671 void Hadd_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt);
672 void Hadd_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt);
673
Lena Djokic51765b02017-06-22 13:49:59 +0200674 // Helper for replicating floating point value in all destination elements.
675 void ReplicateFPToVectorRegister(VectorRegister dst, FRegister src, bool is_double);
676
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200677 // Higher level composite instructions.
678 void LoadConst32(Register rd, int32_t value);
679 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
680 void LoadDConst64(FRegister rd, int64_t value, Register temp);
681 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200682 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
683
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200684 void Bind(MipsLabel* label);
Alexey Frunze0cab6562017-07-25 15:19:36 -0700685 // When `is_bare` is false, the branches will promote to long (if the range
686 // of the individual branch instruction is insufficient) and the delay/
687 // forbidden slots will be taken care of.
688 // Use `is_bare = false` when the branch target may be out of reach of the
689 // individual branch instruction. IOW, this is for general purpose use.
690 //
691 // When `is_bare` is true, just the branch instructions will be generated
692 // leaving delay/forbidden slot filling up to the caller and the branches
693 // won't promote to long if the range is insufficient (you'll get a
694 // compilation error when the range is exceeded).
695 // Use `is_bare = true` when the branch target is known to be within reach
696 // of the individual branch instruction. This is intended for small local
697 // optimizations around delay/forbidden slots.
698 // Also prefer using `is_bare = true` if the code near the branch is to be
699 // patched or analyzed at run time (e.g. introspection) to
700 // - show the intent and
701 // - fail during compilation rather than during patching/execution if the
702 // bare branch range is insufficent but the code size and layout are
703 // expected to remain unchanged
704 //
705 // R2 branches with delay slots that are also available on R6.
706 // On R6 when `is_bare` is false these convert to equivalent R6 compact
707 // branches (to reduce code size). On R2 or when `is_bare` is true they
708 // remain R2 branches with delay slots.
709 void B(MipsLabel* label, bool is_bare = false);
710 void Bal(MipsLabel* label, bool is_bare = false);
711 void Beq(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
712 void Bne(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
713 void Beqz(Register rt, MipsLabel* label, bool is_bare = false);
714 void Bnez(Register rt, MipsLabel* label, bool is_bare = false);
715 void Bltz(Register rt, MipsLabel* label, bool is_bare = false);
716 void Bgez(Register rt, MipsLabel* label, bool is_bare = false);
717 void Blez(Register rt, MipsLabel* label, bool is_bare = false);
718 void Bgtz(Register rt, MipsLabel* label, bool is_bare = false);
719 void Blt(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
720 void Bge(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
721 void Bltu(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
722 void Bgeu(Register rs, Register rt, MipsLabel* label, bool is_bare = false);
723 // R2-only branches with delay slots.
724 void Bc1f(MipsLabel* label, bool is_bare = false); // R2
725 void Bc1f(int cc, MipsLabel* label, bool is_bare = false); // R2
726 void Bc1t(MipsLabel* label, bool is_bare = false); // R2
727 void Bc1t(int cc, MipsLabel* label, bool is_bare = false); // R2
728 // R6-only compact branches without delay/forbidden slots.
729 void Bc(MipsLabel* label, bool is_bare = false); // R6
730 void Balc(MipsLabel* label, bool is_bare = false); // R6
731 // R6-only compact branches with forbidden slots.
732 void Beqc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
733 void Bnec(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
734 void Beqzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
735 void Bnezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
736 void Bltzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
737 void Bgezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
738 void Blezc(Register rt, MipsLabel* label, bool is_bare = false); // R6
739 void Bgtzc(Register rt, MipsLabel* label, bool is_bare = false); // R6
740 void Bltc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
741 void Bgec(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
742 void Bltuc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
743 void Bgeuc(Register rs, Register rt, MipsLabel* label, bool is_bare = false); // R6
744 // R6-only branches with delay slots.
745 void Bc1eqz(FRegister ft, MipsLabel* label, bool is_bare = false); // R6
746 void Bc1nez(FRegister ft, MipsLabel* label, bool is_bare = false); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700747
748 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700749 void AdjustBaseAndOffset(Register& base,
750 int32_t& offset,
751 bool is_doubleword,
752 bool is_float = false);
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200753 void AdjustBaseOffsetAndElementSizeShift(Register& base,
754 int32_t& offset,
755 int& element_size_shift);
Alexey Frunze2923db72016-08-20 01:55:47 -0700756
757 private:
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100758 // This will be used as an argument for loads/stores
759 // when there is no need for implicit null checks.
Alexey Frunze2923db72016-08-20 01:55:47 -0700760 struct NoImplicitNullChecker {
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100761 void operator()() const {}
Alexey Frunze2923db72016-08-20 01:55:47 -0700762 };
763
764 public:
765 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunzef58b2482016-09-02 22:14:06 -0700766 void StoreConstToOffset(StoreOperandType type,
767 int64_t value,
768 Register base,
769 int32_t offset,
770 Register temp,
771 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
772 // We permit `base` and `temp` to coincide (however, we check that neither is AT),
773 // in which case the `base` register may be overwritten in the process.
Alexey Frunze2923db72016-08-20 01:55:47 -0700774 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 -0700775 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
Alexey Frunze2923db72016-08-20 01:55:47 -0700776 uint32_t low = Low32Bits(value);
777 uint32_t high = High32Bits(value);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700778 Register reg;
779 // If the adjustment left `base` unchanged and equal to `temp`, we can't use `temp`
780 // to load and hold the value but we can use AT instead as AT hasn't been used yet.
781 // Otherwise, `temp` can be used for the value. And if `temp` is the same as the
782 // original `base` (that is, `base` prior to the adjustment), the original `base`
783 // register will be overwritten.
784 if (base == temp) {
785 temp = AT;
Alexey Frunze2923db72016-08-20 01:55:47 -0700786 }
Alexey Frunzef58b2482016-09-02 22:14:06 -0700787 if (low == 0) {
788 reg = ZERO;
Alexey Frunze2923db72016-08-20 01:55:47 -0700789 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -0700790 reg = temp;
791 LoadConst32(reg, low);
792 }
793 switch (type) {
794 case kStoreByte:
795 Sb(reg, base, offset);
796 break;
797 case kStoreHalfword:
798 Sh(reg, base, offset);
799 break;
800 case kStoreWord:
801 Sw(reg, base, offset);
802 break;
803 case kStoreDoubleword:
804 Sw(reg, base, offset);
805 null_checker();
806 if (high == 0) {
807 reg = ZERO;
808 } else {
809 reg = temp;
810 if (high != low) {
811 LoadConst32(reg, high);
812 }
813 }
814 Sw(reg, base, offset + kMipsWordSize);
815 break;
816 default:
817 LOG(FATAL) << "UNREACHABLE";
818 }
819 if (type != kStoreDoubleword) {
820 null_checker();
Alexey Frunze2923db72016-08-20 01:55:47 -0700821 }
822 }
823
824 template <typename ImplicitNullChecker = NoImplicitNullChecker>
825 void LoadFromOffset(LoadOperandType type,
826 Register reg,
827 Register base,
828 int32_t offset,
829 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
830 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
831 switch (type) {
832 case kLoadSignedByte:
833 Lb(reg, base, offset);
834 break;
835 case kLoadUnsignedByte:
836 Lbu(reg, base, offset);
837 break;
838 case kLoadSignedHalfword:
839 Lh(reg, base, offset);
840 break;
841 case kLoadUnsignedHalfword:
842 Lhu(reg, base, offset);
843 break;
844 case kLoadWord:
845 Lw(reg, base, offset);
846 break;
847 case kLoadDoubleword:
848 if (reg == base) {
849 // This will clobber the base when loading the lower register. Since we have to load the
850 // higher register as well, this will fail. Solution: reverse the order.
851 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
852 null_checker();
853 Lw(reg, base, offset);
854 } else {
855 Lw(reg, base, offset);
856 null_checker();
857 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
858 }
859 break;
860 default:
861 LOG(FATAL) << "UNREACHABLE";
862 }
863 if (type != kLoadDoubleword) {
864 null_checker();
865 }
866 }
867
868 template <typename ImplicitNullChecker = NoImplicitNullChecker>
869 void LoadSFromOffset(FRegister reg,
870 Register base,
871 int32_t offset,
872 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
873 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
874 Lwc1(reg, base, offset);
875 null_checker();
876 }
877
878 template <typename ImplicitNullChecker = NoImplicitNullChecker>
879 void LoadDFromOffset(FRegister reg,
880 Register base,
881 int32_t offset,
882 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
883 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
884 if (IsAligned<kMipsDoublewordSize>(offset)) {
885 Ldc1(reg, base, offset);
886 null_checker();
887 } else {
888 if (Is32BitFPU()) {
889 Lwc1(reg, base, offset);
890 null_checker();
891 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
892 } else {
893 // 64-bit FPU.
894 Lwc1(reg, base, offset);
895 null_checker();
896 Lw(T8, base, offset + kMipsWordSize);
897 Mthc1(T8, reg);
898 }
899 }
900 }
901
902 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200903 void LoadQFromOffset(FRegister reg,
904 Register base,
905 int32_t offset,
906 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
907 int element_size_shift = -1;
908 AdjustBaseOffsetAndElementSizeShift(base, offset, element_size_shift);
909 switch (element_size_shift) {
910 case TIMES_1: LdB(static_cast<VectorRegister>(reg), base, offset); break;
911 case TIMES_2: LdH(static_cast<VectorRegister>(reg), base, offset); break;
912 case TIMES_4: LdW(static_cast<VectorRegister>(reg), base, offset); break;
913 case TIMES_8: LdD(static_cast<VectorRegister>(reg), base, offset); break;
914 default:
915 LOG(FATAL) << "UNREACHABLE";
916 }
917 null_checker();
918 }
919
920 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunze2923db72016-08-20 01:55:47 -0700921 void StoreToOffset(StoreOperandType type,
922 Register reg,
923 Register base,
924 int32_t offset,
925 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
926 // Must not use AT as `reg`, so as not to overwrite the value being stored
927 // with the adjusted `base`.
928 CHECK_NE(reg, AT);
929 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
930 switch (type) {
931 case kStoreByte:
932 Sb(reg, base, offset);
933 break;
934 case kStoreHalfword:
935 Sh(reg, base, offset);
936 break;
937 case kStoreWord:
938 Sw(reg, base, offset);
939 break;
940 case kStoreDoubleword:
941 CHECK_NE(reg, base);
942 CHECK_NE(static_cast<Register>(reg + 1), base);
943 Sw(reg, base, offset);
944 null_checker();
945 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
946 break;
947 default:
948 LOG(FATAL) << "UNREACHABLE";
949 }
950 if (type != kStoreDoubleword) {
951 null_checker();
952 }
953 }
954
955 template <typename ImplicitNullChecker = NoImplicitNullChecker>
956 void StoreSToOffset(FRegister reg,
957 Register base,
958 int32_t offset,
959 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
960 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
961 Swc1(reg, base, offset);
962 null_checker();
963 }
964
965 template <typename ImplicitNullChecker = NoImplicitNullChecker>
966 void StoreDToOffset(FRegister reg,
967 Register base,
968 int32_t offset,
969 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
970 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
971 if (IsAligned<kMipsDoublewordSize>(offset)) {
972 Sdc1(reg, base, offset);
973 null_checker();
974 } else {
975 if (Is32BitFPU()) {
976 Swc1(reg, base, offset);
977 null_checker();
978 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
979 } else {
980 // 64-bit FPU.
981 Mfhc1(T8, reg);
982 Swc1(reg, base, offset);
983 null_checker();
984 Sw(T8, base, offset + kMipsWordSize);
985 }
986 }
987 }
988
Lena Djokic2e0a7e52017-07-06 11:55:24 +0200989 template <typename ImplicitNullChecker = NoImplicitNullChecker>
990 void StoreQToOffset(FRegister reg,
991 Register base,
992 int32_t offset,
993 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
994 int element_size_shift = -1;
995 AdjustBaseOffsetAndElementSizeShift(base, offset, element_size_shift);
996 switch (element_size_shift) {
997 case TIMES_1: StB(static_cast<VectorRegister>(reg), base, offset); break;
998 case TIMES_2: StH(static_cast<VectorRegister>(reg), base, offset); break;
999 case TIMES_4: StW(static_cast<VectorRegister>(reg), base, offset); break;
1000 case TIMES_8: StD(static_cast<VectorRegister>(reg), base, offset); break;
1001 default:
1002 LOG(FATAL) << "UNREACHABLE";
1003 }
1004 null_checker();
1005 }
1006
jeffhao7fbee072012-08-24 17:56:54 -07001007 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
1008 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001009 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
Lena Djokic2e0a7e52017-07-06 11:55:24 +02001010 void LoadQFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -07001011 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +00001012 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001013 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
Lena Djokic2e0a7e52017-07-06 11:55:24 +02001014 void StoreQToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -07001015
jeffhao7fbee072012-08-24 17:56:54 -07001016 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001017 void Emit(uint32_t value);
1018
1019 // Push/pop composite routines.
1020 void Push(Register rs);
1021 void Pop(Register rd);
1022 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -07001023
Alexey Frunzec061de12017-02-14 13:27:23 -08001024 //
1025 // Heap poisoning.
1026 //
1027
1028 // Poison a heap reference contained in `src` and store it in `dst`.
1029 void PoisonHeapReference(Register dst, Register src) {
1030 // dst = -src.
1031 Subu(dst, ZERO, src);
1032 }
1033 // Poison a heap reference contained in `reg`.
1034 void PoisonHeapReference(Register reg) {
1035 // reg = -reg.
1036 PoisonHeapReference(reg, reg);
1037 }
1038 // Unpoison a heap reference contained in `reg`.
1039 void UnpoisonHeapReference(Register reg) {
1040 // reg = -reg.
1041 Subu(reg, ZERO, reg);
1042 }
1043 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
1044 void MaybePoisonHeapReference(Register reg) {
1045 if (kPoisonHeapReferences) {
1046 PoisonHeapReference(reg);
1047 }
1048 }
1049 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
1050 void MaybeUnpoisonHeapReference(Register reg) {
1051 if (kPoisonHeapReferences) {
1052 UnpoisonHeapReference(reg);
1053 }
1054 }
1055
Andreas Gampe85b62f22015-09-09 13:15:38 -07001056 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001057 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -07001058 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001059 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
1060 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -07001061 }
1062
Igor Murashkinae7ff922016-10-06 14:59:19 -07001063 // Don't warn about a different virtual Bind/Jump in the base class.
1064 using JNIBase::Bind;
1065 using JNIBase::Jump;
1066
1067 // Create a new label that can be used with Jump/Bind calls.
1068 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
1069 LOG(FATAL) << "Not implemented on MIPS32";
1070 UNREACHABLE();
1071 }
1072 // Emit an unconditional jump to the label.
1073 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
1074 LOG(FATAL) << "Not implemented on MIPS32";
1075 UNREACHABLE();
1076 }
1077 // Emit a conditional jump to the label by applying a unary condition test to the register.
1078 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
1079 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
1080 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
1081 LOG(FATAL) << "Not implemented on MIPS32";
1082 UNREACHABLE();
1083 }
1084
1085 // Code at this offset will serve as the target for the Jump call.
1086 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
1087 LOG(FATAL) << "Not implemented on MIPS32";
1088 UNREACHABLE();
1089 }
1090
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001091 // Create a new literal with a given value.
1092 // NOTE: Force the template parameter to be explicitly specified.
1093 template <typename T>
1094 Literal* NewLiteral(typename Identity<T>::type value) {
1095 static_assert(std::is_integral<T>::value, "T must be an integral type.");
1096 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
1097 }
1098
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07001099 // Load label address using PC-relative addressing.
1100 // To be used with data labels in the literal / jump table area only and not
1101 // with regular code labels.
1102 //
1103 // For R6 base_reg must be ZERO.
1104 //
1105 // On R2 there are two possible uses w.r.t. base_reg:
1106 //
1107 // - base_reg = ZERO:
1108 // The NAL instruction will be generated as part of the load and it will
1109 // clobber the RA register.
1110 //
1111 // - base_reg != ZERO:
1112 // The RA-clobbering NAL instruction won't be generated as part of the load.
1113 // The label pc_rel_base_label_ must be bound (with BindPcRelBaseLabel())
1114 // and base_reg must hold the address of the label. Example:
1115 // __ Nal();
1116 // __ Move(S3, RA);
1117 // __ BindPcRelBaseLabel(); // S3 holds the address of pc_rel_base_label_.
1118 // __ LoadLabelAddress(A0, S3, label1);
1119 // __ LoadLabelAddress(A1, S3, label2);
1120 // __ LoadLiteral(V0, S3, literal1);
1121 // __ LoadLiteral(V1, S3, literal2);
Alexey Frunze96b66822016-09-10 02:32:44 -07001122 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
1123
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001124 // Create a new literal with the given data.
1125 Literal* NewLiteral(size_t size, const uint8_t* data);
1126
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07001127 // Load literal using PC-relative addressing.
1128 // See the above comments for LoadLabelAddress() on the value of base_reg.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001129 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
1130
Alexey Frunze96b66822016-09-10 02:32:44 -07001131 // Create a jump table for the given labels that will be emitted when finalizing.
1132 // When the table is emitted, offsets will be relative to the location of the table.
1133 // The table location is determined by the location of its label (the label precedes
1134 // the table data) and should be loaded using LoadLabelAddress().
1135 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
1136
jeffhao7fbee072012-08-24 17:56:54 -07001137 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001138 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -07001139 //
1140
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001141 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001142 void BuildFrame(size_t frame_size,
1143 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +01001144 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001145 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001146
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001147 // Emit code that will remove an activation from the stack.
Roland Levillain0d127e12017-07-05 17:01:11 +01001148 void RemoveFrame(size_t frame_size,
1149 ArrayRef<const ManagedRegister> callee_save_regs,
1150 bool may_suspend) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001151
Ian Rogersdd7624d2014-03-14 17:43:00 -07001152 void IncreaseFrameSize(size_t adjust) OVERRIDE;
1153 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001154
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001155 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001156 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
1157 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
1158 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001159
Ian Rogersdd7624d2014-03-14 17:43:00 -07001160 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001161
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001162 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
1163 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001164 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001165
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001166 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001167
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001168 void StoreSpanning(FrameOffset dest,
1169 ManagedRegister msrc,
1170 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001171 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001172
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001173 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001174 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001175
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001176 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001177
Mathieu Chartiere401d142015-04-22 13:56:20 -07001178 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001179
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001180 void LoadRef(ManagedRegister mdest,
1181 ManagedRegister base,
1182 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +01001183 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001184
Ian Rogersdd7624d2014-03-14 17:43:00 -07001185 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001186
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001187 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001188
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001189 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001190 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001191
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001192 void CopyRawPtrFromThread(FrameOffset fr_offs,
1193 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001194 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001195
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001196 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
1197 FrameOffset fr_offs,
1198 ManagedRegister mscratch) OVERRIDE;
1199
Ian Rogersdd7624d2014-03-14 17:43:00 -07001200 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001201
Ian Rogersdd7624d2014-03-14 17:43:00 -07001202 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001203
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001204 void Copy(FrameOffset dest,
1205 ManagedRegister src_base,
1206 Offset src_offset,
1207 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001208 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001209
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001210 void Copy(ManagedRegister dest_base,
1211 Offset dest_offset,
1212 FrameOffset src,
1213 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -07001214 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001215
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001216 void Copy(FrameOffset dest,
1217 FrameOffset src_base,
1218 Offset src_offset,
1219 ManagedRegister mscratch,
1220 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001221
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001222 void Copy(ManagedRegister dest,
1223 Offset dest_offset,
1224 ManagedRegister src,
1225 Offset src_offset,
1226 ManagedRegister mscratch,
1227 size_t size) OVERRIDE;
1228
1229 void Copy(FrameOffset dest,
1230 Offset dest_offset,
1231 FrameOffset src,
1232 Offset src_offset,
1233 ManagedRegister mscratch,
1234 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001235
Ian Rogersdd7624d2014-03-14 17:43:00 -07001236 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001237
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001238 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001239 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001240
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001241 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001242 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001243
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001244 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -07001245 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
1246 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001247
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001248 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -07001249 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001250 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001251 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001252 void CreateHandleScopeEntry(ManagedRegister out_reg,
1253 FrameOffset handlescope_offset,
1254 ManagedRegister in_reg,
1255 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001256
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001257 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -07001258 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001259 void CreateHandleScopeEntry(FrameOffset out_off,
1260 FrameOffset handlescope_offset,
1261 ManagedRegister mscratch,
1262 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001263
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001264 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001265 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001266
1267 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
1268 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001269 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
1270 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001271
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001272 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -07001273 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
1274 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001275 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001276
jeffhao7fbee072012-08-24 17:56:54 -07001277 // Generate code to check if Thread::Current()->exception_ is non-null
1278 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001279 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -07001280
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001281 // Emit slow paths queued during assembly and promote short branches to long if needed.
1282 void FinalizeCode() OVERRIDE;
1283
1284 // Emit branches and finalize all instructions.
1285 void FinalizeInstructions(const MemoryRegion& region);
1286
1287 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
1288 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001289 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001290
1291 // Get the final position of a label after local fixup based on the old position
1292 // recorded before FinalizeCode().
1293 uint32_t GetAdjustedPosition(uint32_t old_position);
1294
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001295 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
1296 // reading the PC value into a general-purpose register with the NAL instruction and then loading
1297 // literals through this base register. The code generator calls this method (at most once per
1298 // method being compiled) to bind a label to the location for which the PC value is acquired.
1299 // The assembler then computes literal offsets relative to this label.
1300 void BindPcRelBaseLabel();
1301
Alexey Frunze06a46c42016-07-19 15:00:40 -07001302 // Returns the location of the label bound with BindPcRelBaseLabel().
1303 uint32_t GetPcRelBaseLabelLocation() const;
1304
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001305 // Note that PC-relative literal loads are handled as pseudo branches because they need very
1306 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
1307 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001308 enum BranchCondition {
1309 kCondLT,
1310 kCondGE,
1311 kCondLE,
1312 kCondGT,
1313 kCondLTZ,
1314 kCondGEZ,
1315 kCondLEZ,
1316 kCondGTZ,
1317 kCondEQ,
1318 kCondNE,
1319 kCondEQZ,
1320 kCondNEZ,
1321 kCondLTU,
1322 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001323 kCondF, // Floating-point predicate false.
1324 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001325 kUncond,
1326 };
1327 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
1328
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001329 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
1330 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
1331 // Returns the last state, which may be useful for temporary enabling/disabling of
1332 // reordering.
1333 bool SetReorder(bool enable);
1334
jeffhao7fbee072012-08-24 17:56:54 -07001335 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001336 // Description of the last instruction in terms of input and output registers.
1337 // Used to make the decision of moving the instruction into a delay slot.
1338 struct DelaySlot {
1339 DelaySlot();
1340 // Encoded instruction that may be used to fill the delay slot or 0
1341 // (0 conveniently represents NOP).
1342 uint32_t instruction_;
1343 // Mask of output GPRs for the instruction.
1344 uint32_t gpr_outs_mask_;
1345 // Mask of input GPRs for the instruction.
1346 uint32_t gpr_ins_mask_;
1347 // Mask of output FPRs for the instruction.
1348 uint32_t fpr_outs_mask_;
1349 // Mask of input FPRs for the instruction.
1350 uint32_t fpr_ins_mask_;
1351 // Mask of output FPU condition code flags for the instruction.
1352 uint32_t cc_outs_mask_;
1353 // Mask of input FPU condition code flags for the instruction.
1354 uint32_t cc_ins_mask_;
1355 // Branches never operate on the LO and HI registers, hence there's
1356 // no mask for LO and HI.
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001357
1358 // Label for patchable instructions to allow moving them into delay slots.
1359 MipsLabel* patcher_label_;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001360 };
1361
1362 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
1363 // upon every new instruction and label generated. The FSM detects instructions
1364 // suitable for delay slots and immediately preceded with labels. These are target
1365 // instructions for branches. If an unconditional R2 branch does not get its delay
1366 // slot filled with the immediately preceding instruction, it may instead get the
1367 // slot filled with the target instruction (the branch will need its offset
1368 // incremented past the target instruction). We call this "absorption". The FSM
1369 // records PCs of the target instructions suitable for this optimization.
1370 enum DsFsmState {
1371 kExpectingLabel,
1372 kExpectingInstruction,
1373 kExpectingCommit
1374 };
1375 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
1376
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001377 class Branch {
1378 public:
1379 enum Type {
Alexey Frunze0cab6562017-07-25 15:19:36 -07001380 // R2 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001381 kUncondBranch,
1382 kCondBranch,
1383 kCall,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001384 // R2 short branches (can't be promoted to long), delay slots filled manually.
1385 kBareUncondBranch,
1386 kBareCondBranch,
1387 kBareCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001388 // R2 near label.
1389 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001390 // R2 near literal.
1391 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001392 // R2 long branches.
1393 kLongUncondBranch,
1394 kLongCondBranch,
1395 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001396 // R2 far label.
1397 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001398 // R2 far literal.
1399 kFarLiteral,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001400 // R6 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001401 kR6UncondBranch,
1402 kR6CondBranch,
1403 kR6Call,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001404 // R6 short branches (can't be promoted to long), forbidden/delay slots filled manually.
1405 kR6BareUncondBranch,
1406 kR6BareCondBranch,
1407 kR6BareCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001408 // R6 near label.
1409 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001410 // R6 near literal.
1411 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001412 // R6 long branches.
1413 kR6LongUncondBranch,
1414 kR6LongCondBranch,
1415 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001416 // R6 far label.
1417 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001418 // R6 far literal.
1419 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001420 };
1421 // Bit sizes of offsets defined as enums to minimize chance of typos.
1422 enum OffsetBits {
1423 kOffset16 = 16,
1424 kOffset18 = 18,
1425 kOffset21 = 21,
1426 kOffset23 = 23,
1427 kOffset28 = 28,
1428 kOffset32 = 32,
1429 };
1430
1431 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1432 static constexpr int32_t kMaxBranchLength = 32;
1433 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001434 // The following two instruction encodings can never legally occur in branch delay
1435 // slots and are used as markers.
1436 //
1437 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1438 // instruction to fill its delay slot (the latter is only possible with unconditional
1439 // R2 branches and is termed here as "absorption").
1440 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1441 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1442 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1443 // absorption of the target instruction when reordering is disabled.
1444 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001445
1446 struct BranchInfo {
1447 // Branch length as a number of 4-byte-long instructions.
1448 uint32_t length;
1449 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1450 // PC-relative offset (or its most significant 16-bit half, which goes first).
1451 uint32_t instr_offset;
1452 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1453 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1454 // instructions) from the instruction containing the offset.
1455 uint32_t pc_org;
1456 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
Alexey Frunze0cab6562017-07-25 15:19:36 -07001457 // and kR6BareCondBranch are an exception: use kOffset23 for beqzc/bnezc).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001458 OffsetBits offset_size;
1459 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1460 // count.
1461 int offset_shift;
1462 };
1463 static const BranchInfo branch_info_[/* Type */];
1464
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001465 // Unconditional branch or call.
Alexey Frunze0cab6562017-07-25 15:19:36 -07001466 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call, bool is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001467 // Conditional branch.
1468 Branch(bool is_r6,
1469 uint32_t location,
1470 uint32_t target,
1471 BranchCondition condition,
1472 Register lhs_reg,
Alexey Frunze0cab6562017-07-25 15:19:36 -07001473 Register rhs_reg,
1474 bool is_bare);
Alexey Frunze96b66822016-09-10 02:32:44 -07001475 // Label address (in literal area) or literal.
1476 Branch(bool is_r6,
1477 uint32_t location,
1478 Register dest_reg,
1479 Register base_reg,
1480 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001481
1482 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1483 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1484 // So, we need a way to identify such branches in order to emit no instructions for them
1485 // or change them to unconditional.
1486 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1487 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1488
1489 static BranchCondition OppositeCondition(BranchCondition cond);
1490
1491 Type GetType() const;
1492 BranchCondition GetCondition() const;
1493 Register GetLeftRegister() const;
1494 Register GetRightRegister() const;
1495 uint32_t GetTarget() const;
1496 uint32_t GetLocation() const;
1497 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001498 uint32_t GetPrecedingInstructionLength(Type type) const;
1499 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001500 uint32_t GetLength() const;
1501 uint32_t GetOldLength() const;
1502 uint32_t GetSize() const;
1503 uint32_t GetOldSize() const;
1504 uint32_t GetEndLocation() const;
1505 uint32_t GetOldEndLocation() const;
Alexey Frunze0cab6562017-07-25 15:19:36 -07001506 bool IsBare() const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001507 bool IsLong() const;
1508 bool IsResolved() const;
1509
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001510 // Various helpers for branch delay slot management.
1511 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001512 void SetDelayedInstruction(uint32_t instruction, MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001513 uint32_t GetDelayedInstruction() const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001514 MipsLabel* GetPatcherLabel() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001515 void DecrementLocations();
1516
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001517 // Returns the bit size of the signed offset that the branch instruction can handle.
1518 OffsetBits GetOffsetSize() const;
1519
1520 // Calculates the distance between two byte locations in the assembler buffer and
1521 // returns the number of bits needed to represent the distance as a signed integer.
1522 //
1523 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1524 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1525 //
1526 // Composite branches (made of several instructions) with longer reach have 32-bit
1527 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001528 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1529 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1530 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1531 // Consider the following implementation of a long unconditional branch, for
1532 // example:
1533 //
1534 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1535 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1536 //
1537 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1538 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1539 // due to sign extension. This must be compensated for by incrementing offset_31_16
1540 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1541 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1542 // Therefore, the long branch range is something like from PC - 0x80000000 to
1543 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001544 //
1545 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1546 // case with the addiu instruction and a 16 bit offset.
1547 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1548
1549 // Resolve a branch when the target is known.
1550 void Resolve(uint32_t target);
1551
1552 // Relocate a branch by a given delta if needed due to expansion of this or another
1553 // branch at a given location by this delta (just changes location_ and target_).
1554 void Relocate(uint32_t expand_location, uint32_t delta);
1555
1556 // If the branch is short, changes its type to long.
1557 void PromoteToLong();
1558
1559 // If necessary, updates the type by promoting a short branch to a long branch
1560 // based on the branch location and target. Returns the amount (in bytes) by
1561 // which the branch size has increased.
1562 // max_short_distance caps the maximum distance between location_ and target_
1563 // that is allowed for short branches. This is for debugging/testing purposes.
1564 // max_short_distance = 0 forces all short branches to become long.
1565 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001566 uint32_t PromoteIfNeeded(uint32_t location,
1567 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001568
1569 // Returns the location of the instruction(s) containing the offset.
1570 uint32_t GetOffsetLocation() const;
1571
1572 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001573 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001574
1575 private:
1576 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001577 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001578 // Helper for the above.
1579 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1580
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001581 uint32_t old_location_; // Offset into assembler buffer in bytes.
1582 uint32_t location_; // Offset into assembler buffer in bytes.
1583 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001584
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001585 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1586 // FPU condition code. Destination register in literals.
1587 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1588 // Base register in literals (ZERO on R6).
1589 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001590
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001591 Type type_; // Current type of the branch.
1592 Type old_type_; // Initial type of the branch.
1593
1594 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1595 // kUnfilledDelaySlot if none but fillable or
1596 // kUnfillableDelaySlot if none and unfillable
1597 // (the latter is only used for unconditional R2
1598 // branches).
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001599
1600 MipsLabel* patcher_label_; // Patcher label for the instruction in the delay slot.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001601 };
1602 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1603 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1604
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001605 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1606 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1607 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1608 uint32_t EmitI26(int opcode, uint32_t imm26);
1609 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1610 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001611 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1612 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
Lena Djokic0758ae72017-05-23 11:06:23 +02001613 uint32_t EmitMsa3R(int operation,
1614 int df,
1615 VectorRegister wt,
1616 VectorRegister ws,
1617 VectorRegister wd,
1618 int minor_opcode);
1619 uint32_t EmitMsaBIT(int operation,
1620 int df_m,
1621 VectorRegister ws,
1622 VectorRegister wd,
1623 int minor_opcode);
1624 uint32_t EmitMsaELM(int operation,
1625 int df_n,
1626 VectorRegister ws,
1627 VectorRegister wd,
1628 int minor_opcode);
1629 uint32_t EmitMsaMI10(int s10, Register rs, VectorRegister wd, int minor_opcode, int df);
1630 uint32_t EmitMsaI10(int operation, int df, int i10, VectorRegister wd, int minor_opcode);
1631 uint32_t EmitMsa2R(int operation, int df, VectorRegister ws, VectorRegister wd, int minor_opcode);
1632 uint32_t EmitMsa2RF(int operation,
1633 int df,
1634 VectorRegister ws,
1635 VectorRegister wd,
1636 int minor_opcode);
jeffhao7fbee072012-08-24 17:56:54 -07001637
Alexey Frunze0cab6562017-07-25 15:19:36 -07001638 void Buncond(MipsLabel* label, bool is_r6, bool is_bare);
1639 void Bcond(MipsLabel* label,
1640 bool is_r6,
1641 bool is_bare,
1642 BranchCondition condition,
1643 Register lhs,
1644 Register rhs = ZERO);
1645 void Call(MipsLabel* label, bool is_r6, bool is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001646 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001647
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001648 // Various helpers for branch delay slot management.
1649 void DsFsmInstr(uint32_t instruction,
1650 uint32_t gpr_outs_mask,
1651 uint32_t gpr_ins_mask,
1652 uint32_t fpr_outs_mask,
1653 uint32_t fpr_ins_mask,
1654 uint32_t cc_outs_mask,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001655 uint32_t cc_ins_mask,
1656 MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001657 void DsFsmInstrNop(uint32_t instruction);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001658 void DsFsmInstrRrr(uint32_t instruction,
1659 Register out,
1660 Register in1,
1661 Register in2,
1662 MipsLabel* patcher_label = nullptr);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001663 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1664 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1665 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07001666 void DsFsmInstrFffr(uint32_t instruction, FRegister in1_out, FRegister in2, Register in3);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001667 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1668 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1669 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1670 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1671 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1672 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1673 void DsFsmLabel();
1674 void DsFsmCommitLabel();
1675 void DsFsmDropLabel();
1676 void MoveInstructionToDelaySlot(Branch& branch);
1677 bool CanExchangeWithSlt(Register rs, Register rt) const;
1678 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1679 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1680
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001681 Branch* GetBranch(uint32_t branch_id);
1682 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001683 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1684 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001685 void BindRelativeToPrecedingBranch(MipsLabel* label,
1686 uint32_t prev_branch_id_plus_one,
1687 uint32_t position);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001688
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001689 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001690 void ReserveJumpTableSpace();
1691 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001692 void PromoteBranches();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001693 void EmitBranch(uint32_t branch_id);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001694 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001695 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001696
1697 // Emits exception block.
1698 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1699
Lena Djokic0758ae72017-05-23 11:06:23 +02001700 bool HasMsa() const {
1701 return has_msa_;
1702 }
1703
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001704 bool IsR6() const {
1705 if (isa_features_ != nullptr) {
1706 return isa_features_->IsR6();
1707 } else {
1708 return false;
1709 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001710 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001711
1712 bool Is32BitFPU() const {
1713 if (isa_features_ != nullptr) {
1714 return isa_features_->Is32BitFloatingPoint();
1715 } else {
1716 return true;
1717 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001718 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001719
1720 // List of exception blocks to generate at the end of the code cache.
1721 std::vector<MipsExceptionSlowPath> exception_blocks_;
1722
1723 std::vector<Branch> branches_;
1724
1725 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1726 bool overwriting_;
1727 // The current overwrite location.
1728 uint32_t overwrite_location_;
1729
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001730 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1731 bool reordering_;
1732 // Information about the last instruction that may be used to fill a branch delay slot.
1733 DelaySlot delay_slot_;
1734 // Delay slot FSM state.
1735 DsFsmState ds_fsm_state_;
1736 // PC of the current labeled target instruction.
1737 uint32_t ds_fsm_target_pc_;
1738 // PCs of labeled target instructions.
1739 std::vector<uint32_t> ds_fsm_target_pcs_;
1740
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001741 // Use std::deque<> for literal labels to allow insertions at the end
1742 // without invalidating pointers and references to existing elements.
1743 ArenaDeque<Literal> literals_;
1744
Alexey Frunze96b66822016-09-10 02:32:44 -07001745 // Jump table list.
1746 ArenaDeque<JumpTable> jump_tables_;
1747
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001748 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1749 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1750 // that PC (from NAL) points to.
1751 MipsLabel pc_rel_base_label_;
1752
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001753 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001754 uint32_t last_position_adjustment_;
1755 uint32_t last_old_position_;
1756 uint32_t last_branch_id_;
1757
Lena Djokic0758ae72017-05-23 11:06:23 +02001758 const bool has_msa_;
1759
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001760 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001761
jeffhao7fbee072012-08-24 17:56:54 -07001762 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1763};
1764
jeffhao7fbee072012-08-24 17:56:54 -07001765} // namespace mips
1766} // namespace art
1767
Ian Rogers166db042013-07-26 12:05:57 -07001768#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_