blob: cf65d948dd1aa2aa72bfa9bb013b88c6645fa24b [file] [log] [blame]
Jan Voungf76fd372014-10-16 15:39:22 -07001//===- subzero/src/assembler.h - Integrated assembler -----------*- C++ -*-===//
Jan Voung8acded02014-09-22 18:02:25 -07002// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3// for details. All rights reserved. Use of this source code is governed by a
4// BSD-style license that can be found in the LICENSE file.
5//
6// Modified by the Subzero authors.
7//
Jan Voungf76fd372014-10-16 15:39:22 -07008//===----------------------------------------------------------------------===//
Jan Voung8acded02014-09-22 18:02:25 -07009//
10// The Subzero Code Generator
11//
12// This file is distributed under the University of Illinois Open Source
13// License. See LICENSE.TXT for details.
14//
15//===----------------------------------------------------------------------===//
16//
17// This file declares the Assembler base class. Instructions are assembled
18// by architecture-specific assemblers that derive from this base class.
19// This base class manages buffers and fixups for emitting code, etc.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef SUBZERO_SRC_ASSEMBLER_H
24#define SUBZERO_SRC_ASSEMBLER_H
25
26#include "IceDefs.h"
Jan Voung8acded02014-09-22 18:02:25 -070027#include "IceFixups.h"
Jan Voung8acded02014-09-22 18:02:25 -070028
29namespace Ice {
30
Jan Voung8acded02014-09-22 18:02:25 -070031// Assembler buffers are used to emit binary code. They grow on demand.
32class AssemblerBuffer {
Jan Voungf76fd372014-10-16 15:39:22 -070033 AssemblerBuffer(const AssemblerBuffer &) = delete;
34 AssemblerBuffer &operator=(const AssemblerBuffer &) = delete;
35
Jan Voung8acded02014-09-22 18:02:25 -070036public:
37 AssemblerBuffer(Assembler &);
38 ~AssemblerBuffer();
39
40 // Basic support for emitting, loading, and storing.
41 template <typename T> void Emit(T value) {
42 assert(HasEnsuredCapacity());
43 *reinterpret_cast<T *>(cursor_) = value;
44 cursor_ += sizeof(T);
45 }
46
47 template <typename T> T Load(intptr_t position) const {
48 assert(position >= 0 &&
49 position <= (Size() - static_cast<intptr_t>(sizeof(T))));
50 return *reinterpret_cast<T *>(contents_ + position);
51 }
52
53 template <typename T> void Store(intptr_t position, T value) {
54 assert(position >= 0 &&
55 position <= (Size() - static_cast<intptr_t>(sizeof(T))));
56 *reinterpret_cast<T *>(contents_ + position) = value;
57 }
58
59 // Emit a fixup at the current location.
Jim Stichnothdd842db2015-01-27 12:53:53 -080060 void EmitFixup(AssemblerFixup *fixup) { fixup->set_position(Size()); }
Jan Voung8acded02014-09-22 18:02:25 -070061
62 // Get the size of the emitted code.
63 intptr_t Size() const { return cursor_ - contents_; }
64 uintptr_t contents() const { return contents_; }
65
Jan Voung8acded02014-09-22 18:02:25 -070066// To emit an instruction to the assembler buffer, the EnsureCapacity helper
67// must be used to guarantee that the underlying data area is big enough to
68// hold the emitted instruction. Usage:
69//
70// AssemblerBuffer buffer;
71// AssemblerBuffer::EnsureCapacity ensured(&buffer);
72// ... emit bytes for single instruction ...
73
Jan Voungf76fd372014-10-16 15:39:22 -070074#ifndef NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070075 class EnsureCapacity {
Jan Voungf76fd372014-10-16 15:39:22 -070076 EnsureCapacity(const EnsureCapacity &) = delete;
77 EnsureCapacity &operator=(const EnsureCapacity &) = delete;
78
Jan Voung8acded02014-09-22 18:02:25 -070079 public:
80 explicit EnsureCapacity(AssemblerBuffer *buffer);
81 ~EnsureCapacity();
82
83 private:
84 AssemblerBuffer *buffer_;
85 intptr_t gap_;
86
87 intptr_t ComputeGap() { return buffer_->Capacity() - buffer_->Size(); }
88 };
89
90 bool has_ensured_capacity_;
91 bool HasEnsuredCapacity() const { return has_ensured_capacity_; }
Jan Voungf76fd372014-10-16 15:39:22 -070092#else // NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070093 class EnsureCapacity {
Jan Voungf76fd372014-10-16 15:39:22 -070094 EnsureCapacity(const EnsureCapacity &) = delete;
95 EnsureCapacity &operator=(const EnsureCapacity &) = delete;
96
Jan Voung8acded02014-09-22 18:02:25 -070097 public:
98 explicit EnsureCapacity(AssemblerBuffer *buffer) {
99 if (buffer->cursor() >= buffer->limit())
100 buffer->ExtendCapacity();
101 }
102 };
103
104 // When building the C++ tests, assertion code is enabled. To allow
105 // asserting that the user of the assembler buffer has ensured the
106 // capacity needed for emitting, we add a dummy method in non-debug mode.
107 bool HasEnsuredCapacity() const { return true; }
Jan Voungf76fd372014-10-16 15:39:22 -0700108#endif // NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -0700109
110 // Returns the position in the instruction stream.
111 intptr_t GetPosition() const { return cursor_ - contents_; }
112
Jan Voungec270732015-01-12 17:00:22 -0800113 // Create and track a fixup in the current function.
114 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value);
115
116 const FixupRefList &fixups() const { return fixups_; }
Jan Voung8acded02014-09-22 18:02:25 -0700117
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800118 void setSize(intptr_t NewSize) {
119 assert(NewSize <= Size());
120 cursor_ = contents_ + NewSize;
121 }
122
Jan Voung8acded02014-09-22 18:02:25 -0700123private:
124 // The limit is set to kMinimumGap bytes before the end of the data area.
125 // This leaves enough space for the longest possible instruction and allows
126 // for a single, fast space check per instruction.
127 static const intptr_t kMinimumGap = 32;
128
129 uintptr_t contents_;
130 uintptr_t cursor_;
131 uintptr_t limit_;
132 Assembler &assembler_;
Jan Voungec270732015-01-12 17:00:22 -0800133 // List of pool-allocated fixups relative to the current function.
134 FixupRefList fixups_;
Jan Voung8acded02014-09-22 18:02:25 -0700135
136 uintptr_t cursor() const { return cursor_; }
137 uintptr_t limit() const { return limit_; }
138 intptr_t Capacity() const {
139 assert(limit_ >= contents_);
140 return (limit_ - contents_) + kMinimumGap;
141 }
142
Jan Voung8acded02014-09-22 18:02:25 -0700143 // Compute the limit based on the data area and the capacity. See
144 // description of kMinimumGap for the reasoning behind the value.
145 static uintptr_t ComputeLimit(uintptr_t data, intptr_t capacity) {
146 return data + capacity - kMinimumGap;
147 }
148
149 void ExtendCapacity();
Jan Voung8acded02014-09-22 18:02:25 -0700150};
151
152class Assembler {
Jan Voungf76fd372014-10-16 15:39:22 -0700153 Assembler(const Assembler &) = delete;
154 Assembler &operator=(const Assembler &) = delete;
155
Jan Voung8acded02014-09-22 18:02:25 -0700156public:
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800157 Assembler()
158 : FunctionName(""), IsInternal(false), Preliminary(false),
159 buffer_(*this) {}
Jan Voung7e1e4852014-10-24 10:29:30 -0700160 virtual ~Assembler() {}
Jan Voung8acded02014-09-22 18:02:25 -0700161
162 // Allocate a chunk of bytes using the per-Assembler allocator.
163 uintptr_t AllocateBytes(size_t bytes) {
164 // For now, alignment is not related to NaCl bundle alignment, since
165 // the buffer's GetPosition is relative to the base. So NaCl bundle
166 // alignment checks can be relative to that base. Later, the buffer
167 // will be copied out to a ".text" section (or an in memory-buffer
168 // that can be mprotect'ed with executable permission), and that
169 // second buffer should be aligned for NaCl.
170 const size_t Alignment = 16;
171 return reinterpret_cast<uintptr_t>(Allocator.Allocate(bytes, Alignment));
172 }
173
174 // Allocate data of type T using the per-Assembler allocator.
175 template <typename T> T *Allocate() { return Allocator.Allocate<T>(); }
176
Jan Voung08c3bcd2014-12-01 17:55:16 -0800177 // Align the tail end of the function to the required target alignment.
178 virtual void alignFunction() = 0;
179
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800180 // Add nop padding of a particular width to the current bundle.
181 virtual void padWithNop(intptr_t Padding) = 0;
182
Jan Voung08c3bcd2014-12-01 17:55:16 -0800183 virtual SizeT getBundleAlignLog2Bytes() const = 0;
184
Jan Voungb2d50842015-05-12 09:53:50 -0700185 virtual const char *getNonExecPadDirective() const = 0;
Jan Voung08c3bcd2014-12-01 17:55:16 -0800186 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0;
187
188 // Mark the current text location as the start of a CFG node
189 // (represented by NodeNumber).
Jan Voung7e1e4852014-10-24 10:29:30 -0700190 virtual void BindCfgNodeLabel(SizeT NodeNumber) = 0;
191
Jan Voungec270732015-01-12 17:00:22 -0800192 virtual bool fixupIsPCRel(FixupKind Kind) const = 0;
193
Jan Voung08c3bcd2014-12-01 17:55:16 -0800194 // Return a view of all the bytes of code for the current function.
195 llvm::StringRef getBufferView() const;
196
Jan Voungec270732015-01-12 17:00:22 -0800197 const FixupRefList &fixups() const { return buffer_.fixups(); }
198
199 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value) {
200 return buffer_.createFixup(Kind, Value);
201 }
202
Jan Voung0faec4c2014-11-05 17:29:56 -0800203 void emitIASBytes(GlobalContext *Ctx) const;
Jim Stichnothbbca7542015-02-11 16:08:31 -0800204 bool getInternal() const { return IsInternal; }
205 void setInternal(bool Internal) { IsInternal = Internal; }
206 const IceString &getFunctionName() { return FunctionName; }
207 void setFunctionName(const IceString &NewName) { FunctionName = NewName; }
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800208 intptr_t getBufferSize() const { return buffer_.Size(); }
209 // Roll back to a (smaller) size.
210 void setBufferSize(intptr_t NewSize) { buffer_.setSize(NewSize); }
211 void setPreliminary(bool Value) { Preliminary = Value; }
212 bool getPreliminary() const { return Preliminary; }
Jan Voung0faec4c2014-11-05 17:29:56 -0800213
Jan Voung8acded02014-09-22 18:02:25 -0700214private:
Jan Voung1d62cf02015-01-09 14:57:32 -0800215 ArenaAllocator<32 * 1024> Allocator;
Jim Stichnothbbca7542015-02-11 16:08:31 -0800216 // FunctionName and IsInternal are transferred from the original Cfg
217 // object, since the Cfg object may be deleted by the time the
218 // assembler buffer is emitted.
219 IceString FunctionName;
220 bool IsInternal;
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -0800221 // Preliminary indicates whether a preliminary pass is being made
222 // for calculating bundle padding (Preliminary=true), versus the
223 // final pass where all changes to label bindings, label links, and
224 // relocation fixups are fully committed (Preliminary=false).
225 bool Preliminary;
Jan Voung0faec4c2014-11-05 17:29:56 -0800226
227protected:
228 AssemblerBuffer buffer_;
Jan Voung8acded02014-09-22 18:02:25 -0700229};
230
231} // end of namespace Ice
232
233#endif // SUBZERO_SRC_ASSEMBLER_H_