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"
32 class TargetAsmBackend;
33 class TargetLoweringObjectFile;
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&); // DO NOT IMPLEMENT
51 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
56 std::vector<MCDwarfFrameInfo> FrameInfos;
57 MCDwarfFrameInfo *getCurrentFrameInfo();
58 void EnsureValidFrame();
60 const MCSymbol* LastNonPrivate;
62 /// SectionStack - This is stack of current and previous section
63 /// values saved by PushSection.
64 SmallVector<std::pair<const MCSection *,
65 const MCSection *>, 4> SectionStack;
68 MCStreamer(MCContext &Ctx);
70 const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A,
73 const MCExpr *ForceExpAbs(MCStreamer *Streamer, MCContext &Context,
76 void EmitFrames(bool usingCFI);
79 virtual ~MCStreamer();
81 MCContext &getContext() const { return Context; }
83 unsigned getNumFrameInfos() {
84 return FrameInfos.size();
87 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
91 /// @name Assembly File Formatting.
94 /// isVerboseAsm - Return true if this streamer supports verbose assembly
95 /// and if it is enabled.
96 virtual bool isVerboseAsm() const { return false; }
98 /// hasRawTextSupport - Return true if this asm streamer supports emitting
99 /// unformatted text to the .s file with EmitRawText.
100 virtual bool hasRawTextSupport() const { return false; }
102 /// AddComment - Add a comment that can be emitted to the generated .s
103 /// file if applicable as a QoI issue to make the output of the compiler
104 /// more readable. This only affects the MCAsmStreamer, and only when
105 /// verbose assembly output is enabled.
107 /// If the comment includes embedded \n's, they will each get the comment
108 /// prefix as appropriate. The added comment should not end with a \n.
109 virtual void AddComment(const Twine &T) {}
111 /// GetCommentOS - Return a raw_ostream that comments can be written to.
112 /// Unlike AddComment, you are required to terminate comments with \n if you
114 virtual raw_ostream &GetCommentOS();
116 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
117 virtual void AddBlankLine() {}
121 /// @name Symbol & Section Management
124 /// getCurrentSection - Return the current section that the streamer is
125 /// emitting code to.
126 const MCSection *getCurrentSection() const {
127 if (!SectionStack.empty())
128 return SectionStack.back().first;
132 /// getPreviousSection - Return the previous section that the streamer is
133 /// emitting code to.
134 const MCSection *getPreviousSection() const {
135 if (!SectionStack.empty())
136 return SectionStack.back().second;
140 /// ChangeSection - Update streamer for a new active section.
142 /// This is called by PopSection and SwitchSection, if the current
144 virtual void ChangeSection(const MCSection *) = 0;
146 /// pushSection - Save the current and previous section on the
149 SectionStack.push_back(std::make_pair(getCurrentSection(),
150 getPreviousSection()));
153 /// popSection - Restore the current and previous section from
154 /// the section stack. Calls ChangeSection as needed.
156 /// Returns false if the stack was empty.
158 if (SectionStack.size() <= 1)
160 const MCSection *oldSection = SectionStack.pop_back_val().first;
161 const MCSection *curSection = SectionStack.back().first;
163 if (oldSection != curSection)
164 ChangeSection(curSection);
168 /// SwitchSection - Set the current section where code is being emitted to
169 /// @p Section. This is required to update CurSection.
171 /// This corresponds to assembler directives like .section, .text, etc.
172 void SwitchSection(const MCSection *Section) {
173 assert(Section && "Cannot switch to a null section!");
174 const MCSection *curSection = SectionStack.back().first;
175 SectionStack.back().second = curSection;
176 if (Section != curSection) {
177 SectionStack.back().first = Section;
178 ChangeSection(Section);
182 /// InitSections - Create the default sections and set the initial one.
183 virtual void InitSections() = 0;
185 /// EmitLabel - Emit a label for @p Symbol into the current section.
187 /// This corresponds to an assembler statement such as:
190 /// @param Symbol - The symbol to emit. A given symbol should only be
191 /// emitted as a label once, and symbols emitted as a label should never be
192 /// used in an assignment.
193 virtual void EmitLabel(MCSymbol *Symbol);
195 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
198 /// EmitAssemblerFlag - Note in the output the specified @p Flag
199 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
201 /// EmitThumbFunc - Note in the output that the specified @p Func is
202 /// a Thumb mode function (ARM target only).
203 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
205 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
207 /// This corresponds to an assembler statement such as:
210 /// The assignment generates no code, but has the side effect of binding the
211 /// value in the current context. For the assembly streamer, this prints the
212 /// binding into the .s file.
214 /// @param Symbol - The symbol being assigned to.
215 /// @param Value - The value for the symbol.
216 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
218 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
220 /// This corresponds to an assembler statement such as:
221 /// .weakref alias, symbol
223 /// @param Alias - The alias that is being created.
224 /// @param Symbol - The symbol being aliased.
225 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
227 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
228 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
229 MCSymbolAttr Attribute) = 0;
231 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
233 /// @param Symbol - The symbol to have its n_desc field set.
234 /// @param DescValue - The value to set into the n_desc field.
235 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
237 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
239 /// @param Symbol - The symbol to have its External & Type fields set.
240 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
242 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
244 /// @param StorageClass - The storage class the symbol should have.
245 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
247 /// EmitCOFFSymbolType - Emit the type of the symbol.
249 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
250 virtual void EmitCOFFSymbolType(int Type) = 0;
252 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
253 virtual void EndCOFFSymbolDef() = 0;
255 /// EmitELFSize - Emit an ELF .size directive.
257 /// This corresponds to an assembler statement such as:
258 /// .size symbol, expression
260 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
262 /// EmitCommonSymbol - Emit a common symbol.
264 /// @param Symbol - The common symbol to emit.
265 /// @param Size - The size of the common symbol.
266 /// @param ByteAlignment - The alignment of the symbol if
267 /// non-zero. This must be a power of 2.
268 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
269 unsigned ByteAlignment) = 0;
271 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
273 /// @param Symbol - The common symbol to emit.
274 /// @param Size - The size of the common symbol.
275 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
277 /// EmitZerofill - Emit the zerofill section and an optional symbol.
279 /// @param Section - The zerofill section to create and or to put the symbol
280 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
281 /// @param Size - The size of the zerofill symbol.
282 /// @param ByteAlignment - The alignment of the zerofill symbol if
283 /// non-zero. This must be a power of 2 on some targets.
284 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
285 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
287 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
289 /// @param Section - The thread local common section.
290 /// @param Symbol - The thread local common symbol to emit.
291 /// @param Size - The size of the symbol.
292 /// @param ByteAlignment - The alignment of the thread local common symbol
293 /// if non-zero. This must be a power of 2 on some targets.
294 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
295 uint64_t Size, unsigned ByteAlignment = 0) = 0;
298 /// @name Generating Data
301 /// EmitBytes - Emit the bytes in \arg Data into the output.
303 /// This is used to implement assembler directives such as .byte, .ascii,
305 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
307 /// EmitValue - Emit the expression @p Value into the output as a native
308 /// integer of the given @p Size bytes.
310 /// This is used to implement assembler directives such as .word, .quad,
313 /// @param Value - The value to emit.
314 /// @param Size - The size of the integer (in bytes) to emit. This must
315 /// match a native machine width.
316 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
317 unsigned AddrSpace) = 0;
319 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
321 /// EmitIntValue - Special case of EmitValue that avoids the client having
322 /// to pass in a MCExpr for constant integers.
323 virtual void EmitIntValue(uint64_t Value, unsigned Size,
324 unsigned AddrSpace = 0);
326 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
327 /// this is done by producing
330 void EmitAbsValue(const MCExpr *Value, unsigned Size,
331 unsigned AddrSpace = 0);
333 virtual void EmitULEB128Value(const MCExpr *Value) = 0;
335 virtual void EmitSLEB128Value(const MCExpr *Value) = 0;
337 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
338 /// client having to pass in a MCExpr for constant integers.
339 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
341 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
342 /// client having to pass in a MCExpr for constant integers.
343 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
345 /// EmitSymbolValue - Special case of EmitValue that avoids the client
346 /// having to pass in a MCExpr for MCSymbols.
347 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
348 unsigned AddrSpace = 0);
350 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
351 /// gprel32 (32-bit GP relative) value.
353 /// This is used to implement assembler directives such as .gprel32 on
354 /// targets that support them.
355 virtual void EmitGPRel32Value(const MCExpr *Value);
357 /// EmitFill - Emit NumBytes bytes worth of the value specified by
358 /// FillValue. This implements directives such as '.space'.
359 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
362 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
363 /// function that just wraps EmitFill.
364 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
365 EmitFill(NumBytes, 0, AddrSpace);
369 /// EmitValueToAlignment - Emit some number of copies of @p Value until
370 /// the byte alignment @p ByteAlignment is reached.
372 /// If the number of bytes need to emit for the alignment is not a multiple
373 /// of @p ValueSize, then the contents of the emitted fill bytes is
376 /// This used to implement the .align assembler directive.
378 /// @param ByteAlignment - The alignment to reach. This must be a power of
379 /// two on some targets.
380 /// @param Value - The value to use when filling bytes.
381 /// @param ValueSize - The size of the integer (in bytes) to emit for
382 /// @p Value. This must match a native machine width.
383 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
384 /// the alignment cannot be reached in this many bytes, no bytes are
386 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
387 unsigned ValueSize = 1,
388 unsigned MaxBytesToEmit = 0) = 0;
390 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
393 /// This used to align code where the alignment bytes may be executed. This
394 /// can emit different bytes for different sizes to optimize execution.
396 /// @param ByteAlignment - The alignment to reach. This must be a power of
397 /// two on some targets.
398 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
399 /// the alignment cannot be reached in this many bytes, no bytes are
401 virtual void EmitCodeAlignment(unsigned ByteAlignment,
402 unsigned MaxBytesToEmit = 0) = 0;
404 /// EmitValueToOffset - Emit some number of copies of @p Value until the
405 /// byte offset @p Offset is reached.
407 /// This is used to implement assembler directives such as .org.
409 /// @param Offset - The offset to reach. This may be an expression, but the
410 /// expression must be associated with the current section.
411 /// @param Value - The value to use when filling bytes.
412 virtual void EmitValueToOffset(const MCExpr *Offset,
413 unsigned char Value = 0) = 0;
417 /// EmitFileDirective - Switch to a new logical file. This is used to
418 /// implement the '.file "foo.c"' assembler directive.
419 virtual void EmitFileDirective(StringRef Filename) = 0;
421 /// EmitDwarfFileDirective - Associate a filename with a specified logical
422 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
424 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
426 /// EmitDwarfLocDirective - This implements the DWARF2
427 // '.loc fileno lineno ...' assembler directive.
428 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
429 unsigned Column, unsigned Flags,
431 unsigned Discriminator,
434 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
435 const MCSymbol *LastLabel,
436 const MCSymbol *Label) = 0;
438 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
439 const MCSymbol *Label) {
442 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
445 virtual void EmitCFISections(bool EH, bool Debug);
446 virtual void EmitCFIStartProc();
447 virtual void EmitCFIEndProc();
448 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
449 virtual void EmitCFIDefCfaOffset(int64_t Offset);
450 virtual void EmitCFIDefCfaRegister(int64_t Register);
451 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
452 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
453 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
454 virtual void EmitCFIRememberState();
455 virtual void EmitCFIRestoreState();
456 virtual void EmitCFISameValue(int64_t Register);
457 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
458 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
460 virtual void EmitWin64EHStartProc(MCSymbol *Symbol, MCSymbol *EHandler = 0);
461 virtual void EmitWin64EHEndProc();
462 virtual void EmitWin64EHPushReg(int64_t Register);
463 virtual void EmitWin64EHSetFrame(int64_t Register, int64_t Offset);
464 virtual void EmitWin64EHAllocStack(int64_t Size);
465 virtual void EmitWin64EHSaveReg(int64_t Register, int64_t Offset);
466 virtual void EmitWin64EHPushFrame(bool Code);
467 virtual void EmitWin64EHEndProlog();
469 /// EmitInstruction - Emit the given @p Instruction into the current
471 virtual void EmitInstruction(const MCInst &Inst) = 0;
473 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
474 /// the specified string in the output .s file. This capability is
475 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
476 virtual void EmitRawText(StringRef String);
477 void EmitRawText(const Twine &String);
479 /// ARM-related methods.
480 /// FIXME: Eventually we should have some "target MC streamer" and move
481 /// these methods there.
482 virtual void EmitFnStart();
483 virtual void EmitFnEnd();
484 virtual void EmitCantUnwind();
485 virtual void EmitPersonality(const MCSymbol *Personality);
486 virtual void EmitHandlerData();
487 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
488 virtual void EmitPad(int64_t Offset);
489 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
492 /// Finish - Finish emission of machine code.
493 virtual void Finish() = 0;
496 /// createNullStreamer - Create a dummy machine code streamer, which does
497 /// nothing. This is useful for timing the assembler front end.
498 MCStreamer *createNullStreamer(MCContext &Ctx);
500 /// createAsmStreamer - Create a machine code streamer which will print out
501 /// assembly for the native target, suitable for compiling with a native
504 /// \param InstPrint - If given, the instruction printer to use. If not given
505 /// the MCInst representation will be printed. This method takes ownership of
508 /// \param CE - If given, a code emitter to use to show the instruction
509 /// encoding inline with the assembly. This method takes ownership of \arg CE.
511 /// \param TAB - If given, a target asm backend to use to show the fixup
512 /// information in conjunction with encoding information. This method takes
513 /// ownership of \arg TAB.
515 /// \param ShowInst - Whether to show the MCInst representation inline with
517 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
521 MCInstPrinter *InstPrint = 0,
522 MCCodeEmitter *CE = 0,
523 TargetAsmBackend *TAB = 0,
524 bool ShowInst = false);
526 /// createMachOStreamer - Create a machine code streamer which will generate
527 /// Mach-O format object files.
529 /// Takes ownership of \arg TAB and \arg CE.
530 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
531 raw_ostream &OS, MCCodeEmitter *CE,
532 bool RelaxAll = false);
534 /// createWinCOFFStreamer - Create a machine code streamer which will
535 /// generate Microsoft COFF format object files.
537 /// Takes ownership of \arg TAB and \arg CE.
538 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
539 TargetAsmBackend &TAB,
540 MCCodeEmitter &CE, raw_ostream &OS,
541 bool RelaxAll = false);
543 /// createELFStreamer - Create a machine code streamer which will generate
544 /// ELF format object files.
545 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
546 raw_ostream &OS, MCCodeEmitter *CE,
547 bool RelaxAll, bool NoExecStack);
549 /// createLoggingStreamer - Create a machine code streamer which just logs the
550 /// API calls and then dispatches to another streamer.
552 /// The new streamer takes ownership of the \arg Child.
553 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
555 /// createPureStreamer - Create a machine code streamer which will generate
556 /// "pure" MC object files, for use with MC-JIT and testing tools.
558 /// Takes ownership of \arg TAB and \arg CE.
559 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
560 raw_ostream &OS, MCCodeEmitter *CE);
562 } // end namespace llvm