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; }
510 uint64_t getAddress() const {
511 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
512 return getFragment()->getAddress() + getOffset();
516 /// @name Symbol Attributes
519 bool isExternal() const { return IsExternal; }
520 void setExternal(bool Value) { IsExternal = Value; }
522 bool isPrivateExtern() const { return IsPrivateExtern; }
523 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
525 /// isCommon - Is this a 'common' symbol.
526 bool isCommon() const { return CommonSize != 0; }
528 /// setCommon - Mark this symbol as being 'common'.
530 /// \param Size - The size of the symbol.
531 /// \param Align - The alignment of the symbol.
532 void setCommon(uint64_t Size, unsigned Align) {
537 /// getCommonSize - Return the size of a 'common' symbol.
538 uint64_t getCommonSize() const {
539 assert(isCommon() && "Not a 'common' symbol!");
543 /// getCommonAlignment - Return the alignment of a 'common' symbol.
544 unsigned getCommonAlignment() const {
545 assert(isCommon() && "Not a 'common' symbol!");
549 /// getFlags - Get the (implementation defined) symbol flags.
550 uint32_t getFlags() const { return Flags; }
552 /// setFlags - Set the (implementation defined) symbol flags.
553 void setFlags(uint32_t Value) { Flags = Value; }
555 /// getIndex - Get the (implementation defined) index.
556 uint64_t getIndex() const { return Index; }
558 /// setIndex - Set the (implementation defined) index.
559 void setIndex(uint64_t Value) { Index = Value; }
566 // FIXME: This really doesn't belong here. See comments below.
567 struct IndirectSymbolData {
569 MCSectionData *SectionData;
574 typedef iplist<MCSectionData> SectionDataListType;
575 typedef iplist<MCSymbolData> SymbolDataListType;
577 typedef SectionDataListType::const_iterator const_iterator;
578 typedef SectionDataListType::iterator iterator;
580 typedef SymbolDataListType::const_iterator const_symbol_iterator;
581 typedef SymbolDataListType::iterator symbol_iterator;
583 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
586 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
587 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
591 TargetAsmBackend &Backend;
595 iplist<MCSectionData> Sections;
597 iplist<MCSymbolData> Symbols;
599 /// The map of sections to their associated assembler backend data.
601 // FIXME: Avoid this indirection?
602 DenseMap<const MCSection*, MCSectionData*> SectionMap;
604 /// The map of symbols to their associated assembler backend data.
606 // FIXME: Avoid this indirection?
607 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
609 std::vector<IndirectSymbolData> IndirectSymbols;
611 unsigned SubsectionsViaSymbols : 1;
614 /// LayoutSection - Assign offsets and sizes to the fragments in the section
615 /// \arg SD, and update the section size. The section file offset should
616 /// already have been computed.
617 void LayoutSection(MCSectionData &SD);
620 /// Construct a new assembler instance.
622 /// \arg OS - The stream to output to.
624 // FIXME: How are we going to parameterize this? Two obvious options are stay
625 // concrete and require clients to pass in a target like object. The other
626 // option is to make this abstract, and have targets provide concrete
627 // implementations as we do with AsmParser.
628 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS);
631 MCContext &getContext() const { return Context; }
633 TargetAsmBackend &getBackend() const { return Backend; }
635 /// Finish - Do final processing and write the object to the output stream.
638 // FIXME: This does not belong here.
639 bool getSubsectionsViaSymbols() const {
640 return SubsectionsViaSymbols;
642 void setSubsectionsViaSymbols(bool Value) {
643 SubsectionsViaSymbols = Value;
646 /// @name Section List Access
649 const SectionDataListType &getSectionList() const { return Sections; }
650 SectionDataListType &getSectionList() { return Sections; }
652 iterator begin() { return Sections.begin(); }
653 const_iterator begin() const { return Sections.begin(); }
655 iterator end() { return Sections.end(); }
656 const_iterator end() const { return Sections.end(); }
658 size_t size() const { return Sections.size(); }
661 /// @name Symbol List Access
664 const SymbolDataListType &getSymbolList() const { return Symbols; }
665 SymbolDataListType &getSymbolList() { return Symbols; }
667 symbol_iterator symbol_begin() { return Symbols.begin(); }
668 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
670 symbol_iterator symbol_end() { return Symbols.end(); }
671 const_symbol_iterator symbol_end() const { return Symbols.end(); }
673 size_t symbol_size() const { return Symbols.size(); }
676 /// @name Indirect Symbol List Access
679 // FIXME: This is a total hack, this should not be here. Once things are
680 // factored so that the streamer has direct access to the .o writer, it can
682 std::vector<IndirectSymbolData> &getIndirectSymbols() {
683 return IndirectSymbols;
686 indirect_symbol_iterator indirect_symbol_begin() {
687 return IndirectSymbols.begin();
690 indirect_symbol_iterator indirect_symbol_end() {
691 return IndirectSymbols.end();
694 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
697 /// @name Backend Data Access
700 MCSectionData &getSectionData(const MCSection &Section) {
701 MCSectionData *&Entry = SectionMap[&Section];
702 assert(Entry && "Missing section data!");
706 MCSectionData &getOrCreateSectionData(const MCSection &Section,
708 MCSectionData *&Entry = SectionMap[&Section];
710 if (Created) *Created = !Entry;
712 Entry = new MCSectionData(Section, this);
717 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
718 MCSymbolData *&Entry = SymbolMap[&Symbol];
719 assert(Entry && "Missing symbol data!");
723 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
725 MCSymbolData *&Entry = SymbolMap[&Symbol];
727 if (Created) *Created = !Entry;
729 Entry = new MCSymbolData(Symbol, 0, 0, this);
739 } // end namespace llvm