blob: b078f3e4cf265393eb1b645f44bcdc6f23dcf1ac [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#include "assembler_mips64.h"
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080020#include "base/casts.h"
21#include "entrypoints/quick/quick_entrypoints.h"
22#include "memory_region.h"
23#include "thread.h"
24
25namespace art {
26namespace mips64 {
27
Alexey Frunze4dda3372015-06-01 18:31:49 -070028void Mips64Assembler::Emit(uint32_t value) {
Andreas Gampe57b34292015-01-14 15:45:59 -080029 AssemblerBuffer::EnsureCapacity ensured(&buffer_);
Alexey Frunze4dda3372015-06-01 18:31:49 -070030 buffer_.Emit<uint32_t>(value);
Andreas Gampe57b34292015-01-14 15:45:59 -080031}
32
33void Mips64Assembler::EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd,
34 int shamt, int funct) {
35 CHECK_NE(rs, kNoGpuRegister);
36 CHECK_NE(rt, kNoGpuRegister);
37 CHECK_NE(rd, kNoGpuRegister);
Alexey Frunze4dda3372015-06-01 18:31:49 -070038 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
39 static_cast<uint32_t>(rs) << kRsShift |
40 static_cast<uint32_t>(rt) << kRtShift |
41 static_cast<uint32_t>(rd) << kRdShift |
42 shamt << kShamtShift |
43 funct;
Andreas Gampe57b34292015-01-14 15:45:59 -080044 Emit(encoding);
45}
46
Chris Larsen2fadd7b2015-08-14 14:56:10 -070047void Mips64Assembler::EmitRsd(int opcode, GpuRegister rs, GpuRegister rd,
48 int shamt, int funct) {
49 CHECK_NE(rs, kNoGpuRegister);
50 CHECK_NE(rd, kNoGpuRegister);
51 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
52 static_cast<uint32_t>(rs) << kRsShift |
53 static_cast<uint32_t>(ZERO) << kRtShift |
54 static_cast<uint32_t>(rd) << kRdShift |
55 shamt << kShamtShift |
56 funct;
57 Emit(encoding);
58}
59
60void Mips64Assembler::EmitRtd(int opcode, GpuRegister rt, GpuRegister rd,
61 int shamt, int funct) {
62 CHECK_NE(rt, kNoGpuRegister);
63 CHECK_NE(rd, kNoGpuRegister);
64 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
65 static_cast<uint32_t>(ZERO) << kRsShift |
66 static_cast<uint32_t>(rt) << kRtShift |
67 static_cast<uint32_t>(rd) << kRdShift |
68 shamt << kShamtShift |
69 funct;
70 Emit(encoding);
71}
72
Andreas Gampe57b34292015-01-14 15:45:59 -080073void Mips64Assembler::EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm) {
74 CHECK_NE(rs, kNoGpuRegister);
75 CHECK_NE(rt, kNoGpuRegister);
Alexey Frunze4dda3372015-06-01 18:31:49 -070076 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
77 static_cast<uint32_t>(rs) << kRsShift |
78 static_cast<uint32_t>(rt) << kRtShift |
79 imm;
Andreas Gampe57b34292015-01-14 15:45:59 -080080 Emit(encoding);
81}
82
Alexey Frunze4dda3372015-06-01 18:31:49 -070083void Mips64Assembler::EmitI21(int opcode, GpuRegister rs, uint32_t imm21) {
84 CHECK_NE(rs, kNoGpuRegister);
85 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
86 static_cast<uint32_t>(rs) << kRsShift |
87 (imm21 & 0x1FFFFF);
88 Emit(encoding);
89}
90
91void Mips64Assembler::EmitJ(int opcode, uint32_t addr26) {
92 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
93 (addr26 & 0x3FFFFFF);
Andreas Gampe57b34292015-01-14 15:45:59 -080094 Emit(encoding);
95}
96
97void Mips64Assembler::EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd,
Alexey Frunze4dda3372015-06-01 18:31:49 -070098 int funct) {
Andreas Gampe57b34292015-01-14 15:45:59 -080099 CHECK_NE(ft, kNoFpuRegister);
100 CHECK_NE(fs, kNoFpuRegister);
101 CHECK_NE(fd, kNoFpuRegister);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700102 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
103 fmt << kFmtShift |
104 static_cast<uint32_t>(ft) << kFtShift |
105 static_cast<uint32_t>(fs) << kFsShift |
106 static_cast<uint32_t>(fd) << kFdShift |
107 funct;
Andreas Gampe57b34292015-01-14 15:45:59 -0800108 Emit(encoding);
109}
110
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111void Mips64Assembler::EmitFI(int opcode, int fmt, FpuRegister ft, uint16_t imm) {
112 CHECK_NE(ft, kNoFpuRegister);
113 uint32_t encoding = static_cast<uint32_t>(opcode) << kOpcodeShift |
114 fmt << kFmtShift |
115 static_cast<uint32_t>(ft) << kFtShift |
116 imm;
Andreas Gampe57b34292015-01-14 15:45:59 -0800117 Emit(encoding);
118}
119
Andreas Gampe57b34292015-01-14 15:45:59 -0800120void Mips64Assembler::Add(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
121 EmitR(0, rs, rt, rd, 0, 0x20);
122}
123
124void Mips64Assembler::Addi(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
125 EmitI(0x8, rs, rt, imm16);
126}
127
128void Mips64Assembler::Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
129 EmitR(0, rs, rt, rd, 0, 0x21);
130}
131
132void Mips64Assembler::Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
133 EmitI(0x9, rs, rt, imm16);
134}
135
Alexey Frunze4dda3372015-06-01 18:31:49 -0700136void Mips64Assembler::Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
137 EmitR(0, rs, rt, rd, 0, 0x2d);
138}
139
Andreas Gampe57b34292015-01-14 15:45:59 -0800140void Mips64Assembler::Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
141 EmitI(0x19, rs, rt, imm16);
142}
143
144void Mips64Assembler::Sub(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
145 EmitR(0, rs, rt, rd, 0, 0x22);
146}
147
148void Mips64Assembler::Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
149 EmitR(0, rs, rt, rd, 0, 0x23);
150}
151
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152void Mips64Assembler::Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
153 EmitR(0, rs, rt, rd, 0, 0x2f);
154}
155
156void Mips64Assembler::MultR2(GpuRegister rs, GpuRegister rt) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800157 EmitR(0, rs, rt, static_cast<GpuRegister>(0), 0, 0x18);
158}
159
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160void Mips64Assembler::MultuR2(GpuRegister rs, GpuRegister rt) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800161 EmitR(0, rs, rt, static_cast<GpuRegister>(0), 0, 0x19);
162}
163
Alexey Frunze4dda3372015-06-01 18:31:49 -0700164void Mips64Assembler::DivR2(GpuRegister rs, GpuRegister rt) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800165 EmitR(0, rs, rt, static_cast<GpuRegister>(0), 0, 0x1a);
166}
167
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168void Mips64Assembler::DivuR2(GpuRegister rs, GpuRegister rt) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800169 EmitR(0, rs, rt, static_cast<GpuRegister>(0), 0, 0x1b);
170}
171
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172void Mips64Assembler::MulR2(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
173 EmitR(0x1c, rs, rt, rd, 0, 2);
174}
175
176void Mips64Assembler::DivR2(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
177 DivR2(rs, rt);
178 Mflo(rd);
179}
180
181void Mips64Assembler::ModR2(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
182 DivR2(rs, rt);
183 Mfhi(rd);
184}
185
186void Mips64Assembler::DivuR2(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
187 DivuR2(rs, rt);
188 Mflo(rd);
189}
190
191void Mips64Assembler::ModuR2(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
192 DivuR2(rs, rt);
193 Mfhi(rd);
194}
195
196void Mips64Assembler::MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
197 EmitR(0, rs, rt, rd, 2, 0x18);
198}
199
200void Mips64Assembler::DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
201 EmitR(0, rs, rt, rd, 2, 0x1a);
202}
203
204void Mips64Assembler::ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
205 EmitR(0, rs, rt, rd, 3, 0x1a);
206}
207
208void Mips64Assembler::DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
209 EmitR(0, rs, rt, rd, 2, 0x1b);
210}
211
212void Mips64Assembler::ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
213 EmitR(0, rs, rt, rd, 3, 0x1b);
214}
215
216void Mips64Assembler::Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
217 EmitR(0, rs, rt, rd, 2, 0x1c);
218}
219
220void Mips64Assembler::Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
221 EmitR(0, rs, rt, rd, 2, 0x1e);
222}
223
224void Mips64Assembler::Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
225 EmitR(0, rs, rt, rd, 3, 0x1e);
226}
227
228void Mips64Assembler::Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
229 EmitR(0, rs, rt, rd, 2, 0x1f);
230}
231
232void Mips64Assembler::Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
233 EmitR(0, rs, rt, rd, 3, 0x1f);
234}
235
Andreas Gampe57b34292015-01-14 15:45:59 -0800236void Mips64Assembler::And(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
237 EmitR(0, rs, rt, rd, 0, 0x24);
238}
239
240void Mips64Assembler::Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
241 EmitI(0xc, rs, rt, imm16);
242}
243
244void Mips64Assembler::Or(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
245 EmitR(0, rs, rt, rd, 0, 0x25);
246}
247
248void Mips64Assembler::Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
249 EmitI(0xd, rs, rt, imm16);
250}
251
252void Mips64Assembler::Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
253 EmitR(0, rs, rt, rd, 0, 0x26);
254}
255
256void Mips64Assembler::Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
257 EmitI(0xe, rs, rt, imm16);
258}
259
260void Mips64Assembler::Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
261 EmitR(0, rs, rt, rd, 0, 0x27);
262}
263
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700264void Mips64Assembler::Bitswap(GpuRegister rd, GpuRegister rt) {
265 EmitRtd(0x1f, rt, rd, 0x0, 0x20);
266}
267
268void Mips64Assembler::Dbitswap(GpuRegister rd, GpuRegister rt) {
269 EmitRtd(0x1f, rt, rd, 0x0, 0x24);
270}
271
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272void Mips64Assembler::Seb(GpuRegister rd, GpuRegister rt) {
273 EmitR(0x1f, static_cast<GpuRegister>(0), rt, rd, 0x10, 0x20);
Andreas Gampe57b34292015-01-14 15:45:59 -0800274}
275
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276void Mips64Assembler::Seh(GpuRegister rd, GpuRegister rt) {
277 EmitR(0x1f, static_cast<GpuRegister>(0), rt, rd, 0x18, 0x20);
Andreas Gampe57b34292015-01-14 15:45:59 -0800278}
279
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700280void Mips64Assembler::Dsbh(GpuRegister rd, GpuRegister rt) {
281 EmitRtd(0x1f, rt, rd, 0x2, 0x24);
282}
283
284void Mips64Assembler::Dshd(GpuRegister rd, GpuRegister rt) {
285 EmitRtd(0x1f, rt, rd, 0x5, 0x24);
286}
287
Alexey Frunze4dda3372015-06-01 18:31:49 -0700288void Mips64Assembler::Dext(GpuRegister rt, GpuRegister rs, int pos, int size_less_one) {
289 DCHECK(0 <= pos && pos < 32) << pos;
290 DCHECK(0 <= size_less_one && size_less_one < 32) << size_less_one;
291 EmitR(0x1f, rs, rt, static_cast<GpuRegister>(size_less_one), pos, 3);
Andreas Gampe57b34292015-01-14 15:45:59 -0800292}
293
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700294void Mips64Assembler::Wsbh(GpuRegister rd, GpuRegister rt) {
295 EmitRtd(0x1f, rt, rd, 2, 0x20);
296}
297
298void Mips64Assembler::Sc(GpuRegister rt, GpuRegister base, int16_t imm9) {
299 DCHECK((-256 <= imm9) && (imm9 < 256));
300 EmitI(0x1f, base, rt, ((imm9 & 0x1FF) << 7) | 0x26);
301}
302
303void Mips64Assembler::Scd(GpuRegister rt, GpuRegister base, int16_t imm9) {
304 DCHECK((-256 <= imm9) && (imm9 < 256));
305 EmitI(0x1f, base, rt, ((imm9 & 0x1FF) << 7) | 0x27);
306}
307
308void Mips64Assembler::Ll(GpuRegister rt, GpuRegister base, int16_t imm9) {
309 DCHECK((-256 <= imm9) && (imm9 < 256));
310 EmitI(0x1f, base, rt, ((imm9 & 0x1FF) << 7) | 0x36);
311}
312
313void Mips64Assembler::Lld(GpuRegister rt, GpuRegister base, int16_t imm9) {
314 DCHECK((-256 <= imm9) && (imm9 < 256));
315 EmitI(0x1f, base, rt, ((imm9 & 0x1FF) << 7) | 0x37);
316}
317
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318void Mips64Assembler::Sll(GpuRegister rd, GpuRegister rt, int shamt) {
319 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x00);
320}
321
322void Mips64Assembler::Srl(GpuRegister rd, GpuRegister rt, int shamt) {
323 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x02);
324}
325
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700326void Mips64Assembler::Rotr(GpuRegister rd, GpuRegister rt, int shamt) {
327 EmitR(0, static_cast<GpuRegister>(1), rt, rd, shamt, 0x02);
328}
329
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330void Mips64Assembler::Sra(GpuRegister rd, GpuRegister rt, int shamt) {
331 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x03);
332}
333
334void Mips64Assembler::Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800335 EmitR(0, rs, rt, rd, 0, 0x04);
336}
337
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338void Mips64Assembler::Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800339 EmitR(0, rs, rt, rd, 0, 0x06);
340}
341
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342void Mips64Assembler::Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800343 EmitR(0, rs, rt, rd, 0, 0x07);
344}
345
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346void Mips64Assembler::Dsll(GpuRegister rd, GpuRegister rt, int shamt) {
347 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x38);
348}
349
350void Mips64Assembler::Dsrl(GpuRegister rd, GpuRegister rt, int shamt) {
351 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x3a);
352}
353
354void Mips64Assembler::Dsra(GpuRegister rd, GpuRegister rt, int shamt) {
355 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x3b);
356}
357
358void Mips64Assembler::Dsll32(GpuRegister rd, GpuRegister rt, int shamt) {
359 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x3c);
360}
361
362void Mips64Assembler::Dsrl32(GpuRegister rd, GpuRegister rt, int shamt) {
363 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x3e);
364}
365
366void Mips64Assembler::Dsra32(GpuRegister rd, GpuRegister rt, int shamt) {
367 EmitR(0, static_cast<GpuRegister>(0), rt, rd, shamt, 0x3f);
368}
369
370void Mips64Assembler::Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
371 EmitR(0, rs, rt, rd, 0, 0x14);
372}
373
374void Mips64Assembler::Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
375 EmitR(0, rs, rt, rd, 0, 0x16);
376}
377
378void Mips64Assembler::Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs) {
379 EmitR(0, rs, rt, rd, 0, 0x17);
380}
381
Andreas Gampe57b34292015-01-14 15:45:59 -0800382void Mips64Assembler::Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
383 EmitI(0x20, rs, rt, imm16);
384}
385
386void Mips64Assembler::Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
387 EmitI(0x21, rs, rt, imm16);
388}
389
390void Mips64Assembler::Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
391 EmitI(0x23, rs, rt, imm16);
392}
393
394void Mips64Assembler::Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
395 EmitI(0x37, rs, rt, imm16);
396}
397
398void Mips64Assembler::Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
399 EmitI(0x24, rs, rt, imm16);
400}
401
402void Mips64Assembler::Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
403 EmitI(0x25, rs, rt, imm16);
404}
405
Douglas Leungd90957f2015-04-30 19:22:49 -0700406void Mips64Assembler::Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
407 EmitI(0x27, rs, rt, imm16);
408}
409
Andreas Gampe57b34292015-01-14 15:45:59 -0800410void Mips64Assembler::Lui(GpuRegister rt, uint16_t imm16) {
411 EmitI(0xf, static_cast<GpuRegister>(0), rt, imm16);
412}
413
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414void Mips64Assembler::Dahi(GpuRegister rs, uint16_t imm16) {
415 EmitI(1, rs, static_cast<GpuRegister>(6), imm16);
416}
417
418void Mips64Assembler::Dati(GpuRegister rs, uint16_t imm16) {
419 EmitI(1, rs, static_cast<GpuRegister>(0x1e), imm16);
420}
421
422void Mips64Assembler::Sync(uint32_t stype) {
423 EmitR(0, static_cast<GpuRegister>(0), static_cast<GpuRegister>(0),
424 static_cast<GpuRegister>(0), stype & 0x1f, 0xf);
425}
426
Andreas Gampe57b34292015-01-14 15:45:59 -0800427void Mips64Assembler::Mfhi(GpuRegister rd) {
428 EmitR(0, static_cast<GpuRegister>(0), static_cast<GpuRegister>(0), rd, 0, 0x10);
429}
430
431void Mips64Assembler::Mflo(GpuRegister rd) {
432 EmitR(0, static_cast<GpuRegister>(0), static_cast<GpuRegister>(0), rd, 0, 0x12);
433}
434
435void Mips64Assembler::Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
436 EmitI(0x28, rs, rt, imm16);
437}
438
439void Mips64Assembler::Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
440 EmitI(0x29, rs, rt, imm16);
441}
442
443void Mips64Assembler::Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
444 EmitI(0x2b, rs, rt, imm16);
445}
446
447void Mips64Assembler::Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
448 EmitI(0x3f, rs, rt, imm16);
449}
450
451void Mips64Assembler::Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
452 EmitR(0, rs, rt, rd, 0, 0x2a);
453}
454
455void Mips64Assembler::Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
456 EmitR(0, rs, rt, rd, 0, 0x2b);
457}
458
459void Mips64Assembler::Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
460 EmitI(0xa, rs, rt, imm16);
461}
462
463void Mips64Assembler::Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16) {
464 EmitI(0xb, rs, rt, imm16);
465}
466
Alexey Frunze4dda3372015-06-01 18:31:49 -0700467void Mips64Assembler::Beq(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800468 EmitI(0x4, rs, rt, imm16);
469 Nop();
470}
471
Alexey Frunze4dda3372015-06-01 18:31:49 -0700472void Mips64Assembler::Bne(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
Andreas Gampe57b34292015-01-14 15:45:59 -0800473 EmitI(0x5, rs, rt, imm16);
474 Nop();
475}
476
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477void Mips64Assembler::J(uint32_t addr26) {
478 EmitJ(0x2, addr26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800479 Nop();
480}
481
Alexey Frunze4dda3372015-06-01 18:31:49 -0700482void Mips64Assembler::Jal(uint32_t addr26) {
483 EmitJ(0x3, addr26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800484 Nop();
485}
486
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700487void Mips64Assembler::Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
488 EmitR(0, rs, rt, rd, 0, 0x35);
489}
490
491void Mips64Assembler::Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt) {
492 EmitR(0, rs, rt, rd, 0, 0x37);
493}
494
495void Mips64Assembler::Clz(GpuRegister rd, GpuRegister rs) {
496 EmitRsd(0, rs, rd, 0x01, 0x10);
497}
498
499void Mips64Assembler::Clo(GpuRegister rd, GpuRegister rs) {
500 EmitRsd(0, rs, rd, 0x01, 0x11);
501}
502
503void Mips64Assembler::Dclz(GpuRegister rd, GpuRegister rs) {
504 EmitRsd(0, rs, rd, 0x01, 0x12);
505}
506
507void Mips64Assembler::Dclo(GpuRegister rd, GpuRegister rs) {
508 EmitRsd(0, rs, rd, 0x01, 0x13);
509}
510
Alexey Frunze4dda3372015-06-01 18:31:49 -0700511void Mips64Assembler::Jalr(GpuRegister rd, GpuRegister rs) {
512 EmitR(0, rs, static_cast<GpuRegister>(0), rd, 0, 0x09);
Andreas Gampe57b34292015-01-14 15:45:59 -0800513 Nop();
514}
515
516void Mips64Assembler::Jalr(GpuRegister rs) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700517 Jalr(RA, rs);
518}
519
520void Mips64Assembler::Jr(GpuRegister rs) {
521 Jalr(ZERO, rs);
522}
523
524void Mips64Assembler::Auipc(GpuRegister rs, uint16_t imm16) {
525 EmitI(0x3B, rs, static_cast<GpuRegister>(0x1E), imm16);
526}
527
528void Mips64Assembler::Jic(GpuRegister rt, uint16_t imm16) {
529 EmitI(0x36, static_cast<GpuRegister>(0), rt, imm16);
530}
531
532void Mips64Assembler::Jialc(GpuRegister rt, uint16_t imm16) {
533 EmitI(0x3E, static_cast<GpuRegister>(0), rt, imm16);
534}
535
536void Mips64Assembler::Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
537 CHECK_NE(rs, ZERO);
538 CHECK_NE(rt, ZERO);
539 CHECK_NE(rs, rt);
540 EmitI(0x17, rs, rt, imm16);
541}
542
543void Mips64Assembler::Bltzc(GpuRegister rt, uint16_t imm16) {
544 CHECK_NE(rt, ZERO);
545 EmitI(0x17, rt, rt, imm16);
546}
547
548void Mips64Assembler::Bgtzc(GpuRegister rt, uint16_t imm16) {
549 CHECK_NE(rt, ZERO);
550 EmitI(0x17, static_cast<GpuRegister>(0), rt, imm16);
551}
552
553void Mips64Assembler::Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
554 CHECK_NE(rs, ZERO);
555 CHECK_NE(rt, ZERO);
556 CHECK_NE(rs, rt);
557 EmitI(0x16, rs, rt, imm16);
558}
559
560void Mips64Assembler::Bgezc(GpuRegister rt, uint16_t imm16) {
561 CHECK_NE(rt, ZERO);
562 EmitI(0x16, rt, rt, imm16);
563}
564
565void Mips64Assembler::Blezc(GpuRegister rt, uint16_t imm16) {
566 CHECK_NE(rt, ZERO);
567 EmitI(0x16, static_cast<GpuRegister>(0), rt, imm16);
568}
569
570void Mips64Assembler::Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
571 CHECK_NE(rs, ZERO);
572 CHECK_NE(rt, ZERO);
573 CHECK_NE(rs, rt);
574 EmitI(0x7, rs, rt, imm16);
575}
576
577void Mips64Assembler::Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
578 CHECK_NE(rs, ZERO);
579 CHECK_NE(rt, ZERO);
580 CHECK_NE(rs, rt);
581 EmitI(0x6, rs, rt, imm16);
582}
583
584void Mips64Assembler::Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
585 CHECK_NE(rs, ZERO);
586 CHECK_NE(rt, ZERO);
587 CHECK_NE(rs, rt);
588 EmitI(0x8, (rs < rt) ? rs : rt, (rs < rt) ? rt : rs, imm16);
589}
590
591void Mips64Assembler::Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16) {
592 CHECK_NE(rs, ZERO);
593 CHECK_NE(rt, ZERO);
594 CHECK_NE(rs, rt);
595 EmitI(0x18, (rs < rt) ? rs : rt, (rs < rt) ? rt : rs, imm16);
596}
597
598void Mips64Assembler::Beqzc(GpuRegister rs, uint32_t imm21) {
599 CHECK_NE(rs, ZERO);
600 EmitI21(0x36, rs, imm21);
601}
602
603void Mips64Assembler::Bnezc(GpuRegister rs, uint32_t imm21) {
604 CHECK_NE(rs, ZERO);
605 EmitI21(0x3E, rs, imm21);
Andreas Gampe57b34292015-01-14 15:45:59 -0800606}
607
608void Mips64Assembler::AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
609 EmitFR(0x11, 0x10, ft, fs, fd, 0x0);
610}
611
612void Mips64Assembler::SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
613 EmitFR(0x11, 0x10, ft, fs, fd, 0x1);
614}
615
616void Mips64Assembler::MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
617 EmitFR(0x11, 0x10, ft, fs, fd, 0x2);
618}
619
620void Mips64Assembler::DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
621 EmitFR(0x11, 0x10, ft, fs, fd, 0x3);
622}
623
624void Mips64Assembler::AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700625 EmitFR(0x11, 0x11, ft, fs, fd, 0x0);
Andreas Gampe57b34292015-01-14 15:45:59 -0800626}
627
628void Mips64Assembler::SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700629 EmitFR(0x11, 0x11, ft, fs, fd, 0x1);
Andreas Gampe57b34292015-01-14 15:45:59 -0800630}
631
632void Mips64Assembler::MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700633 EmitFR(0x11, 0x11, ft, fs, fd, 0x2);
Andreas Gampe57b34292015-01-14 15:45:59 -0800634}
635
636void Mips64Assembler::DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700637 EmitFR(0x11, 0x11, ft, fs, fd, 0x3);
Andreas Gampe57b34292015-01-14 15:45:59 -0800638}
639
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700640void Mips64Assembler::SqrtS(FpuRegister fd, FpuRegister fs) {
641 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x4);
642}
643
644void Mips64Assembler::SqrtD(FpuRegister fd, FpuRegister fs) {
645 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x4);
646}
647
648void Mips64Assembler::AbsS(FpuRegister fd, FpuRegister fs) {
649 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x5);
650}
651
652void Mips64Assembler::AbsD(FpuRegister fd, FpuRegister fs) {
653 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x5);
654}
655
Andreas Gampe57b34292015-01-14 15:45:59 -0800656void Mips64Assembler::MovS(FpuRegister fd, FpuRegister fs) {
657 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x6);
658}
659
660void Mips64Assembler::MovD(FpuRegister fd, FpuRegister fs) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700661 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x6);
662}
663
664void Mips64Assembler::NegS(FpuRegister fd, FpuRegister fs) {
665 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x7);
666}
667
668void Mips64Assembler::NegD(FpuRegister fd, FpuRegister fs) {
669 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x7);
670}
671
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700672void Mips64Assembler::RoundLS(FpuRegister fd, FpuRegister fs) {
673 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x8);
674}
675
676void Mips64Assembler::RoundLD(FpuRegister fd, FpuRegister fs) {
677 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x8);
678}
679
680void Mips64Assembler::RoundWS(FpuRegister fd, FpuRegister fs) {
681 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0xc);
682}
683
684void Mips64Assembler::RoundWD(FpuRegister fd, FpuRegister fs) {
685 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0xc);
686}
687
688void Mips64Assembler::CeilLS(FpuRegister fd, FpuRegister fs) {
689 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0xa);
690}
691
692void Mips64Assembler::CeilLD(FpuRegister fd, FpuRegister fs) {
693 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0xa);
694}
695
696void Mips64Assembler::CeilWS(FpuRegister fd, FpuRegister fs) {
697 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0xe);
698}
699
700void Mips64Assembler::CeilWD(FpuRegister fd, FpuRegister fs) {
701 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0xe);
702}
703
704void Mips64Assembler::FloorLS(FpuRegister fd, FpuRegister fs) {
705 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0xb);
706}
707
708void Mips64Assembler::FloorLD(FpuRegister fd, FpuRegister fs) {
709 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0xb);
710}
711
712void Mips64Assembler::FloorWS(FpuRegister fd, FpuRegister fs) {
713 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0xf);
714}
715
716void Mips64Assembler::FloorWD(FpuRegister fd, FpuRegister fs) {
717 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0xf);
718}
719
720void Mips64Assembler::SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
721 EmitFR(0x11, 0x10, ft, fs, fd, 0x10);
722}
723
724void Mips64Assembler::SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
725 EmitFR(0x11, 0x11, ft, fs, fd, 0x10);
726}
727
728void Mips64Assembler::RintS(FpuRegister fd, FpuRegister fs) {
729 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x1a);
730}
731
732void Mips64Assembler::RintD(FpuRegister fd, FpuRegister fs) {
733 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x1a);
734}
735
736void Mips64Assembler::ClassS(FpuRegister fd, FpuRegister fs) {
737 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x1b);
738}
739
740void Mips64Assembler::ClassD(FpuRegister fd, FpuRegister fs) {
741 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x1b);
742}
743
744void Mips64Assembler::MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
745 EmitFR(0x11, 0x10, ft, fs, fd, 0x1c);
746}
747
748void Mips64Assembler::MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
749 EmitFR(0x11, 0x11, ft, fs, fd, 0x1c);
750}
751
752void Mips64Assembler::MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
753 EmitFR(0x11, 0x10, ft, fs, fd, 0x1e);
754}
755
756void Mips64Assembler::MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft) {
757 EmitFR(0x11, 0x11, ft, fs, fd, 0x1e);
758}
759
Alexey Frunze4dda3372015-06-01 18:31:49 -0700760void Mips64Assembler::Cvtsw(FpuRegister fd, FpuRegister fs) {
761 EmitFR(0x11, 0x14, static_cast<FpuRegister>(0), fs, fd, 0x20);
762}
763
764void Mips64Assembler::Cvtdw(FpuRegister fd, FpuRegister fs) {
765 EmitFR(0x11, 0x14, static_cast<FpuRegister>(0), fs, fd, 0x21);
766}
767
768void Mips64Assembler::Cvtsd(FpuRegister fd, FpuRegister fs) {
769 EmitFR(0x11, 0x11, static_cast<FpuRegister>(0), fs, fd, 0x20);
770}
771
772void Mips64Assembler::Cvtds(FpuRegister fd, FpuRegister fs) {
773 EmitFR(0x11, 0x10, static_cast<FpuRegister>(0), fs, fd, 0x21);
Andreas Gampe57b34292015-01-14 15:45:59 -0800774}
775
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700776void Mips64Assembler::Cvtdl(FpuRegister fd, FpuRegister fs) {
777 EmitFR(0x11, 0x15, static_cast<FpuRegister>(0), fs, fd, 0x21);
778}
779
Andreas Gampe57b34292015-01-14 15:45:59 -0800780void Mips64Assembler::Mfc1(GpuRegister rt, FpuRegister fs) {
781 EmitFR(0x11, 0x00, static_cast<FpuRegister>(rt), fs, static_cast<FpuRegister>(0), 0x0);
782}
783
Alexey Frunze4dda3372015-06-01 18:31:49 -0700784void Mips64Assembler::Mtc1(GpuRegister rt, FpuRegister fs) {
785 EmitFR(0x11, 0x04, static_cast<FpuRegister>(rt), fs, static_cast<FpuRegister>(0), 0x0);
786}
787
788void Mips64Assembler::Dmfc1(GpuRegister rt, FpuRegister fs) {
789 EmitFR(0x11, 0x01, static_cast<FpuRegister>(rt), fs, static_cast<FpuRegister>(0), 0x0);
790}
791
792void Mips64Assembler::Dmtc1(GpuRegister rt, FpuRegister fs) {
793 EmitFR(0x11, 0x05, static_cast<FpuRegister>(rt), fs, static_cast<FpuRegister>(0), 0x0);
Andreas Gampe57b34292015-01-14 15:45:59 -0800794}
795
796void Mips64Assembler::Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16) {
797 EmitI(0x31, rs, static_cast<GpuRegister>(ft), imm16);
798}
799
800void Mips64Assembler::Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16) {
801 EmitI(0x35, rs, static_cast<GpuRegister>(ft), imm16);
802}
803
804void Mips64Assembler::Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16) {
805 EmitI(0x39, rs, static_cast<GpuRegister>(ft), imm16);
806}
807
808void Mips64Assembler::Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16) {
809 EmitI(0x3d, rs, static_cast<GpuRegister>(ft), imm16);
810}
811
812void Mips64Assembler::Break() {
813 EmitR(0, static_cast<GpuRegister>(0), static_cast<GpuRegister>(0),
814 static_cast<GpuRegister>(0), 0, 0xD);
815}
816
817void Mips64Assembler::Nop() {
818 EmitR(0x0, static_cast<GpuRegister>(0), static_cast<GpuRegister>(0),
819 static_cast<GpuRegister>(0), 0, 0x0);
820}
821
Alexey Frunze4dda3372015-06-01 18:31:49 -0700822void Mips64Assembler::Move(GpuRegister rd, GpuRegister rs) {
823 Or(rd, rs, ZERO);
Andreas Gampe57b34292015-01-14 15:45:59 -0800824}
825
Alexey Frunze4dda3372015-06-01 18:31:49 -0700826void Mips64Assembler::Clear(GpuRegister rd) {
827 Move(rd, ZERO);
Andreas Gampe57b34292015-01-14 15:45:59 -0800828}
829
Alexey Frunze4dda3372015-06-01 18:31:49 -0700830void Mips64Assembler::Not(GpuRegister rd, GpuRegister rs) {
831 Nor(rd, rs, ZERO);
Andreas Gampe57b34292015-01-14 15:45:59 -0800832}
833
Alexey Frunze4dda3372015-06-01 18:31:49 -0700834void Mips64Assembler::LoadConst32(GpuRegister rd, int32_t value) {
835 if (IsUint<16>(value)) {
836 // Use OR with (unsigned) immediate to encode 16b unsigned int.
837 Ori(rd, ZERO, value);
838 } else if (IsInt<16>(value)) {
839 // Use ADD with (signed) immediate to encode 16b signed int.
840 Addiu(rd, ZERO, value);
841 } else {
842 Lui(rd, value >> 16);
843 if (value & 0xFFFF)
844 Ori(rd, rd, value);
845 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800846}
847
Alexey Frunze4dda3372015-06-01 18:31:49 -0700848void Mips64Assembler::LoadConst64(GpuRegister rd, int64_t value) {
849 int bit31 = (value & UINT64_C(0x80000000)) != 0;
850
851 // Loads with 1 instruction.
852 if (IsUint<16>(value)) {
853 Ori(rd, ZERO, value);
854 } else if (IsInt<16>(value)) {
855 Daddiu(rd, ZERO, value);
856 } else if ((value & 0xFFFF) == 0 && IsInt<16>(value >> 16)) {
857 Lui(rd, value >> 16);
858 } else if (IsInt<32>(value)) {
859 // Loads with 2 instructions.
860 Lui(rd, value >> 16);
861 Ori(rd, rd, value);
862 } else if ((value & 0xFFFF0000) == 0 && IsInt<16>(value >> 32)) {
863 Ori(rd, ZERO, value);
864 Dahi(rd, value >> 32);
865 } else if ((value & UINT64_C(0xFFFFFFFF0000)) == 0) {
866 Ori(rd, ZERO, value);
867 Dati(rd, value >> 48);
868 } else if ((value & 0xFFFF) == 0 &&
869 (-32768 - bit31) <= (value >> 32) && (value >> 32) <= (32767 - bit31)) {
870 Lui(rd, value >> 16);
871 Dahi(rd, (value >> 32) + bit31);
872 } else if ((value & 0xFFFF) == 0 && ((value >> 31) & 0x1FFFF) == ((0x20000 - bit31) & 0x1FFFF)) {
873 Lui(rd, value >> 16);
874 Dati(rd, (value >> 48) + bit31);
875 } else {
876 int shift_cnt = CTZ(value);
877 int64_t tmp = value >> shift_cnt;
878 if (IsUint<16>(tmp)) {
879 Ori(rd, ZERO, tmp);
880 if (shift_cnt < 32)
881 Dsll(rd, rd, shift_cnt);
882 else
883 Dsll32(rd, rd, shift_cnt & 31);
884 } else if (IsInt<16>(tmp)) {
885 Daddiu(rd, ZERO, tmp);
886 if (shift_cnt < 32)
887 Dsll(rd, rd, shift_cnt);
888 else
889 Dsll32(rd, rd, shift_cnt & 31);
890 } else if (IsInt<32>(tmp)) {
891 // Loads with 3 instructions.
892 Lui(rd, tmp >> 16);
893 Ori(rd, rd, tmp);
894 if (shift_cnt < 32)
895 Dsll(rd, rd, shift_cnt);
896 else
897 Dsll32(rd, rd, shift_cnt & 31);
898 } else {
899 shift_cnt = 16 + CTZ(value >> 16);
900 tmp = value >> shift_cnt;
901 if (IsUint<16>(tmp)) {
902 Ori(rd, ZERO, tmp);
903 if (shift_cnt < 32)
904 Dsll(rd, rd, shift_cnt);
905 else
906 Dsll32(rd, rd, shift_cnt & 31);
907 Ori(rd, rd, value);
908 } else if (IsInt<16>(tmp)) {
909 Daddiu(rd, ZERO, tmp);
910 if (shift_cnt < 32)
911 Dsll(rd, rd, shift_cnt);
912 else
913 Dsll32(rd, rd, shift_cnt & 31);
914 Ori(rd, rd, value);
915 } else {
916 // Loads with 3-4 instructions.
917 uint64_t tmp2 = value;
918 bool used_lui = false;
919 if (((tmp2 >> 16) & 0xFFFF) != 0 || (tmp2 & 0xFFFFFFFF) == 0) {
920 Lui(rd, tmp2 >> 16);
921 used_lui = true;
922 }
923 if ((tmp2 & 0xFFFF) != 0) {
924 if (used_lui)
925 Ori(rd, rd, tmp2);
926 else
927 Ori(rd, ZERO, tmp2);
928 }
929 if (bit31) {
930 tmp2 += UINT64_C(0x100000000);
931 }
932 if (((tmp2 >> 32) & 0xFFFF) != 0) {
933 Dahi(rd, tmp2 >> 32);
934 }
935 if (tmp2 & UINT64_C(0x800000000000)) {
936 tmp2 += UINT64_C(0x1000000000000);
937 }
938 if ((tmp2 >> 48) != 0) {
939 Dati(rd, tmp2 >> 48);
940 }
941 }
942 }
943 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800944}
945
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946void Mips64Assembler::Addiu32(GpuRegister rt, GpuRegister rs, int32_t value, GpuRegister rtmp) {
947 if (IsInt<16>(value)) {
948 Addiu(rt, rs, value);
949 } else {
950 LoadConst32(rtmp, value);
951 Addu(rt, rs, rtmp);
952 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800953}
954
Alexey Frunze4dda3372015-06-01 18:31:49 -0700955void Mips64Assembler::Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp) {
956 if (IsInt<16>(value)) {
957 Daddiu(rt, rs, value);
958 } else {
959 LoadConst64(rtmp, value);
960 Daddu(rt, rs, rtmp);
961 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800962}
963
Alexey Frunze4dda3372015-06-01 18:31:49 -0700964//
965// MIPS64R6 branches
966//
967//
968// Unconditional (pc + 32-bit signed offset):
969//
970// auipc at, ofs_high
971// jic at, ofs_low
972// // no delay/forbidden slot
973//
974//
975// Conditional (pc + 32-bit signed offset):
976//
977// b<cond>c reg, +2 // skip next 2 instructions
978// auipc at, ofs_high
979// jic at, ofs_low
980// // no delay/forbidden slot
981//
982//
983// Unconditional (pc + 32-bit signed offset) and link:
984//
985// auipc reg, ofs_high
986// daddiu reg, ofs_low
987// jialc reg, 0
988// // no delay/forbidden slot
989//
990//
991// TODO: use shorter instruction sequences whenever possible.
992//
993
994void Mips64Assembler::Bind(Label* label) {
995 CHECK(!label->IsBound());
996 int32_t bound_pc = buffer_.Size();
997
998 // Walk the list of the branches (auipc + jic pairs) referring to and preceding this label.
999 // Embed the previously unknown pc-relative addresses in them.
1000 while (label->IsLinked()) {
1001 int32_t position = label->Position();
1002 // Extract the branch (instruction pair)
1003 uint32_t auipc = buffer_.Load<uint32_t>(position);
1004 uint32_t jic = buffer_.Load<uint32_t>(position + 4); // actually, jic or daddiu
1005
1006 // Extract the location of the previous pair in the list (walking the list backwards;
1007 // the previous pair location was stored in the immediate operands of the instructions)
1008 int32_t prev = (auipc << 16) | (jic & 0xFFFF);
1009
1010 // Get the pc-relative address
1011 uint32_t offset = bound_pc - position;
1012 offset += (offset & 0x8000) << 1; // account for sign extension in jic/daddiu
1013
1014 // Embed it in the two instructions
1015 auipc = (auipc & 0xFFFF0000) | (offset >> 16);
1016 jic = (jic & 0xFFFF0000) | (offset & 0xFFFF);
1017
1018 // Save the adjusted instructions
1019 buffer_.Store<uint32_t>(position, auipc);
1020 buffer_.Store<uint32_t>(position + 4, jic);
1021
1022 // On to the previous branch in the list...
1023 label->position_ = prev;
1024 }
1025
1026 // Now make the label object contain its own location
1027 // (it will be used by the branches referring to and following this label)
1028 label->BindTo(bound_pc);
1029}
1030
1031void Mips64Assembler::B(Label* label) {
1032 if (label->IsBound()) {
1033 // Branch backwards (to a preceding label), distance is known
1034 uint32_t offset = label->Position() - buffer_.Size();
1035 CHECK_LE(static_cast<int32_t>(offset), 0);
1036 offset += (offset & 0x8000) << 1; // account for sign extension in jic
1037 Auipc(AT, offset >> 16);
1038 Jic(AT, offset);
1039 } else {
1040 // Branch forward (to a following label), distance is unknown
1041 int32_t position = buffer_.Size();
1042 // The first branch forward will have 0 in its pc-relative address (copied from label's
1043 // position). It will be the terminator of the list of forward-reaching branches.
1044 uint32_t prev = label->position_;
1045 Auipc(AT, prev >> 16);
1046 Jic(AT, prev);
1047 // Now make the link object point to the location of this branch
1048 // (this forms a linked list of branches preceding this label)
1049 label->LinkTo(position);
1050 }
1051}
1052
1053void Mips64Assembler::Jalr(Label* label, GpuRegister indirect_reg) {
1054 if (label->IsBound()) {
1055 // Branch backwards (to a preceding label), distance is known
1056 uint32_t offset = label->Position() - buffer_.Size();
1057 CHECK_LE(static_cast<int32_t>(offset), 0);
1058 offset += (offset & 0x8000) << 1; // account for sign extension in daddiu
1059 Auipc(indirect_reg, offset >> 16);
1060 Daddiu(indirect_reg, indirect_reg, offset);
1061 Jialc(indirect_reg, 0);
1062 } else {
1063 // Branch forward (to a following label), distance is unknown
1064 int32_t position = buffer_.Size();
1065 // The first branch forward will have 0 in its pc-relative address (copied from label's
1066 // position). It will be the terminator of the list of forward-reaching branches.
1067 uint32_t prev = label->position_;
1068 Auipc(indirect_reg, prev >> 16);
1069 Daddiu(indirect_reg, indirect_reg, prev);
1070 Jialc(indirect_reg, 0);
1071 // Now make the link object point to the location of this branch
1072 // (this forms a linked list of branches preceding this label)
1073 label->LinkTo(position);
1074 }
1075}
1076
1077void Mips64Assembler::Bltc(GpuRegister rs, GpuRegister rt, Label* label) {
1078 Bgec(rs, rt, 2);
1079 B(label);
1080}
1081
1082void Mips64Assembler::Bltzc(GpuRegister rt, Label* label) {
1083 Bgezc(rt, 2);
1084 B(label);
1085}
1086
1087void Mips64Assembler::Bgtzc(GpuRegister rt, Label* label) {
1088 Blezc(rt, 2);
1089 B(label);
1090}
1091
1092void Mips64Assembler::Bgec(GpuRegister rs, GpuRegister rt, Label* label) {
1093 Bltc(rs, rt, 2);
1094 B(label);
1095}
1096
1097void Mips64Assembler::Bgezc(GpuRegister rt, Label* label) {
1098 Bltzc(rt, 2);
1099 B(label);
1100}
1101
1102void Mips64Assembler::Blezc(GpuRegister rt, Label* label) {
1103 Bgtzc(rt, 2);
1104 B(label);
1105}
1106
1107void Mips64Assembler::Bltuc(GpuRegister rs, GpuRegister rt, Label* label) {
1108 Bgeuc(rs, rt, 2);
1109 B(label);
1110}
1111
1112void Mips64Assembler::Bgeuc(GpuRegister rs, GpuRegister rt, Label* label) {
1113 Bltuc(rs, rt, 2);
1114 B(label);
1115}
1116
1117void Mips64Assembler::Beqc(GpuRegister rs, GpuRegister rt, Label* label) {
1118 Bnec(rs, rt, 2);
1119 B(label);
1120}
1121
1122void Mips64Assembler::Bnec(GpuRegister rs, GpuRegister rt, Label* label) {
1123 Beqc(rs, rt, 2);
1124 B(label);
1125}
1126
1127void Mips64Assembler::Beqzc(GpuRegister rs, Label* label) {
1128 Bnezc(rs, 2);
1129 B(label);
1130}
1131
1132void Mips64Assembler::Bnezc(GpuRegister rs, Label* label) {
1133 Beqzc(rs, 2);
1134 B(label);
Andreas Gampe57b34292015-01-14 15:45:59 -08001135}
1136
1137void Mips64Assembler::LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base,
1138 int32_t offset) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001139 if (!IsInt<16>(offset)) {
1140 LoadConst32(AT, offset);
1141 Daddu(AT, AT, base);
1142 base = AT;
1143 offset = 0;
1144 }
1145
Andreas Gampe57b34292015-01-14 15:45:59 -08001146 switch (type) {
1147 case kLoadSignedByte:
1148 Lb(reg, base, offset);
1149 break;
1150 case kLoadUnsignedByte:
1151 Lbu(reg, base, offset);
1152 break;
1153 case kLoadSignedHalfword:
1154 Lh(reg, base, offset);
1155 break;
1156 case kLoadUnsignedHalfword:
1157 Lhu(reg, base, offset);
1158 break;
1159 case kLoadWord:
1160 Lw(reg, base, offset);
1161 break;
Douglas Leungd90957f2015-04-30 19:22:49 -07001162 case kLoadUnsignedWord:
1163 Lwu(reg, base, offset);
1164 break;
Andreas Gampe57b34292015-01-14 15:45:59 -08001165 case kLoadDoubleword:
Andreas Gampe57b34292015-01-14 15:45:59 -08001166 Ld(reg, base, offset);
1167 break;
Andreas Gampe57b34292015-01-14 15:45:59 -08001168 }
1169}
1170
1171void Mips64Assembler::LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base,
1172 int32_t offset) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173 if (!IsInt<16>(offset)) {
1174 LoadConst32(AT, offset);
1175 Daddu(AT, AT, base);
1176 base = AT;
1177 offset = 0;
1178 }
1179
Andreas Gampe57b34292015-01-14 15:45:59 -08001180 switch (type) {
1181 case kLoadWord:
1182 Lwc1(reg, base, offset);
1183 break;
1184 case kLoadDoubleword:
Andreas Gampe57b34292015-01-14 15:45:59 -08001185 Ldc1(reg, base, offset);
1186 break;
1187 default:
1188 LOG(FATAL) << "UNREACHABLE";
1189 }
1190}
1191
1192void Mips64Assembler::EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset,
1193 size_t size) {
1194 Mips64ManagedRegister dst = m_dst.AsMips64();
1195 if (dst.IsNoRegister()) {
1196 CHECK_EQ(0u, size) << dst;
1197 } else if (dst.IsGpuRegister()) {
1198 if (size == 4) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001199 LoadFromOffset(kLoadWord, dst.AsGpuRegister(), src_register, src_offset);
1200 } else if (size == 8) {
1201 CHECK_EQ(8u, size) << dst;
1202 LoadFromOffset(kLoadDoubleword, dst.AsGpuRegister(), src_register, src_offset);
1203 } else {
1204 UNIMPLEMENTED(FATAL) << "We only support Load() of size 4 and 8";
1205 }
1206 } else if (dst.IsFpuRegister()) {
1207 if (size == 4) {
1208 CHECK_EQ(4u, size) << dst;
1209 LoadFpuFromOffset(kLoadWord, dst.AsFpuRegister(), src_register, src_offset);
1210 } else if (size == 8) {
1211 CHECK_EQ(8u, size) << dst;
1212 LoadFpuFromOffset(kLoadDoubleword, dst.AsFpuRegister(), src_register, src_offset);
1213 } else {
1214 UNIMPLEMENTED(FATAL) << "We only support Load() of size 4 and 8";
1215 }
1216 }
1217}
1218
1219void Mips64Assembler::StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base,
1220 int32_t offset) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 if (!IsInt<16>(offset)) {
1222 LoadConst32(AT, offset);
1223 Daddu(AT, AT, base);
1224 base = AT;
1225 offset = 0;
1226 }
1227
Andreas Gampe57b34292015-01-14 15:45:59 -08001228 switch (type) {
1229 case kStoreByte:
1230 Sb(reg, base, offset);
1231 break;
1232 case kStoreHalfword:
1233 Sh(reg, base, offset);
1234 break;
1235 case kStoreWord:
1236 Sw(reg, base, offset);
1237 break;
1238 case kStoreDoubleword:
Andreas Gampe57b34292015-01-14 15:45:59 -08001239 Sd(reg, base, offset);
1240 break;
1241 default:
1242 LOG(FATAL) << "UNREACHABLE";
1243 }
1244}
1245
1246void Mips64Assembler::StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base,
1247 int32_t offset) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 if (!IsInt<16>(offset)) {
1249 LoadConst32(AT, offset);
1250 Daddu(AT, AT, base);
1251 base = AT;
1252 offset = 0;
1253 }
1254
Andreas Gampe57b34292015-01-14 15:45:59 -08001255 switch (type) {
1256 case kStoreWord:
1257 Swc1(reg, base, offset);
1258 break;
1259 case kStoreDoubleword:
1260 Sdc1(reg, base, offset);
1261 break;
1262 default:
1263 LOG(FATAL) << "UNREACHABLE";
1264 }
1265}
1266
David Srbeckydd973932015-04-07 20:29:48 +01001267static dwarf::Reg DWARFReg(GpuRegister reg) {
1268 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1269}
1270
Andreas Gampe57b34292015-01-14 15:45:59 -08001271constexpr size_t kFramePointerSize = 8;
1272
1273void Mips64Assembler::BuildFrame(size_t frame_size, ManagedRegister method_reg,
1274 const std::vector<ManagedRegister>& callee_save_regs,
1275 const ManagedRegisterEntrySpills& entry_spills) {
1276 CHECK_ALIGNED(frame_size, kStackAlignment);
1277
1278 // Increase frame to required size.
1279 IncreaseFrameSize(frame_size);
1280
1281 // Push callee saves and return address
1282 int stack_offset = frame_size - kFramePointerSize;
1283 StoreToOffset(kStoreDoubleword, RA, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01001284 cfi_.RelOffset(DWARFReg(RA), stack_offset);
Andreas Gampe57b34292015-01-14 15:45:59 -08001285 for (int i = callee_save_regs.size() - 1; i >= 0; --i) {
1286 stack_offset -= kFramePointerSize;
1287 GpuRegister reg = callee_save_regs.at(i).AsMips64().AsGpuRegister();
1288 StoreToOffset(kStoreDoubleword, reg, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01001289 cfi_.RelOffset(DWARFReg(reg), stack_offset);
Andreas Gampe57b34292015-01-14 15:45:59 -08001290 }
1291
1292 // Write out Method*.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001293 StoreToOffset(kStoreDoubleword, method_reg.AsMips64().AsGpuRegister(), SP, 0);
Andreas Gampe57b34292015-01-14 15:45:59 -08001294
1295 // Write out entry spills.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001296 int32_t offset = frame_size + kFramePointerSize;
Andreas Gampe57b34292015-01-14 15:45:59 -08001297 for (size_t i = 0; i < entry_spills.size(); ++i) {
1298 Mips64ManagedRegister reg = entry_spills.at(i).AsMips64();
1299 ManagedRegisterSpill spill = entry_spills.at(i);
1300 int32_t size = spill.getSize();
1301 if (reg.IsNoRegister()) {
1302 // only increment stack offset.
1303 offset += size;
1304 } else if (reg.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001305 StoreFpuToOffset((size == 4) ? kStoreWord : kStoreDoubleword,
1306 reg.AsFpuRegister(), SP, offset);
Andreas Gampe57b34292015-01-14 15:45:59 -08001307 offset += size;
1308 } else if (reg.IsGpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 StoreToOffset((size == 4) ? kStoreWord : kStoreDoubleword,
1310 reg.AsGpuRegister(), SP, offset);
Andreas Gampe57b34292015-01-14 15:45:59 -08001311 offset += size;
1312 }
1313 }
1314}
1315
1316void Mips64Assembler::RemoveFrame(size_t frame_size,
1317 const std::vector<ManagedRegister>& callee_save_regs) {
1318 CHECK_ALIGNED(frame_size, kStackAlignment);
David Srbeckydd973932015-04-07 20:29:48 +01001319 cfi_.RememberState();
Andreas Gampe57b34292015-01-14 15:45:59 -08001320
1321 // Pop callee saves and return address
1322 int stack_offset = frame_size - (callee_save_regs.size() * kFramePointerSize) - kFramePointerSize;
1323 for (size_t i = 0; i < callee_save_regs.size(); ++i) {
1324 GpuRegister reg = callee_save_regs.at(i).AsMips64().AsGpuRegister();
1325 LoadFromOffset(kLoadDoubleword, reg, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01001326 cfi_.Restore(DWARFReg(reg));
Andreas Gampe57b34292015-01-14 15:45:59 -08001327 stack_offset += kFramePointerSize;
1328 }
1329 LoadFromOffset(kLoadDoubleword, RA, SP, stack_offset);
David Srbeckydd973932015-04-07 20:29:48 +01001330 cfi_.Restore(DWARFReg(RA));
Andreas Gampe57b34292015-01-14 15:45:59 -08001331
1332 // Decrease frame to required size.
1333 DecreaseFrameSize(frame_size);
1334
1335 // Then jump to the return address.
1336 Jr(RA);
David Srbeckydd973932015-04-07 20:29:48 +01001337
1338 // The CFI should be restored for any code that follows the exit block.
1339 cfi_.RestoreState();
1340 cfi_.DefCFAOffset(frame_size);
Andreas Gampe57b34292015-01-14 15:45:59 -08001341}
1342
1343void Mips64Assembler::IncreaseFrameSize(size_t adjust) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001344 CHECK_ALIGNED(adjust, kFramePointerSize);
1345 Daddiu64(SP, SP, static_cast<int32_t>(-adjust));
David Srbeckydd973932015-04-07 20:29:48 +01001346 cfi_.AdjustCFAOffset(adjust);
Andreas Gampe57b34292015-01-14 15:45:59 -08001347}
1348
1349void Mips64Assembler::DecreaseFrameSize(size_t adjust) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001350 CHECK_ALIGNED(adjust, kFramePointerSize);
1351 Daddiu64(SP, SP, static_cast<int32_t>(adjust));
David Srbeckydd973932015-04-07 20:29:48 +01001352 cfi_.AdjustCFAOffset(-adjust);
Andreas Gampe57b34292015-01-14 15:45:59 -08001353}
1354
1355void Mips64Assembler::Store(FrameOffset dest, ManagedRegister msrc, size_t size) {
1356 Mips64ManagedRegister src = msrc.AsMips64();
1357 if (src.IsNoRegister()) {
1358 CHECK_EQ(0u, size);
1359 } else if (src.IsGpuRegister()) {
1360 CHECK(size == 4 || size == 8) << size;
1361 if (size == 8) {
1362 StoreToOffset(kStoreDoubleword, src.AsGpuRegister(), SP, dest.Int32Value());
1363 } else if (size == 4) {
1364 StoreToOffset(kStoreWord, src.AsGpuRegister(), SP, dest.Int32Value());
1365 } else {
1366 UNIMPLEMENTED(FATAL) << "We only support Store() of size 4 and 8";
1367 }
1368 } else if (src.IsFpuRegister()) {
1369 CHECK(size == 4 || size == 8) << size;
1370 if (size == 8) {
1371 StoreFpuToOffset(kStoreDoubleword, src.AsFpuRegister(), SP, dest.Int32Value());
1372 } else if (size == 4) {
1373 StoreFpuToOffset(kStoreWord, src.AsFpuRegister(), SP, dest.Int32Value());
1374 } else {
1375 UNIMPLEMENTED(FATAL) << "We only support Store() of size 4 and 8";
1376 }
1377 }
1378}
1379
1380void Mips64Assembler::StoreRef(FrameOffset dest, ManagedRegister msrc) {
1381 Mips64ManagedRegister src = msrc.AsMips64();
1382 CHECK(src.IsGpuRegister());
1383 StoreToOffset(kStoreWord, src.AsGpuRegister(), SP, dest.Int32Value());
1384}
1385
1386void Mips64Assembler::StoreRawPtr(FrameOffset dest, ManagedRegister msrc) {
1387 Mips64ManagedRegister src = msrc.AsMips64();
1388 CHECK(src.IsGpuRegister());
1389 StoreToOffset(kStoreDoubleword, src.AsGpuRegister(), SP, dest.Int32Value());
1390}
1391
1392void Mips64Assembler::StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
1393 ManagedRegister mscratch) {
1394 Mips64ManagedRegister scratch = mscratch.AsMips64();
1395 CHECK(scratch.IsGpuRegister()) << scratch;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001396 LoadConst32(scratch.AsGpuRegister(), imm);
Andreas Gampe57b34292015-01-14 15:45:59 -08001397 StoreToOffset(kStoreWord, scratch.AsGpuRegister(), SP, dest.Int32Value());
1398}
1399
1400void Mips64Assembler::StoreImmediateToThread64(ThreadOffset<8> dest, uint32_t imm,
1401 ManagedRegister mscratch) {
1402 Mips64ManagedRegister scratch = mscratch.AsMips64();
1403 CHECK(scratch.IsGpuRegister()) << scratch;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001404 // TODO: it's unclear wether 32 or 64 bits need to be stored (Arm64 and x86/x64 disagree?).
1405 // Is this function even referenced anywhere else in the code?
1406 LoadConst32(scratch.AsGpuRegister(), imm);
Andreas Gampe57b34292015-01-14 15:45:59 -08001407 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), S1, dest.Int32Value());
1408}
1409
1410void Mips64Assembler::StoreStackOffsetToThread64(ThreadOffset<8> thr_offs,
1411 FrameOffset fr_offs,
1412 ManagedRegister mscratch) {
1413 Mips64ManagedRegister scratch = mscratch.AsMips64();
1414 CHECK(scratch.IsGpuRegister()) << scratch;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001415 Daddiu64(scratch.AsGpuRegister(), SP, fr_offs.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001416 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), S1, thr_offs.Int32Value());
1417}
1418
1419void Mips64Assembler::StoreStackPointerToThread64(ThreadOffset<8> thr_offs) {
1420 StoreToOffset(kStoreDoubleword, SP, S1, thr_offs.Int32Value());
1421}
1422
1423void Mips64Assembler::StoreSpanning(FrameOffset dest, ManagedRegister msrc,
1424 FrameOffset in_off, ManagedRegister mscratch) {
1425 Mips64ManagedRegister src = msrc.AsMips64();
1426 Mips64ManagedRegister scratch = mscratch.AsMips64();
1427 StoreToOffset(kStoreDoubleword, src.AsGpuRegister(), SP, dest.Int32Value());
1428 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(), SP, in_off.Int32Value());
1429 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), SP, dest.Int32Value() + 8);
1430}
1431
1432void Mips64Assembler::Load(ManagedRegister mdest, FrameOffset src, size_t size) {
1433 return EmitLoad(mdest, SP, src.Int32Value(), size);
1434}
1435
1436void Mips64Assembler::LoadFromThread64(ManagedRegister mdest, ThreadOffset<8> src, size_t size) {
1437 return EmitLoad(mdest, S1, src.Int32Value(), size);
1438}
1439
1440void Mips64Assembler::LoadRef(ManagedRegister mdest, FrameOffset src) {
1441 Mips64ManagedRegister dest = mdest.AsMips64();
1442 CHECK(dest.IsGpuRegister());
Douglas Leungd90957f2015-04-30 19:22:49 -07001443 LoadFromOffset(kLoadUnsignedWord, dest.AsGpuRegister(), SP, src.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001444}
1445
Mathieu Chartiere401d142015-04-22 13:56:20 -07001446void Mips64Assembler::LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +01001447 bool unpoison_reference) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001448 Mips64ManagedRegister dest = mdest.AsMips64();
Douglas Leungd90957f2015-04-30 19:22:49 -07001449 CHECK(dest.IsGpuRegister() && base.AsMips64().IsGpuRegister());
1450 LoadFromOffset(kLoadUnsignedWord, dest.AsGpuRegister(),
Andreas Gampe57b34292015-01-14 15:45:59 -08001451 base.AsMips64().AsGpuRegister(), offs.Int32Value());
Roland Levillain4d027112015-07-01 15:41:14 +01001452 if (kPoisonHeapReferences && unpoison_reference) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001453 // TODO: review
1454 // Negate the 32-bit ref
1455 Dsubu(dest.AsGpuRegister(), ZERO, dest.AsGpuRegister());
1456 // And constrain it to 32 bits (zero-extend into bits 32 through 63) as on Arm64 and x86/64
1457 Dext(dest.AsGpuRegister(), dest.AsGpuRegister(), 0, 31);
Andreas Gampe57b34292015-01-14 15:45:59 -08001458 }
1459}
1460
1461void Mips64Assembler::LoadRawPtr(ManagedRegister mdest, ManagedRegister base,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001462 Offset offs) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001463 Mips64ManagedRegister dest = mdest.AsMips64();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001464 CHECK(dest.IsGpuRegister() && base.AsMips64().IsGpuRegister());
Andreas Gampe57b34292015-01-14 15:45:59 -08001465 LoadFromOffset(kLoadDoubleword, dest.AsGpuRegister(),
1466 base.AsMips64().AsGpuRegister(), offs.Int32Value());
1467}
1468
1469void Mips64Assembler::LoadRawPtrFromThread64(ManagedRegister mdest,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001470 ThreadOffset<8> offs) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001471 Mips64ManagedRegister dest = mdest.AsMips64();
1472 CHECK(dest.IsGpuRegister());
1473 LoadFromOffset(kLoadDoubleword, dest.AsGpuRegister(), S1, offs.Int32Value());
1474}
1475
1476void Mips64Assembler::SignExtend(ManagedRegister /*mreg*/, size_t /*size*/) {
1477 UNIMPLEMENTED(FATAL) << "no sign extension necessary for mips";
1478}
1479
1480void Mips64Assembler::ZeroExtend(ManagedRegister /*mreg*/, size_t /*size*/) {
1481 UNIMPLEMENTED(FATAL) << "no zero extension necessary for mips";
1482}
1483
1484void Mips64Assembler::Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) {
1485 Mips64ManagedRegister dest = mdest.AsMips64();
1486 Mips64ManagedRegister src = msrc.AsMips64();
1487 if (!dest.Equals(src)) {
1488 if (dest.IsGpuRegister()) {
1489 CHECK(src.IsGpuRegister()) << src;
1490 Move(dest.AsGpuRegister(), src.AsGpuRegister());
1491 } else if (dest.IsFpuRegister()) {
1492 CHECK(src.IsFpuRegister()) << src;
1493 if (size == 4) {
1494 MovS(dest.AsFpuRegister(), src.AsFpuRegister());
1495 } else if (size == 8) {
1496 MovD(dest.AsFpuRegister(), src.AsFpuRegister());
1497 } else {
1498 UNIMPLEMENTED(FATAL) << "We only support Copy() of size 4 and 8";
1499 }
1500 }
1501 }
1502}
1503
1504void Mips64Assembler::CopyRef(FrameOffset dest, FrameOffset src,
1505 ManagedRegister mscratch) {
1506 Mips64ManagedRegister scratch = mscratch.AsMips64();
1507 CHECK(scratch.IsGpuRegister()) << scratch;
1508 LoadFromOffset(kLoadWord, scratch.AsGpuRegister(), SP, src.Int32Value());
1509 StoreToOffset(kStoreWord, scratch.AsGpuRegister(), SP, dest.Int32Value());
1510}
1511
1512void Mips64Assembler::CopyRawPtrFromThread64(FrameOffset fr_offs,
1513 ThreadOffset<8> thr_offs,
1514 ManagedRegister mscratch) {
1515 Mips64ManagedRegister scratch = mscratch.AsMips64();
1516 CHECK(scratch.IsGpuRegister()) << scratch;
1517 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(), S1, thr_offs.Int32Value());
1518 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), SP, fr_offs.Int32Value());
1519}
1520
1521void Mips64Assembler::CopyRawPtrToThread64(ThreadOffset<8> thr_offs,
1522 FrameOffset fr_offs,
1523 ManagedRegister mscratch) {
1524 Mips64ManagedRegister scratch = mscratch.AsMips64();
1525 CHECK(scratch.IsGpuRegister()) << scratch;
1526 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(),
1527 SP, fr_offs.Int32Value());
1528 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(),
1529 S1, thr_offs.Int32Value());
1530}
1531
1532void Mips64Assembler::Copy(FrameOffset dest, FrameOffset src,
1533 ManagedRegister mscratch, size_t size) {
1534 Mips64ManagedRegister scratch = mscratch.AsMips64();
1535 CHECK(scratch.IsGpuRegister()) << scratch;
1536 CHECK(size == 4 || size == 8) << size;
1537 if (size == 4) {
1538 LoadFromOffset(kLoadWord, scratch.AsGpuRegister(), SP, src.Int32Value());
Lazar Trsicf652d602015-06-24 16:30:21 +02001539 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), SP, dest.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001540 } else if (size == 8) {
1541 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(), SP, src.Int32Value());
1542 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), SP, dest.Int32Value());
1543 } else {
1544 UNIMPLEMENTED(FATAL) << "We only support Copy() of size 4 and 8";
1545 }
1546}
1547
1548void Mips64Assembler::Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001549 ManagedRegister mscratch, size_t size) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001550 GpuRegister scratch = mscratch.AsMips64().AsGpuRegister();
1551 CHECK(size == 4 || size == 8) << size;
1552 if (size == 4) {
1553 LoadFromOffset(kLoadWord, scratch, src_base.AsMips64().AsGpuRegister(),
1554 src_offset.Int32Value());
Lazar Trsicf652d602015-06-24 16:30:21 +02001555 StoreToOffset(kStoreDoubleword, scratch, SP, dest.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001556 } else if (size == 8) {
1557 LoadFromOffset(kLoadDoubleword, scratch, src_base.AsMips64().AsGpuRegister(),
1558 src_offset.Int32Value());
1559 StoreToOffset(kStoreDoubleword, scratch, SP, dest.Int32Value());
1560 } else {
1561 UNIMPLEMENTED(FATAL) << "We only support Copy() of size 4 and 8";
1562 }
1563}
1564
1565void Mips64Assembler::Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001566 ManagedRegister mscratch, size_t size) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001567 GpuRegister scratch = mscratch.AsMips64().AsGpuRegister();
1568 CHECK(size == 4 || size == 8) << size;
1569 if (size == 4) {
1570 LoadFromOffset(kLoadWord, scratch, SP, src.Int32Value());
Lazar Trsicf652d602015-06-24 16:30:21 +02001571 StoreToOffset(kStoreDoubleword, scratch, dest_base.AsMips64().AsGpuRegister(),
Andreas Gampe57b34292015-01-14 15:45:59 -08001572 dest_offset.Int32Value());
1573 } else if (size == 8) {
1574 LoadFromOffset(kLoadDoubleword, scratch, SP, src.Int32Value());
1575 StoreToOffset(kStoreDoubleword, scratch, dest_base.AsMips64().AsGpuRegister(),
1576 dest_offset.Int32Value());
1577 } else {
1578 UNIMPLEMENTED(FATAL) << "We only support Copy() of size 4 and 8";
1579 }
1580}
1581
1582void Mips64Assembler::Copy(FrameOffset /*dest*/, FrameOffset /*src_base*/, Offset /*src_offset*/,
1583 ManagedRegister /*mscratch*/, size_t /*size*/) {
1584 UNIMPLEMENTED(FATAL) << "no mips64 implementation";
1585}
1586
1587void Mips64Assembler::Copy(ManagedRegister dest, Offset dest_offset,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001588 ManagedRegister src, Offset src_offset,
1589 ManagedRegister mscratch, size_t size) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001590 GpuRegister scratch = mscratch.AsMips64().AsGpuRegister();
1591 CHECK(size == 4 || size == 8) << size;
1592 if (size == 4) {
1593 LoadFromOffset(kLoadWord, scratch, src.AsMips64().AsGpuRegister(), src_offset.Int32Value());
Lazar Trsicf652d602015-06-24 16:30:21 +02001594 StoreToOffset(kStoreDoubleword, scratch, dest.AsMips64().AsGpuRegister(), dest_offset.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001595 } else if (size == 8) {
1596 LoadFromOffset(kLoadDoubleword, scratch, src.AsMips64().AsGpuRegister(),
1597 src_offset.Int32Value());
1598 StoreToOffset(kStoreDoubleword, scratch, dest.AsMips64().AsGpuRegister(),
1599 dest_offset.Int32Value());
1600 } else {
1601 UNIMPLEMENTED(FATAL) << "We only support Copy() of size 4 and 8";
1602 }
1603}
1604
1605void Mips64Assembler::Copy(FrameOffset /*dest*/, Offset /*dest_offset*/, FrameOffset /*src*/, Offset
1606/*src_offset*/,
1607 ManagedRegister /*mscratch*/, size_t /*size*/) {
1608 UNIMPLEMENTED(FATAL) << "no mips64 implementation";
1609}
1610
1611void Mips64Assembler::MemoryBarrier(ManagedRegister) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001612 // TODO: sync?
Andreas Gampe57b34292015-01-14 15:45:59 -08001613 UNIMPLEMENTED(FATAL) << "no mips64 implementation";
1614}
1615
1616void Mips64Assembler::CreateHandleScopeEntry(ManagedRegister mout_reg,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001617 FrameOffset handle_scope_offset,
1618 ManagedRegister min_reg,
1619 bool null_allowed) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001620 Mips64ManagedRegister out_reg = mout_reg.AsMips64();
1621 Mips64ManagedRegister in_reg = min_reg.AsMips64();
1622 CHECK(in_reg.IsNoRegister() || in_reg.IsGpuRegister()) << in_reg;
1623 CHECK(out_reg.IsGpuRegister()) << out_reg;
1624 if (null_allowed) {
1625 Label null_arg;
1626 // Null values get a handle scope entry value of 0. Otherwise, the handle scope entry is
1627 // the address in the handle scope holding the reference.
1628 // e.g. out_reg = (handle == 0) ? 0 : (SP+handle_offset)
1629 if (in_reg.IsNoRegister()) {
Douglas Leungd90957f2015-04-30 19:22:49 -07001630 LoadFromOffset(kLoadUnsignedWord, out_reg.AsGpuRegister(),
Andreas Gampe57b34292015-01-14 15:45:59 -08001631 SP, handle_scope_offset.Int32Value());
1632 in_reg = out_reg;
1633 }
1634 if (!out_reg.Equals(in_reg)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001635 LoadConst32(out_reg.AsGpuRegister(), 0);
Andreas Gampe57b34292015-01-14 15:45:59 -08001636 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001637 Beqzc(in_reg.AsGpuRegister(), &null_arg);
1638 Daddiu64(out_reg.AsGpuRegister(), SP, handle_scope_offset.Int32Value());
1639 Bind(&null_arg);
Andreas Gampe57b34292015-01-14 15:45:59 -08001640 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 Daddiu64(out_reg.AsGpuRegister(), SP, handle_scope_offset.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001642 }
1643}
1644
1645void Mips64Assembler::CreateHandleScopeEntry(FrameOffset out_off,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646 FrameOffset handle_scope_offset,
1647 ManagedRegister mscratch,
1648 bool null_allowed) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001649 Mips64ManagedRegister scratch = mscratch.AsMips64();
1650 CHECK(scratch.IsGpuRegister()) << scratch;
1651 if (null_allowed) {
1652 Label null_arg;
Douglas Leungd90957f2015-04-30 19:22:49 -07001653 LoadFromOffset(kLoadUnsignedWord, scratch.AsGpuRegister(), SP,
Andreas Gampe57b34292015-01-14 15:45:59 -08001654 handle_scope_offset.Int32Value());
1655 // Null values get a handle scope entry value of 0. Otherwise, the handle scope entry is
1656 // the address in the handle scope holding the reference.
1657 // e.g. scratch = (scratch == 0) ? 0 : (SP+handle_scope_offset)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001658 Beqzc(scratch.AsGpuRegister(), &null_arg);
1659 Daddiu64(scratch.AsGpuRegister(), SP, handle_scope_offset.Int32Value());
1660 Bind(&null_arg);
Andreas Gampe57b34292015-01-14 15:45:59 -08001661 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001662 Daddiu64(scratch.AsGpuRegister(), SP, handle_scope_offset.Int32Value());
Andreas Gampe57b34292015-01-14 15:45:59 -08001663 }
1664 StoreToOffset(kStoreDoubleword, scratch.AsGpuRegister(), SP, out_off.Int32Value());
1665}
1666
1667// Given a handle scope entry, load the associated reference.
1668void Mips64Assembler::LoadReferenceFromHandleScope(ManagedRegister mout_reg,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001669 ManagedRegister min_reg) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001670 Mips64ManagedRegister out_reg = mout_reg.AsMips64();
1671 Mips64ManagedRegister in_reg = min_reg.AsMips64();
1672 CHECK(out_reg.IsGpuRegister()) << out_reg;
1673 CHECK(in_reg.IsGpuRegister()) << in_reg;
1674 Label null_arg;
1675 if (!out_reg.Equals(in_reg)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 LoadConst32(out_reg.AsGpuRegister(), 0);
Andreas Gampe57b34292015-01-14 15:45:59 -08001677 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001678 Beqzc(in_reg.AsGpuRegister(), &null_arg);
Andreas Gampe57b34292015-01-14 15:45:59 -08001679 LoadFromOffset(kLoadDoubleword, out_reg.AsGpuRegister(),
1680 in_reg.AsGpuRegister(), 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 Bind(&null_arg);
Andreas Gampe57b34292015-01-14 15:45:59 -08001682}
1683
1684void Mips64Assembler::VerifyObject(ManagedRegister /*src*/, bool /*could_be_null*/) {
1685 // TODO: not validating references
1686}
1687
1688void Mips64Assembler::VerifyObject(FrameOffset /*src*/, bool /*could_be_null*/) {
1689 // TODO: not validating references
1690}
1691
1692void Mips64Assembler::Call(ManagedRegister mbase, Offset offset, ManagedRegister mscratch) {
1693 Mips64ManagedRegister base = mbase.AsMips64();
1694 Mips64ManagedRegister scratch = mscratch.AsMips64();
1695 CHECK(base.IsGpuRegister()) << base;
1696 CHECK(scratch.IsGpuRegister()) << scratch;
1697 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(),
1698 base.AsGpuRegister(), offset.Int32Value());
1699 Jalr(scratch.AsGpuRegister());
1700 // TODO: place reference map on call
1701}
1702
1703void Mips64Assembler::Call(FrameOffset base, Offset offset, ManagedRegister mscratch) {
1704 Mips64ManagedRegister scratch = mscratch.AsMips64();
1705 CHECK(scratch.IsGpuRegister()) << scratch;
1706 // Call *(*(SP + base) + offset)
Mathieu Chartiere401d142015-04-22 13:56:20 -07001707 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(),
Andreas Gampe57b34292015-01-14 15:45:59 -08001708 SP, base.Int32Value());
1709 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(),
1710 scratch.AsGpuRegister(), offset.Int32Value());
1711 Jalr(scratch.AsGpuRegister());
1712 // TODO: place reference map on call
1713}
1714
1715void Mips64Assembler::CallFromThread64(ThreadOffset<8> /*offset*/, ManagedRegister /*mscratch*/) {
1716 UNIMPLEMENTED(FATAL) << "no mips64 implementation";
1717}
1718
1719void Mips64Assembler::GetCurrentThread(ManagedRegister tr) {
1720 Move(tr.AsMips64().AsGpuRegister(), S1);
1721}
1722
1723void Mips64Assembler::GetCurrentThread(FrameOffset offset,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 ManagedRegister /*mscratch*/) {
Andreas Gampe57b34292015-01-14 15:45:59 -08001725 StoreToOffset(kStoreDoubleword, S1, SP, offset.Int32Value());
1726}
1727
1728void Mips64Assembler::ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) {
1729 Mips64ManagedRegister scratch = mscratch.AsMips64();
1730 Mips64ExceptionSlowPath* slow = new Mips64ExceptionSlowPath(scratch, stack_adjust);
1731 buffer_.EnqueueSlowPath(slow);
1732 LoadFromOffset(kLoadDoubleword, scratch.AsGpuRegister(),
1733 S1, Thread::ExceptionOffset<8>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001734 Bnezc(scratch.AsGpuRegister(), slow->Entry());
Andreas Gampe57b34292015-01-14 15:45:59 -08001735}
1736
1737void Mips64ExceptionSlowPath::Emit(Assembler* sasm) {
1738 Mips64Assembler* sp_asm = down_cast<Mips64Assembler*>(sasm);
1739#define __ sp_asm->
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740 __ Bind(&entry_);
Andreas Gampe57b34292015-01-14 15:45:59 -08001741 if (stack_adjust_ != 0) { // Fix up the frame.
1742 __ DecreaseFrameSize(stack_adjust_);
1743 }
1744 // Pass exception object as argument
1745 // Don't care about preserving A0 as this call won't return
1746 __ Move(A0, scratch_.AsGpuRegister());
1747 // Set up call to Thread::Current()->pDeliverException
1748 __ LoadFromOffset(kLoadDoubleword, T9, S1,
Goran Jakovljevic75c40d42015-04-03 15:45:21 +02001749 QUICK_ENTRYPOINT_OFFSET(8, pDeliverException).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001750 // TODO: check T9 usage
Andreas Gampe57b34292015-01-14 15:45:59 -08001751 __ Jr(T9);
1752 // Call never returns
1753 __ Break();
1754#undef __
1755}
1756
1757} // namespace mips64
1758} // namespace art