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