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 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
166 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
168 fixup_iterator fixup_begin() { return Fixups.begin(); }
169 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
171 fixup_iterator fixup_end() {return Fixups.end();}
172 const_fixup_iterator fixup_end() const {return Fixups.end();}
174 size_t fixup_size() const { return Fixups.size(); }
178 static bool classof(const MCFragment *F) {
179 return F->getKind() == MCFragment::FT_Data;
181 static bool classof(const MCDataFragment *) { return true; }
186 class MCAlignFragment : public MCFragment {
187 /// Alignment - The alignment to ensure, in bytes.
190 /// Value - Value to use for filling padding bytes.
193 /// ValueSize - The size of the integer (in bytes) of \arg Value.
196 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
197 /// cannot be satisfied in this width then this fragment is ignored.
198 unsigned MaxBytesToEmit;
200 /// EmitNops - true when aligning code and optimal nops to be used for filling
204 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
205 unsigned _MaxBytesToEmit, bool _EmitNops,
206 MCSectionData *SD = 0)
207 : MCFragment(FT_Align, SD), Alignment(_Alignment),
208 Value(_Value),ValueSize(_ValueSize),
209 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
214 uint64_t getMaxFileSize() const {
215 return std::max(Alignment - 1, MaxBytesToEmit);
218 unsigned getAlignment() const { return Alignment; }
220 int64_t getValue() const { return Value; }
222 unsigned getValueSize() const { return ValueSize; }
224 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
226 unsigned getEmitNops() const { return EmitNops; }
230 static bool classof(const MCFragment *F) {
231 return F->getKind() == MCFragment::FT_Align;
233 static bool classof(const MCAlignFragment *) { return true; }
238 class MCFillFragment : public MCFragment {
239 /// Value - Value to use for filling bytes.
242 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
245 /// Count - The number of copies of \arg Value to insert.
249 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
250 MCSectionData *SD = 0)
251 : MCFragment(FT_Fill, SD),
252 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
257 uint64_t getMaxFileSize() const {
258 return ValueSize * Count;
261 int64_t getValue() const { return Value; }
263 unsigned getValueSize() const { return ValueSize; }
265 uint64_t getCount() const { return Count; }
269 static bool classof(const MCFragment *F) {
270 return F->getKind() == MCFragment::FT_Fill;
272 static bool classof(const MCFillFragment *) { return true; }
277 class MCOrgFragment : public MCFragment {
278 /// Offset - The offset this fragment should start at.
279 const MCExpr *Offset;
281 /// Value - Value to use for filling bytes.
285 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
286 : MCFragment(FT_Org, SD),
287 Offset(&_Offset), Value(_Value) {}
292 uint64_t getMaxFileSize() const {
293 // FIXME: This doesn't make much sense.
297 const MCExpr &getOffset() const { return *Offset; }
299 uint8_t getValue() const { return Value; }
303 static bool classof(const MCFragment *F) {
304 return F->getKind() == MCFragment::FT_Org;
306 static bool classof(const MCOrgFragment *) { return true; }
311 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
312 /// but requires no physical space in the object file.
313 class MCZeroFillFragment : public MCFragment {
314 /// Size - The size of this fragment.
317 /// Alignment - The alignment for this fragment.
321 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
322 : MCFragment(FT_ZeroFill, SD),
323 Size(_Size), Alignment(_Alignment) {}
328 uint64_t getMaxFileSize() const {
329 // FIXME: This also doesn't make much sense, this method is misnamed.
333 uint64_t getSize() const { return Size; }
335 unsigned getAlignment() const { return Alignment; }
339 static bool classof(const MCFragment *F) {
340 return F->getKind() == MCFragment::FT_ZeroFill;
342 static bool classof(const MCZeroFillFragment *) { return true; }
347 // FIXME: Should this be a separate class, or just merged into MCSection? Since
348 // we anticipate the fast path being through an MCAssembler, the only reason to
349 // keep it out is for API abstraction.
350 class MCSectionData : public ilist_node<MCSectionData> {
351 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
352 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
355 typedef iplist<MCFragment> FragmentListType;
357 typedef FragmentListType::const_iterator const_iterator;
358 typedef FragmentListType::iterator iterator;
360 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
361 typedef FragmentListType::reverse_iterator reverse_iterator;
364 iplist<MCFragment> Fragments;
365 const MCSection *Section;
367 /// Alignment - The maximum alignment seen in this section.
370 /// @name Assembler Backend Data
373 // FIXME: This could all be kept private to the assembler implementation.
375 /// Address - The computed address of this section. This is ~0 until
379 /// Size - The content size of this section. This is ~0 until initialized.
382 /// FileSize - The size of this section in the object file. This is ~0 until
386 /// HasInstructions - Whether this section has had instructions emitted into
388 unsigned HasInstructions : 1;
393 // Only for use as sentinel.
395 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
397 const MCSection &getSection() const { return *Section; }
399 unsigned getAlignment() const { return Alignment; }
400 void setAlignment(unsigned Value) { Alignment = Value; }
402 /// @name Fragment Access
405 const FragmentListType &getFragmentList() const { return Fragments; }
406 FragmentListType &getFragmentList() { return Fragments; }
408 iterator begin() { return Fragments.begin(); }
409 const_iterator begin() const { return Fragments.begin(); }
411 iterator end() { return Fragments.end(); }
412 const_iterator end() const { return Fragments.end(); }
414 reverse_iterator rbegin() { return Fragments.rbegin(); }
415 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
417 reverse_iterator rend() { return Fragments.rend(); }
418 const_reverse_iterator rend() const { return Fragments.rend(); }
420 size_t size() const { return Fragments.size(); }
422 bool empty() const { return Fragments.empty(); }
425 /// @name Assembler Backend Support
428 // FIXME: This could all be kept private to the assembler implementation.
430 uint64_t getAddress() const {
431 assert(Address != ~UINT64_C(0) && "Address not set!");
434 void setAddress(uint64_t Value) { Address = Value; }
436 uint64_t getSize() const {
437 assert(Size != ~UINT64_C(0) && "File size not set!");
440 void setSize(uint64_t Value) { Size = Value; }
442 uint64_t getFileSize() const {
443 assert(FileSize != ~UINT64_C(0) && "File size not set!");
446 void setFileSize(uint64_t Value) { FileSize = Value; }
448 bool hasInstructions() const { return HasInstructions; }
449 void setHasInstructions(bool Value) { HasInstructions = Value; }
456 // FIXME: Same concerns as with SectionData.
457 class MCSymbolData : public ilist_node<MCSymbolData> {
459 const MCSymbol *Symbol;
461 /// Fragment - The fragment this symbol's value is relative to, if any.
462 MCFragment *Fragment;
464 /// Offset - The offset to apply to the fragment address to form this symbol's
468 /// IsExternal - True if this symbol is visible outside this translation
470 unsigned IsExternal : 1;
472 /// IsPrivateExtern - True if this symbol is private extern.
473 unsigned IsPrivateExtern : 1;
475 /// CommonSize - The size of the symbol, if it is 'common', or 0.
477 // FIXME: Pack this in with other fields? We could put it in offset, since a
478 // common symbol can never get a definition.
481 /// CommonAlign - The alignment of the symbol, if it is 'common'.
483 // FIXME: Pack this in with other fields?
484 unsigned CommonAlign;
486 /// Flags - The Flags field is used by object file implementations to store
487 /// additional per symbol information which is not easily classified.
490 /// Index - Index field, for use by the object file implementation.
494 // Only for use as sentinel.
496 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
502 const MCSymbol &getSymbol() const { return *Symbol; }
504 MCFragment *getFragment() const { return Fragment; }
505 void setFragment(MCFragment *Value) { Fragment = Value; }
507 uint64_t getOffset() const { return Offset; }
508 void setOffset(uint64_t Value) { Offset = Value; }
511 /// @name Symbol Attributes
514 bool isExternal() const { return IsExternal; }
515 void setExternal(bool Value) { IsExternal = Value; }
517 bool isPrivateExtern() const { return IsPrivateExtern; }
518 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
520 /// isCommon - Is this a 'common' symbol.
521 bool isCommon() const { return CommonSize != 0; }
523 /// setCommon - Mark this symbol as being 'common'.
525 /// \param Size - The size of the symbol.
526 /// \param Align - The alignment of the symbol.
527 void setCommon(uint64_t Size, unsigned Align) {
532 /// getCommonSize - Return the size of a 'common' symbol.
533 uint64_t getCommonSize() const {
534 assert(isCommon() && "Not a 'common' symbol!");
538 /// getCommonAlignment - Return the alignment of a 'common' symbol.
539 unsigned getCommonAlignment() const {
540 assert(isCommon() && "Not a 'common' symbol!");
544 /// getFlags - Get the (implementation defined) symbol flags.
545 uint32_t getFlags() const { return Flags; }
547 /// setFlags - Set the (implementation defined) symbol flags.
548 void setFlags(uint32_t Value) { Flags = Value; }
550 /// getIndex - Get the (implementation defined) index.
551 uint64_t getIndex() const { return Index; }
553 /// setIndex - Set the (implementation defined) index.
554 void setIndex(uint64_t Value) { Index = Value; }
561 // FIXME: This really doesn't belong here. See comments below.
562 struct IndirectSymbolData {
564 MCSectionData *SectionData;
569 typedef iplist<MCSectionData> SectionDataListType;
570 typedef iplist<MCSymbolData> SymbolDataListType;
572 typedef SectionDataListType::const_iterator const_iterator;
573 typedef SectionDataListType::iterator iterator;
575 typedef SymbolDataListType::const_iterator const_symbol_iterator;
576 typedef SymbolDataListType::iterator symbol_iterator;
578 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
581 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
582 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
586 TargetAsmBackend &Backend;
590 iplist<MCSectionData> Sections;
592 iplist<MCSymbolData> Symbols;
594 /// The map of sections to their associated assembler backend data.
596 // FIXME: Avoid this indirection?
597 DenseMap<const MCSection*, MCSectionData*> SectionMap;
599 /// The map of symbols to their associated assembler backend data.
601 // FIXME: Avoid this indirection?
602 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
604 std::vector<IndirectSymbolData> IndirectSymbols;
606 unsigned SubsectionsViaSymbols : 1;
609 /// LayoutSection - Assign offsets and sizes to the fragments in the section
610 /// \arg SD, and update the section size. The section file offset should
611 /// already have been computed.
612 void LayoutSection(MCSectionData &SD);
615 /// Construct a new assembler instance.
617 /// \arg OS - The stream to output to.
619 // FIXME: How are we going to parameterize this? Two obvious options are stay
620 // concrete and require clients to pass in a target like object. The other
621 // option is to make this abstract, and have targets provide concrete
622 // implementations as we do with AsmParser.
623 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS);
626 MCContext &getContext() const { return Context; }
628 /// Finish - Do final processing and write the object to the output stream.
631 // FIXME: This does not belong here.
632 bool getSubsectionsViaSymbols() const {
633 return SubsectionsViaSymbols;
635 void setSubsectionsViaSymbols(bool Value) {
636 SubsectionsViaSymbols = Value;
639 /// @name Section List Access
642 const SectionDataListType &getSectionList() const { return Sections; }
643 SectionDataListType &getSectionList() { return Sections; }
645 iterator begin() { return Sections.begin(); }
646 const_iterator begin() const { return Sections.begin(); }
648 iterator end() { return Sections.end(); }
649 const_iterator end() const { return Sections.end(); }
651 size_t size() const { return Sections.size(); }
654 /// @name Symbol List Access
657 const SymbolDataListType &getSymbolList() const { return Symbols; }
658 SymbolDataListType &getSymbolList() { return Symbols; }
660 symbol_iterator symbol_begin() { return Symbols.begin(); }
661 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
663 symbol_iterator symbol_end() { return Symbols.end(); }
664 const_symbol_iterator symbol_end() const { return Symbols.end(); }
666 size_t symbol_size() const { return Symbols.size(); }
669 /// @name Indirect Symbol List Access
672 // FIXME: This is a total hack, this should not be here. Once things are
673 // factored so that the streamer has direct access to the .o writer, it can
675 std::vector<IndirectSymbolData> &getIndirectSymbols() {
676 return IndirectSymbols;
679 indirect_symbol_iterator indirect_symbol_begin() {
680 return IndirectSymbols.begin();
683 indirect_symbol_iterator indirect_symbol_end() {
684 return IndirectSymbols.end();
687 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
690 /// @name Backend Data Access
693 MCSectionData &getSectionData(const MCSection &Section) {
694 MCSectionData *&Entry = SectionMap[&Section];
695 assert(Entry && "Missing section data!");
699 MCSectionData &getOrCreateSectionData(const MCSection &Section,
701 MCSectionData *&Entry = SectionMap[&Section];
703 if (Created) *Created = !Entry;
705 Entry = new MCSectionData(Section, this);
710 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
711 MCSymbolData *&Entry = SymbolMap[&Symbol];
712 assert(Entry && "Missing symbol data!");
716 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
718 MCSymbolData *&Entry = SymbolMap[&Symbol];
720 if (Created) *Created = !Entry;
722 Entry = new MCSymbolData(Symbol, 0, 0, this);
732 } // end namespace llvm