blob: af9f94d41cc9e63320e7f7efbb0eae4780ac9a20 [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
17#ifndef ART_SRC_ASSEMBLER_H_
18#define ART_SRC_ASSEMBLER_H_
19
Ian Rogers2c8f6532011-09-02 17:16:34 -070020#include <vector>
21
22#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "logging.h"
24#include "macros.h"
25#include "managed_register.h"
26#include "memory_region.h"
27#include "offsets.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070028
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070029namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030
31class Assembler;
32class AssemblerBuffer;
33class AssemblerFixup;
34
Ian Rogers2c8f6532011-09-02 17:16:34 -070035namespace arm {
36 class ArmAssembler;
37}
38namespace x86 {
39 class X86Assembler;
40}
41
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070042class Label {
43 public:
44 Label() : position_(0) {}
45
46 ~Label() {
47 // Assert if label is being destroyed with unresolved branches pending.
48 CHECK(!IsLinked());
49 }
50
51 // Returns the position for bound and linked labels. Cannot be used
52 // for unused labels.
53 int Position() const {
54 CHECK(!IsUnused());
55 return IsBound() ? -position_ - kPointerSize : position_ - kPointerSize;
56 }
57
58 int LinkPosition() const {
59 CHECK(IsLinked());
60 return position_ - kWordSize;
61 }
62
63 bool IsBound() const { return position_ < 0; }
64 bool IsUnused() const { return position_ == 0; }
65 bool IsLinked() const { return position_ > 0; }
66
67 private:
68 int position_;
69
70 void Reinitialize() {
71 position_ = 0;
72 }
73
74 void BindTo(int position) {
75 CHECK(!IsBound());
76 position_ = -position - kPointerSize;
77 CHECK(IsBound());
78 }
79
80 void LinkTo(int position) {
81 CHECK(!IsBound());
82 position_ = position + kPointerSize;
83 CHECK(IsLinked());
84 }
85
Ian Rogers2c8f6532011-09-02 17:16:34 -070086 friend class arm::ArmAssembler;
87 friend class x86::X86Assembler;
88
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070089 DISALLOW_COPY_AND_ASSIGN(Label);
90};
91
92
93// Assembler fixups are positions in generated code that require processing
94// after the code has been copied to executable memory. This includes building
95// relocation information.
96class AssemblerFixup {
97 public:
98 virtual void Process(const MemoryRegion& region, int position) = 0;
99 virtual ~AssemblerFixup() {}
100
101 private:
102 AssemblerFixup* previous_;
103 int position_;
104
105 AssemblerFixup* previous() const { return previous_; }
106 void set_previous(AssemblerFixup* previous) { previous_ = previous; }
107
108 int position() const { return position_; }
109 void set_position(int position) { position_ = position; }
110
111 friend class AssemblerBuffer;
112};
113
Ian Rogers45a76cb2011-07-21 22:00:15 -0700114// Parent of all queued slow paths, emitted during finalization
115class SlowPath {
116 public:
117 SlowPath() : next_(NULL) {}
118 virtual ~SlowPath() {}
119
120 Label* Continuation() { return &continuation_; }
121 Label* Entry() { return &entry_; }
122 // Generate code for slow path
123 virtual void Emit(Assembler *sp_asm) = 0;
124
125 protected:
126 // Entry branched to by fast path
127 Label entry_;
128 // Optional continuation that is branched to at the end of the slow path
129 Label continuation_;
130 // Next in linked list of slow paths
131 SlowPath *next_;
132
133 friend class AssemblerBuffer;
134 DISALLOW_COPY_AND_ASSIGN(SlowPath);
135};
136
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700137class AssemblerBuffer {
138 public:
139 AssemblerBuffer();
140 ~AssemblerBuffer();
141
142 // Basic support for emitting, loading, and storing.
143 template<typename T> void Emit(T value) {
144 CHECK(HasEnsuredCapacity());
145 *reinterpret_cast<T*>(cursor_) = value;
146 cursor_ += sizeof(T);
147 }
148
149 template<typename T> T Load(size_t position) {
150 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
151 return *reinterpret_cast<T*>(contents_ + position);
152 }
153
154 template<typename T> void Store(size_t position, T value) {
155 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
156 *reinterpret_cast<T*>(contents_ + position) = value;
157 }
158
159 // Emit a fixup at the current location.
160 void EmitFixup(AssemblerFixup* fixup) {
161 fixup->set_previous(fixup_);
162 fixup->set_position(Size());
163 fixup_ = fixup;
164 }
165
Ian Rogers45a76cb2011-07-21 22:00:15 -0700166 void EnqueueSlowPath(SlowPath* slowpath) {
167 if (slow_path_ == NULL) {
168 slow_path_ = slowpath;
169 } else {
170 SlowPath* cur = slow_path_;
171 for ( ; cur->next_ != NULL ; cur = cur->next_) {}
172 cur->next_ = slowpath;
173 }
174 }
175
176 void EmitSlowPaths(Assembler* sp_asm) {
177 SlowPath* cur = slow_path_;
178 SlowPath* next = NULL;
179 slow_path_ = NULL;
180 for ( ; cur != NULL ; cur = next) {
181 cur->Emit(sp_asm);
182 next = cur->next_;
183 delete cur;
184 }
185 }
186
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700187 // Get the size of the emitted code.
188 size_t Size() const {
189 CHECK_GE(cursor_, contents_);
190 return cursor_ - contents_;
191 }
192
193 byte* contents() const { return contents_; }
194
195 // Copy the assembled instructions into the specified memory block
196 // and apply all fixups.
197 void FinalizeInstructions(const MemoryRegion& region);
198
199 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
200 // must be used to guarantee that the underlying data area is big enough to
201 // hold the emitted instruction. Usage:
202 //
203 // AssemblerBuffer buffer;
204 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
205 // ... emit bytes for single instruction ...
206
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700207#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700208
209 class EnsureCapacity {
210 public:
211 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700212 if (buffer->cursor() >= buffer->limit()) {
213 buffer->ExtendCapacity();
214 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700215 // In debug mode, we save the assembler buffer along with the gap
216 // size before we start emitting to the buffer. This allows us to
217 // check that any single generated instruction doesn't overflow the
218 // limit implied by the minimum gap size.
219 buffer_ = buffer;
220 gap_ = ComputeGap();
221 // Make sure that extending the capacity leaves a big enough gap
222 // for any kind of instruction.
223 CHECK_GE(gap_, kMinimumGap);
224 // Mark the buffer as having ensured the capacity.
225 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
226 buffer->has_ensured_capacity_ = true;
227 }
228
229 ~EnsureCapacity() {
230 // Unmark the buffer, so we cannot emit after this.
231 buffer_->has_ensured_capacity_ = false;
232 // Make sure the generated instruction doesn't take up more
233 // space than the minimum gap.
234 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700235 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700236 }
237
238 private:
239 AssemblerBuffer* buffer_;
240 int gap_;
241
242 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
243 };
244
245 bool has_ensured_capacity_;
246 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
247
248#else
249
250 class EnsureCapacity {
251 public:
252 explicit EnsureCapacity(AssemblerBuffer* buffer) {
253 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
254 }
255 };
256
257 // When building the C++ tests, assertion code is enabled. To allow
258 // asserting that the user of the assembler buffer has ensured the
259 // capacity needed for emitting, we add a dummy method in non-debug mode.
260 bool HasEnsuredCapacity() const { return true; }
261
262#endif
263
264 // Returns the position in the instruction stream.
265 int GetPosition() { return cursor_ - contents_; }
266
267 private:
268 // The limit is set to kMinimumGap bytes before the end of the data area.
269 // This leaves enough space for the longest possible instruction and allows
270 // for a single, fast space check per instruction.
271 static const int kMinimumGap = 32;
272
273 byte* contents_;
274 byte* cursor_;
275 byte* limit_;
276 AssemblerFixup* fixup_;
277 bool fixups_processed_;
278
Ian Rogers45a76cb2011-07-21 22:00:15 -0700279 // Head of linked list of slow paths
280 SlowPath* slow_path_;
281
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700282 byte* cursor() const { return cursor_; }
283 byte* limit() const { return limit_; }
284 size_t Capacity() const {
285 CHECK_GE(limit_, contents_);
286 return (limit_ - contents_) + kMinimumGap;
287 }
288
289 // Process the fixup chain starting at the given fixup. The offset is
290 // non-zero for fixups in the body if the preamble is non-empty.
291 void ProcessFixups(const MemoryRegion& region);
292
293 // Compute the limit based on the data area and the capacity. See
294 // description of kMinimumGap for the reasoning behind the value.
295 static byte* ComputeLimit(byte* data, size_t capacity) {
296 return data + capacity - kMinimumGap;
297 }
298
299 void ExtendCapacity();
300
301 friend class AssemblerFixup;
302};
303
Ian Rogers2c8f6532011-09-02 17:16:34 -0700304class Assembler {
305 public:
306 static Assembler* Create(InstructionSet instruction_set);
307
308 // Emit slow paths queued during assembly
309 void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
310
311 // Size of generated code
312 size_t CodeSize() const { return buffer_.Size(); }
313
314 // Copy instructions out of assembly buffer into the given region of memory
315 void FinalizeInstructions(const MemoryRegion& region) {
316 buffer_.FinalizeInstructions(region);
317 }
318
319 // Emit code that will create an activation on the stack
320 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800321 const std::vector<ManagedRegister>& callee_save_regs,
322 const std::vector<ManagedRegister>& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700323
324 // Emit code that will remove an activation from the stack
325 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700326 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700327
328 virtual void IncreaseFrameSize(size_t adjust) = 0;
329 virtual void DecreaseFrameSize(size_t adjust) = 0;
330
331 // Store routines
332 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
333 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
334 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
335
336 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
337 ManagedRegister scratch) = 0;
338
339 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
340 ManagedRegister scratch) = 0;
341
342 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
343 FrameOffset fr_offs,
344 ManagedRegister scratch) = 0;
345
346 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
347
348 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
349 FrameOffset in_off, ManagedRegister scratch) = 0;
350
351 // Load routines
352 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
353
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700354 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
355
Ian Rogers2c8f6532011-09-02 17:16:34 -0700356 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
357
358 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
359 MemberOffset offs) = 0;
360
361 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
362 Offset offs) = 0;
363
364 virtual void LoadRawPtrFromThread(ManagedRegister dest,
365 ThreadOffset offs) = 0;
366
367 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800368 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700369
370 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
371 ManagedRegister scratch) = 0;
372
373 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
374 ManagedRegister scratch) = 0;
375
376 virtual void CopyRef(FrameOffset dest, FrameOffset src,
377 ManagedRegister scratch) = 0;
378
Elliott Hughesa09aea22012-01-06 18:58:27 -0800379 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700380
Ian Rogersdc51b792011-09-22 20:41:37 -0700381 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
382 ManagedRegister scratch, size_t size) = 0;
383
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700384 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
385 ManagedRegister scratch, size_t size) = 0;
386
Ian Rogersdc51b792011-09-22 20:41:37 -0700387 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
388 ManagedRegister scratch, size_t size) = 0;
389
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700390 virtual void Copy(ManagedRegister dest, Offset dest_offset,
391 ManagedRegister src, Offset src_offset,
392 ManagedRegister scratch, size_t size) = 0;
393
394 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
395 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700396
Ian Rogerse5de95b2011-09-18 20:31:38 -0700397 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
398
Ian Rogers2c8f6532011-09-02 17:16:34 -0700399 // Exploit fast access in managed code to Thread::Current()
400 virtual void GetCurrentThread(ManagedRegister tr) = 0;
401 virtual void GetCurrentThread(FrameOffset dest_offset,
402 ManagedRegister scratch) = 0;
403
404 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
405 // value is null and null_allowed. in_reg holds a possibly stale reference
406 // that can be used to avoid loading the SIRT entry to see if the value is
407 // NULL.
408 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
409 ManagedRegister in_reg, bool null_allowed) = 0;
410
411 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
412 // value is null and null_allowed.
413 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
414 ManagedRegister scratch, bool null_allowed) = 0;
415
416 // src holds a SIRT entry (Object**) load this into dst
417 virtual void LoadReferenceFromSirt(ManagedRegister dst,
418 ManagedRegister src) = 0;
419
420 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
421 // know that src may not be null.
422 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
423 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
424
425 // Call to address held at [base+offset]
426 virtual void Call(ManagedRegister base, Offset offset,
427 ManagedRegister scratch) = 0;
428 virtual void Call(FrameOffset base, Offset offset,
429 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700430 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700431
432 // Generate code to check if Thread::Current()->suspend_count_ is non-zero
433 // and branch to a SuspendSlowPath if it is. The SuspendSlowPath will continue
434 // at the next instruction.
435 virtual void SuspendPoll(ManagedRegister scratch, ManagedRegister return_reg,
436 FrameOffset return_save_location,
437 size_t return_size) = 0;
438
439 // Generate code to check if Thread::Current()->exception_ is non-null
440 // and branch to a ExceptionSlowPath if it is.
441 virtual void ExceptionPoll(ManagedRegister scratch) = 0;
442
443 virtual ~Assembler() {}
444
445 protected:
446 Assembler() : buffer_() {}
447
448 AssemblerBuffer buffer_;
449};
450
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700451} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700452
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700453#include "assembler_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454#include "assembler_arm.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700455
456#endif // ART_SRC_ASSEMBLER_H_