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