MCStreamer.h revision f96db468fcf62d671cda99b68b6cfd3f2dc0b839
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 MCContext; 21 class MCValue; 22 class MCInst; 23 class MCSection; 24 class MCSymbol; 25 class raw_ostream; 26 27 /// MCStreamer - Streaming machine code generation interface. This interface 28 /// is intended to provide a programatic interface that is very similar to the 29 /// level that an assembler .s file provides. It has callbacks to emit bytes, 30 /// "emit directives", etc. The implementation of this interface retains 31 /// state to know what the current section is etc. 32 /// 33 /// There are multiple implementations of this interface: one for writing out 34 /// a .s file, and implementations that write out .o files of various formats. 35 /// 36 class MCStreamer { 37 public: 38 enum SymbolAttr { 39 Global, /// .globl 40 Hidden, /// .hidden (ELF) 41 IndirectSymbol, /// .indirect_symbol (Apple) 42 Internal, /// .internal (ELF) 43 LazyReference, /// .lazy_reference (Apple) 44 NoDeadStrip, /// .no_dead_strip (Apple) 45 PrivateExtern, /// .private_extern (Apple) 46 Protected, /// .protected (ELF) 47 Reference, /// .reference (Apple) 48 Weak, /// .weak 49 WeakDefinition, /// .weak_definition (Apple) 50 WeakReference, /// .weak_reference (Apple) 51 52 SymbolAttrFirst = Global, 53 SymbolAttrLast = WeakReference 54 }; 55 56 enum AssemblerFlag { 57 SubsectionsViaSymbols /// .subsections_via_symbols (Apple) 58 }; 59 60 private: 61 MCContext &Context; 62 63 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT 64 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT 65 66 protected: 67 MCStreamer(MCContext &Ctx); 68 69 public: 70 virtual ~MCStreamer(); 71 72 MCContext &getContext() const { return Context; } 73 74 /// @name Symbol & Section Management 75 /// @{ 76 77 /// SwitchSection - Set the current section where code is being emitted to 78 /// @param Section. 79 /// 80 /// This corresponds to assembler directives like .section, .text, etc. 81 virtual void SwitchSection(MCSection *Section) = 0; 82 83 /// EmitLabel - Emit a label for @param Symbol into the current section. 84 /// 85 /// This corresponds to an assembler statement such as: 86 /// foo: 87 /// 88 /// @param Symbol - The symbol to emit. A given symbol should only be 89 /// emitted as a label once, and symbols emitted as a label should never be 90 /// used in an assignment. 91 // 92 // FIXME: What to do about the current section? Should we get rid of the 93 // symbol section in the constructor and initialize it here? 94 virtual void EmitLabel(MCSymbol *Symbol) = 0; 95 96 /// EmitAssemblerFlag - Note in the output the specified @param Flag 97 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0; 98 99 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol. 100 /// 101 /// This corresponds to an assembler statement such as: 102 /// symbol = value 103 /// 104 /// The assignment generates no code, but has the side effect of binding the 105 /// value in the current context. For the assembly streamer, this prints the 106 /// binding into the .s file. 107 /// 108 /// @param Symbol - The symbol being assigned to. 109 /// @param Value - The value for the symbol. 110 /// @param MakeAbsolute - If true, then the symbol should be given the 111 /// absolute value of @param Value, even if @param Value would be 112 /// relocatable expression. This corresponds to the ".set" directive. 113 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, 114 bool MakeAbsolute = false) = 0; 115 116 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol. 117 // 118 // FIXME: This doesn't make much sense, could we just have attributes be on 119 // the symbol and make the printer smart enough to add the right symbols? 120 // This should work as long as the order of attributes in the file doesn't 121 // matter. 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 /// EmitLocalSymbol - Emit a local symbol of @param Value to @param Symbol. 132 /// 133 /// @param Symbol - The local symbol being created. 134 /// @param Value - The value for the symbol. 135 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) = 0; 136 137 /// EmitCommonSymbol - Emit a common or local common symbol of @param Size 138 /// with the @param Pow2Alignment if non-zero. 139 /// 140 /// @param Symbol - The common symbol to emit. 141 /// @param Size - The size of the common symbol. 142 /// @param Pow2Alignment - The alignment of the common symbol if non-zero. 143 /// @param IsLocal - If true, then the symbol is to be a local common 144 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 145 unsigned Pow2Alignment, bool IsLocal) = 0; 146 147 /// EmitZerofill - Emit a the zerofill section and possiblity a symbol, if 148 /// @param Symbol is non-NULL, for @param Size and with the @param 149 /// Pow2Alignment if non-zero. 150 /// 151 /// @param Section - The zerofill section to create and or to put the symbol 152 /// @param Symbol - The zerofill symbol to emit, if non-NULL. 153 /// @param Size - The size of the zerofill symbol. 154 /// @param Pow2Alignment - The alignment of the zerofill symbol if non-zero. 155 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = 0, 156 unsigned Size = 0,unsigned Pow2Alignment = 0) = 0; 157 158 /// AbortAssembly - Stop and don't produce output, printing @param 159 /// AbortReason if non-NULL to indicate the reason the assembly is 160 /// terminated. 161 /// 162 /// @param AbortReason - The reason assembly is terminated, if non-NULL. 163 virtual void AbortAssembly(const char *AbortReason) = 0; 164 165 /// DumpSymbolsandMacros - Dump to the specified file in @param FileName all 166 /// symbols and macros at this point in the assembly. 167 /// 168 /// @param FileName - The file to dump the symbols and macros into. 169 virtual void DumpSymbolsandMacros(const char *FileName) = 0; 170 171 /// LoadSymbolsandMacros - Load from the specified file in @param FileName 172 /// symbols and macros into the assembler at this point in the assembly. 173 /// 174 /// @param FileName - The file to load the symbols and macros from. 175 virtual void LoadSymbolsandMacros(const char *FileName) = 0; 176 177 /// @} 178 /// @name Generating Data 179 /// @{ 180 181 /// EmitBytes - Emit @param Length bytes starting at @param Data into the 182 /// output. 183 /// 184 /// This is used to implement assembler directives such as .byte, .ascii, 185 /// etc. 186 virtual void EmitBytes(const char *Data, unsigned Length) = 0; 187 188 /// EmitValue - Emit the expression @param Value into the output as a native 189 /// integer of the given @param Size bytes. 190 /// 191 /// This is used to implement assembler directives such as .word, .quad, 192 /// etc. 193 /// 194 /// @param Value - The value to emit. 195 /// @param Size - The size of the integer (in bytes) to emit. This must 196 /// match a native machine width. 197 virtual void EmitValue(const MCValue &Value, unsigned Size) = 0; 198 199 /// EmitValueToAlignment - Emit some number of copies of @param Value until 200 /// the byte alignment @param ByteAlignment is reached. 201 /// 202 /// If the number of bytes need to emit for the alignment is not a multiple 203 /// of @param ValueSize, then the contents of the emitted fill bytes is 204 /// undefined. 205 /// 206 /// This used to implement the .align assembler directive. 207 /// 208 /// @param ByteAlignment - The alignment to reach. This must be a power of 209 /// two on some targets. 210 /// @param Value - The value to use when filling bytes. 211 /// @param Size - The size of the integer (in bytes) to emit for @param 212 /// Value. This must match a native machine width. 213 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If 214 /// the alignment cannot be reached in this many bytes, no bytes are 215 /// emitted. 216 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 217 unsigned ValueSize = 1, 218 unsigned MaxBytesToEmit = 0) = 0; 219 220 /// EmitValueToOffset - Emit some number of copies of @param Value until the 221 /// byte offset @param Offset is reached. 222 /// 223 /// This is used to implement assembler directives such as .org. 224 /// 225 /// @param Offset - The offset to reach.This may be an expression, but the 226 /// expression must be associated with the current section. 227 /// @param Value - The value to use when filling bytes. 228 // 229 // FIXME: How are we going to signal failures out of this? 230 virtual void EmitValueToOffset(const MCValue &Offset, 231 unsigned char Value = 0) = 0; 232 233 /// @} 234 235 /// EmitInstruction - Emit the given @param Instruction into the current 236 /// section. 237 virtual void EmitInstruction(const MCInst &Inst) = 0; 238 239 /// Finish - Finish emission of machine code and flush any output. 240 virtual void Finish() = 0; 241 }; 242 243 /// createAsmStreamer - Create a machine code streamer which will print out 244 /// assembly for the native target, suitable for compiling with a native 245 /// assembler. 246 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS); 247 248 // FIXME: These two may end up getting rolled into a single 249 // createObjectStreamer interface, which implements the assembler backend, and 250 // is parameterized on an output object file writer. 251 252 /// createMachOStream - Create a machine code streamer which will generative 253 /// Mach-O format object files. 254 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS); 255 256 /// createELFStreamer - Create a machine code streamer which will generative 257 /// ELF format object files. 258 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS); 259 260} // end namespace llvm 261 262#endif 263