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/MC/MCValue.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/DataTypes.h"
19 #include <vector> // FIXME: Shouldn't be needed.
28 class MCFragment : public ilist_node<MCFragment> {
29 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
30 void operator=(const MCFragment&); // DO NOT IMPLEMENT
44 /// Parent - The data for the section this fragment is in.
45 MCSectionData *Parent;
47 /// @name Assembler Backend Data
50 // FIXME: This could all be kept private to the assembler implementation.
52 /// Offset - The offset of this fragment in its section. This is ~0 until
56 /// FileSize - The file size of this section. This is ~0 until initialized.
62 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
67 virtual ~MCFragment();
69 FragmentType getKind() const { return Kind; }
71 MCSectionData *getParent() const { return Parent; }
72 void setParent(MCSectionData *Value) { Parent = Value; }
74 // FIXME: This should be abstract, fix sentinel.
75 virtual uint64_t getMaxFileSize() const {
76 assert(0 && "Invalid getMaxFileSize call!");
80 /// @name Assembler Backend Support
83 // FIXME: This could all be kept private to the assembler implementation.
85 uint64_t getAddress() const;
87 uint64_t getFileSize() const {
88 assert(FileSize != ~UINT64_C(0) && "File size not set!");
91 void setFileSize(uint64_t Value) {
92 assert(Value <= getMaxFileSize() && "Invalid file size!");
96 uint64_t getOffset() const {
97 assert(Offset != ~UINT64_C(0) && "File offset not set!");
100 void setOffset(uint64_t Value) { Offset = Value; }
104 static bool classof(const MCFragment *O) { return true; }
107 class MCDataFragment : public MCFragment {
108 SmallString<32> Contents;
111 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
116 uint64_t getMaxFileSize() const {
117 return Contents.size();
120 SmallString<32> &getContents() { return Contents; }
121 const SmallString<32> &getContents() const { return Contents; }
125 static bool classof(const MCFragment *F) {
126 return F->getKind() == MCFragment::FT_Data;
128 static bool classof(const MCDataFragment *) { return true; }
131 class MCAlignFragment : public MCFragment {
132 /// Alignment - The alignment to ensure, in bytes.
135 /// Value - Value to use for filling padding bytes.
138 /// ValueSize - The size of the integer (in bytes) of \arg Value.
141 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
142 /// cannot be satisfied in this width then this fragment is ignored.
143 unsigned MaxBytesToEmit;
146 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
147 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
148 : MCFragment(FT_Align, SD), Alignment(_Alignment),
149 Value(_Value),ValueSize(_ValueSize),
150 MaxBytesToEmit(_MaxBytesToEmit) {}
155 uint64_t getMaxFileSize() const {
156 return std::max(Alignment - 1, MaxBytesToEmit);
159 unsigned getAlignment() const { return Alignment; }
161 int64_t getValue() const { return Value; }
163 unsigned getValueSize() const { return ValueSize; }
165 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
169 static bool classof(const MCFragment *F) {
170 return F->getKind() == MCFragment::FT_Align;
172 static bool classof(const MCAlignFragment *) { return true; }
175 class MCFillFragment : public MCFragment {
176 /// Value - Value to use for filling bytes.
179 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
182 /// Count - The number of copies of \arg Value to insert.
186 MCFillFragment(MCValue _Value, unsigned _ValueSize, uint64_t _Count,
187 MCSectionData *SD = 0)
188 : MCFragment(FT_Fill, SD),
189 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
194 uint64_t getMaxFileSize() const {
195 return ValueSize * Count;
198 MCValue getValue() const { return Value; }
200 unsigned getValueSize() const { return ValueSize; }
202 uint64_t getCount() const { return Count; }
206 static bool classof(const MCFragment *F) {
207 return F->getKind() == MCFragment::FT_Fill;
209 static bool classof(const MCFillFragment *) { return true; }
212 class MCOrgFragment : public MCFragment {
213 /// Offset - The offset this fragment should start at.
216 /// Value - Value to use for filling bytes.
220 MCOrgFragment(MCValue _Offset, int8_t _Value, MCSectionData *SD = 0)
221 : MCFragment(FT_Org, SD),
222 Offset(_Offset), Value(_Value) {}
227 uint64_t getMaxFileSize() const {
228 // FIXME: This doesn't make much sense.
232 MCValue getOffset() const { return Offset; }
234 uint8_t getValue() const { return Value; }
238 static bool classof(const MCFragment *F) {
239 return F->getKind() == MCFragment::FT_Org;
241 static bool classof(const MCOrgFragment *) { return true; }
244 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
245 /// but requires no physical space in the object file.
246 class MCZeroFillFragment : public MCFragment {
247 /// Size - The size of this fragment.
250 /// Alignment - The alignment for this fragment.
254 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
255 : MCFragment(FT_ZeroFill, SD),
256 Size(_Size), Alignment(_Alignment) {}
261 uint64_t getMaxFileSize() const {
262 // FIXME: This also doesn't make much sense, this method is misnamed.
266 uint64_t getSize() const { return Size; }
268 unsigned getAlignment() const { return Alignment; }
272 static bool classof(const MCFragment *F) {
273 return F->getKind() == MCFragment::FT_ZeroFill;
275 static bool classof(const MCZeroFillFragment *) { return true; }
278 // FIXME: Should this be a separate class, or just merged into MCSection? Since
279 // we anticipate the fast path being through an MCAssembler, the only reason to
280 // keep it out is for API abstraction.
281 class MCSectionData : public ilist_node<MCSectionData> {
282 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
283 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
286 /// Fixup - Represent a fixed size region of bytes inside some fragment which
287 /// needs to be rewritten. This region will either be rewritten by the
288 /// assembler or cause a relocation entry to be generated.
290 /// Fragment - The fragment containing the fixup.
291 MCFragment *Fragment;
293 /// Offset - The offset inside the fragment which needs to be rewritten.
296 /// Value - The expression to eventually write into the fragment.
298 // FIXME: We could probably get away with requiring the client to pass in an
299 // owned reference whose lifetime extends past that of the fixup.
302 /// Size - The fixup size.
305 /// FixedValue - The value to replace the fix up by.
307 // FIXME: This should not be here.
311 Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCValue &_Value,
313 : Fragment(&_Fragment), Offset(_Offset), Value(_Value), Size(_Size),
317 typedef iplist<MCFragment> FragmentListType;
319 typedef FragmentListType::const_iterator const_iterator;
320 typedef FragmentListType::iterator iterator;
322 typedef std::vector<Fixup>::const_iterator const_fixup_iterator;
323 typedef std::vector<Fixup>::iterator fixup_iterator;
326 iplist<MCFragment> Fragments;
327 const MCSection *Section;
329 /// Alignment - The maximum alignment seen in this section.
332 /// @name Assembler Backend Data
335 // FIXME: This could all be kept private to the assembler implementation.
337 /// Address - The computed address of this section. This is ~0 until
341 /// Size - The content size of this section. This is ~0 until initialized.
344 /// FileSize - The size of this section in the object file. This is ~0 until
348 /// LastFixupLookup - Cache for the last looked up fixup.
349 mutable unsigned LastFixupLookup;
351 /// Fixups - The list of fixups in this section.
352 std::vector<Fixup> Fixups;
357 // Only for use as sentinel.
359 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
361 const MCSection &getSection() const { return *Section; }
363 unsigned getAlignment() const { return Alignment; }
364 void setAlignment(unsigned Value) { Alignment = Value; }
366 /// @name Fragment Access
369 const FragmentListType &getFragmentList() const { return Fragments; }
370 FragmentListType &getFragmentList() { return Fragments; }
372 iterator begin() { return Fragments.begin(); }
373 const_iterator begin() const { return Fragments.begin(); }
375 iterator end() { return Fragments.end(); }
376 const_iterator end() const { return Fragments.end(); }
378 size_t size() const { return Fragments.size(); }
380 bool empty() const { return Fragments.empty(); }
383 /// @name Fixup Access
386 std::vector<Fixup> &getFixups() {
390 fixup_iterator fixup_begin() {
391 return Fixups.begin();
394 fixup_iterator fixup_end() {
398 size_t fixup_size() const { return Fixups.size(); }
401 /// @name Assembler Backend Support
404 // FIXME: This could all be kept private to the assembler implementation.
406 /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
409 /// If multiple fixups exist for the same fragment and offset it is undefined
410 /// which one is returned.
412 // FIXME: This isn't horribly slow in practice, but there are much nicer
413 // solutions to applying the fixups.
414 const Fixup *LookupFixup(const MCFragment *Fragment, uint64_t Offset) const;
416 uint64_t getAddress() const {
417 assert(Address != ~UINT64_C(0) && "Address not set!");
420 void setAddress(uint64_t Value) { Address = Value; }
422 uint64_t getSize() const {
423 assert(Size != ~UINT64_C(0) && "File size not set!");
426 void setSize(uint64_t Value) { Size = Value; }
428 uint64_t getFileSize() const {
429 assert(FileSize != ~UINT64_C(0) && "File size not set!");
432 void setFileSize(uint64_t Value) { FileSize = Value; }
437 // FIXME: Same concerns as with SectionData.
438 class MCSymbolData : public ilist_node<MCSymbolData> {
442 /// Fragment - The fragment this symbol's value is relative to, if any.
443 MCFragment *Fragment;
445 /// Offset - The offset to apply to the fragment address to form this symbol's
449 /// IsExternal - True if this symbol is visible outside this translation
451 unsigned IsExternal : 1;
453 /// IsPrivateExtern - True if this symbol is private extern.
454 unsigned IsPrivateExtern : 1;
456 /// CommonSize - The size of the symbol, if it is 'common', or 0.
458 // FIXME: Pack this in with other fields? We could put it in offset, since a
459 // common symbol can never get a definition.
462 /// CommonAlign - The alignment of the symbol, if it is 'common'.
464 // FIXME: Pack this in with other fields?
465 unsigned CommonAlign;
467 /// Flags - The Flags field is used by object file implementations to store
468 /// additional per symbol information which is not easily classified.
471 /// Index - Index field, for use by the object file implementation.
475 // Only for use as sentinel.
477 MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
483 MCSymbol &getSymbol() const { return Symbol; }
485 MCFragment *getFragment() const { return Fragment; }
486 void setFragment(MCFragment *Value) { Fragment = Value; }
488 uint64_t getOffset() const { return Offset; }
489 void setOffset(uint64_t Value) { Offset = Value; }
492 /// @name Symbol Attributes
495 bool isExternal() const { return IsExternal; }
496 void setExternal(bool Value) { IsExternal = Value; }
498 bool isPrivateExtern() const { return IsPrivateExtern; }
499 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
501 /// isCommon - Is this a 'common' symbol.
502 bool isCommon() const { return CommonSize != 0; }
504 /// setCommon - Mark this symbol as being 'common'.
506 /// \param Size - The size of the symbol.
507 /// \param Align - The alignment of the symbol.
508 void setCommon(uint64_t Size, unsigned Align) {
513 /// getCommonSize - Return the size of a 'common' symbol.
514 uint64_t getCommonSize() const {
515 assert(isCommon() && "Not a 'common' symbol!");
519 /// getCommonAlignment - Return the alignment of a 'common' symbol.
520 unsigned getCommonAlignment() const {
521 assert(isCommon() && "Not a 'common' symbol!");
525 /// getFlags - Get the (implementation defined) symbol flags.
526 uint32_t getFlags() const { return Flags; }
528 /// setFlags - Set the (implementation defined) symbol flags.
529 void setFlags(uint32_t Value) { Flags = Value; }
531 /// getIndex - Get the (implementation defined) index.
532 uint64_t getIndex() const { return Index; }
534 /// setIndex - Set the (implementation defined) index.
535 void setIndex(uint64_t Value) { Index = Value; }
540 // FIXME: This really doesn't belong here. See comments below.
541 struct IndirectSymbolData {
543 MCSectionData *SectionData;
548 typedef iplist<MCSectionData> SectionDataListType;
549 typedef iplist<MCSymbolData> SymbolDataListType;
551 typedef SectionDataListType::const_iterator const_iterator;
552 typedef SectionDataListType::iterator iterator;
554 typedef SymbolDataListType::const_iterator const_symbol_iterator;
555 typedef SymbolDataListType::iterator symbol_iterator;
557 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
560 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
561 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
567 iplist<MCSectionData> Sections;
569 iplist<MCSymbolData> Symbols;
571 std::vector<IndirectSymbolData> IndirectSymbols;
573 unsigned SubsectionsViaSymbols : 1;
576 /// LayoutSection - Assign offsets and sizes to the fragments in the section
577 /// \arg SD, and update the section size. The section file offset should
578 /// already have been computed.
579 void LayoutSection(MCSectionData &SD);
582 /// Construct a new assembler instance.
584 /// \arg OS - The stream to output to.
586 // FIXME: How are we going to parameterize this? Two obvious options are stay
587 // concrete and require clients to pass in a target like object. The other
588 // option is to make this abstract, and have targets provide concrete
589 // implementations as we do with AsmParser.
590 MCAssembler(MCContext &_Context, raw_ostream &OS);
593 MCContext &getContext() const { return Context; }
595 /// Finish - Do final processing and write the object to the output stream.
598 // FIXME: This does not belong here.
599 bool getSubsectionsViaSymbols() const {
600 return SubsectionsViaSymbols;
602 void setSubsectionsViaSymbols(bool Value) {
603 SubsectionsViaSymbols = Value;
606 /// @name Section List Access
609 const SectionDataListType &getSectionList() const { return Sections; }
610 SectionDataListType &getSectionList() { return Sections; }
612 iterator begin() { return Sections.begin(); }
613 const_iterator begin() const { return Sections.begin(); }
615 iterator end() { return Sections.end(); }
616 const_iterator end() const { return Sections.end(); }
618 size_t size() const { return Sections.size(); }
621 /// @name Symbol List Access
624 const SymbolDataListType &getSymbolList() const { return Symbols; }
625 SymbolDataListType &getSymbolList() { return Symbols; }
627 symbol_iterator symbol_begin() { return Symbols.begin(); }
628 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
630 symbol_iterator symbol_end() { return Symbols.end(); }
631 const_symbol_iterator symbol_end() const { return Symbols.end(); }
633 size_t symbol_size() const { return Symbols.size(); }
636 /// @name Indirect Symbol List Access
639 // FIXME: This is a total hack, this should not be here. Once things are
640 // factored so that the streamer has direct access to the .o writer, it can
642 std::vector<IndirectSymbolData> &getIndirectSymbols() {
643 return IndirectSymbols;
646 indirect_symbol_iterator indirect_symbol_begin() {
647 return IndirectSymbols.begin();
650 indirect_symbol_iterator indirect_symbol_end() {
651 return IndirectSymbols.end();
654 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
659 } // end namespace llvm