blob: 72ebdd37415dda7b76dbe8defaa14eda96feb385 [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"
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070027#include "x86_64/constants_x86_64.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070028#include "instruction_set.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "managed_register.h"
30#include "memory_region.h"
31#include "offsets.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070033namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070034
35class Assembler;
36class AssemblerBuffer;
37class AssemblerFixup;
38
Ian Rogers2c8f6532011-09-02 17:16:34 -070039namespace arm {
40 class ArmAssembler;
41}
Stuart Monteithb95a5342014-03-12 13:32:32 +000042namespace arm64 {
43 class Arm64Assembler;
44}
jeffhao7fbee072012-08-24 17:56:54 -070045namespace mips {
46 class MipsAssembler;
47}
Ian Rogers2c8f6532011-09-02 17:16:34 -070048namespace x86 {
49 class X86Assembler;
50}
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070051namespace x86_64 {
52 class X86_64Assembler;
53}
Ian Rogers2c8f6532011-09-02 17:16:34 -070054
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070055class Label {
56 public:
57 Label() : position_(0) {}
58
59 ~Label() {
60 // Assert if label is being destroyed with unresolved branches pending.
61 CHECK(!IsLinked());
62 }
63
64 // Returns the position for bound and linked labels. Cannot be used
65 // for unused labels.
66 int Position() const {
67 CHECK(!IsUnused());
68 return IsBound() ? -position_ - kPointerSize : position_ - kPointerSize;
69 }
70
71 int LinkPosition() const {
72 CHECK(IsLinked());
73 return position_ - kWordSize;
74 }
75
76 bool IsBound() const { return position_ < 0; }
77 bool IsUnused() const { return position_ == 0; }
78 bool IsLinked() const { return position_ > 0; }
79
80 private:
81 int position_;
82
83 void Reinitialize() {
84 position_ = 0;
85 }
86
87 void BindTo(int position) {
88 CHECK(!IsBound());
89 position_ = -position - kPointerSize;
90 CHECK(IsBound());
91 }
92
93 void LinkTo(int position) {
94 CHECK(!IsBound());
95 position_ = position + kPointerSize;
96 CHECK(IsLinked());
97 }
98
Ian Rogers2c8f6532011-09-02 17:16:34 -070099 friend class arm::ArmAssembler;
jeffhao7fbee072012-08-24 17:56:54 -0700100 friend class mips::MipsAssembler;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700101 friend class x86::X86Assembler;
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700102 friend class x86_64::X86_64Assembler;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700103
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700104 DISALLOW_COPY_AND_ASSIGN(Label);
105};
106
107
108// Assembler fixups are positions in generated code that require processing
109// after the code has been copied to executable memory. This includes building
110// relocation information.
111class AssemblerFixup {
112 public:
113 virtual void Process(const MemoryRegion& region, int position) = 0;
114 virtual ~AssemblerFixup() {}
115
116 private:
117 AssemblerFixup* previous_;
118 int position_;
119
120 AssemblerFixup* previous() const { return previous_; }
121 void set_previous(AssemblerFixup* previous) { previous_ = previous; }
122
123 int position() const { return position_; }
124 void set_position(int position) { position_ = position; }
125
126 friend class AssemblerBuffer;
127};
128
Ian Rogers45a76cb2011-07-21 22:00:15 -0700129// Parent of all queued slow paths, emitted during finalization
130class SlowPath {
131 public:
132 SlowPath() : next_(NULL) {}
133 virtual ~SlowPath() {}
134
135 Label* Continuation() { return &continuation_; }
136 Label* Entry() { return &entry_; }
137 // Generate code for slow path
138 virtual void Emit(Assembler *sp_asm) = 0;
139
140 protected:
141 // Entry branched to by fast path
142 Label entry_;
143 // Optional continuation that is branched to at the end of the slow path
144 Label continuation_;
145 // Next in linked list of slow paths
146 SlowPath *next_;
147
Mathieu Chartier02e25112013-08-14 16:14:24 -0700148 private:
Ian Rogers45a76cb2011-07-21 22:00:15 -0700149 friend class AssemblerBuffer;
150 DISALLOW_COPY_AND_ASSIGN(SlowPath);
151};
152
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700153class AssemblerBuffer {
154 public:
155 AssemblerBuffer();
156 ~AssemblerBuffer();
157
158 // Basic support for emitting, loading, and storing.
159 template<typename T> void Emit(T value) {
160 CHECK(HasEnsuredCapacity());
161 *reinterpret_cast<T*>(cursor_) = value;
162 cursor_ += sizeof(T);
163 }
164
165 template<typename T> T Load(size_t position) {
166 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
167 return *reinterpret_cast<T*>(contents_ + position);
168 }
169
170 template<typename T> void Store(size_t position, T value) {
171 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
172 *reinterpret_cast<T*>(contents_ + position) = value;
173 }
174
175 // Emit a fixup at the current location.
176 void EmitFixup(AssemblerFixup* fixup) {
177 fixup->set_previous(fixup_);
178 fixup->set_position(Size());
179 fixup_ = fixup;
180 }
181
Ian Rogers45a76cb2011-07-21 22:00:15 -0700182 void EnqueueSlowPath(SlowPath* slowpath) {
183 if (slow_path_ == NULL) {
184 slow_path_ = slowpath;
185 } else {
186 SlowPath* cur = slow_path_;
187 for ( ; cur->next_ != NULL ; cur = cur->next_) {}
188 cur->next_ = slowpath;
189 }
190 }
191
192 void EmitSlowPaths(Assembler* sp_asm) {
193 SlowPath* cur = slow_path_;
194 SlowPath* next = NULL;
195 slow_path_ = NULL;
196 for ( ; cur != NULL ; cur = next) {
197 cur->Emit(sp_asm);
198 next = cur->next_;
199 delete cur;
200 }
201 }
202
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700203 // Get the size of the emitted code.
204 size_t Size() const {
205 CHECK_GE(cursor_, contents_);
206 return cursor_ - contents_;
207 }
208
209 byte* contents() const { return contents_; }
210
211 // Copy the assembled instructions into the specified memory block
212 // and apply all fixups.
213 void FinalizeInstructions(const MemoryRegion& region);
214
215 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
216 // must be used to guarantee that the underlying data area is big enough to
217 // hold the emitted instruction. Usage:
218 //
219 // AssemblerBuffer buffer;
220 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
221 // ... emit bytes for single instruction ...
222
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700223#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700224
225 class EnsureCapacity {
226 public:
227 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700228 if (buffer->cursor() >= buffer->limit()) {
229 buffer->ExtendCapacity();
230 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700231 // In debug mode, we save the assembler buffer along with the gap
232 // size before we start emitting to the buffer. This allows us to
233 // check that any single generated instruction doesn't overflow the
234 // limit implied by the minimum gap size.
235 buffer_ = buffer;
236 gap_ = ComputeGap();
237 // Make sure that extending the capacity leaves a big enough gap
238 // for any kind of instruction.
239 CHECK_GE(gap_, kMinimumGap);
240 // Mark the buffer as having ensured the capacity.
241 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
242 buffer->has_ensured_capacity_ = true;
243 }
244
245 ~EnsureCapacity() {
246 // Unmark the buffer, so we cannot emit after this.
247 buffer_->has_ensured_capacity_ = false;
248 // Make sure the generated instruction doesn't take up more
249 // space than the minimum gap.
250 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700251 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700252 }
253
254 private:
255 AssemblerBuffer* buffer_;
256 int gap_;
257
258 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
259 };
260
261 bool has_ensured_capacity_;
262 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
263
264#else
265
266 class EnsureCapacity {
267 public:
268 explicit EnsureCapacity(AssemblerBuffer* buffer) {
269 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
270 }
271 };
272
273 // When building the C++ tests, assertion code is enabled. To allow
274 // asserting that the user of the assembler buffer has ensured the
275 // capacity needed for emitting, we add a dummy method in non-debug mode.
276 bool HasEnsuredCapacity() const { return true; }
277
278#endif
279
280 // Returns the position in the instruction stream.
281 int GetPosition() { return cursor_ - contents_; }
282
283 private:
284 // The limit is set to kMinimumGap bytes before the end of the data area.
285 // This leaves enough space for the longest possible instruction and allows
286 // for a single, fast space check per instruction.
287 static const int kMinimumGap = 32;
288
289 byte* contents_;
290 byte* cursor_;
291 byte* limit_;
292 AssemblerFixup* fixup_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800293#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700294 bool fixups_processed_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800295#endif
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700296
Ian Rogers45a76cb2011-07-21 22:00:15 -0700297 // Head of linked list of slow paths
298 SlowPath* slow_path_;
299
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700300 byte* cursor() const { return cursor_; }
301 byte* limit() const { return limit_; }
302 size_t Capacity() const {
303 CHECK_GE(limit_, contents_);
304 return (limit_ - contents_) + kMinimumGap;
305 }
306
307 // Process the fixup chain starting at the given fixup. The offset is
308 // non-zero for fixups in the body if the preamble is non-empty.
309 void ProcessFixups(const MemoryRegion& region);
310
311 // Compute the limit based on the data area and the capacity. See
312 // description of kMinimumGap for the reasoning behind the value.
313 static byte* ComputeLimit(byte* data, size_t capacity) {
314 return data + capacity - kMinimumGap;
315 }
316
317 void ExtendCapacity();
318
319 friend class AssemblerFixup;
320};
321
Ian Rogers2c8f6532011-09-02 17:16:34 -0700322class Assembler {
323 public:
324 static Assembler* Create(InstructionSet instruction_set);
325
326 // Emit slow paths queued during assembly
Serban Constantinescued8dd492014-02-11 14:15:10 +0000327 virtual void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700328
329 // Size of generated code
Serban Constantinescued8dd492014-02-11 14:15:10 +0000330 virtual size_t CodeSize() const { return buffer_.Size(); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700331
332 // Copy instructions out of assembly buffer into the given region of memory
Serban Constantinescued8dd492014-02-11 14:15:10 +0000333 virtual void FinalizeInstructions(const MemoryRegion& region) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700334 buffer_.FinalizeInstructions(region);
335 }
336
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000337 // TODO: Implement with disassembler.
338 virtual void Comment(const char* format, ...) { }
339
Ian Rogers2c8f6532011-09-02 17:16:34 -0700340 // Emit code that will create an activation on the stack
341 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800342 const std::vector<ManagedRegister>& callee_save_regs,
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700343 const ManagedRegisterEntrySpills& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700344
345 // Emit code that will remove an activation from the stack
346 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700347 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700348
349 virtual void IncreaseFrameSize(size_t adjust) = 0;
350 virtual void DecreaseFrameSize(size_t adjust) = 0;
351
352 // Store routines
353 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
354 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
355 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
356
357 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
358 ManagedRegister scratch) = 0;
359
360 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
361 ManagedRegister scratch) = 0;
362
363 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
364 FrameOffset fr_offs,
365 ManagedRegister scratch) = 0;
366
367 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
368
369 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
370 FrameOffset in_off, ManagedRegister scratch) = 0;
371
372 // Load routines
373 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
374
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700375 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
376
Ian Rogers2c8f6532011-09-02 17:16:34 -0700377 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
378
379 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
380 MemberOffset offs) = 0;
381
382 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
383 Offset offs) = 0;
384
385 virtual void LoadRawPtrFromThread(ManagedRegister dest,
386 ThreadOffset offs) = 0;
387
388 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800389 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700390
391 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
392 ManagedRegister scratch) = 0;
393
394 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
395 ManagedRegister scratch) = 0;
396
397 virtual void CopyRef(FrameOffset dest, FrameOffset src,
398 ManagedRegister scratch) = 0;
399
Elliott Hughesa09aea22012-01-06 18:58:27 -0800400 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700401
Ian Rogersdc51b792011-09-22 20:41:37 -0700402 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
403 ManagedRegister scratch, size_t size) = 0;
404
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700405 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
406 ManagedRegister scratch, size_t size) = 0;
407
Ian Rogersdc51b792011-09-22 20:41:37 -0700408 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
409 ManagedRegister scratch, size_t size) = 0;
410
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700411 virtual void Copy(ManagedRegister dest, Offset dest_offset,
412 ManagedRegister src, Offset src_offset,
413 ManagedRegister scratch, size_t size) = 0;
414
415 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
416 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700417
Ian Rogerse5de95b2011-09-18 20:31:38 -0700418 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
419
jeffhao58136ca2012-05-24 13:40:11 -0700420 // Sign extension
421 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
422
jeffhaocee4d0c2012-06-15 14:42:01 -0700423 // Zero extension
424 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
425
Ian Rogers2c8f6532011-09-02 17:16:34 -0700426 // Exploit fast access in managed code to Thread::Current()
427 virtual void GetCurrentThread(ManagedRegister tr) = 0;
428 virtual void GetCurrentThread(FrameOffset dest_offset,
429 ManagedRegister scratch) = 0;
430
431 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
432 // value is null and null_allowed. in_reg holds a possibly stale reference
433 // that can be used to avoid loading the SIRT entry to see if the value is
434 // NULL.
435 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
436 ManagedRegister in_reg, bool null_allowed) = 0;
437
438 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
439 // value is null and null_allowed.
440 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
441 ManagedRegister scratch, bool null_allowed) = 0;
442
443 // src holds a SIRT entry (Object**) load this into dst
444 virtual void LoadReferenceFromSirt(ManagedRegister dst,
445 ManagedRegister src) = 0;
446
447 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
448 // know that src may not be null.
449 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
450 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
451
452 // Call to address held at [base+offset]
453 virtual void Call(ManagedRegister base, Offset offset,
454 ManagedRegister scratch) = 0;
455 virtual void Call(FrameOffset base, Offset offset,
456 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700457 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700458
Ian Rogers2c8f6532011-09-02 17:16:34 -0700459 // Generate code to check if Thread::Current()->exception_ is non-null
460 // and branch to a ExceptionSlowPath if it is.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700462
463 virtual ~Assembler() {}
464
465 protected:
466 Assembler() : buffer_() {}
467
468 AssemblerBuffer buffer_;
469};
470
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700471} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700472
Ian Rogers166db042013-07-26 12:05:57 -0700473#endif // ART_COMPILER_UTILS_ASSEMBLER_H_