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"
35 class TargetLoweringObjectFile;
38 class formatted_raw_ostream;
40 /// MCStreamer - Streaming machine code generation interface. This interface
41 /// is intended to provide a programatic interface that is very similar to the
42 /// level that an assembler .s file provides. It has callbacks to emit bytes,
43 /// handle directives, etc. The implementation of this interface retains
44 /// state to know what the current section is etc.
46 /// There are multiple implementations of this interface: one for writing out
47 /// a .s file, and implementations that write out .o files of various formats.
52 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
53 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
58 std::vector<MCDwarfFrameInfo> FrameInfos;
59 MCDwarfFrameInfo *getCurrentFrameInfo();
60 void EnsureValidFrame();
62 std::vector<MCWin64EHUnwindInfo *> W64UnwindInfos;
63 MCWin64EHUnwindInfo *CurrentW64UnwindInfo;
64 void setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame);
65 void EnsureValidW64UnwindInfo();
69 /// SectionStack - This is stack of current and previous section
70 /// values saved by PushSection.
71 SmallVector<std::pair<const MCSection *,
72 const MCSection *>, 4> SectionStack;
75 MCStreamer(MCContext &Ctx);
77 const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A,
80 const MCExpr *ForceExpAbs(const MCExpr* Expr);
82 void EmitFrames(bool usingCFI);
84 MCWin64EHUnwindInfo *getCurrentW64UnwindInfo(){return CurrentW64UnwindInfo;}
88 virtual ~MCStreamer();
90 MCContext &getContext() const { return Context; }
92 unsigned getNumFrameInfos() {
93 return FrameInfos.size();
96 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
100 ArrayRef<MCDwarfFrameInfo> getFrameInfos() {
104 unsigned getNumW64UnwindInfos() {
105 return W64UnwindInfos.size();
108 MCWin64EHUnwindInfo &getW64UnwindInfo(unsigned i) {
109 return *W64UnwindInfos[i];
112 /// @name Assembly File Formatting.
115 /// isVerboseAsm - Return true if this streamer supports verbose assembly
116 /// and if it is enabled.
117 virtual bool isVerboseAsm() const { return false; }
119 /// hasRawTextSupport - Return true if this asm streamer supports emitting
120 /// unformatted text to the .s file with EmitRawText.
121 virtual bool hasRawTextSupport() const { return false; }
123 /// AddComment - Add a comment that can be emitted to the generated .s
124 /// file if applicable as a QoI issue to make the output of the compiler
125 /// more readable. This only affects the MCAsmStreamer, and only when
126 /// verbose assembly output is enabled.
128 /// If the comment includes embedded \n's, they will each get the comment
129 /// prefix as appropriate. The added comment should not end with a \n.
130 virtual void AddComment(const Twine &T) {}
132 /// GetCommentOS - Return a raw_ostream that comments can be written to.
133 /// Unlike AddComment, you are required to terminate comments with \n if you
135 virtual raw_ostream &GetCommentOS();
137 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
138 virtual void AddBlankLine() {}
142 /// @name Symbol & Section Management
145 /// getCurrentSection - Return the current section that the streamer is
146 /// emitting code to.
147 const MCSection *getCurrentSection() const {
148 if (!SectionStack.empty())
149 return SectionStack.back().first;
153 /// getPreviousSection - Return the previous section that the streamer is
154 /// emitting code to.
155 const MCSection *getPreviousSection() const {
156 if (!SectionStack.empty())
157 return SectionStack.back().second;
161 /// ChangeSection - Update streamer for a new active section.
163 /// This is called by PopSection and SwitchSection, if the current
165 virtual void ChangeSection(const MCSection *) = 0;
167 /// pushSection - Save the current and previous section on the
170 SectionStack.push_back(std::make_pair(getCurrentSection(),
171 getPreviousSection()));
174 /// popSection - Restore the current and previous section from
175 /// the section stack. Calls ChangeSection as needed.
177 /// Returns false if the stack was empty.
179 if (SectionStack.size() <= 1)
181 const MCSection *oldSection = SectionStack.pop_back_val().first;
182 const MCSection *curSection = SectionStack.back().first;
184 if (oldSection != curSection)
185 ChangeSection(curSection);
189 /// SwitchSection - Set the current section where code is being emitted to
190 /// @p Section. This is required to update CurSection.
192 /// This corresponds to assembler directives like .section, .text, etc.
193 void SwitchSection(const MCSection *Section) {
194 assert(Section && "Cannot switch to a null section!");
195 const MCSection *curSection = SectionStack.back().first;
196 SectionStack.back().second = curSection;
197 if (Section != curSection) {
198 SectionStack.back().first = Section;
199 ChangeSection(Section);
203 /// SwitchSectionNoChange - Set the current section where code is being
204 /// emitted to @p Section. This is required to update CurSection. This
205 /// version does not call ChangeSection.
206 void SwitchSectionNoChange(const MCSection *Section) {
207 assert(Section && "Cannot switch to a null section!");
208 const MCSection *curSection = SectionStack.back().first;
209 SectionStack.back().second = curSection;
210 if (Section != curSection)
211 SectionStack.back().first = Section;
214 /// InitSections - Create the default sections and set the initial one.
215 virtual void InitSections() = 0;
217 /// EmitLabel - Emit a label for @p Symbol into the current section.
219 /// This corresponds to an assembler statement such as:
222 /// @param Symbol - The symbol to emit. A given symbol should only be
223 /// emitted as a label once, and symbols emitted as a label should never be
224 /// used in an assignment.
225 virtual void EmitLabel(MCSymbol *Symbol);
227 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
230 /// EmitAssemblerFlag - Note in the output the specified @p Flag
231 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
233 /// EmitThumbFunc - Note in the output that the specified @p Func is
234 /// a Thumb mode function (ARM target only).
235 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
237 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
239 /// This corresponds to an assembler statement such as:
242 /// The assignment generates no code, but has the side effect of binding the
243 /// value in the current context. For the assembly streamer, this prints the
244 /// binding into the .s file.
246 /// @param Symbol - The symbol being assigned to.
247 /// @param Value - The value for the symbol.
248 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
250 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
252 /// This corresponds to an assembler statement such as:
253 /// .weakref alias, symbol
255 /// @param Alias - The alias that is being created.
256 /// @param Symbol - The symbol being aliased.
257 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
259 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
260 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
261 MCSymbolAttr Attribute) = 0;
263 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
265 /// @param Symbol - The symbol to have its n_desc field set.
266 /// @param DescValue - The value to set into the n_desc field.
267 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
269 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
271 /// @param Symbol - The symbol to have its External & Type fields set.
272 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
274 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
276 /// @param StorageClass - The storage class the symbol should have.
277 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
279 /// EmitCOFFSymbolType - Emit the type of the symbol.
281 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
282 virtual void EmitCOFFSymbolType(int Type) = 0;
284 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
285 virtual void EndCOFFSymbolDef() = 0;
287 /// EmitELFSize - Emit an ELF .size directive.
289 /// This corresponds to an assembler statement such as:
290 /// .size symbol, expression
292 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
294 /// EmitCommonSymbol - Emit a common symbol.
296 /// @param Symbol - The common symbol to emit.
297 /// @param Size - The size of the common symbol.
298 /// @param ByteAlignment - The alignment of the symbol if
299 /// non-zero. This must be a power of 2.
300 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
301 unsigned ByteAlignment) = 0;
303 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
305 /// @param Symbol - The common symbol to emit.
306 /// @param Size - The size of the common symbol.
307 /// @param ByteAlignment - The alignment of the common symbol in bytes.
308 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
309 unsigned ByteAlignment) = 0;
311 /// EmitZerofill - Emit the zerofill section and an optional symbol.
313 /// @param Section - The zerofill section to create and or to put the symbol
314 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
315 /// @param Size - The size of the zerofill symbol.
316 /// @param ByteAlignment - The alignment of the zerofill symbol if
317 /// non-zero. This must be a power of 2 on some targets.
318 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
319 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
321 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
323 /// @param Section - The thread local common section.
324 /// @param Symbol - The thread local common symbol to emit.
325 /// @param Size - The size of the symbol.
326 /// @param ByteAlignment - The alignment of the thread local common symbol
327 /// if non-zero. This must be a power of 2 on some targets.
328 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
329 uint64_t Size, unsigned ByteAlignment = 0) = 0;
332 /// @name Generating Data
335 /// EmitBytes - Emit the bytes in \arg Data into the output.
337 /// This is used to implement assembler directives such as .byte, .ascii,
339 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
341 /// EmitValue - Emit the expression @p Value into the output as a native
342 /// integer of the given @p Size bytes.
344 /// This is used to implement assembler directives such as .word, .quad,
347 /// @param Value - The value to emit.
348 /// @param Size - The size of the integer (in bytes) to emit. This must
349 /// match a native machine width.
350 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
351 unsigned AddrSpace) = 0;
353 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
355 /// EmitIntValue - Special case of EmitValue that avoids the client having
356 /// to pass in a MCExpr for constant integers.
357 virtual void EmitIntValue(uint64_t Value, unsigned Size,
358 unsigned AddrSpace = 0);
360 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
361 /// this is done by producing
364 void EmitAbsValue(const MCExpr *Value, unsigned Size,
365 unsigned AddrSpace = 0);
367 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
369 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
371 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
372 /// client having to pass in a MCExpr for constant integers.
373 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
375 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
376 /// client having to pass in a MCExpr for constant integers.
377 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
379 /// EmitSymbolValue - Special case of EmitValue that avoids the client
380 /// having to pass in a MCExpr for MCSymbols.
381 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
382 unsigned AddrSpace = 0);
384 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
385 /// gprel32 (32-bit GP relative) value.
387 /// This is used to implement assembler directives such as .gprel32 on
388 /// targets that support them.
389 virtual void EmitGPRel32Value(const MCExpr *Value);
391 /// EmitFill - Emit NumBytes bytes worth of the value specified by
392 /// FillValue. This implements directives such as '.space'.
393 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
396 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
397 /// function that just wraps EmitFill.
398 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
399 EmitFill(NumBytes, 0, AddrSpace);
403 /// EmitValueToAlignment - Emit some number of copies of @p Value until
404 /// the byte alignment @p ByteAlignment is reached.
406 /// If the number of bytes need to emit for the alignment is not a multiple
407 /// of @p ValueSize, then the contents of the emitted fill bytes is
410 /// This used to implement the .align assembler directive.
412 /// @param ByteAlignment - The alignment to reach. This must be a power of
413 /// two on some targets.
414 /// @param Value - The value to use when filling bytes.
415 /// @param ValueSize - The size of the integer (in bytes) to emit for
416 /// @p Value. This must match a native machine width.
417 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
418 /// the alignment cannot be reached in this many bytes, no bytes are
420 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
421 unsigned ValueSize = 1,
422 unsigned MaxBytesToEmit = 0) = 0;
424 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
427 /// This used to align code where the alignment bytes may be executed. This
428 /// can emit different bytes for different sizes to optimize execution.
430 /// @param ByteAlignment - The alignment to reach. This must be a power of
431 /// two on some targets.
432 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
433 /// the alignment cannot be reached in this many bytes, no bytes are
435 virtual void EmitCodeAlignment(unsigned ByteAlignment,
436 unsigned MaxBytesToEmit = 0) = 0;
438 /// EmitValueToOffset - Emit some number of copies of @p Value until the
439 /// byte offset @p Offset is reached.
441 /// This is used to implement assembler directives such as .org.
443 /// @param Offset - The offset to reach. This may be an expression, but the
444 /// expression must be associated with the current section.
445 /// @param Value - The value to use when filling bytes.
446 virtual void EmitValueToOffset(const MCExpr *Offset,
447 unsigned char Value = 0) = 0;
451 /// EmitFileDirective - Switch to a new logical file. This is used to
452 /// implement the '.file "foo.c"' assembler directive.
453 virtual void EmitFileDirective(StringRef Filename) = 0;
455 /// EmitDwarfFileDirective - Associate a filename with a specified logical
456 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
458 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
460 /// EmitDwarfLocDirective - This implements the DWARF2
461 // '.loc fileno lineno ...' assembler directive.
462 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
463 unsigned Column, unsigned Flags,
465 unsigned Discriminator,
468 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
469 const MCSymbol *LastLabel,
470 const MCSymbol *Label,
471 unsigned PointerSize) = 0;
473 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
474 const MCSymbol *Label) {
477 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
480 virtual void EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding);
481 virtual void EmitCFISections(bool EH, bool Debug);
482 virtual void EmitCFIStartProc();
483 virtual void EmitCFIEndProc();
484 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
485 virtual void EmitCFIDefCfaOffset(int64_t Offset);
486 virtual void EmitCFIDefCfaRegister(int64_t Register);
487 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
488 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
489 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
490 virtual void EmitCFIRememberState();
491 virtual void EmitCFIRestoreState();
492 virtual void EmitCFISameValue(int64_t Register);
493 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
494 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
496 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
497 virtual void EmitWin64EHEndProc();
498 virtual void EmitWin64EHStartChained();
499 virtual void EmitWin64EHEndChained();
500 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
502 virtual void EmitWin64EHHandlerData();
503 virtual void EmitWin64EHPushReg(unsigned Register);
504 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
505 virtual void EmitWin64EHAllocStack(unsigned Size);
506 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
507 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
508 virtual void EmitWin64EHPushFrame(bool Code);
509 virtual void EmitWin64EHEndProlog();
511 /// EmitInstruction - Emit the given @p Instruction into the current
513 virtual void EmitInstruction(const MCInst &Inst) = 0;
515 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
516 /// the specified string in the output .s file. This capability is
517 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
518 virtual void EmitRawText(StringRef String);
519 void EmitRawText(const Twine &String);
521 /// ARM-related methods.
522 /// FIXME: Eventually we should have some "target MC streamer" and move
523 /// these methods there.
524 virtual void EmitFnStart();
525 virtual void EmitFnEnd();
526 virtual void EmitCantUnwind();
527 virtual void EmitPersonality(const MCSymbol *Personality);
528 virtual void EmitHandlerData();
529 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
530 virtual void EmitPad(int64_t Offset);
531 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
534 /// Finish - Finish emission of machine code.
535 virtual void Finish() = 0;
538 /// createNullStreamer - Create a dummy machine code streamer, which does
539 /// nothing. This is useful for timing the assembler front end.
540 MCStreamer *createNullStreamer(MCContext &Ctx);
542 /// createAsmStreamer - Create a machine code streamer which will print out
543 /// assembly for the native target, suitable for compiling with a native
546 /// \param InstPrint - If given, the instruction printer to use. If not given
547 /// the MCInst representation will be printed. This method takes ownership of
550 /// \param CE - If given, a code emitter to use to show the instruction
551 /// encoding inline with the assembly. This method takes ownership of \arg CE.
553 /// \param TAB - If given, a target asm backend to use to show the fixup
554 /// information in conjunction with encoding information. This method takes
555 /// ownership of \arg TAB.
557 /// \param ShowInst - Whether to show the MCInst representation inline with
560 /// \param DecodeLSDA - If true, emit comments that translates the LSDA into a
561 /// human readable format. Only usable with CFI.
562 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
566 MCInstPrinter *InstPrint = 0,
567 MCCodeEmitter *CE = 0,
568 MCAsmBackend *TAB = 0,
569 bool ShowInst = false);
571 /// createMachOStreamer - Create a machine code streamer which will generate
572 /// Mach-O format object files.
574 /// Takes ownership of \arg TAB and \arg CE.
575 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
576 raw_ostream &OS, MCCodeEmitter *CE,
577 bool RelaxAll = false);
579 /// createWinCOFFStreamer - Create a machine code streamer which will
580 /// generate Microsoft COFF format object files.
582 /// Takes ownership of \arg TAB and \arg CE.
583 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
585 MCCodeEmitter &CE, raw_ostream &OS,
586 bool RelaxAll = false);
588 /// createELFStreamer - Create a machine code streamer which will generate
589 /// ELF format object files.
590 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
591 raw_ostream &OS, MCCodeEmitter *CE,
592 bool RelaxAll, bool NoExecStack);
594 /// createLoggingStreamer - Create a machine code streamer which just logs the
595 /// API calls and then dispatches to another streamer.
597 /// The new streamer takes ownership of the \arg Child.
598 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
600 /// createPureStreamer - Create a machine code streamer which will generate
601 /// "pure" MC object files, for use with MC-JIT and testing tools.
603 /// Takes ownership of \arg TAB and \arg CE.
604 MCStreamer *createPureStreamer(MCContext &Ctx, MCAsmBackend &TAB,
605 raw_ostream &OS, MCCodeEmitter *CE);
607 } // end namespace llvm