blob: 1f7781fef932ebb0cfa846527a52abbb187d9868 [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
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800503 void StoreImmediateToThread32(ThreadOffset<kMipsWordSize> dest,
504 uint32_t imm,
505 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700506
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800507 void StoreStackOffsetToThread32(ThreadOffset<kMipsWordSize> thr_offs,
508 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700509 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700510
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800511 void StoreStackPointerToThread32(ThreadOffset<kMipsWordSize> 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
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800521 void LoadFromThread32(ManagedRegister mdest,
522 ThreadOffset<kMipsWordSize> src,
523 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700524
Mathieu Chartiere401d142015-04-22 13:56:20 -0700525 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700526
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800527 void LoadRef(ManagedRegister mdest,
528 ManagedRegister base,
529 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100530 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700531
Ian Rogersdd7624d2014-03-14 17:43:00 -0700532 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700533
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800534 void LoadRawPtrFromThread32(ManagedRegister mdest, ThreadOffset<kMipsWordSize> offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700535
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200536 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700537 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700538
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800539 void CopyRawPtrFromThread32(FrameOffset fr_offs,
540 ThreadOffset<kMipsWordSize> thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700541 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700542
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800543 void CopyRawPtrToThread32(ThreadOffset<kMipsWordSize> thr_offs,
544 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700545 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700546
Ian Rogersdd7624d2014-03-14 17:43:00 -0700547 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700548
Ian Rogersdd7624d2014-03-14 17:43:00 -0700549 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700550
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800551 void Copy(FrameOffset dest,
552 ManagedRegister src_base,
553 Offset src_offset,
554 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700555 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700556
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800557 void Copy(ManagedRegister dest_base,
558 Offset dest_offset,
559 FrameOffset src,
560 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700561 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700562
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800563 void Copy(FrameOffset dest,
564 FrameOffset src_base,
565 Offset src_offset,
566 ManagedRegister mscratch,
567 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700568
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800569 void Copy(ManagedRegister dest,
570 Offset dest_offset,
571 ManagedRegister src,
572 Offset src_offset,
573 ManagedRegister mscratch,
574 size_t size) OVERRIDE;
575
576 void Copy(FrameOffset dest,
577 Offset dest_offset,
578 FrameOffset src,
579 Offset src_offset,
580 ManagedRegister mscratch,
581 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700582
Ian Rogersdd7624d2014-03-14 17:43:00 -0700583 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700584
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200585 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700586 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700587
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200588 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700589 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700590
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200591 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700592 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
593 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700594
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700595 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700596 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700597 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700598 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800599 void CreateHandleScopeEntry(ManagedRegister out_reg,
600 FrameOffset handlescope_offset,
601 ManagedRegister in_reg,
602 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700603
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700604 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700605 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800606 void CreateHandleScopeEntry(FrameOffset out_off,
607 FrameOffset handlescope_offset,
608 ManagedRegister mscratch,
609 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700610
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200611 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700612 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700613
614 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
615 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700616 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
617 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700618
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200619 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700620 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
621 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800622 void CallFromThread32(ThreadOffset<kMipsWordSize> offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700623
jeffhao7fbee072012-08-24 17:56:54 -0700624 // Generate code to check if Thread::Current()->exception_ is non-null
625 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700626 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700627
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200628 // Emit slow paths queued during assembly and promote short branches to long if needed.
629 void FinalizeCode() OVERRIDE;
630
631 // Emit branches and finalize all instructions.
632 void FinalizeInstructions(const MemoryRegion& region);
633
634 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
635 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700636 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200637
638 // Get the final position of a label after local fixup based on the old position
639 // recorded before FinalizeCode().
640 uint32_t GetAdjustedPosition(uint32_t old_position);
641
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700642 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
643 // reading the PC value into a general-purpose register with the NAL instruction and then loading
644 // literals through this base register. The code generator calls this method (at most once per
645 // method being compiled) to bind a label to the location for which the PC value is acquired.
646 // The assembler then computes literal offsets relative to this label.
647 void BindPcRelBaseLabel();
648
649 // Note that PC-relative literal loads are handled as pseudo branches because they need very
650 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
651 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200652 enum BranchCondition {
653 kCondLT,
654 kCondGE,
655 kCondLE,
656 kCondGT,
657 kCondLTZ,
658 kCondGEZ,
659 kCondLEZ,
660 kCondGTZ,
661 kCondEQ,
662 kCondNE,
663 kCondEQZ,
664 kCondNEZ,
665 kCondLTU,
666 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800667 kCondF, // Floating-point predicate false.
668 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200669 kUncond,
670 };
671 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
672
jeffhao7fbee072012-08-24 17:56:54 -0700673 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200674 class Branch {
675 public:
676 enum Type {
677 // R2 short branches.
678 kUncondBranch,
679 kCondBranch,
680 kCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700681 // R2 near literal.
682 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200683 // R2 long branches.
684 kLongUncondBranch,
685 kLongCondBranch,
686 kLongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700687 // R2 far literal.
688 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200689 // R6 short branches.
690 kR6UncondBranch,
691 kR6CondBranch,
692 kR6Call,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700693 // R6 near literal.
694 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200695 // R6 long branches.
696 kR6LongUncondBranch,
697 kR6LongCondBranch,
698 kR6LongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700699 // R6 far literal.
700 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200701 };
702 // Bit sizes of offsets defined as enums to minimize chance of typos.
703 enum OffsetBits {
704 kOffset16 = 16,
705 kOffset18 = 18,
706 kOffset21 = 21,
707 kOffset23 = 23,
708 kOffset28 = 28,
709 kOffset32 = 32,
710 };
711
712 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
713 static constexpr int32_t kMaxBranchLength = 32;
714 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
715
716 struct BranchInfo {
717 // Branch length as a number of 4-byte-long instructions.
718 uint32_t length;
719 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
720 // PC-relative offset (or its most significant 16-bit half, which goes first).
721 uint32_t instr_offset;
722 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
723 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
724 // instructions) from the instruction containing the offset.
725 uint32_t pc_org;
726 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
727 // is an exception: use kOffset23 for beqzc/bnezc).
728 OffsetBits offset_size;
729 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
730 // count.
731 int offset_shift;
732 };
733 static const BranchInfo branch_info_[/* Type */];
734
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700735 // Unconditional branch or call.
736 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200737 // Conditional branch.
738 Branch(bool is_r6,
739 uint32_t location,
740 uint32_t target,
741 BranchCondition condition,
742 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700743 Register rhs_reg);
744 // Literal.
745 Branch(bool is_r6, uint32_t location, Register dest_reg, Register base_reg);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200746
747 // Some conditional branches with lhs = rhs are effectively NOPs, while some
748 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
749 // So, we need a way to identify such branches in order to emit no instructions for them
750 // or change them to unconditional.
751 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
752 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
753
754 static BranchCondition OppositeCondition(BranchCondition cond);
755
756 Type GetType() const;
757 BranchCondition GetCondition() const;
758 Register GetLeftRegister() const;
759 Register GetRightRegister() const;
760 uint32_t GetTarget() const;
761 uint32_t GetLocation() const;
762 uint32_t GetOldLocation() const;
763 uint32_t GetLength() const;
764 uint32_t GetOldLength() const;
765 uint32_t GetSize() const;
766 uint32_t GetOldSize() const;
767 uint32_t GetEndLocation() const;
768 uint32_t GetOldEndLocation() const;
769 bool IsLong() const;
770 bool IsResolved() const;
771
772 // Returns the bit size of the signed offset that the branch instruction can handle.
773 OffsetBits GetOffsetSize() const;
774
775 // Calculates the distance between two byte locations in the assembler buffer and
776 // returns the number of bits needed to represent the distance as a signed integer.
777 //
778 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
779 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
780 //
781 // Composite branches (made of several instructions) with longer reach have 32-bit
782 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800783 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
784 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
785 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
786 // Consider the following implementation of a long unconditional branch, for
787 // example:
788 //
789 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
790 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
791 //
792 // Both of the above instructions take 16-bit signed offsets as immediate operands.
793 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
794 // due to sign extension. This must be compensated for by incrementing offset_31_16
795 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
796 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
797 // Therefore, the long branch range is something like from PC - 0x80000000 to
798 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200799 //
800 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
801 // case with the addiu instruction and a 16 bit offset.
802 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
803
804 // Resolve a branch when the target is known.
805 void Resolve(uint32_t target);
806
807 // Relocate a branch by a given delta if needed due to expansion of this or another
808 // branch at a given location by this delta (just changes location_ and target_).
809 void Relocate(uint32_t expand_location, uint32_t delta);
810
811 // If the branch is short, changes its type to long.
812 void PromoteToLong();
813
814 // If necessary, updates the type by promoting a short branch to a long branch
815 // based on the branch location and target. Returns the amount (in bytes) by
816 // which the branch size has increased.
817 // max_short_distance caps the maximum distance between location_ and target_
818 // that is allowed for short branches. This is for debugging/testing purposes.
819 // max_short_distance = 0 forces all short branches to become long.
820 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700821 uint32_t PromoteIfNeeded(uint32_t location,
822 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200823
824 // Returns the location of the instruction(s) containing the offset.
825 uint32_t GetOffsetLocation() const;
826
827 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700828 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200829
830 private:
831 // Completes branch construction by determining and recording its type.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700832 void InitializeType(bool is_call, bool is_literal, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200833 // Helper for the above.
834 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
835
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800836 uint32_t old_location_; // Offset into assembler buffer in bytes.
837 uint32_t location_; // Offset into assembler buffer in bytes.
838 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200839
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800840 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
841 // indirect call register.
842 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
843 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200844
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800845 Type type_; // Current type of the branch.
846 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200847 };
848 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
849 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
850
jeffhao7fbee072012-08-24 17:56:54 -0700851 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
852 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200853 void EmitI21(int opcode, Register rs, uint32_t imm21);
854 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700855 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
856 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800857 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
858 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -0700859
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200860 void Buncond(MipsLabel* label);
861 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700862 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200863 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700864
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200865 Branch* GetBranch(uint32_t branch_id);
866 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700867 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
868 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200869
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700870 void EmitLiterals();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200871 void PromoteBranches();
872 void EmitBranch(Branch* branch);
873 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100874 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200875
876 // Emits exception block.
877 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
878
879 bool IsR6() const {
880 if (isa_features_ != nullptr) {
881 return isa_features_->IsR6();
882 } else {
883 return false;
884 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000885 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200886
887 bool Is32BitFPU() const {
888 if (isa_features_ != nullptr) {
889 return isa_features_->Is32BitFloatingPoint();
890 } else {
891 return true;
892 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000893 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200894
895 // List of exception blocks to generate at the end of the code cache.
896 std::vector<MipsExceptionSlowPath> exception_blocks_;
897
898 std::vector<Branch> branches_;
899
900 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
901 bool overwriting_;
902 // The current overwrite location.
903 uint32_t overwrite_location_;
904
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700905 // Use std::deque<> for literal labels to allow insertions at the end
906 // without invalidating pointers and references to existing elements.
907 ArenaDeque<Literal> literals_;
908
909 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
910 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
911 // that PC (from NAL) points to.
912 MipsLabel pc_rel_base_label_;
913
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200914 // Data for AdjustedPosition(), see the description there.
915 uint32_t last_position_adjustment_;
916 uint32_t last_old_position_;
917 uint32_t last_branch_id_;
918
919 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000920
jeffhao7fbee072012-08-24 17:56:54 -0700921 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
922};
923
jeffhao7fbee072012-08-24 17:56:54 -0700924} // namespace mips
925} // namespace art
926
Ian Rogers166db042013-07-26 12:05:57 -0700927#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_