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;
198
/// EmitNops - true when aligning code and optimal nops to be used for filling
202
MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
203
unsigned _MaxBytesToEmit, bool _EmitNops,
204
MCSectionData *SD = 0)
205
: MCFragment(FT_Align, SD), Alignment(_Alignment),
206
Value(_Value),ValueSize(_ValueSize),
207
MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
212
uint64_t getMaxFileSize() const {
213
return std::max(Alignment - 1, MaxBytesToEmit);
216
unsigned getAlignment() const { return Alignment; }
218
int64_t getValue() const { return Value; }
220
unsigned getValueSize() const { return ValueSize; }
222
unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
224
unsigned getEmitNops() const { return EmitNops; }
228
static bool classof(const MCFragment *F) {
229
return F->getKind() == MCFragment::FT_Align;
231
static bool classof(const MCAlignFragment *) { return true; }
236
class MCFillFragment : public MCFragment {
237
/// Value - Value to use for filling bytes.
240
/// ValueSize - The size (in bytes) of \arg Value to use when filling.
243
/// Count - The number of copies of \arg Value to insert.
247
MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
248
MCSectionData *SD = 0)
249
: MCFragment(FT_Fill, SD),
250
Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
255
uint64_t getMaxFileSize() const {
256
return ValueSize * Count;
259
int64_t getValue() const { return Value; }
261
unsigned getValueSize() const { return ValueSize; }
263
uint64_t getCount() const { return Count; }
267
static bool classof(const MCFragment *F) {
268
return F->getKind() == MCFragment::FT_Fill;
270
static bool classof(const MCFillFragment *) { return true; }
275
class MCOrgFragment : public MCFragment {
276
/// Offset - The offset this fragment should start at.
277
const MCExpr *Offset;
279
/// Value - Value to use for filling bytes.
283
MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
284
: MCFragment(FT_Org, SD),
285
Offset(&_Offset), Value(_Value) {}
290
uint64_t getMaxFileSize() const {
291
// FIXME: This doesn't make much sense.
295
const MCExpr &getOffset() const { return *Offset; }
297
uint8_t getValue() const { return Value; }
301
static bool classof(const MCFragment *F) {
302
return F->getKind() == MCFragment::FT_Org;
304
static bool classof(const MCOrgFragment *) { return true; }
309
/// MCZeroFillFragment - Represent data which has a fixed size and alignment,
310
/// but requires no physical space in the object file.
311
class MCZeroFillFragment : public MCFragment {
312
/// Size - The size of this fragment.
315
/// Alignment - The alignment for this fragment.
319
MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
320
: MCFragment(FT_ZeroFill, SD),
321
Size(_Size), Alignment(_Alignment) {}
326
uint64_t getMaxFileSize() const {
327
// FIXME: This also doesn't make much sense, this method is misnamed.
331
uint64_t getSize() const { return Size; }
333
unsigned getAlignment() const { return Alignment; }
337
static bool classof(const MCFragment *F) {
338
return F->getKind() == MCFragment::FT_ZeroFill;
340
static bool classof(const MCZeroFillFragment *) { return true; }
345
// FIXME: Should this be a separate class, or just merged into MCSection? Since
346
// we anticipate the fast path being through an MCAssembler, the only reason to
347
// keep it out is for API abstraction.
348
class MCSectionData : public ilist_node<MCSectionData> {
349
MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
350
void operator=(const MCSectionData&); // DO NOT IMPLEMENT
353
typedef iplist<MCFragment> FragmentListType;
355
typedef FragmentListType::const_iterator const_iterator;
356
typedef FragmentListType::iterator iterator;
358
typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
359
typedef FragmentListType::reverse_iterator reverse_iterator;
362
iplist<MCFragment> Fragments;
363
const MCSection *Section;
365
/// Alignment - The maximum alignment seen in this section.
368
/// @name Assembler Backend Data
371
// FIXME: This could all be kept private to the assembler implementation.
373
/// Address - The computed address of this section. This is ~0 until
377
/// Size - The content size of this section. This is ~0 until initialized.
380
/// FileSize - The size of this section in the object file. This is ~0 until
384
/// HasInstructions - Whether this section has had instructions emitted into
386
unsigned HasInstructions : 1;
391
// Only for use as sentinel.
393
MCSectionData(const MCSection &Section, MCAssembler *A = 0);
395
const MCSection &getSection() const { return *Section; }
397
unsigned getAlignment() const { return Alignment; }
398
void setAlignment(unsigned Value) { Alignment = Value; }
400
/// @name Fragment Access
403
const FragmentListType &getFragmentList() const { return Fragments; }
404
FragmentListType &getFragmentList() { return Fragments; }
406
iterator begin() { return Fragments.begin(); }
407
const_iterator begin() const { return Fragments.begin(); }
409
iterator end() { return Fragments.end(); }
410
const_iterator end() const { return Fragments.end(); }
412
reverse_iterator rbegin() { return Fragments.rbegin(); }
413
const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
415
reverse_iterator rend() { return Fragments.rend(); }
416
const_reverse_iterator rend() const { return Fragments.rend(); }
418
size_t size() const { return Fragments.size(); }
420
bool empty() const { return Fragments.empty(); }
423
/// @name Assembler Backend Support
426
// FIXME: This could all be kept private to the assembler implementation.
428
uint64_t getAddress() const {
429
assert(Address != ~UINT64_C(0) && "Address not set!");
432
void setAddress(uint64_t Value) { Address = Value; }
434
uint64_t getSize() const {
435
assert(Size != ~UINT64_C(0) && "File size not set!");
438
void setSize(uint64_t Value) { Size = Value; }
440
uint64_t getFileSize() const {
441
assert(FileSize != ~UINT64_C(0) && "File size not set!");
444
void setFileSize(uint64_t Value) { FileSize = Value; }
446
bool hasInstructions() const { return HasInstructions; }
447
void setHasInstructions(bool Value) { HasInstructions = Value; }
454
// FIXME: Same concerns as with SectionData.
455
class MCSymbolData : public ilist_node<MCSymbolData> {
457
const MCSymbol *Symbol;
459
/// Fragment - The fragment this symbol's value is relative to, if any.
460
MCFragment *Fragment;
462
/// Offset - The offset to apply to the fragment address to form this symbol's
466
/// IsExternal - True if this symbol is visible outside this translation
468
unsigned IsExternal : 1;
470
/// IsPrivateExtern - True if this symbol is private extern.
471
unsigned IsPrivateExtern : 1;
473
/// CommonSize - The size of the symbol, if it is 'common', or 0.
475
// FIXME: Pack this in with other fields? We could put it in offset, since a
476
// common symbol can never get a definition.
479
/// CommonAlign - The alignment of the symbol, if it is 'common'.
481
// FIXME: Pack this in with other fields?
482
unsigned CommonAlign;
484
/// Flags - The Flags field is used by object file implementations to store
485
/// additional per symbol information which is not easily classified.
488
/// Index - Index field, for use by the object file implementation.
492
// Only for use as sentinel.
494
MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
500
const MCSymbol &getSymbol() const { return *Symbol; }
502
MCFragment *getFragment() const { return Fragment; }
503
void setFragment(MCFragment *Value) { Fragment = Value; }
505
uint64_t getOffset() const { return Offset; }
506
void setOffset(uint64_t Value) { Offset = Value; }
509
/// @name Symbol Attributes
512
bool isExternal() const { return IsExternal; }
513
void setExternal(bool Value) { IsExternal = Value; }
515
bool isPrivateExtern() const { return IsPrivateExtern; }
516
void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
518
/// isCommon - Is this a 'common' symbol.
519
bool isCommon() const { return CommonSize != 0; }
521
/// setCommon - Mark this symbol as being 'common'.
523
/// \param Size - The size of the symbol.
524
/// \param Align - The alignment of the symbol.
525
void setCommon(uint64_t Size, unsigned Align) {
530
/// getCommonSize - Return the size of a 'common' symbol.
531
uint64_t getCommonSize() const {
532
assert(isCommon() && "Not a 'common' symbol!");
536
/// getCommonAlignment - Return the alignment of a 'common' symbol.
537
unsigned getCommonAlignment() const {
538
assert(isCommon() && "Not a 'common' symbol!");
542
/// getFlags - Get the (implementation defined) symbol flags.
543
uint32_t getFlags() const { return Flags; }
545
/// setFlags - Set the (implementation defined) symbol flags.
546
void setFlags(uint32_t Value) { Flags = Value; }
548
/// getIndex - Get the (implementation defined) index.
549
uint64_t getIndex() const { return Index; }
551
/// setIndex - Set the (implementation defined) index.
552
void setIndex(uint64_t Value) { Index = Value; }
559
// FIXME: This really doesn't belong here. See comments below.
560
struct IndirectSymbolData {
562
MCSectionData *SectionData;
567
typedef iplist<MCSectionData> SectionDataListType;
568
typedef iplist<MCSymbolData> SymbolDataListType;
570
typedef SectionDataListType::const_iterator const_iterator;
571
typedef SectionDataListType::iterator iterator;
573
typedef SymbolDataListType::const_iterator const_symbol_iterator;
574
typedef SymbolDataListType::iterator 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
586
iplist<MCSectionData> Sections;
588
iplist<MCSymbolData> Symbols;
590
std::vector<IndirectSymbolData> IndirectSymbols;
592
unsigned SubsectionsViaSymbols : 1;
595
/// LayoutSection - Assign offsets and sizes to the fragments in the section
596
/// \arg SD, and update the section size. The section file offset should
597
/// already have been computed.
598
void LayoutSection(MCSectionData &SD);
601
/// Construct a new assembler instance.
603
/// \arg OS - The stream to output to.
605
// FIXME: How are we going to parameterize this? Two obvious options are stay
606
// concrete and require clients to pass in a target like object. The other
607
// option is to make this abstract, and have targets provide concrete
608
// implementations as we do with AsmParser.
609
MCAssembler(MCContext &_Context, raw_ostream &OS);
612
MCContext &getContext() const { return Context; }
614
/// Finish - Do final processing and write the object to the output stream.
617
// FIXME: This does not belong here.
618
bool getSubsectionsViaSymbols() const {
619
return SubsectionsViaSymbols;
621
void setSubsectionsViaSymbols(bool Value) {
622
SubsectionsViaSymbols = Value;
625
/// @name Section List Access
628
const SectionDataListType &getSectionList() const { return Sections; }
629
SectionDataListType &getSectionList() { return Sections; }
631
iterator begin() { return Sections.begin(); }
632
const_iterator begin() const { return Sections.begin(); }
634
iterator end() { return Sections.end(); }
635
const_iterator end() const { return Sections.end(); }
637
size_t size() const { return Sections.size(); }
640
/// @name Symbol List Access
643
const SymbolDataListType &getSymbolList() const { return Symbols; }
644
SymbolDataListType &getSymbolList() { return Symbols; }
646
symbol_iterator symbol_begin() { return Symbols.begin(); }
647
const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
649
symbol_iterator symbol_end() { return Symbols.end(); }
650
const_symbol_iterator symbol_end() const { return Symbols.end(); }
652
size_t symbol_size() const { return Symbols.size(); }
655
/// @name Indirect Symbol List Access
658
// FIXME: This is a total hack, this should not be here. Once things are
659
// factored so that the streamer has direct access to the .o writer, it can
661
std::vector<IndirectSymbolData> &getIndirectSymbols() {
662
return IndirectSymbols;
665
indirect_symbol_iterator indirect_symbol_begin() {
666
return IndirectSymbols.begin();
669
indirect_symbol_iterator indirect_symbol_end() {
670
return IndirectSymbols.end();
673
size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
680
} // end namespace llvm