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;
583 friend class MCAsmLayout;
586 typedef iplist<MCSectionData> SectionDataListType;
587 typedef iplist<MCSymbolData> SymbolDataListType;
589 typedef SectionDataListType::const_iterator const_iterator;
590 typedef SectionDataListType::iterator iterator;
592 typedef SymbolDataListType::const_iterator const_symbol_iterator;
593 typedef SymbolDataListType::iterator symbol_iterator;
595 typedef std::vector<IndirectSymbolData>::const_iterator
596 const_indirect_symbol_iterator;
597 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
600 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
601 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
605 TargetAsmBackend &Backend;
607 MCCodeEmitter &Emitter;
611 iplist<MCSectionData> Sections;
613 iplist<MCSymbolData> Symbols;
615 /// The map of sections to their associated assembler backend data.
617 // FIXME: Avoid this indirection?
618 DenseMap<const MCSection*, MCSectionData*> SectionMap;
620 /// The map of symbols to their associated assembler backend data.
622 // FIXME: Avoid this indirection?
623 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
625 std::vector<IndirectSymbolData> IndirectSymbols;
627 unsigned RelaxAll : 1;
628 unsigned SubsectionsViaSymbols : 1;
631 /// Evaluate a fixup to a relocatable expression and the value which should be
632 /// placed into the fixup.
634 /// \param Layout The layout to use for evaluation.
635 /// \param Fixup The fixup to evaluate.
636 /// \param DF The fragment the fixup is inside.
637 /// \param Target [out] On return, the relocatable expression the fixup
639 /// \param Value [out] On return, the value of the fixup as currently layed
641 /// \return Whether the fixup value was fully resolved. This is true if the
642 /// \arg Value result is fixed, otherwise the value may change due to
644 bool EvaluateFixup(const MCAsmLayout &Layout,
645 const MCAsmFixup &Fixup, const MCFragment *DF,
646 MCValue &Target, uint64_t &Value) const;
648 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
649 /// (increased in size, in order to hold its value correctly).
650 bool FixupNeedsRelaxation(const MCAsmFixup &Fixup, const MCFragment *DF,
651 const MCAsmLayout &Layout) const;
653 /// Check whether the given fragment needs relaxation.
654 bool FragmentNeedsRelaxation(const MCInstFragment *IF,
655 const MCAsmLayout &Layout) const;
657 /// LayoutSection - Assign the section the given \arg StartAddress, and then
658 /// assign offsets and sizes to the fragments in the section \arg SD, and
659 /// update the section size.
661 /// \return The address at the end of the section, for use in laying out the
662 /// succeeding section.
663 uint64_t LayoutSection(MCSectionData &SD, MCAsmLayout &Layout,
664 uint64_t StartAddress);
666 /// LayoutOnce - Perform one layout iteration and return true if any offsets
668 bool LayoutOnce(MCAsmLayout &Layout);
670 /// FinishLayout - Finalize a layout, including fragment lowering.
671 void FinishLayout(MCAsmLayout &Layout);
674 /// Find the symbol which defines the atom containing given address, inside
675 /// the given section, or null if there is no such symbol.
677 // FIXME-PERF: Eliminate this, it is very slow.
678 const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
679 const MCSectionData *Section,
680 uint64_t Address) const;
682 /// Find the symbol which defines the atom containing the given symbol, or
683 /// null if there is no such symbol.
685 // FIXME-PERF: Eliminate this, it is very slow.
686 const MCSymbolData *getAtom(const MCAsmLayout &Layout,
687 const MCSymbolData *Symbol) const;
689 /// Check whether a particular symbol is visible to the linker and is required
690 /// in the symbol table, or whether it can be discarded by the assembler. This
691 /// also effects whether the assembler treats the label as potentially
692 /// defining a separate atom.
693 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
695 /// Emit the section contents using the given object writer.
697 // FIXME: Should MCAssembler always have a reference to the object writer?
698 void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
699 MCObjectWriter *OW) const;
702 /// Construct a new assembler instance.
704 /// \arg OS - The stream to output to.
706 // FIXME: How are we going to parameterize this? Two obvious options are stay
707 // concrete and require clients to pass in a target like object. The other
708 // option is to make this abstract, and have targets provide concrete
709 // implementations as we do with AsmParser.
710 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
711 MCCodeEmitter &_Emitter, raw_ostream &OS);
714 MCContext &getContext() const { return Context; }
716 TargetAsmBackend &getBackend() const { return Backend; }
718 MCCodeEmitter &getEmitter() const { return Emitter; }
720 /// Finish - Do final processing and write the object to the output stream.
723 // FIXME: This does not belong here.
724 bool getSubsectionsViaSymbols() const {
725 return SubsectionsViaSymbols;
727 void setSubsectionsViaSymbols(bool Value) {
728 SubsectionsViaSymbols = Value;
731 bool getRelaxAll() const { return RelaxAll; }
732 void setRelaxAll(bool Value) { RelaxAll = Value; }
734 /// @name Section List Access
737 const SectionDataListType &getSectionList() const { return Sections; }
738 SectionDataListType &getSectionList() { return Sections; }
740 iterator begin() { return Sections.begin(); }
741 const_iterator begin() const { return Sections.begin(); }
743 iterator end() { return Sections.end(); }
744 const_iterator end() const { return Sections.end(); }
746 size_t size() const { return Sections.size(); }
749 /// @name Symbol List Access
752 const SymbolDataListType &getSymbolList() const { return Symbols; }
753 SymbolDataListType &getSymbolList() { return Symbols; }
755 symbol_iterator symbol_begin() { return Symbols.begin(); }
756 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
758 symbol_iterator symbol_end() { return Symbols.end(); }
759 const_symbol_iterator symbol_end() const { return Symbols.end(); }
761 size_t symbol_size() const { return Symbols.size(); }
764 /// @name Indirect Symbol List Access
767 // FIXME: This is a total hack, this should not be here. Once things are
768 // factored so that the streamer has direct access to the .o writer, it can
770 std::vector<IndirectSymbolData> &getIndirectSymbols() {
771 return IndirectSymbols;
774 indirect_symbol_iterator indirect_symbol_begin() {
775 return IndirectSymbols.begin();
777 const_indirect_symbol_iterator indirect_symbol_begin() const {
778 return IndirectSymbols.begin();
781 indirect_symbol_iterator indirect_symbol_end() {
782 return IndirectSymbols.end();
784 const_indirect_symbol_iterator indirect_symbol_end() const {
785 return IndirectSymbols.end();
788 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
791 /// @name Backend Data Access
794 MCSectionData &getSectionData(const MCSection &Section) const {
795 MCSectionData *Entry = SectionMap.lookup(&Section);
796 assert(Entry && "Missing section data!");
800 MCSectionData &getOrCreateSectionData(const MCSection &Section,
802 MCSectionData *&Entry = SectionMap[&Section];
804 if (Created) *Created = !Entry;
806 Entry = new MCSectionData(Section, this);
811 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
812 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
813 assert(Entry && "Missing symbol data!");
817 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
819 MCSymbolData *&Entry = SymbolMap[&Symbol];
821 if (Created) *Created = !Entry;
823 Entry = new MCSymbolData(Symbol, 0, 0, this);
833 } // end namespace llvm