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/DenseMap.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/ilist.h"
16 #include "llvm/ADT/ilist_node.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/MC/MCFixup.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/System/DataTypes.h"
21 #include <vector> // FIXME: Shouldn't be needed.
38 class TargetAsmBackend;
40 class MCFragment : public ilist_node<MCFragment> {
41 friend class MCAsmLayout;
43 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
44 void operator=(const MCFragment&); // DO NOT IMPLEMENT
59 /// Parent - The data for the section this fragment is in.
60 MCSectionData *Parent;
62 /// Atom - The atom this fragment is in, as represented by it's defining
63 /// symbol. Atom's are only used by backends which set
64 /// \see MCAsmBackend::hasReliableSymbolDifference().
67 /// @name Assembler Backend Data
70 // FIXME: This could all be kept private to the assembler implementation.
72 /// Offset - The offset of this fragment in its section. This is ~0 until
76 /// EffectiveSize - The compute size of this section. This is ~0 until
78 uint64_t EffectiveSize;
80 /// LayoutOrder - The global layout order of this fragment. This is the index
81 /// across all fragments in the file, not just within the section.
87 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
92 virtual ~MCFragment();
94 FragmentType getKind() const { return Kind; }
96 MCSectionData *getParent() const { return Parent; }
97 void setParent(MCSectionData *Value) { Parent = Value; }
99 MCSymbolData *getAtom() const { return Atom; }
100 void setAtom(MCSymbolData *Value) { Atom = Value; }
102 unsigned getLayoutOrder() const { return LayoutOrder; }
103 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
105 static bool classof(const MCFragment *O) { return true; }
110 class MCDataFragment : public MCFragment {
111 SmallString<32> Contents;
113 /// Fixups - The list of fixups in this fragment.
114 std::vector<MCFixup> Fixups;
117 typedef std::vector<MCFixup>::const_iterator const_fixup_iterator;
118 typedef std::vector<MCFixup>::iterator fixup_iterator;
121 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
126 SmallString<32> &getContents() { return Contents; }
127 const SmallString<32> &getContents() const { return Contents; }
130 /// @name Fixup Access
133 void addFixup(MCFixup Fixup) {
134 // Enforce invariant that fixups are in offset order.
135 assert((Fixups.empty() || Fixup.getOffset() > Fixups.back().getOffset()) &&
136 "Fixups must be added in order!");
137 Fixups.push_back(Fixup);
140 std::vector<MCFixup> &getFixups() { return Fixups; }
141 const std::vector<MCFixup> &getFixups() const { return Fixups; }
143 fixup_iterator fixup_begin() { return Fixups.begin(); }
144 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
146 fixup_iterator fixup_end() {return Fixups.end();}
147 const_fixup_iterator fixup_end() const {return Fixups.end();}
149 size_t fixup_size() const { return Fixups.size(); }
153 static bool classof(const MCFragment *F) {
154 return F->getKind() == MCFragment::FT_Data;
156 static bool classof(const MCDataFragment *) { return true; }
159 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
160 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
161 // with this approach (as opposed to making MCInstFragment a very light weight
162 // object with just the MCInst and a code size, then we should just change
163 // MCDataFragment to have an optional MCInst at its end.
164 class MCInstFragment : public MCFragment {
165 /// Inst - The instruction this is a fragment for.
168 /// Code - Binary data for the currently encoded instruction.
171 /// Fixups - The list of fixups in this fragment.
172 SmallVector<MCFixup, 1> Fixups;
175 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
176 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
179 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
180 : MCFragment(FT_Inst, SD), Inst(_Inst) {
186 SmallVectorImpl<char> &getCode() { return Code; }
187 const SmallVectorImpl<char> &getCode() const { return Code; }
189 unsigned getInstSize() const { return Code.size(); }
191 MCInst &getInst() { return Inst; }
192 const MCInst &getInst() const { return Inst; }
194 void setInst(MCInst Value) { Inst = Value; }
197 /// @name Fixup Access
200 SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
201 const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
203 fixup_iterator fixup_begin() { return Fixups.begin(); }
204 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
206 fixup_iterator fixup_end() {return Fixups.end();}
207 const_fixup_iterator fixup_end() const {return Fixups.end();}
209 size_t fixup_size() const { return Fixups.size(); }
213 static bool classof(const MCFragment *F) {
214 return F->getKind() == MCFragment::FT_Inst;
216 static bool classof(const MCInstFragment *) { return true; }
219 class MCAlignFragment : public MCFragment {
220 /// Alignment - The alignment to ensure, in bytes.
223 /// Value - Value to use for filling padding bytes.
226 /// ValueSize - The size of the integer (in bytes) of \arg Value.
229 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
230 /// cannot be satisfied in this width then this fragment is ignored.
231 unsigned MaxBytesToEmit;
233 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
234 /// of using the provided value. The exact interpretation of this flag is
235 /// target dependent.
238 /// OnlyAlignAddress - Flag to indicate that this align is only used to adjust
239 /// the address space size of a section and that it should not be included as
240 /// part of the section size. This flag can only be used on the last fragment
242 bool OnlyAlignAddress : 1;
245 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
246 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
247 : MCFragment(FT_Align, SD), Alignment(_Alignment),
248 Value(_Value),ValueSize(_ValueSize),
249 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false),
250 OnlyAlignAddress(false) {}
255 unsigned getAlignment() const { return Alignment; }
257 int64_t getValue() const { return Value; }
259 unsigned getValueSize() const { return ValueSize; }
261 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
263 bool hasEmitNops() const { return EmitNops; }
264 void setEmitNops(bool Value) { EmitNops = Value; }
266 bool hasOnlyAlignAddress() const { return OnlyAlignAddress; }
267 void setOnlyAlignAddress(bool Value) { OnlyAlignAddress = Value; }
271 static bool classof(const MCFragment *F) {
272 return F->getKind() == MCFragment::FT_Align;
274 static bool classof(const MCAlignFragment *) { return true; }
277 class MCFillFragment : public MCFragment {
278 /// Value - Value to use for filling bytes.
281 /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
282 /// this is a virtual fill fragment.
285 /// Size - The number of bytes to insert.
289 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
290 MCSectionData *SD = 0)
291 : MCFragment(FT_Fill, SD),
292 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
293 assert((!ValueSize || (Size % ValueSize) == 0) &&
294 "Fill size must be a multiple of the value size!");
300 int64_t getValue() const { return Value; }
302 unsigned getValueSize() const { return ValueSize; }
304 uint64_t getSize() const { return Size; }
308 static bool classof(const MCFragment *F) {
309 return F->getKind() == MCFragment::FT_Fill;
311 static bool classof(const MCFillFragment *) { return true; }
314 class MCOrgFragment : public MCFragment {
315 /// Offset - The offset this fragment should start at.
316 const MCExpr *Offset;
318 /// Value - Value to use for filling bytes.
322 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
323 : MCFragment(FT_Org, SD),
324 Offset(&_Offset), Value(_Value) {}
329 const MCExpr &getOffset() const { return *Offset; }
331 uint8_t getValue() const { return Value; }
335 static bool classof(const MCFragment *F) {
336 return F->getKind() == MCFragment::FT_Org;
338 static bool classof(const MCOrgFragment *) { return true; }
341 class MCDwarfLineAddrFragment : public MCFragment {
342 /// LineDelta - the value of the difference between the two line numbers
343 /// between two .loc dwarf directives.
346 /// AddrDelta - The expression for the difference of the two symbols that
347 /// make up the address delta between two .loc dwarf directives.
348 const MCExpr *AddrDelta;
351 MCDwarfLineAddrFragment(int64_t _LineDelta, const MCExpr &_AddrDelta,
352 MCSectionData *SD = 0)
353 : MCFragment(FT_Dwarf, SD),
354 LineDelta(_LineDelta), AddrDelta(&_AddrDelta) {}
359 int64_t getLineDelta() const { return LineDelta; }
361 const MCExpr &getAddrDelta() const { return *AddrDelta; }
365 static bool classof(const MCFragment *F) {
366 return F->getKind() == MCFragment::FT_Dwarf;
368 static bool classof(const MCDwarfLineAddrFragment *) { return true; }
371 // FIXME: Should this be a separate class, or just merged into MCSection? Since
372 // we anticipate the fast path being through an MCAssembler, the only reason to
373 // keep it out is for API abstraction.
374 class MCSectionData : public ilist_node<MCSectionData> {
375 friend class MCAsmLayout;
377 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
378 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
381 typedef iplist<MCFragment> FragmentListType;
383 typedef FragmentListType::const_iterator const_iterator;
384 typedef FragmentListType::iterator iterator;
386 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
387 typedef FragmentListType::reverse_iterator reverse_iterator;
390 FragmentListType Fragments;
391 const MCSection *Section;
393 /// Ordinal - The section index in the assemblers section list.
396 /// LayoutOrder - The index of this section in the layout order.
397 unsigned LayoutOrder;
399 /// Alignment - The maximum alignment seen in this section.
402 /// @name Assembler Backend Data
405 // FIXME: This could all be kept private to the assembler implementation.
407 /// Address - The computed address of this section. This is ~0 until
411 /// HasInstructions - Whether this section has had instructions emitted into
413 unsigned HasInstructions : 1;
418 // Only for use as sentinel.
420 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
422 const MCSection &getSection() const { return *Section; }
424 unsigned getAlignment() const { return Alignment; }
425 void setAlignment(unsigned Value) { Alignment = Value; }
427 bool hasInstructions() const { return HasInstructions; }
428 void setHasInstructions(bool Value) { HasInstructions = Value; }
430 unsigned getOrdinal() const { return Ordinal; }
431 void setOrdinal(unsigned Value) { Ordinal = Value; }
433 unsigned getLayoutOrder() const { return LayoutOrder; }
434 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
436 /// @name Fragment Access
439 const FragmentListType &getFragmentList() const { return Fragments; }
440 FragmentListType &getFragmentList() { return Fragments; }
442 iterator begin() { return Fragments.begin(); }
443 const_iterator begin() const { return Fragments.begin(); }
445 iterator end() { return Fragments.end(); }
446 const_iterator end() const { return Fragments.end(); }
448 reverse_iterator rbegin() { return Fragments.rbegin(); }
449 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
451 reverse_iterator rend() { return Fragments.rend(); }
452 const_reverse_iterator rend() const { return Fragments.rend(); }
454 size_t size() const { return Fragments.size(); }
456 bool empty() const { return Fragments.empty(); }
463 // FIXME: Same concerns as with SectionData.
464 class MCSymbolData : public ilist_node<MCSymbolData> {
466 const MCSymbol *Symbol;
468 /// Fragment - The fragment this symbol's value is relative to, if any.
469 MCFragment *Fragment;
471 /// Offset - The offset to apply to the fragment address to form this symbol's
475 /// IsExternal - True if this symbol is visible outside this translation
477 unsigned IsExternal : 1;
479 /// IsPrivateExtern - True if this symbol is private extern.
480 unsigned IsPrivateExtern : 1;
482 /// CommonSize - The size of the symbol, if it is 'common', or 0.
484 // FIXME: Pack this in with other fields? We could put it in offset, since a
485 // common symbol can never get a definition.
488 /// SymbolSize - An expression describing how to calculate the size of
489 /// a symbol. If a symbol has no size this field will be NULL.
490 const MCExpr *SymbolSize;
492 /// CommonAlign - The alignment of the symbol, if it is 'common'.
494 // FIXME: Pack this in with other fields?
495 unsigned CommonAlign;
497 /// Flags - The Flags field is used by object file implementations to store
498 /// additional per symbol information which is not easily classified.
501 /// Index - Index field, for use by the object file implementation.
505 // Only for use as sentinel.
507 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
513 const MCSymbol &getSymbol() const { return *Symbol; }
515 MCFragment *getFragment() const { return Fragment; }
516 void setFragment(MCFragment *Value) { Fragment = Value; }
518 uint64_t getOffset() const { return Offset; }
519 void setOffset(uint64_t Value) { Offset = Value; }
522 /// @name Symbol Attributes
525 bool isExternal() const { return IsExternal; }
526 void setExternal(bool Value) { IsExternal = Value; }
528 bool isPrivateExtern() const { return IsPrivateExtern; }
529 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
531 /// isCommon - Is this a 'common' symbol.
532 bool isCommon() const { return CommonSize != 0; }
534 /// setCommon - Mark this symbol as being 'common'.
536 /// \param Size - The size of the symbol.
537 /// \param Align - The alignment of the symbol.
538 void setCommon(uint64_t Size, unsigned Align) {
543 /// getCommonSize - Return the size of a 'common' symbol.
544 uint64_t getCommonSize() const {
545 assert(isCommon() && "Not a 'common' symbol!");
549 void setSize(const MCExpr *SS) {
553 const MCExpr *getSize() const {
558 /// getCommonAlignment - Return the alignment of a 'common' symbol.
559 unsigned getCommonAlignment() const {
560 assert(isCommon() && "Not a 'common' symbol!");
564 /// getFlags - Get the (implementation defined) symbol flags.
565 uint32_t getFlags() const { return Flags; }
567 /// setFlags - Set the (implementation defined) symbol flags.
568 void setFlags(uint32_t Value) { Flags = Value; }
570 /// modifyFlags - Modify the flags via a mask
571 void modifyFlags(uint32_t Value, uint32_t Mask) {
572 Flags = (Flags & ~Mask) | Value;
575 /// getIndex - Get the (implementation defined) index.
576 uint64_t getIndex() const { return Index; }
578 /// setIndex - Set the (implementation defined) index.
579 void setIndex(uint64_t Value) { Index = Value; }
586 // FIXME: This really doesn't belong here. See comments below.
587 struct IndirectSymbolData {
589 MCSectionData *SectionData;
593 friend class MCAsmLayout;
596 typedef iplist<MCSectionData> SectionDataListType;
597 typedef iplist<MCSymbolData> SymbolDataListType;
599 typedef SectionDataListType::const_iterator const_iterator;
600 typedef SectionDataListType::iterator iterator;
602 typedef SymbolDataListType::const_iterator const_symbol_iterator;
603 typedef SymbolDataListType::iterator symbol_iterator;
605 typedef std::vector<IndirectSymbolData>::const_iterator
606 const_indirect_symbol_iterator;
607 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
610 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
611 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
615 TargetAsmBackend &Backend;
617 MCCodeEmitter &Emitter;
621 iplist<MCSectionData> Sections;
623 iplist<MCSymbolData> Symbols;
625 /// The map of sections to their associated assembler backend data.
627 // FIXME: Avoid this indirection?
628 DenseMap<const MCSection*, MCSectionData*> SectionMap;
630 /// The map of symbols to their associated assembler backend data.
632 // FIXME: Avoid this indirection?
633 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
635 std::vector<IndirectSymbolData> IndirectSymbols;
637 unsigned RelaxAll : 1;
638 unsigned SubsectionsViaSymbols : 1;
639 unsigned PadSectionToAlignment : 1;
642 /// Evaluate a fixup to a relocatable expression and the value which should be
643 /// placed into the fixup.
645 /// \param Layout The layout to use for evaluation.
646 /// \param Fixup The fixup to evaluate.
647 /// \param DF The fragment the fixup is inside.
648 /// \param Target [out] On return, the relocatable expression the fixup
650 /// \param Value [out] On return, the value of the fixup as currently layed
652 /// \return Whether the fixup value was fully resolved. This is true if the
653 /// \arg Value result is fixed, otherwise the value may change due to
655 bool EvaluateFixup(const MCObjectWriter &Writer, const MCAsmLayout &Layout,
656 const MCFixup &Fixup, const MCFragment *DF,
657 MCValue &Target, uint64_t &Value) const;
659 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
660 /// (increased in size, in order to hold its value correctly).
661 bool FixupNeedsRelaxation(const MCObjectWriter &Writer,
662 const MCFixup &Fixup, const MCFragment *DF,
663 const MCAsmLayout &Layout) const;
665 /// Check whether the given fragment needs relaxation.
666 bool FragmentNeedsRelaxation(const MCObjectWriter &Writer,
667 const MCInstFragment *IF,
668 const MCAsmLayout &Layout) const;
670 /// Compute the effective fragment size assuming it is layed out at the given
671 /// \arg SectionAddress and \arg FragmentOffset.
672 uint64_t ComputeFragmentSize(MCAsmLayout &Layout, const MCFragment &F,
673 uint64_t SectionAddress,
674 uint64_t FragmentOffset) const;
676 /// LayoutOnce - Perform one layout iteration and return true if any offsets
678 bool LayoutOnce(const MCObjectWriter &Writer, MCAsmLayout &Layout);
680 /// FinishLayout - Finalize a layout, including fragment lowering.
681 void FinishLayout(MCAsmLayout &Layout);
684 /// Find the symbol which defines the atom containing the given symbol, or
685 /// null if there is no such symbol.
686 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
688 /// Check whether a particular symbol is visible to the linker and is required
689 /// in the symbol table, or whether it can be discarded by the assembler. This
690 /// also effects whether the assembler treats the label as potentially
691 /// defining a separate atom.
692 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
694 /// Emit the section contents using the given object writer.
696 // FIXME: Should MCAssembler always have a reference to the object writer?
697 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
698 MCObjectWriter *OW) const;
700 void AddSectionToTheEnd(const MCObjectWriter &Writer, MCSectionData &SD,
701 MCAsmLayout &Layout);
704 /// Construct a new assembler instance.
706 /// \arg OS - The stream to output to.
708 // FIXME: How are we going to parameterize this? Two obvious options are stay
709 // concrete and require clients to pass in a target like object. The other
710 // option is to make this abstract, and have targets provide concrete
711 // implementations as we do with AsmParser.
712 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
713 MCCodeEmitter &_Emitter, bool _PadSectionToAlignment,
717 MCContext &getContext() const { return Context; }
719 TargetAsmBackend &getBackend() const { return Backend; }
721 MCCodeEmitter &getEmitter() const { return Emitter; }
723 /// Finish - Do final processing and write the object to the output stream.
724 /// \arg Writer is used for custom object writer (as the MCJIT does),
725 /// if not specified it is automatically created from backend.
726 void Finish(MCObjectWriter *Writer = 0);
728 // FIXME: This does not belong here.
729 bool getSubsectionsViaSymbols() const {
730 return SubsectionsViaSymbols;
732 void setSubsectionsViaSymbols(bool Value) {
733 SubsectionsViaSymbols = Value;
736 bool getRelaxAll() const { return RelaxAll; }
737 void setRelaxAll(bool Value) { RelaxAll = Value; }
739 /// @name Section List Access
742 const SectionDataListType &getSectionList() const { return Sections; }
743 SectionDataListType &getSectionList() { return Sections; }
745 iterator begin() { return Sections.begin(); }
746 const_iterator begin() const { return Sections.begin(); }
748 iterator end() { return Sections.end(); }
749 const_iterator end() const { return Sections.end(); }
751 size_t size() const { return Sections.size(); }
754 /// @name Symbol List Access
757 const SymbolDataListType &getSymbolList() const { return Symbols; }
758 SymbolDataListType &getSymbolList() { return Symbols; }
760 symbol_iterator symbol_begin() { return Symbols.begin(); }
761 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
763 symbol_iterator symbol_end() { return Symbols.end(); }
764 const_symbol_iterator symbol_end() const { return Symbols.end(); }
766 size_t symbol_size() const { return Symbols.size(); }
769 /// @name Indirect Symbol List Access
772 // FIXME: This is a total hack, this should not be here. Once things are
773 // factored so that the streamer has direct access to the .o writer, it can
775 std::vector<IndirectSymbolData> &getIndirectSymbols() {
776 return IndirectSymbols;
779 indirect_symbol_iterator indirect_symbol_begin() {
780 return IndirectSymbols.begin();
782 const_indirect_symbol_iterator indirect_symbol_begin() const {
783 return IndirectSymbols.begin();
786 indirect_symbol_iterator indirect_symbol_end() {
787 return IndirectSymbols.end();
789 const_indirect_symbol_iterator indirect_symbol_end() const {
790 return IndirectSymbols.end();
793 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
796 /// @name Backend Data Access
799 MCSectionData &getSectionData(const MCSection &Section) const {
800 MCSectionData *Entry = SectionMap.lookup(&Section);
801 assert(Entry && "Missing section data!");
805 MCSectionData &getOrCreateSectionData(const MCSection &Section,
807 MCSectionData *&Entry = SectionMap[&Section];
809 if (Created) *Created = !Entry;
811 Entry = new MCSectionData(Section, this);
816 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
817 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
818 assert(Entry && "Missing symbol data!");
822 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
824 MCSymbolData *&Entry = SymbolMap[&Symbol];
826 if (Created) *Created = !Entry;
828 Entry = new MCSymbolData(Symbol, 0, 0, this);
838 } // end namespace llvm