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 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
309 /// EmitZerofill - Emit the zerofill section and an optional symbol.
311 /// @param Section - The zerofill section to create and or to put the symbol
312 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
313 /// @param Size - The size of the zerofill symbol.
314 /// @param ByteAlignment - The alignment of the zerofill symbol if
315 /// non-zero. This must be a power of 2 on some targets.
316 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
317 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
319 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
321 /// @param Section - The thread local common section.
322 /// @param Symbol - The thread local common symbol to emit.
323 /// @param Size - The size of the symbol.
324 /// @param ByteAlignment - The alignment of the thread local common symbol
325 /// if non-zero. This must be a power of 2 on some targets.
326 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
327 uint64_t Size, unsigned ByteAlignment = 0) = 0;
330 /// @name Generating Data
333 /// EmitBytes - Emit the bytes in \arg Data into the output.
335 /// This is used to implement assembler directives such as .byte, .ascii,
337 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
339 /// EmitValue - Emit the expression @p Value into the output as a native
340 /// integer of the given @p Size bytes.
342 /// This is used to implement assembler directives such as .word, .quad,
345 /// @param Value - The value to emit.
346 /// @param Size - The size of the integer (in bytes) to emit. This must
347 /// match a native machine width.
348 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
349 unsigned AddrSpace) = 0;
351 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
353 /// EmitIntValue - Special case of EmitValue that avoids the client having
354 /// to pass in a MCExpr for constant integers.
355 virtual void EmitIntValue(uint64_t Value, unsigned Size,
356 unsigned AddrSpace = 0);
358 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
359 /// this is done by producing
362 void EmitAbsValue(const MCExpr *Value, unsigned Size,
363 unsigned AddrSpace = 0);
365 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
367 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
369 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
370 /// client having to pass in a MCExpr for constant integers.
371 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
373 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
374 /// client having to pass in a MCExpr for constant integers.
375 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
377 /// EmitSymbolValue - Special case of EmitValue that avoids the client
378 /// having to pass in a MCExpr for MCSymbols.
379 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
380 unsigned AddrSpace = 0);
382 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
383 /// gprel32 (32-bit GP relative) value.
385 /// This is used to implement assembler directives such as .gprel32 on
386 /// targets that support them.
387 virtual void EmitGPRel32Value(const MCExpr *Value);
389 /// EmitFill - Emit NumBytes bytes worth of the value specified by
390 /// FillValue. This implements directives such as '.space'.
391 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
394 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
395 /// function that just wraps EmitFill.
396 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
397 EmitFill(NumBytes, 0, AddrSpace);
401 /// EmitValueToAlignment - Emit some number of copies of @p Value until
402 /// the byte alignment @p ByteAlignment is reached.
404 /// If the number of bytes need to emit for the alignment is not a multiple
405 /// of @p ValueSize, then the contents of the emitted fill bytes is
408 /// This used to implement the .align assembler directive.
410 /// @param ByteAlignment - The alignment to reach. This must be a power of
411 /// two on some targets.
412 /// @param Value - The value to use when filling bytes.
413 /// @param ValueSize - The size of the integer (in bytes) to emit for
414 /// @p Value. This must match a native machine width.
415 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
416 /// the alignment cannot be reached in this many bytes, no bytes are
418 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
419 unsigned ValueSize = 1,
420 unsigned MaxBytesToEmit = 0) = 0;
422 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
425 /// This used to align code where the alignment bytes may be executed. This
426 /// can emit different bytes for different sizes to optimize execution.
428 /// @param ByteAlignment - The alignment to reach. This must be a power of
429 /// two on some targets.
430 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
431 /// the alignment cannot be reached in this many bytes, no bytes are
433 virtual void EmitCodeAlignment(unsigned ByteAlignment,
434 unsigned MaxBytesToEmit = 0) = 0;
436 /// EmitValueToOffset - Emit some number of copies of @p Value until the
437 /// byte offset @p Offset is reached.
439 /// This is used to implement assembler directives such as .org.
441 /// @param Offset - The offset to reach. This may be an expression, but the
442 /// expression must be associated with the current section.
443 /// @param Value - The value to use when filling bytes.
444 virtual void EmitValueToOffset(const MCExpr *Offset,
445 unsigned char Value = 0) = 0;
449 /// EmitFileDirective - Switch to a new logical file. This is used to
450 /// implement the '.file "foo.c"' assembler directive.
451 virtual void EmitFileDirective(StringRef Filename) = 0;
453 /// EmitDwarfFileDirective - Associate a filename with a specified logical
454 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
456 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
458 /// EmitDwarfLocDirective - This implements the DWARF2
459 // '.loc fileno lineno ...' assembler directive.
460 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
461 unsigned Column, unsigned Flags,
463 unsigned Discriminator,
466 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
467 const MCSymbol *LastLabel,
468 const MCSymbol *Label,
469 unsigned PointerSize) = 0;
471 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
472 const MCSymbol *Label) {
475 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
478 virtual void EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding);
479 virtual void EmitCFISections(bool EH, bool Debug);
480 virtual void EmitCFIStartProc();
481 virtual void EmitCFIEndProc();
482 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
483 virtual void EmitCFIDefCfaOffset(int64_t Offset);
484 virtual void EmitCFIDefCfaRegister(int64_t Register);
485 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
486 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
487 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
488 virtual void EmitCFIRememberState();
489 virtual void EmitCFIRestoreState();
490 virtual void EmitCFISameValue(int64_t Register);
491 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
492 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
494 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
495 virtual void EmitWin64EHEndProc();
496 virtual void EmitWin64EHStartChained();
497 virtual void EmitWin64EHEndChained();
498 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
500 virtual void EmitWin64EHHandlerData();
501 virtual void EmitWin64EHPushReg(unsigned Register);
502 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
503 virtual void EmitWin64EHAllocStack(unsigned Size);
504 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
505 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
506 virtual void EmitWin64EHPushFrame(bool Code);
507 virtual void EmitWin64EHEndProlog();
509 /// EmitInstruction - Emit the given @p Instruction into the current
511 virtual void EmitInstruction(const MCInst &Inst) = 0;
513 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
514 /// the specified string in the output .s file. This capability is
515 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
516 virtual void EmitRawText(StringRef String);
517 void EmitRawText(const Twine &String);
519 /// ARM-related methods.
520 /// FIXME: Eventually we should have some "target MC streamer" and move
521 /// these methods there.
522 virtual void EmitFnStart();
523 virtual void EmitFnEnd();
524 virtual void EmitCantUnwind();
525 virtual void EmitPersonality(const MCSymbol *Personality);
526 virtual void EmitHandlerData();
527 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
528 virtual void EmitPad(int64_t Offset);
529 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
532 /// Finish - Finish emission of machine code.
533 virtual void Finish() = 0;
536 /// createNullStreamer - Create a dummy machine code streamer, which does
537 /// nothing. This is useful for timing the assembler front end.
538 MCStreamer *createNullStreamer(MCContext &Ctx);
540 /// createAsmStreamer - Create a machine code streamer which will print out
541 /// assembly for the native target, suitable for compiling with a native
544 /// \param InstPrint - If given, the instruction printer to use. If not given
545 /// the MCInst representation will be printed. This method takes ownership of
548 /// \param CE - If given, a code emitter to use to show the instruction
549 /// encoding inline with the assembly. This method takes ownership of \arg CE.
551 /// \param TAB - If given, a target asm backend to use to show the fixup
552 /// information in conjunction with encoding information. This method takes
553 /// ownership of \arg TAB.
555 /// \param ShowInst - Whether to show the MCInst representation inline with
558 /// \param DecodeLSDA - If true, emit comments that translates the LSDA into a
559 /// human readable format. Only usable with CFI.
560 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
564 MCInstPrinter *InstPrint = 0,
565 MCCodeEmitter *CE = 0,
566 MCAsmBackend *TAB = 0,
567 bool ShowInst = false);
569 /// createMachOStreamer - Create a machine code streamer which will generate
570 /// Mach-O format object files.
572 /// Takes ownership of \arg TAB and \arg CE.
573 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
574 raw_ostream &OS, MCCodeEmitter *CE,
575 bool RelaxAll = false);
577 /// createWinCOFFStreamer - Create a machine code streamer which will
578 /// generate Microsoft COFF format object files.
580 /// Takes ownership of \arg TAB and \arg CE.
581 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
583 MCCodeEmitter &CE, raw_ostream &OS,
584 bool RelaxAll = false);
586 /// createELFStreamer - Create a machine code streamer which will generate
587 /// ELF format object files.
588 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB,
589 raw_ostream &OS, MCCodeEmitter *CE,
590 bool RelaxAll, bool NoExecStack);
592 /// createLoggingStreamer - Create a machine code streamer which just logs the
593 /// API calls and then dispatches to another streamer.
595 /// The new streamer takes ownership of the \arg Child.
596 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
598 /// createPureStreamer - Create a machine code streamer which will generate
599 /// "pure" MC object files, for use with MC-JIT and testing tools.
601 /// Takes ownership of \arg TAB and \arg CE.
602 MCStreamer *createPureStreamer(MCContext &Ctx, MCAsmBackend &TAB,
603 raw_ostream &OS, MCCodeEmitter *CE);
605 } // end namespace llvm