blob: 852ced6e255ba2b992bfc598bd50346ea8f1b215 [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"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070027#include "constants_mips.h"
28#include "globals.h"
29#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070030#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020031#include "utils/assembler.h"
32#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070033
34namespace art {
35namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070036
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020037static constexpr size_t kMipsWordSize = 4;
38static constexpr size_t kMipsDoublewordSize = 8;
39
jeffhao7fbee072012-08-24 17:56:54 -070040enum LoadOperandType {
41 kLoadSignedByte,
42 kLoadUnsignedByte,
43 kLoadSignedHalfword,
44 kLoadUnsignedHalfword,
45 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020046 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070047};
48
49enum StoreOperandType {
50 kStoreByte,
51 kStoreHalfword,
52 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020053 kStoreDoubleword
54};
55
Chris Larsenb74353a2015-11-20 09:07:09 -080056// Used to test the values returned by ClassS/ClassD.
57enum FPClassMaskType {
58 kSignalingNaN = 0x001,
59 kQuietNaN = 0x002,
60 kNegativeInfinity = 0x004,
61 kNegativeNormal = 0x008,
62 kNegativeSubnormal = 0x010,
63 kNegativeZero = 0x020,
64 kPositiveInfinity = 0x040,
65 kPositiveNormal = 0x080,
66 kPositiveSubnormal = 0x100,
67 kPositiveZero = 0x200,
68};
69
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020070class MipsLabel : public Label {
71 public:
72 MipsLabel() : prev_branch_id_plus_one_(0) {}
73
74 MipsLabel(MipsLabel&& src)
75 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
76
77 private:
78 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
79
80 friend class MipsAssembler;
81 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
82};
83
Alexey Frunzee3fb2452016-05-10 16:08:05 -070084// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
85class Literal {
86 public:
87 static constexpr size_t kMaxSize = 8;
88
89 Literal(uint32_t size, const uint8_t* data)
90 : label_(), size_(size) {
91 DCHECK_LE(size, Literal::kMaxSize);
92 memcpy(data_, data, size);
93 }
94
95 template <typename T>
96 T GetValue() const {
97 DCHECK_EQ(size_, sizeof(T));
98 T value;
99 memcpy(&value, data_, sizeof(T));
100 return value;
101 }
102
103 uint32_t GetSize() const {
104 return size_;
105 }
106
107 const uint8_t* GetData() const {
108 return data_;
109 }
110
111 MipsLabel* GetLabel() {
112 return &label_;
113 }
114
115 const MipsLabel* GetLabel() const {
116 return &label_;
117 }
118
119 private:
120 MipsLabel label_;
121 const uint32_t size_;
122 uint8_t data_[kMaxSize];
123
124 DISALLOW_COPY_AND_ASSIGN(Literal);
125};
126
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200127// Slowpath entered when Thread::Current()->_exception is non-null.
128class MipsExceptionSlowPath {
129 public:
130 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
131 : scratch_(scratch), stack_adjust_(stack_adjust) {}
132
133 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800134 : scratch_(src.scratch_),
135 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200136 exception_entry_(std::move(src.exception_entry_)) {}
137
138 private:
139 MipsLabel* Entry() { return &exception_entry_; }
140 const MipsManagedRegister scratch_;
141 const size_t stack_adjust_;
142 MipsLabel exception_entry_;
143
144 friend class MipsAssembler;
145 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700146};
147
Ian Rogersdd7624d2014-03-14 17:43:00 -0700148class MipsAssembler FINAL : public Assembler {
jeffhao7fbee072012-08-24 17:56:54 -0700149 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100150 explicit MipsAssembler(ArenaAllocator* arena,
151 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
152 : Assembler(arena),
153 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200154 overwrite_location_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700155 literals_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200156 last_position_adjustment_(0),
157 last_old_position_(0),
158 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100159 isa_features_(instruction_set_features) {
160 cfi().DelayEmittingAdvancePCs();
161 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200162
163 virtual ~MipsAssembler() {
164 for (auto& branch : branches_) {
165 CHECK(branch.IsResolved());
166 }
167 }
jeffhao7fbee072012-08-24 17:56:54 -0700168
169 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700170 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700171 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700172 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200173
174 void MultR2(Register rs, Register rt); // R2
175 void MultuR2(Register rs, Register rt); // R2
176 void DivR2(Register rs, Register rt); // R2
177 void DivuR2(Register rs, Register rt); // R2
178 void MulR2(Register rd, Register rs, Register rt); // R2
179 void DivR2(Register rd, Register rs, Register rt); // R2
180 void ModR2(Register rd, Register rs, Register rt); // R2
181 void DivuR2(Register rd, Register rs, Register rt); // R2
182 void ModuR2(Register rd, Register rs, Register rt); // R2
183 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800184 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200185 void MuhuR6(Register rd, Register rs, Register rt); // R6
186 void DivR6(Register rd, Register rs, Register rt); // R6
187 void ModR6(Register rd, Register rs, Register rt); // R6
188 void DivuR6(Register rd, Register rs, Register rt); // R6
189 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700190
191 void And(Register rd, Register rs, Register rt);
192 void Andi(Register rt, Register rs, uint16_t imm16);
193 void Or(Register rd, Register rs, Register rt);
194 void Ori(Register rt, Register rs, uint16_t imm16);
195 void Xor(Register rd, Register rs, Register rt);
196 void Xori(Register rt, Register rs, uint16_t imm16);
197 void Nor(Register rd, Register rs, Register rt);
198
Chris Larsene3845472015-11-18 12:27:15 -0800199 void Movz(Register rd, Register rs, Register rt); // R2
200 void Movn(Register rd, Register rs, Register rt); // R2
201 void Seleqz(Register rd, Register rs, Register rt); // R6
202 void Selnez(Register rd, Register rs, Register rt); // R6
203 void ClzR6(Register rd, Register rs);
204 void ClzR2(Register rd, Register rs);
205 void CloR6(Register rd, Register rs);
206 void CloR2(Register rd, Register rs);
207
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200208 void Seb(Register rd, Register rt); // R2+
209 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700210 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800211 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200212
213 void Sll(Register rd, Register rt, int shamt);
214 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700215 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200216 void Sra(Register rd, Register rt, int shamt);
217 void Sllv(Register rd, Register rt, Register rs);
218 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800219 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200220 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800221 void Ext(Register rd, Register rt, int pos, int size); // R2+
222 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700223
224 void Lb(Register rt, Register rs, uint16_t imm16);
225 void Lh(Register rt, Register rs, uint16_t imm16);
226 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800227 void Lwl(Register rt, Register rs, uint16_t imm16);
228 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700229 void Lbu(Register rt, Register rs, uint16_t imm16);
230 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700231 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700232 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700233 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200234 void Sync(uint32_t stype);
235 void Mfhi(Register rd); // R2
236 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700237
238 void Sb(Register rt, Register rs, uint16_t imm16);
239 void Sh(Register rt, Register rs, uint16_t imm16);
240 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800241 void Swl(Register rt, Register rs, uint16_t imm16);
242 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700243
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700244 void LlR2(Register rt, Register base, int16_t imm16 = 0);
245 void ScR2(Register rt, Register base, int16_t imm16 = 0);
246 void LlR6(Register rt, Register base, int16_t imm9 = 0);
247 void ScR6(Register rt, Register base, int16_t imm9 = 0);
248
jeffhao7fbee072012-08-24 17:56:54 -0700249 void Slt(Register rd, Register rs, Register rt);
250 void Sltu(Register rd, Register rs, Register rt);
251 void Slti(Register rt, Register rs, uint16_t imm16);
252 void Sltiu(Register rt, Register rs, uint16_t imm16);
253
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200254 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700255 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200256 void Beq(Register rs, Register rt, uint16_t imm16);
257 void Bne(Register rs, Register rt, uint16_t imm16);
258 void Beqz(Register rt, uint16_t imm16);
259 void Bnez(Register rt, uint16_t imm16);
260 void Bltz(Register rt, uint16_t imm16);
261 void Bgez(Register rt, uint16_t imm16);
262 void Blez(Register rt, uint16_t imm16);
263 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800264 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800265 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800266 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800267 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200268 void J(uint32_t addr26);
269 void Jal(uint32_t addr26);
270 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700271 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200272 void Jr(Register rs);
273 void Nal();
274 void Auipc(Register rs, uint16_t imm16); // R6
275 void Addiupc(Register rs, uint32_t imm19); // R6
276 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700277 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200278 void Jic(Register rt, uint16_t imm16); // R6
279 void Jialc(Register rt, uint16_t imm16); // R6
280 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
281 void Bltzc(Register rt, uint16_t imm16); // R6
282 void Bgtzc(Register rt, uint16_t imm16); // R6
283 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
284 void Bgezc(Register rt, uint16_t imm16); // R6
285 void Blezc(Register rt, uint16_t imm16); // R6
286 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
287 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
288 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
289 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
290 void Beqzc(Register rs, uint32_t imm21); // R6
291 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800292 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
293 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700294
295 void AddS(FRegister fd, FRegister fs, FRegister ft);
296 void SubS(FRegister fd, FRegister fs, FRegister ft);
297 void MulS(FRegister fd, FRegister fs, FRegister ft);
298 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200299 void AddD(FRegister fd, FRegister fs, FRegister ft);
300 void SubD(FRegister fd, FRegister fs, FRegister ft);
301 void MulD(FRegister fd, FRegister fs, FRegister ft);
302 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800303 void SqrtS(FRegister fd, FRegister fs);
304 void SqrtD(FRegister fd, FRegister fs);
305 void AbsS(FRegister fd, FRegister fs);
306 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700307 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200308 void MovD(FRegister fd, FRegister fs);
309 void NegS(FRegister fd, FRegister fs);
310 void NegD(FRegister fd, FRegister fs);
311
Chris Larsenb74353a2015-11-20 09:07:09 -0800312 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800313 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800314 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800315 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800316 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800317 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800318 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800319 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800320 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800321 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800322 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800323 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800324 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800325 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800326 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800327 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800328 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800329 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800330 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800331 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800332 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800333 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800334 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800335 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800336 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800337 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800338 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800339 void CuleD(int cc, FRegister fs, FRegister ft); // R2
340 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
341 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
342 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
343 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
344 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
345 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
346 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
347 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
348 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
349 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
350 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
351 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
352 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
353 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
354 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
355 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
356 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
357 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
358 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
359 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800360 void Movf(Register rd, Register rs, int cc = 0); // R2
361 void Movt(Register rd, Register rs, int cc = 0); // R2
362 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
363 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
364 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
365 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
366 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
367 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
368 void ClassS(FRegister fd, FRegister fs); // R6
369 void ClassD(FRegister fd, FRegister fs); // R6
370 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
371 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
372 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
373 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800374
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800375 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
376 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
377 void TruncWS(FRegister fd, FRegister fs);
378 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200379 void Cvtsw(FRegister fd, FRegister fs);
380 void Cvtdw(FRegister fd, FRegister fs);
381 void Cvtsd(FRegister fd, FRegister fs);
382 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800383 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
384 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800385 void FloorWS(FRegister fd, FRegister fs);
386 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700387
388 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200389 void Mtc1(Register rt, FRegister fs);
390 void Mfhc1(Register rt, FRegister fs);
391 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800392 void MoveFromFpuHigh(Register rt, FRegister fs);
393 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700394 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200395 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700396 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200397 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700398
399 void Break();
jeffhao07030602012-09-26 14:33:14 -0700400 void Nop();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200401 void Move(Register rd, Register rs);
402 void Clear(Register rd);
403 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700404
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200405 // Higher level composite instructions.
406 void LoadConst32(Register rd, int32_t value);
407 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
408 void LoadDConst64(FRegister rd, int64_t value, Register temp);
409 void LoadSConst32(FRegister r, int32_t value, Register temp);
410 void StoreConst32ToOffset(int32_t value, Register base, int32_t offset, Register temp);
411 void StoreConst64ToOffset(int64_t value, Register base, int32_t offset, Register temp);
412 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
413
414 // These will generate R2 branches or R6 branches as appropriate.
415 void Bind(MipsLabel* label);
416 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700417 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200418 void Beq(Register rs, Register rt, MipsLabel* label);
419 void Bne(Register rs, Register rt, MipsLabel* label);
420 void Beqz(Register rt, MipsLabel* label);
421 void Bnez(Register rt, MipsLabel* label);
422 void Bltz(Register rt, MipsLabel* label);
423 void Bgez(Register rt, MipsLabel* label);
424 void Blez(Register rt, MipsLabel* label);
425 void Bgtz(Register rt, MipsLabel* label);
426 void Blt(Register rs, Register rt, MipsLabel* label);
427 void Bge(Register rs, Register rt, MipsLabel* label);
428 void Bltu(Register rs, Register rt, MipsLabel* label);
429 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800430 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800431 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800432 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800433 void Bc1t(int cc, MipsLabel* label); // R2
434 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
435 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700436
437 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700438 void AdjustBaseAndOffset(Register& base,
439 int32_t& offset,
440 bool is_doubleword,
441 bool is_float = false);
jeffhao7fbee072012-08-24 17:56:54 -0700442 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
443 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200444 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700445 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000446 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200447 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700448
jeffhao7fbee072012-08-24 17:56:54 -0700449 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200450 void Emit(uint32_t value);
451
452 // Push/pop composite routines.
453 void Push(Register rs);
454 void Pop(Register rd);
455 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700456
Andreas Gampe85b62f22015-09-09 13:15:38 -0700457 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200458 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700459 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200460 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
461 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700462 }
463
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700464 // Create a new literal with a given value.
465 // NOTE: Force the template parameter to be explicitly specified.
466 template <typename T>
467 Literal* NewLiteral(typename Identity<T>::type value) {
468 static_assert(std::is_integral<T>::value, "T must be an integral type.");
469 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
470 }
471
472 // Create a new literal with the given data.
473 Literal* NewLiteral(size_t size, const uint8_t* data);
474
475 // Load literal using the base register (for R2 only) or using PC-relative loads
476 // (for R6 only; base_reg must be ZERO).
477 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
478
jeffhao7fbee072012-08-24 17:56:54 -0700479 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200480 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700481 //
482
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200483 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800484 void BuildFrame(size_t frame_size,
485 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100486 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700487 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700488
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200489 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100490 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700491 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700492
Ian Rogersdd7624d2014-03-14 17:43:00 -0700493 void IncreaseFrameSize(size_t adjust) OVERRIDE;
494 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700495
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200496 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700497 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
498 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
499 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700500
Ian Rogersdd7624d2014-03-14 17:43:00 -0700501 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700502
Andreas Gampe542451c2016-07-26 09:02:02 -0700503 void StoreImmediateToThread32(ThreadOffset32 dest,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800504 uint32_t imm,
505 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700506
Andreas Gampe542451c2016-07-26 09:02:02 -0700507 void StoreStackOffsetToThread32(ThreadOffset32 thr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800508 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700509 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700510
Andreas Gampe542451c2016-07-26 09:02:02 -0700511 void StoreStackPointerToThread32(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700512
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800513 void StoreSpanning(FrameOffset dest,
514 ManagedRegister msrc,
515 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700516 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700517
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200518 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700519 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700520
Andreas Gampe542451c2016-07-26 09:02:02 -0700521 void LoadFromThread32(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700522
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700524
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800525 void LoadRef(ManagedRegister mdest,
526 ManagedRegister base,
527 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100528 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700529
Ian Rogersdd7624d2014-03-14 17:43:00 -0700530 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700531
Andreas Gampe542451c2016-07-26 09:02:02 -0700532 void LoadRawPtrFromThread32(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700533
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200534 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700535 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700536
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800537 void CopyRawPtrFromThread32(FrameOffset fr_offs,
Andreas Gampe542451c2016-07-26 09:02:02 -0700538 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700539 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700540
Andreas Gampe542451c2016-07-26 09:02:02 -0700541 void CopyRawPtrToThread32(ThreadOffset32 thr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800542 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700543 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700544
Ian Rogersdd7624d2014-03-14 17:43:00 -0700545 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700546
Ian Rogersdd7624d2014-03-14 17:43:00 -0700547 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700548
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800549 void Copy(FrameOffset dest,
550 ManagedRegister src_base,
551 Offset src_offset,
552 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700553 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700554
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800555 void Copy(ManagedRegister dest_base,
556 Offset dest_offset,
557 FrameOffset src,
558 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700559 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700560
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800561 void Copy(FrameOffset dest,
562 FrameOffset src_base,
563 Offset src_offset,
564 ManagedRegister mscratch,
565 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700566
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800567 void Copy(ManagedRegister dest,
568 Offset dest_offset,
569 ManagedRegister src,
570 Offset src_offset,
571 ManagedRegister mscratch,
572 size_t size) OVERRIDE;
573
574 void Copy(FrameOffset dest,
575 Offset dest_offset,
576 FrameOffset src,
577 Offset src_offset,
578 ManagedRegister mscratch,
579 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700580
Ian Rogersdd7624d2014-03-14 17:43:00 -0700581 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700582
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200583 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700584 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700585
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200586 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700587 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700588
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200589 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700590 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
591 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700592
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700593 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700594 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700595 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700596 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800597 void CreateHandleScopeEntry(ManagedRegister out_reg,
598 FrameOffset handlescope_offset,
599 ManagedRegister in_reg,
600 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700601
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700602 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700603 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800604 void CreateHandleScopeEntry(FrameOffset out_off,
605 FrameOffset handlescope_offset,
606 ManagedRegister mscratch,
607 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700608
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200609 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700610 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700611
612 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
613 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700614 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
615 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700616
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200617 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700618 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
619 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700620 void CallFromThread32(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700621
jeffhao7fbee072012-08-24 17:56:54 -0700622 // Generate code to check if Thread::Current()->exception_ is non-null
623 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700624 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700625
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200626 // Emit slow paths queued during assembly and promote short branches to long if needed.
627 void FinalizeCode() OVERRIDE;
628
629 // Emit branches and finalize all instructions.
630 void FinalizeInstructions(const MemoryRegion& region);
631
632 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
633 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700634 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200635
636 // Get the final position of a label after local fixup based on the old position
637 // recorded before FinalizeCode().
638 uint32_t GetAdjustedPosition(uint32_t old_position);
639
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700640 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
641 // reading the PC value into a general-purpose register with the NAL instruction and then loading
642 // literals through this base register. The code generator calls this method (at most once per
643 // method being compiled) to bind a label to the location for which the PC value is acquired.
644 // The assembler then computes literal offsets relative to this label.
645 void BindPcRelBaseLabel();
646
Alexey Frunze06a46c42016-07-19 15:00:40 -0700647 // Returns the location of the label bound with BindPcRelBaseLabel().
648 uint32_t GetPcRelBaseLabelLocation() const;
649
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700650 // Note that PC-relative literal loads are handled as pseudo branches because they need very
651 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
652 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200653 enum BranchCondition {
654 kCondLT,
655 kCondGE,
656 kCondLE,
657 kCondGT,
658 kCondLTZ,
659 kCondGEZ,
660 kCondLEZ,
661 kCondGTZ,
662 kCondEQ,
663 kCondNE,
664 kCondEQZ,
665 kCondNEZ,
666 kCondLTU,
667 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800668 kCondF, // Floating-point predicate false.
669 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200670 kUncond,
671 };
672 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
673
jeffhao7fbee072012-08-24 17:56:54 -0700674 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200675 class Branch {
676 public:
677 enum Type {
678 // R2 short branches.
679 kUncondBranch,
680 kCondBranch,
681 kCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700682 // R2 near literal.
683 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200684 // R2 long branches.
685 kLongUncondBranch,
686 kLongCondBranch,
687 kLongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700688 // R2 far literal.
689 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200690 // R6 short branches.
691 kR6UncondBranch,
692 kR6CondBranch,
693 kR6Call,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700694 // R6 near literal.
695 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200696 // R6 long branches.
697 kR6LongUncondBranch,
698 kR6LongCondBranch,
699 kR6LongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700700 // R6 far literal.
701 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200702 };
703 // Bit sizes of offsets defined as enums to minimize chance of typos.
704 enum OffsetBits {
705 kOffset16 = 16,
706 kOffset18 = 18,
707 kOffset21 = 21,
708 kOffset23 = 23,
709 kOffset28 = 28,
710 kOffset32 = 32,
711 };
712
713 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
714 static constexpr int32_t kMaxBranchLength = 32;
715 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
716
717 struct BranchInfo {
718 // Branch length as a number of 4-byte-long instructions.
719 uint32_t length;
720 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
721 // PC-relative offset (or its most significant 16-bit half, which goes first).
722 uint32_t instr_offset;
723 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
724 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
725 // instructions) from the instruction containing the offset.
726 uint32_t pc_org;
727 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
728 // is an exception: use kOffset23 for beqzc/bnezc).
729 OffsetBits offset_size;
730 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
731 // count.
732 int offset_shift;
733 };
734 static const BranchInfo branch_info_[/* Type */];
735
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700736 // Unconditional branch or call.
737 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200738 // Conditional branch.
739 Branch(bool is_r6,
740 uint32_t location,
741 uint32_t target,
742 BranchCondition condition,
743 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700744 Register rhs_reg);
745 // Literal.
746 Branch(bool is_r6, uint32_t location, Register dest_reg, Register base_reg);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200747
748 // Some conditional branches with lhs = rhs are effectively NOPs, while some
749 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
750 // So, we need a way to identify such branches in order to emit no instructions for them
751 // or change them to unconditional.
752 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
753 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
754
755 static BranchCondition OppositeCondition(BranchCondition cond);
756
757 Type GetType() const;
758 BranchCondition GetCondition() const;
759 Register GetLeftRegister() const;
760 Register GetRightRegister() const;
761 uint32_t GetTarget() const;
762 uint32_t GetLocation() const;
763 uint32_t GetOldLocation() const;
764 uint32_t GetLength() const;
765 uint32_t GetOldLength() const;
766 uint32_t GetSize() const;
767 uint32_t GetOldSize() const;
768 uint32_t GetEndLocation() const;
769 uint32_t GetOldEndLocation() const;
770 bool IsLong() const;
771 bool IsResolved() const;
772
773 // Returns the bit size of the signed offset that the branch instruction can handle.
774 OffsetBits GetOffsetSize() const;
775
776 // Calculates the distance between two byte locations in the assembler buffer and
777 // returns the number of bits needed to represent the distance as a signed integer.
778 //
779 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
780 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
781 //
782 // Composite branches (made of several instructions) with longer reach have 32-bit
783 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800784 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
785 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
786 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
787 // Consider the following implementation of a long unconditional branch, for
788 // example:
789 //
790 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
791 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
792 //
793 // Both of the above instructions take 16-bit signed offsets as immediate operands.
794 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
795 // due to sign extension. This must be compensated for by incrementing offset_31_16
796 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
797 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
798 // Therefore, the long branch range is something like from PC - 0x80000000 to
799 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200800 //
801 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
802 // case with the addiu instruction and a 16 bit offset.
803 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
804
805 // Resolve a branch when the target is known.
806 void Resolve(uint32_t target);
807
808 // Relocate a branch by a given delta if needed due to expansion of this or another
809 // branch at a given location by this delta (just changes location_ and target_).
810 void Relocate(uint32_t expand_location, uint32_t delta);
811
812 // If the branch is short, changes its type to long.
813 void PromoteToLong();
814
815 // If necessary, updates the type by promoting a short branch to a long branch
816 // based on the branch location and target. Returns the amount (in bytes) by
817 // which the branch size has increased.
818 // max_short_distance caps the maximum distance between location_ and target_
819 // that is allowed for short branches. This is for debugging/testing purposes.
820 // max_short_distance = 0 forces all short branches to become long.
821 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700822 uint32_t PromoteIfNeeded(uint32_t location,
823 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200824
825 // Returns the location of the instruction(s) containing the offset.
826 uint32_t GetOffsetLocation() const;
827
828 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700829 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200830
831 private:
832 // Completes branch construction by determining and recording its type.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700833 void InitializeType(bool is_call, bool is_literal, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200834 // Helper for the above.
835 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
836
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800837 uint32_t old_location_; // Offset into assembler buffer in bytes.
838 uint32_t location_; // Offset into assembler buffer in bytes.
839 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200840
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800841 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
842 // indirect call register.
843 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
844 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200845
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800846 Type type_; // Current type of the branch.
847 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200848 };
849 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
850 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
851
jeffhao7fbee072012-08-24 17:56:54 -0700852 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
853 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200854 void EmitI21(int opcode, Register rs, uint32_t imm21);
855 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700856 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
857 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800858 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
859 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -0700860
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200861 void Buncond(MipsLabel* label);
862 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700863 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200864 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700865
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200866 Branch* GetBranch(uint32_t branch_id);
867 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700868 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
869 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200870
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700871 void EmitLiterals();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200872 void PromoteBranches();
873 void EmitBranch(Branch* branch);
874 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100875 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200876
877 // Emits exception block.
878 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
879
880 bool IsR6() const {
881 if (isa_features_ != nullptr) {
882 return isa_features_->IsR6();
883 } else {
884 return false;
885 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000886 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200887
888 bool Is32BitFPU() const {
889 if (isa_features_ != nullptr) {
890 return isa_features_->Is32BitFloatingPoint();
891 } else {
892 return true;
893 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000894 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200895
896 // List of exception blocks to generate at the end of the code cache.
897 std::vector<MipsExceptionSlowPath> exception_blocks_;
898
899 std::vector<Branch> branches_;
900
901 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
902 bool overwriting_;
903 // The current overwrite location.
904 uint32_t overwrite_location_;
905
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700906 // Use std::deque<> for literal labels to allow insertions at the end
907 // without invalidating pointers and references to existing elements.
908 ArenaDeque<Literal> literals_;
909
910 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
911 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
912 // that PC (from NAL) points to.
913 MipsLabel pc_rel_base_label_;
914
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200915 // Data for AdjustedPosition(), see the description there.
916 uint32_t last_position_adjustment_;
917 uint32_t last_old_position_;
918 uint32_t last_branch_id_;
919
920 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000921
jeffhao7fbee072012-08-24 17:56:54 -0700922 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
923};
924
jeffhao7fbee072012-08-24 17:56:54 -0700925} // namespace mips
926} // namespace art
927
Ian Rogers166db042013-07-26 12:05:57 -0700928#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_