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