blob: 7f71f96d7bdd24d48dd1a1cae68742a5fbc7fb33 [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 Frunzea0e87b02015-09-24 22:57:20 -070020#include <utility>
Andreas Gampe57b34292015-01-14 15:45:59 -080021#include <vector>
22
Andreas Gampe3b165bc2016-08-01 22:07:04 -070023#include "base/enums.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080024#include "base/macros.h"
25#include "constants_mips64.h"
26#include "globals.h"
27#include "managed_register_mips64.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080028#include "offsets.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070029#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070030#include "utils/jni_macro_assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/label.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080032
33namespace art {
34namespace mips64 {
35
Chris Larsenc733dca2016-05-13 16:11:47 -070036enum LoadConst64Path {
37 kLoadConst64PathZero = 0x0,
38 kLoadConst64PathOri = 0x1,
39 kLoadConst64PathDaddiu = 0x2,
40 kLoadConst64PathLui = 0x4,
41 kLoadConst64PathLuiOri = 0x8,
42 kLoadConst64PathOriDahi = 0x10,
43 kLoadConst64PathOriDati = 0x20,
44 kLoadConst64PathLuiDahi = 0x40,
45 kLoadConst64PathLuiDati = 0x80,
46 kLoadConst64PathDaddiuDsrlX = 0x100,
47 kLoadConst64PathOriDsllX = 0x200,
48 kLoadConst64PathDaddiuDsllX = 0x400,
49 kLoadConst64PathLuiOriDsllX = 0x800,
50 kLoadConst64PathOriDsllXOri = 0x1000,
51 kLoadConst64PathDaddiuDsllXOri = 0x2000,
52 kLoadConst64PathDaddiuDahi = 0x4000,
53 kLoadConst64PathDaddiuDati = 0x8000,
54 kLoadConst64PathDinsu1 = 0x10000,
55 kLoadConst64PathDinsu2 = 0x20000,
56 kLoadConst64PathCatchAll = 0x40000,
57 kLoadConst64PathAllPaths = 0x7ffff,
58};
59
60template <typename Asm>
61void TemplateLoadConst32(Asm* a, GpuRegister rd, int32_t value) {
62 if (IsUint<16>(value)) {
63 // Use OR with (unsigned) immediate to encode 16b unsigned int.
64 a->Ori(rd, ZERO, value);
65 } else if (IsInt<16>(value)) {
66 // Use ADD with (signed) immediate to encode 16b signed int.
67 a->Addiu(rd, ZERO, value);
68 } else {
69 // Set 16 most significant bits of value. The "lui" instruction
70 // also clears the 16 least significant bits to zero.
71 a->Lui(rd, value >> 16);
72 if (value & 0xFFFF) {
73 // If the 16 least significant bits are non-zero, set them
74 // here.
75 a->Ori(rd, rd, value);
76 }
77 }
78}
79
80static inline int InstrCountForLoadReplicatedConst32(int64_t value) {
81 int32_t x = Low32Bits(value);
82 int32_t y = High32Bits(value);
83
84 if (x == y) {
85 return (IsUint<16>(x) || IsInt<16>(x) || ((x & 0xFFFF) == 0 && IsInt<16>(value >> 16))) ? 2 : 3;
86 }
87
88 return INT_MAX;
89}
90
91template <typename Asm, typename Rtype, typename Vtype>
92void TemplateLoadConst64(Asm* a, Rtype rd, Vtype value) {
93 int bit31 = (value & UINT64_C(0x80000000)) != 0;
94 int rep32_count = InstrCountForLoadReplicatedConst32(value);
95
96 // Loads with 1 instruction.
97 if (IsUint<16>(value)) {
98 // 64-bit value can be loaded as an unsigned 16-bit number.
99 a->RecordLoadConst64Path(kLoadConst64PathOri);
100 a->Ori(rd, ZERO, value);
101 } else if (IsInt<16>(value)) {
102 // 64-bit value can be loaded as an signed 16-bit number.
103 a->RecordLoadConst64Path(kLoadConst64PathDaddiu);
104 a->Daddiu(rd, ZERO, value);
105 } else if ((value & 0xFFFF) == 0 && IsInt<16>(value >> 16)) {
106 // 64-bit value can be loaded as an signed 32-bit number which has all
107 // of its 16 least significant bits set to zero.
108 a->RecordLoadConst64Path(kLoadConst64PathLui);
109 a->Lui(rd, value >> 16);
110 } else if (IsInt<32>(value)) {
111 // Loads with 2 instructions.
112 // 64-bit value can be loaded as an signed 32-bit number which has some
113 // or all of its 16 least significant bits set to one.
114 a->RecordLoadConst64Path(kLoadConst64PathLuiOri);
115 a->Lui(rd, value >> 16);
116 a->Ori(rd, rd, value);
117 } else if ((value & 0xFFFF0000) == 0 && IsInt<16>(value >> 32)) {
118 // 64-bit value which consists of an unsigned 16-bit value in its
119 // least significant 32-bits, and a signed 16-bit value in its
120 // most significant 32-bits.
121 a->RecordLoadConst64Path(kLoadConst64PathOriDahi);
122 a->Ori(rd, ZERO, value);
123 a->Dahi(rd, value >> 32);
124 } else if ((value & UINT64_C(0xFFFFFFFF0000)) == 0) {
125 // 64-bit value which consists of an unsigned 16-bit value in its
126 // least significant 48-bits, and a signed 16-bit value in its
127 // most significant 16-bits.
128 a->RecordLoadConst64Path(kLoadConst64PathOriDati);
129 a->Ori(rd, ZERO, value);
130 a->Dati(rd, value >> 48);
131 } else if ((value & 0xFFFF) == 0 &&
132 (-32768 - bit31) <= (value >> 32) && (value >> 32) <= (32767 - bit31)) {
133 // 16 LSBs (Least Significant Bits) all set to zero.
134 // 48 MSBs (Most Significant Bits) hold a signed 32-bit value.
135 a->RecordLoadConst64Path(kLoadConst64PathLuiDahi);
136 a->Lui(rd, value >> 16);
137 a->Dahi(rd, (value >> 32) + bit31);
138 } else if ((value & 0xFFFF) == 0 && ((value >> 31) & 0x1FFFF) == ((0x20000 - bit31) & 0x1FFFF)) {
139 // 16 LSBs all set to zero.
140 // 48 MSBs hold a signed value which can't be represented by signed
141 // 32-bit number, and the middle 16 bits are all zero, or all one.
142 a->RecordLoadConst64Path(kLoadConst64PathLuiDati);
143 a->Lui(rd, value >> 16);
144 a->Dati(rd, (value >> 48) + bit31);
145 } else if (IsInt<16>(static_cast<int32_t>(value)) &&
146 (-32768 - bit31) <= (value >> 32) && (value >> 32) <= (32767 - bit31)) {
147 // 32 LSBs contain an unsigned 16-bit number.
148 // 32 MSBs contain a signed 16-bit number.
149 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDahi);
150 a->Daddiu(rd, ZERO, value);
151 a->Dahi(rd, (value >> 32) + bit31);
152 } else if (IsInt<16>(static_cast<int32_t>(value)) &&
153 ((value >> 31) & 0x1FFFF) == ((0x20000 - bit31) & 0x1FFFF)) {
154 // 48 LSBs contain an unsigned 16-bit number.
155 // 16 MSBs contain a signed 16-bit number.
156 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDati);
157 a->Daddiu(rd, ZERO, value);
158 a->Dati(rd, (value >> 48) + bit31);
159 } else if (IsPowerOfTwo(value + UINT64_C(1))) {
160 // 64-bit values which have their "n" MSBs set to one, and their
161 // "64-n" LSBs set to zero. "n" must meet the restrictions 0 < n < 64.
162 int shift_cnt = 64 - CTZ(value + UINT64_C(1));
163 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsrlX);
164 a->Daddiu(rd, ZERO, -1);
165 if (shift_cnt < 32) {
166 a->Dsrl(rd, rd, shift_cnt);
167 } else {
168 a->Dsrl32(rd, rd, shift_cnt & 31);
169 }
170 } else {
171 int shift_cnt = CTZ(value);
172 int64_t tmp = value >> shift_cnt;
173 a->RecordLoadConst64Path(kLoadConst64PathOriDsllX);
174 if (IsUint<16>(tmp)) {
175 // Value can be computed by loading a 16-bit unsigned value, and
176 // then shifting left.
177 a->Ori(rd, ZERO, tmp);
178 if (shift_cnt < 32) {
179 a->Dsll(rd, rd, shift_cnt);
180 } else {
181 a->Dsll32(rd, rd, shift_cnt & 31);
182 }
183 } else if (IsInt<16>(tmp)) {
184 // Value can be computed by loading a 16-bit signed value, and
185 // then shifting left.
186 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsllX);
187 a->Daddiu(rd, ZERO, tmp);
188 if (shift_cnt < 32) {
189 a->Dsll(rd, rd, shift_cnt);
190 } else {
191 a->Dsll32(rd, rd, shift_cnt & 31);
192 }
193 } else if (rep32_count < 3) {
194 // Value being loaded has 32 LSBs equal to the 32 MSBs, and the
195 // value loaded into the 32 LSBs can be loaded with a single
196 // MIPS instruction.
197 a->LoadConst32(rd, value);
198 a->Dinsu(rd, rd, 32, 32);
199 a->RecordLoadConst64Path(kLoadConst64PathDinsu1);
200 } else if (IsInt<32>(tmp)) {
201 // Loads with 3 instructions.
202 // Value can be computed by loading a 32-bit signed value, and
203 // then shifting left.
204 a->RecordLoadConst64Path(kLoadConst64PathLuiOriDsllX);
205 a->Lui(rd, tmp >> 16);
206 a->Ori(rd, rd, tmp);
207 if (shift_cnt < 32) {
208 a->Dsll(rd, rd, shift_cnt);
209 } else {
210 a->Dsll32(rd, rd, shift_cnt & 31);
211 }
212 } else {
213 shift_cnt = 16 + CTZ(value >> 16);
214 tmp = value >> shift_cnt;
215 if (IsUint<16>(tmp)) {
216 // Value can be computed by loading a 16-bit unsigned value,
217 // shifting left, and "or"ing in another 16-bit unsigned value.
218 a->RecordLoadConst64Path(kLoadConst64PathOriDsllXOri);
219 a->Ori(rd, ZERO, tmp);
220 if (shift_cnt < 32) {
221 a->Dsll(rd, rd, shift_cnt);
222 } else {
223 a->Dsll32(rd, rd, shift_cnt & 31);
224 }
225 a->Ori(rd, rd, value);
226 } else if (IsInt<16>(tmp)) {
227 // Value can be computed by loading a 16-bit signed value,
228 // shifting left, and "or"ing in a 16-bit unsigned value.
229 a->RecordLoadConst64Path(kLoadConst64PathDaddiuDsllXOri);
230 a->Daddiu(rd, ZERO, tmp);
231 if (shift_cnt < 32) {
232 a->Dsll(rd, rd, shift_cnt);
233 } else {
234 a->Dsll32(rd, rd, shift_cnt & 31);
235 }
236 a->Ori(rd, rd, value);
237 } else if (rep32_count < 4) {
238 // Value being loaded has 32 LSBs equal to the 32 MSBs, and the
239 // value in the 32 LSBs requires 2 MIPS instructions to load.
240 a->LoadConst32(rd, value);
241 a->Dinsu(rd, rd, 32, 32);
242 a->RecordLoadConst64Path(kLoadConst64PathDinsu2);
243 } else {
244 // Loads with 3-4 instructions.
245 // Catch-all case to get any other 64-bit values which aren't
246 // handled by special cases above.
247 uint64_t tmp2 = value;
248 a->RecordLoadConst64Path(kLoadConst64PathCatchAll);
249 a->LoadConst32(rd, value);
250 if (bit31) {
251 tmp2 += UINT64_C(0x100000000);
252 }
253 if (((tmp2 >> 32) & 0xFFFF) != 0) {
254 a->Dahi(rd, tmp2 >> 32);
255 }
256 if (tmp2 & UINT64_C(0x800000000000)) {
257 tmp2 += UINT64_C(0x1000000000000);
258 }
259 if ((tmp2 >> 48) != 0) {
260 a->Dati(rd, tmp2 >> 48);
261 }
262 }
263 }
264 }
265}
266
Lazar Trsicd9672662015-09-03 17:33:01 +0200267static constexpr size_t kMips64WordSize = 4;
268static constexpr size_t kMips64DoublewordSize = 8;
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700269
Andreas Gampe57b34292015-01-14 15:45:59 -0800270enum LoadOperandType {
271 kLoadSignedByte,
272 kLoadUnsignedByte,
273 kLoadSignedHalfword,
274 kLoadUnsignedHalfword,
275 kLoadWord,
Douglas Leungd90957f2015-04-30 19:22:49 -0700276 kLoadUnsignedWord,
Andreas Gampe57b34292015-01-14 15:45:59 -0800277 kLoadDoubleword
278};
279
280enum StoreOperandType {
281 kStoreByte,
282 kStoreHalfword,
283 kStoreWord,
284 kStoreDoubleword
285};
286
Chris Larsen14500822015-10-01 11:35:18 -0700287// Used to test the values returned by ClassS/ClassD.
288enum FPClassMaskType {
289 kSignalingNaN = 0x001,
290 kQuietNaN = 0x002,
291 kNegativeInfinity = 0x004,
292 kNegativeNormal = 0x008,
293 kNegativeSubnormal = 0x010,
294 kNegativeZero = 0x020,
295 kPositiveInfinity = 0x040,
296 kPositiveNormal = 0x080,
297 kPositiveSubnormal = 0x100,
298 kPositiveZero = 0x200,
299};
300
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700301class Mips64Label : public Label {
302 public:
303 Mips64Label() : prev_branch_id_plus_one_(0) {}
304
305 Mips64Label(Mips64Label&& src)
306 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
307
308 private:
309 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
310
311 friend class Mips64Assembler;
312 DISALLOW_COPY_AND_ASSIGN(Mips64Label);
313};
314
315// Slowpath entered when Thread::Current()->_exception is non-null.
316class Mips64ExceptionSlowPath {
317 public:
318 explicit Mips64ExceptionSlowPath(Mips64ManagedRegister scratch, size_t stack_adjust)
319 : scratch_(scratch), stack_adjust_(stack_adjust) {}
320
321 Mips64ExceptionSlowPath(Mips64ExceptionSlowPath&& src)
322 : scratch_(src.scratch_),
323 stack_adjust_(src.stack_adjust_),
324 exception_entry_(std::move(src.exception_entry_)) {}
325
326 private:
327 Mips64Label* Entry() { return &exception_entry_; }
328 const Mips64ManagedRegister scratch_;
329 const size_t stack_adjust_;
330 Mips64Label exception_entry_;
331
332 friend class Mips64Assembler;
333 DISALLOW_COPY_AND_ASSIGN(Mips64ExceptionSlowPath);
334};
335
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700336class Mips64Assembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k64> {
Andreas Gampe57b34292015-01-14 15:45:59 -0800337 public:
Igor Murashkinae7ff922016-10-06 14:59:19 -0700338 using JNIBase = JNIMacroAssembler<PointerSize::k64>;
339
Vladimir Marko93205e32016-04-13 11:59:46 +0100340 explicit Mips64Assembler(ArenaAllocator* arena)
341 : Assembler(arena),
342 overwriting_(false),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700343 overwrite_location_(0),
344 last_position_adjustment_(0),
345 last_old_position_(0),
346 last_branch_id_(0) {
347 cfi().DelayEmittingAdvancePCs();
348 }
349
350 virtual ~Mips64Assembler() {
351 for (auto& branch : branches_) {
352 CHECK(branch.IsResolved());
353 }
354 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800355
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700356 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
357 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
358
Andreas Gampe57b34292015-01-14 15:45:59 -0800359 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800360 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
361 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
363 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800364 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700365 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
366
Alexey Frunzec857c742015-09-23 15:12:39 -0700367 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
368 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
369 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
370 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
371 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
372 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
373 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
374 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
375 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
376 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
377 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
378 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800379
380 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
381 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
382 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
383 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
384 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
385 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
386 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
387
Alexey Frunzec857c742015-09-23 15:12:39 -0700388 void Bitswap(GpuRegister rd, GpuRegister rt);
389 void Dbitswap(GpuRegister rd, GpuRegister rt);
390 void Seb(GpuRegister rd, GpuRegister rt);
391 void Seh(GpuRegister rd, GpuRegister rt);
392 void Dsbh(GpuRegister rd, GpuRegister rt);
393 void Dshd(GpuRegister rd, GpuRegister rt);
Lazar Trsicd9672662015-09-03 17:33:01 +0200394 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size); // MIPS64
395 void Dinsu(GpuRegister rt, GpuRegister rs, int pos, int size); // MIPS64
Chris Larsene3660592016-11-09 11:13:42 -0800396 void Lsa(GpuRegister rd, GpuRegister rs, GpuRegister rt, int saPlusOne);
397 void Dlsa(GpuRegister rd, GpuRegister rs, GpuRegister rt, int saPlusOne); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700398 void Wsbh(GpuRegister rd, GpuRegister rt);
399 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
400 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
401 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
402 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403
404 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
405 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700406 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
408 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
409 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700410 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
412 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
413 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700414 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700415 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
416 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
417 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700418 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
420 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
421 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700422 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800424
425 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
426 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
427 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700428 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800429 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
430 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800432 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700433 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
434 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700435 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800436
437 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
438 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
439 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700440 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800441
442 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
443 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
444 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
445 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700446 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
447 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
448 void Clz(GpuRegister rd, GpuRegister rs);
449 void Clo(GpuRegister rd, GpuRegister rs);
450 void Dclz(GpuRegister rd, GpuRegister rs);
451 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800452
Alexey Frunze4dda3372015-06-01 18:31:49 -0700453 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800454 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700455 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700456 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700457 void Addiupc(GpuRegister rs, uint32_t imm19);
458 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700459 void Jic(GpuRegister rt, uint16_t imm16);
460 void Jialc(GpuRegister rt, uint16_t imm16);
461 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
462 void Bltzc(GpuRegister rt, uint16_t imm16);
463 void Bgtzc(GpuRegister rt, uint16_t imm16);
464 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
465 void Bgezc(GpuRegister rt, uint16_t imm16);
466 void Blezc(GpuRegister rt, uint16_t imm16);
467 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
468 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
469 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
470 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
471 void Beqzc(GpuRegister rs, uint32_t imm21);
472 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800473 void Bc1eqz(FpuRegister ft, uint16_t imm16);
474 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800475
476 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
477 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
478 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
479 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
480 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
481 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
482 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
483 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700484 void SqrtS(FpuRegister fd, FpuRegister fs);
485 void SqrtD(FpuRegister fd, FpuRegister fs);
486 void AbsS(FpuRegister fd, FpuRegister fs);
487 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800488 void MovS(FpuRegister fd, FpuRegister fs);
489 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490 void NegS(FpuRegister fd, FpuRegister fs);
491 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700492 void RoundLS(FpuRegister fd, FpuRegister fs);
493 void RoundLD(FpuRegister fd, FpuRegister fs);
494 void RoundWS(FpuRegister fd, FpuRegister fs);
495 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800496 void TruncLS(FpuRegister fd, FpuRegister fs);
497 void TruncLD(FpuRegister fd, FpuRegister fs);
498 void TruncWS(FpuRegister fd, FpuRegister fs);
499 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700500 void CeilLS(FpuRegister fd, FpuRegister fs);
501 void CeilLD(FpuRegister fd, FpuRegister fs);
502 void CeilWS(FpuRegister fd, FpuRegister fs);
503 void CeilWD(FpuRegister fd, FpuRegister fs);
504 void FloorLS(FpuRegister fd, FpuRegister fs);
505 void FloorLD(FpuRegister fd, FpuRegister fs);
506 void FloorWS(FpuRegister fd, FpuRegister fs);
507 void FloorWD(FpuRegister fd, FpuRegister fs);
508 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
509 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
510 void RintS(FpuRegister fd, FpuRegister fs);
511 void RintD(FpuRegister fd, FpuRegister fs);
512 void ClassS(FpuRegister fd, FpuRegister fs);
513 void ClassD(FpuRegister fd, FpuRegister fs);
514 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
515 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
516 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
517 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800518 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
519 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
520 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
521 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
522 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
523 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
524 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
525 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
526 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
527 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
528 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
529 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
530 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
531 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
532 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
533 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
534 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
535 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
536 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
537 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700538
539 void Cvtsw(FpuRegister fd, FpuRegister fs);
540 void Cvtdw(FpuRegister fd, FpuRegister fs);
541 void Cvtsd(FpuRegister fd, FpuRegister fs);
542 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700543 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700544 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800545
546 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200547 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700548 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200549 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700550 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
551 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800552 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
553 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
554 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
555 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
556
557 void Break();
558 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700559 void Move(GpuRegister rd, GpuRegister rs);
560 void Clear(GpuRegister rd);
561 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800562
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700563 // Higher level composite instructions.
Chris Larsenc733dca2016-05-13 16:11:47 -0700564 int InstrCountForLoadReplicatedConst32(int64_t);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700565 void LoadConst32(GpuRegister rd, int32_t value);
566 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
567
Chris Larsenc733dca2016-05-13 16:11:47 -0700568 // This function is only used for testing purposes.
569 void RecordLoadConst64Path(int value);
570
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
572
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700573 void Bind(Label* label) OVERRIDE {
574 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700575 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700576 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
577 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
578 }
579
580 void Bind(Mips64Label* label);
Igor Murashkinae7ff922016-10-06 14:59:19 -0700581
582 // Don't warn about a different virtual Bind/Jump in the base class.
583 using JNIBase::Bind;
584 using JNIBase::Jump;
585
586 // Create a new label that can be used with Jump/Bind calls.
587 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
588 LOG(FATAL) << "Not implemented on MIPS64";
589 UNREACHABLE();
590 }
591 // Emit an unconditional jump to the label.
592 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
593 LOG(FATAL) << "Not implemented on MIPS64";
594 UNREACHABLE();
595 }
596 // Emit a conditional jump to the label by applying a unary condition test to the register.
597 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
598 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
599 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
600 LOG(FATAL) << "Not implemented on MIPS64";
601 UNREACHABLE();
602 }
603
604 // Code at this offset will serve as the target for the Jump call.
605 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
606 LOG(FATAL) << "Not implemented on MIPS64";
607 UNREACHABLE();
608 }
609
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700610 void Bc(Mips64Label* label);
611 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
612 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
613 void Bltzc(GpuRegister rt, Mips64Label* label);
614 void Bgtzc(GpuRegister rt, Mips64Label* label);
615 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
616 void Bgezc(GpuRegister rt, Mips64Label* label);
617 void Blezc(GpuRegister rt, Mips64Label* label);
618 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
619 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
620 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
621 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
622 void Beqzc(GpuRegister rs, Mips64Label* label);
623 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800624 void Bc1eqz(FpuRegister ft, Mips64Label* label);
625 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800626
627 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
628 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
629 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
630 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
631 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
632
633 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800635
636 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700637 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800638 //
639
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700640 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100641 void BuildFrame(size_t frame_size,
642 ManagedRegister method_reg,
643 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800644 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
645
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700646 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100647 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800648
649 void IncreaseFrameSize(size_t adjust) OVERRIDE;
650 void DecreaseFrameSize(size_t adjust) OVERRIDE;
651
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700652 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800653 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
654 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
655 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
656
657 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
658
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700659 void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
660 FrameOffset fr_offs,
661 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800662
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700663 void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800664
665 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
666 ManagedRegister mscratch) OVERRIDE;
667
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700668 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800669 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
670
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700671 void LoadFromThread(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800672
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800674
Mathieu Chartiere401d142015-04-22 13:56:20 -0700675 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100676 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800677
678 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
679
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700680 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800681
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700682 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800683 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
684
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700685 void CopyRawPtrFromThread(FrameOffset fr_offs,
686 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800687 ManagedRegister mscratch) OVERRIDE;
688
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700689 void CopyRawPtrToThread(ThreadOffset64 thr_offs,
690 FrameOffset fr_offs,
691 ManagedRegister mscratch) OVERRIDE;
692
Andreas Gampe57b34292015-01-14 15:45:59 -0800693 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
694
695 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
696
697 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
698 size_t size) OVERRIDE;
699
700 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
701 ManagedRegister mscratch, size_t size) OVERRIDE;
702
703 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
704 size_t size) OVERRIDE;
705
706 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
707 ManagedRegister mscratch, size_t size) OVERRIDE;
708
709 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
710 ManagedRegister mscratch, size_t size) OVERRIDE;
711
712 void MemoryBarrier(ManagedRegister) OVERRIDE;
713
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700714 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800715 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
716
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700717 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800718 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
719
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700720 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800721 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
722 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
723
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700724 // 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 -0800725 // value is null and null_allowed. in_reg holds a possibly stale reference
726 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700727 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800728 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
729 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
730
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700731 // 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 -0800732 // value is null and null_allowed.
733 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
734 mscratch, bool null_allowed) OVERRIDE;
735
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700736 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800737 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
738
739 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
740 // know that src may not be null.
741 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
742 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
743
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700744 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800745 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
746 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700747 void CallFromThread(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800748
749 // Generate code to check if Thread::Current()->exception_ is non-null
750 // and branch to a ExceptionSlowPath if it is.
751 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
752
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700753 // Emit slow paths queued during assembly and promote short branches to long if needed.
754 void FinalizeCode() OVERRIDE;
755
756 // Emit branches and finalize all instructions.
757 void FinalizeInstructions(const MemoryRegion& region);
758
759 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
760 // must be used instead of Mips64Label::GetPosition()).
761 uint32_t GetLabelLocation(Mips64Label* label) const;
762
763 // Get the final position of a label after local fixup based on the old position
764 // recorded before FinalizeCode().
765 uint32_t GetAdjustedPosition(uint32_t old_position);
766
767 enum BranchCondition {
768 kCondLT,
769 kCondGE,
770 kCondLE,
771 kCondGT,
772 kCondLTZ,
773 kCondGEZ,
774 kCondLEZ,
775 kCondGTZ,
776 kCondEQ,
777 kCondNE,
778 kCondEQZ,
779 kCondNEZ,
780 kCondLTU,
781 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800782 kCondF, // Floating-point predicate false.
783 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700784 kUncond,
785 };
786 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
787
Andreas Gampe57b34292015-01-14 15:45:59 -0800788 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700789 class Branch {
790 public:
791 enum Type {
792 // Short branches.
793 kUncondBranch,
794 kCondBranch,
795 kCall,
796 // Long branches.
797 kLongUncondBranch,
798 kLongCondBranch,
799 kLongCall,
800 };
801
802 // Bit sizes of offsets defined as enums to minimize chance of typos.
803 enum OffsetBits {
804 kOffset16 = 16,
805 kOffset18 = 18,
806 kOffset21 = 21,
807 kOffset23 = 23,
808 kOffset28 = 28,
809 kOffset32 = 32,
810 };
811
812 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
813 static constexpr int32_t kMaxBranchLength = 32;
814 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
815
816 struct BranchInfo {
817 // Branch length as a number of 4-byte-long instructions.
818 uint32_t length;
819 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
820 // PC-relative offset (or its most significant 16-bit half, which goes first).
821 uint32_t instr_offset;
822 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
823 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
824 // instructions) from the instruction containing the offset.
825 uint32_t pc_org;
826 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
827 // an exception: use kOffset23 for beqzc/bnezc).
828 OffsetBits offset_size;
829 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
830 // count.
831 int offset_shift;
832 };
833 static const BranchInfo branch_info_[/* Type */];
834
835 // Unconditional branch.
836 Branch(uint32_t location, uint32_t target);
837 // Conditional branch.
838 Branch(uint32_t location,
839 uint32_t target,
840 BranchCondition condition,
841 GpuRegister lhs_reg,
842 GpuRegister rhs_reg = ZERO);
843 // Call (branch and link) that stores the target address in a given register (i.e. T9).
844 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
845
846 // Some conditional branches with lhs = rhs are effectively NOPs, while some
847 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
848 // So, we need a way to identify such branches in order to emit no instructions for them
849 // or change them to unconditional.
850 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
851 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
852
853 static BranchCondition OppositeCondition(BranchCondition cond);
854
855 Type GetType() const;
856 BranchCondition GetCondition() const;
857 GpuRegister GetLeftRegister() const;
858 GpuRegister GetRightRegister() const;
859 uint32_t GetTarget() const;
860 uint32_t GetLocation() const;
861 uint32_t GetOldLocation() const;
862 uint32_t GetLength() const;
863 uint32_t GetOldLength() const;
864 uint32_t GetSize() const;
865 uint32_t GetOldSize() const;
866 uint32_t GetEndLocation() const;
867 uint32_t GetOldEndLocation() const;
868 bool IsLong() const;
869 bool IsResolved() const;
870
871 // Returns the bit size of the signed offset that the branch instruction can handle.
872 OffsetBits GetOffsetSize() const;
873
874 // Calculates the distance between two byte locations in the assembler buffer and
875 // returns the number of bits needed to represent the distance as a signed integer.
876 //
877 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
878 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
879 //
880 // Composite branches (made of several instructions) with longer reach have 32-bit
881 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
882 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
883 // however. Consider the following implementation of a long unconditional branch, for
884 // example:
885 //
886 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
887 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
888 //
889 // Both of the above instructions take 16-bit signed offsets as immediate operands.
890 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
891 // due to sign extension. This must be compensated for by incrementing offset_31_16
892 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
893 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
894 // Therefore, the long branch range is something like from PC - 0x80000000 to
895 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
896 //
897 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
898 // case with the addiu instruction and a 16 bit offset.
899 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
900
901 // Resolve a branch when the target is known.
902 void Resolve(uint32_t target);
903
904 // Relocate a branch by a given delta if needed due to expansion of this or another
905 // branch at a given location by this delta (just changes location_ and target_).
906 void Relocate(uint32_t expand_location, uint32_t delta);
907
908 // If the branch is short, changes its type to long.
909 void PromoteToLong();
910
911 // If necessary, updates the type by promoting a short branch to a long branch
912 // based on the branch location and target. Returns the amount (in bytes) by
913 // which the branch size has increased.
914 // max_short_distance caps the maximum distance between location_ and target_
915 // that is allowed for short branches. This is for debugging/testing purposes.
916 // max_short_distance = 0 forces all short branches to become long.
917 // Use the implicit default argument when not debugging/testing.
918 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
919
920 // Returns the location of the instruction(s) containing the offset.
921 uint32_t GetOffsetLocation() const;
922
923 // Calculates and returns the offset ready for encoding in the branch instruction(s).
924 uint32_t GetOffset() const;
925
926 private:
927 // Completes branch construction by determining and recording its type.
928 void InitializeType(bool is_call);
929 // Helper for the above.
930 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
931
932 uint32_t old_location_; // Offset into assembler buffer in bytes.
933 uint32_t location_; // Offset into assembler buffer in bytes.
934 uint32_t target_; // Offset into assembler buffer in bytes.
935
936 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
937 // indirect call register.
938 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
939 BranchCondition condition_; // Condition for conditional branches.
940
941 Type type_; // Current type of the branch.
942 Type old_type_; // Initial type of the branch.
943 };
944 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
945 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
946
Andreas Gampe57b34292015-01-14 15:45:59 -0800947 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700948 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
949 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800950 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700952 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800953 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
954 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700955 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
956
957 void Buncond(Mips64Label* label);
958 void Bcond(Mips64Label* label,
959 BranchCondition condition,
960 GpuRegister lhs,
961 GpuRegister rhs = ZERO);
962 void Call(Mips64Label* label, GpuRegister indirect_reg);
963 void FinalizeLabeledBranch(Mips64Label* label);
964
965 Branch* GetBranch(uint32_t branch_id);
966 const Branch* GetBranch(uint32_t branch_id) const;
967
968 void PromoteBranches();
969 void EmitBranch(Branch* branch);
970 void EmitBranches();
971 void PatchCFI();
972
973 // Emits exception block.
974 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
975
976 // List of exception blocks to generate at the end of the code cache.
977 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
978
979 std::vector<Branch> branches_;
980
981 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
982 bool overwriting_;
983 // The current overwrite location.
984 uint32_t overwrite_location_;
985
986 // Data for AdjustedPosition(), see the description there.
987 uint32_t last_position_adjustment_;
988 uint32_t last_old_position_;
989 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800990
Andreas Gampe57b34292015-01-14 15:45:59 -0800991 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
992};
993
Andreas Gampe57b34292015-01-14 15:45:59 -0800994} // namespace mips64
995} // namespace art
996
997#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_