blob: bbe0f734ba86c903f64761448c418aea4989d473 [file] [log] [blame]
Andreas Gampe3b165bc2016-08-01 22:07:04 -07001/*
2 * Copyright (C) 2011 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_JNI_MACRO_ASSEMBLER_H_
18#define ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_
19
20#include <vector>
21
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
Andreas Gampe3b165bc2016-08-01 22:07:04 -070024#include "arch/instruction_set.h"
25#include "base/arena_allocator.h"
26#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070028#include "base/enums.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070029#include "base/macros.h"
30#include "managed_register.h"
31#include "offsets.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070032
33namespace art {
34
35class ArenaAllocator;
36class DebugFrameOpCodeWriterForAssembler;
37class InstructionSetFeatures;
38class MemoryRegion;
Igor Murashkinae7ff922016-10-06 14:59:19 -070039class JNIMacroLabel;
40
41enum class JNIMacroUnaryCondition {
42 kZero,
43 kNotZero
44};
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045
46template <PointerSize kPointerSize>
47class JNIMacroAssembler : public DeletableArenaObject<kArenaAllocAssembler> {
48 public:
49 static std::unique_ptr<JNIMacroAssembler<kPointerSize>> Create(
Vladimir Markoe764d2e2017-10-05 14:35:55 +010050 ArenaAllocator* allocator,
Andreas Gampe3b165bc2016-08-01 22:07:04 -070051 InstructionSet instruction_set,
52 const InstructionSetFeatures* instruction_set_features = nullptr);
53
54 // Finalize the code; emit slow paths, fixup branches, add literal pool, etc.
55 virtual void FinalizeCode() = 0;
56
57 // Size of generated code
58 virtual size_t CodeSize() const = 0;
59
60 // Copy instructions out of assembly buffer into the given region of memory
61 virtual void FinalizeInstructions(const MemoryRegion& region) = 0;
62
63 // Emit code that will create an activation on the stack
64 virtual void BuildFrame(size_t frame_size,
65 ManagedRegister method_reg,
66 ArrayRef<const ManagedRegister> callee_save_regs,
67 const ManagedRegisterEntrySpills& entry_spills) = 0;
68
69 // Emit code that will remove an activation from the stack
Roland Levillain0d127e12017-07-05 17:01:11 +010070 //
71 // Argument `may_suspend` must be `true` if the compiled method may be
72 // suspended during its execution (otherwise `false`, if it is impossible
73 // to suspend during its execution).
74 virtual void RemoveFrame(size_t frame_size,
75 ArrayRef<const ManagedRegister> callee_save_regs,
76 bool may_suspend) = 0;
Andreas Gampe3b165bc2016-08-01 22:07:04 -070077
78 virtual void IncreaseFrameSize(size_t adjust) = 0;
79 virtual void DecreaseFrameSize(size_t adjust) = 0;
80
81 // Store routines
82 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
83 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
84 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
85
86 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm, ManagedRegister scratch) = 0;
87
88 virtual void StoreStackOffsetToThread(ThreadOffset<kPointerSize> thr_offs,
89 FrameOffset fr_offs,
90 ManagedRegister scratch) = 0;
91
92 virtual void StoreStackPointerToThread(ThreadOffset<kPointerSize> thr_offs) = 0;
93
94 virtual void StoreSpanning(FrameOffset dest,
95 ManagedRegister src,
96 FrameOffset in_off,
97 ManagedRegister scratch) = 0;
98
99 // Load routines
100 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
101
102 virtual void LoadFromThread(ManagedRegister dest,
103 ThreadOffset<kPointerSize> src,
104 size_t size) = 0;
105
106 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
107 // If unpoison_reference is true and kPoisonReference is true, then we negate the read reference.
108 virtual void LoadRef(ManagedRegister dest,
109 ManagedRegister base,
110 MemberOffset offs,
111 bool unpoison_reference) = 0;
112
113 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) = 0;
114
115 virtual void LoadRawPtrFromThread(ManagedRegister dest, ThreadOffset<kPointerSize> offs) = 0;
116
117 // Copying routines
118 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
119
120 virtual void CopyRawPtrFromThread(FrameOffset fr_offs,
121 ThreadOffset<kPointerSize> thr_offs,
122 ManagedRegister scratch) = 0;
123
124 virtual void CopyRawPtrToThread(ThreadOffset<kPointerSize> thr_offs,
125 FrameOffset fr_offs,
126 ManagedRegister scratch) = 0;
127
128 virtual void CopyRef(FrameOffset dest, FrameOffset src, ManagedRegister scratch) = 0;
129
130 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
131
132 virtual void Copy(FrameOffset dest,
133 ManagedRegister src_base,
134 Offset src_offset,
135 ManagedRegister scratch,
136 size_t size) = 0;
137
138 virtual void Copy(ManagedRegister dest_base,
139 Offset dest_offset,
140 FrameOffset src,
141 ManagedRegister scratch,
142 size_t size) = 0;
143
144 virtual void Copy(FrameOffset dest,
145 FrameOffset src_base,
146 Offset src_offset,
147 ManagedRegister scratch,
148 size_t size) = 0;
149
150 virtual void Copy(ManagedRegister dest,
151 Offset dest_offset,
152 ManagedRegister src,
153 Offset src_offset,
154 ManagedRegister scratch,
155 size_t size) = 0;
156
157 virtual void Copy(FrameOffset dest,
158 Offset dest_offset,
159 FrameOffset src,
160 Offset src_offset,
161 ManagedRegister scratch,
162 size_t size) = 0;
163
164 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
165
166 // Sign extension
167 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
168
169 // Zero extension
170 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
171
172 // Exploit fast access in managed code to Thread::Current()
173 virtual void GetCurrentThread(ManagedRegister tr) = 0;
174 virtual void GetCurrentThread(FrameOffset dest_offset, ManagedRegister scratch) = 0;
175
176 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
177 // value is null and null_allowed. in_reg holds a possibly stale reference
178 // that can be used to avoid loading the handle scope entry to see if the value is
179 // null.
180 virtual void CreateHandleScopeEntry(ManagedRegister out_reg,
181 FrameOffset handlescope_offset,
182 ManagedRegister in_reg,
183 bool null_allowed) = 0;
184
185 // Set up out_off to hold a Object** into the handle scope, or to be null if the
186 // value is null and null_allowed.
187 virtual void CreateHandleScopeEntry(FrameOffset out_off,
188 FrameOffset handlescope_offset,
189 ManagedRegister scratch,
190 bool null_allowed) = 0;
191
192 // src holds a handle scope entry (Object**) load this into dst
193 virtual void LoadReferenceFromHandleScope(ManagedRegister dst, ManagedRegister src) = 0;
194
195 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
196 // know that src may not be null.
197 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
198 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
199
Vladimir Marko1c3c1062019-12-03 11:18:44 +0000200 // Jump to address held at [base+offset] (used for tail calls).
201 virtual void Jump(ManagedRegister base, Offset offset, ManagedRegister scratch) = 0;
202
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700203 // Call to address held at [base+offset]
204 virtual void Call(ManagedRegister base, Offset offset, ManagedRegister scratch) = 0;
205 virtual void Call(FrameOffset base, Offset offset, ManagedRegister scratch) = 0;
206 virtual void CallFromThread(ThreadOffset<kPointerSize> offset, ManagedRegister scratch) = 0;
207
208 // Generate code to check if Thread::Current()->exception_ is non-null
209 // and branch to a ExceptionSlowPath if it is.
210 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
211
Igor Murashkinae7ff922016-10-06 14:59:19 -0700212 // Create a new label that can be used with Jump/Bind calls.
213 virtual std::unique_ptr<JNIMacroLabel> CreateLabel() = 0;
214 // Emit an unconditional jump to the label.
215 virtual void Jump(JNIMacroLabel* label) = 0;
216 // Emit a conditional jump to the label by applying a unary condition test to the register.
217 virtual void Jump(JNIMacroLabel* label, JNIMacroUnaryCondition cond, ManagedRegister test) = 0;
218 // Code at this offset will serve as the target for the Jump call.
219 virtual void Bind(JNIMacroLabel* label) = 0;
220
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700221 virtual ~JNIMacroAssembler() {}
222
223 /**
224 * @brief Buffer of DWARF's Call Frame Information opcodes.
225 * @details It is used by debuggers and other tools to unwind the call stack.
226 */
227 virtual DebugFrameOpCodeWriterForAssembler& cfi() = 0;
228
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100229 void SetEmitRunTimeChecksInDebugMode(bool value) {
230 emit_run_time_checks_in_debug_mode_ = value;
231 }
232
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700233 protected:
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100234 JNIMacroAssembler() {}
235
236 // Should run-time checks be emitted in debug mode?
237 bool emit_run_time_checks_in_debug_mode_ = false;
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700238};
239
Igor Murashkinae7ff922016-10-06 14:59:19 -0700240// A "Label" class used with the JNIMacroAssembler
241// allowing one to use branches (jumping from one place to another).
242//
243// This is just an interface, so every platform must provide
244// its own implementation of it.
245//
246// It is only safe to use a label created
247// via JNIMacroAssembler::CreateLabel with that same macro assembler.
248class JNIMacroLabel {
249 public:
250 virtual ~JNIMacroLabel() = 0;
251
252 const InstructionSet isa_;
253 protected:
254 explicit JNIMacroLabel(InstructionSet isa) : isa_(isa) {}
255};
256
257inline JNIMacroLabel::~JNIMacroLabel() {
258 // Compulsory definition for a pure virtual destructor
259 // to avoid linking errors.
260}
261
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700262template <typename T, PointerSize kPointerSize>
263class JNIMacroAssemblerFwd : public JNIMacroAssembler<kPointerSize> {
264 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100265 void FinalizeCode() override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700266 asm_.FinalizeCode();
267 }
268
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100269 size_t CodeSize() const override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700270 return asm_.CodeSize();
271 }
272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100273 void FinalizeInstructions(const MemoryRegion& region) override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700274 asm_.FinalizeInstructions(region);
275 }
276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100277 DebugFrameOpCodeWriterForAssembler& cfi() override {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700278 return asm_.cfi();
279 }
280
281 protected:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100282 explicit JNIMacroAssemblerFwd(ArenaAllocator* allocator) : asm_(allocator) {}
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700283
284 T asm_;
285};
286
Igor Murashkinae7ff922016-10-06 14:59:19 -0700287template <typename Self, typename PlatformLabel, InstructionSet kIsa>
288class JNIMacroLabelCommon : public JNIMacroLabel {
289 public:
290 static Self* Cast(JNIMacroLabel* label) {
291 CHECK(label != nullptr);
292 CHECK_EQ(kIsa, label->isa_);
293
294 return reinterpret_cast<Self*>(label);
295 }
296
297 protected:
298 PlatformLabel* AsPlatformLabel() {
299 return &label_;
300 }
301
302 JNIMacroLabelCommon() : JNIMacroLabel(kIsa) {
303 }
304
Roland Levillainf73caca2018-08-24 17:19:07 +0100305 ~JNIMacroLabelCommon() override {}
Igor Murashkinae7ff922016-10-06 14:59:19 -0700306
307 private:
308 PlatformLabel label_;
309};
310
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700311} // namespace art
312
313#endif // ART_COMPILER_UTILS_JNI_MACRO_ASSEMBLER_H_