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_64_JNI_MACRO_ASSEMBLER_X86_64_H_
18 #define ART_COMPILER_UTILS_X86_64_JNI_MACRO_ASSEMBLER_X86_64_H_
19 
20 #include <vector>
21 
22 #include "assembler_x86_64.h"
23 #include "base/arena_containers.h"
24 #include "base/array_ref.h"
25 #include "base/enums.h"
26 #include "base/macros.h"
27 #include "offsets.h"
28 #include "utils/assembler.h"
29 #include "utils/jni_macro_assembler.h"
30 
31 namespace art {
32 namespace x86_64 {
33 
34 class X86_64JNIMacroAssembler FINAL : public JNIMacroAssemblerFwd<X86_64Assembler,
35                                                                   PointerSize::k64> {
36  public:
X86_64JNIMacroAssembler(ArenaAllocator * allocator)37   explicit X86_64JNIMacroAssembler(ArenaAllocator* allocator)
38       : JNIMacroAssemblerFwd<X86_64Assembler, PointerSize::k64>(allocator) {}
~X86_64JNIMacroAssembler()39   virtual ~X86_64JNIMacroAssembler() {}
40 
41   //
42   // Overridden common assembler high-level functionality
43   //
44 
45   // Emit code that will create an activation on the stack
46   void BuildFrame(size_t frame_size,
47                   ManagedRegister method_reg,
48                   ArrayRef<const ManagedRegister> callee_save_regs,
49                   const ManagedRegisterEntrySpills& entry_spills) OVERRIDE;
50 
51   // Emit code that will remove an activation from the stack
52   void RemoveFrame(size_t frame_size,
53                    ArrayRef<const ManagedRegister> callee_save_regs,
54                    bool may_suspend) OVERRIDE;
55 
56   void IncreaseFrameSize(size_t adjust) OVERRIDE;
57   void DecreaseFrameSize(size_t adjust) OVERRIDE;
58 
59   // Store routines
60   void Store(FrameOffset offs, ManagedRegister src, size_t size) OVERRIDE;
61   void StoreRef(FrameOffset dest, ManagedRegister src) OVERRIDE;
62   void StoreRawPtr(FrameOffset dest, ManagedRegister src) OVERRIDE;
63 
64   void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister scratch) OVERRIDE;
65 
66   void StoreStackOffsetToThread(ThreadOffset64 thr_offs,
67                                 FrameOffset fr_offs,
68                                 ManagedRegister scratch) OVERRIDE;
69 
70   void StoreStackPointerToThread(ThreadOffset64 thr_offs) OVERRIDE;
71 
72   void StoreSpanning(FrameOffset dest,
73                      ManagedRegister src,
74                      FrameOffset in_off,
75                      ManagedRegister scratch) OVERRIDE;
76 
77   // Load routines
78   void Load(ManagedRegister dest, FrameOffset src, size_t size) OVERRIDE;
79 
80   void LoadFromThread(ManagedRegister dest, ThreadOffset64 src, size_t size) OVERRIDE;
81 
82   void LoadRef(ManagedRegister dest, FrameOffset  src) OVERRIDE;
83 
84   void LoadRef(ManagedRegister dest,
85                ManagedRegister base,
86                MemberOffset offs,
87                bool unpoison_reference) OVERRIDE;
88 
89   void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) OVERRIDE;
90 
91   void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset64 offs) OVERRIDE;
92 
93   // Copying routines
94   void Move(ManagedRegister dest, ManagedRegister src, size_t size);
95 
96   void CopyRawPtrFromThread(FrameOffset fr_offs,
97                             ThreadOffset64 thr_offs,
98                             ManagedRegister scratch) OVERRIDE;
99 
100   void CopyRawPtrToThread(ThreadOffset64 thr_offs, FrameOffset fr_offs, ManagedRegister scratch)
101       OVERRIDE;
102 
103   void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister scratch) OVERRIDE;
104 
105   void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) OVERRIDE;
106 
107   void Copy(FrameOffset dest,
108             ManagedRegister src_base,
109             Offset src_offset,
110             ManagedRegister scratch,
111             size_t size) OVERRIDE;
112 
113   void Copy(ManagedRegister dest_base,
114             Offset dest_offset,
115             FrameOffset src,
116             ManagedRegister scratch,
117             size_t size) OVERRIDE;
118 
119   void Copy(FrameOffset dest,
120             FrameOffset src_base,
121             Offset src_offset,
122             ManagedRegister scratch,
123             size_t size) OVERRIDE;
124 
125   void Copy(ManagedRegister dest,
126             Offset dest_offset,
127             ManagedRegister src,
128             Offset src_offset,
129             ManagedRegister scratch,
130             size_t size) OVERRIDE;
131 
132   void Copy(FrameOffset dest,
133             Offset dest_offset,
134             FrameOffset src,
135             Offset src_offset,
136             ManagedRegister scratch,
137             size_t size) OVERRIDE;
138 
139   void MemoryBarrier(ManagedRegister) OVERRIDE;
140 
141   // Sign extension
142   void SignExtend(ManagedRegister mreg, size_t size) OVERRIDE;
143 
144   // Zero extension
145   void ZeroExtend(ManagedRegister mreg, size_t size) OVERRIDE;
146 
147   // Exploit fast access in managed code to Thread::Current()
148   void GetCurrentThread(ManagedRegister tr) OVERRIDE;
149   void GetCurrentThread(FrameOffset dest_offset, ManagedRegister scratch) OVERRIDE;
150 
151   // Set up out_reg to hold a Object** into the handle scope, or to be null if the
152   // value is null and null_allowed. in_reg holds a possibly stale reference
153   // that can be used to avoid loading the handle scope entry to see if the value is
154   // null.
155   void CreateHandleScopeEntry(ManagedRegister out_reg,
156                               FrameOffset handlescope_offset,
157                               ManagedRegister in_reg,
158                               bool null_allowed) OVERRIDE;
159 
160   // Set up out_off to hold a Object** into the handle scope, or to be null if the
161   // value is null and null_allowed.
162   void CreateHandleScopeEntry(FrameOffset out_off,
163                               FrameOffset handlescope_offset,
164                               ManagedRegister scratch,
165                               bool null_allowed) OVERRIDE;
166 
167   // src holds a handle scope entry (Object**) load this into dst
168   virtual void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) OVERRIDE;
169 
170   // Heap::VerifyObject on src. In some cases (such as a reference to this) we
171   // know that src may not be null.
172   void VerifyObject(ManagedRegister src, bool could_be_null) OVERRIDE;
173   void VerifyObject(FrameOffset src, bool could_be_null) OVERRIDE;
174 
175   // Call to address held at [base+offset]
176   void Call(ManagedRegister base, Offset offset, ManagedRegister scratch) OVERRIDE;
177   void Call(FrameOffset base, Offset offset, ManagedRegister scratch) OVERRIDE;
178   void CallFromThread(ThreadOffset64 offset, ManagedRegister scratch) OVERRIDE;
179 
180   // Generate code to check if Thread::Current()->exception_ is non-null
181   // and branch to a ExceptionSlowPath if it is.
182   void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) OVERRIDE;
183 
184   // Create a new label that can be used with Jump/Bind calls.
185   std::unique_ptr<JNIMacroLabel> CreateLabel() OVERRIDE;
186   // Emit an unconditional jump to the label.
187   void Jump(JNIMacroLabel* label) OVERRIDE;
188   // Emit a conditional jump to the label by applying a unary condition test to the register.
189   void Jump(JNIMacroLabel* label, JNIMacroUnaryCondition cond, ManagedRegister test) OVERRIDE;
190   // Code at this offset will serve as the target for the Jump call.
191   void Bind(JNIMacroLabel* label) OVERRIDE;
192 
193  private:
194   DISALLOW_COPY_AND_ASSIGN(X86_64JNIMacroAssembler);
195 };
196 
197 class X86_64JNIMacroLabel FINAL
198     : public JNIMacroLabelCommon<X86_64JNIMacroLabel,
199                                  art::Label,
200                                  InstructionSet::kX86_64> {
201  public:
AsX86_64()202   art::Label* AsX86_64() {
203     return AsPlatformLabel();
204   }
205 };
206 
207 }  // namespace x86_64
208 }  // namespace art
209 
210 #endif  // ART_COMPILER_UTILS_X86_64_JNI_MACRO_ASSEMBLER_X86_64_H_
211