blob: 883f013f874bc86116e37b3aff88c93f1c5dd6c0 [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);
253 void CeilLS(FpuRegister fd, FpuRegister fs);
254 void CeilLD(FpuRegister fd, FpuRegister fs);
255 void CeilWS(FpuRegister fd, FpuRegister fs);
256 void CeilWD(FpuRegister fd, FpuRegister fs);
257 void FloorLS(FpuRegister fd, FpuRegister fs);
258 void FloorLD(FpuRegister fd, FpuRegister fs);
259 void FloorWS(FpuRegister fd, FpuRegister fs);
260 void FloorWD(FpuRegister fd, FpuRegister fs);
261 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
262 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
263 void RintS(FpuRegister fd, FpuRegister fs);
264 void RintD(FpuRegister fd, FpuRegister fs);
265 void ClassS(FpuRegister fd, FpuRegister fs);
266 void ClassD(FpuRegister fd, FpuRegister fs);
267 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
268 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
269 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
270 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800271 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
272 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
273 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
274 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
275 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
276 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
277 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
278 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
279 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
280 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
281 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
282 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
283 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
284 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
285 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
286 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
287 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
288 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
289 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
290 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700291
292 void Cvtsw(FpuRegister fd, FpuRegister fs);
293 void Cvtdw(FpuRegister fd, FpuRegister fs);
294 void Cvtsd(FpuRegister fd, FpuRegister fs);
295 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700296 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700297 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800298
299 void Mfc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300 void Mtc1(GpuRegister rt, FpuRegister fs);
301 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
302 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800303 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
304 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
305 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
306 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
307
308 void Break();
309 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 void Move(GpuRegister rd, GpuRegister rs);
311 void Clear(GpuRegister rd);
312 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800313
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 // Higher level composite instructions.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 void LoadConst32(GpuRegister rd, int32_t value);
316 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
317
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
319
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700320 void Bind(Label* label) OVERRIDE {
321 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700323 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
324 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
325 }
326
327 void Bind(Mips64Label* label);
328 void Bc(Mips64Label* label);
329 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
330 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
331 void Bltzc(GpuRegister rt, Mips64Label* label);
332 void Bgtzc(GpuRegister rt, Mips64Label* label);
333 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
334 void Bgezc(GpuRegister rt, Mips64Label* label);
335 void Blezc(GpuRegister rt, Mips64Label* label);
336 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
337 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
338 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
339 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
340 void Beqzc(GpuRegister rs, Mips64Label* label);
341 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800342 void Bc1eqz(FpuRegister ft, Mips64Label* label);
343 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800344
345 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
346 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
347 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
348 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
349 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
350
351 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700352 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800353
354 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700355 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800356 //
357
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700358 // Emit code that will create an activation on the stack.
Andreas Gampe57b34292015-01-14 15:45:59 -0800359 void BuildFrame(size_t frame_size, ManagedRegister method_reg,
360 const std::vector<ManagedRegister>& callee_save_regs,
361 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
362
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700363 // Emit code that will remove an activation from the stack.
Andreas Gampe57b34292015-01-14 15:45:59 -0800364 void RemoveFrame(size_t frame_size,
365 const std::vector<ManagedRegister>& callee_save_regs) OVERRIDE;
366
367 void IncreaseFrameSize(size_t adjust) OVERRIDE;
368 void DecreaseFrameSize(size_t adjust) OVERRIDE;
369
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700370 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800371 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
372 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
373 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
374
375 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
376
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700377 void StoreStackOffsetToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs, FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800378 ManagedRegister mscratch) OVERRIDE;
379
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700380 void StoreStackPointerToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800381
382 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
383 ManagedRegister mscratch) OVERRIDE;
384
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700385 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800386 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
387
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700388 void LoadFromThread64(ManagedRegister mdest,
389 ThreadOffset<kMipsDoublewordSize> src,
390 size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800391
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800393
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100395 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800396
397 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
398
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700399 void LoadRawPtrFromThread64(ManagedRegister mdest,
400 ThreadOffset<kMipsDoublewordSize> offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800401
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700402 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800403 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
404
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700405 void CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset<kMipsDoublewordSize> thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800406 ManagedRegister mscratch) OVERRIDE;
407
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700408 void CopyRawPtrToThread64(ThreadOffset<kMipsDoublewordSize> thr_offs, FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800409 ManagedRegister mscratch) OVERRIDE;
410
411 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
412
413 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
414
415 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
416 size_t size) OVERRIDE;
417
418 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
419 ManagedRegister mscratch, size_t size) OVERRIDE;
420
421 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
422 size_t size) OVERRIDE;
423
424 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
425 ManagedRegister mscratch, size_t size) OVERRIDE;
426
427 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
428 ManagedRegister mscratch, size_t size) OVERRIDE;
429
430 void MemoryBarrier(ManagedRegister) OVERRIDE;
431
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700432 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800433 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
434
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700435 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800436 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
437
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700438 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800439 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
440 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
441
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700442 // 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 -0800443 // value is null and null_allowed. in_reg holds a possibly stale reference
444 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700445 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800446 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
447 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
448
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700449 // 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 -0800450 // value is null and null_allowed.
451 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
452 mscratch, bool null_allowed) OVERRIDE;
453
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700454 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800455 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
456
457 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
458 // know that src may not be null.
459 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
460 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
461
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700462 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800463 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
464 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700465 void CallFromThread64(ThreadOffset<kMipsDoublewordSize> offset,
466 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800467
468 // Generate code to check if Thread::Current()->exception_ is non-null
469 // and branch to a ExceptionSlowPath if it is.
470 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
471
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700472 // Emit slow paths queued during assembly and promote short branches to long if needed.
473 void FinalizeCode() OVERRIDE;
474
475 // Emit branches and finalize all instructions.
476 void FinalizeInstructions(const MemoryRegion& region);
477
478 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
479 // must be used instead of Mips64Label::GetPosition()).
480 uint32_t GetLabelLocation(Mips64Label* label) const;
481
482 // Get the final position of a label after local fixup based on the old position
483 // recorded before FinalizeCode().
484 uint32_t GetAdjustedPosition(uint32_t old_position);
485
486 enum BranchCondition {
487 kCondLT,
488 kCondGE,
489 kCondLE,
490 kCondGT,
491 kCondLTZ,
492 kCondGEZ,
493 kCondLEZ,
494 kCondGTZ,
495 kCondEQ,
496 kCondNE,
497 kCondEQZ,
498 kCondNEZ,
499 kCondLTU,
500 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800501 kCondF, // Floating-point predicate false.
502 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700503 kUncond,
504 };
505 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
506
Andreas Gampe57b34292015-01-14 15:45:59 -0800507 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700508 class Branch {
509 public:
510 enum Type {
511 // Short branches.
512 kUncondBranch,
513 kCondBranch,
514 kCall,
515 // Long branches.
516 kLongUncondBranch,
517 kLongCondBranch,
518 kLongCall,
519 };
520
521 // Bit sizes of offsets defined as enums to minimize chance of typos.
522 enum OffsetBits {
523 kOffset16 = 16,
524 kOffset18 = 18,
525 kOffset21 = 21,
526 kOffset23 = 23,
527 kOffset28 = 28,
528 kOffset32 = 32,
529 };
530
531 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
532 static constexpr int32_t kMaxBranchLength = 32;
533 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
534
535 struct BranchInfo {
536 // Branch length as a number of 4-byte-long instructions.
537 uint32_t length;
538 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
539 // PC-relative offset (or its most significant 16-bit half, which goes first).
540 uint32_t instr_offset;
541 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
542 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
543 // instructions) from the instruction containing the offset.
544 uint32_t pc_org;
545 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
546 // an exception: use kOffset23 for beqzc/bnezc).
547 OffsetBits offset_size;
548 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
549 // count.
550 int offset_shift;
551 };
552 static const BranchInfo branch_info_[/* Type */];
553
554 // Unconditional branch.
555 Branch(uint32_t location, uint32_t target);
556 // Conditional branch.
557 Branch(uint32_t location,
558 uint32_t target,
559 BranchCondition condition,
560 GpuRegister lhs_reg,
561 GpuRegister rhs_reg = ZERO);
562 // Call (branch and link) that stores the target address in a given register (i.e. T9).
563 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
564
565 // Some conditional branches with lhs = rhs are effectively NOPs, while some
566 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
567 // So, we need a way to identify such branches in order to emit no instructions for them
568 // or change them to unconditional.
569 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
570 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
571
572 static BranchCondition OppositeCondition(BranchCondition cond);
573
574 Type GetType() const;
575 BranchCondition GetCondition() const;
576 GpuRegister GetLeftRegister() const;
577 GpuRegister GetRightRegister() const;
578 uint32_t GetTarget() const;
579 uint32_t GetLocation() const;
580 uint32_t GetOldLocation() const;
581 uint32_t GetLength() const;
582 uint32_t GetOldLength() const;
583 uint32_t GetSize() const;
584 uint32_t GetOldSize() const;
585 uint32_t GetEndLocation() const;
586 uint32_t GetOldEndLocation() const;
587 bool IsLong() const;
588 bool IsResolved() const;
589
590 // Returns the bit size of the signed offset that the branch instruction can handle.
591 OffsetBits GetOffsetSize() const;
592
593 // Calculates the distance between two byte locations in the assembler buffer and
594 // returns the number of bits needed to represent the distance as a signed integer.
595 //
596 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
597 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
598 //
599 // Composite branches (made of several instructions) with longer reach have 32-bit
600 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
601 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
602 // however. Consider the following implementation of a long unconditional branch, for
603 // example:
604 //
605 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
606 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
607 //
608 // Both of the above instructions take 16-bit signed offsets as immediate operands.
609 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
610 // due to sign extension. This must be compensated for by incrementing offset_31_16
611 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
612 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
613 // Therefore, the long branch range is something like from PC - 0x80000000 to
614 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
615 //
616 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
617 // case with the addiu instruction and a 16 bit offset.
618 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
619
620 // Resolve a branch when the target is known.
621 void Resolve(uint32_t target);
622
623 // Relocate a branch by a given delta if needed due to expansion of this or another
624 // branch at a given location by this delta (just changes location_ and target_).
625 void Relocate(uint32_t expand_location, uint32_t delta);
626
627 // If the branch is short, changes its type to long.
628 void PromoteToLong();
629
630 // If necessary, updates the type by promoting a short branch to a long branch
631 // based on the branch location and target. Returns the amount (in bytes) by
632 // which the branch size has increased.
633 // max_short_distance caps the maximum distance between location_ and target_
634 // that is allowed for short branches. This is for debugging/testing purposes.
635 // max_short_distance = 0 forces all short branches to become long.
636 // Use the implicit default argument when not debugging/testing.
637 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
638
639 // Returns the location of the instruction(s) containing the offset.
640 uint32_t GetOffsetLocation() const;
641
642 // Calculates and returns the offset ready for encoding in the branch instruction(s).
643 uint32_t GetOffset() const;
644
645 private:
646 // Completes branch construction by determining and recording its type.
647 void InitializeType(bool is_call);
648 // Helper for the above.
649 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
650
651 uint32_t old_location_; // Offset into assembler buffer in bytes.
652 uint32_t location_; // Offset into assembler buffer in bytes.
653 uint32_t target_; // Offset into assembler buffer in bytes.
654
655 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
656 // indirect call register.
657 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
658 BranchCondition condition_; // Condition for conditional branches.
659
660 Type type_; // Current type of the branch.
661 Type old_type_; // Initial type of the branch.
662 };
663 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
664 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
665
Andreas Gampe57b34292015-01-14 15:45:59 -0800666 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700667 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
668 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800669 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700670 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700671 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800672 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
673 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700674 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
675
676 void Buncond(Mips64Label* label);
677 void Bcond(Mips64Label* label,
678 BranchCondition condition,
679 GpuRegister lhs,
680 GpuRegister rhs = ZERO);
681 void Call(Mips64Label* label, GpuRegister indirect_reg);
682 void FinalizeLabeledBranch(Mips64Label* label);
683
684 Branch* GetBranch(uint32_t branch_id);
685 const Branch* GetBranch(uint32_t branch_id) const;
686
687 void PromoteBranches();
688 void EmitBranch(Branch* branch);
689 void EmitBranches();
690 void PatchCFI();
691
692 // Emits exception block.
693 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
694
695 // List of exception blocks to generate at the end of the code cache.
696 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
697
698 std::vector<Branch> branches_;
699
700 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
701 bool overwriting_;
702 // The current overwrite location.
703 uint32_t overwrite_location_;
704
705 // Data for AdjustedPosition(), see the description there.
706 uint32_t last_position_adjustment_;
707 uint32_t last_old_position_;
708 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800709
Andreas Gampe57b34292015-01-14 15:45:59 -0800710 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
711};
712
Andreas Gampe57b34292015-01-14 15:45:59 -0800713} // namespace mips64
714} // namespace art
715
716#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_