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