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.
37 class TargetAsmBackend;
39 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
40 /// which needs to be rewritten. This region will either be rewritten by the
41 /// assembler or cause a relocation entry to be generated.
43 // FIXME: This should probably just be merged with MCFixup.
46 /// Offset - The offset inside the fragment which needs to be rewritten.
49 /// Value - The expression to eventually write into the fragment.
52 /// Kind - The fixup kind.
56 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
57 : Offset(_Offset), Value(&_Value), Kind(_Kind) {}
60 class MCFragment : public ilist_node<MCFragment> {
61 friend class MCAsmLayout;
63 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
64 void operator=(const MCFragment&); // DO NOT IMPLEMENT
79 /// Parent - The data for the section this fragment is in.
80 MCSectionData *Parent;
82 /// Atom - The atom this fragment is in, as represented by it's defining
83 /// symbol. Atom's are only used by backends which set
84 /// \see MCAsmBackend::hasReliableSymbolDifference().
87 /// @name Assembler Backend Data
90 // FIXME: This could all be kept private to the assembler implementation.
92 /// Offset - The offset of this fragment in its section. This is ~0 until
96 /// EffectiveSize - The compute size of this section. This is ~0 until
98 uint64_t EffectiveSize;
100 /// Ordinal - The global index of this fragment. This is the index across all
101 /// sections, not just the parent section.
107 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
110 // Only for sentinel.
112 virtual ~MCFragment();
114 FragmentType getKind() const { return Kind; }
116 MCSectionData *getParent() const { return Parent; }
117 void setParent(MCSectionData *Value) { Parent = Value; }
119 MCSymbolData *getAtom() const { return Atom; }
120 void setAtom(MCSymbolData *Value) { Atom = Value; }
122 unsigned getOrdinal() const { return Ordinal; }
123 void setOrdinal(unsigned Value) { Ordinal = Value; }
125 static bool classof(const MCFragment *O) { return true; }
130 class MCDataFragment : public MCFragment {
131 SmallString<32> Contents;
133 /// Fixups - The list of fixups in this fragment.
134 std::vector<MCAsmFixup> Fixups;
137 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
138 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
141 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
146 SmallString<32> &getContents() { return Contents; }
147 const SmallString<32> &getContents() const { return Contents; }
150 /// @name Fixup Access
153 void addFixup(MCAsmFixup Fixup) {
154 // Enforce invariant that fixups are in offset order.
155 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
156 "Fixups must be added in order!");
157 Fixups.push_back(Fixup);
160 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
161 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
163 fixup_iterator fixup_begin() { return Fixups.begin(); }
164 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
166 fixup_iterator fixup_end() {return Fixups.end();}
167 const_fixup_iterator fixup_end() const {return Fixups.end();}
169 size_t fixup_size() const { return Fixups.size(); }
173 static bool classof(const MCFragment *F) {
174 return F->getKind() == MCFragment::FT_Data;
176 static bool classof(const MCDataFragment *) { return true; }
181 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
182 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
183 // with this approach (as opposed to making MCInstFragment a very light weight
184 // object with just the MCInst and a code size, then we should just change
185 // MCDataFragment to have an optional MCInst at its end.
186 class MCInstFragment : public MCFragment {
187 /// Inst - The instruction this is a fragment for.
190 /// InstSize - The size of the currently encoded instruction.
193 /// Fixups - The list of fixups in this fragment.
194 SmallVector<MCAsmFixup, 1> Fixups;
197 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator;
198 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator;
201 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
202 : MCFragment(FT_Inst, SD), Inst(_Inst) {
208 SmallVectorImpl<char> &getCode() { return Code; }
209 const SmallVectorImpl<char> &getCode() const { return Code; }
211 unsigned getInstSize() const { return Code.size(); }
213 MCInst &getInst() { return Inst; }
214 const MCInst &getInst() const { return Inst; }
216 void setInst(MCInst Value) { Inst = Value; }
219 /// @name Fixup Access
222 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; }
223 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; }
225 fixup_iterator fixup_begin() { return Fixups.begin(); }
226 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
228 fixup_iterator fixup_end() {return Fixups.end();}
229 const_fixup_iterator fixup_end() const {return Fixups.end();}
231 size_t fixup_size() const { return Fixups.size(); }
235 static bool classof(const MCFragment *F) {
236 return F->getKind() == MCFragment::FT_Inst;
238 static bool classof(const MCInstFragment *) { return true; }
243 class MCAlignFragment : public MCFragment {
244 /// Alignment - The alignment to ensure, in bytes.
247 /// Value - Value to use for filling padding bytes.
250 /// ValueSize - The size of the integer (in bytes) of \arg Value.
253 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
254 /// cannot be satisfied in this width then this fragment is ignored.
255 unsigned MaxBytesToEmit;
257 /// EmitNops - true when aligning code and optimal nops to be used for
262 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
263 unsigned _MaxBytesToEmit, bool _EmitNops,
264 MCSectionData *SD = 0)
265 : MCFragment(FT_Align, SD), Alignment(_Alignment),
266 Value(_Value),ValueSize(_ValueSize),
267 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
272 unsigned getAlignment() const { return Alignment; }
274 int64_t getValue() const { return Value; }
276 unsigned getValueSize() const { return ValueSize; }
278 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
280 unsigned getEmitNops() const { return EmitNops; }
284 static bool classof(const MCFragment *F) {
285 return F->getKind() == MCFragment::FT_Align;
287 static bool classof(const MCAlignFragment *) { return true; }
292 class MCFillFragment : public MCFragment {
293 /// Value - Value to use for filling bytes.
296 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
299 /// Count - The number of copies of \arg Value to insert.
303 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
304 MCSectionData *SD = 0)
305 : MCFragment(FT_Fill, SD),
306 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
311 int64_t getValue() const { return Value; }
313 unsigned getValueSize() const { return ValueSize; }
315 uint64_t getCount() const { return Count; }
319 static bool classof(const MCFragment *F) {
320 return F->getKind() == MCFragment::FT_Fill;
322 static bool classof(const MCFillFragment *) { return true; }
327 class MCOrgFragment : public MCFragment {
328 /// Offset - The offset this fragment should start at.
329 const MCExpr *Offset;
331 /// Value - Value to use for filling bytes.
335 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
336 : MCFragment(FT_Org, SD),
337 Offset(&_Offset), Value(_Value) {}
342 const MCExpr &getOffset() const { return *Offset; }
344 uint8_t getValue() const { return Value; }
348 static bool classof(const MCFragment *F) {
349 return F->getKind() == MCFragment::FT_Org;
351 static bool classof(const MCOrgFragment *) { return true; }
356 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
357 /// but requires no physical space in the object file.
358 class MCZeroFillFragment : public MCFragment {
359 /// Size - The size of this fragment.
362 /// Alignment - The alignment for this fragment.
366 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
367 : MCFragment(FT_ZeroFill, SD),
368 Size(_Size), Alignment(_Alignment) {}
373 uint64_t getSize() const { return Size; }
375 unsigned getAlignment() const { return Alignment; }
379 static bool classof(const MCFragment *F) {
380 return F->getKind() == MCFragment::FT_ZeroFill;
382 static bool classof(const MCZeroFillFragment *) { return true; }
387 // FIXME: Should this be a separate class, or just merged into MCSection? Since
388 // we anticipate the fast path being through an MCAssembler, the only reason to
389 // keep it out is for API abstraction.
390 class MCSectionData : public ilist_node<MCSectionData> {
391 friend class MCAsmLayout;
393 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
394 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
397 typedef iplist<MCFragment> FragmentListType;
399 typedef FragmentListType::const_iterator const_iterator;
400 typedef FragmentListType::iterator iterator;
402 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
403 typedef FragmentListType::reverse_iterator reverse_iterator;
406 iplist<MCFragment> Fragments;
407 const MCSection *Section;
409 /// Ordinal - The section index in the assemblers section list.
412 /// Alignment - The maximum alignment seen in this section.
415 /// @name Assembler Backend Data
418 // FIXME: This could all be kept private to the assembler implementation.
420 /// Address - The computed address of this section. This is ~0 until
424 /// Size - The content size of this section. This is ~0 until initialized.
427 /// FileSize - The size of this section in the object file. This is ~0 until
431 /// HasInstructions - Whether this section has had instructions emitted into
433 unsigned HasInstructions : 1;
438 // Only for use as sentinel.
440 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
442 const MCSection &getSection() const { return *Section; }
444 unsigned getAlignment() const { return Alignment; }
445 void setAlignment(unsigned Value) { Alignment = Value; }
447 bool hasInstructions() const { return HasInstructions; }
448 void setHasInstructions(bool Value) { HasInstructions = Value; }
450 unsigned getOrdinal() const { return Ordinal; }
451 void setOrdinal(unsigned Value) { Ordinal = Value; }
453 /// @name Fragment Access
456 const FragmentListType &getFragmentList() const { return Fragments; }
457 FragmentListType &getFragmentList() { return Fragments; }
459 iterator begin() { return Fragments.begin(); }
460 const_iterator begin() const { return Fragments.begin(); }
462 iterator end() { return Fragments.end(); }
463 const_iterator end() const { return Fragments.end(); }
465 reverse_iterator rbegin() { return Fragments.rbegin(); }
466 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
468 reverse_iterator rend() { return Fragments.rend(); }
469 const_reverse_iterator rend() const { return Fragments.rend(); }
471 size_t size() const { return Fragments.size(); }
473 bool empty() const { return Fragments.empty(); }
480 // FIXME: Same concerns as with SectionData.
481 class MCSymbolData : public ilist_node<MCSymbolData> {
483 const MCSymbol *Symbol;
485 /// Fragment - The fragment this symbol's value is relative to, if any.
486 MCFragment *Fragment;
488 /// Offset - The offset to apply to the fragment address to form this symbol's
492 /// IsExternal - True if this symbol is visible outside this translation
494 unsigned IsExternal : 1;
496 /// IsPrivateExtern - True if this symbol is private extern.
497 unsigned IsPrivateExtern : 1;
499 /// CommonSize - The size of the symbol, if it is 'common', or 0.
501 // FIXME: Pack this in with other fields? We could put it in offset, since a
502 // common symbol can never get a definition.
505 /// CommonAlign - The alignment of the symbol, if it is 'common'.
507 // FIXME: Pack this in with other fields?
508 unsigned CommonAlign;
510 /// Flags - The Flags field is used by object file implementations to store
511 /// additional per symbol information which is not easily classified.
514 /// Index - Index field, for use by the object file implementation.
518 // Only for use as sentinel.
520 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
526 const MCSymbol &getSymbol() const { return *Symbol; }
528 MCFragment *getFragment() const { return Fragment; }
529 void setFragment(MCFragment *Value) { Fragment = Value; }
531 uint64_t getOffset() const { return Offset; }
532 void setOffset(uint64_t Value) { Offset = Value; }
535 /// @name Symbol Attributes
538 bool isExternal() const { return IsExternal; }
539 void setExternal(bool Value) { IsExternal = Value; }
541 bool isPrivateExtern() const { return IsPrivateExtern; }
542 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
544 /// isCommon - Is this a 'common' symbol.
545 bool isCommon() const { return CommonSize != 0; }
547 /// setCommon - Mark this symbol as being 'common'.
549 /// \param Size - The size of the symbol.
550 /// \param Align - The alignment of the symbol.
551 void setCommon(uint64_t Size, unsigned Align) {
556 /// getCommonSize - Return the size of a 'common' symbol.
557 uint64_t getCommonSize() const {
558 assert(isCommon() && "Not a 'common' symbol!");
562 /// getCommonAlignment - Return the alignment of a 'common' symbol.
563 unsigned getCommonAlignment() const {
564 assert(isCommon() && "Not a 'common' symbol!");
568 /// getFlags - Get the (implementation defined) symbol flags.
569 uint32_t getFlags() const { return Flags; }
571 /// setFlags - Set the (implementation defined) symbol flags.
572 void setFlags(uint32_t Value) { Flags = Value; }
574 /// getIndex - Get the (implementation defined) index.
575 uint64_t getIndex() const { return Index; }
577 /// setIndex - Set the (implementation defined) index.
578 void setIndex(uint64_t Value) { Index = Value; }
585 // FIXME: This really doesn't belong here. See comments below.
586 struct IndirectSymbolData {
588 MCSectionData *SectionData;
592 friend class MCAsmLayout;
595 typedef iplist<MCSectionData> SectionDataListType;
596 typedef iplist<MCSymbolData> SymbolDataListType;
598 typedef SectionDataListType::const_iterator const_iterator;
599 typedef SectionDataListType::iterator iterator;
601 typedef SymbolDataListType::const_iterator const_symbol_iterator;
602 typedef SymbolDataListType::iterator symbol_iterator;
604 typedef std::vector<IndirectSymbolData>::const_iterator
605 const_indirect_symbol_iterator;
606 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
609 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
610 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
614 TargetAsmBackend &Backend;
616 MCCodeEmitter &Emitter;
620 iplist<MCSectionData> Sections;
622 iplist<MCSymbolData> Symbols;
624 /// The map of sections to their associated assembler backend data.
626 // FIXME: Avoid this indirection?
627 DenseMap<const MCSection*, MCSectionData*> SectionMap;
629 /// The map of symbols to their associated assembler backend data.
631 // FIXME: Avoid this indirection?
632 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
634 std::vector<IndirectSymbolData> IndirectSymbols;
636 unsigned RelaxAll : 1;
637 unsigned SubsectionsViaSymbols : 1;
640 /// Evaluate a fixup to a relocatable expression and the value which should be
641 /// placed into the fixup.
643 /// \param Layout The layout to use for evaluation.
644 /// \param Fixup The fixup to evaluate.
645 /// \param DF The fragment the fixup is inside.
646 /// \param Target [out] On return, the relocatable expression the fixup
648 /// \param Value [out] On return, the value of the fixup as currently layed
650 /// \return Whether the fixup value was fully resolved. This is true if the
651 /// \arg Value result is fixed, otherwise the value may change due to
653 bool EvaluateFixup(const MCAsmLayout &Layout,
654 const MCAsmFixup &Fixup, const MCFragment *DF,
655 MCValue &Target, uint64_t &Value) const;
657 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
658 /// (increased in size, in order to hold its value correctly).
659 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
660 const MCAsmLayout &Layout) const;
662 /// Check whether the given fragment needs relaxation.
663 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
664 const MCAsmLayout &Layout) const;
666 /// LayoutSection - Assign the section the given \arg StartAddress, and then
667 /// assign offsets and sizes to the fragments in the section \arg SD, and
668 /// update the section size.
670 /// \return The address at the end of the section, for use in laying out the
671 /// succeeding section.
672 uint64_t LayoutSection(MCSectionData &SD, MCAsmLayout &Layout,
673 uint64_t StartAddress);
675 /// LayoutOnce - Perform one layout iteration and return true if any offsets
677 bool LayoutOnce(MCAsmLayout &Layout);
679 /// FinishLayout - Finalize a layout, including fragment lowering.
680 void FinishLayout(MCAsmLayout &Layout);
683 /// Find the symbol which defines the atom containing the given symbol, or
684 /// null if there is no such symbol.
685 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
686 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 MCSymbolData *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;
701 /// Construct a new assembler instance.
703 /// \arg OS - The stream to output to.
705 // FIXME: How are we going to parameterize this? Two obvious options are stay
706 // concrete and require clients to pass in a target like object. The other
707 // option is to make this abstract, and have targets provide concrete
708 // implementations as we do with AsmParser.
709 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
710 MCCodeEmitter &_Emitter, raw_ostream &OS);
713 MCContext &getContext() const { return Context; }
715 TargetAsmBackend &getBackend() const { return Backend; }
717 MCCodeEmitter &getEmitter() const { return Emitter; }
719 /// Finish - Do final processing and write the object to the output stream.
722 // FIXME: This does not belong here.
723 bool getSubsectionsViaSymbols() const {
724 return SubsectionsViaSymbols;
726 void setSubsectionsViaSymbols(bool Value) {
727 SubsectionsViaSymbols = Value;
730 bool getRelaxAll() const { return RelaxAll; }
731 void setRelaxAll(bool Value) { RelaxAll = Value; }
733 /// @name Section List Access
736 const SectionDataListType &getSectionList() const { return Sections; }
737 SectionDataListType &getSectionList() { return Sections; }
739 iterator begin() { return Sections.begin(); }
740 const_iterator begin() const { return Sections.begin(); }
742 iterator end() { return Sections.end(); }
743 const_iterator end() const { return Sections.end(); }
745 size_t size() const { return Sections.size(); }
748 /// @name Symbol List Access
751 const SymbolDataListType &getSymbolList() const { return Symbols; }
752 SymbolDataListType &getSymbolList() { return Symbols; }
754 symbol_iterator symbol_begin() { return Symbols.begin(); }
755 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
757 symbol_iterator symbol_end() { return Symbols.end(); }
758 const_symbol_iterator symbol_end() const { return Symbols.end(); }
760 size_t symbol_size() const { return Symbols.size(); }
763 /// @name Indirect Symbol List Access
766 // FIXME: This is a total hack, this should not be here. Once things are
767 // factored so that the streamer has direct access to the .o writer, it can
769 std::vector<IndirectSymbolData> &getIndirectSymbols() {
770 return IndirectSymbols;
773 indirect_symbol_iterator indirect_symbol_begin() {
774 return IndirectSymbols.begin();
776 const_indirect_symbol_iterator indirect_symbol_begin() const {
777 return IndirectSymbols.begin();
780 indirect_symbol_iterator indirect_symbol_end() {
781 return IndirectSymbols.end();
783 const_indirect_symbol_iterator indirect_symbol_end() const {
784 return IndirectSymbols.end();
787 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
790 /// @name Backend Data Access
793 MCSectionData &getSectionData(const MCSection &Section) const {
794 MCSectionData *Entry = SectionMap.lookup(&Section);
795 assert(Entry && "Missing section data!");
799 MCSectionData &getOrCreateSectionData(const MCSection &Section,
801 MCSectionData *&Entry = SectionMap[&Section];
803 if (Created) *Created = !Entry;
805 Entry = new MCSectionData(Section, this);
810 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
811 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
812 assert(Entry && "Missing symbol data!");
816 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
818 MCSymbolData *&Entry = SymbolMap[&Symbol];
820 if (Created) *Created = !Entry;
822 Entry = new MCSymbolData(Symbol, 0, 0, this);
832 } // end namespace llvm