1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MCStreamer class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSTREAMER_H
15 #define LLVM_MC_MCSTREAMER_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCWin64EH.h"
22 #include "llvm/Support/DataTypes.h"
36 class formatted_raw_ostream;
38 /// MCStreamer - Streaming machine code generation interface. This interface
39 /// is intended to provide a programatic interface that is very similar to the
40 /// level that an assembler .s file provides. It has callbacks to emit bytes,
41 /// handle directives, etc. The implementation of this interface retains
42 /// state to know what the current section is etc.
44 /// There are multiple implementations of this interface: one for writing out
45 /// a .s file, and implementations that write out .o files of various formats.
50 MCStreamer(const MCStreamer&) LLVM_DELETED_FUNCTION;
51 MCStreamer &operator=(const MCStreamer&) LLVM_DELETED_FUNCTION;
56 std::vector<MCDwarfFrameInfo> FrameInfos;
57 MCDwarfFrameInfo *getCurrentFrameInfo();
58 MCSymbol *EmitCFICommon();
59 void EnsureValidFrame();
61 std::vector<MCWin64EHUnwindInfo *> W64UnwindInfos;
62 MCWin64EHUnwindInfo *CurrentW64UnwindInfo;
63 void setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame);
64 void EnsureValidW64UnwindInfo();
68 /// SectionStack - This is stack of current and previous section
69 /// values saved by PushSection.
70 SmallVector<std::pair<const MCSection *,
71 const MCSection *>, 4> SectionStack;
74 MCStreamer(MCContext &Ctx);
76 const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A,
79 const MCExpr *ForceExpAbs(const MCExpr* Expr);
81 void RecordProcStart(MCDwarfFrameInfo &Frame);
82 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
83 void RecordProcEnd(MCDwarfFrameInfo &Frame);
84 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
85 void EmitFrames(bool usingCFI);
87 MCWin64EHUnwindInfo *getCurrentW64UnwindInfo(){return CurrentW64UnwindInfo;}
91 virtual ~MCStreamer();
93 MCContext &getContext() const { return Context; }
95 unsigned getNumFrameInfos() {
96 return FrameInfos.size();
99 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
100 return FrameInfos[i];
103 ArrayRef<MCDwarfFrameInfo> getFrameInfos() {
107 unsigned getNumW64UnwindInfos() {
108 return W64UnwindInfos.size();
111 MCWin64EHUnwindInfo &getW64UnwindInfo(unsigned i) {
112 return *W64UnwindInfos[i];
115 /// @name Assembly File Formatting.
118 /// isVerboseAsm - Return true if this streamer supports verbose assembly
119 /// and if it is enabled.
120 virtual bool isVerboseAsm() const { return false; }
122 /// hasRawTextSupport - Return true if this asm streamer supports emitting
123 /// unformatted text to the .s file with EmitRawText.
124 virtual bool hasRawTextSupport() const { return false; }
126 /// AddComment - Add a comment that can be emitted to the generated .s
127 /// file if applicable as a QoI issue to make the output of the compiler
128 /// more readable. This only affects the MCAsmStreamer, and only when
129 /// verbose assembly output is enabled.
131 /// If the comment includes embedded \n's, they will each get the comment
132 /// prefix as appropriate. The added comment should not end with a \n.
133 virtual void AddComment(const Twine &T) {}
135 /// GetCommentOS - Return a raw_ostream that comments can be written to.
136 /// Unlike AddComment, you are required to terminate comments with \n if you
138 virtual raw_ostream &GetCommentOS();
140 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
141 virtual void AddBlankLine() {}
145 /// @name Symbol & Section Management
148 /// getCurrentSection - Return the current section that the streamer is
149 /// emitting code to.
150 const MCSection *getCurrentSection() const {
151 if (!SectionStack.empty())
152 return SectionStack.back().first;
156 /// getPreviousSection - Return the previous section that the streamer is
157 /// emitting code to.
158 const MCSection *getPreviousSection() const {
159 if (!SectionStack.empty())
160 return SectionStack.back().second;
164 /// ChangeSection - Update streamer for a new active section.
166 /// This is called by PopSection and SwitchSection, if the current
168 virtual void ChangeSection(const MCSection *) = 0;
170 /// pushSection - Save the current and previous section on the
173 SectionStack.push_back(std::make_pair(getCurrentSection(),
174 getPreviousSection()));
177 /// popSection - Restore the current and previous section from
178 /// the section stack. Calls ChangeSection as needed.
180 /// Returns false if the stack was empty.
182 if (SectionStack.size() <= 1)
184 const MCSection *oldSection = SectionStack.pop_back_val().first;
185 const MCSection *curSection = SectionStack.back().first;
187 if (oldSection != curSection)
188 ChangeSection(curSection);
192 /// SwitchSection - Set the current section where code is being emitted to
193 /// @p Section. This is required to update CurSection.
195 /// This corresponds to assembler directives like .section, .text, etc.
196 void SwitchSection(const MCSection *Section) {
197 assert(Section && "Cannot switch to a null section!");
198 const MCSection *curSection = SectionStack.back().first;
199 SectionStack.back().second = curSection;
200 if (Section != curSection) {
201 SectionStack.back().first = Section;
202 ChangeSection(Section);
206 /// SwitchSectionNoChange - Set the current section where code is being
207 /// emitted to @p Section. This is required to update CurSection. This
208 /// version does not call ChangeSection.
209 void SwitchSectionNoChange(const MCSection *Section) {
210 assert(Section && "Cannot switch to a null section!");
211 const MCSection *curSection = SectionStack.back().first;
212 SectionStack.back().second = curSection;
213 if (Section != curSection)
214 SectionStack.back().first = Section;
217 /// InitSections - Create the default sections and set the initial one.
218 virtual void InitSections() = 0;
220 /// EmitLabel - Emit a label for @p Symbol into the current section.
222 /// This corresponds to an assembler statement such as:
225 /// @param Symbol - The symbol to emit. A given symbol should only be
226 /// emitted as a label once, and symbols emitted as a label should never be
227 /// used in an assignment.
228 virtual void EmitLabel(MCSymbol *Symbol);
230 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
233 /// EmitAssemblerFlag - Note in the output the specified @p Flag.
234 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
236 /// EmitDataRegion - Note in the output the specified region @p Kind.
237 virtual void EmitDataRegion(MCDataRegionType Kind) {}
239 /// EmitThumbFunc - Note in the output that the specified @p Func is
240 /// a Thumb mode function (ARM target only).
241 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
243 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
245 /// This corresponds to an assembler statement such as:
248 /// The assignment generates no code, but has the side effect of binding the
249 /// value in the current context. For the assembly streamer, this prints the
250 /// binding into the .s file.
252 /// @param Symbol - The symbol being assigned to.
253 /// @param Value - The value for the symbol.
254 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
256 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
258 /// This corresponds to an assembler statement such as:
259 /// .weakref alias, symbol
261 /// @param Alias - The alias that is being created.
262 /// @param Symbol - The symbol being aliased.
263 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
265 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
266 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
267 MCSymbolAttr Attribute) = 0;
269 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
271 /// @param Symbol - The symbol to have its n_desc field set.
272 /// @param DescValue - The value to set into the n_desc field.
273 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
275 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
277 /// @param Symbol - The symbol to have its External & Type fields set.
278 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
280 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
282 /// @param StorageClass - The storage class the symbol should have.
283 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
285 /// EmitCOFFSymbolType - Emit the type of the symbol.
287 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
288 virtual void EmitCOFFSymbolType(int Type) = 0;
290 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
291 virtual void EndCOFFSymbolDef() = 0;
293 /// EmitCOFFSecRel32 - Emits a COFF section relative relocation.
295 /// @param Symbol - Symbol the section relative realocation should point to.
296 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
298 /// EmitELFSize - Emit an ELF .size directive.
300 /// This corresponds to an assembler statement such as:
301 /// .size symbol, expression
303 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
305 /// EmitCommonSymbol - Emit a common symbol.
307 /// @param Symbol - The common symbol to emit.
308 /// @param Size - The size of the common symbol.
309 /// @param ByteAlignment - The alignment of the symbol if
310 /// non-zero. This must be a power of 2.
311 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
312 unsigned ByteAlignment) = 0;
314 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
316 /// @param Symbol - The common symbol to emit.
317 /// @param Size - The size of the common symbol.
318 /// @param ByteAlignment - The alignment of the common symbol in bytes.
319 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
320 unsigned ByteAlignment) = 0;
322 /// EmitZerofill - Emit the zerofill section and an optional symbol.
324 /// @param Section - The zerofill section to create and or to put the symbol
325 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
326 /// @param Size - The size of the zerofill symbol.
327 /// @param ByteAlignment - The alignment of the zerofill symbol if
328 /// non-zero. This must be a power of 2 on some targets.
329 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
330 uint64_t Size = 0,unsigned ByteAlignment = 0) = 0;
332 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
334 /// @param Section - The thread local common section.
335 /// @param Symbol - The thread local common symbol to emit.
336 /// @param Size - The size of the symbol.
337 /// @param ByteAlignment - The alignment of the thread local common symbol
338 /// if non-zero. This must be a power of 2 on some targets.
339 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
340 uint64_t Size, unsigned ByteAlignment = 0) = 0;
343 /// @name Generating Data
346 /// EmitBytes - Emit the bytes in \p Data into the output.
348 /// This is used to implement assembler directives such as .byte, .ascii,
350 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
352 /// EmitValue - Emit the expression @p Value into the output as a native
353 /// integer of the given @p Size bytes.
355 /// This is used to implement assembler directives such as .word, .quad,
358 /// @param Value - The value to emit.
359 /// @param Size - The size of the integer (in bytes) to emit. This must
360 /// match a native machine width.
361 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
362 unsigned AddrSpace) = 0;
364 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
366 /// EmitIntValue - Special case of EmitValue that avoids the client having
367 /// to pass in a MCExpr for constant integers.
368 virtual void EmitIntValue(uint64_t Value, unsigned Size,
369 unsigned AddrSpace = 0);
371 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
372 /// this is done by producing
375 void EmitAbsValue(const MCExpr *Value, unsigned Size,
376 unsigned AddrSpace = 0);
378 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
380 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
382 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
383 /// client having to pass in a MCExpr for constant integers.
384 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0,
385 unsigned Padding = 0);
387 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
388 /// client having to pass in a MCExpr for constant integers.
389 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
391 /// EmitSymbolValue - Special case of EmitValue that avoids the client
392 /// having to pass in a MCExpr for MCSymbols.
393 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
394 unsigned AddrSpace = 0);
396 /// EmitGPRel64Value - Emit the expression @p Value into the output as a
397 /// gprel64 (64-bit GP relative) value.
399 /// This is used to implement assembler directives such as .gpdword on
400 /// targets that support them.
401 virtual void EmitGPRel64Value(const MCExpr *Value);
403 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
404 /// gprel32 (32-bit GP relative) value.
406 /// This is used to implement assembler directives such as .gprel32 on
407 /// targets that support them.
408 virtual void EmitGPRel32Value(const MCExpr *Value);
410 /// EmitFill - Emit NumBytes bytes worth of the value specified by
411 /// FillValue. This implements directives such as '.space'.
412 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
415 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
416 /// function that just wraps EmitFill.
417 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
418 EmitFill(NumBytes, 0, AddrSpace);
422 /// EmitValueToAlignment - Emit some number of copies of @p Value until
423 /// the byte alignment @p ByteAlignment is reached.
425 /// If the number of bytes need to emit for the alignment is not a multiple
426 /// of @p ValueSize, then the contents of the emitted fill bytes is
429 /// This used to implement the .align assembler directive.
431 /// @param ByteAlignment - The alignment to reach. This must be a power of
432 /// two on some targets.
433 /// @param Value - The value to use when filling bytes.
434 /// @param ValueSize - The size of the integer (in bytes) to emit for
435 /// @p Value. This must match a native machine width.
436 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
437 /// the alignment cannot be reached in this many bytes, no bytes are
439 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
440 unsigned ValueSize = 1,
441 unsigned MaxBytesToEmit = 0) = 0;
443 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
446 /// This used to align code where the alignment bytes may be executed. This
447 /// can emit different bytes for different sizes to optimize execution.
449 /// @param ByteAlignment - The alignment to reach. This must be a power of
450 /// two on some targets.
451 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
452 /// the alignment cannot be reached in this many bytes, no bytes are
454 virtual void EmitCodeAlignment(unsigned ByteAlignment,
455 unsigned MaxBytesToEmit = 0) = 0;
457 /// EmitValueToOffset - Emit some number of copies of @p Value until the
458 /// byte offset @p Offset is reached.
460 /// This is used to implement assembler directives such as .org.
462 /// @param Offset - The offset to reach. This may be an expression, but the
463 /// expression must be associated with the current section.
464 /// @param Value - The value to use when filling bytes.
465 /// @return false on success, true if the offset was invalid.
466 virtual bool EmitValueToOffset(const MCExpr *Offset,
467 unsigned char Value = 0) = 0;
471 /// EmitFileDirective - Switch to a new logical file. This is used to
472 /// implement the '.file "foo.c"' assembler directive.
473 virtual void EmitFileDirective(StringRef Filename) = 0;
475 /// EmitDwarfFileDirective - Associate a filename with a specified logical
476 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
478 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
481 /// EmitDwarfLocDirective - This implements the DWARF2
482 // '.loc fileno lineno ...' assembler directive.
483 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
484 unsigned Column, unsigned Flags,
486 unsigned Discriminator,
489 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
490 const MCSymbol *LastLabel,
491 const MCSymbol *Label,
492 unsigned PointerSize) = 0;
494 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
495 const MCSymbol *Label) {
498 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
501 virtual void EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding);
502 virtual void EmitCFISections(bool EH, bool Debug);
503 void EmitCFIStartProc();
504 void EmitCFIEndProc();
505 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
506 virtual void EmitCFIDefCfaOffset(int64_t Offset);
507 virtual void EmitCFIDefCfaRegister(int64_t Register);
508 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
509 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
510 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
511 virtual void EmitCFIRememberState();
512 virtual void EmitCFIRestoreState();
513 virtual void EmitCFISameValue(int64_t Register);
514 virtual void EmitCFIRestore(int64_t Register);
515 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
516 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
517 virtual void EmitCFIEscape(StringRef Values);
518 virtual void EmitCFISignalFrame();
519 virtual void EmitCFIUndefined(int64_t Register);
520 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
522 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
523 virtual void EmitWin64EHEndProc();
524 virtual void EmitWin64EHStartChained();
525 virtual void EmitWin64EHEndChained();
526 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
528 virtual void EmitWin64EHHandlerData();
529 virtual void EmitWin64EHPushReg(unsigned Register);
530 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
531 virtual void EmitWin64EHAllocStack(unsigned Size);
532 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
533 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
534 virtual void EmitWin64EHPushFrame(bool Code);
535 virtual void EmitWin64EHEndProlog();
537 /// EmitInstruction - Emit the given @p Instruction into the current
539 virtual void EmitInstruction(const MCInst &Inst) = 0;
541 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
542 /// the specified string in the output .s file. This capability is
543 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
544 virtual void EmitRawText(StringRef String);
545 void EmitRawText(const Twine &String);
547 /// ARM-related methods.
548 /// FIXME: Eventually we should have some "target MC streamer" and move
549 /// these methods there.
550 virtual void EmitFnStart();
551 virtual void EmitFnEnd();
552 virtual void EmitCantUnwind();
553 virtual void EmitPersonality(const MCSymbol *Personality);
554 virtual void EmitHandlerData();
555 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
556 virtual void EmitPad(int64_t Offset);
557 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
560 /// PPC-related methods.
561 /// FIXME: Eventually replace it with some "target MC streamer" and move
562 /// these methods there.
563 virtual void EmitTCEntry(const MCSymbol &S);
565 /// FinishImpl - Streamer specific finalization.
566 virtual void FinishImpl() = 0;
567 /// Finish - Finish emission of machine code.
571 /// createNullStreamer - Create a dummy machine code streamer, which does
572 /// nothing. This is useful for timing the assembler front end.
573 MCStreamer *createNullStreamer(MCContext &Ctx);
575 /// createAsmStreamer - Create a machine code streamer which will print out
576 /// assembly for the native target, suitable for compiling with a native
579 /// \param InstPrint - If given, the instruction printer to use. If not given
580 /// the MCInst representation will be printed. This method takes ownership of
583 /// \param CE - If given, a code emitter to use to show the instruction
584 /// encoding inline with the assembly. This method takes ownership of \p CE.
586 /// \param TAB - If given, a target asm backend to use to show the fixup
587 /// information in conjunction with encoding information. This method takes
588 /// ownership of \p TAB.
590 /// \param ShowInst - Whether to show the MCInst representation inline with
592 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
596 bool useDwarfDirectory,
597 MCInstPrinter *InstPrint = 0,
598 MCCodeEmitter *CE = 0,
599 MCAsmBackend *TAB = 0,
600 bool ShowInst = false);
602 /// createMachOStreamer - Create a machine code streamer which will generate
603 /// Mach-O format object files.
605 /// Takes ownership of \p TAB and \p CE.
606 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
607 raw_ostream &OS, MCCodeEmitter *CE,
608 bool RelaxAll = false);
610 /// createWinCOFFStreamer - Create a machine code streamer which will
611 /// generate Microsoft COFF format object files.
613 /// Takes ownership of \p TAB and \p CE.
614 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
616 MCCodeEmitter &CE, raw_ostream &OS,
617 bool RelaxAll = false);
619 /// createELFStreamer - Create a machine code streamer which will generate
620 /// ELF format object files.
621 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
622 raw_ostream &OS, MCCodeEmitter *CE,
623 bool RelaxAll, bool NoExecStack);
625 /// createPureStreamer - Create a machine code streamer which will generate
626 /// "pure" MC object files, for use with MC-JIT and testing tools.
628 /// Takes ownership of \p TAB and \p CE.
629 MCStreamer *createPureStreamer(MCContext &Ctx, MCAsmBackend &TAB,
630 raw_ostream &OS, MCCodeEmitter *CE);
632 } // end namespace llvm