blob: a7d350c0103cf88a68879ffe6e4a0181724fab80 [file] [log] [blame]
Andreas Gampe57b34292015-01-14 15:45:59 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_
18#define ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_
19
Alexey Frunzea0e87b02015-09-24 22:57:20 -070020#include <utility>
Andreas Gampe57b34292015-01-14 15:45:59 -080021#include <vector>
22
Andreas Gampe3b165bc2016-08-01 22:07:04 -070023#include "base/enums.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080024#include "base/macros.h"
25#include "constants_mips64.h"
26#include "globals.h"
27#include "managed_register_mips64.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080028#include "offsets.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070029#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070030#include "utils/jni_macro_assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/label.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080032
33namespace art {
34namespace mips64 {
35
Lazar Trsicd9672662015-09-03 17:33:01 +020036static constexpr size_t kMips64WordSize = 4;
37static constexpr size_t kMips64DoublewordSize = 8;
Alexey Frunzea0e87b02015-09-24 22:57:20 -070038
Andreas Gampe57b34292015-01-14 15:45:59 -080039enum LoadOperandType {
40 kLoadSignedByte,
41 kLoadUnsignedByte,
42 kLoadSignedHalfword,
43 kLoadUnsignedHalfword,
44 kLoadWord,
Douglas Leungd90957f2015-04-30 19:22:49 -070045 kLoadUnsignedWord,
Andreas Gampe57b34292015-01-14 15:45:59 -080046 kLoadDoubleword
47};
48
49enum StoreOperandType {
50 kStoreByte,
51 kStoreHalfword,
52 kStoreWord,
53 kStoreDoubleword
54};
55
Chris Larsen14500822015-10-01 11:35:18 -070056// Used to test the values returned by ClassS/ClassD.
57enum FPClassMaskType {
58 kSignalingNaN = 0x001,
59 kQuietNaN = 0x002,
60 kNegativeInfinity = 0x004,
61 kNegativeNormal = 0x008,
62 kNegativeSubnormal = 0x010,
63 kNegativeZero = 0x020,
64 kPositiveInfinity = 0x040,
65 kPositiveNormal = 0x080,
66 kPositiveSubnormal = 0x100,
67 kPositiveZero = 0x200,
68};
69
Alexey Frunzea0e87b02015-09-24 22:57:20 -070070class Mips64Label : public Label {
71 public:
72 Mips64Label() : prev_branch_id_plus_one_(0) {}
73
74 Mips64Label(Mips64Label&& src)
75 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
76
77 private:
78 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
79
80 friend class Mips64Assembler;
81 DISALLOW_COPY_AND_ASSIGN(Mips64Label);
82};
83
84// Slowpath entered when Thread::Current()->_exception is non-null.
85class Mips64ExceptionSlowPath {
86 public:
87 explicit Mips64ExceptionSlowPath(Mips64ManagedRegister scratch, size_t stack_adjust)
88 : scratch_(scratch), stack_adjust_(stack_adjust) {}
89
90 Mips64ExceptionSlowPath(Mips64ExceptionSlowPath&& src)
91 : scratch_(src.scratch_),
92 stack_adjust_(src.stack_adjust_),
93 exception_entry_(std::move(src.exception_entry_)) {}
94
95 private:
96 Mips64Label* Entry() { return &exception_entry_; }
97 const Mips64ManagedRegister scratch_;
98 const size_t stack_adjust_;
99 Mips64Label exception_entry_;
100
101 friend class Mips64Assembler;
102 DISALLOW_COPY_AND_ASSIGN(Mips64ExceptionSlowPath);
103};
104
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700105class Mips64Assembler FINAL : public Assembler, public JNIMacroAssembler<PointerSize::k64> {
Andreas Gampe57b34292015-01-14 15:45:59 -0800106 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100107 explicit Mips64Assembler(ArenaAllocator* arena)
108 : Assembler(arena),
109 overwriting_(false),
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700110 overwrite_location_(0),
111 last_position_adjustment_(0),
112 last_old_position_(0),
113 last_branch_id_(0) {
114 cfi().DelayEmittingAdvancePCs();
115 }
116
117 virtual ~Mips64Assembler() {
118 for (auto& branch : branches_) {
119 CHECK(branch.IsResolved());
120 }
121 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800122
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700123 size_t CodeSize() const OVERRIDE { return Assembler::CodeSize(); }
124 DebugFrameOpCodeWriterForAssembler& cfi() { return Assembler::cfi(); }
125
Andreas Gampe57b34292015-01-14 15:45:59 -0800126 // Emit Machine Instructions.
Andreas Gampe57b34292015-01-14 15:45:59 -0800127 void Addu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
128 void Addiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700129 void Daddu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
130 void Daddiu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800131 void Subu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 void Dsubu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
133
Alexey Frunzec857c742015-09-23 15:12:39 -0700134 void MulR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
135 void MuhR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
136 void DivR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
137 void ModR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
138 void DivuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
139 void ModuR6(GpuRegister rd, GpuRegister rs, GpuRegister rt);
140 void Dmul(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
141 void Dmuh(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
142 void Ddiv(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
143 void Dmod(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
144 void Ddivu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
145 void Dmodu(GpuRegister rd, GpuRegister rs, GpuRegister rt); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800146
147 void And(GpuRegister rd, GpuRegister rs, GpuRegister rt);
148 void Andi(GpuRegister rt, GpuRegister rs, uint16_t imm16);
149 void Or(GpuRegister rd, GpuRegister rs, GpuRegister rt);
150 void Ori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
151 void Xor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
152 void Xori(GpuRegister rt, GpuRegister rs, uint16_t imm16);
153 void Nor(GpuRegister rd, GpuRegister rs, GpuRegister rt);
154
Alexey Frunzec857c742015-09-23 15:12:39 -0700155 void Bitswap(GpuRegister rd, GpuRegister rt);
156 void Dbitswap(GpuRegister rd, GpuRegister rt);
157 void Seb(GpuRegister rd, GpuRegister rt);
158 void Seh(GpuRegister rd, GpuRegister rt);
159 void Dsbh(GpuRegister rd, GpuRegister rt);
160 void Dshd(GpuRegister rd, GpuRegister rt);
Lazar Trsicd9672662015-09-03 17:33:01 +0200161 void Dext(GpuRegister rs, GpuRegister rt, int pos, int size); // MIPS64
162 void Dinsu(GpuRegister rt, GpuRegister rs, int pos, int size); // MIPS64
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700163 void Wsbh(GpuRegister rd, GpuRegister rt);
164 void Sc(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
165 void Scd(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
166 void Ll(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
167 void Lld(GpuRegister rt, GpuRegister base, int16_t imm9 = 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168
169 void Sll(GpuRegister rd, GpuRegister rt, int shamt);
170 void Srl(GpuRegister rd, GpuRegister rt, int shamt);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700171 void Rotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 void Sra(GpuRegister rd, GpuRegister rt, int shamt);
173 void Sllv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
174 void Srlv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Chris Larsen9aebff22015-09-22 17:54:15 -0700175 void Rotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700176 void Srav(GpuRegister rd, GpuRegister rt, GpuRegister rs);
177 void Dsll(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
178 void Dsrl(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700179 void Drotr(GpuRegister rd, GpuRegister rt, int shamt);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 void Dsra(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
181 void Dsll32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
182 void Dsrl32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700183 void Drotr32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700184 void Dsra32(GpuRegister rd, GpuRegister rt, int shamt); // MIPS64
185 void Dsllv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
186 void Dsrlv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Chris Larsen9aebff22015-09-22 17:54:15 -0700187 void Drotrv(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700188 void Dsrav(GpuRegister rd, GpuRegister rt, GpuRegister rs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800189
190 void Lb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
191 void Lh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
192 void Lw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700193 void Ld(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800194 void Lbu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
195 void Lhu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700196 void Lwu(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800197 void Lui(GpuRegister rt, uint16_t imm16);
Alexey Frunzec857c742015-09-23 15:12:39 -0700198 void Dahi(GpuRegister rs, uint16_t imm16); // MIPS64
199 void Dati(GpuRegister rs, uint16_t imm16); // MIPS64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 void Sync(uint32_t stype);
Andreas Gampe57b34292015-01-14 15:45:59 -0800201
202 void Sb(GpuRegister rt, GpuRegister rs, uint16_t imm16);
203 void Sh(GpuRegister rt, GpuRegister rs, uint16_t imm16);
204 void Sw(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700205 void Sd(GpuRegister rt, GpuRegister rs, uint16_t imm16); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800206
207 void Slt(GpuRegister rd, GpuRegister rs, GpuRegister rt);
208 void Sltu(GpuRegister rd, GpuRegister rs, GpuRegister rt);
209 void Slti(GpuRegister rt, GpuRegister rs, uint16_t imm16);
210 void Sltiu(GpuRegister rt, GpuRegister rs, uint16_t imm16);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700211 void Seleqz(GpuRegister rd, GpuRegister rs, GpuRegister rt);
212 void Selnez(GpuRegister rd, GpuRegister rs, GpuRegister rt);
213 void Clz(GpuRegister rd, GpuRegister rs);
214 void Clo(GpuRegister rd, GpuRegister rs);
215 void Dclz(GpuRegister rd, GpuRegister rs);
216 void Dclo(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 void Jalr(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800219 void Jalr(GpuRegister rs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 void Jr(GpuRegister rs);
Alexey Frunzec857c742015-09-23 15:12:39 -0700221 void Auipc(GpuRegister rs, uint16_t imm16);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700222 void Addiupc(GpuRegister rs, uint32_t imm19);
223 void Bc(uint32_t imm26);
Alexey Frunzec857c742015-09-23 15:12:39 -0700224 void Jic(GpuRegister rt, uint16_t imm16);
225 void Jialc(GpuRegister rt, uint16_t imm16);
226 void Bltc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
227 void Bltzc(GpuRegister rt, uint16_t imm16);
228 void Bgtzc(GpuRegister rt, uint16_t imm16);
229 void Bgec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
230 void Bgezc(GpuRegister rt, uint16_t imm16);
231 void Blezc(GpuRegister rt, uint16_t imm16);
232 void Bltuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
233 void Bgeuc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
234 void Beqc(GpuRegister rs, GpuRegister rt, uint16_t imm16);
235 void Bnec(GpuRegister rs, GpuRegister rt, uint16_t imm16);
236 void Beqzc(GpuRegister rs, uint32_t imm21);
237 void Bnezc(GpuRegister rs, uint32_t imm21);
Alexey Frunze299a9392015-12-08 16:08:02 -0800238 void Bc1eqz(FpuRegister ft, uint16_t imm16);
239 void Bc1nez(FpuRegister ft, uint16_t imm16);
Andreas Gampe57b34292015-01-14 15:45:59 -0800240
241 void AddS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
242 void SubS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
243 void MulS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
244 void DivS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
245 void AddD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
246 void SubD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
247 void MulD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
248 void DivD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700249 void SqrtS(FpuRegister fd, FpuRegister fs);
250 void SqrtD(FpuRegister fd, FpuRegister fs);
251 void AbsS(FpuRegister fd, FpuRegister fs);
252 void AbsD(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800253 void MovS(FpuRegister fd, FpuRegister fs);
254 void MovD(FpuRegister fd, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 void NegS(FpuRegister fd, FpuRegister fs);
256 void NegD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700257 void RoundLS(FpuRegister fd, FpuRegister fs);
258 void RoundLD(FpuRegister fd, FpuRegister fs);
259 void RoundWS(FpuRegister fd, FpuRegister fs);
260 void RoundWD(FpuRegister fd, FpuRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800261 void TruncLS(FpuRegister fd, FpuRegister fs);
262 void TruncLD(FpuRegister fd, FpuRegister fs);
263 void TruncWS(FpuRegister fd, FpuRegister fs);
264 void TruncWD(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700265 void CeilLS(FpuRegister fd, FpuRegister fs);
266 void CeilLD(FpuRegister fd, FpuRegister fs);
267 void CeilWS(FpuRegister fd, FpuRegister fs);
268 void CeilWD(FpuRegister fd, FpuRegister fs);
269 void FloorLS(FpuRegister fd, FpuRegister fs);
270 void FloorLD(FpuRegister fd, FpuRegister fs);
271 void FloorWS(FpuRegister fd, FpuRegister fs);
272 void FloorWD(FpuRegister fd, FpuRegister fs);
273 void SelS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
274 void SelD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
275 void RintS(FpuRegister fd, FpuRegister fs);
276 void RintD(FpuRegister fd, FpuRegister fs);
277 void ClassS(FpuRegister fd, FpuRegister fs);
278 void ClassD(FpuRegister fd, FpuRegister fs);
279 void MinS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
280 void MinD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
281 void MaxS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
282 void MaxD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze299a9392015-12-08 16:08:02 -0800283 void CmpUnS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
284 void CmpEqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
285 void CmpUeqS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
286 void CmpLtS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
287 void CmpUltS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
288 void CmpLeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
289 void CmpUleS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
290 void CmpOrS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
291 void CmpUneS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
292 void CmpNeS(FpuRegister fd, FpuRegister fs, FpuRegister ft);
293 void CmpUnD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
294 void CmpEqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
295 void CmpUeqD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
296 void CmpLtD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
297 void CmpUltD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
298 void CmpLeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
299 void CmpUleD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
300 void CmpOrD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
301 void CmpUneD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
302 void CmpNeD(FpuRegister fd, FpuRegister fs, FpuRegister ft);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303
304 void Cvtsw(FpuRegister fd, FpuRegister fs);
305 void Cvtdw(FpuRegister fd, FpuRegister fs);
306 void Cvtsd(FpuRegister fd, FpuRegister fs);
307 void Cvtds(FpuRegister fd, FpuRegister fs);
Chris Larsen51417632015-10-02 13:24:25 -0700308 void Cvtsl(FpuRegister fd, FpuRegister fs);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700309 void Cvtdl(FpuRegister fd, FpuRegister fs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800310
311 void Mfc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200312 void Mfhc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 void Mtc1(GpuRegister rt, FpuRegister fs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200314 void Mthc1(GpuRegister rt, FpuRegister fs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 void Dmfc1(GpuRegister rt, FpuRegister fs); // MIPS64
316 void Dmtc1(GpuRegister rt, FpuRegister fs); // MIPS64
Andreas Gampe57b34292015-01-14 15:45:59 -0800317 void Lwc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
318 void Ldc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
319 void Swc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
320 void Sdc1(FpuRegister ft, GpuRegister rs, uint16_t imm16);
321
322 void Break();
323 void Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700324 void Move(GpuRegister rd, GpuRegister rs);
325 void Clear(GpuRegister rd);
326 void Not(GpuRegister rd, GpuRegister rs);
Andreas Gampe57b34292015-01-14 15:45:59 -0800327
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700328 // Higher level composite instructions.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 void LoadConst32(GpuRegister rd, int32_t value);
330 void LoadConst64(GpuRegister rd, int64_t value); // MIPS64
331
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332 void Daddiu64(GpuRegister rt, GpuRegister rs, int64_t value, GpuRegister rtmp = AT); // MIPS64
333
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 void Bind(Label* label) OVERRIDE {
335 Bind(down_cast<Mips64Label*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700336 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700337 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
338 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS64";
339 }
340
341 void Bind(Mips64Label* label);
342 void Bc(Mips64Label* label);
343 void Jialc(Mips64Label* label, GpuRegister indirect_reg);
344 void Bltc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
345 void Bltzc(GpuRegister rt, Mips64Label* label);
346 void Bgtzc(GpuRegister rt, Mips64Label* label);
347 void Bgec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
348 void Bgezc(GpuRegister rt, Mips64Label* label);
349 void Blezc(GpuRegister rt, Mips64Label* label);
350 void Bltuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
351 void Bgeuc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
352 void Beqc(GpuRegister rs, GpuRegister rt, Mips64Label* label);
353 void Bnec(GpuRegister rs, GpuRegister rt, Mips64Label* label);
354 void Beqzc(GpuRegister rs, Mips64Label* label);
355 void Bnezc(GpuRegister rs, Mips64Label* label);
Alexey Frunze299a9392015-12-08 16:08:02 -0800356 void Bc1eqz(FpuRegister ft, Mips64Label* label);
357 void Bc1nez(FpuRegister ft, Mips64Label* label);
Andreas Gampe57b34292015-01-14 15:45:59 -0800358
359 void EmitLoad(ManagedRegister m_dst, GpuRegister src_register, int32_t src_offset, size_t size);
360 void LoadFromOffset(LoadOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
361 void LoadFpuFromOffset(LoadOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
362 void StoreToOffset(StoreOperandType type, GpuRegister reg, GpuRegister base, int32_t offset);
363 void StoreFpuToOffset(StoreOperandType type, FpuRegister reg, GpuRegister base, int32_t offset);
364
365 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 void Emit(uint32_t value);
Andreas Gampe57b34292015-01-14 15:45:59 -0800367
368 //
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700369 // Overridden common assembler high-level functionality.
Andreas Gampe57b34292015-01-14 15:45:59 -0800370 //
371
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700372 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100373 void BuildFrame(size_t frame_size,
374 ManagedRegister method_reg,
375 ArrayRef<const ManagedRegister> callee_save_regs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800376 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
377
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700378 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100379 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800380
381 void IncreaseFrameSize(size_t adjust) OVERRIDE;
382 void DecreaseFrameSize(size_t adjust) OVERRIDE;
383
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700384 // Store routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800385 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
386 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
387 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
388
389 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
390
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700391 void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
392 FrameOffset fr_offs,
393 ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800394
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700395 void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800396
397 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
398 ManagedRegister mscratch) OVERRIDE;
399
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700400 // Load routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800401 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
402
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700403 void LoadFromThread(ManagedRegister mdest, ThreadOffset64 src, size_t size) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800404
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100408 bool unpoison_reference) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800409
410 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
411
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700412 void LoadRawPtrFromThread(ManagedRegister mdest, ThreadOffset64 offs) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800413
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700414 // Copying routines.
Andreas Gampe57b34292015-01-14 15:45:59 -0800415 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
416
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700417 void CopyRawPtrFromThread(FrameOffset fr_offs,
418 ThreadOffset64 thr_offs,
Andreas Gampe57b34292015-01-14 15:45:59 -0800419 ManagedRegister mscratch) OVERRIDE;
420
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700421 void CopyRawPtrToThread(ThreadOffset64 thr_offs,
422 FrameOffset fr_offs,
423 ManagedRegister mscratch) OVERRIDE;
424
Andreas Gampe57b34292015-01-14 15:45:59 -0800425 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
426
427 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
428
429 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
430 size_t size) OVERRIDE;
431
432 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
433 ManagedRegister mscratch, size_t size) OVERRIDE;
434
435 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
436 size_t size) OVERRIDE;
437
438 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
439 ManagedRegister mscratch, size_t size) OVERRIDE;
440
441 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
442 ManagedRegister mscratch, size_t size) OVERRIDE;
443
444 void MemoryBarrier(ManagedRegister) OVERRIDE;
445
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700446 // Sign extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800447 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
448
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700449 // Zero extension.
Andreas Gampe57b34292015-01-14 15:45:59 -0800450 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
451
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700452 // Exploit fast access in managed code to Thread::Current().
Andreas Gampe57b34292015-01-14 15:45:59 -0800453 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
454 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
455
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700456 // 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 -0800457 // value is null and null_allowed. in_reg holds a possibly stale reference
458 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700459 // null.
Andreas Gampe57b34292015-01-14 15:45:59 -0800460 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
461 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
462
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700463 // 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 -0800464 // value is null and null_allowed.
465 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset, ManagedRegister
466 mscratch, bool null_allowed) OVERRIDE;
467
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700468 // src holds a handle scope entry (Object**) load this into dst.
Andreas Gampe57b34292015-01-14 15:45:59 -0800469 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
470
471 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
472 // know that src may not be null.
473 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
474 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
475
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700476 // Call to address held at [base+offset].
Andreas Gampe57b34292015-01-14 15:45:59 -0800477 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
478 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700479 void CallFromThread(ThreadOffset64 offset, ManagedRegister mscratch) OVERRIDE;
Andreas Gampe57b34292015-01-14 15:45:59 -0800480
481 // Generate code to check if Thread::Current()->exception_ is non-null
482 // and branch to a ExceptionSlowPath if it is.
483 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
484
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700485 // Emit slow paths queued during assembly and promote short branches to long if needed.
486 void FinalizeCode() OVERRIDE;
487
488 // Emit branches and finalize all instructions.
489 void FinalizeInstructions(const MemoryRegion& region);
490
491 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS64,
492 // must be used instead of Mips64Label::GetPosition()).
493 uint32_t GetLabelLocation(Mips64Label* label) const;
494
495 // Get the final position of a label after local fixup based on the old position
496 // recorded before FinalizeCode().
497 uint32_t GetAdjustedPosition(uint32_t old_position);
498
499 enum BranchCondition {
500 kCondLT,
501 kCondGE,
502 kCondLE,
503 kCondGT,
504 kCondLTZ,
505 kCondGEZ,
506 kCondLEZ,
507 kCondGTZ,
508 kCondEQ,
509 kCondNE,
510 kCondEQZ,
511 kCondNEZ,
512 kCondLTU,
513 kCondGEU,
Alexey Frunze299a9392015-12-08 16:08:02 -0800514 kCondF, // Floating-point predicate false.
515 kCondT, // Floating-point predicate true.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700516 kUncond,
517 };
518 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
519
Andreas Gampe57b34292015-01-14 15:45:59 -0800520 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700521 class Branch {
522 public:
523 enum Type {
524 // Short branches.
525 kUncondBranch,
526 kCondBranch,
527 kCall,
528 // Long branches.
529 kLongUncondBranch,
530 kLongCondBranch,
531 kLongCall,
532 };
533
534 // Bit sizes of offsets defined as enums to minimize chance of typos.
535 enum OffsetBits {
536 kOffset16 = 16,
537 kOffset18 = 18,
538 kOffset21 = 21,
539 kOffset23 = 23,
540 kOffset28 = 28,
541 kOffset32 = 32,
542 };
543
544 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
545 static constexpr int32_t kMaxBranchLength = 32;
546 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
547
548 struct BranchInfo {
549 // Branch length as a number of 4-byte-long instructions.
550 uint32_t length;
551 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
552 // PC-relative offset (or its most significant 16-bit half, which goes first).
553 uint32_t instr_offset;
554 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
555 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
556 // instructions) from the instruction containing the offset.
557 uint32_t pc_org;
558 // How large (in bits) a PC-relative offset can be for a given type of branch (kCondBranch is
559 // an exception: use kOffset23 for beqzc/bnezc).
560 OffsetBits offset_size;
561 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
562 // count.
563 int offset_shift;
564 };
565 static const BranchInfo branch_info_[/* Type */];
566
567 // Unconditional branch.
568 Branch(uint32_t location, uint32_t target);
569 // Conditional branch.
570 Branch(uint32_t location,
571 uint32_t target,
572 BranchCondition condition,
573 GpuRegister lhs_reg,
574 GpuRegister rhs_reg = ZERO);
575 // Call (branch and link) that stores the target address in a given register (i.e. T9).
576 Branch(uint32_t location, uint32_t target, GpuRegister indirect_reg);
577
578 // Some conditional branches with lhs = rhs are effectively NOPs, while some
579 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
580 // So, we need a way to identify such branches in order to emit no instructions for them
581 // or change them to unconditional.
582 static bool IsNop(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
583 static bool IsUncond(BranchCondition condition, GpuRegister lhs, GpuRegister rhs);
584
585 static BranchCondition OppositeCondition(BranchCondition cond);
586
587 Type GetType() const;
588 BranchCondition GetCondition() const;
589 GpuRegister GetLeftRegister() const;
590 GpuRegister GetRightRegister() const;
591 uint32_t GetTarget() const;
592 uint32_t GetLocation() const;
593 uint32_t GetOldLocation() const;
594 uint32_t GetLength() const;
595 uint32_t GetOldLength() const;
596 uint32_t GetSize() const;
597 uint32_t GetOldSize() const;
598 uint32_t GetEndLocation() const;
599 uint32_t GetOldEndLocation() const;
600 bool IsLong() const;
601 bool IsResolved() const;
602
603 // Returns the bit size of the signed offset that the branch instruction can handle.
604 OffsetBits GetOffsetSize() const;
605
606 // Calculates the distance between two byte locations in the assembler buffer and
607 // returns the number of bits needed to represent the distance as a signed integer.
608 //
609 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
610 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
611 //
612 // Composite branches (made of several instructions) with longer reach have 32-bit
613 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
614 // The composite branches cover the range of PC + ~+/-2GB. The range is not end-to-end,
615 // however. Consider the following implementation of a long unconditional branch, for
616 // example:
617 //
618 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
619 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
620 //
621 // Both of the above instructions take 16-bit signed offsets as immediate operands.
622 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
623 // due to sign extension. This must be compensated for by incrementing offset_31_16
624 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
625 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
626 // Therefore, the long branch range is something like from PC - 0x80000000 to
627 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
628 //
629 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
630 // case with the addiu instruction and a 16 bit offset.
631 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
632
633 // Resolve a branch when the target is known.
634 void Resolve(uint32_t target);
635
636 // Relocate a branch by a given delta if needed due to expansion of this or another
637 // branch at a given location by this delta (just changes location_ and target_).
638 void Relocate(uint32_t expand_location, uint32_t delta);
639
640 // If the branch is short, changes its type to long.
641 void PromoteToLong();
642
643 // If necessary, updates the type by promoting a short branch to a long branch
644 // based on the branch location and target. Returns the amount (in bytes) by
645 // which the branch size has increased.
646 // max_short_distance caps the maximum distance between location_ and target_
647 // that is allowed for short branches. This is for debugging/testing purposes.
648 // max_short_distance = 0 forces all short branches to become long.
649 // Use the implicit default argument when not debugging/testing.
650 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
651
652 // Returns the location of the instruction(s) containing the offset.
653 uint32_t GetOffsetLocation() const;
654
655 // Calculates and returns the offset ready for encoding in the branch instruction(s).
656 uint32_t GetOffset() const;
657
658 private:
659 // Completes branch construction by determining and recording its type.
660 void InitializeType(bool is_call);
661 // Helper for the above.
662 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
663
664 uint32_t old_location_; // Offset into assembler buffer in bytes.
665 uint32_t location_; // Offset into assembler buffer in bytes.
666 uint32_t target_; // Offset into assembler buffer in bytes.
667
668 GpuRegister lhs_reg_; // Left-hand side register in conditional branches or
669 // indirect call register.
670 GpuRegister rhs_reg_; // Right-hand side register in conditional branches.
671 BranchCondition condition_; // Condition for conditional branches.
672
673 Type type_; // Current type of the branch.
674 Type old_type_; // Initial type of the branch.
675 };
676 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
677 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
678
Andreas Gampe57b34292015-01-14 15:45:59 -0800679 void EmitR(int opcode, GpuRegister rs, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Chris Larsen2fadd7b2015-08-14 14:56:10 -0700680 void EmitRsd(int opcode, GpuRegister rs, GpuRegister rd, int shamt, int funct);
681 void EmitRtd(int opcode, GpuRegister rt, GpuRegister rd, int shamt, int funct);
Andreas Gampe57b34292015-01-14 15:45:59 -0800682 void EmitI(int opcode, GpuRegister rs, GpuRegister rt, uint16_t imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700683 void EmitI21(int opcode, GpuRegister rs, uint32_t imm21);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700684 void EmitI26(int opcode, uint32_t imm26);
Andreas Gampe57b34292015-01-14 15:45:59 -0800685 void EmitFR(int opcode, int fmt, FpuRegister ft, FpuRegister fs, FpuRegister fd, int funct);
686 void EmitFI(int opcode, int fmt, FpuRegister rt, uint16_t imm);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700687 void EmitBcondc(BranchCondition cond, GpuRegister rs, GpuRegister rt, uint32_t imm16_21);
688
689 void Buncond(Mips64Label* label);
690 void Bcond(Mips64Label* label,
691 BranchCondition condition,
692 GpuRegister lhs,
693 GpuRegister rhs = ZERO);
694 void Call(Mips64Label* label, GpuRegister indirect_reg);
695 void FinalizeLabeledBranch(Mips64Label* label);
696
697 Branch* GetBranch(uint32_t branch_id);
698 const Branch* GetBranch(uint32_t branch_id) const;
699
700 void PromoteBranches();
701 void EmitBranch(Branch* branch);
702 void EmitBranches();
703 void PatchCFI();
704
705 // Emits exception block.
706 void EmitExceptionPoll(Mips64ExceptionSlowPath* exception);
707
708 // List of exception blocks to generate at the end of the code cache.
709 std::vector<Mips64ExceptionSlowPath> exception_blocks_;
710
711 std::vector<Branch> branches_;
712
713 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
714 bool overwriting_;
715 // The current overwrite location.
716 uint32_t overwrite_location_;
717
718 // Data for AdjustedPosition(), see the description there.
719 uint32_t last_position_adjustment_;
720 uint32_t last_old_position_;
721 uint32_t last_branch_id_;
Andreas Gampe57b34292015-01-14 15:45:59 -0800722
Andreas Gampe57b34292015-01-14 15:45:59 -0800723 DISALLOW_COPY_AND_ASSIGN(Mips64Assembler);
724};
725
Andreas Gampe57b34292015-01-14 15:45:59 -0800726} // namespace mips64
727} // namespace art
728
729#endif // ART_COMPILER_UTILS_MIPS64_ASSEMBLER_MIPS64_H_