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/DenseMap.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/ilist.h"
16
#include "llvm/ADT/ilist_node.h"
17
#include "llvm/Support/Casting.h"
18
#include "llvm/MC/MCFixup.h"
19
#include "llvm/MC/MCInst.h"
20
#include "llvm/System/DataTypes.h"
21
#include <vector> // FIXME: Shouldn't be needed.
38
class TargetAsmBackend;
40
class MCFragment : public ilist_node<MCFragment> {
41
friend class MCAsmLayout;
43
MCFragment(const MCFragment&); // DO NOT IMPLEMENT
44
void operator=(const MCFragment&); // DO NOT IMPLEMENT
58
/// Parent - The data for the section this fragment is in.
59
MCSectionData *Parent;
61
/// Atom - The atom this fragment is in, as represented by it's defining
62
/// symbol. Atom's are only used by backends which set
63
/// \see MCAsmBackend::hasReliableSymbolDifference().
66
/// @name Assembler Backend Data
69
// FIXME: This could all be kept private to the assembler implementation.
71
/// Offset - The offset of this fragment in its section. This is ~0 until
75
/// EffectiveSize - The compute size of this section. This is ~0 until
77
uint64_t EffectiveSize;
79
/// LayoutOrder - The global layout order of this fragment. This is the index
80
/// across all fragments in the file, not just within the section.
86
MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
91
virtual ~MCFragment();
93
FragmentType getKind() const { return Kind; }
95
MCSectionData *getParent() const { return Parent; }
96
void setParent(MCSectionData *Value) { Parent = Value; }
98
MCSymbolData *getAtom() const { return Atom; }
99
void setAtom(MCSymbolData *Value) { Atom = Value; }
101
unsigned getLayoutOrder() const { return LayoutOrder; }
102
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
104
static bool classof(const MCFragment *O) { return true; }
109
class MCDataFragment : public MCFragment {
110
SmallString<32> Contents;
112
/// Fixups - The list of fixups in this fragment.
113
std::vector<MCFixup> Fixups;
116
typedef std::vector<MCFixup>::const_iterator const_fixup_iterator;
117
typedef std::vector<MCFixup>::iterator fixup_iterator;
120
MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
125
SmallString<32> &getContents() { return Contents; }
126
const SmallString<32> &getContents() const { return Contents; }
129
/// @name Fixup Access
132
void addFixup(MCFixup Fixup) {
133
// Enforce invariant that fixups are in offset order.
134
assert((Fixups.empty() || Fixup.getOffset() > Fixups.back().getOffset()) &&
135
"Fixups must be added in order!");
136
Fixups.push_back(Fixup);
139
std::vector<MCFixup> &getFixups() { return Fixups; }
140
const std::vector<MCFixup> &getFixups() const { return Fixups; }
142
fixup_iterator fixup_begin() { return Fixups.begin(); }
143
const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
145
fixup_iterator fixup_end() {return Fixups.end();}
146
const_fixup_iterator fixup_end() const {return Fixups.end();}
148
size_t fixup_size() const { return Fixups.size(); }
152
static bool classof(const MCFragment *F) {
153
return F->getKind() == MCFragment::FT_Data;
155
static bool classof(const MCDataFragment *) { return true; }
158
// FIXME: This current incarnation of MCInstFragment doesn't make much sense, as
159
// it is almost entirely a duplicate of MCDataFragment. If we decide to stick
160
// with this approach (as opposed to making MCInstFragment a very light weight
161
// object with just the MCInst and a code size, then we should just change
162
// MCDataFragment to have an optional MCInst at its end.
163
class MCInstFragment : public MCFragment {
164
/// Inst - The instruction this is a fragment for.
167
/// Code - Binary data for the currently encoded instruction.
170
/// Fixups - The list of fixups in this fragment.
171
SmallVector<MCFixup, 1> Fixups;
174
typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
175
typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
178
MCInstFragment(MCInst _Inst, MCSectionData *SD = 0)
179
: MCFragment(FT_Inst, SD), Inst(_Inst) {
185
SmallVectorImpl<char> &getCode() { return Code; }
186
const SmallVectorImpl<char> &getCode() const { return Code; }
188
unsigned getInstSize() const { return Code.size(); }
190
MCInst &getInst() { return Inst; }
191
const MCInst &getInst() const { return Inst; }
193
void setInst(MCInst Value) { Inst = Value; }
196
/// @name Fixup Access
199
SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
200
const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
202
fixup_iterator fixup_begin() { return Fixups.begin(); }
203
const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
205
fixup_iterator fixup_end() {return Fixups.end();}
206
const_fixup_iterator fixup_end() const {return Fixups.end();}
208
size_t fixup_size() const { return Fixups.size(); }
212
static bool classof(const MCFragment *F) {
213
return F->getKind() == MCFragment::FT_Inst;
215
static bool classof(const MCInstFragment *) { return true; }
218
class MCAlignFragment : public MCFragment {
219
/// Alignment - The alignment to ensure, in bytes.
222
/// Value - Value to use for filling padding bytes.
225
/// ValueSize - The size of the integer (in bytes) of \arg Value.
228
/// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
229
/// cannot be satisfied in this width then this fragment is ignored.
230
unsigned MaxBytesToEmit;
232
/// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
233
/// of using the provided value. The exact interpretation of this flag is
234
/// target dependent.
237
/// OnlyAlignAddress - Flag to indicate that this align is only used to adjust
238
/// the address space size of a section and that it should not be included as
239
/// part of the section size. This flag can only be used on the last fragment
241
bool OnlyAlignAddress : 1;
244
MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
245
unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
246
: MCFragment(FT_Align, SD), Alignment(_Alignment),
247
Value(_Value),ValueSize(_ValueSize),
248
MaxBytesToEmit(_MaxBytesToEmit), EmitNops(false),
249
OnlyAlignAddress(false) {}
254
unsigned getAlignment() const { return Alignment; }
256
int64_t getValue() const { return Value; }
258
unsigned getValueSize() const { return ValueSize; }
260
unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
262
bool hasEmitNops() const { return EmitNops; }
263
void setEmitNops(bool Value) { EmitNops = Value; }
265
bool hasOnlyAlignAddress() const { return OnlyAlignAddress; }
266
void setOnlyAlignAddress(bool Value) { OnlyAlignAddress = Value; }
270
static bool classof(const MCFragment *F) {
271
return F->getKind() == MCFragment::FT_Align;
273
static bool classof(const MCAlignFragment *) { return true; }
276
class MCFillFragment : public MCFragment {
277
/// Value - Value to use for filling bytes.
280
/// ValueSize - The size (in bytes) of \arg Value to use when filling, or 0 if
281
/// this is a virtual fill fragment.
284
/// Size - The number of bytes to insert.
288
MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Size,
289
MCSectionData *SD = 0)
290
: MCFragment(FT_Fill, SD),
291
Value(_Value), ValueSize(_ValueSize), Size(_Size) {
292
assert((!ValueSize || (Size % ValueSize) == 0) &&
293
"Fill size must be a multiple of the value size!");
299
int64_t getValue() const { return Value; }
301
unsigned getValueSize() const { return ValueSize; }
303
uint64_t getSize() const { return Size; }
307
static bool classof(const MCFragment *F) {
308
return F->getKind() == MCFragment::FT_Fill;
310
static bool classof(const MCFillFragment *) { return true; }
313
class MCOrgFragment : public MCFragment {
314
/// Offset - The offset this fragment should start at.
315
const MCExpr *Offset;
317
/// Value - Value to use for filling bytes.
321
MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
322
: MCFragment(FT_Org, SD),
323
Offset(&_Offset), Value(_Value) {}
328
const MCExpr &getOffset() const { return *Offset; }
330
uint8_t getValue() const { return Value; }
334
static bool classof(const MCFragment *F) {
335
return F->getKind() == MCFragment::FT_Org;
337
static bool classof(const MCOrgFragment *) { return true; }
340
// FIXME: Should this be a separate class, or just merged into MCSection? Since
341
// we anticipate the fast path being through an MCAssembler, the only reason to
342
// keep it out is for API abstraction.
343
class MCSectionData : public ilist_node<MCSectionData> {
344
friend class MCAsmLayout;
346
MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
347
void operator=(const MCSectionData&); // DO NOT IMPLEMENT
350
typedef iplist<MCFragment> FragmentListType;
352
typedef FragmentListType::const_iterator const_iterator;
353
typedef FragmentListType::iterator iterator;
355
typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
356
typedef FragmentListType::reverse_iterator reverse_iterator;
359
FragmentListType Fragments;
360
const MCSection *Section;
362
/// Ordinal - The section index in the assemblers section list.
365
/// LayoutOrder - The index of this section in the layout order.
366
unsigned LayoutOrder;
368
/// Alignment - The maximum alignment seen in this section.
371
/// @name Assembler Backend Data
374
// FIXME: This could all be kept private to the assembler implementation.
376
/// Address - The computed address of this section. This is ~0 until
380
/// HasInstructions - Whether this section has had instructions emitted into
382
unsigned HasInstructions : 1;
387
// Only for use as sentinel.
389
MCSectionData(const MCSection &Section, MCAssembler *A = 0);
391
const MCSection &getSection() const { return *Section; }
393
unsigned getAlignment() const { return Alignment; }
394
void setAlignment(unsigned Value) { Alignment = Value; }
396
bool hasInstructions() const { return HasInstructions; }
397
void setHasInstructions(bool Value) { HasInstructions = Value; }
399
unsigned getOrdinal() const { return Ordinal; }
400
void setOrdinal(unsigned Value) { Ordinal = Value; }
402
unsigned getLayoutOrder() const { return LayoutOrder; }
403
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
405
/// @name Fragment Access
408
const FragmentListType &getFragmentList() const { return Fragments; }
409
FragmentListType &getFragmentList() { return Fragments; }
411
iterator begin() { return Fragments.begin(); }
412
const_iterator begin() const { return Fragments.begin(); }
414
iterator end() { return Fragments.end(); }
415
const_iterator end() const { return Fragments.end(); }
417
reverse_iterator rbegin() { return Fragments.rbegin(); }
418
const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
420
reverse_iterator rend() { return Fragments.rend(); }
421
const_reverse_iterator rend() const { return Fragments.rend(); }
423
size_t size() const { return Fragments.size(); }
425
bool empty() const { return Fragments.empty(); }
432
// FIXME: Same concerns as with SectionData.
433
class MCSymbolData : public ilist_node<MCSymbolData> {
435
const MCSymbol *Symbol;
437
/// Fragment - The fragment this symbol's value is relative to, if any.
438
MCFragment *Fragment;
440
/// Offset - The offset to apply to the fragment address to form this symbol's
444
/// IsExternal - True if this symbol is visible outside this translation
446
unsigned IsExternal : 1;
448
/// IsPrivateExtern - True if this symbol is private extern.
449
unsigned IsPrivateExtern : 1;
451
/// CommonSize - The size of the symbol, if it is 'common', or 0.
453
// FIXME: Pack this in with other fields? We could put it in offset, since a
454
// common symbol can never get a definition.
457
/// SymbolSize - An expression describing how to calculate the size of
458
/// a symbol. If a symbol has no size this field will be NULL.
459
const MCExpr *SymbolSize;
461
/// CommonAlign - The alignment of the symbol, if it is 'common'.
463
// FIXME: Pack this in with other fields?
464
unsigned CommonAlign;
466
/// Flags - The Flags field is used by object file implementations to store
467
/// additional per symbol information which is not easily classified.
470
/// Index - Index field, for use by the object file implementation.
474
// Only for use as sentinel.
476
MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
482
const MCSymbol &getSymbol() const { return *Symbol; }
484
MCFragment *getFragment() const { return Fragment; }
485
void setFragment(MCFragment *Value) { Fragment = Value; }
487
uint64_t getOffset() const { return Offset; }
488
void setOffset(uint64_t Value) { Offset = Value; }
491
/// @name Symbol Attributes
494
bool isExternal() const { return IsExternal; }
495
void setExternal(bool Value) { IsExternal = Value; }
497
bool isPrivateExtern() const { return IsPrivateExtern; }
498
void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
500
/// isCommon - Is this a 'common' symbol.
501
bool isCommon() const { return CommonSize != 0; }
503
/// setCommon - Mark this symbol as being 'common'.
505
/// \param Size - The size of the symbol.
506
/// \param Align - The alignment of the symbol.
507
void setCommon(uint64_t Size, unsigned Align) {
512
/// getCommonSize - Return the size of a 'common' symbol.
513
uint64_t getCommonSize() const {
514
assert(isCommon() && "Not a 'common' symbol!");
518
void setSize(const MCExpr *SS) {
522
const MCExpr *getSize() const {
527
/// getCommonAlignment - Return the alignment of a 'common' symbol.
528
unsigned getCommonAlignment() const {
529
assert(isCommon() && "Not a 'common' symbol!");
533
/// getFlags - Get the (implementation defined) symbol flags.
534
uint32_t getFlags() const { return Flags; }
536
/// setFlags - Set the (implementation defined) symbol flags.
537
void setFlags(uint32_t Value) { Flags = Value; }
539
/// modifyFlags - Modify the flags via a mask
540
void modifyFlags(uint32_t Value, uint32_t Mask) {
541
Flags = (Flags & ~Mask) | Value;
544
/// getIndex - Get the (implementation defined) index.
545
uint64_t getIndex() const { return Index; }
547
/// setIndex - Set the (implementation defined) index.
548
void setIndex(uint64_t Value) { Index = Value; }
555
// FIXME: This really doesn't belong here. See comments below.
556
struct IndirectSymbolData {
558
MCSectionData *SectionData;
562
friend class MCAsmLayout;
565
typedef iplist<MCSectionData> SectionDataListType;
566
typedef iplist<MCSymbolData> SymbolDataListType;
568
typedef SectionDataListType::const_iterator const_iterator;
569
typedef SectionDataListType::iterator iterator;
571
typedef SymbolDataListType::const_iterator const_symbol_iterator;
572
typedef SymbolDataListType::iterator symbol_iterator;
574
typedef std::vector<IndirectSymbolData>::const_iterator
575
const_indirect_symbol_iterator;
576
typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
579
MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
580
void operator=(const MCAssembler&); // DO NOT IMPLEMENT
584
TargetAsmBackend &Backend;
586
MCCodeEmitter &Emitter;
590
iplist<MCSectionData> Sections;
592
iplist<MCSymbolData> Symbols;
594
/// The map of sections to their associated assembler backend data.
596
// FIXME: Avoid this indirection?
597
DenseMap<const MCSection*, MCSectionData*> SectionMap;
599
/// The map of symbols to their associated assembler backend data.
601
// FIXME: Avoid this indirection?
602
DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
604
std::vector<IndirectSymbolData> IndirectSymbols;
606
unsigned RelaxAll : 1;
607
unsigned SubsectionsViaSymbols : 1;
610
/// Evaluate a fixup to a relocatable expression and the value which should be
611
/// placed into the fixup.
613
/// \param Layout The layout to use for evaluation.
614
/// \param Fixup The fixup to evaluate.
615
/// \param DF The fragment the fixup is inside.
616
/// \param Target [out] On return, the relocatable expression the fixup
618
/// \param Value [out] On return, the value of the fixup as currently layed
620
/// \return Whether the fixup value was fully resolved. This is true if the
621
/// \arg Value result is fixed, otherwise the value may change due to
623
bool EvaluateFixup(const MCAsmLayout &Layout,
624
const MCFixup &Fixup, const MCFragment *DF,
625
MCValue &Target, uint64_t &Value) const;
627
/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
628
/// (increased in size, in order to hold its value correctly).
629
bool FixupNeedsRelaxation(const MCFixup &Fixup, const MCFragment *DF,
630
const MCAsmLayout &Layout) const;
632
/// Check whether the given fragment needs relaxation.
633
bool FragmentNeedsRelaxation(const MCInstFragment *IF,
634
const MCAsmLayout &Layout) const;
636
/// Compute the effective fragment size assuming it is layed out at the given
637
/// \arg SectionAddress and \arg FragmentOffset.
638
uint64_t ComputeFragmentSize(MCAsmLayout &Layout, const MCFragment &F,
639
uint64_t SectionAddress,
640
uint64_t FragmentOffset) const;
642
/// LayoutOnce - Perform one layout iteration and return true if any offsets
644
bool LayoutOnce(MCAsmLayout &Layout);
646
/// FinishLayout - Finalize a layout, including fragment lowering.
647
void FinishLayout(MCAsmLayout &Layout);
650
/// Find the symbol which defines the atom containing the given symbol, or
651
/// null if there is no such symbol.
652
const MCSymbolData *getAtom(const MCAsmLayout &Layout,
653
const MCSymbolData *Symbol) const;
655
/// Check whether a particular symbol is visible to the linker and is required
656
/// in the symbol table, or whether it can be discarded by the assembler. This
657
/// also effects whether the assembler treats the label as potentially
658
/// defining a separate atom.
659
bool isSymbolLinkerVisible(const MCSymbol &SD) const;
661
/// Emit the section contents using the given object writer.
663
// FIXME: Should MCAssembler always have a reference to the object writer?
664
void WriteSectionData(const MCSectionData *Section, const MCAsmLayout &Layout,
665
MCObjectWriter *OW) const;
667
void AddSectionToTheEnd(MCSectionData &SD, MCAsmLayout &Layout);
670
/// Construct a new assembler instance.
672
/// \arg OS - The stream to output to.
674
// FIXME: How are we going to parameterize this? Two obvious options are stay
675
// concrete and require clients to pass in a target like object. The other
676
// option is to make this abstract, and have targets provide concrete
677
// implementations as we do with AsmParser.
678
MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
679
MCCodeEmitter &_Emitter, raw_ostream &OS);
682
MCContext &getContext() const { return Context; }
684
TargetAsmBackend &getBackend() const { return Backend; }
686
MCCodeEmitter &getEmitter() const { return Emitter; }
688
/// Finish - Do final processing and write the object to the output stream.
689
/// \arg Writer is used for custom object writer (as the MCJIT does),
690
/// if not specified it is automatically created from backend.
691
void Finish(MCObjectWriter *Writer = 0);
693
// FIXME: This does not belong here.
694
bool getSubsectionsViaSymbols() const {
695
return SubsectionsViaSymbols;
697
void setSubsectionsViaSymbols(bool Value) {
698
SubsectionsViaSymbols = Value;
701
bool getRelaxAll() const { return RelaxAll; }
702
void setRelaxAll(bool Value) { RelaxAll = Value; }
704
/// @name Section List Access
707
const SectionDataListType &getSectionList() const { return Sections; }
708
SectionDataListType &getSectionList() { return Sections; }
710
iterator begin() { return Sections.begin(); }
711
const_iterator begin() const { return Sections.begin(); }
713
iterator end() { return Sections.end(); }
714
const_iterator end() const { return Sections.end(); }
716
size_t size() const { return Sections.size(); }
719
/// @name Symbol List Access
722
const SymbolDataListType &getSymbolList() const { return Symbols; }
723
SymbolDataListType &getSymbolList() { return Symbols; }
725
symbol_iterator symbol_begin() { return Symbols.begin(); }
726
const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
728
symbol_iterator symbol_end() { return Symbols.end(); }
729
const_symbol_iterator symbol_end() const { return Symbols.end(); }
731
size_t symbol_size() const { return Symbols.size(); }
734
/// @name Indirect Symbol List Access
737
// FIXME: This is a total hack, this should not be here. Once things are
738
// factored so that the streamer has direct access to the .o writer, it can
740
std::vector<IndirectSymbolData> &getIndirectSymbols() {
741
return IndirectSymbols;
744
indirect_symbol_iterator indirect_symbol_begin() {
745
return IndirectSymbols.begin();
747
const_indirect_symbol_iterator indirect_symbol_begin() const {
748
return IndirectSymbols.begin();
751
indirect_symbol_iterator indirect_symbol_end() {
752
return IndirectSymbols.end();
754
const_indirect_symbol_iterator indirect_symbol_end() const {
755
return IndirectSymbols.end();
758
size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
761
/// @name Backend Data Access
764
MCSectionData &getSectionData(const MCSection &Section) const {
765
MCSectionData *Entry = SectionMap.lookup(&Section);
766
assert(Entry && "Missing section data!");
770
MCSectionData &getOrCreateSectionData(const MCSection &Section,
772
MCSectionData *&Entry = SectionMap[&Section];
774
if (Created) *Created = !Entry;
776
Entry = new MCSectionData(Section, this);
781
MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
782
MCSymbolData *Entry = SymbolMap.lookup(&Symbol);
783
assert(Entry && "Missing symbol data!");
787
MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
789
MCSymbolData *&Entry = SymbolMap[&Symbol];
791
if (Created) *Created = !Entry;
793
Entry = new MCSymbolData(Symbol, 0, 0, this);
803
} // end namespace llvm