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;
355 // Only for use as sentinel.
357 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
359 const MCSection &getSection() const { return *Section; }
361 unsigned getAlignment() const { return Alignment; }
362 void setAlignment(unsigned Value) { Alignment = Value; }
364 /// @name Fragment Access
367 const FragmentListType &getFragmentList() const { return Fragments; }
368 FragmentListType &getFragmentList() { return Fragments; }
370 iterator begin() { return Fragments.begin(); }
371 const_iterator begin() const { return Fragments.begin(); }
373 iterator end() { return Fragments.end(); }
374 const_iterator end() const { return Fragments.end(); }
376 size_t size() const { return Fragments.size(); }
378 bool empty() const { return Fragments.empty(); }
381 /// @name Fixup Access
384 std::vector<Fixup> &getFixups() {
388 fixup_iterator fixup_begin() {
389 return Fixups.begin();
392 fixup_iterator fixup_end() {
396 size_t fixup_size() const { return Fixups.size(); }
399 /// @name Assembler Backend Support
402 // FIXME: This could all be kept private to the assembler implementation.
404 /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
407 /// If multiple fixups exist for the same fragment and offset it is undefined
408 /// which one is returned.
410 // FIXME: This isn't horribly slow in practice, but there are much nicer
411 // solutions to applying the fixups.
412 const Fixup *LookupFixup(const MCFragment *Fragment, uint64_t Offset) const;
414 uint64_t getAddress() const {
415 assert(Address != ~UINT64_C(0) && "Address not set!");
418 void setAddress(uint64_t Value) { Address = Value; }
420 uint64_t getSize() const {
421 assert(Size != ~UINT64_C(0) && "File size not set!");
424 void setSize(uint64_t Value) { Size = Value; }
426 uint64_t getFileSize() const {
427 assert(FileSize != ~UINT64_C(0) && "File size not set!");
430 void setFileSize(uint64_t Value) { FileSize = Value; }
435 // FIXME: Same concerns as with SectionData.
436 class MCSymbolData : public ilist_node<MCSymbolData> {
438 const MCSymbol *Symbol;
440 /// Fragment - The fragment this symbol's value is relative to, if any.
441 MCFragment *Fragment;
443 /// Offset - The offset to apply to the fragment address to form this symbol's
447 /// IsExternal - True if this symbol is visible outside this translation
449 unsigned IsExternal : 1;
451 /// IsPrivateExtern - True if this symbol is private extern.
452 unsigned IsPrivateExtern : 1;
454 /// CommonSize - The size of the symbol, if it is 'common', or 0.
456 // FIXME: Pack this in with other fields? We could put it in offset, since a
457 // common symbol can never get a definition.
460 /// CommonAlign - The alignment of the symbol, if it is 'common'.
462 // FIXME: Pack this in with other fields?
463 unsigned CommonAlign;
465 /// Flags - The Flags field is used by object file implementations to store
466 /// additional per symbol information which is not easily classified.
469 /// Index - Index field, for use by the object file implementation.
473 // Only for use as sentinel.
475 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
481 const MCSymbol &getSymbol() const { return *Symbol; }
483 MCFragment *getFragment() const { return Fragment; }
484 void setFragment(MCFragment *Value) { Fragment = Value; }
486 uint64_t getOffset() const { return Offset; }
487 void setOffset(uint64_t Value) { Offset = Value; }
490 /// @name Symbol Attributes
493 bool isExternal() const { return IsExternal; }
494 void setExternal(bool Value) { IsExternal = Value; }
496 bool isPrivateExtern() const { return IsPrivateExtern; }
497 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
499 /// isCommon - Is this a 'common' symbol.
500 bool isCommon() const { return CommonSize != 0; }
502 /// setCommon - Mark this symbol as being 'common'.
504 /// \param Size - The size of the symbol.
505 /// \param Align - The alignment of the symbol.
506 void setCommon(uint64_t Size, unsigned Align) {
511 /// getCommonSize - Return the size of a 'common' symbol.
512 uint64_t getCommonSize() const {
513 assert(isCommon() && "Not a 'common' symbol!");
517 /// getCommonAlignment - Return the alignment of a 'common' symbol.
518 unsigned getCommonAlignment() const {
519 assert(isCommon() && "Not a 'common' symbol!");
523 /// getFlags - Get the (implementation defined) symbol flags.
524 uint32_t getFlags() const { return Flags; }
526 /// setFlags - Set the (implementation defined) symbol flags.
527 void setFlags(uint32_t Value) { Flags = Value; }
529 /// getIndex - Get the (implementation defined) index.
530 uint64_t getIndex() const { return Index; }
532 /// setIndex - Set the (implementation defined) index.
533 void setIndex(uint64_t Value) { Index = Value; }
538 // FIXME: This really doesn't belong here. See comments below.
539 struct IndirectSymbolData {
541 MCSectionData *SectionData;
546 typedef iplist<MCSectionData> SectionDataListType;
547 typedef iplist<MCSymbolData> SymbolDataListType;
549 typedef SectionDataListType::const_iterator const_iterator;
550 typedef SectionDataListType::iterator iterator;
552 typedef SymbolDataListType::const_iterator const_symbol_iterator;
553 typedef SymbolDataListType::iterator symbol_iterator;
555 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
558 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
559 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
565 iplist<MCSectionData> Sections;
567 iplist<MCSymbolData> Symbols;
569 std::vector<IndirectSymbolData> IndirectSymbols;
571 unsigned SubsectionsViaSymbols : 1;
574 /// LayoutSection - Assign offsets and sizes to the fragments in the section
575 /// \arg SD, and update the section size. The section file offset should
576 /// already have been computed.
577 void LayoutSection(MCSectionData &SD);
580 /// Construct a new assembler instance.
582 /// \arg OS - The stream to output to.
584 // FIXME: How are we going to parameterize this? Two obvious options are stay
585 // concrete and require clients to pass in a target like object. The other
586 // option is to make this abstract, and have targets provide concrete
587 // implementations as we do with AsmParser.
588 MCAssembler(MCContext &_Context, raw_ostream &OS);
591 MCContext &getContext() const { return Context; }
593 /// Finish - Do final processing and write the object to the output stream.
596 // FIXME: This does not belong here.
597 bool getSubsectionsViaSymbols() const {
598 return SubsectionsViaSymbols;
600 void setSubsectionsViaSymbols(bool Value) {
601 SubsectionsViaSymbols = Value;
604 /// @name Section List Access
607 const SectionDataListType &getSectionList() const { return Sections; }
608 SectionDataListType &getSectionList() { return Sections; }
610 iterator begin() { return Sections.begin(); }
611 const_iterator begin() const { return Sections.begin(); }
613 iterator end() { return Sections.end(); }
614 const_iterator end() const { return Sections.end(); }
616 size_t size() const { return Sections.size(); }
619 /// @name Symbol List Access
622 const SymbolDataListType &getSymbolList() const { return Symbols; }
623 SymbolDataListType &getSymbolList() { return Symbols; }
625 symbol_iterator symbol_begin() { return Symbols.begin(); }
626 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
628 symbol_iterator symbol_end() { return Symbols.end(); }
629 const_symbol_iterator symbol_end() const { return Symbols.end(); }
631 size_t symbol_size() const { return Symbols.size(); }
634 /// @name Indirect Symbol List Access
637 // FIXME: This is a total hack, this should not be here. Once things are
638 // factored so that the streamer has direct access to the .o writer, it can
640 std::vector<IndirectSymbolData> &getIndirectSymbols() {
641 return IndirectSymbols;
644 indirect_symbol_iterator indirect_symbol_begin() {
645 return IndirectSymbols.begin();
648 indirect_symbol_iterator indirect_symbol_end() {
649 return IndirectSymbols.end();
652 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
657 } // end namespace llvm