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