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/MC/MCFixup.h"
18 #include "llvm/System/DataTypes.h"
19 #include <vector> // FIXME: Shouldn't be needed.
31 /// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
32 /// which needs to be rewritten. This region will either be rewritten by the
33 /// assembler or cause a relocation entry to be generated.
35 /// Offset - The offset inside the fragment which needs to be rewritten.
38 /// Value - The expression to eventually write into the fragment.
41 /// Kind - The fixup kind.
44 /// FixedValue - The value to replace the fix up by.
46 // FIXME: This should not be here.
50 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
51 : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {}
54 class MCFragment : public ilist_node<MCFragment> {
55 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
56 void operator=(const MCFragment&); // DO NOT IMPLEMENT
70 /// Parent - The data for the section this fragment is in.
71 MCSectionData *Parent;
73 /// @name Assembler Backend Data
76 // FIXME: This could all be kept private to the assembler implementation.
78 /// Offset - The offset of this fragment in its section. This is ~0 until
82 /// FileSize - The file size of this section. This is ~0 until initialized.
88 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
93 virtual ~MCFragment();
95 FragmentType getKind() const { return Kind; }
97 MCSectionData *getParent() const { return Parent; }
98 void setParent(MCSectionData *Value) { Parent = Value; }
100 // FIXME: This should be abstract, fix sentinel.
101 virtual uint64_t getMaxFileSize() const {
102 assert(0 && "Invalid getMaxFileSize call!");
106 /// @name Assembler Backend Support
109 // FIXME: This could all be kept private to the assembler implementation.
111 uint64_t getAddress() const;
113 uint64_t getFileSize() const {
114 assert(FileSize != ~UINT64_C(0) && "File size not set!");
117 void setFileSize(uint64_t Value) {
118 assert(Value <= getMaxFileSize() && "Invalid file size!");
122 uint64_t getOffset() const {
123 assert(Offset != ~UINT64_C(0) && "File offset not set!");
126 void setOffset(uint64_t Value) { Offset = Value; }
130 static bool classof(const MCFragment *O) { return true; }
135 class MCDataFragment : public MCFragment {
136 SmallString<32> Contents;
138 /// Fixups - The list of fixups in this fragment.
139 std::vector<MCAsmFixup> Fixups;
142 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
143 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
146 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
151 uint64_t getMaxFileSize() const {
152 return Contents.size();
155 SmallString<32> &getContents() { return Contents; }
156 const SmallString<32> &getContents() const { return Contents; }
160 /// @name Fixup Access
163 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
164 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
166 fixup_iterator fixup_begin() { return Fixups.begin(); }
167 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
169 fixup_iterator fixup_end() {return Fixups.end();}
170 const_fixup_iterator fixup_end() const {return Fixups.end();}
172 size_t fixup_size() const { return Fixups.size(); }
176 static bool classof(const MCFragment *F) {
177 return F->getKind() == MCFragment::FT_Data;
179 static bool classof(const MCDataFragment *) { return true; }
184 class MCAlignFragment : public MCFragment {
185 /// Alignment - The alignment to ensure, in bytes.
188 /// Value - Value to use for filling padding bytes.
191 /// ValueSize - The size of the integer (in bytes) of \arg Value.
194 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
195 /// cannot be satisfied in this width then this fragment is ignored.
196 unsigned MaxBytesToEmit;
199 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
200 unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
201 : MCFragment(FT_Align, SD), Alignment(_Alignment),
202 Value(_Value),ValueSize(_ValueSize),
203 MaxBytesToEmit(_MaxBytesToEmit) {}
208 uint64_t getMaxFileSize() const {
209 return std::max(Alignment - 1, MaxBytesToEmit);
212 unsigned getAlignment() const { return Alignment; }
214 int64_t getValue() const { return Value; }
216 unsigned getValueSize() const { return ValueSize; }
218 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
222 static bool classof(const MCFragment *F) {
223 return F->getKind() == MCFragment::FT_Align;
225 static bool classof(const MCAlignFragment *) { return true; }
230 class MCFillFragment : public MCFragment {
231 /// Value - Value to use for filling bytes.
234 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
237 /// Count - The number of copies of \arg Value to insert.
241 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
242 MCSectionData *SD = 0)
243 : MCFragment(FT_Fill, SD),
244 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
249 uint64_t getMaxFileSize() const {
250 return ValueSize * Count;
253 int64_t getValue() const { return Value; }
255 unsigned getValueSize() const { return ValueSize; }
257 uint64_t getCount() const { return Count; }
261 static bool classof(const MCFragment *F) {
262 return F->getKind() == MCFragment::FT_Fill;
264 static bool classof(const MCFillFragment *) { return true; }
269 class MCOrgFragment : public MCFragment {
270 /// Offset - The offset this fragment should start at.
271 const MCExpr *Offset;
273 /// Value - Value to use for filling bytes.
277 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
278 : MCFragment(FT_Org, SD),
279 Offset(&_Offset), Value(_Value) {}
284 uint64_t getMaxFileSize() const {
285 // FIXME: This doesn't make much sense.
289 const MCExpr &getOffset() const { return *Offset; }
291 uint8_t getValue() const { return Value; }
295 static bool classof(const MCFragment *F) {
296 return F->getKind() == MCFragment::FT_Org;
298 static bool classof(const MCOrgFragment *) { return true; }
303 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
304 /// but requires no physical space in the object file.
305 class MCZeroFillFragment : public MCFragment {
306 /// Size - The size of this fragment.
309 /// Alignment - The alignment for this fragment.
313 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
314 : MCFragment(FT_ZeroFill, SD),
315 Size(_Size), Alignment(_Alignment) {}
320 uint64_t getMaxFileSize() const {
321 // FIXME: This also doesn't make much sense, this method is misnamed.
325 uint64_t getSize() const { return Size; }
327 unsigned getAlignment() const { return Alignment; }
331 static bool classof(const MCFragment *F) {
332 return F->getKind() == MCFragment::FT_ZeroFill;
334 static bool classof(const MCZeroFillFragment *) { return true; }
339 // FIXME: Should this be a separate class, or just merged into MCSection? Since
340 // we anticipate the fast path being through an MCAssembler, the only reason to
341 // keep it out is for API abstraction.
342 class MCSectionData : public ilist_node<MCSectionData> {
343 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
344 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
347 typedef iplist<MCFragment> FragmentListType;
349 typedef FragmentListType::const_iterator const_iterator;
350 typedef FragmentListType::iterator iterator;
352 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
353 typedef FragmentListType::reverse_iterator reverse_iterator;
356 iplist<MCFragment> Fragments;
357 const MCSection *Section;
359 /// Alignment - The maximum alignment seen in this section.
362 /// @name Assembler Backend Data
365 // FIXME: This could all be kept private to the assembler implementation.
367 /// Address - The computed address of this section. This is ~0 until
371 /// Size - The content size of this section. This is ~0 until initialized.
374 /// FileSize - The size of this section in the object file. This is ~0 until
378 /// HasInstructions - Whether this section has had instructions emitted into
380 unsigned HasInstructions : 1;
385 // Only for use as sentinel.
387 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
389 const MCSection &getSection() const { return *Section; }
391 unsigned getAlignment() const { return Alignment; }
392 void setAlignment(unsigned Value) { Alignment = Value; }
394 /// @name Fragment Access
397 const FragmentListType &getFragmentList() const { return Fragments; }
398 FragmentListType &getFragmentList() { return Fragments; }
400 iterator begin() { return Fragments.begin(); }
401 const_iterator begin() const { return Fragments.begin(); }
403 iterator end() { return Fragments.end(); }
404 const_iterator end() const { return Fragments.end(); }
406 reverse_iterator rbegin() { return Fragments.rbegin(); }
407 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
409 reverse_iterator rend() { return Fragments.rend(); }
410 const_reverse_iterator rend() const { return Fragments.rend(); }
412 size_t size() const { return Fragments.size(); }
414 bool empty() const { return Fragments.empty(); }
417 /// @name Assembler Backend Support
420 // FIXME: This could all be kept private to the assembler implementation.
422 uint64_t getAddress() const {
423 assert(Address != ~UINT64_C(0) && "Address not set!");
426 void setAddress(uint64_t Value) { Address = Value; }
428 uint64_t getSize() const {
429 assert(Size != ~UINT64_C(0) && "File size not set!");
432 void setSize(uint64_t Value) { Size = Value; }
434 uint64_t getFileSize() const {
435 assert(FileSize != ~UINT64_C(0) && "File size not set!");
438 void setFileSize(uint64_t Value) { FileSize = Value; }
440 bool hasInstructions() const { return HasInstructions; }
441 void setHasInstructions(bool Value) { HasInstructions = Value; }
448 // FIXME: Same concerns as with SectionData.
449 class MCSymbolData : public ilist_node<MCSymbolData> {
451 const MCSymbol *Symbol;
453 /// Fragment - The fragment this symbol's value is relative to, if any.
454 MCFragment *Fragment;
456 /// Offset - The offset to apply to the fragment address to form this symbol's
460 /// IsExternal - True if this symbol is visible outside this translation
462 unsigned IsExternal : 1;
464 /// IsPrivateExtern - True if this symbol is private extern.
465 unsigned IsPrivateExtern : 1;
467 /// CommonSize - The size of the symbol, if it is 'common', or 0.
469 // FIXME: Pack this in with other fields? We could put it in offset, since a
470 // common symbol can never get a definition.
473 /// CommonAlign - The alignment of the symbol, if it is 'common'.
475 // FIXME: Pack this in with other fields?
476 unsigned CommonAlign;
478 /// Flags - The Flags field is used by object file implementations to store
479 /// additional per symbol information which is not easily classified.
482 /// Index - Index field, for use by the object file implementation.
486 // Only for use as sentinel.
488 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
494 const MCSymbol &getSymbol() const { return *Symbol; }
496 MCFragment *getFragment() const { return Fragment; }
497 void setFragment(MCFragment *Value) { Fragment = Value; }
499 uint64_t getOffset() const { return Offset; }
500 void setOffset(uint64_t Value) { Offset = Value; }
503 /// @name Symbol Attributes
506 bool isExternal() const { return IsExternal; }
507 void setExternal(bool Value) { IsExternal = Value; }
509 bool isPrivateExtern() const { return IsPrivateExtern; }
510 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
512 /// isCommon - Is this a 'common' symbol.
513 bool isCommon() const { return CommonSize != 0; }
515 /// setCommon - Mark this symbol as being 'common'.
517 /// \param Size - The size of the symbol.
518 /// \param Align - The alignment of the symbol.
519 void setCommon(uint64_t Size, unsigned Align) {
524 /// getCommonSize - Return the size of a 'common' symbol.
525 uint64_t getCommonSize() const {
526 assert(isCommon() && "Not a 'common' symbol!");
530 /// getCommonAlignment - Return the alignment of a 'common' symbol.
531 unsigned getCommonAlignment() const {
532 assert(isCommon() && "Not a 'common' symbol!");
536 /// getFlags - Get the (implementation defined) symbol flags.
537 uint32_t getFlags() const { return Flags; }
539 /// setFlags - Set the (implementation defined) symbol flags.
540 void setFlags(uint32_t Value) { Flags = Value; }
542 /// getIndex - Get the (implementation defined) index.
543 uint64_t getIndex() const { return Index; }
545 /// setIndex - Set the (implementation defined) index.
546 void setIndex(uint64_t Value) { Index = Value; }
553 // FIXME: This really doesn't belong here. See comments below.
554 struct IndirectSymbolData {
556 MCSectionData *SectionData;
561 typedef iplist<MCSectionData> SectionDataListType;
562 typedef iplist<MCSymbolData> SymbolDataListType;
564 typedef SectionDataListType::const_iterator const_iterator;
565 typedef SectionDataListType::iterator iterator;
567 typedef SymbolDataListType::const_iterator const_symbol_iterator;
568 typedef SymbolDataListType::iterator symbol_iterator;
570 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
573 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
574 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
580 iplist<MCSectionData> Sections;
582 iplist<MCSymbolData> Symbols;
584 std::vector<IndirectSymbolData> IndirectSymbols;
586 unsigned SubsectionsViaSymbols : 1;
589 /// LayoutSection - Assign offsets and sizes to the fragments in the section
590 /// \arg SD, and update the section size. The section file offset should
591 /// already have been computed.
592 void LayoutSection(MCSectionData &SD);
595 /// Construct a new assembler instance.
597 /// \arg OS - The stream to output to.
599 // FIXME: How are we going to parameterize this? Two obvious options are stay
600 // concrete and require clients to pass in a target like object. The other
601 // option is to make this abstract, and have targets provide concrete
602 // implementations as we do with AsmParser.
603 MCAssembler(MCContext &_Context, raw_ostream &OS);
606 MCContext &getContext() const { return Context; }
608 /// Finish - Do final processing and write the object to the output stream.
611 // FIXME: This does not belong here.
612 bool getSubsectionsViaSymbols() const {
613 return SubsectionsViaSymbols;
615 void setSubsectionsViaSymbols(bool Value) {
616 SubsectionsViaSymbols = Value;
619 /// @name Section List Access
622 const SectionDataListType &getSectionList() const { return Sections; }
623 SectionDataListType &getSectionList() { return Sections; }
625 iterator begin() { return Sections.begin(); }
626 const_iterator begin() const { return Sections.begin(); }
628 iterator end() { return Sections.end(); }
629 const_iterator end() const { return Sections.end(); }
631 size_t size() const { return Sections.size(); }
634 /// @name Symbol List Access
637 const SymbolDataListType &getSymbolList() const { return Symbols; }
638 SymbolDataListType &getSymbolList() { return Symbols; }
640 symbol_iterator symbol_begin() { return Symbols.begin(); }
641 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
643 symbol_iterator symbol_end() { return Symbols.end(); }
644 const_symbol_iterator symbol_end() const { return Symbols.end(); }
646 size_t symbol_size() const { return Symbols.size(); }
649 /// @name Indirect Symbol List Access
652 // FIXME: This is a total hack, this should not be here. Once things are
653 // factored so that the streamer has direct access to the .o writer, it can
655 std::vector<IndirectSymbolData> &getIndirectSymbols() {
656 return IndirectSymbols;
659 indirect_symbol_iterator indirect_symbol_begin() {
660 return IndirectSymbols.begin();
663 indirect_symbol_iterator indirect_symbol_end() {
664 return IndirectSymbols.end();
667 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
674 } // end namespace llvm