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