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>
28 using namespace llvm;
29 
30 // Pin the vtables to this file.
~MCTargetStreamer()31 MCTargetStreamer::~MCTargetStreamer() {}
32 
MCTargetStreamer(MCStreamer & S)33 MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
34   S.setTargetStreamer(this);
35 }
36 
emitLabel(MCSymbol * Symbol)37 void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {}
38 
finish()39 void MCTargetStreamer::finish() {}
40 
emitAssignment(MCSymbol * Symbol,const MCExpr * Value)41 void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
42 
MCStreamer(MCContext & Ctx)43 MCStreamer::MCStreamer(MCContext &Ctx)
44     : Context(Ctx), CurrentWinFrameInfo(nullptr) {
45   SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
46 }
47 
~MCStreamer()48 MCStreamer::~MCStreamer() {
49   for (unsigned i = 0; i < getNumWinFrameInfos(); ++i)
50     delete WinFrameInfos[i];
51 }
52 
reset()53 void 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 
GetCommentOS()64 raw_ostream &MCStreamer::GetCommentOS() {
65   // By default, discard comments.
66   return nulls();
67 }
68 
emitRawComment(const Twine & T,bool TabPrefix)69 void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {}
70 
generateCompactUnwindEncodings(MCAsmBackend * MAB)71 void 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.
EmitIntValue(uint64_t Value,unsigned Size)79 void 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.
EmitULEB128IntValue(uint64_t Value,unsigned Padding)94 void 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.
EmitSLEB128IntValue(int64_t Value)103 void 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 
EmitValue(const MCExpr * Value,unsigned Size,SMLoc Loc)110 void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc) {
111   EmitValueImpl(Value, Size, Loc);
112 }
113 
EmitSymbolValue(const MCSymbol * Sym,unsigned Size,bool IsSectionRelative)114 void 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 
EmitGPRel64Value(const MCExpr * Value)125 void MCStreamer::EmitGPRel64Value(const MCExpr *Value) {
126   report_fatal_error("unsupported directive in streamer");
127 }
128 
EmitGPRel32Value(const MCExpr * Value)129 void 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'.
EmitFill(uint64_t NumBytes,uint8_t FillValue)135 void 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.
EmitZeros(uint64_t NumBytes)142 void MCStreamer::EmitZeros(uint64_t NumBytes) {
143   EmitFill(NumBytes, 0);
144 }
145 
EmitDwarfFileDirective(unsigned FileNo,StringRef Directory,StringRef Filename,unsigned CUID)146 unsigned MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
147                                             StringRef Directory,
148                                             StringRef Filename, unsigned CUID) {
149   return getContext().getDwarfFile(Directory, Filename, FileNo, CUID);
150 }
151 
EmitDwarfLocDirective(unsigned FileNo,unsigned Line,unsigned Column,unsigned Flags,unsigned Isa,unsigned Discriminator,StringRef FileName)152 void 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 
getDwarfLineTableSymbol(unsigned CUID)161 MCSymbol *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 
getCurrentDwarfFrameInfo()171 MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() {
172   if (DwarfFrameInfos.empty())
173     return nullptr;
174   return &DwarfFrameInfos.back();
175 }
176 
EnsureValidDwarfFrame()177 void MCStreamer::EnsureValidDwarfFrame() {
178   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
179   if (!CurFrame || CurFrame->End)
180     report_fatal_error("No open frame");
181 }
182 
EmitEHSymAttributes(const MCSymbol * Symbol,MCSymbol * EHSymbol)183 void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
184                                      MCSymbol *EHSymbol) {
185 }
186 
InitSections(bool NoExecStack)187 void MCStreamer::InitSections(bool NoExecStack) {
188   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
189 }
190 
AssignFragment(MCSymbol * Symbol,MCFragment * Fragment)191 void 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 
EmitLabel(MCSymbol * Symbol)200 void 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 
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 
EmitCFIGnuArgsSize(int64_t Size)362 void 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 
EmitCFISignalFrame()370 void MCStreamer::EmitCFISignalFrame() {
371   EnsureValidDwarfFrame();
372   MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
373   CurFrame->IsSignalFrame = true;
374 }
375 
EmitCFIUndefined(int64_t Register)376 void 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 
EmitCFIRegister(int64_t Register1,int64_t Register2)384 void 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 
EmitCFIWindowSave()392 void 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 
EnsureValidWinFrameInfo()400 void 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 
EmitWinCFIStartProc(const MCSymbol * Symbol)408 void 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 
EmitWinCFIEndProc()422 void 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 
EmitWinCFIStartChained()432 void 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 
EmitWinCFIEndChained()443 void 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 
EmitWinEHHandler(const MCSymbol * Sym,bool Unwind,bool Except)456 void 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 
EmitWinEHHandlerData()470 void MCStreamer::EmitWinEHHandlerData() {
471   EnsureValidWinFrameInfo();
472   if (CurrentWinFrameInfo->ChainedParent)
473     report_fatal_error("Chained unwind areas can't have handlers!");
474 }
475 
EmitSyntaxDirective()476 void MCStreamer::EmitSyntaxDirective() {}
477 
EmitWinCFIPushReg(unsigned Register)478 void 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 
EmitWinCFISetFrame(unsigned Register,unsigned Offset)488 void 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 
EmitWinCFIAllocStack(unsigned Size)506 void 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 
EmitWinCFISaveReg(unsigned Register,unsigned Offset)520 void 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 
EmitWinCFISaveXMM(unsigned Register,unsigned Offset)533 void 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 
EmitWinCFIPushFrame(bool Code)546 void 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 
EmitWinCFIEndProlog()558 void MCStreamer::EmitWinCFIEndProlog() {
559   EnsureValidWinFrameInfo();
560 
561   MCSymbol *Label = getContext().createTempSymbol();
562   EmitLabel(Label);
563 
564   CurrentWinFrameInfo->PrologEnd = Label;
565 }
566 
EmitCOFFSafeSEH(MCSymbol const * Symbol)567 void MCStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
568 }
569 
EmitCOFFSectionIndex(MCSymbol const * Symbol)570 void MCStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
571 }
572 
EmitCOFFSecRel32(MCSymbol const * Symbol)573 void 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.
EmitRawTextImpl(StringRef String)579 void 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 
EmitRawText(const Twine & T)585 void MCStreamer::EmitRawText(const Twine &T) {
586   SmallString<128> Str;
587   EmitRawTextImpl(T.toStringRef(Str));
588 }
589 
EmitWindowsUnwindTables()590 void MCStreamer::EmitWindowsUnwindTables() {
591 }
592 
Finish()593 void 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 
EmitAssignment(MCSymbol * Symbol,const MCExpr * Value)604 void 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 
prettyPrintAsm(MCInstPrinter & InstPrinter,raw_ostream & OS,const MCInst & Inst,const MCSubtargetInfo & STI)613 void MCTargetStreamer::prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
614                               const MCInst &Inst, const MCSubtargetInfo &STI) {
615   InstPrinter.printInst(&Inst, OS, "", STI);
616 }
617 
visitUsedSymbol(const MCSymbol & Sym)618 void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) {
619 }
620 
visitUsedExpr(const MCExpr & Expr)621 void 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 
EmitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)647 void 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 
emitAbsoluteSymbolDiff(const MCSymbol * Hi,const MCSymbol * Lo,unsigned Size)655 void 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 
EmitAssemblerFlag(MCAssemblerFlag Flag)674 void MCStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
EmitThumbFunc(MCSymbol * Func)675 void MCStreamer::EmitThumbFunc(MCSymbol *Func) {}
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)676 void MCStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
BeginCOFFSymbolDef(const MCSymbol * Symbol)677 void MCStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
EndCOFFSymbolDef()678 void MCStreamer::EndCOFFSymbolDef() {}
EmitFileDirective(StringRef Filename)679 void MCStreamer::EmitFileDirective(StringRef Filename) {}
EmitCOFFSymbolStorageClass(int StorageClass)680 void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {}
EmitCOFFSymbolType(int Type)681 void MCStreamer::EmitCOFFSymbolType(int Type) {}
emitELFSize(MCSymbolELF * Symbol,const MCExpr * Value)682 void MCStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {}
EmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)683 void MCStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
684                                        unsigned ByteAlignment) {}
EmitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)685 void MCStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
686                                 uint64_t Size, unsigned ByteAlignment) {}
ChangeSection(MCSection *,const MCExpr *)687 void MCStreamer::ChangeSection(MCSection *, const MCExpr *) {}
EmitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)688 void MCStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
EmitBytes(StringRef Data)689 void MCStreamer::EmitBytes(StringRef Data) {}
EmitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)690 void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) {
691   visitUsedExpr(*Value);
692 }
EmitULEB128Value(const MCExpr * Value)693 void MCStreamer::EmitULEB128Value(const MCExpr *Value) {}
EmitSLEB128Value(const MCExpr * Value)694 void MCStreamer::EmitSLEB128Value(const MCExpr *Value) {}
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)695 void MCStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
696                                       unsigned ValueSize,
697                                       unsigned MaxBytesToEmit) {}
EmitCodeAlignment(unsigned ByteAlignment,unsigned MaxBytesToEmit)698 void MCStreamer::EmitCodeAlignment(unsigned ByteAlignment,
699                                    unsigned MaxBytesToEmit) {}
emitValueToOffset(const MCExpr * Offset,unsigned char Value)700 void MCStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value) {}
EmitBundleAlignMode(unsigned AlignPow2)701 void MCStreamer::EmitBundleAlignMode(unsigned AlignPow2) {}
EmitBundleLock(bool AlignToEnd)702 void MCStreamer::EmitBundleLock(bool AlignToEnd) {}
FinishImpl()703 void MCStreamer::FinishImpl() {}
EmitBundleUnlock()704 void MCStreamer::EmitBundleUnlock() {}
705 
SwitchSection(MCSection * Section,const MCExpr * Subsection)706 void 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 
endSection(MCSection * Section)720 MCSymbol *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