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/SmallString.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/System/DataTypes.h"
18 #include <vector> // FIXME: Shouldn't be needed.
30 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
31 /// which needs to be rewritten. This region will either be rewritten by the
32 /// assembler or cause a relocation entry to be generated.
34 /// Offset - The offset inside the fragment which needs to be rewritten.
37 /// Value - The expression to eventually write into the fragment.
40 /// Size - The fixup size.
43 /// FixedValue - The value to replace the fix up by.
45 // FIXME: This should not be here.
49 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, unsigned _Size)
50 : Offset(_Offset), Value(&_Value), Size(_Size), FixedValue(0) {}
53 class MCFragment : public ilist_node<MCFragment> {
54 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
55 void operator=(const MCFragment&); // DO NOT IMPLEMENT
58 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
59 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
73 /// Parent - The data for the section this fragment is in.
74 MCSectionData *Parent;
76 /// @name Assembler Backend Data
79 // FIXME: This could all be kept private to the assembler implementation.
81 /// Offset - The offset of this fragment in its section. This is ~0 until
85 /// FileSize - The file size of this section. This is ~0 until initialized.
88 /// Fixups - The list of fixups in this fragment.
90 // FIXME: This should be sunk into MCDataFragment.
91 std::vector<MCAsmFixup> Fixups;
96 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
101 virtual ~MCFragment();
103 FragmentType getKind() const { return Kind; }
105 MCSectionData *getParent() const { return Parent; }
106 void setParent(MCSectionData *Value) { Parent = Value; }
108 // FIXME: This should be abstract, fix sentinel.
109 virtual uint64_t getMaxFileSize() const {
110 assert(0 && "Invalid getMaxFileSize call!");
114 /// @name Fixup Access
117 /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
120 /// If multiple fixups exist for the same fragment and offset it is undefined
121 /// which one is returned.
123 // FIXME: This isn't horribly slow in practice, but there are much nicer
124 // solutions to applying the fixups. This will be fixed by sinking fixups into
125 // data fragments exclusively.
126 const MCAsmFixup *LookupFixup(uint64_t Offset) const {
127 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
128 if (Fixups[i].Offset == Offset)
133 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
134 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
136 fixup_iterator fixup_begin() { return Fixups.begin(); }
137 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
139 fixup_iterator fixup_end() {return Fixups.end();}
140 const_fixup_iterator fixup_end() const {return Fixups.end();}
142 size_t fixup_size() const { return Fixups.size(); }
144 /// @name Assembler Backend Support
147 // FIXME: This could all be kept private to the assembler implementation.
149 uint64_t getAddress() const;
151 uint64_t getFileSize() const {
152 assert(FileSize != ~UINT64_C(0) && "File size not set!");
155 void setFileSize(uint64_t Value) {
156 assert(Value <= getMaxFileSize() && "Invalid file size!");
160 uint64_t getOffset() const {
161 assert(Offset != ~UINT64_C(0) && "File offset not set!");
164 void setOffset(uint64_t Value) { Offset = Value; }
168 static bool classof(const MCFragment *O) { return true; }
173 class MCDataFragment : public MCFragment {
174 SmallString<32> Contents;
177 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
182 uint64_t getMaxFileSize() const {
183 return Contents.size();
186 SmallString<32> &getContents() { return Contents; }
187 const SmallString<32> &getContents() const { return Contents; }
191 static bool classof(const MCFragment *F) {
192 return F->getKind() == MCFragment::FT_Data;
194 static bool classof(const MCDataFragment *) { return true; }
199 class MCAlignFragment : public MCFragment {
200 /// Alignment - The alignment to ensure, in bytes.
203 /// Value - Value to use for filling padding bytes.
206 /// ValueSize - The size of the integer (in bytes) of \arg Value.
209 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
210 /// cannot be satisfied in this width then this fragment is ignored.
211 unsigned MaxBytesToEmit;
214 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
215 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
216 : MCFragment(FT_Align, SD), Alignment(_Alignment),
217 Value(_Value),ValueSize(_ValueSize),
218 MaxBytesToEmit(_MaxBytesToEmit) {}
223 uint64_t getMaxFileSize() const {
224 return std::max(Alignment - 1, MaxBytesToEmit);
227 unsigned getAlignment() const { return Alignment; }
229 int64_t getValue() const { return Value; }
231 unsigned getValueSize() const { return ValueSize; }
233 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
237 static bool classof(const MCFragment *F) {
238 return F->getKind() == MCFragment::FT_Align;
240 static bool classof(const MCAlignFragment *) { return true; }
245 class MCFillFragment : public MCFragment {
246 /// Value - Value to use for filling bytes.
249 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
252 /// Count - The number of copies of \arg Value to insert.
256 MCFillFragment(const MCExpr &_Value, unsigned _ValueSize, uint64_t _Count,
257 MCSectionData *SD = 0)
258 : MCFragment(FT_Fill, SD),
259 Value(&_Value), ValueSize(_ValueSize), Count(_Count) {}
264 uint64_t getMaxFileSize() const {
265 return ValueSize * Count;
268 const MCExpr &getValue() const { return *Value; }
270 unsigned getValueSize() const { return ValueSize; }
272 uint64_t getCount() const { return Count; }
276 static bool classof(const MCFragment *F) {
277 return F->getKind() == MCFragment::FT_Fill;
279 static bool classof(const MCFillFragment *) { return true; }
284 class MCOrgFragment : public MCFragment {
285 /// Offset - The offset this fragment should start at.
286 const MCExpr *Offset;
288 /// Value - Value to use for filling bytes.
292 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
293 : MCFragment(FT_Org, SD),
294 Offset(&_Offset), Value(_Value) {}
299 uint64_t getMaxFileSize() const {
300 // FIXME: This doesn't make much sense.
304 const MCExpr &getOffset() const { return *Offset; }
306 uint8_t getValue() const { return Value; }
310 static bool classof(const MCFragment *F) {
311 return F->getKind() == MCFragment::FT_Org;
313 static bool classof(const MCOrgFragment *) { return true; }
318 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
319 /// but requires no physical space in the object file.
320 class MCZeroFillFragment : public MCFragment {
321 /// Size - The size of this fragment.
324 /// Alignment - The alignment for this fragment.
328 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
329 : MCFragment(FT_ZeroFill, SD),
330 Size(_Size), Alignment(_Alignment) {}
335 uint64_t getMaxFileSize() const {
336 // FIXME: This also doesn't make much sense, this method is misnamed.
340 uint64_t getSize() const { return Size; }
342 unsigned getAlignment() const { return Alignment; }
346 static bool classof(const MCFragment *F) {
347 return F->getKind() == MCFragment::FT_ZeroFill;
349 static bool classof(const MCZeroFillFragment *) { return true; }
354 // FIXME: Should this be a separate class, or just merged into MCSection? Since
355 // we anticipate the fast path being through an MCAssembler, the only reason to
356 // keep it out is for API abstraction.
357 class MCSectionData : public ilist_node<MCSectionData> {
358 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
359 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
362 typedef iplist<MCFragment> FragmentListType;
364 typedef FragmentListType::const_iterator const_iterator;
365 typedef FragmentListType::iterator iterator;
367 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
368 typedef FragmentListType::reverse_iterator reverse_iterator;
371 iplist<MCFragment> Fragments;
372 const MCSection *Section;
374 /// Alignment - The maximum alignment seen in this section.
377 /// @name Assembler Backend Data
380 // FIXME: This could all be kept private to the assembler implementation.
382 /// Address - The computed address of this section. This is ~0 until
386 /// Size - The content size of this section. This is ~0 until initialized.
389 /// FileSize - The size of this section in the object file. This is ~0 until
393 /// HasInstructions - Whether this section has had instructions emitted into
395 unsigned HasInstructions : 1;
400 // Only for use as sentinel.
402 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
404 const MCSection &getSection() const { return *Section; }
406 unsigned getAlignment() const { return Alignment; }
407 void setAlignment(unsigned Value) { Alignment = Value; }
409 /// @name Fragment Access
412 const FragmentListType &getFragmentList() const { return Fragments; }
413 FragmentListType &getFragmentList() { return Fragments; }
415 iterator begin() { return Fragments.begin(); }
416 const_iterator begin() const { return Fragments.begin(); }
418 iterator end() { return Fragments.end(); }
419 const_iterator end() const { return Fragments.end(); }
421 reverse_iterator rbegin() { return Fragments.rbegin(); }
422 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
424 reverse_iterator rend() { return Fragments.rend(); }
425 const_reverse_iterator rend() const { return Fragments.rend(); }
427 size_t size() const { return Fragments.size(); }
429 bool empty() const { return Fragments.empty(); }
432 /// @name Assembler Backend Support
435 // FIXME: This could all be kept private to the assembler implementation.
437 uint64_t getAddress() const {
438 assert(Address != ~UINT64_C(0) && "Address not set!");
441 void setAddress(uint64_t Value) { Address = Value; }
443 uint64_t getSize() const {
444 assert(Size != ~UINT64_C(0) && "File size not set!");
447 void setSize(uint64_t Value) { Size = Value; }
449 uint64_t getFileSize() const {
450 assert(FileSize != ~UINT64_C(0) && "File size not set!");
453 void setFileSize(uint64_t Value) { FileSize = Value; }
455 bool hasInstructions() const { return HasInstructions; }
456 void setHasInstructions(bool Value) { HasInstructions = Value; }
463 // FIXME: Same concerns as with SectionData.
464 class MCSymbolData : public ilist_node<MCSymbolData> {
466 const MCSymbol *Symbol;
468 /// Fragment - The fragment this symbol's value is relative to, if any.
469 MCFragment *Fragment;
471 /// Offset - The offset to apply to the fragment address to form this symbol's
475 /// IsExternal - True if this symbol is visible outside this translation
477 unsigned IsExternal : 1;
479 /// IsPrivateExtern - True if this symbol is private extern.
480 unsigned IsPrivateExtern : 1;
482 /// CommonSize - The size of the symbol, if it is 'common', or 0.
484 // FIXME: Pack this in with other fields? We could put it in offset, since a
485 // common symbol can never get a definition.
488 /// CommonAlign - The alignment of the symbol, if it is 'common'.
490 // FIXME: Pack this in with other fields?
491 unsigned CommonAlign;
493 /// Flags - The Flags field is used by object file implementations to store
494 /// additional per symbol information which is not easily classified.
497 /// Index - Index field, for use by the object file implementation.
501 // Only for use as sentinel.
503 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
509 const MCSymbol &getSymbol() const { return *Symbol; }
511 MCFragment *getFragment() const { return Fragment; }
512 void setFragment(MCFragment *Value) { Fragment = Value; }
514 uint64_t getOffset() const { return Offset; }
515 void setOffset(uint64_t Value) { Offset = Value; }
518 /// @name Symbol Attributes
521 bool isExternal() const { return IsExternal; }
522 void setExternal(bool Value) { IsExternal = Value; }
524 bool isPrivateExtern() const { return IsPrivateExtern; }
525 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
527 /// isCommon - Is this a 'common' symbol.
528 bool isCommon() const { return CommonSize != 0; }
530 /// setCommon - Mark this symbol as being 'common'.
532 /// \param Size - The size of the symbol.
533 /// \param Align - The alignment of the symbol.
534 void setCommon(uint64_t Size, unsigned Align) {
539 /// getCommonSize - Return the size of a 'common' symbol.
540 uint64_t getCommonSize() const {
541 assert(isCommon() && "Not a 'common' symbol!");
545 /// getCommonAlignment - Return the alignment of a 'common' symbol.
546 unsigned getCommonAlignment() const {
547 assert(isCommon() && "Not a 'common' symbol!");
551 /// getFlags - Get the (implementation defined) symbol flags.
552 uint32_t getFlags() const { return Flags; }
554 /// setFlags - Set the (implementation defined) symbol flags.
555 void setFlags(uint32_t Value) { Flags = Value; }
557 /// getIndex - Get the (implementation defined) index.
558 uint64_t getIndex() const { return Index; }
560 /// setIndex - Set the (implementation defined) index.
561 void setIndex(uint64_t Value) { Index = Value; }
568 // FIXME: This really doesn't belong here. See comments below.
569 struct IndirectSymbolData {
571 MCSectionData *SectionData;
576 typedef iplist<MCSectionData> SectionDataListType;
577 typedef iplist<MCSymbolData> SymbolDataListType;
579 typedef SectionDataListType::const_iterator const_iterator;
580 typedef SectionDataListType::iterator iterator;
582 typedef SymbolDataListType::const_iterator const_symbol_iterator;
583 typedef SymbolDataListType::iterator symbol_iterator;
585 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
588 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
589 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
595 iplist<MCSectionData> Sections;
597 iplist<MCSymbolData> Symbols;
599 std::vector<IndirectSymbolData> IndirectSymbols;
601 unsigned SubsectionsViaSymbols : 1;
604 /// LayoutSection - Assign offsets and sizes to the fragments in the section
605 /// \arg SD, and update the section size. The section file offset should
606 /// already have been computed.
607 void LayoutSection(MCSectionData &SD);
610 /// Construct a new assembler instance.
612 /// \arg OS - The stream to output to.
614 // FIXME: How are we going to parameterize this? Two obvious options are stay
615 // concrete and require clients to pass in a target like object. The other
616 // option is to make this abstract, and have targets provide concrete
617 // implementations as we do with AsmParser.
618 MCAssembler(MCContext &_Context, raw_ostream &OS);
621 MCContext &getContext() const { return Context; }
623 /// Finish - Do final processing and write the object to the output stream.
626 // FIXME: This does not belong here.
627 bool getSubsectionsViaSymbols() const {
628 return SubsectionsViaSymbols;
630 void setSubsectionsViaSymbols(bool Value) {
631 SubsectionsViaSymbols = Value;
634 /// @name Section List Access
637 const SectionDataListType &getSectionList() const { return Sections; }
638 SectionDataListType &getSectionList() { return Sections; }
640 iterator begin() { return Sections.begin(); }
641 const_iterator begin() const { return Sections.begin(); }
643 iterator end() { return Sections.end(); }
644 const_iterator end() const { return Sections.end(); }
646 size_t size() const { return Sections.size(); }
649 /// @name Symbol List Access
652 const SymbolDataListType &getSymbolList() const { return Symbols; }
653 SymbolDataListType &getSymbolList() { return Symbols; }
655 symbol_iterator symbol_begin() { return Symbols.begin(); }
656 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
658 symbol_iterator symbol_end() { return Symbols.end(); }
659 const_symbol_iterator symbol_end() const { return Symbols.end(); }
661 size_t symbol_size() const { return Symbols.size(); }
664 /// @name Indirect Symbol List Access
667 // FIXME: This is a total hack, this should not be here. Once things are
668 // factored so that the streamer has direct access to the .o writer, it can
670 std::vector<IndirectSymbolData> &getIndirectSymbols() {
671 return IndirectSymbols;
674 indirect_symbol_iterator indirect_symbol_begin() {
675 return IndirectSymbols.begin();
678 indirect_symbol_iterator indirect_symbol_end() {
679 return IndirectSymbols.end();
682 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
689 } // end namespace llvm