blob: cd4fc12e33297bfbf877c63989350d7b4cda9511 [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
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070051class Label {
52 public:
53 Label() : position_(0) {}
54
55 ~Label() {
56 // Assert if label is being destroyed with unresolved branches pending.
57 CHECK(!IsLinked());
58 }
59
60 // Returns the position for bound and linked labels. Cannot be used
61 // for unused labels.
62 int Position() const {
63 CHECK(!IsUnused());
64 return IsBound() ? -position_ - kPointerSize : position_ - kPointerSize;
65 }
66
67 int LinkPosition() const {
68 CHECK(IsLinked());
69 return position_ - kWordSize;
70 }
71
72 bool IsBound() const { return position_ < 0; }
73 bool IsUnused() const { return position_ == 0; }
74 bool IsLinked() const { return position_ > 0; }
75
76 private:
77 int position_;
78
79 void Reinitialize() {
80 position_ = 0;
81 }
82
83 void BindTo(int position) {
84 CHECK(!IsBound());
85 position_ = -position - kPointerSize;
86 CHECK(IsBound());
87 }
88
89 void LinkTo(int position) {
90 CHECK(!IsBound());
91 position_ = position + kPointerSize;
92 CHECK(IsLinked());
93 }
94
Ian Rogers2c8f6532011-09-02 17:16:34 -070095 friend class arm::ArmAssembler;
jeffhao7fbee072012-08-24 17:56:54 -070096 friend class mips::MipsAssembler;
Ian Rogers2c8f6532011-09-02 17:16:34 -070097 friend class x86::X86Assembler;
98
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070099 DISALLOW_COPY_AND_ASSIGN(Label);
100};
101
102
103// Assembler fixups are positions in generated code that require processing
104// after the code has been copied to executable memory. This includes building
105// relocation information.
106class AssemblerFixup {
107 public:
108 virtual void Process(const MemoryRegion& region, int position) = 0;
109 virtual ~AssemblerFixup() {}
110
111 private:
112 AssemblerFixup* previous_;
113 int position_;
114
115 AssemblerFixup* previous() const { return previous_; }
116 void set_previous(AssemblerFixup* previous) { previous_ = previous; }
117
118 int position() const { return position_; }
119 void set_position(int position) { position_ = position; }
120
121 friend class AssemblerBuffer;
122};
123
Ian Rogers45a76cb2011-07-21 22:00:15 -0700124// Parent of all queued slow paths, emitted during finalization
125class SlowPath {
126 public:
127 SlowPath() : next_(NULL) {}
128 virtual ~SlowPath() {}
129
130 Label* Continuation() { return &continuation_; }
131 Label* Entry() { return &entry_; }
132 // Generate code for slow path
133 virtual void Emit(Assembler *sp_asm) = 0;
134
135 protected:
136 // Entry branched to by fast path
137 Label entry_;
138 // Optional continuation that is branched to at the end of the slow path
139 Label continuation_;
140 // Next in linked list of slow paths
141 SlowPath *next_;
142
Mathieu Chartier02e25112013-08-14 16:14:24 -0700143 private:
Ian Rogers45a76cb2011-07-21 22:00:15 -0700144 friend class AssemblerBuffer;
145 DISALLOW_COPY_AND_ASSIGN(SlowPath);
146};
147
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700148class AssemblerBuffer {
149 public:
150 AssemblerBuffer();
151 ~AssemblerBuffer();
152
153 // Basic support for emitting, loading, and storing.
154 template<typename T> void Emit(T value) {
155 CHECK(HasEnsuredCapacity());
156 *reinterpret_cast<T*>(cursor_) = value;
157 cursor_ += sizeof(T);
158 }
159
160 template<typename T> T Load(size_t position) {
161 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
162 return *reinterpret_cast<T*>(contents_ + position);
163 }
164
165 template<typename T> void Store(size_t position, T value) {
166 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
167 *reinterpret_cast<T*>(contents_ + position) = value;
168 }
169
170 // Emit a fixup at the current location.
171 void EmitFixup(AssemblerFixup* fixup) {
172 fixup->set_previous(fixup_);
173 fixup->set_position(Size());
174 fixup_ = fixup;
175 }
176
Ian Rogers45a76cb2011-07-21 22:00:15 -0700177 void EnqueueSlowPath(SlowPath* slowpath) {
178 if (slow_path_ == NULL) {
179 slow_path_ = slowpath;
180 } else {
181 SlowPath* cur = slow_path_;
182 for ( ; cur->next_ != NULL ; cur = cur->next_) {}
183 cur->next_ = slowpath;
184 }
185 }
186
187 void EmitSlowPaths(Assembler* sp_asm) {
188 SlowPath* cur = slow_path_;
189 SlowPath* next = NULL;
190 slow_path_ = NULL;
191 for ( ; cur != NULL ; cur = next) {
192 cur->Emit(sp_asm);
193 next = cur->next_;
194 delete cur;
195 }
196 }
197
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700198 // Get the size of the emitted code.
199 size_t Size() const {
200 CHECK_GE(cursor_, contents_);
201 return cursor_ - contents_;
202 }
203
204 byte* contents() const { return contents_; }
205
206 // Copy the assembled instructions into the specified memory block
207 // and apply all fixups.
208 void FinalizeInstructions(const MemoryRegion& region);
209
210 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
211 // must be used to guarantee that the underlying data area is big enough to
212 // hold the emitted instruction. Usage:
213 //
214 // AssemblerBuffer buffer;
215 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
216 // ... emit bytes for single instruction ...
217
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700218#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700219
220 class EnsureCapacity {
221 public:
222 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700223 if (buffer->cursor() >= buffer->limit()) {
224 buffer->ExtendCapacity();
225 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700226 // In debug mode, we save the assembler buffer along with the gap
227 // size before we start emitting to the buffer. This allows us to
228 // check that any single generated instruction doesn't overflow the
229 // limit implied by the minimum gap size.
230 buffer_ = buffer;
231 gap_ = ComputeGap();
232 // Make sure that extending the capacity leaves a big enough gap
233 // for any kind of instruction.
234 CHECK_GE(gap_, kMinimumGap);
235 // Mark the buffer as having ensured the capacity.
236 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
237 buffer->has_ensured_capacity_ = true;
238 }
239
240 ~EnsureCapacity() {
241 // Unmark the buffer, so we cannot emit after this.
242 buffer_->has_ensured_capacity_ = false;
243 // Make sure the generated instruction doesn't take up more
244 // space than the minimum gap.
245 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700246 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700247 }
248
249 private:
250 AssemblerBuffer* buffer_;
251 int gap_;
252
253 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
254 };
255
256 bool has_ensured_capacity_;
257 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
258
259#else
260
261 class EnsureCapacity {
262 public:
263 explicit EnsureCapacity(AssemblerBuffer* buffer) {
264 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
265 }
266 };
267
268 // When building the C++ tests, assertion code is enabled. To allow
269 // asserting that the user of the assembler buffer has ensured the
270 // capacity needed for emitting, we add a dummy method in non-debug mode.
271 bool HasEnsuredCapacity() const { return true; }
272
273#endif
274
275 // Returns the position in the instruction stream.
276 int GetPosition() { return cursor_ - contents_; }
277
278 private:
279 // The limit is set to kMinimumGap bytes before the end of the data area.
280 // This leaves enough space for the longest possible instruction and allows
281 // for a single, fast space check per instruction.
282 static const int kMinimumGap = 32;
283
284 byte* contents_;
285 byte* cursor_;
286 byte* limit_;
287 AssemblerFixup* fixup_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800288#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700289 bool fixups_processed_;
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800290#endif
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700291
Ian Rogers45a76cb2011-07-21 22:00:15 -0700292 // Head of linked list of slow paths
293 SlowPath* slow_path_;
294
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700295 byte* cursor() const { return cursor_; }
296 byte* limit() const { return limit_; }
297 size_t Capacity() const {
298 CHECK_GE(limit_, contents_);
299 return (limit_ - contents_) + kMinimumGap;
300 }
301
302 // Process the fixup chain starting at the given fixup. The offset is
303 // non-zero for fixups in the body if the preamble is non-empty.
304 void ProcessFixups(const MemoryRegion& region);
305
306 // Compute the limit based on the data area and the capacity. See
307 // description of kMinimumGap for the reasoning behind the value.
308 static byte* ComputeLimit(byte* data, size_t capacity) {
309 return data + capacity - kMinimumGap;
310 }
311
312 void ExtendCapacity();
313
314 friend class AssemblerFixup;
315};
316
Ian Rogers2c8f6532011-09-02 17:16:34 -0700317class Assembler {
318 public:
319 static Assembler* Create(InstructionSet instruction_set);
320
321 // Emit slow paths queued during assembly
Serban Constantinescued8dd492014-02-11 14:15:10 +0000322 virtual void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700323
324 // Size of generated code
Serban Constantinescued8dd492014-02-11 14:15:10 +0000325 virtual size_t CodeSize() const { return buffer_.Size(); }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700326
327 // Copy instructions out of assembly buffer into the given region of memory
Serban Constantinescued8dd492014-02-11 14:15:10 +0000328 virtual void FinalizeInstructions(const MemoryRegion& region) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700329 buffer_.FinalizeInstructions(region);
330 }
331
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332 // TODO: Implement with disassembler.
333 virtual void Comment(const char* format, ...) { }
334
Ian Rogers2c8f6532011-09-02 17:16:34 -0700335 // Emit code that will create an activation on the stack
336 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800337 const std::vector<ManagedRegister>& callee_save_regs,
338 const std::vector<ManagedRegister>& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700339
340 // Emit code that will remove an activation from the stack
341 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700342 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700343
344 virtual void IncreaseFrameSize(size_t adjust) = 0;
345 virtual void DecreaseFrameSize(size_t adjust) = 0;
346
347 // Store routines
348 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
349 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
350 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
351
352 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
353 ManagedRegister scratch) = 0;
354
355 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
356 ManagedRegister scratch) = 0;
357
358 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
359 FrameOffset fr_offs,
360 ManagedRegister scratch) = 0;
361
362 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
363
364 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
365 FrameOffset in_off, ManagedRegister scratch) = 0;
366
367 // Load routines
368 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
369
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700370 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
371
Ian Rogers2c8f6532011-09-02 17:16:34 -0700372 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
373
374 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
375 MemberOffset offs) = 0;
376
377 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
378 Offset offs) = 0;
379
380 virtual void LoadRawPtrFromThread(ManagedRegister dest,
381 ThreadOffset offs) = 0;
382
383 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800384 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700385
386 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
387 ManagedRegister scratch) = 0;
388
389 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
390 ManagedRegister scratch) = 0;
391
392 virtual void CopyRef(FrameOffset dest, FrameOffset src,
393 ManagedRegister scratch) = 0;
394
Elliott Hughesa09aea22012-01-06 18:58:27 -0800395 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700396
Ian Rogersdc51b792011-09-22 20:41:37 -0700397 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
398 ManagedRegister scratch, size_t size) = 0;
399
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700400 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
401 ManagedRegister scratch, size_t size) = 0;
402
Ian Rogersdc51b792011-09-22 20:41:37 -0700403 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
404 ManagedRegister scratch, size_t size) = 0;
405
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700406 virtual void Copy(ManagedRegister dest, Offset dest_offset,
407 ManagedRegister src, Offset src_offset,
408 ManagedRegister scratch, size_t size) = 0;
409
410 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
411 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700412
Ian Rogerse5de95b2011-09-18 20:31:38 -0700413 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
414
jeffhao58136ca2012-05-24 13:40:11 -0700415 // Sign extension
416 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
417
jeffhaocee4d0c2012-06-15 14:42:01 -0700418 // Zero extension
419 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
420
Ian Rogers2c8f6532011-09-02 17:16:34 -0700421 // Exploit fast access in managed code to Thread::Current()
422 virtual void GetCurrentThread(ManagedRegister tr) = 0;
423 virtual void GetCurrentThread(FrameOffset dest_offset,
424 ManagedRegister scratch) = 0;
425
426 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
427 // value is null and null_allowed. in_reg holds a possibly stale reference
428 // that can be used to avoid loading the SIRT entry to see if the value is
429 // NULL.
430 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
431 ManagedRegister in_reg, bool null_allowed) = 0;
432
433 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
434 // value is null and null_allowed.
435 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
436 ManagedRegister scratch, bool null_allowed) = 0;
437
438 // src holds a SIRT entry (Object**) load this into dst
439 virtual void LoadReferenceFromSirt(ManagedRegister dst,
440 ManagedRegister src) = 0;
441
442 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
443 // know that src may not be null.
444 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
445 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
446
447 // Call to address held at [base+offset]
448 virtual void Call(ManagedRegister base, Offset offset,
449 ManagedRegister scratch) = 0;
450 virtual void Call(FrameOffset base, Offset offset,
451 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700452 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700453
Ian Rogers2c8f6532011-09-02 17:16:34 -0700454 // Generate code to check if Thread::Current()->exception_ is non-null
455 // and branch to a ExceptionSlowPath if it is.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700456 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700457
458 virtual ~Assembler() {}
459
460 protected:
461 Assembler() : buffer_() {}
462
463 AssemblerBuffer buffer_;
464};
465
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700466} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700467
Ian Rogers166db042013-07-26 12:05:57 -0700468#endif // ART_COMPILER_UTILS_ASSEMBLER_H_