]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCStreamer.h
Merge ^/head r327886 through r327930.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCStreamer.h
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/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCWinEH.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/TargetParser.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34
35 namespace llvm {
36
37 class AssemblerConstantPools;
38 class formatted_raw_ostream;
39 class MCAsmBackend;
40 class MCCodeEmitter;
41 struct MCCodePaddingContext;
42 class MCContext;
43 class MCExpr;
44 class MCInst;
45 class MCInstPrinter;
46 class MCSection;
47 class MCStreamer;
48 class MCSymbolRefExpr;
49 class MCSubtargetInfo;
50 class raw_ostream;
51 class Twine;
52
53 using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
54
55 /// Target specific streamer interface. This is used so that targets can
56 /// implement support for target specific assembly directives.
57 ///
58 /// If target foo wants to use this, it should implement 3 classes:
59 /// * FooTargetStreamer : public MCTargetStreamer
60 /// * FooTargetAsmStreamer : public FooTargetStreamer
61 /// * FooTargetELFStreamer : public FooTargetStreamer
62 ///
63 /// FooTargetStreamer should have a pure virtual method for each directive. For
64 /// example, for a ".bar symbol_name" directive, it should have
65 /// virtual emitBar(const MCSymbol &Symbol) = 0;
66 ///
67 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
68 /// method. The assembly streamer just prints ".bar symbol_name". The object
69 /// streamer does whatever is needed to implement .bar in the object file.
70 ///
71 /// In the assembly printer and parser the target streamer can be used by
72 /// calling getTargetStreamer and casting it to FooTargetStreamer:
73 ///
74 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
75 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
76 ///
77 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
78 /// *never* be treated differently. Callers should always talk to a
79 /// FooTargetStreamer.
80 class MCTargetStreamer {
81 protected:
82   MCStreamer &Streamer;
83
84 public:
85   MCTargetStreamer(MCStreamer &S);
86   virtual ~MCTargetStreamer();
87
88   MCStreamer &getStreamer() { return Streamer; }
89
90   // Allow a target to add behavior to the EmitLabel of MCStreamer.
91   virtual void emitLabel(MCSymbol *Symbol);
92   // Allow a target to add behavior to the emitAssignment of MCStreamer.
93   virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
94
95   virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
96                               const MCInst &Inst, const MCSubtargetInfo &STI);
97
98   virtual void emitDwarfFileDirective(StringRef Directive);
99
100   /// Update streamer for a new active section.
101   ///
102   /// This is called by PopSection and SwitchSection, if the current
103   /// section changes.
104   virtual void changeSection(const MCSection *CurSection, MCSection *Section,
105                              const MCExpr *SubSection, raw_ostream &OS);
106
107   virtual void emitValue(const MCExpr *Value);
108
109   virtual void finish();
110 };
111
112 // FIXME: declared here because it is used from
113 // lib/CodeGen/AsmPrinter/ARMException.cpp.
114 class ARMTargetStreamer : public MCTargetStreamer {
115 public:
116   ARMTargetStreamer(MCStreamer &S);
117   ~ARMTargetStreamer() override;
118
119   virtual void emitFnStart();
120   virtual void emitFnEnd();
121   virtual void emitCantUnwind();
122   virtual void emitPersonality(const MCSymbol *Personality);
123   virtual void emitPersonalityIndex(unsigned Index);
124   virtual void emitHandlerData();
125   virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
126                          int64_t Offset = 0);
127   virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
128   virtual void emitPad(int64_t Offset);
129   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
130                            bool isVector);
131   virtual void emitUnwindRaw(int64_t StackOffset,
132                              const SmallVectorImpl<uint8_t> &Opcodes);
133
134   virtual void switchVendor(StringRef Vendor);
135   virtual void emitAttribute(unsigned Attribute, unsigned Value);
136   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
137   virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
138                                     StringRef StringValue = "");
139   virtual void emitFPU(unsigned FPU);
140   virtual void emitArch(ARM::ArchKind Arch);
141   virtual void emitArchExtension(unsigned ArchExt);
142   virtual void emitObjectArch(ARM::ArchKind Arch);
143   void emitTargetAttributes(const MCSubtargetInfo &STI);
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   /// Reset any state between object emissions, i.e. the equivalent of
154   /// MCStreamer's reset method.
155   virtual void reset();
156
157   /// Callback used to implement the ldr= pseudo.
158   /// Add a new entry to the constant pool for the current section and return an
159   /// MCExpr that can be used to refer to the constant pool location.
160   const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
161
162   /// Callback used to implemnt the .ltorg directive.
163   /// Emit contents of constant pool for the current section.
164   void emitCurrentConstantPool();
165
166 private:
167   std::unique_ptr<AssemblerConstantPools> ConstantPools;
168 };
169
170 /// \brief Streaming machine code generation interface.
171 ///
172 /// This interface is intended to provide a programatic interface that is very
173 /// similar to the level that an assembler .s file provides.  It has callbacks
174 /// to emit bytes, handle directives, etc.  The implementation of this interface
175 /// retains state to know what the current section is etc.
176 ///
177 /// There are multiple implementations of this interface: one for writing out
178 /// a .s file, and implementations that write out .o files of various formats.
179 ///
180 class MCStreamer {
181   MCContext &Context;
182   std::unique_ptr<MCTargetStreamer> TargetStreamer;
183
184   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
185   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
186
187   /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
188   /// refer to each other, so use std::unique_ptr to provide pointer stability.
189   std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
190
191   WinEH::FrameInfo *CurrentWinFrameInfo;
192
193   /// Retreive the current frame info if one is available and it is not yet
194   /// closed. Otherwise, issue an error and return null.
195   WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
196
197   /// \brief Tracks an index to represent the order a symbol was emitted in.
198   /// Zero means we did not emit that symbol.
199   DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
200
201   /// \brief This is stack of current and previous section values saved by
202   /// PushSection.
203   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
204
205   /// The next unique ID to use when creating a WinCFI-related section (.pdata
206   /// or .xdata). This ID ensures that we have a one-to-one mapping from
207   /// code section to unwind info section, which MSVC's incremental linker
208   /// requires.
209   unsigned NextWinCFIID = 0;
210
211 protected:
212   MCStreamer(MCContext &Ctx);
213
214   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
215   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
216
217   /// When emitting an object file, create and emit a real label. When emitting
218   /// textual assembly, this should do nothing to avoid polluting our output.
219   virtual MCSymbol *EmitCFILabel();
220
221   WinEH::FrameInfo *getCurrentWinFrameInfo() {
222     return CurrentWinFrameInfo;
223   }
224
225   virtual void EmitWindowsUnwindTables();
226
227   virtual void EmitRawTextImpl(StringRef String);
228
229 public:
230   MCStreamer(const MCStreamer &) = delete;
231   MCStreamer &operator=(const MCStreamer &) = delete;
232   virtual ~MCStreamer();
233
234   void visitUsedExpr(const MCExpr &Expr);
235   virtual void visitUsedSymbol(const MCSymbol &Sym);
236
237   void setTargetStreamer(MCTargetStreamer *TS) {
238     TargetStreamer.reset(TS);
239   }
240
241   /// State management
242   ///
243   virtual void reset();
244
245   MCContext &getContext() const { return Context; }
246
247   MCTargetStreamer *getTargetStreamer() {
248     return TargetStreamer.get();
249   }
250
251   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
252   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
253     return DwarfFrameInfos;
254   }
255
256   bool hasUnfinishedDwarfFrameInfo();
257
258   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
259   ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
260     return WinFrameInfos;
261   }
262
263   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
264
265   /// \name Assembly File Formatting.
266   /// @{
267
268   /// \brief Return true if this streamer supports verbose assembly and if it is
269   /// enabled.
270   virtual bool isVerboseAsm() const { return false; }
271
272   /// \brief Return true if this asm streamer supports emitting unformatted text
273   /// to the .s file with EmitRawText.
274   virtual bool hasRawTextSupport() const { return false; }
275
276   /// \brief Is the integrated assembler required for this streamer to function
277   /// correctly?
278   virtual bool isIntegratedAssemblerRequired() const { return false; }
279
280   /// \brief Add a textual comment.
281   ///
282   /// Typically for comments that can be emitted to the generated .s
283   /// file if applicable as a QoI issue to make the output of the compiler
284   /// more readable.  This only affects the MCAsmStreamer, and only when
285   /// verbose assembly output is enabled.
286   ///
287   /// If the comment includes embedded \n's, they will each get the comment
288   /// prefix as appropriate.  The added comment should not end with a \n.
289   /// By default, each comment is terminated with an end of line, i.e. the
290   /// EOL param is set to true by default. If one prefers not to end the 
291   /// comment with a new line then the EOL param should be passed 
292   /// with a false value.
293   virtual void AddComment(const Twine &T, bool EOL = true) {}
294
295   /// \brief Return a raw_ostream that comments can be written to. Unlike
296   /// AddComment, you are required to terminate comments with \n if you use this
297   /// method.
298   virtual raw_ostream &GetCommentOS();
299
300   /// \brief Print T and prefix it with the comment string (normally #) and
301   /// optionally a tab. This prints the comment immediately, not at the end of
302   /// the current line. It is basically a safe version of EmitRawText: since it
303   /// only prints comments, the object streamer ignores it instead of asserting.
304   virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
305
306   /// \brief Add explicit comment T. T is required to be a valid
307   /// comment in the output and does not need to be escaped.
308   virtual void addExplicitComment(const Twine &T);
309
310   /// \brief Emit added explicit comments.
311   virtual void emitExplicitComments();
312
313   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
314   virtual void AddBlankLine() {}
315
316   /// @}
317
318   /// \name Symbol & Section Management
319   /// @{
320
321   /// \brief Return the current section that the streamer is emitting code to.
322   MCSectionSubPair getCurrentSection() const {
323     if (!SectionStack.empty())
324       return SectionStack.back().first;
325     return MCSectionSubPair();
326   }
327   MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
328
329   /// \brief Return the previous section that the streamer is emitting code to.
330   MCSectionSubPair getPreviousSection() const {
331     if (!SectionStack.empty())
332       return SectionStack.back().second;
333     return MCSectionSubPair();
334   }
335
336   /// \brief Returns an index to represent the order a symbol was emitted in.
337   /// (zero if we did not emit that symbol)
338   unsigned GetSymbolOrder(const MCSymbol *Sym) const {
339     return SymbolOrdering.lookup(Sym);
340   }
341
342   /// \brief Update streamer for a new active section.
343   ///
344   /// This is called by PopSection and SwitchSection, if the current
345   /// section changes.
346   virtual void ChangeSection(MCSection *, const MCExpr *);
347
348   /// \brief Save the current and previous section on the section stack.
349   void PushSection() {
350     SectionStack.push_back(
351         std::make_pair(getCurrentSection(), getPreviousSection()));
352   }
353
354   /// \brief Restore the current and previous section from the section stack.
355   /// Calls ChangeSection as needed.
356   ///
357   /// Returns false if the stack was empty.
358   bool PopSection() {
359     if (SectionStack.size() <= 1)
360       return false;
361     auto I = SectionStack.end();
362     --I;
363     MCSectionSubPair OldSection = I->first;
364     --I;
365     MCSectionSubPair NewSection = I->first;
366
367     if (OldSection != NewSection)
368       ChangeSection(NewSection.first, NewSection.second);
369     SectionStack.pop_back();
370     return true;
371   }
372
373   bool SubSection(const MCExpr *Subsection) {
374     if (SectionStack.empty())
375       return false;
376
377     SwitchSection(SectionStack.back().first.first, Subsection);
378     return true;
379   }
380
381   /// Set the current section where code is being emitted to \p Section.  This
382   /// is required to update CurSection.
383   ///
384   /// This corresponds to assembler directives like .section, .text, etc.
385   virtual void SwitchSection(MCSection *Section,
386                              const MCExpr *Subsection = nullptr);
387
388   /// \brief Set the current section where code is being emitted to \p Section.
389   /// This is required to update CurSection. This version does not call
390   /// ChangeSection.
391   void SwitchSectionNoChange(MCSection *Section,
392                              const MCExpr *Subsection = nullptr) {
393     assert(Section && "Cannot switch to a null section!");
394     MCSectionSubPair curSection = SectionStack.back().first;
395     SectionStack.back().second = curSection;
396     if (MCSectionSubPair(Section, Subsection) != curSection)
397       SectionStack.back().first = MCSectionSubPair(Section, Subsection);
398   }
399
400   /// \brief Create the default sections and set the initial one.
401   virtual void InitSections(bool NoExecStack);
402
403   MCSymbol *endSection(MCSection *Section);
404
405   /// \brief Sets the symbol's section.
406   ///
407   /// Each emitted symbol will be tracked in the ordering table,
408   /// so we can sort on them later.
409   void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
410
411   /// \brief Emit a label for \p Symbol into the current section.
412   ///
413   /// This corresponds to an assembler statement such as:
414   ///   foo:
415   ///
416   /// \param Symbol - The symbol to emit. A given symbol should only be
417   /// emitted as a label once, and symbols emitted as a label should never be
418   /// used in an assignment.
419   // FIXME: These emission are non-const because we mutate the symbol to
420   // add the section we're emitting it to later.
421   virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
422
423   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
424
425   /// \brief Note in the output the specified \p Flag.
426   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
427
428   /// \brief Emit the given list \p Options of strings as linker
429   /// options into the output.
430   virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
431
432   /// \brief Note in the output the specified region \p Kind.
433   virtual void EmitDataRegion(MCDataRegionType Kind) {}
434
435   /// \brief Specify the Mach-O minimum deployment target version.
436   virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
437                               unsigned Minor, unsigned Update) {}
438
439   /// Emit/Specify Mach-O build version command.
440   /// \p Platform should be one of MachO::PlatformType.
441   virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
442                                 unsigned Minor, unsigned Update) {}
443
444   void EmitVersionForTarget(const Triple &Target);
445
446   /// \brief Note in the output that the specified \p Func is a Thumb mode
447   /// function (ARM target only).
448   virtual void EmitThumbFunc(MCSymbol *Func);
449
450   /// \brief Emit an assignment of \p Value to \p Symbol.
451   ///
452   /// This corresponds to an assembler statement such as:
453   ///  symbol = value
454   ///
455   /// The assignment generates no code, but has the side effect of binding the
456   /// value in the current context. For the assembly streamer, this prints the
457   /// binding into the .s file.
458   ///
459   /// \param Symbol - The symbol being assigned to.
460   /// \param Value - The value for the symbol.
461   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
462
463   /// \brief Emit an weak reference from \p Alias to \p Symbol.
464   ///
465   /// This corresponds to an assembler statement such as:
466   ///  .weakref alias, symbol
467   ///
468   /// \param Alias - The alias that is being created.
469   /// \param Symbol - The symbol being aliased.
470   virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
471
472   /// \brief Add the given \p Attribute to \p Symbol.
473   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
474                                    MCSymbolAttr Attribute) = 0;
475
476   /// \brief Set the \p DescValue for the \p Symbol.
477   ///
478   /// \param Symbol - The symbol to have its n_desc field set.
479   /// \param DescValue - The value to set into the n_desc field.
480   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
481
482   /// \brief Start emitting COFF symbol definition
483   ///
484   /// \param Symbol - The symbol to have its External & Type fields set.
485   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
486
487   /// \brief Emit the storage class of the symbol.
488   ///
489   /// \param StorageClass - The storage class the symbol should have.
490   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
491
492   /// \brief Emit the type of the symbol.
493   ///
494   /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
495   virtual void EmitCOFFSymbolType(int Type);
496
497   /// \brief Marks the end of the symbol definition.
498   virtual void EndCOFFSymbolDef();
499
500   virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
501
502   /// \brief Emits a COFF section index.
503   ///
504   /// \param Symbol - Symbol the section number relocation should point to.
505   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
506
507   /// \brief Emits a COFF section relative relocation.
508   ///
509   /// \param Symbol - Symbol the section relative relocation should point to.
510   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
511
512   /// \brief Emit an ELF .size directive.
513   ///
514   /// This corresponds to an assembler statement such as:
515   ///  .size symbol, expression
516   virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
517
518   /// \brief Emit an ELF .symver directive.
519   ///
520   /// This corresponds to an assembler statement such as:
521   ///  .symver _start, foo@@SOME_VERSION
522   /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
523   /// \param Aliasee - The aliased symbol (i.e. "_start")
524   virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
525
526   /// \brief Emit a Linker Optimization Hint (LOH) directive.
527   /// \param Args - Arguments of the LOH.
528   virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
529
530   /// \brief Emit a common symbol.
531   ///
532   /// \param Symbol - The common symbol to emit.
533   /// \param Size - The size of the common symbol.
534   /// \param ByteAlignment - The alignment of the symbol if
535   /// non-zero. This must be a power of 2.
536   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
537                                 unsigned ByteAlignment) = 0;
538
539   /// \brief Emit a local common (.lcomm) symbol.
540   ///
541   /// \param Symbol - The common symbol to emit.
542   /// \param Size - The size of the common symbol.
543   /// \param ByteAlignment - The alignment of the common symbol in bytes.
544   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
545                                      unsigned ByteAlignment);
546
547   /// \brief Emit the zerofill section and an optional symbol.
548   ///
549   /// \param Section - The zerofill section to create and or to put the symbol
550   /// \param Symbol - The zerofill symbol to emit, if non-NULL.
551   /// \param Size - The size of the zerofill symbol.
552   /// \param ByteAlignment - The alignment of the zerofill symbol if
553   /// non-zero. This must be a power of 2 on some targets.
554   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
555                             uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
556
557   /// \brief Emit a thread local bss (.tbss) symbol.
558   ///
559   /// \param Section - The thread local common section.
560   /// \param Symbol - The thread local common symbol to emit.
561   /// \param Size - The size of the symbol.
562   /// \param ByteAlignment - The alignment of the thread local common symbol
563   /// if non-zero.  This must be a power of 2 on some targets.
564   virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
565                               uint64_t Size, unsigned ByteAlignment = 0);
566
567   /// @}
568   /// \name Generating Data
569   /// @{
570
571   /// \brief Emit the bytes in \p Data into the output.
572   ///
573   /// This is used to implement assembler directives such as .byte, .ascii,
574   /// etc.
575   virtual void EmitBytes(StringRef Data);
576
577   /// Functionally identical to EmitBytes. When emitting textual assembly, this
578   /// method uses .byte directives instead of .ascii or .asciz for readability.
579   virtual void EmitBinaryData(StringRef Data);
580
581   /// \brief Emit the expression \p Value into the output as a native
582   /// integer of the given \p Size bytes.
583   ///
584   /// This is used to implement assembler directives such as .word, .quad,
585   /// etc.
586   ///
587   /// \param Value - The value to emit.
588   /// \param Size - The size of the integer (in bytes) to emit. This must
589   /// match a native machine width.
590   /// \param Loc - The location of the expression for error reporting.
591   virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
592                              SMLoc Loc = SMLoc());
593
594   void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
595
596   /// \brief Special case of EmitValue that avoids the client having
597   /// to pass in a MCExpr for constant integers.
598   virtual void EmitIntValue(uint64_t Value, unsigned Size);
599
600   virtual void EmitULEB128Value(const MCExpr *Value);
601
602   virtual void EmitSLEB128Value(const MCExpr *Value);
603
604   /// \brief Special case of EmitULEB128Value that avoids the client having to
605   /// pass in a MCExpr for constant integers.
606   void EmitULEB128IntValue(uint64_t Value);
607
608   /// \brief Like EmitULEB128Value but pads the output to specific number of
609   /// bytes.
610   void EmitPaddedULEB128IntValue(uint64_t Value, unsigned PadTo);
611
612   /// \brief Special case of EmitSLEB128Value that avoids the client having to
613   /// pass in a MCExpr for constant integers.
614   void EmitSLEB128IntValue(int64_t Value);
615
616   /// \brief Special case of EmitValue that avoids the client having to pass in
617   /// a MCExpr for MCSymbols.
618   void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
619                        bool IsSectionRelative = false);
620
621   /// \brief Emit the expression \p Value into the output as a dtprel
622   /// (64-bit DTP relative) value.
623   ///
624   /// This is used to implement assembler directives such as .dtpreldword on
625   /// targets that support them.
626   virtual void EmitDTPRel64Value(const MCExpr *Value);
627
628   /// \brief Emit the expression \p Value into the output as a dtprel
629   /// (32-bit DTP relative) value.
630   ///
631   /// This is used to implement assembler directives such as .dtprelword on
632   /// targets that support them.
633   virtual void EmitDTPRel32Value(const MCExpr *Value);
634
635   /// \brief Emit the expression \p Value into the output as a tprel
636   /// (64-bit TP relative) value.
637   ///
638   /// This is used to implement assembler directives such as .tpreldword on
639   /// targets that support them.
640   virtual void EmitTPRel64Value(const MCExpr *Value);
641
642   /// \brief Emit the expression \p Value into the output as a tprel
643   /// (32-bit TP relative) value.
644   ///
645   /// This is used to implement assembler directives such as .tprelword on
646   /// targets that support them.
647   virtual void EmitTPRel32Value(const MCExpr *Value);
648
649   /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
650   /// GP relative) value.
651   ///
652   /// This is used to implement assembler directives such as .gpdword on
653   /// targets that support them.
654   virtual void EmitGPRel64Value(const MCExpr *Value);
655
656   /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
657   /// GP relative) value.
658   ///
659   /// This is used to implement assembler directives such as .gprel32 on
660   /// targets that support them.
661   virtual void EmitGPRel32Value(const MCExpr *Value);
662
663   /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
664   /// This implements directives such as '.space'.
665   virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
666
667   /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
668   ///
669   /// This is used to implement assembler directives such as .space or .skip.
670   ///
671   /// \param NumBytes - The number of bytes to emit.
672   /// \param FillValue - The value to use when filling bytes.
673   /// \param Loc - The location of the expression for error reporting.
674   virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
675                         SMLoc Loc = SMLoc());
676
677   /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
678   /// taken from the lowest order 4 bytes of \p Expr expression.
679   ///
680   /// This is used to implement assembler directives such as .fill.
681   ///
682   /// \param NumValues - The number of copies of \p Size bytes to emit.
683   /// \param Size - The size (in bytes) of each repeated value.
684   /// \param Expr - The expression from which \p Size bytes are used.
685   virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
686   virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
687                         SMLoc Loc = SMLoc());
688
689   /// \brief Emit NumBytes worth of zeros.
690   /// This function properly handles data in virtual sections.
691   void EmitZeros(uint64_t NumBytes);
692
693   /// \brief Emit some number of copies of \p Value until the byte alignment \p
694   /// ByteAlignment is reached.
695   ///
696   /// If the number of bytes need to emit for the alignment is not a multiple
697   /// of \p ValueSize, then the contents of the emitted fill bytes is
698   /// undefined.
699   ///
700   /// This used to implement the .align assembler directive.
701   ///
702   /// \param ByteAlignment - The alignment to reach. This must be a power of
703   /// two on some targets.
704   /// \param Value - The value to use when filling bytes.
705   /// \param ValueSize - The size of the integer (in bytes) to emit for
706   /// \p Value. This must match a native machine width.
707   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
708   /// the alignment cannot be reached in this many bytes, no bytes are
709   /// emitted.
710   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
711                                     unsigned ValueSize = 1,
712                                     unsigned MaxBytesToEmit = 0);
713
714   /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
715   ///
716   /// This used to align code where the alignment bytes may be executed.  This
717   /// can emit different bytes for different sizes to optimize execution.
718   ///
719   /// \param ByteAlignment - The alignment to reach. This must be a power of
720   /// two on some targets.
721   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
722   /// the alignment cannot be reached in this many bytes, no bytes are
723   /// emitted.
724   virtual void EmitCodeAlignment(unsigned ByteAlignment,
725                                  unsigned MaxBytesToEmit = 0);
726
727   /// \brief Emit some number of copies of \p Value until the byte offset \p
728   /// Offset is reached.
729   ///
730   /// This is used to implement assembler directives such as .org.
731   ///
732   /// \param Offset - The offset to reach. This may be an expression, but the
733   /// expression must be associated with the current section.
734   /// \param Value - The value to use when filling bytes.
735   virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
736                                  SMLoc Loc);
737
738   virtual void
739   EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
740
741   virtual void
742   EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
743
744   /// @}
745
746   /// \brief Switch to a new logical file.  This is used to implement the '.file
747   /// "foo.c"' assembler directive.
748   virtual void EmitFileDirective(StringRef Filename);
749
750   /// \brief Emit the "identifiers" directive.  This implements the
751   /// '.ident "version foo"' assembler directive.
752   virtual void EmitIdent(StringRef IdentString) {}
753
754   /// \brief Associate a filename with a specified logical file number.  This
755   /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
756   virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
757                                           StringRef Filename,
758                                           unsigned CUID = 0);
759
760   /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
761   /// directive.
762   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
763                                      unsigned Column, unsigned Flags,
764                                      unsigned Isa, unsigned Discriminator,
765                                      StringRef FileName);
766
767   /// Associate a filename with a specified logical file number, and also
768   /// specify that file's checksum information.  This implements the '.cv_file 4
769   /// "foo.c"' assembler directive. Returns true on success.
770   virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
771                                    ArrayRef<uint8_t> Checksum,
772                                    unsigned ChecksumKind);
773
774   /// \brief Introduces a function id for use with .cv_loc.
775   virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
776
777   /// \brief Introduces an inline call site id for use with .cv_loc. Includes
778   /// extra information for inline line table generation.
779   virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
780                                            unsigned IAFile, unsigned IALine,
781                                            unsigned IACol, SMLoc Loc);
782
783   /// \brief This implements the CodeView '.cv_loc' assembler directive.
784   virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
785                                   unsigned Line, unsigned Column,
786                                   bool PrologueEnd, bool IsStmt,
787                                   StringRef FileName, SMLoc Loc);
788
789   /// \brief This implements the CodeView '.cv_linetable' assembler directive.
790   virtual void EmitCVLinetableDirective(unsigned FunctionId,
791                                         const MCSymbol *FnStart,
792                                         const MCSymbol *FnEnd);
793
794   /// \brief This implements the CodeView '.cv_inline_linetable' assembler
795   /// directive.
796   virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
797                                               unsigned SourceFileId,
798                                               unsigned SourceLineNum,
799                                               const MCSymbol *FnStartSym,
800                                               const MCSymbol *FnEndSym);
801
802   /// \brief This implements the CodeView '.cv_def_range' assembler
803   /// directive.
804   virtual void EmitCVDefRangeDirective(
805       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
806       StringRef FixedSizePortion);
807
808   /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
809   virtual void EmitCVStringTableDirective() {}
810
811   /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
812   virtual void EmitCVFileChecksumsDirective() {}
813
814   /// This implements the CodeView '.cv_filechecksumoffset' assembler
815   /// directive.
816   virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
817
818   /// This implements the CodeView '.cv_fpo_data' assembler directive.
819   virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
820
821   /// Emit the absolute difference between two symbols.
822   ///
823   /// \pre Offset of \c Hi is greater than the offset \c Lo.
824   virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
825                                       unsigned Size);
826
827   virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
828   virtual void EmitCFISections(bool EH, bool Debug);
829   void EmitCFIStartProc(bool IsSimple);
830   void EmitCFIEndProc();
831   virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
832   virtual void EmitCFIDefCfaOffset(int64_t Offset);
833   virtual void EmitCFIDefCfaRegister(int64_t Register);
834   virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
835   virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
836   virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
837   virtual void EmitCFIRememberState();
838   virtual void EmitCFIRestoreState();
839   virtual void EmitCFISameValue(int64_t Register);
840   virtual void EmitCFIRestore(int64_t Register);
841   virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
842   virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
843   virtual void EmitCFIEscape(StringRef Values);
844   virtual void EmitCFIReturnColumn(int64_t Register);
845   virtual void EmitCFIGnuArgsSize(int64_t Size);
846   virtual void EmitCFISignalFrame();
847   virtual void EmitCFIUndefined(int64_t Register);
848   virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
849   virtual void EmitCFIWindowSave();
850
851   virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
852   virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
853   virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
854   virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
855   virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
856   virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
857                                   SMLoc Loc = SMLoc());
858   virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
859   virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
860                                  SMLoc Loc = SMLoc());
861   virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
862                                  SMLoc Loc = SMLoc());
863   virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
864   virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
865   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
866                                 SMLoc Loc = SMLoc());
867   virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
868
869   /// Get the .pdata section used for the given section. Typically the given
870   /// section is either the main .text section or some other COMDAT .text
871   /// section, but it may be any section containing code.
872   MCSection *getAssociatedPDataSection(const MCSection *TextSec);
873
874   /// Get the .xdata section used for the given section.
875   MCSection *getAssociatedXDataSection(const MCSection *TextSec);
876
877   virtual void EmitSyntaxDirective();
878
879   /// \brief Emit a .reloc directive.
880   /// Returns true if the relocation could not be emitted because Name is not
881   /// known.
882   virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
883                                   const MCExpr *Expr, SMLoc Loc) {
884     return true;
885   }
886
887   /// \brief Emit the given \p Instruction into the current section.
888   /// PrintSchedInfo == true then schedul comment should be added to output
889   virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
890                                bool PrintSchedInfo = false);
891
892   /// \brief Set the bundle alignment mode from now on in the section.
893   /// The argument is the power of 2 to which the alignment is set. The
894   /// value 0 means turn the bundle alignment off.
895   virtual void EmitBundleAlignMode(unsigned AlignPow2);
896
897   /// \brief The following instructions are a bundle-locked group.
898   ///
899   /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
900   ///                     the end of a bundle.
901   virtual void EmitBundleLock(bool AlignToEnd);
902
903   /// \brief Ends a bundle-locked group.
904   virtual void EmitBundleUnlock();
905
906   /// \brief If this file is backed by a assembly streamer, this dumps the
907   /// specified string in the output .s file.  This capability is indicated by
908   /// the hasRawTextSupport() predicate.  By default this aborts.
909   void EmitRawText(const Twine &String);
910
911   /// \brief Streamer specific finalization.
912   virtual void FinishImpl();
913   /// \brief Finish emission of machine code.
914   void Finish();
915
916   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
917 };
918
919 /// Create a dummy machine code streamer, which does nothing. This is useful for
920 /// timing the assembler front end.
921 MCStreamer *createNullStreamer(MCContext &Ctx);
922
923 /// Create a machine code streamer which will print out assembly for the native
924 /// target, suitable for compiling with a native assembler.
925 ///
926 /// \param InstPrint - If given, the instruction printer to use. If not given
927 /// the MCInst representation will be printed.  This method takes ownership of
928 /// InstPrint.
929 ///
930 /// \param CE - If given, a code emitter to use to show the instruction
931 /// encoding inline with the assembly. This method takes ownership of \p CE.
932 ///
933 /// \param TAB - If given, a target asm backend to use to show the fixup
934 /// information in conjunction with encoding information. This method takes
935 /// ownership of \p TAB.
936 ///
937 /// \param ShowInst - Whether to show the MCInst representation inline with
938 /// the assembly.
939 MCStreamer *createAsmStreamer(MCContext &Ctx,
940                               std::unique_ptr<formatted_raw_ostream> OS,
941                               bool isVerboseAsm, bool useDwarfDirectory,
942                               MCInstPrinter *InstPrint, MCCodeEmitter *CE,
943                               MCAsmBackend *TAB, bool ShowInst);
944
945 } // end namespace llvm
946
947 #endif // LLVM_MC_MCSTREAMER_H