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.
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) {}
58 MCFixupKind getKind() const { return MCFixupKind(Kind); }
60 uint64_t getOffset() const { return Offset; }
61 void setOffset(uint64_t Value) { Offset = Value; }
63 const MCExpr *getValue() const { return Value; }
66 class MCFragment : public ilist_node<MCFragment> {
67 friend class MCAsmLayout;
69 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
70 void operator=(const MCFragment&); // DO NOT IMPLEMENT
84 /// Parent - The data for the section this fragment is in.
85 MCSectionData *Parent;
87 /// Atom - The atom this fragment is in, as represented by it's defining
88 /// symbol. Atom's are only used by backends which set
89 /// \see MCAsmBackend::hasReliableSymbolDifference().
92 /// @name Assembler Backend Data
95 // FIXME: This could all be kept private to the assembler implementation.
97 /// Offset - The offset of this fragment in its section. This is ~0 until
101 /// EffectiveSize - The compute size of this section. This is ~0 until
103 uint64_t EffectiveSize;
105 /// LayoutOrder - The global layout order of this fragment. This is the index
106 /// across all fragments in the file, not just within the section.
107 unsigned LayoutOrder;
112 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
115 // Only for sentinel.
118 FragmentType getKind() const { return Kind; }
120 MCSectionData *getParent() const { return Parent; }
121 void setParent(MCSectionData *Value) { Parent = Value; }
123 MCSymbolData *getAtom() const { return Atom; }
124 void setAtom(MCSymbolData *Value) { Atom = Value; }
126 unsigned getLayoutOrder() const { return LayoutOrder; }
127 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
129 static bool classof(const MCFragment *O) { return true; }
134 class MCDataFragment : public MCFragment {
135 SmallString<32> Contents;
137 /// Fixups - The list of fixups in this fragment.
138 std::vector<MCAsmFixup> Fixups;
141 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
142 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
145 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
150 SmallString<32> &getContents() { return Contents; }
151 const SmallString<32> &getContents() const { return Contents; }
154 /// @name Fixup Access
157 void addFixup(MCAsmFixup Fixup) {
158 // Enforce invariant that fixups are in offset order.
159 assert((Fixups.empty() || Fixup.getOffset() > Fixups.back().getOffset()) &&
160 "Fixups must be added in order!");
161 Fixups.push_back(Fixup);
164 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
165 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
167 fixup_iterator fixup_begin() { return Fixups.begin(); }
168 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
170 fixup_iterator fixup_end() {return Fixups.end();}
171 const_fixup_iterator fixup_end() const {return Fixups.end();}
173 size_t fixup_size() const { return Fixups.size(); }
177 static bool classof(const MCFragment *F) {
178 return F->getKind() == MCFragment::FT_Data;
180 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; }
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 - Flag to indicate that (optimal) NOPs should be emitted instead
258 /// of using the provided value. The exact interpretation of this flag is
259 /// target dependent.
262 /// OnlyAlignAddress - Flag to indicate that this align is only used to adjust
263 /// the address space size of a section and that it should not be included as
264 /// part of the section size. This flag can only be used on the last fragment
266 bool OnlyAlignAddress : 1;
269 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
270 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
271 : MCFragment(FT_Align, SD), Alignment(_Alignment),
272 Value(_Value),ValueSize(_ValueSize),
273 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false),
274 OnlyAlignAddress(false) {}
279 unsigned getAlignment() const { return Alignment; }
281 int64_t getValue() const { return Value; }
283 unsigned getValueSize() const { return ValueSize; }
285 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
287 bool hasEmitNops() const { return EmitNops; }
288 void setEmitNops(bool Value) { EmitNops = Value; }
290 bool hasOnlyAlignAddress() const { return OnlyAlignAddress; }
291 void setOnlyAlignAddress(bool Value) { OnlyAlignAddress = Value; }
295 static bool classof(const MCFragment *F) {
296 return F->getKind() == MCFragment::FT_Align;
298 static bool classof(const MCAlignFragment *) { return true; }
301 class MCFillFragment : public MCFragment {
302 /// Value - Value to use for filling bytes.
305 /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
306 /// this is a virtual fill fragment.
309 /// Size - The number of bytes to insert.
313 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
314 MCSectionData *SD = 0)
315 : MCFragment(FT_Fill, SD),
316 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
317 assert((!ValueSize || (Size % ValueSize) == 0) &&
318 "Fill size must be a multiple of the value size!");
324 int64_t getValue() const { return Value; }
326 unsigned getValueSize() const { return ValueSize; }
328 uint64_t getSize() const { return Size; }
332 static bool classof(const MCFragment *F) {
333 return F->getKind() == MCFragment::FT_Fill;
335 static bool classof(const MCFillFragment *) { return true; }
338 class MCOrgFragment : public MCFragment {
339 /// Offset - The offset this fragment should start at.
340 const MCExpr *Offset;
342 /// Value - Value to use for filling bytes.
346 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
347 : MCFragment(FT_Org, SD),
348 Offset(&_Offset), Value(_Value) {}
353 const MCExpr &getOffset() const { return *Offset; }
355 uint8_t getValue() const { return Value; }
359 static bool classof(const MCFragment *F) {
360 return F->getKind() == MCFragment::FT_Org;
362 static bool classof(const MCOrgFragment *) { return true; }
365 // FIXME: Should this be a separate class, or just merged into MCSection? Since
366 // we anticipate the fast path being through an MCAssembler, the only reason to
367 // keep it out is for API abstraction.
368 class MCSectionData : public ilist_node<MCSectionData> {
369 friend class MCAsmLayout;
371 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
372 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
375 typedef iplist<MCFragment> FragmentListType;
377 typedef FragmentListType::const_iterator const_iterator;
378 typedef FragmentListType::iterator iterator;
380 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
381 typedef FragmentListType::reverse_iterator reverse_iterator;
384 iplist<MCFragment> Fragments;
385 const MCSection *Section;
387 /// Ordinal - The section index in the assemblers section list.
390 /// LayoutOrder - The index of this section in the layout order.
391 unsigned LayoutOrder;
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 /// HasInstructions - Whether this section has had instructions emitted into
407 unsigned HasInstructions : 1;
412 // Only for use as sentinel.
414 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
416 const MCSection &getSection() const { return *Section; }
418 unsigned getAlignment() const { return Alignment; }
419 void setAlignment(unsigned Value) { Alignment = Value; }
421 bool hasInstructions() const { return HasInstructions; }
422 void setHasInstructions(bool Value) { HasInstructions = Value; }
424 unsigned getOrdinal() const { return Ordinal; }
425 void setOrdinal(unsigned Value) { Ordinal = Value; }
427 unsigned getLayoutOrder() const { return LayoutOrder; }
428 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
430 /// @name Fragment Access
433 const FragmentListType &getFragmentList() const { return Fragments; }
434 FragmentListType &getFragmentList() { return Fragments; }
436 iterator begin() { return Fragments.begin(); }
437 const_iterator begin() const { return Fragments.begin(); }
439 iterator end() { return Fragments.end(); }
440 const_iterator end() const { return Fragments.end(); }
442 reverse_iterator rbegin() { return Fragments.rbegin(); }
443 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
445 reverse_iterator rend() { return Fragments.rend(); }
446 const_reverse_iterator rend() const { return Fragments.rend(); }
448 size_t size() const { return Fragments.size(); }
450 bool empty() const { return Fragments.empty(); }
457 // FIXME: Same concerns as with SectionData.
458 class MCSymbolData : public ilist_node<MCSymbolData> {
460 const MCSymbol *Symbol;
462 /// Fragment - The fragment this symbol's value is relative to, if any.
463 MCFragment *Fragment;
465 /// Offset - The offset to apply to the fragment address to form this symbol's
469 /// IsExternal - True if this symbol is visible outside this translation
471 unsigned IsExternal : 1;
473 /// IsPrivateExtern - True if this symbol is private extern.
474 unsigned IsPrivateExtern : 1;
476 /// CommonSize - The size of the symbol, if it is 'common', or 0.
478 // FIXME: Pack this in with other fields? We could put it in offset, since a
479 // common symbol can never get a definition.
482 /// CommonAlign - The alignment of the symbol, if it is 'common'.
484 // FIXME: Pack this in with other fields?
485 unsigned CommonAlign;
487 /// Flags - The Flags field is used by object file implementations to store
488 /// additional per symbol information which is not easily classified.
491 /// Index - Index field, for use by the object file implementation.
495 // Only for use as sentinel.
497 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
503 const MCSymbol &getSymbol() const { return *Symbol; }
505 MCFragment *getFragment() const { return Fragment; }
506 void setFragment(MCFragment *Value) { Fragment = Value; }
508 uint64_t getOffset() const { return Offset; }
509 void setOffset(uint64_t Value) { Offset = Value; }
512 /// @name Symbol Attributes
515 bool isExternal() const { return IsExternal; }
516 void setExternal(bool Value) { IsExternal = Value; }
518 bool isPrivateExtern() const { return IsPrivateExtern; }
519 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
521 /// isCommon - Is this a 'common' symbol.
522 bool isCommon() const { return CommonSize != 0; }
524 /// setCommon - Mark this symbol as being 'common'.
526 /// \param Size - The size of the symbol.
527 /// \param Align - The alignment of the symbol.
528 void setCommon(uint64_t Size, unsigned Align) {
533 /// getCommonSize - Return the size of a 'common' symbol.
534 uint64_t getCommonSize() const {
535 assert(isCommon() && "Not a 'common' symbol!");
539 /// getCommonAlignment - Return the alignment of a 'common' symbol.
540 unsigned getCommonAlignment() const {
541 assert(isCommon() && "Not a 'common' symbol!");
545 /// getFlags - Get the (implementation defined) symbol flags.
546 uint32_t getFlags() const { return Flags; }
548 /// setFlags - Set the (implementation defined) symbol flags.
549 void setFlags(uint32_t Value) { Flags = Value; }
551 /// modifyFlags - Modify the flags via a mask
552 void modifyFlags(uint32_t Value, uint32_t Mask) {
553 Flags = (Flags & ~Mask) | Value;
556 /// getIndex - Get the (implementation defined) index.
557 uint64_t getIndex() const { return Index; }
559 /// setIndex - Set the (implementation defined) index.
560 void setIndex(uint64_t Value) { Index = Value; }
567 // FIXME: This really doesn't belong here. See comments below.
568 struct IndirectSymbolData {
570 MCSectionData *SectionData;
574 friend class MCAsmLayout;
577 typedef iplist<MCSectionData> SectionDataListType;
578 typedef iplist<MCSymbolData> SymbolDataListType;
580 typedef SectionDataListType::const_iterator const_iterator;
581 typedef SectionDataListType::iterator iterator;
583 typedef SymbolDataListType::const_iterator const_symbol_iterator;
584 typedef SymbolDataListType::iterator symbol_iterator;
586 typedef std::vector<IndirectSymbolData>::const_iterator
587 const_indirect_symbol_iterator;
588 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
591 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
592 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
596 TargetAsmBackend &Backend;
598 MCCodeEmitter &Emitter;
602 iplist<MCSectionData> Sections;
604 iplist<MCSymbolData> Symbols;
606 /// The map of sections to their associated assembler backend data.
608 // FIXME: Avoid this indirection?
609 DenseMap<const MCSection*, MCSectionData*> SectionMap;
611 /// The map of symbols to their associated assembler backend data.
613 // FIXME: Avoid this indirection?
614 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
616 std::vector<IndirectSymbolData> IndirectSymbols;
618 unsigned RelaxAll : 1;
619 unsigned SubsectionsViaSymbols : 1;
622 /// Evaluate a fixup to a relocatable expression and the value which should be
623 /// placed into the fixup.
625 /// \param Layout The layout to use for evaluation.
626 /// \param Fixup The fixup to evaluate.
627 /// \param DF The fragment the fixup is inside.
628 /// \param Target [out] On return, the relocatable expression the fixup
630 /// \param Value [out] On return, the value of the fixup as currently layed
632 /// \return Whether the fixup value was fully resolved. This is true if the
633 /// \arg Value result is fixed, otherwise the value may change due to
635 bool EvaluateFixup(const MCAsmLayout &Layout,
636 const MCAsmFixup &Fixup, const MCFragment *DF,
637 MCValue &Target, uint64_t &Value) const;
639 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
640 /// (increased in size, in order to hold its value correctly).
641 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
642 const MCAsmLayout &Layout) const;
644 /// Check whether the given fragment needs relaxation.
645 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
646 const MCAsmLayout &Layout) const;
648 /// Compute the effective fragment size assuming it is layed out at the given
649 /// \arg SectionAddress and \arg FragmentOffset.
650 uint64_t ComputeFragmentSize(MCAsmLayout &Layout, const MCFragment &F,
651 uint64_t SectionAddress,
652 uint64_t FragmentOffset) const;
654 /// LayoutOnce - Perform one layout iteration and return true if any offsets
656 bool LayoutOnce(MCAsmLayout &Layout);
658 /// FinishLayout - Finalize a layout, including fragment lowering.
659 void FinishLayout(MCAsmLayout &Layout);
662 /// Find the symbol which defines the atom containing the given symbol, or
663 /// null if there is no such symbol.
664 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
665 const MCSymbolData *Symbol) const;
667 /// Check whether a particular symbol is visible to the linker and is required
668 /// in the symbol table, or whether it can be discarded by the assembler. This
669 /// also effects whether the assembler treats the label as potentially
670 /// defining a separate atom.
671 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
673 /// Emit the section contents using the given object writer.
675 // FIXME: Should MCAssembler always have a reference to the object writer?
676 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
677 MCObjectWriter *OW) const;
680 /// Construct a new assembler instance.
682 /// \arg OS - The stream to output to.
684 // FIXME: How are we going to parameterize this? Two obvious options are stay
685 // concrete and require clients to pass in a target like object. The other
686 // option is to make this abstract, and have targets provide concrete
687 // implementations as we do with AsmParser.
688 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
689 MCCodeEmitter &_Emitter, raw_ostream &OS);
692 MCContext &getContext() const { return Context; }
694 TargetAsmBackend &getBackend() const { return Backend; }
696 MCCodeEmitter &getEmitter() const { return Emitter; }
698 /// Finish - Do final processing and write the object to the output stream.
701 // FIXME: This does not belong here.
702 bool getSubsectionsViaSymbols() const {
703 return SubsectionsViaSymbols;
705 void setSubsectionsViaSymbols(bool Value) {
706 SubsectionsViaSymbols = Value;
709 bool getRelaxAll() const { return RelaxAll; }
710 void setRelaxAll(bool Value) { RelaxAll = Value; }
712 /// @name Section List Access
715 const SectionDataListType &getSectionList() const { return Sections; }
716 SectionDataListType &getSectionList() { return Sections; }
718 iterator begin() { return Sections.begin(); }
719 const_iterator begin() const { return Sections.begin(); }
721 iterator end() { return Sections.end(); }
722 const_iterator end() const { return Sections.end(); }
724 size_t size() const { return Sections.size(); }
727 /// @name Symbol List Access
730 const SymbolDataListType &getSymbolList() const { return Symbols; }
731 SymbolDataListType &getSymbolList() { return Symbols; }
733 symbol_iterator symbol_begin() { return Symbols.begin(); }
734 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
736 symbol_iterator symbol_end() { return Symbols.end(); }
737 const_symbol_iterator symbol_end() const { return Symbols.end(); }
739 size_t symbol_size() const { return Symbols.size(); }
742 /// @name Indirect Symbol List Access
745 // FIXME: This is a total hack, this should not be here. Once things are
746 // factored so that the streamer has direct access to the .o writer, it can
748 std::vector<IndirectSymbolData> &getIndirectSymbols() {
749 return IndirectSymbols;
752 indirect_symbol_iterator indirect_symbol_begin() {
753 return IndirectSymbols.begin();
755 const_indirect_symbol_iterator indirect_symbol_begin() const {
756 return IndirectSymbols.begin();
759 indirect_symbol_iterator indirect_symbol_end() {
760 return IndirectSymbols.end();
762 const_indirect_symbol_iterator indirect_symbol_end() const {
763 return IndirectSymbols.end();
766 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
769 /// @name Backend Data Access
772 MCSectionData &getSectionData(const MCSection &Section) const {
773 MCSectionData *Entry = SectionMap.lookup(&Section);
774 assert(Entry && "Missing section data!");
778 MCSectionData &getOrCreateSectionData(const MCSection &Section,
780 MCSectionData *&Entry = SectionMap[&Section];
782 if (Created) *Created = !Entry;
784 Entry = new MCSectionData(Section, this);
789 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
790 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
791 assert(Entry && "Missing symbol data!");
795 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
797 MCSymbolData *&Entry = SymbolMap[&Symbol];
799 if (Created) *Created = !Entry;
801 Entry = new MCSymbolData(Symbol, 0, 0, this);
811 } // end namespace llvm