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