blob: 91ec8042521094161eff6a622523f0c54286644c [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
54class MipsLabel : public Label {
55 public:
56 MipsLabel() : prev_branch_id_plus_one_(0) {}
57
58 MipsLabel(MipsLabel&& src)
59 : Label(std::move(src)), prev_branch_id_plus_one_(src.prev_branch_id_plus_one_) {}
60
61 private:
62 uint32_t prev_branch_id_plus_one_; // To get distance from preceding branch, if any.
63
64 friend class MipsAssembler;
65 DISALLOW_COPY_AND_ASSIGN(MipsLabel);
66};
67
68// Slowpath entered when Thread::Current()->_exception is non-null.
69class MipsExceptionSlowPath {
70 public:
71 explicit MipsExceptionSlowPath(MipsManagedRegister scratch, size_t stack_adjust)
72 : scratch_(scratch), stack_adjust_(stack_adjust) {}
73
74 MipsExceptionSlowPath(MipsExceptionSlowPath&& src)
75 : scratch_(std::move(src.scratch_)),
76 stack_adjust_(std::move(src.stack_adjust_)),
77 exception_entry_(std::move(src.exception_entry_)) {}
78
79 private:
80 MipsLabel* Entry() { return &exception_entry_; }
81 const MipsManagedRegister scratch_;
82 const size_t stack_adjust_;
83 MipsLabel exception_entry_;
84
85 friend class MipsAssembler;
86 DISALLOW_COPY_AND_ASSIGN(MipsExceptionSlowPath);
jeffhao7fbee072012-08-24 17:56:54 -070087};
88
Ian Rogersdd7624d2014-03-14 17:43:00 -070089class MipsAssembler FINAL : public Assembler {
jeffhao7fbee072012-08-24 17:56:54 -070090 public:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020091 explicit MipsAssembler(const MipsInstructionSetFeatures* instruction_set_features = nullptr)
92 : overwriting_(false),
93 overwrite_location_(0),
94 last_position_adjustment_(0),
95 last_old_position_(0),
96 last_branch_id_(0),
Vladimir Marko10ef6942015-10-22 15:25:54 +010097 isa_features_(instruction_set_features) {
98 cfi().DelayEmittingAdvancePCs();
99 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200100
101 virtual ~MipsAssembler() {
102 for (auto& branch : branches_) {
103 CHECK(branch.IsResolved());
104 }
105 }
jeffhao7fbee072012-08-24 17:56:54 -0700106
107 // Emit Machine Instructions.
jeffhao7fbee072012-08-24 17:56:54 -0700108 void Addu(Register rd, Register rs, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700109 void Addiu(Register rt, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700110 void Subu(Register rd, Register rs, Register rt);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200111
112 void MultR2(Register rs, Register rt); // R2
113 void MultuR2(Register rs, Register rt); // R2
114 void DivR2(Register rs, Register rt); // R2
115 void DivuR2(Register rs, Register rt); // R2
116 void MulR2(Register rd, Register rs, Register rt); // R2
117 void DivR2(Register rd, Register rs, Register rt); // R2
118 void ModR2(Register rd, Register rs, Register rt); // R2
119 void DivuR2(Register rd, Register rs, Register rt); // R2
120 void ModuR2(Register rd, Register rs, Register rt); // R2
121 void MulR6(Register rd, Register rs, Register rt); // R6
122 void MuhuR6(Register rd, Register rs, Register rt); // R6
123 void DivR6(Register rd, Register rs, Register rt); // R6
124 void ModR6(Register rd, Register rs, Register rt); // R6
125 void DivuR6(Register rd, Register rs, Register rt); // R6
126 void ModuR6(Register rd, Register rs, Register rt); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700127
128 void And(Register rd, Register rs, Register rt);
129 void Andi(Register rt, Register rs, uint16_t imm16);
130 void Or(Register rd, Register rs, Register rt);
131 void Ori(Register rt, Register rs, uint16_t imm16);
132 void Xor(Register rd, Register rs, Register rt);
133 void Xori(Register rt, Register rs, uint16_t imm16);
134 void Nor(Register rd, Register rs, Register rt);
135
Chris Larsene3845472015-11-18 12:27:15 -0800136 void Movz(Register rd, Register rs, Register rt); // R2
137 void Movn(Register rd, Register rs, Register rt); // R2
138 void Seleqz(Register rd, Register rs, Register rt); // R6
139 void Selnez(Register rd, Register rs, Register rt); // R6
140 void ClzR6(Register rd, Register rs);
141 void ClzR2(Register rd, Register rs);
142 void CloR6(Register rd, Register rs);
143 void CloR2(Register rd, Register rs);
144
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200145 void Seb(Register rd, Register rt); // R2+
146 void Seh(Register rd, Register rt); // R2+
Chris Larsen3f8bf652015-10-28 10:08:56 -0700147 void Wsbh(Register rd, Register rt); // R2+
Chris Larsen70014c82015-11-18 12:26:08 -0800148 void Bitswap(Register rd, Register rt); // R6
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200149
150 void Sll(Register rd, Register rt, int shamt);
151 void Srl(Register rd, Register rt, int shamt);
Chris Larsen3f8bf652015-10-28 10:08:56 -0700152 void Rotr(Register rd, Register rt, int shamt); // R2+
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200153 void Sra(Register rd, Register rt, int shamt);
154 void Sllv(Register rd, Register rt, Register rs);
155 void Srlv(Register rd, Register rt, Register rs);
156 void Srav(Register rd, Register rt, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700157
158 void Lb(Register rt, Register rs, uint16_t imm16);
159 void Lh(Register rt, Register rs, uint16_t imm16);
160 void Lw(Register rt, Register rs, uint16_t imm16);
161 void Lbu(Register rt, Register rs, uint16_t imm16);
162 void Lhu(Register rt, Register rs, uint16_t imm16);
163 void Lui(Register rt, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200164 void Sync(uint32_t stype);
165 void Mfhi(Register rd); // R2
166 void Mflo(Register rd); // R2
jeffhao7fbee072012-08-24 17:56:54 -0700167
168 void Sb(Register rt, Register rs, uint16_t imm16);
169 void Sh(Register rt, Register rs, uint16_t imm16);
170 void Sw(Register rt, Register rs, uint16_t imm16);
171
172 void Slt(Register rd, Register rs, Register rt);
173 void Sltu(Register rd, Register rs, Register rt);
174 void Slti(Register rt, Register rs, uint16_t imm16);
175 void Sltiu(Register rt, Register rs, uint16_t imm16);
176
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200177 void B(uint16_t imm16);
178 void Beq(Register rs, Register rt, uint16_t imm16);
179 void Bne(Register rs, Register rt, uint16_t imm16);
180 void Beqz(Register rt, uint16_t imm16);
181 void Bnez(Register rt, uint16_t imm16);
182 void Bltz(Register rt, uint16_t imm16);
183 void Bgez(Register rt, uint16_t imm16);
184 void Blez(Register rt, uint16_t imm16);
185 void Bgtz(Register rt, uint16_t imm16);
186 void J(uint32_t addr26);
187 void Jal(uint32_t addr26);
188 void Jalr(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700189 void Jalr(Register rs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200190 void Jr(Register rs);
191 void Nal();
192 void Auipc(Register rs, uint16_t imm16); // R6
193 void Addiupc(Register rs, uint32_t imm19); // R6
194 void Bc(uint32_t imm26); // R6
195 void Jic(Register rt, uint16_t imm16); // R6
196 void Jialc(Register rt, uint16_t imm16); // R6
197 void Bltc(Register rs, Register rt, uint16_t imm16); // R6
198 void Bltzc(Register rt, uint16_t imm16); // R6
199 void Bgtzc(Register rt, uint16_t imm16); // R6
200 void Bgec(Register rs, Register rt, uint16_t imm16); // R6
201 void Bgezc(Register rt, uint16_t imm16); // R6
202 void Blezc(Register rt, uint16_t imm16); // R6
203 void Bltuc(Register rs, Register rt, uint16_t imm16); // R6
204 void Bgeuc(Register rs, Register rt, uint16_t imm16); // R6
205 void Beqc(Register rs, Register rt, uint16_t imm16); // R6
206 void Bnec(Register rs, Register rt, uint16_t imm16); // R6
207 void Beqzc(Register rs, uint32_t imm21); // R6
208 void Bnezc(Register rs, uint32_t imm21); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700209
210 void AddS(FRegister fd, FRegister fs, FRegister ft);
211 void SubS(FRegister fd, FRegister fs, FRegister ft);
212 void MulS(FRegister fd, FRegister fs, FRegister ft);
213 void DivS(FRegister fd, FRegister fs, FRegister ft);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200214 void AddD(FRegister fd, FRegister fs, FRegister ft);
215 void SubD(FRegister fd, FRegister fs, FRegister ft);
216 void MulD(FRegister fd, FRegister fs, FRegister ft);
217 void DivD(FRegister fd, FRegister fs, FRegister ft);
jeffhao7fbee072012-08-24 17:56:54 -0700218 void MovS(FRegister fd, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200219 void MovD(FRegister fd, FRegister fs);
220 void NegS(FRegister fd, FRegister fs);
221 void NegD(FRegister fd, FRegister fs);
222
223 void Cvtsw(FRegister fd, FRegister fs);
224 void Cvtdw(FRegister fd, FRegister fs);
225 void Cvtsd(FRegister fd, FRegister fs);
226 void Cvtds(FRegister fd, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700227
228 void Mfc1(Register rt, FRegister fs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200229 void Mtc1(Register rt, FRegister fs);
230 void Mfhc1(Register rt, FRegister fs);
231 void Mthc1(Register rt, FRegister fs);
jeffhao7fbee072012-08-24 17:56:54 -0700232 void Lwc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200233 void Ldc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700234 void Swc1(FRegister ft, Register rs, uint16_t imm16);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200235 void Sdc1(FRegister ft, Register rs, uint16_t imm16);
jeffhao7fbee072012-08-24 17:56:54 -0700236
237 void Break();
jeffhao07030602012-09-26 14:33:14 -0700238 void Nop();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200239 void Move(Register rd, Register rs);
240 void Clear(Register rd);
241 void Not(Register rd, Register rs);
jeffhao7fbee072012-08-24 17:56:54 -0700242
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200243 // Higher level composite instructions.
244 void LoadConst32(Register rd, int32_t value);
245 void LoadConst64(Register reg_hi, Register reg_lo, int64_t value);
246 void LoadDConst64(FRegister rd, int64_t value, Register temp);
247 void LoadSConst32(FRegister r, int32_t value, Register temp);
248 void StoreConst32ToOffset(int32_t value, Register base, int32_t offset, Register temp);
249 void StoreConst64ToOffset(int64_t value, Register base, int32_t offset, Register temp);
250 void Addiu32(Register rt, Register rs, int32_t value, Register rtmp = AT);
251
252 // These will generate R2 branches or R6 branches as appropriate.
253 void Bind(MipsLabel* label);
254 void B(MipsLabel* label);
255 void Jalr(MipsLabel* label, Register indirect_reg);
256 void Beq(Register rs, Register rt, MipsLabel* label);
257 void Bne(Register rs, Register rt, MipsLabel* label);
258 void Beqz(Register rt, MipsLabel* label);
259 void Bnez(Register rt, MipsLabel* label);
260 void Bltz(Register rt, MipsLabel* label);
261 void Bgez(Register rt, MipsLabel* label);
262 void Blez(Register rt, MipsLabel* label);
263 void Bgtz(Register rt, MipsLabel* label);
264 void Blt(Register rs, Register rt, MipsLabel* label);
265 void Bge(Register rs, Register rt, MipsLabel* label);
266 void Bltu(Register rs, Register rt, MipsLabel* label);
267 void Bgeu(Register rs, Register rt, MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700268
269 void EmitLoad(ManagedRegister m_dst, Register src_register, int32_t src_offset, size_t size);
270 void LoadFromOffset(LoadOperandType type, Register reg, Register base, int32_t offset);
271 void LoadSFromOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200272 void LoadDFromOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700273 void StoreToOffset(StoreOperandType type, Register reg, Register base, int32_t offset);
Goran Jakovljevicff734982015-08-24 12:58:55 +0000274 void StoreSToOffset(FRegister reg, Register base, int32_t offset);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200275 void StoreDToOffset(FRegister reg, Register base, int32_t offset);
jeffhao7fbee072012-08-24 17:56:54 -0700276
jeffhao7fbee072012-08-24 17:56:54 -0700277 // Emit data (e.g. encoded instruction or immediate) to the instruction stream.
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200278 void Emit(uint32_t value);
279
280 // Push/pop composite routines.
281 void Push(Register rs);
282 void Pop(Register rd);
283 void PopAndReturn(Register rd, Register rt);
jeffhao7fbee072012-08-24 17:56:54 -0700284
Andreas Gampe85b62f22015-09-09 13:15:38 -0700285 void Bind(Label* label) OVERRIDE {
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200286 Bind(down_cast<MipsLabel*>(label));
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200288 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
289 UNIMPLEMENTED(FATAL) << "Do not use Jump for MIPS";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700290 }
291
jeffhao7fbee072012-08-24 17:56:54 -0700292 //
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200293 // Overridden common assembler high-level functionality.
jeffhao7fbee072012-08-24 17:56:54 -0700294 //
295
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200296 // Emit code that will create an activation on the stack.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700297 void BuildFrame(size_t frame_size, ManagedRegister method_reg,
298 const std::vector<ManagedRegister>& callee_save_regs,
299 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700300
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200301 // Emit code that will remove an activation from the stack.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700302 void RemoveFrame(size_t frame_size, const std::vector<ManagedRegister>& callee_save_regs)
303 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700304
Ian Rogersdd7624d2014-03-14 17:43:00 -0700305 void IncreaseFrameSize(size_t adjust) OVERRIDE;
306 void DecreaseFrameSize(size_t adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700307
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200308 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700309 void Store(FrameOffset offs, ManagedRegister msrc, size_t size) OVERRIDE;
310 void StoreRef(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
311 void StoreRawPtr(FrameOffset dest, ManagedRegister msrc) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700312
Ian Rogersdd7624d2014-03-14 17:43:00 -0700313 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700314
Ian Rogersdd7624d2014-03-14 17:43:00 -0700315 void StoreImmediateToThread32(ThreadOffset<4> dest, uint32_t imm, ManagedRegister mscratch)
316 OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700317
Ian Rogersdd7624d2014-03-14 17:43:00 -0700318 void StoreStackOffsetToThread32(ThreadOffset<4> thr_offs, FrameOffset fr_offs,
319 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700320
Ian Rogersdd7624d2014-03-14 17:43:00 -0700321 void StoreStackPointerToThread32(ThreadOffset<4> thr_offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700322
Ian Rogersdd7624d2014-03-14 17:43:00 -0700323 void StoreSpanning(FrameOffset dest, ManagedRegister msrc, FrameOffset in_off,
324 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700325
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200326 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700327 void Load(ManagedRegister mdest, FrameOffset src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700328
Ian Rogersdd7624d2014-03-14 17:43:00 -0700329 void LoadFromThread32(ManagedRegister mdest, ThreadOffset<4> src, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700330
Mathieu Chartiere401d142015-04-22 13:56:20 -0700331 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700332
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 void LoadRef(ManagedRegister mdest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100334 bool unpoison_reference) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700335
Ian Rogersdd7624d2014-03-14 17:43:00 -0700336 void LoadRawPtr(ManagedRegister mdest, ManagedRegister base, Offset offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700337
Ian Rogersdd7624d2014-03-14 17:43:00 -0700338 void LoadRawPtrFromThread32(ManagedRegister mdest, ThreadOffset<4> offs) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700339
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200340 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700341 void Move(ManagedRegister mdest, ManagedRegister msrc, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700342
Ian Rogersdd7624d2014-03-14 17:43:00 -0700343 void CopyRawPtrFromThread32(FrameOffset fr_offs, ThreadOffset<4> thr_offs,
344 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700345
Ian Rogersdd7624d2014-03-14 17:43:00 -0700346 void CopyRawPtrToThread32(ThreadOffset<4> thr_offs, FrameOffset fr_offs,
347 ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700348
Ian Rogersdd7624d2014-03-14 17:43:00 -0700349 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700350
Ian Rogersdd7624d2014-03-14 17:43:00 -0700351 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700352
Ian Rogersdd7624d2014-03-14 17:43:00 -0700353 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister mscratch,
354 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700355
Ian Rogersdd7624d2014-03-14 17:43:00 -0700356 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
357 ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700358
Ian Rogersdd7624d2014-03-14 17:43:00 -0700359 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister mscratch,
360 size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700361
Ian Rogersdd7624d2014-03-14 17:43:00 -0700362 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
363 ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700364
Ian Rogersdd7624d2014-03-14 17:43:00 -0700365 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
366 ManagedRegister mscratch, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700367
Ian Rogersdd7624d2014-03-14 17:43:00 -0700368 void MemoryBarrier(ManagedRegister) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700369
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200370 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700371 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700372
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200373 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700374 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700375
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200376 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700377 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
378 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700379
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700380 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700381 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700382 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700383 // null.
384 void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
385 ManagedRegister in_reg, bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700386
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700387 // Set up out_off to hold a Object** into the handle scope, or to be null if the
jeffhao7fbee072012-08-24 17:56:54 -0700388 // value is null and null_allowed.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700389 void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset,
390 ManagedRegister mscratch, bool null_allowed) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700391
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200392 // src holds a handle scope entry (Object**) load this into dst.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700393 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700394
395 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
396 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700397 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
398 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700399
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200400 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700401 void Call(ManagedRegister base, Offset offset, ManagedRegister mscratch) OVERRIDE;
402 void Call(FrameOffset base, Offset offset, ManagedRegister mscratch) OVERRIDE;
403 void CallFromThread32(ThreadOffset<4> offset, ManagedRegister mscratch) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700404
jeffhao7fbee072012-08-24 17:56:54 -0700405 // Generate code to check if Thread::Current()->exception_ is non-null
406 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700407 void ExceptionPoll(ManagedRegister mscratch, size_t stack_adjust) OVERRIDE;
jeffhao7fbee072012-08-24 17:56:54 -0700408
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200409 // Emit slow paths queued during assembly and promote short branches to long if needed.
410 void FinalizeCode() OVERRIDE;
411
412 // Emit branches and finalize all instructions.
413 void FinalizeInstructions(const MemoryRegion& region);
414
415 // Returns the (always-)current location of a label (can be used in class CodeGeneratorMIPS,
416 // must be used instead of MipsLabel::GetPosition()).
417 uint32_t GetLabelLocation(MipsLabel* label) const;
418
419 // Get the final position of a label after local fixup based on the old position
420 // recorded before FinalizeCode().
421 uint32_t GetAdjustedPosition(uint32_t old_position);
422
423 enum BranchCondition {
424 kCondLT,
425 kCondGE,
426 kCondLE,
427 kCondGT,
428 kCondLTZ,
429 kCondGEZ,
430 kCondLEZ,
431 kCondGTZ,
432 kCondEQ,
433 kCondNE,
434 kCondEQZ,
435 kCondNEZ,
436 kCondLTU,
437 kCondGEU,
438 kUncond,
439 };
440 friend std::ostream& operator<<(std::ostream& os, const BranchCondition& rhs);
441
jeffhao7fbee072012-08-24 17:56:54 -0700442 private:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200443 class Branch {
444 public:
445 enum Type {
446 // R2 short branches.
447 kUncondBranch,
448 kCondBranch,
449 kCall,
450 // R2 long branches.
451 kLongUncondBranch,
452 kLongCondBranch,
453 kLongCall,
454 // R6 short branches.
455 kR6UncondBranch,
456 kR6CondBranch,
457 kR6Call,
458 // R6 long branches.
459 kR6LongUncondBranch,
460 kR6LongCondBranch,
461 kR6LongCall,
462 };
463 // Bit sizes of offsets defined as enums to minimize chance of typos.
464 enum OffsetBits {
465 kOffset16 = 16,
466 kOffset18 = 18,
467 kOffset21 = 21,
468 kOffset23 = 23,
469 kOffset28 = 28,
470 kOffset32 = 32,
471 };
472
473 static constexpr uint32_t kUnresolved = 0xffffffff; // Unresolved target_
474 static constexpr int32_t kMaxBranchLength = 32;
475 static constexpr int32_t kMaxBranchSize = kMaxBranchLength * sizeof(uint32_t);
476
477 struct BranchInfo {
478 // Branch length as a number of 4-byte-long instructions.
479 uint32_t length;
480 // Ordinal number (0-based) of the first (or the only) instruction that contains the branch's
481 // PC-relative offset (or its most significant 16-bit half, which goes first).
482 uint32_t instr_offset;
483 // Different MIPS instructions with PC-relative offsets apply said offsets to slightly
484 // different origins, e.g. to PC or PC+4. Encode the origin distance (as a number of 4-byte
485 // instructions) from the instruction containing the offset.
486 uint32_t pc_org;
487 // How large (in bits) a PC-relative offset can be for a given type of branch (kR6CondBranch
488 // is an exception: use kOffset23 for beqzc/bnezc).
489 OffsetBits offset_size;
490 // Some MIPS instructions with PC-relative offsets shift the offset by 2. Encode the shift
491 // count.
492 int offset_shift;
493 };
494 static const BranchInfo branch_info_[/* Type */];
495
496 // Unconditional branch.
497 Branch(bool is_r6, uint32_t location, uint32_t target);
498 // Conditional branch.
499 Branch(bool is_r6,
500 uint32_t location,
501 uint32_t target,
502 BranchCondition condition,
503 Register lhs_reg,
504 Register rhs_reg = ZERO);
505 // Call (branch and link) that stores the target address in a given register (i.e. T9).
506 Branch(bool is_r6, uint32_t location, uint32_t target, Register indirect_reg);
507
508 // Some conditional branches with lhs = rhs are effectively NOPs, while some
509 // others are effectively unconditional. MIPSR6 conditional branches require lhs != rhs.
510 // So, we need a way to identify such branches in order to emit no instructions for them
511 // or change them to unconditional.
512 static bool IsNop(BranchCondition condition, Register lhs, Register rhs);
513 static bool IsUncond(BranchCondition condition, Register lhs, Register rhs);
514
515 static BranchCondition OppositeCondition(BranchCondition cond);
516
517 Type GetType() const;
518 BranchCondition GetCondition() const;
519 Register GetLeftRegister() const;
520 Register GetRightRegister() const;
521 uint32_t GetTarget() const;
522 uint32_t GetLocation() const;
523 uint32_t GetOldLocation() const;
524 uint32_t GetLength() const;
525 uint32_t GetOldLength() const;
526 uint32_t GetSize() const;
527 uint32_t GetOldSize() const;
528 uint32_t GetEndLocation() const;
529 uint32_t GetOldEndLocation() const;
530 bool IsLong() const;
531 bool IsResolved() const;
532
533 // Returns the bit size of the signed offset that the branch instruction can handle.
534 OffsetBits GetOffsetSize() const;
535
536 // Calculates the distance between two byte locations in the assembler buffer and
537 // returns the number of bits needed to represent the distance as a signed integer.
538 //
539 // Branch instructions have signed offsets of 16, 19 (addiupc), 21 (beqzc/bnezc),
540 // and 26 (bc) bits, which are additionally shifted left 2 positions at run time.
541 //
542 // Composite branches (made of several instructions) with longer reach have 32-bit
543 // offsets encoded as 2 16-bit "halves" in two instructions (high half goes first).
544 // The composite branches cover the range of PC + +/-2GB.
545 //
546 // The returned values are therefore: 18, 21, 23, 28 and 32. There's also a special
547 // case with the addiu instruction and a 16 bit offset.
548 static OffsetBits GetOffsetSizeNeeded(uint32_t location, uint32_t target);
549
550 // Resolve a branch when the target is known.
551 void Resolve(uint32_t target);
552
553 // Relocate a branch by a given delta if needed due to expansion of this or another
554 // branch at a given location by this delta (just changes location_ and target_).
555 void Relocate(uint32_t expand_location, uint32_t delta);
556
557 // If the branch is short, changes its type to long.
558 void PromoteToLong();
559
560 // If necessary, updates the type by promoting a short branch to a long branch
561 // based on the branch location and target. Returns the amount (in bytes) by
562 // which the branch size has increased.
563 // max_short_distance caps the maximum distance between location_ and target_
564 // that is allowed for short branches. This is for debugging/testing purposes.
565 // max_short_distance = 0 forces all short branches to become long.
566 // Use the implicit default argument when not debugging/testing.
567 uint32_t PromoteIfNeeded(uint32_t max_short_distance = std::numeric_limits<uint32_t>::max());
568
569 // Returns the location of the instruction(s) containing the offset.
570 uint32_t GetOffsetLocation() const;
571
572 // Calculates and returns the offset ready for encoding in the branch instruction(s).
573 uint32_t GetOffset() const;
574
575 private:
576 // Completes branch construction by determining and recording its type.
577 void InitializeType(bool is_call, bool is_r6);
578 // Helper for the above.
579 void InitShortOrLong(OffsetBits ofs_size, Type short_type, Type long_type);
580
581 uint32_t old_location_; // Offset into assembler buffer in bytes.
582 uint32_t location_; // Offset into assembler buffer in bytes.
583 uint32_t target_; // Offset into assembler buffer in bytes.
584
585 uint32_t lhs_reg_ : 5; // Left-hand side register in conditional branches or
586 // indirect call register.
587 uint32_t rhs_reg_ : 5; // Right-hand side register in conditional branches.
588 BranchCondition condition_ : 5; // Condition for conditional branches.
589
590 Type type_ : 5; // Current type of the branch.
591 Type old_type_ : 5; // Initial type of the branch.
592 };
593 friend std::ostream& operator<<(std::ostream& os, const Branch::Type& rhs);
594 friend std::ostream& operator<<(std::ostream& os, const Branch::OffsetBits& rhs);
595
jeffhao7fbee072012-08-24 17:56:54 -0700596 void EmitR(int opcode, Register rs, Register rt, Register rd, int shamt, int funct);
597 void EmitI(int opcode, Register rs, Register rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200598 void EmitI21(int opcode, Register rs, uint32_t imm21);
599 void EmitI26(int opcode, uint32_t imm26);
jeffhao7fbee072012-08-24 17:56:54 -0700600 void EmitFR(int opcode, int fmt, FRegister ft, FRegister fs, FRegister fd, int funct);
601 void EmitFI(int opcode, int fmt, FRegister rt, uint16_t imm);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200602 void EmitBcond(BranchCondition cond, Register rs, Register rt, uint16_t imm16);
603 void EmitBcondc(BranchCondition cond, Register rs, Register rt, uint32_t imm16_21); // R6
jeffhao7fbee072012-08-24 17:56:54 -0700604
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200605 void Buncond(MipsLabel* label);
606 void Bcond(MipsLabel* label, BranchCondition condition, Register lhs, Register rhs = ZERO);
607 void Call(MipsLabel* label, Register indirect_reg);
608 void FinalizeLabeledBranch(MipsLabel* label);
jeffhao7fbee072012-08-24 17:56:54 -0700609
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200610 Branch* GetBranch(uint32_t branch_id);
611 const Branch* GetBranch(uint32_t branch_id) const;
612
613 void PromoteBranches();
614 void EmitBranch(Branch* branch);
615 void EmitBranches();
Vladimir Marko10ef6942015-10-22 15:25:54 +0100616 void PatchCFI(size_t number_of_delayed_adjust_pcs);
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200617
618 // Emits exception block.
619 void EmitExceptionPoll(MipsExceptionSlowPath* exception);
620
621 bool IsR6() const {
622 if (isa_features_ != nullptr) {
623 return isa_features_->IsR6();
624 } else {
625 return false;
626 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000627 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200628
629 bool Is32BitFPU() const {
630 if (isa_features_ != nullptr) {
631 return isa_features_->Is32BitFloatingPoint();
632 } else {
633 return true;
634 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000635 }
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200636
637 // List of exception blocks to generate at the end of the code cache.
638 std::vector<MipsExceptionSlowPath> exception_blocks_;
639
640 std::vector<Branch> branches_;
641
642 // Whether appending instructions at the end of the buffer or overwriting the existing ones.
643 bool overwriting_;
644 // The current overwrite location.
645 uint32_t overwrite_location_;
646
647 // Data for AdjustedPosition(), see the description there.
648 uint32_t last_position_adjustment_;
649 uint32_t last_old_position_;
650 uint32_t last_branch_id_;
651
652 const MipsInstructionSetFeatures* isa_features_;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000653
jeffhao7fbee072012-08-24 17:56:54 -0700654 DISALLOW_COPY_AND_ASSIGN(MipsAssembler);
655};
656
jeffhao7fbee072012-08-24 17:56:54 -0700657} // namespace mips
658} // namespace art
659
Ian Rogers166db042013-07-26 12:05:57 -0700660#endif // ART_COMPILER_UTILS_MIPS_ASSEMBLER_MIPS_H_