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