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
78 /// Parent - The data for the section this fragment is in.
79 MCSectionData *Parent;
81 /// Atom - The atom this fragment is in, as represented by it's defining
82 /// symbol. Atom's are only used by backends which set
83 /// \see MCAsmBackend::hasReliableSymbolDifference().
86 /// @name Assembler Backend Data
89 // FIXME: This could all be kept private to the assembler implementation.
91 /// Offset - The offset of this fragment in its section. This is ~0 until
95 /// EffectiveSize - The compute size of this section. This is ~0 until
97 uint64_t EffectiveSize;
99 /// Ordinal - The global index of this fragment. This is the index across all
100 /// sections, not just the parent section.
106 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
109 // Only for sentinel.
111 virtual ~MCFragment();
113 FragmentType getKind() const { return Kind; }
115 MCSectionData *getParent() const { return Parent; }
116 void setParent(MCSectionData *Value) { Parent = Value; }
118 MCSymbolData *getAtom() const { return Atom; }
119 void setAtom(MCSymbolData *Value) { Atom = Value; }
121 unsigned getOrdinal() const { return Ordinal; }
122 void setOrdinal(unsigned Value) { Ordinal = Value; }
124 static bool classof(const MCFragment *O) { return true; }
129 class MCDataFragment : public MCFragment {
130 SmallString<32> Contents;
132 /// Fixups - The list of fixups in this fragment.
133 std::vector<MCAsmFixup> Fixups;
136 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
137 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
140 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
145 SmallString<32> &getContents() { return Contents; }
146 const SmallString<32> &getContents() const { return Contents; }
149 /// @name Fixup Access
152 void addFixup(MCAsmFixup Fixup) {
153 // Enforce invariant that fixups are in offset order.
154 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
155 "Fixups must be added in order!");
156 Fixups.push_back(Fixup);
159 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
160 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
162 fixup_iterator fixup_begin() { return Fixups.begin(); }
163 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
165 fixup_iterator fixup_end() {return Fixups.end();}
166 const_fixup_iterator fixup_end() const {return Fixups.end();}
168 size_t fixup_size() const { return Fixups.size(); }
172 static bool classof(const MCFragment *F) {
173 return F->getKind() == MCFragment::FT_Data;
175 static bool classof(const MCDataFragment *) { return true; }
180 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
181 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
182 // with this approach (as opposed to making MCInstFragment a very light weight
183 // object with just the MCInst and a code size, then we should just change
184 // MCDataFragment to have an optional MCInst at its end.
185 class MCInstFragment : public MCFragment {
186 /// Inst - The instruction this is a fragment for.
189 /// InstSize - The size of the currently encoded instruction.
192 /// Fixups - The list of fixups in this fragment.
193 SmallVector<MCAsmFixup, 1> Fixups;
196 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator;
197 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator;
200 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
201 : MCFragment(FT_Inst, SD), Inst(_Inst) {
207 SmallVectorImpl<char> &getCode() { return Code; }
208 const SmallVectorImpl<char> &getCode() const { return Code; }
210 unsigned getInstSize() const { return Code.size(); }
212 MCInst &getInst() { return Inst; }
213 const MCInst &getInst() const { return Inst; }
215 void setInst(MCInst Value) { Inst = Value; }
218 /// @name Fixup Access
221 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; }
222 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; }
224 fixup_iterator fixup_begin() { return Fixups.begin(); }
225 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
227 fixup_iterator fixup_end() {return Fixups.end();}
228 const_fixup_iterator fixup_end() const {return Fixups.end();}
230 size_t fixup_size() const { return Fixups.size(); }
234 static bool classof(const MCFragment *F) {
235 return F->getKind() == MCFragment::FT_Inst;
237 static bool classof(const MCInstFragment *) { return true; }
242 class MCAlignFragment : public MCFragment {
243 /// Alignment - The alignment to ensure, in bytes.
246 /// Value - Value to use for filling padding bytes.
249 /// ValueSize - The size of the integer (in bytes) of \arg Value.
252 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
253 /// cannot be satisfied in this width then this fragment is ignored.
254 unsigned MaxBytesToEmit;
256 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
257 /// of using the provided value. The exact interpretation of this flag is
258 /// target dependent.
261 /// OnlyAlignAddress - Flag to indicate that this align is only used to adjust
262 /// the address space size of a section and that it should not be included as
263 /// part of the section size. This flag can only be used on the last fragment
265 bool OnlyAlignAddress : 1;
268 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
269 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
270 : MCFragment(FT_Align, SD), Alignment(_Alignment),
271 Value(_Value),ValueSize(_ValueSize),
272 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false),
273 OnlyAlignAddress(false) {}
278 unsigned getAlignment() const { return Alignment; }
280 int64_t getValue() const { return Value; }
282 unsigned getValueSize() const { return ValueSize; }
284 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
286 bool hasEmitNops() const { return EmitNops; }
287 void setEmitNops(bool Value) { EmitNops = Value; }
289 bool hasOnlyAlignAddress() const { return OnlyAlignAddress; }
290 void setOnlyAlignAddress(bool Value) { OnlyAlignAddress = Value; }
294 static bool classof(const MCFragment *F) {
295 return F->getKind() == MCFragment::FT_Align;
297 static bool classof(const MCAlignFragment *) { return true; }
302 class MCFillFragment : public MCFragment {
303 /// Value - Value to use for filling bytes.
306 /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
307 /// this is a virtual fill fragment.
310 /// Size - The number of bytes to insert.
314 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
315 MCSectionData *SD = 0)
316 : MCFragment(FT_Fill, SD),
317 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
318 assert((!ValueSize || (Size % ValueSize) == 0) &&
319 "Fill size must be a multiple of the value size!");
325 int64_t getValue() const { return Value; }
327 unsigned getValueSize() const { return ValueSize; }
329 uint64_t getSize() const { return Size; }
333 static bool classof(const MCFragment *F) {
334 return F->getKind() == MCFragment::FT_Fill;
336 static bool classof(const MCFillFragment *) { return true; }
341 class MCOrgFragment : public MCFragment {
342 /// Offset - The offset this fragment should start at.
343 const MCExpr *Offset;
345 /// Value - Value to use for filling bytes.
349 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
350 : MCFragment(FT_Org, SD),
351 Offset(&_Offset), Value(_Value) {}
356 const MCExpr &getOffset() const { return *Offset; }
358 uint8_t getValue() const { return Value; }
362 static bool classof(const MCFragment *F) {
363 return F->getKind() == MCFragment::FT_Org;
365 static bool classof(const MCOrgFragment *) { return true; }
370 // FIXME: Should this be a separate class, or just merged into MCSection? Since
371 // we anticipate the fast path being through an MCAssembler, the only reason to
372 // keep it out is for API abstraction.
373 class MCSectionData : public ilist_node<MCSectionData> {
374 friend class MCAsmLayout;
376 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
377 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
380 typedef iplist<MCFragment> FragmentListType;
382 typedef FragmentListType::const_iterator const_iterator;
383 typedef FragmentListType::iterator iterator;
385 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
386 typedef FragmentListType::reverse_iterator reverse_iterator;
389 iplist<MCFragment> Fragments;
390 const MCSection *Section;
392 /// Ordinal - The section index in the assemblers section list.
395 /// LayoutOrder - The index of this section in the layout order.
396 unsigned LayoutOrder;
398 /// Alignment - The maximum alignment seen in this section.
401 /// @name Assembler Backend Data
404 // FIXME: This could all be kept private to the assembler implementation.
406 /// Address - The computed address of this section. This is ~0 until
410 /// HasInstructions - Whether this section has had instructions emitted into
412 unsigned HasInstructions : 1;
417 // Only for use as sentinel.
419 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
421 const MCSection &getSection() const { return *Section; }
423 unsigned getAlignment() const { return Alignment; }
424 void setAlignment(unsigned Value) { Alignment = Value; }
426 bool hasInstructions() const { return HasInstructions; }
427 void setHasInstructions(bool Value) { HasInstructions = Value; }
429 unsigned getOrdinal() const { return Ordinal; }
430 void setOrdinal(unsigned Value) { Ordinal = Value; }
432 unsigned getLayoutOrder() const { return LayoutOrder; }
433 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
435 /// @name Fragment Access
438 const FragmentListType &getFragmentList() const { return Fragments; }
439 FragmentListType &getFragmentList() { return Fragments; }
441 iterator begin() { return Fragments.begin(); }
442 const_iterator begin() const { return Fragments.begin(); }
444 iterator end() { return Fragments.end(); }
445 const_iterator end() const { return Fragments.end(); }
447 reverse_iterator rbegin() { return Fragments.rbegin(); }
448 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
450 reverse_iterator rend() { return Fragments.rend(); }
451 const_reverse_iterator rend() const { return Fragments.rend(); }
453 size_t size() const { return Fragments.size(); }
455 bool empty() const { return Fragments.empty(); }
462 // FIXME: Same concerns as with SectionData.
463 class MCSymbolData : public ilist_node<MCSymbolData> {
465 const MCSymbol *Symbol;
467 /// Fragment - The fragment this symbol's value is relative to, if any.
468 MCFragment *Fragment;
470 /// Offset - The offset to apply to the fragment address to form this symbol's
474 /// IsExternal - True if this symbol is visible outside this translation
476 unsigned IsExternal : 1;
478 /// IsPrivateExtern - True if this symbol is private extern.
479 unsigned IsPrivateExtern : 1;
481 /// CommonSize - The size of the symbol, if it is 'common', or 0.
483 // FIXME: Pack this in with other fields? We could put it in offset, since a
484 // common symbol can never get a definition.
487 /// CommonAlign - The alignment of the symbol, if it is 'common'.
489 // FIXME: Pack this in with other fields?
490 unsigned CommonAlign;
492 /// Flags - The Flags field is used by object file implementations to store
493 /// additional per symbol information which is not easily classified.
496 /// Index - Index field, for use by the object file implementation.
500 // Only for use as sentinel.
502 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
508 const MCSymbol &getSymbol() const { return *Symbol; }
510 MCFragment *getFragment() const { return Fragment; }
511 void setFragment(MCFragment *Value) { Fragment = Value; }
513 uint64_t getOffset() const { return Offset; }
514 void setOffset(uint64_t Value) { Offset = Value; }
517 /// @name Symbol Attributes
520 bool isExternal() const { return IsExternal; }
521 void setExternal(bool Value) { IsExternal = Value; }
523 bool isPrivateExtern() const { return IsPrivateExtern; }
524 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
526 /// isCommon - Is this a 'common' symbol.
527 bool isCommon() const { return CommonSize != 0; }
529 /// setCommon - Mark this symbol as being 'common'.
531 /// \param Size - The size of the symbol.
532 /// \param Align - The alignment of the symbol.
533 void setCommon(uint64_t Size, unsigned Align) {
538 /// getCommonSize - Return the size of a 'common' symbol.
539 uint64_t getCommonSize() const {
540 assert(isCommon() && "Not a 'common' symbol!");
544 /// getCommonAlignment - Return the alignment of a 'common' symbol.
545 unsigned getCommonAlignment() const {
546 assert(isCommon() && "Not a 'common' symbol!");
550 /// getFlags - Get the (implementation defined) symbol flags.
551 uint32_t getFlags() const { return Flags; }
553 /// setFlags - Set the (implementation defined) symbol flags.
554 void setFlags(uint32_t Value) { Flags = Value; }
556 /// modifyFlags - Modify the flags via a mask
557 void modifyFlags(uint32_t Value, uint32_t Mask) {
558 Flags = (Flags & ~Mask) | Value;
561 /// getIndex - Get the (implementation defined) index.
562 uint64_t getIndex() const { return Index; }
564 /// setIndex - Set the (implementation defined) index.
565 void setIndex(uint64_t Value) { Index = Value; }
572 // FIXME: This really doesn't belong here. See comments below.
573 struct IndirectSymbolData {
575 MCSectionData *SectionData;
579 friend class MCAsmLayout;
582 typedef iplist<MCSectionData> SectionDataListType;
583 typedef iplist<MCSymbolData> SymbolDataListType;
585 typedef SectionDataListType::const_iterator const_iterator;
586 typedef SectionDataListType::iterator iterator;
588 typedef SymbolDataListType::const_iterator const_symbol_iterator;
589 typedef SymbolDataListType::iterator symbol_iterator;
591 typedef std::vector<IndirectSymbolData>::const_iterator
592 const_indirect_symbol_iterator;
593 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
596 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
597 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
601 TargetAsmBackend &Backend;
603 MCCodeEmitter &Emitter;
607 iplist<MCSectionData> Sections;
609 iplist<MCSymbolData> Symbols;
611 /// The map of sections to their associated assembler backend data.
613 // FIXME: Avoid this indirection?
614 DenseMap<const MCSection*, MCSectionData*> SectionMap;
616 /// The map of symbols to their associated assembler backend data.
618 // FIXME: Avoid this indirection?
619 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
621 std::vector<IndirectSymbolData> IndirectSymbols;
623 unsigned RelaxAll : 1;
624 unsigned SubsectionsViaSymbols : 1;
627 /// Evaluate a fixup to a relocatable expression and the value which should be
628 /// placed into the fixup.
630 /// \param Layout The layout to use for evaluation.
631 /// \param Fixup The fixup to evaluate.
632 /// \param DF The fragment the fixup is inside.
633 /// \param Target [out] On return, the relocatable expression the fixup
635 /// \param Value [out] On return, the value of the fixup as currently layed
637 /// \return Whether the fixup value was fully resolved. This is true if the
638 /// \arg Value result is fixed, otherwise the value may change due to
640 bool EvaluateFixup(const MCAsmLayout &Layout,
641 const MCAsmFixup &Fixup, const MCFragment *DF,
642 MCValue &Target, uint64_t &Value) const;
644 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
645 /// (increased in size, in order to hold its value correctly).
646 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
647 const MCAsmLayout &Layout) const;
649 /// Check whether the given fragment needs relaxation.
650 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
651 const MCAsmLayout &Layout) const;
653 /// Compute the effective fragment size assuming it is layed out at the given
654 /// \arg SectionAddress and \arg FragmentOffset.
655 uint64_t ComputeFragmentSize(MCAsmLayout &Layout, const MCFragment &F,
656 uint64_t SectionAddress,
657 uint64_t FragmentOffset) const;
659 /// LayoutFragment - Performs layout of the given \arg Fragment; assuming that
660 /// the previous fragment has already been layed out correctly, and the parent
661 /// section has been initialized.
662 void LayoutFragment(MCAsmLayout &Layout, MCFragment &Fragment);
664 /// LayoutSection - Performs layout of the section referenced by the given
665 /// \arg SectionOrderIndex. The layout assumes that the previous section has
666 /// already been layed out correctly.
667 void LayoutSection(MCAsmLayout &Layout, unsigned SectionOrderIndex);
669 /// LayoutOnce - Perform one layout iteration and return true if any offsets
671 bool LayoutOnce(MCAsmLayout &Layout);
673 /// FinishLayout - Finalize a layout, including fragment lowering.
674 void FinishLayout(MCAsmLayout &Layout);
677 /// Find the symbol which defines the atom containing the given symbol, or
678 /// null if there is no such symbol.
679 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
680 const MCSymbolData *Symbol) const;
682 /// Check whether a particular symbol is visible to the linker and is required
683 /// in the symbol table, or whether it can be discarded by the assembler. This
684 /// also effects whether the assembler treats the label as potentially
685 /// defining a separate atom.
686 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
688 /// Emit the section contents using the given object writer.
690 // FIXME: Should MCAssembler always have a reference to the object writer?
691 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
692 MCObjectWriter *OW) const;
695 /// Construct a new assembler instance.
697 /// \arg OS - The stream to output to.
699 // FIXME: How are we going to parameterize this? Two obvious options are stay
700 // concrete and require clients to pass in a target like object. The other
701 // option is to make this abstract, and have targets provide concrete
702 // implementations as we do with AsmParser.
703 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
704 MCCodeEmitter &_Emitter, raw_ostream &OS);
707 MCContext &getContext() const { return Context; }
709 TargetAsmBackend &getBackend() const { return Backend; }
711 MCCodeEmitter &getEmitter() const { return Emitter; }
713 /// Finish - Do final processing and write the object to the output stream.
716 // FIXME: This does not belong here.
717 bool getSubsectionsViaSymbols() const {
718 return SubsectionsViaSymbols;
720 void setSubsectionsViaSymbols(bool Value) {
721 SubsectionsViaSymbols = Value;
724 bool getRelaxAll() const { return RelaxAll; }
725 void setRelaxAll(bool Value) { RelaxAll = Value; }
727 /// @name Section List Access
730 const SectionDataListType &getSectionList() const { return Sections; }
731 SectionDataListType &getSectionList() { return Sections; }
733 iterator begin() { return Sections.begin(); }
734 const_iterator begin() const { return Sections.begin(); }
736 iterator end() { return Sections.end(); }
737 const_iterator end() const { return Sections.end(); }
739 size_t size() const { return Sections.size(); }
742 /// @name Symbol List Access
745 const SymbolDataListType &getSymbolList() const { return Symbols; }
746 SymbolDataListType &getSymbolList() { return Symbols; }
748 symbol_iterator symbol_begin() { return Symbols.begin(); }
749 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
751 symbol_iterator symbol_end() { return Symbols.end(); }
752 const_symbol_iterator symbol_end() const { return Symbols.end(); }
754 size_t symbol_size() const { return Symbols.size(); }
757 /// @name Indirect Symbol List Access
760 // FIXME: This is a total hack, this should not be here. Once things are
761 // factored so that the streamer has direct access to the .o writer, it can
763 std::vector<IndirectSymbolData> &getIndirectSymbols() {
764 return IndirectSymbols;
767 indirect_symbol_iterator indirect_symbol_begin() {
768 return IndirectSymbols.begin();
770 const_indirect_symbol_iterator indirect_symbol_begin() const {
771 return IndirectSymbols.begin();
774 indirect_symbol_iterator indirect_symbol_end() {
775 return IndirectSymbols.end();
777 const_indirect_symbol_iterator indirect_symbol_end() const {
778 return IndirectSymbols.end();
781 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
784 /// @name Backend Data Access
787 MCSectionData &getSectionData(const MCSection &Section) const {
788 MCSectionData *Entry = SectionMap.lookup(&Section);
789 assert(Entry && "Missing section data!");
793 MCSectionData &getOrCreateSectionData(const MCSection &Section,
795 MCSectionData *&Entry = SectionMap[&Section];
797 if (Created) *Created = !Entry;
799 Entry = new MCSectionData(Section, this);
804 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
805 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
806 assert(Entry && "Missing symbol data!");
810 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
812 MCSymbolData *&Entry = SymbolMap[&Symbol];
814 if (Created) *Created = !Entry;
816 Entry = new MCSymbolData(Symbol, 0, 0, this);
826 } // end namespace llvm