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 *getCurrentW64UnwindInfo();
63 void EnsureValidW64UnwindInfo();
65 const MCSymbol* LastNonPrivate;
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(MCStreamer *Streamer, MCContext &Context,
81 void EmitFrames(bool usingCFI);
84 virtual ~MCStreamer();
86 MCContext &getContext() const { return Context; }
88 unsigned getNumFrameInfos() {
89 return FrameInfos.size();
92 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
96 /// @name Assembly File Formatting.
99 /// isVerboseAsm - Return true if this streamer supports verbose assembly
100 /// and if it is enabled.
101 virtual bool isVerboseAsm() const { return false; }
103 /// hasRawTextSupport - Return true if this asm streamer supports emitting
104 /// unformatted text to the .s file with EmitRawText.
105 virtual bool hasRawTextSupport() const { return false; }
107 /// AddComment - Add a comment that can be emitted to the generated .s
108 /// file if applicable as a QoI issue to make the output of the compiler
109 /// more readable. This only affects the MCAsmStreamer, and only when
110 /// verbose assembly output is enabled.
112 /// If the comment includes embedded \n's, they will each get the comment
113 /// prefix as appropriate. The added comment should not end with a \n.
114 virtual void AddComment(const Twine &T) {}
116 /// GetCommentOS - Return a raw_ostream that comments can be written to.
117 /// Unlike AddComment, you are required to terminate comments with \n if you
119 virtual raw_ostream &GetCommentOS();
121 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
122 virtual void AddBlankLine() {}
126 /// @name Symbol & Section Management
129 /// getCurrentSection - Return the current section that the streamer is
130 /// emitting code to.
131 const MCSection *getCurrentSection() const {
132 if (!SectionStack.empty())
133 return SectionStack.back().first;
137 /// getPreviousSection - Return the previous section that the streamer is
138 /// emitting code to.
139 const MCSection *getPreviousSection() const {
140 if (!SectionStack.empty())
141 return SectionStack.back().second;
145 /// ChangeSection - Update streamer for a new active section.
147 /// This is called by PopSection and SwitchSection, if the current
149 virtual void ChangeSection(const MCSection *) = 0;
151 /// pushSection - Save the current and previous section on the
154 SectionStack.push_back(std::make_pair(getCurrentSection(),
155 getPreviousSection()));
158 /// popSection - Restore the current and previous section from
159 /// the section stack. Calls ChangeSection as needed.
161 /// Returns false if the stack was empty.
163 if (SectionStack.size() <= 1)
165 const MCSection *oldSection = SectionStack.pop_back_val().first;
166 const MCSection *curSection = SectionStack.back().first;
168 if (oldSection != curSection)
169 ChangeSection(curSection);
173 /// SwitchSection - Set the current section where code is being emitted to
174 /// @p Section. This is required to update CurSection.
176 /// This corresponds to assembler directives like .section, .text, etc.
177 void SwitchSection(const MCSection *Section) {
178 assert(Section && "Cannot switch to a null section!");
179 const MCSection *curSection = SectionStack.back().first;
180 SectionStack.back().second = curSection;
181 if (Section != curSection) {
182 SectionStack.back().first = Section;
183 ChangeSection(Section);
187 /// InitSections - Create the default sections and set the initial one.
188 virtual void InitSections() = 0;
190 /// EmitLabel - Emit a label for @p Symbol into the current section.
192 /// This corresponds to an assembler statement such as:
195 /// @param Symbol - The symbol to emit. A given symbol should only be
196 /// emitted as a label once, and symbols emitted as a label should never be
197 /// used in an assignment.
198 virtual void EmitLabel(MCSymbol *Symbol);
200 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
203 /// EmitAssemblerFlag - Note in the output the specified @p Flag
204 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
206 /// EmitThumbFunc - Note in the output that the specified @p Func is
207 /// a Thumb mode function (ARM target only).
208 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
210 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
212 /// This corresponds to an assembler statement such as:
215 /// The assignment generates no code, but has the side effect of binding the
216 /// value in the current context. For the assembly streamer, this prints the
217 /// binding into the .s file.
219 /// @param Symbol - The symbol being assigned to.
220 /// @param Value - The value for the symbol.
221 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
223 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
225 /// This corresponds to an assembler statement such as:
226 /// .weakref alias, symbol
228 /// @param Alias - The alias that is being created.
229 /// @param Symbol - The symbol being aliased.
230 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
232 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
233 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
234 MCSymbolAttr Attribute) = 0;
236 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
238 /// @param Symbol - The symbol to have its n_desc field set.
239 /// @param DescValue - The value to set into the n_desc field.
240 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
242 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
244 /// @param Symbol - The symbol to have its External & Type fields set.
245 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
247 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
249 /// @param StorageClass - The storage class the symbol should have.
250 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
252 /// EmitCOFFSymbolType - Emit the type of the symbol.
254 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
255 virtual void EmitCOFFSymbolType(int Type) = 0;
257 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
258 virtual void EndCOFFSymbolDef() = 0;
260 /// EmitELFSize - Emit an ELF .size directive.
262 /// This corresponds to an assembler statement such as:
263 /// .size symbol, expression
265 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
267 /// EmitCommonSymbol - Emit a common symbol.
269 /// @param Symbol - The common symbol to emit.
270 /// @param Size - The size of the common symbol.
271 /// @param ByteAlignment - The alignment of the symbol if
272 /// non-zero. This must be a power of 2.
273 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
274 unsigned ByteAlignment) = 0;
276 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
278 /// @param Symbol - The common symbol to emit.
279 /// @param Size - The size of the common symbol.
280 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
282 /// EmitZerofill - Emit the zerofill section and an optional symbol.
284 /// @param Section - The zerofill section to create and or to put the symbol
285 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
286 /// @param Size - The size of the zerofill symbol.
287 /// @param ByteAlignment - The alignment of the zerofill symbol if
288 /// non-zero. This must be a power of 2 on some targets.
289 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
290 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
292 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
294 /// @param Section - The thread local common section.
295 /// @param Symbol - The thread local common symbol to emit.
296 /// @param Size - The size of the symbol.
297 /// @param ByteAlignment - The alignment of the thread local common symbol
298 /// if non-zero. This must be a power of 2 on some targets.
299 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
300 uint64_t Size, unsigned ByteAlignment = 0) = 0;
303 /// @name Generating Data
306 /// EmitBytes - Emit the bytes in \arg Data into the output.
308 /// This is used to implement assembler directives such as .byte, .ascii,
310 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
312 /// EmitValue - Emit the expression @p Value into the output as a native
313 /// integer of the given @p Size bytes.
315 /// This is used to implement assembler directives such as .word, .quad,
318 /// @param Value - The value to emit.
319 /// @param Size - The size of the integer (in bytes) to emit. This must
320 /// match a native machine width.
321 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
322 unsigned AddrSpace) = 0;
324 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
326 /// EmitIntValue - Special case of EmitValue that avoids the client having
327 /// to pass in a MCExpr for constant integers.
328 virtual void EmitIntValue(uint64_t Value, unsigned Size,
329 unsigned AddrSpace = 0);
331 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
332 /// this is done by producing
335 void EmitAbsValue(const MCExpr *Value, unsigned Size,
336 unsigned AddrSpace = 0);
338 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
340 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
342 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
343 /// client having to pass in a MCExpr for constant integers.
344 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
346 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
347 /// client having to pass in a MCExpr for constant integers.
348 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
350 /// EmitSymbolValue - Special case of EmitValue that avoids the client
351 /// having to pass in a MCExpr for MCSymbols.
352 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
353 unsigned AddrSpace = 0);
355 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
356 /// gprel32 (32-bit GP relative) value.
358 /// This is used to implement assembler directives such as .gprel32 on
359 /// targets that support them.
360 virtual void EmitGPRel32Value(const MCExpr *Value);
362 /// EmitFill - Emit NumBytes bytes worth of the value specified by
363 /// FillValue. This implements directives such as '.space'.
364 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
367 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
368 /// function that just wraps EmitFill.
369 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
370 EmitFill(NumBytes, 0, AddrSpace);
374 /// EmitValueToAlignment - Emit some number of copies of @p Value until
375 /// the byte alignment @p ByteAlignment is reached.
377 /// If the number of bytes need to emit for the alignment is not a multiple
378 /// of @p ValueSize, then the contents of the emitted fill bytes is
381 /// This used to implement the .align assembler directive.
383 /// @param ByteAlignment - The alignment to reach. This must be a power of
384 /// two on some targets.
385 /// @param Value - The value to use when filling bytes.
386 /// @param ValueSize - The size of the integer (in bytes) to emit for
387 /// @p Value. This must match a native machine width.
388 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
389 /// the alignment cannot be reached in this many bytes, no bytes are
391 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
392 unsigned ValueSize = 1,
393 unsigned MaxBytesToEmit = 0) = 0;
395 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
398 /// This used to align code where the alignment bytes may be executed. This
399 /// can emit different bytes for different sizes to optimize execution.
401 /// @param ByteAlignment - The alignment to reach. This must be a power of
402 /// two on some targets.
403 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
404 /// the alignment cannot be reached in this many bytes, no bytes are
406 virtual void EmitCodeAlignment(unsigned ByteAlignment,
407 unsigned MaxBytesToEmit = 0) = 0;
409 /// EmitValueToOffset - Emit some number of copies of @p Value until the
410 /// byte offset @p Offset is reached.
412 /// This is used to implement assembler directives such as .org.
414 /// @param Offset - The offset to reach. This may be an expression, but the
415 /// expression must be associated with the current section.
416 /// @param Value - The value to use when filling bytes.
417 virtual void EmitValueToOffset(const MCExpr *Offset,
418 unsigned char Value = 0) = 0;
422 /// EmitFileDirective - Switch to a new logical file. This is used to
423 /// implement the '.file "foo.c"' assembler directive.
424 virtual void EmitFileDirective(StringRef Filename) = 0;
426 /// EmitDwarfFileDirective - Associate a filename with a specified logical
427 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
429 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
431 /// EmitDwarfLocDirective - This implements the DWARF2
432 // '.loc fileno lineno ...' assembler directive.
433 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
434 unsigned Column, unsigned Flags,
436 unsigned Discriminator,
439 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
440 const MCSymbol *LastLabel,
441 const MCSymbol *Label) = 0;
443 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
444 const MCSymbol *Label) {
447 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
450 virtual void EmitCFISections(bool EH, bool Debug);
451 virtual void EmitCFIStartProc();
452 virtual void EmitCFIEndProc();
453 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
454 virtual void EmitCFIDefCfaOffset(int64_t Offset);
455 virtual void EmitCFIDefCfaRegister(int64_t Register);
456 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
457 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
458 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
459 virtual void EmitCFIRememberState();
460 virtual void EmitCFIRestoreState();
461 virtual void EmitCFISameValue(int64_t Register);
462 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
463 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
465 virtual void EmitWin64EHStartProc(MCSymbol *Symbol, MCSymbol *EHandler = 0);
466 virtual void EmitWin64EHEndProc();
467 virtual void EmitWin64EHStartChained();
468 virtual void EmitWin64EHEndChained();
469 virtual void EmitWin64EHUnwindOnly();
470 virtual void EmitWin64EHLsda(const MCSymbol *Sym, int64_t Size);
471 virtual void EmitWin64EHPushReg(int64_t Register);
472 virtual void EmitWin64EHSetFrame(int64_t Register, int64_t Offset);
473 virtual void EmitWin64EHAllocStack(int64_t Size);
474 virtual void EmitWin64EHSaveReg(int64_t Register, int64_t Offset);
475 virtual void EmitWin64EHPushFrame(bool Code);
476 virtual void EmitWin64EHEndProlog();
478 /// EmitInstruction - Emit the given @p Instruction into the current
480 virtual void EmitInstruction(const MCInst &Inst) = 0;
482 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
483 /// the specified string in the output .s file. This capability is
484 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
485 virtual void EmitRawText(StringRef String);
486 void EmitRawText(const Twine &String);
488 /// ARM-related methods.
489 /// FIXME: Eventually we should have some "target MC streamer" and move
490 /// these methods there.
491 virtual void EmitFnStart();
492 virtual void EmitFnEnd();
493 virtual void EmitCantUnwind();
494 virtual void EmitPersonality(const MCSymbol *Personality);
495 virtual void EmitHandlerData();
496 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
497 virtual void EmitPad(int64_t Offset);
498 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
501 /// Finish - Finish emission of machine code.
502 virtual void Finish() = 0;
505 /// createNullStreamer - Create a dummy machine code streamer, which does
506 /// nothing. This is useful for timing the assembler front end.
507 MCStreamer *createNullStreamer(MCContext &Ctx);
509 /// createAsmStreamer - Create a machine code streamer which will print out
510 /// assembly for the native target, suitable for compiling with a native
513 /// \param InstPrint - If given, the instruction printer to use. If not given
514 /// the MCInst representation will be printed. This method takes ownership of
517 /// \param CE - If given, a code emitter to use to show the instruction
518 /// encoding inline with the assembly. This method takes ownership of \arg CE.
520 /// \param TAB - If given, a target asm backend to use to show the fixup
521 /// information in conjunction with encoding information. This method takes
522 /// ownership of \arg TAB.
524 /// \param ShowInst - Whether to show the MCInst representation inline with
526 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
530 MCInstPrinter *InstPrint = 0,
531 MCCodeEmitter *CE = 0,
532 TargetAsmBackend *TAB = 0,
533 bool ShowInst = false);
535 /// createMachOStreamer - Create a machine code streamer which will generate
536 /// Mach-O format object files.
538 /// Takes ownership of \arg TAB and \arg CE.
539 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
540 raw_ostream &OS, MCCodeEmitter *CE,
541 bool RelaxAll = false);
543 /// createWinCOFFStreamer - Create a machine code streamer which will
544 /// generate Microsoft COFF format object files.
546 /// Takes ownership of \arg TAB and \arg CE.
547 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
548 TargetAsmBackend &TAB,
549 MCCodeEmitter &CE, raw_ostream &OS,
550 bool RelaxAll = false);
552 /// createELFStreamer - Create a machine code streamer which will generate
553 /// ELF format object files.
554 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
555 raw_ostream &OS, MCCodeEmitter *CE,
556 bool RelaxAll, bool NoExecStack);
558 /// createLoggingStreamer - Create a machine code streamer which just logs the
559 /// API calls and then dispatches to another streamer.
561 /// The new streamer takes ownership of the \arg Child.
562 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
564 /// createPureStreamer - Create a machine code streamer which will generate
565 /// "pure" MC object files, for use with MC-JIT and testing tools.
567 /// Takes ownership of \arg TAB and \arg CE.
568 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
569 raw_ostream &OS, MCCodeEmitter *CE);
571 } // end namespace llvm