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