blob: 08a55ed41f2f2f0d733eef3ae8c4170cac08998c [file] [log] [blame]
Andreas Gampe57b34292015-01-14 15:45:59 -08001/*
2 * Copyright (C) 2014 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
17#ifndef ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_
18#define ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_
19
Alexey Frunze19f6c692016-11-30 19:19:55 -080020#include <deque>
Alexey Frunzea0e87b02015-09-24 22:57:20 -070021#include <utility>
Andreas Gampe57b34292015-01-14 15:45:59 -080022#include <vector>
23
Alexey Frunze19f6c692016-11-30 19:19:55 -080024#include "base/arena_containers.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070025#include "base/enums.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080026#include "base/macros.h"
27#include "constants_mips64.h"
28#include "globals.h"
29#include "managed_register_mips64.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080030#include "offsets.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070032#include "utils/jni_macro_assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070033#include "utils/label.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080034
35namespace art {
36namespace mips64 {
37
Chris Larsenc733dca2016-05-13 16:11:47 -070038enum LoadConst64Path {
39 kLoadConst64PathZero = 0x0,
40 kLoadConst64PathOri = 0x1,
41 kLoadConst64PathDaddiu = 0x2,
42 kLoadConst64PathLui = 0x4,
43 kLoadConst64PathLuiOri = 0x8,
44 kLoadConst64PathOriDahi = 0x10,
45 kLoadConst64PathOriDati = 0x20,
46 kLoadConst64PathLuiDahi = 0x40,
47 kLoadConst64PathLuiDati = 0x80,
48 kLoadConst64PathDaddiuDsrlX = 0x100,
49 kLoadConst64PathOriDsllX = 0x200,
50 kLoadConst64PathDaddiuDsllX = 0x400,
51 kLoadConst64PathLuiOriDsllX = 0x800,
52 kLoadConst64PathOriDsllXOri = 0x1000,
53 kLoadConst64PathDaddiuDsllXOri = 0x2000,
54 kLoadConst64PathDaddiuDahi = 0x4000,
55 kLoadConst64PathDaddiuDati = 0x8000,
56 kLoadConst64PathDinsu1 = 0x10000,
57 kLoadConst64PathDinsu2 = 0x20000,
58 kLoadConst64PathCatchAll = 0x40000,
59 kLoadConst64PathAllPaths = 0x7ffff,
60};
61
62template <typename Asm>
63void TemplateLoadConst32(Asm* a, GpuRegister rd, int32_t value) {
64 if (IsUint<16>(value)) {
65 // Use OR with (unsigned) immediate to encode 16b unsigned int.
66 a->Ori(rd, ZERO, value);
67 } else if (IsInt<16>(value)) {
68 // Use ADD with (signed) immediate to encode 16b signed int.
69 a->Addiu(rd, ZERO, value);
70 } else {
71 // Set 16 most significant bits of value. The "lui" instruction
72 // also clears the 16 least significant bits to zero.
73 a->Lui(rd, value >> 16);
74 if (value & 0xFFFF) {
75 // If the 16 least significant bits are non-zero, set them
76 // here.
77 a->Ori(rd, rd, value);
78 }
79 }
80}
81
82static inline int InstrCountForLoadReplicatedConst32(int64_t value) {
83 int32_t x = Low32Bits(value);
84 int32_t y = High32Bits(value);
85
86 if (x == y) {
87 return (IsUint<16>(x) || IsInt<16>(x) || ((x & 0xFFFF) == 0 && IsInt<16>(value >> 16))) ? 2 : 3;
88 }
89
90 return INT_MAX;
91}
92
93template <typename Asm, typename Rtype, typename Vtype>
94void TemplateLoadConst64(Asm* a, Rtype rd, Vtype value) {
95 int bit31 = (value & UINT64_C(0x80000000)) != 0;
96 int rep32_count = InstrCountForLoadReplicatedConst32(value);
97
98 // Loads with 1 instruction.
99 if (IsUint<16>(value)) {
100 // 64-bit value can be loaded as an unsigned 16-bit number.
101 a->RecordLoadConst64Path(kLoadConst64PathOri);
102 a->Ori(rd, ZERO, value);
103 } else if (IsInt<16>(value)) {
104 // 64-bit value can be loaded as an signed 16-bit number.
105 a->RecordLoadConst64Path(kLoadConst64PathDaddiu);
106 a->Daddiu(rd, ZERO, value);
107 } else if ((value & 0xFFFF) == 0 && IsInt<16>(value >> 16)) {
108 // 64-bit value can be loaded as an signed 32-bit number which has all
109 // of its 16 least significant bits set to zero.
110 a->RecordLoadConst64Path(kLoadConst64PathLui);
111 a->Lui(rd, value >> 16);
112 } else if (IsInt<32>(value)) {
113 // Loads with 2 instructions.
114 // 64-bit value can be loaded as an signed 32-bit number which has some
115 // or all of its 16 least significant bits set to one.
116 a->RecordLoadConst64Path(kLoadConst64PathLuiOri);
117 a->Lui(rd, value >> 16);
118 a->Ori(rd, rd, value);
119 } else if ((value & 0xFFFF0000) == 0 && IsInt<16>(value >> 32)) {
120 // 64-bit value which consists of an unsigned 16-bit value in its
121 // least significant 32-bits, and a signed 16-bit value in its
122 // most significant 32-bits.
123 a->RecordLoadConst64Path(kLoadConst64PathOriDahi);
124 a->Ori(rd, ZERO, value);
125 a->Dahi(rd, value >> 32);
126 } else if ((value & UINT64_C(0xFFFFFFFF0000)) == 0) {
127 // 64-bit value which consists of an unsigned 16-bit value in its
128 // least significant 48-bits, and a signed 16-bit value in its
129 // most significant 16-bits.
130 a->RecordLoadConst64Path(kLoadConst64PathOriDati);
131 a->Ori(rd, ZERO, value);
132 a->Dati(rd, value >> 48);
133 } else if ((value & 0xFFFF) == 0 &&
134 (-32768 - bit31) <= (value >> 32) && (value >> 32) <= (32767 - bit31)) {
135 // 16 LSBs (Least Significant Bits) all set to zero.
136 // 48 MSBs (Most Significant Bits) hold a signed 32-bit value.
137 a->RecordLoadConst64Path(kLoadConst64PathLuiDahi);
138 a->Lui(rd, value >> 16);
139 a->Dahi(rd, (value >> 32) + bit31);
140 } else if ((value & 0xFFFF) == 0 && ((value >> 31) & 0x1FFFF) == ((0x20000 - bit31) & 0x1FFFF)) {
141 // 16 LSBs all set to zero.
142 // 48 MSBs hold a signed value which can't be represented by signed
143 // 32-bit number, and the middle 16 bits are all zero, or all one.
144 a->RecordLoadConst64Path(kLoadConst64PathLuiDati);
145 a->Lui(rd, value >> 16);
146 a->Dati(rd, (value >> 48) + bit31);
147 } else if (IsInt<16>(static_cast<int32_t>(value)) &&
148 (-32768 - bit31) <= (value >> 32) && (value >> 32) <= (32767 - bit31)) {
149 // 32 LSBs contain an unsigned 16-bit number.
150 // 32 MSBs contain a signed 16-bit number.
151 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDahi);
152 a->Daddiu(rd, ZERO, value);
153 a->Dahi(rd, (value >> 32) + bit31);
154 } else if (IsInt<16>(static_cast<int32_t>(value)) &&
155 ((value >> 31) & 0x1FFFF) == ((0x20000 - bit31) & 0x1FFFF)) {
156 // 48 LSBs contain an unsigned 16-bit number.
157 // 16 MSBs contain a signed 16-bit number.
158 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDati);
159 a->Daddiu(rd, ZERO, value);
160 a->Dati(rd, (value >> 48) + bit31);
161 } else if (IsPowerOfTwo(value + UINT64_C(1))) {
162 // 64-bit values which have their "n" MSBs set to one, and their
163 // "64-n" LSBs set to zero. "n" must meet the restrictions 0 < n < 64.
164 int shift_cnt = 64 - CTZ(value + UINT64_C(1));
165 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsrlX);
166 a->Daddiu(rd, ZERO, -1);
167 if (shift_cnt < 32) {
168 a->Dsrl(rd, rd, shift_cnt);
169 } else {
170 a->Dsrl32(rd, rd, shift_cnt & 31);
171 }
172 } else {
173 int shift_cnt = CTZ(value);
174 int64_t tmp = value >> shift_cnt;
175 a->RecordLoadConst64Path(kLoadConst64PathOriDsllX);
176 if (IsUint<16>(tmp)) {
177 // Value can be computed by loading a 16-bit unsigned value, and
178 // then shifting left.
179 a->Ori(rd, ZERO, tmp);
180 if (shift_cnt < 32) {
181 a->Dsll(rd, rd, shift_cnt);
182 } else {
183 a->Dsll32(rd, rd, shift_cnt & 31);
184 }
185 } else if (IsInt<16>(tmp)) {
186 // Value can be computed by loading a 16-bit signed value, and
187 // then shifting left.
188 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsllX);
189 a->Daddiu(rd, ZERO, tmp);
190 if (shift_cnt < 32) {
191 a->Dsll(rd, rd, shift_cnt);
192 } else {
193 a->Dsll32(rd, rd, shift_cnt & 31);
194 }
195 } else if (rep32_count < 3) {
196 // Value being loaded has 32 LSBs equal to the 32 MSBs, and the
197 // value loaded into the 32 LSBs can be loaded with a single
198 // MIPS instruction.
199 a->LoadConst32(rd, value);
200 a->Dinsu(rd, rd, 32, 32);
201 a->RecordLoadConst64Path(kLoadConst64PathDinsu1);
202 } else if (IsInt<32>(tmp)) {
203 // Loads with 3 instructions.
204 // Value can be computed by loading a 32-bit signed value, and
205 // then shifting left.
206 a->RecordLoadConst64Path(kLoadConst64PathLuiOriDsllX);
207 a->Lui(rd, tmp >> 16);
208 a->Ori(rd, rd, tmp);
209 if (shift_cnt < 32) {
210 a->Dsll(rd, rd, shift_cnt);
211 } else {
212 a->Dsll32(rd, rd, shift_cnt & 31);
213 }
214 } else {
215 shift_cnt = 16 + CTZ(value >> 16);
216 tmp = value >> shift_cnt;
217 if (IsUint<16>(tmp)) {
218 // Value can be computed by loading a 16-bit unsigned value,
219 // shifting left, and "or"ing in another 16-bit unsigned value.
220 a->RecordLoadConst64Path(kLoadConst64PathOriDsllXOri);
221 a->Ori(rd, ZERO, tmp);
222 if (shift_cnt < 32) {
223 a->Dsll(rd, rd, shift_cnt);
224 } else {
225 a->Dsll32(rd, rd, shift_cnt & 31);
226 }
227 a->Ori(rd, rd, value);
228 } else if (IsInt<16>(tmp)) {
229 // Value can be computed by loading a 16-bit signed value,
230 // shifting left, and "or"ing in a 16-bit unsigned value.
231 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsllXOri);
232 a->Daddiu(rd, ZERO, tmp);
233 if (shift_cnt < 32) {
234 a->Dsll(rd, rd, shift_cnt);
235 } else {
236 a->Dsll32(rd, rd, shift_cnt & 31);
237 }
238 a->Ori(rd, rd, value);
239 } else if (rep32_count < 4) {
240 // Value being loaded has 32 LSBs equal to the 32 MSBs, and the
241 // value in the 32 LSBs requires 2 MIPS instructions to load.
242 a->LoadConst32(rd, value);
243 a->Dinsu(rd, rd, 32, 32);
244 a->RecordLoadConst64Path(kLoadConst64PathDinsu2);
245 } else {
246 // Loads with 3-4 instructions.
247 // Catch-all case to get any other 64-bit values which aren't
248 // handled by special cases above.
249 uint64_t tmp2 = value;
250 a->RecordLoadConst64Path(kLoadConst64PathCatchAll);
251 a->LoadConst32(rd, value);
252 if (bit31) {
253 tmp2 += UINT64_C(0x100000000);
254 }
255 if (((tmp2 >> 32) & 0xFFFF) != 0) {
256 a->Dahi(rd, tmp2 >> 32);
257 }
258 if (tmp2 & UINT64_C(0x800000000000)) {
259 tmp2 += UINT64_C(0x1000000000000);
260 }
261 if ((tmp2 >> 48) != 0) {
262 a->Dati(rd, tmp2 >> 48);
263 }
264 }
265 }
266 }
267}
268
Lazar Trsicd9672662015-09-03 17:33:01 +0200269static constexpr size_t kMips64WordSize = 4;
270static constexpr size_t kMips64DoublewordSize = 8;
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700271
Andreas Gampe57b34292015-01-14 15:45:59 -0800272enum LoadOperandType {
273 kLoadSignedByte,
274 kLoadUnsignedByte,
275 kLoadSignedHalfword,
276 kLoadUnsignedHalfword,
277 kLoadWord,
Douglas Leungd90957f2015-04-30 19:22:49 -0700278 kLoadUnsignedWord,
Andreas Gampe57b34292015-01-14 15:45:59 -0800279 kLoadDoubleword
280};
281
282enum StoreOperandType {
283 kStoreByte,
284 kStoreHalfword,
285 kStoreWord,
286 kStoreDoubleword
287};
288
Chris Larsen14500822015-10-01 11:35:18 -0700289// Used to test the values returned by ClassS/ClassD.
290enum FPClassMaskType {
291 kSignalingNaN = 0x001,
292 kQuietNaN = 0x002,
293 kNegativeInfinity = 0x004,
294 kNegativeNormal = 0x008,
295 kNegativeSubnormal = 0x010,
296 kNegativeZero = 0x020,
297 kPositiveInfinity = 0x040,
298 kPositiveNormal = 0x080,
299 kPositiveSubnormal = 0x100,
300 kPositiveZero = 0x200,
301};
302
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700303class Mips64Label : public Label {
304 public:
305 Mips64Label() : prev_branch_id_plus_one_(0) {}
306
307 Mips64Label(Mips64Label&& src)
308 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
309
310 private:
311 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
312
313 friend class Mips64Assembler;
314 DISALLOW_COPY_AND_ASSIGN(Mips64Label);
315};
316
Alexey Frunze19f6c692016-11-30 19:19:55 -0800317// Assembler literal is a value embedded in code, retrieved using a PC-relative load.
318class Literal {
319 public:
320 static constexpr size_t kMaxSize = 8;
321
322 Literal(uint32_t size, const uint8_t* data)
323 : label_(), size_(size) {
324 DCHECK_LE(size, Literal::kMaxSize);
325 memcpy(data_, data, size);
326 }
327
328 template <typename T>
329 T GetValue() const {
330 DCHECK_EQ(size_, sizeof(T));
331 T value;
332 memcpy(&value, data_, sizeof(T));
333 return value;
334 }
335
336 uint32_t GetSize() const {
337 return size_;
338 }
339
340 const uint8_t* GetData() const {
341 return data_;
342 }
343
344 Mips64Label* GetLabel() {
345 return &label_;
346 }
347
348 const Mips64Label* GetLabel() const {
349 return &label_;
350 }
351
352 private:
353 Mips64Label label_;
354 const uint32_t size_;
355 uint8_t data_[kMaxSize];
356
357 DISALLOW_COPY_AND_ASSIGN(Literal);
358};
359
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700360// Slowpath entered when Thread::Current()->_exception is non-null.
361class Mips64ExceptionSlowPath {
362 public:
363 explicit Mips64ExceptionSlowPath(Mips64ManagedRegister scratch, size_t stack_adjust)
364 : scratch_(scratch), stack_adjust_(stack_adjust) {}
365
366 Mips64ExceptionSlowPath(Mips64ExceptionSlowPath&& src)
367 : scratch_(src.scratch_),
368 stack_adjust_(src.stack_adjust_),
369 exception_entry_(std::move(src.exception_entry_)) {}
370
371 private:
372 Mips64Label* Entry() { return &exception_entry_; }
373 const Mips64ManagedRegister scratch_;
374 const size_t stack_adjust_;
375 Mips64Label exception_entry_;
376
377 friend class Mips64Assembler;
378 DISALLOW_COPY_AND_ASSIGN(Mips64ExceptionSlowPath);
379};
380
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700381class Mips64Assembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k64> {
Andreas Gampe57b34292015-01-14 15:45:59 -0800382 public:
Igor Murashkinae7ff922016-10-06 14:59:19 -0700383 using JNIBase = JNIMacroAssembler<PointerSize::k64>;
384
Vladimir Marko93205e32016-04-13 11:59:46 +0100385 explicit Mips64Assembler(ArenaAllocator* arena)
386 : Assembler(arena),
387 overwriting_(false),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700388 overwrite_location_(0),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800389 literals_(arena->Adapter(kArenaAllocAssembler)),
390 long_literals_(arena->Adapter(kArenaAllocAssembler)),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700391 last_position_adjustment_(0),
392 last_old_position_(0),
393 last_branch_id_(0) {
394 cfi().DelayEmittingAdvancePCs();
395 }
396
397 virtual ~Mips64Assembler() {
398 for (auto& branch : branches_) {
399 CHECK(branch.IsResolved());
400 }
401 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800402
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700403 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
404 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
405
Andreas Gampe57b34292015-01-14 15:45:59 -0800406 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800407 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
408 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
410 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800411 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
413
Alexey Frunzec857c742015-09-23 15:12:39 -0700414 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
415 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
416 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
417 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
418 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
419 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
420 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
421 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
422 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
423 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
424 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
425 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800426
427 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
428 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
429 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
430 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
431 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
432 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
433 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
434
Alexey Frunzec857c742015-09-23 15:12:39 -0700435 void Bitswap(GpuRegister rd, GpuRegister rt);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800436 void Dbitswap(GpuRegister rd, GpuRegister rt); // MIPS64
Alexey Frunzec857c742015-09-23 15:12:39 -0700437 void Seb(GpuRegister rd, GpuRegister rt);
438 void Seh(GpuRegister rd, GpuRegister rt);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800439 void Dsbh(GpuRegister rd, GpuRegister rt); // MIPS64
440 void Dshd(GpuRegister rd, GpuRegister rt); // MIPS64
Lazar Trsicd9672662015-09-03 17:33:01 +0200441 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size); // MIPS64
442 void Dinsu(GpuRegister rt, GpuRegister rs, int pos, int size); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700443 void Wsbh(GpuRegister rd, GpuRegister rt);
444 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800445 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700446 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800447 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700448
449 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
450 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700451 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700452 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
453 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
454 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700455 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700456 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
457 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
458 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze19f6c692016-11-30 19:19:55 -0800459 void Drotr(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
461 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
462 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700463 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700464 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
465 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
466 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700467 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700468 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800469
470 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
471 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
472 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700473 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800474 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
475 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700476 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze19f6c692016-11-30 19:19:55 -0800477 void Lwpc(GpuRegister rs, uint32_t imm19);
478 void Lwupc(GpuRegister rs, uint32_t imm19); // MIPS64
479 void Ldpc(GpuRegister rs, uint32_t imm18); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800480 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700481 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
482 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800484
485 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
486 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
487 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700488 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800489
490 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
491 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
492 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
493 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700494 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
495 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
496 void Clz(GpuRegister rd, GpuRegister rs);
497 void Clo(GpuRegister rd, GpuRegister rs);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800498 void Dclz(GpuRegister rd, GpuRegister rs); // MIPS64
499 void Dclo(GpuRegister rd, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800500
Alexey Frunze4dda3372015-06-01 18:31:49 -0700501 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800502 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700503 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700504 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700505 void Addiupc(GpuRegister rs, uint32_t imm19);
506 void Bc(uint32_t imm26);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800507 void Balc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700508 void Jic(GpuRegister rt, uint16_t imm16);
509 void Jialc(GpuRegister rt, uint16_t imm16);
510 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
511 void Bltzc(GpuRegister rt, uint16_t imm16);
512 void Bgtzc(GpuRegister rt, uint16_t imm16);
513 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
514 void Bgezc(GpuRegister rt, uint16_t imm16);
515 void Blezc(GpuRegister rt, uint16_t imm16);
516 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
517 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
518 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
519 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
520 void Beqzc(GpuRegister rs, uint32_t imm21);
521 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800522 void Bc1eqz(FpuRegister ft, uint16_t imm16);
523 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800524
525 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
526 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
527 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
528 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
529 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
530 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
531 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
532 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700533 void SqrtS(FpuRegister fd, FpuRegister fs);
534 void SqrtD(FpuRegister fd, FpuRegister fs);
535 void AbsS(FpuRegister fd, FpuRegister fs);
536 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800537 void MovS(FpuRegister fd, FpuRegister fs);
538 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700539 void NegS(FpuRegister fd, FpuRegister fs);
540 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700541 void RoundLS(FpuRegister fd, FpuRegister fs);
542 void RoundLD(FpuRegister fd, FpuRegister fs);
543 void RoundWS(FpuRegister fd, FpuRegister fs);
544 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800545 void TruncLS(FpuRegister fd, FpuRegister fs);
546 void TruncLD(FpuRegister fd, FpuRegister fs);
547 void TruncWS(FpuRegister fd, FpuRegister fs);
548 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700549 void CeilLS(FpuRegister fd, FpuRegister fs);
550 void CeilLD(FpuRegister fd, FpuRegister fs);
551 void CeilWS(FpuRegister fd, FpuRegister fs);
552 void CeilWD(FpuRegister fd, FpuRegister fs);
553 void FloorLS(FpuRegister fd, FpuRegister fs);
554 void FloorLD(FpuRegister fd, FpuRegister fs);
555 void FloorWS(FpuRegister fd, FpuRegister fs);
556 void FloorWD(FpuRegister fd, FpuRegister fs);
557 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
558 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
559 void RintS(FpuRegister fd, FpuRegister fs);
560 void RintD(FpuRegister fd, FpuRegister fs);
561 void ClassS(FpuRegister fd, FpuRegister fs);
562 void ClassD(FpuRegister fd, FpuRegister fs);
563 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
564 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
565 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
566 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800567 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
568 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
569 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
570 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
571 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
572 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
573 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
574 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
575 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
576 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
577 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
578 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
579 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
580 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
581 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
582 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
583 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
584 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
585 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
586 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700587
588 void Cvtsw(FpuRegister fd, FpuRegister fs);
589 void Cvtdw(FpuRegister fd, FpuRegister fs);
590 void Cvtsd(FpuRegister fd, FpuRegister fs);
591 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700592 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700593 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800594
595 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200596 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700597 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200598 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700599 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
600 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800601 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
602 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
603 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
604 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
605
606 void Break();
607 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700608 void Move(GpuRegister rd, GpuRegister rs);
609 void Clear(GpuRegister rd);
610 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800611
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700612 // Higher level composite instructions.
Chris Larsenc733dca2016-05-13 16:11:47 -0700613 int InstrCountForLoadReplicatedConst32(int64_t);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700614 void LoadConst32(GpuRegister rd, int32_t value);
615 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
616
Chris Larsenc733dca2016-05-13 16:11:47 -0700617 // This function is only used for testing purposes.
618 void RecordLoadConst64Path(int value);
619
Alexey Frunze4dda3372015-06-01 18:31:49 -0700620 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
621
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700622 void Bind(Label* label) OVERRIDE {
623 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700624 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700625 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
626 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
627 }
628
629 void Bind(Mips64Label* label);
Igor Murashkinae7ff922016-10-06 14:59:19 -0700630
631 // Don't warn about a different virtual Bind/Jump in the base class.
632 using JNIBase::Bind;
633 using JNIBase::Jump;
634
635 // Create a new label that can be used with Jump/Bind calls.
636 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
637 LOG(FATAL) << "Not implemented on MIPS64";
638 UNREACHABLE();
639 }
640 // Emit an unconditional jump to the label.
641 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
642 LOG(FATAL) << "Not implemented on MIPS64";
643 UNREACHABLE();
644 }
645 // Emit a conditional jump to the label by applying a unary condition test to the register.
646 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
647 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
648 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
649 LOG(FATAL) << "Not implemented on MIPS64";
650 UNREACHABLE();
651 }
652
653 // Code at this offset will serve as the target for the Jump call.
654 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
655 LOG(FATAL) << "Not implemented on MIPS64";
656 UNREACHABLE();
657 }
658
Alexey Frunze19f6c692016-11-30 19:19:55 -0800659 // Create a new literal with a given value.
660 // NOTE: Force the template parameter to be explicitly specified.
661 template <typename T>
662 Literal* NewLiteral(typename Identity<T>::type value) {
663 static_assert(std::is_integral<T>::value, "T must be an integral type.");
664 return NewLiteral(sizeof(value), reinterpret_cast<const uint8_t*>(&value));
665 }
666
667 // Load label address using PC-relative loads. To be used with data labels in the literal /
668 // jump table area only and not with regular code labels.
669 void LoadLabelAddress(GpuRegister dest_reg, Mips64Label* label);
670
671 // Create a new literal with the given data.
672 Literal* NewLiteral(size_t size, const uint8_t* data);
673
674 // Load literal using PC-relative loads.
675 void LoadLiteral(GpuRegister dest_reg, LoadOperandType load_type, Literal* literal);
676
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700677 void Bc(Mips64Label* label);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800678 void Balc(Mips64Label* label);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700679 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
680 void Bltzc(GpuRegister rt, Mips64Label* label);
681 void Bgtzc(GpuRegister rt, Mips64Label* label);
682 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
683 void Bgezc(GpuRegister rt, Mips64Label* label);
684 void Blezc(GpuRegister rt, Mips64Label* label);
685 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
686 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
687 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
688 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
689 void Beqzc(GpuRegister rs, Mips64Label* label);
690 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800691 void Bc1eqz(FpuRegister ft, Mips64Label* label);
692 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800693
694 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
695 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
696 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
697 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
698 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
699
700 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800702
703 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700704 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800705 //
706
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700707 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100708 void BuildFrame(size_t frame_size,
709 ManagedRegister method_reg,
710 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800711 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
712
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700713 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100714 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800715
716 void IncreaseFrameSize(size_t adjust) OVERRIDE;
717 void DecreaseFrameSize(size_t adjust) OVERRIDE;
718
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700719 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800720 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
721 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
722 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
723
724 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
725
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700726 void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
727 FrameOffset fr_offs,
728 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800729
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700730 void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800731
732 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
733 ManagedRegister mscratch) OVERRIDE;
734
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700735 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800736 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
737
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700738 void LoadFromThread(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800739
Mathieu Chartiere401d142015-04-22 13:56:20 -0700740 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800741
Mathieu Chartiere401d142015-04-22 13:56:20 -0700742 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100743 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800744
745 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
746
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700747 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800748
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700749 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800750 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
751
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700752 void CopyRawPtrFromThread(FrameOffset fr_offs,
753 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800754 ManagedRegister mscratch) OVERRIDE;
755
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700756 void CopyRawPtrToThread(ThreadOffset64 thr_offs,
757 FrameOffset fr_offs,
758 ManagedRegister mscratch) OVERRIDE;
759
Andreas Gampe57b34292015-01-14 15:45:59 -0800760 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
761
762 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
763
764 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
765 size_t size) OVERRIDE;
766
767 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
768 ManagedRegister mscratch, size_t size) OVERRIDE;
769
770 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
771 size_t size) OVERRIDE;
772
773 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
774 ManagedRegister mscratch, size_t size) OVERRIDE;
775
776 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
777 ManagedRegister mscratch, size_t size) OVERRIDE;
778
779 void MemoryBarrier(ManagedRegister) OVERRIDE;
780
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700781 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800782 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
783
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700784 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800785 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
786
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700787 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800788 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
789 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
790
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700791 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
Andreas Gampe57b34292015-01-14 15:45:59 -0800792 // value is null and null_allowed. in_reg holds a possibly stale reference
793 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700794 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800795 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
796 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
797
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700798 // Set up out_off to hold a Object** into the handle scope, or to be null if the
Andreas Gampe57b34292015-01-14 15:45:59 -0800799 // value is null and null_allowed.
800 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
801 mscratch, bool null_allowed) OVERRIDE;
802
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700803 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800804 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
805
806 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
807 // know that src may not be null.
808 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
809 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
810
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700811 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800812 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
813 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700814 void CallFromThread(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800815
816 // Generate code to check if Thread::Current()->exception_ is non-null
817 // and branch to a ExceptionSlowPath if it is.
818 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
819
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700820 // Emit slow paths queued during assembly and promote short branches to long if needed.
821 void FinalizeCode() OVERRIDE;
822
823 // Emit branches and finalize all instructions.
824 void FinalizeInstructions(const MemoryRegion& region);
825
826 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
827 // must be used instead of Mips64Label::GetPosition()).
Alexey Frunze19f6c692016-11-30 19:19:55 -0800828 uint32_t GetLabelLocation(const Mips64Label* label) const;
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700829
830 // Get the final position of a label after local fixup based on the old position
831 // recorded before FinalizeCode().
832 uint32_t GetAdjustedPosition(uint32_t old_position);
833
Alexey Frunze19f6c692016-11-30 19:19:55 -0800834 // Note that PC-relative literal loads are handled as pseudo branches because they need very
835 // similar relocation and may similarly expand in size to accomodate for larger offsets relative
836 // to PC.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700837 enum BranchCondition {
838 kCondLT,
839 kCondGE,
840 kCondLE,
841 kCondGT,
842 kCondLTZ,
843 kCondGEZ,
844 kCondLEZ,
845 kCondGTZ,
846 kCondEQ,
847 kCondNE,
848 kCondEQZ,
849 kCondNEZ,
850 kCondLTU,
851 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800852 kCondF, // Floating-point predicate false.
853 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700854 kUncond,
855 };
856 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
857
Andreas Gampe57b34292015-01-14 15:45:59 -0800858 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700859 class Branch {
860 public:
861 enum Type {
862 // Short branches.
863 kUncondBranch,
864 kCondBranch,
865 kCall,
Alexey Frunze19f6c692016-11-30 19:19:55 -0800866 // Near label.
867 kLabel,
868 // Near literals.
869 kLiteral,
870 kLiteralUnsigned,
871 kLiteralLong,
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700872 // Long branches.
873 kLongUncondBranch,
874 kLongCondBranch,
875 kLongCall,
Alexey Frunze19f6c692016-11-30 19:19:55 -0800876 // Far label.
877 kFarLabel,
878 // Far literals.
879 kFarLiteral,
880 kFarLiteralUnsigned,
881 kFarLiteralLong,
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700882 };
883
884 // Bit sizes of offsets defined as enums to minimize chance of typos.
885 enum OffsetBits {
886 kOffset16 = 16,
887 kOffset18 = 18,
888 kOffset21 = 21,
889 kOffset23 = 23,
890 kOffset28 = 28,
891 kOffset32 = 32,
892 };
893
894 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
895 static constexpr int32_t kMaxBranchLength = 32;
896 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
897
898 struct BranchInfo {
899 // Branch length as a number of 4-byte-long instructions.
900 uint32_t length;
901 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
902 // PC-relative offset (or its most significant 16-bit half, which goes first).
903 uint32_t instr_offset;
904 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
905 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
906 // instructions) from the instruction containing the offset.
907 uint32_t pc_org;
908 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
909 // an exception: use kOffset23 for beqzc/bnezc).
910 OffsetBits offset_size;
911 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
912 // count.
913 int offset_shift;
914 };
915 static const BranchInfo branch_info_[/* Type */];
916
Alexey Frunze19f6c692016-11-30 19:19:55 -0800917 // Unconditional branch or call.
918 Branch(uint32_t location, uint32_t target, bool is_call);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700919 // Conditional branch.
920 Branch(uint32_t location,
921 uint32_t target,
922 BranchCondition condition,
923 GpuRegister lhs_reg,
Alexey Frunze19f6c692016-11-30 19:19:55 -0800924 GpuRegister rhs_reg);
925 // Label address (in literal area) or literal.
926 Branch(uint32_t location, GpuRegister dest_reg, Type label_or_literal_type);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700927
928 // Some conditional branches with lhs = rhs are effectively NOPs, while some
929 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
930 // So, we need a way to identify such branches in order to emit no instructions for them
931 // or change them to unconditional.
932 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
933 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
934
935 static BranchCondition OppositeCondition(BranchCondition cond);
936
937 Type GetType() const;
938 BranchCondition GetCondition() const;
939 GpuRegister GetLeftRegister() const;
940 GpuRegister GetRightRegister() const;
941 uint32_t GetTarget() const;
942 uint32_t GetLocation() const;
943 uint32_t GetOldLocation() const;
944 uint32_t GetLength() const;
945 uint32_t GetOldLength() const;
946 uint32_t GetSize() const;
947 uint32_t GetOldSize() const;
948 uint32_t GetEndLocation() const;
949 uint32_t GetOldEndLocation() const;
950 bool IsLong() const;
951 bool IsResolved() const;
952
953 // Returns the bit size of the signed offset that the branch instruction can handle.
954 OffsetBits GetOffsetSize() const;
955
956 // Calculates the distance between two byte locations in the assembler buffer and
957 // returns the number of bits needed to represent the distance as a signed integer.
958 //
959 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
960 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
961 //
962 // Composite branches (made of several instructions) with longer reach have 32-bit
963 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
964 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
965 // however. Consider the following implementation of a long unconditional branch, for
966 // example:
967 //
968 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
969 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
970 //
971 // Both of the above instructions take 16-bit signed offsets as immediate operands.
972 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
973 // due to sign extension. This must be compensated for by incrementing offset_31_16
974 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
975 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
976 // Therefore, the long branch range is something like from PC - 0x80000000 to
977 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
978 //
979 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
980 // case with the addiu instruction and a 16 bit offset.
981 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
982
983 // Resolve a branch when the target is known.
984 void Resolve(uint32_t target);
985
986 // Relocate a branch by a given delta if needed due to expansion of this or another
987 // branch at a given location by this delta (just changes location_ and target_).
988 void Relocate(uint32_t expand_location, uint32_t delta);
989
990 // If the branch is short, changes its type to long.
991 void PromoteToLong();
992
993 // If necessary, updates the type by promoting a short branch to a long branch
994 // based on the branch location and target. Returns the amount (in bytes) by
995 // which the branch size has increased.
996 // max_short_distance caps the maximum distance between location_ and target_
997 // that is allowed for short branches. This is for debugging/testing purposes.
998 // max_short_distance = 0 forces all short branches to become long.
999 // Use the implicit default argument when not debugging/testing.
1000 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
1001
1002 // Returns the location of the instruction(s) containing the offset.
1003 uint32_t GetOffsetLocation() const;
1004
1005 // Calculates and returns the offset ready for encoding in the branch instruction(s).
1006 uint32_t GetOffset() const;
1007
1008 private:
1009 // Completes branch construction by determining and recording its type.
Alexey Frunze19f6c692016-11-30 19:19:55 -08001010 void InitializeType(Type initial_type);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001011 // Helper for the above.
1012 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
1013
1014 uint32_t old_location_; // Offset into assembler buffer in bytes.
1015 uint32_t location_; // Offset into assembler buffer in bytes.
1016 uint32_t target_; // Offset into assembler buffer in bytes.
1017
1018 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
Alexey Frunze19f6c692016-11-30 19:19:55 -08001019 // destination register in literals.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001020 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
1021 BranchCondition condition_; // Condition for conditional branches.
1022
1023 Type type_; // Current type of the branch.
1024 Type old_type_; // Initial type of the branch.
1025 };
1026 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
1027 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
1028
Andreas Gampe57b34292015-01-14 15:45:59 -08001029 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -07001030 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
1031 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -08001032 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001033 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001034 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -08001035 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
1036 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001037 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
1038
1039 void Buncond(Mips64Label* label);
1040 void Bcond(Mips64Label* label,
1041 BranchCondition condition,
1042 GpuRegister lhs,
1043 GpuRegister rhs = ZERO);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001044 void Call(Mips64Label* label);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001045 void FinalizeLabeledBranch(Mips64Label* label);
1046
1047 Branch* GetBranch(uint32_t branch_id);
1048 const Branch* GetBranch(uint32_t branch_id) const;
1049
Alexey Frunze19f6c692016-11-30 19:19:55 -08001050 void EmitLiterals();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001051 void PromoteBranches();
1052 void EmitBranch(Branch* branch);
1053 void EmitBranches();
1054 void PatchCFI();
1055
1056 // Emits exception block.
1057 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
1058
1059 // List of exception blocks to generate at the end of the code cache.
1060 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
1061
1062 std::vector<Branch> branches_;
1063
1064 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
1065 bool overwriting_;
1066 // The current overwrite location.
1067 uint32_t overwrite_location_;
1068
Alexey Frunze19f6c692016-11-30 19:19:55 -08001069 // Use std::deque<> for literal labels to allow insertions at the end
1070 // without invalidating pointers and references to existing elements.
1071 ArenaDeque<Literal> literals_;
1072 ArenaDeque<Literal> long_literals_; // 64-bit literals separated for alignment reasons.
1073
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001074 // Data for AdjustedPosition(), see the description there.
1075 uint32_t last_position_adjustment_;
1076 uint32_t last_old_position_;
1077 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -08001078
Andreas Gampe57b34292015-01-14 15:45:59 -08001079 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
1080};
1081
Andreas Gampe57b34292015-01-14 15:45:59 -08001082} // namespace mips64
1083} // namespace art
1084
1085#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_