blob: ffac4c41680b6164ea178be9254679ba3ca6726f [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
2 * Copyright (C) 2011 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
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
18#define ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_
jeffhao7fbee072012-08-24 17:56:54 -070019
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020020#include <utility>
jeffhao7fbee072012-08-24 17:56:54 -070021#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080022
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020023#include "arch/mips/instruction_set_features_mips.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
jeffhao7fbee072012-08-24 17:56:54 -070025#include "constants_mips.h"
26#include "globals.h"
27#include "managed_register_mips.h"
jeffhao7fbee072012-08-24 17:56:54 -070028#include "offsets.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020029#include "utils/assembler.h"
30#include "utils/label.h"
jeffhao7fbee072012-08-24 17:56:54 -070031
32namespace art {
33namespace mips {
jeffhao7fbee072012-08-24 17:56:54 -070034
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020035static constexpr size_t kMipsWordSize = 4;
36static constexpr size_t kMipsDoublewordSize = 8;
37
jeffhao7fbee072012-08-24 17:56:54 -070038enum LoadOperandType {
39 kLoadSignedByte,
40 kLoadUnsignedByte,
41 kLoadSignedHalfword,
42 kLoadUnsignedHalfword,
43 kLoadWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020044 kLoadDoubleword
jeffhao7fbee072012-08-24 17:56:54 -070045};
46
47enum StoreOperandType {
48 kStoreByte,
49 kStoreHalfword,
50 kStoreWord,
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020051 kStoreDoubleword
52};
53
Chris Larsenb74353a2015-11-20 09:07:09 -080054// 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
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020068class MipsLabel : public Label {
69 public:
70 MipsLabel() : prev_branch_id_plus_one_(0) {}
71
72 MipsLabel(MipsLabel&& 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 MipsAssembler;
79 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
80};
81
82// Slowpath entered when Thread::Current()->_exception is non-null.
83class MipsExceptionSlowPath {
84 public:
85 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
86 : scratch_(scratch), stack_adjust_(stack_adjust) {}
87
88 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -080089 : scratch_(src.scratch_),
90 stack_adjust_(src.stack_adjust_),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020091 exception_entry_(std::move(src.exception_entry_)) {}
92
93 private:
94 MipsLabel* Entry() { return &exception_entry_; }
95 const MipsManagedRegister scratch_;
96 const size_t stack_adjust_;
97 MipsLabel exception_entry_;
98
99 friend class MipsAssembler;
100 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -0700101};
102
Ian Rogersdd7624d2014-03-14 17:43:00 -0700103class MipsAssembler FINAL : public Assembler {
jeffhao7fbee072012-08-24 17:56:54 -0700104 public:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200105 explicit MipsAssembler(const MipsInstructionSetFeatures* instruction_set_features = nullptr)
106 : overwriting_(false),
107 overwrite_location_(0),
108 last_position_adjustment_(0),
109 last_old_position_(0),
110 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100111 isa_features_(instruction_set_features) {
112 cfi().DelayEmittingAdvancePCs();
113 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200114
115 virtual ~MipsAssembler() {
116 for (auto& branch : branches_) {
117 CHECK(branch.IsResolved());
118 }
119 }
jeffhao7fbee072012-08-24 17:56:54 -0700120
121 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700122 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700123 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700124 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200125
126 void MultR2(Register rs, Register rt); // R2
127 void MultuR2(Register rs, Register rt); // R2
128 void DivR2(Register rs, Register rt); // R2
129 void DivuR2(Register rs, Register rt); // R2
130 void MulR2(Register rd, Register rs, Register rt); // R2
131 void DivR2(Register rd, Register rs, Register rt); // R2
132 void ModR2(Register rd, Register rs, Register rt); // R2
133 void DivuR2(Register rd, Register rs, Register rt); // R2
134 void ModuR2(Register rd, Register rs, Register rt); // R2
135 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800136 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200137 void MuhuR6(Register rd, Register rs, Register rt); // R6
138 void DivR6(Register rd, Register rs, Register rt); // R6
139 void ModR6(Register rd, Register rs, Register rt); // R6
140 void DivuR6(Register rd, Register rs, Register rt); // R6
141 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700142
143 void And(Register rd, Register rs, Register rt);
144 void Andi(Register rt, Register rs, uint16_t imm16);
145 void Or(Register rd, Register rs, Register rt);
146 void Ori(Register rt, Register rs, uint16_t imm16);
147 void Xor(Register rd, Register rs, Register rt);
148 void Xori(Register rt, Register rs, uint16_t imm16);
149 void Nor(Register rd, Register rs, Register rt);
150
Chris Larsene3845472015-11-18 12:27:15 -0800151 void Movz(Register rd, Register rs, Register rt); // R2
152 void Movn(Register rd, Register rs, Register rt); // R2
153 void Seleqz(Register rd, Register rs, Register rt); // R6
154 void Selnez(Register rd, Register rs, Register rt); // R6
155 void ClzR6(Register rd, Register rs);
156 void ClzR2(Register rd, Register rs);
157 void CloR6(Register rd, Register rs);
158 void CloR2(Register rd, Register rs);
159
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200160 void Seb(Register rd, Register rt); // R2+
161 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700162 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800163 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200164
165 void Sll(Register rd, Register rt, int shamt);
166 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700167 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200168 void Sra(Register rd, Register rt, int shamt);
169 void Sllv(Register rd, Register rt, Register rs);
170 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800171 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200172 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800173 void Ext(Register rd, Register rt, int pos, int size); // R2+
174 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700175
176 void Lb(Register rt, Register rs, uint16_t imm16);
177 void Lh(Register rt, Register rs, uint16_t imm16);
178 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800179 void Lwl(Register rt, Register rs, uint16_t imm16);
180 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700181 void Lbu(Register rt, Register rs, uint16_t imm16);
182 void Lhu(Register rt, Register rs, uint16_t imm16);
183 void Lui(Register rt, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200184 void Sync(uint32_t stype);
185 void Mfhi(Register rd); // R2
186 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700187
188 void Sb(Register rt, Register rs, uint16_t imm16);
189 void Sh(Register rt, Register rs, uint16_t imm16);
190 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800191 void Swl(Register rt, Register rs, uint16_t imm16);
192 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700193
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700194 void LlR2(Register rt, Register base, int16_t imm16 = 0);
195 void ScR2(Register rt, Register base, int16_t imm16 = 0);
196 void LlR6(Register rt, Register base, int16_t imm9 = 0);
197 void ScR6(Register rt, Register base, int16_t imm9 = 0);
198
jeffhao7fbee072012-08-24 17:56:54 -0700199 void Slt(Register rd, Register rs, Register rt);
200 void Sltu(Register rd, Register rs, Register rt);
201 void Slti(Register rt, Register rs, uint16_t imm16);
202 void Sltiu(Register rt, Register rs, uint16_t imm16);
203
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200204 void B(uint16_t imm16);
205 void Beq(Register rs, Register rt, uint16_t imm16);
206 void Bne(Register rs, Register rt, uint16_t imm16);
207 void Beqz(Register rt, uint16_t imm16);
208 void Bnez(Register rt, uint16_t imm16);
209 void Bltz(Register rt, uint16_t imm16);
210 void Bgez(Register rt, uint16_t imm16);
211 void Blez(Register rt, uint16_t imm16);
212 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800213 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800214 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800215 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800216 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200217 void J(uint32_t addr26);
218 void Jal(uint32_t addr26);
219 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700220 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200221 void Jr(Register rs);
222 void Nal();
223 void Auipc(Register rs, uint16_t imm16); // R6
224 void Addiupc(Register rs, uint32_t imm19); // R6
225 void Bc(uint32_t imm26); // R6
226 void Jic(Register rt, uint16_t imm16); // R6
227 void Jialc(Register rt, uint16_t imm16); // R6
228 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
229 void Bltzc(Register rt, uint16_t imm16); // R6
230 void Bgtzc(Register rt, uint16_t imm16); // R6
231 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
232 void Bgezc(Register rt, uint16_t imm16); // R6
233 void Blezc(Register rt, uint16_t imm16); // R6
234 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
235 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
236 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
237 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
238 void Beqzc(Register rs, uint32_t imm21); // R6
239 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800240 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
241 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700242
243 void AddS(FRegister fd, FRegister fs, FRegister ft);
244 void SubS(FRegister fd, FRegister fs, FRegister ft);
245 void MulS(FRegister fd, FRegister fs, FRegister ft);
246 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200247 void AddD(FRegister fd, FRegister fs, FRegister ft);
248 void SubD(FRegister fd, FRegister fs, FRegister ft);
249 void MulD(FRegister fd, FRegister fs, FRegister ft);
250 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800251 void SqrtS(FRegister fd, FRegister fs);
252 void SqrtD(FRegister fd, FRegister fs);
253 void AbsS(FRegister fd, FRegister fs);
254 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700255 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200256 void MovD(FRegister fd, FRegister fs);
257 void NegS(FRegister fd, FRegister fs);
258 void NegD(FRegister fd, FRegister fs);
259
Chris Larsenb74353a2015-11-20 09:07:09 -0800260 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800261 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800262 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800263 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800264 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800265 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800266 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800267 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800268 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800269 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800270 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800271 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800272 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800273 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800274 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800275 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800276 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800277 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800278 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800279 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800280 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800281 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800282 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800283 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800284 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800285 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800286 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800287 void CuleD(int cc, FRegister fs, FRegister ft); // R2
288 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
289 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
290 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
291 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
292 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
293 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
294 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
295 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
296 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
297 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
298 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
299 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
300 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
301 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
302 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
303 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
304 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
305 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
306 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
307 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800308 void Movf(Register rd, Register rs, int cc = 0); // R2
309 void Movt(Register rd, Register rs, int cc = 0); // R2
310 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
311 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
312 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
313 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
314 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
315 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
316 void ClassS(FRegister fd, FRegister fs); // R6
317 void ClassD(FRegister fd, FRegister fs); // R6
318 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
319 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
320 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
321 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800322
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800323 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
324 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
325 void TruncWS(FRegister fd, FRegister fs);
326 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200327 void Cvtsw(FRegister fd, FRegister fs);
328 void Cvtdw(FRegister fd, FRegister fs);
329 void Cvtsd(FRegister fd, FRegister fs);
330 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800331 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
332 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800333 void FloorWS(FRegister fd, FRegister fs);
334 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700335
336 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200337 void Mtc1(Register rt, FRegister fs);
338 void Mfhc1(Register rt, FRegister fs);
339 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800340 void MoveFromFpuHigh(Register rt, FRegister fs);
341 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700342 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200343 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700344 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200345 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700346
347 void Break();
jeffhao07030602012-09-26 14:33:14 -0700348 void Nop();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200349 void Move(Register rd, Register rs);
350 void Clear(Register rd);
351 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700352
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200353 // Higher level composite instructions.
354 void LoadConst32(Register rd, int32_t value);
355 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
356 void LoadDConst64(FRegister rd, int64_t value, Register temp);
357 void LoadSConst32(FRegister r, int32_t value, Register temp);
358 void StoreConst32ToOffset(int32_t value, Register base, int32_t offset, Register temp);
359 void StoreConst64ToOffset(int64_t value, Register base, int32_t offset, Register temp);
360 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
361
362 // These will generate R2 branches or R6 branches as appropriate.
363 void Bind(MipsLabel* label);
364 void B(MipsLabel* label);
365 void Jalr(MipsLabel* label, Register indirect_reg);
366 void Beq(Register rs, Register rt, MipsLabel* label);
367 void Bne(Register rs, Register rt, MipsLabel* label);
368 void Beqz(Register rt, MipsLabel* label);
369 void Bnez(Register rt, MipsLabel* label);
370 void Bltz(Register rt, MipsLabel* label);
371 void Bgez(Register rt, MipsLabel* label);
372 void Blez(Register rt, MipsLabel* label);
373 void Bgtz(Register rt, MipsLabel* label);
374 void Blt(Register rs, Register rt, MipsLabel* label);
375 void Bge(Register rs, Register rt, MipsLabel* label);
376 void Bltu(Register rs, Register rt, MipsLabel* label);
377 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800378 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800379 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800380 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800381 void Bc1t(int cc, MipsLabel* label); // R2
382 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
383 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700384
385 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
386 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
387 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200388 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700389 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000390 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200391 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700392
jeffhao7fbee072012-08-24 17:56:54 -0700393 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200394 void Emit(uint32_t value);
395
396 // Push/pop composite routines.
397 void Push(Register rs);
398 void Pop(Register rd);
399 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700400
Andreas Gampe85b62f22015-09-09 13:15:38 -0700401 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200402 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700403 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200404 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
405 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700406 }
407
jeffhao7fbee072012-08-24 17:56:54 -0700408 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200409 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700410 //
411
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200412 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800413 void BuildFrame(size_t frame_size,
414 ManagedRegister method_reg,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700415 const std::vector<ManagedRegister>& callee_save_regs,
416 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700417
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200418 // Emit code that will remove an activation from the stack.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700419 void RemoveFrame(size_t frame_size, const std::vector<ManagedRegister>& callee_save_regs)
420 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700421
Ian Rogersdd7624d2014-03-14 17:43:00 -0700422 void IncreaseFrameSize(size_t adjust) OVERRIDE;
423 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700424
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200425 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700426 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
427 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
428 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700429
Ian Rogersdd7624d2014-03-14 17:43:00 -0700430 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700431
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800432 void StoreImmediateToThread32(ThreadOffset<kMipsWordSize> dest,
433 uint32_t imm,
434 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800436 void StoreStackOffsetToThread32(ThreadOffset<kMipsWordSize> thr_offs,
437 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700438 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700439
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800440 void StoreStackPointerToThread32(ThreadOffset<kMipsWordSize> thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700441
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800442 void StoreSpanning(FrameOffset dest,
443 ManagedRegister msrc,
444 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700445 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700446
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200447 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700448 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700449
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800450 void LoadFromThread32(ManagedRegister mdest,
451 ThreadOffset<kMipsWordSize> src,
452 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700453
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700455
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800456 void LoadRef(ManagedRegister mdest,
457 ManagedRegister base,
458 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100459 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700460
Ian Rogersdd7624d2014-03-14 17:43:00 -0700461 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700462
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800463 void LoadRawPtrFromThread32(ManagedRegister mdest, ThreadOffset<kMipsWordSize> offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700464
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200465 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700466 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700467
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800468 void CopyRawPtrFromThread32(FrameOffset fr_offs,
469 ThreadOffset<kMipsWordSize> thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700470 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700471
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800472 void CopyRawPtrToThread32(ThreadOffset<kMipsWordSize> thr_offs,
473 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700474 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700475
Ian Rogersdd7624d2014-03-14 17:43:00 -0700476 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700477
Ian Rogersdd7624d2014-03-14 17:43:00 -0700478 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700479
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800480 void Copy(FrameOffset dest,
481 ManagedRegister src_base,
482 Offset src_offset,
483 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700484 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700485
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800486 void Copy(ManagedRegister dest_base,
487 Offset dest_offset,
488 FrameOffset src,
489 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700490 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700491
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800492 void Copy(FrameOffset dest,
493 FrameOffset src_base,
494 Offset src_offset,
495 ManagedRegister mscratch,
496 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700497
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800498 void Copy(ManagedRegister dest,
499 Offset dest_offset,
500 ManagedRegister src,
501 Offset src_offset,
502 ManagedRegister mscratch,
503 size_t size) OVERRIDE;
504
505 void Copy(FrameOffset dest,
506 Offset dest_offset,
507 FrameOffset src,
508 Offset src_offset,
509 ManagedRegister mscratch,
510 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700511
Ian Rogersdd7624d2014-03-14 17:43:00 -0700512 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700513
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200514 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700515 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700516
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200517 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700518 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700519
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200520 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700521 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
522 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700523
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700524 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700525 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700526 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700527 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800528 void CreateHandleScopeEntry(ManagedRegister out_reg,
529 FrameOffset handlescope_offset,
530 ManagedRegister in_reg,
531 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700532
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700533 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700534 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800535 void CreateHandleScopeEntry(FrameOffset out_off,
536 FrameOffset handlescope_offset,
537 ManagedRegister mscratch,
538 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700539
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200540 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700541 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700542
543 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
544 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700545 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
546 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700547
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200548 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700549 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
550 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800551 void CallFromThread32(ThreadOffset<kMipsWordSize> offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700552
jeffhao7fbee072012-08-24 17:56:54 -0700553 // Generate code to check if Thread::Current()->exception_ is non-null
554 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700555 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700556
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200557 // Emit slow paths queued during assembly and promote short branches to long if needed.
558 void FinalizeCode() OVERRIDE;
559
560 // Emit branches and finalize all instructions.
561 void FinalizeInstructions(const MemoryRegion& region);
562
563 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
564 // must be used instead of MipsLabel::GetPosition()).
565 uint32_t GetLabelLocation(MipsLabel* label) const;
566
567 // Get the final position of a label after local fixup based on the old position
568 // recorded before FinalizeCode().
569 uint32_t GetAdjustedPosition(uint32_t old_position);
570
571 enum BranchCondition {
572 kCondLT,
573 kCondGE,
574 kCondLE,
575 kCondGT,
576 kCondLTZ,
577 kCondGEZ,
578 kCondLEZ,
579 kCondGTZ,
580 kCondEQ,
581 kCondNE,
582 kCondEQZ,
583 kCondNEZ,
584 kCondLTU,
585 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800586 kCondF, // Floating-point predicate false.
587 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200588 kUncond,
589 };
590 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
591
jeffhao7fbee072012-08-24 17:56:54 -0700592 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200593 class Branch {
594 public:
595 enum Type {
596 // R2 short branches.
597 kUncondBranch,
598 kCondBranch,
599 kCall,
600 // R2 long branches.
601 kLongUncondBranch,
602 kLongCondBranch,
603 kLongCall,
604 // R6 short branches.
605 kR6UncondBranch,
606 kR6CondBranch,
607 kR6Call,
608 // R6 long branches.
609 kR6LongUncondBranch,
610 kR6LongCondBranch,
611 kR6LongCall,
612 };
613 // Bit sizes of offsets defined as enums to minimize chance of typos.
614 enum OffsetBits {
615 kOffset16 = 16,
616 kOffset18 = 18,
617 kOffset21 = 21,
618 kOffset23 = 23,
619 kOffset28 = 28,
620 kOffset32 = 32,
621 };
622
623 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
624 static constexpr int32_t kMaxBranchLength = 32;
625 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
626
627 struct BranchInfo {
628 // Branch length as a number of 4-byte-long instructions.
629 uint32_t length;
630 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
631 // PC-relative offset (or its most significant 16-bit half, which goes first).
632 uint32_t instr_offset;
633 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
634 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
635 // instructions) from the instruction containing the offset.
636 uint32_t pc_org;
637 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
638 // is an exception: use kOffset23 for beqzc/bnezc).
639 OffsetBits offset_size;
640 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
641 // count.
642 int offset_shift;
643 };
644 static const BranchInfo branch_info_[/* Type */];
645
646 // Unconditional branch.
647 Branch(bool is_r6, uint32_t location, uint32_t target);
648 // Conditional branch.
649 Branch(bool is_r6,
650 uint32_t location,
651 uint32_t target,
652 BranchCondition condition,
653 Register lhs_reg,
654 Register rhs_reg = ZERO);
655 // Call (branch and link) that stores the target address in a given register (i.e. T9).
656 Branch(bool is_r6, uint32_t location, uint32_t target, Register indirect_reg);
657
658 // Some conditional branches with lhs = rhs are effectively NOPs, while some
659 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
660 // So, we need a way to identify such branches in order to emit no instructions for them
661 // or change them to unconditional.
662 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
663 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
664
665 static BranchCondition OppositeCondition(BranchCondition cond);
666
667 Type GetType() const;
668 BranchCondition GetCondition() const;
669 Register GetLeftRegister() const;
670 Register GetRightRegister() const;
671 uint32_t GetTarget() const;
672 uint32_t GetLocation() const;
673 uint32_t GetOldLocation() const;
674 uint32_t GetLength() const;
675 uint32_t GetOldLength() const;
676 uint32_t GetSize() const;
677 uint32_t GetOldSize() const;
678 uint32_t GetEndLocation() const;
679 uint32_t GetOldEndLocation() const;
680 bool IsLong() const;
681 bool IsResolved() const;
682
683 // Returns the bit size of the signed offset that the branch instruction can handle.
684 OffsetBits GetOffsetSize() const;
685
686 // Calculates the distance between two byte locations in the assembler buffer and
687 // returns the number of bits needed to represent the distance as a signed integer.
688 //
689 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
690 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
691 //
692 // Composite branches (made of several instructions) with longer reach have 32-bit
693 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800694 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
695 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
696 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
697 // Consider the following implementation of a long unconditional branch, for
698 // example:
699 //
700 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
701 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
702 //
703 // Both of the above instructions take 16-bit signed offsets as immediate operands.
704 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
705 // due to sign extension. This must be compensated for by incrementing offset_31_16
706 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
707 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
708 // Therefore, the long branch range is something like from PC - 0x80000000 to
709 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200710 //
711 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
712 // case with the addiu instruction and a 16 bit offset.
713 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
714
715 // Resolve a branch when the target is known.
716 void Resolve(uint32_t target);
717
718 // Relocate a branch by a given delta if needed due to expansion of this or another
719 // branch at a given location by this delta (just changes location_ and target_).
720 void Relocate(uint32_t expand_location, uint32_t delta);
721
722 // If the branch is short, changes its type to long.
723 void PromoteToLong();
724
725 // If necessary, updates the type by promoting a short branch to a long branch
726 // based on the branch location and target. Returns the amount (in bytes) by
727 // which the branch size has increased.
728 // max_short_distance caps the maximum distance between location_ and target_
729 // that is allowed for short branches. This is for debugging/testing purposes.
730 // max_short_distance = 0 forces all short branches to become long.
731 // Use the implicit default argument when not debugging/testing.
732 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
733
734 // Returns the location of the instruction(s) containing the offset.
735 uint32_t GetOffsetLocation() const;
736
737 // Calculates and returns the offset ready for encoding in the branch instruction(s).
738 uint32_t GetOffset() const;
739
740 private:
741 // Completes branch construction by determining and recording its type.
742 void InitializeType(bool is_call, bool is_r6);
743 // Helper for the above.
744 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
745
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800746 uint32_t old_location_; // Offset into assembler buffer in bytes.
747 uint32_t location_; // Offset into assembler buffer in bytes.
748 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200749
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800750 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
751 // indirect call register.
752 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
753 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200754
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800755 Type type_; // Current type of the branch.
756 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200757 };
758 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
759 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
760
jeffhao7fbee072012-08-24 17:56:54 -0700761 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
762 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200763 void EmitI21(int opcode, Register rs, uint32_t imm21);
764 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700765 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
766 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800767 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
768 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -0700769
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200770 void Buncond(MipsLabel* label);
771 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
772 void Call(MipsLabel* label, Register indirect_reg);
773 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700774
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200775 Branch* GetBranch(uint32_t branch_id);
776 const Branch* GetBranch(uint32_t branch_id) const;
777
778 void PromoteBranches();
779 void EmitBranch(Branch* branch);
780 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100781 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200782
783 // Emits exception block.
784 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
785
786 bool IsR6() const {
787 if (isa_features_ != nullptr) {
788 return isa_features_->IsR6();
789 } else {
790 return false;
791 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000792 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200793
794 bool Is32BitFPU() const {
795 if (isa_features_ != nullptr) {
796 return isa_features_->Is32BitFloatingPoint();
797 } else {
798 return true;
799 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000800 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200801
802 // List of exception blocks to generate at the end of the code cache.
803 std::vector<MipsExceptionSlowPath> exception_blocks_;
804
805 std::vector<Branch> branches_;
806
807 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
808 bool overwriting_;
809 // The current overwrite location.
810 uint32_t overwrite_location_;
811
812 // Data for AdjustedPosition(), see the description there.
813 uint32_t last_position_adjustment_;
814 uint32_t last_old_position_;
815 uint32_t last_branch_id_;
816
817 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000818
jeffhao7fbee072012-08-24 17:56:54 -0700819 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
820};
821
jeffhao7fbee072012-08-24 17:56:54 -0700822} // namespace mips
823} // namespace art
824
Ian Rogers166db042013-07-26 12:05:57 -0700825#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_