blob: 3dc5b5dd7a787b00da1a7e884616e08ffbfdcf33 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Ian Rogers166db042013-07-26 12:05:57 -070024#include "arm/constants_arm.h"
25#include "mips/constants_mips.h"
26#include "x86/constants_x86.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027#include "instruction_set.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "managed_register.h"
29#include "memory_region.h"
30#include "offsets.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070031
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070032namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033
34class Assembler;
35class AssemblerBuffer;
36class AssemblerFixup;
37
Ian Rogers2c8f6532011-09-02 17:16:34 -070038namespace arm {
39 class ArmAssembler;
40}
Stuart Monteithb95a5342014-03-12 13:32:32 +000041namespace arm64 {
42 class Arm64Assembler;
43}
jeffhao7fbee072012-08-24 17:56:54 -070044namespace mips {
45 class MipsAssembler;
46}
Ian Rogers2c8f6532011-09-02 17:16:34 -070047namespace x86 {
48 class X86Assembler;
49}
50
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000051class ExternalLabel {
52 public:
53 ExternalLabel(const char* name, uword address)
54 : name_(name), address_(address) {
55 DCHECK(name != nullptr);
56 }
57
58 const char* name() const { return name_; }
59 uword address() const {
60 return address_;
61 }
62
63 private:
64 const char* name_;
65 const uword address_;
66};
67
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070068class Label {
69 public:
70 Label() : position_(0) {}
71
72 ~Label() {
73 // Assert if label is being destroyed with unresolved branches pending.
74 CHECK(!IsLinked());
75 }
76
77 // Returns the position for bound and linked labels. Cannot be used
78 // for unused labels.
79 int Position() const {
80 CHECK(!IsUnused());
81 return IsBound() ? -position_ - kPointerSize : position_ - kPointerSize;
82 }
83
84 int LinkPosition() const {
85 CHECK(IsLinked());
86 return position_ - kWordSize;
87 }
88
89 bool IsBound() const { return position_ < 0; }
90 bool IsUnused() const { return position_ == 0; }
91 bool IsLinked() const { return position_ > 0; }
92
93 private:
94 int position_;
95
96 void Reinitialize() {
97 position_ = 0;
98 }
99
100 void BindTo(int position) {
101 CHECK(!IsBound());
102 position_ = -position - kPointerSize;
103 CHECK(IsBound());
104 }
105
106 void LinkTo(int position) {
107 CHECK(!IsBound());
108 position_ = position + kPointerSize;
109 CHECK(IsLinked());
110 }
111
Ian Rogers2c8f6532011-09-02 17:16:34 -0700112 friend class arm::ArmAssembler;
jeffhao7fbee072012-08-24 17:56:54 -0700113 friend class mips::MipsAssembler;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700114 friend class x86::X86Assembler;
115
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700116 DISALLOW_COPY_AND_ASSIGN(Label);
117};
118
119
120// Assembler fixups are positions in generated code that require processing
121// after the code has been copied to executable memory. This includes building
122// relocation information.
123class AssemblerFixup {
124 public:
125 virtual void Process(const MemoryRegion& region, int position) = 0;
126 virtual ~AssemblerFixup() {}
127
128 private:
129 AssemblerFixup* previous_;
130 int position_;
131
132 AssemblerFixup* previous() const { return previous_; }
133 void set_previous(AssemblerFixup* previous) { previous_ = previous; }
134
135 int position() const { return position_; }
136 void set_position(int position) { position_ = position; }
137
138 friend class AssemblerBuffer;
139};
140
Ian Rogers45a76cb2011-07-21 22:00:15 -0700141// Parent of all queued slow paths, emitted during finalization
142class SlowPath {
143 public:
144 SlowPath() : next_(NULL) {}
145 virtual ~SlowPath() {}
146
147 Label* Continuation() { return &continuation_; }
148 Label* Entry() { return &entry_; }
149 // Generate code for slow path
150 virtual void Emit(Assembler *sp_asm) = 0;
151
152 protected:
153 // Entry branched to by fast path
154 Label entry_;
155 // Optional continuation that is branched to at the end of the slow path
156 Label continuation_;
157 // Next in linked list of slow paths
158 SlowPath *next_;
159
Mathieu Chartier02e25112013-08-14 16:14:24 -0700160 private:
Ian Rogers45a76cb2011-07-21 22:00:15 -0700161 friend class AssemblerBuffer;
162 DISALLOW_COPY_AND_ASSIGN(SlowPath);
163};
164
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700165class AssemblerBuffer {
166 public:
167 AssemblerBuffer();
168 ~AssemblerBuffer();
169
170 // Basic support for emitting, loading, and storing.
171 template<typename T> void Emit(T value) {
172 CHECK(HasEnsuredCapacity());
173 *reinterpret_cast<T*>(cursor_) = value;
174 cursor_ += sizeof(T);
175 }
176
177 template<typename T> T Load(size_t position) {
178 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
179 return *reinterpret_cast<T*>(contents_ + position);
180 }
181
182 template<typename T> void Store(size_t position, T value) {
183 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
184 *reinterpret_cast<T*>(contents_ + position) = value;
185 }
186
187 // Emit a fixup at the current location.
188 void EmitFixup(AssemblerFixup* fixup) {
189 fixup->set_previous(fixup_);
190 fixup->set_position(Size());
191 fixup_ = fixup;
192 }
193
Ian Rogers45a76cb2011-07-21 22:00:15 -0700194 void EnqueueSlowPath(SlowPath* slowpath) {
195 if (slow_path_ == NULL) {
196 slow_path_ = slowpath;
197 } else {
198 SlowPath* cur = slow_path_;
199 for ( ; cur->next_ != NULL ; cur = cur->next_) {}
200 cur->next_ = slowpath;
201 }
202 }
203
204 void EmitSlowPaths(Assembler* sp_asm) {
205 SlowPath* cur = slow_path_;
206 SlowPath* next = NULL;
207 slow_path_ = NULL;
208 for ( ; cur != NULL ; cur = next) {
209 cur->Emit(sp_asm);
210 next = cur->next_;
211 delete cur;
212 }
213 }
214
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700215 // Get the size of the emitted code.
216 size_t Size() const {
217 CHECK_GE(cursor_, contents_);
218 return cursor_ - contents_;
219 }
220
221 byte* contents() const { return contents_; }
222
223 // Copy the assembled instructions into the specified memory block
224 // and apply all fixups.
225 void FinalizeInstructions(const MemoryRegion& region);
226
227 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
228 // must be used to guarantee that the underlying data area is big enough to
229 // hold the emitted instruction. Usage:
230 //
231 // AssemblerBuffer buffer;
232 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
233 // ... emit bytes for single instruction ...
234
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700235#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700236
237 class EnsureCapacity {
238 public:
239 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700240 if (buffer->cursor() >= buffer->limit()) {
241 buffer->ExtendCapacity();
242 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700243 // In debug mode, we save the assembler buffer along with the gap
244 // size before we start emitting to the buffer. This allows us to
245 // check that any single generated instruction doesn't overflow the
246 // limit implied by the minimum gap size.
247 buffer_ = buffer;
248 gap_ = ComputeGap();
249 // Make sure that extending the capacity leaves a big enough gap
250 // for any kind of instruction.
251 CHECK_GE(gap_, kMinimumGap);
252 // Mark the buffer as having ensured the capacity.
253 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
254 buffer->has_ensured_capacity_ = true;
255 }
256
257 ~EnsureCapacity() {
258 // Unmark the buffer, so we cannot emit after this.
259 buffer_->has_ensured_capacity_ = false;
260 // Make sure the generated instruction doesn't take up more
261 // space than the minimum gap.
262 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700263 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700264 }
265
266 private:
267 AssemblerBuffer* buffer_;
268 int gap_;
269
270 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
271 };
272
273 bool has_ensured_capacity_;
274 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
275
276#else
277
278 class EnsureCapacity {
279 public:
280 explicit EnsureCapacity(AssemblerBuffer* buffer) {
281 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
282 }
283 };
284
285 // When building the C++ tests, assertion code is enabled. To allow
286 // asserting that the user of the assembler buffer has ensured the
287 // capacity needed for emitting, we add a dummy method in non-debug mode.
288 bool HasEnsuredCapacity() const { return true; }
289
290#endif
291
292 // Returns the position in the instruction stream.
293 int GetPosition() { return cursor_ - contents_; }
294
295 private:
296 // The limit is set to kMinimumGap bytes before the end of the data area.
297 // This leaves enough space for the longest possible instruction and allows
298 // for a single, fast space check per instruction.
299 static const int kMinimumGap = 32;
300
301 byte* contents_;
302 byte* cursor_;
303 byte* limit_;
304 AssemblerFixup* fixup_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800305#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700306 bool fixups_processed_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800307#endif
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700308
Ian Rogers45a76cb2011-07-21 22:00:15 -0700309 // Head of linked list of slow paths
310 SlowPath* slow_path_;
311
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700312 byte* cursor() const { return cursor_; }
313 byte* limit() const { return limit_; }
314 size_t Capacity() const {
315 CHECK_GE(limit_, contents_);
316 return (limit_ - contents_) + kMinimumGap;
317 }
318
319 // Process the fixup chain starting at the given fixup. The offset is
320 // non-zero for fixups in the body if the preamble is non-empty.
321 void ProcessFixups(const MemoryRegion& region);
322
323 // Compute the limit based on the data area and the capacity. See
324 // description of kMinimumGap for the reasoning behind the value.
325 static byte* ComputeLimit(byte* data, size_t capacity) {
326 return data + capacity - kMinimumGap;
327 }
328
329 void ExtendCapacity();
330
331 friend class AssemblerFixup;
332};
333
Ian Rogers2c8f6532011-09-02 17:16:34 -0700334class Assembler {
335 public:
336 static Assembler* Create(InstructionSet instruction_set);
337
338 // Emit slow paths queued during assembly
Serban Constantinescued8dd492014-02-11 14:15:10 +0000339 virtual void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700340
341 // Size of generated code
Serban Constantinescued8dd492014-02-11 14:15:10 +0000342 virtual size_t CodeSize() const { return buffer_.Size(); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700343
344 // Copy instructions out of assembly buffer into the given region of memory
Serban Constantinescued8dd492014-02-11 14:15:10 +0000345 virtual void FinalizeInstructions(const MemoryRegion& region) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700346 buffer_.FinalizeInstructions(region);
347 }
348
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349 // TODO: Implement with disassembler.
350 virtual void Comment(const char* format, ...) { }
351
Ian Rogers2c8f6532011-09-02 17:16:34 -0700352 // Emit code that will create an activation on the stack
353 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800354 const std::vector<ManagedRegister>& callee_save_regs,
355 const std::vector<ManagedRegister>& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700356
357 // Emit code that will remove an activation from the stack
358 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700359 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700360
361 virtual void IncreaseFrameSize(size_t adjust) = 0;
362 virtual void DecreaseFrameSize(size_t adjust) = 0;
363
364 // Store routines
365 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
366 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
367 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
368
369 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
370 ManagedRegister scratch) = 0;
371
372 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
373 ManagedRegister scratch) = 0;
374
375 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
376 FrameOffset fr_offs,
377 ManagedRegister scratch) = 0;
378
379 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
380
381 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
382 FrameOffset in_off, ManagedRegister scratch) = 0;
383
384 // Load routines
385 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
386
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700387 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
388
Ian Rogers2c8f6532011-09-02 17:16:34 -0700389 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
390
391 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
392 MemberOffset offs) = 0;
393
394 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
395 Offset offs) = 0;
396
397 virtual void LoadRawPtrFromThread(ManagedRegister dest,
398 ThreadOffset offs) = 0;
399
400 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800401 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700402
403 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
404 ManagedRegister scratch) = 0;
405
406 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
407 ManagedRegister scratch) = 0;
408
409 virtual void CopyRef(FrameOffset dest, FrameOffset src,
410 ManagedRegister scratch) = 0;
411
Elliott Hughesa09aea22012-01-06 18:58:27 -0800412 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700413
Ian Rogersdc51b792011-09-22 20:41:37 -0700414 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
415 ManagedRegister scratch, size_t size) = 0;
416
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700417 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
418 ManagedRegister scratch, size_t size) = 0;
419
Ian Rogersdc51b792011-09-22 20:41:37 -0700420 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
421 ManagedRegister scratch, size_t size) = 0;
422
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700423 virtual void Copy(ManagedRegister dest, Offset dest_offset,
424 ManagedRegister src, Offset src_offset,
425 ManagedRegister scratch, size_t size) = 0;
426
427 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
428 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700429
Ian Rogerse5de95b2011-09-18 20:31:38 -0700430 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
431
jeffhao58136ca2012-05-24 13:40:11 -0700432 // Sign extension
433 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
434
jeffhaocee4d0c2012-06-15 14:42:01 -0700435 // Zero extension
436 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
437
Ian Rogers2c8f6532011-09-02 17:16:34 -0700438 // Exploit fast access in managed code to Thread::Current()
439 virtual void GetCurrentThread(ManagedRegister tr) = 0;
440 virtual void GetCurrentThread(FrameOffset dest_offset,
441 ManagedRegister scratch) = 0;
442
443 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
444 // value is null and null_allowed. in_reg holds a possibly stale reference
445 // that can be used to avoid loading the SIRT entry to see if the value is
446 // NULL.
447 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
448 ManagedRegister in_reg, bool null_allowed) = 0;
449
450 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
451 // value is null and null_allowed.
452 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
453 ManagedRegister scratch, bool null_allowed) = 0;
454
455 // src holds a SIRT entry (Object**) load this into dst
456 virtual void LoadReferenceFromSirt(ManagedRegister dst,
457 ManagedRegister src) = 0;
458
459 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
460 // know that src may not be null.
461 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
462 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
463
464 // Call to address held at [base+offset]
465 virtual void Call(ManagedRegister base, Offset offset,
466 ManagedRegister scratch) = 0;
467 virtual void Call(FrameOffset base, Offset offset,
468 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700469 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700470
Ian Rogers2c8f6532011-09-02 17:16:34 -0700471 // Generate code to check if Thread::Current()->exception_ is non-null
472 // and branch to a ExceptionSlowPath if it is.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700474
475 virtual ~Assembler() {}
476
477 protected:
478 Assembler() : buffer_() {}
479
480 AssemblerBuffer buffer_;
481};
482
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700483} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700484
Ian Rogers166db042013-07-26 12:05:57 -0700485#endif // ART_COMPILER_UTILS_ASSEMBLER_H_