blob: 41b6c6bd32863c98a62881dac0e98249aa807967 [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
18#define ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
jeffhao7fbee072012-08-24 17:56:54 -070019
Alexey Frunzee3fb2452016-05-10 16:08:05 -070020#include <deque>
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020021#include <utility>
jeffhao7fbee072012-08-24 17:56:54 -070022#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080023
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020024#include "arch/mips/instruction_set_features_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070025#include "base/arena_containers.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070026#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070028#include "constants_mips.h"
29#include "globals.h"
30#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070031#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020032#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070033#include "utils/jni_macro_assembler.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020034#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070035
36namespace art {
37namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070038
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020039static constexpr size_t kMipsWordSize = 4;
40static constexpr size_t kMipsDoublewordSize = 8;
41
jeffhao7fbee072012-08-24 17:56:54 -070042enum LoadOperandType {
43 kLoadSignedByte,
44 kLoadUnsignedByte,
45 kLoadSignedHalfword,
46 kLoadUnsignedHalfword,
47 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020048 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070049};
50
51enum StoreOperandType {
52 kStoreByte,
53 kStoreHalfword,
54 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020055 kStoreDoubleword
56};
57
Chris Larsenb74353a2015-11-20 09:07:09 -080058// Used to test the values returned by ClassS/ClassD.
59enum FPClassMaskType {
60 kSignalingNaN = 0x001,
61 kQuietNaN = 0x002,
62 kNegativeInfinity = 0x004,
63 kNegativeNormal = 0x008,
64 kNegativeSubnormal = 0x010,
65 kNegativeZero = 0x020,
66 kPositiveInfinity = 0x040,
67 kPositiveNormal = 0x080,
68 kPositiveSubnormal = 0x100,
69 kPositiveZero = 0x200,
70};
71
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020072class MipsLabel : public Label {
73 public:
74 MipsLabel() : prev_branch_id_plus_one_(0) {}
75
76 MipsLabel(MipsLabel&& src)
77 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
78
79 private:
80 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
81
82 friend class MipsAssembler;
83 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
84};
85
Alexey Frunzee3fb2452016-05-10 16:08:05 -070086// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
87class Literal {
88 public:
89 static constexpr size_t kMaxSize = 8;
90
91 Literal(uint32_t size, const uint8_t* data)
92 : label_(), size_(size) {
93 DCHECK_LE(size, Literal::kMaxSize);
94 memcpy(data_, data, size);
95 }
96
97 template <typename T>
98 T GetValue() const {
99 DCHECK_EQ(size_, sizeof(T));
100 T value;
101 memcpy(&value, data_, sizeof(T));
102 return value;
103 }
104
105 uint32_t GetSize() const {
106 return size_;
107 }
108
109 const uint8_t* GetData() const {
110 return data_;
111 }
112
113 MipsLabel* GetLabel() {
114 return &label_;
115 }
116
117 const MipsLabel* GetLabel() const {
118 return &label_;
119 }
120
121 private:
122 MipsLabel label_;
123 const uint32_t size_;
124 uint8_t data_[kMaxSize];
125
126 DISALLOW_COPY_AND_ASSIGN(Literal);
127};
128
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200129// Slowpath entered when Thread::Current()->_exception is non-null.
130class MipsExceptionSlowPath {
131 public:
132 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
133 : scratch_(scratch), stack_adjust_(stack_adjust) {}
134
135 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800136 : scratch_(src.scratch_),
137 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200138 exception_entry_(std::move(src.exception_entry_)) {}
139
140 private:
141 MipsLabel* Entry() { return &exception_entry_; }
142 const MipsManagedRegister scratch_;
143 const size_t stack_adjust_;
144 MipsLabel exception_entry_;
145
146 friend class MipsAssembler;
147 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700148};
149
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700150class MipsAssembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k32> {
jeffhao7fbee072012-08-24 17:56:54 -0700151 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100152 explicit MipsAssembler(ArenaAllocator* arena,
153 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
154 : Assembler(arena),
155 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200156 overwrite_location_(0),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700157 literals_(arena->Adapter(kArenaAllocAssembler)),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200158 last_position_adjustment_(0),
159 last_old_position_(0),
160 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100161 isa_features_(instruction_set_features) {
162 cfi().DelayEmittingAdvancePCs();
163 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200164
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700165 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
166 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
167
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200168 virtual ~MipsAssembler() {
169 for (auto& branch : branches_) {
170 CHECK(branch.IsResolved());
171 }
172 }
jeffhao7fbee072012-08-24 17:56:54 -0700173
174 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700175 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700176 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700177 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200178
179 void MultR2(Register rs, Register rt); // R2
180 void MultuR2(Register rs, Register rt); // R2
181 void DivR2(Register rs, Register rt); // R2
182 void DivuR2(Register rs, Register rt); // R2
183 void MulR2(Register rd, Register rs, Register rt); // R2
184 void DivR2(Register rd, Register rs, Register rt); // R2
185 void ModR2(Register rd, Register rs, Register rt); // R2
186 void DivuR2(Register rd, Register rs, Register rt); // R2
187 void ModuR2(Register rd, Register rs, Register rt); // R2
188 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800189 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200190 void MuhuR6(Register rd, Register rs, Register rt); // R6
191 void DivR6(Register rd, Register rs, Register rt); // R6
192 void ModR6(Register rd, Register rs, Register rt); // R6
193 void DivuR6(Register rd, Register rs, Register rt); // R6
194 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700195
196 void And(Register rd, Register rs, Register rt);
197 void Andi(Register rt, Register rs, uint16_t imm16);
198 void Or(Register rd, Register rs, Register rt);
199 void Ori(Register rt, Register rs, uint16_t imm16);
200 void Xor(Register rd, Register rs, Register rt);
201 void Xori(Register rt, Register rs, uint16_t imm16);
202 void Nor(Register rd, Register rs, Register rt);
203
Chris Larsene3845472015-11-18 12:27:15 -0800204 void Movz(Register rd, Register rs, Register rt); // R2
205 void Movn(Register rd, Register rs, Register rt); // R2
206 void Seleqz(Register rd, Register rs, Register rt); // R6
207 void Selnez(Register rd, Register rs, Register rt); // R6
208 void ClzR6(Register rd, Register rs);
209 void ClzR2(Register rd, Register rs);
210 void CloR6(Register rd, Register rs);
211 void CloR2(Register rd, Register rs);
212
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200213 void Seb(Register rd, Register rt); // R2+
214 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700215 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800216 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200217
218 void Sll(Register rd, Register rt, int shamt);
219 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700220 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200221 void Sra(Register rd, Register rt, int shamt);
222 void Sllv(Register rd, Register rt, Register rs);
223 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800224 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200225 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800226 void Ext(Register rd, Register rt, int pos, int size); // R2+
227 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700228
229 void Lb(Register rt, Register rs, uint16_t imm16);
230 void Lh(Register rt, Register rs, uint16_t imm16);
231 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800232 void Lwl(Register rt, Register rs, uint16_t imm16);
233 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700234 void Lbu(Register rt, Register rs, uint16_t imm16);
235 void Lhu(Register rt, Register rs, uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700236 void Lwpc(Register rs, uint32_t imm19); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700237 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700238 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200239 void Sync(uint32_t stype);
240 void Mfhi(Register rd); // R2
241 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700242
243 void Sb(Register rt, Register rs, uint16_t imm16);
244 void Sh(Register rt, Register rs, uint16_t imm16);
245 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800246 void Swl(Register rt, Register rs, uint16_t imm16);
247 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700248
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700249 void LlR2(Register rt, Register base, int16_t imm16 = 0);
250 void ScR2(Register rt, Register base, int16_t imm16 = 0);
251 void LlR6(Register rt, Register base, int16_t imm9 = 0);
252 void ScR6(Register rt, Register base, int16_t imm9 = 0);
253
jeffhao7fbee072012-08-24 17:56:54 -0700254 void Slt(Register rd, Register rs, Register rt);
255 void Sltu(Register rd, Register rs, Register rt);
256 void Slti(Register rt, Register rs, uint16_t imm16);
257 void Sltiu(Register rt, Register rs, uint16_t imm16);
258
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200259 void B(uint16_t imm16);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700260 void Bal(uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200261 void Beq(Register rs, Register rt, uint16_t imm16);
262 void Bne(Register rs, Register rt, uint16_t imm16);
263 void Beqz(Register rt, uint16_t imm16);
264 void Bnez(Register rt, uint16_t imm16);
265 void Bltz(Register rt, uint16_t imm16);
266 void Bgez(Register rt, uint16_t imm16);
267 void Blez(Register rt, uint16_t imm16);
268 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800269 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800270 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800271 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800272 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200273 void J(uint32_t addr26);
274 void Jal(uint32_t addr26);
275 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700276 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200277 void Jr(Register rs);
278 void Nal();
279 void Auipc(Register rs, uint16_t imm16); // R6
280 void Addiupc(Register rs, uint32_t imm19); // R6
281 void Bc(uint32_t imm26); // R6
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700282 void Balc(uint32_t imm26); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200283 void Jic(Register rt, uint16_t imm16); // R6
284 void Jialc(Register rt, uint16_t imm16); // R6
285 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
286 void Bltzc(Register rt, uint16_t imm16); // R6
287 void Bgtzc(Register rt, uint16_t imm16); // R6
288 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
289 void Bgezc(Register rt, uint16_t imm16); // R6
290 void Blezc(Register rt, uint16_t imm16); // R6
291 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
292 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
293 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
294 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
295 void Beqzc(Register rs, uint32_t imm21); // R6
296 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800297 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
298 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700299
300 void AddS(FRegister fd, FRegister fs, FRegister ft);
301 void SubS(FRegister fd, FRegister fs, FRegister ft);
302 void MulS(FRegister fd, FRegister fs, FRegister ft);
303 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200304 void AddD(FRegister fd, FRegister fs, FRegister ft);
305 void SubD(FRegister fd, FRegister fs, FRegister ft);
306 void MulD(FRegister fd, FRegister fs, FRegister ft);
307 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800308 void SqrtS(FRegister fd, FRegister fs);
309 void SqrtD(FRegister fd, FRegister fs);
310 void AbsS(FRegister fd, FRegister fs);
311 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700312 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200313 void MovD(FRegister fd, FRegister fs);
314 void NegS(FRegister fd, FRegister fs);
315 void NegD(FRegister fd, FRegister fs);
316
Chris Larsenb74353a2015-11-20 09:07:09 -0800317 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800318 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800319 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800320 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800321 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800322 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800323 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800324 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800325 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800326 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800327 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800328 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800329 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800330 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800331 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800332 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800333 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800334 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800335 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800336 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800337 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800338 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800339 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800340 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800341 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800342 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800343 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800344 void CuleD(int cc, FRegister fs, FRegister ft); // R2
345 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
346 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
347 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
348 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
349 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
350 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
351 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
352 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
353 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
354 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
355 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
356 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
357 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
358 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
359 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
360 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
361 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
362 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
363 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
364 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800365 void Movf(Register rd, Register rs, int cc = 0); // R2
366 void Movt(Register rd, Register rs, int cc = 0); // R2
367 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
368 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
369 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
370 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
371 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
372 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
373 void ClassS(FRegister fd, FRegister fs); // R6
374 void ClassD(FRegister fd, FRegister fs); // R6
375 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
376 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
377 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
378 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800379
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800380 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
381 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
382 void TruncWS(FRegister fd, FRegister fs);
383 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200384 void Cvtsw(FRegister fd, FRegister fs);
385 void Cvtdw(FRegister fd, FRegister fs);
386 void Cvtsd(FRegister fd, FRegister fs);
387 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800388 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
389 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800390 void FloorWS(FRegister fd, FRegister fs);
391 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700392
393 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200394 void Mtc1(Register rt, FRegister fs);
395 void Mfhc1(Register rt, FRegister fs);
396 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800397 void MoveFromFpuHigh(Register rt, FRegister fs);
398 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700399 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200400 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700401 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200402 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700403
404 void Break();
jeffhao07030602012-09-26 14:33:14 -0700405 void Nop();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200406 void Move(Register rd, Register rs);
407 void Clear(Register rd);
408 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700409
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200410 // Higher level composite instructions.
411 void LoadConst32(Register rd, int32_t value);
412 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
413 void LoadDConst64(FRegister rd, int64_t value, Register temp);
414 void LoadSConst32(FRegister r, int32_t value, Register temp);
415 void StoreConst32ToOffset(int32_t value, Register base, int32_t offset, Register temp);
416 void StoreConst64ToOffset(int64_t value, Register base, int32_t offset, Register temp);
417 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
418
419 // These will generate R2 branches or R6 branches as appropriate.
420 void Bind(MipsLabel* label);
421 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700422 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200423 void Beq(Register rs, Register rt, MipsLabel* label);
424 void Bne(Register rs, Register rt, MipsLabel* label);
425 void Beqz(Register rt, MipsLabel* label);
426 void Bnez(Register rt, MipsLabel* label);
427 void Bltz(Register rt, MipsLabel* label);
428 void Bgez(Register rt, MipsLabel* label);
429 void Blez(Register rt, MipsLabel* label);
430 void Bgtz(Register rt, MipsLabel* label);
431 void Blt(Register rs, Register rt, MipsLabel* label);
432 void Bge(Register rs, Register rt, MipsLabel* label);
433 void Bltu(Register rs, Register rt, MipsLabel* label);
434 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800435 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800436 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800437 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800438 void Bc1t(int cc, MipsLabel* label); // R2
439 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
440 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700441
442 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700443 void AdjustBaseAndOffset(Register& base,
444 int32_t& offset,
445 bool is_doubleword,
446 bool is_float = false);
jeffhao7fbee072012-08-24 17:56:54 -0700447 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
448 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200449 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700450 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000451 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200452 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700453
jeffhao7fbee072012-08-24 17:56:54 -0700454 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200455 void Emit(uint32_t value);
456
457 // Push/pop composite routines.
458 void Push(Register rs);
459 void Pop(Register rd);
460 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700461
Andreas Gampe85b62f22015-09-09 13:15:38 -0700462 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200463 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700464 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200465 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
466 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700467 }
468
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700469 // Create a new literal with a given value.
470 // NOTE: Force the template parameter to be explicitly specified.
471 template <typename T>
472 Literal* NewLiteral(typename Identity<T>::type value) {
473 static_assert(std::is_integral<T>::value, "T must be an integral type.");
474 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
475 }
476
477 // Create a new literal with the given data.
478 Literal* NewLiteral(size_t size, const uint8_t* data);
479
480 // Load literal using the base register (for R2 only) or using PC-relative loads
481 // (for R6 only; base_reg must be ZERO).
482 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
483
jeffhao7fbee072012-08-24 17:56:54 -0700484 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200485 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700486 //
487
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200488 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800489 void BuildFrame(size_t frame_size,
490 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100491 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700492 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700493
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200494 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100495 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700496 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700497
Ian Rogersdd7624d2014-03-14 17:43:00 -0700498 void IncreaseFrameSize(size_t adjust) OVERRIDE;
499 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700500
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200501 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700502 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
503 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
504 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700505
Ian Rogersdd7624d2014-03-14 17:43:00 -0700506 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700507
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700508 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
509 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800510 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700511
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700512 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700513
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800514 void StoreSpanning(FrameOffset dest,
515 ManagedRegister msrc,
516 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700517 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700518
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200519 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700520 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700521
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700522 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700523
Mathieu Chartiere401d142015-04-22 13:56:20 -0700524 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700525
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800526 void LoadRef(ManagedRegister mdest,
527 ManagedRegister base,
528 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100529 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700530
Ian Rogersdd7624d2014-03-14 17:43:00 -0700531 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700532
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700533 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700534
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200535 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700536 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700537
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700538 void CopyRawPtrFromThread(FrameOffset fr_offs,
539 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700540 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700541
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700542 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
543 FrameOffset fr_offs,
544 ManagedRegister mscratch) OVERRIDE;
545
Ian Rogersdd7624d2014-03-14 17:43:00 -0700546 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700547
Ian Rogersdd7624d2014-03-14 17:43:00 -0700548 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700549
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800550 void Copy(FrameOffset dest,
551 ManagedRegister src_base,
552 Offset src_offset,
553 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700554 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700555
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800556 void Copy(ManagedRegister dest_base,
557 Offset dest_offset,
558 FrameOffset src,
559 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700560 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700561
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800562 void Copy(FrameOffset dest,
563 FrameOffset src_base,
564 Offset src_offset,
565 ManagedRegister mscratch,
566 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700567
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800568 void Copy(ManagedRegister dest,
569 Offset dest_offset,
570 ManagedRegister src,
571 Offset src_offset,
572 ManagedRegister mscratch,
573 size_t size) OVERRIDE;
574
575 void Copy(FrameOffset dest,
576 Offset dest_offset,
577 FrameOffset src,
578 Offset src_offset,
579 ManagedRegister mscratch,
580 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700581
Ian Rogersdd7624d2014-03-14 17:43:00 -0700582 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700583
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200584 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700585 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700586
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200587 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700588 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700589
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200590 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700591 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
592 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700593
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700594 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700595 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700596 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700597 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800598 void CreateHandleScopeEntry(ManagedRegister out_reg,
599 FrameOffset handlescope_offset,
600 ManagedRegister in_reg,
601 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700602
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700603 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700604 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800605 void CreateHandleScopeEntry(FrameOffset out_off,
606 FrameOffset handlescope_offset,
607 ManagedRegister mscratch,
608 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700609
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200610 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700611 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700612
613 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
614 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700615 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
616 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700617
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200618 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700619 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
620 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700621 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700622
jeffhao7fbee072012-08-24 17:56:54 -0700623 // Generate code to check if Thread::Current()->exception_ is non-null
624 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700625 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700626
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200627 // Emit slow paths queued during assembly and promote short branches to long if needed.
628 void FinalizeCode() OVERRIDE;
629
630 // Emit branches and finalize all instructions.
631 void FinalizeInstructions(const MemoryRegion& region);
632
633 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
634 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700635 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200636
637 // Get the final position of a label after local fixup based on the old position
638 // recorded before FinalizeCode().
639 uint32_t GetAdjustedPosition(uint32_t old_position);
640
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700641 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
642 // reading the PC value into a general-purpose register with the NAL instruction and then loading
643 // literals through this base register. The code generator calls this method (at most once per
644 // method being compiled) to bind a label to the location for which the PC value is acquired.
645 // The assembler then computes literal offsets relative to this label.
646 void BindPcRelBaseLabel();
647
Alexey Frunze06a46c42016-07-19 15:00:40 -0700648 // Returns the location of the label bound with BindPcRelBaseLabel().
649 uint32_t GetPcRelBaseLabelLocation() const;
650
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700651 // Note that PC-relative literal loads are handled as pseudo branches because they need very
652 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
653 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200654 enum BranchCondition {
655 kCondLT,
656 kCondGE,
657 kCondLE,
658 kCondGT,
659 kCondLTZ,
660 kCondGEZ,
661 kCondLEZ,
662 kCondGTZ,
663 kCondEQ,
664 kCondNE,
665 kCondEQZ,
666 kCondNEZ,
667 kCondLTU,
668 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800669 kCondF, // Floating-point predicate false.
670 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200671 kUncond,
672 };
673 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
674
jeffhao7fbee072012-08-24 17:56:54 -0700675 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200676 class Branch {
677 public:
678 enum Type {
679 // R2 short branches.
680 kUncondBranch,
681 kCondBranch,
682 kCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700683 // R2 near literal.
684 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200685 // R2 long branches.
686 kLongUncondBranch,
687 kLongCondBranch,
688 kLongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700689 // R2 far literal.
690 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200691 // R6 short branches.
692 kR6UncondBranch,
693 kR6CondBranch,
694 kR6Call,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700695 // R6 near literal.
696 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200697 // R6 long branches.
698 kR6LongUncondBranch,
699 kR6LongCondBranch,
700 kR6LongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700701 // R6 far literal.
702 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200703 };
704 // Bit sizes of offsets defined as enums to minimize chance of typos.
705 enum OffsetBits {
706 kOffset16 = 16,
707 kOffset18 = 18,
708 kOffset21 = 21,
709 kOffset23 = 23,
710 kOffset28 = 28,
711 kOffset32 = 32,
712 };
713
714 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
715 static constexpr int32_t kMaxBranchLength = 32;
716 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
717
718 struct BranchInfo {
719 // Branch length as a number of 4-byte-long instructions.
720 uint32_t length;
721 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
722 // PC-relative offset (or its most significant 16-bit half, which goes first).
723 uint32_t instr_offset;
724 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
725 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
726 // instructions) from the instruction containing the offset.
727 uint32_t pc_org;
728 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
729 // is an exception: use kOffset23 for beqzc/bnezc).
730 OffsetBits offset_size;
731 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
732 // count.
733 int offset_shift;
734 };
735 static const BranchInfo branch_info_[/* Type */];
736
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700737 // Unconditional branch or call.
738 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200739 // Conditional branch.
740 Branch(bool is_r6,
741 uint32_t location,
742 uint32_t target,
743 BranchCondition condition,
744 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700745 Register rhs_reg);
746 // Literal.
747 Branch(bool is_r6, uint32_t location, Register dest_reg, Register base_reg);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200748
749 // Some conditional branches with lhs = rhs are effectively NOPs, while some
750 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
751 // So, we need a way to identify such branches in order to emit no instructions for them
752 // or change them to unconditional.
753 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
754 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
755
756 static BranchCondition OppositeCondition(BranchCondition cond);
757
758 Type GetType() const;
759 BranchCondition GetCondition() const;
760 Register GetLeftRegister() const;
761 Register GetRightRegister() const;
762 uint32_t GetTarget() const;
763 uint32_t GetLocation() const;
764 uint32_t GetOldLocation() const;
765 uint32_t GetLength() const;
766 uint32_t GetOldLength() const;
767 uint32_t GetSize() const;
768 uint32_t GetOldSize() const;
769 uint32_t GetEndLocation() const;
770 uint32_t GetOldEndLocation() const;
771 bool IsLong() const;
772 bool IsResolved() const;
773
774 // Returns the bit size of the signed offset that the branch instruction can handle.
775 OffsetBits GetOffsetSize() const;
776
777 // Calculates the distance between two byte locations in the assembler buffer and
778 // returns the number of bits needed to represent the distance as a signed integer.
779 //
780 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
781 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
782 //
783 // Composite branches (made of several instructions) with longer reach have 32-bit
784 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800785 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
786 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
787 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
788 // Consider the following implementation of a long unconditional branch, for
789 // example:
790 //
791 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
792 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
793 //
794 // Both of the above instructions take 16-bit signed offsets as immediate operands.
795 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
796 // due to sign extension. This must be compensated for by incrementing offset_31_16
797 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
798 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
799 // Therefore, the long branch range is something like from PC - 0x80000000 to
800 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200801 //
802 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
803 // case with the addiu instruction and a 16 bit offset.
804 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
805
806 // Resolve a branch when the target is known.
807 void Resolve(uint32_t target);
808
809 // Relocate a branch by a given delta if needed due to expansion of this or another
810 // branch at a given location by this delta (just changes location_ and target_).
811 void Relocate(uint32_t expand_location, uint32_t delta);
812
813 // If the branch is short, changes its type to long.
814 void PromoteToLong();
815
816 // If necessary, updates the type by promoting a short branch to a long branch
817 // based on the branch location and target. Returns the amount (in bytes) by
818 // which the branch size has increased.
819 // max_short_distance caps the maximum distance between location_ and target_
820 // that is allowed for short branches. This is for debugging/testing purposes.
821 // max_short_distance = 0 forces all short branches to become long.
822 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700823 uint32_t PromoteIfNeeded(uint32_t location,
824 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200825
826 // Returns the location of the instruction(s) containing the offset.
827 uint32_t GetOffsetLocation() const;
828
829 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700830 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200831
832 private:
833 // Completes branch construction by determining and recording its type.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700834 void InitializeType(bool is_call, bool is_literal, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200835 // Helper for the above.
836 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
837
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800838 uint32_t old_location_; // Offset into assembler buffer in bytes.
839 uint32_t location_; // Offset into assembler buffer in bytes.
840 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200841
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800842 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
843 // indirect call register.
844 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
845 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200846
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800847 Type type_; // Current type of the branch.
848 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200849 };
850 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
851 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
852
jeffhao7fbee072012-08-24 17:56:54 -0700853 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
854 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200855 void EmitI21(int opcode, Register rs, uint32_t imm21);
856 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700857 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
858 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800859 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
860 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -0700861
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200862 void Buncond(MipsLabel* label);
863 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700864 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200865 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700866
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200867 Branch* GetBranch(uint32_t branch_id);
868 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700869 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
870 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200871
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700872 void EmitLiterals();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200873 void PromoteBranches();
874 void EmitBranch(Branch* branch);
875 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100876 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200877
878 // Emits exception block.
879 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
880
881 bool IsR6() const {
882 if (isa_features_ != nullptr) {
883 return isa_features_->IsR6();
884 } else {
885 return false;
886 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000887 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200888
889 bool Is32BitFPU() const {
890 if (isa_features_ != nullptr) {
891 return isa_features_->Is32BitFloatingPoint();
892 } else {
893 return true;
894 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000895 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200896
897 // List of exception blocks to generate at the end of the code cache.
898 std::vector<MipsExceptionSlowPath> exception_blocks_;
899
900 std::vector<Branch> branches_;
901
902 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
903 bool overwriting_;
904 // The current overwrite location.
905 uint32_t overwrite_location_;
906
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700907 // Use std::deque<> for literal labels to allow insertions at the end
908 // without invalidating pointers and references to existing elements.
909 ArenaDeque<Literal> literals_;
910
911 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
912 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
913 // that PC (from NAL) points to.
914 MipsLabel pc_rel_base_label_;
915
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200916 // Data for AdjustedPosition(), see the description there.
917 uint32_t last_position_adjustment_;
918 uint32_t last_old_position_;
919 uint32_t last_branch_id_;
920
921 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000922
jeffhao7fbee072012-08-24 17:56:54 -0700923 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
924};
925
jeffhao7fbee072012-08-24 17:56:54 -0700926} // namespace mips
927} // namespace art
928
Ian Rogers166db042013-07-26 12:05:57 -0700929#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_