blob: 1ad05b038b513a0d2d23fbabecaa86edefab57a9 [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
Lazar Trsicd9672662015-09-03 17:33:01 +020034static constexpr size_t kMips64WordSize = 4;
35static constexpr size_t kMips64DoublewordSize = 8;
Alexey Frunzea0e87b02015-09-24 22:57:20 -070036
Andreas Gampe57b34292015-01-14 15:45:59 -080037enum LoadOperandType {
38 kLoadSignedByte,
39 kLoadUnsignedByte,
40 kLoadSignedHalfword,
41 kLoadUnsignedHalfword,
42 kLoadWord,
Douglas Leungd90957f2015-04-30 19:22:49 -070043 kLoadUnsignedWord,
Andreas Gampe57b34292015-01-14 15:45:59 -080044 kLoadDoubleword
45};
46
47enum StoreOperandType {
48 kStoreByte,
49 kStoreHalfword,
50 kStoreWord,
51 kStoreDoubleword
52};
53
Chris Larsen14500822015-10-01 11:35:18 -070054// Used to test the values returned by ClassS/ClassD.
55enum FPClassMaskType {
56 kSignalingNaN = 0x001,
57 kQuietNaN = 0x002,
58 kNegativeInfinity = 0x004,
59 kNegativeNormal = 0x008,
60 kNegativeSubnormal = 0x010,
61 kNegativeZero = 0x020,
62 kPositiveInfinity = 0x040,
63 kPositiveNormal = 0x080,
64 kPositiveSubnormal = 0x100,
65 kPositiveZero = 0x200,
66};
67
Alexey Frunzea0e87b02015-09-24 22:57:20 -070068class Mips64Label : public Label {
69 public:
70 Mips64Label() : prev_branch_id_plus_one_(0) {}
71
72 Mips64Label(Mips64Label&& src)
73 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
74
75 private:
76 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
77
78 friend class Mips64Assembler;
79 DISALLOW_COPY_AND_ASSIGN(Mips64Label);
80};
81
82// Slowpath entered when Thread::Current()->_exception is non-null.
83class Mips64ExceptionSlowPath {
84 public:
85 explicit Mips64ExceptionSlowPath(Mips64ManagedRegister scratch, size_t stack_adjust)
86 : scratch_(scratch), stack_adjust_(stack_adjust) {}
87
88 Mips64ExceptionSlowPath(Mips64ExceptionSlowPath&& src)
89 : scratch_(src.scratch_),
90 stack_adjust_(src.stack_adjust_),
91 exception_entry_(std::move(src.exception_entry_)) {}
92
93 private:
94 Mips64Label* Entry() { return &exception_entry_; }
95 const Mips64ManagedRegister scratch_;
96 const size_t stack_adjust_;
97 Mips64Label exception_entry_;
98
99 friend class Mips64Assembler;
100 DISALLOW_COPY_AND_ASSIGN(Mips64ExceptionSlowPath);
101};
102
Andreas Gampe57b34292015-01-14 15:45:59 -0800103class Mips64Assembler FINAL : public Assembler {
104 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100105 explicit Mips64Assembler(ArenaAllocator* arena)
106 : Assembler(arena),
107 overwriting_(false),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700108 overwrite_location_(0),
109 last_position_adjustment_(0),
110 last_old_position_(0),
111 last_branch_id_(0) {
112 cfi().DelayEmittingAdvancePCs();
113 }
114
115 virtual ~Mips64Assembler() {
116 for (auto& branch : branches_) {
117 CHECK(branch.IsResolved());
118 }
119 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800120
121 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800122 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
123 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
125 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800126 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
128
Alexey Frunzec857c742015-09-23 15:12:39 -0700129 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
130 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
131 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
132 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
133 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
134 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
135 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
136 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
137 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
138 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
139 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
140 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800141
142 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
143 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
144 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
145 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
146 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
147 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
148 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
149
Alexey Frunzec857c742015-09-23 15:12:39 -0700150 void Bitswap(GpuRegister rd, GpuRegister rt);
151 void Dbitswap(GpuRegister rd, GpuRegister rt);
152 void Seb(GpuRegister rd, GpuRegister rt);
153 void Seh(GpuRegister rd, GpuRegister rt);
154 void Dsbh(GpuRegister rd, GpuRegister rt);
155 void Dshd(GpuRegister rd, GpuRegister rt);
Lazar Trsicd9672662015-09-03 17:33:01 +0200156 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size); // MIPS64
157 void Dinsu(GpuRegister rt, GpuRegister rs, int pos, int size); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700158 void Wsbh(GpuRegister rd, GpuRegister rt);
159 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
160 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
161 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
162 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700163
164 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
165 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700166 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700167 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
168 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
169 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700170 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
172 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
173 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700174 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700175 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
176 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
177 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700178 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700179 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
180 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
181 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700182 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800184
185 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
186 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
187 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700188 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800189 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
190 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700191 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800192 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700193 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
194 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700195 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800196
197 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
198 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
199 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800201
202 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
203 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
204 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
205 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700206 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
207 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
208 void Clz(GpuRegister rd, GpuRegister rs);
209 void Clo(GpuRegister rd, GpuRegister rs);
210 void Dclz(GpuRegister rd, GpuRegister rs);
211 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800212
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800214 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700215 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700216 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700217 void Addiupc(GpuRegister rs, uint32_t imm19);
218 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700219 void Jic(GpuRegister rt, uint16_t imm16);
220 void Jialc(GpuRegister rt, uint16_t imm16);
221 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
222 void Bltzc(GpuRegister rt, uint16_t imm16);
223 void Bgtzc(GpuRegister rt, uint16_t imm16);
224 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
225 void Bgezc(GpuRegister rt, uint16_t imm16);
226 void Blezc(GpuRegister rt, uint16_t imm16);
227 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
228 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
229 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
230 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
231 void Beqzc(GpuRegister rs, uint32_t imm21);
232 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800233 void Bc1eqz(FpuRegister ft, uint16_t imm16);
234 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800235
236 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
237 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
238 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
239 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
240 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
241 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
242 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
243 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700244 void SqrtS(FpuRegister fd, FpuRegister fs);
245 void SqrtD(FpuRegister fd, FpuRegister fs);
246 void AbsS(FpuRegister fd, FpuRegister fs);
247 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800248 void MovS(FpuRegister fd, FpuRegister fs);
249 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700250 void NegS(FpuRegister fd, FpuRegister fs);
251 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700252 void RoundLS(FpuRegister fd, FpuRegister fs);
253 void RoundLD(FpuRegister fd, FpuRegister fs);
254 void RoundWS(FpuRegister fd, FpuRegister fs);
255 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800256 void TruncLS(FpuRegister fd, FpuRegister fs);
257 void TruncLD(FpuRegister fd, FpuRegister fs);
258 void TruncWS(FpuRegister fd, FpuRegister fs);
259 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700260 void CeilLS(FpuRegister fd, FpuRegister fs);
261 void CeilLD(FpuRegister fd, FpuRegister fs);
262 void CeilWS(FpuRegister fd, FpuRegister fs);
263 void CeilWD(FpuRegister fd, FpuRegister fs);
264 void FloorLS(FpuRegister fd, FpuRegister fs);
265 void FloorLD(FpuRegister fd, FpuRegister fs);
266 void FloorWS(FpuRegister fd, FpuRegister fs);
267 void FloorWD(FpuRegister fd, FpuRegister fs);
268 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
269 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
270 void RintS(FpuRegister fd, FpuRegister fs);
271 void RintD(FpuRegister fd, FpuRegister fs);
272 void ClassS(FpuRegister fd, FpuRegister fs);
273 void ClassD(FpuRegister fd, FpuRegister fs);
274 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
275 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
276 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
277 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800278 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
279 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
280 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
281 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
282 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
283 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
284 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
285 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
286 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
287 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
288 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
289 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
290 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
291 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
292 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
293 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
294 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
295 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
296 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
297 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700298
299 void Cvtsw(FpuRegister fd, FpuRegister fs);
300 void Cvtdw(FpuRegister fd, FpuRegister fs);
301 void Cvtsd(FpuRegister fd, FpuRegister fs);
302 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700303 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700304 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800305
306 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200307 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200309 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
311 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800312 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
313 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
314 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
315 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
316
317 void Break();
318 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 void Move(GpuRegister rd, GpuRegister rs);
320 void Clear(GpuRegister rd);
321 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800322
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700323 // Higher level composite instructions.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700324 void LoadConst32(GpuRegister rd, int32_t value);
325 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
326
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
328
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700329 void Bind(Label* label) OVERRIDE {
330 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700331 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700332 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
333 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
334 }
335
336 void Bind(Mips64Label* label);
337 void Bc(Mips64Label* label);
338 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
339 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
340 void Bltzc(GpuRegister rt, Mips64Label* label);
341 void Bgtzc(GpuRegister rt, Mips64Label* label);
342 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
343 void Bgezc(GpuRegister rt, Mips64Label* label);
344 void Blezc(GpuRegister rt, Mips64Label* label);
345 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
346 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
347 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
348 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
349 void Beqzc(GpuRegister rs, Mips64Label* label);
350 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800351 void Bc1eqz(FpuRegister ft, Mips64Label* label);
352 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800353
354 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
355 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
356 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
357 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
358 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
359
360 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800362
363 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700364 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800365 //
366
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700367 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100368 void BuildFrame(size_t frame_size,
369 ManagedRegister method_reg,
370 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800371 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
372
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700373 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100374 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800375
376 void IncreaseFrameSize(size_t adjust) OVERRIDE;
377 void DecreaseFrameSize(size_t adjust) OVERRIDE;
378
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700379 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800380 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
381 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
382 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
383
384 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
385
Andreas Gampe542451c2016-07-26 09:02:02 -0700386 void StoreStackOffsetToThread64(ThreadOffset64 thr_offs,
387 FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800388 ManagedRegister mscratch) OVERRIDE;
389
Andreas Gampe542451c2016-07-26 09:02:02 -0700390 void StoreStackPointerToThread64(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800391
392 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
393 ManagedRegister mscratch) OVERRIDE;
394
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700395 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800396 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
397
Andreas Gampe542451c2016-07-26 09:02:02 -0700398 void LoadFromThread64(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800399
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800401
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100403 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800404
405 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
406
Andreas Gampe542451c2016-07-26 09:02:02 -0700407 void LoadRawPtrFromThread64(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800408
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700409 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800410 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
411
Andreas Gampe542451c2016-07-26 09:02:02 -0700412 void CopyRawPtrFromThread64(FrameOffset fr_offs,
413 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800414 ManagedRegister mscratch) OVERRIDE;
415
Andreas Gampe542451c2016-07-26 09:02:02 -0700416 void CopyRawPtrToThread64(ThreadOffset64 thr_offs,
417 FrameOffset fr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800418 ManagedRegister mscratch) OVERRIDE;
419
420 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
421
422 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
423
424 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
425 size_t size) OVERRIDE;
426
427 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
428 ManagedRegister mscratch, size_t size) OVERRIDE;
429
430 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
431 size_t size) OVERRIDE;
432
433 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
434 ManagedRegister mscratch, size_t size) OVERRIDE;
435
436 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
437 ManagedRegister mscratch, size_t size) OVERRIDE;
438
439 void MemoryBarrier(ManagedRegister) OVERRIDE;
440
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700441 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800442 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
443
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700444 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800445 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
446
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700447 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800448 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
449 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
450
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700451 // 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 -0800452 // value is null and null_allowed. in_reg holds a possibly stale reference
453 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700454 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800455 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
456 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
457
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700458 // 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 -0800459 // value is null and null_allowed.
460 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
461 mscratch, bool null_allowed) OVERRIDE;
462
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700463 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800464 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
465
466 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
467 // know that src may not be null.
468 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
469 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
470
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700471 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800472 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
473 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700474 void CallFromThread64(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800475
476 // Generate code to check if Thread::Current()->exception_ is non-null
477 // and branch to a ExceptionSlowPath if it is.
478 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
479
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700480 // Emit slow paths queued during assembly and promote short branches to long if needed.
481 void FinalizeCode() OVERRIDE;
482
483 // Emit branches and finalize all instructions.
484 void FinalizeInstructions(const MemoryRegion& region);
485
486 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
487 // must be used instead of Mips64Label::GetPosition()).
488 uint32_t GetLabelLocation(Mips64Label* label) const;
489
490 // Get the final position of a label after local fixup based on the old position
491 // recorded before FinalizeCode().
492 uint32_t GetAdjustedPosition(uint32_t old_position);
493
494 enum BranchCondition {
495 kCondLT,
496 kCondGE,
497 kCondLE,
498 kCondGT,
499 kCondLTZ,
500 kCondGEZ,
501 kCondLEZ,
502 kCondGTZ,
503 kCondEQ,
504 kCondNE,
505 kCondEQZ,
506 kCondNEZ,
507 kCondLTU,
508 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800509 kCondF, // Floating-point predicate false.
510 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700511 kUncond,
512 };
513 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
514
Andreas Gampe57b34292015-01-14 15:45:59 -0800515 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700516 class Branch {
517 public:
518 enum Type {
519 // Short branches.
520 kUncondBranch,
521 kCondBranch,
522 kCall,
523 // Long branches.
524 kLongUncondBranch,
525 kLongCondBranch,
526 kLongCall,
527 };
528
529 // Bit sizes of offsets defined as enums to minimize chance of typos.
530 enum OffsetBits {
531 kOffset16 = 16,
532 kOffset18 = 18,
533 kOffset21 = 21,
534 kOffset23 = 23,
535 kOffset28 = 28,
536 kOffset32 = 32,
537 };
538
539 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
540 static constexpr int32_t kMaxBranchLength = 32;
541 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
542
543 struct BranchInfo {
544 // Branch length as a number of 4-byte-long instructions.
545 uint32_t length;
546 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
547 // PC-relative offset (or its most significant 16-bit half, which goes first).
548 uint32_t instr_offset;
549 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
550 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
551 // instructions) from the instruction containing the offset.
552 uint32_t pc_org;
553 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
554 // an exception: use kOffset23 for beqzc/bnezc).
555 OffsetBits offset_size;
556 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
557 // count.
558 int offset_shift;
559 };
560 static const BranchInfo branch_info_[/* Type */];
561
562 // Unconditional branch.
563 Branch(uint32_t location, uint32_t target);
564 // Conditional branch.
565 Branch(uint32_t location,
566 uint32_t target,
567 BranchCondition condition,
568 GpuRegister lhs_reg,
569 GpuRegister rhs_reg = ZERO);
570 // Call (branch and link) that stores the target address in a given register (i.e. T9).
571 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
572
573 // Some conditional branches with lhs = rhs are effectively NOPs, while some
574 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
575 // So, we need a way to identify such branches in order to emit no instructions for them
576 // or change them to unconditional.
577 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
578 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
579
580 static BranchCondition OppositeCondition(BranchCondition cond);
581
582 Type GetType() const;
583 BranchCondition GetCondition() const;
584 GpuRegister GetLeftRegister() const;
585 GpuRegister GetRightRegister() const;
586 uint32_t GetTarget() const;
587 uint32_t GetLocation() const;
588 uint32_t GetOldLocation() const;
589 uint32_t GetLength() const;
590 uint32_t GetOldLength() const;
591 uint32_t GetSize() const;
592 uint32_t GetOldSize() const;
593 uint32_t GetEndLocation() const;
594 uint32_t GetOldEndLocation() const;
595 bool IsLong() const;
596 bool IsResolved() const;
597
598 // Returns the bit size of the signed offset that the branch instruction can handle.
599 OffsetBits GetOffsetSize() const;
600
601 // Calculates the distance between two byte locations in the assembler buffer and
602 // returns the number of bits needed to represent the distance as a signed integer.
603 //
604 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
605 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
606 //
607 // Composite branches (made of several instructions) with longer reach have 32-bit
608 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
609 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
610 // however. Consider the following implementation of a long unconditional branch, for
611 // example:
612 //
613 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
614 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
615 //
616 // Both of the above instructions take 16-bit signed offsets as immediate operands.
617 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
618 // due to sign extension. This must be compensated for by incrementing offset_31_16
619 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
620 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
621 // Therefore, the long branch range is something like from PC - 0x80000000 to
622 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
623 //
624 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
625 // case with the addiu instruction and a 16 bit offset.
626 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
627
628 // Resolve a branch when the target is known.
629 void Resolve(uint32_t target);
630
631 // Relocate a branch by a given delta if needed due to expansion of this or another
632 // branch at a given location by this delta (just changes location_ and target_).
633 void Relocate(uint32_t expand_location, uint32_t delta);
634
635 // If the branch is short, changes its type to long.
636 void PromoteToLong();
637
638 // If necessary, updates the type by promoting a short branch to a long branch
639 // based on the branch location and target. Returns the amount (in bytes) by
640 // which the branch size has increased.
641 // max_short_distance caps the maximum distance between location_ and target_
642 // that is allowed for short branches. This is for debugging/testing purposes.
643 // max_short_distance = 0 forces all short branches to become long.
644 // Use the implicit default argument when not debugging/testing.
645 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
646
647 // Returns the location of the instruction(s) containing the offset.
648 uint32_t GetOffsetLocation() const;
649
650 // Calculates and returns the offset ready for encoding in the branch instruction(s).
651 uint32_t GetOffset() const;
652
653 private:
654 // Completes branch construction by determining and recording its type.
655 void InitializeType(bool is_call);
656 // Helper for the above.
657 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
658
659 uint32_t old_location_; // Offset into assembler buffer in bytes.
660 uint32_t location_; // Offset into assembler buffer in bytes.
661 uint32_t target_; // Offset into assembler buffer in bytes.
662
663 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
664 // indirect call register.
665 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
666 BranchCondition condition_; // Condition for conditional branches.
667
668 Type type_; // Current type of the branch.
669 Type old_type_; // Initial type of the branch.
670 };
671 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
672 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
673
Andreas Gampe57b34292015-01-14 15:45:59 -0800674 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700675 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
676 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800677 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700679 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800680 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
681 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700682 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
683
684 void Buncond(Mips64Label* label);
685 void Bcond(Mips64Label* label,
686 BranchCondition condition,
687 GpuRegister lhs,
688 GpuRegister rhs = ZERO);
689 void Call(Mips64Label* label, GpuRegister indirect_reg);
690 void FinalizeLabeledBranch(Mips64Label* label);
691
692 Branch* GetBranch(uint32_t branch_id);
693 const Branch* GetBranch(uint32_t branch_id) const;
694
695 void PromoteBranches();
696 void EmitBranch(Branch* branch);
697 void EmitBranches();
698 void PatchCFI();
699
700 // Emits exception block.
701 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
702
703 // List of exception blocks to generate at the end of the code cache.
704 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
705
706 std::vector<Branch> branches_;
707
708 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
709 bool overwriting_;
710 // The current overwrite location.
711 uint32_t overwrite_location_;
712
713 // Data for AdjustedPosition(), see the description there.
714 uint32_t last_position_adjustment_;
715 uint32_t last_old_position_;
716 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800717
Andreas Gampe57b34292015-01-14 15:45:59 -0800718 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
719};
720
Andreas Gampe57b34292015-01-14 15:45:59 -0800721} // namespace mips64
722} // namespace art
723
724#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_