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