blob: d50c439418cbd0485b64ce626fa948572e49d9a8 [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
18#define ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
jeffhao7fbee072012-08-24 17:56:54 -070019
Alexey Frunzee3fb2452016-05-10 16:08:05 -070020#include <deque>
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020021#include <utility>
jeffhao7fbee072012-08-24 17:56:54 -070022#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080023
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020024#include "arch/mips/instruction_set_features_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070025#include "base/arena_containers.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070026#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070028#include "constants_mips.h"
29#include "globals.h"
30#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070031#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020032#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070033#include "utils/jni_macro_assembler.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020034#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070035
36namespace art {
37namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070038
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020039static constexpr size_t kMipsWordSize = 4;
40static constexpr size_t kMipsDoublewordSize = 8;
41
jeffhao7fbee072012-08-24 17:56:54 -070042enum LoadOperandType {
43 kLoadSignedByte,
44 kLoadUnsignedByte,
45 kLoadSignedHalfword,
46 kLoadUnsignedHalfword,
47 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020048 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070049};
50
51enum StoreOperandType {
52 kStoreByte,
53 kStoreHalfword,
54 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020055 kStoreDoubleword
56};
57
Chris Larsenb74353a2015-11-20 09:07:09 -080058// Used to test the values returned by ClassS/ClassD.
59enum FPClassMaskType {
60 kSignalingNaN = 0x001,
61 kQuietNaN = 0x002,
62 kNegativeInfinity = 0x004,
63 kNegativeNormal = 0x008,
64 kNegativeSubnormal = 0x010,
65 kNegativeZero = 0x020,
66 kPositiveInfinity = 0x040,
67 kPositiveNormal = 0x080,
68 kPositiveSubnormal = 0x100,
69 kPositiveZero = 0x200,
70};
71
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020072class MipsLabel : public Label {
73 public:
74 MipsLabel() : prev_branch_id_plus_one_(0) {}
75
76 MipsLabel(MipsLabel&& src)
77 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
78
79 private:
80 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
81
82 friend class MipsAssembler;
83 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
84};
85
Alexey Frunzee3fb2452016-05-10 16:08:05 -070086// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
87class Literal {
88 public:
89 static constexpr size_t kMaxSize = 8;
90
91 Literal(uint32_t size, const uint8_t* data)
92 : label_(), size_(size) {
93 DCHECK_LE(size, Literal::kMaxSize);
94 memcpy(data_, data, size);
95 }
96
97 template <typename T>
98 T GetValue() const {
99 DCHECK_EQ(size_, sizeof(T));
100 T value;
101 memcpy(&value, data_, sizeof(T));
102 return value;
103 }
104
105 uint32_t GetSize() const {
106 return size_;
107 }
108
109 const uint8_t* GetData() const {
110 return data_;
111 }
112
113 MipsLabel* GetLabel() {
114 return &label_;
115 }
116
117 const MipsLabel* GetLabel() const {
118 return &label_;
119 }
120
121 private:
122 MipsLabel label_;
123 const uint32_t size_;
124 uint8_t data_[kMaxSize];
125
126 DISALLOW_COPY_AND_ASSIGN(Literal);
127};
128
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200129// Slowpath entered when Thread::Current()->_exception is non-null.
130class MipsExceptionSlowPath {
131 public:
132 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
133 : scratch_(scratch), stack_adjust_(stack_adjust) {}
134
135 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800136 : scratch_(src.scratch_),
137 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200138 exception_entry_(std::move(src.exception_entry_)) {}
139
140 private:
141 MipsLabel* Entry() { return &exception_entry_; }
142 const MipsManagedRegister scratch_;
143 const size_t stack_adjust_;
144 MipsLabel exception_entry_;
145
146 friend class MipsAssembler;
147 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700148};
149
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700150class MipsAssembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k32> {
jeffhao7fbee072012-08-24 17:56:54 -0700151 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100152 explicit MipsAssembler(ArenaAllocator* arena,
153 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
154 : Assembler(arena),
155 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200156 overwrite_location_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700157 reordering_(true),
158 ds_fsm_state_(kExpectingLabel),
159 ds_fsm_target_pc_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700160 literals_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200161 last_position_adjustment_(0),
162 last_old_position_(0),
163 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100164 isa_features_(instruction_set_features) {
165 cfi().DelayEmittingAdvancePCs();
166 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200167
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700168 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700169 size_t CodePosition() OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700170 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
171
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200172 virtual ~MipsAssembler() {
173 for (auto& branch : branches_) {
174 CHECK(branch.IsResolved());
175 }
176 }
jeffhao7fbee072012-08-24 17:56:54 -0700177
178 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700179 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700180 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700181 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200182
183 void MultR2(Register rs, Register rt); // R2
184 void MultuR2(Register rs, Register rt); // R2
185 void DivR2(Register rs, Register rt); // R2
186 void DivuR2(Register rs, Register rt); // R2
187 void MulR2(Register rd, Register rs, Register rt); // R2
188 void DivR2(Register rd, Register rs, Register rt); // R2
189 void ModR2(Register rd, Register rs, Register rt); // R2
190 void DivuR2(Register rd, Register rs, Register rt); // R2
191 void ModuR2(Register rd, Register rs, Register rt); // R2
192 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800193 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200194 void MuhuR6(Register rd, Register rs, Register rt); // R6
195 void DivR6(Register rd, Register rs, Register rt); // R6
196 void ModR6(Register rd, Register rs, Register rt); // R6
197 void DivuR6(Register rd, Register rs, Register rt); // R6
198 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700199
200 void And(Register rd, Register rs, Register rt);
201 void Andi(Register rt, Register rs, uint16_t imm16);
202 void Or(Register rd, Register rs, Register rt);
203 void Ori(Register rt, Register rs, uint16_t imm16);
204 void Xor(Register rd, Register rs, Register rt);
205 void Xori(Register rt, Register rs, uint16_t imm16);
206 void Nor(Register rd, Register rs, Register rt);
207
Chris Larsene3845472015-11-18 12:27:15 -0800208 void Movz(Register rd, Register rs, Register rt); // R2
209 void Movn(Register rd, Register rs, Register rt); // R2
210 void Seleqz(Register rd, Register rs, Register rt); // R6
211 void Selnez(Register rd, Register rs, Register rt); // R6
212 void ClzR6(Register rd, Register rs);
213 void ClzR2(Register rd, Register rs);
214 void CloR6(Register rd, Register rs);
215 void CloR2(Register rd, Register rs);
216
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200217 void Seb(Register rd, Register rt); // R2+
218 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700219 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800220 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200221
222 void Sll(Register rd, Register rt, int shamt);
223 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700224 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200225 void Sra(Register rd, Register rt, int shamt);
226 void Sllv(Register rd, Register rt, Register rs);
227 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800228 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200229 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800230 void Ext(Register rd, Register rt, int pos, int size); // R2+
231 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700232
233 void Lb(Register rt, Register rs, uint16_t imm16);
234 void Lh(Register rt, Register rs, uint16_t imm16);
235 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800236 void Lwl(Register rt, Register rs, uint16_t imm16);
237 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700238 void Lbu(Register rt, Register rs, uint16_t imm16);
239 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700240 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700241 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700242 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200243 void Sync(uint32_t stype);
244 void Mfhi(Register rd); // R2
245 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700246
247 void Sb(Register rt, Register rs, uint16_t imm16);
248 void Sh(Register rt, Register rs, uint16_t imm16);
249 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800250 void Swl(Register rt, Register rs, uint16_t imm16);
251 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700252
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700253 void LlR2(Register rt, Register base, int16_t imm16 = 0);
254 void ScR2(Register rt, Register base, int16_t imm16 = 0);
255 void LlR6(Register rt, Register base, int16_t imm9 = 0);
256 void ScR6(Register rt, Register base, int16_t imm9 = 0);
257
jeffhao7fbee072012-08-24 17:56:54 -0700258 void Slt(Register rd, Register rs, Register rt);
259 void Sltu(Register rd, Register rs, Register rt);
260 void Slti(Register rt, Register rs, uint16_t imm16);
261 void Sltiu(Register rt, Register rs, uint16_t imm16);
262
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700263 // Branches and jumps to immediate offsets/addresses do not take care of their
264 // delay/forbidden slots and generally should not be used directly. This applies
265 // to the following R2 and R6 branch/jump instructions with imm16, imm21, addr26
266 // offsets/addresses.
267 // Use branches/jumps to labels instead.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200268 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700269 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200270 void Beq(Register rs, Register rt, uint16_t imm16);
271 void Bne(Register rs, Register rt, uint16_t imm16);
272 void Beqz(Register rt, uint16_t imm16);
273 void Bnez(Register rt, uint16_t imm16);
274 void Bltz(Register rt, uint16_t imm16);
275 void Bgez(Register rt, uint16_t imm16);
276 void Blez(Register rt, uint16_t imm16);
277 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800278 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800279 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800280 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800281 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200282 void J(uint32_t addr26);
283 void Jal(uint32_t addr26);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700284 // Jalr() and Jr() fill their delay slots when reordering is enabled.
285 // When reordering is disabled, the delay slots must be filled manually.
286 // You may use NopIfNoReordering() to fill them when reordering is disabled.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200287 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700288 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200289 void Jr(Register rs);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700290 // Nal() does not fill its delay slot. It must be filled manually.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200291 void Nal();
292 void Auipc(Register rs, uint16_t imm16); // R6
293 void Addiupc(Register rs, uint32_t imm19); // R6
294 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700295 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200296 void Jic(Register rt, uint16_t imm16); // R6
297 void Jialc(Register rt, uint16_t imm16); // R6
298 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
299 void Bltzc(Register rt, uint16_t imm16); // R6
300 void Bgtzc(Register rt, uint16_t imm16); // R6
301 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
302 void Bgezc(Register rt, uint16_t imm16); // R6
303 void Blezc(Register rt, uint16_t imm16); // R6
304 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
305 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
306 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
307 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
308 void Beqzc(Register rs, uint32_t imm21); // R6
309 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800310 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
311 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700312
313 void AddS(FRegister fd, FRegister fs, FRegister ft);
314 void SubS(FRegister fd, FRegister fs, FRegister ft);
315 void MulS(FRegister fd, FRegister fs, FRegister ft);
316 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200317 void AddD(FRegister fd, FRegister fs, FRegister ft);
318 void SubD(FRegister fd, FRegister fs, FRegister ft);
319 void MulD(FRegister fd, FRegister fs, FRegister ft);
320 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800321 void SqrtS(FRegister fd, FRegister fs);
322 void SqrtD(FRegister fd, FRegister fs);
323 void AbsS(FRegister fd, FRegister fs);
324 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700325 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200326 void MovD(FRegister fd, FRegister fs);
327 void NegS(FRegister fd, FRegister fs);
328 void NegD(FRegister fd, FRegister fs);
329
Chris Larsenb74353a2015-11-20 09:07:09 -0800330 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800331 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800332 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800333 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800334 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800335 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800336 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800337 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800338 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800339 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800340 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800341 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800342 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800343 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800344 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800345 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800346 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800347 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800348 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800349 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800350 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800351 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800352 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800353 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800354 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800355 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800356 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800357 void CuleD(int cc, FRegister fs, FRegister ft); // R2
358 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
359 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
360 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
361 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
362 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
363 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
364 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
365 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
366 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
367 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
368 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
369 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
370 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
371 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
372 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
373 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
374 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
375 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
376 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
377 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800378 void Movf(Register rd, Register rs, int cc = 0); // R2
379 void Movt(Register rd, Register rs, int cc = 0); // R2
380 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
381 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
382 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
383 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
384 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
385 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
386 void ClassS(FRegister fd, FRegister fs); // R6
387 void ClassD(FRegister fd, FRegister fs); // R6
388 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
389 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
390 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
391 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800392
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800393 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
394 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
395 void TruncWS(FRegister fd, FRegister fs);
396 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200397 void Cvtsw(FRegister fd, FRegister fs);
398 void Cvtdw(FRegister fd, FRegister fs);
399 void Cvtsd(FRegister fd, FRegister fs);
400 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800401 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
402 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800403 void FloorWS(FRegister fd, FRegister fs);
404 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700405
406 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200407 void Mtc1(Register rt, FRegister fs);
408 void Mfhc1(Register rt, FRegister fs);
409 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800410 void MoveFromFpuHigh(Register rt, FRegister fs);
411 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700412 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200413 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700414 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200415 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700416
417 void Break();
jeffhao07030602012-09-26 14:33:14 -0700418 void Nop();
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700419 void NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200420 void Move(Register rd, Register rs);
421 void Clear(Register rd);
422 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700423
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200424 // Higher level composite instructions.
425 void LoadConst32(Register rd, int32_t value);
426 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
427 void LoadDConst64(FRegister rd, int64_t value, Register temp);
428 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200429 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
430
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700431 // These will generate R2 branches or R6 branches as appropriate and take care of
432 // the delay/forbidden slots.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200433 void Bind(MipsLabel* label);
434 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700435 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200436 void Beq(Register rs, Register rt, MipsLabel* label);
437 void Bne(Register rs, Register rt, MipsLabel* label);
438 void Beqz(Register rt, MipsLabel* label);
439 void Bnez(Register rt, MipsLabel* label);
440 void Bltz(Register rt, MipsLabel* label);
441 void Bgez(Register rt, MipsLabel* label);
442 void Blez(Register rt, MipsLabel* label);
443 void Bgtz(Register rt, MipsLabel* label);
444 void Blt(Register rs, Register rt, MipsLabel* label);
445 void Bge(Register rs, Register rt, MipsLabel* label);
446 void Bltu(Register rs, Register rt, MipsLabel* label);
447 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800448 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800449 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800450 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800451 void Bc1t(int cc, MipsLabel* label); // R2
452 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
453 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700454
455 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700456 void AdjustBaseAndOffset(Register& base,
457 int32_t& offset,
458 bool is_doubleword,
459 bool is_float = false);
Alexey Frunze2923db72016-08-20 01:55:47 -0700460
461 private:
462 struct NoImplicitNullChecker {
463 void operator()() {}
464 };
465
466 public:
467 template <typename ImplicitNullChecker = NoImplicitNullChecker>
468 void StoreConst32ToOffset(int32_t value,
469 Register base,
470 int32_t offset,
471 Register temp,
472 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
473 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
474 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false);
475 if (value == 0) {
476 temp = ZERO;
477 } else {
478 LoadConst32(temp, value);
479 }
480 Sw(temp, base, offset);
481 null_checker();
482 }
483
484 template <typename ImplicitNullChecker = NoImplicitNullChecker>
485 void StoreConst64ToOffset(int64_t value,
486 Register base,
487 int32_t offset,
488 Register temp,
489 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
490 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
491 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true);
492 uint32_t low = Low32Bits(value);
493 uint32_t high = High32Bits(value);
494 if (low == 0) {
495 Sw(ZERO, base, offset);
496 } else {
497 LoadConst32(temp, low);
498 Sw(temp, base, offset);
499 }
500 null_checker();
501 if (high == 0) {
502 Sw(ZERO, base, offset + kMipsWordSize);
503 } else {
504 if (high != low) {
505 LoadConst32(temp, high);
506 }
507 Sw(temp, base, offset + kMipsWordSize);
508 }
509 }
510
511 template <typename ImplicitNullChecker = NoImplicitNullChecker>
512 void LoadFromOffset(LoadOperandType type,
513 Register reg,
514 Register base,
515 int32_t offset,
516 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
517 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
518 switch (type) {
519 case kLoadSignedByte:
520 Lb(reg, base, offset);
521 break;
522 case kLoadUnsignedByte:
523 Lbu(reg, base, offset);
524 break;
525 case kLoadSignedHalfword:
526 Lh(reg, base, offset);
527 break;
528 case kLoadUnsignedHalfword:
529 Lhu(reg, base, offset);
530 break;
531 case kLoadWord:
532 Lw(reg, base, offset);
533 break;
534 case kLoadDoubleword:
535 if (reg == base) {
536 // This will clobber the base when loading the lower register. Since we have to load the
537 // higher register as well, this will fail. Solution: reverse the order.
538 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
539 null_checker();
540 Lw(reg, base, offset);
541 } else {
542 Lw(reg, base, offset);
543 null_checker();
544 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
545 }
546 break;
547 default:
548 LOG(FATAL) << "UNREACHABLE";
549 }
550 if (type != kLoadDoubleword) {
551 null_checker();
552 }
553 }
554
555 template <typename ImplicitNullChecker = NoImplicitNullChecker>
556 void LoadSFromOffset(FRegister reg,
557 Register base,
558 int32_t offset,
559 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
560 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
561 Lwc1(reg, base, offset);
562 null_checker();
563 }
564
565 template <typename ImplicitNullChecker = NoImplicitNullChecker>
566 void LoadDFromOffset(FRegister reg,
567 Register base,
568 int32_t offset,
569 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
570 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
571 if (IsAligned<kMipsDoublewordSize>(offset)) {
572 Ldc1(reg, base, offset);
573 null_checker();
574 } else {
575 if (Is32BitFPU()) {
576 Lwc1(reg, base, offset);
577 null_checker();
578 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
579 } else {
580 // 64-bit FPU.
581 Lwc1(reg, base, offset);
582 null_checker();
583 Lw(T8, base, offset + kMipsWordSize);
584 Mthc1(T8, reg);
585 }
586 }
587 }
588
589 template <typename ImplicitNullChecker = NoImplicitNullChecker>
590 void StoreToOffset(StoreOperandType type,
591 Register reg,
592 Register base,
593 int32_t offset,
594 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
595 // Must not use AT as `reg`, so as not to overwrite the value being stored
596 // with the adjusted `base`.
597 CHECK_NE(reg, AT);
598 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
599 switch (type) {
600 case kStoreByte:
601 Sb(reg, base, offset);
602 break;
603 case kStoreHalfword:
604 Sh(reg, base, offset);
605 break;
606 case kStoreWord:
607 Sw(reg, base, offset);
608 break;
609 case kStoreDoubleword:
610 CHECK_NE(reg, base);
611 CHECK_NE(static_cast<Register>(reg + 1), base);
612 Sw(reg, base, offset);
613 null_checker();
614 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
615 break;
616 default:
617 LOG(FATAL) << "UNREACHABLE";
618 }
619 if (type != kStoreDoubleword) {
620 null_checker();
621 }
622 }
623
624 template <typename ImplicitNullChecker = NoImplicitNullChecker>
625 void StoreSToOffset(FRegister reg,
626 Register base,
627 int32_t offset,
628 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
629 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
630 Swc1(reg, base, offset);
631 null_checker();
632 }
633
634 template <typename ImplicitNullChecker = NoImplicitNullChecker>
635 void StoreDToOffset(FRegister reg,
636 Register base,
637 int32_t offset,
638 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
639 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
640 if (IsAligned<kMipsDoublewordSize>(offset)) {
641 Sdc1(reg, base, offset);
642 null_checker();
643 } else {
644 if (Is32BitFPU()) {
645 Swc1(reg, base, offset);
646 null_checker();
647 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
648 } else {
649 // 64-bit FPU.
650 Mfhc1(T8, reg);
651 Swc1(reg, base, offset);
652 null_checker();
653 Sw(T8, base, offset + kMipsWordSize);
654 }
655 }
656 }
657
jeffhao7fbee072012-08-24 17:56:54 -0700658 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
659 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200660 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700661 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000662 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200663 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700664
jeffhao7fbee072012-08-24 17:56:54 -0700665 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200666 void Emit(uint32_t value);
667
668 // Push/pop composite routines.
669 void Push(Register rs);
670 void Pop(Register rd);
671 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700672
Andreas Gampe85b62f22015-09-09 13:15:38 -0700673 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200674 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700675 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200676 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
677 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700678 }
679
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700680 // Create a new literal with a given value.
681 // NOTE: Force the template parameter to be explicitly specified.
682 template <typename T>
683 Literal* NewLiteral(typename Identity<T>::type value) {
684 static_assert(std::is_integral<T>::value, "T must be an integral type.");
685 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
686 }
687
688 // Create a new literal with the given data.
689 Literal* NewLiteral(size_t size, const uint8_t* data);
690
691 // Load literal using the base register (for R2 only) or using PC-relative loads
692 // (for R6 only; base_reg must be ZERO).
693 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
694
jeffhao7fbee072012-08-24 17:56:54 -0700695 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200696 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700697 //
698
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200699 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800700 void BuildFrame(size_t frame_size,
701 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100702 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700703 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700704
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200705 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100706 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700707 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700708
Ian Rogersdd7624d2014-03-14 17:43:00 -0700709 void IncreaseFrameSize(size_t adjust) OVERRIDE;
710 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700711
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200712 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700713 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
714 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
715 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700716
Ian Rogersdd7624d2014-03-14 17:43:00 -0700717 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700718
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700719 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
720 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800721 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700722
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700723 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700724
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800725 void StoreSpanning(FrameOffset dest,
726 ManagedRegister msrc,
727 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700728 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700729
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200730 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700731 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700732
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700733 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700734
Mathieu Chartiere401d142015-04-22 13:56:20 -0700735 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700736
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800737 void LoadRef(ManagedRegister mdest,
738 ManagedRegister base,
739 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100740 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700741
Ian Rogersdd7624d2014-03-14 17:43:00 -0700742 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700743
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700744 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700745
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200746 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700747 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700748
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700749 void CopyRawPtrFromThread(FrameOffset fr_offs,
750 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700751 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700752
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700753 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
754 FrameOffset fr_offs,
755 ManagedRegister mscratch) OVERRIDE;
756
Ian Rogersdd7624d2014-03-14 17:43:00 -0700757 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700758
Ian Rogersdd7624d2014-03-14 17:43:00 -0700759 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700760
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800761 void Copy(FrameOffset dest,
762 ManagedRegister src_base,
763 Offset src_offset,
764 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700765 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700766
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800767 void Copy(ManagedRegister dest_base,
768 Offset dest_offset,
769 FrameOffset src,
770 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700771 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700772
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800773 void Copy(FrameOffset dest,
774 FrameOffset src_base,
775 Offset src_offset,
776 ManagedRegister mscratch,
777 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700778
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800779 void Copy(ManagedRegister dest,
780 Offset dest_offset,
781 ManagedRegister src,
782 Offset src_offset,
783 ManagedRegister mscratch,
784 size_t size) OVERRIDE;
785
786 void Copy(FrameOffset dest,
787 Offset dest_offset,
788 FrameOffset src,
789 Offset src_offset,
790 ManagedRegister mscratch,
791 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700792
Ian Rogersdd7624d2014-03-14 17:43:00 -0700793 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700794
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200795 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700796 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700797
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200798 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700799 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700800
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200801 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700802 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
803 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700804
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700805 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700806 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700807 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700808 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800809 void CreateHandleScopeEntry(ManagedRegister out_reg,
810 FrameOffset handlescope_offset,
811 ManagedRegister in_reg,
812 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700813
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700814 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700815 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800816 void CreateHandleScopeEntry(FrameOffset out_off,
817 FrameOffset handlescope_offset,
818 ManagedRegister mscratch,
819 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700820
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200821 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700822 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700823
824 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
825 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700826 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
827 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700828
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200829 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700830 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
831 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700832 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700833
jeffhao7fbee072012-08-24 17:56:54 -0700834 // Generate code to check if Thread::Current()->exception_ is non-null
835 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700836 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700837
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200838 // Emit slow paths queued during assembly and promote short branches to long if needed.
839 void FinalizeCode() OVERRIDE;
840
841 // Emit branches and finalize all instructions.
842 void FinalizeInstructions(const MemoryRegion& region);
843
844 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
845 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700846 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200847
848 // Get the final position of a label after local fixup based on the old position
849 // recorded before FinalizeCode().
850 uint32_t GetAdjustedPosition(uint32_t old_position);
851
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700852 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
853 // reading the PC value into a general-purpose register with the NAL instruction and then loading
854 // literals through this base register. The code generator calls this method (at most once per
855 // method being compiled) to bind a label to the location for which the PC value is acquired.
856 // The assembler then computes literal offsets relative to this label.
857 void BindPcRelBaseLabel();
858
Alexey Frunze06a46c42016-07-19 15:00:40 -0700859 // Returns the location of the label bound with BindPcRelBaseLabel().
860 uint32_t GetPcRelBaseLabelLocation() const;
861
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700862 // Note that PC-relative literal loads are handled as pseudo branches because they need very
863 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
864 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200865 enum BranchCondition {
866 kCondLT,
867 kCondGE,
868 kCondLE,
869 kCondGT,
870 kCondLTZ,
871 kCondGEZ,
872 kCondLEZ,
873 kCondGTZ,
874 kCondEQ,
875 kCondNE,
876 kCondEQZ,
877 kCondNEZ,
878 kCondLTU,
879 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800880 kCondF, // Floating-point predicate false.
881 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200882 kUncond,
883 };
884 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
885
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700886 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
887 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
888 // Returns the last state, which may be useful for temporary enabling/disabling of
889 // reordering.
890 bool SetReorder(bool enable);
891
jeffhao7fbee072012-08-24 17:56:54 -0700892 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700893 // Description of the last instruction in terms of input and output registers.
894 // Used to make the decision of moving the instruction into a delay slot.
895 struct DelaySlot {
896 DelaySlot();
897 // Encoded instruction that may be used to fill the delay slot or 0
898 // (0 conveniently represents NOP).
899 uint32_t instruction_;
900 // Mask of output GPRs for the instruction.
901 uint32_t gpr_outs_mask_;
902 // Mask of input GPRs for the instruction.
903 uint32_t gpr_ins_mask_;
904 // Mask of output FPRs for the instruction.
905 uint32_t fpr_outs_mask_;
906 // Mask of input FPRs for the instruction.
907 uint32_t fpr_ins_mask_;
908 // Mask of output FPU condition code flags for the instruction.
909 uint32_t cc_outs_mask_;
910 // Mask of input FPU condition code flags for the instruction.
911 uint32_t cc_ins_mask_;
912 // Branches never operate on the LO and HI registers, hence there's
913 // no mask for LO and HI.
914 };
915
916 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
917 // upon every new instruction and label generated. The FSM detects instructions
918 // suitable for delay slots and immediately preceded with labels. These are target
919 // instructions for branches. If an unconditional R2 branch does not get its delay
920 // slot filled with the immediately preceding instruction, it may instead get the
921 // slot filled with the target instruction (the branch will need its offset
922 // incremented past the target instruction). We call this "absorption". The FSM
923 // records PCs of the target instructions suitable for this optimization.
924 enum DsFsmState {
925 kExpectingLabel,
926 kExpectingInstruction,
927 kExpectingCommit
928 };
929 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
930
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200931 class Branch {
932 public:
933 enum Type {
934 // R2 short branches.
935 kUncondBranch,
936 kCondBranch,
937 kCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700938 // R2 near literal.
939 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200940 // R2 long branches.
941 kLongUncondBranch,
942 kLongCondBranch,
943 kLongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700944 // R2 far literal.
945 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200946 // R6 short branches.
947 kR6UncondBranch,
948 kR6CondBranch,
949 kR6Call,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700950 // R6 near literal.
951 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200952 // R6 long branches.
953 kR6LongUncondBranch,
954 kR6LongCondBranch,
955 kR6LongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700956 // R6 far literal.
957 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200958 };
959 // Bit sizes of offsets defined as enums to minimize chance of typos.
960 enum OffsetBits {
961 kOffset16 = 16,
962 kOffset18 = 18,
963 kOffset21 = 21,
964 kOffset23 = 23,
965 kOffset28 = 28,
966 kOffset32 = 32,
967 };
968
969 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
970 static constexpr int32_t kMaxBranchLength = 32;
971 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700972 // The following two instruction encodings can never legally occur in branch delay
973 // slots and are used as markers.
974 //
975 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
976 // instruction to fill its delay slot (the latter is only possible with unconditional
977 // R2 branches and is termed here as "absorption").
978 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
979 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
980 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
981 // absorption of the target instruction when reordering is disabled.
982 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200983
984 struct BranchInfo {
985 // Branch length as a number of 4-byte-long instructions.
986 uint32_t length;
987 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
988 // PC-relative offset (or its most significant 16-bit half, which goes first).
989 uint32_t instr_offset;
990 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
991 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
992 // instructions) from the instruction containing the offset.
993 uint32_t pc_org;
994 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
995 // is an exception: use kOffset23 for beqzc/bnezc).
996 OffsetBits offset_size;
997 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
998 // count.
999 int offset_shift;
1000 };
1001 static const BranchInfo branch_info_[/* Type */];
1002
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001003 // Unconditional branch or call.
1004 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001005 // Conditional branch.
1006 Branch(bool is_r6,
1007 uint32_t location,
1008 uint32_t target,
1009 BranchCondition condition,
1010 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001011 Register rhs_reg);
1012 // Literal.
1013 Branch(bool is_r6, uint32_t location, Register dest_reg, Register base_reg);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001014
1015 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1016 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1017 // So, we need a way to identify such branches in order to emit no instructions for them
1018 // or change them to unconditional.
1019 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1020 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1021
1022 static BranchCondition OppositeCondition(BranchCondition cond);
1023
1024 Type GetType() const;
1025 BranchCondition GetCondition() const;
1026 Register GetLeftRegister() const;
1027 Register GetRightRegister() const;
1028 uint32_t GetTarget() const;
1029 uint32_t GetLocation() const;
1030 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001031 uint32_t GetPrecedingInstructionLength(Type type) const;
1032 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001033 uint32_t GetLength() const;
1034 uint32_t GetOldLength() const;
1035 uint32_t GetSize() const;
1036 uint32_t GetOldSize() const;
1037 uint32_t GetEndLocation() const;
1038 uint32_t GetOldEndLocation() const;
1039 bool IsLong() const;
1040 bool IsResolved() const;
1041
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001042 // Various helpers for branch delay slot management.
1043 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
1044 void SetDelayedInstruction(uint32_t instruction);
1045 uint32_t GetDelayedInstruction() const;
1046 void DecrementLocations();
1047
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001048 // Returns the bit size of the signed offset that the branch instruction can handle.
1049 OffsetBits GetOffsetSize() const;
1050
1051 // Calculates the distance between two byte locations in the assembler buffer and
1052 // returns the number of bits needed to represent the distance as a signed integer.
1053 //
1054 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1055 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1056 //
1057 // Composite branches (made of several instructions) with longer reach have 32-bit
1058 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001059 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1060 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1061 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1062 // Consider the following implementation of a long unconditional branch, for
1063 // example:
1064 //
1065 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1066 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1067 //
1068 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1069 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1070 // due to sign extension. This must be compensated for by incrementing offset_31_16
1071 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1072 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1073 // Therefore, the long branch range is something like from PC - 0x80000000 to
1074 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001075 //
1076 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1077 // case with the addiu instruction and a 16 bit offset.
1078 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1079
1080 // Resolve a branch when the target is known.
1081 void Resolve(uint32_t target);
1082
1083 // Relocate a branch by a given delta if needed due to expansion of this or another
1084 // branch at a given location by this delta (just changes location_ and target_).
1085 void Relocate(uint32_t expand_location, uint32_t delta);
1086
1087 // If the branch is short, changes its type to long.
1088 void PromoteToLong();
1089
1090 // If necessary, updates the type by promoting a short branch to a long branch
1091 // based on the branch location and target. Returns the amount (in bytes) by
1092 // which the branch size has increased.
1093 // max_short_distance caps the maximum distance between location_ and target_
1094 // that is allowed for short branches. This is for debugging/testing purposes.
1095 // max_short_distance = 0 forces all short branches to become long.
1096 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001097 uint32_t PromoteIfNeeded(uint32_t location,
1098 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001099
1100 // Returns the location of the instruction(s) containing the offset.
1101 uint32_t GetOffsetLocation() const;
1102
1103 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001104 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001105
1106 private:
1107 // Completes branch construction by determining and recording its type.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001108 void InitializeType(bool is_call, bool is_literal, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001109 // Helper for the above.
1110 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1111
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001112 uint32_t old_location_; // Offset into assembler buffer in bytes.
1113 uint32_t location_; // Offset into assembler buffer in bytes.
1114 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001115
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001116 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1117 // FPU condition code. Destination register in literals.
1118 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1119 // Base register in literals (ZERO on R6).
1120 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001121
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001122 Type type_; // Current type of the branch.
1123 Type old_type_; // Initial type of the branch.
1124
1125 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1126 // kUnfilledDelaySlot if none but fillable or
1127 // kUnfillableDelaySlot if none and unfillable
1128 // (the latter is only used for unconditional R2
1129 // branches).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001130 };
1131 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1132 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1133
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001134 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1135 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1136 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1137 uint32_t EmitI26(int opcode, uint32_t imm26);
1138 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1139 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001140 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1141 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001142
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001143 void Buncond(MipsLabel* label);
1144 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001145 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001146 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001147
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001148 // Various helpers for branch delay slot management.
1149 void DsFsmInstr(uint32_t instruction,
1150 uint32_t gpr_outs_mask,
1151 uint32_t gpr_ins_mask,
1152 uint32_t fpr_outs_mask,
1153 uint32_t fpr_ins_mask,
1154 uint32_t cc_outs_mask,
1155 uint32_t cc_ins_mask);
1156 void DsFsmInstrNop(uint32_t instruction);
1157 void DsFsmInstrRrr(uint32_t instruction, Register out, Register in1, Register in2);
1158 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1159 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1160 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
1161 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1162 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1163 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1164 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1165 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1166 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1167 void DsFsmLabel();
1168 void DsFsmCommitLabel();
1169 void DsFsmDropLabel();
1170 void MoveInstructionToDelaySlot(Branch& branch);
1171 bool CanExchangeWithSlt(Register rs, Register rt) const;
1172 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1173 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1174
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001175 Branch* GetBranch(uint32_t branch_id);
1176 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001177 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1178 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001179
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001180 void EmitLiterals();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001181 void PromoteBranches();
1182 void EmitBranch(Branch* branch);
1183 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001184 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001185
1186 // Emits exception block.
1187 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1188
1189 bool IsR6() const {
1190 if (isa_features_ != nullptr) {
1191 return isa_features_->IsR6();
1192 } else {
1193 return false;
1194 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001195 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001196
1197 bool Is32BitFPU() const {
1198 if (isa_features_ != nullptr) {
1199 return isa_features_->Is32BitFloatingPoint();
1200 } else {
1201 return true;
1202 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001203 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001204
1205 // List of exception blocks to generate at the end of the code cache.
1206 std::vector<MipsExceptionSlowPath> exception_blocks_;
1207
1208 std::vector<Branch> branches_;
1209
1210 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1211 bool overwriting_;
1212 // The current overwrite location.
1213 uint32_t overwrite_location_;
1214
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001215 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1216 bool reordering_;
1217 // Information about the last instruction that may be used to fill a branch delay slot.
1218 DelaySlot delay_slot_;
1219 // Delay slot FSM state.
1220 DsFsmState ds_fsm_state_;
1221 // PC of the current labeled target instruction.
1222 uint32_t ds_fsm_target_pc_;
1223 // PCs of labeled target instructions.
1224 std::vector<uint32_t> ds_fsm_target_pcs_;
1225
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001226 // Use std::deque<> for literal labels to allow insertions at the end
1227 // without invalidating pointers and references to existing elements.
1228 ArenaDeque<Literal> literals_;
1229
1230 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1231 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1232 // that PC (from NAL) points to.
1233 MipsLabel pc_rel_base_label_;
1234
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001235 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001236 uint32_t last_position_adjustment_;
1237 uint32_t last_old_position_;
1238 uint32_t last_branch_id_;
1239
1240 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001241
jeffhao7fbee072012-08-24 17:56:54 -07001242 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1243};
1244
jeffhao7fbee072012-08-24 17:56:54 -07001245} // namespace mips
1246} // namespace art
1247
Ian Rogers166db042013-07-26 12:05:57 -07001248#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_