blob: 1a5a23d10b1ddc2ce5648bbf72828a8329e36647 [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
18#define ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
jeffhao7fbee072012-08-24 17:56:54 -070019
Alexey Frunzee3fb2452016-05-10 16:08:05 -070020#include <deque>
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020021#include <utility>
jeffhao7fbee072012-08-24 17:56:54 -070022#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080023
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020024#include "arch/mips/instruction_set_features_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070025#include "base/arena_containers.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070026#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070028#include "constants_mips.h"
29#include "globals.h"
30#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070031#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020032#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070033#include "utils/jni_macro_assembler.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020034#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070035
36namespace art {
37namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070038
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020039static constexpr size_t kMipsWordSize = 4;
40static constexpr size_t kMipsDoublewordSize = 8;
41
jeffhao7fbee072012-08-24 17:56:54 -070042enum LoadOperandType {
43 kLoadSignedByte,
44 kLoadUnsignedByte,
45 kLoadSignedHalfword,
46 kLoadUnsignedHalfword,
47 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020048 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070049};
50
51enum StoreOperandType {
52 kStoreByte,
53 kStoreHalfword,
54 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020055 kStoreDoubleword
56};
57
Chris Larsenb74353a2015-11-20 09:07:09 -080058// Used to test the values returned by ClassS/ClassD.
59enum FPClassMaskType {
60 kSignalingNaN = 0x001,
61 kQuietNaN = 0x002,
62 kNegativeInfinity = 0x004,
63 kNegativeNormal = 0x008,
64 kNegativeSubnormal = 0x010,
65 kNegativeZero = 0x020,
66 kPositiveInfinity = 0x040,
67 kPositiveNormal = 0x080,
68 kPositiveSubnormal = 0x100,
69 kPositiveZero = 0x200,
70};
71
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020072class MipsLabel : public Label {
73 public:
74 MipsLabel() : prev_branch_id_plus_one_(0) {}
75
76 MipsLabel(MipsLabel&& src)
77 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
78
79 private:
80 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
81
82 friend class MipsAssembler;
83 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
84};
85
Alexey Frunzee3fb2452016-05-10 16:08:05 -070086// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
87class Literal {
88 public:
89 static constexpr size_t kMaxSize = 8;
90
91 Literal(uint32_t size, const uint8_t* data)
92 : label_(), size_(size) {
93 DCHECK_LE(size, Literal::kMaxSize);
94 memcpy(data_, data, size);
95 }
96
97 template <typename T>
98 T GetValue() const {
99 DCHECK_EQ(size_, sizeof(T));
100 T value;
101 memcpy(&value, data_, sizeof(T));
102 return value;
103 }
104
105 uint32_t GetSize() const {
106 return size_;
107 }
108
109 const uint8_t* GetData() const {
110 return data_;
111 }
112
113 MipsLabel* GetLabel() {
114 return &label_;
115 }
116
117 const MipsLabel* GetLabel() const {
118 return &label_;
119 }
120
121 private:
122 MipsLabel label_;
123 const uint32_t size_;
124 uint8_t data_[kMaxSize];
125
126 DISALLOW_COPY_AND_ASSIGN(Literal);
127};
128
Alexey Frunze96b66822016-09-10 02:32:44 -0700129// Jump table: table of labels emitted after the literals. Similar to literals.
130class JumpTable {
131 public:
132 explicit JumpTable(std::vector<MipsLabel*>&& labels)
133 : label_(), labels_(std::move(labels)) {
134 }
135
136 uint32_t GetSize() const {
137 return static_cast<uint32_t>(labels_.size()) * sizeof(uint32_t);
138 }
139
140 const std::vector<MipsLabel*>& GetData() const {
141 return labels_;
142 }
143
144 MipsLabel* GetLabel() {
145 return &label_;
146 }
147
148 const MipsLabel* GetLabel() const {
149 return &label_;
150 }
151
152 private:
153 MipsLabel label_;
154 std::vector<MipsLabel*> labels_;
155
156 DISALLOW_COPY_AND_ASSIGN(JumpTable);
157};
158
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200159// Slowpath entered when Thread::Current()->_exception is non-null.
160class MipsExceptionSlowPath {
161 public:
162 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
163 : scratch_(scratch), stack_adjust_(stack_adjust) {}
164
165 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800166 : scratch_(src.scratch_),
167 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200168 exception_entry_(std::move(src.exception_entry_)) {}
169
170 private:
171 MipsLabel* Entry() { return &exception_entry_; }
172 const MipsManagedRegister scratch_;
173 const size_t stack_adjust_;
174 MipsLabel exception_entry_;
175
176 friend class MipsAssembler;
177 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700178};
179
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700180class MipsAssembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k32> {
jeffhao7fbee072012-08-24 17:56:54 -0700181 public:
Igor Murashkinae7ff922016-10-06 14:59:19 -0700182 using JNIBase = JNIMacroAssembler<PointerSize::k32>;
183
Vladimir Marko93205e32016-04-13 11:59:46 +0100184 explicit MipsAssembler(ArenaAllocator* arena,
185 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
186 : Assembler(arena),
187 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200188 overwrite_location_(0),
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700189 reordering_(true),
190 ds_fsm_state_(kExpectingLabel),
191 ds_fsm_target_pc_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700192 literals_(arena->Adapter(kArenaAllocAssembler)),
Alexey Frunze96b66822016-09-10 02:32:44 -0700193 jump_tables_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200194 last_position_adjustment_(0),
195 last_old_position_(0),
196 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100197 isa_features_(instruction_set_features) {
198 cfi().DelayEmittingAdvancePCs();
199 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200200
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700201 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700202 size_t CodePosition() OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700203 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
204
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200205 virtual ~MipsAssembler() {
206 for (auto& branch : branches_) {
207 CHECK(branch.IsResolved());
208 }
209 }
jeffhao7fbee072012-08-24 17:56:54 -0700210
211 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700212 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700213 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700214 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200215
216 void MultR2(Register rs, Register rt); // R2
217 void MultuR2(Register rs, Register rt); // R2
218 void DivR2(Register rs, Register rt); // R2
219 void DivuR2(Register rs, Register rt); // R2
220 void MulR2(Register rd, Register rs, Register rt); // R2
221 void DivR2(Register rd, Register rs, Register rt); // R2
222 void ModR2(Register rd, Register rs, Register rt); // R2
223 void DivuR2(Register rd, Register rs, Register rt); // R2
224 void ModuR2(Register rd, Register rs, Register rt); // R2
225 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800226 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200227 void MuhuR6(Register rd, Register rs, Register rt); // R6
228 void DivR6(Register rd, Register rs, Register rt); // R6
229 void ModR6(Register rd, Register rs, Register rt); // R6
230 void DivuR6(Register rd, Register rs, Register rt); // R6
231 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700232
233 void And(Register rd, Register rs, Register rt);
234 void Andi(Register rt, Register rs, uint16_t imm16);
235 void Or(Register rd, Register rs, Register rt);
236 void Ori(Register rt, Register rs, uint16_t imm16);
237 void Xor(Register rd, Register rs, Register rt);
238 void Xori(Register rt, Register rs, uint16_t imm16);
239 void Nor(Register rd, Register rs, Register rt);
240
Chris Larsene3845472015-11-18 12:27:15 -0800241 void Movz(Register rd, Register rs, Register rt); // R2
242 void Movn(Register rd, Register rs, Register rt); // R2
243 void Seleqz(Register rd, Register rs, Register rt); // R6
244 void Selnez(Register rd, Register rs, Register rt); // R6
245 void ClzR6(Register rd, Register rs);
246 void ClzR2(Register rd, Register rs);
247 void CloR6(Register rd, Register rs);
248 void CloR2(Register rd, Register rs);
249
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200250 void Seb(Register rd, Register rt); // R2+
251 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700252 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800253 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200254
255 void Sll(Register rd, Register rt, int shamt);
256 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700257 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200258 void Sra(Register rd, Register rt, int shamt);
259 void Sllv(Register rd, Register rt, Register rs);
260 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800261 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200262 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800263 void Ext(Register rd, Register rt, int pos, int size); // R2+
264 void Ins(Register rd, Register rt, int pos, int size); // R2+
Chris Larsen692235e2016-11-21 16:04:53 -0800265 void Lsa(Register rd, Register rs, Register rt, int saPlusOne); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700266
267 void Lb(Register rt, Register rs, uint16_t imm16);
268 void Lh(Register rt, Register rs, uint16_t imm16);
269 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800270 void Lwl(Register rt, Register rs, uint16_t imm16);
271 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700272 void Lbu(Register rt, Register rs, uint16_t imm16);
273 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700274 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700275 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700276 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200277 void Sync(uint32_t stype);
278 void Mfhi(Register rd); // R2
279 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700280
281 void Sb(Register rt, Register rs, uint16_t imm16);
282 void Sh(Register rt, Register rs, uint16_t imm16);
283 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800284 void Swl(Register rt, Register rs, uint16_t imm16);
285 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700286
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700287 void LlR2(Register rt, Register base, int16_t imm16 = 0);
288 void ScR2(Register rt, Register base, int16_t imm16 = 0);
289 void LlR6(Register rt, Register base, int16_t imm9 = 0);
290 void ScR6(Register rt, Register base, int16_t imm9 = 0);
291
jeffhao7fbee072012-08-24 17:56:54 -0700292 void Slt(Register rd, Register rs, Register rt);
293 void Sltu(Register rd, Register rs, Register rt);
294 void Slti(Register rt, Register rs, uint16_t imm16);
295 void Sltiu(Register rt, Register rs, uint16_t imm16);
296
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700297 // Branches and jumps to immediate offsets/addresses do not take care of their
298 // delay/forbidden slots and generally should not be used directly. This applies
299 // to the following R2 and R6 branch/jump instructions with imm16, imm21, addr26
300 // offsets/addresses.
301 // Use branches/jumps to labels instead.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200302 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700303 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200304 void Beq(Register rs, Register rt, uint16_t imm16);
305 void Bne(Register rs, Register rt, uint16_t imm16);
306 void Beqz(Register rt, uint16_t imm16);
307 void Bnez(Register rt, uint16_t imm16);
308 void Bltz(Register rt, uint16_t imm16);
309 void Bgez(Register rt, uint16_t imm16);
310 void Blez(Register rt, uint16_t imm16);
311 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800312 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800313 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800314 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800315 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200316 void J(uint32_t addr26);
317 void Jal(uint32_t addr26);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700318 // Jalr() and Jr() fill their delay slots when reordering is enabled.
319 // When reordering is disabled, the delay slots must be filled manually.
320 // You may use NopIfNoReordering() to fill them when reordering is disabled.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200321 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700322 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200323 void Jr(Register rs);
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700324 // Nal() does not fill its delay slot. It must be filled manually.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200325 void Nal();
326 void Auipc(Register rs, uint16_t imm16); // R6
327 void Addiupc(Register rs, uint32_t imm19); // R6
328 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700329 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200330 void Jic(Register rt, uint16_t imm16); // R6
331 void Jialc(Register rt, uint16_t imm16); // R6
332 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
333 void Bltzc(Register rt, uint16_t imm16); // R6
334 void Bgtzc(Register rt, uint16_t imm16); // R6
335 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
336 void Bgezc(Register rt, uint16_t imm16); // R6
337 void Blezc(Register rt, uint16_t imm16); // R6
338 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
339 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
340 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
341 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
342 void Beqzc(Register rs, uint32_t imm21); // R6
343 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800344 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
345 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700346
347 void AddS(FRegister fd, FRegister fs, FRegister ft);
348 void SubS(FRegister fd, FRegister fs, FRegister ft);
349 void MulS(FRegister fd, FRegister fs, FRegister ft);
350 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200351 void AddD(FRegister fd, FRegister fs, FRegister ft);
352 void SubD(FRegister fd, FRegister fs, FRegister ft);
353 void MulD(FRegister fd, FRegister fs, FRegister ft);
354 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800355 void SqrtS(FRegister fd, FRegister fs);
356 void SqrtD(FRegister fd, FRegister fs);
357 void AbsS(FRegister fd, FRegister fs);
358 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700359 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200360 void MovD(FRegister fd, FRegister fs);
361 void NegS(FRegister fd, FRegister fs);
362 void NegD(FRegister fd, FRegister fs);
363
Chris Larsenb74353a2015-11-20 09:07:09 -0800364 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800365 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800366 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800367 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800368 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800369 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800370 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800371 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800372 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800373 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800374 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800375 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800376 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800377 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800378 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800379 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800380 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800381 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800382 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800383 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800384 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800385 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800386 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800387 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800388 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800389 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800390 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800391 void CuleD(int cc, FRegister fs, FRegister ft); // R2
392 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
393 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
394 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
395 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
396 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
397 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
398 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
399 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
400 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
401 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
402 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
403 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
404 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
405 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
406 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
407 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
408 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
409 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
410 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
411 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800412 void Movf(Register rd, Register rs, int cc = 0); // R2
413 void Movt(Register rd, Register rs, int cc = 0); // R2
414 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
415 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
416 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
417 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700418 void MovzS(FRegister fd, FRegister fs, Register rt); // R2
419 void MovzD(FRegister fd, FRegister fs, Register rt); // R2
420 void MovnS(FRegister fd, FRegister fs, Register rt); // R2
421 void MovnD(FRegister fd, FRegister fs, Register rt); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800422 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
423 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700424 void SeleqzS(FRegister fd, FRegister fs, FRegister ft); // R6
425 void SeleqzD(FRegister fd, FRegister fs, FRegister ft); // R6
426 void SelnezS(FRegister fd, FRegister fs, FRegister ft); // R6
427 void SelnezD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800428 void ClassS(FRegister fd, FRegister fs); // R6
429 void ClassD(FRegister fd, FRegister fs); // R6
430 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
431 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
432 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
433 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800434
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800435 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
436 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
437 void TruncWS(FRegister fd, FRegister fs);
438 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200439 void Cvtsw(FRegister fd, FRegister fs);
440 void Cvtdw(FRegister fd, FRegister fs);
441 void Cvtsd(FRegister fd, FRegister fs);
442 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800443 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
444 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800445 void FloorWS(FRegister fd, FRegister fs);
446 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700447
448 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200449 void Mtc1(Register rt, FRegister fs);
450 void Mfhc1(Register rt, FRegister fs);
451 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800452 void MoveFromFpuHigh(Register rt, FRegister fs);
453 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700454 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200455 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700456 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200457 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700458
459 void Break();
jeffhao07030602012-09-26 14:33:14 -0700460 void Nop();
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700461 void NopIfNoReordering();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200462 void Move(Register rd, Register rs);
463 void Clear(Register rd);
464 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700465
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200466 // Higher level composite instructions.
467 void LoadConst32(Register rd, int32_t value);
468 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
469 void LoadDConst64(FRegister rd, int64_t value, Register temp);
470 void LoadSConst32(FRegister r, int32_t value, Register temp);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200471 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
472
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700473 // These will generate R2 branches or R6 branches as appropriate and take care of
474 // the delay/forbidden slots.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200475 void Bind(MipsLabel* label);
476 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700477 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200478 void Beq(Register rs, Register rt, MipsLabel* label);
479 void Bne(Register rs, Register rt, MipsLabel* label);
480 void Beqz(Register rt, MipsLabel* label);
481 void Bnez(Register rt, MipsLabel* label);
482 void Bltz(Register rt, MipsLabel* label);
483 void Bgez(Register rt, MipsLabel* label);
484 void Blez(Register rt, MipsLabel* label);
485 void Bgtz(Register rt, MipsLabel* label);
486 void Blt(Register rs, Register rt, MipsLabel* label);
487 void Bge(Register rs, Register rt, MipsLabel* label);
488 void Bltu(Register rs, Register rt, MipsLabel* label);
489 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800490 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800491 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800492 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800493 void Bc1t(int cc, MipsLabel* label); // R2
494 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
495 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700496
497 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700498 void AdjustBaseAndOffset(Register& base,
499 int32_t& offset,
500 bool is_doubleword,
501 bool is_float = false);
Alexey Frunze2923db72016-08-20 01:55:47 -0700502
503 private:
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100504 // This will be used as an argument for loads/stores
505 // when there is no need for implicit null checks.
Alexey Frunze2923db72016-08-20 01:55:47 -0700506 struct NoImplicitNullChecker {
Tijana Jakovljevic57433862017-01-17 16:59:03 +0100507 void operator()() const {}
Alexey Frunze2923db72016-08-20 01:55:47 -0700508 };
509
510 public:
511 template <typename ImplicitNullChecker = NoImplicitNullChecker>
Alexey Frunzef58b2482016-09-02 22:14:06 -0700512 void StoreConstToOffset(StoreOperandType type,
513 int64_t value,
514 Register base,
515 int32_t offset,
516 Register temp,
517 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
518 // We permit `base` and `temp` to coincide (however, we check that neither is AT),
519 // in which case the `base` register may be overwritten in the process.
Alexey Frunze2923db72016-08-20 01:55:47 -0700520 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
Alexey Frunzef58b2482016-09-02 22:14:06 -0700521 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
Alexey Frunze2923db72016-08-20 01:55:47 -0700522 uint32_t low = Low32Bits(value);
523 uint32_t high = High32Bits(value);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700524 Register reg;
525 // If the adjustment left `base` unchanged and equal to `temp`, we can't use `temp`
526 // to load and hold the value but we can use AT instead as AT hasn't been used yet.
527 // Otherwise, `temp` can be used for the value. And if `temp` is the same as the
528 // original `base` (that is, `base` prior to the adjustment), the original `base`
529 // register will be overwritten.
530 if (base == temp) {
531 temp = AT;
Alexey Frunze2923db72016-08-20 01:55:47 -0700532 }
Alexey Frunzef58b2482016-09-02 22:14:06 -0700533 if (low == 0) {
534 reg = ZERO;
Alexey Frunze2923db72016-08-20 01:55:47 -0700535 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -0700536 reg = temp;
537 LoadConst32(reg, low);
538 }
539 switch (type) {
540 case kStoreByte:
541 Sb(reg, base, offset);
542 break;
543 case kStoreHalfword:
544 Sh(reg, base, offset);
545 break;
546 case kStoreWord:
547 Sw(reg, base, offset);
548 break;
549 case kStoreDoubleword:
550 Sw(reg, base, offset);
551 null_checker();
552 if (high == 0) {
553 reg = ZERO;
554 } else {
555 reg = temp;
556 if (high != low) {
557 LoadConst32(reg, high);
558 }
559 }
560 Sw(reg, base, offset + kMipsWordSize);
561 break;
562 default:
563 LOG(FATAL) << "UNREACHABLE";
564 }
565 if (type != kStoreDoubleword) {
566 null_checker();
Alexey Frunze2923db72016-08-20 01:55:47 -0700567 }
568 }
569
570 template <typename ImplicitNullChecker = NoImplicitNullChecker>
571 void LoadFromOffset(LoadOperandType type,
572 Register reg,
573 Register base,
574 int32_t offset,
575 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
576 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
577 switch (type) {
578 case kLoadSignedByte:
579 Lb(reg, base, offset);
580 break;
581 case kLoadUnsignedByte:
582 Lbu(reg, base, offset);
583 break;
584 case kLoadSignedHalfword:
585 Lh(reg, base, offset);
586 break;
587 case kLoadUnsignedHalfword:
588 Lhu(reg, base, offset);
589 break;
590 case kLoadWord:
591 Lw(reg, base, offset);
592 break;
593 case kLoadDoubleword:
594 if (reg == base) {
595 // This will clobber the base when loading the lower register. Since we have to load the
596 // higher register as well, this will fail. Solution: reverse the order.
597 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
598 null_checker();
599 Lw(reg, base, offset);
600 } else {
601 Lw(reg, base, offset);
602 null_checker();
603 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
604 }
605 break;
606 default:
607 LOG(FATAL) << "UNREACHABLE";
608 }
609 if (type != kLoadDoubleword) {
610 null_checker();
611 }
612 }
613
614 template <typename ImplicitNullChecker = NoImplicitNullChecker>
615 void LoadSFromOffset(FRegister reg,
616 Register base,
617 int32_t offset,
618 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
619 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
620 Lwc1(reg, base, offset);
621 null_checker();
622 }
623
624 template <typename ImplicitNullChecker = NoImplicitNullChecker>
625 void LoadDFromOffset(FRegister reg,
626 Register base,
627 int32_t offset,
628 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
629 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
630 if (IsAligned<kMipsDoublewordSize>(offset)) {
631 Ldc1(reg, base, offset);
632 null_checker();
633 } else {
634 if (Is32BitFPU()) {
635 Lwc1(reg, base, offset);
636 null_checker();
637 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
638 } else {
639 // 64-bit FPU.
640 Lwc1(reg, base, offset);
641 null_checker();
642 Lw(T8, base, offset + kMipsWordSize);
643 Mthc1(T8, reg);
644 }
645 }
646 }
647
648 template <typename ImplicitNullChecker = NoImplicitNullChecker>
649 void StoreToOffset(StoreOperandType type,
650 Register reg,
651 Register base,
652 int32_t offset,
653 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
654 // Must not use AT as `reg`, so as not to overwrite the value being stored
655 // with the adjusted `base`.
656 CHECK_NE(reg, AT);
657 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
658 switch (type) {
659 case kStoreByte:
660 Sb(reg, base, offset);
661 break;
662 case kStoreHalfword:
663 Sh(reg, base, offset);
664 break;
665 case kStoreWord:
666 Sw(reg, base, offset);
667 break;
668 case kStoreDoubleword:
669 CHECK_NE(reg, base);
670 CHECK_NE(static_cast<Register>(reg + 1), base);
671 Sw(reg, base, offset);
672 null_checker();
673 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
674 break;
675 default:
676 LOG(FATAL) << "UNREACHABLE";
677 }
678 if (type != kStoreDoubleword) {
679 null_checker();
680 }
681 }
682
683 template <typename ImplicitNullChecker = NoImplicitNullChecker>
684 void StoreSToOffset(FRegister reg,
685 Register base,
686 int32_t offset,
687 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
688 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
689 Swc1(reg, base, offset);
690 null_checker();
691 }
692
693 template <typename ImplicitNullChecker = NoImplicitNullChecker>
694 void StoreDToOffset(FRegister reg,
695 Register base,
696 int32_t offset,
697 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
698 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
699 if (IsAligned<kMipsDoublewordSize>(offset)) {
700 Sdc1(reg, base, offset);
701 null_checker();
702 } else {
703 if (Is32BitFPU()) {
704 Swc1(reg, base, offset);
705 null_checker();
706 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
707 } else {
708 // 64-bit FPU.
709 Mfhc1(T8, reg);
710 Swc1(reg, base, offset);
711 null_checker();
712 Sw(T8, base, offset + kMipsWordSize);
713 }
714 }
715 }
716
jeffhao7fbee072012-08-24 17:56:54 -0700717 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
718 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200719 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700720 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000721 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200722 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700723
jeffhao7fbee072012-08-24 17:56:54 -0700724 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200725 void Emit(uint32_t value);
726
727 // Push/pop composite routines.
728 void Push(Register rs);
729 void Pop(Register rd);
730 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700731
Alexey Frunzec061de12017-02-14 13:27:23 -0800732 //
733 // Heap poisoning.
734 //
735
736 // Poison a heap reference contained in `src` and store it in `dst`.
737 void PoisonHeapReference(Register dst, Register src) {
738 // dst = -src.
739 Subu(dst, ZERO, src);
740 }
741 // Poison a heap reference contained in `reg`.
742 void PoisonHeapReference(Register reg) {
743 // reg = -reg.
744 PoisonHeapReference(reg, reg);
745 }
746 // Unpoison a heap reference contained in `reg`.
747 void UnpoisonHeapReference(Register reg) {
748 // reg = -reg.
749 Subu(reg, ZERO, reg);
750 }
751 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
752 void MaybePoisonHeapReference(Register reg) {
753 if (kPoisonHeapReferences) {
754 PoisonHeapReference(reg);
755 }
756 }
757 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
758 void MaybeUnpoisonHeapReference(Register reg) {
759 if (kPoisonHeapReferences) {
760 UnpoisonHeapReference(reg);
761 }
762 }
763
Andreas Gampe85b62f22015-09-09 13:15:38 -0700764 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200765 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700766 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200767 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
768 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700769 }
770
Igor Murashkinae7ff922016-10-06 14:59:19 -0700771 // Don't warn about a different virtual Bind/Jump in the base class.
772 using JNIBase::Bind;
773 using JNIBase::Jump;
774
775 // Create a new label that can be used with Jump/Bind calls.
776 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
777 LOG(FATAL) << "Not implemented on MIPS32";
778 UNREACHABLE();
779 }
780 // Emit an unconditional jump to the label.
781 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
782 LOG(FATAL) << "Not implemented on MIPS32";
783 UNREACHABLE();
784 }
785 // Emit a conditional jump to the label by applying a unary condition test to the register.
786 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
787 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
788 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
789 LOG(FATAL) << "Not implemented on MIPS32";
790 UNREACHABLE();
791 }
792
793 // Code at this offset will serve as the target for the Jump call.
794 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
795 LOG(FATAL) << "Not implemented on MIPS32";
796 UNREACHABLE();
797 }
798
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700799 // Create a new literal with a given value.
800 // NOTE: Force the template parameter to be explicitly specified.
801 template <typename T>
802 Literal* NewLiteral(typename Identity<T>::type value) {
803 static_assert(std::is_integral<T>::value, "T must be an integral type.");
804 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
805 }
806
Alexey Frunze96b66822016-09-10 02:32:44 -0700807 // Load label address using the base register (for R2 only) or using PC-relative loads
808 // (for R6 only; base_reg must be ZERO). To be used with data labels in the literal /
809 // jump table area only and not with regular code labels.
810 void LoadLabelAddress(Register dest_reg, Register base_reg, MipsLabel* label);
811
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700812 // Create a new literal with the given data.
813 Literal* NewLiteral(size_t size, const uint8_t* data);
814
815 // Load literal using the base register (for R2 only) or using PC-relative loads
816 // (for R6 only; base_reg must be ZERO).
817 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
818
Alexey Frunze96b66822016-09-10 02:32:44 -0700819 // Create a jump table for the given labels that will be emitted when finalizing.
820 // When the table is emitted, offsets will be relative to the location of the table.
821 // The table location is determined by the location of its label (the label precedes
822 // the table data) and should be loaded using LoadLabelAddress().
823 JumpTable* CreateJumpTable(std::vector<MipsLabel*>&& labels);
824
jeffhao7fbee072012-08-24 17:56:54 -0700825 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200826 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700827 //
828
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200829 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800830 void BuildFrame(size_t frame_size,
831 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100832 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700833 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700834
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200835 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100836 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700837 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700838
Ian Rogersdd7624d2014-03-14 17:43:00 -0700839 void IncreaseFrameSize(size_t adjust) OVERRIDE;
840 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700841
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200842 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700843 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
844 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
845 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700846
Ian Rogersdd7624d2014-03-14 17:43:00 -0700847 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700848
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700849 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
850 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800851 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700852
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700853 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700854
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800855 void StoreSpanning(FrameOffset dest,
856 ManagedRegister msrc,
857 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700858 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700859
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200860 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700861 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700862
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700863 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700864
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700866
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800867 void LoadRef(ManagedRegister mdest,
868 ManagedRegister base,
869 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100870 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700871
Ian Rogersdd7624d2014-03-14 17:43:00 -0700872 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700873
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700874 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700875
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200876 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700877 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700878
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700879 void CopyRawPtrFromThread(FrameOffset fr_offs,
880 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700881 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700882
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700883 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
884 FrameOffset fr_offs,
885 ManagedRegister mscratch) OVERRIDE;
886
Ian Rogersdd7624d2014-03-14 17:43:00 -0700887 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700888
Ian Rogersdd7624d2014-03-14 17:43:00 -0700889 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700890
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800891 void Copy(FrameOffset dest,
892 ManagedRegister src_base,
893 Offset src_offset,
894 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700895 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700896
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800897 void Copy(ManagedRegister dest_base,
898 Offset dest_offset,
899 FrameOffset src,
900 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700901 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700902
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800903 void Copy(FrameOffset dest,
904 FrameOffset src_base,
905 Offset src_offset,
906 ManagedRegister mscratch,
907 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700908
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800909 void Copy(ManagedRegister dest,
910 Offset dest_offset,
911 ManagedRegister src,
912 Offset src_offset,
913 ManagedRegister mscratch,
914 size_t size) OVERRIDE;
915
916 void Copy(FrameOffset dest,
917 Offset dest_offset,
918 FrameOffset src,
919 Offset src_offset,
920 ManagedRegister mscratch,
921 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700922
Ian Rogersdd7624d2014-03-14 17:43:00 -0700923 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700924
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200925 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700926 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700927
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200928 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700929 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700930
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200931 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700932 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
933 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700934
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700935 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700936 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700937 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700938 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800939 void CreateHandleScopeEntry(ManagedRegister out_reg,
940 FrameOffset handlescope_offset,
941 ManagedRegister in_reg,
942 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700943
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700944 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700945 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800946 void CreateHandleScopeEntry(FrameOffset out_off,
947 FrameOffset handlescope_offset,
948 ManagedRegister mscratch,
949 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700950
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200951 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700952 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700953
954 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
955 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700956 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
957 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700958
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200959 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700960 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
961 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700962 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700963
jeffhao7fbee072012-08-24 17:56:54 -0700964 // Generate code to check if Thread::Current()->exception_ is non-null
965 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700966 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700967
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200968 // Emit slow paths queued during assembly and promote short branches to long if needed.
969 void FinalizeCode() OVERRIDE;
970
971 // Emit branches and finalize all instructions.
972 void FinalizeInstructions(const MemoryRegion& region);
973
974 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
975 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700976 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200977
978 // Get the final position of a label after local fixup based on the old position
979 // recorded before FinalizeCode().
980 uint32_t GetAdjustedPosition(uint32_t old_position);
981
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700982 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
983 // reading the PC value into a general-purpose register with the NAL instruction and then loading
984 // literals through this base register. The code generator calls this method (at most once per
985 // method being compiled) to bind a label to the location for which the PC value is acquired.
986 // The assembler then computes literal offsets relative to this label.
987 void BindPcRelBaseLabel();
988
Alexey Frunze06a46c42016-07-19 15:00:40 -0700989 // Returns the location of the label bound with BindPcRelBaseLabel().
990 uint32_t GetPcRelBaseLabelLocation() const;
991
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992 // Note that PC-relative literal loads are handled as pseudo branches because they need very
993 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
994 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200995 enum BranchCondition {
996 kCondLT,
997 kCondGE,
998 kCondLE,
999 kCondGT,
1000 kCondLTZ,
1001 kCondGEZ,
1002 kCondLEZ,
1003 kCondGTZ,
1004 kCondEQ,
1005 kCondNE,
1006 kCondEQZ,
1007 kCondNEZ,
1008 kCondLTU,
1009 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001010 kCondF, // Floating-point predicate false.
1011 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001012 kUncond,
1013 };
1014 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
1015
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001016 // Enables or disables instruction reordering (IOW, automatic filling of delay slots)
1017 // similarly to ".set reorder" / ".set noreorder" in traditional MIPS assembly.
1018 // Returns the last state, which may be useful for temporary enabling/disabling of
1019 // reordering.
1020 bool SetReorder(bool enable);
1021
jeffhao7fbee072012-08-24 17:56:54 -07001022 private:
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001023 // Description of the last instruction in terms of input and output registers.
1024 // Used to make the decision of moving the instruction into a delay slot.
1025 struct DelaySlot {
1026 DelaySlot();
1027 // Encoded instruction that may be used to fill the delay slot or 0
1028 // (0 conveniently represents NOP).
1029 uint32_t instruction_;
1030 // Mask of output GPRs for the instruction.
1031 uint32_t gpr_outs_mask_;
1032 // Mask of input GPRs for the instruction.
1033 uint32_t gpr_ins_mask_;
1034 // Mask of output FPRs for the instruction.
1035 uint32_t fpr_outs_mask_;
1036 // Mask of input FPRs for the instruction.
1037 uint32_t fpr_ins_mask_;
1038 // Mask of output FPU condition code flags for the instruction.
1039 uint32_t cc_outs_mask_;
1040 // Mask of input FPU condition code flags for the instruction.
1041 uint32_t cc_ins_mask_;
1042 // Branches never operate on the LO and HI registers, hence there's
1043 // no mask for LO and HI.
1044 };
1045
1046 // Delay slot finite state machine's (DS FSM's) state. The FSM state is updated
1047 // upon every new instruction and label generated. The FSM detects instructions
1048 // suitable for delay slots and immediately preceded with labels. These are target
1049 // instructions for branches. If an unconditional R2 branch does not get its delay
1050 // slot filled with the immediately preceding instruction, it may instead get the
1051 // slot filled with the target instruction (the branch will need its offset
1052 // incremented past the target instruction). We call this "absorption". The FSM
1053 // records PCs of the target instructions suitable for this optimization.
1054 enum DsFsmState {
1055 kExpectingLabel,
1056 kExpectingInstruction,
1057 kExpectingCommit
1058 };
1059 friend std::ostream& operator<<(std::ostream& os, const DsFsmState& rhs);
1060
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001061 class Branch {
1062 public:
1063 enum Type {
1064 // R2 short branches.
1065 kUncondBranch,
1066 kCondBranch,
1067 kCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001068 // R2 near label.
1069 kLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001070 // R2 near literal.
1071 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001072 // R2 long branches.
1073 kLongUncondBranch,
1074 kLongCondBranch,
1075 kLongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001076 // R2 far label.
1077 kFarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001078 // R2 far literal.
1079 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001080 // R6 short branches.
1081 kR6UncondBranch,
1082 kR6CondBranch,
1083 kR6Call,
Alexey Frunze96b66822016-09-10 02:32:44 -07001084 // R6 near label.
1085 kR6Label,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001086 // R6 near literal.
1087 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001088 // R6 long branches.
1089 kR6LongUncondBranch,
1090 kR6LongCondBranch,
1091 kR6LongCall,
Alexey Frunze96b66822016-09-10 02:32:44 -07001092 // R6 far label.
1093 kR6FarLabel,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001094 // R6 far literal.
1095 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001096 };
1097 // Bit sizes of offsets defined as enums to minimize chance of typos.
1098 enum OffsetBits {
1099 kOffset16 = 16,
1100 kOffset18 = 18,
1101 kOffset21 = 21,
1102 kOffset23 = 23,
1103 kOffset28 = 28,
1104 kOffset32 = 32,
1105 };
1106
1107 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
1108 static constexpr int32_t kMaxBranchLength = 32;
1109 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001110 // The following two instruction encodings can never legally occur in branch delay
1111 // slots and are used as markers.
1112 //
1113 // kUnfilledDelaySlot means that the branch may use either the preceding or the target
1114 // instruction to fill its delay slot (the latter is only possible with unconditional
1115 // R2 branches and is termed here as "absorption").
1116 static constexpr uint32_t kUnfilledDelaySlot = 0x10000000; // beq zero, zero, 0.
1117 // kUnfillableDelaySlot means that the branch cannot use an instruction (other than NOP)
1118 // to fill its delay slot. This is only used for unconditional R2 branches to prevent
1119 // absorption of the target instruction when reordering is disabled.
1120 static constexpr uint32_t kUnfillableDelaySlot = 0x13FF0000; // beq ra, ra, 0.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001121
1122 struct BranchInfo {
1123 // Branch length as a number of 4-byte-long instructions.
1124 uint32_t length;
1125 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
1126 // PC-relative offset (or its most significant 16-bit half, which goes first).
1127 uint32_t instr_offset;
1128 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
1129 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
1130 // instructions) from the instruction containing the offset.
1131 uint32_t pc_org;
1132 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
1133 // is an exception: use kOffset23 for beqzc/bnezc).
1134 OffsetBits offset_size;
1135 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
1136 // count.
1137 int offset_shift;
1138 };
1139 static const BranchInfo branch_info_[/* Type */];
1140
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001141 // Unconditional branch or call.
1142 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001143 // Conditional branch.
1144 Branch(bool is_r6,
1145 uint32_t location,
1146 uint32_t target,
1147 BranchCondition condition,
1148 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001149 Register rhs_reg);
Alexey Frunze96b66822016-09-10 02:32:44 -07001150 // Label address (in literal area) or literal.
1151 Branch(bool is_r6,
1152 uint32_t location,
1153 Register dest_reg,
1154 Register base_reg,
1155 Type label_or_literal_type);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001156
1157 // Some conditional branches with lhs = rhs are effectively NOPs, while some
1158 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
1159 // So, we need a way to identify such branches in order to emit no instructions for them
1160 // or change them to unconditional.
1161 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
1162 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
1163
1164 static BranchCondition OppositeCondition(BranchCondition cond);
1165
1166 Type GetType() const;
1167 BranchCondition GetCondition() const;
1168 Register GetLeftRegister() const;
1169 Register GetRightRegister() const;
1170 uint32_t GetTarget() const;
1171 uint32_t GetLocation() const;
1172 uint32_t GetOldLocation() const;
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001173 uint32_t GetPrecedingInstructionLength(Type type) const;
1174 uint32_t GetPrecedingInstructionSize(Type type) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001175 uint32_t GetLength() const;
1176 uint32_t GetOldLength() const;
1177 uint32_t GetSize() const;
1178 uint32_t GetOldSize() const;
1179 uint32_t GetEndLocation() const;
1180 uint32_t GetOldEndLocation() const;
1181 bool IsLong() const;
1182 bool IsResolved() const;
1183
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001184 // Various helpers for branch delay slot management.
1185 bool CanHaveDelayedInstruction(const DelaySlot& delay_slot) const;
1186 void SetDelayedInstruction(uint32_t instruction);
1187 uint32_t GetDelayedInstruction() const;
1188 void DecrementLocations();
1189
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001190 // Returns the bit size of the signed offset that the branch instruction can handle.
1191 OffsetBits GetOffsetSize() const;
1192
1193 // Calculates the distance between two byte locations in the assembler buffer and
1194 // returns the number of bits needed to represent the distance as a signed integer.
1195 //
1196 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
1197 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
1198 //
1199 // Composite branches (made of several instructions) with longer reach have 32-bit
1200 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001201 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
1202 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
1203 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
1204 // Consider the following implementation of a long unconditional branch, for
1205 // example:
1206 //
1207 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
1208 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
1209 //
1210 // Both of the above instructions take 16-bit signed offsets as immediate operands.
1211 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
1212 // due to sign extension. This must be compensated for by incrementing offset_31_16
1213 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
1214 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
1215 // Therefore, the long branch range is something like from PC - 0x80000000 to
1216 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001217 //
1218 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
1219 // case with the addiu instruction and a 16 bit offset.
1220 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1221
1222 // Resolve a branch when the target is known.
1223 void Resolve(uint32_t target);
1224
1225 // Relocate a branch by a given delta if needed due to expansion of this or another
1226 // branch at a given location by this delta (just changes location_ and target_).
1227 void Relocate(uint32_t expand_location, uint32_t delta);
1228
1229 // If the branch is short, changes its type to long.
1230 void PromoteToLong();
1231
1232 // If necessary, updates the type by promoting a short branch to a long branch
1233 // based on the branch location and target. Returns the amount (in bytes) by
1234 // which the branch size has increased.
1235 // max_short_distance caps the maximum distance between location_ and target_
1236 // that is allowed for short branches. This is for debugging/testing purposes.
1237 // max_short_distance = 0 forces all short branches to become long.
1238 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001239 uint32_t PromoteIfNeeded(uint32_t location,
1240 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001241
1242 // Returns the location of the instruction(s) containing the offset.
1243 uint32_t GetOffsetLocation() const;
1244
1245 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001246 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001247
1248 private:
1249 // Completes branch construction by determining and recording its type.
Alexey Frunze96b66822016-09-10 02:32:44 -07001250 void InitializeType(Type initial_type, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001251 // Helper for the above.
1252 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1253
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001254 uint32_t old_location_; // Offset into assembler buffer in bytes.
1255 uint32_t location_; // Offset into assembler buffer in bytes.
1256 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001257
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001258 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1259 // FPU condition code. Destination register in literals.
1260 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1261 // Base register in literals (ZERO on R6).
1262 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001263
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001264 Type type_; // Current type of the branch.
1265 Type old_type_; // Initial type of the branch.
1266
1267 uint32_t delayed_instruction_; // Encoded instruction for the delay slot or
1268 // kUnfilledDelaySlot if none but fillable or
1269 // kUnfillableDelaySlot if none and unfillable
1270 // (the latter is only used for unconditional R2
1271 // branches).
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001272 };
1273 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1274 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1275
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001276 uint32_t EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1277 uint32_t EmitI(int opcode, Register rs, Register rt, uint16_t imm);
1278 uint32_t EmitI21(int opcode, Register rs, uint32_t imm21);
1279 uint32_t EmitI26(int opcode, uint32_t imm26);
1280 uint32_t EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1281 uint32_t EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001282 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1283 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001284
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001285 void Buncond(MipsLabel* label);
1286 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001287 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001288 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001289
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001290 // Various helpers for branch delay slot management.
1291 void DsFsmInstr(uint32_t instruction,
1292 uint32_t gpr_outs_mask,
1293 uint32_t gpr_ins_mask,
1294 uint32_t fpr_outs_mask,
1295 uint32_t fpr_ins_mask,
1296 uint32_t cc_outs_mask,
1297 uint32_t cc_ins_mask);
1298 void DsFsmInstrNop(uint32_t instruction);
1299 void DsFsmInstrRrr(uint32_t instruction, Register out, Register in1, Register in2);
1300 void DsFsmInstrRrrr(uint32_t instruction, Register in1_out, Register in2, Register in3);
1301 void DsFsmInstrFff(uint32_t instruction, FRegister out, FRegister in1, FRegister in2);
1302 void DsFsmInstrFfff(uint32_t instruction, FRegister in1_out, FRegister in2, FRegister in3);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07001303 void DsFsmInstrFffr(uint32_t instruction, FRegister in1_out, FRegister in2, Register in3);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001304 void DsFsmInstrRf(uint32_t instruction, Register out, FRegister in);
1305 void DsFsmInstrFr(uint32_t instruction, FRegister out, Register in);
1306 void DsFsmInstrFR(uint32_t instruction, FRegister in1, Register in2);
1307 void DsFsmInstrCff(uint32_t instruction, int cc_out, FRegister in1, FRegister in2);
1308 void DsFsmInstrRrrc(uint32_t instruction, Register in1_out, Register in2, int cc_in);
1309 void DsFsmInstrFffc(uint32_t instruction, FRegister in1_out, FRegister in2, int cc_in);
1310 void DsFsmLabel();
1311 void DsFsmCommitLabel();
1312 void DsFsmDropLabel();
1313 void MoveInstructionToDelaySlot(Branch& branch);
1314 bool CanExchangeWithSlt(Register rs, Register rt) const;
1315 void ExchangeWithSlt(const DelaySlot& forwarded_slot);
1316 void GenerateSltForCondBranch(bool unsigned_slt, Register rs, Register rt);
1317
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001318 Branch* GetBranch(uint32_t branch_id);
1319 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001320 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1321 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001322
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001323 void EmitLiterals();
Alexey Frunze96b66822016-09-10 02:32:44 -07001324 void ReserveJumpTableSpace();
1325 void EmitJumpTables();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001326 void PromoteBranches();
1327 void EmitBranch(Branch* branch);
1328 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001329 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001330
1331 // Emits exception block.
1332 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1333
1334 bool IsR6() const {
1335 if (isa_features_ != nullptr) {
1336 return isa_features_->IsR6();
1337 } else {
1338 return false;
1339 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001340 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001341
1342 bool Is32BitFPU() const {
1343 if (isa_features_ != nullptr) {
1344 return isa_features_->Is32BitFloatingPoint();
1345 } else {
1346 return true;
1347 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001348 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001349
1350 // List of exception blocks to generate at the end of the code cache.
1351 std::vector<MipsExceptionSlowPath> exception_blocks_;
1352
1353 std::vector<Branch> branches_;
1354
1355 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1356 bool overwriting_;
1357 // The current overwrite location.
1358 uint32_t overwrite_location_;
1359
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001360 // Whether instruction reordering (IOW, automatic filling of delay slots) is enabled.
1361 bool reordering_;
1362 // Information about the last instruction that may be used to fill a branch delay slot.
1363 DelaySlot delay_slot_;
1364 // Delay slot FSM state.
1365 DsFsmState ds_fsm_state_;
1366 // PC of the current labeled target instruction.
1367 uint32_t ds_fsm_target_pc_;
1368 // PCs of labeled target instructions.
1369 std::vector<uint32_t> ds_fsm_target_pcs_;
1370
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001371 // Use std::deque<> for literal labels to allow insertions at the end
1372 // without invalidating pointers and references to existing elements.
1373 ArenaDeque<Literal> literals_;
1374
Alexey Frunze96b66822016-09-10 02:32:44 -07001375 // Jump table list.
1376 ArenaDeque<JumpTable> jump_tables_;
1377
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001378 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1379 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1380 // that PC (from NAL) points to.
1381 MipsLabel pc_rel_base_label_;
1382
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001383 // Data for GetAdjustedPosition(), see the description there.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001384 uint32_t last_position_adjustment_;
1385 uint32_t last_old_position_;
1386 uint32_t last_branch_id_;
1387
1388 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001389
jeffhao7fbee072012-08-24 17:56:54 -07001390 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1391};
1392
jeffhao7fbee072012-08-24 17:56:54 -07001393} // namespace mips
1394} // namespace art
1395
Ian Rogers166db042013-07-26 12:05:57 -07001396#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_