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