1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/DataTypes.h"
26 class MCFragment : public ilist_node<MCFragment> {
27 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
28 void operator=(const MCFragment&); // DO NOT IMPLEMENT
41 /// @name Assembler Backend Data
44 // FIXME: This could all be kept private to the assembler implementation.
46 /// FileOffset - The offset of this section in the object file. This is ~0
47 /// until initialized.
50 /// FileSize - The size of this section in the object file. This is ~0 until
57 MCFragment(FragmentType _Kind, MCSectionData *SD = 0);
62 virtual ~MCFragment();
64 FragmentType getKind() const { return Kind; }
66 // FIXME: This should be abstract, fix sentinel.
67 virtual unsigned getMaxFileSize() const {
68 assert(0 && "Invalid getMaxFileSize call !");
71 /// @name Assembler Backend Support
74 // FIXME: This could all be kept private to the assembler implementation.
76 unsigned getFileSize() const {
77 assert(FileSize != ~UINT64_C(0) && "File size not set!");
80 void setFileSize(uint64_t Value) {
81 assert(Value <= getMaxFileSize() && "Invalid file size!");
85 uint64_t getFileOffset() const {
86 assert(FileOffset != ~UINT64_C(0) && "File offset not set!");
89 void setFileOffset(uint64_t Value) { FileOffset = Value; }
93 static bool classof(const MCFragment *O) { return true; }
96 class MCDataFragment : public MCFragment {
97 SmallString<32> Contents;
100 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
105 unsigned getMaxFileSize() const {
106 return Contents.size();
109 SmallString<32> &getContents() { return Contents; }
110 const SmallString<32> &getContents() const { return Contents; }
114 static bool classof(const MCFragment *F) {
115 return F->getKind() == MCFragment::FT_Data;
117 static bool classof(const MCDataFragment *) { return true; }
120 class MCAlignFragment : public MCFragment {
121 /// Alignment - The alignment to ensure, in bytes.
124 /// Value - Value to use for filling padding bytes.
127 /// ValueSize - The size of the integer (in bytes) of \arg Value.
130 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
131 /// cannot be satisfied in this width then this fragment is ignored.
132 unsigned MaxBytesToEmit;
135 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
136 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
137 : MCFragment(FT_Align, SD), Alignment(_Alignment),
138 Value(_Value),ValueSize(_ValueSize),
139 MaxBytesToEmit(_MaxBytesToEmit) {}
144 unsigned getMaxFileSize() const {
145 return std::max(Alignment - 1, MaxBytesToEmit);
148 unsigned getAlignment() const { return Alignment; }
150 int64_t getValue() const { return Value; }
152 unsigned getValueSize() const { return ValueSize; }
154 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
158 static bool classof(const MCFragment *F) {
159 return F->getKind() == MCFragment::FT_Align;
161 static bool classof(const MCAlignFragment *) { return true; }
164 class MCFillFragment : public MCFragment {
165 /// Value - Value to use for filling bytes.
168 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
171 /// Count - The number of copies of \arg Value to insert.
175 MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count,
176 MCSectionData *SD = 0)
177 : MCFragment(FT_Fill, SD),
178 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
183 unsigned getMaxFileSize() const {
184 return ValueSize * Count;
187 MCValue getValue() const { return Value; }
189 unsigned getValueSize() const { return ValueSize; }
191 uint64_t getCount() const { return Count; }
195 static bool classof(const MCFragment *F) {
196 return F->getKind() == MCFragment::FT_Fill;
198 static bool classof(const MCFillFragment *) { return true; }
201 class MCOrgFragment : public MCFragment {
202 /// Offset - The offset this fragment should start at.
205 /// Value - Value to use for filling bytes.
208 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
212 MCOrgFragment(MCValue _Offset, int64_t _Value, unsigned _ValueSize,
213 MCSectionData *SD = 0)
214 : MCFragment(FT_Org, SD),
215 Offset(_Offset), Value(_Value), ValueSize(_ValueSize) {}
219 unsigned getMaxFileSize() const {
224 MCValue getOffset() const { return Offset; }
226 int64_t getValue() const { return Value; }
228 unsigned getValueSize() const { return ValueSize; }
232 static bool classof(const MCFragment *F) {
233 return F->getKind() == MCFragment::FT_Org;
235 static bool classof(const MCOrgFragment *) { return true; }
238 // FIXME: Should this be a separate class, or just merged into MCSection? Since
239 // we anticipate the fast path being through an MCAssembler, the only reason to
240 // keep it out is for API abstraction.
241 class MCSectionData : public ilist_node<MCSectionData> {
242 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
243 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
246 typedef iplist<MCFragment> FragmentListType;
248 typedef FragmentListType::const_iterator const_iterator;
249 typedef FragmentListType::iterator iterator;
252 iplist<MCFragment> Fragments;
253 const MCSection &Section;
255 /// Alignment - The maximum alignment seen in this section.
258 /// @name Assembler Backend Data
261 // FIXME: This could all be kept private to the assembler implementation.
263 /// FileOffset - The offset of this section in the object file. This is ~0
264 /// until initialized.
267 /// FileSize - The size of this section in the object file. This is ~0 until
274 // Only for use as sentinel.
276 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
278 const MCSection &getSection() const { return Section; }
280 unsigned getAlignment() const { return Alignment; }
281 void setAlignment(unsigned Value) { Alignment = Value; }
284 /// @name Section List Access
287 const FragmentListType &getFragmentList() const { return Fragments; }
288 FragmentListType &getFragmentList() { return Fragments; }
290 iterator begin() { return Fragments.begin(); }
291 const_iterator begin() const { return Fragments.begin(); }
293 iterator end() { return Fragments.end(); }
294 const_iterator end() const { return Fragments.end(); }
296 size_t size() const { return Fragments.size(); }
299 /// @name Assembler Backend Support
302 // FIXME: This could all be kept private to the assembler implementation.
304 unsigned getFileSize() const {
305 assert(FileSize != ~UINT64_C(0) && "File size not set!");
308 void setFileSize(uint64_t Value) { FileSize = Value; }
310 uint64_t getFileOffset() const {
311 assert(FileOffset != ~UINT64_C(0) && "File offset not set!");
314 void setFileOffset(uint64_t Value) { FileOffset = Value; }
321 typedef iplist<MCSectionData> SectionDataListType;
323 typedef SectionDataListType::const_iterator const_iterator;
324 typedef SectionDataListType::iterator iterator;
327 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
328 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
332 iplist<MCSectionData> Sections;
335 /// LayoutSection - Assign offsets and sizes to the fragments in the section
336 /// \arg SD, and update the section size. The section file offset should
337 /// already have been computed.
338 void LayoutSection(MCSectionData &SD);
341 /// Construct a new assembler instance.
343 /// \arg OS - The stream to output to.
345 // FIXME: How are we going to parameterize this? Two obvious options are stay
346 // concrete and require clients to pass in a target like object. The other
347 // option is to make this abstract, and have targets provide concrete
348 // implementations as we do with AsmParser.
349 MCAssembler(raw_ostream &OS);
352 /// Finish - Do final processing and write the object to the output stream.
355 /// @name Section List Access
358 const SectionDataListType &getSectionList() const { return Sections; }
359 SectionDataListType &getSectionList() { return Sections; }
361 iterator begin() { return Sections.begin(); }
362 const_iterator begin() const { return Sections.begin(); }
364 iterator end() { return Sections.end(); }
365 const_iterator end() const { return Sections.end(); }
367 size_t size() const { return Sections.size(); }
372 } // end namespace llvm