blob: cbf145b949ef86e799a3e05153fd83e2def9540c [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 Rogers57b86d42012-03-27 16:05:41 -070017#ifndef ART_SRC_OAT_UTILS_ASSEMBLER_H_
18#define ART_SRC_OAT_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"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024#include "constants_arm.h"
jeffhao7fbee072012-08-24 17:56:54 -070025#include "constants_mips.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070026#include "constants_x86.h"
27#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
140 friend class AssemblerBuffer;
141 DISALLOW_COPY_AND_ASSIGN(SlowPath);
142};
143
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700144class AssemblerBuffer {
145 public:
146 AssemblerBuffer();
147 ~AssemblerBuffer();
148
149 // Basic support for emitting, loading, and storing.
150 template<typename T> void Emit(T value) {
151 CHECK(HasEnsuredCapacity());
152 *reinterpret_cast<T*>(cursor_) = value;
153 cursor_ += sizeof(T);
154 }
155
156 template<typename T> T Load(size_t position) {
157 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
158 return *reinterpret_cast<T*>(contents_ + position);
159 }
160
161 template<typename T> void Store(size_t position, T value) {
162 CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
163 *reinterpret_cast<T*>(contents_ + position) = value;
164 }
165
166 // Emit a fixup at the current location.
167 void EmitFixup(AssemblerFixup* fixup) {
168 fixup->set_previous(fixup_);
169 fixup->set_position(Size());
170 fixup_ = fixup;
171 }
172
Ian Rogers45a76cb2011-07-21 22:00:15 -0700173 void EnqueueSlowPath(SlowPath* slowpath) {
174 if (slow_path_ == NULL) {
175 slow_path_ = slowpath;
176 } else {
177 SlowPath* cur = slow_path_;
178 for ( ; cur->next_ != NULL ; cur = cur->next_) {}
179 cur->next_ = slowpath;
180 }
181 }
182
183 void EmitSlowPaths(Assembler* sp_asm) {
184 SlowPath* cur = slow_path_;
185 SlowPath* next = NULL;
186 slow_path_ = NULL;
187 for ( ; cur != NULL ; cur = next) {
188 cur->Emit(sp_asm);
189 next = cur->next_;
190 delete cur;
191 }
192 }
193
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700194 // Get the size of the emitted code.
195 size_t Size() const {
196 CHECK_GE(cursor_, contents_);
197 return cursor_ - contents_;
198 }
199
200 byte* contents() const { return contents_; }
201
202 // Copy the assembled instructions into the specified memory block
203 // and apply all fixups.
204 void FinalizeInstructions(const MemoryRegion& region);
205
206 // To emit an instruction to the assembler buffer, the EnsureCapacity helper
207 // must be used to guarantee that the underlying data area is big enough to
208 // hold the emitted instruction. Usage:
209 //
210 // AssemblerBuffer buffer;
211 // AssemblerBuffer::EnsureCapacity ensured(&buffer);
212 // ... emit bytes for single instruction ...
213
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700214#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700215
216 class EnsureCapacity {
217 public:
218 explicit EnsureCapacity(AssemblerBuffer* buffer) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -0700219 if (buffer->cursor() >= buffer->limit()) {
220 buffer->ExtendCapacity();
221 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700222 // In debug mode, we save the assembler buffer along with the gap
223 // size before we start emitting to the buffer. This allows us to
224 // check that any single generated instruction doesn't overflow the
225 // limit implied by the minimum gap size.
226 buffer_ = buffer;
227 gap_ = ComputeGap();
228 // Make sure that extending the capacity leaves a big enough gap
229 // for any kind of instruction.
230 CHECK_GE(gap_, kMinimumGap);
231 // Mark the buffer as having ensured the capacity.
232 CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest.
233 buffer->has_ensured_capacity_ = true;
234 }
235
236 ~EnsureCapacity() {
237 // Unmark the buffer, so we cannot emit after this.
238 buffer_->has_ensured_capacity_ = false;
239 // Make sure the generated instruction doesn't take up more
240 // space than the minimum gap.
241 int delta = gap_ - ComputeGap();
Ian Rogersb033c752011-07-20 12:22:35 -0700242 CHECK_LE(delta, kMinimumGap);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700243 }
244
245 private:
246 AssemblerBuffer* buffer_;
247 int gap_;
248
249 int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
250 };
251
252 bool has_ensured_capacity_;
253 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
254
255#else
256
257 class EnsureCapacity {
258 public:
259 explicit EnsureCapacity(AssemblerBuffer* buffer) {
260 if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity();
261 }
262 };
263
264 // When building the C++ tests, assertion code is enabled. To allow
265 // asserting that the user of the assembler buffer has ensured the
266 // capacity needed for emitting, we add a dummy method in non-debug mode.
267 bool HasEnsuredCapacity() const { return true; }
268
269#endif
270
271 // Returns the position in the instruction stream.
272 int GetPosition() { return cursor_ - contents_; }
273
274 private:
275 // The limit is set to kMinimumGap bytes before the end of the data area.
276 // This leaves enough space for the longest possible instruction and allows
277 // for a single, fast space check per instruction.
278 static const int kMinimumGap = 32;
279
280 byte* contents_;
281 byte* cursor_;
282 byte* limit_;
283 AssemblerFixup* fixup_;
284 bool fixups_processed_;
285
Ian Rogers45a76cb2011-07-21 22:00:15 -0700286 // Head of linked list of slow paths
287 SlowPath* slow_path_;
288
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700289 byte* cursor() const { return cursor_; }
290 byte* limit() const { return limit_; }
291 size_t Capacity() const {
292 CHECK_GE(limit_, contents_);
293 return (limit_ - contents_) + kMinimumGap;
294 }
295
296 // Process the fixup chain starting at the given fixup. The offset is
297 // non-zero for fixups in the body if the preamble is non-empty.
298 void ProcessFixups(const MemoryRegion& region);
299
300 // Compute the limit based on the data area and the capacity. See
301 // description of kMinimumGap for the reasoning behind the value.
302 static byte* ComputeLimit(byte* data, size_t capacity) {
303 return data + capacity - kMinimumGap;
304 }
305
306 void ExtendCapacity();
307
308 friend class AssemblerFixup;
309};
310
Ian Rogers2c8f6532011-09-02 17:16:34 -0700311class Assembler {
312 public:
313 static Assembler* Create(InstructionSet instruction_set);
314
315 // Emit slow paths queued during assembly
316 void EmitSlowPaths() { buffer_.EmitSlowPaths(this); }
317
318 // Size of generated code
319 size_t CodeSize() const { return buffer_.Size(); }
320
321 // Copy instructions out of assembly buffer into the given region of memory
322 void FinalizeInstructions(const MemoryRegion& region) {
323 buffer_.FinalizeInstructions(region);
324 }
325
326 // Emit code that will create an activation on the stack
327 virtual void BuildFrame(size_t frame_size, ManagedRegister method_reg,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800328 const std::vector<ManagedRegister>& callee_save_regs,
329 const std::vector<ManagedRegister>& entry_spills) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700330
331 // Emit code that will remove an activation from the stack
332 virtual void RemoveFrame(size_t frame_size,
Ian Rogersbdb03912011-09-14 00:55:44 -0700333 const std::vector<ManagedRegister>& callee_save_regs) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700334
335 virtual void IncreaseFrameSize(size_t adjust) = 0;
336 virtual void DecreaseFrameSize(size_t adjust) = 0;
337
338 // Store routines
339 virtual void Store(FrameOffset offs, ManagedRegister src, size_t size) = 0;
340 virtual void StoreRef(FrameOffset dest, ManagedRegister src) = 0;
341 virtual void StoreRawPtr(FrameOffset dest, ManagedRegister src) = 0;
342
343 virtual void StoreImmediateToFrame(FrameOffset dest, uint32_t imm,
344 ManagedRegister scratch) = 0;
345
346 virtual void StoreImmediateToThread(ThreadOffset dest, uint32_t imm,
347 ManagedRegister scratch) = 0;
348
349 virtual void StoreStackOffsetToThread(ThreadOffset thr_offs,
350 FrameOffset fr_offs,
351 ManagedRegister scratch) = 0;
352
353 virtual void StoreStackPointerToThread(ThreadOffset thr_offs) = 0;
354
355 virtual void StoreSpanning(FrameOffset dest, ManagedRegister src,
356 FrameOffset in_off, ManagedRegister scratch) = 0;
357
358 // Load routines
359 virtual void Load(ManagedRegister dest, FrameOffset src, size_t size) = 0;
360
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700361 virtual void Load(ManagedRegister dest, ThreadOffset src, size_t size) = 0;
362
Ian Rogers2c8f6532011-09-02 17:16:34 -0700363 virtual void LoadRef(ManagedRegister dest, FrameOffset src) = 0;
364
365 virtual void LoadRef(ManagedRegister dest, ManagedRegister base,
366 MemberOffset offs) = 0;
367
368 virtual void LoadRawPtr(ManagedRegister dest, ManagedRegister base,
369 Offset offs) = 0;
370
371 virtual void LoadRawPtrFromThread(ManagedRegister dest,
372 ThreadOffset offs) = 0;
373
374 // Copying routines
Ian Rogersb5d09b22012-03-06 22:14:17 -0800375 virtual void Move(ManagedRegister dest, ManagedRegister src, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700376
377 virtual void CopyRawPtrFromThread(FrameOffset fr_offs, ThreadOffset thr_offs,
378 ManagedRegister scratch) = 0;
379
380 virtual void CopyRawPtrToThread(ThreadOffset thr_offs, FrameOffset fr_offs,
381 ManagedRegister scratch) = 0;
382
383 virtual void CopyRef(FrameOffset dest, FrameOffset src,
384 ManagedRegister scratch) = 0;
385
Elliott Hughesa09aea22012-01-06 18:58:27 -0800386 virtual void Copy(FrameOffset dest, FrameOffset src, ManagedRegister scratch, size_t size) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700387
Ian Rogersdc51b792011-09-22 20:41:37 -0700388 virtual void Copy(FrameOffset dest, ManagedRegister src_base, Offset src_offset,
389 ManagedRegister scratch, size_t size) = 0;
390
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700391 virtual void Copy(ManagedRegister dest_base, Offset dest_offset, FrameOffset src,
392 ManagedRegister scratch, size_t size) = 0;
393
Ian Rogersdc51b792011-09-22 20:41:37 -0700394 virtual void Copy(FrameOffset dest, FrameOffset 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, Offset dest_offset,
398 ManagedRegister src, Offset src_offset,
399 ManagedRegister scratch, size_t size) = 0;
400
401 virtual void Copy(FrameOffset dest, Offset dest_offset, FrameOffset src, Offset src_offset,
402 ManagedRegister scratch, size_t size) = 0;
Ian Rogersdc51b792011-09-22 20:41:37 -0700403
Ian Rogerse5de95b2011-09-18 20:31:38 -0700404 virtual void MemoryBarrier(ManagedRegister scratch) = 0;
405
jeffhao58136ca2012-05-24 13:40:11 -0700406 // Sign extension
407 virtual void SignExtend(ManagedRegister mreg, size_t size) = 0;
408
jeffhaocee4d0c2012-06-15 14:42:01 -0700409 // Zero extension
410 virtual void ZeroExtend(ManagedRegister mreg, size_t size) = 0;
411
Ian Rogers2c8f6532011-09-02 17:16:34 -0700412 // Exploit fast access in managed code to Thread::Current()
413 virtual void GetCurrentThread(ManagedRegister tr) = 0;
414 virtual void GetCurrentThread(FrameOffset dest_offset,
415 ManagedRegister scratch) = 0;
416
417 // Set up out_reg to hold a Object** into the SIRT, or to be NULL if the
418 // value is null and null_allowed. in_reg holds a possibly stale reference
419 // that can be used to avoid loading the SIRT entry to see if the value is
420 // NULL.
421 virtual void CreateSirtEntry(ManagedRegister out_reg, FrameOffset sirt_offset,
422 ManagedRegister in_reg, bool null_allowed) = 0;
423
424 // Set up out_off to hold a Object** into the SIRT, or to be NULL if the
425 // value is null and null_allowed.
426 virtual void CreateSirtEntry(FrameOffset out_off, FrameOffset sirt_offset,
427 ManagedRegister scratch, bool null_allowed) = 0;
428
429 // src holds a SIRT entry (Object**) load this into dst
430 virtual void LoadReferenceFromSirt(ManagedRegister dst,
431 ManagedRegister src) = 0;
432
433 // Heap::VerifyObject on src. In some cases (such as a reference to this) we
434 // know that src may not be null.
435 virtual void VerifyObject(ManagedRegister src, bool could_be_null) = 0;
436 virtual void VerifyObject(FrameOffset src, bool could_be_null) = 0;
437
438 // Call to address held at [base+offset]
439 virtual void Call(ManagedRegister base, Offset offset,
440 ManagedRegister scratch) = 0;
441 virtual void Call(FrameOffset base, Offset offset,
442 ManagedRegister scratch) = 0;
Ian Rogersbdb03912011-09-14 00:55:44 -0700443 virtual void Call(ThreadOffset offset, ManagedRegister scratch) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700444
Ian Rogers2c8f6532011-09-02 17:16:34 -0700445 // Generate code to check if Thread::Current()->exception_ is non-null
446 // and branch to a ExceptionSlowPath if it is.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 virtual void ExceptionPoll(ManagedRegister scratch, size_t stack_adjust) = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700448
449 virtual ~Assembler() {}
450
451 protected:
452 Assembler() : buffer_() {}
453
454 AssemblerBuffer buffer_;
455};
456
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700457} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700458
Ian Rogers57b86d42012-03-27 16:05:41 -0700459#endif // ART_SRC_OAT_UTILS_ASSEMBLER_H_