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/System/DataTypes.h"
20 #include <vector> // FIXME: Shouldn't be needed.
33 class TargetAsmBackend;
35 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
36 /// which needs to be rewritten. This region will either be rewritten by the
37 /// assembler or cause a relocation entry to be generated.
39 /// Offset - The offset inside the fragment which needs to be rewritten.
42 /// Value - The expression to eventually write into the fragment.
45 /// Kind - The fixup kind.
48 /// FixedValue - The value to replace the fix up by.
50 // FIXME: This should not be here.
54 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
55 : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {}
58 class MCFragment : public ilist_node<MCFragment> {
59 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
60 void operator=(const MCFragment&); // DO NOT IMPLEMENT
74 /// Parent - The data for the section this fragment is in.
75 MCSectionData *Parent;
77 /// @name Assembler Backend Data
80 // FIXME: This could all be kept private to the assembler implementation.
82 /// Offset - The offset of this fragment in its section. This is ~0 until
86 /// FileSize - The file size of this section. This is ~0 until initialized.
92 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
97 virtual ~MCFragment();
99 FragmentType getKind() const { return Kind; }
101 MCSectionData *getParent() const { return Parent; }
102 void setParent(MCSectionData *Value) { Parent = Value; }
104 // FIXME: This should be abstract, fix sentinel.
105 virtual uint64_t getMaxFileSize() const {
106 assert(0 && "Invalid getMaxFileSize call!");
110 /// @name Assembler Backend Support
113 // FIXME: This could all be kept private to the assembler implementation.
115 uint64_t getAddress() const;
117 uint64_t getFileSize() const {
118 assert(FileSize != ~UINT64_C(0) && "File size not set!");
121 void setFileSize(uint64_t Value) {
122 assert(Value <= getMaxFileSize() && "Invalid file size!");
126 uint64_t getOffset() const {
127 assert(Offset != ~UINT64_C(0) && "File offset not set!");
130 void setOffset(uint64_t Value) { Offset = Value; }
134 static bool classof(const MCFragment *O) { return true; }
139 class MCDataFragment : public MCFragment {
140 SmallString<32> Contents;
142 /// Fixups - The list of fixups in this fragment.
143 std::vector<MCAsmFixup> Fixups;
146 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
147 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
150 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
155 uint64_t getMaxFileSize() const {
156 return Contents.size();
159 SmallString<32> &getContents() { return Contents; }
160 const SmallString<32> &getContents() const { return Contents; }
164 /// @name Fixup Access
167 void addFixup(MCAsmFixup Fixup) {
168 // Enforce invariant that fixups are in offset order.
169 assert((Fixups.empty() || Fixup.Offset > Fixups.back().Offset) &&
170 "Fixups must be added in order!");
171 Fixups.push_back(Fixup);
174 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
175 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
177 fixup_iterator fixup_begin() { return Fixups.begin(); }
178 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
180 fixup_iterator fixup_end() {return Fixups.end();}
181 const_fixup_iterator fixup_end() const {return Fixups.end();}
183 size_t fixup_size() const { return Fixups.size(); }
187 static bool classof(const MCFragment *F) {
188 return F->getKind() == MCFragment::FT_Data;
190 static bool classof(const MCDataFragment *) { return true; }
195 class MCAlignFragment : public MCFragment {
196 /// Alignment - The alignment to ensure, in bytes.
199 /// Value - Value to use for filling padding bytes.
202 /// ValueSize - The size of the integer (in bytes) of \arg Value.
205 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
206 /// cannot be satisfied in this width then this fragment is ignored.
207 unsigned MaxBytesToEmit;
209 /// EmitNops - true when aligning code and optimal nops to be used for
214 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
215 unsigned _MaxBytesToEmit, bool _EmitNops,
216 MCSectionData *SD = 0)
217 : MCFragment(FT_Align, SD), Alignment(_Alignment),
218 Value(_Value),ValueSize(_ValueSize),
219 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
224 uint64_t getMaxFileSize() const {
225 return std::max(Alignment - 1, MaxBytesToEmit);
228 unsigned getAlignment() const { return Alignment; }
230 int64_t getValue() const { return Value; }
232 unsigned getValueSize() const { return ValueSize; }
234 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
236 unsigned getEmitNops() const { return EmitNops; }
240 static bool classof(const MCFragment *F) {
241 return F->getKind() == MCFragment::FT_Align;
243 static bool classof(const MCAlignFragment *) { return true; }
248 class MCFillFragment : public MCFragment {
249 /// Value - Value to use for filling bytes.
252 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
255 /// Count - The number of copies of \arg Value to insert.
259 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
260 MCSectionData *SD = 0)
261 : MCFragment(FT_Fill, SD),
262 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
267 uint64_t getMaxFileSize() const {
268 return ValueSize * Count;
271 int64_t getValue() const { return Value; }
273 unsigned getValueSize() const { return ValueSize; }
275 uint64_t getCount() const { return Count; }
279 static bool classof(const MCFragment *F) {
280 return F->getKind() == MCFragment::FT_Fill;
282 static bool classof(const MCFillFragment *) { return true; }
287 class MCOrgFragment : public MCFragment {
288 /// Offset - The offset this fragment should start at.
289 const MCExpr *Offset;
291 /// Value - Value to use for filling bytes.
295 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
296 : MCFragment(FT_Org, SD),
297 Offset(&_Offset), Value(_Value) {}
302 uint64_t getMaxFileSize() const {
303 // FIXME: This doesn't make much sense.
307 const MCExpr &getOffset() const { return *Offset; }
309 uint8_t getValue() const { return Value; }
313 static bool classof(const MCFragment *F) {
314 return F->getKind() == MCFragment::FT_Org;
316 static bool classof(const MCOrgFragment *) { return true; }
321 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
322 /// but requires no physical space in the object file.
323 class MCZeroFillFragment : public MCFragment {
324 /// Size - The size of this fragment.
327 /// Alignment - The alignment for this fragment.
331 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
332 : MCFragment(FT_ZeroFill, SD),
333 Size(_Size), Alignment(_Alignment) {}
338 uint64_t getMaxFileSize() const {
339 // FIXME: This also doesn't make much sense, this method is misnamed.
343 uint64_t getSize() const { return Size; }
345 unsigned getAlignment() const { return Alignment; }
349 static bool classof(const MCFragment *F) {
350 return F->getKind() == MCFragment::FT_ZeroFill;
352 static bool classof(const MCZeroFillFragment *) { return true; }
357 // FIXME: Should this be a separate class, or just merged into MCSection? Since
358 // we anticipate the fast path being through an MCAssembler, the only reason to
359 // keep it out is for API abstraction.
360 class MCSectionData : public ilist_node<MCSectionData> {
361 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
362 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
365 typedef iplist<MCFragment> FragmentListType;
367 typedef FragmentListType::const_iterator const_iterator;
368 typedef FragmentListType::iterator iterator;
370 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
371 typedef FragmentListType::reverse_iterator reverse_iterator;
374 iplist<MCFragment> Fragments;
375 const MCSection *Section;
377 /// Alignment - The maximum alignment seen in this section.
380 /// @name Assembler Backend Data
383 // FIXME: This could all be kept private to the assembler implementation.
385 /// Address - The computed address of this section. This is ~0 until
389 /// Size - The content size of this section. This is ~0 until initialized.
392 /// FileSize - The size of this section in the object file. This is ~0 until
396 /// HasInstructions - Whether this section has had instructions emitted into
398 unsigned HasInstructions : 1;
403 // Only for use as sentinel.
405 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
407 const MCSection &getSection() const { return *Section; }
409 unsigned getAlignment() const { return Alignment; }
410 void setAlignment(unsigned Value) { Alignment = Value; }
412 /// @name Fragment Access
415 const FragmentListType &getFragmentList() const { return Fragments; }
416 FragmentListType &getFragmentList() { return Fragments; }
418 iterator begin() { return Fragments.begin(); }
419 const_iterator begin() const { return Fragments.begin(); }
421 iterator end() { return Fragments.end(); }
422 const_iterator end() const { return Fragments.end(); }
424 reverse_iterator rbegin() { return Fragments.rbegin(); }
425 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
427 reverse_iterator rend() { return Fragments.rend(); }
428 const_reverse_iterator rend() const { return Fragments.rend(); }
430 size_t size() const { return Fragments.size(); }
432 bool empty() const { return Fragments.empty(); }
435 /// @name Assembler Backend Support
438 // FIXME: This could all be kept private to the assembler implementation.
440 uint64_t getAddress() const {
441 assert(Address != ~UINT64_C(0) && "Address not set!");
444 void setAddress(uint64_t Value) { Address = Value; }
446 uint64_t getSize() const {
447 assert(Size != ~UINT64_C(0) && "File size not set!");
450 void setSize(uint64_t Value) { Size = Value; }
452 uint64_t getFileSize() const {
453 assert(FileSize != ~UINT64_C(0) && "File size not set!");
456 void setFileSize(uint64_t Value) { FileSize = Value; }
458 bool hasInstructions() const { return HasInstructions; }
459 void setHasInstructions(bool Value) { HasInstructions = Value; }
466 // FIXME: Same concerns as with SectionData.
467 class MCSymbolData : public ilist_node<MCSymbolData> {
469 const MCSymbol *Symbol;
471 /// Fragment - The fragment this symbol's value is relative to, if any.
472 MCFragment *Fragment;
474 /// Offset - The offset to apply to the fragment address to form this symbol's
478 /// IsExternal - True if this symbol is visible outside this translation
480 unsigned IsExternal : 1;
482 /// IsPrivateExtern - True if this symbol is private extern.
483 unsigned IsPrivateExtern : 1;
485 /// CommonSize - The size of the symbol, if it is 'common', or 0.
487 // FIXME: Pack this in with other fields? We could put it in offset, since a
488 // common symbol can never get a definition.
491 /// CommonAlign - The alignment of the symbol, if it is 'common'.
493 // FIXME: Pack this in with other fields?
494 unsigned CommonAlign;
496 /// Flags - The Flags field is used by object file implementations to store
497 /// additional per symbol information which is not easily classified.
500 /// Index - Index field, for use by the object file implementation.
504 // Only for use as sentinel.
506 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
512 const MCSymbol &getSymbol() const { return *Symbol; }
514 MCFragment *getFragment() const { return Fragment; }
515 void setFragment(MCFragment *Value) { Fragment = Value; }
517 uint64_t getOffset() const { return Offset; }
518 void setOffset(uint64_t Value) { Offset = Value; }
520 uint64_t getAddress() const {
521 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
522 return getFragment()->getAddress() + getOffset();
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>::iterator indirect_symbol_iterator;
596 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
597 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
601 TargetAsmBackend &Backend;
605 iplist<MCSectionData> Sections;
607 iplist<MCSymbolData> Symbols;
609 /// The map of sections to their associated assembler backend data.
611 // FIXME: Avoid this indirection?
612 DenseMap<const MCSection*, MCSectionData*> SectionMap;
614 /// The map of symbols to their associated assembler backend data.
616 // FIXME: Avoid this indirection?
617 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
619 std::vector<IndirectSymbolData> IndirectSymbols;
621 unsigned SubsectionsViaSymbols : 1;
624 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
625 /// (increased in size, in order to hold its value correctly).
626 bool FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF);
628 /// LayoutSection - Assign offsets and sizes to the fragments in the section
629 /// \arg SD, and update the section size. The section file offset should
630 /// already have been computed.
631 void LayoutSection(MCSectionData &SD);
633 /// LayoutOnce - Perform one layout iteration and return true if any offsets
637 // FIXME: Make protected once we factor out object writer classes.
639 /// Evaluate a fixup to a relocatable expression and the value which should be
640 /// placed into the fixup.
642 /// \param Layout The layout to use for evaluation.
643 /// \param Fixup The fixup to evaluate.
644 /// \param DF The fragment the fixup is inside.
645 /// \param Target [out] On return, the relocatable expression the fixup
647 /// \param Value [out] On return, the value of the fixup as currently layed
649 /// \return Whether the fixup value was fully resolved. This is true if the
650 /// \arg Value result is fixed, otherwise the value may change due to
652 bool EvaluateFixup(const MCAsmLayout &Layout,
653 MCAsmFixup &Fixup, MCDataFragment *DF,
654 MCValue &Target, uint64_t &Value) const;
657 /// Construct a new assembler instance.
659 /// \arg OS - The stream to output to.
661 // FIXME: How are we going to parameterize this? Two obvious options are stay
662 // concrete and require clients to pass in a target like object. The other
663 // option is to make this abstract, and have targets provide concrete
664 // implementations as we do with AsmParser.
665 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS);
668 MCContext &getContext() const { return Context; }
670 TargetAsmBackend &getBackend() const { return Backend; }
672 /// Finish - Do final processing and write the object to the output stream.
675 // FIXME: This does not belong here.
676 bool getSubsectionsViaSymbols() const {
677 return SubsectionsViaSymbols;
679 void setSubsectionsViaSymbols(bool Value) {
680 SubsectionsViaSymbols = Value;
683 /// @name Section List Access
686 const SectionDataListType &getSectionList() const { return Sections; }
687 SectionDataListType &getSectionList() { return Sections; }
689 iterator begin() { return Sections.begin(); }
690 const_iterator begin() const { return Sections.begin(); }
692 iterator end() { return Sections.end(); }
693 const_iterator end() const { return Sections.end(); }
695 size_t size() const { return Sections.size(); }
698 /// @name Symbol List Access
701 const SymbolDataListType &getSymbolList() const { return Symbols; }
702 SymbolDataListType &getSymbolList() { return Symbols; }
704 symbol_iterator symbol_begin() { return Symbols.begin(); }
705 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
707 symbol_iterator symbol_end() { return Symbols.end(); }
708 const_symbol_iterator symbol_end() const { return Symbols.end(); }
710 size_t symbol_size() const { return Symbols.size(); }
713 /// @name Indirect Symbol List Access
716 // FIXME: This is a total hack, this should not be here. Once things are
717 // factored so that the streamer has direct access to the .o writer, it can
719 std::vector<IndirectSymbolData> &getIndirectSymbols() {
720 return IndirectSymbols;
723 indirect_symbol_iterator indirect_symbol_begin() {
724 return IndirectSymbols.begin();
727 indirect_symbol_iterator indirect_symbol_end() {
728 return IndirectSymbols.end();
731 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
734 /// @name Backend Data Access
737 MCSectionData &getSectionData(const MCSection &Section) const {
738 MCSectionData *Entry = SectionMap.lookup(&Section);
739 assert(Entry && "Missing section data!");
743 MCSectionData &getOrCreateSectionData(const MCSection &Section,
745 MCSectionData *&Entry = SectionMap[&Section];
747 if (Created) *Created = !Entry;
749 Entry = new MCSectionData(Section, this);
754 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
755 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
756 assert(Entry && "Missing symbol data!");
760 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
762 MCSymbolData *&Entry = SymbolMap[&Symbol];
764 if (Created) *Created = !Entry;
766 Entry = new MCSymbolData(Symbol, 0, 0, this);
776 } // end namespace llvm