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/Support/DataTypes.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCDwarf.h"
20 #include "llvm/MC/MCWin64EH.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallVector.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 void EnsureValidFrame();
60 std::vector<MCWin64EHUnwindInfo *> W64UnwindInfos;
61 MCWin64EHUnwindInfo *CurrentW64UnwindInfo;
62 void setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame);
63 void EnsureValidW64UnwindInfo();
67 /// SectionStack - This is stack of current and previous section
68 /// values saved by PushSection.
69 SmallVector<std::pair<const MCSection *,
70 const MCSection *>, 4> SectionStack;
73 MCStreamer(MCContext &Ctx);
75 const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A,
78 const MCExpr *ForceExpAbs(const MCExpr* Expr);
80 void RecordProcStart(MCDwarfFrameInfo &Frame);
81 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
82 void RecordProcEnd(MCDwarfFrameInfo &Frame);
83 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
84 void EmitFrames(bool usingCFI);
86 MCWin64EHUnwindInfo *getCurrentW64UnwindInfo(){return CurrentW64UnwindInfo;}
90 virtual ~MCStreamer();
92 MCContext &getContext() const { return Context; }
94 unsigned getNumFrameInfos() {
95 return FrameInfos.size();
98 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
102 ArrayRef<MCDwarfFrameInfo> getFrameInfos() {
106 unsigned getNumW64UnwindInfos() {
107 return W64UnwindInfos.size();
110 MCWin64EHUnwindInfo &getW64UnwindInfo(unsigned i) {
111 return *W64UnwindInfos[i];
114 /// @name Assembly File Formatting.
117 /// isVerboseAsm - Return true if this streamer supports verbose assembly
118 /// and if it is enabled.
119 virtual bool isVerboseAsm() const { return false; }
121 /// hasRawTextSupport - Return true if this asm streamer supports emitting
122 /// unformatted text to the .s file with EmitRawText.
123 virtual bool hasRawTextSupport() const { return false; }
125 /// AddComment - Add a comment that can be emitted to the generated .s
126 /// file if applicable as a QoI issue to make the output of the compiler
127 /// more readable. This only affects the MCAsmStreamer, and only when
128 /// verbose assembly output is enabled.
130 /// If the comment includes embedded \n's, they will each get the comment
131 /// prefix as appropriate. The added comment should not end with a \n.
132 virtual void AddComment(const Twine &T) {}
134 /// GetCommentOS - Return a raw_ostream that comments can be written to.
135 /// Unlike AddComment, you are required to terminate comments with \n if you
137 virtual raw_ostream &GetCommentOS();
139 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
140 virtual void AddBlankLine() {}
144 /// @name Symbol & Section Management
147 /// getCurrentSection - Return the current section that the streamer is
148 /// emitting code to.
149 const MCSection *getCurrentSection() const {
150 if (!SectionStack.empty())
151 return SectionStack.back().first;
155 /// getPreviousSection - Return the previous section that the streamer is
156 /// emitting code to.
157 const MCSection *getPreviousSection() const {
158 if (!SectionStack.empty())
159 return SectionStack.back().second;
163 /// ChangeSection - Update streamer for a new active section.
165 /// This is called by PopSection and SwitchSection, if the current
167 virtual void ChangeSection(const MCSection *) = 0;
169 /// pushSection - Save the current and previous section on the
172 SectionStack.push_back(std::make_pair(getCurrentSection(),
173 getPreviousSection()));
176 /// popSection - Restore the current and previous section from
177 /// the section stack. Calls ChangeSection as needed.
179 /// Returns false if the stack was empty.
181 if (SectionStack.size() <= 1)
183 const MCSection *oldSection = SectionStack.pop_back_val().first;
184 const MCSection *curSection = SectionStack.back().first;
186 if (oldSection != curSection)
187 ChangeSection(curSection);
191 /// SwitchSection - Set the current section where code is being emitted to
192 /// @p Section. This is required to update CurSection.
194 /// This corresponds to assembler directives like .section, .text, etc.
195 void SwitchSection(const MCSection *Section) {
196 assert(Section && "Cannot switch to a null section!");
197 const MCSection *curSection = SectionStack.back().first;
198 SectionStack.back().second = curSection;
199 if (Section != curSection) {
200 SectionStack.back().first = Section;
201 ChangeSection(Section);
205 /// SwitchSectionNoChange - Set the current section where code is being
206 /// emitted to @p Section. This is required to update CurSection. This
207 /// version does not call ChangeSection.
208 void SwitchSectionNoChange(const MCSection *Section) {
209 assert(Section && "Cannot switch to a null section!");
210 const MCSection *curSection = SectionStack.back().first;
211 SectionStack.back().second = curSection;
212 if (Section != curSection)
213 SectionStack.back().first = Section;
216 /// InitSections - Create the default sections and set the initial one.
217 virtual void InitSections() = 0;
219 /// EmitLabel - Emit a label for @p Symbol into the current section.
221 /// This corresponds to an assembler statement such as:
224 /// @param Symbol - The symbol to emit. A given symbol should only be
225 /// emitted as a label once, and symbols emitted as a label should never be
226 /// used in an assignment.
227 virtual void EmitLabel(MCSymbol *Symbol);
229 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
232 /// EmitAssemblerFlag - Note in the output the specified @p Flag.
233 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
235 /// EmitDataRegion - Note in the output the specified region @p Kind.
236 virtual void EmitDataRegion(MCDataRegionType Kind) {}
238 /// EmitThumbFunc - Note in the output that the specified @p Func is
239 /// a Thumb mode function (ARM target only).
240 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
242 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
244 /// This corresponds to an assembler statement such as:
247 /// The assignment generates no code, but has the side effect of binding the
248 /// value in the current context. For the assembly streamer, this prints the
249 /// binding into the .s file.
251 /// @param Symbol - The symbol being assigned to.
252 /// @param Value - The value for the symbol.
253 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
255 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
257 /// This corresponds to an assembler statement such as:
258 /// .weakref alias, symbol
260 /// @param Alias - The alias that is being created.
261 /// @param Symbol - The symbol being aliased.
262 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
264 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
265 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
266 MCSymbolAttr Attribute) = 0;
268 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
270 /// @param Symbol - The symbol to have its n_desc field set.
271 /// @param DescValue - The value to set into the n_desc field.
272 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
274 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
276 /// @param Symbol - The symbol to have its External & Type fields set.
277 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
279 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
281 /// @param StorageClass - The storage class the symbol should have.
282 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
284 /// EmitCOFFSymbolType - Emit the type of the symbol.
286 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
287 virtual void EmitCOFFSymbolType(int Type) = 0;
289 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
290 virtual void EndCOFFSymbolDef() = 0;
292 /// EmitCOFFSecRel32 - Emits a COFF section relative relocation.
294 /// @param Symbol - Symbol the section relative realocation should point to.
295 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
297 /// EmitELFSize - Emit an ELF .size directive.
299 /// This corresponds to an assembler statement such as:
300 /// .size symbol, expression
302 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
304 /// EmitCommonSymbol - Emit a common symbol.
306 /// @param Symbol - The common symbol to emit.
307 /// @param Size - The size of the common symbol.
308 /// @param ByteAlignment - The alignment of the symbol if
309 /// non-zero. This must be a power of 2.
310 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
311 unsigned ByteAlignment) = 0;
313 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
315 /// @param Symbol - The common symbol to emit.
316 /// @param Size - The size of the common symbol.
317 /// @param ByteAlignment - The alignment of the common symbol in bytes.
318 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
319 unsigned ByteAlignment) = 0;
321 /// EmitZerofill - Emit the zerofill section and an optional symbol.
323 /// @param Section - The zerofill section to create and or to put the symbol
324 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
325 /// @param Size - The size of the zerofill symbol.
326 /// @param ByteAlignment - The alignment of the zerofill symbol if
327 /// non-zero. This must be a power of 2 on some targets.
328 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
329 uint64_t Size = 0,unsigned ByteAlignment = 0) = 0;
331 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
333 /// @param Section - The thread local common section.
334 /// @param Symbol - The thread local common symbol to emit.
335 /// @param Size - The size of the symbol.
336 /// @param ByteAlignment - The alignment of the thread local common symbol
337 /// if non-zero. This must be a power of 2 on some targets.
338 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
339 uint64_t Size, unsigned ByteAlignment = 0) = 0;
342 /// @name Generating Data
345 /// EmitBytes - Emit the bytes in \p Data into the output.
347 /// This is used to implement assembler directives such as .byte, .ascii,
349 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
351 /// EmitValue - Emit the expression @p Value into the output as a native
352 /// integer of the given @p Size bytes.
354 /// This is used to implement assembler directives such as .word, .quad,
357 /// @param Value - The value to emit.
358 /// @param Size - The size of the integer (in bytes) to emit. This must
359 /// match a native machine width.
360 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
361 unsigned AddrSpace) = 0;
363 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
365 /// EmitIntValue - Special case of EmitValue that avoids the client having
366 /// to pass in a MCExpr for constant integers.
367 virtual void EmitIntValue(uint64_t Value, unsigned Size,
368 unsigned AddrSpace = 0);
370 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
371 /// this is done by producing
374 void EmitAbsValue(const MCExpr *Value, unsigned Size,
375 unsigned AddrSpace = 0);
377 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
379 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
381 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
382 /// client having to pass in a MCExpr for constant integers.
383 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0,
384 unsigned Padding = 0);
386 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
387 /// client having to pass in a MCExpr for constant integers.
388 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
390 /// EmitSymbolValue - Special case of EmitValue that avoids the client
391 /// having to pass in a MCExpr for MCSymbols.
392 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
393 unsigned AddrSpace = 0);
395 /// EmitGPRel64Value - Emit the expression @p Value into the output as a
396 /// gprel64 (64-bit GP relative) value.
398 /// This is used to implement assembler directives such as .gpdword on
399 /// targets that support them.
400 virtual void EmitGPRel64Value(const MCExpr *Value);
402 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
403 /// gprel32 (32-bit GP relative) value.
405 /// This is used to implement assembler directives such as .gprel32 on
406 /// targets that support them.
407 virtual void EmitGPRel32Value(const MCExpr *Value);
409 /// EmitFill - Emit NumBytes bytes worth of the value specified by
410 /// FillValue. This implements directives such as '.space'.
411 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
414 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
415 /// function that just wraps EmitFill.
416 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
417 EmitFill(NumBytes, 0, AddrSpace);
421 /// EmitValueToAlignment - Emit some number of copies of @p Value until
422 /// the byte alignment @p ByteAlignment is reached.
424 /// If the number of bytes need to emit for the alignment is not a multiple
425 /// of @p ValueSize, then the contents of the emitted fill bytes is
428 /// This used to implement the .align assembler directive.
430 /// @param ByteAlignment - The alignment to reach. This must be a power of
431 /// two on some targets.
432 /// @param Value - The value to use when filling bytes.
433 /// @param ValueSize - The size of the integer (in bytes) to emit for
434 /// @p Value. This must match a native machine width.
435 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
436 /// the alignment cannot be reached in this many bytes, no bytes are
438 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
439 unsigned ValueSize = 1,
440 unsigned MaxBytesToEmit = 0) = 0;
442 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
445 /// This used to align code where the alignment bytes may be executed. This
446 /// can emit different bytes for different sizes to optimize execution.
448 /// @param ByteAlignment - The alignment to reach. This must be a power of
449 /// two on some targets.
450 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
451 /// the alignment cannot be reached in this many bytes, no bytes are
453 virtual void EmitCodeAlignment(unsigned ByteAlignment,
454 unsigned MaxBytesToEmit = 0) = 0;
456 /// EmitValueToOffset - Emit some number of copies of @p Value until the
457 /// byte offset @p Offset is reached.
459 /// This is used to implement assembler directives such as .org.
461 /// @param Offset - The offset to reach. This may be an expression, but the
462 /// expression must be associated with the current section.
463 /// @param Value - The value to use when filling bytes.
464 /// @return false on success, true if the offset was invalid.
465 virtual bool EmitValueToOffset(const MCExpr *Offset,
466 unsigned char Value = 0) = 0;
470 /// EmitFileDirective - Switch to a new logical file. This is used to
471 /// implement the '.file "foo.c"' assembler directive.
472 virtual void EmitFileDirective(StringRef Filename) = 0;
474 /// EmitDwarfFileDirective - Associate a filename with a specified logical
475 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
477 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
480 /// EmitDwarfLocDirective - This implements the DWARF2
481 // '.loc fileno lineno ...' assembler directive.
482 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
483 unsigned Column, unsigned Flags,
485 unsigned Discriminator,
488 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
489 const MCSymbol *LastLabel,
490 const MCSymbol *Label,
491 unsigned PointerSize) = 0;
493 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
494 const MCSymbol *Label) {
497 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
500 virtual void EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding);
501 virtual void EmitCFISections(bool EH, bool Debug);
502 void EmitCFIStartProc();
503 void EmitCFIEndProc();
504 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
505 virtual void EmitCFIDefCfaOffset(int64_t Offset);
506 virtual void EmitCFIDefCfaRegister(int64_t Register);
507 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
508 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
509 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
510 virtual void EmitCFIRememberState();
511 virtual void EmitCFIRestoreState();
512 virtual void EmitCFISameValue(int64_t Register);
513 virtual void EmitCFIRestore(int64_t Register);
514 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
515 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
516 virtual void EmitCFIEscape(StringRef Values);
517 virtual void EmitCFISignalFrame();
519 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
520 virtual void EmitWin64EHEndProc();
521 virtual void EmitWin64EHStartChained();
522 virtual void EmitWin64EHEndChained();
523 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
525 virtual void EmitWin64EHHandlerData();
526 virtual void EmitWin64EHPushReg(unsigned Register);
527 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
528 virtual void EmitWin64EHAllocStack(unsigned Size);
529 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
530 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
531 virtual void EmitWin64EHPushFrame(bool Code);
532 virtual void EmitWin64EHEndProlog();
534 /// EmitInstruction - Emit the given @p Instruction into the current
536 virtual void EmitInstruction(const MCInst &Inst) = 0;
538 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
539 /// the specified string in the output .s file. This capability is
540 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
541 virtual void EmitRawText(StringRef String);
542 void EmitRawText(const Twine &String);
544 /// ARM-related methods.
545 /// FIXME: Eventually we should have some "target MC streamer" and move
546 /// these methods there.
547 virtual void EmitFnStart();
548 virtual void EmitFnEnd();
549 virtual void EmitCantUnwind();
550 virtual void EmitPersonality(const MCSymbol *Personality);
551 virtual void EmitHandlerData();
552 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
553 virtual void EmitPad(int64_t Offset);
554 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
557 /// FinishImpl - Streamer specific finalization.
558 virtual void FinishImpl() = 0;
559 /// Finish - Finish emission of machine code.
563 /// createNullStreamer - Create a dummy machine code streamer, which does
564 /// nothing. This is useful for timing the assembler front end.
565 MCStreamer *createNullStreamer(MCContext &Ctx);
567 /// createAsmStreamer - Create a machine code streamer which will print out
568 /// assembly for the native target, suitable for compiling with a native
571 /// \param InstPrint - If given, the instruction printer to use. If not given
572 /// the MCInst representation will be printed. This method takes ownership of
575 /// \param CE - If given, a code emitter to use to show the instruction
576 /// encoding inline with the assembly. This method takes ownership of \p CE.
578 /// \param TAB - If given, a target asm backend to use to show the fixup
579 /// information in conjunction with encoding information. This method takes
580 /// ownership of \p TAB.
582 /// \param ShowInst - Whether to show the MCInst representation inline with
584 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
588 bool useDwarfDirectory,
589 MCInstPrinter *InstPrint = 0,
590 MCCodeEmitter *CE = 0,
591 MCAsmBackend *TAB = 0,
592 bool ShowInst = false);
594 /// createMachOStreamer - Create a machine code streamer which will generate
595 /// Mach-O format object files.
597 /// Takes ownership of \p TAB and \p CE.
598 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
599 raw_ostream &OS, MCCodeEmitter *CE,
600 bool RelaxAll = false);
602 /// createWinCOFFStreamer - Create a machine code streamer which will
603 /// generate Microsoft COFF format object files.
605 /// Takes ownership of \p TAB and \p CE.
606 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
608 MCCodeEmitter &CE, raw_ostream &OS,
609 bool RelaxAll = false);
611 /// createELFStreamer - Create a machine code streamer which will generate
612 /// ELF format object files.
613 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
614 raw_ostream &OS, MCCodeEmitter *CE,
615 bool RelaxAll, bool NoExecStack);
617 /// createPureStreamer - Create a machine code streamer which will generate
618 /// "pure" MC object files, for use with MC-JIT and testing tools.
620 /// Takes ownership of \p TAB and \p CE.
621 MCStreamer *createPureStreamer(MCContext &Ctx, MCAsmBackend &TAB,
622 raw_ostream &OS, MCCodeEmitter *CE);
624 } // end namespace llvm