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;
94 /// Ordinal - The global index of this fragment. This is the index across all
95 /// sections, not just the parent section.
101 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
104 // Only for sentinel.
106 virtual ~MCFragment();
108 FragmentType getKind() const { return Kind; }
110 MCSectionData *getParent() const { return Parent; }
111 void setParent(MCSectionData *Value) { Parent = Value; }
113 unsigned getOrdinal() const { return Ordinal; }
114 void setOrdinal(unsigned Value) { Ordinal = Value; }
116 static bool classof(const MCFragment *O) { return true; }
121 class MCDataFragment : public MCFragment {
122 SmallString<32> Contents;
124 /// Fixups - The list of fixups in this fragment.
125 std::vector<MCAsmFixup> Fixups;
128 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
129 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
132 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
137 SmallString<32> &getContents() { return Contents; }
138 const SmallString<32> &getContents() const { return Contents; }
141 /// @name Fixup Access
144 void addFixup(MCAsmFixup Fixup) {
145 // Enforce invariant that fixups are in offset order.
146 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
147 "Fixups must be added in order!");
148 Fixups.push_back(Fixup);
151 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
152 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
154 fixup_iterator fixup_begin() { return Fixups.begin(); }
155 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
157 fixup_iterator fixup_end() {return Fixups.end();}
158 const_fixup_iterator fixup_end() const {return Fixups.end();}
160 size_t fixup_size() const { return Fixups.size(); }
164 static bool classof(const MCFragment *F) {
165 return F->getKind() == MCFragment::FT_Data;
167 static bool classof(const MCDataFragment *) { return true; }
172 // FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
173 // it is almost entirely a duplicate of MCDataFragment. If we decide to stick
174 // with this approach (as opposed to making MCInstFragment a very light weight
175 // object with just the MCInst and a code size, then we should just change
176 // MCDataFragment to have an optional MCInst at its end.
177 class MCInstFragment : public MCFragment {
178 /// Inst - The instruction this is a fragment for.
181 /// InstSize - The size of the currently encoded instruction.
184 /// Fixups - The list of fixups in this fragment.
185 SmallVector<MCAsmFixup, 1> Fixups;
188 typedef SmallVectorImpl<MCAsmFixup>::const_iterator const_fixup_iterator;
189 typedef SmallVectorImpl<MCAsmFixup>::iterator fixup_iterator;
192 MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
193 : MCFragment(FT_Inst, SD), Inst(_Inst) {
199 SmallVectorImpl<char> &getCode() { return Code; }
200 const SmallVectorImpl<char> &getCode() const { return Code; }
202 unsigned getInstSize() const { return Code.size(); }
204 MCInst &getInst() { return Inst; }
205 const MCInst &getInst() const { return Inst; }
207 void setInst(MCInst Value) { Inst = Value; }
210 /// @name Fixup Access
213 SmallVectorImpl<MCAsmFixup> &getFixups() { return Fixups; }
214 const SmallVectorImpl<MCAsmFixup> &getFixups() const { return Fixups; }
216 fixup_iterator fixup_begin() { return Fixups.begin(); }
217 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
219 fixup_iterator fixup_end() {return Fixups.end();}
220 const_fixup_iterator fixup_end() const {return Fixups.end();}
222 size_t fixup_size() const { return Fixups.size(); }
226 static bool classof(const MCFragment *F) {
227 return F->getKind() == MCFragment::FT_Inst;
229 static bool classof(const MCInstFragment *) { return true; }
234 class MCAlignFragment : public MCFragment {
235 /// Alignment - The alignment to ensure, in bytes.
238 /// Value - Value to use for filling padding bytes.
241 /// ValueSize - The size of the integer (in bytes) of \arg Value.
244 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
245 /// cannot be satisfied in this width then this fragment is ignored.
246 unsigned MaxBytesToEmit;
248 /// EmitNops - true when aligning code and optimal nops to be used for
253 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
254 unsigned _MaxBytesToEmit, bool _EmitNops,
255 MCSectionData *SD = 0)
256 : MCFragment(FT_Align, SD), Alignment(_Alignment),
257 Value(_Value),ValueSize(_ValueSize),
258 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
263 unsigned getAlignment() const { return Alignment; }
265 int64_t getValue() const { return Value; }
267 unsigned getValueSize() const { return ValueSize; }
269 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
271 unsigned getEmitNops() const { return EmitNops; }
275 static bool classof(const MCFragment *F) {
276 return F->getKind() == MCFragment::FT_Align;
278 static bool classof(const MCAlignFragment *) { return true; }
283 class MCFillFragment : public MCFragment {
284 /// Value - Value to use for filling bytes.
287 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
290 /// Count - The number of copies of \arg Value to insert.
294 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
295 MCSectionData *SD = 0)
296 : MCFragment(FT_Fill, SD),
297 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
302 int64_t getValue() const { return Value; }
304 unsigned getValueSize() const { return ValueSize; }
306 uint64_t getCount() const { return Count; }
310 static bool classof(const MCFragment *F) {
311 return F->getKind() == MCFragment::FT_Fill;
313 static bool classof(const MCFillFragment *) { return true; }
318 class MCOrgFragment : public MCFragment {
319 /// Offset - The offset this fragment should start at.
320 const MCExpr *Offset;
322 /// Value - Value to use for filling bytes.
326 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
327 : MCFragment(FT_Org, SD),
328 Offset(&_Offset), Value(_Value) {}
333 const MCExpr &getOffset() const { return *Offset; }
335 uint8_t getValue() const { return Value; }
339 static bool classof(const MCFragment *F) {
340 return F->getKind() == MCFragment::FT_Org;
342 static bool classof(const MCOrgFragment *) { return true; }
347 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
348 /// but requires no physical space in the object file.
349 class MCZeroFillFragment : public MCFragment {
350 /// Size - The size of this fragment.
353 /// Alignment - The alignment for this fragment.
357 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
358 : MCFragment(FT_ZeroFill, SD),
359 Size(_Size), Alignment(_Alignment) {}
364 uint64_t getSize() const { return Size; }
366 unsigned getAlignment() const { return Alignment; }
370 static bool classof(const MCFragment *F) {
371 return F->getKind() == MCFragment::FT_ZeroFill;
373 static bool classof(const MCZeroFillFragment *) { return true; }
378 // FIXME: Should this be a separate class, or just merged into MCSection? Since
379 // we anticipate the fast path being through an MCAssembler, the only reason to
380 // keep it out is for API abstraction.
381 class MCSectionData : public ilist_node<MCSectionData> {
382 friend class MCAsmLayout;
384 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
385 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
388 typedef iplist<MCFragment> FragmentListType;
390 typedef FragmentListType::const_iterator const_iterator;
391 typedef FragmentListType::iterator iterator;
393 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
394 typedef FragmentListType::reverse_iterator reverse_iterator;
397 iplist<MCFragment> Fragments;
398 const MCSection *Section;
400 /// Ordinal - The section index in the assemblers section list.
403 /// Alignment - The maximum alignment seen in this section.
406 /// @name Assembler Backend Data
409 // FIXME: This could all be kept private to the assembler implementation.
411 /// Address - The computed address of this section. This is ~0 until
415 /// Size - The content size of this section. This is ~0 until initialized.
418 /// FileSize - The size of this section in the object file. This is ~0 until
422 /// HasInstructions - Whether this section has had instructions emitted into
424 unsigned HasInstructions : 1;
429 // Only for use as sentinel.
431 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
433 const MCSection &getSection() const { return *Section; }
435 unsigned getAlignment() const { return Alignment; }
436 void setAlignment(unsigned Value) { Alignment = Value; }
438 bool hasInstructions() const { return HasInstructions; }
439 void setHasInstructions(bool Value) { HasInstructions = Value; }
441 unsigned getOrdinal() const { return Ordinal; }
442 void setOrdinal(unsigned Value) { Ordinal = Value; }
444 /// @name Fragment Access
447 const FragmentListType &getFragmentList() const { return Fragments; }
448 FragmentListType &getFragmentList() { return Fragments; }
450 iterator begin() { return Fragments.begin(); }
451 const_iterator begin() const { return Fragments.begin(); }
453 iterator end() { return Fragments.end(); }
454 const_iterator end() const { return Fragments.end(); }
456 reverse_iterator rbegin() { return Fragments.rbegin(); }
457 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
459 reverse_iterator rend() { return Fragments.rend(); }
460 const_reverse_iterator rend() const { return Fragments.rend(); }
462 size_t size() const { return Fragments.size(); }
464 bool empty() const { return Fragments.empty(); }
471 // FIXME: Same concerns as with SectionData.
472 class MCSymbolData : public ilist_node<MCSymbolData> {
474 const MCSymbol *Symbol;
476 /// Fragment - The fragment this symbol's value is relative to, if any.
477 MCFragment *Fragment;
479 /// Offset - The offset to apply to the fragment address to form this symbol's
483 /// IsExternal - True if this symbol is visible outside this translation
485 unsigned IsExternal : 1;
487 /// IsPrivateExtern - True if this symbol is private extern.
488 unsigned IsPrivateExtern : 1;
490 /// CommonSize - The size of the symbol, if it is 'common', or 0.
492 // FIXME: Pack this in with other fields? We could put it in offset, since a
493 // common symbol can never get a definition.
496 /// CommonAlign - The alignment of the symbol, if it is 'common'.
498 // FIXME: Pack this in with other fields?
499 unsigned CommonAlign;
501 /// Flags - The Flags field is used by object file implementations to store
502 /// additional per symbol information which is not easily classified.
505 /// Index - Index field, for use by the object file implementation.
509 // Only for use as sentinel.
511 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
517 const MCSymbol &getSymbol() const { return *Symbol; }
519 MCFragment *getFragment() const { return Fragment; }
520 void setFragment(MCFragment *Value) { Fragment = Value; }
522 uint64_t getOffset() const { return Offset; }
523 void setOffset(uint64_t Value) { Offset = Value; }
526 /// @name Symbol Attributes
529 bool isExternal() const { return IsExternal; }
530 void setExternal(bool Value) { IsExternal = Value; }
532 bool isPrivateExtern() const { return IsPrivateExtern; }
533 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
535 /// isCommon - Is this a 'common' symbol.
536 bool isCommon() const { return CommonSize != 0; }
538 /// setCommon - Mark this symbol as being 'common'.
540 /// \param Size - The size of the symbol.
541 /// \param Align - The alignment of the symbol.
542 void setCommon(uint64_t Size, unsigned Align) {
547 /// getCommonSize - Return the size of a 'common' symbol.
548 uint64_t getCommonSize() const {
549 assert(isCommon() && "Not a 'common' symbol!");
553 /// getCommonAlignment - Return the alignment of a 'common' symbol.
554 unsigned getCommonAlignment() const {
555 assert(isCommon() && "Not a 'common' symbol!");
559 /// getFlags - Get the (implementation defined) symbol flags.
560 uint32_t getFlags() const { return Flags; }
562 /// setFlags - Set the (implementation defined) symbol flags.
563 void setFlags(uint32_t Value) { Flags = Value; }
565 /// getIndex - Get the (implementation defined) index.
566 uint64_t getIndex() const { return Index; }
568 /// setIndex - Set the (implementation defined) index.
569 void setIndex(uint64_t Value) { Index = Value; }
576 // FIXME: This really doesn't belong here. See comments below.
577 struct IndirectSymbolData {
579 MCSectionData *SectionData;
584 typedef iplist<MCSectionData> SectionDataListType;
585 typedef iplist<MCSymbolData> SymbolDataListType;
587 typedef SectionDataListType::const_iterator const_iterator;
588 typedef SectionDataListType::iterator iterator;
590 typedef SymbolDataListType::const_iterator const_symbol_iterator;
591 typedef SymbolDataListType::iterator symbol_iterator;
593 typedef std::vector<IndirectSymbolData>::const_iterator
594 const_indirect_symbol_iterator;
595 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
598 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
599 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
603 TargetAsmBackend &Backend;
605 MCCodeEmitter &Emitter;
609 iplist<MCSectionData> Sections;
611 iplist<MCSymbolData> Symbols;
613 /// The map of sections to their associated assembler backend data.
615 // FIXME: Avoid this indirection?
616 DenseMap<const MCSection*, MCSectionData*> SectionMap;
618 /// The map of symbols to their associated assembler backend data.
620 // FIXME: Avoid this indirection?
621 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
623 std::vector<IndirectSymbolData> IndirectSymbols;
625 unsigned SubsectionsViaSymbols : 1;
628 /// Evaluate a fixup to a relocatable expression and the value which should be
629 /// placed into the fixup.
631 /// \param Layout The layout to use for evaluation.
632 /// \param Fixup The fixup to evaluate.
633 /// \param DF The fragment the fixup is inside.
634 /// \param Target [out] On return, the relocatable expression the fixup
636 /// \param Value [out] On return, the value of the fixup as currently layed
638 /// \return Whether the fixup value was fully resolved. This is true if the
639 /// \arg Value result is fixed, otherwise the value may change due to
641 bool EvaluateFixup(const MCAsmLayout &Layout,
642 const MCAsmFixup &Fixup, const MCFragment *DF,
643 MCValue &Target, uint64_t &Value) const;
645 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
646 /// (increased in size, in order to hold its value correctly).
647 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
648 const MCAsmLayout &Layout) const;
650 /// Check whether the given fragment needs relaxation.
651 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
652 const MCAsmLayout &Layout) const;
654 /// LayoutSection - Assign offsets and sizes to the fragments in the section
655 /// \arg SD, and update the section size. The section file offset should
656 /// already have been computed.
657 void LayoutSection(MCSectionData &SD, MCAsmLayout &Layout);
659 /// LayoutOnce - Perform one layout iteration and return true if any offsets
661 bool LayoutOnce(MCAsmLayout &Layout);
663 /// FinishLayout - Finalize a layout, including fragment lowering.
664 void FinishLayout(MCAsmLayout &Layout);
667 /// Find the symbol which defines the atom containing given address, inside
668 /// the given section, or null if there is no such symbol.
670 // FIXME-PERF: Eliminate this, it is very slow.
671 const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
672 const MCSectionData *Section,
673 uint64_t Address) const;
675 /// Find the symbol which defines the atom containing the given symbol, or
676 /// null if there is no such symbol.
678 // FIXME-PERF: Eliminate this, it is very slow.
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 /// @name Section List Access
727 const SectionDataListType &getSectionList() const { return Sections; }
728 SectionDataListType &getSectionList() { return Sections; }
730 iterator begin() { return Sections.begin(); }
731 const_iterator begin() const { return Sections.begin(); }
733 iterator end() { return Sections.end(); }
734 const_iterator end() const { return Sections.end(); }
736 size_t size() const { return Sections.size(); }
739 /// @name Symbol List Access
742 const SymbolDataListType &getSymbolList() const { return Symbols; }
743 SymbolDataListType &getSymbolList() { return Symbols; }
745 symbol_iterator symbol_begin() { return Symbols.begin(); }
746 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
748 symbol_iterator symbol_end() { return Symbols.end(); }
749 const_symbol_iterator symbol_end() const { return Symbols.end(); }
751 size_t symbol_size() const { return Symbols.size(); }
754 /// @name Indirect Symbol List Access
757 // FIXME: This is a total hack, this should not be here. Once things are
758 // factored so that the streamer has direct access to the .o writer, it can
760 std::vector<IndirectSymbolData> &getIndirectSymbols() {
761 return IndirectSymbols;
764 indirect_symbol_iterator indirect_symbol_begin() {
765 return IndirectSymbols.begin();
767 const_indirect_symbol_iterator indirect_symbol_begin() const {
768 return IndirectSymbols.begin();
771 indirect_symbol_iterator indirect_symbol_end() {
772 return IndirectSymbols.end();
774 const_indirect_symbol_iterator indirect_symbol_end() const {
775 return IndirectSymbols.end();
778 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
781 /// @name Backend Data Access
784 MCSectionData &getSectionData(const MCSection &Section) const {
785 MCSectionData *Entry = SectionMap.lookup(&Section);
786 assert(Entry && "Missing section data!");
790 MCSectionData &getOrCreateSectionData(const MCSection &Section,
792 MCSectionData *&Entry = SectionMap[&Section];
794 if (Created) *Created = !Entry;
796 Entry = new MCSectionData(Section, this);
801 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
802 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
803 assert(Entry && "Missing symbol data!");
807 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
809 MCSymbolData *&Entry = SymbolMap[&Symbol];
811 if (Created) *Created = !Entry;
813 Entry = new MCSymbolData(Symbol, 0, 0, this);
823 } // end namespace llvm