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.
29 class MCFragment : public ilist_node<MCFragment> {
30 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
31 void operator=(const MCFragment&); // DO NOT IMPLEMENT
45 /// Parent - The data for the section this fragment is in.
46 MCSectionData *Parent;
48 /// @name Assembler Backend Data
51 // FIXME: This could all be kept private to the assembler implementation.
53 /// Offset - The offset of this fragment in its section. This is ~0 until
57 /// FileSize - The file size of this section. This is ~0 until initialized.
63 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
68 virtual ~MCFragment();
70 FragmentType getKind() const { return Kind; }
72 MCSectionData *getParent() const { return Parent; }
73 void setParent(MCSectionData *Value) { Parent = Value; }
75 // FIXME: This should be abstract, fix sentinel.
76 virtual uint64_t getMaxFileSize() const {
77 assert(0 && "Invalid getMaxFileSize call!");
81 /// @name Assembler Backend Support
84 // FIXME: This could all be kept private to the assembler implementation.
86 uint64_t getAddress() const;
88 uint64_t getFileSize() const {
89 assert(FileSize != ~UINT64_C(0) && "File size not set!");
92 void setFileSize(uint64_t Value) {
93 assert(Value <= getMaxFileSize() && "Invalid file size!");
97 uint64_t getOffset() const {
98 assert(Offset != ~UINT64_C(0) && "File offset not set!");
101 void setOffset(uint64_t Value) { Offset = Value; }
105 static bool classof(const MCFragment *O) { return true; }
108 class MCDataFragment : public MCFragment {
109 SmallString<32> Contents;
112 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
117 uint64_t getMaxFileSize() const {
118 return Contents.size();
121 SmallString<32> &getContents() { return Contents; }
122 const SmallString<32> &getContents() const { return Contents; }
126 static bool classof(const MCFragment *F) {
127 return F->getKind() == MCFragment::FT_Data;
129 static bool classof(const MCDataFragment *) { return true; }
132 class MCAlignFragment : public MCFragment {
133 /// Alignment - The alignment to ensure, in bytes.
136 /// Value - Value to use for filling padding bytes.
139 /// ValueSize - The size of the integer (in bytes) of \arg Value.
142 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
143 /// cannot be satisfied in this width then this fragment is ignored.
144 unsigned MaxBytesToEmit;
147 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
148 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
149 : MCFragment(FT_Align, SD), Alignment(_Alignment),
150 Value(_Value),ValueSize(_ValueSize),
151 MaxBytesToEmit(_MaxBytesToEmit) {}
156 uint64_t getMaxFileSize() const {
157 return std::max(Alignment - 1, MaxBytesToEmit);
160 unsigned getAlignment() const { return Alignment; }
162 int64_t getValue() const { return Value; }
164 unsigned getValueSize() const { return ValueSize; }
166 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
170 static bool classof(const MCFragment *F) {
171 return F->getKind() == MCFragment::FT_Align;
173 static bool classof(const MCAlignFragment *) { return true; }
176 class MCFillFragment : public MCFragment {
177 /// Value - Value to use for filling bytes.
180 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
183 /// Count - The number of copies of \arg Value to insert.
187 MCFillFragment(const MCExpr &_Value, unsigned _ValueSize, uint64_t _Count,
188 MCSectionData *SD = 0)
189 : MCFragment(FT_Fill, SD),
190 Value(&_Value), ValueSize(_ValueSize), Count(_Count) {}
195 uint64_t getMaxFileSize() const {
196 return ValueSize * Count;
199 const MCExpr &getValue() const { return *Value; }
201 unsigned getValueSize() const { return ValueSize; }
203 uint64_t getCount() const { return Count; }
207 static bool classof(const MCFragment *F) {
208 return F->getKind() == MCFragment::FT_Fill;
210 static bool classof(const MCFillFragment *) { return true; }
213 class MCOrgFragment : public MCFragment {
214 /// Offset - The offset this fragment should start at.
215 const MCExpr *Offset;
217 /// Value - Value to use for filling bytes.
221 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
222 : MCFragment(FT_Org, SD),
223 Offset(&_Offset), Value(_Value) {}
228 uint64_t getMaxFileSize() const {
229 // FIXME: This doesn't make much sense.
233 const MCExpr &getOffset() const { return *Offset; }
235 uint8_t getValue() const { return Value; }
239 static bool classof(const MCFragment *F) {
240 return F->getKind() == MCFragment::FT_Org;
242 static bool classof(const MCOrgFragment *) { return true; }
245 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
246 /// but requires no physical space in the object file.
247 class MCZeroFillFragment : public MCFragment {
248 /// Size - The size of this fragment.
251 /// Alignment - The alignment for this fragment.
255 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
256 : MCFragment(FT_ZeroFill, SD),
257 Size(_Size), Alignment(_Alignment) {}
262 uint64_t getMaxFileSize() const {
263 // FIXME: This also doesn't make much sense, this method is misnamed.
267 uint64_t getSize() const { return Size; }
269 unsigned getAlignment() const { return Alignment; }
273 static bool classof(const MCFragment *F) {
274 return F->getKind() == MCFragment::FT_ZeroFill;
276 static bool classof(const MCZeroFillFragment *) { return true; }
279 // FIXME: Should this be a separate class, or just merged into MCSection? Since
280 // we anticipate the fast path being through an MCAssembler, the only reason to
281 // keep it out is for API abstraction.
282 class MCSectionData : public ilist_node<MCSectionData> {
283 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
284 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
287 /// Fixup - Represent a fixed size region of bytes inside some fragment which
288 /// needs to be rewritten. This region will either be rewritten by the
289 /// assembler or cause a relocation entry to be generated.
291 /// Fragment - The fragment containing the fixup.
292 MCFragment *Fragment;
294 /// Offset - The offset inside the fragment which needs to be rewritten.
297 /// Value - The expression to eventually write into the fragment.
300 /// Size - The fixup size.
303 /// FixedValue - The value to replace the fix up by.
305 // FIXME: This should not be here.
309 Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCExpr &_Value,
311 : Fragment(&_Fragment), Offset(_Offset), Value(&_Value), Size(_Size),
315 typedef iplist<MCFragment> FragmentListType;
317 typedef FragmentListType::const_iterator const_iterator;
318 typedef FragmentListType::iterator iterator;
320 typedef std::vector<Fixup>::const_iterator const_fixup_iterator;
321 typedef std::vector<Fixup>::iterator fixup_iterator;
324 iplist<MCFragment> Fragments;
325 const MCSection *Section;
327 /// Alignment - The maximum alignment seen in this section.
330 /// @name Assembler Backend Data
333 // FIXME: This could all be kept private to the assembler implementation.
335 /// Address - The computed address of this section. This is ~0 until
339 /// Size - The content size of this section. This is ~0 until initialized.
342 /// FileSize - The size of this section in the object file. This is ~0 until
346 /// LastFixupLookup - Cache for the last looked up fixup.
347 mutable unsigned LastFixupLookup;
349 /// Fixups - The list of fixups in this section.
350 std::vector<Fixup> Fixups;
352 /// HasInstructions - Whether this section has had instructions emitted into
354 unsigned HasInstructions : 1;
359 // Only for use as sentinel.
361 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
363 const MCSection &getSection() const { return *Section; }
365 unsigned getAlignment() const { return Alignment; }
366 void setAlignment(unsigned Value) { Alignment = Value; }
368 /// @name Fragment Access
371 const FragmentListType &getFragmentList() const { return Fragments; }
372 FragmentListType &getFragmentList() { return Fragments; }
374 iterator begin() { return Fragments.begin(); }
375 const_iterator begin() const { return Fragments.begin(); }
377 iterator end() { return Fragments.end(); }
378 const_iterator end() const { return Fragments.end(); }
380 size_t size() const { return Fragments.size(); }
382 bool empty() const { return Fragments.empty(); }
385 /// @name Fixup Access
388 std::vector<Fixup> &getFixups() {
392 fixup_iterator fixup_begin() {
393 return Fixups.begin();
396 fixup_iterator fixup_end() {
400 size_t fixup_size() const { return Fixups.size(); }
403 /// @name Assembler Backend Support
406 // FIXME: This could all be kept private to the assembler implementation.
408 /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
411 /// If multiple fixups exist for the same fragment and offset it is undefined
412 /// which one is returned.
414 // FIXME: This isn't horribly slow in practice, but there are much nicer
415 // solutions to applying the fixups.
416 const Fixup *LookupFixup(const MCFragment *Fragment, uint64_t Offset) const;
418 uint64_t getAddress() const {
419 assert(Address != ~UINT64_C(0) && "Address not set!");
422 void setAddress(uint64_t Value) { Address = Value; }
424 uint64_t getSize() const {
425 assert(Size != ~UINT64_C(0) && "File size not set!");
428 void setSize(uint64_t Value) { Size = Value; }
430 uint64_t getFileSize() const {
431 assert(FileSize != ~UINT64_C(0) && "File size not set!");
434 void setFileSize(uint64_t Value) { FileSize = Value; }
436 bool hasInstructions() const { return HasInstructions; }
437 void setHasInstructions(bool Value) { HasInstructions = Value; }
442 // FIXME: Same concerns as with SectionData.
443 class MCSymbolData : public ilist_node<MCSymbolData> {
445 const MCSymbol *Symbol;
447 /// Fragment - The fragment this symbol's value is relative to, if any.
448 MCFragment *Fragment;
450 /// Offset - The offset to apply to the fragment address to form this symbol's
454 /// IsExternal - True if this symbol is visible outside this translation
456 unsigned IsExternal : 1;
458 /// IsPrivateExtern - True if this symbol is private extern.
459 unsigned IsPrivateExtern : 1;
461 /// CommonSize - The size of the symbol, if it is 'common', or 0.
463 // FIXME: Pack this in with other fields? We could put it in offset, since a
464 // common symbol can never get a definition.
467 /// CommonAlign - The alignment of the symbol, if it is 'common'.
469 // FIXME: Pack this in with other fields?
470 unsigned CommonAlign;
472 /// Flags - The Flags field is used by object file implementations to store
473 /// additional per symbol information which is not easily classified.
476 /// Index - Index field, for use by the object file implementation.
480 // Only for use as sentinel.
482 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
488 const MCSymbol &getSymbol() const { return *Symbol; }
490 MCFragment *getFragment() const { return Fragment; }
491 void setFragment(MCFragment *Value) { Fragment = Value; }
493 uint64_t getOffset() const { return Offset; }
494 void setOffset(uint64_t Value) { Offset = Value; }
497 /// @name Symbol Attributes
500 bool isExternal() const { return IsExternal; }
501 void setExternal(bool Value) { IsExternal = Value; }
503 bool isPrivateExtern() const { return IsPrivateExtern; }
504 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
506 /// isCommon - Is this a 'common' symbol.
507 bool isCommon() const { return CommonSize != 0; }
509 /// setCommon - Mark this symbol as being 'common'.
511 /// \param Size - The size of the symbol.
512 /// \param Align - The alignment of the symbol.
513 void setCommon(uint64_t Size, unsigned Align) {
518 /// getCommonSize - Return the size of a 'common' symbol.
519 uint64_t getCommonSize() const {
520 assert(isCommon() && "Not a 'common' symbol!");
524 /// getCommonAlignment - Return the alignment of a 'common' symbol.
525 unsigned getCommonAlignment() const {
526 assert(isCommon() && "Not a 'common' symbol!");
530 /// getFlags - Get the (implementation defined) symbol flags.
531 uint32_t getFlags() const { return Flags; }
533 /// setFlags - Set the (implementation defined) symbol flags.
534 void setFlags(uint32_t Value) { Flags = Value; }
536 /// getIndex - Get the (implementation defined) index.
537 uint64_t getIndex() const { return Index; }
539 /// setIndex - Set the (implementation defined) index.
540 void setIndex(uint64_t Value) { Index = Value; }
545 // FIXME: This really doesn't belong here. See comments below.
546 struct IndirectSymbolData {
548 MCSectionData *SectionData;
553 typedef iplist<MCSectionData> SectionDataListType;
554 typedef iplist<MCSymbolData> SymbolDataListType;
556 typedef SectionDataListType::const_iterator const_iterator;
557 typedef SectionDataListType::iterator iterator;
559 typedef SymbolDataListType::const_iterator const_symbol_iterator;
560 typedef SymbolDataListType::iterator symbol_iterator;
562 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
565 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
566 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
572 iplist<MCSectionData> Sections;
574 iplist<MCSymbolData> Symbols;
576 std::vector<IndirectSymbolData> IndirectSymbols;
578 unsigned SubsectionsViaSymbols : 1;
581 /// LayoutSection - Assign offsets and sizes to the fragments in the section
582 /// \arg SD, and update the section size. The section file offset should
583 /// already have been computed.
584 void LayoutSection(MCSectionData &SD);
587 /// Construct a new assembler instance.
589 /// \arg OS - The stream to output to.
591 // FIXME: How are we going to parameterize this? Two obvious options are stay
592 // concrete and require clients to pass in a target like object. The other
593 // option is to make this abstract, and have targets provide concrete
594 // implementations as we do with AsmParser.
595 MCAssembler(MCContext &_Context, raw_ostream &OS);
598 MCContext &getContext() const { return Context; }
600 /// Finish - Do final processing and write the object to the output stream.
603 // FIXME: This does not belong here.
604 bool getSubsectionsViaSymbols() const {
605 return SubsectionsViaSymbols;
607 void setSubsectionsViaSymbols(bool Value) {
608 SubsectionsViaSymbols = Value;
611 /// @name Section List Access
614 const SectionDataListType &getSectionList() const { return Sections; }
615 SectionDataListType &getSectionList() { return Sections; }
617 iterator begin() { return Sections.begin(); }
618 const_iterator begin() const { return Sections.begin(); }
620 iterator end() { return Sections.end(); }
621 const_iterator end() const { return Sections.end(); }
623 size_t size() const { return Sections.size(); }
626 /// @name Symbol List Access
629 const SymbolDataListType &getSymbolList() const { return Symbols; }
630 SymbolDataListType &getSymbolList() { return Symbols; }
632 symbol_iterator symbol_begin() { return Symbols.begin(); }
633 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
635 symbol_iterator symbol_end() { return Symbols.end(); }
636 const_symbol_iterator symbol_end() const { return Symbols.end(); }
638 size_t symbol_size() const { return Symbols.size(); }
641 /// @name Indirect Symbol List Access
644 // FIXME: This is a total hack, this should not be here. Once things are
645 // factored so that the streamer has direct access to the .o writer, it can
647 std::vector<IndirectSymbolData> &getIndirectSymbols() {
648 return IndirectSymbols;
651 indirect_symbol_iterator indirect_symbol_begin() {
652 return IndirectSymbols.begin();
655 indirect_symbol_iterator indirect_symbol_end() {
656 return IndirectSymbols.end();
659 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
664 } // end namespace llvm