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/SmallVector.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCWin64EH.h"
33 class TargetAsmBackend;
34 class TargetLoweringObjectFile;
37 class formatted_raw_ostream;
39 /// MCStreamer - Streaming machine code generation interface. This interface
40 /// is intended to provide a programatic interface that is very similar to the
41 /// level that an assembler .s file provides. It has callbacks to emit bytes,
42 /// handle directives, etc. The implementation of this interface retains
43 /// state to know what the current section is etc.
45 /// There are multiple implementations of this interface: one for writing out
46 /// a .s file, and implementations that write out .o files of various formats.
51 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
52 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
57 std::vector<MCDwarfFrameInfo> FrameInfos;
58 MCDwarfFrameInfo *getCurrentFrameInfo();
59 void EnsureValidFrame();
61 std::vector<MCWin64EHUnwindInfo> W64UnwindInfos;
62 MCWin64EHUnwindInfo *CurrentW64UnwindInfo;
63 void setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame);
64 void EnsureValidW64UnwindInfo();
66 const MCSymbol* LastNonPrivate;
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 EmitFrames(bool usingCFI);
83 MCWin64EHUnwindInfo *getCurrentW64UnwindInfo(){return CurrentW64UnwindInfo;}
87 virtual ~MCStreamer();
89 MCContext &getContext() const { return Context; }
91 unsigned getNumFrameInfos() {
92 return FrameInfos.size();
95 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
99 unsigned getNumW64UnwindInfos() {
100 return W64UnwindInfos.size();
103 MCWin64EHUnwindInfo &getW64UnwindInfo(unsigned i) {
104 return W64UnwindInfos[i];
107 /// @name Assembly File Formatting.
110 /// isVerboseAsm - Return true if this streamer supports verbose assembly
111 /// and if it is enabled.
112 virtual bool isVerboseAsm() const { return false; }
114 /// hasRawTextSupport - Return true if this asm streamer supports emitting
115 /// unformatted text to the .s file with EmitRawText.
116 virtual bool hasRawTextSupport() const { return false; }
118 /// AddComment - Add a comment that can be emitted to the generated .s
119 /// file if applicable as a QoI issue to make the output of the compiler
120 /// more readable. This only affects the MCAsmStreamer, and only when
121 /// verbose assembly output is enabled.
123 /// If the comment includes embedded \n's, they will each get the comment
124 /// prefix as appropriate. The added comment should not end with a \n.
125 virtual void AddComment(const Twine &T) {}
127 /// GetCommentOS - Return a raw_ostream that comments can be written to.
128 /// Unlike AddComment, you are required to terminate comments with \n if you
130 virtual raw_ostream &GetCommentOS();
132 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
133 virtual void AddBlankLine() {}
137 /// @name Symbol & Section Management
140 /// getCurrentSection - Return the current section that the streamer is
141 /// emitting code to.
142 const MCSection *getCurrentSection() const {
143 if (!SectionStack.empty())
144 return SectionStack.back().first;
148 /// getPreviousSection - Return the previous section that the streamer is
149 /// emitting code to.
150 const MCSection *getPreviousSection() const {
151 if (!SectionStack.empty())
152 return SectionStack.back().second;
156 /// ChangeSection - Update streamer for a new active section.
158 /// This is called by PopSection and SwitchSection, if the current
160 virtual void ChangeSection(const MCSection *) = 0;
162 /// pushSection - Save the current and previous section on the
165 SectionStack.push_back(std::make_pair(getCurrentSection(),
166 getPreviousSection()));
169 /// popSection - Restore the current and previous section from
170 /// the section stack. Calls ChangeSection as needed.
172 /// Returns false if the stack was empty.
174 if (SectionStack.size() <= 1)
176 const MCSection *oldSection = SectionStack.pop_back_val().first;
177 const MCSection *curSection = SectionStack.back().first;
179 if (oldSection != curSection)
180 ChangeSection(curSection);
184 /// SwitchSection - Set the current section where code is being emitted to
185 /// @p Section. This is required to update CurSection.
187 /// This corresponds to assembler directives like .section, .text, etc.
188 void SwitchSection(const MCSection *Section) {
189 assert(Section && "Cannot switch to a null section!");
190 const MCSection *curSection = SectionStack.back().first;
191 SectionStack.back().second = curSection;
192 if (Section != curSection) {
193 SectionStack.back().first = Section;
194 ChangeSection(Section);
198 /// InitSections - Create the default sections and set the initial one.
199 virtual void InitSections() = 0;
201 /// EmitLabel - Emit a label for @p Symbol into the current section.
203 /// This corresponds to an assembler statement such as:
206 /// @param Symbol - The symbol to emit. A given symbol should only be
207 /// emitted as a label once, and symbols emitted as a label should never be
208 /// used in an assignment.
209 virtual void EmitLabel(MCSymbol *Symbol);
211 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
214 /// EmitAssemblerFlag - Note in the output the specified @p Flag
215 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
217 /// EmitThumbFunc - Note in the output that the specified @p Func is
218 /// a Thumb mode function (ARM target only).
219 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
221 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
223 /// This corresponds to an assembler statement such as:
226 /// The assignment generates no code, but has the side effect of binding the
227 /// value in the current context. For the assembly streamer, this prints the
228 /// binding into the .s file.
230 /// @param Symbol - The symbol being assigned to.
231 /// @param Value - The value for the symbol.
232 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
234 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
236 /// This corresponds to an assembler statement such as:
237 /// .weakref alias, symbol
239 /// @param Alias - The alias that is being created.
240 /// @param Symbol - The symbol being aliased.
241 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
243 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
244 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
245 MCSymbolAttr Attribute) = 0;
247 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
249 /// @param Symbol - The symbol to have its n_desc field set.
250 /// @param DescValue - The value to set into the n_desc field.
251 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
253 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
255 /// @param Symbol - The symbol to have its External & Type fields set.
256 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
258 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
260 /// @param StorageClass - The storage class the symbol should have.
261 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
263 /// EmitCOFFSymbolType - Emit the type of the symbol.
265 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
266 virtual void EmitCOFFSymbolType(int Type) = 0;
268 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
269 virtual void EndCOFFSymbolDef() = 0;
271 /// EmitELFSize - Emit an ELF .size directive.
273 /// This corresponds to an assembler statement such as:
274 /// .size symbol, expression
276 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
278 /// EmitCommonSymbol - Emit a common symbol.
280 /// @param Symbol - The common symbol to emit.
281 /// @param Size - The size of the common symbol.
282 /// @param ByteAlignment - The alignment of the symbol if
283 /// non-zero. This must be a power of 2.
284 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
285 unsigned ByteAlignment) = 0;
287 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
289 /// @param Symbol - The common symbol to emit.
290 /// @param Size - The size of the common symbol.
291 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
293 /// EmitZerofill - Emit the zerofill section and an optional symbol.
295 /// @param Section - The zerofill section to create and or to put the symbol
296 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
297 /// @param Size - The size of the zerofill symbol.
298 /// @param ByteAlignment - The alignment of the zerofill symbol if
299 /// non-zero. This must be a power of 2 on some targets.
300 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
301 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
303 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
305 /// @param Section - The thread local common section.
306 /// @param Symbol - The thread local common symbol to emit.
307 /// @param Size - The size of the symbol.
308 /// @param ByteAlignment - The alignment of the thread local common symbol
309 /// if non-zero. This must be a power of 2 on some targets.
310 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
311 uint64_t Size, unsigned ByteAlignment = 0) = 0;
314 /// @name Generating Data
317 /// EmitBytes - Emit the bytes in \arg Data into the output.
319 /// This is used to implement assembler directives such as .byte, .ascii,
321 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
323 /// EmitValue - Emit the expression @p Value into the output as a native
324 /// integer of the given @p Size bytes.
326 /// This is used to implement assembler directives such as .word, .quad,
329 /// @param Value - The value to emit.
330 /// @param Size - The size of the integer (in bytes) to emit. This must
331 /// match a native machine width.
332 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
333 unsigned AddrSpace) = 0;
335 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
337 /// EmitIntValue - Special case of EmitValue that avoids the client having
338 /// to pass in a MCExpr for constant integers.
339 virtual void EmitIntValue(uint64_t Value, unsigned Size,
340 unsigned AddrSpace = 0);
342 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
343 /// this is done by producing
346 void EmitAbsValue(const MCExpr *Value, unsigned Size,
347 unsigned AddrSpace = 0);
349 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
351 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
353 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
354 /// client having to pass in a MCExpr for constant integers.
355 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
357 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
358 /// client having to pass in a MCExpr for constant integers.
359 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
361 /// EmitSymbolValue - Special case of EmitValue that avoids the client
362 /// having to pass in a MCExpr for MCSymbols.
363 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
364 unsigned AddrSpace = 0);
366 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
367 /// gprel32 (32-bit GP relative) value.
369 /// This is used to implement assembler directives such as .gprel32 on
370 /// targets that support them.
371 virtual void EmitGPRel32Value(const MCExpr *Value);
373 /// EmitFill - Emit NumBytes bytes worth of the value specified by
374 /// FillValue. This implements directives such as '.space'.
375 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
378 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
379 /// function that just wraps EmitFill.
380 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
381 EmitFill(NumBytes, 0, AddrSpace);
385 /// EmitValueToAlignment - Emit some number of copies of @p Value until
386 /// the byte alignment @p ByteAlignment is reached.
388 /// If the number of bytes need to emit for the alignment is not a multiple
389 /// of @p ValueSize, then the contents of the emitted fill bytes is
392 /// This used to implement the .align assembler directive.
394 /// @param ByteAlignment - The alignment to reach. This must be a power of
395 /// two on some targets.
396 /// @param Value - The value to use when filling bytes.
397 /// @param ValueSize - The size of the integer (in bytes) to emit for
398 /// @p Value. This must match a native machine width.
399 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
400 /// the alignment cannot be reached in this many bytes, no bytes are
402 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
403 unsigned ValueSize = 1,
404 unsigned MaxBytesToEmit = 0) = 0;
406 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
409 /// This used to align code where the alignment bytes may be executed. This
410 /// can emit different bytes for different sizes to optimize execution.
412 /// @param ByteAlignment - The alignment to reach. This must be a power of
413 /// two on some targets.
414 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
415 /// the alignment cannot be reached in this many bytes, no bytes are
417 virtual void EmitCodeAlignment(unsigned ByteAlignment,
418 unsigned MaxBytesToEmit = 0) = 0;
420 /// EmitValueToOffset - Emit some number of copies of @p Value until the
421 /// byte offset @p Offset is reached.
423 /// This is used to implement assembler directives such as .org.
425 /// @param Offset - The offset to reach. This may be an expression, but the
426 /// expression must be associated with the current section.
427 /// @param Value - The value to use when filling bytes.
428 virtual void EmitValueToOffset(const MCExpr *Offset,
429 unsigned char Value = 0) = 0;
433 /// EmitFileDirective - Switch to a new logical file. This is used to
434 /// implement the '.file "foo.c"' assembler directive.
435 virtual void EmitFileDirective(StringRef Filename) = 0;
437 /// EmitDwarfFileDirective - Associate a filename with a specified logical
438 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
440 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
442 /// EmitDwarfLocDirective - This implements the DWARF2
443 // '.loc fileno lineno ...' assembler directive.
444 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
445 unsigned Column, unsigned Flags,
447 unsigned Discriminator,
450 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
451 const MCSymbol *LastLabel,
452 const MCSymbol *Label) = 0;
454 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
455 const MCSymbol *Label) {
458 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
461 virtual void EmitCFISections(bool EH, bool Debug);
462 virtual void EmitCFIStartProc();
463 virtual void EmitCFIEndProc();
464 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
465 virtual void EmitCFIDefCfaOffset(int64_t Offset);
466 virtual void EmitCFIDefCfaRegister(int64_t Register);
467 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
468 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
469 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
470 virtual void EmitCFIRememberState();
471 virtual void EmitCFIRestoreState();
472 virtual void EmitCFISameValue(int64_t Register);
473 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
474 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
476 virtual void EmitWin64EHStartProc(const MCSymbol *Symbol);
477 virtual void EmitWin64EHEndProc();
478 virtual void EmitWin64EHStartChained();
479 virtual void EmitWin64EHEndChained();
480 virtual void EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
482 virtual void EmitWin64EHHandlerData();
483 virtual void EmitWin64EHPushReg(unsigned Register);
484 virtual void EmitWin64EHSetFrame(unsigned Register, unsigned Offset);
485 virtual void EmitWin64EHAllocStack(unsigned Size);
486 virtual void EmitWin64EHSaveReg(unsigned Register, unsigned Offset);
487 virtual void EmitWin64EHSaveXMM(unsigned Register, unsigned Offset);
488 virtual void EmitWin64EHPushFrame(bool Code);
489 virtual void EmitWin64EHEndProlog();
491 /// EmitInstruction - Emit the given @p Instruction into the current
493 virtual void EmitInstruction(const MCInst &Inst) = 0;
495 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
496 /// the specified string in the output .s file. This capability is
497 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
498 virtual void EmitRawText(StringRef String);
499 void EmitRawText(const Twine &String);
501 /// ARM-related methods.
502 /// FIXME: Eventually we should have some "target MC streamer" and move
503 /// these methods there.
504 virtual void EmitFnStart();
505 virtual void EmitFnEnd();
506 virtual void EmitCantUnwind();
507 virtual void EmitPersonality(const MCSymbol *Personality);
508 virtual void EmitHandlerData();
509 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
510 virtual void EmitPad(int64_t Offset);
511 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
514 /// Finish - Finish emission of machine code.
515 virtual void Finish() = 0;
518 /// createNullStreamer - Create a dummy machine code streamer, which does
519 /// nothing. This is useful for timing the assembler front end.
520 MCStreamer *createNullStreamer(MCContext &Ctx);
522 /// createAsmStreamer - Create a machine code streamer which will print out
523 /// assembly for the native target, suitable for compiling with a native
526 /// \param InstPrint - If given, the instruction printer to use. If not given
527 /// the MCInst representation will be printed. This method takes ownership of
530 /// \param CE - If given, a code emitter to use to show the instruction
531 /// encoding inline with the assembly. This method takes ownership of \arg CE.
533 /// \param TAB - If given, a target asm backend to use to show the fixup
534 /// information in conjunction with encoding information. This method takes
535 /// ownership of \arg TAB.
537 /// \param ShowInst - Whether to show the MCInst representation inline with
539 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
543 MCInstPrinter *InstPrint = 0,
544 MCCodeEmitter *CE = 0,
545 TargetAsmBackend *TAB = 0,
546 bool ShowInst = false);
548 /// createMachOStreamer - Create a machine code streamer which will generate
549 /// Mach-O format object files.
551 /// Takes ownership of \arg TAB and \arg CE.
552 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
553 raw_ostream &OS, MCCodeEmitter *CE,
554 bool RelaxAll = false);
556 /// createWinCOFFStreamer - Create a machine code streamer which will
557 /// generate Microsoft COFF format object files.
559 /// Takes ownership of \arg TAB and \arg CE.
560 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
561 TargetAsmBackend &TAB,
562 MCCodeEmitter &CE, raw_ostream &OS,
563 bool RelaxAll = false);
565 /// createELFStreamer - Create a machine code streamer which will generate
566 /// ELF format object files.
567 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
568 raw_ostream &OS, MCCodeEmitter *CE,
569 bool RelaxAll, bool NoExecStack);
571 /// createLoggingStreamer - Create a machine code streamer which just logs the
572 /// API calls and then dispatches to another streamer.
574 /// The new streamer takes ownership of the \arg Child.
575 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
577 /// createPureStreamer - Create a machine code streamer which will generate
578 /// "pure" MC object files, for use with MC-JIT and testing tools.
580 /// Takes ownership of \arg TAB and \arg CE.
581 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
582 raw_ostream &OS, MCCodeEmitter *CE);
584 } // end namespace llvm