blob: ae2619e2ef5432a7516cefc0d104c58837159e29 [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
207#ifdef DEBUG
208
209 class EnsureCapacity {
210 public:
211 explicit EnsureCapacity(AssemblerBuffer* buffer) {
212 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
213 // In debug mode, we save the assembler buffer along with the gap
214 // size before we start emitting to the buffer. This allows us to
215 // check that any single generated instruction doesn't overflow the
216 // limit implied by the minimum gap size.
217 buffer_ = buffer;
218 gap_ = ComputeGap();
219 // Make sure that extending the capacity leaves a big enough gap
220 // for any kind of instruction.
221 CHECK_GE(gap_, kMinimumGap);
222 // Mark the buffer as having ensured the capacity.
223 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
224 buffer->has_ensured_capacity_ = true;
225 }
226
227 ~EnsureCapacity() {
228 // Unmark the buffer, so we cannot emit after this.
229 buffer_->has_ensured_capacity_ = false;
230 // Make sure the generated instruction doesn't take up more
231 // space than the minimum gap.
232 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700233 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700234 }
235
236 private:
237 AssemblerBuffer* buffer_;
238 int gap_;
239
240 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
241 };
242
243 bool has_ensured_capacity_;
244 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
245
246#else
247
248 class EnsureCapacity {
249 public:
250 explicit EnsureCapacity(AssemblerBuffer* buffer) {
251 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
252 }
253 };
254
255 // When building the C++ tests, assertion code is enabled. To allow
256 // asserting that the user of the assembler buffer has ensured the
257 // capacity needed for emitting, we add a dummy method in non-debug mode.
258 bool HasEnsuredCapacity() const { return true; }
259
260#endif
261
262 // Returns the position in the instruction stream.
263 int GetPosition() { return cursor_ - contents_; }
264
265 private:
266 // The limit is set to kMinimumGap bytes before the end of the data area.
267 // This leaves enough space for the longest possible instruction and allows
268 // for a single, fast space check per instruction.
269 static const int kMinimumGap = 32;
270
271 byte* contents_;
272 byte* cursor_;
273 byte* limit_;
274 AssemblerFixup* fixup_;
275 bool fixups_processed_;
276
Ian Rogers45a76cb2011-07-21 22:00:15 -0700277 // Head of linked list of slow paths
278 SlowPath* slow_path_;
279
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700280 byte* cursor() const { return cursor_; }
281 byte* limit() const { return limit_; }
282 size_t Capacity() const {
283 CHECK_GE(limit_, contents_);
284 return (limit_ - contents_) + kMinimumGap;
285 }
286
287 // Process the fixup chain starting at the given fixup. The offset is
288 // non-zero for fixups in the body if the preamble is non-empty.
289 void ProcessFixups(const MemoryRegion& region);
290
291 // Compute the limit based on the data area and the capacity. See
292 // description of kMinimumGap for the reasoning behind the value.
293 static byte* ComputeLimit(byte* data, size_t capacity) {
294 return data + capacity - kMinimumGap;
295 }
296
297 void ExtendCapacity();
298
299 friend class AssemblerFixup;
300};
301
Ian Rogers2c8f6532011-09-02 17:16:34 -0700302class Assembler {
303 public:
304 static Assembler* Create(InstructionSet instruction_set);
305
306 // Emit slow paths queued during assembly
307 void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
308
309 // Size of generated code
310 size_t CodeSize() const { return buffer_.Size(); }
311
312 // Copy instructions out of assembly buffer into the given region of memory
313 void FinalizeInstructions(const MemoryRegion& region) {
314 buffer_.FinalizeInstructions(region);
315 }
316
317 // Emit code that will create an activation on the stack
318 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersbdb03912011-09-14 00:55:44 -0700319 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700320
321 // Emit code that will remove an activation from the stack
322 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700323 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700324
325 virtual void IncreaseFrameSize(size_t adjust) = 0;
326 virtual void DecreaseFrameSize(size_t adjust) = 0;
327
328 // Store routines
329 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
330 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
331 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
332
333 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
334 ManagedRegister scratch) = 0;
335
336 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
337 ManagedRegister scratch) = 0;
338
339 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
340 FrameOffset fr_offs,
341 ManagedRegister scratch) = 0;
342
343 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
344
345 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
346 FrameOffset in_off, ManagedRegister scratch) = 0;
347
348 // Load routines
349 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
350
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700351 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
352
Ian Rogers2c8f6532011-09-02 17:16:34 -0700353 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
354
355 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
356 MemberOffset offs) = 0;
357
358 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
359 Offset offs) = 0;
360
361 virtual void LoadRawPtrFromThread(ManagedRegister dest,
362 ThreadOffset offs) = 0;
363
364 // Copying routines
365 virtual void Move(ManagedRegister dest, ManagedRegister src) = 0;
366
367 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
368 ManagedRegister scratch) = 0;
369
370 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
371 ManagedRegister scratch) = 0;
372
373 virtual void CopyRef(FrameOffset dest, FrameOffset src,
374 ManagedRegister scratch) = 0;
375
Elliott Hughesa09aea22012-01-06 18:58:27 -0800376 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700377
Ian Rogersdc51b792011-09-22 20:41:37 -0700378 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
379 ManagedRegister scratch, size_t size) = 0;
380
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700381 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
382 ManagedRegister scratch, size_t size) = 0;
383
Ian Rogersdc51b792011-09-22 20:41:37 -0700384 virtual void Copy(FrameOffset dest, FrameOffset src_base, Offset src_offset,
385 ManagedRegister scratch, size_t size) = 0;
386
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700387 virtual void Copy(ManagedRegister dest, Offset dest_offset,
388 ManagedRegister src, Offset src_offset,
389 ManagedRegister scratch, size_t size) = 0;
390
391 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
392 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700393
Ian Rogerse5de95b2011-09-18 20:31:38 -0700394 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
395
Ian Rogers2c8f6532011-09-02 17:16:34 -0700396 // Exploit fast access in managed code to Thread::Current()
397 virtual void GetCurrentThread(ManagedRegister tr) = 0;
398 virtual void GetCurrentThread(FrameOffset dest_offset,
399 ManagedRegister scratch) = 0;
400
401 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
402 // value is null and null_allowed. in_reg holds a possibly stale reference
403 // that can be used to avoid loading the SIRT entry to see if the value is
404 // NULL.
405 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
406 ManagedRegister in_reg, bool null_allowed) = 0;
407
408 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
409 // value is null and null_allowed.
410 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
411 ManagedRegister scratch, bool null_allowed) = 0;
412
413 // src holds a SIRT entry (Object**) load this into dst
414 virtual void LoadReferenceFromSirt(ManagedRegister dst,
415 ManagedRegister src) = 0;
416
417 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
418 // know that src may not be null.
419 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
420 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
421
422 // Call to address held at [base+offset]
423 virtual void Call(ManagedRegister base, Offset offset,
424 ManagedRegister scratch) = 0;
425 virtual void Call(FrameOffset base, Offset offset,
426 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700427 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700428
429 // Generate code to check if Thread::Current()->suspend_count_ is non-zero
430 // and branch to a SuspendSlowPath if it is. The SuspendSlowPath will continue
431 // at the next instruction.
432 virtual void SuspendPoll(ManagedRegister scratch, ManagedRegister return_reg,
433 FrameOffset return_save_location,
434 size_t return_size) = 0;
435
436 // Generate code to check if Thread::Current()->exception_ is non-null
437 // and branch to a ExceptionSlowPath if it is.
438 virtual void ExceptionPoll(ManagedRegister scratch) = 0;
439
440 virtual ~Assembler() {}
441
442 protected:
443 Assembler() : buffer_() {}
444
445 AssemblerBuffer buffer_;
446};
447
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700448} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700449
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700450#include "assembler_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700451#include "assembler_arm.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700452
453#endif // ART_SRC_ASSEMBLER_H_