LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCObjectStreamer.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCSection.h"
23 using namespace llvm;
24 
26  MCTargetStreamer *TargetStreamer,
27  MCAsmBackend &TAB, raw_ostream &OS,
28  MCCodeEmitter *Emitter_)
29  : MCStreamer(Context, TargetStreamer),
30  Assembler(new MCAssembler(Context, TAB, *Emitter_,
31  *TAB.createObjectWriter(OS), OS)),
32  CurSectionData(0) {}
33 
35  MCTargetStreamer *TargetStreamer,
36  MCAsmBackend &TAB, raw_ostream &OS,
37  MCCodeEmitter *Emitter_,
38  MCAssembler *_Assembler)
39  : MCStreamer(Context, TargetStreamer), Assembler(_Assembler),
40  CurSectionData(0) {}
41 
43  delete &Assembler->getBackend();
44  delete &Assembler->getEmitter();
45  delete &Assembler->getWriter();
46  delete Assembler;
47 }
48 
50  if (Assembler)
51  Assembler->reset();
52  CurSectionData = 0;
53  CurInsertionPoint = MCSectionData::iterator();
55 }
56 
58  assert(getCurrentSectionData() && "No current section!");
59 
60  if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin())
61  return prior(CurInsertionPoint);
62 
63  return 0;
64 }
65 
67  MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
68  // When bundling is enabled, we don't want to add data to a fragment that
69  // already has instructions (see MCELFStreamer::EmitInstToData for details)
70  if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) {
71  F = new MCDataFragment();
72  insert(F);
73  }
74  return F;
75 }
76 
78  switch (Value->getKind()) {
79  case MCExpr::Target:
80  cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler);
81  break;
82 
83  case MCExpr::Constant:
84  break;
85 
86  case MCExpr::Binary: {
87  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
88  AddValueSymbols(BE->getLHS());
89  AddValueSymbols(BE->getRHS());
90  break;
91  }
92 
93  case MCExpr::SymbolRef:
94  Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
95  break;
96 
97  case MCExpr::Unary:
98  AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
99  break;
100  }
101 
102  return Value;
103 }
104 
105 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
107 
108  MCLineEntry::Make(this, getCurrentSection().first);
109 
110  // Avoid fixups when possible.
111  int64_t AbsValue;
112  if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) {
113  EmitIntValue(AbsValue, Size);
114  return;
115  }
116  DF->getFixups().push_back(
117  MCFixup::Create(DF->getContents().size(), Value,
118  MCFixup::getKindForSize(Size, false)));
119  DF->getContents().resize(DF->getContents().size() + Size, 0);
120 }
121 
122 void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
123  RecordProcStart(Frame);
124 }
125 
126 void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
127  RecordProcEnd(Frame);
128 }
129 
131  MCStreamer::EmitLabel(Symbol);
132 
134 
135  // FIXME: This is wasteful, we don't necessarily need to create a data
136  // fragment. Instead, we should mark the symbol as pointing into the data
137  // fragment if it exists, otherwise we should just queue the label and set its
138  // fragment pointer when we emit the next fragment.
140  assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
141  SD.setFragment(F);
142  SD.setOffset(F->getContents().size());
143 }
144 
146  EmitLabel(Symbol);
147 }
148 
150  int64_t IntValue;
151  if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
152  EmitULEB128IntValue(IntValue);
153  return;
154  }
155  Value = ForceExpAbs(Value);
156  insert(new MCLEBFragment(*Value, false));
157 }
158 
160  int64_t IntValue;
161  if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) {
162  EmitSLEB128IntValue(IntValue);
163  return;
164  }
165  Value = ForceExpAbs(Value);
166  insert(new MCLEBFragment(*Value, true));
167 }
168 
170  const MCSymbol *Symbol) {
171  report_fatal_error("This file format doesn't support weak aliases.");
172 }
173 
175  const MCExpr *Subsection) {
176  assert(Section && "Cannot switch to a null section!");
177 
178  CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
179 
180  int64_t IntSubsection = 0;
181  if (Subsection &&
182  !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler()))
183  report_fatal_error("Cannot evaluate subsection number");
184  if (IntSubsection < 0 || IntSubsection > 8192)
185  report_fatal_error("Subsection number out of range");
186  CurInsertionPoint =
187  CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection));
188 }
189 
192  Symbol->setVariableValue(AddValueSymbols(Value));
193 }
194 
196  // Scan for values.
197  for (unsigned i = Inst.getNumOperands(); i--; )
198  if (Inst.getOperand(i).isExpr())
200 
202  SD->setHasInstructions(true);
203 
204  // Now that a machine instruction has been assembled into this section, make
205  // a line entry for any .loc directive that has been seen.
206  MCLineEntry::Make(this, getCurrentSection().first);
207 
208  // If this instruction doesn't need relaxation, just emit it as data.
209  MCAssembler &Assembler = getAssembler();
210  if (!Assembler.getBackend().mayNeedRelaxation(Inst)) {
211  EmitInstToData(Inst);
212  return;
213  }
214 
215  // Otherwise, relax and emit it as data if either:
216  // - The RelaxAll flag was passed
217  // - Bundling is enabled and this instruction is inside a bundle-locked
218  // group. We want to emit all such instructions into the same data
219  // fragment.
220  if (Assembler.getRelaxAll() ||
221  (Assembler.isBundlingEnabled() && SD->isBundleLocked())) {
222  MCInst Relaxed;
223  getAssembler().getBackend().relaxInstruction(Inst, Relaxed);
224  while (getAssembler().getBackend().mayNeedRelaxation(Relaxed))
225  getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed);
226  EmitInstToData(Relaxed);
227  return;
228  }
229 
230  // Otherwise emit to a separate fragment.
231  EmitInstToFragment(Inst);
232 }
233 
235  // Always create a new, separate fragment here, because its size can change
236  // during relaxation.
238  insert(IF);
239 
241  raw_svector_ostream VecOS(Code);
242  getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups());
243  VecOS.flush();
244  IF->getContents().append(Code.begin(), Code.end());
245 }
246 
247 #ifndef NDEBUG
248 static const char *const BundlingNotImplementedMsg =
249  "Aligned bundling is not implemented for this object format";
250 #endif
251 
252 void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
254 }
255 
256 void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
258 }
259 
262 }
263 
264 void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
265  unsigned Column, unsigned Flags,
266  unsigned Isa,
267  unsigned Discriminator,
268  StringRef FileName) {
269  // In case we see two .loc directives in a row, make sure the
270  // first one gets a line entry.
271  MCLineEntry::Make(this, getCurrentSection().first);
272 
273  this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
274  Isa, Discriminator, FileName);
275 }
276 
278  const MCSymbol *LastLabel,
279  const MCSymbol *Label,
280  unsigned PointerSize) {
281  if (!LastLabel) {
282  EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
283  return;
284  }
285  const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
286  int64_t Res;
287  if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
288  MCDwarfLineAddr::Emit(this, LineDelta, Res);
289  return;
290  }
291  AddrDelta = ForceExpAbs(AddrDelta);
292  insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
293 }
294 
296  const MCSymbol *Label) {
297  const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel);
298  int64_t Res;
299  if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) {
301  return;
302  }
303  AddrDelta = ForceExpAbs(AddrDelta);
304  insert(new MCDwarfCallFrameFragment(*AddrDelta));
305 }
306 
308  MCLineEntry::Make(this, getCurrentSection().first);
309  getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
310 }
311 
313  int64_t Value,
314  unsigned ValueSize,
315  unsigned MaxBytesToEmit) {
316  if (MaxBytesToEmit == 0)
317  MaxBytesToEmit = ByteAlignment;
318  insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
319 
320  // Update the maximum alignment on the current section if necessary.
321  if (ByteAlignment > getCurrentSectionData()->getAlignment())
322  getCurrentSectionData()->setAlignment(ByteAlignment);
323 }
324 
326  unsigned MaxBytesToEmit) {
327  EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
328  cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
329 }
330 
332  unsigned char Value) {
333  int64_t Res;
334  if (Offset->EvaluateAsAbsolute(Res, getAssembler())) {
335  insert(new MCOrgFragment(*Offset, Value));
336  return false;
337  }
338 
339  MCSymbol *CurrentPos = getContext().CreateTempSymbol();
340  EmitLabel(CurrentPos);
342  const MCExpr *Ref =
343  MCSymbolRefExpr::Create(CurrentPos, Variant, getContext());
344  const MCExpr *Delta =
346 
347  if (!Delta->EvaluateAsAbsolute(Res, getAssembler()))
348  return true;
349  EmitFill(Res, Value);
350  return false;
351 }
352 
353 // Associate GPRel32 fixup with data and resize data area
356 
357  DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
358  Value, FK_GPRel_4));
359  DF->getContents().resize(DF->getContents().size() + 4, 0);
360 }
361 
362 // Associate GPRel32 fixup with data and resize data area
365 
366  DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
367  Value, FK_GPRel_4));
368  DF->getContents().resize(DF->getContents().size() + 8, 0);
369 }
370 
371 void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
372  // FIXME: A MCFillFragment would be more memory efficient but MCExpr has
373  // problems evaluating expressions across multiple fragments.
374  getOrCreateDataFragment()->getContents().append(NumBytes, FillValue);
375 }
376 
377 void MCObjectStreamer::EmitZeros(uint64_t NumBytes) {
378  unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1;
379  insert(new MCFillFragment(0, ItemSize, NumBytes));
380 }
381 
383  // Dump out the dwarf file & directory tables and line tables.
384  const MCSymbol *LineSectionSymbol = NULL;
385  if (getContext().hasDwarfFiles())
386  LineSectionSymbol = MCDwarfFileTable::Emit(this);
387 
388  // If we are generating dwarf for assembly source files dump out the sections.
389  if (getContext().getGenDwarfForAssembly())
390  MCGenDwarfInfo::Emit(this, LineSectionSymbol);
391 
392  getAssembler().Finish();
393 }
virtual void EmitGPRel32Value(const MCExpr *Value)
virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName)
EmitDwarfLocDirective - This implements the DWARF2.
Definition: MCStreamer.cpp:176
static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta)
Utility function to emit the encoding to a streamer.
Definition: MCDwarf.cpp:355
const MCExpr * ForceExpAbs(const MCExpr *Expr)
Definition: MCStreamer.cpp:68
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol)
bool isBundleLocked() const
Definition: MCAssembler.h:643
static const char *const BundlingNotImplementedMsg
void EmitSLEB128IntValue(int64_t Value)
Definition: MCStreamer.cpp:128
virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue)
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size)
virtual bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value)
virtual void EmitBundleAlignMode(unsigned AlignPow2)
Set the bundle alignment mode from now on in the section. The argument is the power of 2 to which the...
ExprKind getKind() const
Definition: MCExpr.h:61
virtual void reset()
Definition: MCStreamer.cpp:43
virtual void EmitBundleLock(bool AlignToEnd)
The following instructions are a bundle-locked group.
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:996
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
static MCFixupKind getKindForSize(unsigned Size, bool isPCRel)
Definition: MCFixup.h:97
F(f)
void RecordProcStart(MCDwarfFrameInfo &Frame)
Definition: MCStreamer.cpp:252
virtual SmallVectorImpl< char > & getContents()
Definition: MCAssembler.h:221
static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol)
Definition: MCDwarf.cpp:729
const MCExpr * BuildSymbolDiff(MCContext &Context, const MCSymbol *A, const MCSymbol *B)
Definition: MCStreamer.cpp:55
virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
virtual void EmitGPRel64Value(const MCExpr *Value)
MCFragment * getCurrentFragment() const
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCAssembler.h:224
static void Make(MCStreamer *MCOS, const MCSection *Section)
Definition: MCDwarf.cpp:65
void insert(MCFragment *F) const
MCSymbol * CreateTempSymbol()
Definition: MCContext.cpp:165
static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta)
Definition: MCDwarf.cpp:1480
#define llvm_unreachable(msg)
virtual void EmitDebugLabel(MCSymbol *Symbol)
MCSectionSubPair getCurrentSection() const
Definition: MCStreamer.h:224
virtual bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCAssembler.h:232
virtual void ChangeSection(const MCSection *Section, const MCExpr *Subsection)
virtual void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0)
MCContext & getContext() const
Definition: MCStreamer.h:168
virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName)
EmitDwarfLocDirective - This implements the DWARF2.
A four-byte gp relative fixup.
Definition: MCFixup.h:33
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:998
MCDataFragment * getOrCreateDataFragment() const
virtual void EncodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups) const =0
MCFragment * getFragment() const
Definition: MCAssembler.h:720
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Definition: MCStreamer.cpp:104
Unary expressions.
Definition: MCExpr.h:37
iterator begin() const
Definition: StringRef.h:97
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
const MCExpr * getExpr() const
Definition: MCInst.h:93
MCObjectStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer, MCAsmBackend &TAB, raw_ostream &_OS, MCCodeEmitter *_Emitter)
const MCExpr * getLHS() const
getLHS - Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:477
static const MCBinaryExpr * Create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.cpp:142
MCAssembler & getAssembler()
virtual void relaxInstruction(const MCInst &Inst, MCInst &Res) const =0
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
static const MCSymbol * Emit(MCStreamer *MCOS)
Definition: MCDwarf.cpp:215
bool getRelaxAll() const
Definition: MCAssembler.h:1013
virtual bool mayNeedRelaxation(const MCInst &Inst) const =0
void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label, int PointerSize)
Definition: MCStreamer.cpp:90
bool isExpr() const
Definition: MCInst.h:59
virtual void EmitInstruction(const MCInst &Inst)
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
MCBinaryExpr - Binary assembler expressions.
Definition: MCExpr.h:356
virtual void EmitInstToFragment(const MCInst &Inst)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
MCSymbolData & getOrCreateSymbolData(const MCSymbol &Symbol, bool *Created=0)
Definition: MCAssembler.h:1151
virtual void EmitBytes(StringRef Data)
virtual SmallVectorImpl< char > & getContents()
Definition: MCAssembler.h:302
virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label)
void setOffset(uint64_t Value)
Definition: MCAssembler.h:724
virtual void EmitLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:212
bool isBundlingEnabled() const
Definition: MCAssembler.h:1019
virtual void EmitLabel(MCSymbol *Symbol)
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:54
void setHasInstructions(bool Value)
Definition: MCAssembler.h:611
const MCExpr * AddValueSymbols(const MCExpr *Value)
virtual void EmitULEB128Value(const MCExpr *Value)
const MCExpr * getRHS() const
getRHS - Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:480
MCSectionData & getOrCreateSectionData(const MCSection &Section, bool *Created=0)
Definition: MCAssembler.h:1134
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
virtual void FinishImpl()
FinishImpl - Streamer specific finalization.
void RecordProcEnd(MCDwarfFrameInfo &Frame)
Definition: MCStreamer.cpp:274
unsigned getNumOperands() const
Definition: MCInst.h:165
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:994
void setFragment(MCFragment *Value)
Definition: MCAssembler.h:721
void resize(unsigned N)
Definition: SmallVector.h:401
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)
References to labels and assigned expressions.
Definition: MCExpr.h:36
void setAlignment(unsigned Value)
Definition: MCAssembler.h:608
virtual void EmitSLEB128Value(const MCExpr *Value)
void EmitULEB128IntValue(uint64_t Value, unsigned Padding=0)
Definition: MCStreamer.cpp:119
LLVM Value Representation.
Definition: Value.h:66
virtual void EmitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros. This function properly handles data in virtual sections.
MCAsmBackend - Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
Constant expressions.
Definition: MCExpr.h:35
Binary expressions.
Definition: MCExpr.h:34
static MCFixup Create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:77
MCSectionData * getCurrentSectionData() const
iterator end() const
Definition: StringRef.h:99
ItTy prior(ItTy it, Dist n)
Definition: STLExtras.h:167
Subtraction.
Definition: MCExpr.h:379
Target specific expression.
Definition: MCExpr.h:38
iterator getSubsectionInsertionPoint(unsigned Subsection)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:163
FragmentListType::iterator iterator
Definition: MCAssembler.h:554
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCAssembler.h:308
virtual void EmitBundleUnlock()
Ends a bundle-locked group.
virtual void reset()
state management