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/MCInst.h"
18#include "llvm/MC/MCInstPrinter.h"
19#include "llvm/MC/MCObjectFileInfo.h"
20#include "llvm/MC/MCObjectWriter.h"
21#include "llvm/MC/MCSection.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/MC/MCWin64EH.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/LEB128.h"
26#include "llvm/Support/raw_ostream.h"
27#include <cstdlib>
28using namespace llvm;
29
30// Pin the vtables to this file.
31MCTargetStreamer::~MCTargetStreamer() {}
32
33MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
34  S.setTargetStreamer(this);
35}
36
37void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {}
38
39void MCTargetStreamer::finish() {}
40
41void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
42
43MCStreamer::MCStreamer(MCContext &Ctx)
44    : Context(Ctx), CurrentWinFrameInfo(nullptr) {
45  SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
46}
47
48MCStreamer::~MCStreamer() {
49  for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
50    delete WinFrameInfos[i];
51}
52
53void MCStreamer::reset() {
54  DwarfFrameInfos.clear();
55  for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
56    delete WinFrameInfos[i];
57  WinFrameInfos.clear();
58  CurrentWinFrameInfo = nullptr;
59  SymbolOrdering.clear();
60  SectionStack.clear();
61  SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
62}
63
64raw_ostream &MCStreamer::GetCommentOS() {
65  // By default, discard comments.
66  return nulls();
67}
68
69void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {}
70
71void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) {
72  for (auto &FI : DwarfFrameInfos)
73    FI.CompactUnwindEncoding =
74        (MAB ? MAB->generateCompactUnwindEncoding(FI.Instructions) : 0);
75}
76
77/// EmitIntValue - Special case of EmitValue that avoids the client having to
78/// pass in a MCExpr for constant integers.
79void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
80  assert(1 <= Size && Size <= 8 && "Invalid size");
81  assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
82         "Invalid size");
83  char buf[8];
84  const bool isLittleEndian = Context.getAsmInfo()->isLittleEndian();
85  for (unsigned i = 0; i != Size; ++i) {
86    unsigned index = isLittleEndian ? i : (Size - i - 1);
87    buf[i] = uint8_t(Value >> (index * 8));
88  }
89  EmitBytes(StringRef(buf, Size));
90}
91
92/// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
93/// client having to pass in a MCExpr for constant integers.
94void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned Padding) {
95  SmallString<128> Tmp;
96  raw_svector_ostream OSE(Tmp);
97  encodeULEB128(Value, OSE, Padding);
98  EmitBytes(OSE.str());
99}
100
101/// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
102/// client having to pass in a MCExpr for constant integers.
103void MCStreamer::EmitSLEB128IntValue(int64_t Value) {
104  SmallString<128> Tmp;
105  raw_svector_ostream OSE(Tmp);
106  encodeSLEB128(Value, OSE);
107  EmitBytes(OSE.str());
108}
109
110void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc) {
111  EmitValueImpl(Value, Size, Loc);
112}
113
114void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
115                                 bool IsSectionRelative) {
116  assert((!IsSectionRelative || Size == 4) &&
117         "SectionRelative value requires 4-bytes");
118
119  if (!IsSectionRelative)
120    EmitValueImpl(MCSymbolRefExpr::create(Sym, getContext()), Size);
121  else
122    EmitCOFFSecRel32(Sym);
123}
124
125void MCStreamer::EmitGPRel64Value(const MCExpr *Value) {
126  report_fatal_error("unsupported directive in streamer");
127}
128
129void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
130  report_fatal_error("unsupported directive in streamer");
131}
132
133/// EmitFill - Emit NumBytes bytes worth of the value specified by
134/// FillValue.  This implements directives such as '.space'.
135void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
136  const MCExpr *E = MCConstantExpr::create(FillValue, getContext());
137  for (uint64_t i = 0, e = NumBytes; i != e; ++i)
138    EmitValue(E, 1);
139}
140
141/// The implementation in this class just redirects to EmitFill.
142void MCStreamer::EmitZeros(uint64_t NumBytes) {
143  EmitFill(NumBytes, 0);
144}
145
146unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
147                                            StringRef Directory,
148                                            StringRef Filename, unsigned CUID) {
149  return getContext().getDwarfFile(Directory, Filename, FileNo, CUID);
150}
151
152void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
153                                       unsigned Column, unsigned Flags,
154                                       unsigned Isa,
155                                       unsigned Discriminator,
156                                       StringRef FileName) {
157  getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
158                                  Discriminator);
159}
160
161MCSymbol *MCStreamer::getDwarfLineTableSymbol(unsigned CUID) {
162  MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
163  if (!Table.getLabel()) {
164    StringRef Prefix = Context.getAsmInfo()->getPrivateGlobalPrefix();
165    Table.setLabel(
166        Context.getOrCreateSymbol(Prefix + "line_table_start" + Twine(CUID)));
167  }
168  return Table.getLabel();
169}
170
171MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() {
172  if (DwarfFrameInfos.empty())
173    return nullptr;
174  return &DwarfFrameInfos.back();
175}
176
177void MCStreamer::EnsureValidDwarfFrame() {
178  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
179  if (!CurFrame || CurFrame->End)
180    report_fatal_error("No open frame");
181}
182
183void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
184                                     MCSymbol *EHSymbol) {
185}
186
187void MCStreamer::InitSections(bool NoExecStack) {
188  SwitchSection(getContext().getObjectFileInfo()->getTextSection());
189}
190
191void MCStreamer::AssignFragment(MCSymbol *Symbol, MCFragment *Fragment) {
192  assert(Fragment);
193  Symbol->setFragment(Fragment);
194
195  // As we emit symbols into a section, track the order so that they can
196  // be sorted upon later. Zero is reserved to mean 'unemitted'.
197  SymbolOrdering[Symbol] = 1 + SymbolOrdering.size();
198}
199
200void MCStreamer::EmitLabel(MCSymbol *Symbol) {
201  assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
202  assert(getCurrentSection().first && "Cannot emit before setting section!");
203  assert(!Symbol->getFragment() && "Unexpected fragment on symbol data!");
204  Symbol->setFragment(&getCurrentSectionOnly()->getDummyFragment());
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::EmitCFIGnuArgsSize(int64_t Size) {
363  MCSymbol *Label = EmitCFICommon();
364  MCCFIInstruction Instruction =
365    MCCFIInstruction::createGnuArgsSize(Label, Size);
366  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
367  CurFrame->Instructions.push_back(Instruction);
368}
369
370void MCStreamer::EmitCFISignalFrame() {
371  EnsureValidDwarfFrame();
372  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
373  CurFrame->IsSignalFrame = true;
374}
375
376void MCStreamer::EmitCFIUndefined(int64_t Register) {
377  MCSymbol *Label = EmitCFICommon();
378  MCCFIInstruction Instruction =
379    MCCFIInstruction::createUndefined(Label, Register);
380  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
381  CurFrame->Instructions.push_back(Instruction);
382}
383
384void MCStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
385  MCSymbol *Label = EmitCFICommon();
386  MCCFIInstruction Instruction =
387    MCCFIInstruction::createRegister(Label, Register1, Register2);
388  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
389  CurFrame->Instructions.push_back(Instruction);
390}
391
392void MCStreamer::EmitCFIWindowSave() {
393  MCSymbol *Label = EmitCFICommon();
394  MCCFIInstruction Instruction =
395    MCCFIInstruction::createWindowSave(Label);
396  MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
397  CurFrame->Instructions.push_back(Instruction);
398}
399
400void MCStreamer::EnsureValidWinFrameInfo() {
401  const MCAsmInfo *MAI = Context.getAsmInfo();
402  if (!MAI->usesWindowsCFI())
403    report_fatal_error(".seh_* directives are not supported on this target");
404  if (!CurrentWinFrameInfo || CurrentWinFrameInfo->End)
405    report_fatal_error("No open Win64 EH frame function!");
406}
407
408void MCStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol) {
409  const MCAsmInfo *MAI = Context.getAsmInfo();
410  if (!MAI->usesWindowsCFI())
411    report_fatal_error(".seh_* directives are not supported on this target");
412  if (CurrentWinFrameInfo && !CurrentWinFrameInfo->End)
413    report_fatal_error("Starting a function before ending the previous one!");
414
415  MCSymbol *StartProc = getContext().createTempSymbol();
416  EmitLabel(StartProc);
417
418  WinFrameInfos.push_back(new WinEH::FrameInfo(Symbol, StartProc));
419  CurrentWinFrameInfo = WinFrameInfos.back();
420}
421
422void MCStreamer::EmitWinCFIEndProc() {
423  EnsureValidWinFrameInfo();
424  if (CurrentWinFrameInfo->ChainedParent)
425    report_fatal_error("Not all chained regions terminated!");
426
427  MCSymbol *Label = getContext().createTempSymbol();
428  EmitLabel(Label);
429  CurrentWinFrameInfo->End = Label;
430}
431
432void MCStreamer::EmitWinCFIStartChained() {
433  EnsureValidWinFrameInfo();
434
435  MCSymbol *StartProc = getContext().createTempSymbol();
436  EmitLabel(StartProc);
437
438  WinFrameInfos.push_back(new WinEH::FrameInfo(CurrentWinFrameInfo->Function,
439                                               StartProc, CurrentWinFrameInfo));
440  CurrentWinFrameInfo = WinFrameInfos.back();
441}
442
443void MCStreamer::EmitWinCFIEndChained() {
444  EnsureValidWinFrameInfo();
445  if (!CurrentWinFrameInfo->ChainedParent)
446    report_fatal_error("End of a chained region outside a chained region!");
447
448  MCSymbol *Label = getContext().createTempSymbol();
449  EmitLabel(Label);
450
451  CurrentWinFrameInfo->End = Label;
452  CurrentWinFrameInfo =
453      const_cast<WinEH::FrameInfo *>(CurrentWinFrameInfo->ChainedParent);
454}
455
456void MCStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
457                                  bool Except) {
458  EnsureValidWinFrameInfo();
459  if (CurrentWinFrameInfo->ChainedParent)
460    report_fatal_error("Chained unwind areas can't have handlers!");
461  CurrentWinFrameInfo->ExceptionHandler = Sym;
462  if (!Except && !Unwind)
463    report_fatal_error("Don't know what kind of handler this is!");
464  if (Unwind)
465    CurrentWinFrameInfo->HandlesUnwind = true;
466  if (Except)
467    CurrentWinFrameInfo->HandlesExceptions = true;
468}
469
470void MCStreamer::EmitWinEHHandlerData() {
471  EnsureValidWinFrameInfo();
472  if (CurrentWinFrameInfo->ChainedParent)
473    report_fatal_error("Chained unwind areas can't have handlers!");
474}
475
476void MCStreamer::EmitSyntaxDirective() {}
477
478void MCStreamer::EmitWinCFIPushReg(unsigned Register) {
479  EnsureValidWinFrameInfo();
480
481  MCSymbol *Label = getContext().createTempSymbol();
482  EmitLabel(Label);
483
484  WinEH::Instruction Inst = Win64EH::Instruction::PushNonVol(Label, Register);
485  CurrentWinFrameInfo->Instructions.push_back(Inst);
486}
487
488void MCStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset) {
489  EnsureValidWinFrameInfo();
490  if (CurrentWinFrameInfo->LastFrameInst >= 0)
491    report_fatal_error("Frame register and offset already specified!");
492  if (Offset & 0x0F)
493    report_fatal_error("Misaligned frame pointer offset!");
494  if (Offset > 240)
495    report_fatal_error("Frame offset must be less than or equal to 240!");
496
497  MCSymbol *Label = getContext().createTempSymbol();
498  EmitLabel(Label);
499
500  WinEH::Instruction Inst =
501      Win64EH::Instruction::SetFPReg(Label, Register, Offset);
502  CurrentWinFrameInfo->LastFrameInst = CurrentWinFrameInfo->Instructions.size();
503  CurrentWinFrameInfo->Instructions.push_back(Inst);
504}
505
506void MCStreamer::EmitWinCFIAllocStack(unsigned Size) {
507  EnsureValidWinFrameInfo();
508  if (Size == 0)
509    report_fatal_error("Allocation size must be non-zero!");
510  if (Size & 7)
511    report_fatal_error("Misaligned stack allocation!");
512
513  MCSymbol *Label = getContext().createTempSymbol();
514  EmitLabel(Label);
515
516  WinEH::Instruction Inst = Win64EH::Instruction::Alloc(Label, Size);
517  CurrentWinFrameInfo->Instructions.push_back(Inst);
518}
519
520void MCStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset) {
521  EnsureValidWinFrameInfo();
522  if (Offset & 7)
523    report_fatal_error("Misaligned saved register offset!");
524
525  MCSymbol *Label = getContext().createTempSymbol();
526  EmitLabel(Label);
527
528  WinEH::Instruction Inst =
529      Win64EH::Instruction::SaveNonVol(Label, Register, Offset);
530  CurrentWinFrameInfo->Instructions.push_back(Inst);
531}
532
533void MCStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset) {
534  EnsureValidWinFrameInfo();
535  if (Offset & 0x0F)
536    report_fatal_error("Misaligned saved vector register offset!");
537
538  MCSymbol *Label = getContext().createTempSymbol();
539  EmitLabel(Label);
540
541  WinEH::Instruction Inst =
542      Win64EH::Instruction::SaveXMM(Label, Register, Offset);
543  CurrentWinFrameInfo->Instructions.push_back(Inst);
544}
545
546void MCStreamer::EmitWinCFIPushFrame(bool Code) {
547  EnsureValidWinFrameInfo();
548  if (CurrentWinFrameInfo->Instructions.size() > 0)
549    report_fatal_error("If present, PushMachFrame must be the first UOP");
550
551  MCSymbol *Label = getContext().createTempSymbol();
552  EmitLabel(Label);
553
554  WinEH::Instruction Inst = Win64EH::Instruction::PushMachFrame(Label, Code);
555  CurrentWinFrameInfo->Instructions.push_back(Inst);
556}
557
558void MCStreamer::EmitWinCFIEndProlog() {
559  EnsureValidWinFrameInfo();
560
561  MCSymbol *Label = getContext().createTempSymbol();
562  EmitLabel(Label);
563
564  CurrentWinFrameInfo->PrologEnd = Label;
565}
566
567void MCStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
568}
569
570void MCStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
571}
572
573void MCStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
574}
575
576/// EmitRawText - If this file is backed by an assembly streamer, this dumps
577/// the specified string in the output .s file.  This capability is
578/// indicated by the hasRawTextSupport() predicate.
579void MCStreamer::EmitRawTextImpl(StringRef String) {
580  errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
581  " something must not be fully mc'ized\n";
582  abort();
583}
584
585void MCStreamer::EmitRawText(const Twine &T) {
586  SmallString<128> Str;
587  EmitRawTextImpl(T.toStringRef(Str));
588}
589
590void MCStreamer::EmitWindowsUnwindTables() {
591}
592
593void MCStreamer::Finish() {
594  if (!DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End)
595    report_fatal_error("Unfinished frame!");
596
597  MCTargetStreamer *TS = getTargetStreamer();
598  if (TS)
599    TS->finish();
600
601  FinishImpl();
602}
603
604void MCStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
605  visitUsedExpr(*Value);
606  Symbol->setVariableValue(Value);
607
608  MCTargetStreamer *TS = getTargetStreamer();
609  if (TS)
610    TS->emitAssignment(Symbol, Value);
611}
612
613void MCTargetStreamer::prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
614                              const MCInst &Inst, const MCSubtargetInfo &STI) {
615  InstPrinter.printInst(&Inst, OS, "", STI);
616}
617
618void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) {
619}
620
621void MCStreamer::visitUsedExpr(const MCExpr &Expr) {
622  switch (Expr.getKind()) {
623  case MCExpr::Target:
624    cast<MCTargetExpr>(Expr).visitUsedExpr(*this);
625    break;
626
627  case MCExpr::Constant:
628    break;
629
630  case MCExpr::Binary: {
631    const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr);
632    visitUsedExpr(*BE.getLHS());
633    visitUsedExpr(*BE.getRHS());
634    break;
635  }
636
637  case MCExpr::SymbolRef:
638    visitUsedSymbol(cast<MCSymbolRefExpr>(Expr).getSymbol());
639    break;
640
641  case MCExpr::Unary:
642    visitUsedExpr(*cast<MCUnaryExpr>(Expr).getSubExpr());
643    break;
644  }
645}
646
647void MCStreamer::EmitInstruction(const MCInst &Inst,
648                                 const MCSubtargetInfo &STI) {
649  // Scan for values.
650  for (unsigned i = Inst.getNumOperands(); i--;)
651    if (Inst.getOperand(i).isExpr())
652      visitUsedExpr(*Inst.getOperand(i).getExpr());
653}
654
655void MCStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
656                                        unsigned Size) {
657  // Get the Hi-Lo expression.
658  const MCExpr *Diff =
659      MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context),
660                              MCSymbolRefExpr::create(Lo, Context), Context);
661
662  const MCAsmInfo *MAI = Context.getAsmInfo();
663  if (!MAI->doesSetDirectiveSuppressesReloc()) {
664    EmitValue(Diff, Size);
665    return;
666  }
667
668  // Otherwise, emit with .set (aka assignment).
669  MCSymbol *SetLabel = Context.createTempSymbol("set", true);
670  EmitAssignment(SetLabel, Diff);
671  EmitSymbolValue(SetLabel, Size);
672}
673
674void MCStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
675void MCStreamer::EmitThumbFunc(MCSymbol *Func) {}
676void MCStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
677void MCStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
678void MCStreamer::EndCOFFSymbolDef() {}
679void MCStreamer::EmitFileDirective(StringRef Filename) {}
680void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {}
681void MCStreamer::EmitCOFFSymbolType(int Type) {}
682void MCStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {}
683void MCStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
684                                       unsigned ByteAlignment) {}
685void MCStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
686                                uint64_t Size, unsigned ByteAlignment) {}
687void MCStreamer::ChangeSection(MCSection *, const MCExpr *) {}
688void MCStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
689void MCStreamer::EmitBytes(StringRef Data) {}
690void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) {
691  visitUsedExpr(*Value);
692}
693void MCStreamer::EmitULEB128Value(const MCExpr *Value) {}
694void MCStreamer::EmitSLEB128Value(const MCExpr *Value) {}
695void MCStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
696                                      unsigned ValueSize,
697                                      unsigned MaxBytesToEmit) {}
698void MCStreamer::EmitCodeAlignment(unsigned ByteAlignment,
699                                   unsigned MaxBytesToEmit) {}
700void MCStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value) {}
701void MCStreamer::EmitBundleAlignMode(unsigned AlignPow2) {}
702void MCStreamer::EmitBundleLock(bool AlignToEnd) {}
703void MCStreamer::FinishImpl() {}
704void MCStreamer::EmitBundleUnlock() {}
705
706void MCStreamer::SwitchSection(MCSection *Section, const MCExpr *Subsection) {
707  assert(Section && "Cannot switch to a null section!");
708  MCSectionSubPair curSection = SectionStack.back().first;
709  SectionStack.back().second = curSection;
710  if (MCSectionSubPair(Section, Subsection) != curSection) {
711    ChangeSection(Section, Subsection);
712    SectionStack.back().first = MCSectionSubPair(Section, Subsection);
713    assert(!Section->hasEnded() && "Section already ended");
714    MCSymbol *Sym = Section->getBeginSymbol();
715    if (Sym && !Sym->isInSection())
716      EmitLabel(Sym);
717  }
718}
719
720MCSymbol *MCStreamer::endSection(MCSection *Section) {
721  // TODO: keep track of the last subsection so that this symbol appears in the
722  // correct place.
723  MCSymbol *Sym = Section->getEndSymbol(Context);
724  if (Sym->isInSection())
725    return Sym;
726
727  SwitchSection(Section);
728  EmitLabel(Sym);
729  return Sym;
730}
731