xref: /aosp_15_r20/art/compiler/utils/arm64/jni_macro_assembler_arm64.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2016 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_ARM64_JNI_MACRO_ASSEMBLER_ARM64_H_
18 #define ART_COMPILER_UTILS_ARM64_JNI_MACRO_ASSEMBLER_ARM64_H_
19 
20 #include <stdint.h>
21 #include <memory>
22 #include <vector>
23 
24 #include <android-base/logging.h>
25 
26 #include "assembler_arm64.h"
27 #include "base/arena_containers.h"
28 #include "base/macros.h"
29 #include "base/pointer_size.h"
30 #include "offsets.h"
31 #include "utils/assembler.h"
32 #include "utils/jni_macro_assembler.h"
33 
34 // TODO(VIXL): Make VIXL compile cleanly with -Wshadow, -Wdeprecated-declarations.
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wshadow"
37 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #include "aarch64/macro-assembler-aarch64.h"
39 #pragma GCC diagnostic pop
40 
41 namespace art HIDDEN {
42 namespace arm64 {
43 
44 class Arm64JNIMacroAssembler final : public JNIMacroAssemblerFwd<Arm64Assembler, PointerSize::k64> {
45  public:
Arm64JNIMacroAssembler(ArenaAllocator * allocator)46   explicit Arm64JNIMacroAssembler(ArenaAllocator* allocator)
47       : JNIMacroAssemblerFwd(allocator) {}
48 
49   ~Arm64JNIMacroAssembler();
50 
51   // Finalize the code.
52   void FinalizeCode() override;
53 
54   // Emit code that will create an activation on the stack.
55   void BuildFrame(size_t frame_size,
56                   ManagedRegister method_reg,
57                   ArrayRef<const ManagedRegister> callee_save_regs) override;
58 
59   // Emit code that will remove an activation from the stack.
60   void RemoveFrame(size_t frame_size,
61                    ArrayRef<const ManagedRegister> callee_save_regs,
62                    bool may_suspend) override;
63 
64   void IncreaseFrameSize(size_t adjust) override;
65   void DecreaseFrameSize(size_t adjust) override;
66 
67   ManagedRegister CoreRegisterWithSize(ManagedRegister src, size_t size) override;
68 
69   // Store routines.
70   void Store(FrameOffset offs, ManagedRegister src, size_t size) override;
71   void Store(ManagedRegister base, MemberOffset offs, ManagedRegister src, size_t size) override;
72   void StoreRawPtr(FrameOffset dest, ManagedRegister src) override;
73   void StoreStackPointerToThread(ThreadOffset64 thr_offs, bool tag_sp) override;
74 
75   // Load routines.
76   void Load(ManagedRegister dest, FrameOffset src, size_t size) override;
77   void Load(ManagedRegister dest, ManagedRegister base, MemberOffset offs, size_t size) override;
78   void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset64 offs) override;
79 
80   // Copying routines.
81   void MoveArguments(ArrayRef<ArgumentLocation> dests,
82                      ArrayRef<ArgumentLocation> srcs,
83                      ArrayRef<FrameOffset> refs) override;
84   void Move(ManagedRegister dest, ManagedRegister src, size_t size) override;
85   void Move(ManagedRegister dest, size_t value) override;
86 
87   // Sign extension.
88   void SignExtend(ManagedRegister mreg, size_t size) override;
89 
90   // Zero extension.
91   void ZeroExtend(ManagedRegister mreg, size_t size) override;
92 
93   // Exploit fast access in managed code to Thread::Current().
94   void GetCurrentThread(ManagedRegister dest) override;
95   void GetCurrentThread(FrameOffset dest_offset) override;
96 
97   // Manipulating local reference table states.
98   void LoadLocalReferenceTableStates(ManagedRegister jni_env_reg,
99                                      ManagedRegister previous_state_reg,
100                                      ManagedRegister current_state_reg) override;
101   void StoreLocalReferenceTableStates(ManagedRegister jni_env_reg,
102                                       ManagedRegister previous_state_reg,
103                                       ManagedRegister current_state_reg) override;
104 
105   // Decode JNI transition or local `jobject`. For (weak) global `jobject`, jump to slow path.
106   void DecodeJNITransitionOrLocalJObject(ManagedRegister reg,
107                                          JNIMacroLabel* slow_path,
108                                          JNIMacroLabel* resume) override;
109 
110   // Heap::VerifyObject on src. In some cases (such as a reference to this) we
111   // know that src may not be null.
112   void VerifyObject(ManagedRegister src, bool could_be_null) override;
113   void VerifyObject(FrameOffset src, bool could_be_null) override;
114 
115   // Jump to address held at [base+offset] (used for tail calls).
116   void Jump(ManagedRegister base, Offset offset) override;
117 
118   // Call to address held at [base+offset].
119   void Call(ManagedRegister base, Offset offset) override;
120   void CallFromThread(ThreadOffset64 offset) override;
121 
122   // Generate fast-path for transition to Native. Go to `label` if any thread flag is set.
123   // The implementation can use `scratch_regs` which should be callee save core registers
124   // (already saved before this call) and must preserve all argument registers.
125   void TryToTransitionFromRunnableToNative(
126       JNIMacroLabel* label, ArrayRef<const ManagedRegister> scratch_regs) override;
127 
128   // Generate fast-path for transition to Runnable. Go to `label` if any thread flag is set.
129   // The implementation can use `scratch_regs` which should be core argument registers
130   // not used as return registers and it must preserve the `return_reg` if any.
131   void TryToTransitionFromNativeToRunnable(JNIMacroLabel* label,
132                                            ArrayRef<const ManagedRegister> scratch_regs,
133                                            ManagedRegister return_reg) override;
134 
135   // Generate suspend check and branch to `label` if there is a pending suspend request.
136   void SuspendCheck(JNIMacroLabel* label) override;
137 
138   // Generate code to check if Thread::Current()->exception_ is non-null
139   // and branch to the `label` if it is.
140   void ExceptionPoll(JNIMacroLabel* label) override;
141   // Deliver pending exception.
142   void DeliverPendingException() override;
143 
144   // Create a new label that can be used with Jump/Bind calls.
145   std::unique_ptr<JNIMacroLabel> CreateLabel() override;
146   // Emit an unconditional jump to the label.
147   void Jump(JNIMacroLabel* label) override;
148   // Emit a conditional jump to the label by applying a unary condition test to the GC marking flag.
149   void TestGcMarking(JNIMacroLabel* label, JNIMacroUnaryCondition cond) override;
150   // Emit a conditional jump to the label by applying a unary condition test to object's mark bit.
151   void TestMarkBit(ManagedRegister ref, JNIMacroLabel* label, JNIMacroUnaryCondition cond) override;
152   // Emit a conditional jump to label if the loaded value from specified locations is not zero.
153   void TestByteAndJumpIfNotZero(uintptr_t address, JNIMacroLabel* label) override;
154   // Code at this offset will serve as the target for the Jump call.
155   void Bind(JNIMacroLabel* label) override;
156 
157  private:
158   void StoreWToOffset(StoreOperandType type,
159                       WRegister source,
160                       XRegister base,
161                       int32_t offset);
162   void StoreToOffset(XRegister source, XRegister base, int32_t offset);
163   void StoreSToOffset(SRegister source, XRegister base, int32_t offset);
164   void StoreDToOffset(DRegister source, XRegister base, int32_t offset);
165 
166   void LoadImmediate(XRegister dest,
167                      int32_t value,
168                      vixl::aarch64::Condition cond = vixl::aarch64::al);
169   void Load(Arm64ManagedRegister dst, XRegister src, int32_t src_offset, size_t size);
170   void LoadWFromOffset(LoadOperandType type,
171                        WRegister dest,
172                        XRegister base,
173                        int32_t offset);
174   void LoadFromOffset(XRegister dest, XRegister base, int32_t offset);
175   void LoadSFromOffset(SRegister dest, XRegister base, int32_t offset);
176   void LoadDFromOffset(DRegister dest, XRegister base, int32_t offset);
177 
178   void Copy(FrameOffset dest, FrameOffset src, size_t size);
179 
180   // Set up `out_reg` to hold a `jobject` (`StackReference<Object>*` to a spilled value),
181   // or to be null if the value is null and `null_allowed`. `in_reg` holds a possibly
182   // stale reference that can be used to avoid loading the spilled value to
183   // see if the value is null.
184   void CreateJObject(ManagedRegister out_reg,
185                      FrameOffset spilled_reference_offset,
186                      ManagedRegister in_reg,
187                      bool null_allowed);
188 
189   // Set up `out_off` to hold a `jobject` (`StackReference<Object>*` to a spilled value),
190   // or to be null if the value is null and `null_allowed`.
191   void CreateJObject(FrameOffset out_off,
192                      FrameOffset spilled_reference_offset,
193                      bool null_allowed);
194 
195   void AddConstant(XRegister rd,
196                    int32_t value,
197                    vixl::aarch64::Condition cond = vixl::aarch64::al);
198   void AddConstant(XRegister rd,
199                    XRegister rn,
200                    int32_t value,
201                    vixl::aarch64::Condition cond = vixl::aarch64::al);
202 };
203 
204 class Arm64JNIMacroLabel final
205     : public JNIMacroLabelCommon<Arm64JNIMacroLabel,
206                                  vixl::aarch64::Label,
207                                  InstructionSet::kArm64> {
208  public:
AsArm64()209   vixl::aarch64::Label* AsArm64() {
210     return AsPlatformLabel();
211   }
212 };
213 
214 }  // namespace arm64
215 }  // namespace art
216 
217 #endif  // ART_COMPILER_UTILS_ARM64_JNI_MACRO_ASSEMBLER_ARM64_H_
218