MCStreamer.h revision 32ae3fe0ba469240753e2342e36485f7c9acfb5c
1//===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the MCStreamer class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_MC_MCSTREAMER_H 15#define LLVM_MC_MCSTREAMER_H 16 17#include "llvm/System/DataTypes.h" 18 19namespace llvm { 20 class MCAsmInfo; 21 class MCCodeEmitter; 22 class MCContext; 23 class MCExpr; 24 class MCInst; 25 class MCInstPrinter; 26 class MCSection; 27 class MCSymbol; 28 class StringRef; 29 class raw_ostream; 30 31 /// MCStreamer - Streaming machine code generation interface. This interface 32 /// is intended to provide a programatic interface that is very similar to the 33 /// level that an assembler .s file provides. It has callbacks to emit bytes, 34 /// handle directives, etc. The implementation of this interface retains 35 /// state to know what the current section is etc. 36 /// 37 /// There are multiple implementations of this interface: one for writing out 38 /// a .s file, and implementations that write out .o files of various formats. 39 /// 40 class MCStreamer { 41 public: 42 enum SymbolAttr { 43 Global, /// .globl 44 Hidden, /// .hidden (ELF) 45 IndirectSymbol, /// .indirect_symbol (Apple) 46 Internal, /// .internal (ELF) 47 LazyReference, /// .lazy_reference (Apple) 48 NoDeadStrip, /// .no_dead_strip (Apple) 49 PrivateExtern, /// .private_extern (Apple) 50 Protected, /// .protected (ELF) 51 Reference, /// .reference (Apple) 52 Weak, /// .weak 53 WeakDefinition, /// .weak_definition (Apple) 54 WeakReference, /// .weak_reference (Apple) 55 56 SymbolAttrFirst = Global, 57 SymbolAttrLast = WeakReference 58 }; 59 60 enum AssemblerFlag { 61 SubsectionsViaSymbols /// .subsections_via_symbols (Apple) 62 }; 63 64 private: 65 MCContext &Context; 66 67 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 68 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 69 70 protected: 71 MCStreamer(MCContext &Ctx); 72 73 /// CurSection - This is the current section code is being emitted to, it is 74 /// kept up to date by SwitchSection. 75 const MCSection *CurSection; 76 77 public: 78 virtual ~MCStreamer(); 79 80 MCContext &getContext() const { return Context; } 81 82 /// @name Symbol & Section Management 83 /// @{ 84 85 /// getCurrentSection - Return the current seciton that the streamer is 86 /// emitting code to. 87 const MCSection *getCurrentSection() const { return CurSection; } 88 89 /// SwitchSection - Set the current section where code is being emitted to 90 /// @param Section. This is required to update CurSection. 91 /// 92 /// This corresponds to assembler directives like .section, .text, etc. 93 virtual void SwitchSection(const MCSection *Section) = 0; 94 95 /// EmitLabel - Emit a label for @param Symbol into the current section. 96 /// 97 /// This corresponds to an assembler statement such as: 98 /// foo: 99 /// 100 /// @param Symbol - The symbol to emit. A given symbol should only be 101 /// emitted as a label once, and symbols emitted as a label should never be 102 /// used in an assignment. 103 virtual void EmitLabel(MCSymbol *Symbol) = 0; 104 105 /// EmitAssemblerFlag - Note in the output the specified @param Flag 106 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0; 107 108 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol. 109 /// 110 /// This corresponds to an assembler statement such as: 111 /// symbol = value 112 /// 113 /// The assignment generates no code, but has the side effect of binding the 114 /// value in the current context. For the assembly streamer, this prints the 115 /// binding into the .s file. 116 /// 117 /// @param Symbol - The symbol being assigned to. 118 /// @param Value - The value for the symbol. 119 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0; 120 121 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol. 122 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 123 SymbolAttr Attribute) = 0; 124 125 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol. 126 /// 127 /// @param Symbol - The symbol to have its n_desc field set. 128 /// @param DescValue - The value to set into the n_desc field. 129 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 130 131 /// EmitCommonSymbol - Emit a common or local common symbol. 132 /// 133 /// @param Symbol - The common symbol to emit. 134 /// @param Size - The size of the common symbol. 135 /// @param ByteAlignment - The alignment of the symbol if 136 /// non-zero. This must be a power of 2 on some targets. 137 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 138 unsigned ByteAlignment) = 0; 139 140 /// EmitZerofill - Emit a the zerofill section and an option symbol. 141 /// 142 /// @param Section - The zerofill section to create and or to put the symbol 143 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 144 /// @param Size - The size of the zerofill symbol. 145 /// @param ByteAlignment - The alignment of the zerofill symbol if 146 /// non-zero. This must be a power of 2 on some targets. 147 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 148 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 149 150 /// @} 151 /// @name Generating Data 152 /// @{ 153 154 /// EmitBytes - Emit the bytes in \arg Data into the output. 155 /// 156 /// This is used to implement assembler directives such as .byte, .ascii, 157 /// etc. 158 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0; 159 160 /// EmitValue - Emit the expression @param Value into the output as a native 161 /// integer of the given @param Size bytes. 162 /// 163 /// This is used to implement assembler directives such as .word, .quad, 164 /// etc. 165 /// 166 /// @param Value - The value to emit. 167 /// @param Size - The size of the integer (in bytes) to emit. This must 168 /// match a native machine width. 169 virtual void EmitValue(const MCExpr *Value, unsigned Size, 170 unsigned AddrSpace) = 0; 171 172 /// EmitIntValue - Special case of EmitValue that avoids the client having 173 /// to pass in a MCExpr for constant integers. 174 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace); 175 176 /// EmitFill - Emit NumBytes bytes worth of the value specified by 177 /// FillValue. This implements directives such as '.space'. 178 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue, 179 unsigned AddrSpace); 180 181 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience 182 /// function that just wraps EmitFill. 183 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) { 184 EmitFill(NumBytes, 0, AddrSpace); 185 } 186 187 188 /// EmitValueToAlignment - Emit some number of copies of @param Value until 189 /// the byte alignment @param ByteAlignment is reached. 190 /// 191 /// If the number of bytes need to emit for the alignment is not a multiple 192 /// of @param ValueSize, then the contents of the emitted fill bytes is 193 /// undefined. 194 /// 195 /// This used to implement the .align assembler directive. 196 /// 197 /// @param ByteAlignment - The alignment to reach. This must be a power of 198 /// two on some targets. 199 /// @param Value - The value to use when filling bytes. 200 /// @param Size - The size of the integer (in bytes) to emit for @param 201 /// Value. This must match a native machine width. 202 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 203 /// the alignment cannot be reached in this many bytes, no bytes are 204 /// emitted. 205 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 206 unsigned ValueSize = 1, 207 unsigned MaxBytesToEmit = 0) = 0; 208 209 /// EmitValueToOffset - Emit some number of copies of @param Value until the 210 /// byte offset @param Offset is reached. 211 /// 212 /// This is used to implement assembler directives such as .org. 213 /// 214 /// @param Offset - The offset to reach. This may be an expression, but the 215 /// expression must be associated with the current section. 216 /// @param Value - The value to use when filling bytes. 217 virtual void EmitValueToOffset(const MCExpr *Offset, 218 unsigned char Value = 0) = 0; 219 220 /// @} 221 222 /// EmitInstruction - Emit the given @param Instruction into the current 223 /// section. 224 virtual void EmitInstruction(const MCInst &Inst) = 0; 225 226 /// Finish - Finish emission of machine code and flush any output. 227 virtual void Finish() = 0; 228 }; 229 230 /// createNullStreamer - Create a dummy machine code streamer, which does 231 /// nothing. This is useful for timing the assembler front end. 232 MCStreamer *createNullStreamer(MCContext &Ctx); 233 234 /// createAsmStreamer - Create a machine code streamer which will print out 235 /// assembly for the native target, suitable for compiling with a native 236 /// assembler. 237 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS, 238 const MCAsmInfo &MAI, 239 MCInstPrinter *InstPrint = 0, 240 MCCodeEmitter *CE = 0); 241 242 // FIXME: These two may end up getting rolled into a single 243 // createObjectStreamer interface, which implements the assembler backend, and 244 // is parameterized on an output object file writer. 245 246 /// createMachOStream - Create a machine code streamer which will generative 247 /// Mach-O format object files. 248 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 249 MCCodeEmitter *CE = 0); 250 251 /// createELFStreamer - Create a machine code streamer which will generative 252 /// ELF format object files. 253 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 254 255} // end namespace llvm 256 257#endif 258