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 class MCFragment : public ilist_node<MCFragment> {
40 friend class MCAsmLayout;
42 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
43 void operator=(const MCFragment&); // DO NOT IMPLEMENT
57 /// Parent - The data for the section this fragment is in.
58 MCSectionData *Parent;
60 /// Atom - The atom this fragment is in, as represented by it's defining
61 /// symbol. Atom's are only used by backends which set
62 /// \see MCAsmBackend::hasReliableSymbolDifference().
65 /// @name Assembler Backend Data
68 // FIXME: This could all be kept private to the assembler implementation.
70 /// Offset - The offset of this fragment in its section. This is ~0 until
74 /// EffectiveSize - The compute size of this section. This is ~0 until
76 uint64_t EffectiveSize;
78 /// LayoutOrder - The global layout order of this fragment. This is the index
79 /// across all fragments in the file, not just within the section.
85 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
91 FragmentType getKind() const { return Kind; }
93 MCSectionData *getParent() const { return Parent; }
94 void setParent(MCSectionData *Value) { Parent = Value; }
96 MCSymbolData *getAtom() const { return Atom; }
97 void setAtom(MCSymbolData *Value) { Atom = Value; }
99 unsigned getLayoutOrder() const { return LayoutOrder; }
100 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
102 static bool classof(const MCFragment *O) { return true; }
107 class MCDataFragment : public MCFragment {
108 SmallString<32> Contents;
110 /// Fixups - The list of fixups in this fragment.
111 std::vector<MCFixup> Fixups;
114 typedef std::vector<MCFixup>::const_iterator const_fixup_iterator;
115 typedef std::vector<MCFixup>::iterator fixup_iterator;
118 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
123 SmallString<32> &getContents() { return Contents; }
124 const SmallString<32> &getContents() const { return Contents; }
127 /// @name Fixup Access
130 void addFixup(MCFixup Fixup) {
131 // Enforce invariant that fixups are in offset order.
132 assert((Fixups.empty() || Fixup.getOffset() > Fixups.back().getOffset()) &&
133 "Fixups must be added in order!");
134 Fixups.push_back(Fixup);
137 std::vector<MCFixup> &getFixups() { return Fixups; }
138 const std::vector<MCFixup> &getFixups() const { return Fixups; }
140 fixup_iterator fixup_begin() { return Fixups.begin(); }
141 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
143 fixup_iterator fixup_end() {return Fixups.end();}
144 const_fixup_iterator fixup_end() const {return Fixups.end();}
146 size_t fixup_size() const { return Fixups.size(); }
150 static bool classof(const MCFragment *F) {
151 return F->getKind() == MCFragment::FT_Data;
153 static bool classof(const MCDataFragment *) { return true; }
156 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
157 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
158 // with this approach (as opposed to making MCInstFragment a very light weight
159 // object with just the MCInst and a code size, then we should just change
160 // MCDataFragment to have an optional MCInst at its end.
161 class MCInstFragment : public MCFragment {
162 /// Inst - The instruction this is a fragment for.
165 /// InstSize - The size of the currently encoded instruction.
168 /// Fixups - The list of fixups in this fragment.
169 SmallVector<MCFixup, 1> Fixups;
172 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
173 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
176 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
177 : MCFragment(FT_Inst, SD), Inst(_Inst) {
183 SmallVectorImpl<char> &getCode() { return Code; }
184 const SmallVectorImpl<char> &getCode() const { return Code; }
186 unsigned getInstSize() const { return Code.size(); }
188 MCInst &getInst() { return Inst; }
189 const MCInst &getInst() const { return Inst; }
191 void setInst(MCInst Value) { Inst = Value; }
194 /// @name Fixup Access
197 SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
198 const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
200 fixup_iterator fixup_begin() { return Fixups.begin(); }
201 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
203 fixup_iterator fixup_end() {return Fixups.end();}
204 const_fixup_iterator fixup_end() const {return Fixups.end();}
206 size_t fixup_size() const { return Fixups.size(); }
210 static bool classof(const MCFragment *F) {
211 return F->getKind() == MCFragment::FT_Inst;
213 static bool classof(const MCInstFragment *) { return true; }
216 class MCAlignFragment : public MCFragment {
217 /// Alignment - The alignment to ensure, in bytes.
220 /// Value - Value to use for filling padding bytes.
223 /// ValueSize - The size of the integer (in bytes) of \arg Value.
226 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
227 /// cannot be satisfied in this width then this fragment is ignored.
228 unsigned MaxBytesToEmit;
230 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
231 /// of using the provided value. The exact interpretation of this flag is
232 /// target dependent.
235 /// OnlyAlignAddress - Flag to indicate that this align is only used to adjust
236 /// the address space size of a section and that it should not be included as
237 /// part of the section size. This flag can only be used on the last fragment
239 bool OnlyAlignAddress : 1;
242 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
243 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
244 : MCFragment(FT_Align, SD), Alignment(_Alignment),
245 Value(_Value),ValueSize(_ValueSize),
246 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false),
247 OnlyAlignAddress(false) {}
252 unsigned getAlignment() const { return Alignment; }
254 int64_t getValue() const { return Value; }
256 unsigned getValueSize() const { return ValueSize; }
258 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
260 bool hasEmitNops() const { return EmitNops; }
261 void setEmitNops(bool Value) { EmitNops = Value; }
263 bool hasOnlyAlignAddress() const { return OnlyAlignAddress; }
264 void setOnlyAlignAddress(bool Value) { OnlyAlignAddress = Value; }
268 static bool classof(const MCFragment *F) {
269 return F->getKind() == MCFragment::FT_Align;
271 static bool classof(const MCAlignFragment *) { return true; }
274 class MCFillFragment : public MCFragment {
275 /// Value - Value to use for filling bytes.
278 /// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
279 /// this is a virtual fill fragment.
282 /// Size - The number of bytes to insert.
286 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
287 MCSectionData *SD = 0)
288 : MCFragment(FT_Fill, SD),
289 Value(_Value), ValueSize(_ValueSize), Size(_Size) {
290 assert((!ValueSize || (Size % ValueSize) == 0) &&
291 "Fill size must be a multiple of the value size!");
297 int64_t getValue() const { return Value; }
299 unsigned getValueSize() const { return ValueSize; }
301 uint64_t getSize() const { return Size; }
305 static bool classof(const MCFragment *F) {
306 return F->getKind() == MCFragment::FT_Fill;
308 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; }
338 // FIXME: Should this be a separate class, or just merged into MCSection? Since
339 // we anticipate the fast path being through an MCAssembler, the only reason to
340 // keep it out is for API abstraction.
341 class MCSectionData : public ilist_node<MCSectionData> {
342 friend class MCAsmLayout;
344 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
345 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
348 typedef iplist<MCFragment> FragmentListType;
350 typedef FragmentListType::const_iterator const_iterator;
351 typedef FragmentListType::iterator iterator;
353 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
354 typedef FragmentListType::reverse_iterator reverse_iterator;
357 FragmentListType Fragments;
358 const MCSection *Section;
360 /// Ordinal - The section index in the assemblers section list.
363 /// LayoutOrder - The index of this section in the layout order.
364 unsigned LayoutOrder;
366 /// Alignment - The maximum alignment seen in this section.
369 /// @name Assembler Backend Data
372 // FIXME: This could all be kept private to the assembler implementation.
374 /// Address - The computed address of this section. This is ~0 until
378 /// HasInstructions - Whether this section has had instructions emitted into
380 unsigned HasInstructions : 1;
385 // Only for use as sentinel.
387 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
389 const MCSection &getSection() const { return *Section; }
391 unsigned getAlignment() const { return Alignment; }
392 void setAlignment(unsigned Value) { Alignment = Value; }
394 bool hasInstructions() const { return HasInstructions; }
395 void setHasInstructions(bool Value) { HasInstructions = Value; }
397 unsigned getOrdinal() const { return Ordinal; }
398 void setOrdinal(unsigned Value) { Ordinal = Value; }
400 unsigned getLayoutOrder() const { return LayoutOrder; }
401 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
403 /// @name Fragment Access
406 const FragmentListType &getFragmentList() const { return Fragments; }
407 FragmentListType &getFragmentList() { return Fragments; }
409 iterator begin() { return Fragments.begin(); }
410 const_iterator begin() const { return Fragments.begin(); }
412 iterator end() { return Fragments.end(); }
413 const_iterator end() const { return Fragments.end(); }
415 reverse_iterator rbegin() { return Fragments.rbegin(); }
416 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
418 reverse_iterator rend() { return Fragments.rend(); }
419 const_reverse_iterator rend() const { return Fragments.rend(); }
421 size_t size() const { return Fragments.size(); }
423 bool empty() const { return Fragments.empty(); }
430 // FIXME: Same concerns as with SectionData.
431 class MCSymbolData : public ilist_node<MCSymbolData> {
433 const MCSymbol *Symbol;
435 /// Fragment - The fragment this symbol's value is relative to, if any.
436 MCFragment *Fragment;
438 /// Offset - The offset to apply to the fragment address to form this symbol's
442 /// IsExternal - True if this symbol is visible outside this translation
444 unsigned IsExternal : 1;
446 /// IsPrivateExtern - True if this symbol is private extern.
447 unsigned IsPrivateExtern : 1;
449 /// CommonSize - The size of the symbol, if it is 'common', or 0.
451 // FIXME: Pack this in with other fields? We could put it in offset, since a
452 // common symbol can never get a definition.
455 /// CommonAlign - The alignment of the symbol, if it is 'common'.
457 // FIXME: Pack this in with other fields?
458 unsigned CommonAlign;
460 /// Flags - The Flags field is used by object file implementations to store
461 /// additional per symbol information which is not easily classified.
464 /// Index - Index field, for use by the object file implementation.
468 // Only for use as sentinel.
470 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
476 const MCSymbol &getSymbol() const { return *Symbol; }
478 MCFragment *getFragment() const { return Fragment; }
479 void setFragment(MCFragment *Value) { Fragment = Value; }
481 uint64_t getOffset() const { return Offset; }
482 void setOffset(uint64_t Value) { Offset = Value; }
485 /// @name Symbol Attributes
488 bool isExternal() const { return IsExternal; }
489 void setExternal(bool Value) { IsExternal = Value; }
491 bool isPrivateExtern() const { return IsPrivateExtern; }
492 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
494 /// isCommon - Is this a 'common' symbol.
495 bool isCommon() const { return CommonSize != 0; }
497 /// setCommon - Mark this symbol as being 'common'.
499 /// \param Size - The size of the symbol.
500 /// \param Align - The alignment of the symbol.
501 void setCommon(uint64_t Size, unsigned Align) {
506 /// getCommonSize - Return the size of a 'common' symbol.
507 uint64_t getCommonSize() const {
508 assert(isCommon() && "Not a 'common' symbol!");
512 /// getCommonAlignment - Return the alignment of a 'common' symbol.
513 unsigned getCommonAlignment() const {
514 assert(isCommon() && "Not a 'common' symbol!");
518 /// getFlags - Get the (implementation defined) symbol flags.
519 uint32_t getFlags() const { return Flags; }
521 /// setFlags - Set the (implementation defined) symbol flags.
522 void setFlags(uint32_t Value) { Flags = Value; }
524 /// modifyFlags - Modify the flags via a mask
525 void modifyFlags(uint32_t Value, uint32_t Mask) {
526 Flags = (Flags & ~Mask) | Value;
529 /// getIndex - Get the (implementation defined) index.
530 uint64_t getIndex() const { return Index; }
532 /// setIndex - Set the (implementation defined) index.
533 void setIndex(uint64_t Value) { Index = Value; }
540 // FIXME: This really doesn't belong here. See comments below.
541 struct IndirectSymbolData {
543 MCSectionData *SectionData;
547 friend class MCAsmLayout;
550 typedef iplist<MCSectionData> SectionDataListType;
551 typedef iplist<MCSymbolData> SymbolDataListType;
553 typedef SectionDataListType::const_iterator const_iterator;
554 typedef SectionDataListType::iterator iterator;
556 typedef SymbolDataListType::const_iterator const_symbol_iterator;
557 typedef SymbolDataListType::iterator symbol_iterator;
559 typedef std::vector<IndirectSymbolData>::const_iterator
560 const_indirect_symbol_iterator;
561 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
564 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
565 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
569 TargetAsmBackend &Backend;
571 MCCodeEmitter &Emitter;
575 iplist<MCSectionData> Sections;
577 iplist<MCSymbolData> Symbols;
579 /// The map of sections to their associated assembler backend data.
581 // FIXME: Avoid this indirection?
582 DenseMap<const MCSection*, MCSectionData*> SectionMap;
584 /// The map of symbols to their associated assembler backend data.
586 // FIXME: Avoid this indirection?
587 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
589 std::vector<IndirectSymbolData> IndirectSymbols;
591 unsigned RelaxAll : 1;
592 unsigned SubsectionsViaSymbols : 1;
595 /// Evaluate a fixup to a relocatable expression and the value which should be
596 /// placed into the fixup.
598 /// \param Layout The layout to use for evaluation.
599 /// \param Fixup The fixup to evaluate.
600 /// \param DF The fragment the fixup is inside.
601 /// \param Target [out] On return, the relocatable expression the fixup
603 /// \param Value [out] On return, the value of the fixup as currently layed
605 /// \return Whether the fixup value was fully resolved. This is true if the
606 /// \arg Value result is fixed, otherwise the value may change due to
608 bool EvaluateFixup(const MCAsmLayout &Layout,
609 const MCFixup &Fixup, const MCFragment *DF,
610 MCValue &Target, uint64_t &Value) const;
612 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
613 /// (increased in size, in order to hold its value correctly).
614 bool FixupNeedsRelaxation(const MCFixup &Fixup, const MCFragment *DF,
615 const MCAsmLayout &Layout) const;
617 /// Check whether the given fragment needs relaxation.
618 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
619 const MCAsmLayout &Layout) const;
621 /// Compute the effective fragment size assuming it is layed out at the given
622 /// \arg SectionAddress and \arg FragmentOffset.
623 uint64_t ComputeFragmentSize(MCAsmLayout &Layout, const MCFragment &F,
624 uint64_t SectionAddress,
625 uint64_t FragmentOffset) const;
627 /// LayoutOnce - Perform one layout iteration and return true if any offsets
629 bool LayoutOnce(MCAsmLayout &Layout);
631 /// FinishLayout - Finalize a layout, including fragment lowering.
632 void FinishLayout(MCAsmLayout &Layout);
635 /// Find the symbol which defines the atom containing the given symbol, or
636 /// null if there is no such symbol.
637 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
638 const MCSymbolData *Symbol) const;
640 /// Check whether a particular symbol is visible to the linker and is required
641 /// in the symbol table, or whether it can be discarded by the assembler. This
642 /// also effects whether the assembler treats the label as potentially
643 /// defining a separate atom.
644 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
646 /// Emit the section contents using the given object writer.
648 // FIXME: Should MCAssembler always have a reference to the object writer?
649 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
650 MCObjectWriter *OW) const;
653 /// Construct a new assembler instance.
655 /// \arg OS - The stream to output to.
657 // FIXME: How are we going to parameterize this? Two obvious options are stay
658 // concrete and require clients to pass in a target like object. The other
659 // option is to make this abstract, and have targets provide concrete
660 // implementations as we do with AsmParser.
661 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
662 MCCodeEmitter &_Emitter, raw_ostream &OS);
665 MCContext &getContext() const { return Context; }
667 TargetAsmBackend &getBackend() const { return Backend; }
669 MCCodeEmitter &getEmitter() const { return Emitter; }
671 /// Finish - Do final processing and write the object to the output stream.
674 // FIXME: This does not belong here.
675 bool getSubsectionsViaSymbols() const {
676 return SubsectionsViaSymbols;
678 void setSubsectionsViaSymbols(bool Value) {
679 SubsectionsViaSymbols = Value;
682 bool getRelaxAll() const { return RelaxAll; }
683 void setRelaxAll(bool Value) { RelaxAll = Value; }
685 /// @name Section List Access
688 const SectionDataListType &getSectionList() const { return Sections; }
689 SectionDataListType &getSectionList() { return Sections; }
691 iterator begin() { return Sections.begin(); }
692 const_iterator begin() const { return Sections.begin(); }
694 iterator end() { return Sections.end(); }
695 const_iterator end() const { return Sections.end(); }
697 size_t size() const { return Sections.size(); }
700 /// @name Symbol List Access
703 const SymbolDataListType &getSymbolList() const { return Symbols; }
704 SymbolDataListType &getSymbolList() { return Symbols; }
706 symbol_iterator symbol_begin() { return Symbols.begin(); }
707 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
709 symbol_iterator symbol_end() { return Symbols.end(); }
710 const_symbol_iterator symbol_end() const { return Symbols.end(); }
712 size_t symbol_size() const { return Symbols.size(); }
715 /// @name Indirect Symbol List Access
718 // FIXME: This is a total hack, this should not be here. Once things are
719 // factored so that the streamer has direct access to the .o writer, it can
721 std::vector<IndirectSymbolData> &getIndirectSymbols() {
722 return IndirectSymbols;
725 indirect_symbol_iterator indirect_symbol_begin() {
726 return IndirectSymbols.begin();
728 const_indirect_symbol_iterator indirect_symbol_begin() const {
729 return IndirectSymbols.begin();
732 indirect_symbol_iterator indirect_symbol_end() {
733 return IndirectSymbols.end();
735 const_indirect_symbol_iterator indirect_symbol_end() const {
736 return IndirectSymbols.end();
739 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
742 /// @name Backend Data Access
745 MCSectionData &getSectionData(const MCSection &Section) const {
746 MCSectionData *Entry = SectionMap.lookup(&Section);
747 assert(Entry && "Missing section data!");
751 MCSectionData &getOrCreateSectionData(const MCSection &Section,
753 MCSectionData *&Entry = SectionMap[&Section];
755 if (Created) *Created = !Entry;
757 Entry = new MCSectionData(Section, this);
762 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
763 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
764 assert(Entry && "Missing symbol data!");
768 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
770 MCSymbolData *&Entry = SymbolMap[&Symbol];
772 if (Created) *Created = !Entry;
774 Entry = new MCSymbolData(Symbol, 0, 0, this);
784 } // end namespace llvm