1//===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===//
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
10#include "llvm/MC/MCStreamer.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCAsmBackend.h"
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCObjectFileInfo.h"
18#include "llvm/MC/MCObjectWriter.h"
19#include "llvm/MC/MCSection.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/MC/MCWin64EH.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/LEB128.h"
24#include "llvm/Support/raw_ostream.h"
25#include <cstdlib>
26using namespace llvm;
27
28// Pin the vtables to this file.
29MCTargetStreamer::~MCTargetStreamer() {}
30
31MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
32  S.setTargetStreamer(this);
33}
34
35void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {}
36
37void MCTargetStreamer::finish() {}
38
39void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
40
41MCStreamer::MCStreamer(MCContext &Ctx)
42    : Context(Ctx), CurrentWinFrameInfo(nullptr) {
43  SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
44}
45
46MCStreamer::~MCStreamer() {
47  for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
48    delete WinFrameInfos[i];
49}
50
51void MCStreamer::reset() {
52  DwarfFrameInfos.clear();
53  for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
54    delete WinFrameInfos[i];
55  WinFrameInfos.clear();
56  CurrentWinFrameInfo = nullptr;
57  SymbolOrdering.clear();
58  SectionStack.clear();
59  SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
60}
61
62raw_ostream &MCStreamer::GetCommentOS() {
63  // By default, discard comments.
64  return nulls();
65}
66
67void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {}
68
69void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) {
70  for (auto &FI : DwarfFrameInfos)
71    FI.CompactUnwindEncoding =
72        (MAB ? MAB->generateCompactUnwindEncoding(FI.Instructions) : 0);
73}
74
75/// EmitIntValue - Special case of EmitValue that avoids the client having to
76/// pass in a MCExpr for constant integers.
77void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
78  assert(1 <= Size && Size <= 8 && "Invalid size");
79  assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
80         "Invalid size");
81  char buf[8];
82  const bool isLittleEndian = Context.getAsmInfo()->isLittleEndian();
83  for (unsigned i = 0; i != Size; ++i) {
84    unsigned index = isLittleEndian ? i : (Size - i - 1);
85    buf[i] = uint8_t(Value >> (index * 8));
86  }
87  EmitBytes(StringRef(buf, Size));
88}
89
90/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
91/// client having to pass in a MCExpr for constant integers.
92void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned Padding) {
93  SmallString<128> Tmp;
94  raw_svector_ostream OSE(Tmp);
95  encodeULEB128(Value, OSE, Padding);
96  EmitBytes(OSE.str());
97}
98
99/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
100/// client having to pass in a MCExpr for constant integers.
101void MCStreamer::EmitSLEB128IntValue(int64_t Value) {
102  SmallString<128> Tmp;
103  raw_svector_ostream OSE(Tmp);
104  encodeSLEB128(Value, OSE);
105  EmitBytes(OSE.str());
106}
107
108void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size,
109                           const SMLoc &Loc) {
110  EmitValueImpl(Value, Size, Loc);
111}
112
113void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
114                                 bool IsSectionRelative) {
115  assert((!IsSectionRelative || Size == 4) &&
116         "SectionRelative value requires 4-bytes");
117
118  if (!IsSectionRelative)
119    EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size);
120  else
121    EmitCOFFSecRel32(Sym);
122}
123
124void MCStreamer::EmitGPRel64Value(const MCExpr *Value) {
125  report_fatal_error("unsupported directive in streamer");
126}
127
128void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
129  report_fatal_error("unsupported directive in streamer");
130}
131
132/// EmitFill - Emit NumBytes bytes worth of the value specified by
133/// FillValue.  This implements directives such as '.space'.
134void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
135  const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
136  for (uint64_t i = 0, e = NumBytes; i != e; ++i)
137    EmitValue(E, 1);
138}
139
140/// The implementation in this class just redirects to EmitFill.
141void MCStreamer::EmitZeros(uint64_t NumBytes) {
142  EmitFill(NumBytes, 0);
143}
144
145unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
146                                            StringRef Directory,
147                                            StringRef Filename, unsigned CUID) {
148  return getContext().GetDwarfFile(Directory, Filename, FileNo, CUID);
149}
150
151void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
152                                       unsigned Column, unsigned Flags,
153                                       unsigned Isa,
154                                       unsigned Discriminator,
155                                       StringRef FileName) {
156  getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
157                                  Discriminator);
158}
159
160MCSymbol *MCStreamer::getDwarfLineTableSymbol(unsigned CUID) {
161  MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
162  if (!Table.getLabel()) {
163    StringRef Prefix = Context.getAsmInfo()->getPrivateGlobalPrefix();
164    Table.setLabel(
165        Context.GetOrCreateSymbol(Prefix + "line_table_start" + Twine(CUID)));
166  }
167  return Table.getLabel();
168}
169
170MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() {
171  if (DwarfFrameInfos.empty())
172    return nullptr;
173  return &DwarfFrameInfos.back();
174}
175
176void MCStreamer::EnsureValidDwarfFrame() {
177  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
178  if (!CurFrame || CurFrame->End)
179    report_fatal_error("No open frame");
180}
181
182void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
183                                     MCSymbol *EHSymbol) {
184}
185
186void MCStreamer::InitSections(bool NoExecStack) {
187  SwitchSection(getContext().getObjectFileInfo()->getTextSection());
188}
189
190void MCStreamer::AssignSection(MCSymbol *Symbol, const MCSection *Section) {
191  if (Section)
192    Symbol->setSection(*Section);
193  else
194    Symbol->setUndefined();
195
196  // As we emit symbols into a section, track the order so that they can
197  // be sorted upon later. Zero is reserved to mean 'unemitted'.
198  SymbolOrdering[Symbol] = 1 + SymbolOrdering.size();
199}
200
201void MCStreamer::EmitLabel(MCSymbol *Symbol) {
202  assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
203  assert(getCurrentSection().first && "Cannot emit before setting section!");
204  AssignSection(Symbol, getCurrentSection().first);
205
206  MCTargetStreamer *TS = getTargetStreamer();
207  if (TS)
208    TS->emitLabel(Symbol);
209}
210
211void MCStreamer::EmitCFISections(bool EH, bool Debug) {
212  assert(EH || Debug);
213}
214
215void MCStreamer::EmitCFIStartProc(bool IsSimple) {
216  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
217  if (CurFrame && !CurFrame->End)
218    report_fatal_error("Starting a frame before finishing the previous one!");
219
220  MCDwarfFrameInfo Frame;
221  Frame.IsSimple = IsSimple;
222  EmitCFIStartProcImpl(Frame);
223
224  const MCAsmInfo* MAI = Context.getAsmInfo();
225  if (MAI) {
226    for (const MCCFIInstruction& Inst : MAI->getInitialFrameState()) {
227      if (Inst.getOperation() == MCCFIInstruction::OpDefCfa ||
228          Inst.getOperation() == MCCFIInstruction::OpDefCfaRegister) {
229        Frame.CurrentCfaRegister = Inst.getRegister();
230      }
231    }
232  }
233
234  DwarfFrameInfos.push_back(Frame);
235}
236
237void MCStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
238}
239
240void MCStreamer::EmitCFIEndProc() {
241  EnsureValidDwarfFrame();
242  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
243  EmitCFIEndProcImpl(*CurFrame);
244}
245
246void MCStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
247  // Put a dummy non-null value in Frame.End to mark that this frame has been
248  // closed.
249  Frame.End = (MCSymbol *) 1;
250}
251
252MCSymbol *MCStreamer::EmitCFICommon() {
253  EnsureValidDwarfFrame();
254  MCSymbol *Label = getContext().CreateTempSymbol();
255  EmitLabel(Label);
256  return Label;
257}
258
259void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
260  MCSymbol *Label = EmitCFICommon();
261  MCCFIInstruction Instruction =
262    MCCFIInstruction::createDefCfa(Label, Register, Offset);
263  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
264  CurFrame->Instructions.push_back(Instruction);
265  CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register);
266}
267
268void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
269  MCSymbol *Label = EmitCFICommon();
270  MCCFIInstruction Instruction =
271    MCCFIInstruction::createDefCfaOffset(Label, Offset);
272  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
273  CurFrame->Instructions.push_back(Instruction);
274}
275
276void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
277  MCSymbol *Label = EmitCFICommon();
278  MCCFIInstruction Instruction =
279    MCCFIInstruction::createAdjustCfaOffset(Label, Adjustment);
280  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
281  CurFrame->Instructions.push_back(Instruction);
282}
283
284void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
285  MCSymbol *Label = EmitCFICommon();
286  MCCFIInstruction Instruction =
287    MCCFIInstruction::createDefCfaRegister(Label, Register);
288  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
289  CurFrame->Instructions.push_back(Instruction);
290  CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register);
291}
292
293void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
294  MCSymbol *Label = EmitCFICommon();
295  MCCFIInstruction Instruction =
296    MCCFIInstruction::createOffset(Label, Register, Offset);
297  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
298  CurFrame->Instructions.push_back(Instruction);
299}
300
301void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
302  MCSymbol *Label = EmitCFICommon();
303  MCCFIInstruction Instruction =
304    MCCFIInstruction::createRelOffset(Label, Register, Offset);
305  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
306  CurFrame->Instructions.push_back(Instruction);
307}
308
309void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym,
310                                    unsigned Encoding) {
311  EnsureValidDwarfFrame();
312  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
313  CurFrame->Personality = Sym;
314  CurFrame->PersonalityEncoding = Encoding;
315}
316
317void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
318  EnsureValidDwarfFrame();
319  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
320  CurFrame->Lsda = Sym;
321  CurFrame->LsdaEncoding = Encoding;
322}
323
324void MCStreamer::EmitCFIRememberState() {
325  MCSymbol *Label = EmitCFICommon();
326  MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label);
327  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
328  CurFrame->Instructions.push_back(Instruction);
329}
330
331void MCStreamer::EmitCFIRestoreState() {
332  // FIXME: Error if there is no matching cfi_remember_state.
333  MCSymbol *Label = EmitCFICommon();
334  MCCFIInstruction Instruction = MCCFIInstruction::createRestoreState(Label);
335  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
336  CurFrame->Instructions.push_back(Instruction);
337}
338
339void MCStreamer::EmitCFISameValue(int64_t Register) {
340  MCSymbol *Label = EmitCFICommon();
341  MCCFIInstruction Instruction =
342    MCCFIInstruction::createSameValue(Label, Register);
343  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
344  CurFrame->Instructions.push_back(Instruction);
345}
346
347void MCStreamer::EmitCFIRestore(int64_t Register) {
348  MCSymbol *Label = EmitCFICommon();
349  MCCFIInstruction Instruction =
350    MCCFIInstruction::createRestore(Label, Register);
351  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
352  CurFrame->Instructions.push_back(Instruction);
353}
354
355void MCStreamer::EmitCFIEscape(StringRef Values) {
356  MCSymbol *Label = EmitCFICommon();
357  MCCFIInstruction Instruction = MCCFIInstruction::createEscape(Label, Values);
358  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
359  CurFrame->Instructions.push_back(Instruction);
360}
361
362void MCStreamer::EmitCFISignalFrame() {
363  EnsureValidDwarfFrame();
364  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
365  CurFrame->IsSignalFrame = true;
366}
367
368void MCStreamer::EmitCFIUndefined(int64_t Register) {
369  MCSymbol *Label = EmitCFICommon();
370  MCCFIInstruction Instruction =
371    MCCFIInstruction::createUndefined(Label, Register);
372  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
373  CurFrame->Instructions.push_back(Instruction);
374}
375
376void MCStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
377  MCSymbol *Label = EmitCFICommon();
378  MCCFIInstruction Instruction =
379    MCCFIInstruction::createRegister(Label, Register1, Register2);
380  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
381  CurFrame->Instructions.push_back(Instruction);
382}
383
384void MCStreamer::EmitCFIWindowSave() {
385  MCSymbol *Label = EmitCFICommon();
386  MCCFIInstruction Instruction =
387    MCCFIInstruction::createWindowSave(Label);
388  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
389  CurFrame->Instructions.push_back(Instruction);
390}
391
392void MCStreamer::EnsureValidWinFrameInfo() {
393  if (!CurrentWinFrameInfo || CurrentWinFrameInfo->End)
394    report_fatal_error("No open Win64 EH frame function!");
395}
396
397void MCStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol) {
398  if (CurrentWinFrameInfo && !CurrentWinFrameInfo->End)
399    report_fatal_error("Starting a function before ending the previous one!");
400
401  MCSymbol *StartProc = getContext().CreateTempSymbol();
402  EmitLabel(StartProc);
403
404  WinFrameInfos.push_back(new WinEH::FrameInfo(Symbol, StartProc));
405  CurrentWinFrameInfo = WinFrameInfos.back();
406}
407
408void MCStreamer::EmitWinCFIEndProc() {
409  EnsureValidWinFrameInfo();
410  if (CurrentWinFrameInfo->ChainedParent)
411    report_fatal_error("Not all chained regions terminated!");
412
413  MCSymbol *Label = getContext().CreateTempSymbol();
414  EmitLabel(Label);
415  CurrentWinFrameInfo->End = Label;
416}
417
418void MCStreamer::EmitWinCFIStartChained() {
419  EnsureValidWinFrameInfo();
420
421  MCSymbol *StartProc = getContext().CreateTempSymbol();
422  EmitLabel(StartProc);
423
424  WinFrameInfos.push_back(new WinEH::FrameInfo(CurrentWinFrameInfo->Function,
425                                               StartProc, CurrentWinFrameInfo));
426  CurrentWinFrameInfo = WinFrameInfos.back();
427}
428
429void MCStreamer::EmitWinCFIEndChained() {
430  EnsureValidWinFrameInfo();
431  if (!CurrentWinFrameInfo->ChainedParent)
432    report_fatal_error("End of a chained region outside a chained region!");
433
434  MCSymbol *Label = getContext().CreateTempSymbol();
435  EmitLabel(Label);
436
437  CurrentWinFrameInfo->End = Label;
438  CurrentWinFrameInfo =
439      const_cast<WinEH::FrameInfo *>(CurrentWinFrameInfo->ChainedParent);
440}
441
442void MCStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
443                                  bool Except) {
444  EnsureValidWinFrameInfo();
445  if (CurrentWinFrameInfo->ChainedParent)
446    report_fatal_error("Chained unwind areas can't have handlers!");
447  CurrentWinFrameInfo->ExceptionHandler = Sym;
448  if (!Except && !Unwind)
449    report_fatal_error("Don't know what kind of handler this is!");
450  if (Unwind)
451    CurrentWinFrameInfo->HandlesUnwind = true;
452  if (Except)
453    CurrentWinFrameInfo->HandlesExceptions = true;
454}
455
456void MCStreamer::EmitWinEHHandlerData() {
457  EnsureValidWinFrameInfo();
458  if (CurrentWinFrameInfo->ChainedParent)
459    report_fatal_error("Chained unwind areas can't have handlers!");
460}
461
462void MCStreamer::EmitWinCFIPushReg(unsigned Register) {
463  EnsureValidWinFrameInfo();
464
465  MCSymbol *Label = getContext().CreateTempSymbol();
466  EmitLabel(Label);
467
468  WinEH::Instruction Inst = Win64EH::Instruction::PushNonVol(Label, Register);
469  CurrentWinFrameInfo->Instructions.push_back(Inst);
470}
471
472void MCStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset) {
473  EnsureValidWinFrameInfo();
474  if (CurrentWinFrameInfo->LastFrameInst >= 0)
475    report_fatal_error("Frame register and offset already specified!");
476  if (Offset & 0x0F)
477    report_fatal_error("Misaligned frame pointer offset!");
478  if (Offset > 240)
479    report_fatal_error("Frame offset must be less than or equal to 240!");
480
481  MCSymbol *Label = getContext().CreateTempSymbol();
482  EmitLabel(Label);
483
484  WinEH::Instruction Inst =
485      Win64EH::Instruction::SetFPReg(Label, Register, Offset);
486  CurrentWinFrameInfo->LastFrameInst = CurrentWinFrameInfo->Instructions.size();
487  CurrentWinFrameInfo->Instructions.push_back(Inst);
488}
489
490void MCStreamer::EmitWinCFIAllocStack(unsigned Size) {
491  EnsureValidWinFrameInfo();
492  if (Size == 0)
493    report_fatal_error("Allocation size must be non-zero!");
494  if (Size & 7)
495    report_fatal_error("Misaligned stack allocation!");
496
497  MCSymbol *Label = getContext().CreateTempSymbol();
498  EmitLabel(Label);
499
500  WinEH::Instruction Inst = Win64EH::Instruction::Alloc(Label, Size);
501  CurrentWinFrameInfo->Instructions.push_back(Inst);
502}
503
504void MCStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset) {
505  EnsureValidWinFrameInfo();
506  if (Offset & 7)
507    report_fatal_error("Misaligned saved register offset!");
508
509  MCSymbol *Label = getContext().CreateTempSymbol();
510  EmitLabel(Label);
511
512  WinEH::Instruction Inst =
513      Win64EH::Instruction::SaveNonVol(Label, Register, Offset);
514  CurrentWinFrameInfo->Instructions.push_back(Inst);
515}
516
517void MCStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset) {
518  EnsureValidWinFrameInfo();
519  if (Offset & 0x0F)
520    report_fatal_error("Misaligned saved vector register offset!");
521
522  MCSymbol *Label = getContext().CreateTempSymbol();
523  EmitLabel(Label);
524
525  WinEH::Instruction Inst =
526      Win64EH::Instruction::SaveXMM(Label, Register, Offset);
527  CurrentWinFrameInfo->Instructions.push_back(Inst);
528}
529
530void MCStreamer::EmitWinCFIPushFrame(bool Code) {
531  EnsureValidWinFrameInfo();
532  if (CurrentWinFrameInfo->Instructions.size() > 0)
533    report_fatal_error("If present, PushMachFrame must be the first UOP");
534
535  MCSymbol *Label = getContext().CreateTempSymbol();
536  EmitLabel(Label);
537
538  WinEH::Instruction Inst = Win64EH::Instruction::PushMachFrame(Label, Code);
539  CurrentWinFrameInfo->Instructions.push_back(Inst);
540}
541
542void MCStreamer::EmitWinCFIEndProlog() {
543  EnsureValidWinFrameInfo();
544
545  MCSymbol *Label = getContext().CreateTempSymbol();
546  EmitLabel(Label);
547
548  CurrentWinFrameInfo->PrologEnd = Label;
549}
550
551void MCStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
552}
553
554void MCStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
555}
556
557/// EmitRawText - If this file is backed by an assembly streamer, this dumps
558/// the specified string in the output .s file.  This capability is
559/// indicated by the hasRawTextSupport() predicate.
560void MCStreamer::EmitRawTextImpl(StringRef String) {
561  errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
562  " something must not be fully mc'ized\n";
563  abort();
564}
565
566void MCStreamer::EmitRawText(const Twine &T) {
567  SmallString<128> Str;
568  EmitRawTextImpl(T.toStringRef(Str));
569}
570
571void MCStreamer::EmitWindowsUnwindTables() {
572}
573
574void MCStreamer::Finish() {
575  if (!DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End)
576    report_fatal_error("Unfinished frame!");
577
578  MCTargetStreamer *TS = getTargetStreamer();
579  if (TS)
580    TS->finish();
581
582  FinishImpl();
583}
584
585void MCStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
586  visitUsedExpr(*Value);
587  Symbol->setVariableValue(Value);
588
589  MCTargetStreamer *TS = getTargetStreamer();
590  if (TS)
591    TS->emitAssignment(Symbol, Value);
592}
593
594void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) {
595}
596
597void MCStreamer::visitUsedExpr(const MCExpr &Expr) {
598  switch (Expr.getKind()) {
599  case MCExpr::Target:
600    cast<MCTargetExpr>(Expr).visitUsedExpr(*this);
601    break;
602
603  case MCExpr::Constant:
604    break;
605
606  case MCExpr::Binary: {
607    const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr);
608    visitUsedExpr(*BE.getLHS());
609    visitUsedExpr(*BE.getRHS());
610    break;
611  }
612
613  case MCExpr::SymbolRef:
614    visitUsedSymbol(cast<MCSymbolRefExpr>(Expr).getSymbol());
615    break;
616
617  case MCExpr::Unary:
618    visitUsedExpr(*cast<MCUnaryExpr>(Expr).getSubExpr());
619    break;
620  }
621}
622
623void MCStreamer::EmitInstruction(const MCInst &Inst,
624                                 const MCSubtargetInfo &STI) {
625  // Scan for values.
626  for (unsigned i = Inst.getNumOperands(); i--;)
627    if (Inst.getOperand(i).isExpr())
628      visitUsedExpr(*Inst.getOperand(i).getExpr());
629}
630
631void MCStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
632void MCStreamer::EmitThumbFunc(MCSymbol *Func) {}
633void MCStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
634void MCStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
635void MCStreamer::EndCOFFSymbolDef() {}
636void MCStreamer::EmitFileDirective(StringRef Filename) {}
637void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {}
638void MCStreamer::EmitCOFFSymbolType(int Type) {}
639void MCStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
640void MCStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
641                                       unsigned ByteAlignment) {}
642void MCStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
643                                uint64_t Size, unsigned ByteAlignment) {}
644void MCStreamer::ChangeSection(const MCSection *, const MCExpr *) {}
645void MCStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
646void MCStreamer::EmitBytes(StringRef Data) {}
647void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
648                               const SMLoc &Loc) {
649  visitUsedExpr(*Value);
650}
651void MCStreamer::EmitULEB128Value(const MCExpr *Value) {}
652void MCStreamer::EmitSLEB128Value(const MCExpr *Value) {}
653void MCStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
654                                      unsigned ValueSize,
655                                      unsigned MaxBytesToEmit) {}
656void MCStreamer::EmitCodeAlignment(unsigned ByteAlignment,
657                                   unsigned MaxBytesToEmit) {}
658bool MCStreamer::EmitValueToOffset(const MCExpr *Offset, unsigned char Value) {
659  return false;
660}
661void MCStreamer::EmitBundleAlignMode(unsigned AlignPow2) {}
662void MCStreamer::EmitBundleLock(bool AlignToEnd) {}
663void MCStreamer::FinishImpl() {}
664void MCStreamer::EmitBundleUnlock() {}
665
666void MCStreamer::SwitchSection(const MCSection *Section,
667                               const MCExpr *Subsection) {
668  assert(Section && "Cannot switch to a null section!");
669  MCSectionSubPair curSection = SectionStack.back().first;
670  SectionStack.back().second = curSection;
671  if (MCSectionSubPair(Section, Subsection) != curSection) {
672    SectionStack.back().first = MCSectionSubPair(Section, Subsection);
673    assert(!Section->hasEnded() && "Section already ended");
674    ChangeSection(Section, Subsection);
675    MCSymbol *Sym = Section->getBeginSymbol();
676    if (Sym && !Sym->isInSection())
677      EmitLabel(Sym);
678  }
679}
680
681MCSymbol *MCStreamer::endSection(const MCSection *Section) {
682  // TODO: keep track of the last subsection so that this symbol appears in the
683  // correct place.
684  MCSymbol *Sym = Section->getEndSymbol(Context);
685  if (Sym->isInSection())
686    return Sym;
687
688  SwitchSection(Section);
689  EmitLabel(Sym);
690  return Sym;
691}
692