blob: e85645b446825e004469586ec42993cfaedc95ae [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
17#include "assembler_mips.h"
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080020#include "base/casts.h"
Ian Rogers166db042013-07-26 12:05:57 -070021#include "entrypoints/quick/quick_entrypoints.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020022#include "entrypoints/quick/quick_entrypoints_enum.h"
jeffhao7fbee072012-08-24 17:56:54 -070023#include "memory_region.h"
jeffhao7fbee072012-08-24 17:56:54 -070024#include "thread.h"
25
26namespace art {
27namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070028
Andreas Gampe542451c2016-07-26 09:02:02 -070029static_assert(static_cast<size_t>(kMipsPointerSize) == kMipsWordSize,
30 "Unexpected Mips pointer size.");
31static_assert(kMipsPointerSize == PointerSize::k32, "Unexpected Mips pointer size.");
32
33
jeffhao7fbee072012-08-24 17:56:54 -070034std::ostream& operator<<(std::ostream& os, const DRegister& rhs) {
35 if (rhs >= D0 && rhs < kNumberOfDRegisters) {
36 os << "d" << static_cast<int>(rhs);
37 } else {
38 os << "DRegister[" << static_cast<int>(rhs) << "]";
39 }
40 return os;
41}
42
Alexey Frunze57eb0f52016-07-29 22:04:46 -070043MipsAssembler::DelaySlot::DelaySlot()
44 : instruction_(0),
45 gpr_outs_mask_(0),
46 gpr_ins_mask_(0),
47 fpr_outs_mask_(0),
48 fpr_ins_mask_(0),
49 cc_outs_mask_(0),
Alexey Frunzea663d9d2017-07-31 18:43:18 -070050 cc_ins_mask_(0),
51 patcher_label_(nullptr) {}
Alexey Frunze57eb0f52016-07-29 22:04:46 -070052
53void MipsAssembler::DsFsmInstr(uint32_t instruction,
54 uint32_t gpr_outs_mask,
55 uint32_t gpr_ins_mask,
56 uint32_t fpr_outs_mask,
57 uint32_t fpr_ins_mask,
58 uint32_t cc_outs_mask,
Alexey Frunzea663d9d2017-07-31 18:43:18 -070059 uint32_t cc_ins_mask,
60 MipsLabel* patcher_label) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -070061 if (!reordering_) {
62 CHECK_EQ(ds_fsm_state_, kExpectingLabel);
63 CHECK_EQ(delay_slot_.instruction_, 0u);
64 return;
65 }
66 switch (ds_fsm_state_) {
67 case kExpectingLabel:
68 break;
69 case kExpectingInstruction:
70 CHECK_EQ(ds_fsm_target_pc_ + sizeof(uint32_t), buffer_.Size());
71 // If the last instruction is not suitable for delay slots, drop
72 // the PC of the label preceding it so that no unconditional branch
73 // uses this instruction to fill its delay slot.
74 if (instruction == 0) {
75 DsFsmDropLabel(); // Sets ds_fsm_state_ = kExpectingLabel.
76 } else {
77 // Otherwise wait for another instruction or label before we can
78 // commit the label PC. The label PC will be dropped if instead
79 // of another instruction or label there's a call from the code
80 // generator to CodePosition() to record the buffer size.
81 // Instructions after which the buffer size is recorded cannot
82 // be moved into delay slots or anywhere else because they may
83 // trigger signals and the signal handlers expect these signals
84 // to be coming from the instructions immediately preceding the
85 // recorded buffer locations.
86 ds_fsm_state_ = kExpectingCommit;
87 }
88 break;
89 case kExpectingCommit:
90 CHECK_EQ(ds_fsm_target_pc_ + 2 * sizeof(uint32_t), buffer_.Size());
91 DsFsmCommitLabel(); // Sets ds_fsm_state_ = kExpectingLabel.
92 break;
93 }
94 delay_slot_.instruction_ = instruction;
95 delay_slot_.gpr_outs_mask_ = gpr_outs_mask & ~1u; // Ignore register ZERO.
96 delay_slot_.gpr_ins_mask_ = gpr_ins_mask & ~1u; // Ignore register ZERO.
97 delay_slot_.fpr_outs_mask_ = fpr_outs_mask;
98 delay_slot_.fpr_ins_mask_ = fpr_ins_mask;
99 delay_slot_.cc_outs_mask_ = cc_outs_mask;
100 delay_slot_.cc_ins_mask_ = cc_ins_mask;
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700101 delay_slot_.patcher_label_ = patcher_label;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700102}
103
104void MipsAssembler::DsFsmLabel() {
105 if (!reordering_) {
106 CHECK_EQ(ds_fsm_state_, kExpectingLabel);
107 CHECK_EQ(delay_slot_.instruction_, 0u);
108 return;
109 }
110 switch (ds_fsm_state_) {
111 case kExpectingLabel:
112 ds_fsm_target_pc_ = buffer_.Size();
113 ds_fsm_state_ = kExpectingInstruction;
114 break;
115 case kExpectingInstruction:
116 // Allow consecutive labels.
117 CHECK_EQ(ds_fsm_target_pc_, buffer_.Size());
118 break;
119 case kExpectingCommit:
120 CHECK_EQ(ds_fsm_target_pc_ + sizeof(uint32_t), buffer_.Size());
121 DsFsmCommitLabel();
122 ds_fsm_target_pc_ = buffer_.Size();
123 ds_fsm_state_ = kExpectingInstruction;
124 break;
125 }
126 // We cannot move instructions into delay slots across labels.
127 delay_slot_.instruction_ = 0;
128}
129
130void MipsAssembler::DsFsmCommitLabel() {
131 if (ds_fsm_state_ == kExpectingCommit) {
132 ds_fsm_target_pcs_.emplace_back(ds_fsm_target_pc_);
133 }
134 ds_fsm_state_ = kExpectingLabel;
135}
136
137void MipsAssembler::DsFsmDropLabel() {
138 ds_fsm_state_ = kExpectingLabel;
139}
140
141bool MipsAssembler::SetReorder(bool enable) {
142 bool last_state = reordering_;
143 if (last_state != enable) {
144 DsFsmCommitLabel();
145 DsFsmInstrNop(0);
146 }
147 reordering_ = enable;
148 return last_state;
149}
150
151size_t MipsAssembler::CodePosition() {
152 // The last instruction cannot be used in a delay slot, do not commit
153 // the label before it (if any) and clear the delay slot.
154 DsFsmDropLabel();
155 DsFsmInstrNop(0);
156 size_t size = buffer_.Size();
157 // In theory we can get the following sequence:
158 // label1:
159 // instr
160 // label2: # label1 gets committed when label2 is seen
161 // CodePosition() call
162 // and we need to uncommit label1.
163 if (ds_fsm_target_pcs_.size() != 0 && ds_fsm_target_pcs_.back() + sizeof(uint32_t) == size) {
164 ds_fsm_target_pcs_.pop_back();
165 }
166 return size;
167}
168
169void MipsAssembler::DsFsmInstrNop(uint32_t instruction ATTRIBUTE_UNUSED) {
170 DsFsmInstr(0, 0, 0, 0, 0, 0, 0);
171}
172
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700173void MipsAssembler::DsFsmInstrRrr(uint32_t instruction,
174 Register out,
175 Register in1,
176 Register in2,
177 MipsLabel* patcher_label) {
178 DsFsmInstr(instruction, (1u << out), (1u << in1) | (1u << in2), 0, 0, 0, 0, patcher_label);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700179}
180
181void MipsAssembler::DsFsmInstrRrrr(uint32_t instruction,
182 Register in1_out,
183 Register in2,
184 Register in3) {
185 DsFsmInstr(instruction, (1u << in1_out), (1u << in1_out) | (1u << in2) | (1u << in3), 0, 0, 0, 0);
186}
187
188void MipsAssembler::DsFsmInstrFff(uint32_t instruction,
189 FRegister out,
190 FRegister in1,
191 FRegister in2) {
192 DsFsmInstr(instruction, 0, 0, (1u << out), (1u << in1) | (1u << in2), 0, 0);
193}
194
195void MipsAssembler::DsFsmInstrFfff(uint32_t instruction,
196 FRegister in1_out,
197 FRegister in2,
198 FRegister in3) {
199 DsFsmInstr(instruction, 0, 0, (1u << in1_out), (1u << in1_out) | (1u << in2) | (1u << in3), 0, 0);
200}
201
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700202void MipsAssembler::DsFsmInstrFffr(uint32_t instruction,
203 FRegister in1_out,
204 FRegister in2,
205 Register in3) {
206 DsFsmInstr(instruction, 0, (1u << in3), (1u << in1_out), (1u << in1_out) | (1u << in2), 0, 0);
207}
208
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700209void MipsAssembler::DsFsmInstrRf(uint32_t instruction, Register out, FRegister in) {
210 DsFsmInstr(instruction, (1u << out), 0, 0, (1u << in), 0, 0);
211}
212
213void MipsAssembler::DsFsmInstrFr(uint32_t instruction, FRegister out, Register in) {
214 DsFsmInstr(instruction, 0, (1u << in), (1u << out), 0, 0, 0);
215}
216
217void MipsAssembler::DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2) {
218 DsFsmInstr(instruction, 0, (1u << in2), 0, (1u << in1), 0, 0);
219}
220
221void MipsAssembler::DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2) {
222 DsFsmInstr(instruction, 0, 0, 0, (1u << in1) | (1u << in2), (1 << cc_out), 0);
223}
224
225void MipsAssembler::DsFsmInstrRrrc(uint32_t instruction,
226 Register in1_out,
227 Register in2,
228 int cc_in) {
229 DsFsmInstr(instruction, (1u << in1_out), (1u << in1_out) | (1u << in2), 0, 0, 0, (1 << cc_in));
230}
231
232void MipsAssembler::DsFsmInstrFffc(uint32_t instruction,
233 FRegister in1_out,
234 FRegister in2,
235 int cc_in) {
236 DsFsmInstr(instruction, 0, 0, (1u << in1_out), (1u << in1_out) | (1u << in2), 0, (1 << cc_in));
237}
238
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200239void MipsAssembler::FinalizeCode() {
240 for (auto& exception_block : exception_blocks_) {
241 EmitExceptionPoll(&exception_block);
242 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700243 // Commit the last branch target label (if any) and disable instruction reordering.
244 DsFsmCommitLabel();
245 SetReorder(false);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700246 EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -0700247 ReserveJumpTableSpace();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200248 PromoteBranches();
249}
250
251void MipsAssembler::FinalizeInstructions(const MemoryRegion& region) {
Vladimir Marko10ef6942015-10-22 15:25:54 +0100252 size_t number_of_delayed_adjust_pcs = cfi().NumberOfDelayedAdvancePCs();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200253 EmitBranches();
Alexey Frunze96b66822016-09-10 02:32:44 -0700254 EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200255 Assembler::FinalizeInstructions(region);
Vladimir Marko10ef6942015-10-22 15:25:54 +0100256 PatchCFI(number_of_delayed_adjust_pcs);
257}
258
259void MipsAssembler::PatchCFI(size_t number_of_delayed_adjust_pcs) {
260 if (cfi().NumberOfDelayedAdvancePCs() == 0u) {
261 DCHECK_EQ(number_of_delayed_adjust_pcs, 0u);
262 return;
263 }
264
265 typedef DebugFrameOpCodeWriterForAssembler::DelayedAdvancePC DelayedAdvancePC;
266 const auto data = cfi().ReleaseStreamAndPrepareForDelayedAdvancePC();
267 const std::vector<uint8_t>& old_stream = data.first;
268 const std::vector<DelayedAdvancePC>& advances = data.second;
269
270 // PCs recorded before EmitBranches() need to be adjusted.
271 // PCs recorded during EmitBranches() are already adjusted.
272 // Both ranges are separately sorted but they may overlap.
273 if (kIsDebugBuild) {
274 auto cmp = [](const DelayedAdvancePC& lhs, const DelayedAdvancePC& rhs) {
275 return lhs.pc < rhs.pc;
276 };
277 CHECK(std::is_sorted(advances.begin(), advances.begin() + number_of_delayed_adjust_pcs, cmp));
278 CHECK(std::is_sorted(advances.begin() + number_of_delayed_adjust_pcs, advances.end(), cmp));
279 }
280
281 // Append initial CFI data if any.
282 size_t size = advances.size();
283 DCHECK_NE(size, 0u);
284 cfi().AppendRawData(old_stream, 0u, advances[0].stream_pos);
285 // Emit PC adjustments interleaved with the old CFI stream.
286 size_t adjust_pos = 0u;
287 size_t late_emit_pos = number_of_delayed_adjust_pcs;
288 while (adjust_pos != number_of_delayed_adjust_pcs || late_emit_pos != size) {
289 size_t adjusted_pc = (adjust_pos != number_of_delayed_adjust_pcs)
290 ? GetAdjustedPosition(advances[adjust_pos].pc)
291 : static_cast<size_t>(-1);
292 size_t late_emit_pc = (late_emit_pos != size)
293 ? advances[late_emit_pos].pc
294 : static_cast<size_t>(-1);
295 size_t advance_pc = std::min(adjusted_pc, late_emit_pc);
296 DCHECK_NE(advance_pc, static_cast<size_t>(-1));
297 size_t entry = (adjusted_pc <= late_emit_pc) ? adjust_pos : late_emit_pos;
298 if (adjusted_pc <= late_emit_pc) {
299 ++adjust_pos;
300 } else {
301 ++late_emit_pos;
302 }
303 cfi().AdvancePC(advance_pc);
304 size_t end_pos = (entry + 1u == size) ? old_stream.size() : advances[entry + 1u].stream_pos;
305 cfi().AppendRawData(old_stream, advances[entry].stream_pos, end_pos);
306 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200307}
308
309void MipsAssembler::EmitBranches() {
310 CHECK(!overwriting_);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700311 CHECK(!reordering_);
312 // Now that everything has its final position in the buffer (the branches have
313 // been promoted), adjust the target label PCs.
314 for (size_t cnt = ds_fsm_target_pcs_.size(), i = 0; i < cnt; i++) {
315 ds_fsm_target_pcs_[i] = GetAdjustedPosition(ds_fsm_target_pcs_[i]);
316 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200317 // Switch from appending instructions at the end of the buffer to overwriting
318 // existing instructions (branch placeholders) in the buffer.
319 overwriting_ = true;
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700320 for (size_t id = 0; id < branches_.size(); id++) {
321 EmitBranch(id);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200322 }
323 overwriting_ = false;
324}
325
326void MipsAssembler::Emit(uint32_t value) {
327 if (overwriting_) {
328 // Branches to labels are emitted into their placeholders here.
329 buffer_.Store<uint32_t>(overwrite_location_, value);
330 overwrite_location_ += sizeof(uint32_t);
331 } else {
332 // Other instructions are simply appended at the end here.
333 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
334 buffer_.Emit<uint32_t>(value);
335 }
jeffhao7fbee072012-08-24 17:56:54 -0700336}
337
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700338uint32_t MipsAssembler::EmitR(int opcode,
339 Register rs,
340 Register rt,
341 Register rd,
342 int shamt,
343 int funct) {
jeffhao7fbee072012-08-24 17:56:54 -0700344 CHECK_NE(rs, kNoRegister);
345 CHECK_NE(rt, kNoRegister);
346 CHECK_NE(rd, kNoRegister);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200347 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
348 static_cast<uint32_t>(rs) << kRsShift |
349 static_cast<uint32_t>(rt) << kRtShift |
350 static_cast<uint32_t>(rd) << kRdShift |
351 shamt << kShamtShift |
352 funct;
jeffhao7fbee072012-08-24 17:56:54 -0700353 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700354 return encoding;
jeffhao7fbee072012-08-24 17:56:54 -0700355}
356
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700357uint32_t MipsAssembler::EmitI(int opcode, Register rs, Register rt, uint16_t imm) {
jeffhao7fbee072012-08-24 17:56:54 -0700358 CHECK_NE(rs, kNoRegister);
359 CHECK_NE(rt, kNoRegister);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200360 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
361 static_cast<uint32_t>(rs) << kRsShift |
362 static_cast<uint32_t>(rt) << kRtShift |
363 imm;
jeffhao7fbee072012-08-24 17:56:54 -0700364 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700365 return encoding;
jeffhao7fbee072012-08-24 17:56:54 -0700366}
367
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700368uint32_t MipsAssembler::EmitI21(int opcode, Register rs, uint32_t imm21) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200369 CHECK_NE(rs, kNoRegister);
370 CHECK(IsUint<21>(imm21)) << imm21;
371 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
372 static_cast<uint32_t>(rs) << kRsShift |
373 imm21;
jeffhao7fbee072012-08-24 17:56:54 -0700374 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700375 return encoding;
jeffhao7fbee072012-08-24 17:56:54 -0700376}
377
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700378uint32_t MipsAssembler::EmitI26(int opcode, uint32_t imm26) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200379 CHECK(IsUint<26>(imm26)) << imm26;
380 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift | imm26;
381 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700382 return encoding;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200383}
384
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700385uint32_t MipsAssembler::EmitFR(int opcode,
386 int fmt,
387 FRegister ft,
388 FRegister fs,
389 FRegister fd,
390 int funct) {
jeffhao7fbee072012-08-24 17:56:54 -0700391 CHECK_NE(ft, kNoFRegister);
392 CHECK_NE(fs, kNoFRegister);
393 CHECK_NE(fd, kNoFRegister);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200394 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
395 fmt << kFmtShift |
396 static_cast<uint32_t>(ft) << kFtShift |
397 static_cast<uint32_t>(fs) << kFsShift |
398 static_cast<uint32_t>(fd) << kFdShift |
399 funct;
jeffhao7fbee072012-08-24 17:56:54 -0700400 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700401 return encoding;
jeffhao7fbee072012-08-24 17:56:54 -0700402}
403
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700404uint32_t MipsAssembler::EmitFI(int opcode, int fmt, FRegister ft, uint16_t imm) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200405 CHECK_NE(ft, kNoFRegister);
406 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
407 fmt << kFmtShift |
408 static_cast<uint32_t>(ft) << kFtShift |
409 imm;
jeffhao7fbee072012-08-24 17:56:54 -0700410 Emit(encoding);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700411 return encoding;
jeffhao7fbee072012-08-24 17:56:54 -0700412}
413
Lena Djokic0758ae72017-05-23 11:06:23 +0200414uint32_t MipsAssembler::EmitMsa3R(int operation,
415 int df,
416 VectorRegister wt,
417 VectorRegister ws,
418 VectorRegister wd,
419 int minor_opcode) {
420 CHECK_NE(wt, kNoVectorRegister);
421 CHECK_NE(ws, kNoVectorRegister);
422 CHECK_NE(wd, kNoVectorRegister);
423 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
424 operation << kMsaOperationShift |
425 df << kDfShift |
426 static_cast<uint32_t>(wt) << kWtShift |
427 static_cast<uint32_t>(ws) << kWsShift |
428 static_cast<uint32_t>(wd) << kWdShift |
429 minor_opcode;
430 Emit(encoding);
431 return encoding;
432}
433
434uint32_t MipsAssembler::EmitMsaBIT(int operation,
435 int df_m,
436 VectorRegister ws,
437 VectorRegister wd,
438 int minor_opcode) {
439 CHECK_NE(ws, kNoVectorRegister);
440 CHECK_NE(wd, kNoVectorRegister);
441 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
442 operation << kMsaOperationShift |
443 df_m << kDfMShift |
444 static_cast<uint32_t>(ws) << kWsShift |
445 static_cast<uint32_t>(wd) << kWdShift |
446 minor_opcode;
447 Emit(encoding);
448 return encoding;
449}
450
451uint32_t MipsAssembler::EmitMsaELM(int operation,
452 int df_n,
453 VectorRegister ws,
454 VectorRegister wd,
455 int minor_opcode) {
456 CHECK_NE(ws, kNoVectorRegister);
457 CHECK_NE(wd, kNoVectorRegister);
458 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
459 operation << kMsaELMOperationShift |
460 df_n << kDfNShift |
461 static_cast<uint32_t>(ws) << kWsShift |
462 static_cast<uint32_t>(wd) << kWdShift |
463 minor_opcode;
464 Emit(encoding);
465 return encoding;
466}
467
468uint32_t MipsAssembler::EmitMsaMI10(int s10,
469 Register rs,
470 VectorRegister wd,
471 int minor_opcode,
472 int df) {
473 CHECK_NE(rs, kNoRegister);
474 CHECK_NE(wd, kNoVectorRegister);
475 CHECK(IsUint<10>(s10)) << s10;
476 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
477 s10 << kS10Shift |
478 static_cast<uint32_t>(rs) << kWsShift |
479 static_cast<uint32_t>(wd) << kWdShift |
480 minor_opcode << kS10MinorShift |
481 df;
482 Emit(encoding);
483 return encoding;
484}
485
486uint32_t MipsAssembler::EmitMsaI10(int operation,
487 int df,
488 int i10,
489 VectorRegister wd,
490 int minor_opcode) {
491 CHECK_NE(wd, kNoVectorRegister);
492 CHECK(IsUint<10>(i10)) << i10;
493 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
494 operation << kMsaOperationShift |
495 df << kDfShift |
496 i10 << kI10Shift |
497 static_cast<uint32_t>(wd) << kWdShift |
498 minor_opcode;
499 Emit(encoding);
500 return encoding;
501}
502
503uint32_t MipsAssembler::EmitMsa2R(int operation,
504 int df,
505 VectorRegister ws,
506 VectorRegister wd,
507 int minor_opcode) {
508 CHECK_NE(ws, kNoVectorRegister);
509 CHECK_NE(wd, kNoVectorRegister);
510 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
511 operation << kMsa2ROperationShift |
512 df << kDf2RShift |
513 static_cast<uint32_t>(ws) << kWsShift |
514 static_cast<uint32_t>(wd) << kWdShift |
515 minor_opcode;
516 Emit(encoding);
517 return encoding;
518}
519
520uint32_t MipsAssembler::EmitMsa2RF(int operation,
521 int df,
522 VectorRegister ws,
523 VectorRegister wd,
524 int minor_opcode) {
525 CHECK_NE(ws, kNoVectorRegister);
526 CHECK_NE(wd, kNoVectorRegister);
527 uint32_t encoding = static_cast<uint32_t>(kMsaMajorOpcode) << kOpcodeShift |
528 operation << kMsa2RFOperationShift |
529 df << kDf2RShift |
530 static_cast<uint32_t>(ws) << kWsShift |
531 static_cast<uint32_t>(wd) << kWdShift |
532 minor_opcode;
533 Emit(encoding);
534 return encoding;
535}
536
jeffhao7fbee072012-08-24 17:56:54 -0700537void MipsAssembler::Addu(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700538 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x21), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700539}
540
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700541void MipsAssembler::Addiu(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label) {
542 if (patcher_label != nullptr) {
543 Bind(patcher_label);
544 }
545 DsFsmInstrRrr(EmitI(0x9, rs, rt, imm16), rt, rs, rs, patcher_label);
546}
547
jeffhao7fbee072012-08-24 17:56:54 -0700548void MipsAssembler::Addiu(Register rt, Register rs, uint16_t imm16) {
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700549 Addiu(rt, rs, imm16, /* patcher_label */ nullptr);
jeffhao7fbee072012-08-24 17:56:54 -0700550}
551
jeffhao7fbee072012-08-24 17:56:54 -0700552void MipsAssembler::Subu(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700553 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x23), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700554}
555
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200556void MipsAssembler::MultR2(Register rs, Register rt) {
557 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700558 DsFsmInstrRrr(EmitR(0, rs, rt, static_cast<Register>(0), 0, 0x18), ZERO, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700559}
560
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200561void MipsAssembler::MultuR2(Register rs, Register rt) {
562 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700563 DsFsmInstrRrr(EmitR(0, rs, rt, static_cast<Register>(0), 0, 0x19), ZERO, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700564}
565
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200566void MipsAssembler::DivR2(Register rs, Register rt) {
567 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700568 DsFsmInstrRrr(EmitR(0, rs, rt, static_cast<Register>(0), 0, 0x1a), ZERO, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700569}
570
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200571void MipsAssembler::DivuR2(Register rs, Register rt) {
572 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700573 DsFsmInstrRrr(EmitR(0, rs, rt, static_cast<Register>(0), 0, 0x1b), ZERO, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700574}
575
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200576void MipsAssembler::MulR2(Register rd, Register rs, Register rt) {
577 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700578 DsFsmInstrRrr(EmitR(0x1c, rs, rt, rd, 0, 2), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200579}
580
581void MipsAssembler::DivR2(Register rd, Register rs, Register rt) {
582 CHECK(!IsR6());
583 DivR2(rs, rt);
584 Mflo(rd);
585}
586
587void MipsAssembler::ModR2(Register rd, Register rs, Register rt) {
588 CHECK(!IsR6());
589 DivR2(rs, rt);
590 Mfhi(rd);
591}
592
593void MipsAssembler::DivuR2(Register rd, Register rs, Register rt) {
594 CHECK(!IsR6());
595 DivuR2(rs, rt);
596 Mflo(rd);
597}
598
599void MipsAssembler::ModuR2(Register rd, Register rs, Register rt) {
600 CHECK(!IsR6());
601 DivuR2(rs, rt);
602 Mfhi(rd);
603}
604
605void MipsAssembler::MulR6(Register rd, Register rs, Register rt) {
606 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700607 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 2, 0x18), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200608}
609
Alexey Frunze7e99e052015-11-24 19:28:01 -0800610void MipsAssembler::MuhR6(Register rd, Register rs, Register rt) {
611 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700612 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 3, 0x18), rd, rs, rt);
Alexey Frunze7e99e052015-11-24 19:28:01 -0800613}
614
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200615void MipsAssembler::MuhuR6(Register rd, Register rs, Register rt) {
616 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700617 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 3, 0x19), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200618}
619
620void MipsAssembler::DivR6(Register rd, Register rs, Register rt) {
621 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700622 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 2, 0x1a), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200623}
624
625void MipsAssembler::ModR6(Register rd, Register rs, Register rt) {
626 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700627 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 3, 0x1a), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200628}
629
630void MipsAssembler::DivuR6(Register rd, Register rs, Register rt) {
631 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700632 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 2, 0x1b), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200633}
634
635void MipsAssembler::ModuR6(Register rd, Register rs, Register rt) {
636 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700637 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 3, 0x1b), rd, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200638}
639
jeffhao7fbee072012-08-24 17:56:54 -0700640void MipsAssembler::And(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700641 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x24), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700642}
643
644void MipsAssembler::Andi(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700645 DsFsmInstrRrr(EmitI(0xc, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700646}
647
648void MipsAssembler::Or(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700649 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x25), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700650}
651
652void MipsAssembler::Ori(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700653 DsFsmInstrRrr(EmitI(0xd, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700654}
655
656void MipsAssembler::Xor(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700657 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x26), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700658}
659
660void MipsAssembler::Xori(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700661 DsFsmInstrRrr(EmitI(0xe, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700662}
663
664void MipsAssembler::Nor(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700665 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x27), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700666}
667
Chris Larsene3845472015-11-18 12:27:15 -0800668void MipsAssembler::Movz(Register rd, Register rs, Register rt) {
669 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700670 DsFsmInstrRrrr(EmitR(0, rs, rt, rd, 0, 0x0A), rd, rs, rt);
Chris Larsene3845472015-11-18 12:27:15 -0800671}
672
673void MipsAssembler::Movn(Register rd, Register rs, Register rt) {
674 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700675 DsFsmInstrRrrr(EmitR(0, rs, rt, rd, 0, 0x0B), rd, rs, rt);
Chris Larsene3845472015-11-18 12:27:15 -0800676}
677
678void MipsAssembler::Seleqz(Register rd, Register rs, Register rt) {
679 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700680 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x35), rd, rs, rt);
Chris Larsene3845472015-11-18 12:27:15 -0800681}
682
683void MipsAssembler::Selnez(Register rd, Register rs, Register rt) {
684 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700685 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x37), rd, rs, rt);
Chris Larsene3845472015-11-18 12:27:15 -0800686}
687
688void MipsAssembler::ClzR6(Register rd, Register rs) {
689 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700690 DsFsmInstrRrr(EmitR(0, rs, static_cast<Register>(0), rd, 0x01, 0x10), rd, rs, rs);
Chris Larsene3845472015-11-18 12:27:15 -0800691}
692
693void MipsAssembler::ClzR2(Register rd, Register rs) {
694 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700695 DsFsmInstrRrr(EmitR(0x1C, rs, rd, rd, 0, 0x20), rd, rs, rs);
Chris Larsene3845472015-11-18 12:27:15 -0800696}
697
698void MipsAssembler::CloR6(Register rd, Register rs) {
699 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700700 DsFsmInstrRrr(EmitR(0, rs, static_cast<Register>(0), rd, 0x01, 0x11), rd, rs, rs);
Chris Larsene3845472015-11-18 12:27:15 -0800701}
702
703void MipsAssembler::CloR2(Register rd, Register rs) {
704 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700705 DsFsmInstrRrr(EmitR(0x1C, rs, rd, rd, 0, 0x21), rd, rs, rs);
Chris Larsene3845472015-11-18 12:27:15 -0800706}
707
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200708void MipsAssembler::Seb(Register rd, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700709 DsFsmInstrRrr(EmitR(0x1f, static_cast<Register>(0), rt, rd, 0x10, 0x20), rd, rt, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700710}
711
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200712void MipsAssembler::Seh(Register rd, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700713 DsFsmInstrRrr(EmitR(0x1f, static_cast<Register>(0), rt, rd, 0x18, 0x20), rd, rt, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700714}
715
Chris Larsen3f8bf652015-10-28 10:08:56 -0700716void MipsAssembler::Wsbh(Register rd, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700717 DsFsmInstrRrr(EmitR(0x1f, static_cast<Register>(0), rt, rd, 2, 0x20), rd, rt, rt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700718}
719
Chris Larsen70014c82015-11-18 12:26:08 -0800720void MipsAssembler::Bitswap(Register rd, Register rt) {
721 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700722 DsFsmInstrRrr(EmitR(0x1f, static_cast<Register>(0), rt, rd, 0x0, 0x20), rd, rt, rt);
Chris Larsen70014c82015-11-18 12:26:08 -0800723}
724
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200725void MipsAssembler::Sll(Register rd, Register rt, int shamt) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700726 CHECK(IsUint<5>(shamt)) << shamt;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700727 DsFsmInstrRrr(EmitR(0, static_cast<Register>(0), rt, rd, shamt, 0x00), rd, rt, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700728}
729
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200730void MipsAssembler::Srl(Register rd, Register rt, int shamt) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700731 CHECK(IsUint<5>(shamt)) << shamt;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700732 DsFsmInstrRrr(EmitR(0, static_cast<Register>(0), rt, rd, shamt, 0x02), rd, rt, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200733}
734
Chris Larsen3f8bf652015-10-28 10:08:56 -0700735void MipsAssembler::Rotr(Register rd, Register rt, int shamt) {
736 CHECK(IsUint<5>(shamt)) << shamt;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700737 DsFsmInstrRrr(EmitR(0, static_cast<Register>(1), rt, rd, shamt, 0x02), rd, rt, rt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700738}
739
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200740void MipsAssembler::Sra(Register rd, Register rt, int shamt) {
Chris Larsen3f8bf652015-10-28 10:08:56 -0700741 CHECK(IsUint<5>(shamt)) << shamt;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700742 DsFsmInstrRrr(EmitR(0, static_cast<Register>(0), rt, rd, shamt, 0x03), rd, rt, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200743}
744
745void MipsAssembler::Sllv(Register rd, Register rt, Register rs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700746 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x04), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700747}
748
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200749void MipsAssembler::Srlv(Register rd, Register rt, Register rs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700750 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x06), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700751}
752
Chris Larsene16ce5a2015-11-18 12:30:20 -0800753void MipsAssembler::Rotrv(Register rd, Register rt, Register rs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700754 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 1, 0x06), rd, rs, rt);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800755}
756
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200757void MipsAssembler::Srav(Register rd, Register rt, Register rs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700758 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x07), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700759}
760
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800761void MipsAssembler::Ext(Register rd, Register rt, int pos, int size) {
762 CHECK(IsUint<5>(pos)) << pos;
763 CHECK(0 < size && size <= 32) << size;
764 CHECK(0 < pos + size && pos + size <= 32) << pos << " + " << size;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700765 DsFsmInstrRrr(EmitR(0x1f, rt, rd, static_cast<Register>(size - 1), pos, 0x00), rd, rt, rt);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800766}
767
768void MipsAssembler::Ins(Register rd, Register rt, int pos, int size) {
769 CHECK(IsUint<5>(pos)) << pos;
770 CHECK(0 < size && size <= 32) << size;
771 CHECK(0 < pos + size && pos + size <= 32) << pos << " + " << size;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700772 DsFsmInstrRrr(EmitR(0x1f, rt, rd, static_cast<Register>(pos + size - 1), pos, 0x04), rd, rd, rt);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800773}
774
Chris Larsen692235e2016-11-21 16:04:53 -0800775void MipsAssembler::Lsa(Register rd, Register rs, Register rt, int saPlusOne) {
Lena Djokic0758ae72017-05-23 11:06:23 +0200776 CHECK(IsR6() || HasMsa());
Chris Larsen692235e2016-11-21 16:04:53 -0800777 CHECK(1 <= saPlusOne && saPlusOne <= 4) << saPlusOne;
778 int sa = saPlusOne - 1;
779 DsFsmInstrRrr(EmitR(0x0, rs, rt, rd, sa, 0x05), rd, rs, rt);
780}
781
Chris Larsencd0295d2017-03-31 15:26:54 -0700782void MipsAssembler::ShiftAndAdd(Register dst,
783 Register src_idx,
784 Register src_base,
785 int shamt,
786 Register tmp) {
787 CHECK(0 <= shamt && shamt <= 4) << shamt;
788 CHECK_NE(src_base, tmp);
789 if (shamt == TIMES_1) {
790 // Catch the special case where the shift amount is zero (0).
791 Addu(dst, src_base, src_idx);
Lena Djokic0758ae72017-05-23 11:06:23 +0200792 } else if (IsR6() || HasMsa()) {
Chris Larsencd0295d2017-03-31 15:26:54 -0700793 Lsa(dst, src_idx, src_base, shamt);
794 } else {
795 Sll(tmp, src_idx, shamt);
796 Addu(dst, src_base, tmp);
797 }
798}
799
jeffhao7fbee072012-08-24 17:56:54 -0700800void MipsAssembler::Lb(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700801 DsFsmInstrRrr(EmitI(0x20, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700802}
803
804void MipsAssembler::Lh(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700805 DsFsmInstrRrr(EmitI(0x21, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700806}
807
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700808void MipsAssembler::Lw(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label) {
809 if (patcher_label != nullptr) {
810 Bind(patcher_label);
811 }
812 DsFsmInstrRrr(EmitI(0x23, rs, rt, imm16), rt, rs, rs, patcher_label);
813}
814
jeffhao7fbee072012-08-24 17:56:54 -0700815void MipsAssembler::Lw(Register rt, Register rs, uint16_t imm16) {
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700816 Lw(rt, rs, imm16, /* patcher_label */ nullptr);
jeffhao7fbee072012-08-24 17:56:54 -0700817}
818
Chris Larsen3acee732015-11-18 13:31:08 -0800819void MipsAssembler::Lwl(Register rt, Register rs, uint16_t imm16) {
820 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700821 DsFsmInstrRrr(EmitI(0x22, rs, rt, imm16), rt, rt, rs);
Chris Larsen3acee732015-11-18 13:31:08 -0800822}
823
824void MipsAssembler::Lwr(Register rt, Register rs, uint16_t imm16) {
825 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700826 DsFsmInstrRrr(EmitI(0x26, rs, rt, imm16), rt, rt, rs);
Chris Larsen3acee732015-11-18 13:31:08 -0800827}
828
jeffhao7fbee072012-08-24 17:56:54 -0700829void MipsAssembler::Lbu(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700830 DsFsmInstrRrr(EmitI(0x24, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700831}
832
833void MipsAssembler::Lhu(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700834 DsFsmInstrRrr(EmitI(0x25, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700835}
836
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700837void MipsAssembler::Lwpc(Register rs, uint32_t imm19) {
838 CHECK(IsR6());
839 CHECK(IsUint<19>(imm19)) << imm19;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700840 DsFsmInstrNop(EmitI21(0x3B, rs, (0x01 << 19) | imm19));
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700841}
842
jeffhao7fbee072012-08-24 17:56:54 -0700843void MipsAssembler::Lui(Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700844 DsFsmInstrRrr(EmitI(0xf, static_cast<Register>(0), rt, imm16), rt, ZERO, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -0700845}
846
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700847void MipsAssembler::Aui(Register rt, Register rs, uint16_t imm16) {
848 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700849 DsFsmInstrRrr(EmitI(0xf, rs, rt, imm16), rt, rt, rs);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700850}
851
Alexey Frunze4147fcc2017-06-17 19:57:27 -0700852void MipsAssembler::AddUpper(Register rt, Register rs, uint16_t imm16, Register tmp) {
853 bool increment = (rs == rt);
854 if (increment) {
855 CHECK_NE(rs, tmp);
856 }
857 if (IsR6()) {
858 Aui(rt, rs, imm16);
859 } else if (increment) {
860 Lui(tmp, imm16);
861 Addu(rt, rs, tmp);
862 } else {
863 Lui(rt, imm16);
864 Addu(rt, rs, rt);
865 }
866}
867
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200868void MipsAssembler::Sync(uint32_t stype) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700869 DsFsmInstrNop(EmitR(0, ZERO, ZERO, ZERO, stype & 0x1f, 0xf));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200870}
871
jeffhao7fbee072012-08-24 17:56:54 -0700872void MipsAssembler::Mfhi(Register rd) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200873 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700874 DsFsmInstrRrr(EmitR(0, ZERO, ZERO, rd, 0, 0x10), rd, ZERO, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -0700875}
876
877void MipsAssembler::Mflo(Register rd) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200878 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700879 DsFsmInstrRrr(EmitR(0, ZERO, ZERO, rd, 0, 0x12), rd, ZERO, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -0700880}
881
882void MipsAssembler::Sb(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700883 DsFsmInstrRrr(EmitI(0x28, rs, rt, imm16), ZERO, rt, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700884}
885
886void MipsAssembler::Sh(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700887 DsFsmInstrRrr(EmitI(0x29, rs, rt, imm16), ZERO, rt, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700888}
889
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700890void MipsAssembler::Sw(Register rt, Register rs, uint16_t imm16, MipsLabel* patcher_label) {
891 if (patcher_label != nullptr) {
892 Bind(patcher_label);
893 }
894 DsFsmInstrRrr(EmitI(0x2b, rs, rt, imm16), ZERO, rt, rs, patcher_label);
895}
896
jeffhao7fbee072012-08-24 17:56:54 -0700897void MipsAssembler::Sw(Register rt, Register rs, uint16_t imm16) {
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700898 Sw(rt, rs, imm16, /* patcher_label */ nullptr);
jeffhao7fbee072012-08-24 17:56:54 -0700899}
900
Chris Larsen3acee732015-11-18 13:31:08 -0800901void MipsAssembler::Swl(Register rt, Register rs, uint16_t imm16) {
902 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700903 DsFsmInstrRrr(EmitI(0x2a, rs, rt, imm16), ZERO, rt, rs);
Chris Larsen3acee732015-11-18 13:31:08 -0800904}
905
906void MipsAssembler::Swr(Register rt, Register rs, uint16_t imm16) {
907 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700908 DsFsmInstrRrr(EmitI(0x2e, rs, rt, imm16), ZERO, rt, rs);
Chris Larsen3acee732015-11-18 13:31:08 -0800909}
910
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700911void MipsAssembler::LlR2(Register rt, Register base, int16_t imm16) {
912 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700913 DsFsmInstrRrr(EmitI(0x30, base, rt, imm16), rt, base, base);
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700914}
915
916void MipsAssembler::ScR2(Register rt, Register base, int16_t imm16) {
917 CHECK(!IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700918 DsFsmInstrRrr(EmitI(0x38, base, rt, imm16), rt, rt, base);
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700919}
920
921void MipsAssembler::LlR6(Register rt, Register base, int16_t imm9) {
922 CHECK(IsR6());
923 CHECK(IsInt<9>(imm9));
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700924 DsFsmInstrRrr(EmitI(0x1f, base, rt, ((imm9 & 0x1ff) << 7) | 0x36), rt, base, base);
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700925}
926
927void MipsAssembler::ScR6(Register rt, Register base, int16_t imm9) {
928 CHECK(IsR6());
929 CHECK(IsInt<9>(imm9));
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700930 DsFsmInstrRrr(EmitI(0x1f, base, rt, ((imm9 & 0x1ff) << 7) | 0x26), rt, rt, base);
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700931}
932
jeffhao7fbee072012-08-24 17:56:54 -0700933void MipsAssembler::Slt(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700934 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x2a), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700935}
936
937void MipsAssembler::Sltu(Register rd, Register rs, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700938 DsFsmInstrRrr(EmitR(0, rs, rt, rd, 0, 0x2b), rd, rs, rt);
jeffhao7fbee072012-08-24 17:56:54 -0700939}
940
941void MipsAssembler::Slti(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700942 DsFsmInstrRrr(EmitI(0xa, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700943}
944
945void MipsAssembler::Sltiu(Register rt, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700946 DsFsmInstrRrr(EmitI(0xb, rs, rt, imm16), rt, rs, rs);
jeffhao7fbee072012-08-24 17:56:54 -0700947}
948
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200949void MipsAssembler::B(uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700950 DsFsmInstrNop(EmitI(0x4, static_cast<Register>(0), static_cast<Register>(0), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200951}
952
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700953void MipsAssembler::Bal(uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700954 DsFsmInstrNop(EmitI(0x1, static_cast<Register>(0), static_cast<Register>(0x11), imm16));
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700955}
956
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200957void MipsAssembler::Beq(Register rs, Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700958 DsFsmInstrNop(EmitI(0x4, rs, rt, imm16));
jeffhao7fbee072012-08-24 17:56:54 -0700959}
960
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200961void MipsAssembler::Bne(Register rs, Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700962 DsFsmInstrNop(EmitI(0x5, rs, rt, imm16));
jeffhao7fbee072012-08-24 17:56:54 -0700963}
964
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200965void MipsAssembler::Beqz(Register rt, uint16_t imm16) {
Alexey Frunze0cab6562017-07-25 15:19:36 -0700966 Beq(rt, ZERO, imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700967}
968
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200969void MipsAssembler::Bnez(Register rt, uint16_t imm16) {
Alexey Frunze0cab6562017-07-25 15:19:36 -0700970 Bne(rt, ZERO, imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700971}
972
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200973void MipsAssembler::Bltz(Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700974 DsFsmInstrNop(EmitI(0x1, rt, static_cast<Register>(0), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200975}
976
977void MipsAssembler::Bgez(Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700978 DsFsmInstrNop(EmitI(0x1, rt, static_cast<Register>(0x1), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200979}
980
981void MipsAssembler::Blez(Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700982 DsFsmInstrNop(EmitI(0x6, rt, static_cast<Register>(0), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200983}
984
985void MipsAssembler::Bgtz(Register rt, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700986 DsFsmInstrNop(EmitI(0x7, rt, static_cast<Register>(0), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200987}
988
Chris Larsenb74353a2015-11-20 09:07:09 -0800989void MipsAssembler::Bc1f(uint16_t imm16) {
990 Bc1f(0, imm16);
991}
992
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800993void MipsAssembler::Bc1f(int cc, uint16_t imm16) {
994 CHECK(!IsR6());
995 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700996 DsFsmInstrNop(EmitI(0x11, static_cast<Register>(0x8), static_cast<Register>(cc << 2), imm16));
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800997}
998
Chris Larsenb74353a2015-11-20 09:07:09 -0800999void MipsAssembler::Bc1t(uint16_t imm16) {
1000 Bc1t(0, imm16);
1001}
1002
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001003void MipsAssembler::Bc1t(int cc, uint16_t imm16) {
1004 CHECK(!IsR6());
1005 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001006 DsFsmInstrNop(EmitI(0x11,
1007 static_cast<Register>(0x8),
1008 static_cast<Register>((cc << 2) | 1),
1009 imm16));
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001010}
1011
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001012void MipsAssembler::J(uint32_t addr26) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001013 DsFsmInstrNop(EmitI26(0x2, addr26));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001014}
1015
1016void MipsAssembler::Jal(uint32_t addr26) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001017 DsFsmInstrNop(EmitI26(0x3, addr26));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001018}
1019
1020void MipsAssembler::Jalr(Register rd, Register rs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001021 uint32_t last_instruction = delay_slot_.instruction_;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001022 MipsLabel* patcher_label = delay_slot_.patcher_label_;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001023 bool exchange = (last_instruction != 0 &&
1024 (delay_slot_.gpr_outs_mask_ & (1u << rs)) == 0 &&
1025 ((delay_slot_.gpr_ins_mask_ | delay_slot_.gpr_outs_mask_) & (1u << rd)) == 0);
1026 if (exchange) {
1027 // The last instruction cannot be used in a different delay slot,
1028 // do not commit the label before it (if any).
1029 DsFsmDropLabel();
1030 }
1031 DsFsmInstrNop(EmitR(0, rs, static_cast<Register>(0), rd, 0, 0x09));
1032 if (exchange) {
1033 // Exchange the last two instructions in the assembler buffer.
1034 size_t size = buffer_.Size();
1035 CHECK_GE(size, 2 * sizeof(uint32_t));
1036 size_t pos1 = size - 2 * sizeof(uint32_t);
1037 size_t pos2 = size - sizeof(uint32_t);
1038 uint32_t instr1 = buffer_.Load<uint32_t>(pos1);
1039 uint32_t instr2 = buffer_.Load<uint32_t>(pos2);
1040 CHECK_EQ(instr1, last_instruction);
1041 buffer_.Store<uint32_t>(pos1, instr2);
1042 buffer_.Store<uint32_t>(pos2, instr1);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001043 // Move the patcher label along with the patched instruction.
1044 if (patcher_label != nullptr) {
1045 patcher_label->AdjustBoundPosition(sizeof(uint32_t));
1046 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001047 } else if (reordering_) {
1048 Nop();
1049 }
jeffhao7fbee072012-08-24 17:56:54 -07001050}
1051
1052void MipsAssembler::Jalr(Register rs) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001053 Jalr(RA, rs);
1054}
1055
1056void MipsAssembler::Jr(Register rs) {
1057 Jalr(ZERO, rs);
1058}
1059
1060void MipsAssembler::Nal() {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001061 DsFsmInstrNop(EmitI(0x1, static_cast<Register>(0), static_cast<Register>(0x10), 0));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001062}
1063
1064void MipsAssembler::Auipc(Register rs, uint16_t imm16) {
1065 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001066 DsFsmInstrNop(EmitI(0x3B, rs, static_cast<Register>(0x1E), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001067}
1068
1069void MipsAssembler::Addiupc(Register rs, uint32_t imm19) {
1070 CHECK(IsR6());
1071 CHECK(IsUint<19>(imm19)) << imm19;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001072 DsFsmInstrNop(EmitI21(0x3B, rs, imm19));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001073}
1074
1075void MipsAssembler::Bc(uint32_t imm26) {
1076 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001077 DsFsmInstrNop(EmitI26(0x32, imm26));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001078}
1079
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001080void MipsAssembler::Balc(uint32_t imm26) {
1081 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001082 DsFsmInstrNop(EmitI26(0x3A, imm26));
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001083}
1084
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001085void MipsAssembler::Jic(Register rt, uint16_t imm16) {
1086 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001087 DsFsmInstrNop(EmitI(0x36, static_cast<Register>(0), rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001088}
1089
1090void MipsAssembler::Jialc(Register rt, uint16_t imm16) {
1091 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001092 DsFsmInstrNop(EmitI(0x3E, static_cast<Register>(0), rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001093}
1094
1095void MipsAssembler::Bltc(Register rs, Register rt, uint16_t imm16) {
1096 CHECK(IsR6());
1097 CHECK_NE(rs, ZERO);
1098 CHECK_NE(rt, ZERO);
1099 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001100 DsFsmInstrNop(EmitI(0x17, rs, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001101}
1102
1103void MipsAssembler::Bltzc(Register rt, uint16_t imm16) {
1104 CHECK(IsR6());
1105 CHECK_NE(rt, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001106 DsFsmInstrNop(EmitI(0x17, rt, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001107}
1108
1109void MipsAssembler::Bgtzc(Register rt, uint16_t imm16) {
1110 CHECK(IsR6());
1111 CHECK_NE(rt, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001112 DsFsmInstrNop(EmitI(0x17, static_cast<Register>(0), rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001113}
1114
1115void MipsAssembler::Bgec(Register rs, Register rt, uint16_t imm16) {
1116 CHECK(IsR6());
1117 CHECK_NE(rs, ZERO);
1118 CHECK_NE(rt, ZERO);
1119 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001120 DsFsmInstrNop(EmitI(0x16, rs, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001121}
1122
1123void MipsAssembler::Bgezc(Register rt, uint16_t imm16) {
1124 CHECK(IsR6());
1125 CHECK_NE(rt, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001126 DsFsmInstrNop(EmitI(0x16, rt, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001127}
1128
1129void MipsAssembler::Blezc(Register rt, uint16_t imm16) {
1130 CHECK(IsR6());
1131 CHECK_NE(rt, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001132 DsFsmInstrNop(EmitI(0x16, static_cast<Register>(0), rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001133}
1134
1135void MipsAssembler::Bltuc(Register rs, Register rt, uint16_t imm16) {
1136 CHECK(IsR6());
1137 CHECK_NE(rs, ZERO);
1138 CHECK_NE(rt, ZERO);
1139 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001140 DsFsmInstrNop(EmitI(0x7, rs, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001141}
1142
1143void MipsAssembler::Bgeuc(Register rs, Register rt, uint16_t imm16) {
1144 CHECK(IsR6());
1145 CHECK_NE(rs, ZERO);
1146 CHECK_NE(rt, ZERO);
1147 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001148 DsFsmInstrNop(EmitI(0x6, rs, rt, imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001149}
1150
1151void MipsAssembler::Beqc(Register rs, Register rt, uint16_t imm16) {
1152 CHECK(IsR6());
1153 CHECK_NE(rs, ZERO);
1154 CHECK_NE(rt, ZERO);
1155 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001156 DsFsmInstrNop(EmitI(0x8, std::min(rs, rt), std::max(rs, rt), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001157}
1158
1159void MipsAssembler::Bnec(Register rs, Register rt, uint16_t imm16) {
1160 CHECK(IsR6());
1161 CHECK_NE(rs, ZERO);
1162 CHECK_NE(rt, ZERO);
1163 CHECK_NE(rs, rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001164 DsFsmInstrNop(EmitI(0x18, std::min(rs, rt), std::max(rs, rt), imm16));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001165}
1166
1167void MipsAssembler::Beqzc(Register rs, uint32_t imm21) {
1168 CHECK(IsR6());
1169 CHECK_NE(rs, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001170 DsFsmInstrNop(EmitI21(0x36, rs, imm21));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001171}
1172
1173void MipsAssembler::Bnezc(Register rs, uint32_t imm21) {
1174 CHECK(IsR6());
1175 CHECK_NE(rs, ZERO);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001176 DsFsmInstrNop(EmitI21(0x3E, rs, imm21));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001177}
1178
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001179void MipsAssembler::Bc1eqz(FRegister ft, uint16_t imm16) {
1180 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001181 DsFsmInstrNop(EmitFI(0x11, 0x9, ft, imm16));
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001182}
1183
1184void MipsAssembler::Bc1nez(FRegister ft, uint16_t imm16) {
1185 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001186 DsFsmInstrNop(EmitFI(0x11, 0xD, ft, imm16));
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001187}
1188
1189void MipsAssembler::EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001190 switch (cond) {
1191 case kCondLTZ:
1192 CHECK_EQ(rt, ZERO);
1193 Bltz(rs, imm16);
1194 break;
1195 case kCondGEZ:
1196 CHECK_EQ(rt, ZERO);
1197 Bgez(rs, imm16);
1198 break;
1199 case kCondLEZ:
1200 CHECK_EQ(rt, ZERO);
1201 Blez(rs, imm16);
1202 break;
1203 case kCondGTZ:
1204 CHECK_EQ(rt, ZERO);
1205 Bgtz(rs, imm16);
1206 break;
1207 case kCondEQ:
1208 Beq(rs, rt, imm16);
1209 break;
1210 case kCondNE:
1211 Bne(rs, rt, imm16);
1212 break;
1213 case kCondEQZ:
1214 CHECK_EQ(rt, ZERO);
1215 Beqz(rs, imm16);
1216 break;
1217 case kCondNEZ:
1218 CHECK_EQ(rt, ZERO);
1219 Bnez(rs, imm16);
1220 break;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001221 case kCondF:
1222 CHECK_EQ(rt, ZERO);
1223 Bc1f(static_cast<int>(rs), imm16);
1224 break;
1225 case kCondT:
1226 CHECK_EQ(rt, ZERO);
1227 Bc1t(static_cast<int>(rs), imm16);
1228 break;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001229 case kCondLT:
1230 case kCondGE:
1231 case kCondLE:
1232 case kCondGT:
1233 case kCondLTU:
1234 case kCondGEU:
1235 case kUncond:
1236 // We don't support synthetic R2 branches (preceded with slt[u]) at this level
1237 // (R2 doesn't have branches to compare 2 registers using <, <=, >=, >).
1238 LOG(FATAL) << "Unexpected branch condition " << cond;
1239 UNREACHABLE();
1240 }
1241}
1242
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001243void MipsAssembler::EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001244 switch (cond) {
1245 case kCondLT:
1246 Bltc(rs, rt, imm16_21);
1247 break;
1248 case kCondGE:
1249 Bgec(rs, rt, imm16_21);
1250 break;
1251 case kCondLE:
1252 Bgec(rt, rs, imm16_21);
1253 break;
1254 case kCondGT:
1255 Bltc(rt, rs, imm16_21);
1256 break;
1257 case kCondLTZ:
1258 CHECK_EQ(rt, ZERO);
1259 Bltzc(rs, imm16_21);
1260 break;
1261 case kCondGEZ:
1262 CHECK_EQ(rt, ZERO);
1263 Bgezc(rs, imm16_21);
1264 break;
1265 case kCondLEZ:
1266 CHECK_EQ(rt, ZERO);
1267 Blezc(rs, imm16_21);
1268 break;
1269 case kCondGTZ:
1270 CHECK_EQ(rt, ZERO);
1271 Bgtzc(rs, imm16_21);
1272 break;
1273 case kCondEQ:
1274 Beqc(rs, rt, imm16_21);
1275 break;
1276 case kCondNE:
1277 Bnec(rs, rt, imm16_21);
1278 break;
1279 case kCondEQZ:
1280 CHECK_EQ(rt, ZERO);
1281 Beqzc(rs, imm16_21);
1282 break;
1283 case kCondNEZ:
1284 CHECK_EQ(rt, ZERO);
1285 Bnezc(rs, imm16_21);
1286 break;
1287 case kCondLTU:
1288 Bltuc(rs, rt, imm16_21);
1289 break;
1290 case kCondGEU:
1291 Bgeuc(rs, rt, imm16_21);
1292 break;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001293 case kCondF:
1294 CHECK_EQ(rt, ZERO);
1295 Bc1eqz(static_cast<FRegister>(rs), imm16_21);
1296 break;
1297 case kCondT:
1298 CHECK_EQ(rt, ZERO);
1299 Bc1nez(static_cast<FRegister>(rs), imm16_21);
1300 break;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001301 case kUncond:
1302 LOG(FATAL) << "Unexpected branch condition " << cond;
1303 UNREACHABLE();
1304 }
jeffhao7fbee072012-08-24 17:56:54 -07001305}
1306
1307void MipsAssembler::AddS(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001308 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x0), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001309}
1310
1311void MipsAssembler::SubS(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001312 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x1), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001313}
1314
1315void MipsAssembler::MulS(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001316 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x2), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001317}
1318
1319void MipsAssembler::DivS(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001320 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x3), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001321}
1322
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001323void MipsAssembler::AddD(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001324 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x0), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001325}
1326
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001327void MipsAssembler::SubD(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001328 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x1), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001329}
1330
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001331void MipsAssembler::MulD(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001332 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x2), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001333}
1334
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001335void MipsAssembler::DivD(FRegister fd, FRegister fs, FRegister ft) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001336 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x3), fd, fs, ft);
jeffhao7fbee072012-08-24 17:56:54 -07001337}
1338
Chris Larsenb74353a2015-11-20 09:07:09 -08001339void MipsAssembler::SqrtS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001340 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x4), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001341}
1342
1343void MipsAssembler::SqrtD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001344 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x4), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001345}
1346
1347void MipsAssembler::AbsS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001348 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x5), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001349}
1350
1351void MipsAssembler::AbsD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001352 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x5), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001353}
1354
jeffhao7fbee072012-08-24 17:56:54 -07001355void MipsAssembler::MovS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001356 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x6), fd, fs, fs);
jeffhao7fbee072012-08-24 17:56:54 -07001357}
1358
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001359void MipsAssembler::MovD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001360 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x6), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001361}
1362
1363void MipsAssembler::NegS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001364 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x7), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001365}
1366
1367void MipsAssembler::NegD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001368 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x7), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001369}
1370
Chris Larsenb74353a2015-11-20 09:07:09 -08001371void MipsAssembler::CunS(FRegister fs, FRegister ft) {
1372 CunS(0, fs, ft);
1373}
1374
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001375void MipsAssembler::CunS(int cc, FRegister fs, FRegister ft) {
1376 CHECK(!IsR6());
1377 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001378 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x31), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001379}
1380
Chris Larsenb74353a2015-11-20 09:07:09 -08001381void MipsAssembler::CeqS(FRegister fs, FRegister ft) {
1382 CeqS(0, fs, ft);
1383}
1384
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001385void MipsAssembler::CeqS(int cc, FRegister fs, FRegister ft) {
1386 CHECK(!IsR6());
1387 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001388 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x32), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001389}
1390
Chris Larsenb74353a2015-11-20 09:07:09 -08001391void MipsAssembler::CueqS(FRegister fs, FRegister ft) {
1392 CueqS(0, fs, ft);
1393}
1394
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001395void MipsAssembler::CueqS(int cc, FRegister fs, FRegister ft) {
1396 CHECK(!IsR6());
1397 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001398 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x33), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001399}
1400
Chris Larsenb74353a2015-11-20 09:07:09 -08001401void MipsAssembler::ColtS(FRegister fs, FRegister ft) {
1402 ColtS(0, fs, ft);
1403}
1404
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001405void MipsAssembler::ColtS(int cc, FRegister fs, FRegister ft) {
1406 CHECK(!IsR6());
1407 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001408 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x34), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001409}
1410
Chris Larsenb74353a2015-11-20 09:07:09 -08001411void MipsAssembler::CultS(FRegister fs, FRegister ft) {
1412 CultS(0, fs, ft);
1413}
1414
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001415void MipsAssembler::CultS(int cc, FRegister fs, FRegister ft) {
1416 CHECK(!IsR6());
1417 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001418 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x35), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001419}
1420
Chris Larsenb74353a2015-11-20 09:07:09 -08001421void MipsAssembler::ColeS(FRegister fs, FRegister ft) {
1422 ColeS(0, fs, ft);
1423}
1424
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001425void MipsAssembler::ColeS(int cc, FRegister fs, FRegister ft) {
1426 CHECK(!IsR6());
1427 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001428 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x36), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001429}
1430
Chris Larsenb74353a2015-11-20 09:07:09 -08001431void MipsAssembler::CuleS(FRegister fs, FRegister ft) {
1432 CuleS(0, fs, ft);
1433}
1434
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001435void MipsAssembler::CuleS(int cc, FRegister fs, FRegister ft) {
1436 CHECK(!IsR6());
1437 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001438 DsFsmInstrCff(EmitFR(0x11, 0x10, ft, fs, static_cast<FRegister>(cc << 2), 0x37), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001439}
1440
Chris Larsenb74353a2015-11-20 09:07:09 -08001441void MipsAssembler::CunD(FRegister fs, FRegister ft) {
1442 CunD(0, fs, ft);
1443}
1444
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001445void MipsAssembler::CunD(int cc, FRegister fs, FRegister ft) {
1446 CHECK(!IsR6());
1447 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001448 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x31), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001449}
1450
Chris Larsenb74353a2015-11-20 09:07:09 -08001451void MipsAssembler::CeqD(FRegister fs, FRegister ft) {
1452 CeqD(0, fs, ft);
1453}
1454
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001455void MipsAssembler::CeqD(int cc, FRegister fs, FRegister ft) {
1456 CHECK(!IsR6());
1457 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001458 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x32), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001459}
1460
Chris Larsenb74353a2015-11-20 09:07:09 -08001461void MipsAssembler::CueqD(FRegister fs, FRegister ft) {
1462 CueqD(0, fs, ft);
1463}
1464
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001465void MipsAssembler::CueqD(int cc, FRegister fs, FRegister ft) {
1466 CHECK(!IsR6());
1467 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001468 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x33), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001469}
1470
Chris Larsenb74353a2015-11-20 09:07:09 -08001471void MipsAssembler::ColtD(FRegister fs, FRegister ft) {
1472 ColtD(0, fs, ft);
1473}
1474
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001475void MipsAssembler::ColtD(int cc, FRegister fs, FRegister ft) {
1476 CHECK(!IsR6());
1477 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001478 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x34), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001479}
1480
Chris Larsenb74353a2015-11-20 09:07:09 -08001481void MipsAssembler::CultD(FRegister fs, FRegister ft) {
1482 CultD(0, fs, ft);
1483}
1484
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001485void MipsAssembler::CultD(int cc, FRegister fs, FRegister ft) {
1486 CHECK(!IsR6());
1487 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001488 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x35), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001489}
1490
Chris Larsenb74353a2015-11-20 09:07:09 -08001491void MipsAssembler::ColeD(FRegister fs, FRegister ft) {
1492 ColeD(0, fs, ft);
1493}
1494
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001495void MipsAssembler::ColeD(int cc, FRegister fs, FRegister ft) {
1496 CHECK(!IsR6());
1497 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001498 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x36), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001499}
1500
Chris Larsenb74353a2015-11-20 09:07:09 -08001501void MipsAssembler::CuleD(FRegister fs, FRegister ft) {
1502 CuleD(0, fs, ft);
1503}
1504
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001505void MipsAssembler::CuleD(int cc, FRegister fs, FRegister ft) {
1506 CHECK(!IsR6());
1507 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001508 DsFsmInstrCff(EmitFR(0x11, 0x11, ft, fs, static_cast<FRegister>(cc << 2), 0x37), cc, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001509}
1510
1511void MipsAssembler::CmpUnS(FRegister fd, FRegister fs, FRegister ft) {
1512 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001513 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x01), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001514}
1515
1516void MipsAssembler::CmpEqS(FRegister fd, FRegister fs, FRegister ft) {
1517 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001518 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x02), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001519}
1520
1521void MipsAssembler::CmpUeqS(FRegister fd, FRegister fs, FRegister ft) {
1522 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001523 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x03), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001524}
1525
1526void MipsAssembler::CmpLtS(FRegister fd, FRegister fs, FRegister ft) {
1527 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001528 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x04), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001529}
1530
1531void MipsAssembler::CmpUltS(FRegister fd, FRegister fs, FRegister ft) {
1532 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001533 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x05), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001534}
1535
1536void MipsAssembler::CmpLeS(FRegister fd, FRegister fs, FRegister ft) {
1537 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001538 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x06), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001539}
1540
1541void MipsAssembler::CmpUleS(FRegister fd, FRegister fs, FRegister ft) {
1542 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001543 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x07), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001544}
1545
1546void MipsAssembler::CmpOrS(FRegister fd, FRegister fs, FRegister ft) {
1547 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001548 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x11), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001549}
1550
1551void MipsAssembler::CmpUneS(FRegister fd, FRegister fs, FRegister ft) {
1552 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001553 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x12), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001554}
1555
1556void MipsAssembler::CmpNeS(FRegister fd, FRegister fs, FRegister ft) {
1557 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001558 DsFsmInstrFff(EmitFR(0x11, 0x14, ft, fs, fd, 0x13), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001559}
1560
1561void MipsAssembler::CmpUnD(FRegister fd, FRegister fs, FRegister ft) {
1562 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001563 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x01), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001564}
1565
1566void MipsAssembler::CmpEqD(FRegister fd, FRegister fs, FRegister ft) {
1567 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001568 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x02), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001569}
1570
1571void MipsAssembler::CmpUeqD(FRegister fd, FRegister fs, FRegister ft) {
1572 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001573 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x03), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001574}
1575
1576void MipsAssembler::CmpLtD(FRegister fd, FRegister fs, FRegister ft) {
1577 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001578 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x04), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001579}
1580
1581void MipsAssembler::CmpUltD(FRegister fd, FRegister fs, FRegister ft) {
1582 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001583 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x05), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001584}
1585
1586void MipsAssembler::CmpLeD(FRegister fd, FRegister fs, FRegister ft) {
1587 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001588 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x06), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001589}
1590
1591void MipsAssembler::CmpUleD(FRegister fd, FRegister fs, FRegister ft) {
1592 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001593 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x07), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001594}
1595
1596void MipsAssembler::CmpOrD(FRegister fd, FRegister fs, FRegister ft) {
1597 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001598 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x11), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001599}
1600
1601void MipsAssembler::CmpUneD(FRegister fd, FRegister fs, FRegister ft) {
1602 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001603 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x12), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001604}
1605
1606void MipsAssembler::CmpNeD(FRegister fd, FRegister fs, FRegister ft) {
1607 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001608 DsFsmInstrFff(EmitFR(0x11, 0x15, ft, fs, fd, 0x13), fd, fs, ft);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001609}
1610
1611void MipsAssembler::Movf(Register rd, Register rs, int cc) {
1612 CHECK(!IsR6());
1613 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001614 DsFsmInstrRrrc(EmitR(0, rs, static_cast<Register>(cc << 2), rd, 0, 0x01), rd, rs, cc);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001615}
1616
1617void MipsAssembler::Movt(Register rd, Register rs, int cc) {
1618 CHECK(!IsR6());
1619 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001620 DsFsmInstrRrrc(EmitR(0, rs, static_cast<Register>((cc << 2) | 1), rd, 0, 0x01), rd, rs, cc);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001621}
1622
Chris Larsenb74353a2015-11-20 09:07:09 -08001623void MipsAssembler::MovfS(FRegister fd, FRegister fs, int cc) {
1624 CHECK(!IsR6());
1625 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001626 DsFsmInstrFffc(EmitFR(0x11, 0x10, static_cast<FRegister>(cc << 2), fs, fd, 0x11), fd, fs, cc);
Chris Larsenb74353a2015-11-20 09:07:09 -08001627}
1628
1629void MipsAssembler::MovfD(FRegister fd, FRegister fs, int cc) {
1630 CHECK(!IsR6());
1631 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001632 DsFsmInstrFffc(EmitFR(0x11, 0x11, static_cast<FRegister>(cc << 2), fs, fd, 0x11), fd, fs, cc);
Chris Larsenb74353a2015-11-20 09:07:09 -08001633}
1634
1635void MipsAssembler::MovtS(FRegister fd, FRegister fs, int cc) {
1636 CHECK(!IsR6());
1637 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001638 DsFsmInstrFffc(EmitFR(0x11, 0x10, static_cast<FRegister>((cc << 2) | 1), fs, fd, 0x11),
1639 fd,
1640 fs,
1641 cc);
Chris Larsenb74353a2015-11-20 09:07:09 -08001642}
1643
1644void MipsAssembler::MovtD(FRegister fd, FRegister fs, int cc) {
1645 CHECK(!IsR6());
1646 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001647 DsFsmInstrFffc(EmitFR(0x11, 0x11, static_cast<FRegister>((cc << 2) | 1), fs, fd, 0x11),
1648 fd,
1649 fs,
1650 cc);
Chris Larsenb74353a2015-11-20 09:07:09 -08001651}
1652
Alexey Frunze674b9ee2016-09-20 14:54:15 -07001653void MipsAssembler::MovzS(FRegister fd, FRegister fs, Register rt) {
1654 CHECK(!IsR6());
1655 DsFsmInstrFffr(EmitFR(0x11, 0x10, static_cast<FRegister>(rt), fs, fd, 0x12), fd, fs, rt);
1656}
1657
1658void MipsAssembler::MovzD(FRegister fd, FRegister fs, Register rt) {
1659 CHECK(!IsR6());
1660 DsFsmInstrFffr(EmitFR(0x11, 0x11, static_cast<FRegister>(rt), fs, fd, 0x12), fd, fs, rt);
1661}
1662
1663void MipsAssembler::MovnS(FRegister fd, FRegister fs, Register rt) {
1664 CHECK(!IsR6());
1665 DsFsmInstrFffr(EmitFR(0x11, 0x10, static_cast<FRegister>(rt), fs, fd, 0x13), fd, fs, rt);
1666}
1667
1668void MipsAssembler::MovnD(FRegister fd, FRegister fs, Register rt) {
1669 CHECK(!IsR6());
1670 DsFsmInstrFffr(EmitFR(0x11, 0x11, static_cast<FRegister>(rt), fs, fd, 0x13), fd, fs, rt);
1671}
1672
Chris Larsenb74353a2015-11-20 09:07:09 -08001673void MipsAssembler::SelS(FRegister fd, FRegister fs, FRegister ft) {
1674 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001675 DsFsmInstrFfff(EmitFR(0x11, 0x10, ft, fs, fd, 0x10), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001676}
1677
1678void MipsAssembler::SelD(FRegister fd, FRegister fs, FRegister ft) {
1679 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001680 DsFsmInstrFfff(EmitFR(0x11, 0x11, ft, fs, fd, 0x10), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001681}
1682
Alexey Frunze674b9ee2016-09-20 14:54:15 -07001683void MipsAssembler::SeleqzS(FRegister fd, FRegister fs, FRegister ft) {
1684 CHECK(IsR6());
1685 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x14), fd, fs, ft);
1686}
1687
1688void MipsAssembler::SeleqzD(FRegister fd, FRegister fs, FRegister ft) {
1689 CHECK(IsR6());
1690 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x14), fd, fs, ft);
1691}
1692
1693void MipsAssembler::SelnezS(FRegister fd, FRegister fs, FRegister ft) {
1694 CHECK(IsR6());
1695 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x17), fd, fs, ft);
1696}
1697
1698void MipsAssembler::SelnezD(FRegister fd, FRegister fs, FRegister ft) {
1699 CHECK(IsR6());
1700 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x17), fd, fs, ft);
1701}
1702
Chris Larsenb74353a2015-11-20 09:07:09 -08001703void MipsAssembler::ClassS(FRegister fd, FRegister fs) {
1704 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001705 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x1b), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001706}
1707
1708void MipsAssembler::ClassD(FRegister fd, FRegister fs) {
1709 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001710 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x1b), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001711}
1712
1713void MipsAssembler::MinS(FRegister fd, FRegister fs, FRegister ft) {
1714 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001715 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x1c), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001716}
1717
1718void MipsAssembler::MinD(FRegister fd, FRegister fs, FRegister ft) {
1719 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001720 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x1c), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001721}
1722
1723void MipsAssembler::MaxS(FRegister fd, FRegister fs, FRegister ft) {
1724 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001725 DsFsmInstrFff(EmitFR(0x11, 0x10, ft, fs, fd, 0x1e), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001726}
1727
1728void MipsAssembler::MaxD(FRegister fd, FRegister fs, FRegister ft) {
1729 CHECK(IsR6());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001730 DsFsmInstrFff(EmitFR(0x11, 0x11, ft, fs, fd, 0x1e), fd, fs, ft);
Chris Larsenb74353a2015-11-20 09:07:09 -08001731}
1732
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001733void MipsAssembler::TruncLS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001734 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x09), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001735}
1736
1737void MipsAssembler::TruncLD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001738 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x09), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001739}
1740
1741void MipsAssembler::TruncWS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001742 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x0D), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001743}
1744
1745void MipsAssembler::TruncWD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001746 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x0D), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001747}
1748
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001749void MipsAssembler::Cvtsw(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001750 DsFsmInstrFff(EmitFR(0x11, 0x14, static_cast<FRegister>(0), fs, fd, 0x20), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001751}
1752
1753void MipsAssembler::Cvtdw(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001754 DsFsmInstrFff(EmitFR(0x11, 0x14, static_cast<FRegister>(0), fs, fd, 0x21), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001755}
1756
1757void MipsAssembler::Cvtsd(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001758 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0x20), fd, fs, fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001759}
1760
1761void MipsAssembler::Cvtds(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001762 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0x21), fd, fs, fs);
jeffhao7fbee072012-08-24 17:56:54 -07001763}
1764
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001765void MipsAssembler::Cvtsl(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001766 DsFsmInstrFff(EmitFR(0x11, 0x15, static_cast<FRegister>(0), fs, fd, 0x20), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001767}
1768
1769void MipsAssembler::Cvtdl(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001770 DsFsmInstrFff(EmitFR(0x11, 0x15, static_cast<FRegister>(0), fs, fd, 0x21), fd, fs, fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08001771}
1772
Chris Larsenb74353a2015-11-20 09:07:09 -08001773void MipsAssembler::FloorWS(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001774 DsFsmInstrFff(EmitFR(0x11, 0x10, static_cast<FRegister>(0), fs, fd, 0xf), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001775}
1776
1777void MipsAssembler::FloorWD(FRegister fd, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001778 DsFsmInstrFff(EmitFR(0x11, 0x11, static_cast<FRegister>(0), fs, fd, 0xf), fd, fs, fs);
Chris Larsenb74353a2015-11-20 09:07:09 -08001779}
1780
jeffhao7fbee072012-08-24 17:56:54 -07001781void MipsAssembler::Mfc1(Register rt, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001782 DsFsmInstrRf(EmitFR(0x11, 0x00, static_cast<FRegister>(rt), fs, static_cast<FRegister>(0), 0x0),
1783 rt,
1784 fs);
jeffhao7fbee072012-08-24 17:56:54 -07001785}
1786
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001787void MipsAssembler::Mtc1(Register rt, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001788 DsFsmInstrFr(EmitFR(0x11, 0x04, static_cast<FRegister>(rt), fs, static_cast<FRegister>(0), 0x0),
1789 fs,
1790 rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001791}
1792
1793void MipsAssembler::Mfhc1(Register rt, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001794 DsFsmInstrRf(EmitFR(0x11, 0x03, static_cast<FRegister>(rt), fs, static_cast<FRegister>(0), 0x0),
1795 rt,
1796 fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001797}
1798
1799void MipsAssembler::Mthc1(Register rt, FRegister fs) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001800 DsFsmInstrFr(EmitFR(0x11, 0x07, static_cast<FRegister>(rt), fs, static_cast<FRegister>(0), 0x0),
1801 fs,
1802 rt);
jeffhao7fbee072012-08-24 17:56:54 -07001803}
1804
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001805void MipsAssembler::MoveFromFpuHigh(Register rt, FRegister fs) {
1806 if (Is32BitFPU()) {
1807 CHECK_EQ(fs % 2, 0) << fs;
1808 Mfc1(rt, static_cast<FRegister>(fs + 1));
1809 } else {
1810 Mfhc1(rt, fs);
1811 }
1812}
1813
1814void MipsAssembler::MoveToFpuHigh(Register rt, FRegister fs) {
1815 if (Is32BitFPU()) {
1816 CHECK_EQ(fs % 2, 0) << fs;
1817 Mtc1(rt, static_cast<FRegister>(fs + 1));
1818 } else {
1819 Mthc1(rt, fs);
1820 }
1821}
1822
jeffhao7fbee072012-08-24 17:56:54 -07001823void MipsAssembler::Lwc1(FRegister ft, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001824 DsFsmInstrFr(EmitI(0x31, rs, static_cast<Register>(ft), imm16), ft, rs);
jeffhao7fbee072012-08-24 17:56:54 -07001825}
1826
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001827void MipsAssembler::Ldc1(FRegister ft, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001828 DsFsmInstrFr(EmitI(0x35, rs, static_cast<Register>(ft), imm16), ft, rs);
jeffhao7fbee072012-08-24 17:56:54 -07001829}
1830
1831void MipsAssembler::Swc1(FRegister ft, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001832 DsFsmInstrFR(EmitI(0x39, rs, static_cast<Register>(ft), imm16), ft, rs);
jeffhao7fbee072012-08-24 17:56:54 -07001833}
1834
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001835void MipsAssembler::Sdc1(FRegister ft, Register rs, uint16_t imm16) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001836 DsFsmInstrFR(EmitI(0x3d, rs, static_cast<Register>(ft), imm16), ft, rs);
jeffhao7fbee072012-08-24 17:56:54 -07001837}
1838
1839void MipsAssembler::Break() {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001840 DsFsmInstrNop(EmitR(0, ZERO, ZERO, ZERO, 0, 0xD));
jeffhao7fbee072012-08-24 17:56:54 -07001841}
1842
jeffhao07030602012-09-26 14:33:14 -07001843void MipsAssembler::Nop() {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001844 DsFsmInstrNop(EmitR(0x0, ZERO, ZERO, ZERO, 0, 0x0));
1845}
1846
1847void MipsAssembler::NopIfNoReordering() {
1848 if (!reordering_) {
1849 Nop();
1850 }
jeffhao07030602012-09-26 14:33:14 -07001851}
1852
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001853void MipsAssembler::Move(Register rd, Register rs) {
1854 Or(rd, rs, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -07001855}
1856
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001857void MipsAssembler::Clear(Register rd) {
1858 Move(rd, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -07001859}
1860
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001861void MipsAssembler::Not(Register rd, Register rs) {
1862 Nor(rd, rs, ZERO);
jeffhao7fbee072012-08-24 17:56:54 -07001863}
1864
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001865void MipsAssembler::Push(Register rs) {
1866 IncreaseFrameSize(kMipsWordSize);
1867 Sw(rs, SP, 0);
jeffhao7fbee072012-08-24 17:56:54 -07001868}
1869
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001870void MipsAssembler::Pop(Register rd) {
1871 Lw(rd, SP, 0);
1872 DecreaseFrameSize(kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07001873}
1874
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001875void MipsAssembler::PopAndReturn(Register rd, Register rt) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001876 bool reordering = SetReorder(false);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001877 Lw(rd, SP, 0);
1878 Jr(rt);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001879 DecreaseFrameSize(kMipsWordSize); // Single instruction in delay slot.
1880 SetReorder(reordering);
jeffhao7fbee072012-08-24 17:56:54 -07001881}
1882
Lena Djokic0758ae72017-05-23 11:06:23 +02001883void MipsAssembler::AndV(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1884 CHECK(HasMsa());
1885 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0x1e),
1886 static_cast<FRegister>(wd),
1887 static_cast<FRegister>(ws),
1888 static_cast<FRegister>(wt));
1889}
1890
1891void MipsAssembler::OrV(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1892 CHECK(HasMsa());
1893 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0x1e),
1894 static_cast<FRegister>(wd),
1895 static_cast<FRegister>(ws),
1896 static_cast<FRegister>(wt));
1897}
1898
1899void MipsAssembler::NorV(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1900 CHECK(HasMsa());
1901 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0x1e),
1902 static_cast<FRegister>(wd),
1903 static_cast<FRegister>(ws),
1904 static_cast<FRegister>(wt));
1905}
1906
1907void MipsAssembler::XorV(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1908 CHECK(HasMsa());
1909 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0x1e),
1910 static_cast<FRegister>(wd),
1911 static_cast<FRegister>(ws),
1912 static_cast<FRegister>(wt));
1913}
1914
1915void MipsAssembler::AddvB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1916 CHECK(HasMsa());
1917 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0xe),
1918 static_cast<FRegister>(wd),
1919 static_cast<FRegister>(ws),
1920 static_cast<FRegister>(wt));
1921}
1922
1923void MipsAssembler::AddvH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1924 CHECK(HasMsa());
1925 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0xe),
1926 static_cast<FRegister>(wd),
1927 static_cast<FRegister>(ws),
1928 static_cast<FRegister>(wt));
1929}
1930
1931void MipsAssembler::AddvW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1932 CHECK(HasMsa());
1933 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0xe),
1934 static_cast<FRegister>(wd),
1935 static_cast<FRegister>(ws),
1936 static_cast<FRegister>(wt));
1937}
1938
1939void MipsAssembler::AddvD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1940 CHECK(HasMsa());
1941 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0xe),
1942 static_cast<FRegister>(wd),
1943 static_cast<FRegister>(ws),
1944 static_cast<FRegister>(wt));
1945}
1946
1947void MipsAssembler::SubvB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1948 CHECK(HasMsa());
1949 DsFsmInstrFff(EmitMsa3R(0x1, 0x0, wt, ws, wd, 0xe),
1950 static_cast<FRegister>(wd),
1951 static_cast<FRegister>(ws),
1952 static_cast<FRegister>(wt));
1953}
1954
1955void MipsAssembler::SubvH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1956 CHECK(HasMsa());
1957 DsFsmInstrFff(EmitMsa3R(0x1, 0x1, wt, ws, wd, 0xe),
1958 static_cast<FRegister>(wd),
1959 static_cast<FRegister>(ws),
1960 static_cast<FRegister>(wt));
1961}
1962
1963void MipsAssembler::SubvW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1964 CHECK(HasMsa());
1965 DsFsmInstrFff(EmitMsa3R(0x1, 0x2, wt, ws, wd, 0xe),
1966 static_cast<FRegister>(wd),
1967 static_cast<FRegister>(ws),
1968 static_cast<FRegister>(wt));
1969}
1970
1971void MipsAssembler::SubvD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1972 CHECK(HasMsa());
1973 DsFsmInstrFff(EmitMsa3R(0x1, 0x3, wt, ws, wd, 0xe),
1974 static_cast<FRegister>(wd),
1975 static_cast<FRegister>(ws),
1976 static_cast<FRegister>(wt));
1977}
1978
1979void MipsAssembler::MulvB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1980 CHECK(HasMsa());
1981 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0x12),
1982 static_cast<FRegister>(wd),
1983 static_cast<FRegister>(ws),
1984 static_cast<FRegister>(wt));
1985}
1986
1987void MipsAssembler::MulvH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1988 CHECK(HasMsa());
1989 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0x12),
1990 static_cast<FRegister>(wd),
1991 static_cast<FRegister>(ws),
1992 static_cast<FRegister>(wt));
1993}
1994
1995void MipsAssembler::MulvW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
1996 CHECK(HasMsa());
1997 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0x12),
1998 static_cast<FRegister>(wd),
1999 static_cast<FRegister>(ws),
2000 static_cast<FRegister>(wt));
2001}
2002
2003void MipsAssembler::MulvD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2004 CHECK(HasMsa());
2005 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0x12),
2006 static_cast<FRegister>(wd),
2007 static_cast<FRegister>(ws),
2008 static_cast<FRegister>(wt));
2009}
2010
2011void MipsAssembler::Div_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2012 CHECK(HasMsa());
2013 DsFsmInstrFff(EmitMsa3R(0x4, 0x0, wt, ws, wd, 0x12),
2014 static_cast<FRegister>(wd),
2015 static_cast<FRegister>(ws),
2016 static_cast<FRegister>(wt));
2017}
2018
2019void MipsAssembler::Div_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2020 CHECK(HasMsa());
2021 DsFsmInstrFff(EmitMsa3R(0x4, 0x1, wt, ws, wd, 0x12),
2022 static_cast<FRegister>(wd),
2023 static_cast<FRegister>(ws),
2024 static_cast<FRegister>(wt));
2025}
2026
2027void MipsAssembler::Div_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2028 CHECK(HasMsa());
2029 DsFsmInstrFff(EmitMsa3R(0x4, 0x2, wt, ws, wd, 0x12),
2030 static_cast<FRegister>(wd),
2031 static_cast<FRegister>(ws),
2032 static_cast<FRegister>(wt));
2033}
2034
2035void MipsAssembler::Div_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2036 CHECK(HasMsa());
2037 DsFsmInstrFff(EmitMsa3R(0x4, 0x3, wt, ws, wd, 0x12),
2038 static_cast<FRegister>(wd),
2039 static_cast<FRegister>(ws),
2040 static_cast<FRegister>(wt));
2041}
2042
2043void MipsAssembler::Div_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2044 CHECK(HasMsa());
2045 DsFsmInstrFff(EmitMsa3R(0x5, 0x0, wt, ws, wd, 0x12),
2046 static_cast<FRegister>(wd),
2047 static_cast<FRegister>(ws),
2048 static_cast<FRegister>(wt));
2049}
2050
2051void MipsAssembler::Div_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2052 CHECK(HasMsa());
2053 DsFsmInstrFff(EmitMsa3R(0x5, 0x1, wt, ws, wd, 0x12),
2054 static_cast<FRegister>(wd),
2055 static_cast<FRegister>(ws),
2056 static_cast<FRegister>(wt));
2057}
2058
2059void MipsAssembler::Div_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2060 CHECK(HasMsa());
2061 DsFsmInstrFff(EmitMsa3R(0x5, 0x2, wt, ws, wd, 0x12),
2062 static_cast<FRegister>(wd),
2063 static_cast<FRegister>(ws),
2064 static_cast<FRegister>(wt));
2065}
2066
2067void MipsAssembler::Div_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2068 CHECK(HasMsa());
2069 DsFsmInstrFff(EmitMsa3R(0x5, 0x3, wt, ws, wd, 0x12),
2070 static_cast<FRegister>(wd),
2071 static_cast<FRegister>(ws),
2072 static_cast<FRegister>(wt));
2073}
2074
2075void MipsAssembler::Mod_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2076 CHECK(HasMsa());
2077 DsFsmInstrFff(EmitMsa3R(0x6, 0x0, wt, ws, wd, 0x12),
2078 static_cast<FRegister>(wd),
2079 static_cast<FRegister>(ws),
2080 static_cast<FRegister>(wt));
2081}
2082
2083void MipsAssembler::Mod_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2084 CHECK(HasMsa());
2085 DsFsmInstrFff(EmitMsa3R(0x6, 0x1, wt, ws, wd, 0x12),
2086 static_cast<FRegister>(wd),
2087 static_cast<FRegister>(ws),
2088 static_cast<FRegister>(wt));
2089}
2090
2091void MipsAssembler::Mod_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2092 CHECK(HasMsa());
2093 DsFsmInstrFff(EmitMsa3R(0x6, 0x2, wt, ws, wd, 0x12),
2094 static_cast<FRegister>(wd),
2095 static_cast<FRegister>(ws),
2096 static_cast<FRegister>(wt));
2097}
2098
2099void MipsAssembler::Mod_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2100 CHECK(HasMsa());
2101 DsFsmInstrFff(EmitMsa3R(0x6, 0x3, wt, ws, wd, 0x12),
2102 static_cast<FRegister>(wd),
2103 static_cast<FRegister>(ws),
2104 static_cast<FRegister>(wt));
2105}
2106
2107void MipsAssembler::Mod_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2108 CHECK(HasMsa());
2109 DsFsmInstrFff(EmitMsa3R(0x7, 0x0, wt, ws, wd, 0x12),
2110 static_cast<FRegister>(wd),
2111 static_cast<FRegister>(ws),
2112 static_cast<FRegister>(wt));
2113}
2114
2115void MipsAssembler::Mod_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2116 CHECK(HasMsa());
2117 DsFsmInstrFff(EmitMsa3R(0x7, 0x1, wt, ws, wd, 0x12),
2118 static_cast<FRegister>(wd),
2119 static_cast<FRegister>(ws),
2120 static_cast<FRegister>(wt));
2121}
2122
2123void MipsAssembler::Mod_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2124 CHECK(HasMsa());
2125 DsFsmInstrFff(EmitMsa3R(0x7, 0x2, wt, ws, wd, 0x12),
2126 static_cast<FRegister>(wd),
2127 static_cast<FRegister>(ws),
2128 static_cast<FRegister>(wt));
2129}
2130
2131void MipsAssembler::Mod_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2132 CHECK(HasMsa());
2133 DsFsmInstrFff(EmitMsa3R(0x7, 0x3, wt, ws, wd, 0x12),
2134 static_cast<FRegister>(wd),
2135 static_cast<FRegister>(ws),
2136 static_cast<FRegister>(wt));
2137}
2138
2139void MipsAssembler::Add_aB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2140 CHECK(HasMsa());
2141 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0x10),
2142 static_cast<FRegister>(wd),
2143 static_cast<FRegister>(ws),
2144 static_cast<FRegister>(wt));
2145}
2146
2147void MipsAssembler::Add_aH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2148 CHECK(HasMsa());
2149 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0x10),
2150 static_cast<FRegister>(wd),
2151 static_cast<FRegister>(ws),
2152 static_cast<FRegister>(wt));
2153}
2154
2155void MipsAssembler::Add_aW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2156 CHECK(HasMsa());
2157 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0x10),
2158 static_cast<FRegister>(wd),
2159 static_cast<FRegister>(ws),
2160 static_cast<FRegister>(wt));
2161}
2162
2163void MipsAssembler::Add_aD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2164 CHECK(HasMsa());
2165 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0x10),
2166 static_cast<FRegister>(wd),
2167 static_cast<FRegister>(ws),
2168 static_cast<FRegister>(wt));
2169}
2170
2171void MipsAssembler::Ave_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2172 CHECK(HasMsa());
2173 DsFsmInstrFff(EmitMsa3R(0x4, 0x0, wt, ws, wd, 0x10),
2174 static_cast<FRegister>(wd),
2175 static_cast<FRegister>(ws),
2176 static_cast<FRegister>(wt));
2177}
2178
2179void MipsAssembler::Ave_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2180 CHECK(HasMsa());
2181 DsFsmInstrFff(EmitMsa3R(0x4, 0x1, wt, ws, wd, 0x10),
2182 static_cast<FRegister>(wd),
2183 static_cast<FRegister>(ws),
2184 static_cast<FRegister>(wt));
2185}
2186
2187void MipsAssembler::Ave_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2188 CHECK(HasMsa());
2189 DsFsmInstrFff(EmitMsa3R(0x4, 0x2, wt, ws, wd, 0x10),
2190 static_cast<FRegister>(wd),
2191 static_cast<FRegister>(ws),
2192 static_cast<FRegister>(wt));
2193}
2194
2195void MipsAssembler::Ave_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2196 CHECK(HasMsa());
2197 DsFsmInstrFff(EmitMsa3R(0x4, 0x3, wt, ws, wd, 0x10),
2198 static_cast<FRegister>(wd),
2199 static_cast<FRegister>(ws),
2200 static_cast<FRegister>(wt));
2201}
2202
2203void MipsAssembler::Ave_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2204 CHECK(HasMsa());
2205 DsFsmInstrFff(EmitMsa3R(0x5, 0x0, wt, ws, wd, 0x10),
2206 static_cast<FRegister>(wd),
2207 static_cast<FRegister>(ws),
2208 static_cast<FRegister>(wt));
2209}
2210
2211void MipsAssembler::Ave_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2212 CHECK(HasMsa());
2213 DsFsmInstrFff(EmitMsa3R(0x5, 0x1, wt, ws, wd, 0x10),
2214 static_cast<FRegister>(wd),
2215 static_cast<FRegister>(ws),
2216 static_cast<FRegister>(wt));
2217}
2218
2219void MipsAssembler::Ave_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2220 CHECK(HasMsa());
2221 DsFsmInstrFff(EmitMsa3R(0x5, 0x2, wt, ws, wd, 0x10),
2222 static_cast<FRegister>(wd),
2223 static_cast<FRegister>(ws),
2224 static_cast<FRegister>(wt));
2225}
2226
2227void MipsAssembler::Ave_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2228 CHECK(HasMsa());
2229 DsFsmInstrFff(EmitMsa3R(0x5, 0x3, wt, ws, wd, 0x10),
2230 static_cast<FRegister>(wd),
2231 static_cast<FRegister>(ws),
2232 static_cast<FRegister>(wt));
2233}
2234
2235void MipsAssembler::Aver_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2236 CHECK(HasMsa());
2237 DsFsmInstrFff(EmitMsa3R(0x6, 0x0, wt, ws, wd, 0x10),
2238 static_cast<FRegister>(wd),
2239 static_cast<FRegister>(ws),
2240 static_cast<FRegister>(wt));
2241}
2242
2243void MipsAssembler::Aver_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2244 CHECK(HasMsa());
2245 DsFsmInstrFff(EmitMsa3R(0x6, 0x1, wt, ws, wd, 0x10),
2246 static_cast<FRegister>(wd),
2247 static_cast<FRegister>(ws),
2248 static_cast<FRegister>(wt));
2249}
2250
2251void MipsAssembler::Aver_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2252 CHECK(HasMsa());
2253 DsFsmInstrFff(EmitMsa3R(0x6, 0x2, wt, ws, wd, 0x10),
2254 static_cast<FRegister>(wd),
2255 static_cast<FRegister>(ws),
2256 static_cast<FRegister>(wt));
2257}
2258
2259void MipsAssembler::Aver_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2260 CHECK(HasMsa());
2261 DsFsmInstrFff(EmitMsa3R(0x6, 0x3, wt, ws, wd, 0x10),
2262 static_cast<FRegister>(wd),
2263 static_cast<FRegister>(ws),
2264 static_cast<FRegister>(wt));
2265}
2266
2267void MipsAssembler::Aver_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2268 CHECK(HasMsa());
2269 DsFsmInstrFff(EmitMsa3R(0x7, 0x0, wt, ws, wd, 0x10),
2270 static_cast<FRegister>(wd),
2271 static_cast<FRegister>(ws),
2272 static_cast<FRegister>(wt));
2273}
2274
2275void MipsAssembler::Aver_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2276 CHECK(HasMsa());
2277 DsFsmInstrFff(EmitMsa3R(0x7, 0x1, wt, ws, wd, 0x10),
2278 static_cast<FRegister>(wd),
2279 static_cast<FRegister>(ws),
2280 static_cast<FRegister>(wt));
2281}
2282
2283void MipsAssembler::Aver_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2284 CHECK(HasMsa());
2285 DsFsmInstrFff(EmitMsa3R(0x7, 0x2, wt, ws, wd, 0x10),
2286 static_cast<FRegister>(wd),
2287 static_cast<FRegister>(ws),
2288 static_cast<FRegister>(wt));
2289}
2290
2291void MipsAssembler::Aver_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2292 CHECK(HasMsa());
2293 DsFsmInstrFff(EmitMsa3R(0x7, 0x3, wt, ws, wd, 0x10),
2294 static_cast<FRegister>(wd),
2295 static_cast<FRegister>(ws),
2296 static_cast<FRegister>(wt));
2297}
2298
2299void MipsAssembler::Max_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2300 CHECK(HasMsa());
2301 DsFsmInstrFff(EmitMsa3R(0x2, 0x0, wt, ws, wd, 0xe),
2302 static_cast<FRegister>(wd),
2303 static_cast<FRegister>(ws),
2304 static_cast<FRegister>(wt));
2305}
2306
2307void MipsAssembler::Max_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2308 CHECK(HasMsa());
2309 DsFsmInstrFff(EmitMsa3R(0x2, 0x1, wt, ws, wd, 0xe),
2310 static_cast<FRegister>(wd),
2311 static_cast<FRegister>(ws),
2312 static_cast<FRegister>(wt));
2313}
2314
2315void MipsAssembler::Max_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2316 CHECK(HasMsa());
2317 DsFsmInstrFff(EmitMsa3R(0x2, 0x2, wt, ws, wd, 0xe),
2318 static_cast<FRegister>(wd),
2319 static_cast<FRegister>(ws),
2320 static_cast<FRegister>(wt));
2321}
2322
2323void MipsAssembler::Max_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2324 CHECK(HasMsa());
2325 DsFsmInstrFff(EmitMsa3R(0x2, 0x3, wt, ws, wd, 0xe),
2326 static_cast<FRegister>(wd),
2327 static_cast<FRegister>(ws),
2328 static_cast<FRegister>(wt));
2329}
2330
2331void MipsAssembler::Max_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2332 CHECK(HasMsa());
2333 DsFsmInstrFff(EmitMsa3R(0x3, 0x0, wt, ws, wd, 0xe),
2334 static_cast<FRegister>(wd),
2335 static_cast<FRegister>(ws),
2336 static_cast<FRegister>(wt));
2337}
2338
2339void MipsAssembler::Max_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2340 CHECK(HasMsa());
2341 DsFsmInstrFff(EmitMsa3R(0x3, 0x1, wt, ws, wd, 0xe),
2342 static_cast<FRegister>(wd),
2343 static_cast<FRegister>(ws),
2344 static_cast<FRegister>(wt));
2345}
2346
2347void MipsAssembler::Max_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2348 CHECK(HasMsa());
2349 DsFsmInstrFff(EmitMsa3R(0x3, 0x2, wt, ws, wd, 0xe),
2350 static_cast<FRegister>(wd),
2351 static_cast<FRegister>(ws),
2352 static_cast<FRegister>(wt));
2353}
2354
2355void MipsAssembler::Max_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2356 CHECK(HasMsa());
2357 DsFsmInstrFff(EmitMsa3R(0x3, 0x3, wt, ws, wd, 0xe),
2358 static_cast<FRegister>(wd),
2359 static_cast<FRegister>(ws),
2360 static_cast<FRegister>(wt));
2361}
2362
2363void MipsAssembler::Min_sB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2364 CHECK(HasMsa());
2365 DsFsmInstrFff(EmitMsa3R(0x4, 0x0, wt, ws, wd, 0xe),
2366 static_cast<FRegister>(wd),
2367 static_cast<FRegister>(ws),
2368 static_cast<FRegister>(wt));
2369}
2370
2371void MipsAssembler::Min_sH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2372 CHECK(HasMsa());
2373 DsFsmInstrFff(EmitMsa3R(0x4, 0x1, wt, ws, wd, 0xe),
2374 static_cast<FRegister>(wd),
2375 static_cast<FRegister>(ws),
2376 static_cast<FRegister>(wt));
2377}
2378
2379void MipsAssembler::Min_sW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2380 CHECK(HasMsa());
2381 DsFsmInstrFff(EmitMsa3R(0x4, 0x2, wt, ws, wd, 0xe),
2382 static_cast<FRegister>(wd),
2383 static_cast<FRegister>(ws),
2384 static_cast<FRegister>(wt));
2385}
2386
2387void MipsAssembler::Min_sD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2388 CHECK(HasMsa());
2389 DsFsmInstrFff(EmitMsa3R(0x4, 0x3, wt, ws, wd, 0xe),
2390 static_cast<FRegister>(wd),
2391 static_cast<FRegister>(ws),
2392 static_cast<FRegister>(wt));
2393}
2394
2395void MipsAssembler::Min_uB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2396 CHECK(HasMsa());
2397 DsFsmInstrFff(EmitMsa3R(0x5, 0x0, wt, ws, wd, 0xe),
2398 static_cast<FRegister>(wd),
2399 static_cast<FRegister>(ws),
2400 static_cast<FRegister>(wt));
2401}
2402
2403void MipsAssembler::Min_uH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2404 CHECK(HasMsa());
2405 DsFsmInstrFff(EmitMsa3R(0x5, 0x1, wt, ws, wd, 0xe),
2406 static_cast<FRegister>(wd),
2407 static_cast<FRegister>(ws),
2408 static_cast<FRegister>(wt));
2409}
2410
2411void MipsAssembler::Min_uW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2412 CHECK(HasMsa());
2413 DsFsmInstrFff(EmitMsa3R(0x5, 0x2, wt, ws, wd, 0xe),
2414 static_cast<FRegister>(wd),
2415 static_cast<FRegister>(ws),
2416 static_cast<FRegister>(wt));
2417}
2418
2419void MipsAssembler::Min_uD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2420 CHECK(HasMsa());
2421 DsFsmInstrFff(EmitMsa3R(0x5, 0x3, wt, ws, wd, 0xe),
2422 static_cast<FRegister>(wd),
2423 static_cast<FRegister>(ws),
2424 static_cast<FRegister>(wt));
2425}
2426
2427void MipsAssembler::FaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2428 CHECK(HasMsa());
2429 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0x1b),
2430 static_cast<FRegister>(wd),
2431 static_cast<FRegister>(ws),
2432 static_cast<FRegister>(wt));
2433}
2434
2435void MipsAssembler::FaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2436 CHECK(HasMsa());
2437 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0x1b),
2438 static_cast<FRegister>(wd),
2439 static_cast<FRegister>(ws),
2440 static_cast<FRegister>(wt));
2441}
2442
2443void MipsAssembler::FsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2444 CHECK(HasMsa());
2445 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0x1b),
2446 static_cast<FRegister>(wd),
2447 static_cast<FRegister>(ws),
2448 static_cast<FRegister>(wt));
2449}
2450
2451void MipsAssembler::FsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2452 CHECK(HasMsa());
2453 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0x1b),
2454 static_cast<FRegister>(wd),
2455 static_cast<FRegister>(ws),
2456 static_cast<FRegister>(wt));
2457}
2458
2459void MipsAssembler::FmulW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2460 CHECK(HasMsa());
2461 DsFsmInstrFff(EmitMsa3R(0x1, 0x0, wt, ws, wd, 0x1b),
2462 static_cast<FRegister>(wd),
2463 static_cast<FRegister>(ws),
2464 static_cast<FRegister>(wt));
2465}
2466
2467void MipsAssembler::FmulD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2468 CHECK(HasMsa());
2469 DsFsmInstrFff(EmitMsa3R(0x1, 0x1, wt, ws, wd, 0x1b),
2470 static_cast<FRegister>(wd),
2471 static_cast<FRegister>(ws),
2472 static_cast<FRegister>(wt));
2473}
2474
2475void MipsAssembler::FdivW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2476 CHECK(HasMsa());
2477 DsFsmInstrFff(EmitMsa3R(0x1, 0x2, wt, ws, wd, 0x1b),
2478 static_cast<FRegister>(wd),
2479 static_cast<FRegister>(ws),
2480 static_cast<FRegister>(wt));
2481}
2482
2483void MipsAssembler::FdivD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2484 CHECK(HasMsa());
2485 DsFsmInstrFff(EmitMsa3R(0x1, 0x3, wt, ws, wd, 0x1b),
2486 static_cast<FRegister>(wd),
2487 static_cast<FRegister>(ws),
2488 static_cast<FRegister>(wt));
2489}
2490
2491void MipsAssembler::FmaxW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2492 CHECK(HasMsa());
2493 DsFsmInstrFff(EmitMsa3R(0x7, 0x0, wt, ws, wd, 0x1b),
2494 static_cast<FRegister>(wd),
2495 static_cast<FRegister>(ws),
2496 static_cast<FRegister>(wt));
2497}
2498
2499void MipsAssembler::FmaxD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2500 CHECK(HasMsa());
2501 DsFsmInstrFff(EmitMsa3R(0x7, 0x1, wt, ws, wd, 0x1b),
2502 static_cast<FRegister>(wd),
2503 static_cast<FRegister>(ws),
2504 static_cast<FRegister>(wt));
2505}
2506
2507void MipsAssembler::FminW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2508 CHECK(HasMsa());
2509 DsFsmInstrFff(EmitMsa3R(0x6, 0x0, wt, ws, wd, 0x1b),
2510 static_cast<FRegister>(wd),
2511 static_cast<FRegister>(ws),
2512 static_cast<FRegister>(wt));
2513}
2514
2515void MipsAssembler::FminD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2516 CHECK(HasMsa());
2517 DsFsmInstrFff(EmitMsa3R(0x6, 0x1, wt, ws, wd, 0x1b),
2518 static_cast<FRegister>(wd),
2519 static_cast<FRegister>(ws),
2520 static_cast<FRegister>(wt));
2521}
2522
2523void MipsAssembler::Ffint_sW(VectorRegister wd, VectorRegister ws) {
2524 CHECK(HasMsa());
2525 DsFsmInstrFff(EmitMsa2RF(0x19e, 0x0, ws, wd, 0x1e),
2526 static_cast<FRegister>(wd),
2527 static_cast<FRegister>(ws),
2528 static_cast<FRegister>(ws));
2529}
2530
2531void MipsAssembler::Ffint_sD(VectorRegister wd, VectorRegister ws) {
2532 CHECK(HasMsa());
2533 DsFsmInstrFff(EmitMsa2RF(0x19e, 0x1, ws, wd, 0x1e),
2534 static_cast<FRegister>(wd),
2535 static_cast<FRegister>(ws),
2536 static_cast<FRegister>(ws));
2537}
2538
2539void MipsAssembler::Ftint_sW(VectorRegister wd, VectorRegister ws) {
2540 CHECK(HasMsa());
2541 DsFsmInstrFff(EmitMsa2RF(0x19c, 0x0, ws, wd, 0x1e),
2542 static_cast<FRegister>(wd),
2543 static_cast<FRegister>(ws),
2544 static_cast<FRegister>(ws));
2545}
2546
2547void MipsAssembler::Ftint_sD(VectorRegister wd, VectorRegister ws) {
2548 CHECK(HasMsa());
2549 DsFsmInstrFff(EmitMsa2RF(0x19c, 0x1, ws, wd, 0x1e),
2550 static_cast<FRegister>(wd),
2551 static_cast<FRegister>(ws),
2552 static_cast<FRegister>(ws));
2553}
2554
2555void MipsAssembler::SllB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2556 CHECK(HasMsa());
2557 DsFsmInstrFff(EmitMsa3R(0x0, 0x0, wt, ws, wd, 0xd),
2558 static_cast<FRegister>(wd),
2559 static_cast<FRegister>(ws),
2560 static_cast<FRegister>(wt));
2561}
2562
2563void MipsAssembler::SllH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2564 CHECK(HasMsa());
2565 DsFsmInstrFff(EmitMsa3R(0x0, 0x1, wt, ws, wd, 0xd),
2566 static_cast<FRegister>(wd),
2567 static_cast<FRegister>(ws),
2568 static_cast<FRegister>(wt));
2569}
2570
2571void MipsAssembler::SllW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2572 CHECK(HasMsa());
2573 DsFsmInstrFff(EmitMsa3R(0x0, 0x2, wt, ws, wd, 0xd),
2574 static_cast<FRegister>(wd),
2575 static_cast<FRegister>(ws),
2576 static_cast<FRegister>(wt));
2577}
2578
2579void MipsAssembler::SllD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2580 CHECK(HasMsa());
2581 DsFsmInstrFff(EmitMsa3R(0x0, 0x3, wt, ws, wd, 0xd),
2582 static_cast<FRegister>(wd),
2583 static_cast<FRegister>(ws),
2584 static_cast<FRegister>(wt));
2585}
2586
2587void MipsAssembler::SraB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2588 CHECK(HasMsa());
2589 DsFsmInstrFff(EmitMsa3R(0x1, 0x0, wt, ws, wd, 0xd),
2590 static_cast<FRegister>(wd),
2591 static_cast<FRegister>(ws),
2592 static_cast<FRegister>(wt));
2593}
2594
2595void MipsAssembler::SraH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2596 CHECK(HasMsa());
2597 DsFsmInstrFff(EmitMsa3R(0x1, 0x1, wt, ws, wd, 0xd),
2598 static_cast<FRegister>(wd),
2599 static_cast<FRegister>(ws),
2600 static_cast<FRegister>(wt));
2601}
2602
2603void MipsAssembler::SraW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2604 CHECK(HasMsa());
2605 DsFsmInstrFff(EmitMsa3R(0x1, 0x2, wt, ws, wd, 0xd),
2606 static_cast<FRegister>(wd),
2607 static_cast<FRegister>(ws),
2608 static_cast<FRegister>(wt));
2609}
2610
2611void MipsAssembler::SraD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2612 CHECK(HasMsa());
2613 DsFsmInstrFff(EmitMsa3R(0x1, 0x3, wt, ws, wd, 0xd),
2614 static_cast<FRegister>(wd),
2615 static_cast<FRegister>(ws),
2616 static_cast<FRegister>(wt));
2617}
2618
2619void MipsAssembler::SrlB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2620 CHECK(HasMsa());
2621 DsFsmInstrFff(EmitMsa3R(0x2, 0x0, wt, ws, wd, 0xd),
2622 static_cast<FRegister>(wd),
2623 static_cast<FRegister>(ws),
2624 static_cast<FRegister>(wt));
2625}
2626
2627void MipsAssembler::SrlH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2628 CHECK(HasMsa());
2629 DsFsmInstrFff(EmitMsa3R(0x2, 0x1, wt, ws, wd, 0xd),
2630 static_cast<FRegister>(wd),
2631 static_cast<FRegister>(ws),
2632 static_cast<FRegister>(wt));
2633}
2634
2635void MipsAssembler::SrlW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2636 CHECK(HasMsa());
2637 DsFsmInstrFff(EmitMsa3R(0x2, 0x2, wt, ws, wd, 0xd),
2638 static_cast<FRegister>(wd),
2639 static_cast<FRegister>(ws),
2640 static_cast<FRegister>(wt));
2641}
2642
2643void MipsAssembler::SrlD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2644 CHECK(HasMsa());
2645 DsFsmInstrFff(EmitMsa3R(0x2, 0x3, wt, ws, wd, 0xd),
2646 static_cast<FRegister>(wd),
2647 static_cast<FRegister>(ws),
2648 static_cast<FRegister>(wt));
2649}
2650
2651void MipsAssembler::SlliB(VectorRegister wd, VectorRegister ws, int shamt3) {
2652 CHECK(HasMsa());
2653 CHECK(IsUint<3>(shamt3)) << shamt3;
2654 DsFsmInstrFff(EmitMsaBIT(0x0, shamt3 | kMsaDfMByteMask, ws, wd, 0x9),
2655 static_cast<FRegister>(wd),
2656 static_cast<FRegister>(ws),
2657 static_cast<FRegister>(ws));
2658}
2659
2660void MipsAssembler::SlliH(VectorRegister wd, VectorRegister ws, int shamt4) {
2661 CHECK(HasMsa());
2662 CHECK(IsUint<4>(shamt4)) << shamt4;
2663 DsFsmInstrFff(EmitMsaBIT(0x0, shamt4 | kMsaDfMHalfwordMask, ws, wd, 0x9),
2664 static_cast<FRegister>(wd),
2665 static_cast<FRegister>(ws),
2666 static_cast<FRegister>(ws));
2667}
2668
2669void MipsAssembler::SlliW(VectorRegister wd, VectorRegister ws, int shamt5) {
2670 CHECK(HasMsa());
2671 CHECK(IsUint<5>(shamt5)) << shamt5;
2672 DsFsmInstrFff(EmitMsaBIT(0x0, shamt5 | kMsaDfMWordMask, ws, wd, 0x9),
2673 static_cast<FRegister>(wd),
2674 static_cast<FRegister>(ws),
2675 static_cast<FRegister>(ws));
2676}
2677
2678void MipsAssembler::SlliD(VectorRegister wd, VectorRegister ws, int shamt6) {
2679 CHECK(HasMsa());
2680 CHECK(IsUint<6>(shamt6)) << shamt6;
2681 DsFsmInstrFff(EmitMsaBIT(0x0, shamt6 | kMsaDfMDoublewordMask, ws, wd, 0x9),
2682 static_cast<FRegister>(wd),
2683 static_cast<FRegister>(ws),
2684 static_cast<FRegister>(ws));
2685}
2686
2687void MipsAssembler::SraiB(VectorRegister wd, VectorRegister ws, int shamt3) {
2688 CHECK(HasMsa());
2689 CHECK(IsUint<3>(shamt3)) << shamt3;
2690 DsFsmInstrFff(EmitMsaBIT(0x1, shamt3 | kMsaDfMByteMask, ws, wd, 0x9),
2691 static_cast<FRegister>(wd),
2692 static_cast<FRegister>(ws),
2693 static_cast<FRegister>(ws));
2694}
2695
2696void MipsAssembler::SraiH(VectorRegister wd, VectorRegister ws, int shamt4) {
2697 CHECK(HasMsa());
2698 CHECK(IsUint<4>(shamt4)) << shamt4;
2699 DsFsmInstrFff(EmitMsaBIT(0x1, shamt4 | kMsaDfMHalfwordMask, ws, wd, 0x9),
2700 static_cast<FRegister>(wd),
2701 static_cast<FRegister>(ws),
2702 static_cast<FRegister>(ws));
2703}
2704
2705void MipsAssembler::SraiW(VectorRegister wd, VectorRegister ws, int shamt5) {
2706 CHECK(HasMsa());
2707 CHECK(IsUint<5>(shamt5)) << shamt5;
2708 DsFsmInstrFff(EmitMsaBIT(0x1, shamt5 | kMsaDfMWordMask, ws, wd, 0x9),
2709 static_cast<FRegister>(wd),
2710 static_cast<FRegister>(ws),
2711 static_cast<FRegister>(ws));
2712}
2713
2714void MipsAssembler::SraiD(VectorRegister wd, VectorRegister ws, int shamt6) {
2715 CHECK(HasMsa());
2716 CHECK(IsUint<6>(shamt6)) << shamt6;
2717 DsFsmInstrFff(EmitMsaBIT(0x1, shamt6 | kMsaDfMDoublewordMask, ws, wd, 0x9),
2718 static_cast<FRegister>(wd),
2719 static_cast<FRegister>(ws),
2720 static_cast<FRegister>(ws));
2721}
2722
2723void MipsAssembler::SrliB(VectorRegister wd, VectorRegister ws, int shamt3) {
2724 CHECK(HasMsa());
2725 CHECK(IsUint<3>(shamt3)) << shamt3;
2726 DsFsmInstrFff(EmitMsaBIT(0x2, shamt3 | kMsaDfMByteMask, ws, wd, 0x9),
2727 static_cast<FRegister>(wd),
2728 static_cast<FRegister>(ws),
2729 static_cast<FRegister>(ws));
2730}
2731
2732void MipsAssembler::SrliH(VectorRegister wd, VectorRegister ws, int shamt4) {
2733 CHECK(HasMsa());
2734 CHECK(IsUint<4>(shamt4)) << shamt4;
2735 DsFsmInstrFff(EmitMsaBIT(0x2, shamt4 | kMsaDfMHalfwordMask, ws, wd, 0x9),
2736 static_cast<FRegister>(wd),
2737 static_cast<FRegister>(ws),
2738 static_cast<FRegister>(ws));
2739}
2740
2741void MipsAssembler::SrliW(VectorRegister wd, VectorRegister ws, int shamt5) {
2742 CHECK(HasMsa());
2743 CHECK(IsUint<5>(shamt5)) << shamt5;
2744 DsFsmInstrFff(EmitMsaBIT(0x2, shamt5 | kMsaDfMWordMask, ws, wd, 0x9),
2745 static_cast<FRegister>(wd),
2746 static_cast<FRegister>(ws),
2747 static_cast<FRegister>(ws));
2748}
2749
2750void MipsAssembler::SrliD(VectorRegister wd, VectorRegister ws, int shamt6) {
2751 CHECK(HasMsa());
2752 CHECK(IsUint<6>(shamt6)) << shamt6;
2753 DsFsmInstrFff(EmitMsaBIT(0x2, shamt6 | kMsaDfMDoublewordMask, ws, wd, 0x9),
2754 static_cast<FRegister>(wd),
2755 static_cast<FRegister>(ws),
2756 static_cast<FRegister>(ws));
2757}
2758
2759void MipsAssembler::MoveV(VectorRegister wd, VectorRegister ws) {
2760 CHECK(HasMsa());
2761 DsFsmInstrFff(EmitMsaBIT(0x1, 0x3e, ws, wd, 0x19),
2762 static_cast<FRegister>(wd),
2763 static_cast<FRegister>(ws),
2764 static_cast<FRegister>(ws));
2765}
2766
2767void MipsAssembler::SplatiB(VectorRegister wd, VectorRegister ws, int n4) {
2768 CHECK(HasMsa());
2769 CHECK(IsUint<4>(n4)) << n4;
2770 DsFsmInstrFff(EmitMsaELM(0x1, n4 | kMsaDfNByteMask, ws, wd, 0x19),
2771 static_cast<FRegister>(wd),
2772 static_cast<FRegister>(ws),
2773 static_cast<FRegister>(ws));
2774}
2775
2776void MipsAssembler::SplatiH(VectorRegister wd, VectorRegister ws, int n3) {
2777 CHECK(HasMsa());
2778 CHECK(IsUint<3>(n3)) << n3;
2779 DsFsmInstrFff(EmitMsaELM(0x1, n3 | kMsaDfNHalfwordMask, ws, wd, 0x19),
2780 static_cast<FRegister>(wd),
2781 static_cast<FRegister>(ws),
2782 static_cast<FRegister>(ws));
2783}
2784
2785void MipsAssembler::SplatiW(VectorRegister wd, VectorRegister ws, int n2) {
2786 CHECK(HasMsa());
2787 CHECK(IsUint<2>(n2)) << n2;
2788 DsFsmInstrFff(EmitMsaELM(0x1, n2 | kMsaDfNWordMask, ws, wd, 0x19),
2789 static_cast<FRegister>(wd),
2790 static_cast<FRegister>(ws),
2791 static_cast<FRegister>(ws));
2792}
2793
2794void MipsAssembler::SplatiD(VectorRegister wd, VectorRegister ws, int n1) {
2795 CHECK(HasMsa());
2796 CHECK(IsUint<1>(n1)) << n1;
2797 DsFsmInstrFff(EmitMsaELM(0x1, n1 | kMsaDfNDoublewordMask, ws, wd, 0x19),
2798 static_cast<FRegister>(wd),
2799 static_cast<FRegister>(ws),
2800 static_cast<FRegister>(ws));
2801}
2802
2803void MipsAssembler::FillB(VectorRegister wd, Register rs) {
2804 CHECK(HasMsa());
2805 DsFsmInstrFr(EmitMsa2R(0xc0, 0x0, static_cast<VectorRegister>(rs), wd, 0x1e),
2806 static_cast<FRegister>(wd),
2807 rs);
2808}
2809
2810void MipsAssembler::FillH(VectorRegister wd, Register rs) {
2811 CHECK(HasMsa());
2812 DsFsmInstrFr(EmitMsa2R(0xc0, 0x1, static_cast<VectorRegister>(rs), wd, 0x1e),
2813 static_cast<FRegister>(wd),
2814 rs);
2815}
2816
2817void MipsAssembler::FillW(VectorRegister wd, Register rs) {
2818 CHECK(HasMsa());
2819 DsFsmInstrFr(EmitMsa2R(0xc0, 0x2, static_cast<VectorRegister>(rs), wd, 0x1e),
2820 static_cast<FRegister>(wd),
2821 rs);
2822}
2823
2824void MipsAssembler::LdiB(VectorRegister wd, int imm8) {
2825 CHECK(HasMsa());
2826 CHECK(IsInt<8>(imm8)) << imm8;
2827 DsFsmInstrFr(EmitMsaI10(0x6, 0x0, imm8 & kMsaS10Mask, wd, 0x7),
2828 static_cast<FRegister>(wd),
2829 ZERO);
2830}
2831
2832void MipsAssembler::LdiH(VectorRegister wd, int imm10) {
2833 CHECK(HasMsa());
2834 CHECK(IsInt<10>(imm10)) << imm10;
2835 DsFsmInstrFr(EmitMsaI10(0x6, 0x1, imm10 & kMsaS10Mask, wd, 0x7),
2836 static_cast<FRegister>(wd),
2837 ZERO);
2838}
2839
2840void MipsAssembler::LdiW(VectorRegister wd, int imm10) {
2841 CHECK(HasMsa());
2842 CHECK(IsInt<10>(imm10)) << imm10;
2843 DsFsmInstrFr(EmitMsaI10(0x6, 0x2, imm10 & kMsaS10Mask, wd, 0x7),
2844 static_cast<FRegister>(wd),
2845 ZERO);
2846}
2847
2848void MipsAssembler::LdiD(VectorRegister wd, int imm10) {
2849 CHECK(HasMsa());
2850 CHECK(IsInt<10>(imm10)) << imm10;
2851 DsFsmInstrFr(EmitMsaI10(0x6, 0x3, imm10 & kMsaS10Mask, wd, 0x7),
2852 static_cast<FRegister>(wd),
2853 ZERO);
2854}
2855
2856void MipsAssembler::LdB(VectorRegister wd, Register rs, int offset) {
2857 CHECK(HasMsa());
2858 CHECK(IsInt<10>(offset)) << offset;
2859 DsFsmInstrFr(EmitMsaMI10(offset & kMsaS10Mask, rs, wd, 0x8, 0x0),
2860 static_cast<FRegister>(wd),
2861 rs);
2862}
2863
2864void MipsAssembler::LdH(VectorRegister wd, Register rs, int offset) {
2865 CHECK(HasMsa());
2866 CHECK(IsInt<11>(offset)) << offset;
2867 CHECK_ALIGNED(offset, kMipsHalfwordSize);
2868 DsFsmInstrFr(EmitMsaMI10((offset >> TIMES_2) & kMsaS10Mask, rs, wd, 0x8, 0x1),
2869 static_cast<FRegister>(wd),
2870 rs);
2871}
2872
2873void MipsAssembler::LdW(VectorRegister wd, Register rs, int offset) {
2874 CHECK(HasMsa());
2875 CHECK(IsInt<12>(offset)) << offset;
2876 CHECK_ALIGNED(offset, kMipsWordSize);
2877 DsFsmInstrFr(EmitMsaMI10((offset >> TIMES_4) & kMsaS10Mask, rs, wd, 0x8, 0x2),
2878 static_cast<FRegister>(wd),
2879 rs);
2880}
2881
2882void MipsAssembler::LdD(VectorRegister wd, Register rs, int offset) {
2883 CHECK(HasMsa());
2884 CHECK(IsInt<13>(offset)) << offset;
2885 CHECK_ALIGNED(offset, kMipsDoublewordSize);
2886 DsFsmInstrFr(EmitMsaMI10((offset >> TIMES_8) & kMsaS10Mask, rs, wd, 0x8, 0x3),
2887 static_cast<FRegister>(wd),
2888 rs);
2889}
2890
2891void MipsAssembler::StB(VectorRegister wd, Register rs, int offset) {
2892 CHECK(HasMsa());
2893 CHECK(IsInt<10>(offset)) << offset;
2894 DsFsmInstrFR(EmitMsaMI10(offset & kMsaS10Mask, rs, wd, 0x9, 0x0), static_cast<FRegister>(wd), rs);
2895}
2896
2897void MipsAssembler::StH(VectorRegister wd, Register rs, int offset) {
2898 CHECK(HasMsa());
2899 CHECK(IsInt<11>(offset)) << offset;
2900 CHECK_ALIGNED(offset, kMipsHalfwordSize);
2901 DsFsmInstrFR(EmitMsaMI10((offset >> TIMES_2) & kMsaS10Mask, rs, wd, 0x9, 0x1),
2902 static_cast<FRegister>(wd),
2903 rs);
2904}
2905
2906void MipsAssembler::StW(VectorRegister wd, Register rs, int offset) {
2907 CHECK(HasMsa());
2908 CHECK(IsInt<12>(offset)) << offset;
2909 CHECK_ALIGNED(offset, kMipsWordSize);
2910 DsFsmInstrFR(EmitMsaMI10((offset >> TIMES_4) & kMsaS10Mask, rs, wd, 0x9, 0x2),
2911 static_cast<FRegister>(wd),
2912 rs);
2913}
2914
2915void MipsAssembler::StD(VectorRegister wd, Register rs, int offset) {
2916 CHECK(HasMsa());
2917 CHECK(IsInt<13>(offset)) << offset;
2918 CHECK_ALIGNED(offset, kMipsDoublewordSize);
2919 DsFsmInstrFR(EmitMsaMI10((offset >> TIMES_8) & kMsaS10Mask, rs, wd, 0x9, 0x3),
2920 static_cast<FRegister>(wd),
2921 rs);
2922}
2923
2924void MipsAssembler::IlvrB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2925 CHECK(HasMsa());
2926 DsFsmInstrFff(EmitMsa3R(0x5, 0x0, wt, ws, wd, 0x14),
2927 static_cast<FRegister>(wd),
2928 static_cast<FRegister>(ws),
2929 static_cast<FRegister>(wt));
2930}
2931
2932void MipsAssembler::IlvrH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2933 CHECK(HasMsa());
2934 DsFsmInstrFff(EmitMsa3R(0x5, 0x1, wt, ws, wd, 0x14),
2935 static_cast<FRegister>(wd),
2936 static_cast<FRegister>(ws),
2937 static_cast<FRegister>(wt));
2938}
2939
2940void MipsAssembler::IlvrW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2941 CHECK(HasMsa());
2942 DsFsmInstrFff(EmitMsa3R(0x5, 0x2, wt, ws, wd, 0x14),
2943 static_cast<FRegister>(wd),
2944 static_cast<FRegister>(ws),
2945 static_cast<FRegister>(wt));
2946}
2947
2948void MipsAssembler::IlvrD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2949 CHECK(HasMsa());
2950 DsFsmInstrFff(EmitMsa3R(0x5, 0x3, wt, ws, wd, 0x14),
2951 static_cast<FRegister>(wd),
2952 static_cast<FRegister>(ws),
2953 static_cast<FRegister>(wt));
2954}
2955
Lena Djokicb3d79e42017-07-25 11:20:52 +02002956void MipsAssembler::MaddvB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2957 CHECK(HasMsa());
2958 DsFsmInstrFff(EmitMsa3R(0x1, 0x0, wt, ws, wd, 0x12),
2959 static_cast<FRegister>(wd),
2960 static_cast<FRegister>(ws),
2961 static_cast<FRegister>(wt));
2962}
2963
2964void MipsAssembler::MaddvH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2965 CHECK(HasMsa());
2966 DsFsmInstrFff(EmitMsa3R(0x1, 0x1, wt, ws, wd, 0x12),
2967 static_cast<FRegister>(wd),
2968 static_cast<FRegister>(ws),
2969 static_cast<FRegister>(wt));
2970}
2971
2972void MipsAssembler::MaddvW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2973 CHECK(HasMsa());
2974 DsFsmInstrFff(EmitMsa3R(0x1, 0x2, wt, ws, wd, 0x12),
2975 static_cast<FRegister>(wd),
2976 static_cast<FRegister>(ws),
2977 static_cast<FRegister>(wt));
2978}
2979
2980void MipsAssembler::MaddvD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2981 CHECK(HasMsa());
2982 DsFsmInstrFff(EmitMsa3R(0x1, 0x3, wt, ws, wd, 0x12),
2983 static_cast<FRegister>(wd),
2984 static_cast<FRegister>(ws),
2985 static_cast<FRegister>(wt));
2986}
2987
2988void MipsAssembler::MsubvB(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2989 CHECK(HasMsa());
2990 DsFsmInstrFff(EmitMsa3R(0x2, 0x0, wt, ws, wd, 0x12),
2991 static_cast<FRegister>(wd),
2992 static_cast<FRegister>(ws),
2993 static_cast<FRegister>(wt));
2994}
2995
2996void MipsAssembler::MsubvH(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
2997 CHECK(HasMsa());
2998 DsFsmInstrFff(EmitMsa3R(0x2, 0x1, wt, ws, wd, 0x12),
2999 static_cast<FRegister>(wd),
3000 static_cast<FRegister>(ws),
3001 static_cast<FRegister>(wt));
3002}
3003
3004void MipsAssembler::MsubvW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3005 CHECK(HasMsa());
3006 DsFsmInstrFff(EmitMsa3R(0x2, 0x2, wt, ws, wd, 0x12),
3007 static_cast<FRegister>(wd),
3008 static_cast<FRegister>(ws),
3009 static_cast<FRegister>(wt));
3010}
3011
3012void MipsAssembler::MsubvD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3013 CHECK(HasMsa());
3014 DsFsmInstrFff(EmitMsa3R(0x2, 0x3, wt, ws, wd, 0x12),
3015 static_cast<FRegister>(wd),
3016 static_cast<FRegister>(ws),
3017 static_cast<FRegister>(wt));
3018}
3019
3020void MipsAssembler::FmaddW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3021 CHECK(HasMsa());
3022 DsFsmInstrFff(EmitMsa3R(0x2, 0x0, wt, ws, wd, 0x1b),
3023 static_cast<FRegister>(wd),
3024 static_cast<FRegister>(ws),
3025 static_cast<FRegister>(wt));
3026}
3027
3028void MipsAssembler::FmaddD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3029 CHECK(HasMsa());
3030 DsFsmInstrFff(EmitMsa3R(0x2, 0x1, wt, ws, wd, 0x1b),
3031 static_cast<FRegister>(wd),
3032 static_cast<FRegister>(ws),
3033 static_cast<FRegister>(wt));
3034}
3035
3036void MipsAssembler::FmsubW(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3037 CHECK(HasMsa());
3038 DsFsmInstrFff(EmitMsa3R(0x2, 0x2, wt, ws, wd, 0x1b),
3039 static_cast<FRegister>(wd),
3040 static_cast<FRegister>(ws),
3041 static_cast<FRegister>(wt));
3042}
3043
3044void MipsAssembler::FmsubD(VectorRegister wd, VectorRegister ws, VectorRegister wt) {
3045 CHECK(HasMsa());
3046 DsFsmInstrFff(EmitMsa3R(0x2, 0x3, wt, ws, wd, 0x1b),
3047 static_cast<FRegister>(wd),
3048 static_cast<FRegister>(ws),
3049 static_cast<FRegister>(wt));
3050}
3051
Lena Djokic51765b02017-06-22 13:49:59 +02003052void MipsAssembler::ReplicateFPToVectorRegister(VectorRegister dst,
3053 FRegister src,
3054 bool is_double) {
3055 // Float or double in FPU register Fx can be considered as 0th element in vector register Wx.
3056 if (is_double) {
3057 SplatiD(dst, static_cast<VectorRegister>(src), 0);
3058 } else {
3059 SplatiW(dst, static_cast<VectorRegister>(src), 0);
3060 }
3061}
3062
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003063void MipsAssembler::LoadConst32(Register rd, int32_t value) {
3064 if (IsUint<16>(value)) {
3065 // Use OR with (unsigned) immediate to encode 16b unsigned int.
3066 Ori(rd, ZERO, value);
3067 } else if (IsInt<16>(value)) {
3068 // Use ADD with (signed) immediate to encode 16b signed int.
3069 Addiu(rd, ZERO, value);
jeffhao7fbee072012-08-24 17:56:54 -07003070 } else {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003071 Lui(rd, High16Bits(value));
3072 if (value & 0xFFFF)
3073 Ori(rd, rd, Low16Bits(value));
3074 }
3075}
3076
3077void MipsAssembler::LoadConst64(Register reg_hi, Register reg_lo, int64_t value) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08003078 uint32_t low = Low32Bits(value);
3079 uint32_t high = High32Bits(value);
3080 LoadConst32(reg_lo, low);
3081 if (high != low) {
3082 LoadConst32(reg_hi, high);
3083 } else {
3084 Move(reg_hi, reg_lo);
3085 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003086}
3087
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003088void MipsAssembler::LoadSConst32(FRegister r, int32_t value, Register temp) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08003089 if (value == 0) {
3090 temp = ZERO;
3091 } else {
3092 LoadConst32(temp, value);
3093 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003094 Mtc1(temp, r);
3095}
3096
3097void MipsAssembler::LoadDConst64(FRegister rd, int64_t value, Register temp) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08003098 uint32_t low = Low32Bits(value);
3099 uint32_t high = High32Bits(value);
3100 if (low == 0) {
3101 Mtc1(ZERO, rd);
3102 } else {
3103 LoadConst32(temp, low);
3104 Mtc1(temp, rd);
3105 }
3106 if (high == 0) {
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003107 MoveToFpuHigh(ZERO, rd);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08003108 } else {
3109 LoadConst32(temp, high);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003110 MoveToFpuHigh(temp, rd);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08003111 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003112}
3113
3114void MipsAssembler::Addiu32(Register rt, Register rs, int32_t value, Register temp) {
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07003115 CHECK_NE(rs, temp); // Must not overwrite the register `rs` while loading `value`.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003116 if (IsInt<16>(value)) {
3117 Addiu(rt, rs, value);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07003118 } else if (IsR6()) {
3119 int16_t high = High16Bits(value);
3120 int16_t low = Low16Bits(value);
3121 high += (low < 0) ? 1 : 0; // Account for sign extension in addiu.
3122 if (low != 0) {
3123 Aui(temp, rs, high);
3124 Addiu(rt, temp, low);
3125 } else {
3126 Aui(rt, rs, high);
3127 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003128 } else {
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07003129 // Do not load the whole 32-bit `value` if it can be represented as
3130 // a sum of two 16-bit signed values. This can save an instruction.
3131 constexpr int32_t kMinValueForSimpleAdjustment = std::numeric_limits<int16_t>::min() * 2;
3132 constexpr int32_t kMaxValueForSimpleAdjustment = std::numeric_limits<int16_t>::max() * 2;
3133 if (0 <= value && value <= kMaxValueForSimpleAdjustment) {
3134 Addiu(temp, rs, kMaxValueForSimpleAdjustment / 2);
3135 Addiu(rt, temp, value - kMaxValueForSimpleAdjustment / 2);
3136 } else if (kMinValueForSimpleAdjustment <= value && value < 0) {
3137 Addiu(temp, rs, kMinValueForSimpleAdjustment / 2);
3138 Addiu(rt, temp, value - kMinValueForSimpleAdjustment / 2);
3139 } else {
3140 // Now that all shorter options have been exhausted, load the full 32-bit value.
3141 LoadConst32(temp, value);
3142 Addu(rt, rs, temp);
3143 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003144 }
3145}
3146
3147void MipsAssembler::Branch::InitShortOrLong(MipsAssembler::Branch::OffsetBits offset_size,
3148 MipsAssembler::Branch::Type short_type,
3149 MipsAssembler::Branch::Type long_type) {
3150 type_ = (offset_size <= branch_info_[short_type].offset_size) ? short_type : long_type;
3151}
3152
Alexey Frunze96b66822016-09-10 02:32:44 -07003153void MipsAssembler::Branch::InitializeType(Type initial_type, bool is_r6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003154 OffsetBits offset_size_needed = GetOffsetSizeNeeded(location_, target_);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003155 if (is_r6) {
3156 // R6
Alexey Frunze96b66822016-09-10 02:32:44 -07003157 switch (initial_type) {
3158 case kLabel:
3159 CHECK(!IsResolved());
3160 type_ = kR6Label;
3161 break;
3162 case kLiteral:
3163 CHECK(!IsResolved());
3164 type_ = kR6Literal;
3165 break;
3166 case kCall:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003167 InitShortOrLong(offset_size_needed, kR6Call, kR6LongCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07003168 break;
3169 case kCondBranch:
3170 switch (condition_) {
3171 case kUncond:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003172 InitShortOrLong(offset_size_needed, kR6UncondBranch, kR6LongUncondBranch);
Alexey Frunze96b66822016-09-10 02:32:44 -07003173 break;
3174 case kCondEQZ:
3175 case kCondNEZ:
3176 // Special case for beqzc/bnezc with longer offset than in other b<cond>c instructions.
Alexey Frunze0cab6562017-07-25 15:19:36 -07003177 type_ = (offset_size_needed <= kOffset23) ? kR6CondBranch : kR6LongCondBranch;
Alexey Frunze96b66822016-09-10 02:32:44 -07003178 break;
3179 default:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003180 InitShortOrLong(offset_size_needed, kR6CondBranch, kR6LongCondBranch);
Alexey Frunze96b66822016-09-10 02:32:44 -07003181 break;
3182 }
3183 break;
Alexey Frunze0cab6562017-07-25 15:19:36 -07003184 case kBareCall:
3185 type_ = kR6BareCall;
3186 CHECK_LE(offset_size_needed, GetOffsetSize());
3187 break;
3188 case kBareCondBranch:
3189 type_ = (condition_ == kUncond) ? kR6BareUncondBranch : kR6BareCondBranch;
3190 CHECK_LE(offset_size_needed, GetOffsetSize());
3191 break;
Alexey Frunze96b66822016-09-10 02:32:44 -07003192 default:
3193 LOG(FATAL) << "Unexpected branch type " << initial_type;
3194 UNREACHABLE();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003195 }
3196 } else {
3197 // R2
Alexey Frunze96b66822016-09-10 02:32:44 -07003198 switch (initial_type) {
3199 case kLabel:
3200 CHECK(!IsResolved());
3201 type_ = kLabel;
3202 break;
3203 case kLiteral:
3204 CHECK(!IsResolved());
3205 type_ = kLiteral;
3206 break;
3207 case kCall:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003208 InitShortOrLong(offset_size_needed, kCall, kLongCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07003209 break;
3210 case kCondBranch:
3211 switch (condition_) {
3212 case kUncond:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003213 InitShortOrLong(offset_size_needed, kUncondBranch, kLongUncondBranch);
Alexey Frunze96b66822016-09-10 02:32:44 -07003214 break;
3215 default:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003216 InitShortOrLong(offset_size_needed, kCondBranch, kLongCondBranch);
Alexey Frunze96b66822016-09-10 02:32:44 -07003217 break;
3218 }
3219 break;
Alexey Frunze0cab6562017-07-25 15:19:36 -07003220 case kBareCall:
3221 type_ = kBareCall;
3222 CHECK_LE(offset_size_needed, GetOffsetSize());
3223 break;
3224 case kBareCondBranch:
3225 type_ = (condition_ == kUncond) ? kBareUncondBranch : kBareCondBranch;
3226 CHECK_LE(offset_size_needed, GetOffsetSize());
3227 break;
Alexey Frunze96b66822016-09-10 02:32:44 -07003228 default:
3229 LOG(FATAL) << "Unexpected branch type " << initial_type;
3230 UNREACHABLE();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003231 }
3232 }
3233 old_type_ = type_;
3234}
3235
3236bool MipsAssembler::Branch::IsNop(BranchCondition condition, Register lhs, Register rhs) {
3237 switch (condition) {
3238 case kCondLT:
3239 case kCondGT:
3240 case kCondNE:
3241 case kCondLTU:
3242 return lhs == rhs;
3243 default:
3244 return false;
3245 }
3246}
3247
3248bool MipsAssembler::Branch::IsUncond(BranchCondition condition, Register lhs, Register rhs) {
3249 switch (condition) {
3250 case kUncond:
3251 return true;
3252 case kCondGE:
3253 case kCondLE:
3254 case kCondEQ:
3255 case kCondGEU:
3256 return lhs == rhs;
3257 default:
3258 return false;
3259 }
3260}
3261
Alexey Frunze0cab6562017-07-25 15:19:36 -07003262MipsAssembler::Branch::Branch(bool is_r6,
3263 uint32_t location,
3264 uint32_t target,
3265 bool is_call,
3266 bool is_bare)
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003267 : old_location_(location),
3268 location_(location),
3269 target_(target),
3270 lhs_reg_(0),
3271 rhs_reg_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003272 condition_(kUncond),
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003273 delayed_instruction_(kUnfilledDelaySlot),
3274 patcher_label_(nullptr) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003275 InitializeType(
3276 (is_call ? (is_bare ? kBareCall : kCall) : (is_bare ? kBareCondBranch : kCondBranch)),
3277 is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003278}
3279
3280MipsAssembler::Branch::Branch(bool is_r6,
3281 uint32_t location,
3282 uint32_t target,
3283 MipsAssembler::BranchCondition condition,
3284 Register lhs_reg,
Alexey Frunze0cab6562017-07-25 15:19:36 -07003285 Register rhs_reg,
3286 bool is_bare)
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003287 : old_location_(location),
3288 location_(location),
3289 target_(target),
3290 lhs_reg_(lhs_reg),
3291 rhs_reg_(rhs_reg),
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003292 condition_(condition),
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003293 delayed_instruction_(kUnfilledDelaySlot),
3294 patcher_label_(nullptr) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003295 CHECK_NE(condition, kUncond);
3296 switch (condition) {
3297 case kCondLT:
3298 case kCondGE:
3299 case kCondLE:
3300 case kCondGT:
3301 case kCondLTU:
3302 case kCondGEU:
3303 // We don't support synthetic R2 branches (preceded with slt[u]) at this level
3304 // (R2 doesn't have branches to compare 2 registers using <, <=, >=, >).
3305 // We leave this up to the caller.
3306 CHECK(is_r6);
3307 FALLTHROUGH_INTENDED;
3308 case kCondEQ:
3309 case kCondNE:
3310 // Require registers other than 0 not only for R6, but also for R2 to catch errors.
3311 // To compare with 0, use dedicated kCond*Z conditions.
3312 CHECK_NE(lhs_reg, ZERO);
3313 CHECK_NE(rhs_reg, ZERO);
3314 break;
3315 case kCondLTZ:
3316 case kCondGEZ:
3317 case kCondLEZ:
3318 case kCondGTZ:
3319 case kCondEQZ:
3320 case kCondNEZ:
3321 // Require registers other than 0 not only for R6, but also for R2 to catch errors.
3322 CHECK_NE(lhs_reg, ZERO);
3323 CHECK_EQ(rhs_reg, ZERO);
3324 break;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003325 case kCondF:
3326 case kCondT:
3327 CHECK_EQ(rhs_reg, ZERO);
3328 break;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003329 case kUncond:
3330 UNREACHABLE();
3331 }
3332 CHECK(!IsNop(condition, lhs_reg, rhs_reg));
3333 if (IsUncond(condition, lhs_reg, rhs_reg)) {
3334 // Branch condition is always true, make the branch unconditional.
3335 condition_ = kUncond;
3336 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07003337 InitializeType((is_bare ? kBareCondBranch : kCondBranch), is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003338}
3339
Alexey Frunze96b66822016-09-10 02:32:44 -07003340MipsAssembler::Branch::Branch(bool is_r6,
3341 uint32_t location,
3342 Register dest_reg,
3343 Register base_reg,
3344 Type label_or_literal_type)
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003345 : old_location_(location),
3346 location_(location),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003347 target_(kUnresolved),
3348 lhs_reg_(dest_reg),
3349 rhs_reg_(base_reg),
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003350 condition_(kUncond),
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003351 delayed_instruction_(kUnfilledDelaySlot),
3352 patcher_label_(nullptr) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003353 CHECK_NE(dest_reg, ZERO);
3354 if (is_r6) {
3355 CHECK_EQ(base_reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003356 }
Alexey Frunze96b66822016-09-10 02:32:44 -07003357 InitializeType(label_or_literal_type, is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003358}
3359
3360MipsAssembler::BranchCondition MipsAssembler::Branch::OppositeCondition(
3361 MipsAssembler::BranchCondition cond) {
3362 switch (cond) {
3363 case kCondLT:
3364 return kCondGE;
3365 case kCondGE:
3366 return kCondLT;
3367 case kCondLE:
3368 return kCondGT;
3369 case kCondGT:
3370 return kCondLE;
3371 case kCondLTZ:
3372 return kCondGEZ;
3373 case kCondGEZ:
3374 return kCondLTZ;
3375 case kCondLEZ:
3376 return kCondGTZ;
3377 case kCondGTZ:
3378 return kCondLEZ;
3379 case kCondEQ:
3380 return kCondNE;
3381 case kCondNE:
3382 return kCondEQ;
3383 case kCondEQZ:
3384 return kCondNEZ;
3385 case kCondNEZ:
3386 return kCondEQZ;
3387 case kCondLTU:
3388 return kCondGEU;
3389 case kCondGEU:
3390 return kCondLTU;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003391 case kCondF:
3392 return kCondT;
3393 case kCondT:
3394 return kCondF;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003395 case kUncond:
3396 LOG(FATAL) << "Unexpected branch condition " << cond;
3397 }
3398 UNREACHABLE();
3399}
3400
3401MipsAssembler::Branch::Type MipsAssembler::Branch::GetType() const {
3402 return type_;
3403}
3404
3405MipsAssembler::BranchCondition MipsAssembler::Branch::GetCondition() const {
3406 return condition_;
3407}
3408
3409Register MipsAssembler::Branch::GetLeftRegister() const {
3410 return static_cast<Register>(lhs_reg_);
3411}
3412
3413Register MipsAssembler::Branch::GetRightRegister() const {
3414 return static_cast<Register>(rhs_reg_);
3415}
3416
3417uint32_t MipsAssembler::Branch::GetTarget() const {
3418 return target_;
3419}
3420
3421uint32_t MipsAssembler::Branch::GetLocation() const {
3422 return location_;
3423}
3424
3425uint32_t MipsAssembler::Branch::GetOldLocation() const {
3426 return old_location_;
3427}
3428
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003429uint32_t MipsAssembler::Branch::GetPrecedingInstructionLength(Type type) const {
3430 // Short branches with delay slots always consist of two instructions, the branch
3431 // and the delay slot, irrespective of whether the delay slot is filled with a
3432 // useful instruction or not.
3433 // Long composite branches may have a length longer by one instruction than
3434 // specified in branch_info_[].length. This happens when an instruction is taken
3435 // to fill the short branch delay slot, but the branch eventually becomes long
3436 // and formally has no delay slot to fill. This instruction is placed at the
3437 // beginning of the long composite branch and this needs to be accounted for in
3438 // the branch length and the location of the offset encoded in the branch.
3439 switch (type) {
3440 case kLongUncondBranch:
3441 case kLongCondBranch:
3442 case kLongCall:
3443 case kR6LongCondBranch:
3444 return (delayed_instruction_ != kUnfilledDelaySlot &&
3445 delayed_instruction_ != kUnfillableDelaySlot) ? 1 : 0;
3446 default:
3447 return 0;
3448 }
3449}
3450
3451uint32_t MipsAssembler::Branch::GetPrecedingInstructionSize(Type type) const {
3452 return GetPrecedingInstructionLength(type) * sizeof(uint32_t);
3453}
3454
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003455uint32_t MipsAssembler::Branch::GetLength() const {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003456 return GetPrecedingInstructionLength(type_) + branch_info_[type_].length;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003457}
3458
3459uint32_t MipsAssembler::Branch::GetOldLength() const {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003460 return GetPrecedingInstructionLength(old_type_) + branch_info_[old_type_].length;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003461}
3462
3463uint32_t MipsAssembler::Branch::GetSize() const {
3464 return GetLength() * sizeof(uint32_t);
3465}
3466
3467uint32_t MipsAssembler::Branch::GetOldSize() const {
3468 return GetOldLength() * sizeof(uint32_t);
3469}
3470
3471uint32_t MipsAssembler::Branch::GetEndLocation() const {
3472 return GetLocation() + GetSize();
3473}
3474
3475uint32_t MipsAssembler::Branch::GetOldEndLocation() const {
3476 return GetOldLocation() + GetOldSize();
3477}
3478
Alexey Frunze0cab6562017-07-25 15:19:36 -07003479bool MipsAssembler::Branch::IsBare() const {
3480 switch (type_) {
3481 // R2 short branches (can't be promoted to long), delay slots filled manually.
3482 case kBareUncondBranch:
3483 case kBareCondBranch:
3484 case kBareCall:
3485 // R6 short branches (can't be promoted to long), forbidden/delay slots filled manually.
3486 case kR6BareUncondBranch:
3487 case kR6BareCondBranch:
3488 case kR6BareCall:
3489 return true;
3490 default:
3491 return false;
3492 }
3493}
3494
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003495bool MipsAssembler::Branch::IsLong() const {
3496 switch (type_) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003497 // R2 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003498 case kUncondBranch:
3499 case kCondBranch:
3500 case kCall:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003501 // R2 short branches (can't be promoted to long), delay slots filled manually.
3502 case kBareUncondBranch:
3503 case kBareCondBranch:
3504 case kBareCall:
Alexey Frunze96b66822016-09-10 02:32:44 -07003505 // R2 near label.
3506 case kLabel:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003507 // R2 near literal.
3508 case kLiteral:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003509 // R6 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003510 case kR6UncondBranch:
3511 case kR6CondBranch:
3512 case kR6Call:
Alexey Frunze0cab6562017-07-25 15:19:36 -07003513 // R6 short branches (can't be promoted to long), forbidden/delay slots filled manually.
3514 case kR6BareUncondBranch:
3515 case kR6BareCondBranch:
3516 case kR6BareCall:
Alexey Frunze96b66822016-09-10 02:32:44 -07003517 // R6 near label.
3518 case kR6Label:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003519 // R6 near literal.
3520 case kR6Literal:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003521 return false;
3522 // R2 long branches.
3523 case kLongUncondBranch:
3524 case kLongCondBranch:
3525 case kLongCall:
Alexey Frunze96b66822016-09-10 02:32:44 -07003526 // R2 far label.
3527 case kFarLabel:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003528 // R2 far literal.
3529 case kFarLiteral:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003530 // R6 long branches.
3531 case kR6LongUncondBranch:
3532 case kR6LongCondBranch:
3533 case kR6LongCall:
Alexey Frunze96b66822016-09-10 02:32:44 -07003534 // R6 far label.
3535 case kR6FarLabel:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003536 // R6 far literal.
3537 case kR6FarLiteral:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003538 return true;
3539 }
3540 UNREACHABLE();
3541}
3542
3543bool MipsAssembler::Branch::IsResolved() const {
3544 return target_ != kUnresolved;
3545}
3546
3547MipsAssembler::Branch::OffsetBits MipsAssembler::Branch::GetOffsetSize() const {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003548 bool r6_cond_branch = (type_ == kR6CondBranch || type_ == kR6BareCondBranch);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003549 OffsetBits offset_size =
Alexey Frunze0cab6562017-07-25 15:19:36 -07003550 (r6_cond_branch && (condition_ == kCondEQZ || condition_ == kCondNEZ))
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003551 ? kOffset23
3552 : branch_info_[type_].offset_size;
3553 return offset_size;
3554}
3555
3556MipsAssembler::Branch::OffsetBits MipsAssembler::Branch::GetOffsetSizeNeeded(uint32_t location,
3557 uint32_t target) {
3558 // For unresolved targets assume the shortest encoding
3559 // (later it will be made longer if needed).
3560 if (target == kUnresolved)
3561 return kOffset16;
3562 int64_t distance = static_cast<int64_t>(target) - location;
3563 // To simplify calculations in composite branches consisting of multiple instructions
3564 // bump up the distance by a value larger than the max byte size of a composite branch.
3565 distance += (distance >= 0) ? kMaxBranchSize : -kMaxBranchSize;
3566 if (IsInt<kOffset16>(distance))
3567 return kOffset16;
3568 else if (IsInt<kOffset18>(distance))
3569 return kOffset18;
3570 else if (IsInt<kOffset21>(distance))
3571 return kOffset21;
3572 else if (IsInt<kOffset23>(distance))
3573 return kOffset23;
3574 else if (IsInt<kOffset28>(distance))
3575 return kOffset28;
3576 return kOffset32;
3577}
3578
3579void MipsAssembler::Branch::Resolve(uint32_t target) {
3580 target_ = target;
3581}
3582
3583void MipsAssembler::Branch::Relocate(uint32_t expand_location, uint32_t delta) {
3584 if (location_ > expand_location) {
3585 location_ += delta;
3586 }
3587 if (!IsResolved()) {
3588 return; // Don't know the target yet.
3589 }
3590 if (target_ > expand_location) {
3591 target_ += delta;
3592 }
3593}
3594
3595void MipsAssembler::Branch::PromoteToLong() {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003596 CHECK(!IsBare()); // Bare branches do not promote.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003597 switch (type_) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003598 // R2 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003599 case kUncondBranch:
3600 type_ = kLongUncondBranch;
3601 break;
3602 case kCondBranch:
3603 type_ = kLongCondBranch;
3604 break;
3605 case kCall:
3606 type_ = kLongCall;
3607 break;
Alexey Frunze96b66822016-09-10 02:32:44 -07003608 // R2 near label.
3609 case kLabel:
3610 type_ = kFarLabel;
3611 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003612 // R2 near literal.
3613 case kLiteral:
3614 type_ = kFarLiteral;
3615 break;
Alexey Frunze0cab6562017-07-25 15:19:36 -07003616 // R6 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003617 case kR6UncondBranch:
3618 type_ = kR6LongUncondBranch;
3619 break;
3620 case kR6CondBranch:
3621 type_ = kR6LongCondBranch;
3622 break;
3623 case kR6Call:
3624 type_ = kR6LongCall;
3625 break;
Alexey Frunze96b66822016-09-10 02:32:44 -07003626 // R6 near label.
3627 case kR6Label:
3628 type_ = kR6FarLabel;
3629 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003630 // R6 near literal.
3631 case kR6Literal:
3632 type_ = kR6FarLiteral;
3633 break;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003634 default:
3635 // Note: 'type_' is already long.
3636 break;
3637 }
3638 CHECK(IsLong());
3639}
3640
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003641uint32_t MipsAssembler::GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const {
3642 switch (branch->GetType()) {
Alexey Frunze96b66822016-09-10 02:32:44 -07003643 case Branch::kLabel:
3644 case Branch::kFarLabel:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003645 case Branch::kLiteral:
3646 case Branch::kFarLiteral:
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07003647 if (branch->GetRightRegister() != ZERO) {
3648 return GetLabelLocation(&pc_rel_base_label_);
3649 }
3650 // For those label/literal loads which come with their own NAL instruction
3651 // and don't depend on `pc_rel_base_label_` we can simply use the location
3652 // of the "branch" (the NAL precedes the "branch" immediately). The location
3653 // is close enough for the user of the returned location, PromoteIfNeeded(),
3654 // to not miss needed promotion to a far load.
3655 // (GetOffsetSizeNeeded() provides a little leeway by means of kMaxBranchSize,
3656 // which is larger than all composite branches and label/literal loads: it's
3657 // OK to promote a bit earlier than strictly necessary, it makes things
3658 // simpler.)
3659 FALLTHROUGH_INTENDED;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003660 default:
3661 return branch->GetLocation();
3662 }
3663}
3664
3665uint32_t MipsAssembler::Branch::PromoteIfNeeded(uint32_t location, uint32_t max_short_distance) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07003666 // `location` comes from GetBranchLocationOrPcRelBase() and is either the location
3667 // of the PC-relative branch or (for some R2 label and literal loads) the location
3668 // of `pc_rel_base_label_`. The PC-relative offset of the branch/load is relative
3669 // to this location.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003670 // If the branch is still unresolved or already long, nothing to do.
3671 if (IsLong() || !IsResolved()) {
3672 return 0;
3673 }
3674 // Promote the short branch to long if the offset size is too small
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003675 // to hold the distance between location and target_.
3676 if (GetOffsetSizeNeeded(location, target_) > GetOffsetSize()) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003677 PromoteToLong();
3678 uint32_t old_size = GetOldSize();
3679 uint32_t new_size = GetSize();
3680 CHECK_GT(new_size, old_size);
3681 return new_size - old_size;
3682 }
3683 // The following logic is for debugging/testing purposes.
3684 // Promote some short branches to long when it's not really required.
Alexey Frunze0cab6562017-07-25 15:19:36 -07003685 if (UNLIKELY(max_short_distance != std::numeric_limits<uint32_t>::max() && !IsBare())) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003686 int64_t distance = static_cast<int64_t>(target_) - location;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003687 distance = (distance >= 0) ? distance : -distance;
3688 if (distance >= max_short_distance) {
3689 PromoteToLong();
3690 uint32_t old_size = GetOldSize();
3691 uint32_t new_size = GetSize();
3692 CHECK_GT(new_size, old_size);
3693 return new_size - old_size;
3694 }
3695 }
3696 return 0;
3697}
3698
3699uint32_t MipsAssembler::Branch::GetOffsetLocation() const {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003700 return location_ + GetPrecedingInstructionSize(type_) +
3701 branch_info_[type_].instr_offset * sizeof(uint32_t);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003702}
3703
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003704uint32_t MipsAssembler::GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const {
3705 switch (branch->GetType()) {
Alexey Frunze96b66822016-09-10 02:32:44 -07003706 case Branch::kLabel:
3707 case Branch::kFarLabel:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003708 case Branch::kLiteral:
3709 case Branch::kFarLiteral:
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07003710 if (branch->GetRightRegister() == ZERO) {
3711 // These loads don't use `pc_rel_base_label_` and instead rely on their own
3712 // NAL instruction (it immediately precedes the "branch"). Therefore the
3713 // effective PC-relative base register is RA and it corresponds to the 2nd
3714 // instruction after the NAL.
3715 return branch->GetLocation() + sizeof(uint32_t);
3716 } else {
3717 return GetLabelLocation(&pc_rel_base_label_);
3718 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003719 default:
3720 return branch->GetOffsetLocation() +
3721 Branch::branch_info_[branch->GetType()].pc_org * sizeof(uint32_t);
3722 }
3723}
3724
3725uint32_t MipsAssembler::Branch::GetOffset(uint32_t location) const {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07003726 // `location` comes from GetBranchOrPcRelBaseForEncoding() and is either a location
3727 // within/near the PC-relative branch or (for some R2 label and literal loads) the
3728 // location of `pc_rel_base_label_`. The PC-relative offset of the branch/load is
3729 // relative to this location.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003730 CHECK(IsResolved());
3731 uint32_t ofs_mask = 0xFFFFFFFF >> (32 - GetOffsetSize());
3732 // Calculate the byte distance between instructions and also account for
3733 // different PC-relative origins.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003734 uint32_t offset = target_ - location;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003735 // Prepare the offset for encoding into the instruction(s).
3736 offset = (offset & ofs_mask) >> branch_info_[type_].offset_shift;
3737 return offset;
3738}
3739
3740MipsAssembler::Branch* MipsAssembler::GetBranch(uint32_t branch_id) {
3741 CHECK_LT(branch_id, branches_.size());
3742 return &branches_[branch_id];
3743}
3744
3745const MipsAssembler::Branch* MipsAssembler::GetBranch(uint32_t branch_id) const {
3746 CHECK_LT(branch_id, branches_.size());
3747 return &branches_[branch_id];
3748}
3749
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003750void MipsAssembler::BindRelativeToPrecedingBranch(MipsLabel* label,
3751 uint32_t prev_branch_id_plus_one,
3752 uint32_t position) {
3753 if (prev_branch_id_plus_one != 0) {
3754 const Branch* branch = GetBranch(prev_branch_id_plus_one - 1);
3755 position -= branch->GetEndLocation();
3756 }
3757 label->prev_branch_id_plus_one_ = prev_branch_id_plus_one;
3758 label->BindTo(position);
3759}
3760
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003761void MipsAssembler::Bind(MipsLabel* label) {
3762 CHECK(!label->IsBound());
3763 uint32_t bound_pc = buffer_.Size();
3764
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003765 // Make the delay slot FSM aware of the new label.
3766 DsFsmLabel();
3767
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003768 // Walk the list of branches referring to and preceding this label.
3769 // Store the previously unknown target addresses in them.
3770 while (label->IsLinked()) {
3771 uint32_t branch_id = label->Position();
3772 Branch* branch = GetBranch(branch_id);
3773 branch->Resolve(bound_pc);
3774
3775 uint32_t branch_location = branch->GetLocation();
3776 // Extract the location of the previous branch in the list (walking the list backwards;
3777 // the previous branch ID was stored in the space reserved for this branch).
3778 uint32_t prev = buffer_.Load<uint32_t>(branch_location);
3779
3780 // On to the previous branch in the list...
3781 label->position_ = prev;
3782 }
3783
3784 // Now make the label object contain its own location (relative to the end of the preceding
3785 // branch, if any; it will be used by the branches referring to and following this label).
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003786 BindRelativeToPrecedingBranch(label, branches_.size(), bound_pc);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003787}
3788
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003789uint32_t MipsAssembler::GetLabelLocation(const MipsLabel* label) const {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003790 CHECK(label->IsBound());
3791 uint32_t target = label->Position();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003792 if (label->prev_branch_id_plus_one_ != 0) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003793 // Get label location based on the branch preceding it.
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003794 const Branch* branch = GetBranch(label->prev_branch_id_plus_one_ - 1);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003795 target += branch->GetEndLocation();
3796 }
3797 return target;
3798}
3799
3800uint32_t MipsAssembler::GetAdjustedPosition(uint32_t old_position) {
3801 // We can reconstruct the adjustment by going through all the branches from the beginning
3802 // up to the old_position. Since we expect AdjustedPosition() to be called in a loop
3803 // with increasing old_position, we can use the data from last AdjustedPosition() to
3804 // continue where we left off and the whole loop should be O(m+n) where m is the number
3805 // of positions to adjust and n is the number of branches.
3806 if (old_position < last_old_position_) {
3807 last_position_adjustment_ = 0;
3808 last_old_position_ = 0;
3809 last_branch_id_ = 0;
3810 }
3811 while (last_branch_id_ != branches_.size()) {
3812 const Branch* branch = GetBranch(last_branch_id_);
3813 if (branch->GetLocation() >= old_position + last_position_adjustment_) {
3814 break;
3815 }
3816 last_position_adjustment_ += branch->GetSize() - branch->GetOldSize();
3817 ++last_branch_id_;
3818 }
3819 last_old_position_ = old_position;
3820 return old_position + last_position_adjustment_;
3821}
3822
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003823void MipsAssembler::BindPcRelBaseLabel() {
3824 Bind(&pc_rel_base_label_);
3825}
3826
Alexey Frunze06a46c42016-07-19 15:00:40 -07003827uint32_t MipsAssembler::GetPcRelBaseLabelLocation() const {
3828 return GetLabelLocation(&pc_rel_base_label_);
3829}
3830
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003831void MipsAssembler::FinalizeLabeledBranch(MipsLabel* label) {
3832 uint32_t length = branches_.back().GetLength();
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003833 // Commit the last branch target label (if any).
3834 DsFsmCommitLabel();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003835 if (!label->IsBound()) {
3836 // Branch forward (to a following label), distance is unknown.
3837 // The first branch forward will contain 0, serving as the terminator of
3838 // the list of forward-reaching branches.
3839 Emit(label->position_);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003840 // Nothing for the delay slot (yet).
3841 DsFsmInstrNop(0);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003842 length--;
3843 // Now make the label object point to this branch
3844 // (this forms a linked list of branches preceding this label).
3845 uint32_t branch_id = branches_.size() - 1;
3846 label->LinkTo(branch_id);
3847 }
3848 // Reserve space for the branch.
3849 while (length--) {
3850 Nop();
3851 }
3852}
3853
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003854bool MipsAssembler::Branch::CanHaveDelayedInstruction(const DelaySlot& delay_slot) const {
3855 if (delay_slot.instruction_ == 0) {
3856 // NOP or no instruction for the delay slot.
3857 return false;
3858 }
3859 switch (type_) {
3860 // R2 unconditional branches.
3861 case kUncondBranch:
3862 case kLongUncondBranch:
3863 // There are no register interdependencies.
3864 return true;
3865
3866 // R2 calls.
3867 case kCall:
3868 case kLongCall:
3869 // Instructions depending on or modifying RA should not be moved into delay slots
3870 // of branches modifying RA.
3871 return ((delay_slot.gpr_ins_mask_ | delay_slot.gpr_outs_mask_) & (1u << RA)) == 0;
3872
3873 // R2 conditional branches.
3874 case kCondBranch:
3875 case kLongCondBranch:
3876 switch (condition_) {
3877 // Branches with one GPR source.
3878 case kCondLTZ:
3879 case kCondGEZ:
3880 case kCondLEZ:
3881 case kCondGTZ:
3882 case kCondEQZ:
3883 case kCondNEZ:
3884 return (delay_slot.gpr_outs_mask_ & (1u << lhs_reg_)) == 0;
3885
3886 // Branches with two GPR sources.
3887 case kCondEQ:
3888 case kCondNE:
3889 return (delay_slot.gpr_outs_mask_ & ((1u << lhs_reg_) | (1u << rhs_reg_))) == 0;
3890
3891 // Branches with one FPU condition code source.
3892 case kCondF:
3893 case kCondT:
3894 return (delay_slot.cc_outs_mask_ & (1u << lhs_reg_)) == 0;
3895
3896 default:
3897 // We don't support synthetic R2 branches (preceded with slt[u]) at this level
3898 // (R2 doesn't have branches to compare 2 registers using <, <=, >=, >).
3899 LOG(FATAL) << "Unexpected branch condition " << condition_;
3900 UNREACHABLE();
3901 }
3902
3903 // R6 unconditional branches.
3904 case kR6UncondBranch:
3905 case kR6LongUncondBranch:
3906 // R6 calls.
3907 case kR6Call:
3908 case kR6LongCall:
3909 // There are no delay slots.
3910 return false;
3911
3912 // R6 conditional branches.
3913 case kR6CondBranch:
3914 case kR6LongCondBranch:
3915 switch (condition_) {
3916 // Branches with one FPU register source.
3917 case kCondF:
3918 case kCondT:
3919 return (delay_slot.fpr_outs_mask_ & (1u << lhs_reg_)) == 0;
3920 // Others have a forbidden slot instead of a delay slot.
3921 default:
3922 return false;
3923 }
3924
3925 // Literals.
3926 default:
3927 LOG(FATAL) << "Unexpected branch type " << type_;
3928 UNREACHABLE();
3929 }
3930}
3931
3932uint32_t MipsAssembler::Branch::GetDelayedInstruction() const {
3933 return delayed_instruction_;
3934}
3935
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003936MipsLabel* MipsAssembler::Branch::GetPatcherLabel() const {
3937 return patcher_label_;
3938}
3939
3940void MipsAssembler::Branch::SetDelayedInstruction(uint32_t instruction, MipsLabel* patcher_label) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003941 CHECK_NE(instruction, kUnfilledDelaySlot);
3942 CHECK_EQ(delayed_instruction_, kUnfilledDelaySlot);
3943 delayed_instruction_ = instruction;
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003944 patcher_label_ = patcher_label;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003945}
3946
3947void MipsAssembler::Branch::DecrementLocations() {
3948 // We first create a branch object, which gets its type and locations initialized,
3949 // and then we check if the branch can actually have the preceding instruction moved
3950 // into its delay slot. If it can, the branch locations need to be decremented.
3951 //
3952 // We could make the check before creating the branch object and avoid the location
3953 // adjustment, but the check is cleaner when performed on an initialized branch
3954 // object.
3955 //
3956 // If the branch is backwards (to a previously bound label), reducing the locations
3957 // cannot cause a short branch to exceed its offset range because the offset reduces.
3958 // And this is not at all a problem for a long branch backwards.
3959 //
3960 // If the branch is forward (not linked to any label yet), reducing the locations
3961 // is harmless. The branch will be promoted to long if needed when the target is known.
3962 CHECK_EQ(location_, old_location_);
3963 CHECK_GE(old_location_, sizeof(uint32_t));
3964 old_location_ -= sizeof(uint32_t);
3965 location_ = old_location_;
3966}
3967
3968void MipsAssembler::MoveInstructionToDelaySlot(Branch& branch) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07003969 if (branch.IsBare()) {
3970 // Delay slots are filled manually in bare branches.
3971 return;
3972 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003973 if (branch.CanHaveDelayedInstruction(delay_slot_)) {
3974 // The last instruction cannot be used in a different delay slot,
3975 // do not commit the label before it (if any).
3976 DsFsmDropLabel();
3977 // Remove the last emitted instruction.
3978 size_t size = buffer_.Size();
3979 CHECK_GE(size, sizeof(uint32_t));
3980 size -= sizeof(uint32_t);
3981 CHECK_EQ(buffer_.Load<uint32_t>(size), delay_slot_.instruction_);
3982 buffer_.Resize(size);
3983 // Attach it to the branch and adjust the branch locations.
3984 branch.DecrementLocations();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07003985 branch.SetDelayedInstruction(delay_slot_.instruction_, delay_slot_.patcher_label_);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003986 } else if (!reordering_ && branch.GetType() == Branch::kUncondBranch) {
3987 // If reordefing is disabled, prevent absorption of the target instruction.
3988 branch.SetDelayedInstruction(Branch::kUnfillableDelaySlot);
3989 }
3990}
3991
Alexey Frunze0cab6562017-07-25 15:19:36 -07003992void MipsAssembler::Buncond(MipsLabel* label, bool is_r6, bool is_bare) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003993 uint32_t target = label->IsBound() ? GetLabelLocation(label) : Branch::kUnresolved;
Alexey Frunze0cab6562017-07-25 15:19:36 -07003994 branches_.emplace_back(is_r6, buffer_.Size(), target, /* is_call */ false, is_bare);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07003995 MoveInstructionToDelaySlot(branches_.back());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02003996 FinalizeLabeledBranch(label);
3997}
3998
Alexey Frunze0cab6562017-07-25 15:19:36 -07003999void MipsAssembler::Bcond(MipsLabel* label,
4000 bool is_r6,
4001 bool is_bare,
4002 BranchCondition condition,
4003 Register lhs,
4004 Register rhs) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004005 // If lhs = rhs, this can be a NOP.
4006 if (Branch::IsNop(condition, lhs, rhs)) {
4007 return;
4008 }
4009 uint32_t target = label->IsBound() ? GetLabelLocation(label) : Branch::kUnresolved;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004010 branches_.emplace_back(is_r6, buffer_.Size(), target, condition, lhs, rhs, is_bare);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004011 MoveInstructionToDelaySlot(branches_.back());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004012 FinalizeLabeledBranch(label);
4013}
4014
Alexey Frunze0cab6562017-07-25 15:19:36 -07004015void MipsAssembler::Call(MipsLabel* label, bool is_r6, bool is_bare) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004016 uint32_t target = label->IsBound() ? GetLabelLocation(label) : Branch::kUnresolved;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004017 branches_.emplace_back(is_r6, buffer_.Size(), target, /* is_call */ true, is_bare);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004018 MoveInstructionToDelaySlot(branches_.back());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004019 FinalizeLabeledBranch(label);
4020}
4021
Alexey Frunze96b66822016-09-10 02:32:44 -07004022void MipsAssembler::LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label) {
4023 // Label address loads are treated as pseudo branches since they require very similar handling.
4024 DCHECK(!label->IsBound());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004025 // If `pc_rel_base_label_` isn't bound or none of registers contains its address, we
4026 // may generate an individual NAL instruction to simulate PC-relative addressing on R2
4027 // by specifying `base_reg` of `ZERO`. Check for it.
4028 if (base_reg == ZERO && !IsR6()) {
4029 Nal();
4030 }
Alexey Frunze96b66822016-09-10 02:32:44 -07004031 branches_.emplace_back(IsR6(), buffer_.Size(), dest_reg, base_reg, Branch::kLabel);
4032 FinalizeLabeledBranch(label);
4033}
4034
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004035Literal* MipsAssembler::NewLiteral(size_t size, const uint8_t* data) {
4036 DCHECK(size == 4u || size == 8u) << size;
4037 literals_.emplace_back(size, data);
4038 return &literals_.back();
4039}
4040
4041void MipsAssembler::LoadLiteral(Register dest_reg, Register base_reg, Literal* literal) {
4042 // Literal loads are treated as pseudo branches since they require very similar handling.
4043 DCHECK_EQ(literal->GetSize(), 4u);
4044 MipsLabel* label = literal->GetLabel();
4045 DCHECK(!label->IsBound());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004046 // If `pc_rel_base_label_` isn't bound or none of registers contains its address, we
4047 // may generate an individual NAL instruction to simulate PC-relative addressing on R2
4048 // by specifying `base_reg` of `ZERO`. Check for it.
4049 if (base_reg == ZERO && !IsR6()) {
4050 Nal();
4051 }
Alexey Frunze96b66822016-09-10 02:32:44 -07004052 branches_.emplace_back(IsR6(), buffer_.Size(), dest_reg, base_reg, Branch::kLiteral);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004053 FinalizeLabeledBranch(label);
4054}
4055
Alexey Frunze96b66822016-09-10 02:32:44 -07004056JumpTable* MipsAssembler::CreateJumpTable(std::vector<MipsLabel*>&& labels) {
4057 jump_tables_.emplace_back(std::move(labels));
4058 JumpTable* table = &jump_tables_.back();
4059 DCHECK(!table->GetLabel()->IsBound());
4060 return table;
4061}
4062
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004063void MipsAssembler::EmitLiterals() {
4064 if (!literals_.empty()) {
4065 // We don't support byte and half-word literals.
4066 // TODO: proper alignment for 64-bit literals when they're implemented.
4067 for (Literal& literal : literals_) {
4068 MipsLabel* label = literal.GetLabel();
4069 Bind(label);
4070 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
4071 DCHECK(literal.GetSize() == 4u || literal.GetSize() == 8u);
4072 for (size_t i = 0, size = literal.GetSize(); i != size; ++i) {
4073 buffer_.Emit<uint8_t>(literal.GetData()[i]);
4074 }
4075 }
4076 }
4077}
4078
Alexey Frunze96b66822016-09-10 02:32:44 -07004079void MipsAssembler::ReserveJumpTableSpace() {
4080 if (!jump_tables_.empty()) {
4081 for (JumpTable& table : jump_tables_) {
4082 MipsLabel* label = table.GetLabel();
4083 Bind(label);
4084
4085 // Bulk ensure capacity, as this may be large.
4086 size_t orig_size = buffer_.Size();
4087 size_t required_capacity = orig_size + table.GetSize();
4088 if (required_capacity > buffer_.Capacity()) {
4089 buffer_.ExtendCapacity(required_capacity);
4090 }
4091#ifndef NDEBUG
4092 buffer_.has_ensured_capacity_ = true;
4093#endif
4094
4095 // Fill the space with dummy data as the data is not final
4096 // until the branches have been promoted. And we shouldn't
4097 // be moving uninitialized data during branch promotion.
4098 for (size_t cnt = table.GetData().size(), i = 0; i < cnt; i++) {
4099 buffer_.Emit<uint32_t>(0x1abe1234u);
4100 }
4101
4102#ifndef NDEBUG
4103 buffer_.has_ensured_capacity_ = false;
4104#endif
4105 }
4106 }
4107}
4108
4109void MipsAssembler::EmitJumpTables() {
4110 if (!jump_tables_.empty()) {
4111 CHECK(!overwriting_);
4112 // Switch from appending instructions at the end of the buffer to overwriting
4113 // existing instructions (here, jump tables) in the buffer.
4114 overwriting_ = true;
4115
4116 for (JumpTable& table : jump_tables_) {
4117 MipsLabel* table_label = table.GetLabel();
4118 uint32_t start = GetLabelLocation(table_label);
4119 overwrite_location_ = start;
4120
4121 for (MipsLabel* target : table.GetData()) {
4122 CHECK_EQ(buffer_.Load<uint32_t>(overwrite_location_), 0x1abe1234u);
4123 // The table will contain target addresses relative to the table start.
4124 uint32_t offset = GetLabelLocation(target) - start;
4125 Emit(offset);
4126 }
4127 }
4128
4129 overwriting_ = false;
4130 }
4131}
4132
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004133void MipsAssembler::PromoteBranches() {
4134 // Promote short branches to long as necessary.
4135 bool changed;
4136 do {
4137 changed = false;
4138 for (auto& branch : branches_) {
4139 CHECK(branch.IsResolved());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004140 uint32_t base = GetBranchLocationOrPcRelBase(&branch);
4141 uint32_t delta = branch.PromoteIfNeeded(base);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004142 // If this branch has been promoted and needs to expand in size,
4143 // relocate all branches by the expansion size.
4144 if (delta) {
4145 changed = true;
4146 uint32_t expand_location = branch.GetLocation();
4147 for (auto& branch2 : branches_) {
4148 branch2.Relocate(expand_location, delta);
4149 }
4150 }
4151 }
4152 } while (changed);
4153
4154 // Account for branch expansion by resizing the code buffer
4155 // and moving the code in it to its final location.
4156 size_t branch_count = branches_.size();
4157 if (branch_count > 0) {
4158 // Resize.
4159 Branch& last_branch = branches_[branch_count - 1];
4160 uint32_t size_delta = last_branch.GetEndLocation() - last_branch.GetOldEndLocation();
4161 uint32_t old_size = buffer_.Size();
4162 buffer_.Resize(old_size + size_delta);
4163 // Move the code residing between branch placeholders.
4164 uint32_t end = old_size;
4165 for (size_t i = branch_count; i > 0; ) {
4166 Branch& branch = branches_[--i];
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004167 CHECK_GE(end, branch.GetOldEndLocation());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004168 uint32_t size = end - branch.GetOldEndLocation();
4169 buffer_.Move(branch.GetEndLocation(), branch.GetOldEndLocation(), size);
4170 end = branch.GetOldLocation();
4171 }
4172 }
4173}
4174
4175// Note: make sure branch_info_[] and EmitBranch() are kept synchronized.
4176const MipsAssembler::Branch::BranchInfo MipsAssembler::Branch::branch_info_[] = {
Alexey Frunze0cab6562017-07-25 15:19:36 -07004177 // R2 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004178 { 2, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kUncondBranch
4179 { 2, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kCondBranch
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004180 { 2, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kCall
Alexey Frunze0cab6562017-07-25 15:19:36 -07004181 // R2 short branches (can't be promoted to long), delay slots filled manually.
4182 { 1, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kBareUncondBranch
4183 { 1, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kBareCondBranch
4184 { 1, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kBareCall
Alexey Frunze96b66822016-09-10 02:32:44 -07004185 // R2 near label.
4186 { 1, 0, 0, MipsAssembler::Branch::kOffset16, 0 }, // kLabel
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004187 // R2 near literal.
4188 { 1, 0, 0, MipsAssembler::Branch::kOffset16, 0 }, // kLiteral
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004189 // R2 long branches.
4190 { 9, 3, 1, MipsAssembler::Branch::kOffset32, 0 }, // kLongUncondBranch
4191 { 10, 4, 1, MipsAssembler::Branch::kOffset32, 0 }, // kLongCondBranch
4192 { 6, 1, 1, MipsAssembler::Branch::kOffset32, 0 }, // kLongCall
Alexey Frunze96b66822016-09-10 02:32:44 -07004193 // R2 far label.
4194 { 3, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kFarLabel
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004195 // R2 far literal.
4196 { 3, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kFarLiteral
Alexey Frunze0cab6562017-07-25 15:19:36 -07004197 // R6 short branches (can be promoted to long).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004198 { 1, 0, 1, MipsAssembler::Branch::kOffset28, 2 }, // kR6UncondBranch
4199 { 2, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kR6CondBranch
4200 // Exception: kOffset23 for beqzc/bnezc.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004201 { 1, 0, 1, MipsAssembler::Branch::kOffset28, 2 }, // kR6Call
Alexey Frunze0cab6562017-07-25 15:19:36 -07004202 // R6 short branches (can't be promoted to long), forbidden/delay slots filled manually.
4203 { 1, 0, 1, MipsAssembler::Branch::kOffset28, 2 }, // kR6BareUncondBranch
4204 { 1, 0, 1, MipsAssembler::Branch::kOffset18, 2 }, // kR6BareCondBranch
4205 // Exception: kOffset23 for beqzc/bnezc.
4206 { 1, 0, 1, MipsAssembler::Branch::kOffset28, 2 }, // kR6BareCall
Alexey Frunze96b66822016-09-10 02:32:44 -07004207 // R6 near label.
4208 { 1, 0, 0, MipsAssembler::Branch::kOffset21, 2 }, // kR6Label
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004209 // R6 near literal.
4210 { 1, 0, 0, MipsAssembler::Branch::kOffset21, 2 }, // kR6Literal
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004211 // R6 long branches.
4212 { 2, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kR6LongUncondBranch
4213 { 3, 1, 0, MipsAssembler::Branch::kOffset32, 0 }, // kR6LongCondBranch
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004214 { 2, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kR6LongCall
Alexey Frunze96b66822016-09-10 02:32:44 -07004215 // R6 far label.
4216 { 2, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kR6FarLabel
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004217 // R6 far literal.
4218 { 2, 0, 0, MipsAssembler::Branch::kOffset32, 0 }, // kR6FarLiteral
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004219};
4220
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004221static inline bool IsAbsorbableInstruction(uint32_t instruction) {
4222 // The relative patcher patches addiu, lw and sw with an immediate operand of 0x5678.
4223 // We want to make sure that these instructions do not get absorbed into delay slots
4224 // of unconditional branches on R2. Absorption would otherwise make copies of
4225 // unpatched instructions.
4226 if ((instruction & 0xFFFF) != 0x5678) {
4227 return true;
4228 }
4229 switch (instruction >> kOpcodeShift) {
4230 case 0x09: // Addiu.
4231 case 0x23: // Lw.
4232 case 0x2B: // Sw.
4233 return false;
4234 default:
4235 return true;
4236 }
4237}
4238
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004239static inline Register GetR2PcRelBaseRegister(Register reg) {
4240 // LoadLabelAddress() and LoadLiteral() generate individual NAL
4241 // instructions on R2 when the specified base register is ZERO
4242 // and so the effective PC-relative base register is RA, not ZERO.
4243 return (reg == ZERO) ? RA : reg;
4244}
4245
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004246// Note: make sure branch_info_[] and EmitBranch() are kept synchronized.
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004247void MipsAssembler::EmitBranch(uint32_t branch_id) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004248 CHECK_EQ(overwriting_, true);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004249 Branch* branch = GetBranch(branch_id);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004250 overwrite_location_ = branch->GetLocation();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004251 uint32_t offset = branch->GetOffset(GetBranchOrPcRelBaseForEncoding(branch));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004252 BranchCondition condition = branch->GetCondition();
4253 Register lhs = branch->GetLeftRegister();
4254 Register rhs = branch->GetRightRegister();
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004255 uint32_t delayed_instruction = branch->GetDelayedInstruction();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004256 MipsLabel* patcher_label = branch->GetPatcherLabel();
4257 if (patcher_label != nullptr) {
4258 // Update the patcher label location to account for branch promotion and
4259 // delay slot filling.
4260 CHECK(patcher_label->IsBound());
4261 uint32_t bound_pc = branch->GetLocation();
4262 if (!branch->IsLong()) {
4263 // Short branches precede delay slots.
4264 // Long branches follow "delay slots".
4265 bound_pc += sizeof(uint32_t);
4266 }
4267 // Rebind the label.
4268 patcher_label->Reinitialize();
4269 BindRelativeToPrecedingBranch(patcher_label, branch_id, bound_pc);
4270 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004271 switch (branch->GetType()) {
4272 // R2 short branches.
4273 case Branch::kUncondBranch:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004274 if (delayed_instruction == Branch::kUnfillableDelaySlot) {
4275 // The branch was created when reordering was disabled, do not absorb the target
4276 // instruction.
4277 delayed_instruction = 0; // NOP.
4278 } else if (delayed_instruction == Branch::kUnfilledDelaySlot) {
4279 // Try to absorb the target instruction into the delay slot.
4280 delayed_instruction = 0; // NOP.
4281 // Incrementing the signed 16-bit offset past the target instruction must not
4282 // cause overflow into the negative subrange, check for the max offset.
4283 if (offset != 0x7FFF) {
4284 uint32_t target = branch->GetTarget();
4285 if (std::binary_search(ds_fsm_target_pcs_.begin(), ds_fsm_target_pcs_.end(), target)) {
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004286 uint32_t target_instruction = buffer_.Load<uint32_t>(target);
4287 if (IsAbsorbableInstruction(target_instruction)) {
4288 delayed_instruction = target_instruction;
4289 offset++;
4290 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004291 }
4292 }
4293 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004294 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4295 B(offset);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004296 Emit(delayed_instruction);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004297 break;
4298 case Branch::kCondBranch:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004299 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4300 if (delayed_instruction == Branch::kUnfilledDelaySlot) {
4301 delayed_instruction = 0; // NOP.
4302 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004303 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004304 EmitBcondR2(condition, lhs, rhs, offset);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004305 Emit(delayed_instruction);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004306 break;
4307 case Branch::kCall:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004308 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4309 if (delayed_instruction == Branch::kUnfilledDelaySlot) {
4310 delayed_instruction = 0; // NOP.
4311 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004312 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004313 Bal(offset);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004314 Emit(delayed_instruction);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004315 break;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004316 case Branch::kBareUncondBranch:
4317 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4318 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4319 B(offset);
4320 break;
4321 case Branch::kBareCondBranch:
4322 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4323 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4324 EmitBcondR2(condition, lhs, rhs, offset);
4325 break;
4326 case Branch::kBareCall:
4327 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4328 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4329 Bal(offset);
4330 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004331
Alexey Frunze96b66822016-09-10 02:32:44 -07004332 // R2 near label.
4333 case Branch::kLabel:
4334 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4335 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004336 Addiu(lhs, GetR2PcRelBaseRegister(rhs), offset);
Alexey Frunze96b66822016-09-10 02:32:44 -07004337 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004338 // R2 near literal.
4339 case Branch::kLiteral:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004340 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004341 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004342 Lw(lhs, GetR2PcRelBaseRegister(rhs), offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004343 break;
4344
4345 // R2 long branches.
4346 case Branch::kLongUncondBranch:
4347 // To get the value of the PC register we need to use the NAL instruction.
4348 // NAL clobbers the RA register. However, RA must be preserved if the
4349 // method is compiled without the entry/exit sequences that would take care
4350 // of preserving RA (typically, leaf methods don't preserve RA explicitly).
4351 // So, we need to preserve RA in some temporary storage ourselves. The AT
4352 // register can't be used for this because we need it to load a constant
4353 // which will be added to the value that NAL stores in RA. And we can't
4354 // use T9 for this in the context of the JNI compiler, which uses it
4355 // as a scratch register (see InterproceduralScratchRegister()).
4356 // If we were to add a 32-bit constant to RA using two ADDIU instructions,
4357 // we'd also need to use the ROTR instruction, which requires no less than
4358 // MIPSR2.
4359 // Perhaps, we could use T8 or one of R2's multiplier/divider registers
4360 // (LO or HI) or even a floating-point register, but that doesn't seem
4361 // like a nice solution. We may want this to work on both R6 and pre-R6.
4362 // For now simply use the stack for RA. This should be OK since for the
4363 // vast majority of code a short PC-relative branch is sufficient.
4364 // TODO: can this be improved?
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004365 // TODO: consider generation of a shorter sequence when we know that RA
4366 // is explicitly preserved by the method entry/exit code.
4367 if (delayed_instruction != Branch::kUnfilledDelaySlot &&
4368 delayed_instruction != Branch::kUnfillableDelaySlot) {
4369 Emit(delayed_instruction);
4370 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004371 Push(RA);
4372 Nal();
4373 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4374 Lui(AT, High16Bits(offset));
4375 Ori(AT, AT, Low16Bits(offset));
4376 Addu(AT, AT, RA);
4377 Lw(RA, SP, 0);
4378 Jr(AT);
4379 DecreaseFrameSize(kMipsWordSize);
4380 break;
4381 case Branch::kLongCondBranch:
4382 // The comment on case 'Branch::kLongUncondBranch' applies here as well.
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004383 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4384 if (delayed_instruction != Branch::kUnfilledDelaySlot) {
4385 Emit(delayed_instruction);
4386 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004387 // Note: the opposite condition branch encodes 8 as the distance, which is equal to the
4388 // number of instructions skipped:
4389 // (PUSH(IncreaseFrameSize(ADDIU) + SW) + NAL + LUI + ORI + ADDU + LW + JR).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004390 EmitBcondR2(Branch::OppositeCondition(condition), lhs, rhs, 8);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004391 Push(RA);
4392 Nal();
4393 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4394 Lui(AT, High16Bits(offset));
4395 Ori(AT, AT, Low16Bits(offset));
4396 Addu(AT, AT, RA);
4397 Lw(RA, SP, 0);
4398 Jr(AT);
4399 DecreaseFrameSize(kMipsWordSize);
4400 break;
4401 case Branch::kLongCall:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004402 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4403 if (delayed_instruction != Branch::kUnfilledDelaySlot) {
4404 Emit(delayed_instruction);
4405 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004406 Nal();
4407 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4408 Lui(AT, High16Bits(offset));
4409 Ori(AT, AT, Low16Bits(offset));
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004410 Addu(AT, AT, RA);
4411 Jalr(AT);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004412 Nop();
4413 break;
4414
Alexey Frunze96b66822016-09-10 02:32:44 -07004415 // R2 far label.
4416 case Branch::kFarLabel:
4417 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4418 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4419 Lui(AT, High16Bits(offset));
4420 Ori(AT, AT, Low16Bits(offset));
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004421 Addu(lhs, AT, GetR2PcRelBaseRegister(rhs));
Alexey Frunze96b66822016-09-10 02:32:44 -07004422 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004423 // R2 far literal.
4424 case Branch::kFarLiteral:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004425 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004426 offset += (offset & 0x8000) << 1; // Account for sign extension in lw.
4427 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4428 Lui(AT, High16Bits(offset));
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07004429 Addu(AT, AT, GetR2PcRelBaseRegister(rhs));
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004430 Lw(lhs, AT, Low16Bits(offset));
4431 break;
4432
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004433 // R6 short branches.
4434 case Branch::kR6UncondBranch:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004435 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004436 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4437 Bc(offset);
4438 break;
4439 case Branch::kR6CondBranch:
4440 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004441 EmitBcondR6(condition, lhs, rhs, offset);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004442 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4443 if (delayed_instruction != Branch::kUnfilledDelaySlot) {
4444 Emit(delayed_instruction);
4445 } else {
4446 // TODO: improve by filling the forbidden slot (IFF this is
4447 // a forbidden and not a delay slot).
4448 Nop();
4449 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004450 break;
4451 case Branch::kR6Call:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004452 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004453 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004454 Balc(offset);
4455 break;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004456 case Branch::kR6BareUncondBranch:
4457 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4458 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4459 Bc(offset);
4460 break;
4461 case Branch::kR6BareCondBranch:
4462 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4463 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4464 EmitBcondR6(condition, lhs, rhs, offset);
4465 break;
4466 case Branch::kR6BareCall:
4467 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4468 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4469 Balc(offset);
4470 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004471
Alexey Frunze96b66822016-09-10 02:32:44 -07004472 // R6 near label.
4473 case Branch::kR6Label:
4474 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4475 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4476 Addiupc(lhs, offset);
4477 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004478 // R6 near literal.
4479 case Branch::kR6Literal:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004480 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004481 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4482 Lwpc(lhs, offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004483 break;
4484
4485 // R6 long branches.
4486 case Branch::kR6LongUncondBranch:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004487 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004488 offset += (offset & 0x8000) << 1; // Account for sign extension in jic.
4489 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4490 Auipc(AT, High16Bits(offset));
4491 Jic(AT, Low16Bits(offset));
4492 break;
4493 case Branch::kR6LongCondBranch:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004494 DCHECK_NE(delayed_instruction, Branch::kUnfillableDelaySlot);
4495 if (delayed_instruction != Branch::kUnfilledDelaySlot) {
4496 Emit(delayed_instruction);
4497 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004498 EmitBcondR6(Branch::OppositeCondition(condition), lhs, rhs, 2);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004499 offset += (offset & 0x8000) << 1; // Account for sign extension in jic.
4500 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4501 Auipc(AT, High16Bits(offset));
4502 Jic(AT, Low16Bits(offset));
4503 break;
4504 case Branch::kR6LongCall:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004505 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004506 offset += (offset & 0x8000) << 1; // Account for sign extension in jialc.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004507 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004508 Auipc(AT, High16Bits(offset));
4509 Jialc(AT, Low16Bits(offset));
4510 break;
4511
Alexey Frunze96b66822016-09-10 02:32:44 -07004512 // R6 far label.
4513 case Branch::kR6FarLabel:
4514 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
4515 offset += (offset & 0x8000) << 1; // Account for sign extension in addiu.
4516 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4517 Auipc(AT, High16Bits(offset));
4518 Addiu(lhs, AT, Low16Bits(offset));
4519 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004520 // R6 far literal.
4521 case Branch::kR6FarLiteral:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004522 DCHECK_EQ(delayed_instruction, Branch::kUnfilledDelaySlot);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004523 offset += (offset & 0x8000) << 1; // Account for sign extension in lw.
4524 CHECK_EQ(overwrite_location_, branch->GetOffsetLocation());
4525 Auipc(AT, High16Bits(offset));
4526 Lw(lhs, AT, Low16Bits(offset));
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004527 break;
4528 }
4529 CHECK_EQ(overwrite_location_, branch->GetEndLocation());
4530 CHECK_LT(branch->GetSize(), static_cast<uint32_t>(Branch::kMaxBranchSize));
Alexey Frunzea663d9d2017-07-31 18:43:18 -07004531 if (patcher_label != nullptr) {
4532 // The patched instruction should look like one.
4533 uint32_t patched_instruction = buffer_.Load<uint32_t>(GetLabelLocation(patcher_label));
4534 CHECK(!IsAbsorbableInstruction(patched_instruction));
4535 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004536}
4537
Alexey Frunze0cab6562017-07-25 15:19:36 -07004538void MipsAssembler::B(MipsLabel* label, bool is_bare) {
4539 Buncond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004540}
4541
Alexey Frunze0cab6562017-07-25 15:19:36 -07004542void MipsAssembler::Bal(MipsLabel* label, bool is_bare) {
4543 Call(label, /* is_r6 */ (IsR6() && !is_bare), is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004544}
4545
Alexey Frunze0cab6562017-07-25 15:19:36 -07004546void MipsAssembler::Beq(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4547 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondEQ, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004548}
4549
Alexey Frunze0cab6562017-07-25 15:19:36 -07004550void MipsAssembler::Bne(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4551 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondNE, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004552}
4553
Alexey Frunze0cab6562017-07-25 15:19:36 -07004554void MipsAssembler::Beqz(Register rt, MipsLabel* label, bool is_bare) {
4555 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondEQZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004556}
4557
Alexey Frunze0cab6562017-07-25 15:19:36 -07004558void MipsAssembler::Bnez(Register rt, MipsLabel* label, bool is_bare) {
4559 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondNEZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004560}
4561
Alexey Frunze0cab6562017-07-25 15:19:36 -07004562void MipsAssembler::Bltz(Register rt, MipsLabel* label, bool is_bare) {
4563 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondLTZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004564}
4565
Alexey Frunze0cab6562017-07-25 15:19:36 -07004566void MipsAssembler::Bgez(Register rt, MipsLabel* label, bool is_bare) {
4567 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondGEZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004568}
4569
Alexey Frunze0cab6562017-07-25 15:19:36 -07004570void MipsAssembler::Blez(Register rt, MipsLabel* label, bool is_bare) {
4571 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondLEZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004572}
4573
Alexey Frunze0cab6562017-07-25 15:19:36 -07004574void MipsAssembler::Bgtz(Register rt, MipsLabel* label, bool is_bare) {
4575 Bcond(label, /* is_r6 */ (IsR6() && !is_bare), is_bare, kCondGTZ, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004576}
4577
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004578bool MipsAssembler::CanExchangeWithSlt(Register rs, Register rt) const {
4579 // If the instruction modifies AT, `rs` or `rt`, it can't be exchanged with the slt[u]
4580 // instruction because either slt[u] depends on `rs` or `rt` or the following
4581 // conditional branch depends on AT set by slt[u].
4582 // Likewise, if the instruction depends on AT, it can't be exchanged with slt[u]
4583 // because slt[u] changes AT.
4584 return (delay_slot_.instruction_ != 0 &&
4585 (delay_slot_.gpr_outs_mask_ & ((1u << AT) | (1u << rs) | (1u << rt))) == 0 &&
4586 (delay_slot_.gpr_ins_mask_ & (1u << AT)) == 0);
4587}
4588
4589void MipsAssembler::ExchangeWithSlt(const DelaySlot& forwarded_slot) {
4590 // Exchange the last two instructions in the assembler buffer.
4591 size_t size = buffer_.Size();
4592 CHECK_GE(size, 2 * sizeof(uint32_t));
4593 size_t pos1 = size - 2 * sizeof(uint32_t);
4594 size_t pos2 = size - sizeof(uint32_t);
4595 uint32_t instr1 = buffer_.Load<uint32_t>(pos1);
4596 uint32_t instr2 = buffer_.Load<uint32_t>(pos2);
4597 CHECK_EQ(instr1, forwarded_slot.instruction_);
4598 CHECK_EQ(instr2, delay_slot_.instruction_);
4599 buffer_.Store<uint32_t>(pos1, instr2);
4600 buffer_.Store<uint32_t>(pos2, instr1);
4601 // Set the current delay slot information to that of the last instruction
4602 // in the buffer.
4603 delay_slot_ = forwarded_slot;
4604}
4605
4606void MipsAssembler::GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt) {
4607 // If possible, exchange the slt[u] instruction with the preceding instruction,
4608 // so it can fill the delay slot.
4609 DelaySlot forwarded_slot = delay_slot_;
4610 bool exchange = CanExchangeWithSlt(rs, rt);
4611 if (exchange) {
4612 // The last instruction cannot be used in a different delay slot,
4613 // do not commit the label before it (if any).
4614 DsFsmDropLabel();
4615 }
4616 if (unsigned_slt) {
4617 Sltu(AT, rs, rt);
4618 } else {
4619 Slt(AT, rs, rt);
4620 }
4621 if (exchange) {
4622 ExchangeWithSlt(forwarded_slot);
4623 }
4624}
4625
Alexey Frunze0cab6562017-07-25 15:19:36 -07004626void MipsAssembler::Blt(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4627 if (IsR6() && !is_bare) {
4628 Bcond(label, IsR6(), is_bare, kCondLT, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004629 } else if (!Branch::IsNop(kCondLT, rs, rt)) {
4630 // Synthesize the instruction (not available on R2).
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004631 GenerateSltForCondBranch(/* unsigned_slt */ false, rs, rt);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004632 Bnez(AT, label, is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004633 }
4634}
4635
Alexey Frunze0cab6562017-07-25 15:19:36 -07004636void MipsAssembler::Bge(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4637 if (IsR6() && !is_bare) {
4638 Bcond(label, IsR6(), is_bare, kCondGE, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004639 } else if (Branch::IsUncond(kCondGE, rs, rt)) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07004640 B(label, is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004641 } else {
4642 // Synthesize the instruction (not available on R2).
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004643 GenerateSltForCondBranch(/* unsigned_slt */ false, rs, rt);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004644 Beqz(AT, label, is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004645 }
4646}
4647
Alexey Frunze0cab6562017-07-25 15:19:36 -07004648void MipsAssembler::Bltu(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4649 if (IsR6() && !is_bare) {
4650 Bcond(label, IsR6(), is_bare, kCondLTU, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004651 } else if (!Branch::IsNop(kCondLTU, rs, rt)) {
4652 // Synthesize the instruction (not available on R2).
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004653 GenerateSltForCondBranch(/* unsigned_slt */ true, rs, rt);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004654 Bnez(AT, label, is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004655 }
4656}
4657
Alexey Frunze0cab6562017-07-25 15:19:36 -07004658void MipsAssembler::Bgeu(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4659 if (IsR6() && !is_bare) {
4660 Bcond(label, IsR6(), is_bare, kCondGEU, rs, rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004661 } else if (Branch::IsUncond(kCondGEU, rs, rt)) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07004662 B(label, is_bare);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004663 } else {
4664 // Synthesize the instruction (not available on R2).
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004665 GenerateSltForCondBranch(/* unsigned_slt */ true, rs, rt);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004666 Beqz(AT, label, is_bare);
jeffhao7fbee072012-08-24 17:56:54 -07004667 }
4668}
4669
Alexey Frunze0cab6562017-07-25 15:19:36 -07004670void MipsAssembler::Bc1f(MipsLabel* label, bool is_bare) {
4671 Bc1f(0, label, is_bare);
Chris Larsenb74353a2015-11-20 09:07:09 -08004672}
4673
Alexey Frunze0cab6562017-07-25 15:19:36 -07004674void MipsAssembler::Bc1f(int cc, MipsLabel* label, bool is_bare) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004675 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004676 Bcond(label, /* is_r6 */ false, is_bare, kCondF, static_cast<Register>(cc), ZERO);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004677}
4678
Alexey Frunze0cab6562017-07-25 15:19:36 -07004679void MipsAssembler::Bc1t(MipsLabel* label, bool is_bare) {
4680 Bc1t(0, label, is_bare);
Chris Larsenb74353a2015-11-20 09:07:09 -08004681}
4682
Alexey Frunze0cab6562017-07-25 15:19:36 -07004683void MipsAssembler::Bc1t(int cc, MipsLabel* label, bool is_bare) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004684 CHECK(IsUint<3>(cc)) << cc;
Alexey Frunze0cab6562017-07-25 15:19:36 -07004685 Bcond(label, /* is_r6 */ false, is_bare, kCondT, static_cast<Register>(cc), ZERO);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004686}
4687
Alexey Frunze0cab6562017-07-25 15:19:36 -07004688void MipsAssembler::Bc(MipsLabel* label, bool is_bare) {
4689 Buncond(label, /* is_r6 */ true, is_bare);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004690}
4691
Alexey Frunze0cab6562017-07-25 15:19:36 -07004692void MipsAssembler::Balc(MipsLabel* label, bool is_bare) {
4693 Call(label, /* is_r6 */ true, is_bare);
4694}
4695
4696void MipsAssembler::Beqc(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4697 Bcond(label, /* is_r6 */ true, is_bare, kCondEQ, rs, rt);
4698}
4699
4700void MipsAssembler::Bnec(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4701 Bcond(label, /* is_r6 */ true, is_bare, kCondNE, rs, rt);
4702}
4703
4704void MipsAssembler::Beqzc(Register rt, MipsLabel* label, bool is_bare) {
4705 Bcond(label, /* is_r6 */ true, is_bare, kCondEQZ, rt);
4706}
4707
4708void MipsAssembler::Bnezc(Register rt, MipsLabel* label, bool is_bare) {
4709 Bcond(label, /* is_r6 */ true, is_bare, kCondNEZ, rt);
4710}
4711
4712void MipsAssembler::Bltzc(Register rt, MipsLabel* label, bool is_bare) {
4713 Bcond(label, /* is_r6 */ true, is_bare, kCondLTZ, rt);
4714}
4715
4716void MipsAssembler::Bgezc(Register rt, MipsLabel* label, bool is_bare) {
4717 Bcond(label, /* is_r6 */ true, is_bare, kCondGEZ, rt);
4718}
4719
4720void MipsAssembler::Blezc(Register rt, MipsLabel* label, bool is_bare) {
4721 Bcond(label, /* is_r6 */ true, is_bare, kCondLEZ, rt);
4722}
4723
4724void MipsAssembler::Bgtzc(Register rt, MipsLabel* label, bool is_bare) {
4725 Bcond(label, /* is_r6 */ true, is_bare, kCondGTZ, rt);
4726}
4727
4728void MipsAssembler::Bltc(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4729 Bcond(label, /* is_r6 */ true, is_bare, kCondLT, rs, rt);
4730}
4731
4732void MipsAssembler::Bgec(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4733 Bcond(label, /* is_r6 */ true, is_bare, kCondGE, rs, rt);
4734}
4735
4736void MipsAssembler::Bltuc(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4737 Bcond(label, /* is_r6 */ true, is_bare, kCondLTU, rs, rt);
4738}
4739
4740void MipsAssembler::Bgeuc(Register rs, Register rt, MipsLabel* label, bool is_bare) {
4741 Bcond(label, /* is_r6 */ true, is_bare, kCondGEU, rs, rt);
4742}
4743
4744void MipsAssembler::Bc1eqz(FRegister ft, MipsLabel* label, bool is_bare) {
4745 Bcond(label, /* is_r6 */ true, is_bare, kCondF, static_cast<Register>(ft), ZERO);
4746}
4747
4748void MipsAssembler::Bc1nez(FRegister ft, MipsLabel* label, bool is_bare) {
4749 Bcond(label, /* is_r6 */ true, is_bare, kCondT, static_cast<Register>(ft), ZERO);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004750}
4751
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07004752void MipsAssembler::AdjustBaseAndOffset(Register& base,
4753 int32_t& offset,
4754 bool is_doubleword,
4755 bool is_float) {
4756 // This method is used to adjust the base register and offset pair
4757 // for a load/store when the offset doesn't fit into int16_t.
4758 // It is assumed that `base + offset` is sufficiently aligned for memory
4759 // operands that are machine word in size or smaller. For doubleword-sized
4760 // operands it's assumed that `base` is a multiple of 8, while `offset`
4761 // may be a multiple of 4 (e.g. 4-byte-aligned long and double arguments
4762 // and spilled variables on the stack accessed relative to the stack
4763 // pointer register).
4764 // We preserve the "alignment" of `offset` by adjusting it by a multiple of 8.
4765 CHECK_NE(base, AT); // Must not overwrite the register `base` while loading `offset`.
4766
4767 bool doubleword_aligned = IsAligned<kMipsDoublewordSize>(offset);
4768 bool two_accesses = is_doubleword && (!is_float || !doubleword_aligned);
4769
4770 // IsInt<16> must be passed a signed value, hence the static cast below.
4771 if (IsInt<16>(offset) &&
4772 (!two_accesses || IsInt<16>(static_cast<int32_t>(offset + kMipsWordSize)))) {
4773 // Nothing to do: `offset` (and, if needed, `offset + 4`) fits into int16_t.
4774 return;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004775 }
4776
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07004777 // Remember the "(mis)alignment" of `offset`, it will be checked at the end.
4778 uint32_t misalignment = offset & (kMipsDoublewordSize - 1);
4779
4780 // Do not load the whole 32-bit `offset` if it can be represented as
4781 // a sum of two 16-bit signed offsets. This can save an instruction or two.
4782 // To simplify matters, only do this for a symmetric range of offsets from
4783 // about -64KB to about +64KB, allowing further addition of 4 when accessing
4784 // 64-bit variables with two 32-bit accesses.
4785 constexpr int32_t kMinOffsetForSimpleAdjustment = 0x7ff8; // Max int16_t that's a multiple of 8.
4786 constexpr int32_t kMaxOffsetForSimpleAdjustment = 2 * kMinOffsetForSimpleAdjustment;
4787 if (0 <= offset && offset <= kMaxOffsetForSimpleAdjustment) {
4788 Addiu(AT, base, kMinOffsetForSimpleAdjustment);
4789 offset -= kMinOffsetForSimpleAdjustment;
4790 } else if (-kMaxOffsetForSimpleAdjustment <= offset && offset < 0) {
4791 Addiu(AT, base, -kMinOffsetForSimpleAdjustment);
4792 offset += kMinOffsetForSimpleAdjustment;
4793 } else if (IsR6()) {
4794 // On R6 take advantage of the aui instruction, e.g.:
4795 // aui AT, base, offset_high
4796 // lw reg_lo, offset_low(AT)
4797 // lw reg_hi, (offset_low+4)(AT)
4798 // or when offset_low+4 overflows int16_t:
4799 // aui AT, base, offset_high
4800 // addiu AT, AT, 8
4801 // lw reg_lo, (offset_low-8)(AT)
4802 // lw reg_hi, (offset_low-4)(AT)
4803 int16_t offset_high = High16Bits(offset);
4804 int16_t offset_low = Low16Bits(offset);
4805 offset_high += (offset_low < 0) ? 1 : 0; // Account for offset sign extension in load/store.
4806 Aui(AT, base, offset_high);
4807 if (two_accesses && !IsInt<16>(static_cast<int32_t>(offset_low + kMipsWordSize))) {
4808 // Avoid overflow in the 16-bit offset of the load/store instruction when adding 4.
4809 Addiu(AT, AT, kMipsDoublewordSize);
4810 offset_low -= kMipsDoublewordSize;
4811 }
4812 offset = offset_low;
4813 } else {
4814 // Do not load the whole 32-bit `offset` if it can be represented as
4815 // a sum of three 16-bit signed offsets. This can save an instruction.
4816 // To simplify matters, only do this for a symmetric range of offsets from
4817 // about -96KB to about +96KB, allowing further addition of 4 when accessing
4818 // 64-bit variables with two 32-bit accesses.
4819 constexpr int32_t kMinOffsetForMediumAdjustment = 2 * kMinOffsetForSimpleAdjustment;
4820 constexpr int32_t kMaxOffsetForMediumAdjustment = 3 * kMinOffsetForSimpleAdjustment;
4821 if (0 <= offset && offset <= kMaxOffsetForMediumAdjustment) {
4822 Addiu(AT, base, kMinOffsetForMediumAdjustment / 2);
4823 Addiu(AT, AT, kMinOffsetForMediumAdjustment / 2);
4824 offset -= kMinOffsetForMediumAdjustment;
4825 } else if (-kMaxOffsetForMediumAdjustment <= offset && offset < 0) {
4826 Addiu(AT, base, -kMinOffsetForMediumAdjustment / 2);
4827 Addiu(AT, AT, -kMinOffsetForMediumAdjustment / 2);
4828 offset += kMinOffsetForMediumAdjustment;
4829 } else {
4830 // Now that all shorter options have been exhausted, load the full 32-bit offset.
4831 int32_t loaded_offset = RoundDown(offset, kMipsDoublewordSize);
4832 LoadConst32(AT, loaded_offset);
4833 Addu(AT, AT, base);
4834 offset -= loaded_offset;
4835 }
4836 }
4837 base = AT;
4838
4839 CHECK(IsInt<16>(offset));
4840 if (two_accesses) {
4841 CHECK(IsInt<16>(static_cast<int32_t>(offset + kMipsWordSize)));
4842 }
4843 CHECK_EQ(misalignment, offset & (kMipsDoublewordSize - 1));
4844}
4845
Lena Djokic2e0a7e52017-07-06 11:55:24 +02004846void MipsAssembler::AdjustBaseOffsetAndElementSizeShift(Register& base,
4847 int32_t& offset,
4848 int& element_size_shift) {
4849 // This method is used to adjust the base register, offset and element_size_shift
4850 // for a vector load/store when the offset doesn't fit into allowed number of bits.
4851 // MSA ld.df and st.df instructions take signed offsets as arguments, but maximum
4852 // offset is dependant on the size of the data format df (10-bit offsets for ld.b,
4853 // 11-bit for ld.h, 12-bit for ld.w and 13-bit for ld.d).
4854 // If element_size_shift is non-negative at entry, it won't be changed, but offset
4855 // will be checked for appropriate alignment. If negative at entry, it will be
4856 // adjusted based on offset for maximum fit.
4857 // It's assumed that `base` is a multiple of 8.
4858 CHECK_NE(base, AT); // Must not overwrite the register `base` while loading `offset`.
4859
4860 if (element_size_shift >= 0) {
4861 CHECK_LE(element_size_shift, TIMES_8);
4862 CHECK_GE(JAVASTYLE_CTZ(offset), element_size_shift);
4863 } else if (IsAligned<kMipsDoublewordSize>(offset)) {
4864 element_size_shift = TIMES_8;
4865 } else if (IsAligned<kMipsWordSize>(offset)) {
4866 element_size_shift = TIMES_4;
4867 } else if (IsAligned<kMipsHalfwordSize>(offset)) {
4868 element_size_shift = TIMES_2;
4869 } else {
4870 element_size_shift = TIMES_1;
4871 }
4872
4873 const int low_len = 10 + element_size_shift; // How many low bits of `offset` ld.df/st.df
4874 // will take.
4875 int16_t low = offset & ((1 << low_len) - 1); // Isolate these bits.
4876 low -= (low & (1 << (low_len - 1))) << 1; // Sign-extend these bits.
4877 if (low == offset) {
4878 return; // `offset` fits into ld.df/st.df.
4879 }
4880
4881 // First, see if `offset` can be represented as a sum of two or three signed offsets.
4882 // This can save an instruction or two.
4883
4884 // Max int16_t that's a multiple of element size.
4885 const int32_t kMaxDeltaForSimpleAdjustment = 0x8000 - (1 << element_size_shift);
4886 // Max ld.df/st.df offset that's a multiple of element size.
4887 const int32_t kMaxLoadStoreOffset = 0x1ff << element_size_shift;
4888 const int32_t kMaxOffsetForSimpleAdjustment = kMaxDeltaForSimpleAdjustment + kMaxLoadStoreOffset;
4889 const int32_t kMinOffsetForMediumAdjustment = 2 * kMaxDeltaForSimpleAdjustment;
4890 const int32_t kMaxOffsetForMediumAdjustment = kMinOffsetForMediumAdjustment + kMaxLoadStoreOffset;
4891
4892 if (IsInt<16>(offset)) {
4893 Addiu(AT, base, offset);
4894 offset = 0;
4895 } else if (0 <= offset && offset <= kMaxOffsetForSimpleAdjustment) {
4896 Addiu(AT, base, kMaxDeltaForSimpleAdjustment);
4897 offset -= kMaxDeltaForSimpleAdjustment;
4898 } else if (-kMaxOffsetForSimpleAdjustment <= offset && offset < 0) {
4899 Addiu(AT, base, -kMaxDeltaForSimpleAdjustment);
4900 offset += kMaxDeltaForSimpleAdjustment;
4901 } else if (!IsR6() && 0 <= offset && offset <= kMaxOffsetForMediumAdjustment) {
4902 Addiu(AT, base, kMaxDeltaForSimpleAdjustment);
4903 if (offset <= kMinOffsetForMediumAdjustment) {
4904 Addiu(AT, AT, offset - kMaxDeltaForSimpleAdjustment);
4905 offset = 0;
4906 } else {
4907 Addiu(AT, AT, kMaxDeltaForSimpleAdjustment);
4908 offset -= kMinOffsetForMediumAdjustment;
4909 }
4910 } else if (!IsR6() && -kMaxOffsetForMediumAdjustment <= offset && offset < 0) {
4911 Addiu(AT, base, -kMaxDeltaForSimpleAdjustment);
4912 if (-kMinOffsetForMediumAdjustment <= offset) {
4913 Addiu(AT, AT, offset + kMaxDeltaForSimpleAdjustment);
4914 offset = 0;
4915 } else {
4916 Addiu(AT, AT, -kMaxDeltaForSimpleAdjustment);
4917 offset += kMinOffsetForMediumAdjustment;
4918 }
4919 } else {
4920 // 16-bit or smaller parts of `offset`:
4921 // |31 hi 16|15 mid 13-10|12-9 low 0|
4922 //
4923 // Instructions that supply each part as a signed integer addend:
4924 // |aui |addiu |ld.df/st.df |
4925 uint32_t tmp = static_cast<uint32_t>(offset) - low; // Exclude `low` from the rest of `offset`
4926 // (accounts for sign of `low`).
4927 tmp += (tmp & (UINT32_C(1) << 15)) << 1; // Account for sign extension in addiu.
4928 int16_t mid = Low16Bits(tmp);
4929 int16_t hi = High16Bits(tmp);
4930 if (IsR6()) {
4931 Aui(AT, base, hi);
4932 } else {
4933 Lui(AT, hi);
4934 Addu(AT, AT, base);
4935 }
4936 if (mid != 0) {
4937 Addiu(AT, AT, mid);
4938 }
4939 offset = low;
4940 }
4941 base = AT;
4942 CHECK_GE(JAVASTYLE_CTZ(offset), element_size_shift);
4943 CHECK(IsInt<10>(offset >> element_size_shift));
4944}
4945
Alexey Frunze2923db72016-08-20 01:55:47 -07004946void MipsAssembler::LoadFromOffset(LoadOperandType type,
4947 Register reg,
4948 Register base,
Alexey Frunzecad3a4c2016-06-07 23:40:37 -07004949 int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004950 LoadFromOffset<>(type, reg, base, offset);
jeffhao7fbee072012-08-24 17:56:54 -07004951}
4952
4953void MipsAssembler::LoadSFromOffset(FRegister reg, Register base, int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004954 LoadSFromOffset<>(reg, base, offset);
jeffhao7fbee072012-08-24 17:56:54 -07004955}
4956
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004957void MipsAssembler::LoadDFromOffset(FRegister reg, Register base, int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004958 LoadDFromOffset<>(reg, base, offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004959}
4960
Lena Djokic2e0a7e52017-07-06 11:55:24 +02004961void MipsAssembler::LoadQFromOffset(FRegister reg, Register base, int32_t offset) {
4962 LoadQFromOffset<>(reg, base, offset);
4963}
4964
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004965void MipsAssembler::EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset,
4966 size_t size) {
4967 MipsManagedRegister dst = m_dst.AsMips();
4968 if (dst.IsNoRegister()) {
4969 CHECK_EQ(0u, size) << dst;
4970 } else if (dst.IsCoreRegister()) {
4971 CHECK_EQ(kMipsWordSize, size) << dst;
4972 LoadFromOffset(kLoadWord, dst.AsCoreRegister(), src_register, src_offset);
4973 } else if (dst.IsRegisterPair()) {
4974 CHECK_EQ(kMipsDoublewordSize, size) << dst;
4975 LoadFromOffset(kLoadDoubleword, dst.AsRegisterPairLow(), src_register, src_offset);
4976 } else if (dst.IsFRegister()) {
4977 if (size == kMipsWordSize) {
4978 LoadSFromOffset(dst.AsFRegister(), src_register, src_offset);
4979 } else {
4980 CHECK_EQ(kMipsDoublewordSize, size) << dst;
4981 LoadDFromOffset(dst.AsFRegister(), src_register, src_offset);
4982 }
Alexey Frunze1b8464d2016-11-12 17:22:05 -08004983 } else if (dst.IsDRegister()) {
4984 CHECK_EQ(kMipsDoublewordSize, size) << dst;
4985 LoadDFromOffset(dst.AsOverlappingDRegisterLow(), src_register, src_offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02004986 }
jeffhao7fbee072012-08-24 17:56:54 -07004987}
4988
Alexey Frunze2923db72016-08-20 01:55:47 -07004989void MipsAssembler::StoreToOffset(StoreOperandType type,
4990 Register reg,
4991 Register base,
jeffhao7fbee072012-08-24 17:56:54 -07004992 int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004993 StoreToOffset<>(type, reg, base, offset);
jeffhao7fbee072012-08-24 17:56:54 -07004994}
4995
Goran Jakovljevicff734982015-08-24 12:58:55 +00004996void MipsAssembler::StoreSToOffset(FRegister reg, Register base, int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004997 StoreSToOffset<>(reg, base, offset);
jeffhao7fbee072012-08-24 17:56:54 -07004998}
4999
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005000void MipsAssembler::StoreDToOffset(FRegister reg, Register base, int32_t offset) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005001 StoreDToOffset<>(reg, base, offset);
jeffhao7fbee072012-08-24 17:56:54 -07005002}
5003
Lena Djokic2e0a7e52017-07-06 11:55:24 +02005004void MipsAssembler::StoreQToOffset(FRegister reg, Register base, int32_t offset) {
5005 StoreQToOffset<>(reg, base, offset);
5006}
5007
David Srbeckydd973932015-04-07 20:29:48 +01005008static dwarf::Reg DWARFReg(Register reg) {
5009 return dwarf::Reg::MipsCore(static_cast<int>(reg));
5010}
5011
Ian Rogers790a6b72014-04-01 10:36:00 -07005012constexpr size_t kFramePointerSize = 4;
5013
Vladimir Marko32248382016-05-19 10:37:24 +01005014void MipsAssembler::BuildFrame(size_t frame_size,
5015 ManagedRegister method_reg,
5016 ArrayRef<const ManagedRegister> callee_save_regs,
Dmitry Petrochenkofca82202014-03-21 11:21:37 +07005017 const ManagedRegisterEntrySpills& entry_spills) {
jeffhao7fbee072012-08-24 17:56:54 -07005018 CHECK_ALIGNED(frame_size, kStackAlignment);
Vladimir Marko10ef6942015-10-22 15:25:54 +01005019 DCHECK(!overwriting_);
jeffhao7fbee072012-08-24 17:56:54 -07005020
5021 // Increase frame to required size.
5022 IncreaseFrameSize(frame_size);
5023
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005024 // Push callee saves and return address.
Ian Rogers790a6b72014-04-01 10:36:00 -07005025 int stack_offset = frame_size - kFramePointerSize;
jeffhao7fbee072012-08-24 17:56:54 -07005026 StoreToOffset(kStoreWord, RA, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01005027 cfi_.RelOffset(DWARFReg(RA), stack_offset);
jeffhao7fbee072012-08-24 17:56:54 -07005028 for (int i = callee_save_regs.size() - 1; i >= 0; --i) {
Ian Rogers790a6b72014-04-01 10:36:00 -07005029 stack_offset -= kFramePointerSize;
Vladimir Marko32248382016-05-19 10:37:24 +01005030 Register reg = callee_save_regs[i].AsMips().AsCoreRegister();
jeffhao7fbee072012-08-24 17:56:54 -07005031 StoreToOffset(kStoreWord, reg, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01005032 cfi_.RelOffset(DWARFReg(reg), stack_offset);
jeffhao7fbee072012-08-24 17:56:54 -07005033 }
5034
5035 // Write out Method*.
5036 StoreToOffset(kStoreWord, method_reg.AsMips().AsCoreRegister(), SP, 0);
5037
5038 // Write out entry spills.
Goran Jakovljevicff734982015-08-24 12:58:55 +00005039 int32_t offset = frame_size + kFramePointerSize;
jeffhao7fbee072012-08-24 17:56:54 -07005040 for (size_t i = 0; i < entry_spills.size(); ++i) {
Goran Jakovljevicff734982015-08-24 12:58:55 +00005041 MipsManagedRegister reg = entry_spills.at(i).AsMips();
5042 if (reg.IsNoRegister()) {
5043 ManagedRegisterSpill spill = entry_spills.at(i);
5044 offset += spill.getSize();
5045 } else if (reg.IsCoreRegister()) {
5046 StoreToOffset(kStoreWord, reg.AsCoreRegister(), SP, offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005047 offset += kMipsWordSize;
Goran Jakovljevicff734982015-08-24 12:58:55 +00005048 } else if (reg.IsFRegister()) {
5049 StoreSToOffset(reg.AsFRegister(), SP, offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005050 offset += kMipsWordSize;
Goran Jakovljevicff734982015-08-24 12:58:55 +00005051 } else if (reg.IsDRegister()) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005052 StoreDToOffset(reg.AsOverlappingDRegisterLow(), SP, offset);
5053 offset += kMipsDoublewordSize;
Goran Jakovljevicff734982015-08-24 12:58:55 +00005054 }
jeffhao7fbee072012-08-24 17:56:54 -07005055 }
5056}
5057
5058void MipsAssembler::RemoveFrame(size_t frame_size,
Roland Levillain0d127e12017-07-05 17:01:11 +01005059 ArrayRef<const ManagedRegister> callee_save_regs,
5060 bool may_suspend ATTRIBUTE_UNUSED) {
jeffhao7fbee072012-08-24 17:56:54 -07005061 CHECK_ALIGNED(frame_size, kStackAlignment);
Vladimir Marko10ef6942015-10-22 15:25:54 +01005062 DCHECK(!overwriting_);
David Srbeckydd973932015-04-07 20:29:48 +01005063 cfi_.RememberState();
jeffhao7fbee072012-08-24 17:56:54 -07005064
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005065 // Pop callee saves and return address.
Ian Rogers790a6b72014-04-01 10:36:00 -07005066 int stack_offset = frame_size - (callee_save_regs.size() * kFramePointerSize) - kFramePointerSize;
jeffhao7fbee072012-08-24 17:56:54 -07005067 for (size_t i = 0; i < callee_save_regs.size(); ++i) {
Vladimir Marko32248382016-05-19 10:37:24 +01005068 Register reg = callee_save_regs[i].AsMips().AsCoreRegister();
jeffhao7fbee072012-08-24 17:56:54 -07005069 LoadFromOffset(kLoadWord, reg, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01005070 cfi_.Restore(DWARFReg(reg));
Ian Rogers790a6b72014-04-01 10:36:00 -07005071 stack_offset += kFramePointerSize;
jeffhao7fbee072012-08-24 17:56:54 -07005072 }
5073 LoadFromOffset(kLoadWord, RA, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01005074 cfi_.Restore(DWARFReg(RA));
jeffhao7fbee072012-08-24 17:56:54 -07005075
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005076 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
5077 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
5078 bool reordering = SetReorder(false);
5079 if (exchange) {
5080 // Jump to the return address.
5081 Jr(RA);
5082 // Decrease frame to required size.
5083 DecreaseFrameSize(frame_size); // Single instruction in delay slot.
5084 } else {
5085 // Decrease frame to required size.
5086 DecreaseFrameSize(frame_size);
5087 // Jump to the return address.
5088 Jr(RA);
5089 Nop(); // In delay slot.
5090 }
5091 SetReorder(reordering);
David Srbeckydd973932015-04-07 20:29:48 +01005092
5093 // The CFI should be restored for any code that follows the exit block.
5094 cfi_.RestoreState();
5095 cfi_.DefCFAOffset(frame_size);
jeffhao7fbee072012-08-24 17:56:54 -07005096}
5097
5098void MipsAssembler::IncreaseFrameSize(size_t adjust) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005099 CHECK_ALIGNED(adjust, kFramePointerSize);
5100 Addiu32(SP, SP, -adjust);
David Srbeckydd973932015-04-07 20:29:48 +01005101 cfi_.AdjustCFAOffset(adjust);
Vladimir Marko10ef6942015-10-22 15:25:54 +01005102 if (overwriting_) {
5103 cfi_.OverrideDelayedPC(overwrite_location_);
5104 }
jeffhao7fbee072012-08-24 17:56:54 -07005105}
5106
5107void MipsAssembler::DecreaseFrameSize(size_t adjust) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005108 CHECK_ALIGNED(adjust, kFramePointerSize);
5109 Addiu32(SP, SP, adjust);
David Srbeckydd973932015-04-07 20:29:48 +01005110 cfi_.AdjustCFAOffset(-adjust);
Vladimir Marko10ef6942015-10-22 15:25:54 +01005111 if (overwriting_) {
5112 cfi_.OverrideDelayedPC(overwrite_location_);
5113 }
jeffhao7fbee072012-08-24 17:56:54 -07005114}
5115
5116void MipsAssembler::Store(FrameOffset dest, ManagedRegister msrc, size_t size) {
5117 MipsManagedRegister src = msrc.AsMips();
5118 if (src.IsNoRegister()) {
5119 CHECK_EQ(0u, size);
5120 } else if (src.IsCoreRegister()) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005121 CHECK_EQ(kMipsWordSize, size);
jeffhao7fbee072012-08-24 17:56:54 -07005122 StoreToOffset(kStoreWord, src.AsCoreRegister(), SP, dest.Int32Value());
5123 } else if (src.IsRegisterPair()) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005124 CHECK_EQ(kMipsDoublewordSize, size);
jeffhao7fbee072012-08-24 17:56:54 -07005125 StoreToOffset(kStoreWord, src.AsRegisterPairLow(), SP, dest.Int32Value());
5126 StoreToOffset(kStoreWord, src.AsRegisterPairHigh(),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005127 SP, dest.Int32Value() + kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005128 } else if (src.IsFRegister()) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005129 if (size == kMipsWordSize) {
5130 StoreSToOffset(src.AsFRegister(), SP, dest.Int32Value());
5131 } else {
5132 CHECK_EQ(kMipsDoublewordSize, size);
5133 StoreDToOffset(src.AsFRegister(), SP, dest.Int32Value());
5134 }
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005135 } else if (src.IsDRegister()) {
5136 CHECK_EQ(kMipsDoublewordSize, size);
5137 StoreDToOffset(src.AsOverlappingDRegisterLow(), SP, dest.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005138 }
5139}
5140
5141void MipsAssembler::StoreRef(FrameOffset dest, ManagedRegister msrc) {
5142 MipsManagedRegister src = msrc.AsMips();
5143 CHECK(src.IsCoreRegister());
5144 StoreToOffset(kStoreWord, src.AsCoreRegister(), SP, dest.Int32Value());
5145}
5146
5147void MipsAssembler::StoreRawPtr(FrameOffset dest, ManagedRegister msrc) {
5148 MipsManagedRegister src = msrc.AsMips();
5149 CHECK(src.IsCoreRegister());
5150 StoreToOffset(kStoreWord, src.AsCoreRegister(), SP, dest.Int32Value());
5151}
5152
5153void MipsAssembler::StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
5154 ManagedRegister mscratch) {
5155 MipsManagedRegister scratch = mscratch.AsMips();
5156 CHECK(scratch.IsCoreRegister()) << scratch;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005157 LoadConst32(scratch.AsCoreRegister(), imm);
jeffhao7fbee072012-08-24 17:56:54 -07005158 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value());
5159}
5160
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005161void MipsAssembler::StoreStackOffsetToThread(ThreadOffset32 thr_offs,
5162 FrameOffset fr_offs,
jeffhao7fbee072012-08-24 17:56:54 -07005163 ManagedRegister mscratch) {
5164 MipsManagedRegister scratch = mscratch.AsMips();
5165 CHECK(scratch.IsCoreRegister()) << scratch;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005166 Addiu32(scratch.AsCoreRegister(), SP, fr_offs.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005167 StoreToOffset(kStoreWord, scratch.AsCoreRegister(),
5168 S1, thr_offs.Int32Value());
5169}
5170
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005171void MipsAssembler::StoreStackPointerToThread(ThreadOffset32 thr_offs) {
jeffhao7fbee072012-08-24 17:56:54 -07005172 StoreToOffset(kStoreWord, SP, S1, thr_offs.Int32Value());
5173}
5174
5175void MipsAssembler::StoreSpanning(FrameOffset dest, ManagedRegister msrc,
5176 FrameOffset in_off, ManagedRegister mscratch) {
5177 MipsManagedRegister src = msrc.AsMips();
5178 MipsManagedRegister scratch = mscratch.AsMips();
5179 StoreToOffset(kStoreWord, src.AsCoreRegister(), SP, dest.Int32Value());
5180 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, in_off.Int32Value());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005181 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value() + kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005182}
5183
5184void MipsAssembler::Load(ManagedRegister mdest, FrameOffset src, size_t size) {
5185 return EmitLoad(mdest, SP, src.Int32Value(), size);
5186}
5187
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005188void MipsAssembler::LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) {
jeffhao7fbee072012-08-24 17:56:54 -07005189 return EmitLoad(mdest, S1, src.Int32Value(), size);
5190}
5191
5192void MipsAssembler::LoadRef(ManagedRegister mdest, FrameOffset src) {
5193 MipsManagedRegister dest = mdest.AsMips();
5194 CHECK(dest.IsCoreRegister());
5195 LoadFromOffset(kLoadWord, dest.AsCoreRegister(), SP, src.Int32Value());
5196}
5197
Mathieu Chartiere401d142015-04-22 13:56:20 -07005198void MipsAssembler::LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +01005199 bool unpoison_reference) {
jeffhao7fbee072012-08-24 17:56:54 -07005200 MipsManagedRegister dest = mdest.AsMips();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005201 CHECK(dest.IsCoreRegister() && base.AsMips().IsCoreRegister());
jeffhao7fbee072012-08-24 17:56:54 -07005202 LoadFromOffset(kLoadWord, dest.AsCoreRegister(),
5203 base.AsMips().AsCoreRegister(), offs.Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08005204 if (unpoison_reference) {
5205 MaybeUnpoisonHeapReference(dest.AsCoreRegister());
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -08005206 }
jeffhao7fbee072012-08-24 17:56:54 -07005207}
5208
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005209void MipsAssembler::LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) {
jeffhao7fbee072012-08-24 17:56:54 -07005210 MipsManagedRegister dest = mdest.AsMips();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005211 CHECK(dest.IsCoreRegister() && base.AsMips().IsCoreRegister());
jeffhao7fbee072012-08-24 17:56:54 -07005212 LoadFromOffset(kLoadWord, dest.AsCoreRegister(),
5213 base.AsMips().AsCoreRegister(), offs.Int32Value());
5214}
5215
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005216void MipsAssembler::LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) {
jeffhao7fbee072012-08-24 17:56:54 -07005217 MipsManagedRegister dest = mdest.AsMips();
5218 CHECK(dest.IsCoreRegister());
5219 LoadFromOffset(kLoadWord, dest.AsCoreRegister(), S1, offs.Int32Value());
5220}
5221
5222void MipsAssembler::SignExtend(ManagedRegister /*mreg*/, size_t /*size*/) {
5223 UNIMPLEMENTED(FATAL) << "no sign extension necessary for mips";
5224}
5225
5226void MipsAssembler::ZeroExtend(ManagedRegister /*mreg*/, size_t /*size*/) {
5227 UNIMPLEMENTED(FATAL) << "no zero extension necessary for mips";
5228}
5229
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005230void MipsAssembler::Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) {
jeffhao7fbee072012-08-24 17:56:54 -07005231 MipsManagedRegister dest = mdest.AsMips();
5232 MipsManagedRegister src = msrc.AsMips();
5233 if (!dest.Equals(src)) {
5234 if (dest.IsCoreRegister()) {
5235 CHECK(src.IsCoreRegister()) << src;
5236 Move(dest.AsCoreRegister(), src.AsCoreRegister());
5237 } else if (dest.IsFRegister()) {
5238 CHECK(src.IsFRegister()) << src;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005239 if (size == kMipsWordSize) {
5240 MovS(dest.AsFRegister(), src.AsFRegister());
5241 } else {
5242 CHECK_EQ(kMipsDoublewordSize, size);
5243 MovD(dest.AsFRegister(), src.AsFRegister());
5244 }
jeffhao7fbee072012-08-24 17:56:54 -07005245 } else if (dest.IsDRegister()) {
5246 CHECK(src.IsDRegister()) << src;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005247 MovD(dest.AsOverlappingDRegisterLow(), src.AsOverlappingDRegisterLow());
jeffhao7fbee072012-08-24 17:56:54 -07005248 } else {
5249 CHECK(dest.IsRegisterPair()) << dest;
5250 CHECK(src.IsRegisterPair()) << src;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005251 // Ensure that the first move doesn't clobber the input of the second.
jeffhao7fbee072012-08-24 17:56:54 -07005252 if (src.AsRegisterPairHigh() != dest.AsRegisterPairLow()) {
5253 Move(dest.AsRegisterPairLow(), src.AsRegisterPairLow());
5254 Move(dest.AsRegisterPairHigh(), src.AsRegisterPairHigh());
5255 } else {
5256 Move(dest.AsRegisterPairHigh(), src.AsRegisterPairHigh());
5257 Move(dest.AsRegisterPairLow(), src.AsRegisterPairLow());
5258 }
5259 }
5260 }
5261}
5262
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005263void MipsAssembler::CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) {
jeffhao7fbee072012-08-24 17:56:54 -07005264 MipsManagedRegister scratch = mscratch.AsMips();
5265 CHECK(scratch.IsCoreRegister()) << scratch;
5266 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, src.Int32Value());
5267 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value());
5268}
5269
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005270void MipsAssembler::CopyRawPtrFromThread(FrameOffset fr_offs,
5271 ThreadOffset32 thr_offs,
5272 ManagedRegister mscratch) {
jeffhao7fbee072012-08-24 17:56:54 -07005273 MipsManagedRegister scratch = mscratch.AsMips();
5274 CHECK(scratch.IsCoreRegister()) << scratch;
5275 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(),
5276 S1, thr_offs.Int32Value());
5277 StoreToOffset(kStoreWord, scratch.AsCoreRegister(),
5278 SP, fr_offs.Int32Value());
5279}
5280
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005281void MipsAssembler::CopyRawPtrToThread(ThreadOffset32 thr_offs,
5282 FrameOffset fr_offs,
5283 ManagedRegister mscratch) {
jeffhao7fbee072012-08-24 17:56:54 -07005284 MipsManagedRegister scratch = mscratch.AsMips();
5285 CHECK(scratch.IsCoreRegister()) << scratch;
5286 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(),
5287 SP, fr_offs.Int32Value());
5288 StoreToOffset(kStoreWord, scratch.AsCoreRegister(),
5289 S1, thr_offs.Int32Value());
5290}
5291
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005292void MipsAssembler::Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) {
jeffhao7fbee072012-08-24 17:56:54 -07005293 MipsManagedRegister scratch = mscratch.AsMips();
5294 CHECK(scratch.IsCoreRegister()) << scratch;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005295 CHECK(size == kMipsWordSize || size == kMipsDoublewordSize) << size;
5296 if (size == kMipsWordSize) {
jeffhao7fbee072012-08-24 17:56:54 -07005297 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, src.Int32Value());
5298 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005299 } else if (size == kMipsDoublewordSize) {
jeffhao7fbee072012-08-24 17:56:54 -07005300 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, src.Int32Value());
5301 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005302 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, src.Int32Value() + kMipsWordSize);
5303 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, dest.Int32Value() + kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005304 }
5305}
5306
5307void MipsAssembler::Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
5308 ManagedRegister mscratch, size_t size) {
5309 Register scratch = mscratch.AsMips().AsCoreRegister();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005310 CHECK_EQ(size, kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005311 LoadFromOffset(kLoadWord, scratch, src_base.AsMips().AsCoreRegister(), src_offset.Int32Value());
5312 StoreToOffset(kStoreWord, scratch, SP, dest.Int32Value());
5313}
5314
5315void MipsAssembler::Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
5316 ManagedRegister mscratch, size_t size) {
5317 Register scratch = mscratch.AsMips().AsCoreRegister();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005318 CHECK_EQ(size, kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005319 LoadFromOffset(kLoadWord, scratch, SP, src.Int32Value());
5320 StoreToOffset(kStoreWord, scratch, dest_base.AsMips().AsCoreRegister(), dest_offset.Int32Value());
5321}
5322
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005323void MipsAssembler::Copy(FrameOffset dest ATTRIBUTE_UNUSED,
5324 FrameOffset src_base ATTRIBUTE_UNUSED,
5325 Offset src_offset ATTRIBUTE_UNUSED,
5326 ManagedRegister mscratch ATTRIBUTE_UNUSED,
5327 size_t size ATTRIBUTE_UNUSED) {
5328 UNIMPLEMENTED(FATAL) << "no MIPS implementation";
jeffhao7fbee072012-08-24 17:56:54 -07005329}
5330
5331void MipsAssembler::Copy(ManagedRegister dest, Offset dest_offset,
5332 ManagedRegister src, Offset src_offset,
5333 ManagedRegister mscratch, size_t size) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005334 CHECK_EQ(size, kMipsWordSize);
jeffhao7fbee072012-08-24 17:56:54 -07005335 Register scratch = mscratch.AsMips().AsCoreRegister();
5336 LoadFromOffset(kLoadWord, scratch, src.AsMips().AsCoreRegister(), src_offset.Int32Value());
5337 StoreToOffset(kStoreWord, scratch, dest.AsMips().AsCoreRegister(), dest_offset.Int32Value());
5338}
5339
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005340void MipsAssembler::Copy(FrameOffset dest ATTRIBUTE_UNUSED,
5341 Offset dest_offset ATTRIBUTE_UNUSED,
5342 FrameOffset src ATTRIBUTE_UNUSED,
5343 Offset src_offset ATTRIBUTE_UNUSED,
5344 ManagedRegister mscratch ATTRIBUTE_UNUSED,
5345 size_t size ATTRIBUTE_UNUSED) {
5346 UNIMPLEMENTED(FATAL) << "no MIPS implementation";
jeffhao7fbee072012-08-24 17:56:54 -07005347}
5348
5349void MipsAssembler::MemoryBarrier(ManagedRegister) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005350 // TODO: sync?
5351 UNIMPLEMENTED(FATAL) << "no MIPS implementation";
jeffhao7fbee072012-08-24 17:56:54 -07005352}
5353
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005354void MipsAssembler::CreateHandleScopeEntry(ManagedRegister mout_reg,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005355 FrameOffset handle_scope_offset,
5356 ManagedRegister min_reg,
5357 bool null_allowed) {
jeffhao7fbee072012-08-24 17:56:54 -07005358 MipsManagedRegister out_reg = mout_reg.AsMips();
5359 MipsManagedRegister in_reg = min_reg.AsMips();
5360 CHECK(in_reg.IsNoRegister() || in_reg.IsCoreRegister()) << in_reg;
5361 CHECK(out_reg.IsCoreRegister()) << out_reg;
5362 if (null_allowed) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005363 MipsLabel null_arg;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005364 // Null values get a handle scope entry value of 0. Otherwise, the handle scope entry is
5365 // the address in the handle scope holding the reference.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005366 // E.g. out_reg = (handle == 0) ? 0 : (SP+handle_offset).
jeffhao7fbee072012-08-24 17:56:54 -07005367 if (in_reg.IsNoRegister()) {
5368 LoadFromOffset(kLoadWord, out_reg.AsCoreRegister(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005369 SP, handle_scope_offset.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005370 in_reg = out_reg;
5371 }
5372 if (!out_reg.Equals(in_reg)) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005373 LoadConst32(out_reg.AsCoreRegister(), 0);
jeffhao7fbee072012-08-24 17:56:54 -07005374 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005375 Beqz(in_reg.AsCoreRegister(), &null_arg);
5376 Addiu32(out_reg.AsCoreRegister(), SP, handle_scope_offset.Int32Value());
5377 Bind(&null_arg);
jeffhao7fbee072012-08-24 17:56:54 -07005378 } else {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005379 Addiu32(out_reg.AsCoreRegister(), SP, handle_scope_offset.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005380 }
5381}
5382
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005383void MipsAssembler::CreateHandleScopeEntry(FrameOffset out_off,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005384 FrameOffset handle_scope_offset,
5385 ManagedRegister mscratch,
5386 bool null_allowed) {
jeffhao7fbee072012-08-24 17:56:54 -07005387 MipsManagedRegister scratch = mscratch.AsMips();
5388 CHECK(scratch.IsCoreRegister()) << scratch;
5389 if (null_allowed) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005390 MipsLabel null_arg;
5391 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, handle_scope_offset.Int32Value());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005392 // Null values get a handle scope entry value of 0. Otherwise, the handle scope entry is
5393 // the address in the handle scope holding the reference.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005394 // E.g. scratch = (scratch == 0) ? 0 : (SP+handle_scope_offset).
5395 Beqz(scratch.AsCoreRegister(), &null_arg);
5396 Addiu32(scratch.AsCoreRegister(), SP, handle_scope_offset.Int32Value());
5397 Bind(&null_arg);
jeffhao7fbee072012-08-24 17:56:54 -07005398 } else {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005399 Addiu32(scratch.AsCoreRegister(), SP, handle_scope_offset.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005400 }
5401 StoreToOffset(kStoreWord, scratch.AsCoreRegister(), SP, out_off.Int32Value());
5402}
5403
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07005404// Given a handle scope entry, load the associated reference.
5405void MipsAssembler::LoadReferenceFromHandleScope(ManagedRegister mout_reg,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005406 ManagedRegister min_reg) {
jeffhao7fbee072012-08-24 17:56:54 -07005407 MipsManagedRegister out_reg = mout_reg.AsMips();
5408 MipsManagedRegister in_reg = min_reg.AsMips();
5409 CHECK(out_reg.IsCoreRegister()) << out_reg;
5410 CHECK(in_reg.IsCoreRegister()) << in_reg;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005411 MipsLabel null_arg;
jeffhao7fbee072012-08-24 17:56:54 -07005412 if (!out_reg.Equals(in_reg)) {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005413 LoadConst32(out_reg.AsCoreRegister(), 0);
jeffhao7fbee072012-08-24 17:56:54 -07005414 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005415 Beqz(in_reg.AsCoreRegister(), &null_arg);
jeffhao7fbee072012-08-24 17:56:54 -07005416 LoadFromOffset(kLoadWord, out_reg.AsCoreRegister(),
5417 in_reg.AsCoreRegister(), 0);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005418 Bind(&null_arg);
jeffhao7fbee072012-08-24 17:56:54 -07005419}
5420
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005421void MipsAssembler::VerifyObject(ManagedRegister src ATTRIBUTE_UNUSED,
5422 bool could_be_null ATTRIBUTE_UNUSED) {
5423 // TODO: not validating references.
jeffhao7fbee072012-08-24 17:56:54 -07005424}
5425
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005426void MipsAssembler::VerifyObject(FrameOffset src ATTRIBUTE_UNUSED,
5427 bool could_be_null ATTRIBUTE_UNUSED) {
5428 // TODO: not validating references.
jeffhao7fbee072012-08-24 17:56:54 -07005429}
5430
5431void MipsAssembler::Call(ManagedRegister mbase, Offset offset, ManagedRegister mscratch) {
5432 MipsManagedRegister base = mbase.AsMips();
5433 MipsManagedRegister scratch = mscratch.AsMips();
5434 CHECK(base.IsCoreRegister()) << base;
5435 CHECK(scratch.IsCoreRegister()) << scratch;
5436 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(),
5437 base.AsCoreRegister(), offset.Int32Value());
5438 Jalr(scratch.AsCoreRegister());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005439 NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005440 // TODO: place reference map on call.
jeffhao7fbee072012-08-24 17:56:54 -07005441}
5442
5443void MipsAssembler::Call(FrameOffset base, Offset offset, ManagedRegister mscratch) {
5444 MipsManagedRegister scratch = mscratch.AsMips();
5445 CHECK(scratch.IsCoreRegister()) << scratch;
5446 // Call *(*(SP + base) + offset)
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005447 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(), SP, base.Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -07005448 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(),
5449 scratch.AsCoreRegister(), offset.Int32Value());
5450 Jalr(scratch.AsCoreRegister());
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005451 NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005452 // TODO: place reference map on call.
jeffhao7fbee072012-08-24 17:56:54 -07005453}
5454
Andreas Gampe3b165bc2016-08-01 22:07:04 -07005455void MipsAssembler::CallFromThread(ThreadOffset32 offset ATTRIBUTE_UNUSED,
5456 ManagedRegister mscratch ATTRIBUTE_UNUSED) {
Ian Rogers468532e2013-08-05 10:56:33 -07005457 UNIMPLEMENTED(FATAL) << "no mips implementation";
jeffhao7fbee072012-08-24 17:56:54 -07005458}
5459
5460void MipsAssembler::GetCurrentThread(ManagedRegister tr) {
5461 Move(tr.AsMips().AsCoreRegister(), S1);
5462}
5463
5464void MipsAssembler::GetCurrentThread(FrameOffset offset,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005465 ManagedRegister mscratch ATTRIBUTE_UNUSED) {
jeffhao7fbee072012-08-24 17:56:54 -07005466 StoreToOffset(kStoreWord, S1, SP, offset.Int32Value());
5467}
5468
jeffhao7fbee072012-08-24 17:56:54 -07005469void MipsAssembler::ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) {
5470 MipsManagedRegister scratch = mscratch.AsMips();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005471 exception_blocks_.emplace_back(scratch, stack_adjust);
jeffhao7fbee072012-08-24 17:56:54 -07005472 LoadFromOffset(kLoadWord, scratch.AsCoreRegister(),
Andreas Gampe542451c2016-07-26 09:02:02 -07005473 S1, Thread::ExceptionOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005474 Bnez(scratch.AsCoreRegister(), exception_blocks_.back().Entry());
jeffhao7fbee072012-08-24 17:56:54 -07005475}
5476
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005477void MipsAssembler::EmitExceptionPoll(MipsExceptionSlowPath* exception) {
5478 Bind(exception->Entry());
5479 if (exception->stack_adjust_ != 0) { // Fix up the frame.
5480 DecreaseFrameSize(exception->stack_adjust_);
jeffhao7fbee072012-08-24 17:56:54 -07005481 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005482 // Pass exception object as argument.
5483 // Don't care about preserving A0 as this call won't return.
5484 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5485 Move(A0, exception->scratch_.AsCoreRegister());
5486 // Set up call to Thread::Current()->pDeliverException.
5487 LoadFromOffset(kLoadWord, T9, S1,
Andreas Gampe542451c2016-07-26 09:02:02 -07005488 QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, pDeliverException).Int32Value());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005489 Jr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005490 NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02005491
5492 // Call never returns.
5493 Break();
jeffhao7fbee072012-08-24 17:56:54 -07005494}
5495
5496} // namespace mips
5497} // namespace art