blob: 6277b5d66dd2bf12416d57756f66bac2b0244fbf [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:
Vladimir Marko93205e32016-04-13 11:59:46 +0100338 explicit Mips64Assembler(ArenaAllocator* arena)
339 : Assembler(arena),
340 overwriting_(false),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700341 overwrite_location_(0),
342 last_position_adjustment_(0),
343 last_old_position_(0),
344 last_branch_id_(0) {
345 cfi().DelayEmittingAdvancePCs();
346 }
347
348 virtual ~Mips64Assembler() {
349 for (auto& branch : branches_) {
350 CHECK(branch.IsResolved());
351 }
352 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800353
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700354 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
355 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
356
Andreas Gampe57b34292015-01-14 15:45:59 -0800357 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800358 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
359 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700360 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
361 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800362 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
364
Alexey Frunzec857c742015-09-23 15:12:39 -0700365 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
366 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
367 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
368 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
369 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
370 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
371 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
372 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
373 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
374 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
375 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
376 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800377
378 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
379 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
380 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
381 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
382 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
383 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
384 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
385
Alexey Frunzec857c742015-09-23 15:12:39 -0700386 void Bitswap(GpuRegister rd, GpuRegister rt);
387 void Dbitswap(GpuRegister rd, GpuRegister rt);
388 void Seb(GpuRegister rd, GpuRegister rt);
389 void Seh(GpuRegister rd, GpuRegister rt);
390 void Dsbh(GpuRegister rd, GpuRegister rt);
391 void Dshd(GpuRegister rd, GpuRegister rt);
Lazar Trsicd9672662015-09-03 17:33:01 +0200392 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size); // MIPS64
393 void Dinsu(GpuRegister rt, GpuRegister rs, int pos, int size); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700394 void Wsbh(GpuRegister rd, GpuRegister rt);
395 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
396 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
397 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
398 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399
400 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
401 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700402 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
404 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
405 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700406 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
408 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
409 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700410 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
412 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
413 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700414 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700415 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
416 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
417 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700418 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800420
421 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
422 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
423 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700424 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800425 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
426 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700427 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800428 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700429 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
430 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800432
433 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
434 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
435 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700436 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800437
438 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
439 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
440 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
441 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700442 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
443 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
444 void Clz(GpuRegister rd, GpuRegister rs);
445 void Clo(GpuRegister rd, GpuRegister rs);
446 void Dclz(GpuRegister rd, GpuRegister rs);
447 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800448
Alexey Frunze4dda3372015-06-01 18:31:49 -0700449 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800450 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700451 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700452 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700453 void Addiupc(GpuRegister rs, uint32_t imm19);
454 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700455 void Jic(GpuRegister rt, uint16_t imm16);
456 void Jialc(GpuRegister rt, uint16_t imm16);
457 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
458 void Bltzc(GpuRegister rt, uint16_t imm16);
459 void Bgtzc(GpuRegister rt, uint16_t imm16);
460 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
461 void Bgezc(GpuRegister rt, uint16_t imm16);
462 void Blezc(GpuRegister rt, uint16_t imm16);
463 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
464 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
465 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
466 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
467 void Beqzc(GpuRegister rs, uint32_t imm21);
468 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800469 void Bc1eqz(FpuRegister ft, uint16_t imm16);
470 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800471
472 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
473 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
474 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
475 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
476 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
477 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
478 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
479 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700480 void SqrtS(FpuRegister fd, FpuRegister fs);
481 void SqrtD(FpuRegister fd, FpuRegister fs);
482 void AbsS(FpuRegister fd, FpuRegister fs);
483 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800484 void MovS(FpuRegister fd, FpuRegister fs);
485 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700486 void NegS(FpuRegister fd, FpuRegister fs);
487 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700488 void RoundLS(FpuRegister fd, FpuRegister fs);
489 void RoundLD(FpuRegister fd, FpuRegister fs);
490 void RoundWS(FpuRegister fd, FpuRegister fs);
491 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800492 void TruncLS(FpuRegister fd, FpuRegister fs);
493 void TruncLD(FpuRegister fd, FpuRegister fs);
494 void TruncWS(FpuRegister fd, FpuRegister fs);
495 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700496 void CeilLS(FpuRegister fd, FpuRegister fs);
497 void CeilLD(FpuRegister fd, FpuRegister fs);
498 void CeilWS(FpuRegister fd, FpuRegister fs);
499 void CeilWD(FpuRegister fd, FpuRegister fs);
500 void FloorLS(FpuRegister fd, FpuRegister fs);
501 void FloorLD(FpuRegister fd, FpuRegister fs);
502 void FloorWS(FpuRegister fd, FpuRegister fs);
503 void FloorWD(FpuRegister fd, FpuRegister fs);
504 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
505 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
506 void RintS(FpuRegister fd, FpuRegister fs);
507 void RintD(FpuRegister fd, FpuRegister fs);
508 void ClassS(FpuRegister fd, FpuRegister fs);
509 void ClassD(FpuRegister fd, FpuRegister fs);
510 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
511 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
512 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
513 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800514 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
515 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
516 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
517 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
518 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
519 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
520 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
521 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
522 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
523 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
524 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
525 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
526 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
527 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
528 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
529 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
530 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
531 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
532 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
533 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700534
535 void Cvtsw(FpuRegister fd, FpuRegister fs);
536 void Cvtdw(FpuRegister fd, FpuRegister fs);
537 void Cvtsd(FpuRegister fd, FpuRegister fs);
538 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700539 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700540 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800541
542 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200543 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700544 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200545 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700546 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
547 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800548 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
549 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
550 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
551 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
552
553 void Break();
554 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700555 void Move(GpuRegister rd, GpuRegister rs);
556 void Clear(GpuRegister rd);
557 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800558
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700559 // Higher level composite instructions.
Chris Larsenc733dca2016-05-13 16:11:47 -0700560 int InstrCountForLoadReplicatedConst32(int64_t);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700561 void LoadConst32(GpuRegister rd, int32_t value);
562 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
563
Chris Larsenc733dca2016-05-13 16:11:47 -0700564 // This function is only used for testing purposes.
565 void RecordLoadConst64Path(int value);
566
Alexey Frunze4dda3372015-06-01 18:31:49 -0700567 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
568
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700569 void Bind(Label* label) OVERRIDE {
570 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700571 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700572 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
573 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
574 }
575
576 void Bind(Mips64Label* label);
577 void Bc(Mips64Label* label);
578 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
579 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
580 void Bltzc(GpuRegister rt, Mips64Label* label);
581 void Bgtzc(GpuRegister rt, Mips64Label* label);
582 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
583 void Bgezc(GpuRegister rt, Mips64Label* label);
584 void Blezc(GpuRegister rt, Mips64Label* label);
585 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
586 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
587 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
588 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
589 void Beqzc(GpuRegister rs, Mips64Label* label);
590 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800591 void Bc1eqz(FpuRegister ft, Mips64Label* label);
592 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800593
594 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
595 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
596 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
597 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
598 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
599
600 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700601 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800602
603 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700604 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800605 //
606
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700607 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100608 void BuildFrame(size_t frame_size,
609 ManagedRegister method_reg,
610 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800611 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
612
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700613 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100614 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800615
616 void IncreaseFrameSize(size_t adjust) OVERRIDE;
617 void DecreaseFrameSize(size_t adjust) OVERRIDE;
618
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700619 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800620 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
621 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
622 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
623
624 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
625
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700626 void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
627 FrameOffset fr_offs,
628 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800629
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700630 void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800631
632 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
633 ManagedRegister mscratch) OVERRIDE;
634
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700635 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800636 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
637
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700638 void LoadFromThread(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800639
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800641
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100643 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800644
645 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
646
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700647 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800648
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700649 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800650 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
651
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700652 void CopyRawPtrFromThread(FrameOffset fr_offs,
653 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800654 ManagedRegister mscratch) OVERRIDE;
655
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700656 void CopyRawPtrToThread(ThreadOffset64 thr_offs,
657 FrameOffset fr_offs,
658 ManagedRegister mscratch) OVERRIDE;
659
Andreas Gampe57b34292015-01-14 15:45:59 -0800660 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
661
662 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
663
664 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
665 size_t size) OVERRIDE;
666
667 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
668 ManagedRegister mscratch, size_t size) OVERRIDE;
669
670 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
671 size_t size) OVERRIDE;
672
673 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
674 ManagedRegister mscratch, size_t size) OVERRIDE;
675
676 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
677 ManagedRegister mscratch, size_t size) OVERRIDE;
678
679 void MemoryBarrier(ManagedRegister) OVERRIDE;
680
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700681 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800682 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
683
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700684 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800685 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
686
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700687 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800688 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
689 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
690
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700691 // 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 -0800692 // value is null and null_allowed. in_reg holds a possibly stale reference
693 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700694 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800695 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
696 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
697
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 // 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 -0800699 // value is null and null_allowed.
700 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
701 mscratch, bool null_allowed) OVERRIDE;
702
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700703 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800704 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
705
706 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
707 // know that src may not be null.
708 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
709 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
710
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700711 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800712 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
713 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700714 void CallFromThread(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800715
716 // Generate code to check if Thread::Current()->exception_ is non-null
717 // and branch to a ExceptionSlowPath if it is.
718 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
719
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700720 // Emit slow paths queued during assembly and promote short branches to long if needed.
721 void FinalizeCode() OVERRIDE;
722
723 // Emit branches and finalize all instructions.
724 void FinalizeInstructions(const MemoryRegion& region);
725
726 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
727 // must be used instead of Mips64Label::GetPosition()).
728 uint32_t GetLabelLocation(Mips64Label* label) const;
729
730 // Get the final position of a label after local fixup based on the old position
731 // recorded before FinalizeCode().
732 uint32_t GetAdjustedPosition(uint32_t old_position);
733
734 enum BranchCondition {
735 kCondLT,
736 kCondGE,
737 kCondLE,
738 kCondGT,
739 kCondLTZ,
740 kCondGEZ,
741 kCondLEZ,
742 kCondGTZ,
743 kCondEQ,
744 kCondNE,
745 kCondEQZ,
746 kCondNEZ,
747 kCondLTU,
748 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800749 kCondF, // Floating-point predicate false.
750 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700751 kUncond,
752 };
753 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
754
Andreas Gampe57b34292015-01-14 15:45:59 -0800755 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700756 class Branch {
757 public:
758 enum Type {
759 // Short branches.
760 kUncondBranch,
761 kCondBranch,
762 kCall,
763 // Long branches.
764 kLongUncondBranch,
765 kLongCondBranch,
766 kLongCall,
767 };
768
769 // Bit sizes of offsets defined as enums to minimize chance of typos.
770 enum OffsetBits {
771 kOffset16 = 16,
772 kOffset18 = 18,
773 kOffset21 = 21,
774 kOffset23 = 23,
775 kOffset28 = 28,
776 kOffset32 = 32,
777 };
778
779 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
780 static constexpr int32_t kMaxBranchLength = 32;
781 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
782
783 struct BranchInfo {
784 // Branch length as a number of 4-byte-long instructions.
785 uint32_t length;
786 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
787 // PC-relative offset (or its most significant 16-bit half, which goes first).
788 uint32_t instr_offset;
789 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
790 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
791 // instructions) from the instruction containing the offset.
792 uint32_t pc_org;
793 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
794 // an exception: use kOffset23 for beqzc/bnezc).
795 OffsetBits offset_size;
796 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
797 // count.
798 int offset_shift;
799 };
800 static const BranchInfo branch_info_[/* Type */];
801
802 // Unconditional branch.
803 Branch(uint32_t location, uint32_t target);
804 // Conditional branch.
805 Branch(uint32_t location,
806 uint32_t target,
807 BranchCondition condition,
808 GpuRegister lhs_reg,
809 GpuRegister rhs_reg = ZERO);
810 // Call (branch and link) that stores the target address in a given register (i.e. T9).
811 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
812
813 // Some conditional branches with lhs = rhs are effectively NOPs, while some
814 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
815 // So, we need a way to identify such branches in order to emit no instructions for them
816 // or change them to unconditional.
817 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
818 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
819
820 static BranchCondition OppositeCondition(BranchCondition cond);
821
822 Type GetType() const;
823 BranchCondition GetCondition() const;
824 GpuRegister GetLeftRegister() const;
825 GpuRegister GetRightRegister() const;
826 uint32_t GetTarget() const;
827 uint32_t GetLocation() const;
828 uint32_t GetOldLocation() const;
829 uint32_t GetLength() const;
830 uint32_t GetOldLength() const;
831 uint32_t GetSize() const;
832 uint32_t GetOldSize() const;
833 uint32_t GetEndLocation() const;
834 uint32_t GetOldEndLocation() const;
835 bool IsLong() const;
836 bool IsResolved() const;
837
838 // Returns the bit size of the signed offset that the branch instruction can handle.
839 OffsetBits GetOffsetSize() const;
840
841 // Calculates the distance between two byte locations in the assembler buffer and
842 // returns the number of bits needed to represent the distance as a signed integer.
843 //
844 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
845 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
846 //
847 // Composite branches (made of several instructions) with longer reach have 32-bit
848 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
849 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
850 // however. Consider the following implementation of a long unconditional branch, for
851 // example:
852 //
853 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
854 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
855 //
856 // Both of the above instructions take 16-bit signed offsets as immediate operands.
857 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
858 // due to sign extension. This must be compensated for by incrementing offset_31_16
859 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
860 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
861 // Therefore, the long branch range is something like from PC - 0x80000000 to
862 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
863 //
864 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
865 // case with the addiu instruction and a 16 bit offset.
866 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
867
868 // Resolve a branch when the target is known.
869 void Resolve(uint32_t target);
870
871 // Relocate a branch by a given delta if needed due to expansion of this or another
872 // branch at a given location by this delta (just changes location_ and target_).
873 void Relocate(uint32_t expand_location, uint32_t delta);
874
875 // If the branch is short, changes its type to long.
876 void PromoteToLong();
877
878 // If necessary, updates the type by promoting a short branch to a long branch
879 // based on the branch location and target. Returns the amount (in bytes) by
880 // which the branch size has increased.
881 // max_short_distance caps the maximum distance between location_ and target_
882 // that is allowed for short branches. This is for debugging/testing purposes.
883 // max_short_distance = 0 forces all short branches to become long.
884 // Use the implicit default argument when not debugging/testing.
885 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
886
887 // Returns the location of the instruction(s) containing the offset.
888 uint32_t GetOffsetLocation() const;
889
890 // Calculates and returns the offset ready for encoding in the branch instruction(s).
891 uint32_t GetOffset() const;
892
893 private:
894 // Completes branch construction by determining and recording its type.
895 void InitializeType(bool is_call);
896 // Helper for the above.
897 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
898
899 uint32_t old_location_; // Offset into assembler buffer in bytes.
900 uint32_t location_; // Offset into assembler buffer in bytes.
901 uint32_t target_; // Offset into assembler buffer in bytes.
902
903 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
904 // indirect call register.
905 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
906 BranchCondition condition_; // Condition for conditional branches.
907
908 Type type_; // Current type of the branch.
909 Type old_type_; // Initial type of the branch.
910 };
911 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
912 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
913
Andreas Gampe57b34292015-01-14 15:45:59 -0800914 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700915 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
916 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800917 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700918 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700919 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800920 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
921 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700922 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
923
924 void Buncond(Mips64Label* label);
925 void Bcond(Mips64Label* label,
926 BranchCondition condition,
927 GpuRegister lhs,
928 GpuRegister rhs = ZERO);
929 void Call(Mips64Label* label, GpuRegister indirect_reg);
930 void FinalizeLabeledBranch(Mips64Label* label);
931
932 Branch* GetBranch(uint32_t branch_id);
933 const Branch* GetBranch(uint32_t branch_id) const;
934
935 void PromoteBranches();
936 void EmitBranch(Branch* branch);
937 void EmitBranches();
938 void PatchCFI();
939
940 // Emits exception block.
941 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
942
943 // List of exception blocks to generate at the end of the code cache.
944 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
945
946 std::vector<Branch> branches_;
947
948 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
949 bool overwriting_;
950 // The current overwrite location.
951 uint32_t overwrite_location_;
952
953 // Data for AdjustedPosition(), see the description there.
954 uint32_t last_position_adjustment_;
955 uint32_t last_old_position_;
956 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800957
Andreas Gampe57b34292015-01-14 15:45:59 -0800958 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
959};
960
Andreas Gampe57b34292015-01-14 15:45:59 -0800961} // namespace mips64
962} // namespace art
963
964#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_