blob: 68fa6bf918e5f186080ae8d55a76605c45ca6d3f [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:
Vladimir Marko93205e32016-04-13 11:59:46 +0100105 explicit MipsAssembler(ArenaAllocator* arena,
106 const MipsInstructionSetFeatures* instruction_set_features = nullptr)
107 : Assembler(arena),
108 overwriting_(false),
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200109 overwrite_location_(0),
110 last_position_adjustment_(0),
111 last_old_position_(0),
112 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +0100113 isa_features_(instruction_set_features) {
114 cfi().DelayEmittingAdvancePCs();
115 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200116
117 virtual ~MipsAssembler() {
118 for (auto& branch : branches_) {
119 CHECK(branch.IsResolved());
120 }
121 }
jeffhao7fbee072012-08-24 17:56:54 -0700122
123 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700124 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700125 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700126 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200127
128 void MultR2(Register rs, Register rt); // R2
129 void MultuR2(Register rs, Register rt); // R2
130 void DivR2(Register rs, Register rt); // R2
131 void DivuR2(Register rs, Register rt); // R2
132 void MulR2(Register rd, Register rs, Register rt); // R2
133 void DivR2(Register rd, Register rs, Register rt); // R2
134 void ModR2(Register rd, Register rs, Register rt); // R2
135 void DivuR2(Register rd, Register rs, Register rt); // R2
136 void ModuR2(Register rd, Register rs, Register rt); // R2
137 void MulR6(Register rd, Register rs, Register rt); // R6
Alexey Frunze7e99e052015-11-24 19:28:01 -0800138 void MuhR6(Register rd, Register rs, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200139 void MuhuR6(Register rd, Register rs, Register rt); // R6
140 void DivR6(Register rd, Register rs, Register rt); // R6
141 void ModR6(Register rd, Register rs, Register rt); // R6
142 void DivuR6(Register rd, Register rs, Register rt); // R6
143 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700144
145 void And(Register rd, Register rs, Register rt);
146 void Andi(Register rt, Register rs, uint16_t imm16);
147 void Or(Register rd, Register rs, Register rt);
148 void Ori(Register rt, Register rs, uint16_t imm16);
149 void Xor(Register rd, Register rs, Register rt);
150 void Xori(Register rt, Register rs, uint16_t imm16);
151 void Nor(Register rd, Register rs, Register rt);
152
Chris Larsene3845472015-11-18 12:27:15 -0800153 void Movz(Register rd, Register rs, Register rt); // R2
154 void Movn(Register rd, Register rs, Register rt); // R2
155 void Seleqz(Register rd, Register rs, Register rt); // R6
156 void Selnez(Register rd, Register rs, Register rt); // R6
157 void ClzR6(Register rd, Register rs);
158 void ClzR2(Register rd, Register rs);
159 void CloR6(Register rd, Register rs);
160 void CloR2(Register rd, Register rs);
161
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200162 void Seb(Register rd, Register rt); // R2+
163 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700164 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800165 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200166
167 void Sll(Register rd, Register rt, int shamt);
168 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700169 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200170 void Sra(Register rd, Register rt, int shamt);
171 void Sllv(Register rd, Register rt, Register rs);
172 void Srlv(Register rd, Register rt, Register rs);
Chris Larsene16ce5a2015-11-18 12:30:20 -0800173 void Rotrv(Register rd, Register rt, Register rs); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200174 void Srav(Register rd, Register rt, Register rs);
Alexey Frunze5c7aed32015-11-25 19:41:54 -0800175 void Ext(Register rd, Register rt, int pos, int size); // R2+
176 void Ins(Register rd, Register rt, int pos, int size); // R2+
jeffhao7fbee072012-08-24 17:56:54 -0700177
178 void Lb(Register rt, Register rs, uint16_t imm16);
179 void Lh(Register rt, Register rs, uint16_t imm16);
180 void Lw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800181 void Lwl(Register rt, Register rs, uint16_t imm16);
182 void Lwr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700183 void Lbu(Register rt, Register rs, uint16_t imm16);
184 void Lhu(Register rt, Register rs, uint16_t imm16);
185 void Lui(Register rt, uint16_t imm16);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700186 void Aui(Register rt, Register rs, uint16_t imm16); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200187 void Sync(uint32_t stype);
188 void Mfhi(Register rd); // R2
189 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700190
191 void Sb(Register rt, Register rs, uint16_t imm16);
192 void Sh(Register rt, Register rs, uint16_t imm16);
193 void Sw(Register rt, Register rs, uint16_t imm16);
Chris Larsen3acee732015-11-18 13:31:08 -0800194 void Swl(Register rt, Register rs, uint16_t imm16);
195 void Swr(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700196
Alexey Frunze51aff3a2016-03-17 17:21:45 -0700197 void LlR2(Register rt, Register base, int16_t imm16 = 0);
198 void ScR2(Register rt, Register base, int16_t imm16 = 0);
199 void LlR6(Register rt, Register base, int16_t imm9 = 0);
200 void ScR6(Register rt, Register base, int16_t imm9 = 0);
201
jeffhao7fbee072012-08-24 17:56:54 -0700202 void Slt(Register rd, Register rs, Register rt);
203 void Sltu(Register rd, Register rs, Register rt);
204 void Slti(Register rt, Register rs, uint16_t imm16);
205 void Sltiu(Register rt, Register rs, uint16_t imm16);
206
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200207 void B(uint16_t imm16);
208 void Beq(Register rs, Register rt, uint16_t imm16);
209 void Bne(Register rs, Register rt, uint16_t imm16);
210 void Beqz(Register rt, uint16_t imm16);
211 void Bnez(Register rt, uint16_t imm16);
212 void Bltz(Register rt, uint16_t imm16);
213 void Bgez(Register rt, uint16_t imm16);
214 void Blez(Register rt, uint16_t imm16);
215 void Bgtz(Register rt, uint16_t imm16);
Chris Larsenb74353a2015-11-20 09:07:09 -0800216 void Bc1f(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800217 void Bc1f(int cc, uint16_t imm16); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800218 void Bc1t(uint16_t imm16); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800219 void Bc1t(int cc, uint16_t imm16); // R2
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200220 void J(uint32_t addr26);
221 void Jal(uint32_t addr26);
222 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700223 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200224 void Jr(Register rs);
225 void Nal();
226 void Auipc(Register rs, uint16_t imm16); // R6
227 void Addiupc(Register rs, uint32_t imm19); // R6
228 void Bc(uint32_t imm26); // R6
229 void Jic(Register rt, uint16_t imm16); // R6
230 void Jialc(Register rt, uint16_t imm16); // R6
231 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
232 void Bltzc(Register rt, uint16_t imm16); // R6
233 void Bgtzc(Register rt, uint16_t imm16); // R6
234 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
235 void Bgezc(Register rt, uint16_t imm16); // R6
236 void Blezc(Register rt, uint16_t imm16); // R6
237 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
238 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
239 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
240 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
241 void Beqzc(Register rs, uint32_t imm21); // R6
242 void Bnezc(Register rs, uint32_t imm21); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800243 void Bc1eqz(FRegister ft, uint16_t imm16); // R6
244 void Bc1nez(FRegister ft, uint16_t imm16); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700245
246 void AddS(FRegister fd, FRegister fs, FRegister ft);
247 void SubS(FRegister fd, FRegister fs, FRegister ft);
248 void MulS(FRegister fd, FRegister fs, FRegister ft);
249 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200250 void AddD(FRegister fd, FRegister fs, FRegister ft);
251 void SubD(FRegister fd, FRegister fs, FRegister ft);
252 void MulD(FRegister fd, FRegister fs, FRegister ft);
253 void DivD(FRegister fd, FRegister fs, FRegister ft);
Chris Larsenb74353a2015-11-20 09:07:09 -0800254 void SqrtS(FRegister fd, FRegister fs);
255 void SqrtD(FRegister fd, FRegister fs);
256 void AbsS(FRegister fd, FRegister fs);
257 void AbsD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700258 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200259 void MovD(FRegister fd, FRegister fs);
260 void NegS(FRegister fd, FRegister fs);
261 void NegD(FRegister fd, FRegister fs);
262
Chris Larsenb74353a2015-11-20 09:07:09 -0800263 void CunS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800264 void CunS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800265 void CeqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800266 void CeqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800267 void CueqS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800268 void CueqS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800269 void ColtS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800270 void ColtS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800271 void CultS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800272 void CultS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800273 void ColeS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800274 void ColeS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800275 void CuleS(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800276 void CuleS(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800277 void CunD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800278 void CunD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800279 void CeqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800280 void CeqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800281 void CueqD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800282 void CueqD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800283 void ColtD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800284 void ColtD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800285 void CultD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800286 void CultD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800287 void ColeD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800288 void ColeD(int cc, FRegister fs, FRegister ft); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800289 void CuleD(FRegister fs, FRegister ft); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800290 void CuleD(int cc, FRegister fs, FRegister ft); // R2
291 void CmpUnS(FRegister fd, FRegister fs, FRegister ft); // R6
292 void CmpEqS(FRegister fd, FRegister fs, FRegister ft); // R6
293 void CmpUeqS(FRegister fd, FRegister fs, FRegister ft); // R6
294 void CmpLtS(FRegister fd, FRegister fs, FRegister ft); // R6
295 void CmpUltS(FRegister fd, FRegister fs, FRegister ft); // R6
296 void CmpLeS(FRegister fd, FRegister fs, FRegister ft); // R6
297 void CmpUleS(FRegister fd, FRegister fs, FRegister ft); // R6
298 void CmpOrS(FRegister fd, FRegister fs, FRegister ft); // R6
299 void CmpUneS(FRegister fd, FRegister fs, FRegister ft); // R6
300 void CmpNeS(FRegister fd, FRegister fs, FRegister ft); // R6
301 void CmpUnD(FRegister fd, FRegister fs, FRegister ft); // R6
302 void CmpEqD(FRegister fd, FRegister fs, FRegister ft); // R6
303 void CmpUeqD(FRegister fd, FRegister fs, FRegister ft); // R6
304 void CmpLtD(FRegister fd, FRegister fs, FRegister ft); // R6
305 void CmpUltD(FRegister fd, FRegister fs, FRegister ft); // R6
306 void CmpLeD(FRegister fd, FRegister fs, FRegister ft); // R6
307 void CmpUleD(FRegister fd, FRegister fs, FRegister ft); // R6
308 void CmpOrD(FRegister fd, FRegister fs, FRegister ft); // R6
309 void CmpUneD(FRegister fd, FRegister fs, FRegister ft); // R6
310 void CmpNeD(FRegister fd, FRegister fs, FRegister ft); // R6
Chris Larsenb74353a2015-11-20 09:07:09 -0800311 void Movf(Register rd, Register rs, int cc = 0); // R2
312 void Movt(Register rd, Register rs, int cc = 0); // R2
313 void MovfS(FRegister fd, FRegister fs, int cc = 0); // R2
314 void MovfD(FRegister fd, FRegister fs, int cc = 0); // R2
315 void MovtS(FRegister fd, FRegister fs, int cc = 0); // R2
316 void MovtD(FRegister fd, FRegister fs, int cc = 0); // R2
317 void SelS(FRegister fd, FRegister fs, FRegister ft); // R6
318 void SelD(FRegister fd, FRegister fs, FRegister ft); // R6
319 void ClassS(FRegister fd, FRegister fs); // R6
320 void ClassD(FRegister fd, FRegister fs); // R6
321 void MinS(FRegister fd, FRegister fs, FRegister ft); // R6
322 void MinD(FRegister fd, FRegister fs, FRegister ft); // R6
323 void MaxS(FRegister fd, FRegister fs, FRegister ft); // R6
324 void MaxD(FRegister fd, FRegister fs, FRegister ft); // R6
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800325
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800326 void TruncLS(FRegister fd, FRegister fs); // R2+, FR=1
327 void TruncLD(FRegister fd, FRegister fs); // R2+, FR=1
328 void TruncWS(FRegister fd, FRegister fs);
329 void TruncWD(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200330 void Cvtsw(FRegister fd, FRegister fs);
331 void Cvtdw(FRegister fd, FRegister fs);
332 void Cvtsd(FRegister fd, FRegister fs);
333 void Cvtds(FRegister fd, FRegister fs);
Alexey Frunzebaf60b72015-12-22 15:15:03 -0800334 void Cvtsl(FRegister fd, FRegister fs); // R2+, FR=1
335 void Cvtdl(FRegister fd, FRegister fs); // R2+, FR=1
Chris Larsenb74353a2015-11-20 09:07:09 -0800336 void FloorWS(FRegister fd, FRegister fs);
337 void FloorWD(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700338
339 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200340 void Mtc1(Register rt, FRegister fs);
341 void Mfhc1(Register rt, FRegister fs);
342 void Mthc1(Register rt, FRegister fs);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800343 void MoveFromFpuHigh(Register rt, FRegister fs);
344 void MoveToFpuHigh(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700345 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200346 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700347 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200348 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700349
350 void Break();
jeffhao07030602012-09-26 14:33:14 -0700351 void Nop();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200352 void Move(Register rd, Register rs);
353 void Clear(Register rd);
354 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700355
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200356 // Higher level composite instructions.
357 void LoadConst32(Register rd, int32_t value);
358 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
359 void LoadDConst64(FRegister rd, int64_t value, Register temp);
360 void LoadSConst32(FRegister r, int32_t value, Register temp);
361 void StoreConst32ToOffset(int32_t value, Register base, int32_t offset, Register temp);
362 void StoreConst64ToOffset(int64_t value, Register base, int32_t offset, Register temp);
363 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
364
365 // These will generate R2 branches or R6 branches as appropriate.
366 void Bind(MipsLabel* label);
367 void B(MipsLabel* label);
368 void Jalr(MipsLabel* label, Register indirect_reg);
369 void Beq(Register rs, Register rt, MipsLabel* label);
370 void Bne(Register rs, Register rt, MipsLabel* label);
371 void Beqz(Register rt, MipsLabel* label);
372 void Bnez(Register rt, MipsLabel* label);
373 void Bltz(Register rt, MipsLabel* label);
374 void Bgez(Register rt, MipsLabel* label);
375 void Blez(Register rt, MipsLabel* label);
376 void Bgtz(Register rt, MipsLabel* label);
377 void Blt(Register rs, Register rt, MipsLabel* label);
378 void Bge(Register rs, Register rt, MipsLabel* label);
379 void Bltu(Register rs, Register rt, MipsLabel* label);
380 void Bgeu(Register rs, Register rt, MipsLabel* label);
Chris Larsenb74353a2015-11-20 09:07:09 -0800381 void Bc1f(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800382 void Bc1f(int cc, MipsLabel* label); // R2
Chris Larsenb74353a2015-11-20 09:07:09 -0800383 void Bc1t(MipsLabel* label); // R2
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800384 void Bc1t(int cc, MipsLabel* label); // R2
385 void Bc1eqz(FRegister ft, MipsLabel* label); // R6
386 void Bc1nez(FRegister ft, MipsLabel* label); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700387
388 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
Alexey Frunzecad3a4c2016-06-07 23:40:37 -0700389 void AdjustBaseAndOffset(Register& base,
390 int32_t& offset,
391 bool is_doubleword,
392 bool is_float = false);
jeffhao7fbee072012-08-24 17:56:54 -0700393 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
394 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200395 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700396 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000397 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200398 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700399
jeffhao7fbee072012-08-24 17:56:54 -0700400 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200401 void Emit(uint32_t value);
402
403 // Push/pop composite routines.
404 void Push(Register rs);
405 void Pop(Register rd);
406 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700407
Andreas Gampe85b62f22015-09-09 13:15:38 -0700408 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200409 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700410 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200411 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
412 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700413 }
414
jeffhao7fbee072012-08-24 17:56:54 -0700415 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200416 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700417 //
418
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200419 // Emit code that will create an activation on the stack.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800420 void BuildFrame(size_t frame_size,
421 ManagedRegister method_reg,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700422 const std::vector<ManagedRegister>& callee_save_regs,
423 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700424
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200425 // Emit code that will remove an activation from the stack.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700426 void RemoveFrame(size_t frame_size, const std::vector<ManagedRegister>& callee_save_regs)
427 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700428
Ian Rogersdd7624d2014-03-14 17:43:00 -0700429 void IncreaseFrameSize(size_t adjust) OVERRIDE;
430 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700431
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200432 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700433 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
434 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
435 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700436
Ian Rogersdd7624d2014-03-14 17:43:00 -0700437 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800439 void StoreImmediateToThread32(ThreadOffset<kMipsWordSize> dest,
440 uint32_t imm,
441 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700442
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800443 void StoreStackOffsetToThread32(ThreadOffset<kMipsWordSize> thr_offs,
444 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700445 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700446
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800447 void StoreStackPointerToThread32(ThreadOffset<kMipsWordSize> thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700448
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800449 void StoreSpanning(FrameOffset dest,
450 ManagedRegister msrc,
451 FrameOffset in_off,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700452 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700453
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200454 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700455 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700456
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800457 void LoadFromThread32(ManagedRegister mdest,
458 ThreadOffset<kMipsWordSize> src,
459 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700460
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700462
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800463 void LoadRef(ManagedRegister mdest,
464 ManagedRegister base,
465 MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100466 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700467
Ian Rogersdd7624d2014-03-14 17:43:00 -0700468 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700469
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800470 void LoadRawPtrFromThread32(ManagedRegister mdest, ThreadOffset<kMipsWordSize> offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700471
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200472 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700473 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700474
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800475 void CopyRawPtrFromThread32(FrameOffset fr_offs,
476 ThreadOffset<kMipsWordSize> thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700477 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700478
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800479 void CopyRawPtrToThread32(ThreadOffset<kMipsWordSize> thr_offs,
480 FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700481 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700482
Ian Rogersdd7624d2014-03-14 17:43:00 -0700483 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700484
Ian Rogersdd7624d2014-03-14 17:43:00 -0700485 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700486
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800487 void Copy(FrameOffset dest,
488 ManagedRegister src_base,
489 Offset src_offset,
490 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700491 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700492
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800493 void Copy(ManagedRegister dest_base,
494 Offset dest_offset,
495 FrameOffset src,
496 ManagedRegister mscratch,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700497 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700498
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800499 void Copy(FrameOffset dest,
500 FrameOffset src_base,
501 Offset src_offset,
502 ManagedRegister mscratch,
503 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700504
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800505 void Copy(ManagedRegister dest,
506 Offset dest_offset,
507 ManagedRegister src,
508 Offset src_offset,
509 ManagedRegister mscratch,
510 size_t size) OVERRIDE;
511
512 void Copy(FrameOffset dest,
513 Offset dest_offset,
514 FrameOffset src,
515 Offset src_offset,
516 ManagedRegister mscratch,
517 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700518
Ian Rogersdd7624d2014-03-14 17:43:00 -0700519 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700520
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200521 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700522 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700523
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200524 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700525 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700526
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200527 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700528 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
529 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700530
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700531 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700532 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700533 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700534 // null.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800535 void CreateHandleScopeEntry(ManagedRegister out_reg,
536 FrameOffset handlescope_offset,
537 ManagedRegister in_reg,
538 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700539
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700540 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700541 // value is null and null_allowed.
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800542 void CreateHandleScopeEntry(FrameOffset out_off,
543 FrameOffset handlescope_offset,
544 ManagedRegister mscratch,
545 bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700546
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200547 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700548 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700549
550 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
551 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700552 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
553 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700554
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200555 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700556 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
557 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800558 void CallFromThread32(ThreadOffset<kMipsWordSize> offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700559
jeffhao7fbee072012-08-24 17:56:54 -0700560 // Generate code to check if Thread::Current()->exception_ is non-null
561 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700562 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700563
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200564 // Emit slow paths queued during assembly and promote short branches to long if needed.
565 void FinalizeCode() OVERRIDE;
566
567 // Emit branches and finalize all instructions.
568 void FinalizeInstructions(const MemoryRegion& region);
569
570 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
571 // must be used instead of MipsLabel::GetPosition()).
572 uint32_t GetLabelLocation(MipsLabel* label) const;
573
574 // Get the final position of a label after local fixup based on the old position
575 // recorded before FinalizeCode().
576 uint32_t GetAdjustedPosition(uint32_t old_position);
577
578 enum BranchCondition {
579 kCondLT,
580 kCondGE,
581 kCondLE,
582 kCondGT,
583 kCondLTZ,
584 kCondGEZ,
585 kCondLEZ,
586 kCondGTZ,
587 kCondEQ,
588 kCondNE,
589 kCondEQZ,
590 kCondNEZ,
591 kCondLTU,
592 kCondGEU,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800593 kCondF, // Floating-point predicate false.
594 kCondT, // Floating-point predicate true.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200595 kUncond,
596 };
597 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
598
jeffhao7fbee072012-08-24 17:56:54 -0700599 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200600 class Branch {
601 public:
602 enum Type {
603 // R2 short branches.
604 kUncondBranch,
605 kCondBranch,
606 kCall,
607 // R2 long branches.
608 kLongUncondBranch,
609 kLongCondBranch,
610 kLongCall,
611 // R6 short branches.
612 kR6UncondBranch,
613 kR6CondBranch,
614 kR6Call,
615 // R6 long branches.
616 kR6LongUncondBranch,
617 kR6LongCondBranch,
618 kR6LongCall,
619 };
620 // Bit sizes of offsets defined as enums to minimize chance of typos.
621 enum OffsetBits {
622 kOffset16 = 16,
623 kOffset18 = 18,
624 kOffset21 = 21,
625 kOffset23 = 23,
626 kOffset28 = 28,
627 kOffset32 = 32,
628 };
629
630 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
631 static constexpr int32_t kMaxBranchLength = 32;
632 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
633
634 struct BranchInfo {
635 // Branch length as a number of 4-byte-long instructions.
636 uint32_t length;
637 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
638 // PC-relative offset (or its most significant 16-bit half, which goes first).
639 uint32_t instr_offset;
640 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
641 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
642 // instructions) from the instruction containing the offset.
643 uint32_t pc_org;
644 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
645 // is an exception: use kOffset23 for beqzc/bnezc).
646 OffsetBits offset_size;
647 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
648 // count.
649 int offset_shift;
650 };
651 static const BranchInfo branch_info_[/* Type */];
652
653 // Unconditional branch.
654 Branch(bool is_r6, uint32_t location, uint32_t target);
655 // Conditional branch.
656 Branch(bool is_r6,
657 uint32_t location,
658 uint32_t target,
659 BranchCondition condition,
660 Register lhs_reg,
661 Register rhs_reg = ZERO);
662 // Call (branch and link) that stores the target address in a given register (i.e. T9).
663 Branch(bool is_r6, uint32_t location, uint32_t target, Register indirect_reg);
664
665 // Some conditional branches with lhs = rhs are effectively NOPs, while some
666 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
667 // So, we need a way to identify such branches in order to emit no instructions for them
668 // or change them to unconditional.
669 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
670 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
671
672 static BranchCondition OppositeCondition(BranchCondition cond);
673
674 Type GetType() const;
675 BranchCondition GetCondition() const;
676 Register GetLeftRegister() const;
677 Register GetRightRegister() const;
678 uint32_t GetTarget() const;
679 uint32_t GetLocation() const;
680 uint32_t GetOldLocation() const;
681 uint32_t GetLength() const;
682 uint32_t GetOldLength() const;
683 uint32_t GetSize() const;
684 uint32_t GetOldSize() const;
685 uint32_t GetEndLocation() const;
686 uint32_t GetOldEndLocation() const;
687 bool IsLong() const;
688 bool IsResolved() const;
689
690 // Returns the bit size of the signed offset that the branch instruction can handle.
691 OffsetBits GetOffsetSize() const;
692
693 // Calculates the distance between two byte locations in the assembler buffer and
694 // returns the number of bits needed to represent the distance as a signed integer.
695 //
696 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
697 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
698 //
699 // Composite branches (made of several instructions) with longer reach have 32-bit
700 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800701 // The composite branches cover the range of PC + +/-2GB on MIPS32 CPUs. However,
702 // the range is not end-to-end on MIPS64 (unless addresses are forced to zero- or
703 // sign-extend from 32 to 64 bits by the appropriate CPU configuration).
704 // Consider the following implementation of a long unconditional branch, for
705 // example:
706 //
707 // auipc at, offset_31_16 // at = pc + sign_extend(offset_31_16) << 16
708 // jic at, offset_15_0 // pc = at + sign_extend(offset_15_0)
709 //
710 // Both of the above instructions take 16-bit signed offsets as immediate operands.
711 // When bit 15 of offset_15_0 is 1, it effectively causes subtraction of 0x10000
712 // due to sign extension. This must be compensated for by incrementing offset_31_16
713 // by 1. offset_31_16 can only be incremented by 1 if it's not 0x7FFF. If it is
714 // 0x7FFF, adding 1 will overflow the positive offset into the negative range.
715 // Therefore, the long branch range is something like from PC - 0x80000000 to
716 // PC + 0x7FFF7FFF, IOW, shorter by 32KB on one side.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200717 //
718 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
719 // case with the addiu instruction and a 16 bit offset.
720 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
721
722 // Resolve a branch when the target is known.
723 void Resolve(uint32_t target);
724
725 // Relocate a branch by a given delta if needed due to expansion of this or another
726 // branch at a given location by this delta (just changes location_ and target_).
727 void Relocate(uint32_t expand_location, uint32_t delta);
728
729 // If the branch is short, changes its type to long.
730 void PromoteToLong();
731
732 // If necessary, updates the type by promoting a short branch to a long branch
733 // based on the branch location and target. Returns the amount (in bytes) by
734 // which the branch size has increased.
735 // max_short_distance caps the maximum distance between location_ and target_
736 // that is allowed for short branches. This is for debugging/testing purposes.
737 // max_short_distance = 0 forces all short branches to become long.
738 // Use the implicit default argument when not debugging/testing.
739 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
740
741 // Returns the location of the instruction(s) containing the offset.
742 uint32_t GetOffsetLocation() const;
743
744 // Calculates and returns the offset ready for encoding in the branch instruction(s).
745 uint32_t GetOffset() const;
746
747 private:
748 // Completes branch construction by determining and recording its type.
749 void InitializeType(bool is_call, bool is_r6);
750 // Helper for the above.
751 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
752
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800753 uint32_t old_location_; // Offset into assembler buffer in bytes.
754 uint32_t location_; // Offset into assembler buffer in bytes.
755 uint32_t target_; // Offset into assembler buffer in bytes.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200756
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800757 uint32_t lhs_reg_; // Left-hand side register in conditional branches or
758 // indirect call register.
759 uint32_t rhs_reg_; // Right-hand side register in conditional branches.
760 BranchCondition condition_; // Condition for conditional branches.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200761
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800762 Type type_; // Current type of the branch.
763 Type old_type_; // Initial type of the branch.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200764 };
765 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
766 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
767
jeffhao7fbee072012-08-24 17:56:54 -0700768 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
769 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200770 void EmitI21(int opcode, Register rs, uint32_t imm21);
771 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700772 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
773 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800774 void EmitBcondR2(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
775 void EmitBcondR6(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21);
jeffhao7fbee072012-08-24 17:56:54 -0700776
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200777 void Buncond(MipsLabel* label);
778 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
779 void Call(MipsLabel* label, Register indirect_reg);
780 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700781
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200782 Branch* GetBranch(uint32_t branch_id);
783 const Branch* GetBranch(uint32_t branch_id) const;
784
785 void PromoteBranches();
786 void EmitBranch(Branch* branch);
787 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100788 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200789
790 // Emits exception block.
791 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
792
793 bool IsR6() const {
794 if (isa_features_ != nullptr) {
795 return isa_features_->IsR6();
796 } else {
797 return false;
798 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000799 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200800
801 bool Is32BitFPU() const {
802 if (isa_features_ != nullptr) {
803 return isa_features_->Is32BitFloatingPoint();
804 } else {
805 return true;
806 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000807 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200808
809 // List of exception blocks to generate at the end of the code cache.
810 std::vector<MipsExceptionSlowPath> exception_blocks_;
811
812 std::vector<Branch> branches_;
813
814 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
815 bool overwriting_;
816 // The current overwrite location.
817 uint32_t overwrite_location_;
818
819 // Data for AdjustedPosition(), see the description there.
820 uint32_t last_position_adjustment_;
821 uint32_t last_old_position_;
822 uint32_t last_branch_id_;
823
824 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000825
jeffhao7fbee072012-08-24 17:56:54 -0700826 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
827};
828
jeffhao7fbee072012-08-24 17:56:54 -0700829} // namespace mips
830} // namespace art
831
Ian Rogers166db042013-07-26 12:05:57 -0700832#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_