1 /*
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  */
16 
17 #ifndef ART_COMPILER_UTILS_ASSEMBLER_H_
18 #define ART_COMPILER_UTILS_ASSEMBLER_H_
19 
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 
24 #include "arch/instruction_set.h"
25 #include "arch/instruction_set_features.h"
26 #include "arm/constants_arm.h"
27 #include "base/arena_allocator.h"
28 #include "base/arena_object.h"
29 #include "base/array_ref.h"
30 #include "base/macros.h"
31 #include "base/memory_region.h"
32 #include "base/pointer_size.h"
33 #include "dwarf/debug_frame_opcode_writer.h"
34 #include "label.h"
35 #include "managed_register.h"
36 #include "offsets.h"
37 #include "x86/constants_x86.h"
38 #include "x86_64/constants_x86_64.h"
39 
40 namespace art HIDDEN {
41 
42 class Assembler;
43 class AssemblerBuffer;
44 
45 // Assembler fixups are positions in generated code that require processing
46 // after the code has been copied to executable memory. This includes building
47 // relocation information.
48 class AssemblerFixup {
49  public:
50   virtual void Process(const MemoryRegion& region, int position) = 0;
~AssemblerFixup()51   virtual ~AssemblerFixup() {}
52 
53  private:
54   AssemblerFixup* previous_;
55   int position_;
56 
previous()57   AssemblerFixup* previous() const { return previous_; }
set_previous(AssemblerFixup * previous_in)58   void set_previous(AssemblerFixup* previous_in) { previous_ = previous_in; }
59 
position()60   int position() const { return position_; }
set_position(int position_in)61   void set_position(int position_in) { position_ = position_in; }
62 
63   friend class AssemblerBuffer;
64 };
65 
66 // Parent of all queued slow paths, emitted during finalization
67 class SlowPath : public DeletableArenaObject<kArenaAllocAssembler> {
68  public:
SlowPath()69   SlowPath() : next_(nullptr) {}
~SlowPath()70   virtual ~SlowPath() {}
71 
Continuation()72   Label* Continuation() { return &continuation_; }
Entry()73   Label* Entry() { return &entry_; }
74   // Generate code for slow path
75   virtual void Emit(Assembler *sp_asm) = 0;
76 
77  protected:
78   // Entry branched to by fast path
79   Label entry_;
80   // Optional continuation that is branched to at the end of the slow path
81   Label continuation_;
82   // Next in linked list of slow paths
83   SlowPath *next_;
84 
85  private:
86   friend class AssemblerBuffer;
87   DISALLOW_COPY_AND_ASSIGN(SlowPath);
88 };
89 
90 class AssemblerBuffer {
91  public:
92   explicit AssemblerBuffer(ArenaAllocator* allocator);
93   ~AssemblerBuffer();
94 
GetAllocator()95   ArenaAllocator* GetAllocator() {
96     return allocator_;
97   }
98 
99   // Basic support for emitting, loading, and storing.
Emit(T value)100   template<typename T> void Emit(T value) {
101     CHECK(HasEnsuredCapacity());
102     *reinterpret_cast<T*>(cursor_) = value;
103     cursor_ += sizeof(T);
104   }
105 
Load(size_t position)106   template<typename T> T Load(size_t position) {
107     CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
108     return *reinterpret_cast<T*>(contents_ + position);
109   }
110 
Store(size_t position,T value)111   template<typename T> void Store(size_t position, T value) {
112     CHECK_LE(position, Size() - static_cast<int>(sizeof(T)));
113     *reinterpret_cast<T*>(contents_ + position) = value;
114   }
115 
Resize(size_t new_size)116   void Resize(size_t new_size) {
117     if (new_size > Capacity()) {
118       ExtendCapacity(new_size);
119     }
120     cursor_ = contents_ + new_size;
121   }
122 
Move(size_t newposition,size_t oldposition,size_t size)123   void Move(size_t newposition, size_t oldposition, size_t size) {
124     // Move a chunk of the buffer from oldposition to newposition.
125     DCHECK_LE(oldposition + size, Size());
126     DCHECK_LE(newposition + size, Size());
127     memmove(contents_ + newposition, contents_ + oldposition, size);
128   }
129 
130   // Emit a fixup at the current location.
EmitFixup(AssemblerFixup * fixup)131   void EmitFixup(AssemblerFixup* fixup) {
132     fixup->set_previous(fixup_);
133     fixup->set_position(Size());
134     fixup_ = fixup;
135   }
136 
EnqueueSlowPath(SlowPath * slowpath)137   void EnqueueSlowPath(SlowPath* slowpath) {
138     if (slow_path_ == nullptr) {
139       slow_path_ = slowpath;
140     } else {
141       SlowPath* cur = slow_path_;
142       for ( ; cur->next_ != nullptr ; cur = cur->next_) {}
143       cur->next_ = slowpath;
144     }
145   }
146 
EmitSlowPaths(Assembler * sp_asm)147   void EmitSlowPaths(Assembler* sp_asm) {
148     SlowPath* cur = slow_path_;
149     SlowPath* next = nullptr;
150     slow_path_ = nullptr;
151     for ( ; cur != nullptr ; cur = next) {
152       cur->Emit(sp_asm);
153       next = cur->next_;
154       delete cur;
155     }
156   }
157 
158   // Get the size of the emitted code.
Size()159   size_t Size() const {
160     CHECK_GE(cursor_, contents_);
161     return cursor_ - contents_;
162   }
163 
contents()164   uint8_t* contents() const { return contents_; }
165 
166   // Copy the assembled instructions into the specified memory block.
167   void CopyInstructions(const MemoryRegion& region);
168 
169   // To emit an instruction to the assembler buffer, the EnsureCapacity helper
170   // must be used to guarantee that the underlying data area is big enough to
171   // hold the emitted instruction. Usage:
172   //
173   //     AssemblerBuffer buffer;
174   //     AssemblerBuffer::EnsureCapacity ensured(&buffer);
175   //     ... emit bytes for single instruction ...
176 
177 #ifndef NDEBUG
178 
179   class EnsureCapacity {
180    public:
EnsureCapacity(AssemblerBuffer * buffer)181     explicit EnsureCapacity(AssemblerBuffer* buffer) {
182       if (buffer->cursor() > buffer->limit()) {
183         buffer->ExtendCapacity(buffer->Size() + kMinimumGap);
184       }
185       // In debug mode, we save the assembler buffer along with the gap
186       // size before we start emitting to the buffer. This allows us to
187       // check that any single generated instruction doesn't overflow the
188       // limit implied by the minimum gap size.
189       buffer_ = buffer;
190       gap_ = ComputeGap();
191       // Make sure that extending the capacity leaves a big enough gap
192       // for any kind of instruction.
193       CHECK_GE(gap_, kMinimumGap);
194       // Mark the buffer as having ensured the capacity.
195       CHECK(!buffer->HasEnsuredCapacity());  // Cannot nest.
196       buffer->has_ensured_capacity_ = true;
197     }
198 
~EnsureCapacity()199     ~EnsureCapacity() {
200       // Unmark the buffer, so we cannot emit after this.
201       buffer_->has_ensured_capacity_ = false;
202       // Make sure the generated instruction doesn't take up more
203       // space than the minimum gap.
204       int delta = gap_ - ComputeGap();
205       CHECK_LE(delta, kMinimumGap);
206     }
207 
208    private:
209     AssemblerBuffer* buffer_;
210     int gap_;
211 
ComputeGap()212     int ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
213   };
214 
215   bool has_ensured_capacity_;
HasEnsuredCapacity()216   bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
217 
218 #else
219 
220   class EnsureCapacity {
221    public:
EnsureCapacity(AssemblerBuffer * buffer)222     explicit EnsureCapacity(AssemblerBuffer* buffer) {
223       if (buffer->cursor() > buffer->limit()) {
224         buffer->ExtendCapacity(buffer->Size() + kMinimumGap);
225       }
226     }
227   };
228 
229   // When building the C++ tests, assertion code is enabled. To allow
230   // asserting that the user of the assembler buffer has ensured the
231   // capacity needed for emitting, we add a placeholder method in non-debug mode.
HasEnsuredCapacity()232   bool HasEnsuredCapacity() const { return true; }
233 
234 #endif
235 
236   // Returns the position in the instruction stream.
GetPosition()237   int GetPosition() { return  cursor_ - contents_; }
238 
Capacity()239   size_t Capacity() const {
240     CHECK_GE(limit_, contents_);
241     return (limit_ - contents_) + kMinimumGap;
242   }
243 
244   // Unconditionally increase the capacity.
245   // The provided `min_capacity` must be higher than current `Capacity()`.
246   void ExtendCapacity(size_t min_capacity);
247 
248   void ProcessFixups();
249 
250  private:
251   // The limit is set to kMinimumGap bytes before the end of the data area.
252   // This leaves enough space for the longest possible instruction and allows
253   // for a single, fast space check per instruction.
254   static constexpr int kMinimumGap = 32;
255 
256   ArenaAllocator* const allocator_;
257   uint8_t* contents_;
258   uint8_t* cursor_;
259   uint8_t* limit_;
260   AssemblerFixup* fixup_;
261 #ifndef NDEBUG
262   bool fixups_processed_;
263 #endif
264 
265   // Head of linked list of slow paths
266   SlowPath* slow_path_;
267 
cursor()268   uint8_t* cursor() const { return cursor_; }
limit()269   uint8_t* limit() const { return limit_; }
270 
271   // Process the fixup chain starting at the given fixup. The offset is
272   // non-zero for fixups in the body if the preamble is non-empty.
273   void ProcessFixups(const MemoryRegion& region);
274 
275   // Compute the limit based on the data area and the capacity. See
276   // description of kMinimumGap for the reasoning behind the value.
ComputeLimit(uint8_t * data,size_t capacity)277   static uint8_t* ComputeLimit(uint8_t* data, size_t capacity) {
278     return data + capacity - kMinimumGap;
279   }
280 
281   friend class AssemblerFixup;
282 };
283 
284 // The purpose of this class is to ensure that we do not have to explicitly
285 // call the AdvancePC method (which is good for convenience and correctness).
286 class DebugFrameOpCodeWriterForAssembler final
287     : public dwarf::DebugFrameOpCodeWriter<> {
288  public:
289   struct DelayedAdvancePC {
290     uint32_t stream_pos;
291     uint32_t pc;
292   };
293 
294   // This method is called the by the opcode writers.
295   void ImplicitlyAdvancePC() final;
296 
DebugFrameOpCodeWriterForAssembler(Assembler * buffer)297   explicit DebugFrameOpCodeWriterForAssembler(Assembler* buffer)
298       : dwarf::DebugFrameOpCodeWriter<>(/* enabled= */ false),
299         assembler_(buffer),
300         delay_emitting_advance_pc_(false),
301         delayed_advance_pcs_() {
302   }
303 
~DebugFrameOpCodeWriterForAssembler()304   ~DebugFrameOpCodeWriterForAssembler() {
305     DCHECK(delayed_advance_pcs_.empty());
306   }
307 
308   // Tell the writer to delay emitting advance PC info.
309   // The assembler must explicitly process all the delayed advances.
DelayEmittingAdvancePCs()310   void DelayEmittingAdvancePCs() {
311     delay_emitting_advance_pc_ = true;
312   }
313 
314   // Override the last delayed PC. The new PC can be out of order.
OverrideDelayedPC(size_t pc)315   void OverrideDelayedPC(size_t pc) {
316     DCHECK(delay_emitting_advance_pc_);
317     if (enabled_) {
318       DCHECK(!delayed_advance_pcs_.empty());
319       delayed_advance_pcs_.back().pc = pc;
320     }
321   }
322 
323   // Return the number of delayed advance PC entries.
NumberOfDelayedAdvancePCs()324   size_t NumberOfDelayedAdvancePCs() const {
325     return delayed_advance_pcs_.size();
326   }
327 
328   // Release the CFI stream and advance PC infos so that the assembler can patch it.
329   std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>>
ReleaseStreamAndPrepareForDelayedAdvancePC()330   ReleaseStreamAndPrepareForDelayedAdvancePC() {
331     DCHECK(delay_emitting_advance_pc_);
332     delay_emitting_advance_pc_ = false;
333     std::pair<std::vector<uint8_t>, std::vector<DelayedAdvancePC>> result;
334     result.first.swap(opcodes_);
335     result.second.swap(delayed_advance_pcs_);
336     return result;
337   }
338 
339   // Reserve space for the CFI stream.
ReserveCFIStream(size_t capacity)340   void ReserveCFIStream(size_t capacity) {
341     opcodes_.reserve(capacity);
342   }
343 
344   // Append raw data to the CFI stream.
AppendRawData(const std::vector<uint8_t> & raw_data,size_t first,size_t last)345   void AppendRawData(const std::vector<uint8_t>& raw_data, size_t first, size_t last) {
346     DCHECK_LE(0u, first);
347     DCHECK_LE(first, last);
348     DCHECK_LE(last, raw_data.size());
349     opcodes_.insert(opcodes_.end(), raw_data.begin() + first, raw_data.begin() + last);
350   }
351 
352  private:
353   Assembler* assembler_;
354   bool delay_emitting_advance_pc_;
355   std::vector<DelayedAdvancePC> delayed_advance_pcs_;
356 };
357 
358 class Assembler : public DeletableArenaObject<kArenaAllocAssembler> {
359  public:
360   // Finalize the code; emit slow paths, fixup branches, add literal pool, etc.
FinalizeCode()361   virtual void FinalizeCode() {
362     buffer_.EmitSlowPaths(this);
363     buffer_.ProcessFixups();
364   }
365 
366   // Size of generated code
CodeSize()367   virtual size_t CodeSize() const { return buffer_.Size(); }
CodeBufferBaseAddress()368   virtual const uint8_t* CodeBufferBaseAddress() const { return buffer_.contents(); }
369   // CodePosition() is a non-const method similar to CodeSize(), which is used to
370   // record positions within the code buffer for the purpose of signal handling
371   // (stack overflow checks and implicit null checks may trigger signals and the
372   // signal handlers expect them right before the recorded positions).
373   // On most architectures CodePosition() should be equivalent to CodeSize(), but
374   // the MIPS assembler needs to be aware of this recording, so it doesn't put
375   // the instructions that can trigger signals into branch delay slots. Handling
376   // signals from instructions in delay slots is a bit problematic and should be
377   // avoided.
378   // TODO: Re-evaluate whether we still need this now that MIPS support has been removed.
CodePosition()379   virtual size_t CodePosition() { return CodeSize(); }
380 
381   // Copy instructions out of assembly buffer into the given region of memory
CopyInstructions(const MemoryRegion & region)382   virtual void CopyInstructions(const MemoryRegion& region) {
383     buffer_.CopyInstructions(region);
384   }
385 
386   // TODO: Implement with disassembler.
Comment(const char * format,...)387   virtual void Comment([[maybe_unused]] const char* format, ...) {}
388 
389   virtual void Bind(Label* label) = 0;
390   virtual void Jump(Label* label) = 0;
391 
~Assembler()392   virtual ~Assembler() {}
393 
394   /**
395    * @brief Buffer of DWARF's Call Frame Information opcodes.
396    * @details It is used by debuggers and other tools to unwind the call stack.
397    */
cfi()398   DebugFrameOpCodeWriterForAssembler& cfi() { return cfi_; }
399 
GetAllocator()400   ArenaAllocator* GetAllocator() {
401     return buffer_.GetAllocator();
402   }
403 
GetBuffer()404   AssemblerBuffer* GetBuffer() {
405     return &buffer_;
406   }
407 
408  protected:
Assembler(ArenaAllocator * allocator)409   explicit Assembler(ArenaAllocator* allocator) : buffer_(allocator), cfi_(this) {}
410 
411   AssemblerBuffer buffer_;
412 
413   DebugFrameOpCodeWriterForAssembler cfi_;
414 };
415 
416 enum ScaleFactor {
417   TIMES_1 = 0,
418   TIMES_2 = 1,
419   TIMES_4 = 2,
420   TIMES_8 = 3
421 };
422 
423 }  // namespace art
424 
425 #endif  // ART_COMPILER_UTILS_ASSEMBLER_H_
426