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_ARM_JNI_MACRO_ASSEMBLER_ARM_VIXL_H_
18 #define ART_COMPILER_UTILS_ARM_JNI_MACRO_ASSEMBLER_ARM_VIXL_H_
19 
20 #include <android-base/logging.h>
21 
22 #include "base/arena_containers.h"
23 #include "base/macros.h"
24 #include "constants_arm.h"
25 #include "offsets.h"
26 #include "utils/arm/assembler_arm_vixl.h"
27 #include "utils/arm/managed_register_arm.h"
28 #include "utils/assembler.h"
29 #include "utils/jni_macro_assembler.h"
30 
31 namespace art HIDDEN {
32 namespace arm {
33 
34 class ArmVIXLJNIMacroAssembler final
35     : public JNIMacroAssemblerFwd<ArmVIXLAssembler, PointerSize::k32> {
36  public:
ArmVIXLJNIMacroAssembler(ArenaAllocator * allocator)37   explicit ArmVIXLJNIMacroAssembler(ArenaAllocator* allocator)
38       : JNIMacroAssemblerFwd(allocator) {}
39 
~ArmVIXLJNIMacroAssembler()40   virtual ~ArmVIXLJNIMacroAssembler() {}
41   void FinalizeCode() override;
42 
43   //
44   // Overridden common assembler high-level functionality
45   //
46 
47   // Emit code that will create an activation on the stack.
48   void BuildFrame(size_t frame_size,
49                   ManagedRegister method_reg,
50                   ArrayRef<const ManagedRegister> callee_save_regs) override;
51 
52   // Emit code that will remove an activation from the stack.
53   void RemoveFrame(size_t frame_size,
54                    ArrayRef<const ManagedRegister> callee_save_regs,
55                    bool may_suspend) override;
56 
57   void IncreaseFrameSize(size_t adjust) override;
58   void DecreaseFrameSize(size_t adjust) override;
59 
60   ManagedRegister CoreRegisterWithSize(ManagedRegister src, size_t size) override;
61 
62   // Store routines.
63   void Store(FrameOffset offs, ManagedRegister src, size_t size) override;
64   void Store(ManagedRegister base, MemberOffset offs, ManagedRegister src, size_t size) override;
65   void StoreRawPtr(FrameOffset dest, ManagedRegister src) override;
66 
67   void StoreStackPointerToThread(ThreadOffset32 thr_offs, bool tag_sp) override;
68 
69   // Load routines.
70   void Load(ManagedRegister dest, FrameOffset src, size_t size) override;
71   void Load(ManagedRegister dest, ManagedRegister base, MemberOffset offs, size_t size) override;
72 
73   void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset32 offs) override;
74 
75   // Copying routines.
76   void MoveArguments(ArrayRef<ArgumentLocation> dests,
77                      ArrayRef<ArgumentLocation> srcs,
78                      ArrayRef<FrameOffset> refs) override;
79 
80   void Move(ManagedRegister dest, ManagedRegister src, size_t size) override;
81 
82   void Move(ManagedRegister dest, size_t value) override;
83 
84   // Sign extension.
85   void SignExtend(ManagedRegister mreg, size_t size) override;
86 
87   // Zero extension.
88   void ZeroExtend(ManagedRegister mreg, size_t size) override;
89 
90   // Exploit fast access in managed code to Thread::Current().
91   void GetCurrentThread(ManagedRegister dest) override;
92   void GetCurrentThread(FrameOffset dest_offset) override;
93 
94   // Manipulating local reference table states.
95   void LoadLocalReferenceTableStates(ManagedRegister jni_env_reg,
96                                      ManagedRegister previous_state_reg,
97                                      ManagedRegister current_state_reg) override;
98   void StoreLocalReferenceTableStates(ManagedRegister jni_env_reg,
99                                       ManagedRegister previous_state_reg,
100                                       ManagedRegister current_state_reg) override;
101 
102   // Decode JNI transition or local `jobject`. For (weak) global `jobject`, jump to slow path.
103   void DecodeJNITransitionOrLocalJObject(ManagedRegister reg,
104                                          JNIMacroLabel* slow_path,
105                                          JNIMacroLabel* resume) override;
106 
107   // Heap::VerifyObject on src. In some cases (such as a reference to this) we
108   // know that src may not be null.
109   void VerifyObject(ManagedRegister src, bool could_be_null) override;
110   void VerifyObject(FrameOffset src, bool could_be_null) override;
111 
112   // Jump to address held at [base+offset] (used for tail calls).
113   void Jump(ManagedRegister base, Offset offset) override;
114 
115   // Call to address held at [base+offset].
116   void Call(ManagedRegister base, Offset offset) override;
117   void CallFromThread(ThreadOffset32 offset) override;
118 
119   // Generate fast-path for transition to Native. Go to `label` if any thread flag is set.
120   // The implementation can use `scratch_regs` which should be callee save core registers
121   // (already saved before this call) and must preserve all argument registers.
122   void TryToTransitionFromRunnableToNative(
123       JNIMacroLabel* label, ArrayRef<const ManagedRegister> scratch_regs) override;
124 
125   // Generate fast-path for transition to Runnable. Go to `label` if any thread flag is set.
126   // The implementation can use `scratch_regs` which should be core argument registers
127   // not used as return registers and it must preserve the `return_reg` if any.
128   void TryToTransitionFromNativeToRunnable(JNIMacroLabel* label,
129                                            ArrayRef<const ManagedRegister> scratch_regs,
130                                            ManagedRegister return_reg) override;
131 
132   // Generate suspend check and branch to `label` if there is a pending suspend request.
133   void SuspendCheck(JNIMacroLabel* label) override;
134 
135   // Generate code to check if Thread::Current()->exception_ is non-null
136   // and branch to the `label` if it is.
137   void ExceptionPoll(JNIMacroLabel* label) override;
138   // Deliver pending exception.
139   void DeliverPendingException() override;
140 
141   // Create a new label that can be used with Jump/Bind calls.
142   std::unique_ptr<JNIMacroLabel> CreateLabel() override;
143   // Emit an unconditional jump to the label.
144   void Jump(JNIMacroLabel* label) override;
145   // Emit a conditional jump to the label by applying a unary condition test to the GC marking flag.
146   void TestGcMarking(JNIMacroLabel* label, JNIMacroUnaryCondition cond) override;
147   // Emit a conditional jump to the label by applying a unary condition test to object's mark bit.
148   void TestMarkBit(ManagedRegister ref, JNIMacroLabel* label, JNIMacroUnaryCondition cond) override;
149   // Emit a conditional jump to label if the loaded value from specified locations is not zero.
150   void TestByteAndJumpIfNotZero(uintptr_t address, JNIMacroLabel* label) override;
151   // Code at this offset will serve as the target for the Jump call.
152   void Bind(JNIMacroLabel* label) override;
153 
154  private:
155   void Copy(FrameOffset dest, FrameOffset src, size_t size);
156   void Load(ArmManagedRegister dest, vixl32::Register base, int32_t offset, size_t size);
157 
158   // Set up `out_reg` to hold a `jobject` (`StackReference<Object>*` to a spilled value),
159   // or to be null if the value is null and `null_allowed`. `in_reg` holds a possibly
160   // stale reference that can be used to avoid loading the spilled value to
161   // see if the value is null.
162   void CreateJObject(ManagedRegister out_reg,
163                      FrameOffset spilled_reference_offset,
164                      ManagedRegister in_reg,
165                      bool null_allowed);
166 
167   // Used for testing.
168   ART_FRIEND_TEST(ArmVIXLAssemblerTest, VixlJniHelpers);
169   ART_FRIEND_TEST(ArmVIXLAssemblerTest, VixlLoadFromOffset);
170   ART_FRIEND_TEST(ArmVIXLAssemblerTest, VixlStoreToOffset);
171 };
172 
173 class ArmVIXLJNIMacroLabel final
174     : public JNIMacroLabelCommon<ArmVIXLJNIMacroLabel,
175                                  vixl32::Label,
176                                  InstructionSet::kArm> {
177  public:
AsArm()178   vixl32::Label* AsArm() {
179     return AsPlatformLabel();
180   }
181 };
182 
183 }  // namespace arm
184 }  // namespace art
185 
186 #endif  // ART_COMPILER_UTILS_ARM_JNI_MACRO_ASSEMBLER_ARM_VIXL_H_
187