blob: 434ca679d5bd677c16b5010e44b4149bb6460a30 [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);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200415 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
416
417 // These will generate R2 branches or R6 branches as appropriate.
418 void Bind(MipsLabel* label);
419 void B(MipsLabel* label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700420 void Bal(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200421 void Beq(Register rs, Register rt, MipsLabel* label);
422 void Bne(Register rs, Register rt, MipsLabel* label);
423 void Beqz(Register rt, MipsLabel* label);
424 void Bnez(Register rt, MipsLabel* label);
425 void Bltz(Register rt, MipsLabel* label);
426 void Bgez(Register rt, MipsLabel* label);
427 void Blez(Register rt, MipsLabel* label);
428 void Bgtz(Register rt, MipsLabel* label);
429 void Blt(Register rs, Register rt, MipsLabel* label);
430 void Bge(Register rs, Register rt, MipsLabel* label);
431 void Bltu(Register rs, Register rt, MipsLabel* label);
432 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800433 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800434 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800435 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800436 void Bc1t(int cc, MipsLabel* label); // R2
437 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
438 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700439
440 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700441 void AdjustBaseAndOffset(Register& base,
442 int32_t& offset,
443 bool is_doubleword,
444 bool is_float = false);
Alexey Frunze2923db72016-08-20 01:55:47 -0700445
446 private:
447 struct NoImplicitNullChecker {
448 void operator()() {}
449 };
450
451 public:
452 template <typename ImplicitNullChecker = NoImplicitNullChecker>
453 void StoreConst32ToOffset(int32_t value,
454 Register base,
455 int32_t offset,
456 Register temp,
457 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
458 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
459 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false);
460 if (value == 0) {
461 temp = ZERO;
462 } else {
463 LoadConst32(temp, value);
464 }
465 Sw(temp, base, offset);
466 null_checker();
467 }
468
469 template <typename ImplicitNullChecker = NoImplicitNullChecker>
470 void StoreConst64ToOffset(int64_t value,
471 Register base,
472 int32_t offset,
473 Register temp,
474 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
475 CHECK_NE(temp, AT); // Must not use AT as temp, so as not to overwrite the adjusted base.
476 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true);
477 uint32_t low = Low32Bits(value);
478 uint32_t high = High32Bits(value);
479 if (low == 0) {
480 Sw(ZERO, base, offset);
481 } else {
482 LoadConst32(temp, low);
483 Sw(temp, base, offset);
484 }
485 null_checker();
486 if (high == 0) {
487 Sw(ZERO, base, offset + kMipsWordSize);
488 } else {
489 if (high != low) {
490 LoadConst32(temp, high);
491 }
492 Sw(temp, base, offset + kMipsWordSize);
493 }
494 }
495
496 template <typename ImplicitNullChecker = NoImplicitNullChecker>
497 void LoadFromOffset(LoadOperandType type,
498 Register reg,
499 Register base,
500 int32_t offset,
501 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
502 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kLoadDoubleword));
503 switch (type) {
504 case kLoadSignedByte:
505 Lb(reg, base, offset);
506 break;
507 case kLoadUnsignedByte:
508 Lbu(reg, base, offset);
509 break;
510 case kLoadSignedHalfword:
511 Lh(reg, base, offset);
512 break;
513 case kLoadUnsignedHalfword:
514 Lhu(reg, base, offset);
515 break;
516 case kLoadWord:
517 Lw(reg, base, offset);
518 break;
519 case kLoadDoubleword:
520 if (reg == base) {
521 // This will clobber the base when loading the lower register. Since we have to load the
522 // higher register as well, this will fail. Solution: reverse the order.
523 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
524 null_checker();
525 Lw(reg, base, offset);
526 } else {
527 Lw(reg, base, offset);
528 null_checker();
529 Lw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
530 }
531 break;
532 default:
533 LOG(FATAL) << "UNREACHABLE";
534 }
535 if (type != kLoadDoubleword) {
536 null_checker();
537 }
538 }
539
540 template <typename ImplicitNullChecker = NoImplicitNullChecker>
541 void LoadSFromOffset(FRegister reg,
542 Register base,
543 int32_t offset,
544 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
545 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
546 Lwc1(reg, base, offset);
547 null_checker();
548 }
549
550 template <typename ImplicitNullChecker = NoImplicitNullChecker>
551 void LoadDFromOffset(FRegister reg,
552 Register base,
553 int32_t offset,
554 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
555 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
556 if (IsAligned<kMipsDoublewordSize>(offset)) {
557 Ldc1(reg, base, offset);
558 null_checker();
559 } else {
560 if (Is32BitFPU()) {
561 Lwc1(reg, base, offset);
562 null_checker();
563 Lwc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
564 } else {
565 // 64-bit FPU.
566 Lwc1(reg, base, offset);
567 null_checker();
568 Lw(T8, base, offset + kMipsWordSize);
569 Mthc1(T8, reg);
570 }
571 }
572 }
573
574 template <typename ImplicitNullChecker = NoImplicitNullChecker>
575 void StoreToOffset(StoreOperandType type,
576 Register reg,
577 Register base,
578 int32_t offset,
579 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
580 // Must not use AT as `reg`, so as not to overwrite the value being stored
581 // with the adjusted `base`.
582 CHECK_NE(reg, AT);
583 AdjustBaseAndOffset(base, offset, /* is_doubleword */ (type == kStoreDoubleword));
584 switch (type) {
585 case kStoreByte:
586 Sb(reg, base, offset);
587 break;
588 case kStoreHalfword:
589 Sh(reg, base, offset);
590 break;
591 case kStoreWord:
592 Sw(reg, base, offset);
593 break;
594 case kStoreDoubleword:
595 CHECK_NE(reg, base);
596 CHECK_NE(static_cast<Register>(reg + 1), base);
597 Sw(reg, base, offset);
598 null_checker();
599 Sw(static_cast<Register>(reg + 1), base, offset + kMipsWordSize);
600 break;
601 default:
602 LOG(FATAL) << "UNREACHABLE";
603 }
604 if (type != kStoreDoubleword) {
605 null_checker();
606 }
607 }
608
609 template <typename ImplicitNullChecker = NoImplicitNullChecker>
610 void StoreSToOffset(FRegister reg,
611 Register base,
612 int32_t offset,
613 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
614 AdjustBaseAndOffset(base, offset, /* is_doubleword */ false, /* is_float */ true);
615 Swc1(reg, base, offset);
616 null_checker();
617 }
618
619 template <typename ImplicitNullChecker = NoImplicitNullChecker>
620 void StoreDToOffset(FRegister reg,
621 Register base,
622 int32_t offset,
623 ImplicitNullChecker null_checker = NoImplicitNullChecker()) {
624 AdjustBaseAndOffset(base, offset, /* is_doubleword */ true, /* is_float */ true);
625 if (IsAligned<kMipsDoublewordSize>(offset)) {
626 Sdc1(reg, base, offset);
627 null_checker();
628 } else {
629 if (Is32BitFPU()) {
630 Swc1(reg, base, offset);
631 null_checker();
632 Swc1(static_cast<FRegister>(reg + 1), base, offset + kMipsWordSize);
633 } else {
634 // 64-bit FPU.
635 Mfhc1(T8, reg);
636 Swc1(reg, base, offset);
637 null_checker();
638 Sw(T8, base, offset + kMipsWordSize);
639 }
640 }
641 }
642
jeffhao7fbee072012-08-24 17:56:54 -0700643 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
644 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200645 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700646 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000647 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200648 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700649
jeffhao7fbee072012-08-24 17:56:54 -0700650 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200651 void Emit(uint32_t value);
652
653 // Push/pop composite routines.
654 void Push(Register rs);
655 void Pop(Register rd);
656 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700657
Andreas Gampe85b62f22015-09-09 13:15:38 -0700658 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200659 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700660 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200661 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
662 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700663 }
664
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700665 // Create a new literal with a given value.
666 // NOTE: Force the template parameter to be explicitly specified.
667 template <typename T>
668 Literal* NewLiteral(typename Identity<T>::type value) {
669 static_assert(std::is_integral<T>::value, "T must be an integral type.");
670 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
671 }
672
673 // Create a new literal with the given data.
674 Literal* NewLiteral(size_t size, const uint8_t* data);
675
676 // Load literal using the base register (for R2 only) or using PC-relative loads
677 // (for R6 only; base_reg must be ZERO).
678 void LoadLiteral(Register dest_reg, Register base_reg, Literal* literal);
679
jeffhao7fbee072012-08-24 17:56:54 -0700680 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200681 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700682 //
683
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200684 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800685 void BuildFrame(size_t frame_size,
686 ManagedRegister method_reg,
Vladimir Marko32248382016-05-19 10:37:24 +0100687 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700688 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700689
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200690 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100691 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700692 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700693
Ian Rogersdd7624d2014-03-14 17:43:00 -0700694 void IncreaseFrameSize(size_t adjust) OVERRIDE;
695 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700696
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200697 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700698 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
699 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
700 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700701
Ian Rogersdd7624d2014-03-14 17:43:00 -0700702 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700703
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700704 void StoreStackOffsetToThread(ThreadOffset32 thr_offs,
705 FrameOffset fr_offs,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800706 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700707
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700708 void StoreStackPointerToThread(ThreadOffset32 thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700709
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800710 void StoreSpanning(FrameOffset dest,
711 ManagedRegister msrc,
712 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700713 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700714
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200715 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700716 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700717
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700718 void LoadFromThread(ManagedRegister mdest, ThreadOffset32 src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700719
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700721
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800722 void LoadRef(ManagedRegister mdest,
723 ManagedRegister base,
724 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100725 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700726
Ian Rogersdd7624d2014-03-14 17:43:00 -0700727 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700728
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700729 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset32 offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700730
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200731 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700732 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700733
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700734 void CopyRawPtrFromThread(FrameOffset fr_offs,
735 ThreadOffset32 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700736 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700737
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700738 void CopyRawPtrToThread(ThreadOffset32 thr_offs,
739 FrameOffset fr_offs,
740 ManagedRegister mscratch) OVERRIDE;
741
Ian Rogersdd7624d2014-03-14 17:43:00 -0700742 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700743
Ian Rogersdd7624d2014-03-14 17:43:00 -0700744 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700745
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800746 void Copy(FrameOffset dest,
747 ManagedRegister src_base,
748 Offset src_offset,
749 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700750 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700751
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800752 void Copy(ManagedRegister dest_base,
753 Offset dest_offset,
754 FrameOffset src,
755 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700756 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700757
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800758 void Copy(FrameOffset dest,
759 FrameOffset src_base,
760 Offset src_offset,
761 ManagedRegister mscratch,
762 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700763
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800764 void Copy(ManagedRegister dest,
765 Offset dest_offset,
766 ManagedRegister src,
767 Offset src_offset,
768 ManagedRegister mscratch,
769 size_t size) OVERRIDE;
770
771 void Copy(FrameOffset dest,
772 Offset dest_offset,
773 FrameOffset src,
774 Offset src_offset,
775 ManagedRegister mscratch,
776 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700777
Ian Rogersdd7624d2014-03-14 17:43:00 -0700778 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700779
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200780 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700781 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700782
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200783 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700784 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700785
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200786 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700787 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
788 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700789
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700790 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700791 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700792 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700793 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800794 void CreateHandleScopeEntry(ManagedRegister out_reg,
795 FrameOffset handlescope_offset,
796 ManagedRegister in_reg,
797 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700798
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700799 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700800 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800801 void CreateHandleScopeEntry(FrameOffset out_off,
802 FrameOffset handlescope_offset,
803 ManagedRegister mscratch,
804 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700805
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200806 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700807 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700808
809 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
810 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700811 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
812 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700813
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200814 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700815 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
816 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700817 void CallFromThread(ThreadOffset32 offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700818
jeffhao7fbee072012-08-24 17:56:54 -0700819 // Generate code to check if Thread::Current()->exception_ is non-null
820 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700821 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700822
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200823 // Emit slow paths queued during assembly and promote short branches to long if needed.
824 void FinalizeCode() OVERRIDE;
825
826 // Emit branches and finalize all instructions.
827 void FinalizeInstructions(const MemoryRegion& region);
828
829 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
830 // must be used instead of MipsLabel::GetPosition()).
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700831 uint32_t GetLabelLocation(const MipsLabel* label) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200832
833 // Get the final position of a label after local fixup based on the old position
834 // recorded before FinalizeCode().
835 uint32_t GetAdjustedPosition(uint32_t old_position);
836
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700837 // R2 doesn't have PC-relative addressing, which we need to access literals. We simulate it by
838 // reading the PC value into a general-purpose register with the NAL instruction and then loading
839 // literals through this base register. The code generator calls this method (at most once per
840 // method being compiled) to bind a label to the location for which the PC value is acquired.
841 // The assembler then computes literal offsets relative to this label.
842 void BindPcRelBaseLabel();
843
Alexey Frunze06a46c42016-07-19 15:00:40 -0700844 // Returns the location of the label bound with BindPcRelBaseLabel().
845 uint32_t GetPcRelBaseLabelLocation() const;
846
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700847 // Note that PC-relative literal loads are handled as pseudo branches because they need very
848 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
849 // to PC.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200850 enum BranchCondition {
851 kCondLT,
852 kCondGE,
853 kCondLE,
854 kCondGT,
855 kCondLTZ,
856 kCondGEZ,
857 kCondLEZ,
858 kCondGTZ,
859 kCondEQ,
860 kCondNE,
861 kCondEQZ,
862 kCondNEZ,
863 kCondLTU,
864 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800865 kCondF, // Floating-point predicate false.
866 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200867 kUncond,
868 };
869 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
870
jeffhao7fbee072012-08-24 17:56:54 -0700871 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200872 class Branch {
873 public:
874 enum Type {
875 // R2 short branches.
876 kUncondBranch,
877 kCondBranch,
878 kCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700879 // R2 near literal.
880 kLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200881 // R2 long branches.
882 kLongUncondBranch,
883 kLongCondBranch,
884 kLongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700885 // R2 far literal.
886 kFarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200887 // R6 short branches.
888 kR6UncondBranch,
889 kR6CondBranch,
890 kR6Call,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700891 // R6 near literal.
892 kR6Literal,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200893 // R6 long branches.
894 kR6LongUncondBranch,
895 kR6LongCondBranch,
896 kR6LongCall,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700897 // R6 far literal.
898 kR6FarLiteral,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200899 };
900 // Bit sizes of offsets defined as enums to minimize chance of typos.
901 enum OffsetBits {
902 kOffset16 = 16,
903 kOffset18 = 18,
904 kOffset21 = 21,
905 kOffset23 = 23,
906 kOffset28 = 28,
907 kOffset32 = 32,
908 };
909
910 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
911 static constexpr int32_t kMaxBranchLength = 32;
912 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
913
914 struct BranchInfo {
915 // Branch length as a number of 4-byte-long instructions.
916 uint32_t length;
917 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
918 // PC-relative offset (or its most significant 16-bit half, which goes first).
919 uint32_t instr_offset;
920 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
921 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
922 // instructions) from the instruction containing the offset.
923 uint32_t pc_org;
924 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
925 // is an exception: use kOffset23 for beqzc/bnezc).
926 OffsetBits offset_size;
927 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
928 // count.
929 int offset_shift;
930 };
931 static const BranchInfo branch_info_[/* Type */];
932
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700933 // Unconditional branch or call.
934 Branch(bool is_r6, uint32_t location, uint32_t target, bool is_call);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200935 // Conditional branch.
936 Branch(bool is_r6,
937 uint32_t location,
938 uint32_t target,
939 BranchCondition condition,
940 Register lhs_reg,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700941 Register rhs_reg);
942 // Literal.
943 Branch(bool is_r6, uint32_t location, Register dest_reg, Register base_reg);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200944
945 // Some conditional branches with lhs = rhs are effectively NOPs, while some
946 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
947 // So, we need a way to identify such branches in order to emit no instructions for them
948 // or change them to unconditional.
949 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
950 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
951
952 static BranchCondition OppositeCondition(BranchCondition cond);
953
954 Type GetType() const;
955 BranchCondition GetCondition() const;
956 Register GetLeftRegister() const;
957 Register GetRightRegister() const;
958 uint32_t GetTarget() const;
959 uint32_t GetLocation() const;
960 uint32_t GetOldLocation() const;
961 uint32_t GetLength() const;
962 uint32_t GetOldLength() const;
963 uint32_t GetSize() const;
964 uint32_t GetOldSize() const;
965 uint32_t GetEndLocation() const;
966 uint32_t GetOldEndLocation() const;
967 bool IsLong() const;
968 bool IsResolved() const;
969
970 // Returns the bit size of the signed offset that the branch instruction can handle.
971 OffsetBits GetOffsetSize() const;
972
973 // Calculates the distance between two byte locations in the assembler buffer and
974 // returns the number of bits needed to represent the distance as a signed integer.
975 //
976 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
977 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
978 //
979 // Composite branches (made of several instructions) with longer reach have 32-bit
980 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800981 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
982 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
983 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
984 // Consider the following implementation of a long unconditional branch, for
985 // example:
986 //
987 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
988 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
989 //
990 // Both of the above instructions take 16-bit signed offsets as immediate operands.
991 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
992 // due to sign extension. This must be compensated for by incrementing offset_31_16
993 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
994 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
995 // Therefore, the long branch range is something like from PC - 0x80000000 to
996 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200997 //
998 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
999 // case with the addiu instruction and a 16 bit offset.
1000 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
1001
1002 // Resolve a branch when the target is known.
1003 void Resolve(uint32_t target);
1004
1005 // Relocate a branch by a given delta if needed due to expansion of this or another
1006 // branch at a given location by this delta (just changes location_ and target_).
1007 void Relocate(uint32_t expand_location, uint32_t delta);
1008
1009 // If the branch is short, changes its type to long.
1010 void PromoteToLong();
1011
1012 // If necessary, updates the type by promoting a short branch to a long branch
1013 // based on the branch location and target. Returns the amount (in bytes) by
1014 // which the branch size has increased.
1015 // max_short_distance caps the maximum distance between location_ and target_
1016 // that is allowed for short branches. This is for debugging/testing purposes.
1017 // max_short_distance = 0 forces all short branches to become long.
1018 // Use the implicit default argument when not debugging/testing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001019 uint32_t PromoteIfNeeded(uint32_t location,
1020 uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001021
1022 // Returns the location of the instruction(s) containing the offset.
1023 uint32_t GetOffsetLocation() const;
1024
1025 // Calculates and returns the offset ready for encoding in the branch instruction(s).
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001026 uint32_t GetOffset(uint32_t location) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001027
1028 private:
1029 // Completes branch construction by determining and recording its type.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001030 void InitializeType(bool is_call, bool is_literal, bool is_r6);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001031 // Helper for the above.
1032 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1033
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001034 uint32_t old_location_; // Offset into assembler buffer in bytes.
1035 uint32_t location_; // Offset into assembler buffer in bytes.
1036 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001037
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001038 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
1039 // indirect call register.
1040 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
1041 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001042
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001043 Type type_; // Current type of the branch.
1044 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001045 };
1046 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1047 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1048
jeffhao7fbee072012-08-24 17:56:54 -07001049 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
1050 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001051 void EmitI21(int opcode, Register rs, uint32_t imm21);
1052 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -07001053 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
1054 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001055 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
1056 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -07001057
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001058 void Buncond(MipsLabel* label);
1059 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001060 void Call(MipsLabel* label);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001061 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -07001062
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001063 Branch* GetBranch(uint32_t branch_id);
1064 const Branch* GetBranch(uint32_t branch_id) const;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001065 uint32_t GetBranchLocationOrPcRelBase(const MipsAssembler::Branch* branch) const;
1066 uint32_t GetBranchOrPcRelBaseForEncoding(const MipsAssembler::Branch* branch) const;
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001067
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001068 void EmitLiterals();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001069 void PromoteBranches();
1070 void EmitBranch(Branch* branch);
1071 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +01001072 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001073
1074 // Emits exception block.
1075 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
1076
1077 bool IsR6() const {
1078 if (isa_features_ != nullptr) {
1079 return isa_features_->IsR6();
1080 } else {
1081 return false;
1082 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001083 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001084
1085 bool Is32BitFPU() const {
1086 if (isa_features_ != nullptr) {
1087 return isa_features_->Is32BitFloatingPoint();
1088 } else {
1089 return true;
1090 }
Goran Jakovljevicff734982015-08-24 12:58:55 +00001091 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001092
1093 // List of exception blocks to generate at the end of the code cache.
1094 std::vector<MipsExceptionSlowPath> exception_blocks_;
1095
1096 std::vector<Branch> branches_;
1097
1098 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1099 bool overwriting_;
1100 // The current overwrite location.
1101 uint32_t overwrite_location_;
1102
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001103 // Use std::deque<> for literal labels to allow insertions at the end
1104 // without invalidating pointers and references to existing elements.
1105 ArenaDeque<Literal> literals_;
1106
1107 // There's no PC-relative addressing on MIPS32R2. So, in order to access literals relative to PC
1108 // we get PC using the NAL instruction. This label marks the position within the assembler buffer
1109 // that PC (from NAL) points to.
1110 MipsLabel pc_rel_base_label_;
1111
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +02001112 // Data for AdjustedPosition(), see the description there.
1113 uint32_t last_position_adjustment_;
1114 uint32_t last_old_position_;
1115 uint32_t last_branch_id_;
1116
1117 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +00001118
jeffhao7fbee072012-08-24 17:56:54 -07001119 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
1120};
1121
jeffhao7fbee072012-08-24 17:56:54 -07001122} // namespace mips
1123} // namespace art
1124
Ian Rogers166db042013-07-26 12:05:57 -07001125#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_