blob: b8434b9263663fede6e58afc2261660bd9673fc5 [file] [log] [blame]
Serban Constantinescued8dd492014-02-11 14:15:10 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Roland Levillain4d027112015-07-01 15:41:14 +010013 * See the License for the specific language governing permissions and
Serban Constantinescued8dd492014-02-11 14:15:10 +000014 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_
18#define ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_
19
Stuart Monteithb95a5342014-03-12 13:32:32 +000020#include <stdint.h>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
22#include <vector>
Serban Constantinescued8dd492014-02-11 14:15:10 +000023
Vladimir Marko93205e32016-04-13 11:59:46 +010024#include "base/arena_containers.h"
Serban Constantinescued8dd492014-02-11 14:15:10 +000025#include "base/logging.h"
Serban Constantinescued8dd492014-02-11 14:15:10 +000026#include "utils/arm64/managed_register_arm64.h"
27#include "utils/assembler.h"
28#include "offsets.h"
Andreas Gampe277ccbd2014-11-03 21:36:10 -080029
Scott Wakeling97c72b72016-06-24 16:19:36 +010030// TODO: make vixl clean wrt -Wshadow, -Wunknown-pragmas, -Wmissing-noreturn
Andreas Gampe277ccbd2014-11-03 21:36:10 -080031#pragma GCC diagnostic push
Andreas Gampe65b798e2015-04-06 09:35:22 -070032#pragma GCC diagnostic ignored "-Wunknown-pragmas"
Andreas Gampe277ccbd2014-11-03 21:36:10 -080033#pragma GCC diagnostic ignored "-Wshadow"
Andreas Gampe65b798e2015-04-06 09:35:22 -070034#pragma GCC diagnostic ignored "-Wmissing-noreturn"
Scott Wakeling97c72b72016-06-24 16:19:36 +010035#include "a64/disasm-a64.h"
36#include "a64/macro-assembler-a64.h"
Andreas Gampe277ccbd2014-11-03 21:36:10 -080037#pragma GCC diagnostic pop
Serban Constantinescued8dd492014-02-11 14:15:10 +000038
39namespace art {
40namespace arm64 {
41
Scott Wakeling97c72b72016-06-24 16:19:36 +010042#define MEM_OP(...) vixl::aarch64::MemOperand(__VA_ARGS__)
Serban Constantinescued8dd492014-02-11 14:15:10 +000043
44enum LoadOperandType {
45 kLoadSignedByte,
46 kLoadUnsignedByte,
47 kLoadSignedHalfword,
48 kLoadUnsignedHalfword,
49 kLoadWord,
50 kLoadCoreWord,
51 kLoadSWord,
52 kLoadDWord
53};
54
55enum StoreOperandType {
56 kStoreByte,
57 kStoreHalfword,
58 kStoreWord,
59 kStoreCoreWord,
60 kStoreSWord,
61 kStoreDWord
62};
63
Alexandre Ramesc01a6642016-04-15 11:54:06 +010064class Arm64Exception {
65 private:
66 Arm64Exception(Arm64ManagedRegister scratch, size_t stack_adjust)
67 : scratch_(scratch), stack_adjust_(stack_adjust) {
68 }
69
Scott Wakeling97c72b72016-06-24 16:19:36 +010070 vixl::aarch64::Label* Entry() { return &exception_entry_; }
Alexandre Ramesc01a6642016-04-15 11:54:06 +010071
72 // Register used for passing Thread::Current()->exception_ .
73 const Arm64ManagedRegister scratch_;
74
75 // Stack adjust for ExceptionPool.
76 const size_t stack_adjust_;
77
Scott Wakeling97c72b72016-06-24 16:19:36 +010078 vixl::aarch64::Label exception_entry_;
Alexandre Ramesc01a6642016-04-15 11:54:06 +010079
80 friend class Arm64Assembler;
81 DISALLOW_COPY_AND_ASSIGN(Arm64Exception);
82};
Serban Constantinescued8dd492014-02-11 14:15:10 +000083
Ian Rogersdd7624d2014-03-14 17:43:00 -070084class Arm64Assembler FINAL : public Assembler {
Serban Constantinescued8dd492014-02-11 14:15:10 +000085 public:
Vladimir Marko93205e32016-04-13 11:59:46 +010086 explicit Arm64Assembler(ArenaAllocator* arena)
87 : Assembler(arena),
Alexandre Rames087930f2016-08-02 13:45:28 +010088 exception_blocks_(arena->Adapter(kArenaAllocAssembler)) {}
Serban Constantinescued8dd492014-02-11 14:15:10 +000089
Alexandre Rames087930f2016-08-02 13:45:28 +010090 virtual ~Arm64Assembler() {}
91
92 vixl::aarch64::MacroAssembler* GetVIXLAssembler() { return &vixl_masm_; }
Serban Constantinescued8dd492014-02-11 14:15:10 +000093
Vladimir Markocf93a5c2015-06-16 11:33:24 +000094 // Finalize the code.
95 void FinalizeCode() OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +000096
97 // Size of generated code.
Alexandre Rameseb7b7392015-06-19 14:47:01 +010098 size_t CodeSize() const OVERRIDE;
99 const uint8_t* CodeBufferBaseAddress() const OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000100
101 // Copy instructions out of assembly buffer into the given region of memory.
102 void FinalizeInstructions(const MemoryRegion& region);
103
Scott Wakeling97c72b72016-06-24 16:19:36 +0100104 void SpillRegisters(vixl::aarch64::CPURegList registers, int offset);
105 void UnspillRegisters(vixl::aarch64::CPURegList registers, int offset);
Zheng Xu69a50302015-04-14 20:04:41 +0800106
Serban Constantinescued8dd492014-02-11 14:15:10 +0000107 // Emit code that will create an activation on the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100108 void BuildFrame(size_t frame_size,
109 ManagedRegister method_reg,
110 ArrayRef<const ManagedRegister> callee_save_regs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700111 const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000112
113 // Emit code that will remove an activation from the stack.
Vladimir Marko32248382016-05-19 10:37:24 +0100114 void RemoveFrame(size_t frame_size, ArrayRef<const ManagedRegister> callee_save_regs)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700115 OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000116
Ian Rogersdd7624d2014-03-14 17:43:00 -0700117 void IncreaseFrameSize(size_t adjust) OVERRIDE;
118 void DecreaseFrameSize(size_t adjust) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000119
120 // Store routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700121 void Store(FrameOffset offs, ManagedRegister src, size_t size) OVERRIDE;
122 void StoreRef(FrameOffset dest, ManagedRegister src) OVERRIDE;
123 void StoreRawPtr(FrameOffset dest, ManagedRegister src) OVERRIDE;
124 void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister scratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700125 void StoreImmediateToThread64(ThreadOffset64 dest, uint32_t imm, ManagedRegister scratch)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700126 OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700127 void StoreStackOffsetToThread64(ThreadOffset64 thr_offs, FrameOffset fr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700128 ManagedRegister scratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700129 void StoreStackPointerToThread64(ThreadOffset64 thr_offs) OVERRIDE;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700130 void StoreSpanning(FrameOffset dest, ManagedRegister src, FrameOffset in_off,
131 ManagedRegister scratch) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000132
133 // Load routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700134 void Load(ManagedRegister dest, FrameOffset src, size_t size) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700135 void LoadFromThread64(ManagedRegister dest, ThreadOffset64 src, size_t size) OVERRIDE;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 void LoadRef(ManagedRegister dest, FrameOffset src) OVERRIDE;
137 void LoadRef(ManagedRegister dest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100138 bool unpoison_reference) OVERRIDE;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700139 void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700140 void LoadRawPtrFromThread64(ManagedRegister dest, ThreadOffset64 offs) OVERRIDE;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700141
Serban Constantinescued8dd492014-02-11 14:15:10 +0000142 // Copying routines.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700143 void Move(ManagedRegister dest, ManagedRegister src, size_t size) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700144 void CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset64 thr_offs,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700145 ManagedRegister scratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700146 void CopyRawPtrToThread64(ThreadOffset64 thr_offs, FrameOffset fr_offs, ManagedRegister scratch)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700147 OVERRIDE;
148 void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister scratch) OVERRIDE;
149 void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) OVERRIDE;
150 void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset, ManagedRegister scratch,
151 size_t size) OVERRIDE;
152 void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src, ManagedRegister scratch,
153 size_t size) OVERRIDE;
154 void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset, ManagedRegister scratch,
155 size_t size) OVERRIDE;
156 void Copy(ManagedRegister dest, Offset dest_offset, ManagedRegister src, Offset src_offset,
157 ManagedRegister scratch, size_t size) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000158 void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
Ian Rogersdd7624d2014-03-14 17:43:00 -0700159 ManagedRegister scratch, size_t size) OVERRIDE;
160 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000161
162 // Sign extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700163 void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000164
165 // Zero extension.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700166 void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000167
168 // Exploit fast access in managed code to Thread::Current().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700169 void GetCurrentThread(ManagedRegister tr) OVERRIDE;
170 void GetCurrentThread(FrameOffset dest_offset, ManagedRegister scratch) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000171
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
Serban Constantinescued8dd492014-02-11 14:15:10 +0000173 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700175 // null.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100176 void CreateHandleScopeEntry(ManagedRegister out_reg,
177 FrameOffset handlescope_offset,
178 ManagedRegister in_reg,
179 bool null_allowed) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000180
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700181 // Set up out_off to hold a Object** into the handle scope, or to be null if the
Serban Constantinescued8dd492014-02-11 14:15:10 +0000182 // value is null and null_allowed.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100183 void CreateHandleScopeEntry(FrameOffset out_off,
184 FrameOffset handlescope_offset,
185 ManagedRegister scratch,
186 bool null_allowed) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000187
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 // src holds a handle scope entry (Object**) load this into dst.
189 void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000190
191 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
192 // know that src may not be null.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700193 void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
194 void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000195
196 // Call to address held at [base+offset].
Ian Rogersdd7624d2014-03-14 17:43:00 -0700197 void Call(ManagedRegister base, Offset offset, ManagedRegister scratch) OVERRIDE;
198 void Call(FrameOffset base, Offset offset, ManagedRegister scratch) OVERRIDE;
Andreas Gampe542451c2016-07-26 09:02:02 -0700199 void CallFromThread64(ThreadOffset64 offset, ManagedRegister scratch) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000200
Andreas Gampec6ee54e2014-03-24 16:45:44 -0700201 // Jump to address (not setting link register)
202 void JumpTo(ManagedRegister m_base, Offset offs, ManagedRegister m_scratch);
203
Serban Constantinescued8dd492014-02-11 14:15:10 +0000204 // Generate code to check if Thread::Current()->exception_ is non-null
205 // and branch to a ExceptionSlowPath if it is.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700206 void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) OVERRIDE;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000207
Roland Levillain4d027112015-07-01 15:41:14 +0100208 //
209 // Heap poisoning.
210 //
211
212 // Poison a heap reference contained in `reg`.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100213 void PoisonHeapReference(vixl::aarch64::Register reg);
Roland Levillain4d027112015-07-01 15:41:14 +0100214 // Unpoison a heap reference contained in `reg`.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100215 void UnpoisonHeapReference(vixl::aarch64::Register reg);
Roland Levillain4d027112015-07-01 15:41:14 +0100216 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100217 void MaybeUnpoisonHeapReference(vixl::aarch64::Register reg);
Roland Levillain4d027112015-07-01 15:41:14 +0100218
Andreas Gampe85b62f22015-09-09 13:15:38 -0700219 void Bind(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
220 UNIMPLEMENTED(FATAL) << "Do not use Bind for ARM64";
221 }
222 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
223 UNIMPLEMENTED(FATAL) << "Do not use Jump for ARM64";
224 }
225
Serban Constantinescued8dd492014-02-11 14:15:10 +0000226 private:
Scott Wakeling97c72b72016-06-24 16:19:36 +0100227 static vixl::aarch64::Register reg_x(int code) {
Alexandre Rames37c92df2014-10-17 14:35:27 +0100228 CHECK(code < kNumberOfXRegisters) << code;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000229 if (code == SP) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100230 return vixl::aarch64::sp;
Serban Constantinescu15523732014-04-02 13:18:05 +0100231 } else if (code == XZR) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100232 return vixl::aarch64::xzr;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000233 }
Scott Wakeling97c72b72016-06-24 16:19:36 +0100234 return vixl::aarch64::Register::GetXRegFromCode(code);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000235 }
236
Scott Wakeling97c72b72016-06-24 16:19:36 +0100237 static vixl::aarch64::Register reg_w(int code) {
Alexandre Rames37c92df2014-10-17 14:35:27 +0100238 CHECK(code < kNumberOfWRegisters) << code;
Alexandre Ramesa304f972014-10-17 14:35:27 +0100239 if (code == WSP) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100240 return vixl::aarch64::wsp;
Alexandre Ramesa304f972014-10-17 14:35:27 +0100241 } else if (code == WZR) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100242 return vixl::aarch64::wzr;
Alexandre Ramesa304f972014-10-17 14:35:27 +0100243 }
Scott Wakeling97c72b72016-06-24 16:19:36 +0100244 return vixl::aarch64::Register::GetWRegFromCode(code);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000245 }
246
Scott Wakeling97c72b72016-06-24 16:19:36 +0100247 static vixl::aarch64::FPRegister reg_d(int code) {
248 return vixl::aarch64::FPRegister::GetDRegFromCode(code);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000249 }
250
Scott Wakeling97c72b72016-06-24 16:19:36 +0100251 static vixl::aarch64::FPRegister reg_s(int code) {
252 return vixl::aarch64::FPRegister::GetSRegFromCode(code);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000253 }
254
255 // Emits Exception block.
256 void EmitExceptionPoll(Arm64Exception *exception);
257
258 void StoreWToOffset(StoreOperandType type, WRegister source,
Alexandre Rames37c92df2014-10-17 14:35:27 +0100259 XRegister base, int32_t offset);
260 void StoreToOffset(XRegister source, XRegister base, int32_t offset);
261 void StoreSToOffset(SRegister source, XRegister base, int32_t offset);
262 void StoreDToOffset(DRegister source, XRegister base, int32_t offset);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000263
Scott Wakeling97c72b72016-06-24 16:19:36 +0100264 void LoadImmediate(XRegister dest,
265 int32_t value,
266 vixl::aarch64::Condition cond = vixl::aarch64::al);
Alexandre Rames37c92df2014-10-17 14:35:27 +0100267 void Load(Arm64ManagedRegister dst, XRegister src, int32_t src_offset, size_t size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100268 void LoadWFromOffset(LoadOperandType type,
269 WRegister dest,
270 XRegister base,
271 int32_t offset);
Alexandre Rames37c92df2014-10-17 14:35:27 +0100272 void LoadFromOffset(XRegister dest, XRegister base, int32_t offset);
273 void LoadSFromOffset(SRegister dest, XRegister base, int32_t offset);
274 void LoadDFromOffset(DRegister dest, XRegister base, int32_t offset);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100275 void AddConstant(XRegister rd,
276 int32_t value,
277 vixl::aarch64::Condition cond = vixl::aarch64::al);
278 void AddConstant(XRegister rd,
279 XRegister rn,
280 int32_t value,
281 vixl::aarch64::Condition cond = vixl::aarch64::al);
Serban Constantinescued8dd492014-02-11 14:15:10 +0000282
Serban Constantinescued8dd492014-02-11 14:15:10 +0000283 // List of exception blocks to generate at the end of the code cache.
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100284 ArenaVector<std::unique_ptr<Arm64Exception>> exception_blocks_;
Serban Constantinescu15523732014-04-02 13:18:05 +0100285
Alexandre Rames087930f2016-08-02 13:45:28 +0100286 // VIXL assembler.
287 vixl::aarch64::MacroAssembler vixl_masm_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100288
Serban Constantinescu15523732014-04-02 13:18:05 +0100289 // Used for testing.
290 friend class Arm64ManagedRegister_VixlRegisters_Test;
Serban Constantinescued8dd492014-02-11 14:15:10 +0000291};
292
Serban Constantinescued8dd492014-02-11 14:15:10 +0000293} // namespace arm64
294} // namespace art
295
296#endif // ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_