• 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/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>
26 using namespace llvm;
27 
28 // Pin the vtables to this file.
~MCTargetStreamer()29 MCTargetStreamer::~MCTargetStreamer() {}
30 
MCTargetStreamer(MCStreamer & S)31 MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
32   S.setTargetStreamer(this);
33 }
34 
emitLabel(MCSymbol * Symbol)35 void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {}
36 
finish()37 void MCTargetStreamer::finish() {}
38 
emitAssignment(MCSymbol * Symbol,const MCExpr * Value)39 void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
40 
MCStreamer(MCContext & Ctx)41 MCStreamer::MCStreamer(MCContext &Ctx)
42     : Context(Ctx), CurrentWinFrameInfo(nullptr) {
43   SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
44 }
45 
~MCStreamer()46 MCStreamer::~MCStreamer() {
47   for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
48     delete WinFrameInfos[i];
49 }
50 
reset()51 void 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 
GetCommentOS()62 raw_ostream &MCStreamer::GetCommentOS() {
63   // By default, discard comments.
64   return nulls();
65 }
66 
emitRawComment(const Twine & T,bool TabPrefix)67 void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {}
68 
generateCompactUnwindEncodings(MCAsmBackend * MAB)69 void 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.
EmitIntValue(uint64_t Value,unsigned Size)77 void 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.
EmitULEB128IntValue(uint64_t Value,unsigned Padding)92 void 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.
EmitSLEB128IntValue(int64_t Value)101 void 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 
EmitValue(const MCExpr * Value,unsigned Size,const SMLoc & Loc)108 void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size,
109                            const SMLoc &Loc) {
110   EmitValueImpl(Value, Size, Loc);
111 }
112 
EmitSymbolValue(const MCSymbol * Sym,unsigned Size,bool IsSectionRelative)113 void 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 
EmitGPRel64Value(const MCExpr * Value)124 void MCStreamer::EmitGPRel64Value(const MCExpr *Value) {
125   report_fatal_error("unsupported directive in streamer");
126 }
127 
EmitGPRel32Value(const MCExpr * Value)128 void 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'.
EmitFill(uint64_t NumBytes,uint8_t FillValue)134 void 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.
EmitZeros(uint64_t NumBytes)141 void MCStreamer::EmitZeros(uint64_t NumBytes) {
142   EmitFill(NumBytes, 0);
143 }
144 
EmitDwarfFileDirective(unsigned FileNo,StringRef Directory,StringRef Filename,unsigned CUID)145 unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
146                                             StringRef Directory,
147                                             StringRef Filename, unsigned CUID) {
148   return getContext().GetDwarfFile(Directory, Filename, FileNo, CUID);
149 }
150 
EmitDwarfLocDirective(unsigned FileNo,unsigned Line,unsigned Column,unsigned Flags,unsigned Isa,unsigned Discriminator,StringRef FileName)151 void 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 
getDwarfLineTableSymbol(unsigned CUID)160 MCSymbol *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 
getCurrentDwarfFrameInfo()170 MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() {
171   if (DwarfFrameInfos.empty())
172     return nullptr;
173   return &DwarfFrameInfos.back();
174 }
175 
EnsureValidDwarfFrame()176 void MCStreamer::EnsureValidDwarfFrame() {
177   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
178   if (!CurFrame || CurFrame->End)
179     report_fatal_error("No open frame");
180 }
181 
EmitEHSymAttributes(const MCSymbol * Symbol,MCSymbol * EHSymbol)182 void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
183                                      MCSymbol *EHSymbol) {
184 }
185 
InitSections(bool NoExecStack)186 void MCStreamer::InitSections(bool NoExecStack) {
187   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
188 }
189 
AssignSection(MCSymbol * Symbol,const MCSection * Section)190 void 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 
EmitLabel(MCSymbol * Symbol)201 void 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 
EmitCFISections(bool EH,bool Debug)211 void MCStreamer::EmitCFISections(bool EH, bool Debug) {
212   assert(EH || Debug);
213 }
214 
EmitCFIStartProc(bool IsSimple)215 void 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 
EmitCFIStartProcImpl(MCDwarfFrameInfo & Frame)237 void MCStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
238 }
239 
EmitCFIEndProc()240 void MCStreamer::EmitCFIEndProc() {
241   EnsureValidDwarfFrame();
242   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
243   EmitCFIEndProcImpl(*CurFrame);
244 }
245 
EmitCFIEndProcImpl(MCDwarfFrameInfo & Frame)246 void 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 
EmitCFICommon()252 MCSymbol *MCStreamer::EmitCFICommon() {
253   EnsureValidDwarfFrame();
254   MCSymbol *Label = getContext().CreateTempSymbol();
255   EmitLabel(Label);
256   return Label;
257 }
258 
EmitCFIDefCfa(int64_t Register,int64_t Offset)259 void 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 
EmitCFIDefCfaOffset(int64_t Offset)268 void 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 
EmitCFIAdjustCfaOffset(int64_t Adjustment)276 void 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 
EmitCFIDefCfaRegister(int64_t Register)284 void 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 
EmitCFIOffset(int64_t Register,int64_t Offset)293 void 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 
EmitCFIRelOffset(int64_t Register,int64_t Offset)301 void 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 
EmitCFIPersonality(const MCSymbol * Sym,unsigned Encoding)309 void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym,
310                                     unsigned Encoding) {
311   EnsureValidDwarfFrame();
312   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
313   CurFrame->Personality = Sym;
314   CurFrame->PersonalityEncoding = Encoding;
315 }
316 
EmitCFILsda(const MCSymbol * Sym,unsigned Encoding)317 void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
318   EnsureValidDwarfFrame();
319   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
320   CurFrame->Lsda = Sym;
321   CurFrame->LsdaEncoding = Encoding;
322 }
323 
EmitCFIRememberState()324 void MCStreamer::EmitCFIRememberState() {
325   MCSymbol *Label = EmitCFICommon();
326   MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label);
327   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
328   CurFrame->Instructions.push_back(Instruction);
329 }
330 
EmitCFIRestoreState()331 void 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 
EmitCFISameValue(int64_t Register)339 void 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 
EmitCFIRestore(int64_t Register)347 void 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 
EmitCFIEscape(StringRef Values)355 void 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 
EmitCFISignalFrame()362 void MCStreamer::EmitCFISignalFrame() {
363   EnsureValidDwarfFrame();
364   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
365   CurFrame->IsSignalFrame = true;
366 }
367 
EmitCFIUndefined(int64_t Register)368 void 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 
EmitCFIRegister(int64_t Register1,int64_t Register2)376 void 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 
EmitCFIWindowSave()384 void 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 
EnsureValidWinFrameInfo()392 void MCStreamer::EnsureValidWinFrameInfo() {
393   if (!CurrentWinFrameInfo || CurrentWinFrameInfo->End)
394     report_fatal_error("No open Win64 EH frame function!");
395 }
396 
EmitWinCFIStartProc(const MCSymbol * Symbol)397 void 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 
EmitWinCFIEndProc()408 void 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 
EmitWinCFIStartChained()418 void 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 
EmitWinCFIEndChained()429 void 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 
EmitWinEHHandler(const MCSymbol * Sym,bool Unwind,bool Except)442 void 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 
EmitWinEHHandlerData()456 void MCStreamer::EmitWinEHHandlerData() {
457   EnsureValidWinFrameInfo();
458   if (CurrentWinFrameInfo->ChainedParent)
459     report_fatal_error("Chained unwind areas can't have handlers!");
460 }
461 
EmitWinCFIPushReg(unsigned Register)462 void 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 
EmitWinCFISetFrame(unsigned Register,unsigned Offset)472 void 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 
EmitWinCFIAllocStack(unsigned Size)490 void 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 
EmitWinCFISaveReg(unsigned Register,unsigned Offset)504 void 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 
EmitWinCFISaveXMM(unsigned Register,unsigned Offset)517 void 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 
EmitWinCFIPushFrame(bool Code)530 void 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 
EmitWinCFIEndProlog()542 void MCStreamer::EmitWinCFIEndProlog() {
543   EnsureValidWinFrameInfo();
544 
545   MCSymbol *Label = getContext().CreateTempSymbol();
546   EmitLabel(Label);
547 
548   CurrentWinFrameInfo->PrologEnd = Label;
549 }
550 
EmitCOFFSectionIndex(MCSymbol const * Symbol)551 void MCStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
552 }
553 
EmitCOFFSecRel32(MCSymbol const * Symbol)554 void 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.
EmitRawTextImpl(StringRef String)560 void 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 
EmitRawText(const Twine & T)566 void MCStreamer::EmitRawText(const Twine &T) {
567   SmallString<128> Str;
568   EmitRawTextImpl(T.toStringRef(Str));
569 }
570 
EmitWindowsUnwindTables()571 void MCStreamer::EmitWindowsUnwindTables() {
572 }
573 
Finish()574 void 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 
EmitAssignment(MCSymbol * Symbol,const MCExpr * Value)585 void 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 
visitUsedSymbol(const MCSymbol & Sym)594 void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) {
595 }
596 
visitUsedExpr(const MCExpr & Expr)597 void 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 
EmitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)623 void 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 
EmitAssemblerFlag(MCAssemblerFlag Flag)631 void MCStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
EmitThumbFunc(MCSymbol * Func)632 void MCStreamer::EmitThumbFunc(MCSymbol *Func) {}
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)633 void MCStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
BeginCOFFSymbolDef(const MCSymbol * Symbol)634 void MCStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
EndCOFFSymbolDef()635 void MCStreamer::EndCOFFSymbolDef() {}
EmitFileDirective(StringRef Filename)636 void MCStreamer::EmitFileDirective(StringRef Filename) {}
EmitCOFFSymbolStorageClass(int StorageClass)637 void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {}
EmitCOFFSymbolType(int Type)638 void MCStreamer::EmitCOFFSymbolType(int Type) {}
EmitELFSize(MCSymbol * Symbol,const MCExpr * Value)639 void MCStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
EmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)640 void MCStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
641                                        unsigned ByteAlignment) {}
EmitTBSSSymbol(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)642 void MCStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
643                                 uint64_t Size, unsigned ByteAlignment) {}
ChangeSection(const MCSection *,const MCExpr *)644 void MCStreamer::ChangeSection(const MCSection *, const MCExpr *) {}
EmitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)645 void MCStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
EmitBytes(StringRef Data)646 void MCStreamer::EmitBytes(StringRef Data) {}
EmitValueImpl(const MCExpr * Value,unsigned Size,const SMLoc & Loc)647 void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
648                                const SMLoc &Loc) {
649   visitUsedExpr(*Value);
650 }
EmitULEB128Value(const MCExpr * Value)651 void MCStreamer::EmitULEB128Value(const MCExpr *Value) {}
EmitSLEB128Value(const MCExpr * Value)652 void MCStreamer::EmitSLEB128Value(const MCExpr *Value) {}
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)653 void MCStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
654                                       unsigned ValueSize,
655                                       unsigned MaxBytesToEmit) {}
EmitCodeAlignment(unsigned ByteAlignment,unsigned MaxBytesToEmit)656 void MCStreamer::EmitCodeAlignment(unsigned ByteAlignment,
657                                    unsigned MaxBytesToEmit) {}
EmitValueToOffset(const MCExpr * Offset,unsigned char Value)658 bool MCStreamer::EmitValueToOffset(const MCExpr *Offset, unsigned char Value) {
659   return false;
660 }
EmitBundleAlignMode(unsigned AlignPow2)661 void MCStreamer::EmitBundleAlignMode(unsigned AlignPow2) {}
EmitBundleLock(bool AlignToEnd)662 void MCStreamer::EmitBundleLock(bool AlignToEnd) {}
FinishImpl()663 void MCStreamer::FinishImpl() {}
EmitBundleUnlock()664 void MCStreamer::EmitBundleUnlock() {}
665 
SwitchSection(const MCSection * Section,const MCExpr * Subsection)666 void 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 
endSection(const MCSection * Section)681 MCSymbol *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