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 10 matching lines...) Expand all Loading... |
21 //===----------------------------------------------------------------------===// | 21 //===----------------------------------------------------------------------===// |
22 | 22 |
23 #ifndef SUBZERO_SRC_ASSEMBLER_H | 23 #ifndef SUBZERO_SRC_ASSEMBLER_H |
24 #define SUBZERO_SRC_ASSEMBLER_H | 24 #define SUBZERO_SRC_ASSEMBLER_H |
25 | 25 |
26 #include "IceDefs.h" | 26 #include "IceDefs.h" |
27 #include "IceFixups.h" | 27 #include "IceFixups.h" |
28 | 28 |
29 namespace Ice { | 29 namespace Ice { |
30 | 30 |
31 // Forward declarations. | |
32 class Assembler; | |
33 class AssemblerFixup; | |
34 class AssemblerBuffer; | |
35 class ConstantRelocatable; | |
36 | |
37 // Assembler fixups are positions in generated code that hold relocation | |
38 // information that needs to be processed before finalizing the code | |
39 // into executable memory. | |
40 class AssemblerFixup { | |
41 AssemblerFixup(const AssemblerFixup &) = delete; | |
42 AssemblerFixup &operator=(const AssemblerFixup &) = delete; | |
43 | |
44 public: | |
45 | |
46 // It would be ideal if the destructor method could be made private, | |
47 // but the g++ compiler complains when this is subclassed. | |
48 virtual ~AssemblerFixup() { llvm_unreachable("~AssemblerFixup used"); } | |
49 | |
50 intptr_t position() const { return position_; } | |
51 | |
52 FixupKind kind() const { return kind_; } | |
53 | |
54 RelocOffsetT offset() const; | |
55 | |
56 IceString symbol(GlobalContext *Ctx) const; | |
57 | |
58 void emit(GlobalContext *Ctx) const; | |
59 | |
60 protected: | |
61 AssemblerFixup(FixupKind Kind, const Constant *Value) | |
62 : position_(0), kind_(Kind), value_(Value) {} | |
63 | |
64 private: | |
65 intptr_t position_; | |
66 FixupKind kind_; | |
67 const Constant *value_; | |
68 | |
69 void set_position(intptr_t position) { position_ = position; } | |
70 | |
71 friend class AssemblerBuffer; | |
72 }; | |
73 | |
74 // Assembler buffers are used to emit binary code. They grow on demand. | 31 // Assembler buffers are used to emit binary code. They grow on demand. |
75 class AssemblerBuffer { | 32 class AssemblerBuffer { |
76 AssemblerBuffer(const AssemblerBuffer &) = delete; | 33 AssemblerBuffer(const AssemblerBuffer &) = delete; |
77 AssemblerBuffer &operator=(const AssemblerBuffer &) = delete; | 34 AssemblerBuffer &operator=(const AssemblerBuffer &) = delete; |
78 | 35 |
79 public: | 36 public: |
80 AssemblerBuffer(Assembler &); | 37 AssemblerBuffer(Assembler &); |
81 ~AssemblerBuffer(); | 38 ~AssemblerBuffer(); |
82 | 39 |
83 // Basic support for emitting, loading, and storing. | 40 // Basic support for emitting, loading, and storing. |
(...skipping 11 matching lines...) Expand all Loading... |
95 | 52 |
96 template <typename T> void Store(intptr_t position, T value) { | 53 template <typename T> void Store(intptr_t position, T value) { |
97 assert(position >= 0 && | 54 assert(position >= 0 && |
98 position <= (Size() - static_cast<intptr_t>(sizeof(T)))); | 55 position <= (Size() - static_cast<intptr_t>(sizeof(T)))); |
99 *reinterpret_cast<T *>(contents_ + position) = value; | 56 *reinterpret_cast<T *>(contents_ + position) = value; |
100 } | 57 } |
101 | 58 |
102 // Emit a fixup at the current location. | 59 // Emit a fixup at the current location. |
103 void EmitFixup(AssemblerFixup *fixup) { | 60 void EmitFixup(AssemblerFixup *fixup) { |
104 fixup->set_position(Size()); | 61 fixup->set_position(Size()); |
105 fixups_.push_back(fixup); | |
106 } | 62 } |
107 | 63 |
108 // Get the size of the emitted code. | 64 // Get the size of the emitted code. |
109 intptr_t Size() const { return cursor_ - contents_; } | 65 intptr_t Size() const { return cursor_ - contents_; } |
110 uintptr_t contents() const { return contents_; } | 66 uintptr_t contents() const { return contents_; } |
111 | 67 |
112 // To emit an instruction to the assembler buffer, the EnsureCapacity helper | 68 // To emit an instruction to the assembler buffer, the EnsureCapacity helper |
113 // must be used to guarantee that the underlying data area is big enough to | 69 // must be used to guarantee that the underlying data area is big enough to |
114 // hold the emitted instruction. Usage: | 70 // hold the emitted instruction. Usage: |
115 // | 71 // |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 105 |
150 // When building the C++ tests, assertion code is enabled. To allow | 106 // When building the C++ tests, assertion code is enabled. To allow |
151 // asserting that the user of the assembler buffer has ensured the | 107 // asserting that the user of the assembler buffer has ensured the |
152 // capacity needed for emitting, we add a dummy method in non-debug mode. | 108 // capacity needed for emitting, we add a dummy method in non-debug mode. |
153 bool HasEnsuredCapacity() const { return true; } | 109 bool HasEnsuredCapacity() const { return true; } |
154 #endif // NDEBUG | 110 #endif // NDEBUG |
155 | 111 |
156 // Returns the position in the instruction stream. | 112 // Returns the position in the instruction stream. |
157 intptr_t GetPosition() const { return cursor_ - contents_; } | 113 intptr_t GetPosition() const { return cursor_ - contents_; } |
158 | 114 |
159 // List of pool-allocated fixups. | 115 // Create and track a fixup in the current function. |
160 typedef std::vector<AssemblerFixup *> FixupList; | 116 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value); |
161 FixupList::const_iterator fixups_begin() const { return fixups_.begin(); } | 117 |
162 FixupList::const_iterator fixups_end() const { return fixups_.end(); } | 118 const FixupRefList &fixups() const { return fixups_; } |
163 | 119 |
164 private: | 120 private: |
165 // The limit is set to kMinimumGap bytes before the end of the data area. | 121 // The limit is set to kMinimumGap bytes before the end of the data area. |
166 // This leaves enough space for the longest possible instruction and allows | 122 // This leaves enough space for the longest possible instruction and allows |
167 // for a single, fast space check per instruction. | 123 // for a single, fast space check per instruction. |
168 static const intptr_t kMinimumGap = 32; | 124 static const intptr_t kMinimumGap = 32; |
169 | 125 |
170 uintptr_t contents_; | 126 uintptr_t contents_; |
171 uintptr_t cursor_; | 127 uintptr_t cursor_; |
172 uintptr_t limit_; | 128 uintptr_t limit_; |
173 Assembler &assembler_; | 129 Assembler &assembler_; |
174 FixupList fixups_; | 130 // List of pool-allocated fixups relative to the current function. |
175 #ifndef NDEBUG | 131 FixupRefList fixups_; |
176 bool fixups_processed_; | |
177 #endif // !NDEBUG | |
178 | 132 |
179 uintptr_t cursor() const { return cursor_; } | 133 uintptr_t cursor() const { return cursor_; } |
180 uintptr_t limit() const { return limit_; } | 134 uintptr_t limit() const { return limit_; } |
181 intptr_t Capacity() const { | 135 intptr_t Capacity() const { |
182 assert(limit_ >= contents_); | 136 assert(limit_ >= contents_); |
183 return (limit_ - contents_) + kMinimumGap; | 137 return (limit_ - contents_) + kMinimumGap; |
184 } | 138 } |
185 | 139 |
186 // Compute the limit based on the data area and the capacity. See | 140 // Compute the limit based on the data area and the capacity. See |
187 // description of kMinimumGap for the reasoning behind the value. | 141 // description of kMinimumGap for the reasoning behind the value. |
188 static uintptr_t ComputeLimit(uintptr_t data, intptr_t capacity) { | 142 static uintptr_t ComputeLimit(uintptr_t data, intptr_t capacity) { |
189 return data + capacity - kMinimumGap; | 143 return data + capacity - kMinimumGap; |
190 } | 144 } |
191 | 145 |
192 void ExtendCapacity(); | 146 void ExtendCapacity(); |
193 | |
194 friend class AssemblerFixup; | |
195 }; | 147 }; |
196 | 148 |
197 class Assembler { | 149 class Assembler { |
198 Assembler(const Assembler &) = delete; | 150 Assembler(const Assembler &) = delete; |
199 Assembler &operator=(const Assembler &) = delete; | 151 Assembler &operator=(const Assembler &) = delete; |
200 | 152 |
201 public: | 153 public: |
202 Assembler() : buffer_(*this) {} | 154 Assembler() : buffer_(*this) {} |
203 virtual ~Assembler() {} | 155 virtual ~Assembler() {} |
204 | 156 |
(...skipping 16 matching lines...) Expand all Loading... |
221 virtual void alignFunction() = 0; | 173 virtual void alignFunction() = 0; |
222 | 174 |
223 virtual SizeT getBundleAlignLog2Bytes() const = 0; | 175 virtual SizeT getBundleAlignLog2Bytes() const = 0; |
224 | 176 |
225 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0; | 177 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0; |
226 | 178 |
227 // Mark the current text location as the start of a CFG node | 179 // Mark the current text location as the start of a CFG node |
228 // (represented by NodeNumber). | 180 // (represented by NodeNumber). |
229 virtual void BindCfgNodeLabel(SizeT NodeNumber) = 0; | 181 virtual void BindCfgNodeLabel(SizeT NodeNumber) = 0; |
230 | 182 |
| 183 virtual bool fixupIsPCRel(FixupKind Kind) const = 0; |
| 184 |
231 // Return a view of all the bytes of code for the current function. | 185 // Return a view of all the bytes of code for the current function. |
232 llvm::StringRef getBufferView() const; | 186 llvm::StringRef getBufferView() const; |
233 | 187 |
| 188 const FixupRefList &fixups() const { return buffer_.fixups(); } |
| 189 |
| 190 AssemblerFixup *createFixup(FixupKind Kind, const Constant *Value) { |
| 191 return buffer_.createFixup(Kind, Value); |
| 192 } |
| 193 |
234 void emitIASBytes(GlobalContext *Ctx) const; | 194 void emitIASBytes(GlobalContext *Ctx) const; |
235 | 195 |
236 private: | 196 private: |
237 ArenaAllocator<32 * 1024> Allocator; | 197 ArenaAllocator<32 * 1024> Allocator; |
238 | 198 |
239 protected: | 199 protected: |
240 AssemblerBuffer buffer_; | 200 AssemblerBuffer buffer_; |
241 }; | 201 }; |
242 | 202 |
243 } // end of namespace Ice | 203 } // end of namespace Ice |
244 | 204 |
245 #endif // SUBZERO_SRC_ASSEMBLER_H_ | 205 #endif // SUBZERO_SRC_ASSEMBLER_H_ |
OLD | NEW |