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.
31 class TargetAsmBackend;
33 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
34 /// which needs to be rewritten. This region will either be rewritten by the
35 /// assembler or cause a relocation entry to be generated.
37 /// Offset - The offset inside the fragment which needs to be rewritten.
40 /// Value - The expression to eventually write into the fragment.
43 /// Kind - The fixup kind.
46 /// FixedValue - The value to replace the fix up by.
48 // FIXME: This should not be here.
52 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
53 : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {}
56 class MCFragment : public ilist_node<MCFragment> {
57 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
58 void operator=(const MCFragment&); // DO NOT IMPLEMENT
72 /// Parent - The data for the section this fragment is in.
73 MCSectionData *Parent;
75 /// @name Assembler Backend Data
78 // FIXME: This could all be kept private to the assembler implementation.
80 /// Offset - The offset of this fragment in its section. This is ~0 until
84 /// FileSize - The file size of this section. This is ~0 until initialized.
90 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
95 virtual ~MCFragment();
97 FragmentType getKind() const { return Kind; }
99 MCSectionData *getParent() const { return Parent; }
100 void setParent(MCSectionData *Value) { Parent = Value; }
102 // FIXME: This should be abstract, fix sentinel.
103 virtual uint64_t getMaxFileSize() const {
104 assert(0 && "Invalid getMaxFileSize call!");
108 /// @name Assembler Backend Support
111 // FIXME: This could all be kept private to the assembler implementation.
113 uint64_t getAddress() const;
115 uint64_t getFileSize() const {
116 assert(FileSize != ~UINT64_C(0) && "File size not set!");
119 void setFileSize(uint64_t Value) {
120 assert(Value <= getMaxFileSize() && "Invalid file size!");
124 uint64_t getOffset() const {
125 assert(Offset != ~UINT64_C(0) && "File offset not set!");
128 void setOffset(uint64_t Value) { Offset = Value; }
132 static bool classof(const MCFragment *O) { return true; }
137 class MCDataFragment : public MCFragment {
138 SmallString<32> Contents;
140 /// Fixups - The list of fixups in this fragment.
141 std::vector<MCAsmFixup> Fixups;
144 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
145 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
148 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
153 uint64_t getMaxFileSize() const {
154 return Contents.size();
157 SmallString<32> &getContents() { return Contents; }
158 const SmallString<32> &getContents() const { return Contents; }
162 /// @name Fixup Access
165 void addFixup(MCAsmFixup Fixup) {
166 // Enforce invariant that fixups are in offset order.
167 assert(Fixups.empty() || Fixup.Offset > Fixups.back().Offset &&
168 "Fixups must be added in order!");
169 Fixups.push_back(Fixup);
172 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
173 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
175 fixup_iterator fixup_begin() { return Fixups.begin(); }
176 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
178 fixup_iterator fixup_end() {return Fixups.end();}
179 const_fixup_iterator fixup_end() const {return Fixups.end();}
181 size_t fixup_size() const { return Fixups.size(); }
185 static bool classof(const MCFragment *F) {
186 return F->getKind() == MCFragment::FT_Data;
188 static bool classof(const MCDataFragment *) { return true; }
193 class MCAlignFragment : public MCFragment {
194 /// Alignment - The alignment to ensure, in bytes.
197 /// Value - Value to use for filling padding bytes.
200 /// ValueSize - The size of the integer (in bytes) of \arg Value.
203 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
204 /// cannot be satisfied in this width then this fragment is ignored.
205 unsigned MaxBytesToEmit;
207 /// EmitNops - true when aligning code and optimal nops to be used for
212 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
213 unsigned _MaxBytesToEmit, bool _EmitNops,
214 MCSectionData *SD = 0)
215 : MCFragment(FT_Align, SD), Alignment(_Alignment),
216 Value(_Value),ValueSize(_ValueSize),
217 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
222 uint64_t getMaxFileSize() const {
223 return std::max(Alignment - 1, MaxBytesToEmit);
226 unsigned getAlignment() const { return Alignment; }
228 int64_t getValue() const { return Value; }
230 unsigned getValueSize() const { return ValueSize; }
232 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
234 unsigned getEmitNops() const { return EmitNops; }
238 static bool classof(const MCFragment *F) {
239 return F->getKind() == MCFragment::FT_Align;
241 static bool classof(const MCAlignFragment *) { return true; }
246 class MCFillFragment : public MCFragment {
247 /// Value - Value to use for filling bytes.
250 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
253 /// Count - The number of copies of \arg Value to insert.
257 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
258 MCSectionData *SD = 0)
259 : MCFragment(FT_Fill, SD),
260 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
265 uint64_t getMaxFileSize() const {
266 return ValueSize * Count;
269 int64_t getValue() const { return Value; }
271 unsigned getValueSize() const { return ValueSize; }
273 uint64_t getCount() const { return Count; }
277 static bool classof(const MCFragment *F) {
278 return F->getKind() == MCFragment::FT_Fill;
280 static bool classof(const MCFillFragment *) { return true; }
285 class MCOrgFragment : public MCFragment {
286 /// Offset - The offset this fragment should start at.
287 const MCExpr *Offset;
289 /// Value - Value to use for filling bytes.
293 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
294 : MCFragment(FT_Org, SD),
295 Offset(&_Offset), Value(_Value) {}
300 uint64_t getMaxFileSize() const {
301 // FIXME: This doesn't make much sense.
305 const MCExpr &getOffset() const { return *Offset; }
307 uint8_t getValue() const { return Value; }
311 static bool classof(const MCFragment *F) {
312 return F->getKind() == MCFragment::FT_Org;
314 static bool classof(const MCOrgFragment *) { return true; }
319 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
320 /// but requires no physical space in the object file.
321 class MCZeroFillFragment : public MCFragment {
322 /// Size - The size of this fragment.
325 /// Alignment - The alignment for this fragment.
329 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
330 : MCFragment(FT_ZeroFill, SD),
331 Size(_Size), Alignment(_Alignment) {}
336 uint64_t getMaxFileSize() const {
337 // FIXME: This also doesn't make much sense, this method is misnamed.
341 uint64_t getSize() const { return Size; }
343 unsigned getAlignment() const { return Alignment; }
347 static bool classof(const MCFragment *F) {
348 return F->getKind() == MCFragment::FT_ZeroFill;
350 static bool classof(const MCZeroFillFragment *) { return true; }
355 // FIXME: Should this be a separate class, or just merged into MCSection? Since
356 // we anticipate the fast path being through an MCAssembler, the only reason to
357 // keep it out is for API abstraction.
358 class MCSectionData : public ilist_node<MCSectionData> {
359 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
360 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
363 typedef iplist<MCFragment> FragmentListType;
365 typedef FragmentListType::const_iterator const_iterator;
366 typedef FragmentListType::iterator iterator;
368 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
369 typedef FragmentListType::reverse_iterator reverse_iterator;
372 iplist<MCFragment> Fragments;
373 const MCSection *Section;
375 /// Alignment - The maximum alignment seen in this section.
378 /// @name Assembler Backend Data
381 // FIXME: This could all be kept private to the assembler implementation.
383 /// Address - The computed address of this section. This is ~0 until
387 /// Size - The content size of this section. This is ~0 until initialized.
390 /// FileSize - The size of this section in the object file. This is ~0 until
394 /// HasInstructions - Whether this section has had instructions emitted into
396 unsigned HasInstructions : 1;
401 // Only for use as sentinel.
403 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
405 const MCSection &getSection() const { return *Section; }
407 unsigned getAlignment() const { return Alignment; }
408 void setAlignment(unsigned Value) { Alignment = Value; }
410 /// @name Fragment Access
413 const FragmentListType &getFragmentList() const { return Fragments; }
414 FragmentListType &getFragmentList() { return Fragments; }
416 iterator begin() { return Fragments.begin(); }
417 const_iterator begin() const { return Fragments.begin(); }
419 iterator end() { return Fragments.end(); }
420 const_iterator end() const { return Fragments.end(); }
422 reverse_iterator rbegin() { return Fragments.rbegin(); }
423 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
425 reverse_iterator rend() { return Fragments.rend(); }
426 const_reverse_iterator rend() const { return Fragments.rend(); }
428 size_t size() const { return Fragments.size(); }
430 bool empty() const { return Fragments.empty(); }
433 /// @name Assembler Backend Support
436 // FIXME: This could all be kept private to the assembler implementation.
438 uint64_t getAddress() const {
439 assert(Address != ~UINT64_C(0) && "Address not set!");
442 void setAddress(uint64_t Value) { Address = Value; }
444 uint64_t getSize() const {
445 assert(Size != ~UINT64_C(0) && "File size not set!");
448 void setSize(uint64_t Value) { Size = Value; }
450 uint64_t getFileSize() const {
451 assert(FileSize != ~UINT64_C(0) && "File size not set!");
454 void setFileSize(uint64_t Value) { FileSize = Value; }
456 bool hasInstructions() const { return HasInstructions; }
457 void setHasInstructions(bool Value) { HasInstructions = Value; }
464 // FIXME: Same concerns as with SectionData.
465 class MCSymbolData : public ilist_node<MCSymbolData> {
467 const MCSymbol *Symbol;
469 /// Fragment - The fragment this symbol's value is relative to, if any.
470 MCFragment *Fragment;
472 /// Offset - The offset to apply to the fragment address to form this symbol's
476 /// IsExternal - True if this symbol is visible outside this translation
478 unsigned IsExternal : 1;
480 /// IsPrivateExtern - True if this symbol is private extern.
481 unsigned IsPrivateExtern : 1;
483 /// CommonSize - The size of the symbol, if it is 'common', or 0.
485 // FIXME: Pack this in with other fields? We could put it in offset, since a
486 // common symbol can never get a definition.
489 /// CommonAlign - The alignment of the symbol, if it is 'common'.
491 // FIXME: Pack this in with other fields?
492 unsigned CommonAlign;
494 /// Flags - The Flags field is used by object file implementations to store
495 /// additional per symbol information which is not easily classified.
498 /// Index - Index field, for use by the object file implementation.
502 // Only for use as sentinel.
504 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
510 const MCSymbol &getSymbol() const { return *Symbol; }
512 MCFragment *getFragment() const { return Fragment; }
513 void setFragment(MCFragment *Value) { Fragment = Value; }
515 uint64_t getOffset() const { return Offset; }
516 void setOffset(uint64_t Value) { Offset = Value; }
518 uint64_t getAddress() const {
519 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
520 return getFragment()->getAddress() + getOffset();
524 /// @name Symbol Attributes
527 bool isExternal() const { return IsExternal; }
528 void setExternal(bool Value) { IsExternal = Value; }
530 bool isPrivateExtern() const { return IsPrivateExtern; }
531 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
533 /// isCommon - Is this a 'common' symbol.
534 bool isCommon() const { return CommonSize != 0; }
536 /// setCommon - Mark this symbol as being 'common'.
538 /// \param Size - The size of the symbol.
539 /// \param Align - The alignment of the symbol.
540 void setCommon(uint64_t Size, unsigned Align) {
545 /// getCommonSize - Return the size of a 'common' symbol.
546 uint64_t getCommonSize() const {
547 assert(isCommon() && "Not a 'common' symbol!");
551 /// getCommonAlignment - Return the alignment of a 'common' symbol.
552 unsigned getCommonAlignment() const {
553 assert(isCommon() && "Not a 'common' symbol!");
557 /// getFlags - Get the (implementation defined) symbol flags.
558 uint32_t getFlags() const { return Flags; }
560 /// setFlags - Set the (implementation defined) symbol flags.
561 void setFlags(uint32_t Value) { Flags = Value; }
563 /// getIndex - Get the (implementation defined) index.
564 uint64_t getIndex() const { return Index; }
566 /// setIndex - Set the (implementation defined) index.
567 void setIndex(uint64_t Value) { Index = Value; }
574 // FIXME: This really doesn't belong here. See comments below.
575 struct IndirectSymbolData {
577 MCSectionData *SectionData;
582 typedef iplist<MCSectionData> SectionDataListType;
583 typedef iplist<MCSymbolData> SymbolDataListType;
585 typedef SectionDataListType::const_iterator const_iterator;
586 typedef SectionDataListType::iterator iterator;
588 typedef SymbolDataListType::const_iterator const_symbol_iterator;
589 typedef SymbolDataListType::iterator symbol_iterator;
591 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
594 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
595 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
599 TargetAsmBackend &Backend;
603 iplist<MCSectionData> Sections;
605 iplist<MCSymbolData> Symbols;
607 /// The map of sections to their associated assembler backend data.
609 // FIXME: Avoid this indirection?
610 DenseMap<const MCSection*, MCSectionData*> SectionMap;
612 /// The map of symbols to their associated assembler backend data.
614 // FIXME: Avoid this indirection?
615 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
617 std::vector<IndirectSymbolData> IndirectSymbols;
619 unsigned SubsectionsViaSymbols : 1;
622 /// LayoutSection - Assign offsets and sizes to the fragments in the section
623 /// \arg SD, and update the section size. The section file offset should
624 /// already have been computed.
625 void LayoutSection(MCSectionData &SD);
628 /// Construct a new assembler instance.
630 /// \arg OS - The stream to output to.
632 // FIXME: How are we going to parameterize this? Two obvious options are stay
633 // concrete and require clients to pass in a target like object. The other
634 // option is to make this abstract, and have targets provide concrete
635 // implementations as we do with AsmParser.
636 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS);
639 MCContext &getContext() const { return Context; }
641 TargetAsmBackend &getBackend() const { return Backend; }
643 /// Finish - Do final processing and write the object to the output stream.
646 // FIXME: This does not belong here.
647 bool getSubsectionsViaSymbols() const {
648 return SubsectionsViaSymbols;
650 void setSubsectionsViaSymbols(bool Value) {
651 SubsectionsViaSymbols = Value;
654 /// @name Section List Access
657 const SectionDataListType &getSectionList() const { return Sections; }
658 SectionDataListType &getSectionList() { return Sections; }
660 iterator begin() { return Sections.begin(); }
661 const_iterator begin() const { return Sections.begin(); }
663 iterator end() { return Sections.end(); }
664 const_iterator end() const { return Sections.end(); }
666 size_t size() const { return Sections.size(); }
669 /// @name Symbol List Access
672 const SymbolDataListType &getSymbolList() const { return Symbols; }
673 SymbolDataListType &getSymbolList() { return Symbols; }
675 symbol_iterator symbol_begin() { return Symbols.begin(); }
676 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
678 symbol_iterator symbol_end() { return Symbols.end(); }
679 const_symbol_iterator symbol_end() const { return Symbols.end(); }
681 size_t symbol_size() const { return Symbols.size(); }
684 /// @name Indirect Symbol List Access
687 // FIXME: This is a total hack, this should not be here. Once things are
688 // factored so that the streamer has direct access to the .o writer, it can
690 std::vector<IndirectSymbolData> &getIndirectSymbols() {
691 return IndirectSymbols;
694 indirect_symbol_iterator indirect_symbol_begin() {
695 return IndirectSymbols.begin();
698 indirect_symbol_iterator indirect_symbol_end() {
699 return IndirectSymbols.end();
702 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
705 /// @name Backend Data Access
708 MCSectionData &getSectionData(const MCSection &Section) const {
709 MCSectionData *Entry = SectionMap.lookup(&Section);
710 assert(Entry && "Missing section data!");
714 MCSectionData &getOrCreateSectionData(const MCSection &Section,
716 MCSectionData *&Entry = SectionMap[&Section];
718 if (Created) *Created = !Entry;
720 Entry = new MCSectionData(Section, this);
725 MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
726 MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
727 assert(Entry && "Missing symbol data!");
731 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
733 MCSymbolData *&Entry = SymbolMap[&Symbol];
735 if (Created) *Created = !Entry;
737 Entry = new MCSymbolData(Symbol, 0, 0, this);
747 } // end namespace llvm