MCStreamer.h revision 4a1fadaf5207e46d19c64e5773ff8d9e65e607d7
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/Support/DataTypes.h" 18 19namespace llvm { 20 class AsmPrinter; 21 class MCAsmInfo; 22 class MCCodeEmitter; 23 class MCContext; 24 class MCInst; 25 class MCSection; 26 class MCSymbol; 27 class MCValue; 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 /// @param MakeAbsolute - If true, then the symbol should be given the 120 /// absolute value of @param Value, even if @param Value would be 121 /// relocatable expression. This corresponds to the ".set" directive. 122 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, 123 bool MakeAbsolute = false) = 0; 124 125 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol. 126 virtual void EmitSymbolAttribute(MCSymbol *Symbol, 127 SymbolAttr Attribute) = 0; 128 129 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol. 130 /// 131 /// @param Symbol - The symbol to have its n_desc field set. 132 /// @param DescValue - The value to set into the n_desc field. 133 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0; 134 135 /// EmitCommonSymbol - Emit a common or local common symbol. 136 /// 137 /// @param Symbol - The common symbol to emit. 138 /// @param Size - The size of the common symbol. 139 /// @param ByteAlignment - The alignment of the symbol if 140 /// non-zero. This must be a power of 2 on some targets. 141 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 142 unsigned ByteAlignment) = 0; 143 144 /// EmitZerofill - Emit a the zerofill section and an option symbol. 145 /// 146 /// @param Section - The zerofill section to create and or to put the symbol 147 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 148 /// @param Size - The size of the zerofill symbol. 149 /// @param ByteAlignment - The alignment of the zerofill symbol if 150 /// non-zero. This must be a power of 2 on some targets. 151 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, 152 unsigned Size = 0,unsigned ByteAlignment = 0) = 0; 153 154 /// @} 155 /// @name Generating Data 156 /// @{ 157 158 /// EmitBytes - Emit the bytes in \arg Data into the output. 159 /// 160 /// This is used to implement assembler directives such as .byte, .ascii, 161 /// etc. 162 virtual void EmitBytes(const StringRef &Data) = 0; 163 164 /// EmitValue - Emit the expression @param Value into the output as a native 165 /// integer of the given @param Size bytes. 166 /// 167 /// This is used to implement assembler directives such as .word, .quad, 168 /// etc. 169 /// 170 /// @param Value - The value to emit. 171 /// @param Size - The size of the integer (in bytes) to emit. This must 172 /// match a native machine width. 173 virtual void EmitValue(const MCValue &Value, unsigned Size) = 0; 174 175 /// EmitValueToAlignment - Emit some number of copies of @param Value until 176 /// the byte alignment @param ByteAlignment is reached. 177 /// 178 /// If the number of bytes need to emit for the alignment is not a multiple 179 /// of @param ValueSize, then the contents of the emitted fill bytes is 180 /// undefined. 181 /// 182 /// This used to implement the .align assembler directive. 183 /// 184 /// @param ByteAlignment - The alignment to reach. This must be a power of 185 /// two on some targets. 186 /// @param Value - The value to use when filling bytes. 187 /// @param Size - The size of the integer (in bytes) to emit for @param 188 /// Value. This must match a native machine width. 189 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 190 /// the alignment cannot be reached in this many bytes, no bytes are 191 /// emitted. 192 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 193 unsigned ValueSize = 1, 194 unsigned MaxBytesToEmit = 0) = 0; 195 196 /// EmitValueToOffset - Emit some number of copies of @param Value until the 197 /// byte offset @param Offset is reached. 198 /// 199 /// This is used to implement assembler directives such as .org. 200 /// 201 /// @param Offset - The offset to reach. This may be an expression, but the 202 /// expression must be associated with the current section. 203 /// @param Value - The value to use when filling bytes. 204 virtual void EmitValueToOffset(const MCValue &Offset, 205 unsigned char Value = 0) = 0; 206 207 /// @} 208 209 /// EmitInstruction - Emit the given @param Instruction into the current 210 /// section. 211 virtual void EmitInstruction(const MCInst &Inst) = 0; 212 213 /// Finish - Finish emission of machine code and flush any output. 214 virtual void Finish() = 0; 215 }; 216 217 /// createNullStreamer - Create a dummy machine code streamer, which does 218 /// nothing. This is useful for timing the assembler front end. 219 MCStreamer *createNullStreamer(MCContext &Ctx); 220 221 /// createAsmStreamer - Create a machine code streamer which will print out 222 /// assembly for the native target, suitable for compiling with a native 223 /// assembler. 224 /// 225 /// \arg AP - If given, an AsmPrinter to use for printing instructions. 226 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS, 227 const MCAsmInfo &MAI, AsmPrinter *AP = 0, 228 MCCodeEmitter *CE = 0); 229 230 // FIXME: These two may end up getting rolled into a single 231 // createObjectStreamer interface, which implements the assembler backend, and 232 // is parameterized on an output object file writer. 233 234 /// createMachOStream - Create a machine code streamer which will generative 235 /// Mach-O format object files. 236 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS, 237 MCCodeEmitter *CE = 0); 238 239 /// createELFStreamer - Create a machine code streamer which will generative 240 /// ELF format object files. 241 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 242 243} // end namespace llvm 244 245#endif 246