blob: 79dd98708186484b9a5b6ffac937a2fdf9fe4028 [file] [log] [blame]
Andreas Gampe3b165bc2016-08-01 22:07:04 -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
17#ifndef ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_
18#define ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_
19
20#include <vector>
21
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
Andreas Gampe3b165bc2016-08-01 22:07:04 -070024#include "arch/instruction_set.h"
25#include "base/arena_allocator.h"
26#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070028#include "base/enums.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070029#include "base/macros.h"
30#include "managed_register.h"
31#include "offsets.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070032
Vladimir Marko176362a2022-11-08 11:47:50 +000033namespace art HIDDEN {
Andreas Gampe3b165bc2016-08-01 22:07:04 -070034
35class ArenaAllocator;
36class DebugFrameOpCodeWriterForAssembler;
37class InstructionSetFeatures;
38class MemoryRegion;
Igor Murashkinae7ff922016-10-06 14:59:19 -070039class JNIMacroLabel;
40
41enum class JNIMacroUnaryCondition {
42 kZero,
43 kNotZero
44};
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045
Vladimir Marko03008222020-03-06 14:04:21 +000046class ArgumentLocation {
47 public:
48 ArgumentLocation(ManagedRegister reg, size_t size)
49 : reg_(reg), frame_offset_(0u), size_(size) {
50 DCHECK(reg.IsRegister());
51 }
52
53 ArgumentLocation(FrameOffset frame_offset, size_t size)
54 : reg_(ManagedRegister::NoRegister()), frame_offset_(frame_offset), size_(size) {}
55
56 bool IsRegister() const {
57 return reg_.IsRegister();
58 }
59
60 ManagedRegister GetRegister() const {
61 DCHECK(IsRegister());
62 return reg_;
63 }
64
65 FrameOffset GetFrameOffset() const {
66 DCHECK(!IsRegister());
67 return frame_offset_;
68 }
69
70 size_t GetSize() const {
71 return size_;
72 }
73
74 private:
75 ManagedRegister reg_;
76 FrameOffset frame_offset_;
77 size_t size_;
78};
79
Andreas Gampe3b165bc2016-08-01 22:07:04 -070080template <PointerSize kPointerSize>
81class JNIMacroAssembler : public DeletableArenaObject<kArenaAllocAssembler> {
82 public:
83 static std::unique_ptr<JNIMacroAssembler<kPointerSize>> Create(
Vladimir Markoe764d2e2017-10-05 14:35:55 +010084 ArenaAllocator* allocator,
Andreas Gampe3b165bc2016-08-01 22:07:04 -070085 InstructionSet instruction_set,
86 const InstructionSetFeatures* instruction_set_features = nullptr);
87
88 // Finalize the code; emit slow paths, fixup branches, add literal pool, etc.
89 virtual void FinalizeCode() = 0;
90
91 // Size of generated code
92 virtual size_t CodeSize() const = 0;
93
94 // Copy instructions out of assembly buffer into the given region of memory
95 virtual void FinalizeInstructions(const MemoryRegion& region) = 0;
96
97 // Emit code that will create an activation on the stack
98 virtual void BuildFrame(size_t frame_size,
99 ManagedRegister method_reg,
Vladimir Marko662f12e2020-02-26 12:46:09 +0000100 ArrayRef<const ManagedRegister> callee_save_regs) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700101
102 // Emit code that will remove an activation from the stack
Roland Levillain0d127e12017-07-05 17:01:11 +0100103 //
104 // Argument `may_suspend` must be `true` if the compiled method may be
105 // suspended during its execution (otherwise `false`, if it is impossible
106 // to suspend during its execution).
107 virtual void RemoveFrame(size_t frame_size,
108 ArrayRef<const ManagedRegister> callee_save_regs,
109 bool may_suspend) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700110
111 virtual void IncreaseFrameSize(size_t adjust) = 0;
112 virtual void DecreaseFrameSize(size_t adjust) = 0;
113
Vladimir Markod95a1f22021-03-23 16:32:52 +0000114 // Return the same core register but with correct size if the architecture-specific
115 // ManagedRegister has different representation for different sizes.
116 virtual ManagedRegister CoreRegisterWithSize(ManagedRegister src, size_t size) = 0;
117
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700118 // Store routines
119 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
Vladimir Markod95a1f22021-03-23 16:32:52 +0000120 virtual void Store(ManagedRegister base, MemberOffset offs, ManagedRegister src, size_t size) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700121 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
122 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
123
Vladimir Marko662f12e2020-02-26 12:46:09 +0000124 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700125
126 virtual void StoreStackOffsetToThread(ThreadOffset<kPointerSize> thr_offs,
Vladimir Marko662f12e2020-02-26 12:46:09 +0000127 FrameOffset fr_offs) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700128
Mythri Allec2632ac2022-05-13 14:37:52 +0000129 // Stores stack pointer by tagging it if required so we can walk the stack. In debuggable runtimes
130 // we use tag to tell if we are using JITed code or AOT code. In non-debuggable runtimes we never
131 // use JITed code when AOT code is present. So checking for AOT code is sufficient to detect which
132 // code is being executed. We avoid tagging in non-debuggable runtimes to reduce instructions.
133 virtual void StoreStackPointerToThread(ThreadOffset<kPointerSize> thr_offs, bool tag_sp) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700134
135 virtual void StoreSpanning(FrameOffset dest,
136 ManagedRegister src,
Vladimir Marko662f12e2020-02-26 12:46:09 +0000137 FrameOffset in_off) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700138
139 // Load routines
140 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
Vladimir Markod95a1f22021-03-23 16:32:52 +0000141 virtual void Load(ManagedRegister dest, ManagedRegister base, MemberOffset offs, size_t size) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700142
143 virtual void LoadFromThread(ManagedRegister dest,
144 ThreadOffset<kPointerSize> src,
145 size_t size) = 0;
146
147 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
148 // If unpoison_reference is true and kPoisonReference is true, then we negate the read reference.
149 virtual void LoadRef(ManagedRegister dest,
150 ManagedRegister base,
151 MemberOffset offs,
152 bool unpoison_reference) = 0;
153
154 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) = 0;
155
156 virtual void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset<kPointerSize> offs) = 0;
157
158 // Copying routines
Vladimir Markod3aaf942021-11-02 10:51:57 +0000159
160 // Move arguments from `srcs` locations to `dests` locations.
161 //
162 // References shall be spilled to `refs` frame offsets (kInvalidReferenceOffset indicates
163 // a non-reference type) if they are in registers and corresponding `dests` shall be
164 // filled with `jobject` replacements. If the first argument is a reference, it is
165 // assumed to be `this` and cannot be null, all other reference arguments can be null.
166 virtual void MoveArguments(ArrayRef<ArgumentLocation> dests,
167 ArrayRef<ArgumentLocation> srcs,
168 ArrayRef<FrameOffset> refs) = 0;
Vladimir Marko03008222020-03-06 14:04:21 +0000169
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700170 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
171
Vladimir Marko662f12e2020-02-26 12:46:09 +0000172 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset<kPointerSize> thr_offs) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700173
174 virtual void CopyRawPtrToThread(ThreadOffset<kPointerSize> thr_offs,
175 FrameOffset fr_offs,
176 ManagedRegister scratch) = 0;
177
Vladimir Marko662f12e2020-02-26 12:46:09 +0000178 virtual void CopyRef(FrameOffset dest, FrameOffset src) = 0;
179 virtual void CopyRef(FrameOffset dest,
180 ManagedRegister base,
181 MemberOffset offs,
182 bool unpoison_reference) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700183
Vladimir Marko662f12e2020-02-26 12:46:09 +0000184 virtual void Copy(FrameOffset dest, FrameOffset src, size_t size) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700185
186 virtual void Copy(FrameOffset dest,
187 ManagedRegister src_base,
188 Offset src_offset,
189 ManagedRegister scratch,
190 size_t size) = 0;
191
192 virtual void Copy(ManagedRegister dest_base,
193 Offset dest_offset,
194 FrameOffset src,
195 ManagedRegister scratch,
196 size_t size) = 0;
197
198 virtual void Copy(FrameOffset dest,
199 FrameOffset src_base,
200 Offset src_offset,
201 ManagedRegister scratch,
202 size_t size) = 0;
203
204 virtual void Copy(ManagedRegister dest,
205 Offset dest_offset,
206 ManagedRegister src,
207 Offset src_offset,
208 ManagedRegister scratch,
209 size_t size) = 0;
210
211 virtual void Copy(FrameOffset dest,
212 Offset dest_offset,
213 FrameOffset src,
214 Offset src_offset,
215 ManagedRegister scratch,
216 size_t size) = 0;
217
Mythri Allebab6beb2022-10-21 13:28:05 +0000218 virtual void Move(ManagedRegister dst, size_t value) = 0;
219
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700220 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
221
222 // Sign extension
223 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
224
225 // Zero extension
226 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
227
228 // Exploit fast access in managed code to Thread::Current()
Vladimir Marko662f12e2020-02-26 12:46:09 +0000229 virtual void GetCurrentThread(ManagedRegister dest) = 0;
230 virtual void GetCurrentThread(FrameOffset dest_offset) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700231
Vladimir Markocedec9d2021-02-08 16:16:13 +0000232 // Set up `out_reg` to hold a `jobject` (`StackReference<Object>*` to a spilled value),
233 // or to be null if the value is null and `null_allowed`. `in_reg` holds a possibly
234 // stale reference that can be used to avoid loading the spilled value to
235 // see if the value is null.
236 virtual void CreateJObject(ManagedRegister out_reg,
237 FrameOffset spilled_reference_offset,
238 ManagedRegister in_reg,
239 bool null_allowed) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700240
Vladimir Markocedec9d2021-02-08 16:16:13 +0000241 // Set up `out_off` to hold a `jobject` (`StackReference<Object>*` to a spilled value),
242 // or to be null if the value is null and `null_allowed`.
243 virtual void CreateJObject(FrameOffset out_off,
244 FrameOffset spilled_reference_offset,
245 bool null_allowed) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700246
247 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
248 // know that src may not be null.
249 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
250 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
251
Vladimir Marko1c3c1062019-12-03 11:18:44 +0000252 // Jump to address held at [base+offset] (used for tail calls).
Vladimir Marko662f12e2020-02-26 12:46:09 +0000253 virtual void Jump(ManagedRegister base, Offset offset) = 0;
Vladimir Marko1c3c1062019-12-03 11:18:44 +0000254
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700255 // Call to address held at [base+offset]
Vladimir Marko662f12e2020-02-26 12:46:09 +0000256 virtual void Call(ManagedRegister base, Offset offset) = 0;
Vladimir Marko662f12e2020-02-26 12:46:09 +0000257 virtual void CallFromThread(ThreadOffset<kPointerSize> offset) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700258
Vladimir Markoce2a3442021-11-24 15:10:26 +0000259 // Generate fast-path for transition to Native. Go to `label` if any thread flag is set.
Vladimir Markoe74e0ce2021-12-08 14:16:21 +0000260 // The implementation can use `scratch_regs` which should be callee save core registers
261 // (already saved before this call) and must preserve all argument registers.
Vladimir Markoce2a3442021-11-24 15:10:26 +0000262 virtual void TryToTransitionFromRunnableToNative(
263 JNIMacroLabel* label, ArrayRef<const ManagedRegister> scratch_regs) = 0;
264
Vladimir Markoe74e0ce2021-12-08 14:16:21 +0000265 // Generate fast-path for transition to Runnable. Go to `label` if any thread flag is set.
266 // The implementation can use `scratch_regs` which should be core argument registers
267 // not used as return registers and it must preserve the `return_reg` if any.
268 virtual void TryToTransitionFromNativeToRunnable(JNIMacroLabel* label,
269 ArrayRef<const ManagedRegister> scratch_regs,
270 ManagedRegister return_reg) = 0;
271
Vladimir Marko46a89102021-10-21 13:05:46 +0000272 // Generate suspend check and branch to `label` if there is a pending suspend request.
273 virtual void SuspendCheck(JNIMacroLabel* label) = 0;
274
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700275 // Generate code to check if Thread::Current()->exception_ is non-null
Vladimir Markoc8c2bb62021-10-15 09:33:09 +0100276 // and branch to the `label` if it is.
277 virtual void ExceptionPoll(JNIMacroLabel* label) = 0;
278 // Deliver pending exception.
279 virtual void DeliverPendingException() = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700280
Igor Murashkinae7ff922016-10-06 14:59:19 -0700281 // Create a new label that can be used with Jump/Bind calls.
282 virtual std::unique_ptr<JNIMacroLabel> CreateLabel() = 0;
283 // Emit an unconditional jump to the label.
284 virtual void Jump(JNIMacroLabel* label) = 0;
Vladimir Marko662f12e2020-02-26 12:46:09 +0000285 // Emit a conditional jump to the label by applying a unary condition test to the GC marking flag.
286 virtual void TestGcMarking(JNIMacroLabel* label, JNIMacroUnaryCondition cond) = 0;
Vladimir Markoad333922021-11-02 10:51:57 +0000287 // Emit a conditional jump to the label by applying a unary condition test to object's mark bit.
288 virtual void TestMarkBit(ManagedRegister ref,
289 JNIMacroLabel* label,
290 JNIMacroUnaryCondition cond) = 0;
Mythri Alle5eb7ad22022-07-05 12:44:52 +0000291 // Emit a conditional jump to label if the loaded value from specified locations is not zero.
292 virtual void TestByteAndJumpIfNotZero(uintptr_t address, JNIMacroLabel* label) = 0;
Igor Murashkinae7ff922016-10-06 14:59:19 -0700293 // Code at this offset will serve as the target for the Jump call.
294 virtual void Bind(JNIMacroLabel* label) = 0;
295
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700296 virtual ~JNIMacroAssembler() {}
297
298 /**
299 * @brief Buffer of DWARF's Call Frame Information opcodes.
300 * @details It is used by debuggers and other tools to unwind the call stack.
301 */
302 virtual DebugFrameOpCodeWriterForAssembler& cfi() = 0;
303
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100304 void SetEmitRunTimeChecksInDebugMode(bool value) {
305 emit_run_time_checks_in_debug_mode_ = value;
306 }
307
Vladimir Markod3aaf942021-11-02 10:51:57 +0000308 static constexpr FrameOffset kInvalidReferenceOffset = FrameOffset(0);
309
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700310 protected:
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100311 JNIMacroAssembler() {}
312
313 // Should run-time checks be emitted in debug mode?
314 bool emit_run_time_checks_in_debug_mode_ = false;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700315};
316
Igor Murashkinae7ff922016-10-06 14:59:19 -0700317// A "Label" class used with the JNIMacroAssembler
318// allowing one to use branches (jumping from one place to another).
319//
320// This is just an interface, so every platform must provide
321// its own implementation of it.
322//
323// It is only safe to use a label created
324// via JNIMacroAssembler::CreateLabel with that same macro assembler.
325class JNIMacroLabel {
326 public:
327 virtual ~JNIMacroLabel() = 0;
328
329 const InstructionSet isa_;
330 protected:
331 explicit JNIMacroLabel(InstructionSet isa) : isa_(isa) {}
332};
333
334inline JNIMacroLabel::~JNIMacroLabel() {
335 // Compulsory definition for a pure virtual destructor
336 // to avoid linking errors.
337}
338
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700339template <typename T, PointerSize kPointerSize>
340class JNIMacroAssemblerFwd : public JNIMacroAssembler<kPointerSize> {
341 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100342 void FinalizeCode() override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700343 asm_.FinalizeCode();
344 }
345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100346 size_t CodeSize() const override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700347 return asm_.CodeSize();
348 }
349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100350 void FinalizeInstructions(const MemoryRegion& region) override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700351 asm_.FinalizeInstructions(region);
352 }
353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100354 DebugFrameOpCodeWriterForAssembler& cfi() override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700355 return asm_.cfi();
356 }
357
358 protected:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100359 explicit JNIMacroAssemblerFwd(ArenaAllocator* allocator) : asm_(allocator) {}
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700360
361 T asm_;
362};
363
Igor Murashkinae7ff922016-10-06 14:59:19 -0700364template <typename Self, typename PlatformLabel, InstructionSet kIsa>
365class JNIMacroLabelCommon : public JNIMacroLabel {
366 public:
367 static Self* Cast(JNIMacroLabel* label) {
368 CHECK(label != nullptr);
369 CHECK_EQ(kIsa, label->isa_);
370
371 return reinterpret_cast<Self*>(label);
372 }
373
374 protected:
375 PlatformLabel* AsPlatformLabel() {
376 return &label_;
377 }
378
379 JNIMacroLabelCommon() : JNIMacroLabel(kIsa) {
380 }
381
Roland Levillainf73caca2018-08-24 17:19:07 +0100382 ~JNIMacroLabelCommon() override {}
Igor Murashkinae7ff922016-10-06 14:59:19 -0700383
384 private:
385 PlatformLabel label_;
386};
387
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700388} // namespace art
389
390#endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_