OLD | NEW |
1 //===- subzero/src/assembler.h - Integrated assembler -----------*- C++ -*-===// | 1 //===- subzero/src/assembler.h - Integrated assembler -----------*- C++ -*-===// |
2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 // 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 | 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. | 4 // BSD-style license that can be found in the LICENSE file. |
5 // | 5 // |
6 // Modified by the Subzero authors. | 6 // Modified by the Subzero authors. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // The Subzero Code Generator | 10 // The Subzero Code Generator |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 #endif // NDEBUG | 108 #endif // NDEBUG |
109 | 109 |
110 // Returns the position in the instruction stream. | 110 // Returns the position in the instruction stream. |
111 intptr_t GetPosition() const { return cursor_ - contents_; } | 111 intptr_t GetPosition() const { return cursor_ - contents_; } |
112 | 112 |
113 // Create and track a fixup in the current function. | 113 // Create and track a fixup in the current function. |
114 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value); | 114 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value); |
115 | 115 |
116 const FixupRefList &fixups() const { return fixups_; } | 116 const FixupRefList &fixups() const { return fixups_; } |
117 | 117 |
| 118 void setSize(intptr_t NewSize) { |
| 119 assert(NewSize <= Size()); |
| 120 cursor_ = contents_ + NewSize; |
| 121 } |
| 122 |
118 private: | 123 private: |
119 // The limit is set to kMinimumGap bytes before the end of the data area. | 124 // The limit is set to kMinimumGap bytes before the end of the data area. |
120 // This leaves enough space for the longest possible instruction and allows | 125 // This leaves enough space for the longest possible instruction and allows |
121 // for a single, fast space check per instruction. | 126 // for a single, fast space check per instruction. |
122 static const intptr_t kMinimumGap = 32; | 127 static const intptr_t kMinimumGap = 32; |
123 | 128 |
124 uintptr_t contents_; | 129 uintptr_t contents_; |
125 uintptr_t cursor_; | 130 uintptr_t cursor_; |
126 uintptr_t limit_; | 131 uintptr_t limit_; |
127 Assembler &assembler_; | 132 Assembler &assembler_; |
(...skipping 14 matching lines...) Expand all Loading... |
142 } | 147 } |
143 | 148 |
144 void ExtendCapacity(); | 149 void ExtendCapacity(); |
145 }; | 150 }; |
146 | 151 |
147 class Assembler { | 152 class Assembler { |
148 Assembler(const Assembler &) = delete; | 153 Assembler(const Assembler &) = delete; |
149 Assembler &operator=(const Assembler &) = delete; | 154 Assembler &operator=(const Assembler &) = delete; |
150 | 155 |
151 public: | 156 public: |
152 Assembler() : FunctionName(""), IsInternal(false), buffer_(*this) {} | 157 Assembler() |
| 158 : FunctionName(""), IsInternal(false), Preliminary(false), |
| 159 buffer_(*this) {} |
153 virtual ~Assembler() {} | 160 virtual ~Assembler() {} |
154 | 161 |
155 // Allocate a chunk of bytes using the per-Assembler allocator. | 162 // Allocate a chunk of bytes using the per-Assembler allocator. |
156 uintptr_t AllocateBytes(size_t bytes) { | 163 uintptr_t AllocateBytes(size_t bytes) { |
157 // For now, alignment is not related to NaCl bundle alignment, since | 164 // For now, alignment is not related to NaCl bundle alignment, since |
158 // the buffer's GetPosition is relative to the base. So NaCl bundle | 165 // the buffer's GetPosition is relative to the base. So NaCl bundle |
159 // alignment checks can be relative to that base. Later, the buffer | 166 // alignment checks can be relative to that base. Later, the buffer |
160 // will be copied out to a ".text" section (or an in memory-buffer | 167 // will be copied out to a ".text" section (or an in memory-buffer |
161 // that can be mprotect'ed with executable permission), and that | 168 // that can be mprotect'ed with executable permission), and that |
162 // second buffer should be aligned for NaCl. | 169 // second buffer should be aligned for NaCl. |
163 const size_t Alignment = 16; | 170 const size_t Alignment = 16; |
164 return reinterpret_cast<uintptr_t>(Allocator.Allocate(bytes, Alignment)); | 171 return reinterpret_cast<uintptr_t>(Allocator.Allocate(bytes, Alignment)); |
165 } | 172 } |
166 | 173 |
167 // Allocate data of type T using the per-Assembler allocator. | 174 // Allocate data of type T using the per-Assembler allocator. |
168 template <typename T> T *Allocate() { return Allocator.Allocate<T>(); } | 175 template <typename T> T *Allocate() { return Allocator.Allocate<T>(); } |
169 | 176 |
170 // Align the tail end of the function to the required target alignment. | 177 // Align the tail end of the function to the required target alignment. |
171 virtual void alignFunction() = 0; | 178 virtual void alignFunction() = 0; |
172 | 179 |
| 180 // Add nop padding of a particular width to the current bundle. |
| 181 virtual void padWithNop(intptr_t Padding) = 0; |
| 182 |
173 virtual SizeT getBundleAlignLog2Bytes() const = 0; | 183 virtual SizeT getBundleAlignLog2Bytes() const = 0; |
174 | 184 |
175 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0; | 185 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0; |
176 | 186 |
177 // Mark the current text location as the start of a CFG node | 187 // Mark the current text location as the start of a CFG node |
178 // (represented by NodeNumber). | 188 // (represented by NodeNumber). |
179 virtual void BindCfgNodeLabel(SizeT NodeNumber) = 0; | 189 virtual void BindCfgNodeLabel(SizeT NodeNumber) = 0; |
180 | 190 |
181 virtual bool fixupIsPCRel(FixupKind Kind) const = 0; | 191 virtual bool fixupIsPCRel(FixupKind Kind) const = 0; |
182 | 192 |
183 // Return a view of all the bytes of code for the current function. | 193 // Return a view of all the bytes of code for the current function. |
184 llvm::StringRef getBufferView() const; | 194 llvm::StringRef getBufferView() const; |
185 | 195 |
186 const FixupRefList &fixups() const { return buffer_.fixups(); } | 196 const FixupRefList &fixups() const { return buffer_.fixups(); } |
187 | 197 |
188 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value) { | 198 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value) { |
189 return buffer_.createFixup(Kind, Value); | 199 return buffer_.createFixup(Kind, Value); |
190 } | 200 } |
191 | 201 |
192 void emitIASBytes(GlobalContext *Ctx) const; | 202 void emitIASBytes(GlobalContext *Ctx) const; |
193 bool getInternal() const { return IsInternal; } | 203 bool getInternal() const { return IsInternal; } |
194 void setInternal(bool Internal) { IsInternal = Internal; } | 204 void setInternal(bool Internal) { IsInternal = Internal; } |
195 const IceString &getFunctionName() { return FunctionName; } | 205 const IceString &getFunctionName() { return FunctionName; } |
196 void setFunctionName(const IceString &NewName) { FunctionName = NewName; } | 206 void setFunctionName(const IceString &NewName) { FunctionName = NewName; } |
| 207 intptr_t getBufferSize() const { return buffer_.Size(); } |
| 208 // Roll back to a (smaller) size. |
| 209 void setBufferSize(intptr_t NewSize) { buffer_.setSize(NewSize); } |
| 210 void setPreliminary(bool Value) { Preliminary = Value; } |
| 211 bool getPreliminary() const { return Preliminary; } |
197 | 212 |
198 private: | 213 private: |
199 ArenaAllocator<32 * 1024> Allocator; | 214 ArenaAllocator<32 * 1024> Allocator; |
200 // FunctionName and IsInternal are transferred from the original Cfg | 215 // FunctionName and IsInternal are transferred from the original Cfg |
201 // object, since the Cfg object may be deleted by the time the | 216 // object, since the Cfg object may be deleted by the time the |
202 // assembler buffer is emitted. | 217 // assembler buffer is emitted. |
203 IceString FunctionName; | 218 IceString FunctionName; |
204 bool IsInternal; | 219 bool IsInternal; |
| 220 // Preliminary indicates whether a preliminary pass is being made |
| 221 // for calculating bundle padding (Preliminary=true), versus the |
| 222 // final pass where all changes to label bindings, label links, and |
| 223 // relocation fixups are fully committed (Preliminary=false). |
| 224 bool Preliminary; |
205 | 225 |
206 protected: | 226 protected: |
207 AssemblerBuffer buffer_; | 227 AssemblerBuffer buffer_; |
208 }; | 228 }; |
209 | 229 |
210 } // end of namespace Ice | 230 } // end of namespace Ice |
211 | 231 |
212 #endif // SUBZERO_SRC_ASSEMBLER_H_ | 232 #endif // SUBZERO_SRC_ASSEMBLER_H_ |
OLD | NEW |