blob: 80aa6304247d132da08040283b9ee75c796d60fe [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Ian Rogers166db042013-07-26 12:05:57 -070017#ifndef ART_COMPILER_UTILS_ASSEMBLER_H_
18#define ART_COMPILER_UTILS_ASSEMBLER_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Ian Rogers2c8f6532011-09-02 17:16:34 -070020#include <vector>
21
Ian Rogersd582fa42014-11-05 23:46:43 -080022#include "arch/instruction_set.h"
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020023#include "arch/instruction_set_features.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000024#include "arm/constants_arm.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010025#include "base/arena_allocator.h"
26#include "base/arena_object.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/macros.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000029#include "debug/dwarf/debug_frame_opcode_writer.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070030#include "label.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "managed_register.h"
32#include "memory_region.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080033#include "mips/constants_mips.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "offsets.h"
Vladimir Marko32248382016-05-19 10:37:24 +010035#include "utils/array_ref.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080036#include "x86/constants_x86.h"
37#include "x86_64/constants_x86_64.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070038
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070039namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070040
41class Assembler;
42class AssemblerBuffer;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070043
44// Assembler fixups are positions in generated code that require processing
45// after the code has been copied to executable memory. This includes building
46// relocation information.
47class AssemblerFixup {
48 public:
49 virtual void Process(const MemoryRegion& region, int position) = 0;
50 virtual ~AssemblerFixup() {}
51
52 private:
53 AssemblerFixup* previous_;
54 int position_;
55
56 AssemblerFixup* previous() const { return previous_; }
Andreas Gampe277ccbd2014-11-03 21:36:10 -080057 void set_previous(AssemblerFixup* previous_in) { previous_ = previous_in; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070058
59 int position() const { return position_; }
Andreas Gampe277ccbd2014-11-03 21:36:10 -080060 void set_position(int position_in) { position_ = position_in; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070061
62 friend class AssemblerBuffer;
63};
64
Ian Rogers45a76cb2011-07-21 22:00:15 -070065// Parent of all queued slow paths, emitted during finalization
Vladimir Marko93205e32016-04-13 11:59:46 +010066class SlowPath : public DeletableArenaObject<kArenaAllocAssembler> {
Ian Rogers45a76cb2011-07-21 22:00:15 -070067 public:
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 SlowPath() : next_(nullptr) {}
Ian Rogers45a76cb2011-07-21 22:00:15 -070069 virtual ~SlowPath() {}
70
71 Label* Continuation() { return &continuation_; }
72 Label* Entry() { return &entry_; }
73 // Generate code for slow path
74 virtual void Emit(Assembler *sp_asm) = 0;
75
76 protected:
77 // Entry branched to by fast path
78 Label entry_;
79 // Optional continuation that is branched to at the end of the slow path
80 Label continuation_;
81 // Next in linked list of slow paths
82 SlowPath *next_;
83
Mathieu Chartier02e25112013-08-14 16:14:24 -070084 private:
Ian Rogers45a76cb2011-07-21 22:00:15 -070085 friend class AssemblerBuffer;
86 DISALLOW_COPY_AND_ASSIGN(SlowPath);
87};
88
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070089class AssemblerBuffer {
90 public:
Vladimir Marko93205e32016-04-13 11:59:46 +010091 explicit AssemblerBuffer(ArenaAllocator* arena);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070092 ~AssemblerBuffer();
93
Vladimir Marko93205e32016-04-13 11:59:46 +010094 ArenaAllocator* GetArena() {
95 return arena_;
96 }
97
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070098 // Basic support for emitting, loading, and storing.
99 template<typename T> void Emit(T value) {
100 CHECK(HasEnsuredCapacity());
101 *reinterpret_cast<T*>(cursor_) = value;
102 cursor_ += sizeof(T);
103 }
104
105 template<typename T> T Load(size_t position) {
106 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
107 return *reinterpret_cast<T*>(contents_ + position);
108 }
109
110 template<typename T> void Store(size_t position, T value) {
111 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
112 *reinterpret_cast<T*>(contents_ + position) = value;
113 }
114
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000115 void Resize(size_t new_size) {
116 if (new_size > Capacity()) {
117 ExtendCapacity(new_size);
118 }
119 cursor_ = contents_ + new_size;
120 }
121
122 void Move(size_t newposition, size_t oldposition, size_t size) {
123 // Move a chunk of the buffer from oldposition to newposition.
124 DCHECK_LE(oldposition + size, Size());
125 DCHECK_LE(newposition + size, Size());
126 memmove(contents_ + newposition, contents_ + oldposition, size);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700127 }
128
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700129 // Emit a fixup at the current location.
130 void EmitFixup(AssemblerFixup* fixup) {
131 fixup->set_previous(fixup_);
132 fixup->set_position(Size());
133 fixup_ = fixup;
134 }
135
Ian Rogers45a76cb2011-07-21 22:00:15 -0700136 void EnqueueSlowPath(SlowPath* slowpath) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 if (slow_path_ == nullptr) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700138 slow_path_ = slowpath;
139 } else {
140 SlowPath* cur = slow_path_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700141 for ( ; cur->next_ != nullptr ; cur = cur->next_) {}
Ian Rogers45a76cb2011-07-21 22:00:15 -0700142 cur->next_ = slowpath;
143 }
144 }
145
146 void EmitSlowPaths(Assembler* sp_asm) {
147 SlowPath* cur = slow_path_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 SlowPath* next = nullptr;
149 slow_path_ = nullptr;
150 for ( ; cur != nullptr ; cur = next) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700151 cur->Emit(sp_asm);
152 next = cur->next_;
153 delete cur;
154 }
155 }
156
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700157 // Get the size of the emitted code.
158 size_t Size() const {
159 CHECK_GE(cursor_, contents_);
160 return cursor_ - contents_;
161 }
162
Ian Rogers13735952014-10-08 12:43:28 -0700163 uint8_t* contents() const { return contents_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700164
165 // Copy the assembled instructions into the specified memory block
166 // and apply all fixups.
167 void FinalizeInstructions(const MemoryRegion& region);
168
169 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
170 // must be used to guarantee that the underlying data area is big enough to
171 // hold the emitted instruction. Usage:
172 //
173 // AssemblerBuffer buffer;
174 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
175 // ... emit bytes for single instruction ...
176
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700177#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700178
179 class EnsureCapacity {
180 public:
181 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Vladimir Marko9152fed2016-04-20 14:39:47 +0100182 if (buffer->cursor() > buffer->limit()) {
183 buffer->ExtendCapacity(buffer->Size() + kMinimumGap);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700184 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700185 // In debug mode, we save the assembler buffer along with the gap
186 // size before we start emitting to the buffer. This allows us to
187 // check that any single generated instruction doesn't overflow the
188 // limit implied by the minimum gap size.
189 buffer_ = buffer;
190 gap_ = ComputeGap();
191 // Make sure that extending the capacity leaves a big enough gap
192 // for any kind of instruction.
193 CHECK_GE(gap_, kMinimumGap);
194 // Mark the buffer as having ensured the capacity.
195 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
196 buffer->has_ensured_capacity_ = true;
197 }
198
199 ~EnsureCapacity() {
200 // Unmark the buffer, so we cannot emit after this.
201 buffer_->has_ensured_capacity_ = false;
202 // Make sure the generated instruction doesn't take up more
203 // space than the minimum gap.
204 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700205 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700206 }
207
208 private:
209 AssemblerBuffer* buffer_;
210 int gap_;
211
212 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
213 };
214
215 bool has_ensured_capacity_;
216 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
217
218#else
219
220 class EnsureCapacity {
221 public:
222 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Vladimir Marko9152fed2016-04-20 14:39:47 +0100223 if (buffer->cursor() > buffer->limit()) {
224 buffer->ExtendCapacity(buffer->Size() + kMinimumGap);
225 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700226 }
227 };
228
229 // When building the C++ tests, assertion code is enabled. To allow
230 // asserting that the user of the assembler buffer has ensured the
231 // capacity needed for emitting, we add a dummy method in non-debug mode.
232 bool HasEnsuredCapacity() const { return true; }
233
234#endif
235
236 // Returns the position in the instruction stream.
237 int GetPosition() { return cursor_ - contents_; }
238
Vladimir Marko9152fed2016-04-20 14:39:47 +0100239 size_t Capacity() const {
240 CHECK_GE(limit_, contents_);
241 return (limit_ - contents_) + kMinimumGap;
242 }
243
244 // Unconditionally increase the capacity.
245 // The provided `min_capacity` must be higher than current `Capacity()`.
246 void ExtendCapacity(size_t min_capacity);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700247
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700248 private:
249 // The limit is set to kMinimumGap bytes before the end of the data area.
250 // This leaves enough space for the longest possible instruction and allows
251 // for a single, fast space check per instruction.
252 static const int kMinimumGap = 32;
253
Vladimir Marko93205e32016-04-13 11:59:46 +0100254 ArenaAllocator* arena_;
Ian Rogers13735952014-10-08 12:43:28 -0700255 uint8_t* contents_;
256 uint8_t* cursor_;
257 uint8_t* limit_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700258 AssemblerFixup* fixup_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800259#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700260 bool fixups_processed_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800261#endif
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700262
Ian Rogers45a76cb2011-07-21 22:00:15 -0700263 // Head of linked list of slow paths
264 SlowPath* slow_path_;
265
Ian Rogers13735952014-10-08 12:43:28 -0700266 uint8_t* cursor() const { return cursor_; }
267 uint8_t* limit() const { return limit_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700268
269 // Process the fixup chain starting at the given fixup. The offset is
270 // non-zero for fixups in the body if the preamble is non-empty.
271 void ProcessFixups(const MemoryRegion& region);
272
273 // Compute the limit based on the data area and the capacity. See
274 // description of kMinimumGap for the reasoning behind the value.
Ian Rogers13735952014-10-08 12:43:28 -0700275 static uint8_t* ComputeLimit(uint8_t* data, size_t capacity) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700276 return data + capacity - kMinimumGap;
277 }
278
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700279 friend class AssemblerFixup;
280};
281
David Srbeckydd973932015-04-07 20:29:48 +0100282// The purpose of this class is to ensure that we do not have to explicitly
283// call the AdvancePC method (which is good for convenience and correctness).
284class DebugFrameOpCodeWriterForAssembler FINAL
285 : public dwarf::DebugFrameOpCodeWriter<> {
286 public:
Vladimir Marko10ef6942015-10-22 15:25:54 +0100287 struct DelayedAdvancePC {
288 uint32_t stream_pos;
289 uint32_t pc;
290 };
291
David Srbeckydd973932015-04-07 20:29:48 +0100292 // This method is called the by the opcode writers.
293 virtual void ImplicitlyAdvancePC() FINAL;
294
295 explicit DebugFrameOpCodeWriterForAssembler(Assembler* buffer)
Vladimir Marko10ef6942015-10-22 15:25:54 +0100296 : dwarf::DebugFrameOpCodeWriter<>(false /* enabled */),
297 assembler_(buffer),
298 delay_emitting_advance_pc_(false),
299 delayed_advance_pcs_() {
300 }
301
302 ~DebugFrameOpCodeWriterForAssembler() {
303 DCHECK(delayed_advance_pcs_.empty());
304 }
305
306 // Tell the writer to delay emitting advance PC info.
307 // The assembler must explicitly process all the delayed advances.
308 void DelayEmittingAdvancePCs() {
309 delay_emitting_advance_pc_ = true;
310 }
311
312 // Override the last delayed PC. The new PC can be out of order.
313 void OverrideDelayedPC(size_t pc) {
314 DCHECK(delay_emitting_advance_pc_);
Vladimir Marko6134ba12016-04-14 11:27:34 +0100315 if (enabled_) {
316 DCHECK(!delayed_advance_pcs_.empty());
317 delayed_advance_pcs_.back().pc = pc;
318 }
Vladimir Marko10ef6942015-10-22 15:25:54 +0100319 }
320
321 // Return the number of delayed advance PC entries.
322 size_t NumberOfDelayedAdvancePCs() const {
323 return delayed_advance_pcs_.size();
324 }
325
326 // Release the CFI stream and advance PC infos so that the assembler can patch it.
327 std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>>
328 ReleaseStreamAndPrepareForDelayedAdvancePC() {
329 DCHECK(delay_emitting_advance_pc_);
330 delay_emitting_advance_pc_ = false;
331 std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>> result;
332 result.first.swap(opcodes_);
333 result.second.swap(delayed_advance_pcs_);
334 return result;
335 }
336
337 // Reserve space for the CFI stream.
338 void ReserveCFIStream(size_t capacity) {
339 opcodes_.reserve(capacity);
340 }
341
342 // Append raw data to the CFI stream.
343 void AppendRawData(const std::vector<uint8_t>& raw_data, size_t first, size_t last) {
344 DCHECK_LE(0u, first);
345 DCHECK_LE(first, last);
346 DCHECK_LE(last, raw_data.size());
347 opcodes_.insert(opcodes_.end(), raw_data.begin() + first, raw_data.begin() + last);
David Srbeckydd973932015-04-07 20:29:48 +0100348 }
349
350 private:
351 Assembler* assembler_;
Vladimir Marko10ef6942015-10-22 15:25:54 +0100352 bool delay_emitting_advance_pc_;
353 std::vector<DelayedAdvancePC> delayed_advance_pcs_;
David Srbeckydd973932015-04-07 20:29:48 +0100354};
355
Vladimir Marko93205e32016-04-13 11:59:46 +0100356class Assembler : public DeletableArenaObject<kArenaAllocAssembler> {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700357 public:
Vladimir Marko93205e32016-04-13 11:59:46 +0100358 static std::unique_ptr<Assembler> Create(
359 ArenaAllocator* arena,
360 InstructionSet instruction_set,
361 const InstructionSetFeatures* instruction_set_features = nullptr);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700362
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000363 // Finalize the code; emit slow paths, fixup branches, add literal pool, etc.
364 virtual void FinalizeCode() { buffer_.EmitSlowPaths(this); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700365
366 // Size of generated code
Serban Constantinescued8dd492014-02-11 14:15:10 +0000367 virtual size_t CodeSize() const { return buffer_.Size(); }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100368 virtual const uint8_t* CodeBufferBaseAddress() const { return buffer_.contents(); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700369
370 // Copy instructions out of assembly buffer into the given region of memory
Serban Constantinescued8dd492014-02-11 14:15:10 +0000371 virtual void FinalizeInstructions(const MemoryRegion& region) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700372 buffer_.FinalizeInstructions(region);
373 }
374
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000375 // TODO: Implement with disassembler.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100376 virtual void Comment(const char* format ATTRIBUTE_UNUSED, ...) {}
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000377
Ian Rogers2c8f6532011-09-02 17:16:34 -0700378 // Emit code that will create an activation on the stack
Vladimir Marko32248382016-05-19 10:37:24 +0100379 virtual void BuildFrame(size_t frame_size,
380 ManagedRegister method_reg,
381 ArrayRef<const ManagedRegister> callee_save_regs,
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700382 const ManagedRegisterEntrySpills& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700383
384 // Emit code that will remove an activation from the stack
385 virtual void RemoveFrame(size_t frame_size,
Vladimir Marko32248382016-05-19 10:37:24 +0100386 ArrayRef<const ManagedRegister> callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700387
388 virtual void IncreaseFrameSize(size_t adjust) = 0;
389 virtual void DecreaseFrameSize(size_t adjust) = 0;
390
391 // Store routines
392 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
393 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
394 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
395
396 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
397 ManagedRegister scratch) = 0;
398
Ian Rogersdd7624d2014-03-14 17:43:00 -0700399 virtual void StoreImmediateToThread32(ThreadOffset<4> dest, uint32_t imm,
400 ManagedRegister scratch);
401 virtual void StoreImmediateToThread64(ThreadOffset<8> dest, uint32_t imm,
402 ManagedRegister scratch);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700403
Ian Rogersdd7624d2014-03-14 17:43:00 -0700404 virtual void StoreStackOffsetToThread32(ThreadOffset<4> thr_offs,
405 FrameOffset fr_offs,
406 ManagedRegister scratch);
407 virtual void StoreStackOffsetToThread64(ThreadOffset<8> thr_offs,
408 FrameOffset fr_offs,
409 ManagedRegister scratch);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700410
Ian Rogersdd7624d2014-03-14 17:43:00 -0700411 virtual void StoreStackPointerToThread32(ThreadOffset<4> thr_offs);
412 virtual void StoreStackPointerToThread64(ThreadOffset<8> thr_offs);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700413
414 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
415 FrameOffset in_off, ManagedRegister scratch) = 0;
416
417 // Load routines
418 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
419
Ian Rogersdd7624d2014-03-14 17:43:00 -0700420 virtual void LoadFromThread32(ManagedRegister dest, ThreadOffset<4> src, size_t size);
421 virtual void LoadFromThread64(ManagedRegister dest, ThreadOffset<8> src, size_t size);
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700422
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
Roland Levillain4d027112015-07-01 15:41:14 +0100424 // If unpoison_reference is true and kPoisonReference is true, then we negate the read reference.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 virtual void LoadRef(ManagedRegister dest, ManagedRegister base, MemberOffset offs,
Roland Levillain4d027112015-07-01 15:41:14 +0100426 bool unpoison_reference) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700427
Ian Rogersdd7624d2014-03-14 17:43:00 -0700428 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700429
Ian Rogersdd7624d2014-03-14 17:43:00 -0700430 virtual void LoadRawPtrFromThread32(ManagedRegister dest, ThreadOffset<4> offs);
431 virtual void LoadRawPtrFromThread64(ManagedRegister dest, ThreadOffset<8> offs);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700432
433 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800434 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700435
Ian Rogersdd7624d2014-03-14 17:43:00 -0700436 virtual void CopyRawPtrFromThread32(FrameOffset fr_offs, ThreadOffset<4> thr_offs,
437 ManagedRegister scratch);
438 virtual void CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset<8> thr_offs,
439 ManagedRegister scratch);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700440
Ian Rogersdd7624d2014-03-14 17:43:00 -0700441 virtual void CopyRawPtrToThread32(ThreadOffset<4> thr_offs, FrameOffset fr_offs,
442 ManagedRegister scratch);
443 virtual void CopyRawPtrToThread64(ThreadOffset<8> thr_offs, FrameOffset fr_offs,
444 ManagedRegister scratch);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700445
446 virtual void CopyRef(FrameOffset dest, FrameOffset src,
447 ManagedRegister scratch) = 0;
448
Elliott Hughesa09aea22012-01-06 18:58:27 -0800449 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700450
Ian Rogersdc51b792011-09-22 20:41:37 -0700451 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
452 ManagedRegister scratch, size_t size) = 0;
453
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700454 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
455 ManagedRegister scratch, size_t size) = 0;
456
Ian Rogersdc51b792011-09-22 20:41:37 -0700457 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
458 ManagedRegister scratch, size_t size) = 0;
459
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700460 virtual void Copy(ManagedRegister dest, Offset dest_offset,
461 ManagedRegister src, Offset src_offset,
462 ManagedRegister scratch, size_t size) = 0;
463
464 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
465 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700466
Ian Rogerse5de95b2011-09-18 20:31:38 -0700467 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
468
jeffhao58136ca2012-05-24 13:40:11 -0700469 // Sign extension
470 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
471
jeffhaocee4d0c2012-06-15 14:42:01 -0700472 // Zero extension
473 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
474
Ian Rogers2c8f6532011-09-02 17:16:34 -0700475 // Exploit fast access in managed code to Thread::Current()
476 virtual void GetCurrentThread(ManagedRegister tr) = 0;
477 virtual void GetCurrentThread(FrameOffset dest_offset,
478 ManagedRegister scratch) = 0;
479
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700480 // Set up out_reg to hold a Object** into the handle scope, or to be null if the
Ian Rogers2c8f6532011-09-02 17:16:34 -0700481 // value is null and null_allowed. in_reg holds a possibly stale reference
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700482 // that can be used to avoid loading the handle scope entry to see if the value is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700483 // null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700484 virtual void CreateHandleScopeEntry(ManagedRegister out_reg, FrameOffset handlescope_offset,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700485 ManagedRegister in_reg, bool null_allowed) = 0;
486
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700487 // Set up out_off to hold a Object** into the handle scope, or to be null if the
Ian Rogers2c8f6532011-09-02 17:16:34 -0700488 // value is null and null_allowed.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700489 virtual void CreateHandleScopeEntry(FrameOffset out_off, FrameOffset handlescope_offset,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700490 ManagedRegister scratch, bool null_allowed) = 0;
491
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 // src holds a handle scope entry (Object**) load this into dst
493 virtual void LoadReferenceFromHandleScope(ManagedRegister dst,
Ian Rogers2c8f6532011-09-02 17:16:34 -0700494 ManagedRegister src) = 0;
495
496 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
497 // know that src may not be null.
498 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
499 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
500
501 // Call to address held at [base+offset]
502 virtual void Call(ManagedRegister base, Offset offset,
503 ManagedRegister scratch) = 0;
504 virtual void Call(FrameOffset base, Offset offset,
505 ManagedRegister scratch) = 0;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700506 virtual void CallFromThread32(ThreadOffset<4> offset, ManagedRegister scratch);
507 virtual void CallFromThread64(ThreadOffset<8> offset, ManagedRegister scratch);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700508
Ian Rogers2c8f6532011-09-02 17:16:34 -0700509 // Generate code to check if Thread::Current()->exception_ is non-null
510 // and branch to a ExceptionSlowPath if it is.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700512
Andreas Gampe85b62f22015-09-09 13:15:38 -0700513 virtual void Bind(Label* label) = 0;
514 virtual void Jump(Label* label) = 0;
515
Ian Rogers2c8f6532011-09-02 17:16:34 -0700516 virtual ~Assembler() {}
517
David Srbeckydd973932015-04-07 20:29:48 +0100518 /**
519 * @brief Buffer of DWARF's Call Frame Information opcodes.
520 * @details It is used by debuggers and other tools to unwind the call stack.
521 */
522 DebugFrameOpCodeWriterForAssembler& cfi() { return cfi_; }
523
Ian Rogers2c8f6532011-09-02 17:16:34 -0700524 protected:
Vladimir Marko93205e32016-04-13 11:59:46 +0100525 explicit Assembler(ArenaAllocator* arena) : buffer_(arena), cfi_(this) {}
526
527 ArenaAllocator* GetArena() {
528 return buffer_.GetArena();
529 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700530
531 AssemblerBuffer buffer_;
David Srbeckydd973932015-04-07 20:29:48 +0100532
533 DebugFrameOpCodeWriterForAssembler cfi_;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700534};
535
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700536} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700537
Ian Rogers166db042013-07-26 12:05:57 -0700538#endif // ART_COMPILER_UTILS_ASSEMBLER_H_