1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
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 // This file declares the MCStreamer class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_MC_MCSTREAMER_H
15 #define LLVM_MC_MCSTREAMER_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCLinkerOptimizationHint.h"
23 #include "llvm/MC/MCWinEH.h"
24 #include "llvm/Support/DataTypes.h"
25 #include <string>
26 
27 namespace llvm {
28 class MCAsmBackend;
29 class MCCodeEmitter;
30 class MCContext;
31 class MCExpr;
32 class MCInst;
33 class MCInstPrinter;
34 class MCSection;
35 class MCStreamer;
36 class MCSymbol;
37 class MCSymbolRefExpr;
38 class MCSubtargetInfo;
39 class StringRef;
40 class Twine;
41 class raw_ostream;
42 class formatted_raw_ostream;
43 class AssemblerConstantPools;
44 
45 typedef std::pair<const MCSection *, const MCExpr *> MCSectionSubPair;
46 
47 /// Target specific streamer interface. This is used so that targets can
48 /// implement support for target specific assembly directives.
49 ///
50 /// If target foo wants to use this, it should implement 3 classes:
51 /// * FooTargetStreamer : public MCTargetStreamer
52 /// * FooTargetAsmStreamer : public FooTargetStreamer
53 /// * FooTargetELFStreamer : public FooTargetStreamer
54 ///
55 /// FooTargetStreamer should have a pure virtual method for each directive. For
56 /// example, for a ".bar symbol_name" directive, it should have
57 /// virtual emitBar(const MCSymbol &Symbol) = 0;
58 ///
59 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
60 /// method. The assembly streamer just prints ".bar symbol_name". The object
61 /// streamer does whatever is needed to implement .bar in the object file.
62 ///
63 /// In the assembly printer and parser the target streamer can be used by
64 /// calling getTargetStreamer and casting it to FooTargetStreamer:
65 ///
66 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
67 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
68 ///
69 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
70 /// *never* be treated differently. Callers should always talk to a
71 /// FooTargetStreamer.
72 class MCTargetStreamer {
73 protected:
74   MCStreamer &Streamer;
75 
76 public:
77   MCTargetStreamer(MCStreamer &S);
78   virtual ~MCTargetStreamer();
79 
getStreamer()80   const MCStreamer &getStreamer() { return Streamer; }
81 
82   // Allow a target to add behavior to the EmitLabel of MCStreamer.
83   virtual void emitLabel(MCSymbol *Symbol);
84   // Allow a target to add behavior to the emitAssignment of MCStreamer.
85   virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
86 
87   virtual void finish();
88 };
89 
90 class AArch64TargetStreamer : public MCTargetStreamer {
91 public:
92   AArch64TargetStreamer(MCStreamer &S);
93   ~AArch64TargetStreamer() override;
94 
95   void finish() override;
96 
97   /// Callback used to implement the ldr= pseudo.
98   /// Add a new entry to the constant pool for the current section and return an
99   /// MCExpr that can be used to refer to the constant pool location.
100   const MCExpr *addConstantPoolEntry(const MCExpr *, unsigned Size);
101 
102   /// Callback used to implemnt the .ltorg directive.
103   /// Emit contents of constant pool for the current section.
104   void emitCurrentConstantPool();
105 
106   /// Callback used to implement the .inst directive.
107   virtual void emitInst(uint32_t Inst);
108 
109 private:
110   std::unique_ptr<AssemblerConstantPools> ConstantPools;
111 };
112 
113 // FIXME: declared here because it is used from
114 // lib/CodeGen/AsmPrinter/ARMException.cpp.
115 class ARMTargetStreamer : public MCTargetStreamer {
116 public:
117   ARMTargetStreamer(MCStreamer &S);
118   ~ARMTargetStreamer() override;
119 
120   virtual void emitFnStart();
121   virtual void emitFnEnd();
122   virtual void emitCantUnwind();
123   virtual void emitPersonality(const MCSymbol *Personality);
124   virtual void emitPersonalityIndex(unsigned Index);
125   virtual void emitHandlerData();
126   virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
127                          int64_t Offset = 0);
128   virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
129   virtual void emitPad(int64_t Offset);
130   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
131                            bool isVector);
132   virtual void emitUnwindRaw(int64_t StackOffset,
133                              const SmallVectorImpl<uint8_t> &Opcodes);
134 
135   virtual void switchVendor(StringRef Vendor);
136   virtual void emitAttribute(unsigned Attribute, unsigned Value);
137   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
138   virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
139                                     StringRef StringValue = "");
140   virtual void emitFPU(unsigned FPU);
141   virtual void emitArch(unsigned Arch);
142   virtual void emitArchExtension(unsigned ArchExt);
143   virtual void emitObjectArch(unsigned Arch);
144   virtual void finishAttributeSection();
145   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
146 
147   virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
148 
149   virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
150 
151   void finish() override;
152 
153   /// Callback used to implement the ldr= pseudo.
154   /// Add a new entry to the constant pool for the current section and return an
155   /// MCExpr that can be used to refer to the constant pool location.
156   const MCExpr *addConstantPoolEntry(const MCExpr *);
157 
158   /// Callback used to implemnt the .ltorg directive.
159   /// Emit contents of constant pool for the current section.
160   void emitCurrentConstantPool();
161 
162 private:
163   std::unique_ptr<AssemblerConstantPools> ConstantPools;
164 };
165 
166 /// MCStreamer - Streaming machine code generation interface.  This interface
167 /// is intended to provide a programatic interface that is very similar to the
168 /// level that an assembler .s file provides.  It has callbacks to emit bytes,
169 /// handle directives, etc.  The implementation of this interface retains
170 /// state to know what the current section is etc.
171 ///
172 /// There are multiple implementations of this interface: one for writing out
173 /// a .s file, and implementations that write out .o files of various formats.
174 ///
175 class MCStreamer {
176   MCContext &Context;
177   std::unique_ptr<MCTargetStreamer> TargetStreamer;
178 
179   MCStreamer(const MCStreamer &) = delete;
180   MCStreamer &operator=(const MCStreamer &) = delete;
181 
182   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
183   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
184   void EnsureValidDwarfFrame();
185 
186   MCSymbol *EmitCFICommon();
187 
188   std::vector<WinEH::FrameInfo *> WinFrameInfos;
189   WinEH::FrameInfo *CurrentWinFrameInfo;
190   void EnsureValidWinFrameInfo();
191 
192   // SymbolOrdering - Tracks an index to represent the order
193   // a symbol was emitted in. Zero means we did not emit that symbol.
194   DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
195 
196   /// SectionStack - This is stack of current and previous section
197   /// values saved by PushSection.
198   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
199 
200 protected:
201   MCStreamer(MCContext &Ctx);
202 
203   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
204   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
205 
getCurrentWinFrameInfo()206   WinEH::FrameInfo *getCurrentWinFrameInfo() {
207     return CurrentWinFrameInfo;
208   }
209 
210   virtual void EmitWindowsUnwindTables();
211 
212   virtual void EmitRawTextImpl(StringRef String);
213 
214 public:
215   virtual ~MCStreamer();
216 
217   void visitUsedExpr(const MCExpr &Expr);
218   virtual void visitUsedSymbol(const MCSymbol &Sym);
219 
setTargetStreamer(MCTargetStreamer * TS)220   void setTargetStreamer(MCTargetStreamer *TS) {
221     TargetStreamer.reset(TS);
222   }
223 
224   /// State management
225   ///
226   virtual void reset();
227 
getContext()228   MCContext &getContext() const { return Context; }
229 
getTargetStreamer()230   MCTargetStreamer *getTargetStreamer() {
231     return TargetStreamer.get();
232   }
233 
getNumFrameInfos()234   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
getDwarfFrameInfos()235   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
236     return DwarfFrameInfos;
237   }
238 
getNumWinFrameInfos()239   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
getWinFrameInfos()240   ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
241     return WinFrameInfos;
242   }
243 
244   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
245 
246   /// @name Assembly File Formatting.
247   /// @{
248 
249   /// isVerboseAsm - Return true if this streamer supports verbose assembly
250   /// and if it is enabled.
isVerboseAsm()251   virtual bool isVerboseAsm() const { return false; }
252 
253   /// hasRawTextSupport - Return true if this asm streamer supports emitting
254   /// unformatted text to the .s file with EmitRawText.
hasRawTextSupport()255   virtual bool hasRawTextSupport() const { return false; }
256 
257   /// Is the integrated assembler required for this streamer to function
258   /// correctly?
isIntegratedAssemblerRequired()259   virtual bool isIntegratedAssemblerRequired() const { return false; }
260 
261   /// AddComment - Add a comment that can be emitted to the generated .s
262   /// file if applicable as a QoI issue to make the output of the compiler
263   /// more readable.  This only affects the MCAsmStreamer, and only when
264   /// verbose assembly output is enabled.
265   ///
266   /// If the comment includes embedded \n's, they will each get the comment
267   /// prefix as appropriate.  The added comment should not end with a \n.
AddComment(const Twine & T)268   virtual void AddComment(const Twine &T) {}
269 
270   /// GetCommentOS - Return a raw_ostream that comments can be written to.
271   /// Unlike AddComment, you are required to terminate comments with \n if you
272   /// use this method.
273   virtual raw_ostream &GetCommentOS();
274 
275   /// Print T and prefix it with the comment string (normally #) and optionally
276   /// a tab. This prints the comment immediately, not at the end of the
277   /// current line. It is basically a safe version of EmitRawText: since it
278   /// only prints comments, the object streamer ignores it instead of asserting.
279   virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
280 
281   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
AddBlankLine()282   virtual void AddBlankLine() {}
283 
284   /// @}
285 
286   /// @name Symbol & Section Management
287   /// @{
288 
289   /// getCurrentSection - Return the current section that the streamer is
290   /// emitting code to.
getCurrentSection()291   MCSectionSubPair getCurrentSection() const {
292     if (!SectionStack.empty())
293       return SectionStack.back().first;
294     return MCSectionSubPair();
295   }
296 
297   /// getPreviousSection - Return the previous section that the streamer is
298   /// emitting code to.
getPreviousSection()299   MCSectionSubPair getPreviousSection() const {
300     if (!SectionStack.empty())
301       return SectionStack.back().second;
302     return MCSectionSubPair();
303   }
304 
305   /// GetSymbolOrder - Returns an index to represent the order
306   /// a symbol was emitted in. (zero if we did not emit that symbol)
GetSymbolOrder(const MCSymbol * Sym)307   unsigned GetSymbolOrder(const MCSymbol *Sym) const {
308     return SymbolOrdering.lookup(Sym);
309   }
310 
311   /// ChangeSection - Update streamer for a new active section.
312   ///
313   /// This is called by PopSection and SwitchSection, if the current
314   /// section changes.
315   virtual void ChangeSection(const MCSection *, const MCExpr *);
316 
317   /// pushSection - Save the current and previous section on the
318   /// section stack.
PushSection()319   void PushSection() {
320     SectionStack.push_back(
321         std::make_pair(getCurrentSection(), getPreviousSection()));
322   }
323 
324   /// popSection - Restore the current and previous section from
325   /// the section stack.  Calls ChangeSection as needed.
326   ///
327   /// Returns false if the stack was empty.
PopSection()328   bool PopSection() {
329     if (SectionStack.size() <= 1)
330       return false;
331     MCSectionSubPair oldSection = SectionStack.pop_back_val().first;
332     MCSectionSubPair curSection = SectionStack.back().first;
333 
334     if (oldSection != curSection)
335       ChangeSection(curSection.first, curSection.second);
336     return true;
337   }
338 
SubSection(const MCExpr * Subsection)339   bool SubSection(const MCExpr *Subsection) {
340     if (SectionStack.empty())
341       return false;
342 
343     SwitchSection(SectionStack.back().first.first, Subsection);
344     return true;
345   }
346 
347   /// Set the current section where code is being emitted to @p Section.  This
348   /// is required to update CurSection.
349   ///
350   /// This corresponds to assembler directives like .section, .text, etc.
351   virtual void SwitchSection(const MCSection *Section,
352                              const MCExpr *Subsection = nullptr);
353 
354   /// SwitchSectionNoChange - Set the current section where code is being
355   /// emitted to @p Section.  This is required to update CurSection. This
356   /// version does not call ChangeSection.
357   void SwitchSectionNoChange(const MCSection *Section,
358                              const MCExpr *Subsection = nullptr) {
359     assert(Section && "Cannot switch to a null section!");
360     MCSectionSubPair curSection = SectionStack.back().first;
361     SectionStack.back().second = curSection;
362     if (MCSectionSubPair(Section, Subsection) != curSection)
363       SectionStack.back().first = MCSectionSubPair(Section, Subsection);
364   }
365 
366   /// Create the default sections and set the initial one.
367   virtual void InitSections(bool NoExecStack);
368 
369   MCSymbol *endSection(const MCSection *Section);
370 
371   /// AssignSection - Sets the symbol's section.
372   ///
373   /// Each emitted symbol will be tracked in the ordering table,
374   /// so we can sort on them later.
375   void AssignSection(MCSymbol *Symbol, const MCSection *Section);
376 
377   /// EmitLabel - Emit a label for @p Symbol into the current section.
378   ///
379   /// This corresponds to an assembler statement such as:
380   ///   foo:
381   ///
382   /// @param Symbol - The symbol to emit. A given symbol should only be
383   /// emitted as a label once, and symbols emitted as a label should never be
384   /// used in an assignment.
385   // FIXME: These emission are non-const because we mutate the symbol to
386   // add the section we're emitting it to later.
387   virtual void EmitLabel(MCSymbol *Symbol);
388 
389   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
390 
391   /// EmitAssemblerFlag - Note in the output the specified @p Flag.
392   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
393 
394   /// EmitLinkerOptions - Emit the given list @p Options of strings as linker
395   /// options into the output.
EmitLinkerOptions(ArrayRef<std::string> Kind)396   virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
397 
398   /// EmitDataRegion - Note in the output the specified region @p Kind.
EmitDataRegion(MCDataRegionType Kind)399   virtual void EmitDataRegion(MCDataRegionType Kind) {}
400 
401   /// EmitVersionMin - Specify the MachO minimum deployment target version.
EmitVersionMin(MCVersionMinType,unsigned Major,unsigned Minor,unsigned Update)402   virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
403                               unsigned Update) {}
404 
405   /// EmitThumbFunc - Note in the output that the specified @p Func is
406   /// a Thumb mode function (ARM target only).
407   virtual void EmitThumbFunc(MCSymbol *Func);
408 
409   /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
410   ///
411   /// This corresponds to an assembler statement such as:
412   ///  symbol = value
413   ///
414   /// The assignment generates no code, but has the side effect of binding the
415   /// value in the current context. For the assembly streamer, this prints the
416   /// binding into the .s file.
417   ///
418   /// @param Symbol - The symbol being assigned to.
419   /// @param Value - The value for the symbol.
420   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
421 
422   /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
423   ///
424   /// This corresponds to an assembler statement such as:
425   ///  .weakref alias, symbol
426   ///
427   /// @param Alias - The alias that is being created.
428   /// @param Symbol - The symbol being aliased.
429   virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
430 
431   /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
432   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
433                                    MCSymbolAttr Attribute) = 0;
434 
435   /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
436   ///
437   /// @param Symbol - The symbol to have its n_desc field set.
438   /// @param DescValue - The value to set into the n_desc field.
439   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
440 
441   /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
442   ///
443   /// @param Symbol - The symbol to have its External & Type fields set.
444   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
445 
446   /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
447   ///
448   /// @param StorageClass - The storage class the symbol should have.
449   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
450 
451   /// EmitCOFFSymbolType - Emit the type of the symbol.
452   ///
453   /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
454   virtual void EmitCOFFSymbolType(int Type);
455 
456   /// EndCOFFSymbolDef - Marks the end of the symbol definition.
457   virtual void EndCOFFSymbolDef();
458 
459   /// EmitCOFFSectionIndex - Emits a COFF section index.
460   ///
461   /// @param Symbol - Symbol the section number relocation should point to.
462   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
463 
464   /// EmitCOFFSecRel32 - Emits a COFF section relative relocation.
465   ///
466   /// @param Symbol - Symbol the section relative relocation should point to.
467   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
468 
469   /// EmitELFSize - Emit an ELF .size directive.
470   ///
471   /// This corresponds to an assembler statement such as:
472   ///  .size symbol, expression
473   ///
474   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
475 
476   /// \brief Emit a Linker Optimization Hint (LOH) directive.
477   /// \param Args - Arguments of the LOH.
EmitLOHDirective(MCLOHType Kind,const MCLOHArgs & Args)478   virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
479 
480   /// EmitCommonSymbol - Emit a common symbol.
481   ///
482   /// @param Symbol - The common symbol to emit.
483   /// @param Size - The size of the common symbol.
484   /// @param ByteAlignment - The alignment of the symbol if
485   /// non-zero. This must be a power of 2.
486   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
487                                 unsigned ByteAlignment) = 0;
488 
489   /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
490   ///
491   /// @param Symbol - The common symbol to emit.
492   /// @param Size - The size of the common symbol.
493   /// @param ByteAlignment - The alignment of the common symbol in bytes.
494   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
495                                      unsigned ByteAlignment);
496 
497   /// EmitZerofill - Emit the zerofill section and an optional symbol.
498   ///
499   /// @param Section - The zerofill section to create and or to put the symbol
500   /// @param Symbol - The zerofill symbol to emit, if non-NULL.
501   /// @param Size - The size of the zerofill symbol.
502   /// @param ByteAlignment - The alignment of the zerofill symbol if
503   /// non-zero. This must be a power of 2 on some targets.
504   virtual void EmitZerofill(const MCSection *Section,
505                             MCSymbol *Symbol = nullptr, uint64_t Size = 0,
506                             unsigned ByteAlignment = 0) = 0;
507 
508   /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
509   ///
510   /// @param Section - The thread local common section.
511   /// @param Symbol - The thread local common symbol to emit.
512   /// @param Size - The size of the symbol.
513   /// @param ByteAlignment - The alignment of the thread local common symbol
514   /// if non-zero.  This must be a power of 2 on some targets.
515   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
516                               uint64_t Size, unsigned ByteAlignment = 0);
517 
518   /// @}
519   /// @name Generating Data
520   /// @{
521 
522   /// EmitBytes - Emit the bytes in \p Data into the output.
523   ///
524   /// This is used to implement assembler directives such as .byte, .ascii,
525   /// etc.
526   virtual void EmitBytes(StringRef Data);
527 
528   /// EmitValue - Emit the expression @p Value into the output as a native
529   /// integer of the given @p Size bytes.
530   ///
531   /// This is used to implement assembler directives such as .word, .quad,
532   /// etc.
533   ///
534   /// @param Value - The value to emit.
535   /// @param Size - The size of the integer (in bytes) to emit. This must
536   /// match a native machine width.
537   /// @param Loc - The location of the expression for error reporting.
538   virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
539                              const SMLoc &Loc = SMLoc());
540 
541   void EmitValue(const MCExpr *Value, unsigned Size,
542                  const SMLoc &Loc = SMLoc());
543 
544   /// EmitIntValue - Special case of EmitValue that avoids the client having
545   /// to pass in a MCExpr for constant integers.
546   virtual void EmitIntValue(uint64_t Value, unsigned Size);
547 
548   virtual void EmitULEB128Value(const MCExpr *Value);
549 
550   virtual void EmitSLEB128Value(const MCExpr *Value);
551 
552   /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
553   /// client having to pass in a MCExpr for constant integers.
554   void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
555 
556   /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
557   /// client having to pass in a MCExpr for constant integers.
558   void EmitSLEB128IntValue(int64_t Value);
559 
560   /// EmitSymbolValue - Special case of EmitValue that avoids the client
561   /// having to pass in a MCExpr for MCSymbols.
562   void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
563                        bool IsSectionRelative = false);
564 
565   /// EmitGPRel64Value - Emit the expression @p Value into the output as a
566   /// gprel64 (64-bit GP relative) value.
567   ///
568   /// This is used to implement assembler directives such as .gpdword on
569   /// targets that support them.
570   virtual void EmitGPRel64Value(const MCExpr *Value);
571 
572   /// EmitGPRel32Value - Emit the expression @p Value into the output as a
573   /// gprel32 (32-bit GP relative) value.
574   ///
575   /// This is used to implement assembler directives such as .gprel32 on
576   /// targets that support them.
577   virtual void EmitGPRel32Value(const MCExpr *Value);
578 
579   /// EmitFill - Emit NumBytes bytes worth of the value specified by
580   /// FillValue.  This implements directives such as '.space'.
581   virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
582 
583   /// \brief Emit NumBytes worth of zeros.
584   /// This function properly handles data in virtual sections.
585   virtual void EmitZeros(uint64_t NumBytes);
586 
587   /// EmitValueToAlignment - Emit some number of copies of @p Value until
588   /// the byte alignment @p ByteAlignment is reached.
589   ///
590   /// If the number of bytes need to emit for the alignment is not a multiple
591   /// of @p ValueSize, then the contents of the emitted fill bytes is
592   /// undefined.
593   ///
594   /// This used to implement the .align assembler directive.
595   ///
596   /// @param ByteAlignment - The alignment to reach. This must be a power of
597   /// two on some targets.
598   /// @param Value - The value to use when filling bytes.
599   /// @param ValueSize - The size of the integer (in bytes) to emit for
600   /// @p Value. This must match a native machine width.
601   /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
602   /// the alignment cannot be reached in this many bytes, no bytes are
603   /// emitted.
604   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
605                                     unsigned ValueSize = 1,
606                                     unsigned MaxBytesToEmit = 0);
607 
608   /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
609   /// is reached.
610   ///
611   /// This used to align code where the alignment bytes may be executed.  This
612   /// can emit different bytes for different sizes to optimize execution.
613   ///
614   /// @param ByteAlignment - The alignment to reach. This must be a power of
615   /// two on some targets.
616   /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
617   /// the alignment cannot be reached in this many bytes, no bytes are
618   /// emitted.
619   virtual void EmitCodeAlignment(unsigned ByteAlignment,
620                                  unsigned MaxBytesToEmit = 0);
621 
622   /// EmitValueToOffset - Emit some number of copies of @p Value until the
623   /// byte offset @p Offset is reached.
624   ///
625   /// This is used to implement assembler directives such as .org.
626   ///
627   /// @param Offset - The offset to reach. This may be an expression, but the
628   /// expression must be associated with the current section.
629   /// @param Value - The value to use when filling bytes.
630   /// @return false on success, true if the offset was invalid.
631   virtual bool EmitValueToOffset(const MCExpr *Offset,
632                                  unsigned char Value = 0);
633 
634   /// @}
635 
636   /// EmitFileDirective - Switch to a new logical file.  This is used to
637   /// implement the '.file "foo.c"' assembler directive.
638   virtual void EmitFileDirective(StringRef Filename);
639 
640   /// Emit the "identifiers" directive.  This implements the
641   /// '.ident "version foo"' assembler directive.
EmitIdent(StringRef IdentString)642   virtual void EmitIdent(StringRef IdentString) {}
643 
644   /// EmitDwarfFileDirective - Associate a filename with a specified logical
645   /// file number.  This implements the DWARF2 '.file 4 "foo.c"' assembler
646   /// directive.
647   virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
648                                           StringRef Filename,
649                                           unsigned CUID = 0);
650 
651   /// EmitDwarfLocDirective - This implements the DWARF2
652   // '.loc fileno lineno ...' assembler directive.
653   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
654                                      unsigned Column, unsigned Flags,
655                                      unsigned Isa, unsigned Discriminator,
656                                      StringRef FileName);
657 
658   virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
659   virtual void EmitCFISections(bool EH, bool Debug);
660   void EmitCFIStartProc(bool IsSimple);
661   void EmitCFIEndProc();
662   virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
663   virtual void EmitCFIDefCfaOffset(int64_t Offset);
664   virtual void EmitCFIDefCfaRegister(int64_t Register);
665   virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
666   virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
667   virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
668   virtual void EmitCFIRememberState();
669   virtual void EmitCFIRestoreState();
670   virtual void EmitCFISameValue(int64_t Register);
671   virtual void EmitCFIRestore(int64_t Register);
672   virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
673   virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
674   virtual void EmitCFIEscape(StringRef Values);
675   virtual void EmitCFISignalFrame();
676   virtual void EmitCFIUndefined(int64_t Register);
677   virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
678   virtual void EmitCFIWindowSave();
679 
680   virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
681   virtual void EmitWinCFIEndProc();
682   virtual void EmitWinCFIStartChained();
683   virtual void EmitWinCFIEndChained();
684   virtual void EmitWinCFIPushReg(unsigned Register);
685   virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
686   virtual void EmitWinCFIAllocStack(unsigned Size);
687   virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
688   virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
689   virtual void EmitWinCFIPushFrame(bool Code);
690   virtual void EmitWinCFIEndProlog();
691 
692   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
693   virtual void EmitWinEHHandlerData();
694 
695   /// EmitInstruction - Emit the given @p Instruction into the current
696   /// section.
697   virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
698 
699   /// \brief Set the bundle alignment mode from now on in the section.
700   /// The argument is the power of 2 to which the alignment is set. The
701   /// value 0 means turn the bundle alignment off.
702   virtual void EmitBundleAlignMode(unsigned AlignPow2);
703 
704   /// \brief The following instructions are a bundle-locked group.
705   ///
706   /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
707   ///                     the end of a bundle.
708   virtual void EmitBundleLock(bool AlignToEnd);
709 
710   /// \brief Ends a bundle-locked group.
711   virtual void EmitBundleUnlock();
712 
713   /// EmitRawText - If this file is backed by a assembly streamer, this dumps
714   /// the specified string in the output .s file.  This capability is
715   /// indicated by the hasRawTextSupport() predicate.  By default this aborts.
716   void EmitRawText(const Twine &String);
717 
718   /// Flush - Causes any cached state to be written out.
Flush()719   virtual void Flush() {}
720 
721   /// FinishImpl - Streamer specific finalization.
722   virtual void FinishImpl();
723   /// Finish - Finish emission of machine code.
724   void Finish();
725 
mayHaveInstructions()726   virtual bool mayHaveInstructions() const { return true; }
727 };
728 
729 /// Create a dummy machine code streamer, which does nothing. This is useful for
730 /// timing the assembler front end.
731 MCStreamer *createNullStreamer(MCContext &Ctx);
732 
733 /// Create a machine code streamer which will print out assembly for the native
734 /// target, suitable for compiling with a native assembler.
735 ///
736 /// \param InstPrint - If given, the instruction printer to use. If not given
737 /// the MCInst representation will be printed.  This method takes ownership of
738 /// InstPrint.
739 ///
740 /// \param CE - If given, a code emitter to use to show the instruction
741 /// encoding inline with the assembly. This method takes ownership of \p CE.
742 ///
743 /// \param TAB - If given, a target asm backend to use to show the fixup
744 /// information in conjunction with encoding information. This method takes
745 /// ownership of \p TAB.
746 ///
747 /// \param ShowInst - Whether to show the MCInst representation inline with
748 /// the assembly.
749 MCStreamer *createAsmStreamer(MCContext &Ctx,
750                               std::unique_ptr<formatted_raw_ostream> OS,
751                               bool isVerboseAsm, bool useDwarfDirectory,
752                               MCInstPrinter *InstPrint, MCCodeEmitter *CE,
753                               MCAsmBackend *TAB, bool ShowInst);
754 } // end namespace llvm
755 
756 #endif
757