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"
19 #include <vector> // FIXME: Shouldn't be needed.
27 class MCFragment : public ilist_node<MCFragment> {
28 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
29 void operator=(const MCFragment&); // DO NOT IMPLEMENT
42 /// Parent - The data for the section this fragment is in.
43 MCSectionData *Parent;
45 /// @name Assembler Backend Data
48 // FIXME: This could all be kept private to the assembler implementation.
50 /// Offset - The offset of this fragment in its section. This is ~0 until
54 /// FileSize - The file size of this section. This is ~0 until initialized.
60 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
65 virtual ~MCFragment();
67 FragmentType getKind() const { return Kind; }
69 MCSectionData *getParent() const { return Parent; }
70 void setParent(MCSectionData *Value) { Parent = Value; }
72 // FIXME: This should be abstract, fix sentinel.
73 virtual uint64_t getMaxFileSize() const {
74 assert(0 && "Invalid getMaxFileSize call!");
78 /// @name Assembler Backend Support
81 // FIXME: This could all be kept private to the assembler implementation.
83 uint64_t getAddress() const;
85 unsigned getFileSize() const {
86 assert(FileSize != ~UINT64_C(0) && "File size not set!");
89 void setFileSize(uint64_t Value) {
90 assert(Value <= getMaxFileSize() && "Invalid file size!");
94 uint64_t getOffset() const {
95 assert(Offset != ~UINT64_C(0) && "File offset not set!");
98 void setOffset(uint64_t Value) { Offset = Value; }
102 static bool classof(const MCFragment *O) { return true; }
105 class MCDataFragment : public MCFragment {
106 SmallString<32> Contents;
109 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
114 uint64_t getMaxFileSize() const {
115 return Contents.size();
118 SmallString<32> &getContents() { return Contents; }
119 const SmallString<32> &getContents() const { return Contents; }
123 static bool classof(const MCFragment *F) {
124 return F->getKind() == MCFragment::FT_Data;
126 static bool classof(const MCDataFragment *) { return true; }
129 class MCAlignFragment : public MCFragment {
130 /// Alignment - The alignment to ensure, in bytes.
133 /// Value - Value to use for filling padding bytes.
136 /// ValueSize - The size of the integer (in bytes) of \arg Value.
139 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
140 /// cannot be satisfied in this width then this fragment is ignored.
141 unsigned MaxBytesToEmit;
144 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
145 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
146 : MCFragment(FT_Align, SD), Alignment(_Alignment),
147 Value(_Value),ValueSize(_ValueSize),
148 MaxBytesToEmit(_MaxBytesToEmit) {}
153 uint64_t getMaxFileSize() const {
154 return std::max(Alignment - 1, MaxBytesToEmit);
157 unsigned getAlignment() const { return Alignment; }
159 int64_t getValue() const { return Value; }
161 unsigned getValueSize() const { return ValueSize; }
163 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
167 static bool classof(const MCFragment *F) {
168 return F->getKind() == MCFragment::FT_Align;
170 static bool classof(const MCAlignFragment *) { return true; }
173 class MCFillFragment : public MCFragment {
174 /// Value - Value to use for filling bytes.
177 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
180 /// Count - The number of copies of \arg Value to insert.
184 MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count,
185 MCSectionData *SD = 0)
186 : MCFragment(FT_Fill, SD),
187 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
192 uint64_t getMaxFileSize() const {
193 return ValueSize * Count;
196 MCValue getValue() const { return Value; }
198 unsigned getValueSize() const { return ValueSize; }
200 uint64_t getCount() const { return Count; }
204 static bool classof(const MCFragment *F) {
205 return F->getKind() == MCFragment::FT_Fill;
207 static bool classof(const MCFillFragment *) { return true; }
210 class MCOrgFragment : public MCFragment {
211 /// Offset - The offset this fragment should start at.
214 /// Value - Value to use for filling bytes.
218 MCOrgFragment(MCValue _Offset, int8_t _Value, MCSectionData *SD = 0)
219 : MCFragment(FT_Org, SD),
220 Offset(_Offset), Value(_Value) {}
224 uint64_t getMaxFileSize() const {
225 // FIXME: This doesn't make much sense.
229 MCValue getOffset() const { return Offset; }
231 uint8_t getValue() const { return Value; }
235 static bool classof(const MCFragment *F) {
236 return F->getKind() == MCFragment::FT_Org;
238 static bool classof(const MCOrgFragment *) { return true; }
241 // FIXME: Should this be a separate class, or just merged into MCSection? Since
242 // we anticipate the fast path being through an MCAssembler, the only reason to
243 // keep it out is for API abstraction.
244 class MCSectionData : public ilist_node<MCSectionData> {
245 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
246 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
249 typedef iplist<MCFragment> FragmentListType;
251 typedef FragmentListType::const_iterator const_iterator;
252 typedef FragmentListType::iterator iterator;
255 iplist<MCFragment> Fragments;
256 const MCSection &Section;
258 /// Alignment - The maximum alignment seen in this section.
261 /// @name Assembler Backend Data
264 // FIXME: This could all be kept private to the assembler implementation.
266 /// Address - The computed address of this section. This is ~0 until
270 /// FileSize - The size of this section in the object file. This is ~0 until
277 // Only for use as sentinel.
279 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
281 const MCSection &getSection() const { return Section; }
283 unsigned getAlignment() const { return Alignment; }
284 void setAlignment(unsigned Value) { Alignment = Value; }
286 /// @name Section List Access
289 const FragmentListType &getFragmentList() const { return Fragments; }
290 FragmentListType &getFragmentList() { return Fragments; }
292 iterator begin() { return Fragments.begin(); }
293 const_iterator begin() const { return Fragments.begin(); }
295 iterator end() { return Fragments.end(); }
296 const_iterator end() const { return Fragments.end(); }
298 size_t size() const { return Fragments.size(); }
300 bool empty() const { return Fragments.empty(); }
303 /// @name Assembler Backend Support
306 // FIXME: This could all be kept private to the assembler implementation.
308 unsigned getAddress() const {
309 assert(Address != ~UINT64_C(0) && "Address not set!");
312 void setAddress(uint64_t Value) { Address = Value; }
314 unsigned getFileSize() const {
315 assert(FileSize != ~UINT64_C(0) && "File size not set!");
318 void setFileSize(uint64_t Value) { FileSize = Value; }
323 // FIXME: Same concerns as with SectionData.
324 class MCSymbolData : public ilist_node<MCSymbolData> {
328 /// Fragment - The fragment this symbol's value is relative to, if any.
329 MCFragment *Fragment;
331 /// Offset - The offset to apply to the fragment address to form this symbol's
335 /// IsExternal - True if this symbol is visible outside this translation
337 unsigned IsExternal : 1;
339 /// IsPrivateExtern - True if this symbol is private extern.
340 unsigned IsPrivateExtern : 1;
342 /// Flags - The Flags field is used by object file implementations to store
343 /// additional per symbol information which is not easily classified.
347 // Only for use as sentinel.
349 MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
355 MCSymbol &getSymbol() const { return Symbol; }
357 MCFragment *getFragment() const { return Fragment; }
358 void setFragment(MCFragment *Value) { Fragment = Value; }
360 uint64_t getOffset() const { return Offset; }
361 void setOffset(uint64_t Value) { Offset = Value; }
364 /// @name Symbol Attributes
367 bool isExternal() const { return IsExternal; }
368 void setExternal(bool Value) { IsExternal = Value; }
370 bool isPrivateExtern() const { return IsPrivateExtern; }
371 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
373 /// getFlags - Get the (implementation defined) symbol flags.
374 uint32_t getFlags() const { return Flags; }
376 /// setFlags - Set the (implementation defined) symbol flags.
377 void setFlags(uint32_t Value) { Flags = Value; }
382 // FIXME: This really doesn't belong here. See comments below.
383 struct IndirectSymbolData {
385 MCSectionData *SectionData;
390 typedef iplist<MCSectionData> SectionDataListType;
391 typedef iplist<MCSymbolData> SymbolDataListType;
393 typedef SectionDataListType::const_iterator const_iterator;
394 typedef SectionDataListType::iterator iterator;
396 typedef SymbolDataListType::const_iterator const_symbol_iterator;
397 typedef SymbolDataListType::iterator symbol_iterator;
399 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
402 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
403 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
407 iplist<MCSectionData> Sections;
409 iplist<MCSymbolData> Symbols;
411 std::vector<IndirectSymbolData> IndirectSymbols;
414 /// LayoutSection - Assign offsets and sizes to the fragments in the section
415 /// \arg SD, and update the section size. The section file offset should
416 /// already have been computed.
417 void LayoutSection(MCSectionData &SD);
420 /// Construct a new assembler instance.
422 /// \arg OS - The stream to output to.
424 // FIXME: How are we going to parameterize this? Two obvious options are stay
425 // concrete and require clients to pass in a target like object. The other
426 // option is to make this abstract, and have targets provide concrete
427 // implementations as we do with AsmParser.
428 MCAssembler(raw_ostream &OS);
431 /// Finish - Do final processing and write the object to the output stream.
434 /// @name Section List Access
437 const SectionDataListType &getSectionList() const { return Sections; }
438 SectionDataListType &getSectionList() { return Sections; }
440 iterator begin() { return Sections.begin(); }
441 const_iterator begin() const { return Sections.begin(); }
443 iterator end() { return Sections.end(); }
444 const_iterator end() const { return Sections.end(); }
446 size_t size() const { return Sections.size(); }
449 /// @name Symbol List Access
452 const SymbolDataListType &getSymbolList() const { return Symbols; }
453 SymbolDataListType &getSymbolList() { return Symbols; }
455 symbol_iterator symbol_begin() { return Symbols.begin(); }
456 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
458 symbol_iterator symbol_end() { return Symbols.end(); }
459 const_symbol_iterator symbol_end() const { return Symbols.end(); }
461 size_t symbol_size() const { return Symbols.size(); }
464 /// @name Indirect Symbol List Access
467 // FIXME: This is a total hack, this should not be here. Once things are
468 // factored so that the streamer has direct access to the .o writer, it can
470 std::vector<IndirectSymbolData> &getIndirectSymbols() {
471 return IndirectSymbols;
474 indirect_symbol_iterator indirect_symbol_begin() {
475 return IndirectSymbols.begin();
478 indirect_symbol_iterator indirect_symbol_end() {
479 return IndirectSymbols.end();
482 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
487 } // end namespace llvm