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