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