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
43 /// Parent - The data for the section this fragment is in.
44 MCSectionData *Parent;
46 /// @name Assembler Backend Data
49 // FIXME: This could all be kept private to the assembler implementation.
51 /// Offset - The offset of this fragment in its section. This is ~0 until
55 /// FileSize - The file size of this section. This is ~0 until initialized.
61 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
66 virtual ~MCFragment();
68 FragmentType getKind() const { return Kind; }
70 MCSectionData *getParent() const { return Parent; }
71 void setParent(MCSectionData *Value) { Parent = Value; }
73 // FIXME: This should be abstract, fix sentinel.
74 virtual uint64_t getMaxFileSize() const {
75 assert(0 && "Invalid getMaxFileSize call!");
79 /// @name Assembler Backend Support
82 // FIXME: This could all be kept private to the assembler implementation.
84 uint64_t getAddress() const;
86 uint64_t getFileSize() const {
87 assert(FileSize != ~UINT64_C(0) && "File size not set!");
90 void setFileSize(uint64_t Value) {
91 assert(Value <= getMaxFileSize() && "Invalid file size!");
95 uint64_t getOffset() const {
96 assert(Offset != ~UINT64_C(0) && "File offset not set!");
99 void setOffset(uint64_t Value) { Offset = Value; }
103 static bool classof(const MCFragment *O) { return true; }
106 class MCDataFragment : public MCFragment {
107 SmallString<32> Contents;
110 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
115 uint64_t getMaxFileSize() const {
116 return Contents.size();
119 SmallString<32> &getContents() { return Contents; }
120 const SmallString<32> &getContents() const { return Contents; }
124 static bool classof(const MCFragment *F) {
125 return F->getKind() == MCFragment::FT_Data;
127 static bool classof(const MCDataFragment *) { return true; }
130 class MCAlignFragment : public MCFragment {
131 /// Alignment - The alignment to ensure, in bytes.
134 /// Value - Value to use for filling padding bytes.
137 /// ValueSize - The size of the integer (in bytes) of \arg Value.
140 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
141 /// cannot be satisfied in this width then this fragment is ignored.
142 unsigned MaxBytesToEmit;
145 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
146 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
147 : MCFragment(FT_Align, SD), Alignment(_Alignment),
148 Value(_Value),ValueSize(_ValueSize),
149 MaxBytesToEmit(_MaxBytesToEmit) {}
154 uint64_t getMaxFileSize() const {
155 return std::max(Alignment - 1, MaxBytesToEmit);
158 unsigned getAlignment() const { return Alignment; }
160 int64_t getValue() const { return Value; }
162 unsigned getValueSize() const { return ValueSize; }
164 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
168 static bool classof(const MCFragment *F) {
169 return F->getKind() == MCFragment::FT_Align;
171 static bool classof(const MCAlignFragment *) { return true; }
174 class MCFillFragment : public MCFragment {
175 /// Value - Value to use for filling bytes.
178 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
181 /// Count - The number of copies of \arg Value to insert.
185 MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count,
186 MCSectionData *SD = 0)
187 : MCFragment(FT_Fill, SD),
188 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
193 uint64_t getMaxFileSize() const {
194 return ValueSize * Count;
197 MCValue getValue() const { return Value; }
199 unsigned getValueSize() const { return ValueSize; }
201 uint64_t getCount() const { return Count; }
205 static bool classof(const MCFragment *F) {
206 return F->getKind() == MCFragment::FT_Fill;
208 static bool classof(const MCFillFragment *) { return true; }
211 class MCOrgFragment : public MCFragment {
212 /// Offset - The offset this fragment should start at.
215 /// Value - Value to use for filling bytes.
219 MCOrgFragment(MCValue _Offset, int8_t _Value, MCSectionData *SD = 0)
220 : MCFragment(FT_Org, SD),
221 Offset(_Offset), Value(_Value) {}
226 uint64_t getMaxFileSize() const {
227 // FIXME: This doesn't make much sense.
231 MCValue getOffset() const { return Offset; }
233 uint8_t getValue() const { return Value; }
237 static bool classof(const MCFragment *F) {
238 return F->getKind() == MCFragment::FT_Org;
240 static bool classof(const MCOrgFragment *) { return true; }
243 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
244 /// but requires no physical space in the object file.
245 class MCZeroFillFragment : public MCFragment {
246 /// Size - The size of this fragment.
249 /// Alignment - The alignment for this fragment.
253 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
254 : MCFragment(FT_ZeroFill, SD),
255 Size(_Size), Alignment(_Alignment) {}
260 uint64_t getMaxFileSize() const {
261 // FIXME: This also doesn't make much sense, this method is misnamed.
265 uint64_t getSize() const { return Size; }
267 unsigned getAlignment() const { return Alignment; }
271 static bool classof(const MCFragment *F) {
272 return F->getKind() == MCFragment::FT_ZeroFill;
274 static bool classof(const MCZeroFillFragment *) { return true; }
277 // FIXME: Should this be a separate class, or just merged into MCSection? Since
278 // we anticipate the fast path being through an MCAssembler, the only reason to
279 // keep it out is for API abstraction.
280 class MCSectionData : public ilist_node<MCSectionData> {
281 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
282 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
285 /// Fixup - Represent a fixed size region of bytes inside some fragment which
286 /// needs to be rewritten. This region will either be rewritten by the
287 /// assembler or cause a relocation entry to be generated.
289 /// Fragment - The fragment containing the fixup.
290 MCFragment *Fragment;
292 /// Offset - The offset inside the fragment which needs to be rewritten.
295 /// Value - The expression to eventually write into the fragment.
297 // FIXME: We could probably get away with requiring the client to pass in an
298 // owned reference whose lifetime extends past that of the fixup.
301 /// Size - The fixup size.
304 /// FixedValue - The value to replace the fix up by.
306 // FIXME: This should not be here.
310 Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCValue &_Value,
312 : Fragment(&_Fragment), Offset(_Offset), Value(_Value), Size(_Size),
316 typedef iplist<MCFragment> FragmentListType;
318 typedef FragmentListType::const_iterator const_iterator;
319 typedef FragmentListType::iterator iterator;
321 typedef std::vector<Fixup>::const_iterator const_fixup_iterator;
322 typedef std::vector<Fixup>::iterator fixup_iterator;
325 iplist<MCFragment> Fragments;
326 const MCSection *Section;
328 /// Alignment - The maximum alignment seen in this section.
331 /// @name Assembler Backend Data
334 // FIXME: This could all be kept private to the assembler implementation.
336 /// Address - The computed address of this section. This is ~0 until
340 /// Size - The content size of this section. This is ~0 until initialized.
343 /// FileSize - The size of this section in the object file. This is ~0 until
347 /// LastFixupLookup - Cache for the last looked up fixup.
348 mutable unsigned LastFixupLookup;
350 /// Fixups - The list of fixups in this section.
351 std::vector<Fixup> Fixups;
356 // Only for use as sentinel.
358 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
360 const MCSection &getSection() const { return *Section; }
362 unsigned getAlignment() const { return Alignment; }
363 void setAlignment(unsigned Value) { Alignment = Value; }
365 /// @name Fragment Access
368 const FragmentListType &getFragmentList() const { return Fragments; }
369 FragmentListType &getFragmentList() { return Fragments; }
371 iterator begin() { return Fragments.begin(); }
372 const_iterator begin() const { return Fragments.begin(); }
374 iterator end() { return Fragments.end(); }
375 const_iterator end() const { return Fragments.end(); }
377 size_t size() const { return Fragments.size(); }
379 bool empty() const { return Fragments.empty(); }
382 /// @name Fixup Access
385 std::vector<Fixup> &getFixups() {
389 fixup_iterator fixup_begin() {
390 return Fixups.begin();
393 fixup_iterator fixup_end() {
397 size_t fixup_size() const { return Fixups.size(); }
400 /// @name Assembler Backend Support
403 // FIXME: This could all be kept private to the assembler implementation.
405 /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
408 /// If multiple fixups exist for the same fragment and offset it is undefined
409 /// which one is returned.
411 // FIXME: This isn't horribly slow in practice, but there are much nicer
412 // solutions to applying the fixups.
413 const Fixup *LookupFixup(const MCFragment *Fragment, uint64_t Offset) const;
415 uint64_t getAddress() const {
416 assert(Address != ~UINT64_C(0) && "Address not set!");
419 void setAddress(uint64_t Value) { Address = Value; }
421 uint64_t getSize() const {
422 assert(Size != ~UINT64_C(0) && "File size not set!");
425 void setSize(uint64_t Value) { Size = Value; }
427 uint64_t getFileSize() const {
428 assert(FileSize != ~UINT64_C(0) && "File size not set!");
431 void setFileSize(uint64_t Value) { FileSize = Value; }
436 // FIXME: Same concerns as with SectionData.
437 class MCSymbolData : public ilist_node<MCSymbolData> {
441 /// Fragment - The fragment this symbol's value is relative to, if any.
442 MCFragment *Fragment;
444 /// Offset - The offset to apply to the fragment address to form this symbol's
448 /// IsExternal - True if this symbol is visible outside this translation
450 unsigned IsExternal : 1;
452 /// IsPrivateExtern - True if this symbol is private extern.
453 unsigned IsPrivateExtern : 1;
455 /// CommonSize - The size of the symbol, if it is 'common', or 0.
457 // FIXME: Pack this in with other fields? We could put it in offset, since a
458 // common symbol can never get a definition.
461 /// CommonAlign - The alignment of the symbol, if it is 'common'.
463 // FIXME: Pack this in with other fields?
464 unsigned CommonAlign;
466 /// Flags - The Flags field is used by object file implementations to store
467 /// additional per symbol information which is not easily classified.
470 /// Index - Index field, for use by the object file implementation.
474 // Only for use as sentinel.
476 MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
482 MCSymbol &getSymbol() const { return Symbol; }
484 MCFragment *getFragment() const { return Fragment; }
485 void setFragment(MCFragment *Value) { Fragment = Value; }
487 uint64_t getOffset() const { return Offset; }
488 void setOffset(uint64_t Value) { Offset = Value; }
491 /// @name Symbol Attributes
494 bool isExternal() const { return IsExternal; }
495 void setExternal(bool Value) { IsExternal = Value; }
497 bool isPrivateExtern() const { return IsPrivateExtern; }
498 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
500 /// isCommon - Is this a 'common' symbol.
501 bool isCommon() const { return CommonSize != 0; }
503 /// setCommon - Mark this symbol as being 'common'.
505 /// \param Size - The size of the symbol.
506 /// \param Align - The alignment of the symbol.
507 void setCommon(uint64_t Size, unsigned Align) {
512 /// getCommonSize - Return the size of a 'common' symbol.
513 uint64_t getCommonSize() const {
514 assert(isCommon() && "Not a 'common' symbol!");
518 /// getCommonAlignment - Return the alignment of a 'common' symbol.
519 unsigned getCommonAlignment() const {
520 assert(isCommon() && "Not a 'common' symbol!");
524 /// getFlags - Get the (implementation defined) symbol flags.
525 uint32_t getFlags() const { return Flags; }
527 /// setFlags - Set the (implementation defined) symbol flags.
528 void setFlags(uint32_t Value) { Flags = Value; }
530 /// getIndex - Get the (implementation defined) index.
531 uint64_t getIndex() const { return Index; }
533 /// setIndex - Set the (implementation defined) index.
534 void setIndex(uint64_t Value) { Index = Value; }
539 // FIXME: This really doesn't belong here. See comments below.
540 struct IndirectSymbolData {
542 MCSectionData *SectionData;
547 typedef iplist<MCSectionData> SectionDataListType;
548 typedef iplist<MCSymbolData> SymbolDataListType;
550 typedef SectionDataListType::const_iterator const_iterator;
551 typedef SectionDataListType::iterator iterator;
553 typedef SymbolDataListType::const_iterator const_symbol_iterator;
554 typedef SymbolDataListType::iterator symbol_iterator;
556 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
559 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
560 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
564 iplist<MCSectionData> Sections;
566 iplist<MCSymbolData> Symbols;
568 std::vector<IndirectSymbolData> IndirectSymbols;
570 unsigned SubsectionsViaSymbols : 1;
573 /// LayoutSection - Assign offsets and sizes to the fragments in the section
574 /// \arg SD, and update the section size. The section file offset should
575 /// already have been computed.
576 void LayoutSection(MCSectionData &SD);
579 /// Construct a new assembler instance.
581 /// \arg OS - The stream to output to.
583 // FIXME: How are we going to parameterize this? Two obvious options are stay
584 // concrete and require clients to pass in a target like object. The other
585 // option is to make this abstract, and have targets provide concrete
586 // implementations as we do with AsmParser.
587 MCAssembler(raw_ostream &OS);
590 /// Finish - Do final processing and write the object to the output stream.
593 // FIXME: This does not belong here.
594 bool getSubsectionsViaSymbols() const {
595 return SubsectionsViaSymbols;
597 void setSubsectionsViaSymbols(bool Value) {
598 SubsectionsViaSymbols = Value;
601 /// @name Section List Access
604 const SectionDataListType &getSectionList() const { return Sections; }
605 SectionDataListType &getSectionList() { return Sections; }
607 iterator begin() { return Sections.begin(); }
608 const_iterator begin() const { return Sections.begin(); }
610 iterator end() { return Sections.end(); }
611 const_iterator end() const { return Sections.end(); }
613 size_t size() const { return Sections.size(); }
616 /// @name Symbol List Access
619 const SymbolDataListType &getSymbolList() const { return Symbols; }
620 SymbolDataListType &getSymbolList() { return Symbols; }
622 symbol_iterator symbol_begin() { return Symbols.begin(); }
623 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
625 symbol_iterator symbol_end() { return Symbols.end(); }
626 const_symbol_iterator symbol_end() const { return Symbols.end(); }
628 size_t symbol_size() const { return Symbols.size(); }
631 /// @name Indirect Symbol List Access
634 // FIXME: This is a total hack, this should not be here. Once things are
635 // factored so that the streamer has direct access to the .o writer, it can
637 std::vector<IndirectSymbolData> &getIndirectSymbols() {
638 return IndirectSymbols;
641 indirect_symbol_iterator indirect_symbol_begin() {
642 return IndirectSymbols.begin();
645 indirect_symbol_iterator indirect_symbol_end() {
646 return IndirectSymbols.end();
649 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
654 } // end namespace llvm