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