blob: 3262640ce7ec817058abedc56149bc8dafa69811 [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
23#include "base/macros.h"
24#include "constants_mips64.h"
25#include "globals.h"
26#include "managed_register_mips64.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080027#include "offsets.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070028#include "utils/assembler.h"
29#include "utils/label.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080030
31namespace art {
32namespace mips64 {
33
Alexey Frunzea0e87b02015-09-24 22:57:20 -070034static constexpr size_t kMipsDoublewordSize = 8;
35
Andreas Gampe57b34292015-01-14 15:45:59 -080036enum LoadOperandType {
37 kLoadSignedByte,
38 kLoadUnsignedByte,
39 kLoadSignedHalfword,
40 kLoadUnsignedHalfword,
41 kLoadWord,
Douglas Leungd90957f2015-04-30 19:22:49 -070042 kLoadUnsignedWord,
Andreas Gampe57b34292015-01-14 15:45:59 -080043 kLoadDoubleword
44};
45
46enum StoreOperandType {
47 kStoreByte,
48 kStoreHalfword,
49 kStoreWord,
50 kStoreDoubleword
51};
52
Chris Larsen14500822015-10-01 11:35:18 -070053// Used to test the values returned by ClassS/ClassD.
54enum FPClassMaskType {
55 kSignalingNaN = 0x001,
56 kQuietNaN = 0x002,
57 kNegativeInfinity = 0x004,
58 kNegativeNormal = 0x008,
59 kNegativeSubnormal = 0x010,
60 kNegativeZero = 0x020,
61 kPositiveInfinity = 0x040,
62 kPositiveNormal = 0x080,
63 kPositiveSubnormal = 0x100,
64 kPositiveZero = 0x200,
65};
66
Alexey Frunzea0e87b02015-09-24 22:57:20 -070067class Mips64Label : public Label {
68 public:
69 Mips64Label() : prev_branch_id_plus_one_(0) {}
70
71 Mips64Label(Mips64Label&& src)
72 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
73
74 private:
75 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
76
77 friend class Mips64Assembler;
78 DISALLOW_COPY_AND_ASSIGN(Mips64Label);
79};
80
81// Slowpath entered when Thread::Current()->_exception is non-null.
82class Mips64ExceptionSlowPath {
83 public:
84 explicit Mips64ExceptionSlowPath(Mips64ManagedRegister scratch, size_t stack_adjust)
85 : scratch_(scratch), stack_adjust_(stack_adjust) {}
86
87 Mips64ExceptionSlowPath(Mips64ExceptionSlowPath&& src)
88 : scratch_(src.scratch_),
89 stack_adjust_(src.stack_adjust_),
90 exception_entry_(std::move(src.exception_entry_)) {}
91
92 private:
93 Mips64Label* Entry() { return &exception_entry_; }
94 const Mips64ManagedRegister scratch_;
95 const size_t stack_adjust_;
96 Mips64Label exception_entry_;
97
98 friend class Mips64Assembler;
99 DISALLOW_COPY_AND_ASSIGN(Mips64ExceptionSlowPath);
100};
101
Andreas Gampe57b34292015-01-14 15:45:59 -0800102class Mips64Assembler FINAL : public Assembler {
103 public:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700104 Mips64Assembler()
105 : overwriting_(false),
106 overwrite_location_(0),
107 last_position_adjustment_(0),
108 last_old_position_(0),
109 last_branch_id_(0) {
110 cfi().DelayEmittingAdvancePCs();
111 }
112
113 virtual ~Mips64Assembler() {
114 for (auto& branch : branches_) {
115 CHECK(branch.IsResolved());
116 }
117 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800118
119 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800120 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
121 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
123 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800124 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
126
Alexey Frunzec857c742015-09-23 15:12:39 -0700127 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
128 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
129 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
130 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
131 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
132 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
133 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
134 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
135 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
136 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
137 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
138 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800139
140 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
141 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
142 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
143 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
144 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
145 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
146 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
147
Alexey Frunzec857c742015-09-23 15:12:39 -0700148 void Bitswap(GpuRegister rd, GpuRegister rt);
149 void Dbitswap(GpuRegister rd, GpuRegister rt);
150 void Seb(GpuRegister rd, GpuRegister rt);
151 void Seh(GpuRegister rd, GpuRegister rt);
152 void Dsbh(GpuRegister rd, GpuRegister rt);
153 void Dshd(GpuRegister rd, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700154 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size_less_one); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700155 void Wsbh(GpuRegister rd, GpuRegister rt);
156 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
157 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
158 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
159 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160
161 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
162 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700163 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700164 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
165 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
166 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700167 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
169 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
170 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700171 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
173 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
174 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700175 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700176 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
177 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
178 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700179 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800181
182 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
183 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
184 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800186 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
187 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700188 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800189 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700190 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
191 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800193
194 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
195 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
196 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700197 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800198
199 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
200 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
201 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
202 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700203 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
204 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
205 void Clz(GpuRegister rd, GpuRegister rs);
206 void Clo(GpuRegister rd, GpuRegister rs);
207 void Dclz(GpuRegister rd, GpuRegister rs);
208 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800209
Alexey Frunze4dda3372015-06-01 18:31:49 -0700210 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800211 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700213 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700214 void Addiupc(GpuRegister rs, uint32_t imm19);
215 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700216 void Jic(GpuRegister rt, uint16_t imm16);
217 void Jialc(GpuRegister rt, uint16_t imm16);
218 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
219 void Bltzc(GpuRegister rt, uint16_t imm16);
220 void Bgtzc(GpuRegister rt, uint16_t imm16);
221 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
222 void Bgezc(GpuRegister rt, uint16_t imm16);
223 void Blezc(GpuRegister rt, uint16_t imm16);
224 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
225 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
226 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
227 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
228 void Beqzc(GpuRegister rs, uint32_t imm21);
229 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800230 void Bc1eqz(FpuRegister ft, uint16_t imm16);
231 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800232
233 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
234 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
235 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
236 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
237 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
238 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
239 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
240 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700241 void SqrtS(FpuRegister fd, FpuRegister fs);
242 void SqrtD(FpuRegister fd, FpuRegister fs);
243 void AbsS(FpuRegister fd, FpuRegister fs);
244 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800245 void MovS(FpuRegister fd, FpuRegister fs);
246 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 void NegS(FpuRegister fd, FpuRegister fs);
248 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700249 void RoundLS(FpuRegister fd, FpuRegister fs);
250 void RoundLD(FpuRegister fd, FpuRegister fs);
251 void RoundWS(FpuRegister fd, FpuRegister fs);
252 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800253 void TruncLS(FpuRegister fd, FpuRegister fs);
254 void TruncLD(FpuRegister fd, FpuRegister fs);
255 void TruncWS(FpuRegister fd, FpuRegister fs);
256 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700257 void CeilLS(FpuRegister fd, FpuRegister fs);
258 void CeilLD(FpuRegister fd, FpuRegister fs);
259 void CeilWS(FpuRegister fd, FpuRegister fs);
260 void CeilWD(FpuRegister fd, FpuRegister fs);
261 void FloorLS(FpuRegister fd, FpuRegister fs);
262 void FloorLD(FpuRegister fd, FpuRegister fs);
263 void FloorWS(FpuRegister fd, FpuRegister fs);
264 void FloorWD(FpuRegister fd, FpuRegister fs);
265 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
266 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
267 void RintS(FpuRegister fd, FpuRegister fs);
268 void RintD(FpuRegister fd, FpuRegister fs);
269 void ClassS(FpuRegister fd, FpuRegister fs);
270 void ClassD(FpuRegister fd, FpuRegister fs);
271 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
272 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
273 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
274 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800275 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
276 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
277 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
278 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
279 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
280 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
281 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
282 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
283 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
284 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
285 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
286 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
287 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
288 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
289 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
290 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
291 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
292 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
293 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
294 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295
296 void Cvtsw(FpuRegister fd, FpuRegister fs);
297 void Cvtdw(FpuRegister fd, FpuRegister fs);
298 void Cvtsd(FpuRegister fd, FpuRegister fs);
299 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700300 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700301 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800302
303 void Mfc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700304 void Mtc1(GpuRegister rt, FpuRegister fs);
305 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
306 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800307 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
308 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
309 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
310 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
311
312 void Break();
313 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 void Move(GpuRegister rd, GpuRegister rs);
315 void Clear(GpuRegister rd);
316 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800317
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 // Higher level composite instructions.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 void LoadConst32(GpuRegister rd, int32_t value);
320 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
321
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
323
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700324 void Bind(Label* label) OVERRIDE {
325 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700326 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700327 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
328 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
329 }
330
331 void Bind(Mips64Label* label);
332 void Bc(Mips64Label* label);
333 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
334 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
335 void Bltzc(GpuRegister rt, Mips64Label* label);
336 void Bgtzc(GpuRegister rt, Mips64Label* label);
337 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
338 void Bgezc(GpuRegister rt, Mips64Label* label);
339 void Blezc(GpuRegister rt, Mips64Label* label);
340 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
341 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
342 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
343 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
344 void Beqzc(GpuRegister rs, Mips64Label* label);
345 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800346 void Bc1eqz(FpuRegister ft, Mips64Label* label);
347 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800348
349 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
350 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
351 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
352 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
353 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
354
355 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800357
358 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700359 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800360 //
361
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700362 // Emit code that will create an activation on the stack.
Andreas Gampe57b34292015-01-14 15:45:59 -0800363 void BuildFrame(size_t frame_size, ManagedRegister method_reg,
364 const std::vector<ManagedRegister>& callee_save_regs,
365 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
366
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700367 // Emit code that will remove an activation from the stack.
Andreas Gampe57b34292015-01-14 15:45:59 -0800368 void RemoveFrame(size_t frame_size,
369 const std::vector<ManagedRegister>& callee_save_regs) OVERRIDE;
370
371 void IncreaseFrameSize(size_t adjust) OVERRIDE;
372 void DecreaseFrameSize(size_t adjust) OVERRIDE;
373
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700374 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800375 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
376 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
377 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
378
379 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
380
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700381 void StoreStackOffsetToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs, FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800382 ManagedRegister mscratch) OVERRIDE;
383
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700384 void StoreStackPointerToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800385
386 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
387 ManagedRegister mscratch) OVERRIDE;
388
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700389 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800390 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
391
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700392 void LoadFromThread64(ManagedRegister mdest,
393 ThreadOffset<kMipsDoublewordSize> src,
394 size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800395
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800397
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100399 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800400
401 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
402
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700403 void LoadRawPtrFromThread64(ManagedRegister mdest,
404 ThreadOffset<kMipsDoublewordSize> offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800405
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700406 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800407 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
408
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700409 void CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset<kMipsDoublewordSize> thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800410 ManagedRegister mscratch) OVERRIDE;
411
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700412 void CopyRawPtrToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs, FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800413 ManagedRegister mscratch) OVERRIDE;
414
415 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
416
417 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
418
419 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
420 size_t size) OVERRIDE;
421
422 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
423 ManagedRegister mscratch, size_t size) OVERRIDE;
424
425 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
426 size_t size) OVERRIDE;
427
428 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
429 ManagedRegister mscratch, size_t size) OVERRIDE;
430
431 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
432 ManagedRegister mscratch, size_t size) OVERRIDE;
433
434 void MemoryBarrier(ManagedRegister) OVERRIDE;
435
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700436 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800437 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
438
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700439 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800440 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
441
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700442 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800443 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
444 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
445
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700446 // 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 -0800447 // value is null and null_allowed. in_reg holds a possibly stale reference
448 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700449 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800450 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
451 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
452
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 // 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 -0800454 // value is null and null_allowed.
455 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
456 mscratch, bool null_allowed) OVERRIDE;
457
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700458 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800459 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
460
461 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
462 // know that src may not be null.
463 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
464 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
465
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700466 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800467 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
468 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700469 void CallFromThread64(ThreadOffset<kMipsDoublewordSize> offset,
470 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800471
472 // Generate code to check if Thread::Current()->exception_ is non-null
473 // and branch to a ExceptionSlowPath if it is.
474 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
475
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700476 // Emit slow paths queued during assembly and promote short branches to long if needed.
477 void FinalizeCode() OVERRIDE;
478
479 // Emit branches and finalize all instructions.
480 void FinalizeInstructions(const MemoryRegion& region);
481
482 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
483 // must be used instead of Mips64Label::GetPosition()).
484 uint32_t GetLabelLocation(Mips64Label* label) const;
485
486 // Get the final position of a label after local fixup based on the old position
487 // recorded before FinalizeCode().
488 uint32_t GetAdjustedPosition(uint32_t old_position);
489
490 enum BranchCondition {
491 kCondLT,
492 kCondGE,
493 kCondLE,
494 kCondGT,
495 kCondLTZ,
496 kCondGEZ,
497 kCondLEZ,
498 kCondGTZ,
499 kCondEQ,
500 kCondNE,
501 kCondEQZ,
502 kCondNEZ,
503 kCondLTU,
504 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800505 kCondF, // Floating-point predicate false.
506 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700507 kUncond,
508 };
509 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
510
Andreas Gampe57b34292015-01-14 15:45:59 -0800511 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700512 class Branch {
513 public:
514 enum Type {
515 // Short branches.
516 kUncondBranch,
517 kCondBranch,
518 kCall,
519 // Long branches.
520 kLongUncondBranch,
521 kLongCondBranch,
522 kLongCall,
523 };
524
525 // Bit sizes of offsets defined as enums to minimize chance of typos.
526 enum OffsetBits {
527 kOffset16 = 16,
528 kOffset18 = 18,
529 kOffset21 = 21,
530 kOffset23 = 23,
531 kOffset28 = 28,
532 kOffset32 = 32,
533 };
534
535 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
536 static constexpr int32_t kMaxBranchLength = 32;
537 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
538
539 struct BranchInfo {
540 // Branch length as a number of 4-byte-long instructions.
541 uint32_t length;
542 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
543 // PC-relative offset (or its most significant 16-bit half, which goes first).
544 uint32_t instr_offset;
545 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
546 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
547 // instructions) from the instruction containing the offset.
548 uint32_t pc_org;
549 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
550 // an exception: use kOffset23 for beqzc/bnezc).
551 OffsetBits offset_size;
552 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
553 // count.
554 int offset_shift;
555 };
556 static const BranchInfo branch_info_[/* Type */];
557
558 // Unconditional branch.
559 Branch(uint32_t location, uint32_t target);
560 // Conditional branch.
561 Branch(uint32_t location,
562 uint32_t target,
563 BranchCondition condition,
564 GpuRegister lhs_reg,
565 GpuRegister rhs_reg = ZERO);
566 // Call (branch and link) that stores the target address in a given register (i.e. T9).
567 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
568
569 // Some conditional branches with lhs = rhs are effectively NOPs, while some
570 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
571 // So, we need a way to identify such branches in order to emit no instructions for them
572 // or change them to unconditional.
573 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
574 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
575
576 static BranchCondition OppositeCondition(BranchCondition cond);
577
578 Type GetType() const;
579 BranchCondition GetCondition() const;
580 GpuRegister GetLeftRegister() const;
581 GpuRegister GetRightRegister() const;
582 uint32_t GetTarget() const;
583 uint32_t GetLocation() const;
584 uint32_t GetOldLocation() const;
585 uint32_t GetLength() const;
586 uint32_t GetOldLength() const;
587 uint32_t GetSize() const;
588 uint32_t GetOldSize() const;
589 uint32_t GetEndLocation() const;
590 uint32_t GetOldEndLocation() const;
591 bool IsLong() const;
592 bool IsResolved() const;
593
594 // Returns the bit size of the signed offset that the branch instruction can handle.
595 OffsetBits GetOffsetSize() const;
596
597 // Calculates the distance between two byte locations in the assembler buffer and
598 // returns the number of bits needed to represent the distance as a signed integer.
599 //
600 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
601 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
602 //
603 // Composite branches (made of several instructions) with longer reach have 32-bit
604 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
605 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
606 // however. Consider the following implementation of a long unconditional branch, for
607 // example:
608 //
609 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
610 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
611 //
612 // Both of the above instructions take 16-bit signed offsets as immediate operands.
613 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
614 // due to sign extension. This must be compensated for by incrementing offset_31_16
615 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
616 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
617 // Therefore, the long branch range is something like from PC - 0x80000000 to
618 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
619 //
620 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
621 // case with the addiu instruction and a 16 bit offset.
622 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
623
624 // Resolve a branch when the target is known.
625 void Resolve(uint32_t target);
626
627 // Relocate a branch by a given delta if needed due to expansion of this or another
628 // branch at a given location by this delta (just changes location_ and target_).
629 void Relocate(uint32_t expand_location, uint32_t delta);
630
631 // If the branch is short, changes its type to long.
632 void PromoteToLong();
633
634 // If necessary, updates the type by promoting a short branch to a long branch
635 // based on the branch location and target. Returns the amount (in bytes) by
636 // which the branch size has increased.
637 // max_short_distance caps the maximum distance between location_ and target_
638 // that is allowed for short branches. This is for debugging/testing purposes.
639 // max_short_distance = 0 forces all short branches to become long.
640 // Use the implicit default argument when not debugging/testing.
641 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
642
643 // Returns the location of the instruction(s) containing the offset.
644 uint32_t GetOffsetLocation() const;
645
646 // Calculates and returns the offset ready for encoding in the branch instruction(s).
647 uint32_t GetOffset() const;
648
649 private:
650 // Completes branch construction by determining and recording its type.
651 void InitializeType(bool is_call);
652 // Helper for the above.
653 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
654
655 uint32_t old_location_; // Offset into assembler buffer in bytes.
656 uint32_t location_; // Offset into assembler buffer in bytes.
657 uint32_t target_; // Offset into assembler buffer in bytes.
658
659 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
660 // indirect call register.
661 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
662 BranchCondition condition_; // Condition for conditional branches.
663
664 Type type_; // Current type of the branch.
665 Type old_type_; // Initial type of the branch.
666 };
667 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
668 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
669
Andreas Gampe57b34292015-01-14 15:45:59 -0800670 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700671 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
672 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800673 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700674 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700675 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800676 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
677 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700678 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
679
680 void Buncond(Mips64Label* label);
681 void Bcond(Mips64Label* label,
682 BranchCondition condition,
683 GpuRegister lhs,
684 GpuRegister rhs = ZERO);
685 void Call(Mips64Label* label, GpuRegister indirect_reg);
686 void FinalizeLabeledBranch(Mips64Label* label);
687
688 Branch* GetBranch(uint32_t branch_id);
689 const Branch* GetBranch(uint32_t branch_id) const;
690
691 void PromoteBranches();
692 void EmitBranch(Branch* branch);
693 void EmitBranches();
694 void PatchCFI();
695
696 // Emits exception block.
697 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
698
699 // List of exception blocks to generate at the end of the code cache.
700 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
701
702 std::vector<Branch> branches_;
703
704 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
705 bool overwriting_;
706 // The current overwrite location.
707 uint32_t overwrite_location_;
708
709 // Data for AdjustedPosition(), see the description there.
710 uint32_t last_position_adjustment_;
711 uint32_t last_old_position_;
712 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800713
Andreas Gampe57b34292015-01-14 15:45:59 -0800714 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
715};
716
Andreas Gampe57b34292015-01-14 15:45:59 -0800717} // namespace mips64
718} // namespace art
719
720#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_