blob: 238cb9d765e9c77a4634460dd9c954786e527782 [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 Larsen2fadd7b2015-08-14 14:56:10 -0700396 void Wsbh(GpuRegister rd, GpuRegister rt);
397 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
398 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
399 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
400 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401
402 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
403 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700404 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
406 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
407 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700408 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
410 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
411 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700412 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
414 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
415 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700416 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
418 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
419 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700420 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700421 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800422
423 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
424 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
425 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700426 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800427 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
428 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700429 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800430 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700431 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
432 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700433 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800434
435 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
436 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
437 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700438 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800439
440 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
441 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
442 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
443 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700444 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
445 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
446 void Clz(GpuRegister rd, GpuRegister rs);
447 void Clo(GpuRegister rd, GpuRegister rs);
448 void Dclz(GpuRegister rd, GpuRegister rs);
449 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800450
Alexey Frunze4dda3372015-06-01 18:31:49 -0700451 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800452 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700453 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700454 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700455 void Addiupc(GpuRegister rs, uint32_t imm19);
456 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700457 void Jic(GpuRegister rt, uint16_t imm16);
458 void Jialc(GpuRegister rt, uint16_t imm16);
459 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
460 void Bltzc(GpuRegister rt, uint16_t imm16);
461 void Bgtzc(GpuRegister rt, uint16_t imm16);
462 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
463 void Bgezc(GpuRegister rt, uint16_t imm16);
464 void Blezc(GpuRegister rt, uint16_t imm16);
465 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
466 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
467 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
468 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
469 void Beqzc(GpuRegister rs, uint32_t imm21);
470 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800471 void Bc1eqz(FpuRegister ft, uint16_t imm16);
472 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800473
474 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
475 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
476 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
477 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
478 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
479 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
480 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
481 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700482 void SqrtS(FpuRegister fd, FpuRegister fs);
483 void SqrtD(FpuRegister fd, FpuRegister fs);
484 void AbsS(FpuRegister fd, FpuRegister fs);
485 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800486 void MovS(FpuRegister fd, FpuRegister fs);
487 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700488 void NegS(FpuRegister fd, FpuRegister fs);
489 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700490 void RoundLS(FpuRegister fd, FpuRegister fs);
491 void RoundLD(FpuRegister fd, FpuRegister fs);
492 void RoundWS(FpuRegister fd, FpuRegister fs);
493 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800494 void TruncLS(FpuRegister fd, FpuRegister fs);
495 void TruncLD(FpuRegister fd, FpuRegister fs);
496 void TruncWS(FpuRegister fd, FpuRegister fs);
497 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700498 void CeilLS(FpuRegister fd, FpuRegister fs);
499 void CeilLD(FpuRegister fd, FpuRegister fs);
500 void CeilWS(FpuRegister fd, FpuRegister fs);
501 void CeilWD(FpuRegister fd, FpuRegister fs);
502 void FloorLS(FpuRegister fd, FpuRegister fs);
503 void FloorLD(FpuRegister fd, FpuRegister fs);
504 void FloorWS(FpuRegister fd, FpuRegister fs);
505 void FloorWD(FpuRegister fd, FpuRegister fs);
506 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
507 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
508 void RintS(FpuRegister fd, FpuRegister fs);
509 void RintD(FpuRegister fd, FpuRegister fs);
510 void ClassS(FpuRegister fd, FpuRegister fs);
511 void ClassD(FpuRegister fd, FpuRegister fs);
512 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
513 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
514 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
515 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800516 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
517 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
518 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
519 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
520 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
521 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
522 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
523 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
524 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
525 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
526 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
527 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
528 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
529 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
530 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
531 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
532 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
533 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
534 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
535 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700536
537 void Cvtsw(FpuRegister fd, FpuRegister fs);
538 void Cvtdw(FpuRegister fd, FpuRegister fs);
539 void Cvtsd(FpuRegister fd, FpuRegister fs);
540 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700541 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700542 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800543
544 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200545 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700546 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200547 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700548 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
549 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800550 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
551 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
552 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
553 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
554
555 void Break();
556 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700557 void Move(GpuRegister rd, GpuRegister rs);
558 void Clear(GpuRegister rd);
559 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800560
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700561 // Higher level composite instructions.
Chris Larsenc733dca2016-05-13 16:11:47 -0700562 int InstrCountForLoadReplicatedConst32(int64_t);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700563 void LoadConst32(GpuRegister rd, int32_t value);
564 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
565
Chris Larsenc733dca2016-05-13 16:11:47 -0700566 // This function is only used for testing purposes.
567 void RecordLoadConst64Path(int value);
568
Alexey Frunze4dda3372015-06-01 18:31:49 -0700569 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
570
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700571 void Bind(Label* label) OVERRIDE {
572 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700573 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700574 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
575 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
576 }
577
578 void Bind(Mips64Label* label);
Igor Murashkinae7ff922016-10-06 14:59:19 -0700579
580 // Don't warn about a different virtual Bind/Jump in the base class.
581 using JNIBase::Bind;
582 using JNIBase::Jump;
583
584 // Create a new label that can be used with Jump/Bind calls.
585 std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE {
586 LOG(FATAL) << "Not implemented on MIPS64";
587 UNREACHABLE();
588 }
589 // Emit an unconditional jump to the label.
590 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
591 LOG(FATAL) << "Not implemented on MIPS64";
592 UNREACHABLE();
593 }
594 // Emit a conditional jump to the label by applying a unary condition test to the register.
595 void Jump(JNIMacroLabel* label ATTRIBUTE_UNUSED,
596 JNIMacroUnaryCondition cond ATTRIBUTE_UNUSED,
597 ManagedRegister test ATTRIBUTE_UNUSED) OVERRIDE {
598 LOG(FATAL) << "Not implemented on MIPS64";
599 UNREACHABLE();
600 }
601
602 // Code at this offset will serve as the target for the Jump call.
603 void Bind(JNIMacroLabel* label ATTRIBUTE_UNUSED) OVERRIDE {
604 LOG(FATAL) << "Not implemented on MIPS64";
605 UNREACHABLE();
606 }
607
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700608 void Bc(Mips64Label* label);
609 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
610 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
611 void Bltzc(GpuRegister rt, Mips64Label* label);
612 void Bgtzc(GpuRegister rt, Mips64Label* label);
613 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
614 void Bgezc(GpuRegister rt, Mips64Label* label);
615 void Blezc(GpuRegister rt, Mips64Label* label);
616 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
617 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
618 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
619 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
620 void Beqzc(GpuRegister rs, Mips64Label* label);
621 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800622 void Bc1eqz(FpuRegister ft, Mips64Label* label);
623 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800624
625 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
626 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
627 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
628 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
629 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
630
631 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700632 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800633
634 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700635 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800636 //
637
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700638 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100639 void BuildFrame(size_t frame_size,
640 ManagedRegister method_reg,
641 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800642 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
643
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700644 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100645 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800646
647 void IncreaseFrameSize(size_t adjust) OVERRIDE;
648 void DecreaseFrameSize(size_t adjust) OVERRIDE;
649
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700650 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800651 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
652 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
653 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
654
655 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
656
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700657 void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
658 FrameOffset fr_offs,
659 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800660
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700661 void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800662
663 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
664 ManagedRegister mscratch) OVERRIDE;
665
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700666 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800667 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
668
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700669 void LoadFromThread(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800670
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800672
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100674 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800675
676 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
677
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700678 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800679
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700680 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800681 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
682
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700683 void CopyRawPtrFromThread(FrameOffset fr_offs,
684 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800685 ManagedRegister mscratch) OVERRIDE;
686
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700687 void CopyRawPtrToThread(ThreadOffset64 thr_offs,
688 FrameOffset fr_offs,
689 ManagedRegister mscratch) OVERRIDE;
690
Andreas Gampe57b34292015-01-14 15:45:59 -0800691 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
692
693 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
694
695 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
696 size_t size) OVERRIDE;
697
698 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
699 ManagedRegister mscratch, size_t size) OVERRIDE;
700
701 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
702 size_t size) OVERRIDE;
703
704 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
705 ManagedRegister mscratch, size_t size) OVERRIDE;
706
707 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
708 ManagedRegister mscratch, size_t size) OVERRIDE;
709
710 void MemoryBarrier(ManagedRegister) OVERRIDE;
711
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700712 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800713 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
714
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700715 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800716 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
717
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700718 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800719 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
720 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
721
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700722 // 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 -0800723 // value is null and null_allowed. in_reg holds a possibly stale reference
724 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700725 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800726 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
727 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
728
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700729 // 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 -0800730 // value is null and null_allowed.
731 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
732 mscratch, bool null_allowed) OVERRIDE;
733
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700734 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800735 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
736
737 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
738 // know that src may not be null.
739 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
740 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
741
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700742 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800743 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
744 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700745 void CallFromThread(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800746
747 // Generate code to check if Thread::Current()->exception_ is non-null
748 // and branch to a ExceptionSlowPath if it is.
749 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
750
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700751 // Emit slow paths queued during assembly and promote short branches to long if needed.
752 void FinalizeCode() OVERRIDE;
753
754 // Emit branches and finalize all instructions.
755 void FinalizeInstructions(const MemoryRegion& region);
756
757 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
758 // must be used instead of Mips64Label::GetPosition()).
759 uint32_t GetLabelLocation(Mips64Label* label) const;
760
761 // Get the final position of a label after local fixup based on the old position
762 // recorded before FinalizeCode().
763 uint32_t GetAdjustedPosition(uint32_t old_position);
764
765 enum BranchCondition {
766 kCondLT,
767 kCondGE,
768 kCondLE,
769 kCondGT,
770 kCondLTZ,
771 kCondGEZ,
772 kCondLEZ,
773 kCondGTZ,
774 kCondEQ,
775 kCondNE,
776 kCondEQZ,
777 kCondNEZ,
778 kCondLTU,
779 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800780 kCondF, // Floating-point predicate false.
781 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700782 kUncond,
783 };
784 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
785
Andreas Gampe57b34292015-01-14 15:45:59 -0800786 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700787 class Branch {
788 public:
789 enum Type {
790 // Short branches.
791 kUncondBranch,
792 kCondBranch,
793 kCall,
794 // Long branches.
795 kLongUncondBranch,
796 kLongCondBranch,
797 kLongCall,
798 };
799
800 // Bit sizes of offsets defined as enums to minimize chance of typos.
801 enum OffsetBits {
802 kOffset16 = 16,
803 kOffset18 = 18,
804 kOffset21 = 21,
805 kOffset23 = 23,
806 kOffset28 = 28,
807 kOffset32 = 32,
808 };
809
810 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
811 static constexpr int32_t kMaxBranchLength = 32;
812 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
813
814 struct BranchInfo {
815 // Branch length as a number of 4-byte-long instructions.
816 uint32_t length;
817 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
818 // PC-relative offset (or its most significant 16-bit half, which goes first).
819 uint32_t instr_offset;
820 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
821 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
822 // instructions) from the instruction containing the offset.
823 uint32_t pc_org;
824 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
825 // an exception: use kOffset23 for beqzc/bnezc).
826 OffsetBits offset_size;
827 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
828 // count.
829 int offset_shift;
830 };
831 static const BranchInfo branch_info_[/* Type */];
832
833 // Unconditional branch.
834 Branch(uint32_t location, uint32_t target);
835 // Conditional branch.
836 Branch(uint32_t location,
837 uint32_t target,
838 BranchCondition condition,
839 GpuRegister lhs_reg,
840 GpuRegister rhs_reg = ZERO);
841 // Call (branch and link) that stores the target address in a given register (i.e. T9).
842 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
843
844 // Some conditional branches with lhs = rhs are effectively NOPs, while some
845 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
846 // So, we need a way to identify such branches in order to emit no instructions for them
847 // or change them to unconditional.
848 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
849 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
850
851 static BranchCondition OppositeCondition(BranchCondition cond);
852
853 Type GetType() const;
854 BranchCondition GetCondition() const;
855 GpuRegister GetLeftRegister() const;
856 GpuRegister GetRightRegister() const;
857 uint32_t GetTarget() const;
858 uint32_t GetLocation() const;
859 uint32_t GetOldLocation() const;
860 uint32_t GetLength() const;
861 uint32_t GetOldLength() const;
862 uint32_t GetSize() const;
863 uint32_t GetOldSize() const;
864 uint32_t GetEndLocation() const;
865 uint32_t GetOldEndLocation() const;
866 bool IsLong() const;
867 bool IsResolved() const;
868
869 // Returns the bit size of the signed offset that the branch instruction can handle.
870 OffsetBits GetOffsetSize() const;
871
872 // Calculates the distance between two byte locations in the assembler buffer and
873 // returns the number of bits needed to represent the distance as a signed integer.
874 //
875 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
876 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
877 //
878 // Composite branches (made of several instructions) with longer reach have 32-bit
879 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
880 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
881 // however. Consider the following implementation of a long unconditional branch, for
882 // example:
883 //
884 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
885 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
886 //
887 // Both of the above instructions take 16-bit signed offsets as immediate operands.
888 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
889 // due to sign extension. This must be compensated for by incrementing offset_31_16
890 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
891 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
892 // Therefore, the long branch range is something like from PC - 0x80000000 to
893 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
894 //
895 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
896 // case with the addiu instruction and a 16 bit offset.
897 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
898
899 // Resolve a branch when the target is known.
900 void Resolve(uint32_t target);
901
902 // Relocate a branch by a given delta if needed due to expansion of this or another
903 // branch at a given location by this delta (just changes location_ and target_).
904 void Relocate(uint32_t expand_location, uint32_t delta);
905
906 // If the branch is short, changes its type to long.
907 void PromoteToLong();
908
909 // If necessary, updates the type by promoting a short branch to a long branch
910 // based on the branch location and target. Returns the amount (in bytes) by
911 // which the branch size has increased.
912 // max_short_distance caps the maximum distance between location_ and target_
913 // that is allowed for short branches. This is for debugging/testing purposes.
914 // max_short_distance = 0 forces all short branches to become long.
915 // Use the implicit default argument when not debugging/testing.
916 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
917
918 // Returns the location of the instruction(s) containing the offset.
919 uint32_t GetOffsetLocation() const;
920
921 // Calculates and returns the offset ready for encoding in the branch instruction(s).
922 uint32_t GetOffset() const;
923
924 private:
925 // Completes branch construction by determining and recording its type.
926 void InitializeType(bool is_call);
927 // Helper for the above.
928 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
929
930 uint32_t old_location_; // Offset into assembler buffer in bytes.
931 uint32_t location_; // Offset into assembler buffer in bytes.
932 uint32_t target_; // Offset into assembler buffer in bytes.
933
934 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
935 // indirect call register.
936 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
937 BranchCondition condition_; // Condition for conditional branches.
938
939 Type type_; // Current type of the branch.
940 Type old_type_; // Initial type of the branch.
941 };
942 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
943 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
944
Andreas Gampe57b34292015-01-14 15:45:59 -0800945 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700946 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
947 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800948 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700949 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700950 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800951 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
952 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700953 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
954
955 void Buncond(Mips64Label* label);
956 void Bcond(Mips64Label* label,
957 BranchCondition condition,
958 GpuRegister lhs,
959 GpuRegister rhs = ZERO);
960 void Call(Mips64Label* label, GpuRegister indirect_reg);
961 void FinalizeLabeledBranch(Mips64Label* label);
962
963 Branch* GetBranch(uint32_t branch_id);
964 const Branch* GetBranch(uint32_t branch_id) const;
965
966 void PromoteBranches();
967 void EmitBranch(Branch* branch);
968 void EmitBranches();
969 void PatchCFI();
970
971 // Emits exception block.
972 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
973
974 // List of exception blocks to generate at the end of the code cache.
975 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
976
977 std::vector<Branch> branches_;
978
979 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
980 bool overwriting_;
981 // The current overwrite location.
982 uint32_t overwrite_location_;
983
984 // Data for AdjustedPosition(), see the description there.
985 uint32_t last_position_adjustment_;
986 uint32_t last_old_position_;
987 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800988
Andreas Gampe57b34292015-01-14 15:45:59 -0800989 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
990};
991
Andreas Gampe57b34292015-01-14 15:45:59 -0800992} // namespace mips64
993} // namespace art
994
995#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_