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 /// EffectiveSize - The compute size of this section. This is ~0 until
92 uint64_t EffectiveSize;
97 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
100 // Only for sentinel.
102 virtual ~MCFragment();
104 FragmentType getKind() const { return Kind; }
106 MCSectionData *getParent() const { return Parent; }
107 void setParent(MCSectionData *Value) { Parent = Value; }
109 static bool classof(const MCFragment *O) { return true; }
114 class MCDataFragment : public MCFragment {
115 SmallString<32> Contents;
117 /// Fixups - The list of fixups in this fragment.
118 std::vector<MCAsmFixup> Fixups;
121 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
122 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
125 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
130 SmallString<32> &getContents() { return Contents; }
131 const SmallString<32> &getContents() const { return Contents; }
134 /// @name Fixup Access
137 void addFixup(MCAsmFixup Fixup) {
138 // Enforce invariant that fixups are in offset order.
139 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
140 "Fixups must be added in order!");
141 Fixups.push_back(Fixup);
144 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
145 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
147 fixup_iterator fixup_begin() { return Fixups.begin(); }
148 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
150 fixup_iterator fixup_end() {return Fixups.end();}
151 const_fixup_iterator fixup_end() const {return Fixups.end();}
153 size_t fixup_size() const { return Fixups.size(); }
157 static bool classof(const MCFragment *F) {
158 return F->getKind() == MCFragment::FT_Data;
160 static bool classof(const MCDataFragment *) { return true; }
165 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
166 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
167 // with this approach (as opposed to making MCInstFragment a very light weight
168 // object with just the MCInst and a code size, then we should just change
169 // MCDataFragment to have an optional MCInst at its end.
170 class MCInstFragment : public MCFragment {
171 /// Inst - The instruction this is a fragment for.
174 /// InstSize - The size of the currently encoded instruction.
177 /// Fixups - The list of fixups in this fragment.
178 SmallVector<MCAsmFixup, 1> Fixups;
181 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator;
182 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator;
185 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
186 : MCFragment(FT_Inst, SD), Inst(_Inst) {
192 SmallVectorImpl<char> &getCode() { return Code; }
193 const SmallVectorImpl<char> &getCode() const { return Code; }
195 unsigned getInstSize() const { return Code.size(); }
197 MCInst &getInst() { return Inst; }
198 const MCInst &getInst() const { return Inst; }
200 void setInst(MCInst Value) { Inst = Value; }
203 /// @name Fixup Access
206 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; }
207 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; }
209 fixup_iterator fixup_begin() { return Fixups.begin(); }
210 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
212 fixup_iterator fixup_end() {return Fixups.end();}
213 const_fixup_iterator fixup_end() const {return Fixups.end();}
215 size_t fixup_size() const { return Fixups.size(); }
219 static bool classof(const MCFragment *F) {
220 return F->getKind() == MCFragment::FT_Inst;
222 static bool classof(const MCInstFragment *) { return true; }
227 class MCAlignFragment : public MCFragment {
228 /// Alignment - The alignment to ensure, in bytes.
231 /// Value - Value to use for filling padding bytes.
234 /// ValueSize - The size of the integer (in bytes) of \arg Value.
237 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
238 /// cannot be satisfied in this width then this fragment is ignored.
239 unsigned MaxBytesToEmit;
241 /// EmitNops - true when aligning code and optimal nops to be used for
246 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
247 unsigned _MaxBytesToEmit, bool _EmitNops,
248 MCSectionData *SD = 0)
249 : MCFragment(FT_Align, SD), Alignment(_Alignment),
250 Value(_Value),ValueSize(_ValueSize),
251 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
256 unsigned getAlignment() const { return Alignment; }
258 int64_t getValue() const { return Value; }
260 unsigned getValueSize() const { return ValueSize; }
262 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
264 unsigned getEmitNops() const { return EmitNops; }
268 static bool classof(const MCFragment *F) {
269 return F->getKind() == MCFragment::FT_Align;
271 static bool classof(const MCAlignFragment *) { return true; }
276 class MCFillFragment : public MCFragment {
277 /// Value - Value to use for filling bytes.
280 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
283 /// Count - The number of copies of \arg Value to insert.
287 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
288 MCSectionData *SD = 0)
289 : MCFragment(FT_Fill, SD),
290 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
295 int64_t getValue() const { return Value; }
297 unsigned getValueSize() const { return ValueSize; }
299 uint64_t getCount() const { return Count; }
303 static bool classof(const MCFragment *F) {
304 return F->getKind() == MCFragment::FT_Fill;
306 static bool classof(const MCFillFragment *) { return true; }
311 class MCOrgFragment : public MCFragment {
312 /// Offset - The offset this fragment should start at.
313 const MCExpr *Offset;
315 /// Value - Value to use for filling bytes.
319 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
320 : MCFragment(FT_Org, SD),
321 Offset(&_Offset), Value(_Value) {}
326 const MCExpr &getOffset() const { return *Offset; }
328 uint8_t getValue() const { return Value; }
332 static bool classof(const MCFragment *F) {
333 return F->getKind() == MCFragment::FT_Org;
335 static bool classof(const MCOrgFragment *) { return true; }
340 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
341 /// but requires no physical space in the object file.
342 class MCZeroFillFragment : public MCFragment {
343 /// Size - The size of this fragment.
346 /// Alignment - The alignment for this fragment.
350 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
351 : MCFragment(FT_ZeroFill, SD),
352 Size(_Size), Alignment(_Alignment) {}
357 uint64_t getSize() const { return Size; }
359 unsigned getAlignment() const { return Alignment; }
363 static bool classof(const MCFragment *F) {
364 return F->getKind() == MCFragment::FT_ZeroFill;
366 static bool classof(const MCZeroFillFragment *) { 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 iplist<MCFragment> Fragments;
391 const MCSection *Section;
393 /// Alignment - The maximum alignment seen in this section.
396 /// @name Assembler Backend Data
399 // FIXME: This could all be kept private to the assembler implementation.
401 /// Address - The computed address of this section. This is ~0 until
405 /// Size - The content size of this section. This is ~0 until initialized.
408 /// FileSize - The size of this section in the object file. This is ~0 until
412 /// HasInstructions - Whether this section has had instructions emitted into
414 unsigned HasInstructions : 1;
419 // Only for use as sentinel.
421 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
423 const MCSection &getSection() const { return *Section; }
425 unsigned getAlignment() const { return Alignment; }
426 void setAlignment(unsigned Value) { Alignment = Value; }
428 /// @name Fragment Access
431 const FragmentListType &getFragmentList() const { return Fragments; }
432 FragmentListType &getFragmentList() { return Fragments; }
434 iterator begin() { return Fragments.begin(); }
435 const_iterator begin() const { return Fragments.begin(); }
437 iterator end() { return Fragments.end(); }
438 const_iterator end() const { return Fragments.end(); }
440 reverse_iterator rbegin() { return Fragments.rbegin(); }
441 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
443 reverse_iterator rend() { return Fragments.rend(); }
444 const_reverse_iterator rend() const { return Fragments.rend(); }
446 size_t size() const { return Fragments.size(); }
448 bool empty() const { return Fragments.empty(); }
451 /// @name Assembler Backend Support
454 // FIXME: This could all be kept private to the assembler implementation.
456 uint64_t getSize() const {
457 assert(Size != ~UINT64_C(0) && "File size not set!");
460 void setSize(uint64_t Value) { Size = Value; }
462 uint64_t getFileSize() const {
463 assert(FileSize != ~UINT64_C(0) && "File size not set!");
466 void setFileSize(uint64_t Value) { FileSize = Value; }
468 bool hasInstructions() const { return HasInstructions; }
469 void setHasInstructions(bool Value) { HasInstructions = Value; }
476 // FIXME: Same concerns as with SectionData.
477 class MCSymbolData : public ilist_node<MCSymbolData> {
479 const MCSymbol *Symbol;
481 /// Fragment - The fragment this symbol's value is relative to, if any.
482 MCFragment *Fragment;
484 /// Offset - The offset to apply to the fragment address to form this symbol's
488 /// IsExternal - True if this symbol is visible outside this translation
490 unsigned IsExternal : 1;
492 /// IsPrivateExtern - True if this symbol is private extern.
493 unsigned IsPrivateExtern : 1;
495 /// CommonSize - The size of the symbol, if it is 'common', or 0.
497 // FIXME: Pack this in with other fields? We could put it in offset, since a
498 // common symbol can never get a definition.
501 /// CommonAlign - The alignment of the symbol, if it is 'common'.
503 // FIXME: Pack this in with other fields?
504 unsigned CommonAlign;
506 /// Flags - The Flags field is used by object file implementations to store
507 /// additional per symbol information which is not easily classified.
510 /// Index - Index field, for use by the object file implementation.
514 // Only for use as sentinel.
516 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
522 const MCSymbol &getSymbol() const { return *Symbol; }
524 MCFragment *getFragment() const { return Fragment; }
525 void setFragment(MCFragment *Value) { Fragment = Value; }
527 uint64_t getOffset() const { return Offset; }
528 void setOffset(uint64_t Value) { Offset = Value; }
531 /// @name Symbol Attributes
534 bool isExternal() const { return IsExternal; }
535 void setExternal(bool Value) { IsExternal = Value; }
537 bool isPrivateExtern() const { return IsPrivateExtern; }
538 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
540 /// isCommon - Is this a 'common' symbol.
541 bool isCommon() const { return CommonSize != 0; }
543 /// setCommon - Mark this symbol as being 'common'.
545 /// \param Size - The size of the symbol.
546 /// \param Align - The alignment of the symbol.
547 void setCommon(uint64_t Size, unsigned Align) {
552 /// getCommonSize - Return the size of a 'common' symbol.
553 uint64_t getCommonSize() const {
554 assert(isCommon() && "Not a 'common' symbol!");
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 /// getIndex - Get the (implementation defined) index.
571 uint64_t getIndex() const { return Index; }
573 /// setIndex - Set the (implementation defined) index.
574 void setIndex(uint64_t Value) { Index = Value; }
581 // FIXME: This really doesn't belong here. See comments below.
582 struct IndirectSymbolData {
584 MCSectionData *SectionData;
589 typedef iplist<MCSectionData> SectionDataListType;
590 typedef iplist<MCSymbolData> SymbolDataListType;
592 typedef SectionDataListType::const_iterator const_iterator;
593 typedef SectionDataListType::iterator iterator;
595 typedef SymbolDataListType::const_iterator const_symbol_iterator;
596 typedef SymbolDataListType::iterator symbol_iterator;
598 typedef std::vector<IndirectSymbolData>::const_iterator
599 const_indirect_symbol_iterator;
600 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
603 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
604 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
608 TargetAsmBackend &Backend;
610 MCCodeEmitter &Emitter;
614 iplist<MCSectionData> Sections;
616 iplist<MCSymbolData> Symbols;
618 /// The map of sections to their associated assembler backend data.
620 // FIXME: Avoid this indirection?
621 DenseMap<const MCSection*, MCSectionData*> SectionMap;
623 /// The map of symbols to their associated assembler backend data.
625 // FIXME: Avoid this indirection?
626 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
628 std::vector<IndirectSymbolData> IndirectSymbols;
630 unsigned SubsectionsViaSymbols : 1;
633 /// Evaluate a fixup to a relocatable expression and the value which should be
634 /// placed into the fixup.
636 /// \param Layout The layout to use for evaluation.
637 /// \param Fixup The fixup to evaluate.
638 /// \param DF The fragment the fixup is inside.
639 /// \param Target [out] On return, the relocatable expression the fixup
641 /// \param Value [out] On return, the value of the fixup as currently layed
643 /// \return Whether the fixup value was fully resolved. This is true if the
644 /// \arg Value result is fixed, otherwise the value may change due to
646 bool EvaluateFixup(const MCAsmLayout &Layout,
647 const MCAsmFixup &Fixup, const MCFragment *DF,
648 MCValue &Target, uint64_t &Value) const;
650 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
651 /// (increased in size, in order to hold its value correctly).
652 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
653 const MCAsmLayout &Layout) const;
655 /// Check whether the given fragment needs relaxation.
656 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
657 const MCAsmLayout &Layout) const;
659 /// LayoutSection - Assign offsets and sizes to the fragments in the section
660 /// \arg SD, and update the section size. The section file offset should
661 /// already have been computed.
662 void LayoutSection(MCSectionData &SD, MCAsmLayout &Layout);
664 /// LayoutOnce - Perform one layout iteration and return true if any offsets
666 bool LayoutOnce(MCAsmLayout &Layout);
668 /// FinishLayout - Finalize a layout, including fragment lowering.
669 void FinishLayout(MCAsmLayout &Layout);
672 /// Find the symbol which defines the atom containing given address, inside
673 /// the given section, or null if there is no such symbol.
675 // FIXME-PERF: Eliminate this, it is very slow.
676 const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
677 const MCSectionData *Section,
678 uint64_t Address) const;
680 /// Find the symbol which defines the atom containing the given symbol, or
681 /// null if there is no such symbol.
683 // FIXME-PERF: Eliminate this, it is very slow.
684 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
685 const MCSymbolData *Symbol) const;
687 /// Check whether a particular symbol is visible to the linker and is required
688 /// in the symbol table, or whether it can be discarded by the assembler. This
689 /// also effects whether the assembler treats the label as potentially
690 /// defining a separate atom.
691 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
693 /// Emit the section contents using the given object writer.
695 // FIXME: Should MCAssembler always have a reference to the object writer?
696 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
697 MCObjectWriter *OW) const;
700 /// Construct a new assembler instance.
702 /// \arg OS - The stream to output to.
704 // FIXME: How are we going to parameterize this? Two obvious options are stay
705 // concrete and require clients to pass in a target like object. The other
706 // option is to make this abstract, and have targets provide concrete
707 // implementations as we do with AsmParser.
708 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
709 MCCodeEmitter &_Emitter, raw_ostream &OS);
712 MCContext &getContext() const { return Context; }
714 TargetAsmBackend &getBackend() const { return Backend; }
716 MCCodeEmitter &getEmitter() const { return Emitter; }
718 /// Finish - Do final processing and write the object to the output stream.
721 // FIXME: This does not belong here.
722 bool getSubsectionsViaSymbols() const {
723 return SubsectionsViaSymbols;
725 void setSubsectionsViaSymbols(bool Value) {
726 SubsectionsViaSymbols = Value;
729 /// @name Section List Access
732 const SectionDataListType &getSectionList() const { return Sections; }
733 SectionDataListType &getSectionList() { return Sections; }
735 iterator begin() { return Sections.begin(); }
736 const_iterator begin() const { return Sections.begin(); }
738 iterator end() { return Sections.end(); }
739 const_iterator end() const { return Sections.end(); }
741 size_t size() const { return Sections.size(); }
744 /// @name Symbol List Access
747 const SymbolDataListType &getSymbolList() const { return Symbols; }
748 SymbolDataListType &getSymbolList() { return Symbols; }
750 symbol_iterator symbol_begin() { return Symbols.begin(); }
751 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
753 symbol_iterator symbol_end() { return Symbols.end(); }
754 const_symbol_iterator symbol_end() const { return Symbols.end(); }
756 size_t symbol_size() const { return Symbols.size(); }
759 /// @name Indirect Symbol List Access
762 // FIXME: This is a total hack, this should not be here. Once things are
763 // factored so that the streamer has direct access to the .o writer, it can
765 std::vector<IndirectSymbolData> &getIndirectSymbols() {
766 return IndirectSymbols;
769 indirect_symbol_iterator indirect_symbol_begin() {
770 return IndirectSymbols.begin();
772 const_indirect_symbol_iterator indirect_symbol_begin() const {
773 return IndirectSymbols.begin();
776 indirect_symbol_iterator indirect_symbol_end() {
777 return IndirectSymbols.end();
779 const_indirect_symbol_iterator indirect_symbol_end() const {
780 return IndirectSymbols.end();
783 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
786 /// @name Backend Data Access
789 MCSectionData &getSectionData(const MCSection &Section) const {
790 MCSectionData *Entry = SectionMap.lookup(&Section);
791 assert(Entry && "Missing section data!");
795 MCSectionData &getOrCreateSectionData(const MCSection &Section,
797 MCSectionData *&Entry = SectionMap[&Section];
799 if (Created) *Created = !Entry;
801 Entry = new MCSectionData(Section, this);
806 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
807 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
808 assert(Entry && "Missing symbol data!");
812 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
814 MCSymbolData *&Entry = SymbolMap[&Symbol];
816 if (Created) *Created = !Entry;
818 Entry = new MCSymbolData(Symbol, 0, 0, this);
828 } // end namespace llvm