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.
36 class TargetAsmBackend;
38 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
39 /// which needs to be rewritten. This region will either be rewritten by the
40 /// assembler or cause a relocation entry to be generated.
42 // FIXME: This should probably just be merged with MCFixup.
45 /// Offset - The offset inside the fragment which needs to be rewritten.
48 /// Value - The expression to eventually write into the fragment.
51 /// Kind - The fixup kind.
55 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
56 : Offset(_Offset), Value(&_Value), Kind(_Kind) {}
59 class MCFragment : public ilist_node<MCFragment> {
60 friend class MCAsmLayout;
62 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
63 void operator=(const MCFragment&); // DO NOT IMPLEMENT
78 /// Parent - The data for the section this fragment is in.
79 MCSectionData *Parent;
81 /// @name Assembler Backend Data
84 // FIXME: This could all be kept private to the assembler implementation.
86 /// Offset - The offset of this fragment in its section. This is ~0 until
90 /// FileSize - The file size of this section. This is ~0 until initialized.
96 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
101 virtual ~MCFragment();
103 FragmentType getKind() const { return Kind; }
105 MCSectionData *getParent() const { return Parent; }
106 void setParent(MCSectionData *Value) { Parent = Value; }
108 /// @name Assembler Backend Support
111 // FIXME: This could all be kept private to the assembler implementation.
113 uint64_t getFileSize() const {
114 assert(FileSize != ~UINT64_C(0) && "File size not set!");
117 void setFileSize(uint64_t Value) { FileSize = Value; }
119 uint64_t getOffset() const {
120 assert(Offset != ~UINT64_C(0) && "File offset not set!");
123 void setOffset(uint64_t Value) { Offset = Value; }
127 static bool classof(const MCFragment *O) { return true; }
132 class MCDataFragment : public MCFragment {
133 SmallString<32> Contents;
135 /// Fixups - The list of fixups in this fragment.
136 std::vector<MCAsmFixup> Fixups;
139 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
140 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
143 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
148 SmallString<32> &getContents() { return Contents; }
149 const SmallString<32> &getContents() const { return Contents; }
152 /// @name Fixup Access
155 void addFixup(MCAsmFixup Fixup) {
156 // Enforce invariant that fixups are in offset order.
157 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
158 "Fixups must be added in order!");
159 Fixups.push_back(Fixup);
162 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
163 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
165 fixup_iterator fixup_begin() { return Fixups.begin(); }
166 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
168 fixup_iterator fixup_end() {return Fixups.end();}
169 const_fixup_iterator fixup_end() const {return Fixups.end();}
171 size_t fixup_size() const { return Fixups.size(); }
175 static bool classof(const MCFragment *F) {
176 return F->getKind() == MCFragment::FT_Data;
178 static bool classof(const MCDataFragment *) { return true; }
183 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
184 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
185 // with this approach (as opposed to making MCInstFragment a very light weight
186 // object with just the MCInst and a code size, then we should just change
187 // MCDataFragment to have an optional MCInst at its end.
188 class MCInstFragment : public MCFragment {
189 /// Inst - The instruction this is a fragment for.
192 /// InstSize - The size of the currently encoded instruction.
195 /// Fixups - The list of fixups in this fragment.
196 SmallVector<MCAsmFixup, 1> Fixups;
199 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator;
200 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator;
203 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
204 : MCFragment(FT_Inst, SD), Inst(_Inst) {
210 SmallVectorImpl<char> &getCode() { return Code; }
211 const SmallVectorImpl<char> &getCode() const { return Code; }
213 unsigned getInstSize() const { return Code.size(); }
215 MCInst &getInst() { return Inst; }
216 const MCInst &getInst() const { return Inst; }
218 void setInst(MCInst Value) { Inst = Value; }
221 /// @name Fixup Access
224 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; }
225 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; }
227 fixup_iterator fixup_begin() { return Fixups.begin(); }
228 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
230 fixup_iterator fixup_end() {return Fixups.end();}
231 const_fixup_iterator fixup_end() const {return Fixups.end();}
233 size_t fixup_size() const { return Fixups.size(); }
237 static bool classof(const MCFragment *F) {
238 return F->getKind() == MCFragment::FT_Inst;
240 static bool classof(const MCInstFragment *) { return true; }
245 class MCAlignFragment : public MCFragment {
246 /// Alignment - The alignment to ensure, in bytes.
249 /// Value - Value to use for filling padding bytes.
252 /// ValueSize - The size of the integer (in bytes) of \arg Value.
255 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
256 /// cannot be satisfied in this width then this fragment is ignored.
257 unsigned MaxBytesToEmit;
259 /// EmitNops - true when aligning code and optimal nops to be used for
264 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
265 unsigned _MaxBytesToEmit, bool _EmitNops,
266 MCSectionData *SD = 0)
267 : MCFragment(FT_Align, SD), Alignment(_Alignment),
268 Value(_Value),ValueSize(_ValueSize),
269 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
274 unsigned getAlignment() const { return Alignment; }
276 int64_t getValue() const { return Value; }
278 unsigned getValueSize() const { return ValueSize; }
280 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
282 unsigned getEmitNops() const { return EmitNops; }
286 static bool classof(const MCFragment *F) {
287 return F->getKind() == MCFragment::FT_Align;
289 static bool classof(const MCAlignFragment *) { return true; }
294 class MCFillFragment : public MCFragment {
295 /// Value - Value to use for filling bytes.
298 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
301 /// Count - The number of copies of \arg Value to insert.
305 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
306 MCSectionData *SD = 0)
307 : MCFragment(FT_Fill, SD),
308 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
313 int64_t getValue() const { return Value; }
315 unsigned getValueSize() const { return ValueSize; }
317 uint64_t getCount() const { return Count; }
321 static bool classof(const MCFragment *F) {
322 return F->getKind() == MCFragment::FT_Fill;
324 static bool classof(const MCFillFragment *) { return true; }
329 class MCOrgFragment : public MCFragment {
330 /// Offset - The offset this fragment should start at.
331 const MCExpr *Offset;
333 /// Value - Value to use for filling bytes.
337 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
338 : MCFragment(FT_Org, SD),
339 Offset(&_Offset), Value(_Value) {}
344 const MCExpr &getOffset() const { return *Offset; }
346 uint8_t getValue() const { return Value; }
350 static bool classof(const MCFragment *F) {
351 return F->getKind() == MCFragment::FT_Org;
353 static bool classof(const MCOrgFragment *) { return true; }
358 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
359 /// but requires no physical space in the object file.
360 class MCZeroFillFragment : public MCFragment {
361 /// Size - The size of this fragment.
364 /// Alignment - The alignment for this fragment.
368 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
369 : MCFragment(FT_ZeroFill, SD),
370 Size(_Size), Alignment(_Alignment) {}
375 uint64_t getSize() const { return Size; }
377 unsigned getAlignment() const { return Alignment; }
381 static bool classof(const MCFragment *F) {
382 return F->getKind() == MCFragment::FT_ZeroFill;
384 static bool classof(const MCZeroFillFragment *) { return true; }
389 // FIXME: Should this be a separate class, or just merged into MCSection? Since
390 // we anticipate the fast path being through an MCAssembler, the only reason to
391 // keep it out is for API abstraction.
392 class MCSectionData : public ilist_node<MCSectionData> {
393 friend class MCAsmLayout;
395 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
396 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
399 typedef iplist<MCFragment> FragmentListType;
401 typedef FragmentListType::const_iterator const_iterator;
402 typedef FragmentListType::iterator iterator;
404 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
405 typedef FragmentListType::reverse_iterator reverse_iterator;
408 iplist<MCFragment> Fragments;
409 const MCSection *Section;
411 /// Alignment - The maximum alignment seen in this section.
414 /// @name Assembler Backend Data
417 // FIXME: This could all be kept private to the assembler implementation.
419 /// Address - The computed address of this section. This is ~0 until
423 /// Size - The content size of this section. This is ~0 until initialized.
426 /// FileSize - The size of this section in the object file. This is ~0 until
430 /// HasInstructions - Whether this section has had instructions emitted into
432 unsigned HasInstructions : 1;
437 // Only for use as sentinel.
439 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
441 const MCSection &getSection() const { return *Section; }
443 unsigned getAlignment() const { return Alignment; }
444 void setAlignment(unsigned Value) { Alignment = Value; }
446 /// @name Fragment Access
449 const FragmentListType &getFragmentList() const { return Fragments; }
450 FragmentListType &getFragmentList() { return Fragments; }
452 iterator begin() { return Fragments.begin(); }
453 const_iterator begin() const { return Fragments.begin(); }
455 iterator end() { return Fragments.end(); }
456 const_iterator end() const { return Fragments.end(); }
458 reverse_iterator rbegin() { return Fragments.rbegin(); }
459 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
461 reverse_iterator rend() { return Fragments.rend(); }
462 const_reverse_iterator rend() const { return Fragments.rend(); }
464 size_t size() const { return Fragments.size(); }
466 bool empty() const { return Fragments.empty(); }
469 /// @name Assembler Backend Support
472 // FIXME: This could all be kept private to the assembler implementation.
474 uint64_t getSize() const {
475 assert(Size != ~UINT64_C(0) && "File size not set!");
478 void setSize(uint64_t Value) { Size = Value; }
480 uint64_t getFileSize() const {
481 assert(FileSize != ~UINT64_C(0) && "File size not set!");
484 void setFileSize(uint64_t Value) { FileSize = Value; }
486 bool hasInstructions() const { return HasInstructions; }
487 void setHasInstructions(bool Value) { HasInstructions = Value; }
494 // FIXME: Same concerns as with SectionData.
495 class MCSymbolData : public ilist_node<MCSymbolData> {
497 const MCSymbol *Symbol;
499 /// Fragment - The fragment this symbol's value is relative to, if any.
500 MCFragment *Fragment;
502 /// Offset - The offset to apply to the fragment address to form this symbol's
506 /// IsExternal - True if this symbol is visible outside this translation
508 unsigned IsExternal : 1;
510 /// IsPrivateExtern - True if this symbol is private extern.
511 unsigned IsPrivateExtern : 1;
513 /// CommonSize - The size of the symbol, if it is 'common', or 0.
515 // FIXME: Pack this in with other fields? We could put it in offset, since a
516 // common symbol can never get a definition.
519 /// CommonAlign - The alignment of the symbol, if it is 'common'.
521 // FIXME: Pack this in with other fields?
522 unsigned CommonAlign;
524 /// Flags - The Flags field is used by object file implementations to store
525 /// additional per symbol information which is not easily classified.
528 /// Index - Index field, for use by the object file implementation.
532 // Only for use as sentinel.
534 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
540 const MCSymbol &getSymbol() const { return *Symbol; }
542 MCFragment *getFragment() const { return Fragment; }
543 void setFragment(MCFragment *Value) { Fragment = Value; }
545 uint64_t getOffset() const { return Offset; }
546 void setOffset(uint64_t Value) { Offset = Value; }
549 /// @name Symbol Attributes
552 bool isExternal() const { return IsExternal; }
553 void setExternal(bool Value) { IsExternal = Value; }
555 bool isPrivateExtern() const { return IsPrivateExtern; }
556 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
558 /// isCommon - Is this a 'common' symbol.
559 bool isCommon() const { return CommonSize != 0; }
561 /// setCommon - Mark this symbol as being 'common'.
563 /// \param Size - The size of the symbol.
564 /// \param Align - The alignment of the symbol.
565 void setCommon(uint64_t Size, unsigned Align) {
570 /// getCommonSize - Return the size of a 'common' symbol.
571 uint64_t getCommonSize() const {
572 assert(isCommon() && "Not a 'common' symbol!");
576 /// getCommonAlignment - Return the alignment of a 'common' symbol.
577 unsigned getCommonAlignment() const {
578 assert(isCommon() && "Not a 'common' symbol!");
582 /// getFlags - Get the (implementation defined) symbol flags.
583 uint32_t getFlags() const { return Flags; }
585 /// setFlags - Set the (implementation defined) symbol flags.
586 void setFlags(uint32_t Value) { Flags = Value; }
588 /// getIndex - Get the (implementation defined) index.
589 uint64_t getIndex() const { return Index; }
591 /// setIndex - Set the (implementation defined) index.
592 void setIndex(uint64_t Value) { Index = Value; }
599 // FIXME: This really doesn't belong here. See comments below.
600 struct IndirectSymbolData {
602 MCSectionData *SectionData;
607 typedef iplist<MCSectionData> SectionDataListType;
608 typedef iplist<MCSymbolData> SymbolDataListType;
610 typedef SectionDataListType::const_iterator const_iterator;
611 typedef SectionDataListType::iterator iterator;
613 typedef SymbolDataListType::const_iterator const_symbol_iterator;
614 typedef SymbolDataListType::iterator symbol_iterator;
616 typedef std::vector<IndirectSymbolData>::const_iterator
617 const_indirect_symbol_iterator;
618 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
621 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
622 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
626 TargetAsmBackend &Backend;
628 MCCodeEmitter &Emitter;
632 iplist<MCSectionData> Sections;
634 iplist<MCSymbolData> Symbols;
636 /// The map of sections to their associated assembler backend data.
638 // FIXME: Avoid this indirection?
639 DenseMap<const MCSection*, MCSectionData*> SectionMap;
641 /// The map of symbols to their associated assembler backend data.
643 // FIXME: Avoid this indirection?
644 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
646 std::vector<IndirectSymbolData> IndirectSymbols;
648 unsigned SubsectionsViaSymbols : 1;
651 /// Evaluate a fixup to a relocatable expression and the value which should be
652 /// placed into the fixup.
654 /// \param Layout The layout to use for evaluation.
655 /// \param Fixup The fixup to evaluate.
656 /// \param DF The fragment the fixup is inside.
657 /// \param Target [out] On return, the relocatable expression the fixup
659 /// \param Value [out] On return, the value of the fixup as currently layed
661 /// \return Whether the fixup value was fully resolved. This is true if the
662 /// \arg Value result is fixed, otherwise the value may change due to
664 bool EvaluateFixup(const MCAsmLayout &Layout,
665 const MCAsmFixup &Fixup, const MCFragment *DF,
666 MCValue &Target, uint64_t &Value) const;
668 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
669 /// (increased in size, in order to hold its value correctly).
670 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
671 const MCAsmLayout &Layout) const;
673 /// Check whether the given fragment needs relaxation.
674 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
675 const MCAsmLayout &Layout) const;
677 /// LayoutSection - Assign offsets and sizes to the fragments in the section
678 /// \arg SD, and update the section size. The section file offset should
679 /// already have been computed.
680 void LayoutSection(MCSectionData &SD, MCAsmLayout &Layout);
682 /// LayoutOnce - Perform one layout iteration and return true if any offsets
684 bool LayoutOnce(MCAsmLayout &Layout);
686 /// FinishLayout - Finalize a layout, including fragment lowering.
687 void FinishLayout(MCAsmLayout &Layout);
690 /// Find the symbol which defines the atom containing given address, inside
691 /// the given section, or null if there is no such symbol.
693 // FIXME-PERF: Eliminate this, it is very slow.
694 const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
695 const MCSectionData *Section,
696 uint64_t Address) const;
698 /// Find the symbol which defines the atom containing the given symbol, or
699 /// null if there is no such symbol.
701 // FIXME-PERF: Eliminate this, it is very slow.
702 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
703 const MCSymbolData *Symbol) const;
705 /// Check whether a particular symbol is visible to the linker and is required
706 /// in the symbol table, or whether it can be discarded by the assembler. This
707 /// also effects whether the assembler treats the label as potentially
708 /// defining a separate atom.
709 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
711 /// Emit the section contents using the given object writer.
713 // FIXME: Should MCAssembler always have a reference to the object writer?
714 void WriteSectionData(const MCSectionData *Section, MCObjectWriter *OW) const;
717 /// Construct a new assembler instance.
719 /// \arg OS - The stream to output to.
721 // FIXME: How are we going to parameterize this? Two obvious options are stay
722 // concrete and require clients to pass in a target like object. The other
723 // option is to make this abstract, and have targets provide concrete
724 // implementations as we do with AsmParser.
725 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
726 MCCodeEmitter &_Emitter, raw_ostream &OS);
729 MCContext &getContext() const { return Context; }
731 TargetAsmBackend &getBackend() const { return Backend; }
733 MCCodeEmitter &getEmitter() const { return Emitter; }
735 /// Finish - Do final processing and write the object to the output stream.
738 // FIXME: This does not belong here.
739 bool getSubsectionsViaSymbols() const {
740 return SubsectionsViaSymbols;
742 void setSubsectionsViaSymbols(bool Value) {
743 SubsectionsViaSymbols = Value;
746 /// @name Section List Access
749 const SectionDataListType &getSectionList() const { return Sections; }
750 SectionDataListType &getSectionList() { return Sections; }
752 iterator begin() { return Sections.begin(); }
753 const_iterator begin() const { return Sections.begin(); }
755 iterator end() { return Sections.end(); }
756 const_iterator end() const { return Sections.end(); }
758 size_t size() const { return Sections.size(); }
761 /// @name Symbol List Access
764 const SymbolDataListType &getSymbolList() const { return Symbols; }
765 SymbolDataListType &getSymbolList() { return Symbols; }
767 symbol_iterator symbol_begin() { return Symbols.begin(); }
768 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
770 symbol_iterator symbol_end() { return Symbols.end(); }
771 const_symbol_iterator symbol_end() const { return Symbols.end(); }
773 size_t symbol_size() const { return Symbols.size(); }
776 /// @name Indirect Symbol List Access
779 // FIXME: This is a total hack, this should not be here. Once things are
780 // factored so that the streamer has direct access to the .o writer, it can
782 std::vector<IndirectSymbolData> &getIndirectSymbols() {
783 return IndirectSymbols;
786 indirect_symbol_iterator indirect_symbol_begin() {
787 return IndirectSymbols.begin();
789 const_indirect_symbol_iterator indirect_symbol_begin() const {
790 return IndirectSymbols.begin();
793 indirect_symbol_iterator indirect_symbol_end() {
794 return IndirectSymbols.end();
796 const_indirect_symbol_iterator indirect_symbol_end() const {
797 return IndirectSymbols.end();
800 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
803 /// @name Backend Data Access
806 MCSectionData &getSectionData(const MCSection &Section) const {
807 MCSectionData *Entry = SectionMap.lookup(&Section);
808 assert(Entry && "Missing section data!");
812 MCSectionData &getOrCreateSectionData(const MCSection &Section,
814 MCSectionData *&Entry = SectionMap[&Section];
816 if (Created) *Created = !Entry;
818 Entry = new MCSectionData(Section, this);
823 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
824 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
825 assert(Entry && "Missing symbol data!");
829 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
831 MCSymbolData *&Entry = SymbolMap[&Symbol];
833 if (Created) *Created = !Entry;
835 Entry = new MCSymbolData(Symbol, 0, 0, this);
845 } // end namespace llvm