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 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
61 void operator=(const MCFragment&); // DO NOT IMPLEMENT
76 /// Parent - The data for the section this fragment is in.
77 MCSectionData *Parent;
79 /// @name Assembler Backend Data
82 // FIXME: This could all be kept private to the assembler implementation.
84 /// Offset - The offset of this fragment in its section. This is ~0 until
88 /// FileSize - The file size of this section. This is ~0 until initialized.
94 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
99 virtual ~MCFragment();
101 FragmentType getKind() const { return Kind; }
103 MCSectionData *getParent() const { return Parent; }
104 void setParent(MCSectionData *Value) { Parent = Value; }
106 /// @name Assembler Backend Support
109 // FIXME: This could all be kept private to the assembler implementation.
111 uint64_t getAddress() const;
113 uint64_t getFileSize() const {
114 assert(FileSize != ~UINT64_C(0) && "File size not set!");
117 void setFileSize(uint64_t Value) { FileSize = Value; }
119 uint64_t getOffset() const {
120 assert(Offset != ~UINT64_C(0) && "File offset not set!");
123 void setOffset(uint64_t Value) { Offset = Value; }
127 static bool classof(const MCFragment *O) { return true; }
132 class MCDataFragment : public MCFragment {
133 SmallString<32> Contents;
135 /// Fixups - The list of fixups in this fragment.
136 std::vector<MCAsmFixup> Fixups;
139 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
140 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
143 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
148 SmallString<32> &getContents() { return Contents; }
149 const SmallString<32> &getContents() const { return Contents; }
152 /// @name Fixup Access
155 void addFixup(MCAsmFixup Fixup) {
156 // Enforce invariant that fixups are in offset order.
157 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
158 "Fixups must be added in order!");
159 Fixups.push_back(Fixup);
162 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
163 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
165 fixup_iterator fixup_begin() { return Fixups.begin(); }
166 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
168 fixup_iterator fixup_end() {return Fixups.end();}
169 const_fixup_iterator fixup_end() const {return Fixups.end();}
171 size_t fixup_size() const { return Fixups.size(); }
175 static bool classof(const MCFragment *F) {
176 return F->getKind() == MCFragment::FT_Data;
178 static bool classof(const MCDataFragment *) { return true; }
183 class MCInstFragment : public MCFragment {
184 /// Inst - The instruction this is a fragment for.
187 /// InstSize - The size of the currently encoded instruction.
191 MCInstFragment(MCInst _Inst, unsigned _InstSize, MCSectionData *SD = 0)
192 : MCFragment(FT_Inst, SD), Inst(_Inst), InstSize(_InstSize) {}
197 unsigned getInstSize() const { return InstSize; }
199 const MCInst &getInst() const { return Inst; }
201 void setInst(MCInst Inst, unsigned InstSize) {
203 this->InstSize = InstSize;
208 static bool classof(const MCFragment *F) {
209 return F->getKind() == MCFragment::FT_Inst;
211 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 - true when aligning code and optimal nops to be used for
235 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
236 unsigned _MaxBytesToEmit, bool _EmitNops,
237 MCSectionData *SD = 0)
238 : MCFragment(FT_Align, SD), Alignment(_Alignment),
239 Value(_Value),ValueSize(_ValueSize),
240 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
245 unsigned getAlignment() const { return Alignment; }
247 int64_t getValue() const { return Value; }
249 unsigned getValueSize() const { return ValueSize; }
251 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
253 unsigned getEmitNops() const { return EmitNops; }
257 static bool classof(const MCFragment *F) {
258 return F->getKind() == MCFragment::FT_Align;
260 static bool classof(const MCAlignFragment *) { return true; }
265 class MCFillFragment : public MCFragment {
266 /// Value - Value to use for filling bytes.
269 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
272 /// Count - The number of copies of \arg Value to insert.
276 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
277 MCSectionData *SD = 0)
278 : MCFragment(FT_Fill, SD),
279 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
284 int64_t getValue() const { return Value; }
286 unsigned getValueSize() const { return ValueSize; }
288 uint64_t getCount() const { return Count; }
292 static bool classof(const MCFragment *F) {
293 return F->getKind() == MCFragment::FT_Fill;
295 static bool classof(const MCFillFragment *) { return true; }
300 class MCOrgFragment : public MCFragment {
301 /// Offset - The offset this fragment should start at.
302 const MCExpr *Offset;
304 /// Value - Value to use for filling bytes.
308 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
309 : MCFragment(FT_Org, SD),
310 Offset(&_Offset), Value(_Value) {}
315 const MCExpr &getOffset() const { return *Offset; }
317 uint8_t getValue() const { return Value; }
321 static bool classof(const MCFragment *F) {
322 return F->getKind() == MCFragment::FT_Org;
324 static bool classof(const MCOrgFragment *) { return true; }
329 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
330 /// but requires no physical space in the object file.
331 class MCZeroFillFragment : public MCFragment {
332 /// Size - The size of this fragment.
335 /// Alignment - The alignment for this fragment.
339 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
340 : MCFragment(FT_ZeroFill, SD),
341 Size(_Size), Alignment(_Alignment) {}
346 uint64_t getSize() const { return Size; }
348 unsigned getAlignment() const { return Alignment; }
352 static bool classof(const MCFragment *F) {
353 return F->getKind() == MCFragment::FT_ZeroFill;
355 static bool classof(const MCZeroFillFragment *) { return true; }
360 // FIXME: Should this be a separate class, or just merged into MCSection? Since
361 // we anticipate the fast path being through an MCAssembler, the only reason to
362 // keep it out is for API abstraction.
363 class MCSectionData : public ilist_node<MCSectionData> {
364 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
365 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
368 typedef iplist<MCFragment> FragmentListType;
370 typedef FragmentListType::const_iterator const_iterator;
371 typedef FragmentListType::iterator iterator;
373 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
374 typedef FragmentListType::reverse_iterator reverse_iterator;
377 iplist<MCFragment> Fragments;
378 const MCSection *Section;
380 /// Alignment - The maximum alignment seen in this section.
383 /// @name Assembler Backend Data
386 // FIXME: This could all be kept private to the assembler implementation.
388 /// Address - The computed address of this section. This is ~0 until
392 /// Size - The content size of this section. This is ~0 until initialized.
395 /// FileSize - The size of this section in the object file. This is ~0 until
399 /// HasInstructions - Whether this section has had instructions emitted into
401 unsigned HasInstructions : 1;
406 // Only for use as sentinel.
408 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
410 const MCSection &getSection() const { return *Section; }
412 unsigned getAlignment() const { return Alignment; }
413 void setAlignment(unsigned Value) { Alignment = Value; }
415 /// @name Fragment Access
418 const FragmentListType &getFragmentList() const { return Fragments; }
419 FragmentListType &getFragmentList() { return Fragments; }
421 iterator begin() { return Fragments.begin(); }
422 const_iterator begin() const { return Fragments.begin(); }
424 iterator end() { return Fragments.end(); }
425 const_iterator end() const { return Fragments.end(); }
427 reverse_iterator rbegin() { return Fragments.rbegin(); }
428 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
430 reverse_iterator rend() { return Fragments.rend(); }
431 const_reverse_iterator rend() const { return Fragments.rend(); }
433 size_t size() const { return Fragments.size(); }
435 bool empty() const { return Fragments.empty(); }
438 /// @name Assembler Backend Support
441 // FIXME: This could all be kept private to the assembler implementation.
443 uint64_t getAddress() const {
444 assert(Address != ~UINT64_C(0) && "Address not set!");
447 void setAddress(uint64_t Value) { Address = Value; }
449 uint64_t getSize() const {
450 assert(Size != ~UINT64_C(0) && "File size not set!");
453 void setSize(uint64_t Value) { Size = Value; }
455 uint64_t getFileSize() const {
456 assert(FileSize != ~UINT64_C(0) && "File size not set!");
459 void setFileSize(uint64_t Value) { FileSize = Value; }
461 bool hasInstructions() const { return HasInstructions; }
462 void setHasInstructions(bool Value) { HasInstructions = Value; }
469 // FIXME: Same concerns as with SectionData.
470 class MCSymbolData : public ilist_node<MCSymbolData> {
472 const MCSymbol *Symbol;
474 /// Fragment - The fragment this symbol's value is relative to, if any.
475 MCFragment *Fragment;
477 /// Offset - The offset to apply to the fragment address to form this symbol's
481 /// IsExternal - True if this symbol is visible outside this translation
483 unsigned IsExternal : 1;
485 /// IsPrivateExtern - True if this symbol is private extern.
486 unsigned IsPrivateExtern : 1;
488 /// CommonSize - The size of the symbol, if it is 'common', or 0.
490 // FIXME: Pack this in with other fields? We could put it in offset, since a
491 // common symbol can never get a definition.
494 /// CommonAlign - The alignment of the symbol, if it is 'common'.
496 // FIXME: Pack this in with other fields?
497 unsigned CommonAlign;
499 /// Flags - The Flags field is used by object file implementations to store
500 /// additional per symbol information which is not easily classified.
503 /// Index - Index field, for use by the object file implementation.
507 // Only for use as sentinel.
509 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
515 const MCSymbol &getSymbol() const { return *Symbol; }
517 MCFragment *getFragment() const { return Fragment; }
518 void setFragment(MCFragment *Value) { Fragment = Value; }
520 uint64_t getOffset() const { return Offset; }
521 void setOffset(uint64_t Value) { Offset = Value; }
523 uint64_t getAddress() const {
524 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
525 return getFragment()->getAddress() + getOffset();
529 /// @name Symbol Attributes
532 bool isExternal() const { return IsExternal; }
533 void setExternal(bool Value) { IsExternal = Value; }
535 bool isPrivateExtern() const { return IsPrivateExtern; }
536 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
538 /// isCommon - Is this a 'common' symbol.
539 bool isCommon() const { return CommonSize != 0; }
541 /// setCommon - Mark this symbol as being 'common'.
543 /// \param Size - The size of the symbol.
544 /// \param Align - The alignment of the symbol.
545 void setCommon(uint64_t Size, unsigned Align) {
550 /// getCommonSize - Return the size of a 'common' symbol.
551 uint64_t getCommonSize() const {
552 assert(isCommon() && "Not a 'common' symbol!");
556 /// getCommonAlignment - Return the alignment of a 'common' symbol.
557 unsigned getCommonAlignment() const {
558 assert(isCommon() && "Not a 'common' symbol!");
562 /// getFlags - Get the (implementation defined) symbol flags.
563 uint32_t getFlags() const { return Flags; }
565 /// setFlags - Set the (implementation defined) symbol flags.
566 void setFlags(uint32_t Value) { Flags = Value; }
568 /// getIndex - Get the (implementation defined) index.
569 uint64_t getIndex() const { return Index; }
571 /// setIndex - Set the (implementation defined) index.
572 void setIndex(uint64_t Value) { Index = Value; }
579 // FIXME: This really doesn't belong here. See comments below.
580 struct IndirectSymbolData {
582 MCSectionData *SectionData;
587 typedef iplist<MCSectionData> SectionDataListType;
588 typedef iplist<MCSymbolData> SymbolDataListType;
590 typedef SectionDataListType::const_iterator const_iterator;
591 typedef SectionDataListType::iterator iterator;
593 typedef SymbolDataListType::const_iterator const_symbol_iterator;
594 typedef SymbolDataListType::iterator symbol_iterator;
596 typedef std::vector<IndirectSymbolData>::const_iterator
597 const_indirect_symbol_iterator;
598 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
601 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
602 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
606 TargetAsmBackend &Backend;
608 MCCodeEmitter &Emitter;
612 iplist<MCSectionData> Sections;
614 iplist<MCSymbolData> Symbols;
616 /// The map of sections to their associated assembler backend data.
618 // FIXME: Avoid this indirection?
619 DenseMap<const MCSection*, MCSectionData*> SectionMap;
621 /// The map of symbols to their associated assembler backend data.
623 // FIXME: Avoid this indirection?
624 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
626 std::vector<IndirectSymbolData> IndirectSymbols;
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 /// LayoutSection - Assign offsets and sizes to the fragments in the section
654 /// \arg SD, and update the section size. The section file offset should
655 /// already have been computed.
656 void LayoutSection(MCSectionData &SD, MCAsmLayout &Layout);
658 /// LayoutOnce - Perform one layout iteration and return true if any offsets
660 bool LayoutOnce(MCAsmLayout &Layout);
662 /// FinishLayout - Finalize a layout, including fragment lowering.
663 void FinishLayout(MCAsmLayout &Layout);
666 /// Find the symbol which defines the atom containing given address, inside
667 /// the given section, or null if there is no such symbol.
669 // FIXME: Eliminate this, it is very slow.
670 const MCSymbolData *getAtomForAddress(const MCSectionData *Section,
671 uint64_t Address) const;
673 /// Find the symbol which defines the atom containing the given symbol, or
674 /// null if there is no such symbol.
676 // FIXME: Eliminate this, it is very slow.
677 const MCSymbolData *getAtom(const MCSymbolData *Symbol) const;
679 /// Check whether a particular symbol is visible to the linker and is required
680 /// in the symbol table, or whether it can be discarded by the assembler. This
681 /// also effects whether the assembler treats the label as potentially
682 /// defining a separate atom.
683 bool isSymbolLinkerVisible(const MCSymbolData *SD) const;
685 /// Emit the section contents using the given object writer.
687 // FIXME: Should MCAssembler always have a reference to the object writer?
688 void WriteSectionData(const MCSectionData *Section, MCObjectWriter *OW) const;
691 /// Construct a new assembler instance.
693 /// \arg OS - The stream to output to.
695 // FIXME: How are we going to parameterize this? Two obvious options are stay
696 // concrete and require clients to pass in a target like object. The other
697 // option is to make this abstract, and have targets provide concrete
698 // implementations as we do with AsmParser.
699 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
700 MCCodeEmitter &_Emitter, raw_ostream &OS);
703 MCContext &getContext() const { return Context; }
705 TargetAsmBackend &getBackend() const { return Backend; }
707 MCCodeEmitter &getEmitter() const { return Emitter; }
709 /// Finish - Do final processing and write the object to the output stream.
712 // FIXME: This does not belong here.
713 bool getSubsectionsViaSymbols() const {
714 return SubsectionsViaSymbols;
716 void setSubsectionsViaSymbols(bool Value) {
717 SubsectionsViaSymbols = Value;
720 /// @name Section List Access
723 const SectionDataListType &getSectionList() const { return Sections; }
724 SectionDataListType &getSectionList() { return Sections; }
726 iterator begin() { return Sections.begin(); }
727 const_iterator begin() const { return Sections.begin(); }
729 iterator end() { return Sections.end(); }
730 const_iterator end() const { return Sections.end(); }
732 size_t size() const { return Sections.size(); }
735 /// @name Symbol List Access
738 const SymbolDataListType &getSymbolList() const { return Symbols; }
739 SymbolDataListType &getSymbolList() { return Symbols; }
741 symbol_iterator symbol_begin() { return Symbols.begin(); }
742 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
744 symbol_iterator symbol_end() { return Symbols.end(); }
745 const_symbol_iterator symbol_end() const { return Symbols.end(); }
747 size_t symbol_size() const { return Symbols.size(); }
750 /// @name Indirect Symbol List Access
753 // FIXME: This is a total hack, this should not be here. Once things are
754 // factored so that the streamer has direct access to the .o writer, it can
756 std::vector<IndirectSymbolData> &getIndirectSymbols() {
757 return IndirectSymbols;
760 indirect_symbol_iterator indirect_symbol_begin() {
761 return IndirectSymbols.begin();
763 const_indirect_symbol_iterator indirect_symbol_begin() const {
764 return IndirectSymbols.begin();
767 indirect_symbol_iterator indirect_symbol_end() {
768 return IndirectSymbols.end();
770 const_indirect_symbol_iterator indirect_symbol_end() const {
771 return IndirectSymbols.end();
774 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
777 /// @name Backend Data Access
780 MCSectionData &getSectionData(const MCSection &Section) const {
781 MCSectionData *Entry = SectionMap.lookup(&Section);
782 assert(Entry && "Missing section data!");
786 MCSectionData &getOrCreateSectionData(const MCSection &Section,
788 MCSectionData *&Entry = SectionMap[&Section];
790 if (Created) *Created = !Entry;
792 Entry = new MCSectionData(Section, this);
797 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
798 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
799 assert(Entry && "Missing symbol data!");
803 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
805 MCSymbolData *&Entry = SymbolMap[&Symbol];
807 if (Created) *Created = !Entry;
809 Entry = new MCSymbolData(Symbol, 0, 0, this);
819 } // end namespace llvm